-
Notifications
You must be signed in to change notification settings - Fork 2
/
StreamingMediaServer.js
524 lines (461 loc) · 15.1 KB
/
StreamingMediaServer.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
const WebSocket = require("ws");
const net = require("net");
const fs = require("fs");
const EventEmitter = require("events");
const _ = require("lodash");
/** Handles connections to the media sink */
class StreamingMediaServer extends EventEmitter {
constructor({ streamingPath, mediaSinkPath, infoSinkPath }) {
super();
this.paths = {
stream: streamingPath,
media: mediaSinkPath,
info: infoSinkPath
};
this.clients = new Map();
this.clientServer = null;
this.mediaServer = null;
this.infoServer = null;
this.sinkConnection = {};
this.closed = {};
}
start() {
if (this.mediaServer) {
throw Error("Already started");
}
this.clientServer = this.createClientServer();
this.mediaServer = this.createMediaServer();
this.infoServer = this.createInfoServer();
[this.paths.media, this.paths.info].forEach(path => {
if (fs.existsSync(path)) {
fs.unlinkSync(path);
}
});
this.mediaServer.listen({ path: this.paths.media });
this.infoServer.listen({ path: this.paths.info });
}
createClientServer() {
const clientServer = new WebSocket.Server({
noServer: true,
path: this.paths.stream
});
clientServer.on("connection", ws => {
console.log("new streaming connection");
this.addClient(ws);
ws.on("close", () => {
console.log("closed streaming connection");
this.removeClient(ws);
});
ws.on("error", err => console.log("streaming connection err", err));
});
clientServer.on("error", err =>
console.log("media streaming server err", err)
);
clientServer.on("close", () => this.closeServer("client"));
return clientServer;
}
createMediaServer() {
const mediaServer = net.createServer(socket => {
console.log("new media sink connection");
this.openSinkConnection({ media: socket });
socket.on("close", hadError => {
console.log(`closed media sink connection, hadError: ${hadError}`);
this.closeSinkConnection();
});
socket.on("data", data => this.sinkConnection.sink.addMedia(data));
socket.on("error", err => console.log("media sink err", err));
});
mediaServer.on("error", err => console.log("media sink server err", err));
mediaServer.on("close", () => this.closeServer("media"));
mediaServer.maxConnections = 1;
return mediaServer;
}
createInfoServer() {
const infoServer = net.createServer(socket => {
console.log("new info sink connection");
socket.setEncoding("utf8");
this.openSinkConnection({ info: socket });
socket.on("close", hadError => {
console.log(`closed info sink connection, hadError: ${hadError}`);
this.closeSinkConnection();
});
socket.on("data", data => this.sinkConnection.sink.addInfo(data));
socket.on("error", err =>
console.log(`info sink connection error ${err}`)
);
});
infoServer.on("error", err => console.log("info sink server err", err));
infoServer.on("close", () => this.closeServer("info"));
infoServer.maxConnections = 1;
return infoServer;
}
closeServer(type) {
console.log(`closed ${type} server`);
this.closed[type] = true;
if (
!this.closed.all &&
this.closed.media &&
this.closed.info &&
this.closed.client
) {
this.closed.all = true;
this.emit("close");
}
}
/**
* Close the media server, closing contained servers and ending open connections.
*
* The `close` event is emitted after all servers have closed and connections
* have ended (but not necessarily closed)
*/
close(callback) {
if (!this.mediaServer) {
throw Error("Not started");
} else if (this.closed.all) {
return;
}
if (callback) {
this.once("close", callback);
}
this.closeSinkConnection();
this.mediaServer.close();
this.infoServer.close();
this.clientServer.close();
}
openSinkConnection(sockets = {}) {
this.sinkConnection.sink = this.sinkConnection.sink || new MediaSink();
Object.assign(this.sinkConnection, sockets);
return this.sinkConnection;
}
closeSinkConnection() {
if (this.sinkConnection.info) {
this.sinkConnection.info.end();
}
if (this.sinkConnection.media) {
this.sinkConnection.media.end();
}
if (this.sinkConnection.sink) {
Array.from(this.clients.keys()).forEach(client => {
this.removeClient(client);
client.close();
});
}
this.sinkConnection = {};
}
addClient(ws) {
const onData = data => ws.send(data);
this.clients.set(ws, onData);
this.openSinkConnection().sink.addConsumer(onData);
}
removeClient(ws) {
if (this.clients.has(ws) && this.sinkConnection.sink) {
this.sinkConnection.sink.removeConsumer(this.clients.get(ws));
this.clients.delete(ws);
}
}
shouldHandle(req) {
return this.clientServer.shouldHandle(req);
}
handleUpgrade(req, socket, head) {
this.clientServer.handleUpgrade(req, socket, head, ws => {
this.clientServer.emit("connection", ws, req);
});
}
}
const INIT_SEGMENT = (INIT_SEGMENT_START = 1);
const MEDIA_SEGMENT = (MEDIA_SEGMENT_START = 2);
/**
* Top-level interface for sharing a media stream across consumers.
*/
class MediaSink {
constructor() {
this.mediaParser = new MediaParser();
this.initSegmentParser = new InitSegmentParser(this.mediaParser);
this.mediaMultiplexer = new MediaMultiplexer(
this.mediaParser,
this.initSegmentParser
);
this.partialInfo = "";
}
/**
* Consumes a chunk of the media stream contained in `buffer`.
*
* The buffers passed to this method in order should form the complete stream.
*/
addMedia(buffer) {
this.mediaParser.addMedia(buffer);
}
/**
* Consumes information about the stream.
*
* This information is used to split the stream into segments and then reconstruct valid
* streams to pass to consumers.
*
* `info` is a string that forms a stream of newline-delimited JSON of the form
* `{event: INIT_SEGMENT_START|MEDIA_SEGMENT_START, offset: Integer}`.
*/
addInfo(info) {
const infos = (this.partialInfo + info).split("\n").filter(s => s.length);
infos.forEach((info, i) => {
try {
const { event, offset } = JSON.parse(info);
this.mediaParser.addInfo({ event, offset });
} catch (e) {
if (e instanceof SyntaxError && i === infos.length - 1) {
// The trailing string couldn't be parsed, presumably because network packets aren't
// aligned with info boundaries.
this.partialInfo = info;
} else {
throw e;
}
}
});
}
/**
* Add a new consumer to start receiving media.
*
* The consumer is fed the initialization segment, then media segments. As the sink ingests
* media and info, it matches the events in info to the data available in media. Consumers
* will receive a continuous stream of media segments starting with the first one that is
* matched after the consumer is added.
*
* The sink does not modify timing of frames within segments, so the stream may start at a
* non-zero time. Clients can detect this based on the available buffered time ranges.
*
* @param dataCallback a function that accepts buffers which form a valid media stream.
* The buffers should not be modified.
*/
addConsumer(dataCallback) {
this.mediaMultiplexer.addConsumer(dataCallback);
}
/**
* Stops sending data to the given consumer, if it was previously added with `addConsumer`
*/
removeConsumer(dataCallback) {
this.mediaMultiplexer.removeConsumer(dataCallback);
}
}
/**
* Breaks a media stream into segments.
*
* emits a `data` event with `{start: Integer, buffer: Buffer}` when a new media buffer
* is received. `start` is the offset of the first byte in the buffer relative to the
* start of the stream. The buffer should not be modified.
*
* emits a `segmentStart` event with
* `{type: INIT_SEGMENT|MEDIA_SEGMENT, start: Integer, buffers: [Buffer]}` when a new
* segment is detected. `buffers` contains all data from the start of the segment through
* the last `data` event emitted.
*
* `data` events always precede `segmentStart` events. This means that listening once
* for `segmentStart` then on `data` results in a seemless media stream beginning at
* the received segment.
*/
class MediaParser extends EventEmitter {
constructor() {
super();
this.started = false;
this.info = {
// Offset of the start of the latest segment received in the info stream, or 0.
end: 0,
// Holds stream info when info leads media.
buffered: []
};
this.media = {
// Offset of the end of the latest media buffer received in the media stream, or 0.
end: 0,
// Holds stream media when media leads info
buffered: []
};
}
addMedia(buffer) {
this.started = true;
const mediaStart = this.media.end,
mediaEnd = mediaStart + buffer.length,
mediaData = { start: mediaStart, buffer };
const [newSegmentBoundaries, futureSegmentBoundaries] = _.partition(
this.info.buffered,
info => mediaStart <= info.offset && mediaEnd > info.offset
);
this.emit("data", mediaData);
newSegmentBoundaries.forEach(boundary => {
this.emit("newSegment", {
type:
boundary.event === INIT_SEGMENT_START ? INIT_SEGMENT : MEDIA_SEGMENT,
start: boundary.offset,
buffers: [buffer.subarray(boundary.offset - mediaStart)]
});
});
// Buffer media if it now leads info
if (mediaEnd > this.info.end) {
this.media.buffered.push(mediaData);
}
this.media.end = mediaEnd;
this.info.buffered = futureSegmentBoundaries;
}
addInfo({ event, offset }) {
this.started = true;
if (event !== INIT_SEGMENT_START && event !== MEDIA_SEGMENT_START) {
throw Error(`Invalid event ${event}`);
} else if (this.initSegmentReceived && event === INIT_SEGMENT_START) {
throw Error(`Expected media segment start, got init at offset ${offset}`);
} else if (!this.initSegmentReceived && event === MEDIA_SEGMENT_START) {
throw Error(`Expected init segment start, got media at offset ${offset}`);
} else if (offset < this.info.end) {
throw Error(`Expected info offset >= ${this.info.end}, got ${offset}`);
}
this.info.end = offset;
if (event === INIT_SEGMENT_START) {
this.initSegmentReceived = true;
}
if (offset < this.media.end) {
// newSegmentData is nonempty since media is buffered whenever media.end > offset.
// Find all segments that start after or contain offset
const [newSegmentData, unused] = _.partition(
this.media.buffered,
data => data.start >= offset || data.start + data.buffer.length > offset
);
this.emit("newSegment", {
type: event === INIT_SEGMENT_START ? INIT_SEGMENT : MEDIA_SEGMENT,
start: newSegmentData[0].start,
buffers: newSegmentData.map(({ start, buffer }) =>
offset > start ? buffer.subarray(offset - start) : buffer
)
});
this.media.buffered = newSegmentData;
} else {
// Info leads media
this.info.buffered.push({ event, offset });
}
}
}
/** Extracts and stores the initialization segment from a parsed media stream. */
class InitSegmentParser extends EventEmitter {
constructor(mediaParser) {
if (mediaParser.started) {
throw Error("mediaParser is already started, must not be");
}
super();
this.started = false;
this.mediaParser = mediaParser;
this.initBuffers = [];
this.initStart = null;
this.initEnd = null;
this.mediaParser.on("newSegment", this.onNewSegment);
}
onNewSegment = ({ type, start, buffers }) => {
this.started = true;
if (type === INIT_SEGMENT) {
this.initStart = start;
this.initBuffers.push(...buffers);
this.mediaParser.on("data", this.onData);
} else {
this.initEnd = start;
this.mediaParser.off("data", this.onData);
this.mediaParser.off("newSegment", this.onNewSegment);
this.buildInitSegment();
}
};
onData = ({ buffer }) => {
this.initBuffers.push(buffer);
};
buildInitSegment() {
const initSegmentSize = this.initEnd - this.initStart,
initSegment = Buffer.alloc(initSegmentSize);
let i,
buffer,
copySize,
offset = 0;
for (i = 0; i < this.initBuffers.length; i++) {
buffer = this.initBuffers[i];
copySize = Math.min(initSegmentSize - offset, buffer.length);
buffer.copy(initSegment, offset, 0, copySize);
offset += copySize;
if (offset == this.initEnd) {
break;
}
}
this.initSegment = initSegment;
this.emit("initSegment", initSegment);
}
}
/** Forwards a media stream to a single data callback. */
class MediaConsumer {
constructor(mediaParser, onData) {
this.mediaParser = mediaParser;
this.onData = onData;
}
start(initSegment) {
if (initSegment) {
this.onData(initSegment);
}
this.mediaParser.once("newSegment", this.onNewSegment);
}
onNewSegment = ({ buffers }) => {
buffers.forEach(buffer => this.onData(buffer));
this.dataListener = ({ buffer }) => this.onData(buffer);
this.mediaParser.on("data", this.dataListener);
};
unregister() {
this.mediaParser.off("newSegment", this.onNewSegment);
if (this.dataListener) {
this.mediaParser.off("data", this.dataListener);
}
}
}
/** Splits a parsed media stream between attached consumers. */
class MediaMultiplexer {
constructor(mediaParser, initSegmentParser) {
if (mediaParser.started) {
throw Error("mediaParser is already started, must not be");
}
this.consumers = new Map();
this.mediaParser = mediaParser;
this.initSegmentParser = initSegmentParser;
this.delayedConsumers = [];
if (initSegmentParser.initSegment) {
this.initSegment = this.initSegment;
} else {
initSegmentParser.once("initSegment", initSegment => {
this.initSegment = initSegment;
this.delayedConsumers.forEach(consumer => consumer.start(initSegment));
this.delayedConsumers = null;
});
}
}
/**
* Add a data callback to receive a media stream.
*
* `dataCallback` is a function that accepts a Buffer of media bytes.
*/
addConsumer(dataCallback) {
if (this.consumers.has(dataCallback)) {
throw Error("Consumer already added");
}
const consumer = new MediaConsumer(this.mediaParser, dataCallback);
this.consumers.set(dataCallback, consumer);
if (this.initSegment) {
consumer.start(this.initSegment);
} else if (this.initSegmentParser.started) {
this.delayedConsumers.push(consumer);
} else {
consumer.start();
}
}
removeConsumer(dataCallback) {
if (this.consumers.has(dataCallback)) {
this.consumers.get(dataCallback).unregister();
this.consumers.delete(dataCallback);
}
}
}
module.exports = {
StreamingMediaServer,
MediaSink,
MediaParser,
MediaConsumer,
MediaMultiplexer,
InitSegmentParser,
INIT_SEGMENT,
MEDIA_SEGMENT
};