2022-01-15 13:14:43 +08:00
|
|
|
var janus = null;
|
|
|
|
var streaming = [];
|
2020-09-01 06:30:05 +08:00
|
|
|
|
|
|
|
function MonitorStream(monitorData) {
|
|
|
|
this.id = monitorData.id;
|
|
|
|
this.connKey = monitorData.connKey;
|
2022-01-09 01:14:28 +08:00
|
|
|
this.auth_relay = auth_relay;
|
|
|
|
this.auth_hash = auth_hash;
|
2020-09-01 06:30:05 +08:00
|
|
|
this.url = monitorData.url;
|
2021-11-11 03:20:19 +08:00
|
|
|
this.url_to_zms = monitorData.url_to_zms;
|
2020-09-01 06:30:05 +08:00
|
|
|
this.width = monitorData.width;
|
|
|
|
this.height = monitorData.height;
|
2022-01-15 13:14:43 +08:00
|
|
|
this.janusEnabled = monitorData.janusEnabled;
|
2021-11-11 03:20:19 +08:00
|
|
|
this.scale = 100;
|
2020-09-01 06:30:05 +08:00
|
|
|
this.status = null;
|
|
|
|
this.lastAlarmState = STATE_IDLE;
|
2022-01-21 00:47:28 +08:00
|
|
|
this.streamCmdTimer = null;
|
2021-01-07 05:57:30 +08:00
|
|
|
this.streamCmdParms = {
|
2021-01-07 04:19:25 +08:00
|
|
|
view: 'request',
|
|
|
|
request: 'stream',
|
|
|
|
connkey: this.connKey
|
|
|
|
};
|
2022-01-21 00:47:28 +08:00
|
|
|
this.ajaxQueue = null;
|
2020-09-01 06:30:05 +08:00
|
|
|
this.type = monitorData.type;
|
|
|
|
this.refresh = monitorData.refresh;
|
2022-01-21 00:47:28 +08:00
|
|
|
|
|
|
|
this.buttons = {}; // index by name
|
|
|
|
this.setButton = function(name, element) {
|
|
|
|
this.buttons.name = element;
|
|
|
|
};
|
|
|
|
|
2021-11-11 03:20:19 +08:00
|
|
|
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);
|
2020-09-03 02:37:48 +08:00
|
|
|
}
|
2021-11-11 03:20:19 +08:00
|
|
|
return this.element;
|
|
|
|
};
|
2022-01-27 10:39:29 +08:00
|
|
|
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;
|
|
|
|
};
|
2021-11-11 03:20:19 +08:00
|
|
|
|
|
|
|
/* 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) {
|
2022-01-21 00:47:28 +08:00
|
|
|
stream.src = this.url_to_zms+"&mode=single&scale=100&connkey="+this.connKey+this.auth_relay;
|
2021-11-11 03:20:19 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.setScale = function(newscale) {
|
|
|
|
const img = this.getElement();
|
|
|
|
if (!img) return;
|
|
|
|
|
|
|
|
this.scale = newscale;
|
|
|
|
|
|
|
|
const oldSrc = img.getAttribute('src');
|
2022-01-19 03:30:10 +08:00
|
|
|
if (!oldSrc) {
|
2022-01-21 00:47:28 +08:00
|
|
|
console.log('No src on img?!');
|
2022-01-19 03:30:10 +08:00
|
|
|
console.log(img);
|
|
|
|
return;
|
|
|
|
}
|
2021-11-11 03:20:19 +08:00
|
|
|
let newSrc = '';
|
|
|
|
|
|
|
|
img.setAttribute('src', '');
|
|
|
|
console.log("Scaling to: " + newscale);
|
|
|
|
|
|
|
|
if (newscale == '0' || newscale == 'auto') {
|
2022-01-21 00:47:28 +08:00
|
|
|
const bottomElement = document.getElementById('monitorState'+this.id);
|
2021-11-11 03:20:19 +08:00
|
|
|
var newSize = scaleToFit(this.width, this.height, $j(img), $j(bottomElement));
|
|
|
|
|
2022-01-21 00:47:28 +08:00
|
|
|
//console.log(newSize);
|
2021-11-11 03:20:19 +08:00
|
|
|
newWidth = newSize.width;
|
|
|
|
newHeight = newSize.height;
|
|
|
|
autoScale = parseInt(newSize.autoScale);
|
|
|
|
// This is so that we don't waste bandwidth and let the browser do all the scaling.
|
|
|
|
if (autoScale > 100) autoScale = 100;
|
|
|
|
if (autoScale) {
|
|
|
|
newSrc = oldSrc.replace(/scale=\d+/i, 'scale='+autoScale);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newWidth = this.width * newscale / SCALE_BASE;
|
|
|
|
newHeight = this.height * newscale / SCALE_BASE;
|
|
|
|
img.width(newWidth);
|
|
|
|
img.height(newHeight);
|
|
|
|
if (newscale > 100) newscale = 100;
|
|
|
|
newSrc = oldSrc.replace(/scale=\d+/i, 'scale='+newscale);
|
2021-03-24 01:53:45 +08:00
|
|
|
}
|
2021-11-11 03:20:19 +08:00
|
|
|
img.setAttribute('src', newSrc);
|
|
|
|
};
|
|
|
|
|
2022-01-21 00:47:28 +08:00
|
|
|
this.start = function(delay) {
|
2022-01-15 13:14:43 +08:00
|
|
|
if (this.janusEnabled) {
|
|
|
|
var id = parseInt(this.id);
|
2022-01-19 03:30:10 +08:00
|
|
|
var server;
|
2022-01-22 12:23:41 +08:00
|
|
|
if (ZM_JANUS_PATH) {
|
|
|
|
server = ZM_JANUS_PATH;
|
|
|
|
} else if (window.location.protocol=='https:') {
|
2022-01-19 03:30:10 +08:00
|
|
|
// Assume reverse proxy setup for now
|
|
|
|
server = "https://" + window.location.hostname + "/janus";
|
|
|
|
} else {
|
|
|
|
server = "http://" + window.location.hostname + ":8088/janus";
|
|
|
|
}
|
|
|
|
|
2022-01-15 13:14:43 +08:00
|
|
|
if (janus == null) {
|
|
|
|
Janus.init({debug: "all", callback: function() {
|
|
|
|
janus = new Janus({server: server}); //new Janus
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
attachVideo(id);
|
|
|
|
return;
|
|
|
|
}
|
2022-01-21 00:47:28 +08:00
|
|
|
|
|
|
|
const stream = this.getElement();
|
|
|
|
if (!stream) return;
|
2021-11-11 03:20:19 +08:00
|
|
|
if (!stream.src) {
|
2022-01-21 00:47:28 +08:00
|
|
|
// Website Monitors won't have an img tag, neither will video
|
2021-03-24 01:53:45 +08:00
|
|
|
console.log('No src for #liveStream'+this.id);
|
|
|
|
console.log(stream);
|
|
|
|
return;
|
|
|
|
}
|
2022-01-21 00:47:28 +08:00
|
|
|
// Step 1 make sure we are streaming instead of a static image
|
2020-09-03 02:37:48 +08:00
|
|
|
src = stream.src.replace(/mode=single/i, 'mode=jpeg');
|
2022-01-21 00:47:28 +08:00
|
|
|
if (-1 == src.search('connkey')) {
|
2020-09-03 02:37:48 +08:00
|
|
|
src += '&connkey='+this.connKey;
|
|
|
|
}
|
2022-01-21 00:47:28 +08:00
|
|
|
if (stream.src != src) {
|
2021-11-11 03:20:19 +08:00
|
|
|
console.log("Setting to streaming: " + src);
|
2020-09-03 02:37:48 +08:00
|
|
|
stream.src = '';
|
|
|
|
stream.src = src;
|
|
|
|
}
|
2022-01-21 00:47:28 +08:00
|
|
|
setTimeout(this.statusQuery.bind(this), delay);
|
2020-09-01 06:30:05 +08:00
|
|
|
};
|
2022-01-21 00:47:28 +08:00
|
|
|
|
2020-09-03 02:37:48 +08:00
|
|
|
this.stop = function() {
|
|
|
|
if ( 0 ) {
|
2022-01-21 00:47:28 +08:00
|
|
|
const stream = this.getElement();
|
|
|
|
if (!stream) return;
|
2020-09-03 02:37:48 +08:00
|
|
|
src = stream.src.replace(/mode=jpeg/i, 'mode=single');
|
2022-01-21 00:47:28 +08:00
|
|
|
if (stream.src != src) {
|
2020-09-03 02:37:48 +08:00
|
|
|
console.log("Setting to stopped");
|
|
|
|
stream.src = '';
|
|
|
|
stream.src = src;
|
|
|
|
}
|
|
|
|
}
|
2022-01-21 00:47:28 +08:00
|
|
|
this.streamCommand(CMD_STOP);
|
2020-09-03 02:37:48 +08:00
|
|
|
};
|
|
|
|
this.pause = function() {
|
2022-01-21 00:47:28 +08:00
|
|
|
this.streamCommand(CMD_PAUSE);
|
2020-09-04 22:02:43 +08:00
|
|
|
};
|
2020-09-03 02:37:48 +08:00
|
|
|
this.play = function() {
|
2022-01-21 00:47:28 +08:00
|
|
|
this.streamCommand(CMD_PLAY);
|
2020-09-04 22:02:43 +08:00
|
|
|
};
|
2020-09-01 06:30:05 +08:00
|
|
|
|
|
|
|
this.eventHandler = function(event) {
|
|
|
|
console.log(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onclick = function(evt) {
|
2022-01-21 00:47:28 +08:00
|
|
|
console.log('onclick');
|
2020-09-01 06:30:05 +08:00
|
|
|
};
|
|
|
|
|
2022-01-21 00:47:28 +08:00
|
|
|
this.setup_onclick = function(func) {
|
|
|
|
this.onclick = func;
|
2022-01-27 10:39:29 +08:00
|
|
|
const el = this.getFrame();
|
2022-01-21 00:47:28 +08:00
|
|
|
if (!el) return;
|
|
|
|
el.addEventListener('click', this.onclick, false);
|
2020-09-01 06:30:05 +08:00
|
|
|
};
|
2022-01-21 00:47:28 +08:00
|
|
|
|
2020-09-01 06:30:05 +08:00
|
|
|
this.disable_onclick = function() {
|
2022-01-21 00:47:28 +08:00
|
|
|
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;
|
2020-09-01 06:30:05 +08:00
|
|
|
};
|
|
|
|
|
2021-01-07 04:19:25 +08:00
|
|
|
this.setStateClass = function(jobj, stateClass) {
|
2022-01-21 00:47:28 +08:00
|
|
|
if (!jobj) {
|
|
|
|
console.log("No obj in setStateClass");
|
2020-09-03 02:37:48 +08:00
|
|
|
return;
|
2020-09-04 22:02:43 +08:00
|
|
|
}
|
2022-01-21 00:47:28 +08:00
|
|
|
if (!jobj.hasClass(stateClass)) {
|
|
|
|
if (stateClass != 'alarm') jobj.removeClass('alarm');
|
|
|
|
if (stateClass != 'alert') jobj.removeClass('alert');
|
|
|
|
if (stateClass != 'idle') jobj.removeClass('idle');
|
2021-01-07 04:19:25 +08:00
|
|
|
|
|
|
|
jobj.addClass(stateClass);
|
2020-09-01 06:30:05 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-21 00:47:28 +08:00
|
|
|
this.setAlarmState = function(alarmState) {
|
|
|
|
var stateClass = '';
|
|
|
|
if (alarmState == STATE_ALARM) {
|
|
|
|
stateClass = 'alarm';
|
|
|
|
} else if (alarmState == STATE_ALERT) {
|
|
|
|
stateClass = 'alert';
|
|
|
|
}
|
|
|
|
|
|
|
|
const stateValue = $j('#stateValue'+this.id);
|
|
|
|
if (stateValue.length) {
|
|
|
|
stateValue.text(stateStrings[alarmState]);
|
|
|
|
if (stateClass) {
|
|
|
|
stateValue.addClass(stateClass);
|
|
|
|
} else {
|
|
|
|
stateValue.removeClass();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log("No statevalue");
|
|
|
|
}
|
|
|
|
//const monitorState = $j('#monitorState'+this.id);
|
|
|
|
//if (monitorState.length) this.setStateClass(monitorState, 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 (SOUND_ON_ALARM) {
|
|
|
|
// Enable the alarm sound
|
|
|
|
if (!msieVer) {
|
|
|
|
$j('#alarmSound').removeClass('hidden');
|
|
|
|
} else {
|
|
|
|
$j('#MediaPlayer').trigger('play');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (POPUP_ON_ALARM) {
|
|
|
|
window.focus();
|
|
|
|
}
|
|
|
|
if (this.onalarm) {
|
|
|
|
this.onalarm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (oldAlarm) { // done with an event do a refresh
|
|
|
|
if (SOUND_ON_ALARM) {
|
|
|
|
// Disable alarm sound
|
|
|
|
if (!msieVer) {
|
|
|
|
$j('#alarmSound').addClass('hidden');
|
|
|
|
} else {
|
|
|
|
$j('#MediaPlayer').trigger('pause');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.onalarm) {
|
|
|
|
this.onalarm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.lastAlarmState = alarmState;
|
|
|
|
}; // end function setAlarmState( currentAlarmState )
|
|
|
|
|
|
|
|
this.onalarm = null;
|
|
|
|
this.setup_onalarm = function(func) {
|
|
|
|
this.onalarm = func;
|
|
|
|
};
|
|
|
|
|
2021-01-07 04:19:25 +08:00
|
|
|
this.onFailure = function(jqxhr, textStatus, error) {
|
2022-01-21 00:47:28 +08:00
|
|
|
// Assuming temporary problem, retry in a bit.
|
2021-01-11 04:21:52 +08:00
|
|
|
setTimeout(this.streamCmdQuery.bind(this), 1000*statusRefreshTimeout);
|
2021-01-07 04:19:25 +08:00
|
|
|
logAjaxFail(jqxhr, textStatus, error);
|
2020-09-01 06:30:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
this.getStreamCmdResponse = function(respObj, respText) {
|
2022-01-21 00:47:28 +08:00
|
|
|
var stream = this.getElement();
|
2022-01-19 03:30:10 +08:00
|
|
|
if (!stream) {
|
2020-09-03 02:37:48 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-09-01 06:30:05 +08:00
|
|
|
|
2022-01-19 03:30:10 +08:00
|
|
|
//watchdogOk('stream');
|
2022-01-21 00:47:28 +08:00
|
|
|
if (this.streamCmdTimer) {
|
|
|
|
this.streamCmdTimer = clearTimeout(this.streamCmdTimer);
|
2022-01-19 03:30:10 +08:00
|
|
|
}
|
|
|
|
|
2022-01-21 00:47:28 +08:00
|
|
|
if (respObj.result == 'Ok') {
|
|
|
|
if (respObj.status) {
|
|
|
|
const streamStatus = this.status = respObj.status;
|
2020-09-01 06:30:05 +08:00
|
|
|
|
2020-09-03 02:37:48 +08:00
|
|
|
if ( (
|
2020-09-04 22:02:43 +08:00
|
|
|
(typeof COMPACT_MONTAGE === 'undefined') ||
|
|
|
|
!COMPACT_MONTAGE) &&
|
|
|
|
(this.type != 'WebSite')
|
2020-09-03 02:37:48 +08:00
|
|
|
) {
|
2022-01-21 00:47:28 +08:00
|
|
|
const viewingFPSValue = $j('#viewingFPSValue'+this.id);
|
2022-01-19 03:30:10 +08:00
|
|
|
const captureFPSValue = $j('#captureFPSValue'+this.id);
|
|
|
|
const analysisFPSValue = $j('#analysisFPSValue'+this.id);
|
|
|
|
|
|
|
|
|
|
|
|
if (viewingFPSValue.length && (viewingFPSValue.text != this.status.fps)) {
|
|
|
|
viewingFPSValue.text(this.status.fps);
|
|
|
|
}
|
|
|
|
if (analysisFPSValue.length && (analysisFPSValue.text != this.status.analysisfps)) {
|
|
|
|
analysisFPSValue.text(this.status.analysisfps);
|
|
|
|
}
|
|
|
|
if (captureFPSValue.length && (captureFPSValue.text != this.status.capturefps)) {
|
|
|
|
captureFPSValue.text(this.status.capturefps);
|
|
|
|
}
|
2021-01-07 05:57:30 +08:00
|
|
|
|
2022-01-21 00:47:28 +08:00
|
|
|
const levelValue = $j('#levelValue');
|
|
|
|
if (levelValue.length) {
|
|
|
|
levelValue.text(this.status.level);
|
|
|
|
var newClass = 'ok';
|
|
|
|
if (this.status.level > 95) {
|
|
|
|
newClass = 'alarm';
|
|
|
|
} else if (this.status.level > 80) {
|
|
|
|
newClass = 'alert';
|
|
|
|
}
|
|
|
|
levelValue.removeClass();
|
|
|
|
levelValue.addClass(newClass);
|
|
|
|
}
|
2020-09-01 06:30:05 +08:00
|
|
|
|
2022-01-21 00:47:28 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
$j('#zoomValue'+this.id).text(this.status.zoom);
|
|
|
|
if ('zoomOutBtn' in this.buttons) {
|
|
|
|
if (this.status.zoom == '1.0') {
|
|
|
|
setButtonState('zoomOutBtn', 'unavail');
|
|
|
|
} else {
|
|
|
|
setButtonState('zoomOutBtn', 'inactive');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // end if compact montage
|
2020-09-01 06:30:05 +08:00
|
|
|
|
2022-01-21 00:47:28 +08:00
|
|
|
this.setAlarmState(this.status.state);
|
2020-09-01 06:30:05 +08:00
|
|
|
|
2022-01-21 00:47:28 +08:00
|
|
|
if (canEdit.Monitors) {
|
|
|
|
if (streamStatus.enabled) {
|
|
|
|
if ('enableAlarmButton' in this.buttons) {
|
|
|
|
this.buttons.enableAlarmButton.addClass('disabled');
|
|
|
|
this.buttons.enableAlarmButton.prop('title', disableAlarmsStr);
|
|
|
|
}
|
|
|
|
if ('forceAlarmButton' in this.buttons) {
|
|
|
|
if (streamStatus.forced) {
|
|
|
|
this.buttons.forceAlarmButton.addClass('disabled');
|
|
|
|
this.buttons.forceAlarmButton.prop('title', cancelForcedAlarmStr);
|
|
|
|
} else {
|
|
|
|
this.buttons.forceAlarmButton.removeClass('disabled');
|
|
|
|
this.buttons.forceAlarmButton.prop('title', forceAlarmStr);
|
|
|
|
}
|
|
|
|
this.buttons.forceAlarmButton.prop('disabled', false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ('enableAlarmButton' in this.buttons) {
|
|
|
|
this.buttons.enableAlarmButton.removeClass('disabled');
|
|
|
|
this.buttons.enableAlarmButton.prop('title', enableAlarmsStr);
|
|
|
|
}
|
|
|
|
if ('forceAlarmButton' in this.buttons) {
|
|
|
|
this.buttons.forceAlarmButton.prop('disabled', true);
|
|
|
|
}
|
2020-09-01 06:30:05 +08:00
|
|
|
}
|
2022-01-21 00:47:28 +08:00
|
|
|
if ('enableAlarmButton' in this.buttons) {
|
|
|
|
this.buttons.enableAlarmButton.prop('disabled', false);
|
2020-09-01 06:30:05 +08:00
|
|
|
}
|
2022-01-21 00:47:28 +08:00
|
|
|
} // end if canEdit.Monitors
|
|
|
|
|
2022-01-09 01:14:28 +08:00
|
|
|
if (this.status.auth) {
|
2022-01-19 03:30:10 +08:00
|
|
|
if (this.status.auth != this.auth_hash) {
|
2020-09-01 06:30:05 +08:00
|
|
|
// Try to reload the image stream.
|
2022-01-21 00:47:28 +08:00
|
|
|
if (stream && stream.src) {
|
2022-01-09 01:14:28 +08:00
|
|
|
const oldsrc = stream.src;
|
|
|
|
stream.src = '';
|
|
|
|
stream.src = oldsrc.replace(/auth=\w+/i, 'auth='+this.status.auth);
|
2020-09-01 06:30:05 +08:00
|
|
|
}
|
2022-01-09 01:14:28 +08:00
|
|
|
console.log("Changed auth from " + this.auth_hash + " to " + this.status.auth);
|
|
|
|
this.auth_hash = this.status.auth;
|
2020-09-01 06:30:05 +08:00
|
|
|
}
|
|
|
|
} // end if have a new auth hash
|
|
|
|
} // end if has state
|
|
|
|
} else {
|
|
|
|
console.error(respObj.message);
|
|
|
|
// Try to reload the image stream.
|
2022-01-09 01:14:28 +08:00
|
|
|
if (stream) {
|
2022-01-21 00:47:28 +08:00
|
|
|
if (stream.src) {
|
2020-09-01 06:30:05 +08:00
|
|
|
console.log('Reloading stream: ' + stream.src);
|
2020-09-03 02:37:48 +08:00
|
|
|
src = stream.src.replace(/rand=\d+/i, 'rand='+Math.floor((Math.random() * 1000000) ));
|
2022-01-21 00:47:28 +08:00
|
|
|
// Maybe navbar updated auth FIXME
|
|
|
|
if (src != stream.src) {
|
2020-09-03 02:37:48 +08:00
|
|
|
stream.src = src;
|
|
|
|
} else {
|
|
|
|
console.log("Failed to update rand on stream src");
|
|
|
|
}
|
2020-09-01 06:30:05 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log('No stream to reload?');
|
|
|
|
}
|
|
|
|
} // end if Ok or not
|
2022-01-21 00:47:28 +08:00
|
|
|
};
|
2020-09-01 06:30:05 +08:00
|
|
|
|
2022-01-21 00:47:28 +08:00
|
|
|
this.statusQuery = function() {
|
|
|
|
this.streamCmdQuery(CMD_QUERY);
|
|
|
|
setTimeout(this.statusQuery.bind(this), statusRefreshTimeout);
|
2020-09-01 06:30:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
this.streamCmdQuery = function(resent) {
|
|
|
|
//console.log("Starting CmdQuery for " + this.connKey );
|
|
|
|
if ( this.type != 'WebSite' ) {
|
2021-01-07 04:19:25 +08:00
|
|
|
this.streamCmdParms.command = CMD_QUERY;
|
|
|
|
this.streamCmdReq(this.streamCmdParms);
|
2020-09-01 06:30:05 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-21 00:47:28 +08:00
|
|
|
this.streamCommand = function(command) {
|
|
|
|
if (typeof(command) == 'object') {
|
|
|
|
for (const key in command) this.streamCmdParms[key] = command[key];
|
|
|
|
} else {
|
|
|
|
this.streamCmdParms.command = command;
|
|
|
|
}
|
|
|
|
this.streamCmdReq(this.streamCmdParms);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.alarmCommand = function(command) {
|
|
|
|
if (this.ajaxQueue) {
|
|
|
|
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,
|
|
|
|
data: alarmCmdParms, dataType: "json"})
|
|
|
|
.done(this.getStreamCmdResponse.bind(this))
|
|
|
|
.fail(this.onFailure.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.type != 'WebSite') {
|
|
|
|
$j.ajaxSetup({timeout: AJAX_TIMEOUT});
|
|
|
|
if (auth_hash) {
|
|
|
|
this.streamCmdParms.auth = auth_hash;
|
|
|
|
} else if ( auth_relay ) {
|
|
|
|
this.streamCmdParms.auth_relay = '';
|
|
|
|
}
|
|
|
|
|
2021-01-07 04:19:25 +08:00
|
|
|
this.streamCmdReq = function(streamCmdParms) {
|
2022-01-21 00:47:28 +08:00
|
|
|
if (this.ajaxQueue) {
|
|
|
|
this.ajaxQueue.abort();
|
2021-03-14 01:12:20 +08:00
|
|
|
}
|
2022-01-21 00:47:28 +08:00
|
|
|
this.ajaxQueue = jQuery.ajaxQueue({url: this.url, data: streamCmdParms, dataType: "json"})
|
2021-01-07 04:19:25 +08:00
|
|
|
.done(this.getStreamCmdResponse.bind(this))
|
|
|
|
.fail(this.onFailure.bind(this));
|
|
|
|
};
|
2020-09-01 06:30:05 +08:00
|
|
|
}
|
2022-01-08 05:05:52 +08:00
|
|
|
this.analyse_frames = true;
|
|
|
|
this.show_analyse_frames = function(toggle) {
|
|
|
|
this.analyse_frames = toggle;
|
2022-01-21 00:47:28 +08:00
|
|
|
this.streamCmdParms.command = this.analyse_frames ? CMD_ANALYZE_ON : CMD_ANALYZE_OFF;
|
2022-01-08 05:05:52 +08:00
|
|
|
this.streamCmdReq(this.streamCmdParms);
|
|
|
|
};
|
2020-09-01 06:30:05 +08:00
|
|
|
} // end function MonitorStream
|
2022-01-15 13:14:43 +08:00
|
|
|
|
|
|
|
async function attachVideo(id) {
|
2022-01-18 05:19:27 +08:00
|
|
|
await waitUntil(() => janus.isConnected() );
|
2022-01-15 13:14:43 +08:00
|
|
|
janus.attach({
|
|
|
|
plugin: "janus.plugin.streaming",
|
|
|
|
opaqueId: "streamingtest-"+Janus.randomString(12),
|
|
|
|
success: function(pluginHandle) {
|
|
|
|
streaming[id] = pluginHandle;
|
2022-01-18 05:19:27 +08:00
|
|
|
var body = {"request": "watch", "id": id};
|
2022-01-15 13:14:43 +08:00
|
|
|
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"];
|
2022-01-18 05:19:27 +08:00
|
|
|
if (result !== null && result !== undefined) {
|
|
|
|
if (result["status"] !== undefined && result["status"] !== null) {
|
2022-01-15 13:14:43 +08:00
|
|
|
var status = result["status"];
|
|
|
|
Janus.debug(status);
|
|
|
|
}
|
2022-01-18 05:19:27 +08:00
|
|
|
} else if (msg["error"] !== undefined && msg["error"] !== null) {
|
2022-01-15 13:14:43 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-01-18 05:19:27 +08:00
|
|
|
if (jsep !== undefined && jsep !== null) {
|
2022-01-15 13:14:43 +08:00
|
|
|
Janus.debug("Handling SDP as well...");
|
|
|
|
Janus.debug(jsep);
|
2022-01-22 13:21:41 +08:00
|
|
|
if ((navigator.userAgent.toLowerCase().indexOf('firefox') > -1) && (jsep["sdp"].includes("420029"))) { //because firefox devs are stubborn
|
|
|
|
jsep["sdp"] = jsep["sdp"].replace("420029", "42e01f");
|
|
|
|
}
|
2022-01-15 13:14:43 +08:00
|
|
|
// Offer from the plugin, let's answer
|
|
|
|
streaming[id].createAnswer({
|
|
|
|
jsep: jsep,
|
|
|
|
// We want recvonly audio/video and, if negotiated, datachannels
|
2022-01-18 05:19:27 +08:00
|
|
|
media: {audioSend: false, videoSend: false, data: true},
|
2022-01-15 13:14:43 +08:00
|
|
|
success: function(jsep) {
|
|
|
|
Janus.debug("Got SDP!");
|
|
|
|
Janus.debug(jsep);
|
2022-01-18 05:19:27 +08:00
|
|
|
var body = {"request": "start"};
|
2022-01-15 13:14:43 +08:00
|
|
|
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 track :::");
|
|
|
|
Janus.debug(ourstream);
|
|
|
|
Janus.attachMediaStream(document.getElementById("liveStream" + id), ourstream);
|
|
|
|
}
|
|
|
|
}); // janus.attach
|
|
|
|
} //function attachVideo
|
|
|
|
|
|
|
|
const waitUntil = (condition) => {
|
|
|
|
return new Promise((resolve) => {
|
2022-01-18 05:19:27 +08:00
|
|
|
const interval = setInterval(() => {
|
2022-01-15 13:14:43 +08:00
|
|
|
if (!condition()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
clearInterval(interval);
|
|
|
|
resolve();
|
|
|
|
}, 100);
|
|
|
|
});
|
2022-01-18 05:19:27 +08:00
|
|
|
};
|