2019-09-20 11:09:15 -05:00
|
|
|
import * as NodeMediaServer from "node-media-server";
|
|
|
|
import { mkdir } from "fs";
|
2019-09-14 21:22:07 -05:00
|
|
|
const { exec } = require('child_process');
|
2019-09-11 19:28:17 -05:00
|
|
|
|
|
|
|
//initialize configs, eventually grab from runtime config file
|
2019-09-20 11:09:15 -05:00
|
|
|
function initConfig(): void{
|
|
|
|
;
|
|
|
|
}
|
2019-09-11 19:28:17 -05:00
|
|
|
|
2019-09-20 11:09:15 -05:00
|
|
|
/*function streamAuth(path: string){
|
2019-09-11 19:28:17 -05:00
|
|
|
if (path.split("/").length > 3){
|
|
|
|
console.log("[NodeMediaServer] Malformed URL, closing connection.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let app: string = path.split("/")[1];
|
|
|
|
let key: string = path.split("/")[2];
|
|
|
|
console.log("[NodeMediaServer] Authenticating stream with credentials: ",`app=${app} key=${key}`);
|
|
|
|
if (app !== "stream"){
|
|
|
|
console.log("[NodeMediaServer] Invalid app name, closing connection.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
console.log("[NodeMediaServer] App name ok.");
|
|
|
|
if (key !== "temp"){
|
|
|
|
console.log("[NodeMediaServer] Invalid stream key, closing connection.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
console.log("[NodeMediaServer] Stream key ok.");
|
|
|
|
return true;
|
2019-09-20 11:09:15 -05:00
|
|
|
}*/
|
2019-09-11 19:28:17 -05:00
|
|
|
|
2019-09-20 11:09:15 -05:00
|
|
|
function boot (config: any){
|
|
|
|
const nms = new NodeMediaServer(config);
|
2019-09-11 19:28:17 -05:00
|
|
|
|
2019-09-20 11:09:15 -05:00
|
|
|
nms.run();
|
2019-09-11 19:28:17 -05:00
|
|
|
|
2019-09-20 11:09:15 -05:00
|
|
|
nms.on('prePublish', (id, StreamPath, args) => {
|
|
|
|
console.log("[NodeMediaServer] Prepublish Hook for stream id=",id);
|
|
|
|
let session = nms.getSession(id);
|
|
|
|
if (StreamPath.split("/").length > 3){
|
|
|
|
console.log("[NodeMediaServer] Malformed URL, closing connection.");
|
|
|
|
session.reject();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let app: string = StreamPath.split("/")[1];
|
|
|
|
let key: string = StreamPath.split("/")[2];
|
|
|
|
console.log("[NodeMediaServer] Authenticating stream with credentials: ",`app=${app} key=${key}`);
|
|
|
|
if (app !== "stream"){
|
|
|
|
console.log("[NodeMediaServer] Invalid app name, closing connection.");
|
|
|
|
session.reject();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
console.log("[NodeMediaServer] App name ok.");
|
|
|
|
//TODO: Hook up to DB and redirect from query
|
|
|
|
if (key !== "temp"){
|
|
|
|
console.log("[NodeMediaServer] Invalid stream key, closing connection.");
|
|
|
|
session.reject();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
console.log("[NodeMediaServer] Stream key ok.");
|
|
|
|
session.publishStreamPath = "/live/amy";
|
|
|
|
});
|
2019-09-14 21:22:07 -05:00
|
|
|
|
2019-09-20 11:09:15 -05:00
|
|
|
nms.on('postPublish', (id, StreamPath, args) => {
|
|
|
|
console.log('[NodeMediaServer] Checking record flag for ', `id=${id} StreamPath=${StreamPath}`);
|
|
|
|
//Hook up to postgres DB.
|
|
|
|
if(true){
|
|
|
|
console.log('[NodeMediaServer] Initiating recording for ', `id=${id} StreamPath=${StreamPath}`);
|
|
|
|
mkdir('./media'+StreamPath, { recursive : true }, (err) => {
|
|
|
|
if (err) throw err;
|
|
|
|
});
|
|
|
|
let subprocess = exec('ffmpeg -i rtmp://127.0.0.1'+StreamPath+' -vcodec copy -acodec copy ./media'+StreamPath+'/$(date +%d%b%Y-%H%M).mp4',{
|
|
|
|
detached : true,
|
|
|
|
stdio : 'inherit'
|
|
|
|
});
|
|
|
|
subprocess.unref();
|
|
|
|
//spawn an ffmpeg process to record the stream, then detach it completely
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
console.log('[NodeMediaServer] Skipping recording for ', `id=${id} StreamPath=${StreamPath}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
export { boot };
|