dhsdk.min.js 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  1. /**@license dhsdk v2.7.0 Copyright (c) 2021-12-13 diihoo*/
  2. "use strict";
  3. var DHAlarm = DHAlarm || {};
  4. var DHAlarmWeb = function () {
  5. this.localStream = null;
  6. this.pc = null;
  7. this.webrtcWs = null;
  8. this.alarmWs = null;
  9. this.audioWs = {};
  10. this.deviceAudioWs = {};
  11. this.started = false;
  12. this.webrtcSocketOpened = false;
  13. this.alarmSocketOpened = 0;
  14. this.clientid = null;
  15. this.roomid = null;
  16. this.onStreamCallBack = null;
  17. this.alarmUrl = null;
  18. this.webrtcUrl = null;
  19. this.onAlarmMessage = null;
  20. this.onLoging = null;
  21. this.onDeviceList = null;
  22. this.onNotify = null;
  23. this.onPlayRT = null;
  24. this.onPlayRTState = null;
  25. this.onDeviceRemove = null;
  26. this.onParseMsgError = null;
  27. this.uname = null;
  28. this.pwd = null;
  29. this.isLogin = false;
  30. this.loginHandle = null;
  31. this.deviceId = null;
  32. this.context = null;
  33. this.recorder = null;
  34. this.audioInput = null;
  35. this.wfs = {};
  36. DHAlarm.wrc = this;
  37. this.player = {};
  38. this.bufferPool = {};
  39. this.isDeviceAudioPlay = {};
  40. this.onAlarmServerClosed = null;
  41. this.keepAliveInterval = null;
  42. this.onDeviceAudioData = null;
  43. this.onLocalAudioData = null;
  44. this.onDeviceVideoData = null;
  45. this.onDHAlarmWebError = null;
  46. this.hasDeviceList = false;
  47. this.loginState = 0;
  48. this.loginTimer = null;
  49. this.deviceListTimer = null;
  50. this.loginInterval = 0;
  51. this.talkDeviceIds = [];
  52. this.multiTalkDeviceIds = [];
  53. this.notStartBCWsDids = [];
  54. this.multiTalkFilePath = null;
  55. this.multiFileTalkIntv = 0;
  56. this.multiTalkType = 0;
  57. this.sendIds = [];
  58. this.bufferFileArr_8k = [];
  59. this.bufferFileArr_16k = [];
  60. this.broadcastType = 0;
  61. this.remainFileBuffer_8k = 0;
  62. this.remainFileBuffer_16k = 0;
  63. this.bcPlayState = "pause";
  64. this.uploadFilePath = null;
  65. this.audioFileStream = null;
  66. this.audioUploadWs = null;
  67. this.uploadFileArr = [];
  68. this.totalUploadFileArr = [];
  69. this.overInter = 0;
  70. this.tunnelDataId = 1;
  71. this.tunnelQueue = [];
  72. this.tunnelReqTimeout = 20;
  73. this.pbAudioWs = {};
  74. this.pbWfs = {};
  75. this.pbPlayer = {};
  76. this.pbBufferPool = {};
  77. this.dataWsPort = 8088;
  78. this.mediaWsPort = 8088;
  79. this.onGroupList = null;
  80. this.onAddGroup = null;
  81. this.onEditGroup = null;
  82. this.onDelGroup = null;
  83. this.onUserList = null;
  84. this.onUserGroups = null;
  85. this.onAddUser = null;
  86. this.onEditPassword = null;
  87. this.onDelUser = null;
  88. this.onAuthorizeGroup = null;
  89. this.onEditDevice = null;
  90. this.onMoveDevice = null;
  91. this.onGetWebPush = null;
  92. this.onEditWebPush = null;
  93. this.onPlayAudioFileEnd = null;
  94. this.onAlarmMultiTalkStopped = null;
  95. this.onMultiTalkStartFinished = null;
  96. this.onMultiTalkCountLimit = null;
  97. this.onMultiTalkClosedError = null;
  98. this.onStartBroadcast = null;
  99. this.onBroadcastWsClosed = null;
  100. this.onUploadAudioFile = null;
  101. this.onDeleteAudioFile = null;
  102. this.onDecodeAudioError = null;
  103. this.onUploadAudioFileEnd = null;
  104. this.onGetAudioFileList = null;
  105. this.onGetBCTaskList = null;
  106. this.onUploadBCTask = null;
  107. this.onEditBCTask = null;
  108. this.onDeleteBCTask = null;
  109. this.onConfigTunnel = null;
  110. this.onGetDeviceExtra = null;
  111. this.onSetDeviceExtra = null;
  112. this.onGetRecordList = null
  113. };
  114. window.onbeforeunload = function () {
  115. }.bind(this);
  116. DHAlarmWeb.prototype.openAlarmSocket = function () {
  117. console.log("openAlarmSocket");
  118. this.alarmSocketOpened = 1;
  119. this.alarmWs = new WebSocket(this.alarmUrl);
  120. this.alarmWs.onopen = this.onAlarmSocketOpened.bind(this);
  121. this.alarmWs.onmessage = this.onAlarmSocketMessage.bind(this);
  122. this.alarmWs.onclose = this.onAlarmSocketClosed.bind(this);
  123. this.alarmWs.onerror = this.onAlarmSocketError.bind(this)
  124. };
  125. DHAlarmWeb.prototype.onAlarmSocketOpened = function () {
  126. this.alarmSocketOpened = 2;
  127. console.log("onAlarmSocketOpened");
  128. this.registerAlarm()
  129. };
  130. DHAlarmWeb.prototype.registerAlarm = function () {
  131. var e = {cmd: "register", msg: ""};
  132. this.alarmWs.send(JSON.stringify(e))
  133. };
  134. DHAlarmWeb.prototype.onAlarmSocketMessage = function (e) {
  135. try {
  136. var t = JSON.parse(e.data);
  137. this.clientid = t.clientid
  138. } catch (t) {
  139. console.log(e)
  140. }
  141. this.processAlarmMessage(e.data)
  142. };
  143. DHAlarmWeb.prototype.onAlarmSocketError = function (e) {
  144. console.log("onAlarmSocketError: " + JSON.stringify(e));
  145. this.loginState = 0;
  146. var t = {method: "systemManager.onError", error: "socketError"};
  147. var i = {clientid: "", error: "fail", msg: t};
  148. if (this.onDHAlarmWebError) {
  149. this.onDHAlarmWebError(i)
  150. }
  151. this.clearLoginInt()
  152. };
  153. DHAlarmWeb.prototype.onAlarmSocketClosed = function () {
  154. console.log("onAlarmSocketClosed");
  155. var e = this.loginState;
  156. var t = this.alarmSocketOpened;
  157. var i = {clientid: "", error: "fail", msg: {method: "systemManager.onError", error: "loginTimeout"}};
  158. this.alarmSocketOpened = 4;
  159. this.loginState = 0;
  160. this.clearKeepAliveInt();
  161. this.clearLoginInt();
  162. if (this.onDHAlarmWebError && e == 3) {
  163. this.onDHAlarmWebError(i)
  164. }
  165. if (this.onAlarmServerClosed && t != 3) {
  166. this.onAlarmServerClosed()
  167. }
  168. };
  169. DHAlarmWeb.prototype.sendAlarmMessage = function (e) {
  170. var t = JSON.stringify(e);
  171. if (this.alarmWs && this.alarmWs.readyState === WebSocket.OPEN) {
  172. this.alarmWs.send(t)
  173. }
  174. };
  175. DHAlarmWeb.prototype.sendAlarmMessageCallBack = function (e, t) {
  176. var i = JSON.stringify(e);
  177. this.alarmWs.send(i)
  178. };
  179. DHAlarmWeb.prototype.processAlarmMessage = function (e) {
  180. if (this.onAlarmMessage) {
  181. this.onAlarmMessage(e)
  182. }
  183. try {
  184. var t = JSON.parse(e);
  185. var i = JSON.parse(t.msg);
  186. if (i.method == "eventManager.notify") {
  187. var s = i.params;
  188. if (s.code == "DeviceStatus" && s.action == "Start") {
  189. if (this.multiTalkType == 1) {
  190. this.stopMultiMicTalk(this.loginHandle, false, [s.deviceId])
  191. } else if (this.multiTalkType == 2) {
  192. this.stopMultiFileTalk(this.loginHandle, false, [s.deviceId])
  193. }
  194. if (this.onAlarmMultiTalkStopped) {
  195. this.onAlarmMultiTalkStopped(s.deviceId)
  196. }
  197. }
  198. if (this.onNotify) {
  199. this.onNotify(i)
  200. }
  201. } else if (i.method == "configManager.onDeviceList") {
  202. this.hasDeviceList = true;
  203. this.clearDeviceListTimer();
  204. if (this.onDeviceList) {
  205. this.onDeviceList(i)
  206. }
  207. } else if (i.method == "systemManager.onLogin") {
  208. this.clearLoginTimer();
  209. if (i.error == "success") {
  210. this.loginHandle = i.params.loginHandle, this.clientid = t.clientid, this.loginState = 2;
  211. this.clearKeepAliveInt();
  212. this.keepAliveInterval = setInterval(function () {
  213. var e = {
  214. cmd: "send",
  215. clientid: t.clientid,
  216. msg: JSON.stringify({method: "systemManager.keepAlive", params: {loginHandle: i.params.loginHandle}})
  217. };
  218. this.sendAlarmMessage(e)
  219. }.bind(this), 1e4);
  220. this.clearDeviceListTimer();
  221. this.deviceListTimer = setTimeout(function () {
  222. if (!this.hasDeviceList) {
  223. var e = {clientid: "", error: "fail", msg: {method: "systemManager.onError", error: "dataTimeout"}};
  224. if (this.onDHAlarmWebError) {
  225. this.onDHAlarmWebError(e)
  226. }
  227. }
  228. }.bind(this), 1e4)
  229. } else {
  230. this.loginState = 0
  231. }
  232. if (this.onLogin) {
  233. this.onLogin(i)
  234. }
  235. } else if (i.method == "systemManager.onPlayRT") {
  236. if (this.onPlayRT) {
  237. this.onPlayRT(i)
  238. }
  239. } else if (i.method == "systemManager.onPlayRTState") {
  240. if (this.onPlayRTState) {
  241. this.onPlayRTState(i)
  242. }
  243. } else if (i.method == "configManager.onDeviceRemove") {
  244. if (this.onDeviceRemove) {
  245. this.onDeviceRemove(i)
  246. }
  247. } else if (i.method == "configManager.onGroupList") {
  248. if (this.onGroupList) {
  249. this.onGroupList(i)
  250. }
  251. } else if (i.method == "configManager.onAddGroup") {
  252. if (this.onAddGroup) {
  253. this.onAddGroup(i)
  254. }
  255. } else if (i.method == "configManager.onEditGroup") {
  256. if (this.onEditGroup) {
  257. this.onEditGroup(i)
  258. }
  259. } else if (i.method == "configManager.onDelGroup") {
  260. if (this.onDelGroup) {
  261. this.onDelGroup(i)
  262. }
  263. } else if (i.method == "configManager.onUserList") {
  264. if (this.onUserList) {
  265. this.onUserList(i)
  266. }
  267. } else if (i.method == "configManager.onUserGroups") {
  268. if (this.onUserGroups) {
  269. this.onUserGroups(i)
  270. }
  271. } else if (i.method == "configManager.onAddUser") {
  272. if (this.onAddUser) {
  273. this.onAddUser(i)
  274. }
  275. } else if (i.method == "configManager.onEditPassword") {
  276. if (this.onEditPassword) {
  277. this.onEditPassword(i)
  278. }
  279. } else if (i.method == "configManager.onDelUser") {
  280. if (this.onDelUser) {
  281. this.onDelUser(i)
  282. }
  283. } else if (i.method == "configManager.onAuthorizeGroup") {
  284. if (this.onAuthorizeGroup) {
  285. this.onAuthorizeGroup(i)
  286. }
  287. } else if (i.method == "configManager.onEditDevice") {
  288. if (this.onEditDevice) {
  289. this.onEditDevice(i)
  290. }
  291. } else if (i.method == "configManager.onMoveDevice") {
  292. if (this.onMoveDevice) {
  293. this.onMoveDevice(i)
  294. }
  295. } else if (i.method == "configManager.onGetWebPush") {
  296. if (this.onGetWebPush) {
  297. this.onGetWebPush(i)
  298. }
  299. } else if (i.method == "configManager.onEditWebPush") {
  300. if (this.onEditWebPush) {
  301. this.onEditWebPush(i)
  302. }
  303. } else if (i.method == "deviceManager.onStartBroadcast") {
  304. if (this.onStartBroadcast) {
  305. this.onStartBroadcast(i)
  306. }
  307. } else if (i.method == "audioManager.onGetAudioFileList") {
  308. if (this.onGetAudioFileList) {
  309. this.onGetAudioFileList(i)
  310. }
  311. } else if (i.method == "audioManager.onUploadAudioFile") {
  312. if (this.onUploadAudioFile) {
  313. this.onUploadAudioFile(i)
  314. }
  315. if (i.error == "success") {
  316. if (this.audioUploadWs && this.audioUploadWs.readyState == this.audioUploadWs.OPEN) {
  317. for (var a = 0; a < 50; a++) {
  318. if (this.uploadFileArr.length == 0) break;
  319. this.audioUploadWs.send(this.uploadFileArr.shift())
  320. }
  321. if (this.totalUploadFileArr.length <= 50 && this.uploadFileArr.length == 0) {
  322. this.audioUploadWs.send("OVER")
  323. }
  324. if (this.overInter > 0) return;
  325. this.overInter = setInterval(function () {
  326. if (this.overInter > 0) {
  327. if (this.uploadFileArr.length == 0) {
  328. this.audioUploadWs.send("OVER")
  329. } else {
  330. this.audioUploadWs.send("CANCEL")
  331. }
  332. }
  333. }.bind(this), 1e4)
  334. } else {
  335. this.openAudioUploadWs(this.loginHandle)
  336. }
  337. }
  338. } else if (i.method == "audioManager.onDeleteAudioFile") {
  339. if (this.onDeleteAudioFile) {
  340. this.onDeleteAudioFile(i)
  341. }
  342. } else if (i.method == "audioManager.onGetBCTaskList") {
  343. if (this.onGetBCTaskList) {
  344. this.onGetBCTaskList(i)
  345. }
  346. } else if (i.method == "audioManager.onUploadBCTask") {
  347. if (this.onUploadBCTask) {
  348. this.onUploadBCTask(i)
  349. }
  350. } else if (i.method == "audioManager.onEditBCTask") {
  351. if (this.onEditBCTask) {
  352. this.onEditBCTask(i)
  353. }
  354. } else if (i.method == "audioManager.onDeleteBCTask") {
  355. if (this.onDeleteBCTask) {
  356. this.onDeleteBCTask(i)
  357. }
  358. } else if (i.method == "deviceManager.onConfigTunnel") {
  359. var r = JSON.parse(i.params.tunnelData);
  360. if (r == null) return;
  361. var n = this.tunnelQueue.findIndex(e => e.id == r.id);
  362. if (n == -1) {
  363. if (this.onConfigTunnel) {
  364. this.onConfigTunnel(i)
  365. }
  366. return
  367. }
  368. var o = this.tunnelQueue.splice(n, 1)[0];
  369. var l = {};
  370. if (this.onConfigTunnel) {
  371. i.id = o.data.requestId;
  372. this.onConfigTunnel(i)
  373. }
  374. if (o.method == "setBroadcastVolume") {
  375. if (this.onSetBroadcastVolume) {
  376. l.id = o.data.requestId;
  377. l.volume = o.data.volume;
  378. i.params.reqData = l;
  379. this.onSetBroadcastVolume(i.params)
  380. }
  381. } else if (o.method == "getBroadcastVolume") {
  382. if (this.onGetBroadcastVolume) {
  383. l.id = o.data.requestId;
  384. i.params.reqData = l;
  385. this.onGetBroadcastVolume(i.params)
  386. }
  387. } else if (o.method == "configPayStatus") {
  388. if (this.onConfigPayStatus) {
  389. l.id = o.data.requestId;
  390. i.params.reqData = l;
  391. this.onConfigPayStatus(i.params)
  392. }
  393. } else if (o.method == "getDeviceVolume") {
  394. if (this.onGetDeviceVolume) {
  395. l.id = o.data.requestId;
  396. l.type = o.data.type;
  397. i.params.reqData = l;
  398. this.onGetDeviceVolume(i.params)
  399. }
  400. } else if (o.method == "setDeviceVolume") {
  401. if (this.onSetDeviceVolume) {
  402. l.id = o.data.requestId;
  403. l.type = o.data.type;
  404. l.volume = o.data.volume;
  405. i.params.reqData = l;
  406. this.onSetDeviceVolume(i.params)
  407. }
  408. } else if (o.method == "getAudioOutputChannels") {
  409. if (this.onGetAudioOutputChannels) {
  410. l.id = o.data.requestId;
  411. i.params.reqData = l;
  412. this.onGetAudioOutputChannels(i.params)
  413. }
  414. }
  415. } else if (i.method == "deviceManager.onGetDeviceExtra") {
  416. if (this.onGetDeviceExtra) {
  417. this.onGetDeviceExtra(i)
  418. }
  419. } else if (i.method == "deviceManager.onSetDeviceExtra") {
  420. if (this.onSetDeviceExtra) {
  421. this.onSetDeviceExtra(i)
  422. }
  423. } else if (i.method == "deviceManager.onGetRecordList") {
  424. if (this.onGetRecordList) {
  425. this.onGetRecordList(i)
  426. }
  427. }
  428. } catch (e) {
  429. if (this.onParseMsgError) {
  430. this.onParseMsgError(t)
  431. }
  432. console.log(e)
  433. }
  434. };
  435. DHAlarmWeb.prototype.setWebsocketPort = function (e) {
  436. this.dataWsPort = e.dataWsPort;
  437. this.mediaWsPort = e.mediaWsPort
  438. };
  439. DHAlarmWeb.prototype.login = function (e, t, i) {
  440. if (!i) {
  441. return
  442. }
  443. this.alarmUrl = "wss://" + i + ":" + this.dataWsPort + "/ws";
  444. this.webrtcUrl = "wss://" + i + ":8089/ws";
  445. this.uname = e;
  446. this.pwd = t;
  447. if (this.loginState != 0 && this.loginState != 3) return;
  448. if (this.alarmUrl == "" || this.alarmSocketOpened == 0 || this.alarmSocketOpened == 4) {
  449. this.openAlarmSocket()
  450. }
  451. this.loginState = 1;
  452. this.clearLoginInt();
  453. this.loginInterval = setInterval(function () {
  454. if (DHAlarm.wrc.alarmSocketOpened == 2) {
  455. this.clearLoginInt();
  456. var i = {method: "systemManager.login", params: {userName: e, password: t}};
  457. var s = {cmd: "send", msg: JSON.stringify(i)};
  458. DHAlarm.wrc.sendAlarmMessage(s);
  459. this.clearLoginTimer();
  460. this.loginTimer = setTimeout(function () {
  461. if (this.loginState == 1) {
  462. this.loginState = 3;
  463. this.closeAlarmWs()
  464. }
  465. }.bind(this), 1e4)
  466. }
  467. }.bind(this), 1e3)
  468. };
  469. DHAlarmWeb.prototype.logout = function (e) {
  470. if (!e) return;
  471. var t = {method: "systemManager.logout", params: {loginHandle: parseInt(e)}};
  472. var i = {cmd: "send", msg: JSON.stringify(t)};
  473. this.sendAlarmMessage(i);
  474. var s = {cmd: "deregister", msg: ""};
  475. this.sendAlarmMessage(s);
  476. this.clientid = null;
  477. this.clearKeepAliveInt();
  478. this.clearLoginInt();
  479. this.clearLoginTimer();
  480. this.clearDeviceListTimer();
  481. this.hasDeviceList = false;
  482. this.loginState = 0;
  483. this.closeAlarmWs();
  484. for (var a = 0; a < this.audioWs.length; a++) {
  485. if (this.audioWs[a]) {
  486. this.audioWs[a].close()
  487. }
  488. if (this.deviceAudioWs[a]) {
  489. this.deviceAudioWs[a].close()
  490. }
  491. if (this.player[a]) {
  492. this.player[a].destroy()
  493. }
  494. }
  495. for (var a = 0; a < this.pbAudioWs.length; a++) {
  496. if (this.pbAudioWs[a]) {
  497. this.pbAudioWs[a].close()
  498. }
  499. if (this.pbWfs[a]) {
  500. this.pbWfs[a].close()
  501. }
  502. if (this.pbPlayer[a]) {
  503. this.pbPlayer[a].destroy()
  504. }
  505. }
  506. };
  507. DHAlarmWeb.prototype.playRT = function (e, t, i, s) {
  508. if (!t || !i) return;
  509. var a = {method: "deviceManager.playRT", params: {deviceId: parseInt(t), loginHandle: parseInt(i), preview: false}};
  510. var r = {cmd: "send", msg: JSON.stringify(a)};
  511. t = parseInt(t);
  512. if (s != false) {
  513. s = true;
  514. if (this.talkDeviceIds.indexOf(t) == -1) {
  515. this.talkDeviceIds.push(t)
  516. }
  517. }
  518. this.sendAlarmMessage(r);
  519. if (!this.audioWs[t] || this.audioWs[t].readyState != 1) {
  520. this.openAudio(i, t, s, false)
  521. }
  522. if (s) {
  523. this.startTalk(t);
  524. this.playDeviceAudio(t)
  525. }
  526. if (!this.deviceAudioWs[t] || this.deviceAudioWs[t].readyState != 1) {
  527. this.openDeviceAudio(i, t)
  528. }
  529. this.playVideo(e, t, i)
  530. };
  531. DHAlarmWeb.prototype.stopRT = function (e, t) {
  532. if (!e || !t) return;
  533. var i = {method: "deviceManager.stopRT", params: {deviceId: parseInt(e), loginHandle: parseInt(t)}};
  534. var s = {cmd: "send", msg: JSON.stringify(i)};
  535. e = parseInt(e);
  536. this.sendAlarmMessage(s);
  537. this.closeAudio(e);
  538. if (this.deviceAudioWs[e]) {
  539. this.deviceAudioWs[e].close()
  540. }
  541. if (this.player[e]) {
  542. this.player[e].destroy()
  543. }
  544. if (this.bufferPool[e]) {
  545. this.bufferPool[e].stopBuffering()
  546. }
  547. if (this.wfs[e]) {
  548. this.wfs[e].destroy()
  549. }
  550. this.closeDeviceAudio(e);
  551. this.deviceId = null;
  552. this.playDeviceId = null
  553. };
  554. DHAlarmWeb.prototype.doControl = function (e, t, i) {
  555. var s = {
  556. method: "deviceManager.doControl",
  557. params: {deviceId: parseInt(e), loginHandle: parseInt(t), index: parseInt(i)}
  558. };
  559. var a = {cmd: "send", msg: JSON.stringify(s)};
  560. this.sendAlarmMessage(a)
  561. };
  562. DHAlarmWeb.prototype.playVideo = function (e, t, i) {
  563. this.playDeviceId = t;
  564. if (!this.wfs[t] || this.wfs[t].websocketLoader.client.readyState != 1) {
  565. this.wfs[t] = new Wfs;
  566. this.wfs[t].attachMedia(e, t, this.clientid, this.loginHandle, this.alarmUrl);
  567. this.wfs[t].websocketLoader.onVideoFrame = function (e) {
  568. this.onVideoFrame(e, t)
  569. }.bind(this)
  570. }
  571. };
  572. DHAlarmWeb.prototype.onVideoFrame = function (e, t) {
  573. if (this.onDeviceVideoData) {
  574. this.onDeviceVideoData(e, t)
  575. }
  576. };
  577. DHAlarmWeb.prototype.startTalk = function (e) {
  578. e = parseInt(e);
  579. if (this.talkDeviceIds.indexOf(e) == -1) {
  580. this.talkDeviceIds.push(e)
  581. }
  582. var t = this.multiTalkDeviceIds.indexOf(parseInt(e));
  583. if (t > -1) {
  584. this.multiTalkDeviceIds.splice(t, 1)
  585. }
  586. this.sendAudio(e)
  587. };
  588. DHAlarmWeb.prototype.stopTalk = function (e) {
  589. this.removeTalkDid(e)
  590. };
  591. DHAlarmWeb.prototype.registerTalk = function (e) {
  592. var t = {
  593. cmd: "register",
  594. clientid: this.clientid,
  595. loginHandle: parseInt(this.loginHandle),
  596. sessiontype: "talk",
  597. deviceid: parseInt(e),
  598. msg: ""
  599. };
  600. this.audioWs[e].send(JSON.stringify(t))
  601. };
  602. DHAlarmWeb.prototype.openAudio = function (e, t, i, s) {
  603. this.loginHandle = e;
  604. this.deviceId = t;
  605. this.audioWs[t] = new WebSocket(this.alarmUrl);
  606. this.audioWs[t].onopen = function () {
  607. this.onAudioSocketOpened(t)
  608. }.bind(this);
  609. this.audioWs[t].onmessage = function (e) {
  610. this.onAudioSocketMessage(e, t, i, s)
  611. }.bind(this);
  612. this.audioWs[t].onclose = function () {
  613. this.onAudioSocketClosed(t, s)
  614. }.bind(this);
  615. this.audioWs[t].onerror = function (e) {
  616. this.onAudioSocketError(e, t)
  617. }.bind(this)
  618. };
  619. DHAlarmWeb.prototype.onAudioSocketOpened = function (e) {
  620. console.log("onAudioSocketOpened did:" + e);
  621. this.registerTalk(e);
  622. var t = this.notStartBCWsDids.indexOf(e);
  623. if (t == -1) return;
  624. this.notStartBCWsDids.splice(t, 1)
  625. };
  626. DHAlarmWeb.prototype.onAudioSocketMessage = function (e, t, i, s) {
  627. var a = JSON.parse(e.data);
  628. if (a.error == "success") {
  629. if (!i) {
  630. if (this.notStartBCWsDids.length == 0 && s == true) {
  631. if (this.multiTalkType == 1) {
  632. if (this.onMultiTalkStartFinished) {
  633. this.onMultiTalkStartFinished()
  634. }
  635. this.sendAudio()
  636. } else if (this.multiTalkType == 2) {
  637. if (this.onMultiTalkStartFinished) {
  638. this.onMultiTalkStartFinished()
  639. }
  640. setTimeout(function () {
  641. this.sendFileAuido()
  642. }.bind(this), 100)
  643. }
  644. }
  645. return
  646. }
  647. }
  648. };
  649. DHAlarmWeb.prototype.onAudioSocketError = function (e, t) {
  650. console.log("onAudioSocketError: " + JSON.stringify(e))
  651. };
  652. DHAlarmWeb.prototype.onAudioSocketClosed = function (e, t) {
  653. console.log("onAudioSocketClosed did:" + e);
  654. var i = this.multiTalkDeviceIds.indexOf(e);
  655. if (i > -1) {
  656. this.multiTalkDeviceIds.splice(i, 1)
  657. }
  658. var s = this.notStartBCWsDids.indexOf(e);
  659. if (s > -1) {
  660. this.notStartBCWsDids.splice(s, 1)
  661. }
  662. if (t && this.multiTalkType > 0) {
  663. var a = false;
  664. if (this.multiTalkDeviceIds.length == 0) a = true;
  665. if (this.onMultiTalkClosedError) {
  666. this.onMultiTalkClosedError(e, a)
  667. }
  668. }
  669. };
  670. DHAlarmWeb.prototype.sendAudio = function (e) {
  671. if (this.recorder) return;
  672. var t = new ArrayBuffer(320);
  673. var i = new DataView(t);
  674. var s = 0;
  675. var a = new ArrayBuffer(640);
  676. var r = new DataView(a);
  677. var n = 0;
  678. if (navigator.mediaDevices === undefined) {
  679. navigator.mediaDevices = {}
  680. }
  681. if (navigator.mediaDevices.getUserMedia === undefined) {
  682. navigator.mediaDevices.getUserMedia = function (e) {
  683. var t = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
  684. if (!t) {
  685. return Promise.reject(new Error("getUserMedia is not implemented in this browser"))
  686. }
  687. return new Promise(function (i, s) {
  688. t.call(navigator, e, i, s)
  689. })
  690. }
  691. }
  692. navigator.mediaDevices.getUserMedia({audio: true}).then(function (e) {
  693. if (this.recorder) return;
  694. this.context = new AudioContext;
  695. this.recorder = this.context.createScriptProcessor(0, 1, 1);
  696. this.audioInput = this.context.createMediaStreamSource(e);
  697. this.audioInput.connect(this.recorder);
  698. this.recorder.connect(this.context.destination);
  699. this.recorder.onaudioprocess = function (e) {
  700. if (!this.recorder) return;
  701. var o = e.inputBuffer.getChannelData(0);
  702. var l = this.context.createBuffer(1, this.recorder.bufferSize, this.context.sampleRate);
  703. var d = l.getChannelData(0);
  704. for (var h = 0; h < l.length; h++) {
  705. d[h] = o[h]
  706. }
  707. var u = new OfflineAudioContext(1, parseInt(this.recorder.bufferSize * 8e3 / this.context.sampleRate), 8e3);
  708. var c = u.createBufferSource();
  709. c.buffer = l;
  710. c.connect(u.destination);
  711. c.start();
  712. u.startRendering().then(function (e) {
  713. var a = e.getChannelData(0);
  714. c.stop();
  715. var r = 0;
  716. var n = a.length;
  717. if (this.broadcastType == 2 && this.bcPlayState == "play" && this.talkDeviceIds.length == 0) {
  718. if (this.bufferFileArr_8k.length == 0 || this.bufferFileArr_16k.length == 0) {
  719. this.bcPlayState = "pause";
  720. if (this.onPlayAudioFileEnd) {
  721. this.onPlayAudioFileEnd()
  722. }
  723. return
  724. }
  725. for (var o = 0; o < parseInt(n / 160); o++) {
  726. if (this.broadcastWs_8k && this.broadcastWs_8k.readyState == this.broadcastWs_8k.OPEN) {
  727. var l = this.bufferFileArr_8k.shift();
  728. if (!l) continue;
  729. this.broadcastWs_8k.send(l)
  730. }
  731. }
  732. this.remainFileBuffer_8k += n % 160;
  733. if (this.remainFileBuffer_8k >= 160) {
  734. if (this.broadcastWs_8k && this.broadcastWs_8k.readyState == this.broadcastWs_8k.OPEN) {
  735. var l = this.bufferFileArr_8k.shift();
  736. if (!l) return;
  737. this.broadcastWs_8k.send(l);
  738. this.remainFileBuffer_8k -= 160
  739. }
  740. }
  741. return
  742. }
  743. while (r < n) {
  744. var d = a[r];
  745. var h = Math.max(-1, Math.min(1, d));
  746. var u = h < 0 ? h * 32768 : h * 32767;
  747. if (s < 320) {
  748. i.setInt16(s, u, true)
  749. } else {
  750. if (this.multiTalkType == 2) {
  751. this.sendIds = this.talkDeviceIds.concat()
  752. } else {
  753. if (this.multiTalkDeviceIds.length > 0) {
  754. this.sendIds = this.multiTalkDeviceIds.concat();
  755. for (var o in this.talkDeviceIds) {
  756. if (this.sendIds.indexOf(this.talkDeviceIds[o]) == -1) {
  757. this.sendIds.push(this.talkDeviceIds[o])
  758. }
  759. }
  760. } else {
  761. this.sendIds = this.talkDeviceIds.concat()
  762. }
  763. }
  764. var p = t.slice(0, 320);
  765. var f = this;
  766. var m = p;
  767. var g = new Int16Array(m);
  768. if (this.broadcastType == 1 && this.talkDeviceIds.length == 0) {
  769. if (this.broadcastWs_8k && this.broadcastWs_8k.readyState == this.broadcastWs_8k.OPEN && this.bcPlayState == "play") {
  770. this.broadcastWs_8k.send(g)
  771. }
  772. } else {
  773. for (let e in f.sendIds) {
  774. var v = f.sendIds[e];
  775. if (f.audioWs[v] && f.audioWs[v].readyState == f.audioWs[v].OPEN) {
  776. f.audioWs[v].send(g)
  777. }
  778. if (f.onLocalAudioData) {
  779. f.onLocalAudioData(m, v)
  780. }
  781. }
  782. }
  783. i = new DataView(t);
  784. s = 0;
  785. i.setInt16(s, u, true)
  786. }
  787. r++;
  788. s = s + 2
  789. }
  790. }.bind(this));
  791. if (this.broadcastType == 0) return;
  792. var p = new OfflineAudioContext(1, parseInt(this.recorder.bufferSize * 16e3 / this.context.sampleRate), 16e3);
  793. var f = p.createBufferSource();
  794. f.buffer = l;
  795. f.connect(p.destination);
  796. f.start();
  797. p.startRendering().then(function (e) {
  798. var t = e.getChannelData(0);
  799. f.stop();
  800. var i = 0;
  801. var s = t.length;
  802. if (this.broadcastType == 2 && this.bcPlayState == "play") {
  803. for (var o = 0; o < parseInt(s / 320); o++) {
  804. if (this.broadcastWs_16k && this.broadcastWs_16k.readyState == this.broadcastWs_16k.OPEN) {
  805. var l = this.bufferFileArr_16k.shift();
  806. if (!l) continue;
  807. this.broadcastWs_16k.send(l)
  808. }
  809. }
  810. this.remainFileBuffer_16k += s % 320;
  811. if (this.remainFileBuffer_16k >= 320) {
  812. if (this.broadcastWs_16k && this.broadcastWs_16k.readyState == this.broadcastWs_16k.OPEN) {
  813. var l = this.bufferFileArr_16k.shift();
  814. if (!l) return;
  815. this.broadcastWs_16k.send(l);
  816. this.remainFileBuffer_16k -= 320
  817. }
  818. }
  819. return
  820. }
  821. while (i < s) {
  822. var d = t[i];
  823. var h = Math.max(-1, Math.min(1, d));
  824. var u = h < 0 ? h * 32768 : h * 32767;
  825. if (n < 640) {
  826. r.setInt16(n, u, true)
  827. } else {
  828. var c = a.slice(0, 640);
  829. var p = c;
  830. var m = new Int16Array(p);
  831. if (this.broadcastType == 1 && this.talkDeviceIds.length == 0) {
  832. if (this.broadcastWs_16k && this.broadcastWs_16k.readyState == this.broadcastWs_16k.OPEN && this.bcPlayState == "play") {
  833. this.broadcastWs_16k.send(m)
  834. }
  835. }
  836. r = new DataView(a);
  837. n = 0;
  838. r.setInt16(n, u, true)
  839. }
  840. i++;
  841. n = n + 2
  842. }
  843. }.bind(this))
  844. }.bind(this)
  845. }.bind(this)).catch(function (t) {
  846. this.closeAudio(e);
  847. console.log(t.name + ": " + t.message)
  848. }.bind(this))
  849. };
  850. DHAlarmWeb.prototype.closeAudio = function (e) {
  851. this.removeTalkDid(e);
  852. if (this.audioWs[e] && this.multiTalkDeviceIds.indexOf(parseInt(e)) == -1 && this.talkDeviceIds.indexOf(parseInt(e)) == -1) {
  853. this.audioWs[e].close()
  854. }
  855. };
  856. DHAlarmWeb.prototype.removeTalkDid = function (e) {
  857. var t = this.talkDeviceIds.indexOf(parseInt(e));
  858. if (t > -1) {
  859. this.talkDeviceIds.splice(t, 1)
  860. }
  861. if (this.recorder && this.talkDeviceIds.length == 0 && (this.multiTalkDeviceIds.length == 0 || this.multiTalkType == 2) && this.broadcastType == 0) {
  862. this.recorder.disconnect();
  863. this.recorder = null;
  864. if (this.audioInput) {
  865. this.audioInput.disconnect();
  866. this.audioInput = null
  867. }
  868. }
  869. };
  870. DHAlarmWeb.prototype.registerDeviceAudio = function (e) {
  871. var t = {
  872. cmd: "register",
  873. clientid: this.clientid,
  874. loginHandle: parseInt(this.loginHandle),
  875. sessiontype: "audio",
  876. deviceid: parseInt(e),
  877. msg: ""
  878. };
  879. this.deviceAudioWs[e].send(JSON.stringify(t))
  880. };
  881. DHAlarmWeb.prototype.openDeviceAudio = function (e, t) {
  882. this.loginHandle = e;
  883. this.deviceId = t;
  884. this.deviceAudioWs[t] = new WebSocket(this.alarmUrl);
  885. this.deviceAudioWs[t].binaryType = "arraybuffer";
  886. this.deviceAudioWs[t].onopen = function () {
  887. this.onDeviceAudioSocketOpened(t)
  888. }.bind(this);
  889. this.deviceAudioWs[t].onmessage = function (e) {
  890. this.onDeviceAudioSocketMessage(e, t)
  891. }.bind(this);
  892. this.deviceAudioWs[t].onclose = function () {
  893. this.onDeviceAudioSocketClosed(t)
  894. }.bind(this);
  895. this.deviceAudioWs[t].onerror = function (e) {
  896. this.onDeviceAudioSocketError(e)
  897. }.bind(this)
  898. };
  899. DHAlarmWeb.prototype.onDeviceAudioSocketOpened = function (e) {
  900. console.log("onDeviceAudioSocketOpened did:" + e);
  901. this.registerDeviceAudio(e)
  902. };
  903. DHAlarmWeb.prototype.onDeviceAudioSocketMessage = function (e, t) {
  904. if (typeof e.data == "object") {
  905. if (!this.isDeviceAudioPlay || !this.isDeviceAudioPlay[t]) return;
  906. this.parseDeviceAudio(e.data, t);
  907. if (this.onDeviceAudioData) {
  908. this.onDeviceAudioData(e.data, t)
  909. }
  910. } else {
  911. var i = JSON.parse(e.data);
  912. if (i.error == "success") {
  913. this.player[t] = new PCMPlayer({encoding: "16bitInt", channels: 1, sampleRate: 8e3, flushingTime: 500});
  914. this.bufferPool[t] = new DHBufferPool;
  915. this.bufferPool[t].onFrame = function (e) {
  916. this.feedData(e, t)
  917. }.bind(this);
  918. this.bufferPool[t].startBuffering()
  919. }
  920. }
  921. };
  922. DHAlarmWeb.prototype.onDeviceAudioSocketError = function (e) {
  923. console.log("onDeviceAudioSocketError: " + JSON.stringify(e))
  924. };
  925. DHAlarmWeb.prototype.onDeviceAudioSocketClosed = function (e) {
  926. console.log("onDeviceAudioSocketClosed did:" + e)
  927. };
  928. DHAlarmWeb.prototype.parseDeviceAudio = function (e, t) {
  929. var i = new Int16Array(e);
  930. this.bufferPool[t].addFrame(i)
  931. };
  932. DHAlarmWeb.prototype.feedData = function (e, t) {
  933. this.player[t].feed(e);
  934. this.player[t].volume(1)
  935. };
  936. DHAlarmWeb.prototype.playDeviceAudio = function (e) {
  937. this.isDeviceAudioPlay = {};
  938. this.isDeviceAudioPlay[e] = true
  939. };
  940. DHAlarmWeb.prototype.closeDeviceAudio = function (e) {
  941. this.isDeviceAudioPlay = {};
  942. this.isDeviceAudioPlay[e] = false
  943. };
  944. DHAlarmWeb.prototype.clearKeepAliveInt = function () {
  945. if (this.keepAliveInterval) clearInterval(this.keepAliveInterval);
  946. this.keepAliveInterval = 0
  947. };
  948. DHAlarmWeb.prototype.clearLoginInt = function () {
  949. if (this.loginInterval) clearInterval(this.loginInterval);
  950. this.loginInterval = 0
  951. };
  952. DHAlarmWeb.prototype.clearLoginTimer = function () {
  953. if (this.loginTimer) clearTimeout(this.loginTimer);
  954. this.loginTimer = 0
  955. };
  956. DHAlarmWeb.prototype.clearDeviceListTimer = function () {
  957. if (this.deviceListTimer) clearTimeout(this.deviceListTimer);
  958. this.deviceListTimer = 0
  959. };
  960. DHAlarmWeb.prototype.closeAlarmWs = function () {
  961. if (this.alarmWs && this.alarmSocketOpened != 0 && this.alarmSocketOpened != 4) {
  962. this.alarmSocketOpened = 3;
  963. this.alarmWs.close()
  964. }
  965. };
  966. DHAlarmWeb.prototype.addGroup = function (e, t, i, s, a, r) {
  967. var n = {
  968. method: "configManager.addGroup",
  969. id: e,
  970. params: {loginHandle: parseInt(t), groupType: i, groupName: s, groupContact: a, groupPhone: r}
  971. };
  972. var o = {cmd: "send", msg: JSON.stringify(n)};
  973. this.sendAlarmMessage(o)
  974. };
  975. DHAlarmWeb.prototype.editGroup = function (e, t, i, s, a, r) {
  976. var n = {
  977. method: "configManager.editGroup",
  978. id: e,
  979. params: {loginHandle: parseInt(t), groupId: parseInt(i), groupName: s}
  980. };
  981. if (a !== "" && a !== null && a !== undefined) {
  982. n.params.groupContact = a
  983. }
  984. if (r !== "" && r !== null && r !== undefined) {
  985. n.params.groupPhone = r
  986. }
  987. var o = {cmd: "send", msg: JSON.stringify(n)};
  988. this.sendAlarmMessage(o)
  989. };
  990. DHAlarmWeb.prototype.delGroup = function (e, t, i) {
  991. var s = {method: "configManager.delGroup", id: e, params: {loginHandle: parseInt(t), groupId: parseInt(i)}};
  992. var a = {cmd: "send", msg: JSON.stringify(s)};
  993. this.sendAlarmMessage(a)
  994. };
  995. DHAlarmWeb.prototype.getUsers = function (e, t) {
  996. var i = {method: "configManager.getUsers", id: e, params: {loginHandle: parseInt(t)}};
  997. var s = {cmd: "send", msg: JSON.stringify(i)};
  998. this.sendAlarmMessage(s)
  999. };
  1000. DHAlarmWeb.prototype.getUserGroups = function (e, t, i) {
  1001. var s = {method: "configManager.getUserGroups", id: e, params: {loginHandle: parseInt(t), userId: parseInt(i)}};
  1002. var a = {cmd: "send", msg: JSON.stringify(s)};
  1003. this.sendAlarmMessage(a)
  1004. };
  1005. DHAlarmWeb.prototype.addUser = function (e, t, i, s) {
  1006. var a = {method: "configManager.addUser", id: e, params: {loginHandle: parseInt(t), userName: i, password: s}};
  1007. var r = {cmd: "send", msg: JSON.stringify(a)};
  1008. this.sendAlarmMessage(r)
  1009. };
  1010. DHAlarmWeb.prototype.editPassword = function (e, t, i, s) {
  1011. var a = {
  1012. method: "configManager.editPassword",
  1013. id: e,
  1014. params: {loginHandle: parseInt(t), userId: parseInt(i), password: s}
  1015. };
  1016. var r = {cmd: "send", msg: JSON.stringify(a)};
  1017. this.sendAlarmMessage(r)
  1018. };
  1019. DHAlarmWeb.prototype.delUser = function (e, t, i) {
  1020. var s = {method: "configManager.delUser", id: e, params: {loginHandle: parseInt(t), userId: parseInt(i)}};
  1021. var a = {cmd: "send", msg: JSON.stringify(s)};
  1022. this.sendAlarmMessage(a)
  1023. };
  1024. DHAlarmWeb.prototype.authorizeGroup = function (e, t, i, s) {
  1025. var a = {
  1026. method: "configManager.authorizeGroup",
  1027. id: e,
  1028. params: {loginHandle: parseInt(t), userId: parseInt(i), groupIds: s}
  1029. };
  1030. var r = {cmd: "send", msg: JSON.stringify(a)};
  1031. this.sendAlarmMessage(r)
  1032. };
  1033. DHAlarmWeb.prototype.editDevice = function (e, t, i, s, a, r) {
  1034. var n = {
  1035. method: "configManager.editDevice",
  1036. id: e,
  1037. params: {loginHandle: parseInt(t), deviceId: parseInt(i), deviceName: s}
  1038. };
  1039. if (a !== "" && a !== null && a !== undefined) {
  1040. n.params.deviceContact = a
  1041. }
  1042. if (r !== "" && r !== null && r !== undefined) {
  1043. n.params.devicePhone = r
  1044. }
  1045. var o = {cmd: "send", msg: JSON.stringify(n)};
  1046. this.sendAlarmMessage(o)
  1047. };
  1048. DHAlarmWeb.prototype.moveDevice = function (e, t, i, s) {
  1049. var a = {
  1050. method: "configManager.moveDevice",
  1051. id: e,
  1052. params: {loginHandle: parseInt(t), deviceId: parseInt(i), groupId: parseInt(s)}
  1053. };
  1054. var r = {cmd: "send", msg: JSON.stringify(a)};
  1055. this.sendAlarmMessage(r)
  1056. };
  1057. DHAlarmWeb.prototype.getGroups = function (e, t) {
  1058. var i = {method: "configManager.getGroups", id: e, params: {loginHandle: parseInt(t)}};
  1059. var s = {cmd: "send", msg: JSON.stringify(i)};
  1060. this.sendAlarmMessage(s)
  1061. };
  1062. DHAlarmWeb.prototype.getWebPush = function (e, t, i) {
  1063. var s = {method: "configManager.getWebPush", id: e, params: {loginHandle: parseInt(t), deviceId: parseInt(i)}};
  1064. var a = {cmd: "send", msg: JSON.stringify(s)};
  1065. this.sendAlarmMessage(a)
  1066. };
  1067. DHAlarmWeb.prototype.editWebPush = function (e, t, i, s, a, r, n, o) {
  1068. var l = {
  1069. method: "configManager.editWebPush",
  1070. id: e,
  1071. params: {
  1072. loginHandle: parseInt(t),
  1073. deviceId: parseInt(i),
  1074. url: s,
  1075. context: a,
  1076. isAuth: parseInt(r),
  1077. userName: n,
  1078. password: o
  1079. }
  1080. };
  1081. var d = {cmd: "send", msg: JSON.stringify(l)};
  1082. this.sendAlarmMessage(d)
  1083. };
  1084. DHAlarmWeb.prototype.startMultiMicTalk = function (e, t) {
  1085. this.multiTalkType = 1;
  1086. this.notStartBCWsDids = [];
  1087. for (let n in t) {
  1088. if (this.multiTalkDeviceIds.length >= 100) {
  1089. if (this.onMultiTalkCountLimit) {
  1090. this.onMultiTalkCountLimit()
  1091. }
  1092. return
  1093. }
  1094. var i = parseInt(t[n]);
  1095. if (this.talkDeviceIds.indexOf(i) > -1) continue;
  1096. var s = this.multiTalkDeviceIds.indexOf(i);
  1097. var a = {method: "deviceManager.playRT", params: {deviceId: parseInt(i), loginHandle: parseInt(e), preview: false}};
  1098. var r = {cmd: "send", msg: JSON.stringify(a)};
  1099. if (s == -1) {
  1100. this.multiTalkDeviceIds.push(i);
  1101. this.notStartBCWsDids.push(i);
  1102. this.sendAlarmMessage(r)
  1103. }
  1104. if (!this.audioWs[i] || this.audioWs[i].readyState != 1) {
  1105. this.openAudio(e, i, false, true)
  1106. } else {
  1107. var s = this.notStartBCWsDids.indexOf(i);
  1108. if (s == -1) continue;
  1109. this.notStartBCWsDids.splice(s, 1)
  1110. }
  1111. }
  1112. if (this.notStartBCWsDids.length == 0) {
  1113. if (this.onMultiTalkStartFinished) {
  1114. this.onMultiTalkStartFinished()
  1115. }
  1116. this.sendAudio()
  1117. }
  1118. };
  1119. DHAlarmWeb.prototype.stopMultiMicTalk = function (e, t, i) {
  1120. if (t) i = this.multiTalkDeviceIds.concat();
  1121. for (let t in i) {
  1122. var s = parseInt(i[t]);
  1123. var a = {method: "deviceManager.stopRT", params: {deviceId: parseInt(s), loginHandle: parseInt(e)}};
  1124. var r = {cmd: "send", msg: JSON.stringify(a)};
  1125. var n = this.multiTalkDeviceIds.indexOf(s);
  1126. if (n > -1) {
  1127. this.multiTalkDeviceIds.splice(n, 1)
  1128. }
  1129. if (this.audioWs[s] && this.talkDeviceIds.indexOf(s) == -1) {
  1130. this.sendAlarmMessage(r);
  1131. this.audioWs[s].close();
  1132. this.removeTalkDid(s)
  1133. }
  1134. }
  1135. if (this.multiTalkDeviceIds.length == 0) {
  1136. this.multiTalkType = 0;
  1137. this.notStartBCWsDids = []
  1138. }
  1139. };
  1140. DHAlarmWeb.prototype.startMultiFileTalk = function (e, t, i) {
  1141. this.multiTalkType = 2;
  1142. this.multiTalkFilePath = i;
  1143. this.notStartBCWsDids = [];
  1144. for (let i in t) {
  1145. if (this.multiTalkDeviceIds.length >= 100) {
  1146. if (this.onMultiTalkCountLimit) {
  1147. this.onMultiTalkCountLimit()
  1148. }
  1149. return
  1150. }
  1151. var s = parseInt(t[i]);
  1152. if (this.talkDeviceIds.indexOf(s) > -1) continue;
  1153. var a = this.multiTalkDeviceIds.indexOf(s);
  1154. var r = {method: "deviceManager.playRT", params: {deviceId: parseInt(s), loginHandle: parseInt(e), preview: false}};
  1155. var n = {cmd: "send", msg: JSON.stringify(r)};
  1156. if (a == -1) {
  1157. this.multiTalkDeviceIds.push(s);
  1158. this.notStartBCWsDids.push(s);
  1159. this.sendAlarmMessage(n)
  1160. }
  1161. if (!this.audioWs[s] || this.audioWs[s].readyState != 1) {
  1162. this.openAudio(e, s, false, true)
  1163. } else {
  1164. var a = this.notStartBCWsDids.indexOf(s);
  1165. if (a == -1) continue;
  1166. this.notStartBCWsDids.splice(a, 1)
  1167. }
  1168. }
  1169. if (this.notStartBCWsDids.length == 0) {
  1170. if (this.onMultiTalkStartFinished) {
  1171. this.onMultiTalkStartFinished()
  1172. }
  1173. this.sendFileAuido()
  1174. }
  1175. };
  1176. DHAlarmWeb.prototype.stopMultiFileTalk = function (e, t, i) {
  1177. if (t) i = this.multiTalkDeviceIds.concat();
  1178. for (let o in i) {
  1179. var s = parseInt(i[o]);
  1180. var a = {method: "deviceManager.stopRT", params: {deviceId: parseInt(s), loginHandle: parseInt(e)}};
  1181. var r = {cmd: "send", msg: JSON.stringify(a)};
  1182. var n = this.multiTalkDeviceIds.indexOf(s);
  1183. if (n == -1) continue;
  1184. this.multiTalkDeviceIds.splice(n, 1);
  1185. this.sendAlarmMessage(r);
  1186. if (this.audioWs[s] && this.talkDeviceIds.indexOf(s) == -1) {
  1187. this.audioWs[s].close()
  1188. }
  1189. if (this.multiFileTalkIntv > 0 && (t || this.multiTalkDeviceIds.length == 0)) {
  1190. clearInterval(this.multiFileTalkIntv);
  1191. this.multiFileTalkIntv = 0
  1192. }
  1193. }
  1194. if (this.multiTalkDeviceIds.length == 0) {
  1195. this.multiTalkType = 0;
  1196. this.notStartBCWsDids = []
  1197. }
  1198. };
  1199. DHAlarmWeb.prototype.sendFileAuido = function () {
  1200. if (this.multiFileTalkIntv > 0) return;
  1201. var e = new AudioContext;
  1202. var t = new ArrayBuffer(320);
  1203. var i = new DataView(t);
  1204. var s = 0;
  1205. var a = new ArrayBuffer(640);
  1206. var r = new DataView(a);
  1207. var n = 0;
  1208. var o = new XMLHttpRequest;
  1209. var l = this;
  1210. o.open("GET", this.multiTalkFilePath, true);
  1211. o.responseType = "arraybuffer";
  1212. o.onload = function () {
  1213. var o = this.response;
  1214. e.decodeAudioData(o, function (e) {
  1215. var o = new OfflineAudioContext(1, parseInt(e.length * 8e3 / e.sampleRate), 8e3);
  1216. var d = o.createBufferSource();
  1217. d.buffer = e;
  1218. d.connect(o.destination);
  1219. d.start();
  1220. l.bufferFileArr_8k = [];
  1221. var h = [];
  1222. o.startRendering().then(function (e) {
  1223. var a = e.getChannelData(0);
  1224. d.stop();
  1225. var r = 0;
  1226. var n = a.length;
  1227. while (r < n) {
  1228. var o = a[r];
  1229. var u = Math.max(-1, Math.min(1, o));
  1230. var c = u < 0 ? u * 32768 : u * 32767;
  1231. if (s < 320) {
  1232. i.setInt16(s, c, true)
  1233. } else {
  1234. var p = t.slice(0, 320);
  1235. var f = new Int16Array(p);
  1236. h.push(f);
  1237. l.bufferFileArr_8k.push(f);
  1238. i = new DataView(t);
  1239. s = 0;
  1240. i.setInt16(s, c, true)
  1241. }
  1242. r++;
  1243. s = s + 2
  1244. }
  1245. if (l.broadcastWs_8k && l.broadcastWs_8k.readyState == WebSocket.OPEN) {
  1246. l.sendAudio()
  1247. }
  1248. let m = 0;
  1249. if (l.multiFileTalkIntv > 0 || l.broadcastType > 0 || l.multiTalkType == 0) return;
  1250. l.multiFileTalkIntv = setInterval(function () {
  1251. if (m == h.length) {
  1252. clearInterval(l.multiFileTalkIntv);
  1253. l.multiFileTalkIntv = 0;
  1254. if (l.onPlayAudioFileEnd) {
  1255. l.onPlayAudioFileEnd()
  1256. }
  1257. h = [];
  1258. return
  1259. }
  1260. if (l.multiTalkType == 2) {
  1261. let t = h[m];
  1262. for (let i in l.multiTalkDeviceIds) {
  1263. var e = l.multiTalkDeviceIds[i];
  1264. if (l.talkDeviceIds.indexOf(e) > -1) continue;
  1265. if (l.audioWs[e] && l.audioWs[e].readyState == l.audioWs[e].OPEN) {
  1266. l.audioWs[e].send(t)
  1267. }
  1268. }
  1269. }
  1270. m++
  1271. }, 20)
  1272. }).catch(function (e) {
  1273. console.log(e);
  1274. var t = {
  1275. clientid: "",
  1276. error: "fail",
  1277. msg: {method: "systemManager.onError", error: "renderError", errorMsg: e}
  1278. };
  1279. if (l.onDHAlarmWebError) {
  1280. l.onDHAlarmWebError(t)
  1281. }
  1282. });
  1283. var u = new OfflineAudioContext(1, parseInt(e.length * 16e3 / e.sampleRate), 16e3);
  1284. var c = u.createBufferSource();
  1285. c.buffer = e;
  1286. c.connect(u.destination);
  1287. c.start();
  1288. l.bufferFileArr_16k = [];
  1289. var p = [];
  1290. u.startRendering().then(function (e) {
  1291. var t = e.getChannelData(0);
  1292. c.stop();
  1293. var i = 0;
  1294. var s = t.length;
  1295. while (i < s) {
  1296. var o = t[i];
  1297. var d = Math.max(-1, Math.min(1, o));
  1298. var h = d < 0 ? d * 32768 : d * 32767;
  1299. if (n < 640) {
  1300. r.setInt16(n, h, true)
  1301. } else {
  1302. var u = a.slice(0, 640);
  1303. var f = new Int16Array(u);
  1304. p.push(f);
  1305. l.bufferFileArr_16k.push(f);
  1306. r = new DataView(a);
  1307. n = 0;
  1308. r.setInt16(n, h, true)
  1309. }
  1310. i++;
  1311. n = n + 2
  1312. }
  1313. if (l.broadcastWs_16k && l.broadcastWs_16k.readyState == WebSocket.OPEN) {
  1314. l.sendAudio()
  1315. }
  1316. }).catch(function (e) {
  1317. console.log(e);
  1318. var t = {clientid: "", error: "fail", msg: {method: "systemManager.onError", error: "renderError"}};
  1319. if (l.onDHAlarmWebError) {
  1320. l.onDHAlarmWebError(t)
  1321. }
  1322. })
  1323. }, function (e) {
  1324. console.log(e);
  1325. var t = {clientid: "", error: "fail", msg: {method: "systemManager.onError", error: "decodeError", errorMsg: e}};
  1326. if (l.onDecodeAudioError) {
  1327. l.onDecodeAudioError(t)
  1328. }
  1329. })
  1330. };
  1331. o.send()
  1332. };
  1333. DHAlarmWeb.prototype.startBroadcast = function (e, t, i, s) {
  1334. if (i !== 1 && i !== 2) return;
  1335. this.broadcastType = i;
  1336. this.bcPlayState = "play";
  1337. if (i == 2) this.multiTalkFilePath = s;
  1338. t = t.map(e => {
  1339. return +e
  1340. });
  1341. var a = {
  1342. method: "deviceManager.startBroadcast",
  1343. params: {loginHandle: parseInt(e), mode: "RealTime", audioparam: {sampleRate: [8e3, 16e3]}, deviceIds: t}
  1344. };
  1345. var r = {cmd: "send", msg: JSON.stringify(a)};
  1346. this.sendAlarmMessage(r);
  1347. if (!this.broadcastWs_8k || this.broadcastWs_8k.readyState != 1) {
  1348. this.openBrocastWs_8k(e)
  1349. }
  1350. if (!this.broadcastWs_16k || this.broadcastWs_16k.readyState != 1) {
  1351. this.openBrocastWs_16k(e)
  1352. }
  1353. if (this.broadcastWs_8k && this.broadcastWs_8k.readyState == WebSocket.OPEN || this.broadcastWs_16k && this.broadcastWs_16k.readyState == WebSocket.OPEN) {
  1354. if (i == 1) {
  1355. this.sendAudio()
  1356. } else {
  1357. this.sendFileAuido()
  1358. }
  1359. }
  1360. };
  1361. DHAlarmWeb.prototype.updateBroadcastFile = function (e) {
  1362. this.broadcastType = 2;
  1363. this.bcPlayState = "play";
  1364. this.multiTalkFilePath = e;
  1365. if (this.recorder && this.talkDeviceIds.length == 0) {
  1366. this.recorder.disconnect();
  1367. this.recorder = null;
  1368. if (this.audioInput) {
  1369. this.audioInput.disconnect();
  1370. this.audioInput = null
  1371. }
  1372. }
  1373. if (!this.broadcastWs_8k || this.broadcastWs_8k.readyState != 1) {
  1374. this.openBrocastWs_8k(this.loginHandle)
  1375. }
  1376. if (!this.broadcastWs_16k || this.broadcastWs_16k.readyState != 1) {
  1377. this.openBrocastWs_16k(this.loginHandle)
  1378. }
  1379. if (this.broadcastWs_8k && this.broadcastWs_8k.readyState == WebSocket.OPEN || this.broadcastWs_16k && this.broadcastWs_16k.readyState == WebSocket.OPEN) {
  1380. this.sendFileAuido()
  1381. }
  1382. };
  1383. DHAlarmWeb.prototype.stopBroadcast = function (e) {
  1384. this.broadcastType = 0;
  1385. this.bcPlayState = "pause";
  1386. var t = {method: "deviceManager.stopBroadcast", params: {loginHandle: parseInt(e)}};
  1387. var i = {cmd: "send", msg: JSON.stringify(t)};
  1388. this.sendAlarmMessage(i);
  1389. this.bufferFileArr_8k = [];
  1390. this.bufferFileArr_16k = [];
  1391. if (this.recorder && this.talkDeviceIds.length == 0) {
  1392. this.recorder.disconnect();
  1393. this.recorder = null;
  1394. if (this.audioInput) {
  1395. this.audioInput.disconnect();
  1396. this.audioInput = null
  1397. }
  1398. }
  1399. if (this.broadcastWs_8k && this.broadcastWs_8k.readyState == WebSocket.OPEN) {
  1400. this.broadcastWs_8k.close()
  1401. }
  1402. if (this.broadcastWs_16k && this.broadcastWs_16k.readyState == WebSocket.OPEN) {
  1403. this.broadcastWs_16k.close()
  1404. }
  1405. if (this.multiFileTalkIntv > 0) {
  1406. clearInterval(this.multiFileTalkIntv)
  1407. }
  1408. };
  1409. DHAlarmWeb.prototype.setBroadcastState = function (e) {
  1410. if (e != "play" && e != "pause") return false;
  1411. this.bcPlayState = e;
  1412. return true
  1413. };
  1414. DHAlarmWeb.prototype.openBrocastWs_8k = function (e) {
  1415. this.broadcastWs_8k = new WebSocket(this.alarmUrl);
  1416. this.broadcastWs_8k.onopen = function () {
  1417. var t = {
  1418. cmd: "register",
  1419. clientid: this.clientid,
  1420. loginHandle: parseInt(e),
  1421. sessiontype: "mt8",
  1422. deviceid: 1,
  1423. msg: ""
  1424. };
  1425. this.broadcastWs_8k.send(JSON.stringify(t))
  1426. }.bind(this);
  1427. this.broadcastWs_8k.onmessage = function (e) {
  1428. var t = JSON.parse(e.data);
  1429. if (t.error == "success") {
  1430. if (this.broadcastType == 1) {
  1431. this.sendAudio()
  1432. } else if (this.broadcastType == 2) {
  1433. this.sendFileAuido()
  1434. }
  1435. }
  1436. }.bind(this);
  1437. this.broadcastWs_8k.onclose = function (e) {
  1438. if (this.onBroadcastWsClosed) {
  1439. this.onBroadcastWsClosed(e)
  1440. }
  1441. }.bind(this);
  1442. this.broadcastWs_8k.onerror = function (e) {
  1443. }.bind(this)
  1444. };
  1445. DHAlarmWeb.prototype.openBrocastWs_16k = function (e) {
  1446. this.broadcastWs_16k = new WebSocket(this.alarmUrl);
  1447. this.broadcastWs_16k.onopen = function () {
  1448. var t = {
  1449. cmd: "register",
  1450. clientid: this.clientid,
  1451. loginHandle: parseInt(e),
  1452. sessiontype: "mt16",
  1453. deviceid: 1,
  1454. msg: ""
  1455. };
  1456. this.broadcastWs_16k.send(JSON.stringify(t))
  1457. }.bind(this);
  1458. this.broadcastWs_16k.onmessage = function (e) {
  1459. var t = JSON.parse(e.data);
  1460. if (t.error == "success") {
  1461. if (this.broadcastType == 1) {
  1462. this.sendAudio()
  1463. } else if (this.broadcastType == 2) {
  1464. this.sendFileAuido()
  1465. }
  1466. }
  1467. }.bind(this);
  1468. this.broadcastWs_16k.onclose = function () {
  1469. }.bind(this);
  1470. this.broadcastWs_16k.onerror = function (e) {
  1471. }.bind(this)
  1472. };
  1473. DHAlarmWeb.prototype.getAudioFileList = function (e, t) {
  1474. var i = {method: "audioManager.getAudioFileList", id: e, params: {loginHandle: parseInt(t)}};
  1475. var s = {cmd: "send", msg: JSON.stringify(i)};
  1476. this.sendAlarmMessage(s)
  1477. };
  1478. DHAlarmWeb.prototype.uploadAudioFile = function (e, t, i, s) {
  1479. var a = new AudioContext;
  1480. var r = new XMLHttpRequest;
  1481. var n = this;
  1482. r.open("GET", s, true);
  1483. r.responseType = "arraybuffer";
  1484. r.onload = function () {
  1485. var s = this.response;
  1486. if (s.length > 10 * 1024 * 1024) {
  1487. var r = {
  1488. clientid: "",
  1489. error: "fail",
  1490. msg: {method: "systemManager.onError", error: "fileError", errorMsg: "the file is too big"}
  1491. };
  1492. if (n.onDecodeAudioError) {
  1493. n.onDecodeAudioError(r)
  1494. }
  1495. }
  1496. a.decodeAudioData(s, function (s) {
  1497. n.audioFileStream = s;
  1498. var a = new ArrayBuffer(640);
  1499. var r = new DataView(a);
  1500. var o = 0;
  1501. var l = new OfflineAudioContext(1, parseInt(s.length * 16e3 / s.sampleRate), 16e3);
  1502. var d = l.createBufferSource();
  1503. d.buffer = s;
  1504. d.connect(l.destination);
  1505. d.start();
  1506. l.startRendering().then(function (s) {
  1507. var l = s.getChannelData(0);
  1508. d.stop();
  1509. var h = 0;
  1510. var u = l.length;
  1511. while (h < u) {
  1512. var c = l[h];
  1513. var p = Math.max(-1, Math.min(1, c));
  1514. var f = p < 0 ? p * 32768 : p * 32767;
  1515. if (o < 640) {
  1516. r.setInt16(o, f, true)
  1517. } else {
  1518. var m = a.slice(0, 640);
  1519. var g = new Int16Array(m);
  1520. n.uploadFileArr.push(g);
  1521. r = new DataView(a);
  1522. o = 0;
  1523. r.setInt16(o, f, true)
  1524. }
  1525. h++;
  1526. o = o + 2
  1527. }
  1528. n.totalUploadFileArr = n.uploadFileArr.concat();
  1529. var v = {
  1530. method: "audioManager.uploadAudioFile",
  1531. id: e,
  1532. params: {
  1533. loginHandle: parseInt(t),
  1534. file: {name: i, length: n.totalUploadFileArr.length * 640, sampleRate: 16e3, bitsPerSec: 128, encode: "pcm"}
  1535. }
  1536. };
  1537. var A = {cmd: "send", msg: JSON.stringify(v)};
  1538. n.sendAlarmMessage(A)
  1539. }).catch(function (e) {
  1540. console.log(e);
  1541. var t = {
  1542. clientid: "",
  1543. error: "fail",
  1544. msg: {method: "systemManager.onError", error: "renderError", errorMsg: e}
  1545. };
  1546. if (n.onDHAlarmWebError) {
  1547. n.onDHAlarmWebError(t)
  1548. }
  1549. })
  1550. }, function (e) {
  1551. console.log(e);
  1552. var t = {clientid: "", error: "fail", msg: {method: "systemManager.onError", error: "decodeError", errorMsg: e}};
  1553. if (n.onDecodeAudioError) {
  1554. n.onDecodeAudioError(t)
  1555. }
  1556. })
  1557. };
  1558. r.send()
  1559. };
  1560. DHAlarmWeb.prototype.openAudioUploadWs = function (e) {
  1561. this.audioUploadWs = new WebSocket(this.alarmUrl);
  1562. this.audioUploadWs.onopen = function () {
  1563. var t = {
  1564. cmd: "register",
  1565. clientid: this.clientid,
  1566. loginHandle: parseInt(e),
  1567. sessiontype: "mt16",
  1568. deviceid: 1,
  1569. msg: ""
  1570. };
  1571. this.audioUploadWs.send(JSON.stringify(t))
  1572. }.bind(this);
  1573. this.audioUploadWs.onmessage = function (e) {
  1574. var t = JSON.parse(e.data);
  1575. if (t.error != "fail") {
  1576. if (this.onUploadAudioFileProgress) {
  1577. var i = 1 - this.uploadFileArr.length / this.totalUploadFileArr.length;
  1578. if (i < 1) {
  1579. this.onUploadAudioFileProgress(i.toFixed(4))
  1580. }
  1581. }
  1582. for (var s = 0; s < 50; s++) {
  1583. if (this.uploadFileArr.length == 0) break;
  1584. this.audioUploadWs.send(this.uploadFileArr.shift())
  1585. }
  1586. if (this.overInter > 0) {
  1587. clearInterval(this.overInter);
  1588. this.overInter = 0
  1589. }
  1590. if (t.msg == "OVER") {
  1591. this.onUploadAudioFileProgress(1);
  1592. this.audioUploadWs.close();
  1593. return
  1594. }
  1595. if (this.uploadFileArr.length == 0 && t.msg != "OVER") {
  1596. this.audioUploadWs.send("OVER")
  1597. }
  1598. this.overInter = setInterval(function () {
  1599. if (this.overInter > 0) {
  1600. if (this.uploadFileArr.length == 0) {
  1601. this.audioUploadWs.send("OVER")
  1602. } else {
  1603. this.audioUploadWs.send("CANCEL")
  1604. }
  1605. }
  1606. }.bind(this), 1e4)
  1607. }
  1608. }.bind(this);
  1609. this.audioUploadWs.onclose = function () {
  1610. console.log("audioUploadWs closed");
  1611. clearInterval(this.overInter)
  1612. }.bind(this);
  1613. this.audioUploadWs.onerror = function (e) {
  1614. console.log("audioUploadWs error");
  1615. clearInterval(this.overInter)
  1616. }.bind(this)
  1617. };
  1618. DHAlarmWeb.prototype.deleteAudioFile = function (e, t, i) {
  1619. var s = {method: "audioManager.deleteAudioFile", id: e, params: {loginHandle: parseInt(t), fileId: i}};
  1620. var a = {cmd: "send", msg: JSON.stringify(s)};
  1621. this.sendAlarmMessage(a)
  1622. };
  1623. DHAlarmWeb.prototype.getBCTaskList = function (e, t, i = "timed") {
  1624. var s = {method: "audioManager.getBCTaskList", id: e, params: {loginHandle: parseInt(t), taskType: i}};
  1625. var a = {cmd: "send", msg: JSON.stringify(s)};
  1626. this.sendAlarmMessage(a)
  1627. };
  1628. DHAlarmWeb.prototype.uploadBCTask = function (e, t, i, s, a, r, n, o, l, d, h, u, c, p = "timed", f = "file") {
  1629. var m = {}, g = {}, v = {};
  1630. if (h && h.enable) {
  1631. m.enable = h.enable;
  1632. if (h.beginDate) m.beginDate = h.beginDate;
  1633. if (h.endDate) m.endDate = h.endDate
  1634. } else {
  1635. m.enable = false;
  1636. m.beginDate = "";
  1637. m.endDate = ""
  1638. }
  1639. if (u == "seconds") {
  1640. g.enable = true;
  1641. g.seconds = parseInt(c);
  1642. v.enable = false;
  1643. v.times = 0
  1644. } else if (u == "times") {
  1645. g.enable = false;
  1646. g.seconds = 0;
  1647. v.enable = true;
  1648. v.times = parseInt(c)
  1649. }
  1650. a = a.map(e => {
  1651. return +e
  1652. });
  1653. r = r.map(e => {
  1654. return +e
  1655. });
  1656. l = l.map(e => {
  1657. return +e
  1658. });
  1659. var A = {
  1660. method: "audioManager.uploadBCTask",
  1661. id: e,
  1662. params: {
  1663. loginHandle: parseInt(t),
  1664. id: e,
  1665. task: {
  1666. fileIds: r,
  1667. deviceIds: a,
  1668. name: i,
  1669. volume: parseInt(s),
  1670. expire: m,
  1671. enable: n,
  1672. duration: g,
  1673. loop: v,
  1674. execMode: o,
  1675. weekDay: l,
  1676. startTimeOfDay: d,
  1677. taskType: p,
  1678. bcType: f
  1679. }
  1680. }
  1681. };
  1682. var y = {cmd: "send", msg: JSON.stringify(A)};
  1683. this.sendAlarmMessage(y)
  1684. };
  1685. DHAlarmWeb.prototype.editBCTask = function (e, t, i, s, a, r, n, o, l, d, h, u, c, p, f = "timed", m = "file") {
  1686. var g = {}, v = {}, A = {};
  1687. if (u && u.enable) {
  1688. g.enable = u.enable;
  1689. if (u.beginDate) g.beginDate = u.beginDate;
  1690. if (u.endDate) g.endDate = u.endDate
  1691. } else {
  1692. g.enable = false;
  1693. g.beginDate = "";
  1694. g.endDate = ""
  1695. }
  1696. if (c == "seconds") {
  1697. v.enable = true;
  1698. v.seconds = parseInt(p);
  1699. A.enable = false;
  1700. A.times = 0
  1701. } else if (c == "times") {
  1702. v.enable = false;
  1703. v.seconds = 0;
  1704. A.enable = true;
  1705. A.times = parseInt(p)
  1706. }
  1707. r = r.map(e => {
  1708. return +e
  1709. });
  1710. n = n.map(e => {
  1711. return +e
  1712. });
  1713. d = d.map(e => {
  1714. return +e
  1715. });
  1716. var y = {
  1717. method: "audioManager.editBCTask",
  1718. id: e,
  1719. params: {
  1720. loginHandle: parseInt(t),
  1721. id: e,
  1722. task: {
  1723. id: parseInt(i),
  1724. fileIds: n,
  1725. deviceIds: r,
  1726. name: s,
  1727. volume: parseInt(a),
  1728. expire: g,
  1729. enable: o,
  1730. duration: v,
  1731. loop: A,
  1732. execMode: l,
  1733. weekDay: d,
  1734. startTimeOfDay: h,
  1735. taskType: f,
  1736. bcType: m
  1737. }
  1738. }
  1739. };
  1740. var b = {cmd: "send", msg: JSON.stringify(y)};
  1741. this.sendAlarmMessage(b)
  1742. };
  1743. DHAlarmWeb.prototype.deleteBCTask = function (e, t, i) {
  1744. var s = {method: "audioManager.deleteBCTask", id: e, params: {loginHandle: parseInt(t), taskId: i}};
  1745. var a = {cmd: "send", msg: JSON.stringify(s)};
  1746. this.sendAlarmMessage(a)
  1747. };
  1748. DHAlarmWeb.prototype.tryBCTask = function (e, t, i, s) {
  1749. if (s != "start" && s != "stop") return;
  1750. var a = {method: "audioManager.tryBCTask", id: e, params: {loginHandle: parseInt(t), taskId: i, action: s}};
  1751. var r = {cmd: "send", msg: JSON.stringify(a)};
  1752. this.sendAlarmMessage(r)
  1753. };
  1754. DHAlarmWeb.prototype.configPayStatus = function (e, t, i, s, a, r, n, o, l, d, h, u) {
  1755. var c = this.tunnelDataId++;
  1756. var p = {
  1757. id: c,
  1758. method: "configManager.setConfig",
  1759. name: "ui.lcd.payStatus",
  1760. context: "",
  1761. data: {
  1762. status: parseInt(s),
  1763. divCode: parseInt(a),
  1764. orgCode: parseInt(r),
  1765. carNumber: n,
  1766. carType: parseInt(o),
  1767. qrText: l,
  1768. duration: d,
  1769. validDate: h,
  1770. decimal: u.toString()
  1771. }
  1772. };
  1773. var f = {
  1774. method: "deviceManager.configTunnel",
  1775. params: {loginHandle: parseInt(t), deviceId: parseInt(i), tunnelData: JSON.stringify(p)}
  1776. };
  1777. var m = {cmd: "send", msg: JSON.stringify(f)};
  1778. this.sendAlarmMessage(m);
  1779. var g = {};
  1780. g.requestId = e;
  1781. g.deviceId = i;
  1782. g.timestamp = Date.parse(new Date) / 1e3;
  1783. this.setTunnelQueue(c, "configPayStatus", g)
  1784. };
  1785. DHAlarmWeb.prototype.setDeviceExtra = function (e, t, i, s, a, r, n) {
  1786. var o = {
  1787. method: "deviceManager.setDeviceExtra",
  1788. id: e,
  1789. params: {loginHandle: parseInt(t), deviceId: parseInt(i), location: s, contact: a, phone: r, remark: n}
  1790. };
  1791. var l = {cmd: "send", msg: JSON.stringify(o)};
  1792. this.sendAlarmMessage(l)
  1793. };
  1794. DHAlarmWeb.prototype.getDeviceExtra = function (e, t, i) {
  1795. var s = {method: "deviceManager.getDeviceExtra", id: e, params: {loginHandle: parseInt(t), deviceId: parseInt(i)}};
  1796. var a = {cmd: "send", msg: JSON.stringify(s)};
  1797. this.sendAlarmMessage(a)
  1798. };
  1799. DHAlarmWeb.prototype.uploadBCTaskRealtime = function (e, t, i, s, a, r, n, o) {
  1800. a = a.map(e => {
  1801. return +e
  1802. });
  1803. r = r.map(e => {
  1804. return +e
  1805. });
  1806. var l = {
  1807. method: "audioManager.uploadBCTask",
  1808. id: e,
  1809. params: {
  1810. loginHandle: parseInt(t),
  1811. id: e,
  1812. task: {
  1813. fileIds: r,
  1814. deviceIds: a,
  1815. name: i,
  1816. volume: parseInt(s),
  1817. expire: {enable: false, beginDate: "", endDate: ""},
  1818. enable: true,
  1819. duration: {enable: false, seconds: 0},
  1820. loop: {enable: true, times: parseInt(n)},
  1821. execMode: "Single",
  1822. weekDay: [],
  1823. startTimeOfDay: "",
  1824. taskType: "realtime",
  1825. bcType: o
  1826. }
  1827. }
  1828. };
  1829. var d = {cmd: "send", msg: JSON.stringify(l)};
  1830. this.sendAlarmMessage(d)
  1831. };
  1832. DHAlarmWeb.prototype.editBCTaskRealtime = function (e, t, i, s, a, r, n, o, l) {
  1833. r = r.map(e => {
  1834. return +e
  1835. });
  1836. n = n.map(e => {
  1837. return +e
  1838. });
  1839. var d = {
  1840. method: "audioManager.editBCTask",
  1841. id: e,
  1842. params: {
  1843. loginHandle: parseInt(t),
  1844. id: e,
  1845. task: {
  1846. id: parseInt(i),
  1847. fileIds: n,
  1848. deviceIds: r,
  1849. name: s,
  1850. volume: parseInt(a),
  1851. expire: {enable: false, beginDate: "", endDate: ""},
  1852. enable: true,
  1853. duration: {enable: false, seconds: 0},
  1854. loop: {enable: true, times: parseInt(o)},
  1855. execMode: "Single",
  1856. weekDay: [],
  1857. startTimeOfDay: "",
  1858. taskType: "realtime",
  1859. bcType: l
  1860. }
  1861. }
  1862. };
  1863. var h = {cmd: "send", msg: JSON.stringify(d)};
  1864. this.sendAlarmMessage(h)
  1865. };
  1866. DHAlarmWeb.prototype.getAudioOutputChannels = function (e, t, i) {
  1867. var s = this.tunnelDataId++;
  1868. var a = {id: s, method: "devAudioOutput.getAudioOutputChannels", deviceId: parseInt(i), params: {}};
  1869. var r = {
  1870. method: "deviceManager.configTunnel",
  1871. params: {loginHandle: parseInt(t), deviceId: parseInt(i), tunnelData: JSON.stringify(a)}
  1872. };
  1873. var n = {cmd: "send", msg: JSON.stringify(r)};
  1874. this.sendAlarmMessage(n);
  1875. var o = {};
  1876. o.requestId = e;
  1877. o.deviceId = i;
  1878. o.timestamp = Date.parse(new Date) / 1e3;
  1879. this.setTunnelQueue(s, "getAudioOutputChannels", o)
  1880. };
  1881. DHAlarmWeb.prototype.setBroadcastVolume = function (e, t, i, s) {
  1882. if (Array.isArray(s)) {
  1883. s = s.map(e => {
  1884. return +e
  1885. })
  1886. } else {
  1887. s = [parseInt(s)]
  1888. }
  1889. var a = this.tunnelDataId++;
  1890. var r = {id: a, method: "devAudioOutput.setBroadcastVolume", deviceId: parseInt(i), params: {volume: s}};
  1891. var n = {
  1892. method: "deviceManager.configTunnel",
  1893. params: {loginHandle: parseInt(t), deviceId: parseInt(i), tunnelData: JSON.stringify(r)}
  1894. };
  1895. var o = {cmd: "send", msg: JSON.stringify(n)};
  1896. this.sendAlarmMessage(o);
  1897. var l = {};
  1898. l.requestId = e;
  1899. l.deviceId = i;
  1900. l.volume = s;
  1901. l.timestamp = Date.parse(new Date) / 1e3;
  1902. this.setTunnelQueue(a, "setBroadcastVolume", l)
  1903. };
  1904. DHAlarmWeb.prototype.getBroadcastVolume = function (e, t, i) {
  1905. var s = this.tunnelDataId++;
  1906. var a = {id: s, method: "devAudioOutput.getBroadcastVolume", deviceId: parseInt(i), params: {}};
  1907. var r = {
  1908. method: "deviceManager.configTunnel",
  1909. params: {loginHandle: parseInt(t), deviceId: parseInt(i), tunnelData: JSON.stringify(a)}
  1910. };
  1911. var n = {cmd: "send", msg: JSON.stringify(r)};
  1912. this.sendAlarmMessage(n);
  1913. var o = {};
  1914. o.requestId = e;
  1915. o.deviceId = i;
  1916. o.timestamp = Date.parse(new Date) / 1e3;
  1917. this.setTunnelQueue(s, "getBroadcastVolume", o)
  1918. };
  1919. DHAlarmWeb.prototype.setTunnelQueue = function (e, t, i) {
  1920. this.tunnelQueue = this.tunnelQueue.filter(e => e.data.timestamp + this.tunnelReqTimeout > i.timestamp);
  1921. this.tunnelQueue.push({id: e, method: t, data: i})
  1922. };
  1923. DHAlarmWeb.prototype.getDeviceVolume = function (e, t, i, s) {
  1924. var a = s == "input" ? "AudioInputVolume" : "AudioOutputVolume";
  1925. var r = this.tunnelDataId++;
  1926. var n = {id: r, method: "configManager.getConfig", name: a, deviceId: parseInt(i), params: {}};
  1927. var o = {
  1928. method: "deviceManager.configTunnel",
  1929. params: {loginHandle: parseInt(t), deviceId: parseInt(i), tunnelData: JSON.stringify(n)}
  1930. };
  1931. var l = {cmd: "send", msg: JSON.stringify(o)};
  1932. this.sendAlarmMessage(l);
  1933. var d = {};
  1934. d.requestId = e;
  1935. d.deviceId = i;
  1936. d.type = s;
  1937. d.timestamp = Date.parse(new Date) / 1e3;
  1938. this.setTunnelQueue(r, "getDeviceVolume", d)
  1939. };
  1940. DHAlarmWeb.prototype.setDeviceVolume = function (e, t, i, s, a) {
  1941. if (Array.isArray(a)) {
  1942. a = a.map(e => {
  1943. return +e
  1944. })
  1945. } else {
  1946. a = [parseInt(a)]
  1947. }
  1948. var r = s == "input" ? "AudioInputVolume" : "AudioOutputVolume";
  1949. var n = this.tunnelDataId++;
  1950. var o = {id: n, method: "configManager.setConfig", name: r, deviceId: parseInt(i), params: a};
  1951. var l = {
  1952. method: "deviceManager.configTunnel",
  1953. params: {loginHandle: parseInt(t), deviceId: parseInt(i), tunnelData: JSON.stringify(o)}
  1954. };
  1955. var d = {cmd: "send", msg: JSON.stringify(l)};
  1956. this.sendAlarmMessage(d);
  1957. var h = {};
  1958. h.requestId = e;
  1959. h.deviceId = i;
  1960. h.type = s;
  1961. h.timestamp = Date.parse(new Date) / 1e3;
  1962. this.setTunnelQueue(n, "setDeviceVolume", h)
  1963. };
  1964. DHAlarmWeb.prototype.getRecordList = function (e, t, i, s) {
  1965. var a = {
  1966. method: "deviceManager.getRecordList",
  1967. params: {loginHandle: parseInt(e), deviceId: parseInt(t), beginTime: parseInt(i), endTime: parseInt(s)}
  1968. };
  1969. var r = {cmd: "send", msg: JSON.stringify(a)};
  1970. this.sendAlarmMessage(r)
  1971. };
  1972. DHAlarmWeb.prototype.playBack = function (e, t, i, s) {
  1973. var a = {
  1974. method: "deviceManager.playBack",
  1975. params: {loginHandle: parseInt(e), deviceId: parseInt(t), beginTime: parseInt(i)}
  1976. };
  1977. var r = {cmd: "send", msg: JSON.stringify(a)};
  1978. this.sendAlarmMessage(r);
  1979. var n = t + "_" + i;
  1980. if (!this.pbWfs[n] || this.pbWfs[n].websocketLoader.client.readyState != 1) {
  1981. this.pbWfs[n] = new Wfs;
  1982. this.pbWfs[n].attachMedia(s, t, this.clientid, this.loginHandle, this.alarmUrl, "pbvideo", i);
  1983. this.pbWfs[n].websocketLoader.onVideoFrame = function (e) {
  1984. this.onVideoFrame(e, t)
  1985. }.bind(this)
  1986. }
  1987. if (!this.pbAudioWs[n] || this.pbAudioWs[n].readyState != 1) {
  1988. this.openPBAudio(e, t, i)
  1989. }
  1990. };
  1991. DHAlarmWeb.prototype.openPBAudio = function (e, t, i) {
  1992. var s = t + "_" + i;
  1993. this.pbAudioWs[s] = new WebSocket(this.alarmUrl);
  1994. this.pbAudioWs[s].binaryType = "arraybuffer";
  1995. this.pbAudioWs[s].onopen = function () {
  1996. var e = {
  1997. cmd: "register",
  1998. clientid: this.clientid,
  1999. loginHandle: parseInt(this.loginHandle),
  2000. sessiontype: "pbaudio",
  2001. deviceid: parseInt(t),
  2002. msg: i.toString()
  2003. };
  2004. this.pbAudioWs[s].send(JSON.stringify(e))
  2005. }.bind(this);
  2006. this.pbAudioWs[s].onmessage = function (e) {
  2007. if (typeof e.data == "object") {
  2008. var a = new Int16Array(e.data);
  2009. this.pbBufferPool[s].addFrame(a);
  2010. if (this.onPBAudioData) {
  2011. this.onPBAudioData(e.data, t, i)
  2012. }
  2013. } else {
  2014. var r = JSON.parse(e.data);
  2015. if (r.error == "success") {
  2016. this.pbPlayer[s] = new PCMPlayer({encoding: "16bitInt", channels: 1, sampleRate: 8e3, flushingTime: 500});
  2017. this.pbBufferPool[s] = new DHBufferPool;
  2018. this.pbBufferPool[s].onFrame = function (e) {
  2019. this.pbPlayer[s].feed(e);
  2020. this.pbPlayer[s].volume(1)
  2021. }.bind(this);
  2022. this.pbBufferPool[s].startBuffering()
  2023. }
  2024. }
  2025. }.bind(this);
  2026. this.pbAudioWs[s].onclose = function () {
  2027. console.log("pbAudioWs onclosed ")
  2028. }.bind(this);
  2029. this.pbAudioWs[s].onerror = function (e) {
  2030. console.log("pbAudioWs onerror " + JSON.stringify(e))
  2031. }.bind(this)
  2032. };
  2033. DHAlarmWeb.prototype.stopBack = function (e, t, i) {
  2034. var s = t + "_" + i;
  2035. var a = {
  2036. method: "deviceManager.stopBack",
  2037. params: {loginHandle: parseInt(e), deviceId: parseInt(t), beginTime: parseInt(i)}
  2038. };
  2039. var r = {cmd: "send", msg: JSON.stringify(a)};
  2040. this.sendAlarmMessage(r);
  2041. if (this.pbAudioWs[s]) {
  2042. this.pbAudioWs[s].close()
  2043. }
  2044. if (this.pbPlayer[s]) {
  2045. this.pbPlayer[s].destroy()
  2046. }
  2047. if (this.pbBufferPool[s]) {
  2048. this.pbBufferPool[s].stopBuffering()
  2049. }
  2050. if (this.pbWfs[s]) {
  2051. this.pbWfs[s].destroy()
  2052. }
  2053. };
  2054. "use strict";
  2055. var DHBufferPool = function () {
  2056. this.frameArray = null;
  2057. this.startTime = null;
  2058. this.firstFrameTime = null;
  2059. this.timer = null;
  2060. this.onFrame = null;
  2061. this.nrDelay = null;
  2062. this.sampleRate = null;
  2063. this.sampleIn = null;
  2064. this.channel = null;
  2065. this.intv = null;
  2066. this.flag = null;
  2067. this.count = null;
  2068. this.renderTick = null;
  2069. this.lastTick = null;
  2070. this.dropTick = null;
  2071. this.interval = null
  2072. };
  2073. DHBufferPool.prototype.setOption = function (e) {
  2074. };
  2075. DHBufferPool.prototype.startBuffering = function () {
  2076. console.log("startBuffering");
  2077. this.frameArray = new Array;
  2078. this.sampleRate = 8e3;
  2079. this.sampleIn = 16;
  2080. this.channel = 1;
  2081. this.flag = 0;
  2082. this.count = 0;
  2083. this.renderTick = 0;
  2084. this.lastTick = 0;
  2085. this.nrDelay = 5 * 320;
  2086. this.intv = 35;
  2087. this.dropTick = 0;
  2088. this.interval = setInterval(function () {
  2089. this.consume()
  2090. }.bind(this), 1)
  2091. };
  2092. DHBufferPool.prototype.stopBuffering = function () {
  2093. clearInterval(this.interval);
  2094. this.frameArray.splice(0, this.frameArray.length)
  2095. };
  2096. DHBufferPool.prototype.addFrame = function (e) {
  2097. if (this.renderTick == 0) {
  2098. this.renderTick = performance.now()
  2099. }
  2100. if (this.renderTick >= this.dropTick + 100) {
  2101. this.dropTick = this.renderTick;
  2102. return
  2103. }
  2104. this.frameArray.unshift(e)
  2105. };
  2106. DHBufferPool.prototype.consume = function () {
  2107. var e = performance.now();
  2108. if (this.renderTick > 0) {
  2109. if (e < this.renderTick + 120) {
  2110. return
  2111. }
  2112. } else {
  2113. return
  2114. }
  2115. if (this.lastTick + this.intv > e) {
  2116. return
  2117. }
  2118. this.lastTick = performance.now();
  2119. if (this.frameArray.length < 4) return;
  2120. var t = this.frameArray.pop();
  2121. var i = this.frameArray.pop();
  2122. var s = new Int16Array(t.length * 2);
  2123. s.set(t, 0);
  2124. s.set(i, t.length);
  2125. if (this.onFrame) {
  2126. this.onFrame(s)
  2127. }
  2128. };
  2129. function PCMPlayer(e) {
  2130. this.init(e)
  2131. }
  2132. PCMPlayer.prototype.init = function (e) {
  2133. var t = {encoding: "16bitInt", channels: 1, sampleRate: 8e3, flushingTime: 1e3};
  2134. this.option = Object.assign({}, t, e);
  2135. this.samples = new Float32Array;
  2136. this.flush = this.flush.bind(this);
  2137. this.interval = setInterval(this.flush, this.option.flushingTime);
  2138. this.maxValue = this.getMaxValue();
  2139. this.typedArray = this.getTypedArray();
  2140. this.createContext()
  2141. };
  2142. PCMPlayer.prototype.getMaxValue = function () {
  2143. var e = {"8bitInt": 128, "16bitInt": 32768, "32bitInt": 2147483648, "32bitFloat": 1};
  2144. return e[this.option.encoding] ? e[this.option.encoding] : e["16bitInt"]
  2145. };
  2146. PCMPlayer.prototype.getTypedArray = function () {
  2147. var e = {"8bitInt": Int8Array, "16bitInt": Int16Array, "32bitInt": Int32Array, "32bitFloat": Float32Array};
  2148. return e[this.option.encoding] ? e[this.option.encoding] : e["16bitInt"]
  2149. };
  2150. PCMPlayer.prototype.createContext = function () {
  2151. this.audioCtx = new (window.AudioContext || window.webkitAudioContext);
  2152. this.gainNode = this.audioCtx.createGain();
  2153. this.gainNode.gain.value = 1;
  2154. this.gainNode.connect(this.audioCtx.destination);
  2155. this.startTime = this.audioCtx.currentTime
  2156. };
  2157. PCMPlayer.prototype.isTypedArray = function (e) {
  2158. return e.byteLength && e.buffer && e.buffer.constructor == ArrayBuffer
  2159. };
  2160. PCMPlayer.prototype.feed = function (e) {
  2161. if (!this.isTypedArray(e)) return;
  2162. e = this.getFormatedValue(e);
  2163. var t = new Float32Array(this.samples.length + e.length);
  2164. t.set(this.samples, 0);
  2165. t.set(e, this.samples.length);
  2166. this.samples = t
  2167. };
  2168. PCMPlayer.prototype.getFormatedValue = function (e) {
  2169. var e = new this.typedArray(e.buffer), t = new Float32Array(e.length), i;
  2170. for (i = 0; i < e.length; i++) {
  2171. t[i] = e[i] / this.maxValue
  2172. }
  2173. return t
  2174. };
  2175. PCMPlayer.prototype.volume = function (e) {
  2176. this.gainNode.gain.value = e
  2177. };
  2178. PCMPlayer.prototype.destroy = function () {
  2179. if (this.interval) {
  2180. clearInterval(this.interval)
  2181. }
  2182. this.samples = null;
  2183. if (this.audioCtx) {
  2184. this.audioCtx.close()
  2185. }
  2186. this.audioCtx = null
  2187. };
  2188. PCMPlayer.prototype.flush = function () {
  2189. if (!this.samples.length) return;
  2190. var e = this.audioCtx.createBufferSource(), t = this.samples.length / this.option.channels,
  2191. i = this.audioCtx.createBuffer(this.option.channels, t, this.option.sampleRate), s, a, r, n, o;
  2192. for (a = 0; a < this.option.channels; a++) {
  2193. s = i.getChannelData(a);
  2194. r = a;
  2195. o = 50;
  2196. for (n = 0; n < t; n++) {
  2197. s[n] = this.samples[r];
  2198. if (n < 50) {
  2199. s[n] = s[n] * n / 50
  2200. }
  2201. if (n >= t - 51) {
  2202. s[n] = s[n] * o-- / 50
  2203. }
  2204. r += this.option.channels
  2205. }
  2206. }
  2207. if (this.startTime < this.audioCtx.currentTime) {
  2208. this.startTime = this.audioCtx.currentTime
  2209. }
  2210. e.buffer = i;
  2211. e.connect(this.gainNode);
  2212. e.start(this.startTime);
  2213. this.startTime += i.duration;
  2214. this.samples = new Float32Array
  2215. };