+ /* Start data channel, triggers on negotiation needed */
+ dataChannel = cpc.createDataChannel("sendChannel");
+
+ /* Triggered when Host adds track to peer connection */
+ cpc.ontrack = (event) => {
+ let remoteRTPReceivers = cpc.getReceivers()
+ let hostScreen
+ let video = document.querySelector('video')
+ /* TODO: Audio, video, or other track? */
+ console.log(remoteRTPReceivers)
+ console.log(video)
+ hostScreen = new MediaStream([remoteRTPReceivers[0].track])
+ if(!video.srcObject) {
+ video.srcObject = hostScreen
+ }
+ console.log(hostScreen.getVideoTracks())
+ console.log(video.srcObject)
+ video.onloadedmetadata = () => {
+ video.play()
+ }
+ }
+
+ dataChannel.onmessage = (msg) => {
+ /* Get mediaStream from host and add it to the video */
+ let hostMessage = JSON.parse(msg.data)
+ console.log('Client: Renego')
+ cpc.setRemoteDescription(hostMessage.sdp).then(() => {
+ cpc.createAnswer().then((answer) => {
+ return cpc.setLocalDescription(answer)
+ }).then(() => {
+ dataChannel.send(JSON.stringify({
+ "cmd": "> screen dataChannel",
+ "sdp": cpc.localDescription
+ }))
+ })
+ })
+
+
+ }
+ dataChannel.onopen = () => {
+ document.body.innerHTML = (`<div><button> Connection with host established! </button></div> <video controls></video>`)
+ }
+