forked from iizukanao/node-rtsp-rtmp-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstream_server.js
More file actions
485 lines (421 loc) · 15 KB
/
stream_server.js
File metadata and controls
485 lines (421 loc) · 15 KB
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
// RTSP and RTMP/RTMPE/RTMPT/RTMPTE server implementation.
// Also serves HTTP contents as this server is meant to
// be run on port 80.
import net from 'net';
import fs from 'fs';
import crypto from 'crypto';
import config from './config';
import rtmp from './rtmp';
import http from './http';
import rtsp from './rtsp';
import h264 from './h264';
import aac from './aac';
import mp4 from './mp4';
import Bits from './bits';
import avstreams from './avstreams';
import CustomReceiver from './custom_receiver';
import logger from './logger';
import packageJson from './package.json';
import Sequent from 'sequent';
// If true, incoming video/audio packets are printed to the console
let DEBUG_INCOMING_PACKET_DATA = false;
// If true, hash value of each incoming video/audio access unit is printed to the console
let DEBUG_INCOMING_PACKET_HASH = false;
//# Default server name for RTSP and HTTP responses
let DEFAULT_SERVER_NAME = `node-rtsp-rtmp-server/${packageJson.version}`;
let serverName = config.serverName != null ? config.serverName : DEFAULT_SERVER_NAME;
class StreamServer {
constructor(opts) {
this.serverName = (opts != null ? opts.serverName : undefined) != null ? (opts != null ? opts.serverName : undefined) : serverName;
if (config.enableRTMP || config.enableRTMPT) {
// Create RTMP server
let stream;
this.rtmpServer = new rtmp.RTMPServer;
this.rtmpServer.on('video_start', streamId => {
stream = avstreams.getOrCreate(streamId);
return this.onReceiveVideoControlBuffer(stream);
}
);
this.rtmpServer.on('video_data', (streamId, pts, dts, nalUnits) => {
stream = avstreams.get(streamId);
if (stream != null) {
return this.onReceiveVideoPacket(stream, nalUnits, pts, dts);
} else {
return logger.warn(`warn: Received invalid streamId from rtmp: ${streamId}`);
}
}
);
this.rtmpServer.on('audio_start', streamId => {
stream = avstreams.getOrCreate(streamId);
return this.onReceiveAudioControlBuffer(stream);
}
);
this.rtmpServer.on('audio_data', (streamId, pts, dts, adtsFrame) => {
stream = avstreams.get(streamId);
if (stream != null) {
return this.onReceiveAudioPacket(stream, adtsFrame, pts, dts);
} else {
return logger.warn(`warn: Received invalid streamId from rtmp: ${streamId}`);
}
}
);
}
if (config.enableCustomReceiver) {
// Setup data receivers for custom protocol
this.customReceiver = new CustomReceiver(config.receiverType, {
videoControl: function() {
return this.onReceiveVideoControlBuffer(...arguments);
}.bind(this),
audioControl: function() {
return this.onReceiveAudioControlBuffer(...arguments);
}.bind(this),
videoData: function() {
return this.onReceiveVideoDataBuffer(...arguments);
}.bind(this),
audioData: function() {
return this.onReceiveAudioDataBuffer(...arguments);
}.bind(this)
}
);
// Delete old sockets
this.customReceiver.deleteReceiverSocketsSync();
}
if (config.enableHTTP) {
this.httpHandler = new http.HTTPHandler({
serverName: this.serverName,
documentRoot: (opts != null ? opts.documentRoot : undefined)
});
}
if (config.enableRTSP || config.enableHTTP || config.enableRTMPT) {
let httpHandler, rtmptCallback;
if (config.enableRTMPT) {
rtmptCallback = function() {
return this.rtmpServer.handleRTMPTRequest(...arguments);
}.bind(this);
} else {
rtmptCallback = null;
}
if (config.enableHTTP) {
({ httpHandler } = this);
} else {
httpHandler = null;
}
this.rtspServer = new rtsp.RTSPServer({
serverName : this.serverName,
httpHandler,
rtmptCallback
});
this.rtspServer.on('video_start', stream => {
return this.onReceiveVideoControlBuffer(stream);
}
);
this.rtspServer.on('audio_start', stream => {
return this.onReceiveAudioControlBuffer(stream);
}
);
this.rtspServer.on('video', (stream, nalUnits, pts, dts) => {
return this.onReceiveVideoNALUnits(stream, nalUnits, pts, dts);
}
);
this.rtspServer.on('audio', (stream, accessUnits, pts, dts) => {
return this.onReceiveAudioAccessUnits(stream, accessUnits, pts, dts);
}
);
}
avstreams.on('new', function(stream) {
if (DEBUG_INCOMING_PACKET_HASH) {
return stream.lastSentVideoTimestamp = 0;
}
});
avstreams.on('reset', function(stream) {
if (DEBUG_INCOMING_PACKET_HASH) {
return stream.lastSentVideoTimestamp = 0;
}
});
avstreams.on('end', stream => {
if (config.enableRTSP) {
this.rtspServer.sendEOS(stream);
}
if (config.enableRTMP || config.enableRTMPT) {
return this.rtmpServer.sendEOS(stream);
}
}
);
// for mp4
avstreams.on('audio_data', (stream, data, pts) => {
return this.onReceiveAudioAccessUnits(stream, [ data ], pts, pts);
}
);
avstreams.on('video_data', (stream, nalUnits, pts, dts) => {
if ((dts == null)) {
dts = pts;
}
return this.onReceiveVideoNALUnits(stream, nalUnits, pts, dts);
}
);
avstreams.on('audio_start', stream => {
return this.onReceiveAudioControlBuffer(stream);
}
);
avstreams.on('video_start', stream => {
return this.onReceiveVideoControlBuffer(stream);
}
);
}
//# TODO: Do we need to do something for remove_stream event?
//avstreams.on 'remove_stream', (stream) ->
// logger.raw "received remove_stream event from stream #{stream.id}"
attachRecordedDir(dir) {
if (config.recordedApplicationName != null) {
logger.info(`attachRecordedDir: dir=${dir} app=${config.recordedApplicationName}`);
return avstreams.attachRecordedDirToApp(dir, config.recordedApplicationName);
}
}
attachMP4(filename, streamName) {
logger.info(`attachMP4: file=${filename} stream=${streamName}`);
let context = this;
let generator = new avstreams.AVStreamGenerator({
// Generate an AVStream upon request
generate() {
let mp4File;
try {
mp4File = new mp4.MP4File(filename);
} catch (err) {
logger.error(`error opening MP4 file ${filename}: ${err}`);
return null;
}
let streamId = avstreams.createNewStreamId();
let mp4Stream = new avstreams.MP4Stream(streamId);
logger.info(`created stream ${streamId} from ${filename}`);
avstreams.emit('new', mp4Stream);
avstreams.add(mp4Stream);
mp4Stream.type = avstreams.STREAM_TYPE_RECORDED;
let audioSpecificConfig = null;
mp4File.on('audio_data', (data, pts) => context.onReceiveAudioAccessUnits(mp4Stream, [ data ], pts, pts));
mp4File.on('video_data', function(nalUnits, pts, dts) {
if ((dts == null)) {
dts = pts;
}
return context.onReceiveVideoNALUnits(mp4Stream, nalUnits, pts, dts);
});
mp4File.on('eof', () => {
return mp4Stream.emit('end');
}
);
mp4File.parse();
mp4Stream.updateSPS(mp4File.getSPS());
mp4Stream.updatePPS(mp4File.getPPS());
let ascBuf = mp4File.getAudioSpecificConfig();
let bits = new Bits(ascBuf);
let ascInfo = aac.readAudioSpecificConfig(bits);
mp4Stream.updateConfig({
audioSpecificConfig: ascBuf,
audioASCInfo: ascInfo,
audioSampleRate: ascInfo.samplingFrequency,
audioClockRate: 90000,
audioChannels: ascInfo.channelConfiguration,
audioObjectType: ascInfo.audioObjectType
});
mp4Stream.durationSeconds = mp4File.getDurationSeconds();
mp4Stream.lastTagTimestamp = mp4File.getLastTimestamp();
mp4Stream.mp4File = mp4File;
mp4File.fillBuffer(function() {
context.onReceiveAudioControlBuffer(mp4Stream);
return context.onReceiveVideoControlBuffer(mp4Stream);
});
return mp4Stream;
},
play() {
return this.mp4File.play();
},
pause() {
return this.mp4File.pause();
},
resume() {
return this.mp4File.resume();
},
seek(seekSeconds, callback) {
let actualStartTime = this.mp4File.seek(seekSeconds);
return callback(null, actualStartTime);
},
sendVideoPacketsSinceLastKeyFrame(endSeconds, callback) {
return this.mp4File.sendVideoPacketsSinceLastKeyFrame(endSeconds, callback);
},
teardown() {
this.mp4File.close();
return this.destroy();
},
getCurrentPlayTime() {
return this.mp4File.currentPlayTime;
},
isPaused() {
return this.mp4File.isPaused();
}
});
return avstreams.addGenerator(streamName, generator);
}
stop(callback) {
if (config.enableCustomReceiver) {
this.customReceiver.deleteReceiverSocketsSync();
}
return (typeof callback === 'function' ? callback() : undefined);
}
start(callback) {
let seq = new Sequent;
let waitCount = 0;
if (config.enableRTMP) {
waitCount++;
this.rtmpServer.start({ port: config.rtmpServerPort }, () => seq.done());
}
// RTMP server is ready
if (config.enableCustomReceiver) {
// Start data receivers for custom protocol
this.customReceiver.start();
}
if (config.enableRTSP || config.enableHTTP || config.enableRTMPT) {
waitCount++;
this.rtspServer.start({ port: config.serverPort }, () => seq.done());
}
return seq.wait(waitCount, () => typeof callback === 'function' ? callback() : undefined);
}
setLivePathConsumer(func) {
if (config.enableRTSP) {
return this.rtspServer.setLivePathConsumer(func);
}
}
// buf argument can be null (not used)
onReceiveVideoControlBuffer(stream, buf) {
stream.resetFrameRate(stream);
stream.isVideoStarted = true;
stream.timeAtVideoStart = Date.now();
return stream.timeAtAudioStart = stream.timeAtVideoStart;
}
// stream.spropParameterSets = ''
// buf argument can be null (not used)
onReceiveAudioControlBuffer(stream, buf) {
stream.isAudioStarted = true;
stream.timeAtAudioStart = Date.now();
return stream.timeAtVideoStart = stream.timeAtAudioStart;
}
onReceiveVideoDataBuffer(stream, buf) {
let pts = (buf[1] * 0x010000000000) +
(buf[2] * 0x0100000000) +
(buf[3] * 0x01000000) +
(buf[4] * 0x010000) +
(buf[5] * 0x0100) +
buf[6];
// TODO: Support dts
let dts = pts;
let nalUnit = buf.slice(7);
return this.onReceiveVideoPacket(stream, nalUnit, pts, dts);
}
onReceiveAudioDataBuffer(stream, buf) {
let pts = (buf[1] * 0x010000000000) +
(buf[2] * 0x0100000000) +
(buf[3] * 0x01000000) +
(buf[4] * 0x010000) +
(buf[5] * 0x0100) +
buf[6];
// TODO: Support dts
let dts = pts;
let adtsFrame = buf.slice(7);
return this.onReceiveAudioPacket(stream, adtsFrame, pts, dts);
}
// nal_unit_type 5 must not separated with 7 and 8 which
// share the same timestamp as 5
onReceiveVideoNALUnits(stream, nalUnits, pts, dts) {
if (DEBUG_INCOMING_PACKET_DATA) {
logger.info(`receive video: num_nal_units=${nalUnits.length} pts=${pts}`);
}
if (config.enableRTSP) {
// rtspServer will parse nalUnits and updates SPS/PPS for the stream,
// so we don't need to parse them here.
// TODO: Should SPS/PPS be parsed here?
this.rtspServer.sendVideoData(stream, nalUnits, pts, dts);
}
if (config.enableRTMP || config.enableRTMPT) {
this.rtmpServer.sendVideoPacket(stream, nalUnits, pts, dts);
}
let hasVideoFrame = false;
for (let nalUnit of Array.from(nalUnits)) {
let nalUnitType = h264.getNALUnitType(nalUnit);
if (nalUnitType === h264.NAL_UNIT_TYPE_SPS) { // 7
stream.updateSPS(nalUnit);
} else if (nalUnitType === h264.NAL_UNIT_TYPE_PPS) { // 8
stream.updatePPS(nalUnit);
} else if ((nalUnitType === h264.NAL_UNIT_TYPE_IDR_PICTURE) ||
(nalUnitType === h264.NAL_UNIT_TYPE_NON_IDR_PICTURE)) { // 5 (key frame) or 1 (inter frame)
hasVideoFrame = true;
}
if (DEBUG_INCOMING_PACKET_HASH) {
let md5 = crypto.createHash('md5');
md5.update(nalUnit);
let tsDiff = pts - stream.lastSentVideoTimestamp;
logger.info(`video: pts=${pts} pts_diff=${tsDiff} md5=${md5.digest('hex').slice(0, 7)} nal_unit_type=${nalUnitType} bytes=${nalUnit.length}`);
stream.lastSentVideoTimestamp = pts;
}
}
if (hasVideoFrame) {
stream.calcFrameRate(pts);
}
}
// Takes H.264 NAL units separated by start code (0x000001)
//
// arguments:
// nalUnit: Buffer
// pts: timestamp in 90 kHz clock rate (PTS)
onReceiveVideoPacket(stream, nalUnitGlob, pts, dts) {
let nalUnits = h264.splitIntoNALUnits(nalUnitGlob);
if (nalUnits.length === 0) {
return;
}
this.onReceiveVideoNALUnits(stream, nalUnits, pts, dts);
}
// pts, dts: in 90KHz clock rate
onReceiveAudioAccessUnits(stream, accessUnits, pts, dts) {
if (config.enableRTSP) {
this.rtspServer.sendAudioData(stream, accessUnits, pts, dts);
}
if (DEBUG_INCOMING_PACKET_DATA) {
logger.info(`receive audio: num_access_units=${accessUnits.length} pts=${pts}`);
}
let ptsPerFrame = 90000 / (stream.audioSampleRate / 1024);
for (let i = 0; i < accessUnits.length; i++) {
let accessUnit = accessUnits[i];
if (DEBUG_INCOMING_PACKET_HASH) {
let md5 = crypto.createHash('md5');
md5.update(accessUnit);
logger.info(`audio: pts=${pts} md5=${md5.digest('hex').slice(0, 7)} bytes=${accessUnit.length}`);
}
if (config.enableRTMP || config.enableRTMPT) {
this.rtmpServer.sendAudioPacket(stream, accessUnit,
Math.round(pts + (ptsPerFrame * i)),
Math.round(dts + (ptsPerFrame * i)));
}
}
}
// pts, dts: in 90KHz clock rate
onReceiveAudioPacket(stream, adtsFrameGlob, pts, dts) {
let adtsFrames = aac.splitIntoADTSFrames(adtsFrameGlob);
if (adtsFrames.length === 0) {
return;
}
let adtsInfo = aac.parseADTSFrame(adtsFrames[0]);
let isConfigUpdated = false;
stream.updateConfig({
audioSampleRate: adtsInfo.sampleRate,
audioClockRate: adtsInfo.sampleRate,
audioChannels: adtsInfo.channels,
audioObjectType: adtsInfo.audioObjectType
});
let rtpTimePerFrame = 1024;
let rawDataBlocks = [];
for (let i = 0; i < adtsFrames.length; i++) {
let adtsFrame = adtsFrames[i];
let rawDataBlock = adtsFrame.slice(7);
rawDataBlocks.push(rawDataBlock);
}
return this.onReceiveAudioAccessUnits(stream, rawDataBlocks, pts, dts);
}
}
export default StreamServer;