-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathGB28181Session.js
1027 lines (838 loc) · 36.5 KB
/
GB28181Session.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
const xml2js = require('xml2js');
const SIP = require('./sip/sip');
const SDP = require('./sdp/parser');
const Logger = require('./core/logger');
const context = require('./core/ctx');
class NodeSipSession {
constructor(config, userid, remote, uas) {
this.request = remote.request;
this.protocol = remote.info.protocol;
this.id = userid;
//注册请求
if (this.request && this.request.headers) {
if (this.request.headers.via[0])
this.via = this.request.headers.via[0];
//过期时间
if (this.request.headers.expires)
this.expires = this.request.headers.expires;
}
//主机通讯地址&端口
this.host = remote.info.address;
this.port = remote.info.port;
//设备目录
this.catalog = { devicelist: [] };
this.sn = 0;
this.callbacks = {};
this.dialogs = {};
//当前域
this.GBDomain = config.GB28181.sipServer.realm || '3402000000';
//SIP服务通讯端口
this.GBServerPort = config.GB28181.sipServer.mapPort || config.GB28181.sipServer.listen;
//SIP服务主机地址
this.GBserverHost = config.GB28181.sipServer.mapHost || config.GB28181.sipServer.host;
//SIP服务编号
this.GBServerId = config.GB28181.sipServer.serial || config.GB28181.streamServer.serial;
//超时
this.pingTime = config.GB28181.sipServer.ping ? config.GB28181.sipServer.ping * 1000 : 60000;
//重试次数
this.pingTimeout = config.GB28181.sipServer.ping_timeout || 3;
//最后一个保活包接收时间
this.startTimestamp = Date.now();
//丢包统计,连接3次丢包,表示对象下线
this.lostPacketCount = 0;
this.pingInterval = null;
this.uas = uas;
this.TAG = 'sip';
context.sessions.set(this.id, this);
}
//启动
async run() {
this.pingInterval = setInterval(() => {
let timevalue = Date.now() - this.startTimestamp;
if (timevalue > this.pingTime) {
this.lostPacketCount++;
if (this.lostPacketCount > this.pingTimeout) {
this.stop();
context.nodeEvent.emit('offline', this.id);
}
}
}, this.pingTime);
this.isStarting = true;
Logger.log(`[${this.id}] New Device Connected ip=${this.via.host} port=${this.via.port} `);
context.nodeEvent.emit('online', this.id);
//获取设备基本信息
this.deviceinfo = await this.getDeviceInfo();
//获取设备状态
this.devicestatus = await this.getDeviceStatus();
//获取设备目录
this.catalog = await this.getCatalog();
}
// 停止
stop() {
if (this.isStarting) {
if (this.pingInterval != null) {
clearInterval(this.pingInterval);
this.pingInterval = null;
}
this.isStarting = false;
context.sessions.delete(this.id);
}
}
//将XML转JSON
parseXml(xml) {
let json = {};
xml2js.parseString(xml, { explicitArray: false, ignoreAttrs: true }, (err, result) => {
json = result;
});
return json;
}
//获取设备基础信息
async getDeviceInfo() {
return await this.QueryDeviceInfo();
}
//获取设备目录
async getCatalog() {
return await this.QueryDeviceCatalog();
}
//获取设备状态信息
async getDeviceStatus() {
return await this.QueryDeviceStatus();
}
//录像文件查询
async getRecordInfos(channelId, begin, end) {
return await this.QueryRecordInfo(channelId, begin, end);
}
//云台控制
ControlPTZ(channelId, ptzvalue) {
this.Control(channelId, 'PTZCmd', ptzvalue);
}
//重启
ControlBoot() {
this.Control(this.id, 'TeleBoot', 'Boot',);
}
//设备信息
QueryDeviceInfo() {
return new Promise((resolve, reject) => {
let deviceinfo = {};
this.Query('DeviceInfo', (content) => {
if (content.Result === 'OK') {
switch (content.CmdType) {
case 'DeviceInfo':
deviceinfo = { manufacturer: content.Manufacturer, model: content.Model, firmware: content.Firmware, name: content.DeviceName };
break;
}
}
else {
deviceinfo = { result: false, message: content.Result, errorcode: content.ErrorCode };
}
resolve(deviceinfo);
return true;
});
});
}
//设备目录
QueryDeviceCatalog() {
return new Promise((resolve, reject) => {
let catalog = { total: 0, devicelist: [] };
this.Query('Catalog', (content) => {
if (content.Result) {
catalog = { result: false, message: content.Result, errorcode: content.ErrorCode };
}
else {
switch (content.CmdType) {
case 'Catalog':
{
if (content.SumNum)
catalog.total = Number(content.SumNum);
if (content.DeviceList) {
if (catalog.total > 1) {
content.DeviceList.Item.forEach(device => {
catalog.devicelist.push(device);
});
}
else {
catalog.devicelist.push(content.DeviceList.Item);
}
}
}
break;
}
}
if (catalog.total != catalog.devicelist.length)
return false;
resolve(catalog);
return true;
});
});
}
//设备状态
QueryDeviceStatus() {
return new Promise((resolve, reject) => {
let devicestatus = {};
this.Query('DeviceStatus', (content) => {
if (content.Result === 'OK') {
switch (content.CmdType) {
case 'DeviceStatus'://设备状态
devicestatus = { online: content.Online, status: content.Status, encode: content.Encode, record: content.Record, devicetime: content.DeviceTime };
break;
}
}
else {
devicestatus = { result: false, message: content.Result, errorcode: content.ErrorCode };
}
resolve(devicestatus);
return true;
});
});
}
//录像文件查询
QueryRecordInfo(channelId, startTime, endTime) {
return new Promise((resolve, reject) => {
let recordinfos = { total: 0, recordlist: [] };
this.sendQueryRecordInfoMessage(channelId, startTime, endTime, (content) => {
switch (content.CmdType) {
case 'RecordInfo'://设备状态
{
if (content.SumNum)
recordinfos.total = Number(content.SumNum);
if (content.RecordList) {
if (recordinfos.total > 0) {
content.RecordList.Item.forEach(record => {
recordinfos.recordlist.push(record);
});
}
}
}
break;
}
if (recordinfos.total != recordinfos.recordlist.length) {
return false;
}
resolve(recordinfos);
return true;
});
});
}
//控制 channelId 设备通道国标编码
Control(channelId, cmdtype, cmdvalue, callback) {
//PTZCmd/TeleBoot
let json = {
Control: {
CmdType: 'DeviceControl',
SN: this.sn++,
DeviceID: channelId
}
};
switch (cmdtype) {
case 'PTZCmd':
{
let cmd = Buffer.alloc(8);
cmd[0] = 0xA5;//首字节以05H开头
cmd[1] = 0x0F;//组合码,高4位为版本信息v1.0,版本信息0H,低四位为校验码
// 校验码 = (cmd[0]的高4位+cmd[0]的低4位+cmd[1]的高4位)%16
cmd[2] = 0x01;
let ptzSpeed = 0x5f; //默认速度
switch (Number(cmdvalue)) {
//停止
case 0:
cmd[3] = 0x00;
break;
//向右
case 1:
cmd[3] = 0x01;
cmd[4] = ptzSpeed;
break;
//向左
case 2:
cmd[3] = 0x02;
cmd[4] = ptzSpeed;
break;
//向下
case 3:
cmd[3] = 0x04;
cmd[5] = ptzSpeed;
break;
//向上
case 4:
cmd[3] = 0x08;
cmd[5] = ptzSpeed;
break;
//放大
case 5:
cmd[3] = 0x10;
cmd[6] = 0x10;
break;
//缩小
case 6:
cmd[3] = 0x20;
cmd[6] = 0x10;
break;
//组合
case 7:
cmd[3] = 0x29;
break;
}
cmd[7] = (cmd[0] + cmd[1] + cmd[2] + cmd[3] + cmd[4] + cmd[5] + cmd[6]) % 256;
json.Control.PTZCmd = this.Bytes2HexString(cmd);
}
break;
case 'TeleBoot':
json.Control.TeleBoot = cmdvalue;
break;
}
let id = [json.Control.CmdType, json.Control.SN].join(':');
if (!this.callbacks[id])
this.callbacks[id] = callback;
//JSON 转XML
let builder = new xml2js.Builder();
let content = builder.buildObject(json);
let options = {
method: 'MESSAGE',
contentType: 'application/MANSCDP+xml',
content: content
};
this.send(options);
}
//字节转字符串
Bytes2HexString(b) {
let hexs = "";
for (let i = 0; i < b.length; i++) {
let hex = (b[i]).toString(16);
if (hex.length === 1) {
hex = '0' + hex;
}
hexs += hex.toUpperCase();
}
return hexs;
}
//查询
Query(cmdtype, callback) {
//DeviceInfo/Catalog/DeviceStatus
let json = {
Query: {
CmdType: cmdtype,
SN: this.sn++,
DeviceID: this.id
}
};
let id = [json.Query.CmdType, json.Query.SN].join(':');
if (!this.callbacks[id])
this.callbacks[id] = callback;
//JSON 转XML
let builder = new xml2js.Builder();
let content = builder.buildObject(json);
let options = {
method: 'MESSAGE',
contentType: 'application/MANSCDP+xml',
content: content
};
this.send(options);
}
//发送查询通道录像文件请求
sendQueryRecordInfoMessage(channelId, startTime, endTime, callback) {
let json = {
Query: {
CmdType: 'RecordInfo',
SN: this.sn++,
DeviceID: channelId,
StartTime: startTime,
EndTime: endTime,
Secrecy: 0, //保密属性 0:不保密 1:涉密
Type: 'all', //录像产生类型 time/alarm/manual/all
IndistinctQuery: 0//字段代表模糊查询,缺省为 0。 值为 0 时:不进行模糊查询。此时根据 SIP 消息中 To 头域 URI 中的 ID 值确定查询录像位置,若 ID 值为本域系统 ID 则进行中心历史记录检索,若为前端设备 ID 则进行前端设备历史记录检
}
};
let id = [json.Query.CmdType, json.Query.SN].join(':');
if (!this.callbacks[id])
this.callbacks[id] = callback;
//JSON 转XML
let builder = new xml2js.Builder();
let content = builder.buildObject(json);
let options = {
id: channelId,
method: 'MESSAGE',
contentType: 'application/MANSCDP+xml',
content: content
};
this.send(options);
}
//下载
Download() {
}
//补位0
_prefixInteger(num, m) {
return (Array(m).join(0) + num).slice(-m);
}
//回放 begin-开始时间 end-结束时间 channelId-设备通道国标编码
sendPlaybackMessage(channelId, begin, end, nhost, nport, mode) {
return new Promise((resolve, reject) => {
let isFinded = false;
let findssrc = '';
let result = { result: true, message: 'OK' };
for (var key in this.dialogs) {
let session = this.dialogs[key];
if (session.bye && session.port === rport && session.host === rhost && session.channelId === channelId && session.play === 'playback' && session.begin == begin && session.end == end) {
isFinded = true;
findssrc = session.ssrc;
break;
}
}
if (isFinded) {
result.data = { ssrc: findssrc };
resolve(result);
return;
}
//0: udp,1:tcp/passive ,2:tcp/active
let selectMode = mode || 0;
//产生1-9999随机数
let random = Math.floor(Math.random() * 9999);
let streamId = this._prefixInteger(random, 4);
//回看以1开头,同一个通道编码可能存在许多不同时间段的请求,所以ssrc后四位要处理一下,不能用通道编码了
let ssrc = "1" + channelId.substring(3, 8) + streamId;
let host = nhost || "127.0.0.1";
let port = nport || 9200;
let sdpV = "";
let mValue = "RTP/AVP"
switch (Number(selectMode)) {
default:
break;
case 1:
sdpV = `a=setup:passive\r\n` +
`a=connection:new\r\n`;
mValue = "TCP/RTP/AVP";
break;
case 2:
sdpV = `a=setup:active\r\n` +
`a=connection:new\r\n`;
mValue = "TCP/RTP/AVP";
break;
}
let content = `v=0\r\n` +
`o=${this.GBServerId} 0 0 IN IP4 ${host}\r\n` +
`s=Playback\r\n` +
`u=${channelId}:0\r\n` +
`c=IN IP4 ${host}\r\n` +
`t=${begin} ${end}\r\n` +
`m=video ${port} ${mValue} 96\r\n` +
`a=rtpmap:96 PS/90000\r\n` +
`a=recvonly\r\n` +
sdpV +
`y=${ssrc}\r\n` +
`f=v/2/4///a///\r\n`;
let that = this;
let options = {
id: channelId,
subject: `${channelId}:${ssrc},${this.GBServerId}:0`,
method: 'INVITE',
contentType: 'application/sdp',
content: content,
callback: function (response) {
if (response.status >= 300) {
//错误信息
Logger.error(`[${that.TAG}] id=${that.id} ssrc=${ssrc} status=${response.status}`);
result.result = false;
result.data = { status: response.status };
result.message = `ErrorCode=${response.status}`;
resolve(result);
}
else if (response.status < 200) {
Logger.log(`[${that.TAG}] id=${that.id} ssrc=${ssrc} status=${response.status}`);
}
else {
//判断消息类型
switch (options.method) {
case 'INVITE':
//SDP
if (response.content) {
// 响应消息体
let sdp = SDP.parse(response.content);
Logger.log(`[${that.TAG}] id=${that.id} ssrc=${ssrc} sdp=${sdp}`);
//Step 6 SIP服务器收到媒体流发送者返回的200OK响应后,向 媒体服务器 发送 ACK请求,请求中携带 消息5中媒体流发送者回复的200 ok响应消息体,完成与媒体服务器的invite会话建立过程
context.nodeEvent.emit('sdpReceived', sdp);
//Step 7 SIP服务器收到媒体流发送者返回200 OK响应后,向 媒体流发送者 发送 ACK请求,请求中不携带消息体,完成与媒体流发送者的invite会话建立过程
that.uas.send({
method: 'ACK',
uri: response.headers.contact[0].uri,
headers: {
to: response.headers.to,
from: response.headers.from,
'call-id': response.headers['call-id'],
cseq: { method: 'ACK', seq: response.headers.cseq.seq }
}
});
//会话标识
let key = [response.headers['call-id'], response.headers.from.params.tag, response.headers.to.params.tag].join(':');
//创建会话
if (!that.dialogs[key]) {
// 断开会话请求
let byeRequest = {
method: 'BYE',
uri: response.headers.contact[0].uri,
headers: {
to: response.headers.to,
from: response.headers.from,
'call-id': response.headers['call-id'],
cseq: { method: 'BYE', seq: response.headers.cseq.seq++ }//需额外加1
}
}
that.dialogs[key] = { channelId: channelId, ssrc: ssrc, host: host, port: port, begin: begin, end: end, bye: byeRequest, play: 'playback' };
}
result.data = { ssrc: ssrc };
resolve(result);
}
break;
}
}
}
};
this.send(options);
});
}
//回看播放控制
sendPlayControlMessage(channelId, begin, end, cmd, value) {
return new Promise((resolve, reject) => {
let result = { result: false, message: 'OK' };
//PLAY/PAUSE/TEARDOWN
//播放速度,其中 1 为正常
let scale = ['0.25', '0.5', '1.0', '2.0', '4.0', '-0.25', '-0.5', '-1.0', '-2.0', '-4.0'];
//播放/倍速播放/暂停/停止
let method = ['PLAY', 'PLAY', 'PAUSE', 'TEARDOWN'];
let findSession = null;
for (var key in this.dialogs) {
let session = this.dialogs[key];
if (session.bye && session.channelId === channelId && session.play === 'playback' && session.begin == begin && session.end == end) {
findSession = session;
break;
}
}
if (findSession == null) {
result.message = 'dialog not found.';
resolve(result);
return;
}
//引用参数
var request = SIP.copyMessage(findSession.bye);
let findssrc = findSession.ssrc;
if (!findSession.cseq) {
findSession.cseq = 1;
}
else {
findSession.cseq++;
}
if (request && findssrc) {
request.method = 'INFO';
request['content-type'] = 'application/MANSRTSP';
request.headers.cseq.method = 'Info';
request.headers.cseq.seq = Math.floor(Math.random() * 100);
request.headers.contact = [{ uri: 'sip:' + this.GBServerId + '@' + this.GBserverHost + ':' + this.GBServerPort }];
switch (Number(cmd)) {
//播放/随机播放
case 0:
{
request.content = "PLAY MANSRTSP/1.0\r\n" +
`CSeq:${findSession.cseq}\r\n` +
`Range:npt=${(value || 'now-')}\r\n`;
}
break;
//快进/慢退
case 1:
{
//传参数如果不符合条件,用原速播放
let speed = Number(value);
if (speed < 0 || speed > 9) {
speed = 2;
}
request.content = "PLAY MANSRTSP/1.0\r\n" +
`CSeq:${findSession.cseq}\r\n` +
`Scale:${scale[value]}\r\n`;
}
break;
//暂停(当前位置)
case 2:
{
request.content = "PAUSE MANSRTSP/1.0\r\n" +
`CSeq:${findSession.cseq}\r\n` +
`PauseTime:now\r\n`;
}
break;
//停止
case 3:
{
request.content = "TEARDOWN MANSRTSP/1.0\r\n" +
`CSeq:${findSession.cseq}\r\n`;
}
break;
}
//发送请求
this.uas.send(request, (response) => {
//响应
Logger.log(`[${this.id}] INFO ${method[cmd]} result=${response.status}`);
if (response.status == 200) {
result.result = true;
}
else {
result.message = `ErrorCode=${response.status}`;
result.data = { status: response.status };
}
resolve(result);
});
}
});
}
//预览 channelId 通道国标编码
sendRealPlayMessage(channelId, rhost, rport, mode) {
return new Promise((resolve, reject) => {
let result = { result: true, message: 'OK' };
let isFinded = false;
let findssrc = "";
for (var key in this.dialogs) {
let session = this.dialogs[key];
if (session.bye && session.port === rport && session.host === rhost && session.channelId === channelId && session.play === 'realplay') {
isFinded = true;
findssrc = session.ssrc;
break;
}
}
//己存在会话,同一个流媒体不需要重复请求
if (isFinded) {
result.data = { ssrc: findssrc };
resolve(result);
return;
}
//0: udp,1:tcp/passive ,2:tcp/active
let selectMode = mode || 0;
let ssrc = "0" + channelId.substring(16, 20) + channelId.substring(3, 8);
let host = rhost || "127.0.0.1";
let port = rport || 9200;
let sdpV = "";
let mValue = "RTP/AVP"
switch (Number(selectMode)) {
default:
break;
case 1:
sdpV = `a=setup:passive\r\n` +
`a=connection:new\r\n`;
mValue = "TCP/RTP/AVP";
break;
case 2:
sdpV = `a=setup:active\r\n` +
`a=connection:new\r\n`;
mValue = "TCP/RTP/AVP";
break;
}
//s=Play/Playback/Download/Talk
let content = `v=0\r\n` +
`o=${this.GBServerId} 0 0 IN IP4 ${host}\r\n` +
`s=Play\r\n` +
`c=IN IP4 ${host}\r\n` +
`t=0 0\r\n` +
`m=video ${port} ${mValue} 96\r\n` +
`a=rtpmap:96 PS/90000\r\n` +
`a=recvonly\r\n` +
sdpV +
`y=${ssrc}\r\n` +
`f=v/2/4///a///\r\n`;
let that = this;
let options = {
id: channelId,
subject: `${channelId}:${ssrc},${this.GBServerId}:0`,
method: 'INVITE',
contentType: 'application/sdp',
content: content,
callback: function (response) {
if (response.status >= 300) {
//错误信息
Logger.error(`[${that.id}] ssrc=${ssrc} status=${response.status}`);
result.result = false;
result.data = { status: response.status };
result.message = `ErrorCode=${response.status}`;
resolve(result);
}
else if (response.status < 200) {
Logger.log(`[${that.id} ] ssrc=${ssrc} status=${response.status}`);
}
else {
//判断消息类型
switch (options.method) {
case 'INVITE':
//SDP
if (response.content) {
//响应消息体
let sdp = SDP.parse(response.content);
Logger.log(`[${that.id}] ssrc=${ssrc} sdp=${sdp}`);
//Step 6 SIP服务器收到媒体流发送者返回的200OK响应后,向 媒体服务器 发送 ACK请求,请求中携带 消息5中媒体流发送者回复的200 ok响应消息体,完成与媒体服务器的invite会话建立过程
context.nodeEvent.emit('sdpReceived', sdp);
//Step 7 SIP服务器收到媒体流发送者返回200 OK响应后,向 媒体流发送者 发送 ACK请求,请求中不携带消息体,完成与媒体流发送者的invite会话建立过程
that.uas.send({
method: 'ACK',
uri: response.headers.contact[0].uri,
headers: {
to: response.headers.to,
from: response.headers.from,
'call-id': response.headers['call-id'],
cseq: { method: 'ACK', seq: response.headers.cseq.seq }
}
});
//会话标识
let key = [response.headers['call-id'], response.headers.from.params.tag, response.headers.to.params.tag].join(':');
//创建会话
if (!that.dialogs[key]) {
// 断开会话请求
let byeRequest = {
method: 'BYE',
uri: response.headers.contact[0].uri,
headers: {
to: response.headers.to,
from: response.headers.from,
'call-id': response.headers['call-id'],
cseq: { method: 'BYE', seq: response.headers.cseq.seq++ }//需额外加1
}
}
that.dialogs[key] = { channelId: channelId, ssrc: ssrc, host: host, port: port, bye: byeRequest, play: 'realplay' };
}
result.data = { ssrc: ssrc };
resolve(result);
}
break;
}
}
}
};
this.send(options);
});
}
//停止实时预览
async sendStopRealPlayMessage(channelId, rhost, rport) {
return new Promise((resolve, reject) => {
let result = { result: false, message: 'not find dialog.' };
for (var key in this.dialogs) {
//搜索满足条件的会话
let session = this.dialogs[key];
if (session.bye && session.port === rport && session.host === rhost && session.channelId === channelId && session.play === 'realplay') {
context.nodeEvent.emit('stopPlayed', session.ssrc);
this.uas.send(session.bye, (response) => {
Logger.log(`[${this.id}] StopRealPlay status=${response.status}`);
delete this.dialogs[key];
});
result.result = true;
result.message = 'OK';
}
}
resolve(result);
});
}
//停止录像回看
async sendStopPlayBackMessage(channelId, begin, end, nhost, nport) {
let result = { result: false, message: 'not find dialog.' };
for (var key in this.dialogs) {
//搜索满足条件的会话
let session = this.dialogs[key];
if (session.bye && session.begin == begin && session.end == end && session.port === nport && session.host === nhost && session.channelId === channelId && session.play === 'playback') {
//先发送停止回看命令
result = await this.sendPlayControlMessage(session.channelId, session.begin, session.end, 3);
context.nodeEvent.emit('stopPlayed', session.ssrc);
//发送BYE
this.uas.send(session.bye, (response) => {
Logger.log(`[${this.id}] StopPlayback status=${response.status}`);
delete this.dialogs[key];
});
break;
}
}
return result;
}
//处理 MESSAGE
onMessage(request) {
let content = this.parseXml(request.content);
// 回复
if (content.hasOwnProperty('Response')) {
let id = [content.Response.CmdType, content.Response.SN].join(':');
if (this.callbacks[id]) {
//如果是查询有返回多条消息,还需等待。
let result = this.callbacks[id](content.Response);
if (result)
delete this.callbacks[id];
}
}
// 通知
if (content.hasOwnProperty('Notify')) {
Logger.log(`[${this.id}] Notify CmdType=${content.Notify.CmdType} SN=${content.Notify.SN} length=${request.content.length}`);
switch (content.Notify.CmdType) {
//保活消息
case 'Keepalive':
{
//更新时间
this.startTimestamp = Date.now();
this.lostPacketCount = 0;
}
break;
//媒体播放完成消息
case 'MediaStatus':
{
switch (content.Notify.NotifyType) {
//录像发送完毕
case 121:
{
let key = [request.headers['call-id'], request.headers.from.params.tag, request.headers.to.params.tag].join(':');
if (this.dialogs[key]) {
let session = this.dialogs[key];
if (session.bye && content.Notify.DeviceID == session.channelId) {
this.uas.send(session.bye, (reqponse) => {
if (reqponse.status == 200 || reqponse.status == 481)
delete this.dialogs[key];
});
}
}
}
break;
}
}
break;
}
}
}
//发送SIP消息
send(options) {
//设备国标编码+设备主机地址+通讯端口
let uri = 'sip:' + (options.id || this.id) + '@' + this.host + ':' + this.port;
let request = {
method: options.method,
uri: uri,
headers: {
to: { uri: 'sip:' + (options.id || this.id) + '@' + this.GBDomain },
from: { uri: 'sip:' + this.GBServerId + '@' + this.GBDomain, params: { tag: options.tag || this.getTagRandom(8) } },
'call-id': this.getCallId(),
cseq: { method: options.method, seq: Math.floor(Math.random() * 1e5) },
'content-type': options.contentType,
subject: options.subject,
contact: [{ uri: 'sip:' + this.GBServerId + '@' + this.GBserverHost + ':' + this.GBServerPort }]
},
content: options.content
}
this.uas.send(request, options.callback);
}
//
getSN() {