forked from ZoneMinder/zoneminder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonitorStream.js
More file actions
1105 lines (1025 loc) · 39.2 KB
/
MonitorStream.js
File metadata and controls
1105 lines (1025 loc) · 39.2 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
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
"use strict";
var janus = null;
const streaming = [];
function MonitorStream(monitorData) {
this.id = monitorData.id;
this.connKey = monitorData.connKey;
this.url = monitorData.url;
this.url_to_zms = monitorData.url_to_zms;
this.width = monitorData.width;
this.height = monitorData.height;
this.RTSP2WebEnabled = monitorData.RTSP2WebEnabled;
this.RTSP2WebType = monitorData.RTSP2WebType;
this.webrtc = null;
this.hls = null;
this.mse = null;
this.wsMSE = null;
this.mseStreamingStarted = false;
this.mseQueue = [];
this.mseSourceBuffer = null;
this.janusEnabled = monitorData.janusEnabled;
this.janusPin = monitorData.janus_pin;
this.server_id = monitorData.server_id;
this.scale = 100;
this.status = {capturefps: 0, analysisfps: 0}; // json object with alarmstatus, fps etc
this.lastAlarmState = STATE_IDLE;
this.statusCmdTimer = null; // timer for requests using ajax to get monitor status
this.statusCmdParms = {
view: 'request',
request: 'status',
connkey: this.connKey
};
this.streamCmdTimer = null; // timer for requests to zms for status
this.streamCmdParms = {
view: 'request',
request: 'stream',
connkey: this.connKey
};
this.ajaxQueue = null;
this.type = monitorData.type;
this.refresh = monitorData.refresh;
this.buttons = {}; // index by name
this.setButton = function(name, element) {
this.buttons[name] = element;
};
this.gridstack = null;
this.setGridStack = function(gs) {
this.gridstack = gs;
};
this.bottomElement = null;
this.setBottomElement = function(e) {
if (!e) {
console.error("Empty bottomElement");
}
this.bottomElement = e;
};
this.img_onerror = function() {
console.log('Image stream has been stopped! stopping streamCmd');
this.streamCmdTimer = clearInterval(this.streamCmdTimer);
};
this.img_onload = function() {
if (!this.streamCmdTimer) {
console.log('Image stream has loaded! starting streamCmd for '+this.connKey+' in '+statusRefreshTimeout + 'ms');
this.streamCmdQuery.bind(this);
this.streamCmdTimer = setInterval(this.streamCmdQuery.bind(this), statusRefreshTimeout);
}
};
this.element = null;
this.getElement = function() {
if (this.element) return this.element;
this.element = document.getElementById('liveStream'+this.id);
if (!this.element) {
console.error("No img for #liveStream"+this.id);
}
return this.element;
};
this.getFrame = function() {
if (this.frame) return this.frame;
this.frame = document.getElementById('imageFeed'+this.id);
if (!this.frame) {
console.error("No frame div for #imageFeed"+this.id);
}
return this.frame;
};
/* if the img element didn't have a src, this would fill it in, causing it to show. */
this.show = function() {
const stream = this.getElement();
if (!stream.src) {
stream.src = this.url_to_zms+"&mode=single&scale="+this.scale+"&connkey="+this.connKey+'&'+auth_relay;
}
};
/* scale should be '0' for auto, or an integer value
* width should be auto, 100%, integer +px
* height should be auto, 100%, integer +px
* param.resizeImg be boolean (added only for using GridStack & PanZoom on Montage page)
* param.scaleImg scaling 1=100% (added only for using PanZoom on Montage & Watch page)
* param.streamQuality in %, numeric value from -50 to +50)
* */
this.setScale = function(newscale, width, height, param = {}) {
const img = this.getElement();
const newscaleSelect = newscale;
if (!img) {
console.log('No img in setScale');
return;
}
// Scale the frame
const monitor_frame = $j('#monitor'+this.id);
if (!monitor_frame) {
console.log('Error finding frame');
return;
}
if (((newscale == '0') || (newscale == 0) || (newscale=='auto')) && (width=='auto' || !width)) {
if (!this.bottomElement) {
if (param.scaleImg) {
newscale = Math.floor(100*monitor_frame.width() / this.width * param.scaleImg);
} else {
newscale = Math.floor(100*monitor_frame.width() / this.width);
}
// We don't want to change the existing css, cuz it might be 59% or 123px or auto;
width = monitor_frame.css('width');
height = Math.round(parseInt(this.height) * newscale / 100)+'px';
} else {
const newSize = scaleToFit(this.width, this.height, $j(img), $j(this.bottomElement), $j('#wrapperMonitor'));
width = newSize.width+'px';
height = newSize.height+'px';
if (param.scaleImg) {
newscale = parseInt(newSize.autoScale * param.scaleImg);
} else {
newscale = parseInt(newSize.autoScale);
}
if (newscale < 25) newscale = 25; // Arbitrary. 4k shown on 1080p screen looks terrible
}
} else if (parseInt(width) || parseInt(height)) {
if (width) {
if (width.search('px') != -1) {
newscale = parseInt(100*parseInt(width)/this.width);
} else { // %
// Set it, then get the calculated width
if (param.resizeImg) {
monitor_frame.css('width', width);
}
newscale = parseInt(100*parseInt(monitor_frame.width())/this.width);
}
} else if (height) {
newscale = parseInt(100*parseInt(height)/this.height);
width = parseInt(this.width * newscale / 100)+'px';
}
} else {
// a numeric scale, must take actual monitor dimensions and calculate
width = Math.round(parseInt(this.width) * newscale / 100)+'px';
height = Math.round(parseInt(this.height) * newscale / 100)+'px';
}
if (width && (width != '0px') && (img.style.width.search('%') == -1)) {
if (param.resizeImg) {
monitor_frame.css('width', parseInt(width));
}
}
if (param.resizeImg) {
if (img.style.width) img.style.width = '100%';
if (height && height != '0px') img.style.height = height;
} else { //This code will not be needed when using GridStack & PanZoom on Montage page. Only required when trying to use "scaleControl"
if (newscaleSelect != 0) {
img.style.width = 'auto';
$j(img).closest('.monitorStream')[0].style.overflow = 'auto';
} else {
//const monitor_stream = $j(img).closest('.monitorStream');
//const realWidth = monitor_stream.attr('data-width');
//const realHeight = monitor_stream.attr('data-height');
//const ratio = realWidth / realHeight;
//const imgWidth = $j(img)[0].offsetWidth + 4; // including border
img.style.width = '100%';
$j(img).closest('.monitorStream')[0].style.overflow = 'hidden';
}
}
let streamQuality = 0;
if (param.streamQuality) {
streamQuality = param.streamQuality;
newscale += parseInt(newscale/100*streamQuality);
}
this.setStreamScale(newscale, streamQuality);
}; // setScale
this.setStreamScale = function(newscale, streamQuality=0) {
const img = this.getElement();
if (!img) {
console.log("No img in setScale");
return;
}
const stream_frame = $j('#monitor'+this.id);
if (!newscale) {
newscale = parseInt(100*parseInt(stream_frame.width())/this.width);
}
if (newscale > 100) newscale = 100; // we never request a larger image, as it just wastes bandwidth
if (newscale < 25 && streamQuality > -1) newscale = 25; // Arbitrary, lower values look bad
if (newscale <= 0) newscale = 100;
this.scale = newscale;
if (this.connKey) {
/* Can just tell it to scale, in fact will happen automatically on next query */
} else {
if (img.nodeName == 'IMG') {
const oldSrc = img.src;
if (!oldSrc) {
console.log('No src on img?!', img);
return;
}
let newSrc = oldSrc.replace(/scale=\d+/i, 'scale='+newscale);
newSrc = newSrc.replace(/auth=\w+/i, 'auth='+auth_hash);
if (newSrc != oldSrc) {
this.streamCmdTimer = clearTimeout(this.streamCmdTimer);
// We know that only the first zms will get the command because the
// second can't open the commandQueue until the first exits
// This is necessary because safari will never close the first image
if (-1 != img.src.search('connkey') && -1 != img.src.search('mode=single')) {
this.streamCommand(CMD_QUIT);
}
console.log("Changing src from " + img.src + " to " + newSrc + 'refresh timeout:' + statusRefreshTimeout);
img.src = '';
img.src = newSrc;
this.streamCmdTimer = setInterval(this.streamCmdQuery.bind(this), statusRefreshTimeout);
}
}
}
}; // setStreamScale
this.start = function() {
if (this.janusEnabled) {
let server;
if (ZM_JANUS_PATH) {
server = ZM_JANUS_PATH;
} else if (this.server_id && Servers[this.server_id]) {
server = Servers[this.server_id].urlToJanus();
} else if (window.location.protocol=='https:') {
// Assume reverse proxy setup for now
server = "https://" + window.location.hostname + "/janus";
} else {
server = "http://" + window.location.hostname + "/janus";
}
if (janus == null) {
Janus.init({debug: "all", callback: function() {
janus = new Janus({server: server}); //new Janus
}});
}
attachVideo(parseInt(this.id), this.janusPin);
this.statusCmdTimer = setInterval(this.statusCmdQuery.bind(this), statusRefreshTimeout);
this.started = true;
return;
}
if (this.RTSP2WebEnabled) {
if (ZM_RTSP2WEB_PATH) {
const videoEl = document.getElementById("liveStream" + this.id);
const url = new URL(ZM_RTSP2WEB_PATH);
const useSSL = (url.protocol == 'https');
const rtsp2webModUrl = url;
rtsp2webModUrl.username = '';
rtsp2webModUrl.password = '';
//.urlParts.length > 1 ? urlParts[1] : urlParts[0]; // drop the username and password for viewing
if (this.RTSP2WebType == 'HLS') {
const hlsUrl = rtsp2webModUrl;
hlsUrl.pathname = "/stream/" + this.id + "/channel/0/hls/live/index.m3u8";
/*
if (useSSL) {
hlsUrl = "https://" + rtsp2webModUrl + "/stream/" + this.id + "/channel/0/hls/live/index.m3u8";
} else {
hlsUrl = "http://" + rtsp2webModUrl + "/stream/" + this.id + "/channel/0/hls/live/index.m3u8";
}
*/
if (Hls.isSupported()) {
this.hls = new Hls();
this.hls.loadSource(hlsUrl.href);
this.hls.attachMedia(videoEl);
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
videoEl.src = hlsUrl.href;
}
} else if (this.RTSP2WebType == 'MSE') {
videoEl.addEventListener('pause', () => {
if (videoEl.currentTime > videoEl.buffered.end(videoEl.buffered.length - 1)) {
videoEl.currentTime = videoEl.buffered.end(videoEl.buffered.length - 1) - 0.1;
videoEl.play();
}
});
const mseUrl = rtsp2webModUrl;
mseUrl.protocol = useSSL ? 'wss' : 'ws';
mseUrl.pathname = "/stream/" + this.id + "/channel/0/mse";
mseUrl.search = "uuid=" + this.id + "&channel=0";
startMsePlay(this, videoEl, mseUrl.href);
} else if (this.RTSP2WebType == 'WebRTC') {
const webrtcUrl = rtsp2webModUrl;
webrtcUrl.pathname = "/stream/" + this.id + "/channel/0/webrtc";
startRTSP2WebPlay(videoEl, webrtcUrl.href, this);
}
this.statusCmdTimer = setInterval(this.statusCmdQuery.bind(this), statusRefreshTimeout);
this.started = true;
return;
} else {
console.log("ZM_RTSP2WEB_PATH is empty. Go to Options->System and set ZM_RTSP2WEB_PATH accordingly.");
}
}
// zms stream
const stream = this.getElement();
if (!stream) return;
if (!stream.src) {
// Website Monitors won't have an img tag, neither will video
console.log('No src for #liveStream'+this.id);
console.log(stream);
return;
}
this.streamCmdTimer = clearTimeout(this.streamCmdTimer);
// Step 1 make sure we are streaming instead of a static image
if (stream.getAttribute('loading') == 'lazy') {
stream.setAttribute('loading', 'eager');
}
let src = stream.src.replace(/mode=single/i, 'mode=jpeg');
src = src.replace(/auth=\w+/i, 'auth='+auth_hash);
if (-1 == src.search('connkey')) {
src += '&connkey='+this.connKey;
}
if (stream.src != src) {
console.log("Setting to streaming: " + src);
stream.src = '';
stream.src = src;
}
stream.onerror = this.img_onerror.bind(this);
stream.onload = this.img_onload.bind(this);
this.started = true;
}; // this.start
this.stop = function() {
if ( 0 ) {
const stream = this.getElement();
if (!stream) return;
const src = stream.src.replace(/mode=jpeg/i, 'mode=single');
if (stream.src != src) {
stream.src = '';
stream.src = src;
}
}
this.streamCommand(CMD_STOP);
this.statusCmdTimer = clearInterval(this.statusCmdTimer);
this.streamCmdTimer = clearInterval(this.streamCmdTimer);
this.started = false;
if (this.webrtc) {
this.webrtc.close();
this.webrtc = null;
} else if (this.hls) {
this.hls.destroy();
this.hls = null;
} else if (this.wsMSE) {
this.mse.endOfStream();
this.wsMSE.close();
this.wsMSE = null;
this.mseStreamingStarted = false;
this.mseSourceBuffer = null;
}
};
this.kill = function() {
if (janus) {
if (streaming[this.id]) {
streaming[this.id].detach();
}
}
const stream = this.getElement();
if (!stream) {
console.log("No element found for monitor "+this.id);
return;
}
stream.onerror = null;
stream.onload = null;
this.stop();
// this.stop tells zms to stop streaming, but the process remains. We need to turn the stream into an image.
if (stream.src) {
const src = stream.src.replace(/mode=jpeg/i, 'mode=single');
if (stream.src != src) {
stream.src = '';
stream.src = src;
}
}
// Because we stopped the zms process above, any remaining ajaxes will fail. But aborting them will also cause them to fail, so why bother?
if (0 && this.ajaxQueue) {
console.log("Aborting in progress ajax for kill");
// Doing this for responsiveness, but we could be aborting something important. Need smarter logic
this.ajaxQueue.abort();
}
};
this.pause = function() {
if (this.element.src) {
this.streamCommand(CMD_PAUSE);
} else {
this.element.pause();
this.statusCmdTimer = clearInterval(this.statusCmdTimer);
}
};
this.play = function() {
if (this.element.src) {
this.streamCommand(CMD_PLAY);
} else {
this.element.play();
this.statusCmdTimer = setInterval(this.statusCmdQuery.bind(this), statusRefreshTimeout);
}
};
this.eventHandler = function(event) {
console.log(event);
};
this.onclick = function(evt) {
console.log('onclick');
};
this.onmove = function(evt) {
console.log('onmove');
};
this.setup_onclick = function(func) {
if (func) {
this.onclick = func;
}
if (this.onclick) {
const el = this.getFrame();
if (!el) return;
el.addEventListener('click', this.onclick, false);
}
};
this.setup_onmove = function(func) {
if (func) {
this.onmove = func;
}
if (this.onmove) {
const el = this.getFrame();
if (!el) return;
el.addEventListener('mousemove', this.onmove, false);
}
};
this.disable_onclick = function() {
const el = this.getElement();
if (!el) return;
el.removeEventListener('click', this.onclick);
};
this.onpause = function() {
console.log('onpause');
};
this.setup_onpause = function(func) {
this.onpause = func;
};
this.onplay = null;
this.setup_onplay = function(func) {
this.onplay = func;
};
this.setStateClass = function(jobj, stateClass) {
if (!jobj) {
console.log("No obj in setStateClass");
return;
}
if (!jobj.hasClass(stateClass)) {
if (stateClass != 'alarm') jobj.removeClass('alarm');
if (stateClass != 'alert') jobj.removeClass('alert');
if (stateClass != 'idle') jobj.removeClass('idle');
jobj.addClass(stateClass);
}
};
this.setAlarmState = function(alarmState) {
let stateClass = '';
if (alarmState == STATE_ALARM) {
stateClass = 'alarm';
} else if (alarmState == STATE_ALERT) {
stateClass = 'alert';
}
const stateValue = $j('#stateValue'+this.id);
if (stateValue.length) {
if (stateValue.text() != stateStrings[alarmState]) {
stateValue.text(stateStrings[alarmState]);
this.setStateClass(stateValue, stateClass);
}
}
const monitorFrame = $j('#monitor'+this.id);
if (monitorFrame.length) this.setStateClass(monitorFrame, stateClass);
const isAlarmed = ( alarmState == STATE_ALARM || alarmState == STATE_ALERT );
const wasAlarmed = ( this.lastAlarmState == STATE_ALARM || this.lastAlarmState == STATE_ALERT );
const newAlarm = ( isAlarmed && !wasAlarmed );
const oldAlarm = ( !isAlarmed && wasAlarmed );
if (newAlarm) {
if (ZM_WEB_SOUND_ON_ALARM !== '0') {
console.log('Attempting to play alarm sound');
if (ZM_DIR_SOUNDS != '' && ZM_WEB_ALARM_SOUND != '') {
const sound = new Audio(ZM_DIR_SOUNDS+'/'+ZM_WEB_ALARM_SOUND);
sound.play();
} else {
console.log("You must specify ZM_DIR_SOUNDS and ZM_WEB_ALARM_SOUND as well");
}
}
if (ZM_WEB_POPUP_ON_ALARM) {
window.focus();
}
if (this.onalarm) {
this.onalarm();
}
}
if (oldAlarm) { // done with an event do a refresh
if (this.onalarm) {
this.onalarm();
}
}
this.lastAlarmState = alarmState;
}; // end function setAlarmState( currentAlarmState )
this.onalarm = null;
this.setup_onalarm = function(func) {
this.onalarm = func;
};
this.onFailure = function(jqxhr, textStatus, error) {
// Assuming temporary problem, retry in a bit.
if (error == 'abort') {
console.log('have abort, will trust someone else to start us back up');
} else if (error == 'Unauthorized') {
window.location.reload();
} else {
logAjaxFail(jqxhr, textStatus, error);
}
};
this.getStreamCmdResponse = function(respObj, respText) {
const stream = this.getElement();
if (!stream) return;
//watchdogOk('stream');
//this.streamCmdTimer = clearTimeout(this.streamCmdTimer);
if (respObj.result == 'Ok') {
if (respObj.status) {
const streamStatus = this.status = respObj.status;
if (this.type != 'WebSite') {
const viewingFPSValue = $j('#viewingFPSValue'+this.id);
const captureFPSValue = $j('#captureFPSValue'+this.id);
const analysisFPSValue = $j('#analysisFPSValue'+this.id);
this.status.fps = this.status.fps.toLocaleString(undefined, {minimumFractionDigits: 1, maximumFractionDigits: 2});
if (viewingFPSValue.length && (viewingFPSValue.text != this.status.fps)) {
viewingFPSValue.text(this.status.fps);
}
this.status.analysisfps = this.status.analysisfps.toLocaleString(undefined, {minimumFractionDigits: 1, maximumFractionDigits: 1});
if (analysisFPSValue.length && (analysisFPSValue.text != this.status.analysisfps)) {
analysisFPSValue.text(this.status.analysisfps);
}
this.status.capturefps = this.status.capturefps.toLocaleString(undefined, {minimumFractionDigits: 1, maximumFractionDigits: 1});
if (captureFPSValue.length && (captureFPSValue.text != this.status.capturefps)) {
captureFPSValue.text(this.status.capturefps);
}
const levelValue = $j('#levelValue');
if (levelValue.length) {
levelValue.text(this.status.level);
let newClass = 'ok';
if (this.status.level > 95) {
newClass = 'alarm';
} else if (this.status.level > 80) {
newClass = 'alert';
}
levelValue.removeClass();
levelValue.addClass(newClass);
}
if (this.status.score) {
}
const delayString = secsToTime(this.status.delay);
if (this.status.paused == true) {
$j('#modeValue'+this.id).text('Paused');
$j('#rate'+this.id).addClass('hidden');
$j('#delayValue'+this.id).text(delayString);
$j('#delay'+this.id).removeClass('hidden');
$j('#level'+this.id).removeClass('hidden');
this.onpause();
} else if (this.status.delayed == true) {
$j('#modeValue'+this.id).text('Replay');
$j('#rateValue'+this.id).text(this.status.rate);
$j('#rate'+this.id).removeClass('hidden');
$j('#delayValue'+this.id).text(delayString);
$j('#delay'+this.id).removeClass('hidden');
$j('#level'+this.id).removeClass('hidden');
if (this.status.rate == 1) {
if (this.onplay) this.onplay();
} else if (this.status.rate > 0) {
if (this.status.rate < 1) {
streamCmdSlowFwd(false);
} else {
streamCmdFastFwd(false);
}
} else {
if (this.status.rate > -1) {
streamCmdSlowRev(false);
} else {
streamCmdFastRev(false);
}
} // rate
} else {
$j('#modeValue'+this.id).text('Live');
$j('#rate'+this.id).addClass('hidden');
$j('#delay'+this.id).addClass('hidden');
$j('#level'+this.id).addClass('hidden');
if (this.onplay) this.onplay();
} // end if paused or delayed
if ((this.status.scale !== undefined) && (this.status.scale !== undefined) && (this.status.scale != this.scale)) {
if (this.status.scale != 0) {
console.log("Stream not scaled, re-applying", this.scale, this.status.scale);
this.streamCommand({command: CMD_SCALE, scale: this.scale});
}
}
$j('#zoomValue'+this.id).text(this.status.zoom);
if (this.status.zoom == '1.0') {
$j('#zoom'+this.id).addClass('hidden');
}
if ('zoomOutBtn' in this.buttons) {
if (this.status.zoom == '1.0') {
setButtonState('zoomOutBtn', 'unavail');
} else {
setButtonState('zoomOutBtn', 'inactive');
}
}
} // end if compact montage
this.setAlarmState(this.status.state);
if (canEdit.Monitors) {
if ('enableAlarmButton' in this.buttons) {
if (streamStatus.analysing == ANALYSING_NONE) {
// Not doing analysis, so enable/disable button should be grey
if (!this.buttons.enableAlarmButton.hasClass('disabled')) {
this.buttons.enableAlarmButton.addClass('disabled');
this.buttons.enableAlarmButton.prop('title', disableAlarmsStr);
}
} else {
this.buttons.enableAlarmButton.removeClass('disabled');
this.buttons.enableAlarmButton.prop('title', enableAlarmsStr);
} // end if doing analysis
this.buttons.enableAlarmButton.prop('disabled', false);
} // end if have enableAlarmButton
if ('forceAlarmButton' in this.buttons) {
if (streamStatus.state == STATE_ALARM || streamStatus.state == STATE_ALERT) {
// Ic0n: My thought here is that the non-disabled state should be for killing an alarm
// and the disabled state should be to force an alarm
if (this.buttons.forceAlarmButton.hasClass('disabled')) {
this.buttons.forceAlarmButton.removeClass('disabled');
this.buttons.forceAlarmButton.prop('title', cancelForcedAlarmStr);
}
} else {
if (!this.buttons.forceAlarmButton.hasClass('disabled')) {
// Looks disabled
this.buttons.forceAlarmButton.addClass('disabled');
this.buttons.forceAlarmButton.prop('title', forceAlarmStr);
}
}
this.buttons.forceAlarmButton.prop('disabled', false);
}
} // end if canEdit.Monitors
if (this.status.auth) {
if (this.status.auth != auth_hash) {
// Don't reload the stream because it causes annoying flickering. Wait until the stream breaks.
console.log("Changed auth from " + auth_hash + " to " + this.status.auth);
auth_hash = this.status.auth;
auth_relay = this.status.auth_relay;
}
} // end if have a new auth hash
} // end if has state
} else {
if (!this.started) return;
console.error(respObj.message);
// Try to reload the image stream.
if (stream.src) {
console.log('Reloading stream: ' + stream.src);
let src = stream.src.replace(/rand=\d+/i, 'rand='+Math.floor((Math.random() * 1000000) ));
src = src.replace(/auth=\w+/i, 'auth='+auth_hash);
// Maybe updated auth
if (src != stream.src) {
stream.src = '';
stream.src = src;
} else {
console.log("Failed to update rand on stream src");
}
}
} // end if Ok or not
};
/* getStatusCmd is used when not streaming, since there is no persistent zms */
this.getStatusCmdResponse=function(respObj, respText) {
//watchdogOk('status');
if (respObj.result == 'Ok') {
const captureFPSValue = $j('#captureFPSValue'+this.id);
const analysisFPSValue = $j('#analysisFPSValue'+this.id);
const viewingFPSValue = $j('#viewingFPSValue'+this.id);
const monitor = respObj.monitor;
if (monitor.FrameRate) {
const fpses = monitor.FrameRate.split(',');
fpses.forEach(function(fps) {
const name_values = fps.split(':');
const name = name_values[0].trim();
const value = name_values[1].trim().toLocaleString(undefined, {minimumFractionDigits: 1, maximumFractionDigits: 2});
if (name == 'analysis') {
this.status.analysisfps = value;
if (analysisFPSValue.length && (analysisFPSValue.text() != value)) {
analysisFPSValue.text(value);
}
} else if (name == 'capture') {
if (captureFPSValue.length && (captureFPSValue.text() != value)) {
captureFPSValue.text(value);
}
} else {
console.log("Unknown fps name " + name);
}
});
} else {
if (analysisFPSValue.length && (analysisFPSValue.text() != monitor.AnalysisFPS)) {
analysisFPSValue.text(monitor.AnalysisFPS);
}
if (captureFPSValue.length && (captureFPSValue.text() != monitor.CaptureFPS)) {
captureFPSValue.text(monitor.CaptureFPS);
}
if (viewingFPSValue.length && viewingFPSValue.text() == '') {
$j('#viewingFPS'+this.id).hide();
}
}
if (canEdit.Monitors) {
if ('enableAlarmButton' in this.buttons) {
if (monitor.Analysing == 'None') {
// Not doing analysis, so enable/disable button should be grey
if (!this.buttons.enableAlarmButton.hasClass('disabled')) {
this.buttons.enableAlarmButton.addClass('disabled');
this.buttons.enableAlarmButton.prop('title', disableAlarmsStr);
}
} else {
this.buttons.enableAlarmButton.removeClass('disabled');
this.buttons.enableAlarmButton.prop('title', enableAlarmsStr);
} // end if doing analysis
this.buttons.enableAlarmButton.prop('disabled', false);
} // end if have enableAlarmButton
if ('forceAlarmButton' in this.buttons) {
if (monitor.Status == STATE_ALARM || monitor.Status == STATE_ALERT) {
// Ic0n: My thought here is that the non-disabled state should be for killing an alarm
// and the disabled state should be to force an alarm
if (this.buttons.forceAlarmButton.hasClass('disabled')) {
this.buttons.forceAlarmButton.removeClass('disabled');
this.buttons.forceAlarmButton.prop('title', cancelForcedAlarmStr);
}
} else {
if (!this.buttons.forceAlarmButton.hasClass('disabled')) {
// Looks disabled
this.buttons.forceAlarmButton.addClass('disabled');
this.buttons.forceAlarmButton.prop('title', forceAlarmStr);
}
}
this.buttons.forceAlarmButton.prop('disabled', false);
}
} // end if canEdit.Monitors
this.setAlarmState(monitor.Status);
if (respObj.auth_hash) {
if (auth_hash != respObj.auth_hash) {
// Don't reload the stream because it causes annoying flickering. Wait until the stream breaks.
console.log("Changed auth from " + auth_hash + " to " + respObj.auth_hash);
auth_hash = respObj.auth_hash;
auth_relay = respObj.auth_relay;
}
} // end if have a new auth hash
} else {
checkStreamForErrors('getStatusCmdResponse', respObj);
}
}; // this.getStatusCmdResponse
this.statusCmdQuery = function() {
$j.getJSON(this.url + '?view=request&request=status&entity=monitor&element[]=Status&element[]=CaptureFPS&element[]=AnalysisFPS&element[]=Analysing&element[]=Recording&id='+this.id+'&'+auth_relay)
.done(this.getStatusCmdResponse.bind(this))
.fail(logAjaxFail);
};
this.statusQuery = function() {
this.streamCommand(CMD_QUERY);
};
this.streamCmdQuery = function(resent) {
if (this.type != 'WebSite') {
// Websites don't have streaming
// Can't use streamCommand because it aborts
this.streamCmdParms.command = CMD_QUERY;
this.streamCmdReq(this.streamCmdParms);
}
};
this.streamCommand = function(command) {
const params = Object.assign({}, this.streamCmdParms);
if (typeof(command) == 'object') {
for (const key in command) params[key] = command[key];
} else {
params.command = command;
}
/*
if (this.ajaxQueue) {
this.ajaxQueue.abort();
}
*/
this.streamCmdReq(params);
};
this.alarmCommand = function(command) {
if (this.ajaxQueue) {
console.log('Aborting in progress ajax for alarm', this.ajaxQueue);
// Doing this for responsiveness, but we could be aborting something important. Need smarter logic
this.ajaxQueue.abort();
}
const alarmCmdParms = Object.assign({}, this.streamCmdParms);
alarmCmdParms.request = 'alarm';
alarmCmdParms.command = command;
alarmCmdParms.id = this.id;
this.ajaxQueue = jQuery.ajaxQueue({
url: this.url + (auth_relay?'?'+auth_relay:''),
xhrFields: {withCredentials: true},
data: alarmCmdParms,
dataType: 'json'
})
.done(this.getStreamCmdResponse.bind(this))
.fail(this.onFailure.bind(this));
};
if (this.type != 'WebSite') {
$j.ajaxSetup({timeout: AJAX_TIMEOUT});
this.streamCmdReq = function(streamCmdParms) {
this.ajaxQueue = jQuery.ajaxQueue({
url: this.url + (auth_relay?'?'+auth_relay:''),
xhrFields: {withCredentials: true},
data: streamCmdParms,
dataType: 'json'
})
.done(this.getStreamCmdResponse.bind(this))
.fail(this.onFailure.bind(this));
};
}
this.analyse_frames = true;
this.show_analyse_frames = function(toggle) {
const streamImage = this.getElement();
if (streamImage.nodeName == 'IMG') {
this.analyse_frames = toggle;
this.streamCmdParms.command = this.analyse_frames ? CMD_ANALYZE_ON : CMD_ANALYZE_OFF;
this.streamCmdReq(this.streamCmdParms);
} else {
console.log("Not streaming from zms, can't show analysis frames");
}
};
this.setMaxFPS = function(maxfps) {
if (1) {
this.streamCommand({command: CMD_MAXFPS, maxfps: maxfps});
} else {
var streamImage = this.getElement();
const oldsrc = streamImage.attr('src');
streamImage.attr('src', ''); // stop streaming
if (maxfps == '0') {
// Unlimited
streamImage.attr('src', oldsrc.replace(/maxfps=\d+/i, 'maxfps=0.00100'));
} else {
streamImage.attr('src', oldsrc.replace(/maxfps=\d+/i, 'maxfps='+newvalue));
}
}
}; // end setMaxFPS
} // end function MonitorStream
async function attachVideo(id, pin) {
await waitUntil(() => janus.isConnected() );
janus.attach({
plugin: "janus.plugin.streaming",
opaqueId: "streamingtest-"+Janus.randomString(12),
success: function(pluginHandle) {
streaming[id] = pluginHandle;
const body = {"request": "watch", "id": id, "pin": pin};
streaming[id].send({"message": body});
},
error: function(error) {
Janus.error(" -- Error attaching plugin... ", error);
},
onmessage: function(msg, jsep) {
Janus.debug(" ::: Got a message :::");
Janus.debug(msg);
var result = msg["result"];
if (result !== null && result !== undefined) {
if (result["status"] !== undefined && result["status"] !== null) {
var status = result["status"];
Janus.debug(status);
}
} else if (msg["error"] !== undefined && msg["error"] !== null) {
return;
}
if (jsep !== undefined && jsep !== null) {
Janus.debug("Handling SDP as well...");
Janus.debug(jsep);
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
if (jsep["sdp"].includes("420029")) {
jsep["sdp"] = jsep["sdp"].replace("420029", "42e01f");
} else if (jsep["sdp"].includes("4d002a")) {
jsep["sdp"] = jsep["sdp"].replace("4d002a", "4de02a");
}
}
// Offer from the plugin, let's answer
streaming[id].createAnswer({
jsep: jsep,
// We want recvonly audio/video and, if negotiated, datachannels
media: {audioSend: false, videoSend: false, data: true},
success: function(jsep) {
Janus.debug("Got SDP!");
Janus.debug(jsep);
var body = {"request": "start"};
streaming[id].send({"message": body, "jsep": jsep});
},
error: function(error) {
Janus.error("WebRTC error:", error);
}
});
}
}, //onmessage function
onremotestream: function(ourstream) {
Janus.debug(" ::: Got a remote stream :::");
Janus.debug(ourstream);
Janus.attachMediaStream(document.getElementById("liveStream" + id), ourstream);
},
onremotetrack: function(track, mid, on) {
Janus.debug(" ::: Got a remote track :::");
Janus.debug(track);
if (track.kind ==="audio") {
stream = new MediaStream();
stream.addTrack(track.clone());
if (document.getElementById("liveAudio" + id) == null) {
audioElement = document.createElement('audio');
audioElement.setAttribute("id", "liveAudio" + id);
audioElement.controls = true;
document.getElementById("imageFeed" + id).append(audioElement);
}
Janus.attachMediaStream(document.getElementById("liveAudio" + id), stream);
} else {
stream = new MediaStream();
stream.addTrack(track.clone());
Janus.attachMediaStream(document.getElementById("liveStream" + id), stream);
}
}
}); // janus.attach
} //function attachVideo
const waitUntil = (condition) => {
return new Promise((resolve) => {
const interval = setInterval(() => {
if (!condition()) {
return;
}
clearInterval(interval);
resolve();
}, 100);
});
};
function startRTSP2WebPlay(videoEl, url, stream) {
stream.webrtc = new RTCPeerConnection({
iceServers: [{