forked from StreamMachine/StreamMachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
302 lines (278 loc) · 8.84 KB
/
index.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
var Alerts, HTTP, IO, Server, Slave, SocketSource, Stream, URL, debug, tz, _,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
_ = require("underscore");
Stream = require("./stream");
Server = require("./server");
Alerts = require("../alerts");
IO = require("./slave_io");
SocketSource = require("./socket_source");
URL = require("url");
HTTP = require("http");
tz = require('timezone');
debug = require("debug")("sm:slave:slave");
module.exports = Slave = (function(_super) {
__extends(Slave, _super);
Slave.prototype.Outputs = {
pumper: require("../outputs/pumper"),
shoutcast: require("../outputs/shoutcast"),
raw: require("../outputs/raw_audio"),
live_streaming: require("../outputs/live_streaming")
};
function Slave(options, _worker) {
var _ref;
this.options = options;
this._worker = _worker;
this._configured = false;
debug("Init for Slave");
this.master = null;
this.streams = {};
this.stream_groups = {};
this.root_route = null;
this.connected = false;
this._retrying = null;
this._shuttingDown = false;
this._totalConnections = 0;
this._totalKBytesSent = 0;
this.log = this.options.logger;
this.alerts = new Alerts({
logger: this.log.child({
module: "alerts"
})
});
if ((_ref = this.options.slave) != null ? _ref.master : void 0) {
debug("Connecting IO to master");
this.io = new IO(this, this.log.child({
module: "slave_io"
}), this.options.slave);
this.io.on("connected", (function(_this) {
return function() {
debug("IO is connected");
_this.alerts.update("slave_disconnected", _this.io.id, false);
return _this.log.proxyToMaster(_this.io);
};
})(this));
this.io.on("disconnected", (function(_this) {
return function() {
debug("IO is disconnected");
_this.alerts.update("slave_disconnected", _this.io.id, true);
return _this.log.proxyToMaster();
};
})(this));
}
this.once("streams", (function(_this) {
return function() {
debug("Streams event received");
return _this._configured = true;
};
})(this));
this.server = new Server({
core: this,
logger: this.log.child({
subcomponent: "server"
}),
config: this.options
});
}
Slave.prototype.once_configured = function(cb) {
if (this._configured) {
return cb();
} else {
return this.once("streams", (function(_this) {
return function() {
return cb();
};
})(this));
}
};
Slave.prototype.once_rewinds_loaded = function(cb) {
return this.once_configured((function(_this) {
return function() {
var aFunc, k, obj, _ref, _results;
_this.log.debug("Looking for sources to load in " + (Object.keys(_this.streams).length) + " streams.");
aFunc = _.after(Object.keys(_this.streams).length, function() {
_this.log.debug("All sources are loaded.");
return cb();
});
_ref = _this.streams;
_results = [];
for (k in _ref) {
obj = _ref[k];
_results.push(obj._once_source_loaded(aFunc));
}
return _results;
};
})(this));
};
Slave.prototype._shutdown = function(cb) {
if (!this._worker) {
cb("Don't have _worker to trigger shutdown on.");
return false;
}
if (this._shuttingDown) {
cb("Shutdown already in progress.");
return false;
}
this._shuttingDown = true;
this.server.close();
return this._worker.shutdown(cb);
};
Slave.prototype.configureStreams = function(options) {
var g, k, key, obj, opts, sg, source, stream, _base, _ref;
debug("In configureStreams");
this.log.debug("In slave configureStreams with ", {
options: options
});
_ref = this.streams;
for (k in _ref) {
obj = _ref[k];
if (!(options != null ? options[k] : void 0)) {
debug("configureStreams: Disconnecting stream " + k);
this.log.info("configureStreams: Calling disconnect on " + k);
obj.disconnect();
delete this.streams[k];
}
}
debug("configureStreams: New options start");
for (key in options) {
opts = options[key];
debug("configureStreams: Configuring " + key);
if (this.streams[key]) {
this.log.debug("Passing updated config to stream: " + key, {
opts: opts
});
this.streams[key].configure(opts);
} else {
this.log.debug("Starting up stream: " + key, {
opts: opts
});
if (this.options.hls) {
opts.hls = true;
}
opts.tz = tz(require("timezone/zones"))(this.options.timezone || "UTC");
stream = this.streams[key] = new Stream(this, key, this.log.child({
stream: key
}), opts);
if (this.io) {
source = this.socketSource(stream);
stream.useSource(source);
}
}
if (g = this.streams[key].opts.group) {
sg = ((_base = this.stream_groups)[g] || (_base[g] = new Stream.StreamGroup(g, this.log.child({
stream_group: g
}))));
sg.addStream(this.streams[key]);
}
if (opts.root_route) {
this.root_route = key;
}
}
debug("Done with configureStreams");
return this.emit("streams", this.streams);
};
Slave.prototype._streamStatus = function(cb) {
var key, s, status, totalConnections, totalKBytes, _ref;
status = {};
totalKBytes = 0;
totalConnections = 0;
_ref = this.streams;
for (key in _ref) {
s = _ref[key];
status[key] = s.status();
totalKBytes += status[key].kbytes_sent;
totalConnections += status[key].connections;
}
return cb(null, _.extend(status, {
_stats: {
kbytes_sent: totalKBytes,
connections: totalConnections
}
}));
};
Slave.prototype.socketSource = function(stream) {
return new SocketSource(this, stream);
};
Slave.prototype.ejectListeners = function(lFunc, cb) {
var id, k, obj, s, sFunc, _ref, _ref1;
this.log.info("Preparing to eject listeners from slave.");
this._enqueued = [];
_ref = this.streams;
for (k in _ref) {
s = _ref[k];
this.log.info("Preparing " + (Object.keys(s._lmeta).length) + " listeners for " + s.key);
_ref1 = s._lmeta;
for (id in _ref1) {
obj = _ref1[id];
this._enqueued.push([s, obj]);
}
}
if (this._enqueued.length === 0) {
return typeof cb === "function" ? cb() : void 0;
}
sFunc = (function(_this) {
return function() {
var d, l, sl, stream;
sl = _this._enqueued.shift();
if (!sl) {
_this.log.info("All listeners have been ejected.");
return cb(null);
}
stream = sl[0], l = sl[1];
d = require("domain").create();
d.on("error", function(err) {
console.error("Handoff error: " + err);
_this.log.error("Eject listener for " + l.id + " hit error: " + err);
d.exit();
return sFunc();
});
return d.run(function() {
return l.obj.prepForHandoff(function(skipHandoff) {
var lopts, socket;
if (skipHandoff == null) {
skipHandoff = false;
}
if (skipHandoff) {
return sFunc();
}
socket = l.obj.socket;
lopts = {
key: [stream.key, l.id].join("::"),
stream: stream.key,
id: l.id,
startTime: l.startTime,
client: l.obj.client
};
if (socket && !socket.destroyed) {
return lFunc(lopts, socket, function(err) {
if (err) {
_this.log.error("Failed to send listener " + lopts.id + ": " + err);
}
return sFunc();
});
} else {
_this.log.info("Listener " + lopts.id + " perished in the queue. Moving on.");
return sFunc();
}
});
});
};
})(this);
return sFunc();
};
Slave.prototype.landListener = function(obj, socket, cb) {
var output;
if (socket && !socket.destroyed) {
output = new this.Outputs[obj.client.output](this.streams[obj.stream], {
socket: socket,
client: obj.client,
startTime: new Date(obj.startTime)
});
return cb(null);
} else {
return cb("Listener disconnected in-flight");
}
};
return Slave;
})(require("events").EventEmitter);
//# sourceMappingURL=index.js.map