File size: 1,336 Bytes
289918b
64ac97a
289918b
64ac97a
289918b
 
 
c87c807
289918b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64ac97a
 
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
<!-- consumer.html -->
<!DOCTYPE html>
<html>
<body>
  <h3>Viewer</h3>
  <video id="remoteVideo" autoplay></video>
  <script>
  const ws = new WebSocket('ws://pepguy-swarming-1.hf.space');
  let transport, consumer;

  ws.onmessage = async ({ data }) => {
    const { action, data: d } = JSON.parse(data);

    if (action === 'consumerTransportCreated') {
      transport = device.createRecvTransport(d);
      transport.on('connect', ({ dtlsParameters }, cb) =>
        ws.send(JSON.stringify({ action: 'connectConsumerTransport', data: { dtlsParameters } })), cb()
      );
      // once connected, ask to consume
      ws.send(JSON.stringify({ action: 'consume' }));
    }

    if (action === 'consumed') {
      consumer = await transport.consume(d);
      const stream = new MediaStream([consumer.track]);
      document.getElementById('remoteVideo').srcObject = stream;
    }
  };

  async function start() {
    // load device
    const { rtpCapabilities } = await (await fetch('/rtpCapabilities')).json();
    device = new mediasoupClient.Device();
    await device.load({ routerRtpCapabilities: rtpCapabilities });

    // create transport
    ws.send(JSON.stringify({ action: 'createConsumerTransport' }));
  }

  start();
  </script>
  <script src="https://unpkg.com/mediasoup-client@3/lib/index.js"></script>
</body>
</html>