forked from illuspas/Node-Media-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_rtmp_session.js
1019 lines (931 loc) · 34.2 KB
/
node_rtmp_session.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by Mingliang Chen on 17/8/1.
// illuspas[a]gmail.com
// Copyright (c) 2017 Nodemedia. All rights reserved.
//
const EventEmitter = require('events');
const QueryString = require('querystring');
const AAC = require('./node_core_aac');
const AMF = require('./node_core_amf');
const Handshake = require('./node_rtmp_handshake');
const BufferPool = require('./node_core_bufferpool');
const NodeFlvSession = require('./node_flv_session');
const NodeCoreUtils = require('./node_core_utils');
const EXTENDED_TIMESTAMP_TYPE_NOT_USED = 'not-used';
const EXTENDED_TIMESTAMP_TYPE_ABSOLUTE = 'absolute';
const EXTENDED_TIMESTAMP_TYPE_DELTA = 'delta';
const TIMESTAMP_ROUNDOFF = 4294967296;
const STREAM_BEGIN = 0x00;
const STREAM_EOF = 0x01;
const STREAM_DRY = 0x02;
const STREAM_EMPTY = 0x1f;
const STREAM_READY = 0x20;
const RTMP_CHUNK_SIZE = 128;
const RTMP_PING_TIME = 60000;
const RTMP_PING_TIMEOUT = 30000;
const AUDIO_CODEC_NAME = [
'',
'ADPCM',
"MP3",
"LinearLE",
"Nellymoser16",
"Nellymoser8",
"Nellymoser",
"G711A",
"G711U",
"",
"AAC",
"Speex",
"",
"",
"MP3-8K",
"DeviceSpecific",
"Uncompressed"
];
const VIDEO_CODEC_NAME = [
"",
"Jpeg",
"Sorenson-H263",
"ScreenVideo",
"On2-VP6",
"On2-VP6-Alpha",
"ScreenVideo2",
"H264",
"",
"",
"",
"",
"H265"
];
class NodeRtmpSession extends EventEmitter {
constructor(config, socket) {
super();
this.config = config;
this.bp = new BufferPool(this.handleData());
this.nodeEvent = NodeCoreUtils.nodeEvent;
this.socket = socket;
this.players = null;
this.inChunkSize = RTMP_CHUNK_SIZE;
this.outChunkSize = config.rtmp.chunk_size ? config.rtmp.chunk_size : RTMP_CHUNK_SIZE;
this.previousChunkMessage = {};
this.ping = config.rtmp.ping ? config.rtmp.ping * 1000 : RTMP_PING_TIME;
this.pingTimeout = config.rtmp.ping_timeout ? config.rtmp.ping_timeout * 1000 : RTMP_PING_TIMEOUT;
this.pingInterval = null;
this.socket.setTimeout(this.pingTimeout); //Use nodejs network timeout mechanism
this.isStarting = false;
this.isPublishing = false;
this.isPlaying = false;
this.isIdling = false;
this.isFirstAudioReceived = false;
this.isFirstVideoReceived = false;
this.metaData = null;
this.aacSequenceHeader = null;
this.avcSequenceHeader = null;
this.audioCodec = 0;
this.audioCodecName = '';
this.audioProfileName = '';
this.audioSamplerate = 0;
this.audioChannels = 1;
this.videoCodec = 0;
this.videoCodecName = '';
this.videoSize = 0 + 'x' + 0;
this.videoFps = 0;
this.gopCacheEnable = config.rtmp.gop_cache;
this.rtmpGopCacheQueue = null;
this.flvGopCacheQueue = null;
this.ackSize = 0;
this.inLastAck = 0;
this.appname = '';
this.streams = 0;
this.playStreamId = 0;
this.playStreamPath = '';
this.playArgs = '';
this.publishStreamId = 0;
this.publishStreamPath = '';
this.publishArgs = '';
this.on('connect', this.onConnect);
this.on('publish', this.onPublish);
this.on('play', this.onPlay);
this.on('closeStream', this.onCloseStream);
this.on('deleteStream', this.onDeleteStream);
this.socket.on('data', this.onSocketData.bind(this));
this.socket.on('close', this.onSocketClose.bind(this));
this.socket.on('error', this.onSocketError.bind(this));
this.socket.on('timeout', this.onSocketTimeout.bind(this));
}
run() {
this.isStarting = true;
this.bp.init();
}
stop() {
if (this.isStarting) {
this.isStarting = false;
this.bp.stop();
}
}
reject() {
this.isStarting = false;
}
onSocketData(data) {
this.bp.push(data);
}
onSocketError(e) {
// console.log(`[rtmp socket error] id:${this.id}`,e);
this.stop();
}
onSocketClose() {
// console.log(`[rtmp socket close] id:${this.id}`);
this.stop();
}
onSocketTimeout() {
// console.log(`[rtmp socket timeout] id:${this.id}`);
this.stop();
}
* handleData() {
console.log('[rtmp handshake] start');
if (this.bp.need(1537)) {
if (yield) return;
}
let c0c1 = this.bp.read(1537);
let s0s1s2 = Handshake.generateS0S1S2(c0c1);
this.socket.write(s0s1s2);
if (this.bp.need(1536)) {
if (yield) return;
}
let c2 = this.bp.read(1536);
console.log('[rtmp handshake] done');
console.log('[rtmp message parser] start');
this.bp.readBytes = 0;
while (this.isStarting) {
let message = {};
let chunkMessageHeader = null;
let previousChunk = null;
if (this.bp.need(1)) {
if (yield) break;
}
let chunkBasicHeader = this.bp.read(1);
message.formatType = chunkBasicHeader[0] >> 6;
message.chunkStreamID = chunkBasicHeader[0] & 0x3F;
if (message.chunkStreamID === 0) {
// Chunk basic header 2 64-319
if (this.bp.need(1)) {
if (yield) break;
}
let exCSID = this.bp.read(1);
message.chunkStreamID = exCSID[0] + 64;
} else if (message.chunkStreamID === 1) {
// Chunk basic header 3 64-65599
if (this.bp.need(2)) {
if (yield) break;
}
let exCSID = this.bp.read(2);
message.chunkStreamID = (exCSID[1] << 8) + exCSID[0] + 64;
} else {
// Chunk basic header 1 2-63
}
previousChunk = this.previousChunkMessage[message.chunkStreamID];
if (message.formatType === 0) {
//Type 0 (11 bytes)
if (this.bp.need(11)) {
if (yield) break;
}
chunkMessageHeader = this.bp.read(11);
message.timestamp = chunkMessageHeader.readUIntBE(0, 3);
if (message.timestamp === 0xffffff) {
message.extendedTimestampType = EXTENDED_TIMESTAMP_TYPE_ABSOLUTE;
} else {
message.extendedTimestampType = EXTENDED_TIMESTAMP_TYPE_NOT_USED;
}
message.timestampDelta = 0;
message.messageLength = chunkMessageHeader.readUIntBE(3, 3);
message.messageTypeID = chunkMessageHeader[6];
message.messageStreamID = chunkMessageHeader.readUInt32LE(7);
message.receivedLength = 0;
message.chunks = [];
} else if (message.formatType === 1) {
//Type 1 (7 bytes)
if (this.bp.need(7)) {
if (yield) break;
}
chunkMessageHeader = this.bp.read(7);
message.timestampDelta = chunkMessageHeader.readUIntBE(0, 3);
if (message.timestampDelta === 0xffffff) {
message.extendedTimestampType = EXTENDED_TIMESTAMP_TYPE_DELTA;
} else {
message.extendedTimestampType = EXTENDED_TIMESTAMP_TYPE_NOT_USED;
}
message.messageLength = chunkMessageHeader.readUIntBE(3, 3);
message.messageTypeID = chunkMessageHeader[6];
if (previousChunk != null) {
message.timestamp = previousChunk.timestamp;
message.messageStreamID = previousChunk.messageStreamID;
message.receivedLength = previousChunk.receivedLength;
message.chunks = previousChunk.chunks;
} else {
console.error(`Chunk reference error for type ${message.formatType}: previous chunk for id ${message.chunkStreamID} is not found`);
break;
}
} else if (message.formatType === 2) {
// Type 2 (3 bytes)
if (this.bp.need(3)) {
if (yield) break;
}
chunkMessageHeader = this.bp.read(3);
message.timestampDelta = chunkMessageHeader.readUIntBE(0, 3);
if (message.timestampDelta === 0xffffff) {
message.extendedTimestampType = EXTENDED_TIMESTAMP_TYPE_DELTA;
} else {
message.extendedTimestampType = EXTENDED_TIMESTAMP_TYPE_NOT_USED;
}
if (previousChunk != null) {
message.timestamp = previousChunk.timestamp;
message.messageStreamID = previousChunk.messageStreamID;
message.messageLength = previousChunk.messageLength;
message.messageTypeID = previousChunk.messageTypeID;
message.receivedLength = previousChunk.receivedLength;
message.chunks = previousChunk.chunks;
} else {
console.error(`Chunk reference error for type ${message.formatType}: previous chunk for id ${message.chunkStreamID} is not found`);
break;
}
} else if (message.formatType == 3) {
// Type 3 (0 byte)
if (previousChunk != null) {
message.timestamp = previousChunk.timestamp;
message.messageStreamID = previousChunk.messageStreamID;
message.messageLength = previousChunk.messageLength;
message.timestampDelta = previousChunk.timestampDelta;
message.messageTypeID = previousChunk.messageTypeID;
message.receivedLength = previousChunk.receivedLength;
message.chunks = previousChunk.chunks;
} else {
console.error(`Chunk reference error for type ${message.formatType}: previous chunk for id ${message.chunkStreamID} is not found`);
break;
}
} else {
console.error("Unknown format type: " + message.formatType);
break;
}
if (message.extendedTimestampType === EXTENDED_TIMESTAMP_TYPE_ABSOLUTE) {
if (this.bp.need(4)) {
if (yield) break;
}
let extTimestamp = this.bp.read(4);
message.timestamp = extTimestamp.readUInt32BE();
} else if (message.extendedTimestampType === EXTENDED_TIMESTAMP_TYPE_DELTA) {
let extTimestamp = this.bp.read(4);
message.timestampDelta = extTimestamp.readUInt32BE();
}
let chunkBodySize = message.messageLength;
chunkBodySize -= message.receivedLength;
chunkBodySize = Math.min(chunkBodySize, this.inChunkSize);
if (this.bp.need(chunkBodySize)) {
if (yield) break;
}
let chunkBody = this.bp.read(chunkBodySize);
message.receivedLength += chunkBodySize;
message.chunks.push(chunkBody);
if (message.receivedLength == message.messageLength) {
if (message.timestampDelta != null) {
message.timestamp += message.timestampDelta;
if (message.timestamp > TIMESTAMP_ROUNDOFF) {
message.timestamp %= TIMESTAMP_ROUNDOFF;
}
}
let rtmpBody = Buffer.concat(message.chunks);
this.handleRTMPMessage(message, rtmpBody);
message.receivedLength = 0;
message.chunks = [];
rtmpBody = null;
}
this.previousChunkMessage[message.chunkStreamID] = message;
if (this.bp.readBytes >= 0xf0000000) {
this.bp.readBytes = 0;
this.inLastAck = 0;
}
if (this.ackSize > 0 && this.bp.readBytes - this.inLastAck >= this.ackSize) {
this.inLastAck = this.bp.readBytes;
this.sendACK(this.bp.readBytes);
}
}
console.log('[rtmp message parser] done');
this.onCloseStream(this.playStreamId);
this.onCloseStream(this.publishStreamId);
if (this.pingInterval != null) {
clearImmediate(this.pingInterval);
this.pingInterval = null;
}
this.nodeEvent.emit('doneConnect', this.id, this.connectCmdObj);
this.socket.destroy();
this.sessions.delete(this.id);
this.idlePlayers = null;
this.publishers = null;
this.sessions = null;
}
createChunkBasicHeader(fmt, id) {
let out;
if (id >= 64 + 255) {
out = Buffer.alloc(3);
out[0] = (fmt << 6) | 1;
out[1] = (id - 64) & 0xFF;
out[2] = ((id - 64) >> 8) & 0xFF;
} else if (id >= 64) {
out = Buffer.alloc(2);
out[0] = (fmt << 6) | 0;
out[1] = (id - 64) & 0xFF;
} else {
out = Buffer.alloc(1);
out[0] = (fmt << 6) | id;
}
return out;
}
createRtmpMessage(rtmpHeader, rtmpBody) {
let chunkBasicHeader = this.createChunkBasicHeader(0, rtmpHeader.chunkStreamID);
let chunkMessageHeader = Buffer.alloc(11);
let chunkExtendedTimestamp;
let extendedTimestamp = 0;
let useExtendedTimestamp = false
let rtmpBodySize = rtmpBody.length;
let rtmpBodyPos = 0;
let chunkBodys = [];
rtmpHeader.messageLength = rtmpBody.length;
if (rtmpHeader.timestamp >= 0xffffff) {
useExtendedTimestamp = true;
extendedTimestamp = rtmpHeader.timestamp;
chunkExtendedTimestamp = Buffer.alloc(4);
chunkExtendedTimestamp.writeUInt32BE(extendedTimestamp);
}
chunkMessageHeader.writeUIntBE(useExtendedTimestamp ? 0xffffff : rtmpHeader.timestamp, 0, 3);
chunkMessageHeader.writeUIntBE(rtmpHeader.messageLength, 3, 3);
chunkMessageHeader.writeUInt8(rtmpHeader.messageTypeID, 6);
chunkMessageHeader.writeUInt32LE(rtmpHeader.messageStreamID, 7);
chunkBodys.push(chunkBasicHeader);
chunkBodys.push(chunkMessageHeader);
if (useExtendedTimestamp) {
chunkBodys.push(chunkExtendedTimestamp);
}
do {
if (rtmpBodySize > this.outChunkSize) {
chunkBodys.push(rtmpBody.slice(rtmpBodyPos, rtmpBodyPos + this.outChunkSize));
rtmpBodySize -= this.outChunkSize
rtmpBodyPos += this.outChunkSize;
chunkBodys.push(this.createChunkBasicHeader(3, rtmpHeader.chunkStreamID));
if (useExtendedTimestamp) {
chunkBodys.push(chunkExtendedTimestamp);
}
} else {
chunkBodys.push(rtmpBody.slice(rtmpBodyPos, rtmpBodyPos + rtmpBodySize));
rtmpBodySize -= rtmpBodySize;
rtmpBodyPos += rtmpBodySize;
}
} while (rtmpBodySize > 0)
return Buffer.concat(chunkBodys);
}
handleRTMPMessage(rtmpHeader, rtmpBody) {
// console.log(`[rtmp handleRtmpMessage] rtmpHeader.messageTypeID=${rtmpHeader.messageTypeID}`);
switch (rtmpHeader.messageTypeID) {
case 1:
this.inChunkSize = rtmpBody.readUInt32BE();
console.log('[rtmp handleRtmpMessage] Set In chunkSize:' + this.inChunkSize);
break;
case 3:
// console.log('[rtmp handleRtmpMessage] Ack:' + rtmpBody.readUInt32BE());
break;
case 4:
let userControlMessage = {};
userControlMessage.eventType = rtmpBody.readUInt16BE();
userControlMessage.eventData = rtmpBody.slice(2);
this.handleUserControlMessage(userControlMessage);
break;
case 5:
this.ackSize = rtmpBody.readUInt32BE();
// console.log(`[rtmp handleRtmpMessage] WindowAck: ${this.ackSize}`);
break;
case 8:
//Audio Data
this.handleAudioMessage(rtmpHeader, rtmpBody);
break;
case 9:
//Video Data
this.handleVideoMessage(rtmpHeader, rtmpBody);
break;
case 15:
//AMF3 DataMessage
let amf3Data = AMF.decodeAmf0Data(rtmpBody.slice(1));
this.handleAMFDataMessage(rtmpHeader.messageStreamID, amf3Data);
break;
case 17:
//AMF3 CommandMessage
let amf3Cmd = AMF.decodeAmf0Cmd(rtmpBody.slice(1));
this.handleAMFCommandMessage(rtmpHeader.messageStreamID, amf3Cmd);
break;
case 18:
//AMF0 DataMessage
let amf0Data = AMF.decodeAmf0Data(rtmpBody);
this.handleAMFDataMessage(rtmpHeader.messageStreamID, amf0Data);
break;
case 20:
//AMF0 CommandMessage
let amf0Cmd = AMF.decodeAmf0Cmd(rtmpBody);
this.handleAMFCommandMessage(rtmpHeader.messageStreamID, amf0Cmd);
break;
}
}
handleUserControlMessage(userControlMessage) {
switch (userControlMessage.eventType) {
case 3:
let streamID = userControlMessage.eventData.readUInt32BE();
let bufferLength = userControlMessage.eventData.readUInt32BE(4);
console.log(`[rtmp handleUserControlMessage] SetBufferLength: streamID=${streamID} bufferLength=${bufferLength}`);
break;
case 7:
let timestamp = userControlMessage.eventData.readUInt32BE();
// console.log(`[rtmp handleUserControlMessage] PingResponse: timestamp=${timestamp}`);
break;
}
}
handleAMFDataMessage(streamID, dataMessage) {
// console.log('handleAMFDataMessage', dataMessage);
switch (dataMessage.cmd) {
case '@setDataFrame':
if (dataMessage.dataObj != null) {
let opt = {
cmd: 'onMetaData',
cmdObj: dataMessage.dataObj
};
this.metaData = AMF.encodeAmf0Data(opt);
this.audioSamplerate = dataMessage.dataObj.audiosamplerate;
this.audioChannels = dataMessage.dataObj.stereo ? 2 : 1;
this.videoSize = dataMessage.dataObj.width + 'x' + dataMessage.dataObj.height;
this.videoFps = dataMessage.dataObj.framerate;
}
break;
default:
break;
}
}
handleAMFCommandMessage(streamID, commandMessage) {
// console.log('handleAMFCommandMessage:', commandMessage);
switch (commandMessage.cmd) {
case 'connect':
this.emit('connect', commandMessage.cmdObj);
break;
case 'createStream':
this.respondCreateStream(commandMessage);
break;
case 'FCPublish':
// this.respondFCPublish();
break;
case 'publish':
this.publishStreamPath = '/' + this.appname + '/' + commandMessage.streamName.split('?')[0];
this.publishArgs = QueryString.parse(commandMessage.streamName.split('?')[1]);
this.publishStreamId = streamID;
// console.log('publish streamID=' + streamID);
this.emit('publish');
break;
case 'play':
this.playStreamPath = '/' + this.appname + '/' + commandMessage.streamName.split('?')[0];
this.playArgs = QueryString.parse(commandMessage.streamName.split('?')[1]);
this.playStreamId = streamID;
// console.log('play streamID=' + streamID);
this.emit('play');
break;
case 'closeStream':
this.emit('closeStream', streamID);
break;
case 'deleteStream':
this.emit('deleteStream', streamID);
break;
case 'pause':
// this.pauseOrUnpauseStream();
break;
case 'releaseStream':
// this.respondReleaseStream();
break;
case 'FCUnpublish':
// this.respondFCUnpublish();
break;
default:
console.warn("[rtmp handleCommandMessage] unknown AMF command: " + commandMessage.cmd);
break;
}
}
handleAudioMessage(rtmpHeader, rtmpBody) {
if (!this.isPublishing) {
return;
}
if (!this.isFirstAudioReceived) {
let sound_format = rtmpBody[0];
let sound_type = sound_format & 0x01;
let sound_size = (sound_format >> 1) & 0x01;
let sound_rate = (sound_format >> 2) & 0x03;
sound_format = (sound_format >> 4) & 0x0f;
this.audioCodec = sound_format;
this.audioCodecName = AUDIO_CODEC_NAME[sound_format];
console.log(`[rtmp handleAudioMessage] Parse AudioTagHeader sound_format=${sound_format} sound_type=${sound_type} sound_size=${sound_size} sound_rate=${sound_rate} codec_name=${this.audioCodecName}`);
if (sound_format == 10) {
//cache aac sequence header
if (rtmpBody[1] == 0) {
this.aacSequenceHeader = Buffer.from(rtmpBody);
this.isFirstAudioReceived = true;
let info = AAC.readAudioSpecificConfig(this.aacSequenceHeader);
this.audioProfileName = AAC.getProfileName(info);
this.audioSamplerate = info.sample_rate;
this.audioChannels = info.channels;
}
} else {
this.isFirstAudioReceived = true;
}
}
// console.log('Audio chunkStreamID='+rtmpHeader.chunkStreamID+' '+rtmpHeader.messageStreamID);
// console.log(`Send Audio message timestamp=${rtmpHeader.timestamp} timestampDelta=${rtmpHeader.timestampDelta} bytesRead=${this.socket.bytesRead}`);
let rtmpMessage = this.createRtmpMessage(rtmpHeader, rtmpBody);
let flvMessage = NodeFlvSession.createFlvMessage(rtmpHeader, rtmpBody);
if (this.rtmpGopCacheQueue != null) {
if (this.aacSequenceHeader != null && rtmpBody[1] == 0) {
//skip aac sequence header
} else {
this.rtmpGopCacheQueue.add(rtmpMessage);
this.flvGopCacheQueue.add(flvMessage);
}
}
for (let playerId of this.players) {
let session = this.sessions.get(playerId);
if (session instanceof NodeRtmpSession) {
rtmpMessage.writeUInt32LE(session.playStreamId, 8);
session.socket.write(rtmpMessage);
} else if (session instanceof NodeFlvSession) {
session.res.write(flvMessage, null, (e) => {
//websocket will throw a error if not set the cb when closed
});
}
}
}
handleVideoMessage(rtmpHeader, rtmpBody) {
if (!this.isPublishing) {
return;
}
let frame_type = rtmpBody[0];
let codec_id = frame_type & 0x0f;
frame_type = (frame_type >> 4) & 0x0f;
if (!this.isFirstVideoReceived) {
this.videoCodec = codec_id;
this.videoCodecName = VIDEO_CODEC_NAME[codec_id];
console.log(`[rtmp handleVideoMessage] Parse VideoTagHeader frame_type=${frame_type} codec_id=${codec_id} codec_name=${this.videoCodecName}`);
if (codec_id == 7 || codec_id == 12) {
//cache avc sequence header
if (frame_type == 1 && rtmpBody[1] == 0) {
this.avcSequenceHeader = Buffer.from(rtmpBody);
this.isFirstVideoReceived = true;
this.rtmpGopCacheQueue = this.gopCacheEnable ? new Set() : null;
this.flvGopCacheQueue = this.gopCacheEnable ? new Set() : null;
}
} else {
this.isFirstVideoReceived = true;
}
}
// console.log('Video chunkStreamID='+rtmpHeader.chunkStreamID+' '+rtmpHeader.messageStreamID);
// console.log(`Send Video message timestamp=${rtmpHeader.timestamp} timestampDelta=${rtmpHeader.timestampDelta} `);
let rtmpMessage = this.createRtmpMessage(rtmpHeader, rtmpBody);
let flvMessage = NodeFlvSession.createFlvMessage(rtmpHeader, rtmpBody);
if ((codec_id == 7 || codec_id == 12) && this.rtmpGopCacheQueue != null) {
if (frame_type == 1 && rtmpBody[1] == 1) {
this.rtmpGopCacheQueue.clear();
this.flvGopCacheQueue.clear();
}
if (frame_type == 1 && rtmpBody[1] == 0) {
//skip avc sequence header
} else {
this.rtmpGopCacheQueue.add(rtmpMessage);
this.flvGopCacheQueue.add(flvMessage);
}
}
for (let playerId of this.players) {
let session = this.sessions.get(playerId);
if (session instanceof NodeRtmpSession) {
rtmpMessage.writeUInt32LE(session.playStreamId, 8);
session.socket.write(rtmpMessage);
} else if (session instanceof NodeFlvSession) {
session.res.write(flvMessage, null, (e) => {
//websocket will throw a error if not set the cb when closed
});
}
}
}
sendACK(size) {
let rtmpBuffer = new Buffer('02000000000004030000000000000000', 'hex');
rtmpBuffer.writeUInt32BE(size, 12);
// //console.log('windowACK: '+rtmpBuffer.hex());
this.socket.write(rtmpBuffer);
}
sendWindowACK(size) {
let rtmpBuffer = new Buffer('02000000000004050000000000000000', 'hex');
rtmpBuffer.writeUInt32BE(size, 12);
// //console.log('windowACK: '+rtmpBuffer.hex());
this.socket.write(rtmpBuffer);
};
setPeerBandwidth(size, type) {
let rtmpBuffer = new Buffer('0200000000000506000000000000000000', 'hex');
rtmpBuffer.writeUInt32BE(size, 12);
rtmpBuffer[16] = type;
// //console.log('setPeerBandwidth: '+rtmpBuffer.hex());
this.socket.write(rtmpBuffer);
};
setChunkSize(size) {
let rtmpBuffer = new Buffer('02000000000004010000000000000000', 'hex');
rtmpBuffer.writeUInt32BE(size, 12);
// //console.log('setChunkSize: '+rtmpBuffer.hex());
this.socket.write(rtmpBuffer);
};
sendStreamStatus(st, id) {
let rtmpBuffer = new Buffer('020000000000060400000000000000000000', 'hex');
rtmpBuffer.writeUInt16BE(st, 12);
rtmpBuffer.writeUInt32BE(id, 14);
this.socket.write(rtmpBuffer);
}
sendRtmpSampleAccess() {
let rtmpHeader = {
chunkStreamID: 5,
timestamp: 0,
messageTypeID: 0x12,
messageStreamID: 1
};
let opt = {
cmd: '|RtmpSampleAccess',
bool1: false,
bool2: false
};
let rtmpBody = AMF.encodeAmf0Data(opt);
let rtmpMessage = this.createRtmpMessage(rtmpHeader, rtmpBody);
this.socket.write(rtmpMessage);
}
sendStatusMessage(id, level, code, description) {
let rtmpHeader = {
chunkStreamID: 5,
timestamp: 0,
messageTypeID: 0x14,
messageStreamID: id
};
let opt = {
cmd: 'onStatus',
transId: 0,
cmdObj: null,
info: {
level: level,
code: code,
description: description
}
};
let rtmpBody = AMF.encodeAmf0Cmd(opt);
let rtmpMessage = this.createRtmpMessage(rtmpHeader, rtmpBody);
this.socket.write(rtmpMessage);
}
pingRequest() {
let currentTimestamp = Date.now() - this.startTimestamp;
let rtmpHeader = {
chunkStreamID: 2,
timestamp: currentTimestamp,
messageTypeID: 0x4,
messageStreamID: 0
};
let rtmpBody = new Buffer([0, 6, (currentTimestamp >> 24) & 0xff, (currentTimestamp >> 16) & 0xff, (currentTimestamp >> 8) & 0xff, currentTimestamp & 0xff])
let rtmpMessage = this.createRtmpMessage(rtmpHeader, rtmpBody);
this.socket.write(rtmpMessage);
// console.log('pingRequest',rtmpMessage.toString('hex'));
}
respondConnect() {
let rtmpHeader = {
chunkStreamID: 3,
timestamp: 0,
messageTypeID: 0x14,
messageStreamID: 0
};
let opt = {
cmd: '_result',
transId: 1,
cmdObj: {
fmsVer: 'FMS/3,0,1,123',
capabilities: 31
},
info: {
level: 'status',
code: 'NetConnection.Connect.Success',
description: 'Connection succeeded.',
objectEncoding: this.objectEncoding
}
};
let rtmpBody = AMF.encodeAmf0Cmd(opt);
let rtmpMessage = this.createRtmpMessage(rtmpHeader, rtmpBody);
this.socket.write(rtmpMessage);
}
respondCreateStream(cmd) {
this.streams++;
let rtmpHeader = {
chunkStreamID: 3,
timestamp: 0,
messageTypeID: 0x14,
messageStreamID: 0
};
let opt = {
cmd: "_result",
transId: cmd.transId,
cmdObj: null,
info: this.streams
};
let rtmpBody = AMF.encodeAmf0Cmd(opt);
let rtmpMessage = this.createRtmpMessage(rtmpHeader, rtmpBody);
this.socket.write(rtmpMessage);
}
respondPlay() {
this.sendStreamStatus(STREAM_BEGIN, this.playStreamId);
this.sendStatusMessage(this.playStreamId, 'status', 'NetStream.Play.Reset', 'Playing and resetting stream.');
this.sendStatusMessage(this.playStreamId, 'status', 'NetStream.Play.Start', 'Started playing stream.');
this.sendRtmpSampleAccess();
}
onConnect(cmdObj) {
cmdObj.app = cmdObj.app.replace('/', '');
this.nodeEvent.emit('preConnect', this.id, cmdObj);
if (!this.isStarting) {
return;
}
this.connectCmdObj = cmdObj;
this.appname = cmdObj.app;
this.objectEncoding = cmdObj.objectEncoding != null ? cmdObj.objectEncoding : 0;
this.sendWindowACK(5000000);
this.setPeerBandwidth(5000000, 2);
this.setChunkSize(this.outChunkSize);
this.respondConnect();
this.startTimestamp = Date.now();
this.connectTime = new Date();
this.pingInterval = setInterval(() => {
this.pingRequest();
}, this.ping);
console.log('[rtmp connect] app: ' + cmdObj.app);
this.nodeEvent.emit('postConnect', this.id, cmdObj);
}
onPublish() {
this.nodeEvent.emit('prePublish', this.id, this.publishStreamPath, this.publishArgs);
if (!this.isStarting) {
return;
}
if (this.config.auth !== undefined && this.config.auth.publish) {
let results = NodeCoreUtils.verifyAuth(this.publishArgs.sign, this.publishStreamPath, this.config.auth.secret);
if (!results) {
console.log(`[rtmp publish] Unauthorized. ID=${this.id} streamPath=${this.publishStreamPath} sign=${this.publishArgs.sign}`);
this.sendStatusMessage(this.publishStreamId, 'error', 'NetStream.publish.Unauthorized', 'Authorization required.');
return;
}
}
if (this.publishers.has(this.publishStreamPath)) {
console.warn("[rtmp publish] Already has a stream path " + this.publishStreamPath);
this.sendStatusMessage(this.publishStreamId, 'error', 'NetStream.Publish.BadName', 'Stream already publishing');
} else if (this.isPublishing) {
console.warn("[rtmp publish] NetConnection is publishing ");
this.sendStatusMessage(this.publishStreamId, 'error', 'NetStream.Publish.BadConnection', 'Connection already publishing');
} else {
console.log("[rtmp publish] new stream path " + this.publishStreamPath + ' streamId:' + this.publishStreamId);
this.publishers.set(this.publishStreamPath, this.id);
this.isPublishing = true;
this.players = new Set();
this.sendStatusMessage(this.publishStreamId, 'status', 'NetStream.Publish.Start', `${this.publishStreamPath} is now published.`);
for (let idlePlayerId of this.idlePlayers) {
let idlePlayer = this.sessions.get(idlePlayerId);
if (idlePlayer.playStreamPath === this.publishStreamPath) {
idlePlayer.emit('play');
this.idlePlayers.delete(idlePlayerId);
}
}
this.nodeEvent.emit('postPublish', this.id, this.publishStreamPath, this.publishArgs);
}
}
onPlay() {
this.nodeEvent.emit('prePlay', this.id, this.playStreamPath, this.playArgs);
if (!this.isStarting) {
return;
}
if (this.config.auth !== undefined && this.config.auth.play) {
let results = NodeCoreUtils.verifyAuth(this.playArgs.sign, this.playStreamPath, this.config.auth.secret);
if (!results) {
console.log(`[rtmp play] Unauthorized. ID=${this.id} streamPath=${this.playStreamPath} sign=${this.playArgs.sign}`);
this.sendStatusMessage(this.playStreamId, 'error', 'NetStream.play.Unauthorized', 'Authorization required.');
return;
}
}
if (this.isPlaying) {
console.warn("[rtmp play] NetConnection is playing");
this.sendStatusMessage(this.playStreamId, 'error', 'NetStream.Play.BadConnection', 'Connection already playing');
} else if (!this.publishers.has(this.playStreamPath)) {
console.log("[rtmp play] stream not found " + this.playStreamPath + ' streamId:' + this.playStreamId);
this.respondPlay();
// this.sendStreamEmpty();
this.isIdling = true;
this.idlePlayers.add(this.id);
} else {
if (this.isIdling) {
this.sendStatusMessage(this.playStreamId, 'status', 'NetStream.Play.PublishNotify', `${this.publishStreamPath} is now published.`);
} else {
this.respondPlay();
}
let publisherPath = this.publishers.get(this.playStreamPath);
let publisher = this.sessions.get(publisherPath);
let players = publisher.players;
this.isPlaying = true;
//metaData
if (publisher.metaData != null) {
let rtmpHeader = {
chunkStreamID: 5,
timestamp: 0,
messageTypeID: 0x12,
messageStreamID: this.playStreamId
};
let metaDataRtmpMessage = this.createRtmpMessage(rtmpHeader, publisher.metaData);
this.socket.write(metaDataRtmpMessage);
}
//send aacSequenceHeader
if (publisher.audioCodec == 10) {
let rtmpHeader = {
chunkStreamID: 4,
timestamp: 0,
messageTypeID: 0x08,
messageStreamID: this.playStreamId
};
let rtmpMessage = this.createRtmpMessage(rtmpHeader, publisher.aacSequenceHeader);
this.socket.write(rtmpMessage);
}
//send avcSequenceHeader
if (publisher.videoCodec == 7 || publisher.videoCodec == 12) {
let rtmpHeader = {
chunkStreamID: 6,
timestamp: 0,
messageTypeID: 0x09,
messageStreamID: this.playStreamId
};
let rtmpMessage = this.createRtmpMessage(rtmpHeader, publisher.avcSequenceHeader);
this.socket.write(rtmpMessage);
}
//send gop cache
if (publisher.rtmpGopCacheQueue != null) {
for (let rtmpMessage of publisher.rtmpGopCacheQueue) {
rtmpMessage.writeUInt32LE(this.playStreamId, 8);
this.socket.write(rtmpMessage);
}
}
if (this.isIdling) {
this.sendStreamStatus(STREAM_READY, this.playStreamId);
this.isIdling = false;
}
console.log("[rtmp play] join stream " + this.playStreamPath + ' streamId:' + this.playStreamId);
players.add(this.id);
this.nodeEvent.emit('postPlay', this.id, this.playStreamPath, this.playArgs);
}
}
onCloseStream(streamID, del) {
if (this.isIdling && this.playStreamId == streamID) {
this.sendStatusMessage(this.playStreamId, 'status', 'NetStream.Play.Stop', 'Stopped playing stream.');
this.idlePlayers.delete(this.id);
this.isIdling = false;
this.playStreamId = del ? 0 : this.playStreamId;
}
if (this.isPlaying && this.playStreamId == streamID) {
this.sendStatusMessage(this.playStreamId, 'status', 'NetStream.Play.Stop', 'Stopped playing stream.');
let publisherPath = this.publishers.get(this.playStreamPath);
if (publisherPath != null) {
this.sessions.get(publisherPath).players.delete(this.id);
}
this.isPlaying = false;
this.playStreamId = del ? 0 : this.playStreamId;
this.nodeEvent.emit('donePlay', this.id, this.playStreamPath, this.playArgs);
}
if (this.isPublishing && this.publishStreamId == streamID) {
this.sendStatusMessage(this.publishStreamId, 'status', 'NetStream.Unpublish.Success', `${this.publishStreamPath} is now unpublished.`);
for (let playerId of this.players) {
let player = this.sessions.get(playerId);
if (player instanceof NodeRtmpSession) {
player.sendStatusMessage(player.playStreamId, 'status', 'NetStream.Play.UnpublishNotify', 'stream is now unpublished.');
} else {
player.stop();
}
}
//let the players to idlePlayers
for (let playerId of this.players) {
let player = this.sessions.get(playerId);
this.idlePlayers.add(playerId);
player.isPlaying = false;
player.isIdling = true;
if (player instanceof NodeRtmpSession) {