{"version":3,"sources":["videoplayer-webrtc.js"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"videoplayer.js","sourcesContent":["'use strict';\r\n/*\r\n Get these from WowzaStreamingEngine WebRTC Application\r\n */\r\nconst wsConnect = () => {\r\n let _this = this;\r\n let streamInfo = { applicationName, streamName };\r\n let userData = { 'iceServers': [] };\r\n let peerConnection = new RTCPeerConnection(userData);\r\n let repeaterRetryCount;\r\n\r\n try {\r\n wsConnection = new WebSocket(wssUrl);\r\n }\r\n catch (e) {\r\n console.error(e);\r\n }\r\n wsConnection.binaryType = 'arraybuffer';\r\n\r\n wsConnection.onopen = () => {\r\n console.log(\"onopen\");\r\n\r\n peerConnection.onicecandidate = _this.gotIceCandidate;\r\n\r\n peerConnection.ontrack = (event) => {\r\n console.log('gotRemoteTrack: kind:' + event.track.kind + ' stream:' + event.streams[0]);\r\n try {\r\n videoElement.srcObject = event.streams[0];\r\n } catch (error) {\r\n videoElement.src = window.URL.createObjectURL(event.streams[0]);\r\n }\r\n }\r\n sendPlayGetOffer();\r\n }\r\n\r\n const sendPlayGetOffer = () => {\r\n console.log(\"sendPlayGetOffer: \" + JSON.stringify(streamInfo));\r\n wsConnection.send('{\"direction\":\"play\", \"command\":\"getOffer\", \"streamInfo\":' + JSON.stringify(streamInfo) + ', \"userData\":' + JSON.stringify(userData) + '}');\r\n }\r\n\r\n const stop = () => {\r\n if (peerConnection != null) { peerConnection.close(); }\r\n if (wsConnection != null) { wsConnection.close(); }\r\n peerConnection = null;\r\n wsConnection = null;\r\n videoElement.src = \"\";\r\n }\r\n\r\n wsConnection.onmessage = function (evt) {\r\n console.log(\"wsConnection.onmessage: \" + evt.data);\r\n\r\n let msgJSON = JSON.parse(evt.data);\r\n let msgStatus = Number(msgJSON['status']);\r\n let msgCommand = msgJSON['command'];\r\n\r\n if (msgStatus == 514) // repeater stream not ready\r\n {\r\n repeaterRetryCount++;\r\n if (repeaterRetryCount < 10) {\r\n setTimeout(sendPlayGetOffer, 500);\r\n }\r\n else {\r\n console.log('Live stream repeater timeout: ' + streamName);\r\n stop();\r\n }\r\n }\r\n else if (msgStatus != 200) {\r\n console.log(msgJSON['statusDescription']);\r\n stop();\r\n }\r\n else {\r\n\r\n let streamInfoResponse = msgJSON['streamInfo'];\r\n if (streamInfoResponse !== undefined) {\r\n streamInfo.sessionId = streamInfoResponse.sessionId;\r\n }\r\n\r\n let sdpData = msgJSON['sdp'];\r\n if (sdpData != null) {\r\n console.log('sdp: ' + JSON.stringify(msgJSON['sdp']));\r\n\r\n if (mungeSDP != null) {\r\n msgJSON.sdp.sdp = mungeSDP(msgJSON.sdp.sdp);\r\n }\r\n\r\n // Enhance here if Safari is a published stream.\r\n console.log(\"SDP Data: \" + msgJSON.sdp.sdp);\r\n\r\n peerConnection\r\n .setRemoteDescription(new RTCSessionDescription(msgJSON.sdp))\r\n .then(() => peerConnection\r\n .createAnswer()\r\n .then((description) => {\r\n peerConnection.setLocalDescription(description)\r\n .then(() => {\r\n console.log('sendAnswer');\r\n wsConnection.send('{\"direction\":\"play\", \"command\":\"sendResponse\", \"streamInfo\":' + JSON.stringify(streamInfo) + ', \"sdp\":' + JSON.stringify(description) + ', \"userData\":' + JSON.stringify(userData) + '}');\r\n })\r\n .catch(err => console.log('set local description error', err));\r\n })\r\n ).catch(err => console.log('set remote description error', err));\r\n }\r\n\r\n let iceCandidates = msgJSON['iceCandidates'];\r\n if (iceCandidates != null) {\r\n for (let index in iceCandidates) {\r\n console.log('iceCandidates: ' + JSON.stringify(iceCandidates[index]));\r\n peerConnection.addIceCandidate(new RTCIceCandidate(iceCandidates[index]));\r\n }\r\n }\r\n }\r\n\r\n if ('sendResponse'.localeCompare(msgCommand) == 0) {\r\n if (wsConnection != null) {\r\n wsConnection.close();\r\n }\r\n\r\n wsConnection = null;\r\n }\r\n }\r\n\r\n wsConnection.onclose = function () {\r\n console.log(\"wsConnection.onclose\");\r\n }\r\n\r\n wsConnection.onerror = function (evt) {\r\n console.log(evt);\r\n }\r\n}\r\n\r\nconst mungeSDP = (sdpStr) => {\r\n // For greatest playback compatibility,\r\n // force H.264 playback to baseline (42e01f).\r\n let sdpLines = sdpStr.split(/\\r\\n/);\r\n let sdpStrRet = '';\r\n\r\n for (var sdpIndex in sdpLines) {\r\n var sdpLine = sdpLines[sdpIndex];\r\n\r\n if (sdpLine.length == 0)\r\n continue;\r\n\r\n if (sdpLine.includes(\"profile-level-id\")) {\r\n // The profile-level-id string has three parts: XXYYZZ, where\r\n // XX: 42 baseline, 4D main, 64 high\r\n // YY: constraint\r\n // ZZ: level ID\r\n // Look for codecs higher than baseline and force downward.\r\n let profileLevelId = sdpLine.substr(sdpLine.indexOf(\"profile-level-id\") + 17, 6);\r\n let profile = Number('0x' + profileLevelId.substr(0, 2));\r\n let constraint = Number('0x' + profileLevelId.substr(2, 2));\r\n let level = Number('0x' + profileLevelId.substr(4, 2));\r\n if (profile > 0x42) {\r\n profile = 0x42;\r\n constraint = 0xE0;\r\n level = 0x1F;\r\n }\r\n let newProfileLevelId = (\"00\" + profile.toString(16)).slice(-2).toLowerCase() +\r\n (\"00\" + constraint.toString(16)).slice(-2).toLowerCase() +\r\n (\"00\" + level.toString(16)).slice(-2).toLowerCase();\r\n\r\n sdpLine = sdpLine.replace(profileLevelId, newProfileLevelId);\r\n }\r\n\r\n sdpStrRet += sdpLine;\r\n sdpStrRet += '\\r\\n';\r\n }\r\n\r\n return sdpStrRet;\r\n}\r\n\r\n/*\r\n initialize and play, wire in play button here\r\n*/\r\nif ((applicationName == \"\") || (streamName == \"\" || wssUrl == \"\")) {\r\n console.error(\"Please fill out the connection details\")\r\n} else {\r\n videoElement = document.getElementById(\"stream-player\");\r\n wsConnect()\r\n}"]}