hLiveManage.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. var _this, baseTok = null, heAnth = null;
  2. var nowHeml = {};
  3. var playSucc = false;
  4. var lightSucc = false;
  5. var sip_id = null,
  6. sip_pwd = null,
  7. sip_host = null,
  8. wss_url = null,
  9. stun_host = null,
  10. turn_host = null,
  11. turn_pwd = null,
  12. turn_user = null,
  13. device_sipId = null;
  14. var userAgent = null;
  15. var sipsession = null;
  16. var errorNum = 0;
  17. var remoteVideo = document.getElementById("helmeVi");
  18. $(document).ready(function(){
  19. heAnth = JSON.parse(localStorage.getItem("nowShip"));
  20. initAjax();
  21. sipInit();
  22. })
  23. window.addEventListener('message',(respon)=>{
  24. let _data = respon.data;
  25. if(_data.type == "helmet"){
  26. if(_data.cmd == "ma_open_rtsp"&& _data.status){
  27. let _ind = _data.play_url.findIndex((i,ind) => {return i.indexOf("webrtc://") != -1;});
  28. startPlay(_data.api_url, _data.play_url[_ind]);
  29. $(".playSucc").css("display","none");
  30. }else if(_data.cmd == "ma_stop_rtsp"&& _data.status){
  31. $(".playSucc").css("display","block");
  32. playSucc = false;
  33. }else if(_data.cmd == "ma_push_take_photo"){
  34. if(_data.status){
  35. ShowLoading("截图成功!",1);
  36. return;
  37. }else{
  38. ShowLoading(_data.msg,1);
  39. return;
  40. }
  41. }else if(_data.msg.indexOf("打开手") != -1){
  42. lightSucc = true;
  43. }else if(_data.msg.indexOf("关闭手") != -1){
  44. lightSucc = false;
  45. }
  46. }else{
  47. if(playSucc){
  48. stopPlay();
  49. }
  50. heAnth = JSON.parse(localStorage.getItem("nowShip"));
  51. initAjax();
  52. }
  53. })
  54. function sipInit() {
  55. let _obj = JSON.parse(localStorage.getItem("sipInfo"));
  56. sip_id = _obj["sip_id"];
  57. sip_pwd = _obj["sip_pwd"];
  58. sip_host = _obj["sip_host"];
  59. wss_url = _obj["wss_url"];
  60. stun_host = _obj["stun_host"];
  61. turn_host = _obj["turn_host"];
  62. turn_pwd = _obj["turn_pwd"];
  63. turn_user = _obj["turn_user"];
  64. var e = {
  65. uri: sip_id + '@' + sip_host, //此sip_id为拨打者账号
  66. transportOptions: {
  67. wsServers: [wss_url],
  68. connectionTimeout: 30
  69. },
  70. authorizationUser: sip_id,
  71. password: sip_pwd,
  72. sessionDescriptionHandlerFactoryOptions: {
  73. peerConnectionOptions: {
  74. rtcConfiguration: {
  75. iceServers: [{
  76. urls: 'stun:' + stun_host
  77. },
  78. {
  79. urls: 'turn:' + turn_host,
  80. username: turn_user,
  81. credential: turn_pwd
  82. }
  83. ]
  84. }
  85. }
  86. }
  87. };
  88. userAgent = new SIP.UA(e);
  89. userAgent.on("registered", function() {
  90. console.log("registered ok");
  91. })
  92. userAgent.on("registrationFailed", function(){
  93. console.log("registrationFailed ok")
  94. })
  95. userAgent.on("invite", function(session) {
  96. var url = session.remoteIdentity.uri.toString() + "--->call";
  97. var isaccept = confirm(url);
  98. if (isaccept) {
  99. //接收来电
  100. session.accept({
  101. sessionDescriptionHandlerOptions: {
  102. constraints: {
  103. audio: true,
  104. video: false
  105. }
  106. }
  107. });
  108. sipsession = session;
  109. session.on("accepted", function() {
  110. var pc = session.sessionDescriptionHandler.peerConnection;
  111. var remoteStream = new MediaStream();
  112. pc.getReceivers().forEach(function(receiver) {
  113. remoteStream.addTrack(receiver.track);
  114. });
  115. remoteVideo.srcObject = remoteStream;
  116. remoteVideo.play();
  117. if (pc.getSenders()) {
  118. var localStream = new MediaStream();
  119. pc.getSenders().forEach(function(sender) {
  120. localStream.addTrack(sender.track);
  121. });
  122. localVideo.srcObject = localStream;
  123. localVideo.play();
  124. }
  125. });
  126. } else {
  127. //拒绝来电
  128. session.reject();
  129. }
  130. })
  131. }
  132. function initAjax(){
  133. if(heAnth !=null && heAnth != "null"&&heAnth.other_info !=null && heAnth.other_info != ""){
  134. var dsd = heAnth.other_info.split(",");
  135. let _opts = {
  136. url: helUrl + "/index.php?ctl=bruce&act=get_group_member_list",
  137. type:"POST"
  138. };
  139. let _data = {
  140. admin_id: 7903,
  141. udid: "11111111",
  142. g_id: dsd[0]
  143. };
  144. postAxios(_opts, _data, {}).then((res) =>{
  145. if(res.status){
  146. let _obj = res.data;
  147. let sipList = JSON.parse(localStorage.getItem("sipLists")) || [];
  148. let _shui = _obj.filter(item => item.device_id == dsd[1])[0];
  149. let _anm = sipList.filter(sipitem => sipitem.user_info.device_id == _shui.device_id);
  150. if(_anm.length){
  151. _shui["locaInfo"] = _anm[0]["location_info"];
  152. }
  153. nowHeml = _shui;
  154. setValhtml();
  155. // changePlay();
  156. }else{
  157. nowHeml = {};
  158. setValhtml();
  159. }
  160. })
  161. }else{
  162. nowHeml = {};
  163. setValhtml();
  164. }
  165. };
  166. function setValhtml(){
  167. let _tem = nowHeml.locaInfo?nowHeml.locaInfo.tempdata:"--0000";
  168. $("#helName").text(nowHeml.nick);
  169. $("#helId").text(nowHeml.device_id);
  170. $("#Elec").text(nowHeml.locaInfo?nowHeml.locaInfo.bat_l:"--");
  171. $("#Temp").text((parseInt(_tem.split("--").pop())/1000).toFixed(1));
  172. $("#Flow").text(nowHeml.locaInfo?nowHeml.locaInfo.sim_data_num:"--");
  173. $("#Signal").text(nowHeml.locaInfo?(nowHeml.locaInfo.net_strenth<2?"弱":(nowHeml.locaInfo.net_strenth<4?"中":"强")):"--");
  174. }
  175. function jumpToSee(key){
  176. top.location.href = "./"+key+".html";
  177. };
  178. function changePlay(){
  179. errorNum = 0;
  180. if(nowHeml.on_line == 0){
  181. ShowLoading("安全帽离线中!",1);
  182. return;
  183. }else{
  184. window.parent.postMessage({
  185. act: "ma_open_rtsp",
  186. device_id: nowHeml.device_id,
  187. },staticUrl+ "/");
  188. }
  189. };
  190. function stopPlay(){
  191. if(nowHeml.on_line == 0){
  192. ShowLoading("安全帽离线中!",1);
  193. return;
  194. }else{
  195. window.parent.postMessage({
  196. act: "ma_stop_rtsp",
  197. device_id: nowHeml.device_id,
  198. },staticUrl+ "/");
  199. }
  200. }
  201. var apiVal = null;
  202. var urlVal = null;
  203. function startPlay(_api, _url){
  204. var video = document.getElementById('playWind');
  205. var player = new JSWebrtc.Player(_api, _url, {
  206. video: video,
  207. autoplay: !0,
  208. onPlay: onPlay,
  209. onFail: onFail
  210. });
  211. apiVal = _api;
  212. urlVal = _url;
  213. };
  214. function onPlay() {
  215. playSucc = true;
  216. };
  217. function onFail() {
  218. if(errorNum < 5){
  219. errorNum += 1;
  220. setTimeout(function() {
  221. startPlay(apiVal, urlVal);
  222. }, 1000)
  223. }else{
  224. setTimeout(function() {
  225. stopPlay();
  226. }, 1000)
  227. }
  228. }
  229. // 截图
  230. function handleCamer(){
  231. if(nowHeml.on_line == 0){
  232. ShowLoading("安全帽离线中!",1);
  233. return;
  234. }else{
  235. window.parent.postMessage({
  236. act: "ma_push_take_photo",
  237. user_id: nowHeml.user_id,
  238. device_id: nowHeml.device_id
  239. },staticUrl+ "/");
  240. }
  241. }
  242. // 手电筒
  243. function handleLight(){
  244. let _uuid = guid();
  245. window.parent.postMessage({
  246. device_id: nowHeml.device_id,
  247. act: "push_to_client",
  248. data: {
  249. cmd: "server_push_turnLights",
  250. talkback_id: _uuid,
  251. type: lightSucc?0:1
  252. },
  253. p_id: nowHeml.user_id,
  254. p_type: nowHeml.cap_type,
  255. sendback: 1,
  256. talkback_id: _uuid
  257. },staticUrl+ "/");
  258. }
  259. function guid() {
  260. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(e) {
  261. var t = 16 * Math.random() | 0;
  262. return ("x" == e ? t : 3 & t | 8).toString(16)
  263. })
  264. }