mirror of
https://github.com/pion/webrtc.git
synced 2025-10-05 23:26:58 +08:00

jsfiddle provides a way to create snippets from Github via a URL. This way we can still provide easy demos, but get all the nice things from having them in Git Closes #32
32 lines
873 B
JavaScript
32 lines
873 B
JavaScript
/* eslint-env browser */
|
|
|
|
let pc = new RTCPeerConnection()
|
|
var log = msg => {
|
|
document.getElementById('logs').innerHTML += msg + '<br>'
|
|
}
|
|
|
|
navigator.mediaDevices.getUserMedia({video: true, audio: true})
|
|
.then(stream => pc.addStream(document.getElementById('video1').srcObject = stream))
|
|
.catch(log)
|
|
|
|
pc.oniceconnectionstatechange = e => log(pc.iceConnectionState)
|
|
|
|
pc.onnegotiationneeded = e =>
|
|
pc.createOffer().then(d => {
|
|
document.getElementById('localSessionDescription').value = btoa(d.sdp)
|
|
return pc.setLocalDescription(d)
|
|
}).catch(log)
|
|
|
|
window.startSession = () => {
|
|
let sd = document.getElementById('remoteSessionDescription').value
|
|
if (sd === '') {
|
|
return alert('Session Description must not be empty')
|
|
}
|
|
|
|
try {
|
|
pc.setRemoteDescription(new RTCSessionDescription({type: 'answer', sdp: atob(sd)}))
|
|
} catch (e) {
|
|
alert(e)
|
|
}
|
|
}
|