X-Git-Url: https://git.kengrimes.com/?p=henge%2Fkiak.git;a=blobdiff_plain;f=client.js;fp=client.js;h=4d1bf9ae012d28acecc89451c2852923e9e80cbc;hp=160d5ddb4be53af90713898460f05e54f51297b8;hb=5ae8bb9adc37111641414da912600ea97d834874;hpb=6e147610b0a69225f15e09dcbdac29adb4ed66fc diff --git a/client.js b/client.js index 160d5dd..4d1bf9a 100644 --- a/client.js +++ b/client.js @@ -2,10 +2,8 @@ const body = document.createElement('body') const root = document.createElement('div') document.title = "Strapp.io Client" const conf = {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] } -let dataChannel -let hostScreen -/* TODO: duplicate in both client.js and host.js */ +/* TODO: This is duplicated in both client.js and host.js */ function getPublicKey() { return new Promise( (resolve, reject) => { /* Check local storage for public key */ @@ -55,7 +53,7 @@ function requestHostAnswer(url, data) { /* But there is no JSON? */ request.setRequestHeader('Content-Type', 'application/json' ) request.setRequestHeader('X-Strapp-Type', 'client-sdp-offer') - request.setRequestHeader('X-Client-Offer', JSON.stringify(data)) + request.setRequestHeader('X-Strapp-Pubkey', data.pubKey) request.onreadystatechange = () => { if (request.status === 200) { if(request.readyState === 4) { @@ -72,7 +70,7 @@ function requestHostAnswer(url, data) { reject('server unhandled response of status ' + request.status) } } - request.send() + request.send(data) }) } @@ -125,6 +123,7 @@ function requestHostICE(cpc, url, pubKey) { /* Create and send offer -> Send ICE Candidates -> Poll for ICE Candidates */ getPublicKey().then((cpk) => { + let dataChannel console.log('Client: Create and send offer') const cpc = new RTCPeerConnection(conf) @@ -170,16 +169,16 @@ getPublicKey().then((cpk) => { console.log('error in sdp handshake: ' + err) }) } - /* Start data channel */ + /* Start data channel, triggers on negotiation needed */ dataChannel = cpc.createDataChannel("sendChannel"); + + /* Triggered when Host adds track to peer connection */ cpc.ontrack = (event) => { - console.log(`track event is ${event}`) - let remoteRTPSenders = cpc.getSenders() let remoteRTPReceivers = cpc.getReceivers() - console.log(remoteRTPReceivers) - /* Add each remoteRTPSenders.track to own stream */ + let hostScreen let video = document.querySelector('video') - video.autoplay = true + /* TODO: Audio, video, or other track? */ + console.log(remoteRTPReceivers) console.log(video) hostScreen = new MediaStream([remoteRTPReceivers[0].track]) if(!video.srcObject) { @@ -187,14 +186,15 @@ getPublicKey().then((cpk) => { } console.log(hostScreen.getVideoTracks()) console.log(video.srcObject) - video.play() 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)