Added runtime config.

This commit is contained in:
knotteye
2019-09-23 14:27:01 -05:00
parent fcff93c533
commit 27eafbd71d
7 changed files with 172 additions and 55 deletions

View File

@@ -1,44 +1,71 @@
import * as mediaserver from "./server";
import * as ircd from "./ircd";
import * as db from "./database";
const config = require('config');
const mediaconfig: any = {
rtmp: {
port: 1935,
chunk_size: 60000,
gop_cache: true,
ping: 30,
ping_timeout: 60
},
http: {
port:8000,
allow_origin: '*',
mediaroot: './site'
},
trans: {
ffmpeg: '/usr/bin/ffmpeg',
tasks: [
{
app: 'live',
hls: 'true',
hlsFlags: '[hls_time=2:hls_list_size=3:hls_flags=delete_segments]'
}
]
}
};
/*var dbcfg: object;
var servercfg: object;
var bcryptcfg: object;
var satyrcfg: object;
var ircdcfg: object;
const dbconfig: any = {
connectionLimit: 50,
host : 'localhost',
user : 'satyr',
password : 'password',
database : 'satyr_db'
};
function boot(): void{
db.run(dbconfig);
mediaserver.boot(mediaconfig);
function init(): void{
dbcfg = config.get('database');
bcryptcfg = config.get('bcrypt');
servercfg = config.get('server');
satyrcfg = config.get('satyr');
ircdcfg = config.get('ircd');
}*/
function run(): void{
//init();
const dbcfg = config.database;
const bcryptcfg = config.bcrypt;
const satyr: object = {
privateEndpoint: config.media.privateEndpoint,
record: config.media.record,
streamKeys: config.media.streamKeys,
registration: config.satyr.registration,
webFormat: config.satyr.webFormat,
restrictedNames: config.satyr.restrictedNames
};
const nms: object = {
logType: config.server.logs,
rtmp: {
port: config.server.rtmp.port,
chunk_size: config.server.rtmp.chunk_size,
gop_cache: config.server.rtmp.gop_cache,
ping: config.server.rtmp.ping,
ping_timeout: config.server.rtmp.ping_timeout,
},
http: {
port: config.server.http.port,
mediaroot: config.server.http.directory,
allow_origin: config.server.http.allow_origin
},
trans: {
ffmpeg: config.media.ffmpeg,
tasks: [
{
app: config.media.publicEndpoint,
hls: config.transcode.hls,
hlsFlags: config.transcode.hlsFlags,
dash: config.transcode.dash,
dashFlags: config.transcode.dashFlags
}
]
},
auth: {
api: config.server.api,
api_user: config.server.api_user,
api_pass: config.server.api_pass
}
};
db.run(dbcfg, bcryptcfg);
mediaserver.boot(nms, satyr);
ircd.boot();
}
boot();
export { boot };
run();
export { run };

View File

@@ -1,9 +1,11 @@
import * as mysql from "mysql";
import * as bcrypt from "bcrypt";
var raw: any;
var cryptoconfig: object;
function run (dbconfig: any){
raw = mysql.createPool(dbconfig);
function run (db: object, bcrypt: object){
raw = mysql.createPool(db);
cryptoconfig = bcrypt;
}
function streamKeyAuth(key: string){

View File

@@ -1,14 +1,16 @@
import * as NodeMediaServer from "node-media-server";
import { mkdir } from "fs";
import * as db from "./database";
//import { transcode } from "buffer";
const isLocal = require("check-localhost");
const { exec } = require('child_process');
function boot (mediaconfig: any) {
function boot (mediaconfig: any, satyrconfig: any) {
const nms = new NodeMediaServer(mediaconfig);
nms.run();
nms.on('postPublish', (id, StreamPath, args) => {
//this is unreadable, add some comments
console.log("[NodeMediaServer] Prepublish Hook for stream:",id);
let session = nms.getSession(id);
let app: string = StreamPath.split("/")[1];
@@ -18,30 +20,30 @@ function boot (mediaconfig: any) {
session.reject();
return false;
}
if(app === "live") {
if(app === mediaconfig.trans.tasks.app) {
isLocal(session.ip).then( (local) => {
if(local) {
console.log("[NodeMediaServer] Local publish, stream:",`${id} ok.`);
}
else{
console.log("[NodeMediaServer] Non-local Publish to /live, rejecting stream:",id);
console.log("[NodeMediaServer] Non-local Publish to public endpoint, rejecting stream:",id);
session.reject();
}
});
console.log("[NodeMediaServer] Public endpoint, checking record flag.");
db.raw.query('select username,record_flag from users where username=\''+key+'\' and record_flag=true limit 1', (error, results, fields) => {
if (error) {throw error;}
if(results[0]){
if(results[0] && satyrconfig.record){
console.log('[NodeMediaServer] Initiating recording for stream:',id);
mkdir('./site/live/'+results[0].username, { recursive : true }, (err) => {
mkdir(mediaconfig.http.mediaroot+'/'+mediaconfig.trans.tasks.app+'/'+results[0].username, { recursive : true }, (err) => {
if (err) throw err;
let subprocess = exec('ffmpeg -i rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+mediaconfig.trans.tasks.app+'/'+results[0].username+' -vcodec copy -acodec copy '+mediaconfig.http.mediaroot+'/'+mediaconfig.trans.tasks.app+'/'+results[0].username+'/$(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
});
let subprocess = exec('ffmpeg -i rtmp://127.0.0.1/live/'+results[0].username+' -vcodec copy -acodec copy ./site/live/'+results[0].username+'/$(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
}
else {
console.log('[NodeMediaServer] Skipping recording for stream:',id);
@@ -49,8 +51,8 @@ function boot (mediaconfig: any) {
});
return true;
}
if(app !== "stream"){
//app isn't 'live' if we've reached this point
if(app !== satyrconfig.privateEndpoint){
//app isn't at public endpoint if we've reached this point
console.log("[NodeMediaServer] Wrong endpoint, rejecting stream:",id);
session.reject();
return false;
@@ -58,7 +60,7 @@ function boot (mediaconfig: any) {
db.raw.query('select username from users where stream_key=\''+key+'\' limit 1', (error, results, fields) => {
if (error) {throw error;}
if(results[0]){
exec('ffmpeg -analyzeduration 0 -i rtmp://localhost/stream/'+key+' -vcodec copy -acodec copy -crf 18 -f flv rtmp://localhost:1935/live/'+results[0].username);
exec('ffmpeg -analyzeduration 0 -i rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+satyrconfig.privateEndpoint+'/'+key+' -vcodec copy -acodec copy -crf 18 -f flv rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+mediaconfig.trans.tasks.app+'/'+results[0].username);
console.log('[NodeMediaServer] Stream key okay for stream:',id);
}
else{
@@ -76,13 +78,13 @@ function boot (mediaconfig: any) {
session.reject();
return false;
}
if(app === "stream") {
if(app === satyrconfig.privateEndpoint) {
isLocal(session.ip).then( (local) => {
if(local) {
console.log("[NodeMediaServer] Local play, client:",`${id} ok.`);
}
else{
console.log("[NodeMediaServer] Non-local Play from /stream, rejecting client:",id);
console.log("[NodeMediaServer] Non-local Play from private endpoint, rejecting client:",id);
session.reject();
}
});