Created
October 25, 2019 17:51
-
-
Save mekya/aa93e5611445f7ca229fb0ce8e0b6902 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<h1>local</h1> | |
<video id="local_video" autoplay></video> | |
<br /> | |
<h1>remote</h1> | |
<video id="remote_video" autoplay></video> | |
</body> | |
<script> | |
const signalingChannel = new WebSocket( | |
"ws://localhost:5080/WebRTCAppEE/websocket" | |
); | |
const peerConn = new RTCPeerConnection(null); | |
navigator.getUserMedia( | |
{ | |
video: true, | |
audio: false | |
}, | |
function(stream) { | |
document.getElementById("local_video").srcObject = stream; | |
}, | |
function(error) { | |
console.error(error); | |
} | |
); | |
// peerConn.ontrack = function(evt) { | |
// document.getElementById("remote_video").srcObject = event.streams[0]; | |
// }; | |
peerConn.onaddstream = function({ stream }) { | |
document.getElementById("remote_video").srcObject = stream; | |
}; | |
signalingChannel.onopen = function(evt) { | |
signalingChannel.send( | |
JSON.stringify({ | |
command: "play", | |
streamId: "stream1" | |
}) | |
); | |
}; | |
signalingChannel.onmessage = async function(evt) { | |
var signal = JSON.parse(evt.data); | |
console.log({ signal }); | |
if (signal.command === "takeConfiguration") { | |
peerConn.setRemoteDescription( | |
new RTCSessionDescription({ type: signal.type, sdp: signal.sdp }) | |
); | |
if (signal.type === "offer") { | |
peerConn.createAnswer().then(function(configuration) { | |
peerConn.setLocalDescription(configuration).then(() => { | |
var jsCmd = { | |
command : "takeConfiguration", | |
streamId : "stream1", | |
type : configuration.type, | |
sdp : configuration.sdp | |
}; | |
signalingChannel.send(JSON.stringify(jsCmd)); | |
} | |
); | |
}); | |
} | |
} | |
if (signal.command === "takeCandidate") { | |
peerConn.addIceCandidate( | |
new RTCIceCandidate({ | |
candidate: signal.candidate, | |
sdpMid: signal.id, | |
sdpMLineIndex: signal.label | |
}) | |
); | |
} | |
}; | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment