aboutsummaryrefslogtreecommitdiff
path: root/index.html
blob: fc641bfef93ba03098e42c6e8e8290d38c7c8e10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!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 conn = new WebSocket('wss://' + window.location.host + window.location.pathname + '/ws/')
     let pc = new RTCPeerConnection()
 
     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
     }
 
     conn.onopen = () => {
       navigator.mediaDevices.getUserMedia({video: true, audio: true}).then(g => {
         for (const track of g.getTracks()) {
           pc.addTrack(track,g);
         }
 
         pc.createOffer({offerToReceiveVideo: true, offerToReceiveAudio: true}).then(offer => {
           return pc.setLocalDescription(offer);
   }).then(() => {
           conn.send(JSON.stringify({event: 'offer', data: JSON.stringify(pc.localDescription)}))
         })
       }).catch(e => { console.log(`failed getUserMedia: ${e}`) });
     }
 
     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 'answer':
           answer = JSON.parse(msg.data)
           if (!answer) {
             return console.log('failed to parse answer')
           }
           pc.setRemoteDescription(answer)
       }
     }
     window.conn = conn
    </script> 
  </body>
</html>