-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcaster.js
231 lines (177 loc) · 5.26 KB
/
broadcaster.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// ================ DEPENDENCIES ================================
// NPM Dependencies
var spawn = require('child_process').spawn;
var EventEmitter = require('events').EventEmitter;
// Project modules
var presetsManager = require('./presets/presets-manager');
var logger = require('./logging/logger');
var secret = require('./secret');
// The stream process objects will be stored here
var broadcastStream = null;
var processes = [];
// emits the broadcast started event with the stream
var ev = new EventEmitter();
// ==============================================================
// Constants
var BROADCAST_STARTED_MSG = '============ Broadcast Started ============';
var BROADCAST_ENDED_MSG = '============ Broadcast Ended ============';
var BROADCAST_ABORTED_MSG = '============ Broadcast Aborted ============';
var CONFIGURATION_MSG = '============ Configuration ============ \n';
// Auto start the broadcast with the last configuration used
if (process.argv[2] === 'auto') {
broadcast();
}
// To ensure clean exit send the 'SIGTERM' code whe terminating node externally
process.on('SIGTERM', abort);
process.on('SIGINT', abort);
/**
* Fires up a video stream from the shell with the last set configutation
*/
function broadcast() {
var commands = prepareForBroadcast();
startCommands(commands);
logStreamData(broadcastStream);
ev.emit('broadcast-started', broadcastStream.stdout);
logger.log(BROADCAST_STARTED_MSG, 'success');
}
function stopPreviousBroadcast() {
var prevProc = null;
// Unpipe Streams
processes.forEach(function(prc) {
if (prevProc) {
prevProc.stdout.unpipe(prc.stdin);
}
prevProc = prc;
});
// Terminate Processes
processes.forEach(function(prc) {
prc.kill();
});
if (broadcastStream) {
logger.log(BROADCAST_ENDED_MSG, 'danger');
ev.emit('broadcast-ended');
}
processes = [];
broadcastStream = null;
}
function prepareForBroadcast() {
var broadcastConfig = presetsManager.getActivePreset();
var data = broadcastConfig.command.split('|');
var commands = [];
data.forEach(function(element) {
var args = element.split(' ').cleanPV('');
commands.push({
command: args[0],
params: args.slice(1)
});
});
if (broadcastConfig.output) {
setOutput(broadcastConfig.output, commands[commands.length - 1]);
}
var message =
'{{configMsg}} Preset: {{preset}} \n Command: {{command}} \n {{configMsg}}'
.formatPV({
configMsg: CONFIGURATION_MSG,
preset: broadcastConfig.name,
command: broadcastConfig.command
});
logger.log(message, 'warning');
stopPreviousBroadcast();
return commands;
}
function setOutput(output, command) {
if (output === 'test') {
output = 'recordings/test-{{now}}'
.formatPV({
now: (new Date()).getTime()
});
} else if (output === 'default') {
output = secret.output;
}
command.params.push(output);
}
function isRunning() {
if (broadcastStream || processes.length) {
return true;
}
return false;
}
function startCommands(commands) {
commands.forEach(function(cmd) {
logger.log(
'Running command: "{{command}}" with args: "{{params}}"'
.formatPV(cmd), 'warning');
var child = spawn(cmd.command, cmd.params);
processes.push(child);
child.on('error', function(err) {
logger.log('Child Process Failed: \n {{err}}'.formatPV({
err: err.toString()
}), 'danger');
});
});
var prevProc = null;
processes.forEach(function(prc) {
if (prevProc) {
prevProc.stdout.pipe(prc.stdin);
prevProc.stdout.on('error', function(err) {
logger.log('Child stdout Error: {{err}}'.formatPV({
err: err.toString()
}), 'danger');
});
}
prc.stdin.on('error', function(err) {
logger.log('Child stdin Error: {{err}}'.formatPV({
err: err.toString()
}), 'error');
});
prevProc = prc;
});
broadcastStream = processes[processes.length - 1];
logger.log('Spawned Processes: ' + processes.length, 'warning');
}
function abort() {
logger.log('Exiting nodejs', 'danger');
processes.forEach(function(prc) {
prc.kill();
});
process.exit();
}
// ================ EXPORTS =====================================
module.exports = {
broadcast: broadcast,
stop: stopPreviousBroadcast,
isRunning: isRunning,
event: ev
};
// ==============================================================
// ================ HELPERS =====================================
function logStreamData(childProcess) {
// childProcess.on('error', function(err) {
// logger.log(err.toString());
// stopPreviousBroadcast();
// });
// ffmpeg or avconv outputs stream information to stderr
// Log the general information to the main log
// then logging is handled by another log monitor (for bitrate, fps and etc.)
var logToGeneral = function(data) {
if (data.indexOf('frame') === 0) {
logger.log('Swiching logging to broadcast mode', 'warning');
childProcess.stderr.removeListener('data', logToGeneral);
childProcess.stderr.on('data', function(data) {
logger.broadcastLog(data);
});
return;
}
logger.log(data);
};
childProcess.stderr.setEncoding('utf8');
childProcess.stderr.on('data', logToGeneral);
// Todo: parameters check
childProcess.once('exit', function(exitCode, signal) {
logger.log('...broadcast received exit call', 'warning');
if (isRunning) {
stopPreviousBroadcast();
}
});
}
// ==============================================================