/**@license dhsdk v2.7.0 Copyright (c) 2021-12-13 diihoo*/ "use strict"; var DHAlarm = DHAlarm || {}; var DHAlarmWeb = function () { this.localStream = null; this.pc = null; this.webrtcWs = null; this.alarmWs = null; this.audioWs = {}; this.deviceAudioWs = {}; this.started = false; this.webrtcSocketOpened = false; this.alarmSocketOpened = 0; this.clientid = null; this.roomid = null; this.onStreamCallBack = null; this.alarmUrl = null; this.webrtcUrl = null; this.onAlarmMessage = null; this.onLoging = null; this.onDeviceList = null; this.onNotify = null; this.onPlayRT = null; this.onPlayRTState = null; this.onDeviceRemove = null; this.onParseMsgError = null; this.uname = null; this.pwd = null; this.isLogin = false; this.loginHandle = null; this.deviceId = null; this.context = null; this.recorder = null; this.audioInput = null; this.wfs = {}; DHAlarm.wrc = this; this.player = {}; this.bufferPool = {}; this.isDeviceAudioPlay = {}; this.onAlarmServerClosed = null; this.keepAliveInterval = null; this.onDeviceAudioData = null; this.onLocalAudioData = null; this.onDeviceVideoData = null; this.onDHAlarmWebError = null; this.hasDeviceList = false; this.loginState = 0; this.loginTimer = null; this.deviceListTimer = null; this.loginInterval = 0; this.talkDeviceIds = []; this.multiTalkDeviceIds = []; this.notStartBCWsDids = []; this.multiTalkFilePath = null; this.multiFileTalkIntv = 0; this.multiTalkType = 0; this.sendIds = []; this.bufferFileArr_8k = []; this.bufferFileArr_16k = []; this.broadcastType = 0; this.remainFileBuffer_8k = 0; this.remainFileBuffer_16k = 0; this.bcPlayState = "pause"; this.uploadFilePath = null; this.audioFileStream = null; this.audioUploadWs = null; this.uploadFileArr = []; this.totalUploadFileArr = []; this.overInter = 0; this.tunnelDataId = 1; this.tunnelQueue = []; this.tunnelReqTimeout = 20; this.pbAudioWs = {}; this.pbWfs = {}; this.pbPlayer = {}; this.pbBufferPool = {}; this.dataWsPort = 8088; this.mediaWsPort = 8088; this.onGroupList = null; this.onAddGroup = null; this.onEditGroup = null; this.onDelGroup = null; this.onUserList = null; this.onUserGroups = null; this.onAddUser = null; this.onEditPassword = null; this.onDelUser = null; this.onAuthorizeGroup = null; this.onEditDevice = null; this.onMoveDevice = null; this.onGetWebPush = null; this.onEditWebPush = null; this.onPlayAudioFileEnd = null; this.onAlarmMultiTalkStopped = null; this.onMultiTalkStartFinished = null; this.onMultiTalkCountLimit = null; this.onMultiTalkClosedError = null; this.onStartBroadcast = null; this.onBroadcastWsClosed = null; this.onUploadAudioFile = null; this.onDeleteAudioFile = null; this.onDecodeAudioError = null; this.onUploadAudioFileEnd = null; this.onGetAudioFileList = null; this.onGetBCTaskList = null; this.onUploadBCTask = null; this.onEditBCTask = null; this.onDeleteBCTask = null; this.onConfigTunnel = null; this.onGetDeviceExtra = null; this.onSetDeviceExtra = null; this.onGetRecordList = null }; window.onbeforeunload = function () { }.bind(this); DHAlarmWeb.prototype.openAlarmSocket = function () { console.log("openAlarmSocket"); this.alarmSocketOpened = 1; this.alarmWs = new WebSocket(this.alarmUrl); this.alarmWs.onopen = this.onAlarmSocketOpened.bind(this); this.alarmWs.onmessage = this.onAlarmSocketMessage.bind(this); this.alarmWs.onclose = this.onAlarmSocketClosed.bind(this); this.alarmWs.onerror = this.onAlarmSocketError.bind(this) }; DHAlarmWeb.prototype.onAlarmSocketOpened = function () { this.alarmSocketOpened = 2; console.log("onAlarmSocketOpened"); this.registerAlarm() }; DHAlarmWeb.prototype.registerAlarm = function () { var e = {cmd: "register", msg: ""}; this.alarmWs.send(JSON.stringify(e)) }; DHAlarmWeb.prototype.onAlarmSocketMessage = function (e) { try { var t = JSON.parse(e.data); this.clientid = t.clientid } catch (t) { console.log(e) } this.processAlarmMessage(e.data) }; DHAlarmWeb.prototype.onAlarmSocketError = function (e) { console.log("onAlarmSocketError: " + JSON.stringify(e)); this.loginState = 0; var t = {method: "systemManager.onError", error: "socketError"}; var i = {clientid: "", error: "fail", msg: t}; if (this.onDHAlarmWebError) { this.onDHAlarmWebError(i) } this.clearLoginInt() }; DHAlarmWeb.prototype.onAlarmSocketClosed = function () { console.log("onAlarmSocketClosed"); var e = this.loginState; var t = this.alarmSocketOpened; var i = {clientid: "", error: "fail", msg: {method: "systemManager.onError", error: "loginTimeout"}}; this.alarmSocketOpened = 4; this.loginState = 0; this.clearKeepAliveInt(); this.clearLoginInt(); if (this.onDHAlarmWebError && e == 3) { this.onDHAlarmWebError(i) } if (this.onAlarmServerClosed && t != 3) { this.onAlarmServerClosed() } }; DHAlarmWeb.prototype.sendAlarmMessage = function (e) { var t = JSON.stringify(e); if (this.alarmWs && this.alarmWs.readyState === WebSocket.OPEN) { this.alarmWs.send(t) } }; DHAlarmWeb.prototype.sendAlarmMessageCallBack = function (e, t) { var i = JSON.stringify(e); this.alarmWs.send(i) }; DHAlarmWeb.prototype.processAlarmMessage = function (e) { if (this.onAlarmMessage) { this.onAlarmMessage(e) } try { var t = JSON.parse(e); var i = JSON.parse(t.msg); if (i.method == "eventManager.notify") { var s = i.params; if (s.code == "DeviceStatus" && s.action == "Start") { if (this.multiTalkType == 1) { this.stopMultiMicTalk(this.loginHandle, false, [s.deviceId]) } else if (this.multiTalkType == 2) { this.stopMultiFileTalk(this.loginHandle, false, [s.deviceId]) } if (this.onAlarmMultiTalkStopped) { this.onAlarmMultiTalkStopped(s.deviceId) } } if (this.onNotify) { this.onNotify(i) } } else if (i.method == "configManager.onDeviceList") { this.hasDeviceList = true; this.clearDeviceListTimer(); if (this.onDeviceList) { this.onDeviceList(i) } } else if (i.method == "systemManager.onLogin") { this.clearLoginTimer(); if (i.error == "success") { this.loginHandle = i.params.loginHandle, this.clientid = t.clientid, this.loginState = 2; this.clearKeepAliveInt(); this.keepAliveInterval = setInterval(function () { var e = { cmd: "send", clientid: t.clientid, msg: JSON.stringify({method: "systemManager.keepAlive", params: {loginHandle: i.params.loginHandle}}) }; this.sendAlarmMessage(e) }.bind(this), 1e4); this.clearDeviceListTimer(); this.deviceListTimer = setTimeout(function () { if (!this.hasDeviceList) { var e = {clientid: "", error: "fail", msg: {method: "systemManager.onError", error: "dataTimeout"}}; if (this.onDHAlarmWebError) { this.onDHAlarmWebError(e) } } }.bind(this), 1e4) } else { this.loginState = 0 } if (this.onLogin) { this.onLogin(i) } } else if (i.method == "systemManager.onPlayRT") { if (this.onPlayRT) { this.onPlayRT(i) } } else if (i.method == "systemManager.onPlayRTState") { if (this.onPlayRTState) { this.onPlayRTState(i) } } else if (i.method == "configManager.onDeviceRemove") { if (this.onDeviceRemove) { this.onDeviceRemove(i) } } else if (i.method == "configManager.onGroupList") { if (this.onGroupList) { this.onGroupList(i) } } else if (i.method == "configManager.onAddGroup") { if (this.onAddGroup) { this.onAddGroup(i) } } else if (i.method == "configManager.onEditGroup") { if (this.onEditGroup) { this.onEditGroup(i) } } else if (i.method == "configManager.onDelGroup") { if (this.onDelGroup) { this.onDelGroup(i) } } else if (i.method == "configManager.onUserList") { if (this.onUserList) { this.onUserList(i) } } else if (i.method == "configManager.onUserGroups") { if (this.onUserGroups) { this.onUserGroups(i) } } else if (i.method == "configManager.onAddUser") { if (this.onAddUser) { this.onAddUser(i) } } else if (i.method == "configManager.onEditPassword") { if (this.onEditPassword) { this.onEditPassword(i) } } else if (i.method == "configManager.onDelUser") { if (this.onDelUser) { this.onDelUser(i) } } else if (i.method == "configManager.onAuthorizeGroup") { if (this.onAuthorizeGroup) { this.onAuthorizeGroup(i) } } else if (i.method == "configManager.onEditDevice") { if (this.onEditDevice) { this.onEditDevice(i) } } else if (i.method == "configManager.onMoveDevice") { if (this.onMoveDevice) { this.onMoveDevice(i) } } else if (i.method == "configManager.onGetWebPush") { if (this.onGetWebPush) { this.onGetWebPush(i) } } else if (i.method == "configManager.onEditWebPush") { if (this.onEditWebPush) { this.onEditWebPush(i) } } else if (i.method == "deviceManager.onStartBroadcast") { if (this.onStartBroadcast) { this.onStartBroadcast(i) } } else if (i.method == "audioManager.onGetAudioFileList") { if (this.onGetAudioFileList) { this.onGetAudioFileList(i) } } else if (i.method == "audioManager.onUploadAudioFile") { if (this.onUploadAudioFile) { this.onUploadAudioFile(i) } if (i.error == "success") { if (this.audioUploadWs && this.audioUploadWs.readyState == this.audioUploadWs.OPEN) { for (var a = 0; a < 50; a++) { if (this.uploadFileArr.length == 0) break; this.audioUploadWs.send(this.uploadFileArr.shift()) } if (this.totalUploadFileArr.length <= 50 && this.uploadFileArr.length == 0) { this.audioUploadWs.send("OVER") } if (this.overInter > 0) return; this.overInter = setInterval(function () { if (this.overInter > 0) { if (this.uploadFileArr.length == 0) { this.audioUploadWs.send("OVER") } else { this.audioUploadWs.send("CANCEL") } } }.bind(this), 1e4) } else { this.openAudioUploadWs(this.loginHandle) } } } else if (i.method == "audioManager.onDeleteAudioFile") { if (this.onDeleteAudioFile) { this.onDeleteAudioFile(i) } } else if (i.method == "audioManager.onGetBCTaskList") { if (this.onGetBCTaskList) { this.onGetBCTaskList(i) } } else if (i.method == "audioManager.onUploadBCTask") { if (this.onUploadBCTask) { this.onUploadBCTask(i) } } else if (i.method == "audioManager.onEditBCTask") { if (this.onEditBCTask) { this.onEditBCTask(i) } } else if (i.method == "audioManager.onDeleteBCTask") { if (this.onDeleteBCTask) { this.onDeleteBCTask(i) } } else if (i.method == "deviceManager.onConfigTunnel") { var r = JSON.parse(i.params.tunnelData); if (r == null) return; var n = this.tunnelQueue.findIndex(e => e.id == r.id); if (n == -1) { if (this.onConfigTunnel) { this.onConfigTunnel(i) } return } var o = this.tunnelQueue.splice(n, 1)[0]; var l = {}; if (this.onConfigTunnel) { i.id = o.data.requestId; this.onConfigTunnel(i) } if (o.method == "setBroadcastVolume") { if (this.onSetBroadcastVolume) { l.id = o.data.requestId; l.volume = o.data.volume; i.params.reqData = l; this.onSetBroadcastVolume(i.params) } } else if (o.method == "getBroadcastVolume") { if (this.onGetBroadcastVolume) { l.id = o.data.requestId; i.params.reqData = l; this.onGetBroadcastVolume(i.params) } } else if (o.method == "configPayStatus") { if (this.onConfigPayStatus) { l.id = o.data.requestId; i.params.reqData = l; this.onConfigPayStatus(i.params) } } else if (o.method == "getDeviceVolume") { if (this.onGetDeviceVolume) { l.id = o.data.requestId; l.type = o.data.type; i.params.reqData = l; this.onGetDeviceVolume(i.params) } } else if (o.method == "setDeviceVolume") { if (this.onSetDeviceVolume) { l.id = o.data.requestId; l.type = o.data.type; l.volume = o.data.volume; i.params.reqData = l; this.onSetDeviceVolume(i.params) } } else if (o.method == "getAudioOutputChannels") { if (this.onGetAudioOutputChannels) { l.id = o.data.requestId; i.params.reqData = l; this.onGetAudioOutputChannels(i.params) } } } else if (i.method == "deviceManager.onGetDeviceExtra") { if (this.onGetDeviceExtra) { this.onGetDeviceExtra(i) } } else if (i.method == "deviceManager.onSetDeviceExtra") { if (this.onSetDeviceExtra) { this.onSetDeviceExtra(i) } } else if (i.method == "deviceManager.onGetRecordList") { if (this.onGetRecordList) { this.onGetRecordList(i) } } } catch (e) { if (this.onParseMsgError) { this.onParseMsgError(t) } console.log(e) } }; DHAlarmWeb.prototype.setWebsocketPort = function (e) { this.dataWsPort = e.dataWsPort; this.mediaWsPort = e.mediaWsPort }; DHAlarmWeb.prototype.login = function (e, t, i) { if (!i) { return } this.alarmUrl = "wss://" + i + ":" + this.dataWsPort + "/ws"; this.webrtcUrl = "wss://" + i + ":8089/ws"; this.uname = e; this.pwd = t; if (this.loginState != 0 && this.loginState != 3) return; if (this.alarmUrl == "" || this.alarmSocketOpened == 0 || this.alarmSocketOpened == 4) { this.openAlarmSocket() } this.loginState = 1; this.clearLoginInt(); this.loginInterval = setInterval(function () { if (DHAlarm.wrc.alarmSocketOpened == 2) { this.clearLoginInt(); var i = {method: "systemManager.login", params: {userName: e, password: t}}; var s = {cmd: "send", msg: JSON.stringify(i)}; DHAlarm.wrc.sendAlarmMessage(s); this.clearLoginTimer(); this.loginTimer = setTimeout(function () { if (this.loginState == 1) { this.loginState = 3; this.closeAlarmWs() } }.bind(this), 1e4) } }.bind(this), 1e3) }; DHAlarmWeb.prototype.logout = function (e) { if (!e) return; var t = {method: "systemManager.logout", params: {loginHandle: parseInt(e)}}; var i = {cmd: "send", msg: JSON.stringify(t)}; this.sendAlarmMessage(i); var s = {cmd: "deregister", msg: ""}; this.sendAlarmMessage(s); this.clientid = null; this.clearKeepAliveInt(); this.clearLoginInt(); this.clearLoginTimer(); this.clearDeviceListTimer(); this.hasDeviceList = false; this.loginState = 0; this.closeAlarmWs(); for (var a = 0; a < this.audioWs.length; a++) { if (this.audioWs[a]) { this.audioWs[a].close() } if (this.deviceAudioWs[a]) { this.deviceAudioWs[a].close() } if (this.player[a]) { this.player[a].destroy() } } for (var a = 0; a < this.pbAudioWs.length; a++) { if (this.pbAudioWs[a]) { this.pbAudioWs[a].close() } if (this.pbWfs[a]) { this.pbWfs[a].close() } if (this.pbPlayer[a]) { this.pbPlayer[a].destroy() } } }; DHAlarmWeb.prototype.playRT = function (e, t, i, s) { if (!t || !i) return; var a = {method: "deviceManager.playRT", params: {deviceId: parseInt(t), loginHandle: parseInt(i), preview: false}}; var r = {cmd: "send", msg: JSON.stringify(a)}; t = parseInt(t); if (s != false) { s = true; if (this.talkDeviceIds.indexOf(t) == -1) { this.talkDeviceIds.push(t) } } this.sendAlarmMessage(r); if (!this.audioWs[t] || this.audioWs[t].readyState != 1) { this.openAudio(i, t, s, false) } if (s) { this.startTalk(t); this.playDeviceAudio(t) } if (!this.deviceAudioWs[t] || this.deviceAudioWs[t].readyState != 1) { this.openDeviceAudio(i, t) } this.playVideo(e, t, i) }; DHAlarmWeb.prototype.stopRT = function (e, t) { if (!e || !t) return; var i = {method: "deviceManager.stopRT", params: {deviceId: parseInt(e), loginHandle: parseInt(t)}}; var s = {cmd: "send", msg: JSON.stringify(i)}; e = parseInt(e); this.sendAlarmMessage(s); this.closeAudio(e); if (this.deviceAudioWs[e]) { this.deviceAudioWs[e].close() } if (this.player[e]) { this.player[e].destroy() } if (this.bufferPool[e]) { this.bufferPool[e].stopBuffering() } if (this.wfs[e]) { this.wfs[e].destroy() } this.closeDeviceAudio(e); this.deviceId = null; this.playDeviceId = null }; DHAlarmWeb.prototype.doControl = function (e, t, i) { var s = { method: "deviceManager.doControl", params: {deviceId: parseInt(e), loginHandle: parseInt(t), index: parseInt(i)} }; var a = {cmd: "send", msg: JSON.stringify(s)}; this.sendAlarmMessage(a) }; DHAlarmWeb.prototype.playVideo = function (e, t, i) { this.playDeviceId = t; if (!this.wfs[t] || this.wfs[t].websocketLoader.client.readyState != 1) { this.wfs[t] = new Wfs; this.wfs[t].attachMedia(e, t, this.clientid, this.loginHandle, this.alarmUrl); this.wfs[t].websocketLoader.onVideoFrame = function (e) { this.onVideoFrame(e, t) }.bind(this) } }; DHAlarmWeb.prototype.onVideoFrame = function (e, t) { if (this.onDeviceVideoData) { this.onDeviceVideoData(e, t) } }; DHAlarmWeb.prototype.startTalk = function (e) { e = parseInt(e); if (this.talkDeviceIds.indexOf(e) == -1) { this.talkDeviceIds.push(e) } var t = this.multiTalkDeviceIds.indexOf(parseInt(e)); if (t > -1) { this.multiTalkDeviceIds.splice(t, 1) } this.sendAudio(e) }; DHAlarmWeb.prototype.stopTalk = function (e) { this.removeTalkDid(e) }; DHAlarmWeb.prototype.registerTalk = function (e) { var t = { cmd: "register", clientid: this.clientid, loginHandle: parseInt(this.loginHandle), sessiontype: "talk", deviceid: parseInt(e), msg: "" }; this.audioWs[e].send(JSON.stringify(t)) }; DHAlarmWeb.prototype.openAudio = function (e, t, i, s) { this.loginHandle = e; this.deviceId = t; this.audioWs[t] = new WebSocket(this.alarmUrl); this.audioWs[t].onopen = function () { this.onAudioSocketOpened(t) }.bind(this); this.audioWs[t].onmessage = function (e) { this.onAudioSocketMessage(e, t, i, s) }.bind(this); this.audioWs[t].onclose = function () { this.onAudioSocketClosed(t, s) }.bind(this); this.audioWs[t].onerror = function (e) { this.onAudioSocketError(e, t) }.bind(this) }; DHAlarmWeb.prototype.onAudioSocketOpened = function (e) { console.log("onAudioSocketOpened did:" + e); this.registerTalk(e); var t = this.notStartBCWsDids.indexOf(e); if (t == -1) return; this.notStartBCWsDids.splice(t, 1) }; DHAlarmWeb.prototype.onAudioSocketMessage = function (e, t, i, s) { var a = JSON.parse(e.data); if (a.error == "success") { if (!i) { if (this.notStartBCWsDids.length == 0 && s == true) { if (this.multiTalkType == 1) { if (this.onMultiTalkStartFinished) { this.onMultiTalkStartFinished() } this.sendAudio() } else if (this.multiTalkType == 2) { if (this.onMultiTalkStartFinished) { this.onMultiTalkStartFinished() } setTimeout(function () { this.sendFileAuido() }.bind(this), 100) } } return } } }; DHAlarmWeb.prototype.onAudioSocketError = function (e, t) { console.log("onAudioSocketError: " + JSON.stringify(e)) }; DHAlarmWeb.prototype.onAudioSocketClosed = function (e, t) { console.log("onAudioSocketClosed did:" + e); var i = this.multiTalkDeviceIds.indexOf(e); if (i > -1) { this.multiTalkDeviceIds.splice(i, 1) } var s = this.notStartBCWsDids.indexOf(e); if (s > -1) { this.notStartBCWsDids.splice(s, 1) } if (t && this.multiTalkType > 0) { var a = false; if (this.multiTalkDeviceIds.length == 0) a = true; if (this.onMultiTalkClosedError) { this.onMultiTalkClosedError(e, a) } } }; DHAlarmWeb.prototype.sendAudio = function (e) { if (this.recorder) return; var t = new ArrayBuffer(320); var i = new DataView(t); var s = 0; var a = new ArrayBuffer(640); var r = new DataView(a); var n = 0; if (navigator.mediaDevices === undefined) { navigator.mediaDevices = {} } if (navigator.mediaDevices.getUserMedia === undefined) { navigator.mediaDevices.getUserMedia = function (e) { var t = navigator.webkitGetUserMedia || navigator.mozGetUserMedia; if (!t) { return Promise.reject(new Error("getUserMedia is not implemented in this browser")) } return new Promise(function (i, s) { t.call(navigator, e, i, s) }) } } navigator.mediaDevices.getUserMedia({audio: true}).then(function (e) { if (this.recorder) return; this.context = new AudioContext; this.recorder = this.context.createScriptProcessor(0, 1, 1); this.audioInput = this.context.createMediaStreamSource(e); this.audioInput.connect(this.recorder); this.recorder.connect(this.context.destination); this.recorder.onaudioprocess = function (e) { if (!this.recorder) return; var o = e.inputBuffer.getChannelData(0); var l = this.context.createBuffer(1, this.recorder.bufferSize, this.context.sampleRate); var d = l.getChannelData(0); for (var h = 0; h < l.length; h++) { d[h] = o[h] } var u = new OfflineAudioContext(1, parseInt(this.recorder.bufferSize * 8e3 / this.context.sampleRate), 8e3); var c = u.createBufferSource(); c.buffer = l; c.connect(u.destination); c.start(); u.startRendering().then(function (e) { var a = e.getChannelData(0); c.stop(); var r = 0; var n = a.length; if (this.broadcastType == 2 && this.bcPlayState == "play" && this.talkDeviceIds.length == 0) { if (this.bufferFileArr_8k.length == 0 || this.bufferFileArr_16k.length == 0) { this.bcPlayState = "pause"; if (this.onPlayAudioFileEnd) { this.onPlayAudioFileEnd() } return } for (var o = 0; o < parseInt(n / 160); o++) { if (this.broadcastWs_8k && this.broadcastWs_8k.readyState == this.broadcastWs_8k.OPEN) { var l = this.bufferFileArr_8k.shift(); if (!l) continue; this.broadcastWs_8k.send(l) } } this.remainFileBuffer_8k += n % 160; if (this.remainFileBuffer_8k >= 160) { if (this.broadcastWs_8k && this.broadcastWs_8k.readyState == this.broadcastWs_8k.OPEN) { var l = this.bufferFileArr_8k.shift(); if (!l) return; this.broadcastWs_8k.send(l); this.remainFileBuffer_8k -= 160 } } return } while (r < n) { var d = a[r]; var h = Math.max(-1, Math.min(1, d)); var u = h < 0 ? h * 32768 : h * 32767; if (s < 320) { i.setInt16(s, u, true) } else { if (this.multiTalkType == 2) { this.sendIds = this.talkDeviceIds.concat() } else { if (this.multiTalkDeviceIds.length > 0) { this.sendIds = this.multiTalkDeviceIds.concat(); for (var o in this.talkDeviceIds) { if (this.sendIds.indexOf(this.talkDeviceIds[o]) == -1) { this.sendIds.push(this.talkDeviceIds[o]) } } } else { this.sendIds = this.talkDeviceIds.concat() } } var p = t.slice(0, 320); var f = this; var m = p; var g = new Int16Array(m); if (this.broadcastType == 1 && this.talkDeviceIds.length == 0) { if (this.broadcastWs_8k && this.broadcastWs_8k.readyState == this.broadcastWs_8k.OPEN && this.bcPlayState == "play") { this.broadcastWs_8k.send(g) } } else { for (let e in f.sendIds) { var v = f.sendIds[e]; if (f.audioWs[v] && f.audioWs[v].readyState == f.audioWs[v].OPEN) { f.audioWs[v].send(g) } if (f.onLocalAudioData) { f.onLocalAudioData(m, v) } } } i = new DataView(t); s = 0; i.setInt16(s, u, true) } r++; s = s + 2 } }.bind(this)); if (this.broadcastType == 0) return; var p = new OfflineAudioContext(1, parseInt(this.recorder.bufferSize * 16e3 / this.context.sampleRate), 16e3); var f = p.createBufferSource(); f.buffer = l; f.connect(p.destination); f.start(); p.startRendering().then(function (e) { var t = e.getChannelData(0); f.stop(); var i = 0; var s = t.length; if (this.broadcastType == 2 && this.bcPlayState == "play") { for (var o = 0; o < parseInt(s / 320); o++) { if (this.broadcastWs_16k && this.broadcastWs_16k.readyState == this.broadcastWs_16k.OPEN) { var l = this.bufferFileArr_16k.shift(); if (!l) continue; this.broadcastWs_16k.send(l) } } this.remainFileBuffer_16k += s % 320; if (this.remainFileBuffer_16k >= 320) { if (this.broadcastWs_16k && this.broadcastWs_16k.readyState == this.broadcastWs_16k.OPEN) { var l = this.bufferFileArr_16k.shift(); if (!l) return; this.broadcastWs_16k.send(l); this.remainFileBuffer_16k -= 320 } } return } while (i < s) { var d = t[i]; var h = Math.max(-1, Math.min(1, d)); var u = h < 0 ? h * 32768 : h * 32767; if (n < 640) { r.setInt16(n, u, true) } else { var c = a.slice(0, 640); var p = c; var m = new Int16Array(p); if (this.broadcastType == 1 && this.talkDeviceIds.length == 0) { if (this.broadcastWs_16k && this.broadcastWs_16k.readyState == this.broadcastWs_16k.OPEN && this.bcPlayState == "play") { this.broadcastWs_16k.send(m) } } r = new DataView(a); n = 0; r.setInt16(n, u, true) } i++; n = n + 2 } }.bind(this)) }.bind(this) }.bind(this)).catch(function (t) { this.closeAudio(e); console.log(t.name + ": " + t.message) }.bind(this)) }; DHAlarmWeb.prototype.closeAudio = function (e) { this.removeTalkDid(e); if (this.audioWs[e] && this.multiTalkDeviceIds.indexOf(parseInt(e)) == -1 && this.talkDeviceIds.indexOf(parseInt(e)) == -1) { this.audioWs[e].close() } }; DHAlarmWeb.prototype.removeTalkDid = function (e) { var t = this.talkDeviceIds.indexOf(parseInt(e)); if (t > -1) { this.talkDeviceIds.splice(t, 1) } if (this.recorder && this.talkDeviceIds.length == 0 && (this.multiTalkDeviceIds.length == 0 || this.multiTalkType == 2) && this.broadcastType == 0) { this.recorder.disconnect(); this.recorder = null; if (this.audioInput) { this.audioInput.disconnect(); this.audioInput = null } } }; DHAlarmWeb.prototype.registerDeviceAudio = function (e) { var t = { cmd: "register", clientid: this.clientid, loginHandle: parseInt(this.loginHandle), sessiontype: "audio", deviceid: parseInt(e), msg: "" }; this.deviceAudioWs[e].send(JSON.stringify(t)) }; DHAlarmWeb.prototype.openDeviceAudio = function (e, t) { this.loginHandle = e; this.deviceId = t; this.deviceAudioWs[t] = new WebSocket(this.alarmUrl); this.deviceAudioWs[t].binaryType = "arraybuffer"; this.deviceAudioWs[t].onopen = function () { this.onDeviceAudioSocketOpened(t) }.bind(this); this.deviceAudioWs[t].onmessage = function (e) { this.onDeviceAudioSocketMessage(e, t) }.bind(this); this.deviceAudioWs[t].onclose = function () { this.onDeviceAudioSocketClosed(t) }.bind(this); this.deviceAudioWs[t].onerror = function (e) { this.onDeviceAudioSocketError(e) }.bind(this) }; DHAlarmWeb.prototype.onDeviceAudioSocketOpened = function (e) { console.log("onDeviceAudioSocketOpened did:" + e); this.registerDeviceAudio(e) }; DHAlarmWeb.prototype.onDeviceAudioSocketMessage = function (e, t) { if (typeof e.data == "object") { if (!this.isDeviceAudioPlay || !this.isDeviceAudioPlay[t]) return; this.parseDeviceAudio(e.data, t); if (this.onDeviceAudioData) { this.onDeviceAudioData(e.data, t) } } else { var i = JSON.parse(e.data); if (i.error == "success") { this.player[t] = new PCMPlayer({encoding: "16bitInt", channels: 1, sampleRate: 8e3, flushingTime: 500}); this.bufferPool[t] = new DHBufferPool; this.bufferPool[t].onFrame = function (e) { this.feedData(e, t) }.bind(this); this.bufferPool[t].startBuffering() } } }; DHAlarmWeb.prototype.onDeviceAudioSocketError = function (e) { console.log("onDeviceAudioSocketError: " + JSON.stringify(e)) }; DHAlarmWeb.prototype.onDeviceAudioSocketClosed = function (e) { console.log("onDeviceAudioSocketClosed did:" + e) }; DHAlarmWeb.prototype.parseDeviceAudio = function (e, t) { var i = new Int16Array(e); this.bufferPool[t].addFrame(i) }; DHAlarmWeb.prototype.feedData = function (e, t) { this.player[t].feed(e); this.player[t].volume(1) }; DHAlarmWeb.prototype.playDeviceAudio = function (e) { this.isDeviceAudioPlay = {}; this.isDeviceAudioPlay[e] = true }; DHAlarmWeb.prototype.closeDeviceAudio = function (e) { this.isDeviceAudioPlay = {}; this.isDeviceAudioPlay[e] = false }; DHAlarmWeb.prototype.clearKeepAliveInt = function () { if (this.keepAliveInterval) clearInterval(this.keepAliveInterval); this.keepAliveInterval = 0 }; DHAlarmWeb.prototype.clearLoginInt = function () { if (this.loginInterval) clearInterval(this.loginInterval); this.loginInterval = 0 }; DHAlarmWeb.prototype.clearLoginTimer = function () { if (this.loginTimer) clearTimeout(this.loginTimer); this.loginTimer = 0 }; DHAlarmWeb.prototype.clearDeviceListTimer = function () { if (this.deviceListTimer) clearTimeout(this.deviceListTimer); this.deviceListTimer = 0 }; DHAlarmWeb.prototype.closeAlarmWs = function () { if (this.alarmWs && this.alarmSocketOpened != 0 && this.alarmSocketOpened != 4) { this.alarmSocketOpened = 3; this.alarmWs.close() } }; DHAlarmWeb.prototype.addGroup = function (e, t, i, s, a, r) { var n = { method: "configManager.addGroup", id: e, params: {loginHandle: parseInt(t), groupType: i, groupName: s, groupContact: a, groupPhone: r} }; var o = {cmd: "send", msg: JSON.stringify(n)}; this.sendAlarmMessage(o) }; DHAlarmWeb.prototype.editGroup = function (e, t, i, s, a, r) { var n = { method: "configManager.editGroup", id: e, params: {loginHandle: parseInt(t), groupId: parseInt(i), groupName: s} }; if (a !== "" && a !== null && a !== undefined) { n.params.groupContact = a } if (r !== "" && r !== null && r !== undefined) { n.params.groupPhone = r } var o = {cmd: "send", msg: JSON.stringify(n)}; this.sendAlarmMessage(o) }; DHAlarmWeb.prototype.delGroup = function (e, t, i) { var s = {method: "configManager.delGroup", id: e, params: {loginHandle: parseInt(t), groupId: parseInt(i)}}; var a = {cmd: "send", msg: JSON.stringify(s)}; this.sendAlarmMessage(a) }; DHAlarmWeb.prototype.getUsers = function (e, t) { var i = {method: "configManager.getUsers", id: e, params: {loginHandle: parseInt(t)}}; var s = {cmd: "send", msg: JSON.stringify(i)}; this.sendAlarmMessage(s) }; DHAlarmWeb.prototype.getUserGroups = function (e, t, i) { var s = {method: "configManager.getUserGroups", id: e, params: {loginHandle: parseInt(t), userId: parseInt(i)}}; var a = {cmd: "send", msg: JSON.stringify(s)}; this.sendAlarmMessage(a) }; DHAlarmWeb.prototype.addUser = function (e, t, i, s) { var a = {method: "configManager.addUser", id: e, params: {loginHandle: parseInt(t), userName: i, password: s}}; var r = {cmd: "send", msg: JSON.stringify(a)}; this.sendAlarmMessage(r) }; DHAlarmWeb.prototype.editPassword = function (e, t, i, s) { var a = { method: "configManager.editPassword", id: e, params: {loginHandle: parseInt(t), userId: parseInt(i), password: s} }; var r = {cmd: "send", msg: JSON.stringify(a)}; this.sendAlarmMessage(r) }; DHAlarmWeb.prototype.delUser = function (e, t, i) { var s = {method: "configManager.delUser", id: e, params: {loginHandle: parseInt(t), userId: parseInt(i)}}; var a = {cmd: "send", msg: JSON.stringify(s)}; this.sendAlarmMessage(a) }; DHAlarmWeb.prototype.authorizeGroup = function (e, t, i, s) { var a = { method: "configManager.authorizeGroup", id: e, params: {loginHandle: parseInt(t), userId: parseInt(i), groupIds: s} }; var r = {cmd: "send", msg: JSON.stringify(a)}; this.sendAlarmMessage(r) }; DHAlarmWeb.prototype.editDevice = function (e, t, i, s, a, r) { var n = { method: "configManager.editDevice", id: e, params: {loginHandle: parseInt(t), deviceId: parseInt(i), deviceName: s} }; if (a !== "" && a !== null && a !== undefined) { n.params.deviceContact = a } if (r !== "" && r !== null && r !== undefined) { n.params.devicePhone = r } var o = {cmd: "send", msg: JSON.stringify(n)}; this.sendAlarmMessage(o) }; DHAlarmWeb.prototype.moveDevice = function (e, t, i, s) { var a = { method: "configManager.moveDevice", id: e, params: {loginHandle: parseInt(t), deviceId: parseInt(i), groupId: parseInt(s)} }; var r = {cmd: "send", msg: JSON.stringify(a)}; this.sendAlarmMessage(r) }; DHAlarmWeb.prototype.getGroups = function (e, t) { var i = {method: "configManager.getGroups", id: e, params: {loginHandle: parseInt(t)}}; var s = {cmd: "send", msg: JSON.stringify(i)}; this.sendAlarmMessage(s) }; DHAlarmWeb.prototype.getWebPush = function (e, t, i) { var s = {method: "configManager.getWebPush", id: e, params: {loginHandle: parseInt(t), deviceId: parseInt(i)}}; var a = {cmd: "send", msg: JSON.stringify(s)}; this.sendAlarmMessage(a) }; DHAlarmWeb.prototype.editWebPush = function (e, t, i, s, a, r, n, o) { var l = { method: "configManager.editWebPush", id: e, params: { loginHandle: parseInt(t), deviceId: parseInt(i), url: s, context: a, isAuth: parseInt(r), userName: n, password: o } }; var d = {cmd: "send", msg: JSON.stringify(l)}; this.sendAlarmMessage(d) }; DHAlarmWeb.prototype.startMultiMicTalk = function (e, t) { this.multiTalkType = 1; this.notStartBCWsDids = []; for (let n in t) { if (this.multiTalkDeviceIds.length >= 100) { if (this.onMultiTalkCountLimit) { this.onMultiTalkCountLimit() } return } var i = parseInt(t[n]); if (this.talkDeviceIds.indexOf(i) > -1) continue; var s = this.multiTalkDeviceIds.indexOf(i); var a = {method: "deviceManager.playRT", params: {deviceId: parseInt(i), loginHandle: parseInt(e), preview: false}}; var r = {cmd: "send", msg: JSON.stringify(a)}; if (s == -1) { this.multiTalkDeviceIds.push(i); this.notStartBCWsDids.push(i); this.sendAlarmMessage(r) } if (!this.audioWs[i] || this.audioWs[i].readyState != 1) { this.openAudio(e, i, false, true) } else { var s = this.notStartBCWsDids.indexOf(i); if (s == -1) continue; this.notStartBCWsDids.splice(s, 1) } } if (this.notStartBCWsDids.length == 0) { if (this.onMultiTalkStartFinished) { this.onMultiTalkStartFinished() } this.sendAudio() } }; DHAlarmWeb.prototype.stopMultiMicTalk = function (e, t, i) { if (t) i = this.multiTalkDeviceIds.concat(); for (let t in i) { var s = parseInt(i[t]); var a = {method: "deviceManager.stopRT", params: {deviceId: parseInt(s), loginHandle: parseInt(e)}}; var r = {cmd: "send", msg: JSON.stringify(a)}; var n = this.multiTalkDeviceIds.indexOf(s); if (n > -1) { this.multiTalkDeviceIds.splice(n, 1) } if (this.audioWs[s] && this.talkDeviceIds.indexOf(s) == -1) { this.sendAlarmMessage(r); this.audioWs[s].close(); this.removeTalkDid(s) } } if (this.multiTalkDeviceIds.length == 0) { this.multiTalkType = 0; this.notStartBCWsDids = [] } }; DHAlarmWeb.prototype.startMultiFileTalk = function (e, t, i) { this.multiTalkType = 2; this.multiTalkFilePath = i; this.notStartBCWsDids = []; for (let i in t) { if (this.multiTalkDeviceIds.length >= 100) { if (this.onMultiTalkCountLimit) { this.onMultiTalkCountLimit() } return } var s = parseInt(t[i]); if (this.talkDeviceIds.indexOf(s) > -1) continue; var a = this.multiTalkDeviceIds.indexOf(s); var r = {method: "deviceManager.playRT", params: {deviceId: parseInt(s), loginHandle: parseInt(e), preview: false}}; var n = {cmd: "send", msg: JSON.stringify(r)}; if (a == -1) { this.multiTalkDeviceIds.push(s); this.notStartBCWsDids.push(s); this.sendAlarmMessage(n) } if (!this.audioWs[s] || this.audioWs[s].readyState != 1) { this.openAudio(e, s, false, true) } else { var a = this.notStartBCWsDids.indexOf(s); if (a == -1) continue; this.notStartBCWsDids.splice(a, 1) } } if (this.notStartBCWsDids.length == 0) { if (this.onMultiTalkStartFinished) { this.onMultiTalkStartFinished() } this.sendFileAuido() } }; DHAlarmWeb.prototype.stopMultiFileTalk = function (e, t, i) { if (t) i = this.multiTalkDeviceIds.concat(); for (let o in i) { var s = parseInt(i[o]); var a = {method: "deviceManager.stopRT", params: {deviceId: parseInt(s), loginHandle: parseInt(e)}}; var r = {cmd: "send", msg: JSON.stringify(a)}; var n = this.multiTalkDeviceIds.indexOf(s); if (n == -1) continue; this.multiTalkDeviceIds.splice(n, 1); this.sendAlarmMessage(r); if (this.audioWs[s] && this.talkDeviceIds.indexOf(s) == -1) { this.audioWs[s].close() } if (this.multiFileTalkIntv > 0 && (t || this.multiTalkDeviceIds.length == 0)) { clearInterval(this.multiFileTalkIntv); this.multiFileTalkIntv = 0 } } if (this.multiTalkDeviceIds.length == 0) { this.multiTalkType = 0; this.notStartBCWsDids = [] } }; DHAlarmWeb.prototype.sendFileAuido = function () { if (this.multiFileTalkIntv > 0) return; var e = new AudioContext; var t = new ArrayBuffer(320); var i = new DataView(t); var s = 0; var a = new ArrayBuffer(640); var r = new DataView(a); var n = 0; var o = new XMLHttpRequest; var l = this; o.open("GET", this.multiTalkFilePath, true); o.responseType = "arraybuffer"; o.onload = function () { var o = this.response; e.decodeAudioData(o, function (e) { var o = new OfflineAudioContext(1, parseInt(e.length * 8e3 / e.sampleRate), 8e3); var d = o.createBufferSource(); d.buffer = e; d.connect(o.destination); d.start(); l.bufferFileArr_8k = []; var h = []; o.startRendering().then(function (e) { var a = e.getChannelData(0); d.stop(); var r = 0; var n = a.length; while (r < n) { var o = a[r]; var u = Math.max(-1, Math.min(1, o)); var c = u < 0 ? u * 32768 : u * 32767; if (s < 320) { i.setInt16(s, c, true) } else { var p = t.slice(0, 320); var f = new Int16Array(p); h.push(f); l.bufferFileArr_8k.push(f); i = new DataView(t); s = 0; i.setInt16(s, c, true) } r++; s = s + 2 } if (l.broadcastWs_8k && l.broadcastWs_8k.readyState == WebSocket.OPEN) { l.sendAudio() } let m = 0; if (l.multiFileTalkIntv > 0 || l.broadcastType > 0 || l.multiTalkType == 0) return; l.multiFileTalkIntv = setInterval(function () { if (m == h.length) { clearInterval(l.multiFileTalkIntv); l.multiFileTalkIntv = 0; if (l.onPlayAudioFileEnd) { l.onPlayAudioFileEnd() } h = []; return } if (l.multiTalkType == 2) { let t = h[m]; for (let i in l.multiTalkDeviceIds) { var e = l.multiTalkDeviceIds[i]; if (l.talkDeviceIds.indexOf(e) > -1) continue; if (l.audioWs[e] && l.audioWs[e].readyState == l.audioWs[e].OPEN) { l.audioWs[e].send(t) } } } m++ }, 20) }).catch(function (e) { console.log(e); var t = { clientid: "", error: "fail", msg: {method: "systemManager.onError", error: "renderError", errorMsg: e} }; if (l.onDHAlarmWebError) { l.onDHAlarmWebError(t) } }); var u = new OfflineAudioContext(1, parseInt(e.length * 16e3 / e.sampleRate), 16e3); var c = u.createBufferSource(); c.buffer = e; c.connect(u.destination); c.start(); l.bufferFileArr_16k = []; var p = []; u.startRendering().then(function (e) { var t = e.getChannelData(0); c.stop(); var i = 0; var s = t.length; while (i < s) { var o = t[i]; var d = Math.max(-1, Math.min(1, o)); var h = d < 0 ? d * 32768 : d * 32767; if (n < 640) { r.setInt16(n, h, true) } else { var u = a.slice(0, 640); var f = new Int16Array(u); p.push(f); l.bufferFileArr_16k.push(f); r = new DataView(a); n = 0; r.setInt16(n, h, true) } i++; n = n + 2 } if (l.broadcastWs_16k && l.broadcastWs_16k.readyState == WebSocket.OPEN) { l.sendAudio() } }).catch(function (e) { console.log(e); var t = {clientid: "", error: "fail", msg: {method: "systemManager.onError", error: "renderError"}}; if (l.onDHAlarmWebError) { l.onDHAlarmWebError(t) } }) }, function (e) { console.log(e); var t = {clientid: "", error: "fail", msg: {method: "systemManager.onError", error: "decodeError", errorMsg: e}}; if (l.onDecodeAudioError) { l.onDecodeAudioError(t) } }) }; o.send() }; DHAlarmWeb.prototype.startBroadcast = function (e, t, i, s) { if (i !== 1 && i !== 2) return; this.broadcastType = i; this.bcPlayState = "play"; if (i == 2) this.multiTalkFilePath = s; t = t.map(e => { return +e }); var a = { method: "deviceManager.startBroadcast", params: {loginHandle: parseInt(e), mode: "RealTime", audioparam: {sampleRate: [8e3, 16e3]}, deviceIds: t} }; var r = {cmd: "send", msg: JSON.stringify(a)}; this.sendAlarmMessage(r); if (!this.broadcastWs_8k || this.broadcastWs_8k.readyState != 1) { this.openBrocastWs_8k(e) } if (!this.broadcastWs_16k || this.broadcastWs_16k.readyState != 1) { this.openBrocastWs_16k(e) } if (this.broadcastWs_8k && this.broadcastWs_8k.readyState == WebSocket.OPEN || this.broadcastWs_16k && this.broadcastWs_16k.readyState == WebSocket.OPEN) { if (i == 1) { this.sendAudio() } else { this.sendFileAuido() } } }; DHAlarmWeb.prototype.updateBroadcastFile = function (e) { this.broadcastType = 2; this.bcPlayState = "play"; this.multiTalkFilePath = e; if (this.recorder && this.talkDeviceIds.length == 0) { this.recorder.disconnect(); this.recorder = null; if (this.audioInput) { this.audioInput.disconnect(); this.audioInput = null } } if (!this.broadcastWs_8k || this.broadcastWs_8k.readyState != 1) { this.openBrocastWs_8k(this.loginHandle) } if (!this.broadcastWs_16k || this.broadcastWs_16k.readyState != 1) { this.openBrocastWs_16k(this.loginHandle) } if (this.broadcastWs_8k && this.broadcastWs_8k.readyState == WebSocket.OPEN || this.broadcastWs_16k && this.broadcastWs_16k.readyState == WebSocket.OPEN) { this.sendFileAuido() } }; DHAlarmWeb.prototype.stopBroadcast = function (e) { this.broadcastType = 0; this.bcPlayState = "pause"; var t = {method: "deviceManager.stopBroadcast", params: {loginHandle: parseInt(e)}}; var i = {cmd: "send", msg: JSON.stringify(t)}; this.sendAlarmMessage(i); this.bufferFileArr_8k = []; this.bufferFileArr_16k = []; if (this.recorder && this.talkDeviceIds.length == 0) { this.recorder.disconnect(); this.recorder = null; if (this.audioInput) { this.audioInput.disconnect(); this.audioInput = null } } if (this.broadcastWs_8k && this.broadcastWs_8k.readyState == WebSocket.OPEN) { this.broadcastWs_8k.close() } if (this.broadcastWs_16k && this.broadcastWs_16k.readyState == WebSocket.OPEN) { this.broadcastWs_16k.close() } if (this.multiFileTalkIntv > 0) { clearInterval(this.multiFileTalkIntv) } }; DHAlarmWeb.prototype.setBroadcastState = function (e) { if (e != "play" && e != "pause") return false; this.bcPlayState = e; return true }; DHAlarmWeb.prototype.openBrocastWs_8k = function (e) { this.broadcastWs_8k = new WebSocket(this.alarmUrl); this.broadcastWs_8k.onopen = function () { var t = { cmd: "register", clientid: this.clientid, loginHandle: parseInt(e), sessiontype: "mt8", deviceid: 1, msg: "" }; this.broadcastWs_8k.send(JSON.stringify(t)) }.bind(this); this.broadcastWs_8k.onmessage = function (e) { var t = JSON.parse(e.data); if (t.error == "success") { if (this.broadcastType == 1) { this.sendAudio() } else if (this.broadcastType == 2) { this.sendFileAuido() } } }.bind(this); this.broadcastWs_8k.onclose = function (e) { if (this.onBroadcastWsClosed) { this.onBroadcastWsClosed(e) } }.bind(this); this.broadcastWs_8k.onerror = function (e) { }.bind(this) }; DHAlarmWeb.prototype.openBrocastWs_16k = function (e) { this.broadcastWs_16k = new WebSocket(this.alarmUrl); this.broadcastWs_16k.onopen = function () { var t = { cmd: "register", clientid: this.clientid, loginHandle: parseInt(e), sessiontype: "mt16", deviceid: 1, msg: "" }; this.broadcastWs_16k.send(JSON.stringify(t)) }.bind(this); this.broadcastWs_16k.onmessage = function (e) { var t = JSON.parse(e.data); if (t.error == "success") { if (this.broadcastType == 1) { this.sendAudio() } else if (this.broadcastType == 2) { this.sendFileAuido() } } }.bind(this); this.broadcastWs_16k.onclose = function () { }.bind(this); this.broadcastWs_16k.onerror = function (e) { }.bind(this) }; DHAlarmWeb.prototype.getAudioFileList = function (e, t) { var i = {method: "audioManager.getAudioFileList", id: e, params: {loginHandle: parseInt(t)}}; var s = {cmd: "send", msg: JSON.stringify(i)}; this.sendAlarmMessage(s) }; DHAlarmWeb.prototype.uploadAudioFile = function (e, t, i, s) { var a = new AudioContext; var r = new XMLHttpRequest; var n = this; r.open("GET", s, true); r.responseType = "arraybuffer"; r.onload = function () { var s = this.response; if (s.length > 10 * 1024 * 1024) { var r = { clientid: "", error: "fail", msg: {method: "systemManager.onError", error: "fileError", errorMsg: "the file is too big"} }; if (n.onDecodeAudioError) { n.onDecodeAudioError(r) } } a.decodeAudioData(s, function (s) { n.audioFileStream = s; var a = new ArrayBuffer(640); var r = new DataView(a); var o = 0; var l = new OfflineAudioContext(1, parseInt(s.length * 16e3 / s.sampleRate), 16e3); var d = l.createBufferSource(); d.buffer = s; d.connect(l.destination); d.start(); l.startRendering().then(function (s) { var l = s.getChannelData(0); d.stop(); var h = 0; var u = l.length; while (h < u) { var c = l[h]; var p = Math.max(-1, Math.min(1, c)); var f = p < 0 ? p * 32768 : p * 32767; if (o < 640) { r.setInt16(o, f, true) } else { var m = a.slice(0, 640); var g = new Int16Array(m); n.uploadFileArr.push(g); r = new DataView(a); o = 0; r.setInt16(o, f, true) } h++; o = o + 2 } n.totalUploadFileArr = n.uploadFileArr.concat(); var v = { method: "audioManager.uploadAudioFile", id: e, params: { loginHandle: parseInt(t), file: {name: i, length: n.totalUploadFileArr.length * 640, sampleRate: 16e3, bitsPerSec: 128, encode: "pcm"} } }; var A = {cmd: "send", msg: JSON.stringify(v)}; n.sendAlarmMessage(A) }).catch(function (e) { console.log(e); var t = { clientid: "", error: "fail", msg: {method: "systemManager.onError", error: "renderError", errorMsg: e} }; if (n.onDHAlarmWebError) { n.onDHAlarmWebError(t) } }) }, function (e) { console.log(e); var t = {clientid: "", error: "fail", msg: {method: "systemManager.onError", error: "decodeError", errorMsg: e}}; if (n.onDecodeAudioError) { n.onDecodeAudioError(t) } }) }; r.send() }; DHAlarmWeb.prototype.openAudioUploadWs = function (e) { this.audioUploadWs = new WebSocket(this.alarmUrl); this.audioUploadWs.onopen = function () { var t = { cmd: "register", clientid: this.clientid, loginHandle: parseInt(e), sessiontype: "mt16", deviceid: 1, msg: "" }; this.audioUploadWs.send(JSON.stringify(t)) }.bind(this); this.audioUploadWs.onmessage = function (e) { var t = JSON.parse(e.data); if (t.error != "fail") { if (this.onUploadAudioFileProgress) { var i = 1 - this.uploadFileArr.length / this.totalUploadFileArr.length; if (i < 1) { this.onUploadAudioFileProgress(i.toFixed(4)) } } for (var s = 0; s < 50; s++) { if (this.uploadFileArr.length == 0) break; this.audioUploadWs.send(this.uploadFileArr.shift()) } if (this.overInter > 0) { clearInterval(this.overInter); this.overInter = 0 } if (t.msg == "OVER") { this.onUploadAudioFileProgress(1); this.audioUploadWs.close(); return } if (this.uploadFileArr.length == 0 && t.msg != "OVER") { this.audioUploadWs.send("OVER") } this.overInter = setInterval(function () { if (this.overInter > 0) { if (this.uploadFileArr.length == 0) { this.audioUploadWs.send("OVER") } else { this.audioUploadWs.send("CANCEL") } } }.bind(this), 1e4) } }.bind(this); this.audioUploadWs.onclose = function () { console.log("audioUploadWs closed"); clearInterval(this.overInter) }.bind(this); this.audioUploadWs.onerror = function (e) { console.log("audioUploadWs error"); clearInterval(this.overInter) }.bind(this) }; DHAlarmWeb.prototype.deleteAudioFile = function (e, t, i) { var s = {method: "audioManager.deleteAudioFile", id: e, params: {loginHandle: parseInt(t), fileId: i}}; var a = {cmd: "send", msg: JSON.stringify(s)}; this.sendAlarmMessage(a) }; DHAlarmWeb.prototype.getBCTaskList = function (e, t, i = "timed") { var s = {method: "audioManager.getBCTaskList", id: e, params: {loginHandle: parseInt(t), taskType: i}}; var a = {cmd: "send", msg: JSON.stringify(s)}; this.sendAlarmMessage(a) }; DHAlarmWeb.prototype.uploadBCTask = function (e, t, i, s, a, r, n, o, l, d, h, u, c, p = "timed", f = "file") { var m = {}, g = {}, v = {}; if (h && h.enable) { m.enable = h.enable; if (h.beginDate) m.beginDate = h.beginDate; if (h.endDate) m.endDate = h.endDate } else { m.enable = false; m.beginDate = ""; m.endDate = "" } if (u == "seconds") { g.enable = true; g.seconds = parseInt(c); v.enable = false; v.times = 0 } else if (u == "times") { g.enable = false; g.seconds = 0; v.enable = true; v.times = parseInt(c) } a = a.map(e => { return +e }); r = r.map(e => { return +e }); l = l.map(e => { return +e }); var A = { method: "audioManager.uploadBCTask", id: e, params: { loginHandle: parseInt(t), id: e, task: { fileIds: r, deviceIds: a, name: i, volume: parseInt(s), expire: m, enable: n, duration: g, loop: v, execMode: o, weekDay: l, startTimeOfDay: d, taskType: p, bcType: f } } }; var y = {cmd: "send", msg: JSON.stringify(A)}; this.sendAlarmMessage(y) }; DHAlarmWeb.prototype.editBCTask = function (e, t, i, s, a, r, n, o, l, d, h, u, c, p, f = "timed", m = "file") { var g = {}, v = {}, A = {}; if (u && u.enable) { g.enable = u.enable; if (u.beginDate) g.beginDate = u.beginDate; if (u.endDate) g.endDate = u.endDate } else { g.enable = false; g.beginDate = ""; g.endDate = "" } if (c == "seconds") { v.enable = true; v.seconds = parseInt(p); A.enable = false; A.times = 0 } else if (c == "times") { v.enable = false; v.seconds = 0; A.enable = true; A.times = parseInt(p) } r = r.map(e => { return +e }); n = n.map(e => { return +e }); d = d.map(e => { return +e }); var y = { method: "audioManager.editBCTask", id: e, params: { loginHandle: parseInt(t), id: e, task: { id: parseInt(i), fileIds: n, deviceIds: r, name: s, volume: parseInt(a), expire: g, enable: o, duration: v, loop: A, execMode: l, weekDay: d, startTimeOfDay: h, taskType: f, bcType: m } } }; var b = {cmd: "send", msg: JSON.stringify(y)}; this.sendAlarmMessage(b) }; DHAlarmWeb.prototype.deleteBCTask = function (e, t, i) { var s = {method: "audioManager.deleteBCTask", id: e, params: {loginHandle: parseInt(t), taskId: i}}; var a = {cmd: "send", msg: JSON.stringify(s)}; this.sendAlarmMessage(a) }; DHAlarmWeb.prototype.tryBCTask = function (e, t, i, s) { if (s != "start" && s != "stop") return; var a = {method: "audioManager.tryBCTask", id: e, params: {loginHandle: parseInt(t), taskId: i, action: s}}; var r = {cmd: "send", msg: JSON.stringify(a)}; this.sendAlarmMessage(r) }; DHAlarmWeb.prototype.configPayStatus = function (e, t, i, s, a, r, n, o, l, d, h, u) { var c = this.tunnelDataId++; var p = { id: c, method: "configManager.setConfig", name: "ui.lcd.payStatus", context: "", data: { status: parseInt(s), divCode: parseInt(a), orgCode: parseInt(r), carNumber: n, carType: parseInt(o), qrText: l, duration: d, validDate: h, decimal: u.toString() } }; var f = { method: "deviceManager.configTunnel", params: {loginHandle: parseInt(t), deviceId: parseInt(i), tunnelData: JSON.stringify(p)} }; var m = {cmd: "send", msg: JSON.stringify(f)}; this.sendAlarmMessage(m); var g = {}; g.requestId = e; g.deviceId = i; g.timestamp = Date.parse(new Date) / 1e3; this.setTunnelQueue(c, "configPayStatus", g) }; DHAlarmWeb.prototype.setDeviceExtra = function (e, t, i, s, a, r, n) { var o = { method: "deviceManager.setDeviceExtra", id: e, params: {loginHandle: parseInt(t), deviceId: parseInt(i), location: s, contact: a, phone: r, remark: n} }; var l = {cmd: "send", msg: JSON.stringify(o)}; this.sendAlarmMessage(l) }; DHAlarmWeb.prototype.getDeviceExtra = function (e, t, i) { var s = {method: "deviceManager.getDeviceExtra", id: e, params: {loginHandle: parseInt(t), deviceId: parseInt(i)}}; var a = {cmd: "send", msg: JSON.stringify(s)}; this.sendAlarmMessage(a) }; DHAlarmWeb.prototype.uploadBCTaskRealtime = function (e, t, i, s, a, r, n, o) { a = a.map(e => { return +e }); r = r.map(e => { return +e }); var l = { method: "audioManager.uploadBCTask", id: e, params: { loginHandle: parseInt(t), id: e, task: { fileIds: r, deviceIds: a, name: i, volume: parseInt(s), expire: {enable: false, beginDate: "", endDate: ""}, enable: true, duration: {enable: false, seconds: 0}, loop: {enable: true, times: parseInt(n)}, execMode: "Single", weekDay: [], startTimeOfDay: "", taskType: "realtime", bcType: o } } }; var d = {cmd: "send", msg: JSON.stringify(l)}; this.sendAlarmMessage(d) }; DHAlarmWeb.prototype.editBCTaskRealtime = function (e, t, i, s, a, r, n, o, l) { r = r.map(e => { return +e }); n = n.map(e => { return +e }); var d = { method: "audioManager.editBCTask", id: e, params: { loginHandle: parseInt(t), id: e, task: { id: parseInt(i), fileIds: n, deviceIds: r, name: s, volume: parseInt(a), expire: {enable: false, beginDate: "", endDate: ""}, enable: true, duration: {enable: false, seconds: 0}, loop: {enable: true, times: parseInt(o)}, execMode: "Single", weekDay: [], startTimeOfDay: "", taskType: "realtime", bcType: l } } }; var h = {cmd: "send", msg: JSON.stringify(d)}; this.sendAlarmMessage(h) }; DHAlarmWeb.prototype.getAudioOutputChannels = function (e, t, i) { var s = this.tunnelDataId++; var a = {id: s, method: "devAudioOutput.getAudioOutputChannels", deviceId: parseInt(i), params: {}}; var r = { method: "deviceManager.configTunnel", params: {loginHandle: parseInt(t), deviceId: parseInt(i), tunnelData: JSON.stringify(a)} }; var n = {cmd: "send", msg: JSON.stringify(r)}; this.sendAlarmMessage(n); var o = {}; o.requestId = e; o.deviceId = i; o.timestamp = Date.parse(new Date) / 1e3; this.setTunnelQueue(s, "getAudioOutputChannels", o) }; DHAlarmWeb.prototype.setBroadcastVolume = function (e, t, i, s) { if (Array.isArray(s)) { s = s.map(e => { return +e }) } else { s = [parseInt(s)] } var a = this.tunnelDataId++; var r = {id: a, method: "devAudioOutput.setBroadcastVolume", deviceId: parseInt(i), params: {volume: s}}; var n = { method: "deviceManager.configTunnel", params: {loginHandle: parseInt(t), deviceId: parseInt(i), tunnelData: JSON.stringify(r)} }; var o = {cmd: "send", msg: JSON.stringify(n)}; this.sendAlarmMessage(o); var l = {}; l.requestId = e; l.deviceId = i; l.volume = s; l.timestamp = Date.parse(new Date) / 1e3; this.setTunnelQueue(a, "setBroadcastVolume", l) }; DHAlarmWeb.prototype.getBroadcastVolume = function (e, t, i) { var s = this.tunnelDataId++; var a = {id: s, method: "devAudioOutput.getBroadcastVolume", deviceId: parseInt(i), params: {}}; var r = { method: "deviceManager.configTunnel", params: {loginHandle: parseInt(t), deviceId: parseInt(i), tunnelData: JSON.stringify(a)} }; var n = {cmd: "send", msg: JSON.stringify(r)}; this.sendAlarmMessage(n); var o = {}; o.requestId = e; o.deviceId = i; o.timestamp = Date.parse(new Date) / 1e3; this.setTunnelQueue(s, "getBroadcastVolume", o) }; DHAlarmWeb.prototype.setTunnelQueue = function (e, t, i) { this.tunnelQueue = this.tunnelQueue.filter(e => e.data.timestamp + this.tunnelReqTimeout > i.timestamp); this.tunnelQueue.push({id: e, method: t, data: i}) }; DHAlarmWeb.prototype.getDeviceVolume = function (e, t, i, s) { var a = s == "input" ? "AudioInputVolume" : "AudioOutputVolume"; var r = this.tunnelDataId++; var n = {id: r, method: "configManager.getConfig", name: a, deviceId: parseInt(i), params: {}}; var o = { method: "deviceManager.configTunnel", params: {loginHandle: parseInt(t), deviceId: parseInt(i), tunnelData: JSON.stringify(n)} }; var l = {cmd: "send", msg: JSON.stringify(o)}; this.sendAlarmMessage(l); var d = {}; d.requestId = e; d.deviceId = i; d.type = s; d.timestamp = Date.parse(new Date) / 1e3; this.setTunnelQueue(r, "getDeviceVolume", d) }; DHAlarmWeb.prototype.setDeviceVolume = function (e, t, i, s, a) { if (Array.isArray(a)) { a = a.map(e => { return +e }) } else { a = [parseInt(a)] } var r = s == "input" ? "AudioInputVolume" : "AudioOutputVolume"; var n = this.tunnelDataId++; var o = {id: n, method: "configManager.setConfig", name: r, deviceId: parseInt(i), params: a}; var l = { method: "deviceManager.configTunnel", params: {loginHandle: parseInt(t), deviceId: parseInt(i), tunnelData: JSON.stringify(o)} }; var d = {cmd: "send", msg: JSON.stringify(l)}; this.sendAlarmMessage(d); var h = {}; h.requestId = e; h.deviceId = i; h.type = s; h.timestamp = Date.parse(new Date) / 1e3; this.setTunnelQueue(n, "setDeviceVolume", h) }; DHAlarmWeb.prototype.getRecordList = function (e, t, i, s) { var a = { method: "deviceManager.getRecordList", params: {loginHandle: parseInt(e), deviceId: parseInt(t), beginTime: parseInt(i), endTime: parseInt(s)} }; var r = {cmd: "send", msg: JSON.stringify(a)}; this.sendAlarmMessage(r) }; DHAlarmWeb.prototype.playBack = function (e, t, i, s) { var a = { method: "deviceManager.playBack", params: {loginHandle: parseInt(e), deviceId: parseInt(t), beginTime: parseInt(i)} }; var r = {cmd: "send", msg: JSON.stringify(a)}; this.sendAlarmMessage(r); var n = t + "_" + i; if (!this.pbWfs[n] || this.pbWfs[n].websocketLoader.client.readyState != 1) { this.pbWfs[n] = new Wfs; this.pbWfs[n].attachMedia(s, t, this.clientid, this.loginHandle, this.alarmUrl, "pbvideo", i); this.pbWfs[n].websocketLoader.onVideoFrame = function (e) { this.onVideoFrame(e, t) }.bind(this) } if (!this.pbAudioWs[n] || this.pbAudioWs[n].readyState != 1) { this.openPBAudio(e, t, i) } }; DHAlarmWeb.prototype.openPBAudio = function (e, t, i) { var s = t + "_" + i; this.pbAudioWs[s] = new WebSocket(this.alarmUrl); this.pbAudioWs[s].binaryType = "arraybuffer"; this.pbAudioWs[s].onopen = function () { var e = { cmd: "register", clientid: this.clientid, loginHandle: parseInt(this.loginHandle), sessiontype: "pbaudio", deviceid: parseInt(t), msg: i.toString() }; this.pbAudioWs[s].send(JSON.stringify(e)) }.bind(this); this.pbAudioWs[s].onmessage = function (e) { if (typeof e.data == "object") { var a = new Int16Array(e.data); this.pbBufferPool[s].addFrame(a); if (this.onPBAudioData) { this.onPBAudioData(e.data, t, i) } } else { var r = JSON.parse(e.data); if (r.error == "success") { this.pbPlayer[s] = new PCMPlayer({encoding: "16bitInt", channels: 1, sampleRate: 8e3, flushingTime: 500}); this.pbBufferPool[s] = new DHBufferPool; this.pbBufferPool[s].onFrame = function (e) { this.pbPlayer[s].feed(e); this.pbPlayer[s].volume(1) }.bind(this); this.pbBufferPool[s].startBuffering() } } }.bind(this); this.pbAudioWs[s].onclose = function () { console.log("pbAudioWs onclosed ") }.bind(this); this.pbAudioWs[s].onerror = function (e) { console.log("pbAudioWs onerror " + JSON.stringify(e)) }.bind(this) }; DHAlarmWeb.prototype.stopBack = function (e, t, i) { var s = t + "_" + i; var a = { method: "deviceManager.stopBack", params: {loginHandle: parseInt(e), deviceId: parseInt(t), beginTime: parseInt(i)} }; var r = {cmd: "send", msg: JSON.stringify(a)}; this.sendAlarmMessage(r); if (this.pbAudioWs[s]) { this.pbAudioWs[s].close() } if (this.pbPlayer[s]) { this.pbPlayer[s].destroy() } if (this.pbBufferPool[s]) { this.pbBufferPool[s].stopBuffering() } if (this.pbWfs[s]) { this.pbWfs[s].destroy() } }; "use strict"; var DHBufferPool = function () { this.frameArray = null; this.startTime = null; this.firstFrameTime = null; this.timer = null; this.onFrame = null; this.nrDelay = null; this.sampleRate = null; this.sampleIn = null; this.channel = null; this.intv = null; this.flag = null; this.count = null; this.renderTick = null; this.lastTick = null; this.dropTick = null; this.interval = null }; DHBufferPool.prototype.setOption = function (e) { }; DHBufferPool.prototype.startBuffering = function () { console.log("startBuffering"); this.frameArray = new Array; this.sampleRate = 8e3; this.sampleIn = 16; this.channel = 1; this.flag = 0; this.count = 0; this.renderTick = 0; this.lastTick = 0; this.nrDelay = 5 * 320; this.intv = 35; this.dropTick = 0; this.interval = setInterval(function () { this.consume() }.bind(this), 1) }; DHBufferPool.prototype.stopBuffering = function () { clearInterval(this.interval); this.frameArray.splice(0, this.frameArray.length) }; DHBufferPool.prototype.addFrame = function (e) { if (this.renderTick == 0) { this.renderTick = performance.now() } if (this.renderTick >= this.dropTick + 100) { this.dropTick = this.renderTick; return } this.frameArray.unshift(e) }; DHBufferPool.prototype.consume = function () { var e = performance.now(); if (this.renderTick > 0) { if (e < this.renderTick + 120) { return } } else { return } if (this.lastTick + this.intv > e) { return } this.lastTick = performance.now(); if (this.frameArray.length < 4) return; var t = this.frameArray.pop(); var i = this.frameArray.pop(); var s = new Int16Array(t.length * 2); s.set(t, 0); s.set(i, t.length); if (this.onFrame) { this.onFrame(s) } }; function PCMPlayer(e) { this.init(e) } PCMPlayer.prototype.init = function (e) { var t = {encoding: "16bitInt", channels: 1, sampleRate: 8e3, flushingTime: 1e3}; this.option = Object.assign({}, t, e); this.samples = new Float32Array; this.flush = this.flush.bind(this); this.interval = setInterval(this.flush, this.option.flushingTime); this.maxValue = this.getMaxValue(); this.typedArray = this.getTypedArray(); this.createContext() }; PCMPlayer.prototype.getMaxValue = function () { var e = {"8bitInt": 128, "16bitInt": 32768, "32bitInt": 2147483648, "32bitFloat": 1}; return e[this.option.encoding] ? e[this.option.encoding] : e["16bitInt"] }; PCMPlayer.prototype.getTypedArray = function () { var e = {"8bitInt": Int8Array, "16bitInt": Int16Array, "32bitInt": Int32Array, "32bitFloat": Float32Array}; return e[this.option.encoding] ? e[this.option.encoding] : e["16bitInt"] }; PCMPlayer.prototype.createContext = function () { this.audioCtx = new (window.AudioContext || window.webkitAudioContext); this.gainNode = this.audioCtx.createGain(); this.gainNode.gain.value = 1; this.gainNode.connect(this.audioCtx.destination); this.startTime = this.audioCtx.currentTime }; PCMPlayer.prototype.isTypedArray = function (e) { return e.byteLength && e.buffer && e.buffer.constructor == ArrayBuffer }; PCMPlayer.prototype.feed = function (e) { if (!this.isTypedArray(e)) return; e = this.getFormatedValue(e); var t = new Float32Array(this.samples.length + e.length); t.set(this.samples, 0); t.set(e, this.samples.length); this.samples = t }; PCMPlayer.prototype.getFormatedValue = function (e) { var e = new this.typedArray(e.buffer), t = new Float32Array(e.length), i; for (i = 0; i < e.length; i++) { t[i] = e[i] / this.maxValue } return t }; PCMPlayer.prototype.volume = function (e) { this.gainNode.gain.value = e }; PCMPlayer.prototype.destroy = function () { if (this.interval) { clearInterval(this.interval) } this.samples = null; if (this.audioCtx) { this.audioCtx.close() } this.audioCtx = null }; PCMPlayer.prototype.flush = function () { if (!this.samples.length) return; var e = this.audioCtx.createBufferSource(), t = this.samples.length / this.option.channels, i = this.audioCtx.createBuffer(this.option.channels, t, this.option.sampleRate), s, a, r, n, o; for (a = 0; a < this.option.channels; a++) { s = i.getChannelData(a); r = a; o = 50; for (n = 0; n < t; n++) { s[n] = this.samples[r]; if (n < 50) { s[n] = s[n] * n / 50 } if (n >= t - 51) { s[n] = s[n] * o-- / 50 } r += this.option.channels } } if (this.startTime < this.audioCtx.currentTime) { this.startTime = this.audioCtx.currentTime } e.buffer = i; e.connect(this.gainNode); e.start(this.startTime); this.startTime += i.duration; this.samples = new Float32Array };