| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217 |
- /**@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
- };
|