Created
September 25, 2020 02:01
-
-
Save murillo128/abc662723b6998b761698aeed8a24a45 to your computer and use it in GitHub Desktop.
WHIP browser client example
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
//Get mic+cam | |
const stream = await navigator.mediaDevices.getUserMedia({audio:true, video:true}); | |
//Create peerconnection | |
const pc = new RTCPeerConnection(); | |
//Listen for state change events | |
pc.onconnectionstatechange = (event) =>{ | |
switch(pc.connectionState) { | |
case "connected": | |
// The connection has become fully connected | |
break; | |
case "disconnected": | |
case "failed": | |
// One or more transports has terminated unexpectedly or in an error | |
break; | |
case "closed": | |
// The connection has been closed | |
break; | |
} | |
} | |
//Send all tracks | |
for (const track of stream.getTracks()) | |
//You could add simulcast too here | |
pc.addTrack(track); | |
//Create SDP offer | |
const offer = await pc.createOffer(); | |
//Set it | |
await pc.setLocalDescription(offer) | |
//Do the post request to the WHIP endpoint with the SDP offer | |
const fetched = await fetch(url, { | |
method: "POST", | |
body: offer.sdp, | |
headers:{ | |
"Content-Type": "application/sdp" | |
} | |
}); | |
//Get the SDP answer | |
const answer = await fetched.text(); | |
//And set it | |
await pc.setRemoteDescription({type:"answer",sdp: answer}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have created two PRs with minor changes.
medooze/whip-whep-js#28
medooze/whip-whep-js#27