2019-09-20 11:09:15 -05:00
|
|
|
import * as NodeMediaServer from "node-media-server";
|
2019-11-27 22:18:55 -06:00
|
|
|
import * as dirty from "dirty";
|
2019-11-16 11:34:16 -06:00
|
|
|
import { mkdir, fstat, access } from "fs";
|
2019-11-16 14:42:26 -06:00
|
|
|
import * as strf from "strftime";
|
2019-09-22 16:33:18 -05:00
|
|
|
import * as db from "./database";
|
2019-12-21 08:59:35 -06:00
|
|
|
import {config} from "./config";
|
2019-11-16 11:34:16 -06:00
|
|
|
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
const { exec, execFile } = require('child_process');
|
2019-09-11 19:28:17 -05:00
|
|
|
|
2019-11-27 22:18:55 -06:00
|
|
|
const keystore = dirty();
|
|
|
|
|
2019-12-21 08:59:35 -06:00
|
|
|
function init () {
|
|
|
|
const nms = new NodeMediaServer({logType: 0,rtmp: config['rtmp']});
|
2019-09-20 11:09:15 -05:00
|
|
|
nms.run();
|
2019-09-11 19:28:17 -05:00
|
|
|
|
2019-09-22 16:33:18 -05:00
|
|
|
nms.on('postPublish', (id, StreamPath, args) => {
|
2019-10-05 14:34:57 -05:00
|
|
|
console.log("[NodeMediaServer] Publish Hook for stream:",id);
|
2019-09-20 11:09:15 -05:00
|
|
|
let session = nms.getSession(id);
|
2019-09-22 16:33:18 -05:00
|
|
|
let app: string = StreamPath.split("/")[1];
|
|
|
|
let key: string = StreamPath.split("/")[2];
|
2019-09-23 15:59:07 -05:00
|
|
|
//disallow urls not formatted exactly right
|
2019-11-27 22:18:55 -06:00
|
|
|
if (StreamPath.split("/").length !== 3 || key.includes(' ')){
|
2019-09-22 16:33:18 -05:00
|
|
|
console.log("[NodeMediaServer] Malformed URL, closing connection for stream:",id);
|
2019-09-20 11:09:15 -05:00
|
|
|
session.reject();
|
|
|
|
return false;
|
|
|
|
}
|
2019-12-21 08:59:35 -06:00
|
|
|
if(app !== config['media']['privateEndpoint']){
|
2019-09-23 14:27:01 -05:00
|
|
|
//app isn't at public endpoint if we've reached this point
|
2019-09-22 16:33:18 -05:00
|
|
|
console.log("[NodeMediaServer] Wrong endpoint, rejecting stream:",id);
|
2019-09-20 11:09:15 -05:00
|
|
|
session.reject();
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-23 15:59:07 -05:00
|
|
|
//if the url is formatted correctly and the user is streaming to the correct private endpoint
|
|
|
|
//grab the username from the database and redirect the stream there if the key is valid
|
|
|
|
//otherwise kill the session
|
2019-11-27 22:18:55 -06:00
|
|
|
db.query('select username,record_flag from users where stream_key='+db.raw.escape(key)+' limit 1').then(async (results) => {
|
2019-09-22 16:33:18 -05:00
|
|
|
if(results[0]){
|
2019-12-05 18:27:29 -06:00
|
|
|
//transcode to mpd after making sure directory exists
|
2019-11-27 22:18:55 -06:00
|
|
|
keystore[results[0].username] = key;
|
2019-12-21 08:59:35 -06:00
|
|
|
mkdir(config['http']['directory']+'/'+config['media']['publicEndpoint']+'/'+results[0].username, { recursive : true }, ()=>{;});
|
2019-11-30 14:15:31 -06:00
|
|
|
while(true){
|
|
|
|
if(session.audioCodec !== 0 && session.videoCodec !== 0){
|
2019-12-21 08:59:35 -06:00
|
|
|
transCommand(results[0].username, key).then((r) => {
|
2020-08-08 23:08:09 -05:00
|
|
|
execFile(config['media']['ffmpeg'], r, {maxBuffer: Infinity}, (err, stdout, stderr) => {
|
2020-08-08 23:37:10 -05:00
|
|
|
/*console.log(err);
|
2020-08-08 23:08:09 -05:00
|
|
|
console.log(stdout);
|
2020-08-08 23:37:10 -05:00
|
|
|
console.log(stderr);*/
|
2020-08-08 23:08:09 -05:00
|
|
|
});
|
2019-12-05 18:27:29 -06:00
|
|
|
});
|
2019-11-30 14:15:31 -06:00
|
|
|
break;
|
2019-11-24 20:17:15 -06:00
|
|
|
}
|
2019-11-30 14:15:31 -06:00
|
|
|
await sleep(300);
|
|
|
|
}
|
2019-12-21 08:59:35 -06:00
|
|
|
if(results[0].record_flag && config['media']['record']){
|
2019-11-27 22:18:55 -06:00
|
|
|
console.log('[NodeMediaServer] Initiating recording for stream:',id);
|
2019-12-21 08:59:35 -06:00
|
|
|
mkdir(config['http']['directory']+'/'+config['media']['publicEndpoint']+'/'+results[0].username, { recursive : true }, (err) => {
|
2019-11-27 22:18:55 -06:00
|
|
|
if (err) throw err;
|
2019-12-21 08:59:35 -06:00
|
|
|
execFile(config['media']['ffmpeg'], ['-loglevel', 'fatal', '-i', 'rtmp://127.0.0.1:'+config['rtmp']['port']+'/'+config['media']['privateEndpoint']+'/'+key, '-vcodec', 'copy', '-acodec', 'copy', config['http']['directory']+'/'+config['media']['publicEndpoint']+'/'+results[0].username+'/'+strf('%d%b%Y-%H%M')+'.mp4'], {
|
2019-11-27 22:18:55 -06:00
|
|
|
detached : true,
|
|
|
|
stdio : 'inherit',
|
|
|
|
maxBuffer: Infinity
|
|
|
|
}).unref();
|
|
|
|
//spawn an ffmpeg process to record the stream, then detach it completely
|
|
|
|
//ffmpeg can then (probably) finalize the recording if satyr crashes mid-stream
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log('[NodeMediaServer] Skipping recording for stream:',id);
|
|
|
|
}
|
|
|
|
db.query('update user_meta set live=true where username=\''+results[0].username+'\' limit 1');
|
2020-10-12 11:14:59 -05:00
|
|
|
db.query('SELECT twitch_key,enabled from twitch_mirror where username='+db.raw.escape(results[0].username)+' limit 1').then(async (tm) => {
|
|
|
|
if(!tm[0]['enabled'] || !config['twitch_mirror']['enabled'] || !config['twitch_mirror']['ingest']) return;
|
|
|
|
else
|
|
|
|
execFile(config['media']['ffmpeg'], ['-loglevel', 'fatal', '-i', 'rtmp://127.0.0.1:'+config['rtmp']['port']+'/'+config['media']['privateEndpoint']+'/'+key, '-vcodec', 'libx264', '-acodec', 'libaac', '-f', 'flv', config['twitch_mirror']['ingest']+tm[0]['twitch_key']], {
|
|
|
|
detached: true,
|
|
|
|
stdio : 'inherit',
|
|
|
|
maxBuffer: Infinity
|
|
|
|
}).unref();
|
|
|
|
});
|
2019-11-27 22:18:55 -06:00
|
|
|
console.log('[NodeMediaServer] Stream key ok for stream:',id);
|
2019-09-22 16:33:18 -05:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
console.log('[NodeMediaServer] Invalid stream key for stream:',id);
|
|
|
|
session.reject();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-10-05 14:34:57 -05:00
|
|
|
nms.on('donePublish', (id, StreamPath, args) => {
|
|
|
|
let app: string = StreamPath.split("/")[1];
|
|
|
|
let key: string = StreamPath.split("/")[2];
|
2019-12-21 08:59:35 -06:00
|
|
|
if(app === config['media']['privateEndpoint']) {
|
2019-11-27 22:18:55 -06:00
|
|
|
db.query('update user_meta,users set user_meta.live=false where users.stream_key='+db.raw.escape(key));
|
|
|
|
db.query('select username from users where stream_key='+db.raw.escape(key)+' limit 1').then(async (results) => {
|
2019-12-03 19:51:14 -06:00
|
|
|
if(results[0]) keystore.rm(results[0].username);
|
2019-11-27 22:18:55 -06:00
|
|
|
});
|
|
|
|
|
2019-10-05 14:34:57 -05:00
|
|
|
}
|
|
|
|
});
|
2019-09-22 16:33:18 -05:00
|
|
|
nms.on('prePlay', (id, StreamPath, args) => {
|
|
|
|
let session = nms.getSession(id);
|
|
|
|
let app: string = StreamPath.split("/")[1];
|
|
|
|
let key: string = StreamPath.split("/")[2];
|
2019-09-23 15:59:07 -05:00
|
|
|
//correctly formatted urls again
|
|
|
|
if (StreamPath.split("/").length !== 3){
|
2019-09-22 16:33:18 -05:00
|
|
|
console.log("[NodeMediaServer] Malformed URL, closing connection for stream:",id);
|
2019-09-20 11:09:15 -05:00
|
|
|
session.reject();
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-30 14:15:31 -06:00
|
|
|
//localhost can play from whatever endpoint
|
|
|
|
//other clients must use private endpoint
|
2019-12-21 08:59:35 -06:00
|
|
|
if(app !== config['media']['publicEndpoint'] && !session.isLocal) {
|
2019-11-24 20:17:15 -06:00
|
|
|
console.log("[NodeMediaServer] Non-local Play from private endpoint, rejecting client:",id);
|
|
|
|
session.reject();
|
2019-11-27 22:18:55 -06:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-30 14:15:31 -06:00
|
|
|
//rewrite playpath to private endpoint serverside
|
|
|
|
//(hopefully)
|
2019-12-21 08:59:35 -06:00
|
|
|
if(app === config['media']['publicEndpoint']) {
|
2019-11-27 22:18:55 -06:00
|
|
|
if(keystore[key]){
|
2019-12-21 08:59:35 -06:00
|
|
|
session.playStreamPath = '/'+config['media']['privateEndpoint']+'/'+keystore[key];
|
2019-11-27 22:18:55 -06:00
|
|
|
return true;
|
|
|
|
}
|
2019-09-20 11:09:15 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-12-05 18:27:29 -06:00
|
|
|
|
2019-12-21 08:59:35 -06:00
|
|
|
async function transCommand(user: string, key: string): Promise<string[]>{
|
2020-08-08 23:08:09 -05:00
|
|
|
let args: string[] = ['-loglevel', 'fatal', '-y'];
|
|
|
|
if(config['transcode']['inputflags'] !== null && config['transcode']['inputflags'] !== "") args = args.concat(config['transcode']['inputflags'].split(" "));
|
|
|
|
args = args.concat(['-i', 'rtmp://127.0.0.1:'+config['rtmp']['port']+'/'+config['media']['privateEndpoint']+'/'+key, '-movflags', '+faststart']);
|
2019-12-05 18:27:29 -06:00
|
|
|
if(config['transcode']['adaptive']===true && config['transcode']['variants'] > 1) {
|
|
|
|
for(let i=0;i<config['transcode']['variants'];i++){
|
|
|
|
args = args.concat(['-map', '0:2']);
|
|
|
|
}
|
2020-06-30 01:26:42 -05:00
|
|
|
args = args.concat(['-map', '0:1', '-c:a', 'aac', '-c:v:0', 'libx264']);
|
2019-12-05 18:27:29 -06:00
|
|
|
for(let i=1;i<config['transcode']['variants'];i++){
|
|
|
|
args = args.concat(['-c:v:'+i, 'libx264',]);
|
|
|
|
}
|
|
|
|
for(let i=1;i<config['transcode']['variants'];i++){
|
|
|
|
let crf: number = Math.floor(18 + (i * 8)) > 51 ? 51 : Math.floor(18 + (i * 7));
|
|
|
|
args = args.concat(['-crf:'+i, ''+crf]);
|
|
|
|
}
|
|
|
|
for(let i=1;i<config['transcode']['variants'];i++){
|
|
|
|
let bv: number = Math.floor((5000 / config['transcode']['variants']) * (config['transcode']['variants'] - i));
|
|
|
|
args = args.concat(['-b:v:'+i, ''+bv]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-06-30 01:26:42 -05:00
|
|
|
args = args.concat(['-c:a', 'aac', '-c:v', 'libx264']);
|
2019-12-05 18:27:29 -06:00
|
|
|
}
|
2020-08-08 23:08:09 -05:00
|
|
|
args = args.concat(['-preset', 'veryfast', '-tune', 'zerolatency']);
|
2020-06-27 12:11:42 -05:00
|
|
|
//if(config['transcode']['format'] === 'dash')
|
2020-08-08 23:08:09 -05:00
|
|
|
args = args.concat(['-remove_at_exit', '1', '-seg_duration', '1', '-window_size', '30']);
|
|
|
|
if(config['transcode']['outputflags'] !== null && config['transcode']['outputflags'] !== "") args = args.concat(config['transcode']['outputflags'].split(" "));
|
|
|
|
args = args.concat(['-f', 'dash', config['http']['directory']+'/'+config['media']['publicEndpoint']+'/'+user+'/index.mpd']);
|
2020-06-27 12:11:42 -05:00
|
|
|
//else if(config['transcode']['format'] === 'hls')
|
|
|
|
//args = args.concat(['-remove_at_exit', '1', '-hls_time', '1', '-hls_list_size', '30', '-f', 'hls', config['http']['directory']+'/'+config['media']['publicEndpoint']+'/'+user+'/index.m3u8']);
|
2019-12-05 18:27:29 -06:00
|
|
|
return args;
|
|
|
|
}
|
2020-06-30 01:26:42 -05:00
|
|
|
export { init };
|