<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>synced-playback</title>
</head>
<body id="body">
<video id="video1" autoplay playsinline></video>
<div>
<input type="number" id="seekTime" value="30">
<button type="button" onClick="seekClick()">Seek</button>
<button type="button" onClick="playClick()">Play</button>
<button type="button" onClick="pauseClick()">Pause</button>
</div>
<script>
let pc = new RTCPeerConnection()
pc.addTransceiver('video', {direction: 'recvonly'});
pc.addTransceiver('audio', {direction: 'recvonly'});
window.seekClick = () => {
conn.send(JSON.stringify({event: 'seek', data: document.getElementById('seekTime').value}))
}
window.playClick = () => {
conn.send(JSON.stringify({event: 'play', data: ''}))
}
window.pauseClick = () => {
conn.send(JSON.stringify({event: 'pause', data: ''}))
}
pc.ontrack = function (event) {
if (event.track.kind === 'audio') {
return
}
var el = document.getElementById('video1')
el.srcObject = event.streams[0];
el.autoplay = true;
el.controls = true;
}
let conn = new WebSocket('wss://' + window.location.host + window.location.pathname + '/ws/')
window.conn = conn
conn.onopen = () => {
console.log('Connection opened')
}
conn.onclose = evt => {
console.log('Connection closed')
}
conn.onmessage = evt => {
let msg = JSON.parse(evt.data)
if (!msg) {
return console.log('failed to parse msg')
}
switch (msg.event) {
case 'offer':
offer = JSON.parse(msg.data)
if (!offer) {
return console.log('failed to parse offer')
}
console.log('Received offer', offer);
(async () => {
pc.setRemoteDescription(offer);
const answer = await pc.createAnswer();
await pc.setLocalDescription(answer);
console.log('Sending answer', answer);
conn.send(JSON.stringify({event: 'answer', data: JSON.stringify(answer)}));
})();
}
}
</script>
</body>
</html>