Web Interface For Lite Argus Control modules
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // UnrealWebRTCPlayer - Copyright 2017-2019 Unreal Streaming Technologies
  2. // Version 1.5 - May 10 2019. Added support for overlayed Play button.
  3. function UnrealWebRTCPlayer(videoID, alias, sid, ipAddress, port, useSecureWebsocket, useSingleWebRTCPort, WebRTCProtocol) {
  4. "use strict";
  5. var pc = null;
  6. var ws = null;
  7. var state = -1;
  8. var audioCodec = "";
  9. var videoCodec = "";
  10. var latestStopTime = 0;
  11. var connOK = false;
  12. var remoteVideo = document.getElementById(videoID);
  13. var showControls = remoteVideo.controls;
  14. this.Play = function Play() {
  15. var nowTime = new Date().getTime();
  16. if ((state == -1) && (nowTime - latestStopTime > 1000)) {
  17. remoteVideo.srcObject = null;
  18. state = 0;
  19. connOK = false;
  20. ToggleOverlay(false);
  21. remoteVideo.setAttribute('style', 'background: black url(loader.gif) center no-repeat;');
  22. showControls = remoteVideo.controls;
  23. remoteVideo.controls = false;
  24. DoSignaling();
  25. }
  26. if (nowTime - latestStopTime <= 1000)
  27. remoteVideo.pause();
  28. }
  29. this.Stop = function Stop() {
  30. showControls = remoteVideo.controls;
  31. Terminate();
  32. }
  33. remoteVideo.onplay = this.Play;
  34. remoteVideo.onpause = this.Stop;
  35. function Terminate() {
  36. latestStopTime = new Date().getTime();
  37. state = -1;
  38. ToggleOverlay(true);
  39. remoteVideo.setAttribute('style', 'background-color:black');
  40. if (showControls)
  41. remoteVideo.controls = true;
  42. if (pc != null) {
  43. pc.onconnectionstatechange = null;
  44. pc.close();
  45. pc = null;
  46. }
  47. if (ws != null) {
  48. ws.onerror = null;
  49. ws.close();
  50. ws = null;
  51. }
  52. remoteVideo.pause();
  53. }
  54. function onCreateSessionDescriptionError(error) {
  55. Terminate();
  56. alert("Failed to create session description: " + error.toString());
  57. }
  58. function onCreateOfferSuccess(desc) {
  59. var audioRate = 8000;
  60. if (audioCodec === "opus")
  61. audioRate = 48000;
  62. if (audioCodec != "")
  63. desc.sdp = setCodec(desc.sdp, "audio", audioCodec, audioRate);
  64. if (videoCodec != "")
  65. desc.sdp = setCodec(desc.sdp, "video", videoCodec, 90000);
  66. //Fix for some browsers and/or adapter incorrect behavior
  67. desc.sdp = desc.sdp.replace("a=sendrecv", "a=recvonly");
  68. desc.sdp = desc.sdp.replace("a=sendrecv", "a=recvonly");
  69. //Signal the SDP to the server
  70. var msgString = JSON.stringify(desc);
  71. ws.send(msgString);
  72. pc.setLocalDescription(desc);
  73. }
  74. function onIceCandidate(event) {
  75. //Do nothing! We only need one endpoint from server; browser is going to connect to it
  76. }
  77. function gotRemoteStream(e) {
  78. remoteVideo.srcObject = e.streams[0];
  79. if (videoCodec == "")
  80. remoteVideo.setAttribute('style', 'background-color:black');
  81. if (showControls)
  82. remoteVideo.controls = true;
  83. }
  84. function onConnStateChange(event) {
  85. if (pc.connectionState === "failed") {
  86. Terminate();
  87. alert("Connection failed; playback stopped");
  88. }
  89. }
  90. function DoSignaling() {
  91. var centralWebRTCPort = useSingleWebRTCPort ? "singleport/" : "randomport/";
  92. var URL = useSecureWebsocket ? "wss://" : "ws://";
  93. URL += ipAddress + ":" + port + "/webrtc_playnow/" + centralWebRTCPort + WebRTCProtocol + "/" + alias;
  94. if (sid != "")
  95. URL += "/sid:" + sid;
  96. try {
  97. ws = new WebSocket(URL);
  98. }
  99. catch (error) {
  100. Terminate();
  101. alert("Error creating websocket: " + error);
  102. }
  103. ws.onmessage = function(evt) {
  104. var response = evt.data;
  105. var strArr = response.split("|-|-|");
  106. connOK = true;
  107. if (state == 0) {
  108. state = 1;
  109. if (strArr.length == 1) {
  110. Terminate();
  111. alert(response);
  112. }
  113. else {
  114. var servers = null;
  115. var offerOptions = null;
  116. videoCodec = strArr[0];
  117. audioCodec = strArr[1];
  118. if ((videoCodec != "") && (audioCodec != ""))
  119. offerOptions = { offerToReceiveAudio: 1, offerToReceiveVideo: 1 };
  120. else if (videoCodec != "")
  121. offerOptions = { offerToReceiveAudio: 0, offerToReceiveVideo: 1 };
  122. else if (audioCodec != "")
  123. offerOptions = { offerToReceiveAudio: 1, offerToReceiveVideo: 0 };
  124. pc = new RTCPeerConnection(servers);
  125. pc.onicecandidate = onIceCandidate;
  126. pc.onconnectionstatechange = onConnStateChange;
  127. pc.ontrack = gotRemoteStream;
  128. pc.createOffer(offerOptions).then(
  129. onCreateOfferSuccess,
  130. onCreateSessionDescriptionError
  131. );
  132. }
  133. }
  134. else {
  135. if (strArr.length == 1) {
  136. Terminate();
  137. alert(response);
  138. }
  139. else {
  140. var serverSDP = JSON.parse(strArr[0]);
  141. var serverEndpoint = JSON.parse(strArr[1]);
  142. serverEndpoint.candidate = EnsureValidCandidate(serverEndpoint.candidate);
  143. pc.setRemoteDescription(new RTCSessionDescription(serverSDP));
  144. var candidate = new RTCIceCandidate({ sdpMLineIndex: serverEndpoint.sdpMLineIndex, candidate: serverEndpoint.candidate });
  145. pc.addIceCandidate(candidate);
  146. ws.close();
  147. ws = null;
  148. }
  149. }
  150. }
  151. ws.onerror = function(evt) {
  152. if (!connOK) {
  153. Terminate();
  154. alert("Error connecting to Unreal Media Server");
  155. }
  156. }
  157. }
  158. function EnsureValidCandidate(candidate) {
  159. if ((candidate.search(ipAddress) !== -1) || !useSingleWebRTCPort || (ipAddress == "127.0.0.1") || !ValidateIPaddress(ipAddress)) {
  160. return candidate;
  161. }
  162. //In case the server is behind the NAT router, replace private IP with public IP in the candidate
  163. var candLines = candidate.split(" ");
  164. var ipIndex = 4;
  165. for (var i = 0; i < candLines.length; i++) {
  166. if (candLines[i] === "typ") {
  167. ipIndex = i - 2;
  168. break;
  169. }
  170. }
  171. candLines[ipIndex] = ipAddress;
  172. candidate = candLines.join(" ");
  173. return candidate;
  174. }
  175. function ValidateIPaddress(ipaddr) {
  176. if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddr)) {
  177. return true;
  178. }
  179. return false;
  180. }
  181. function setCodec(sdp, type, codec, clockRate) {
  182. var sdpLines = sdp.split("\r\n");
  183. for (var i = 0; i < sdpLines.length; i++) {
  184. if (sdpLines[i].search("m=" + type) !== -1) {
  185. var mLineIndex = i;
  186. break;
  187. }
  188. }
  189. if (mLineIndex === null) return sdp;
  190. var codecPayload = null;
  191. var re = new RegExp(":(\\d+) " + codec + "\/" + clockRate);
  192. for (var i = mLineIndex; i < sdpLines.length; i++) {
  193. if (sdpLines[i].search(codec + "/" + clockRate) !== -1) {
  194. codecPayload = extractPayloadType(sdpLines[i], re);
  195. if (codecPayload && (EnsureSupportedProfile(codec, sdpLines, mLineIndex, codecPayload))) {
  196. sdpLines[mLineIndex] = setDefaultCodec(sdpLines[mLineIndex], codecPayload);
  197. break;
  198. }
  199. }
  200. }
  201. if (codecPayload === null) return sdp;
  202. var rtmpmap = "a=rtpmap:";
  203. var rtcp = "a=rtcp-fb:";
  204. var fmptp = "a=fmtp:";
  205. var rtmpmapThis = "a=rtpmap:" + codecPayload;
  206. var rtcpThis = "a=rtcp-fb:" + codecPayload;
  207. var fmptpThis = "a=fmtp:" + codecPayload;
  208. var bAddAll = false;
  209. var resSDPLines = new Array();
  210. for (var i = 0; i < sdpLines.length; i++) {
  211. if (i <= mLineIndex) {
  212. resSDPLines.push(sdpLines[i]);
  213. }
  214. else {
  215. if (sdpLines[i].search("m=") === 0)
  216. bAddAll = true;
  217. var bNotToAdd = ((sdpLines[i].search(rtmpmap) === 0) && (sdpLines[i].search(rtmpmapThis) !== 0)) || ((sdpLines[i].search(rtcp) === 0) && (sdpLines[i].search(rtcpThis) !== 0)) || ((sdpLines[i].search(fmptp) === 0) && (sdpLines[i].search(fmptpThis) !== 0));
  218. if (bAddAll || !bNotToAdd)
  219. resSDPLines.push(sdpLines[i]);
  220. }
  221. }
  222. sdp = resSDPLines.join("\r\n");
  223. return sdp;
  224. };
  225. function extractPayloadType(sdpLine, pattern) {
  226. var result = sdpLine.match(pattern);
  227. return (result && result.length == 2) ? result[1] : null;
  228. };
  229. function EnsureSupportedProfile(codec, sdpLines, mLineIndex, codecPayload) {
  230. if (codec != "H264")
  231. return true;
  232. //Server can send any profile/level H264, but SDP has to specify supported one
  233. for (var i = mLineIndex; i < sdpLines.length; i++) {
  234. if ((sdpLines[i].search("a=fmtp:" + codecPayload) === 0) && (sdpLines[i].search("profile-level-id=42") !== -1))
  235. return true;
  236. }
  237. return false;
  238. };
  239. function setDefaultCodec(mLine, payload) {
  240. var elements = mLine.split(" ");
  241. var newLine = new Array();
  242. var index = 0;
  243. for (var i = 0; i < elements.length; i++) {
  244. if (index === 3) {
  245. newLine[index++] = payload;
  246. break;
  247. }
  248. if (elements[i] !== payload) newLine[index++] = elements[i];
  249. }
  250. return newLine.join(" ");
  251. };
  252. function ToggleOverlay(show) {
  253. var Overlay = document.getElementById(videoID + "_Overlay");
  254. if (Overlay != null) {
  255. if(show)
  256. Overlay.style.display = "inline";
  257. else
  258. Overlay.style.display = "none";
  259. }
  260. }
  261. };