Skip to content

Instantly share code, notes, and snippets.

@murillo128
Created September 25, 2020 02:01
Show Gist options
  • Save murillo128/abc662723b6998b761698aeed8a24a45 to your computer and use it in GitHub Desktop.
Save murillo128/abc662723b6998b761698aeed8a24a45 to your computer and use it in GitHub Desktop.
WHIP browser client example
//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});
@neilyoung
Copy link

I have created two PRs with minor changes.

medooze/whip-whep-js#28
medooze/whip-whep-js#27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment