File size: 1,615 Bytes
25ed14d
c16538d
25ed14d
c16538d
25ed14d
3e02329
25ed14d
 
ed7b516
25ed14d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c16538d
 
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
<!-- producer.html -->
<!DOCTYPE html>
<html>
<body>
  <h3>Broadcaster</h3>
  <p>Hello </p>
  <video id="localVideo" autoplay muted></video>
  <script>
  const ws = new WebSocket('ws://pepguy-swarming-1.hf.space');
  let transport, producer;

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

    if (action === 'producerTransportCreated') {
      transport = device.createSendTransport(d);
      transport.on('connect', ({ dtlsParameters }, cb) =>
        ws.send(JSON.stringify({ action: 'connectProducerTransport', data: { dtlsParameters } })), cb()
      );
      transport.on('produce', ({ kind, rtpParameters }, cb) =>
        ws.send(JSON.stringify({ action: 'produce', data: { kind, rtpParameters } })), cb({ id: '' })
      );
    }

    if (action === 'produced') {
      console.log('Producer ready:', d.producerId);
    }
  };

  async function start() {
    // 1) getUserMedia
    const stream = await navigator.mediaDevices.getUserMedia({ audio: true, video: true });
    document.getElementById('localVideo').srcObject = stream;

    // 2) load device RTP capabilities
    ws.send(JSON.stringify({ action: 'createProducerTransport' }));
    const { rtpCapabilities } = await (await fetch('/rtpCapabilities')).json();
    device = new mediasoupClient.Device();
    await device.load({ routerRtpCapabilities: rtpCapabilities });

    // 3) wait for transport then produce
    stream.getTracks().forEach(track => transport.produce({ track }));
  }

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