107 lines
2.7 KiB
JavaScript
107 lines
2.7 KiB
JavaScript
const express = require('express');
|
|
const http = require('http');
|
|
const child_process = require('child_process');
|
|
const { URL } = require('url');
|
|
const io = require('socket.io');
|
|
|
|
|
|
const app = express();
|
|
const server = http.createServer(app).listen(8443, () => {
|
|
console.log('Listening...');
|
|
});
|
|
|
|
// IceCast settings
|
|
const iceServerDomain = 'kurento.fishrungames.com';
|
|
const iceServerPort = '8000';
|
|
|
|
const wss = io(server);
|
|
|
|
/*
|
|
function ffmpegArgs(iceUrl) {
|
|
args = [
|
|
// FFmpeg will read input video from STDIN.
|
|
'-i', '-',
|
|
'-f', 'webm',
|
|
'-cluster_size_limit', '2M', '-cluster_time_limit', '5100', '-content_type', 'video/webm',
|
|
// We should be getting video and audio from the browser using a format that icecast
|
|
// can handle so we can just copy to save CPU cycles on the server.
|
|
'-vcodec', 'copy', '-acodec', 'copy',
|
|
iceUrl + '.webm'
|
|
];
|
|
return args;
|
|
}
|
|
*/
|
|
|
|
function ffmpegArgs(iceUrl) {
|
|
args = [
|
|
// FFmpeg will read input video from STDIN.
|
|
'-i', '-',
|
|
'-f', 'webm',
|
|
'-cluster_size_limit', '2M', '-cluster_time_limit', '5100', '-content_type', 'video/webm',
|
|
// We should be getting video and audio from the browser using a format that icecast
|
|
// can handle so we can just copy to save CPU cycles on the server.
|
|
'-vcodec', 'copy', '-acodec', 'copy',
|
|
iceUrl + '.webm'
|
|
];
|
|
return args;
|
|
}
|
|
|
|
|
|
wss.on('connection', (ws, req) => {
|
|
|
|
console.log('connected ' + ws.id );
|
|
|
|
ws.on('error', (e) => {
|
|
console.log('websocket error' + e);
|
|
});
|
|
|
|
// Creds for IceCast
|
|
const iceUser = 'source';
|
|
const icePassword = 'frg$$frg';
|
|
const iceMountPoint = 'qq';
|
|
|
|
const iceUrl = 'icecast://' + iceUser + ':' + icePassword + '@' +
|
|
iceServerDomain + ':' + iceServerPort + '/' + iceMountPoint;
|
|
|
|
const ffmpeg = child_process.spawn('ffmpeg', ffmpegArgs(iceUrl));
|
|
|
|
|
|
// If ffmpeg is crashing
|
|
ffmpeg.on('close', (code, signal) => {
|
|
console.log('FFmpeg child process closed, code ' + code + ', signal ' + signal);
|
|
ws.disconnect();
|
|
console.log('Disconnected ' + ws.id + 'because ffmpeg crashing');
|
|
});
|
|
|
|
|
|
ffmpeg.stdin.on('error', (e) => {
|
|
console.log('FFmpeg STDIN Error: ' + e);
|
|
});
|
|
|
|
ffmpeg.stderr.on('data', (data) => {
|
|
//console.log('ffmpeg stderr');
|
|
//console.log(data.toString());
|
|
});
|
|
|
|
// When new blob
|
|
ws.on('blob', (msg) => {
|
|
//console.log('blob')
|
|
ffmpeg.stdin.write(msg);
|
|
});
|
|
|
|
// When client leave
|
|
ws.on('close', (e) => {
|
|
console.log('Closing connection ' + ws.id);
|
|
ffmpeg.stdin.end();
|
|
ffmpeg.kill();
|
|
});
|
|
|
|
// If user disconnect or somethink
|
|
ws.on('disconnect', (e) => {
|
|
console.log('Closing connection ' + ws.id);
|
|
ffmpeg.stdin.end();
|
|
ffmpeg.kill();
|
|
});
|
|
|
|
});
|