Files
go2rtc/www/webrtc-sync.html
T
2023-03-19 17:17:05 +03:00

67 lines
1.9 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>go2rtc - WebRTC</title>
<style>
body {
background-color: black;
margin: 0;
padding: 0;
}
html, body, video {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<video id="video" autoplay controls playsinline muted></video>
<script>
function PeerConnection(userMedia) {
return new Promise((resolve, reject) => {
const pc = new RTCPeerConnection({
iceServers: [{urls: 'stun:stun.l.google.com:19302'}]
})
pc.addEventListener('icegatheringstatechange', () => {
if (pc.iceGatheringState === 'complete') resolve(pc)
})
document.getElementById('video').srcObject = new MediaStream([
pc.addTransceiver('video', {direction: 'recvonly'}).receiver.track,
pc.addTransceiver('audio', {direction: 'recvonly'}).receiver.track
])
if (userMedia) {
userMedia.getTracks().forEach(track => {
pc.addTransceiver(track, {direction: 'sendonly'})
})
}
pc.createOffer().then(offer => pc.setLocalDescription(offer))
setTimeout(() => resolve(pc), 3000)
})
}
async function userMedia() {
try {
return await navigator.mediaDevices.getUserMedia({audio: true})
} catch (e) {
return null
}
}
async function connect() {
const pc = await PeerConnection(await userMedia())
const url = new URL('api/webrtc' + location.search, location.href)
const r = await fetch(url, {method: 'POST', body: pc.localDescription.sdp})
await pc.setRemoteDescription({type: 'answer', sdp: await r.text()})
}
connect()
</script>
</body>
</html>