summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webrtc-encoded-transform/RTCEncodedAudioFrame-clone.https.html
blob: ec99338772029bf1e47de724edf85b96a3d0bc75 (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
<!DOCTYPE html>
<meta charset="utf-8">
<title>RTCEncodedAudioFrame can be cloned and distributed</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src=/resources/testdriver.js></script>
<script src=/resources/testdriver-vendor.js></script>
<script src='../mediacapture-streams/permission-helper.js'></script>
<script src="../webrtc/RTCPeerConnection-helper.js"></script>
<script src="../service-workers/service-worker/resources/test-helpers.sub.js"></script>

<script>
"use strict";
promise_test(async t => {
  const caller1 = new RTCPeerConnection();
  t.add_cleanup(() => caller1.close());
  const callee1 = new RTCPeerConnection({encodedInsertableStreams:true});
  t.add_cleanup(() => callee1.close());
  await setMediaPermission("granted", ["microphone"]);
  const inputStream = await navigator.mediaDevices.getUserMedia({audio:true});
  const inputTrack = inputStream.getAudioTracks()[0];
  t.add_cleanup(() => inputTrack.stop());
  caller1.addTrack(inputTrack)
  exchangeIceCandidates(caller1, callee1);

  const caller2 = new RTCPeerConnection({encodedInsertableStreams:true});
  t.add_cleanup(() => caller2.close());
  const sender2 = caller2.addTransceiver("audio").sender;
  const writer2 = sender2.createEncodedStreams().writable.getWriter();
  sender2.replaceTrack(new MediaStreamTrackGenerator({ kind: 'audio' }));

  const framesReceivedCorrectly = new Promise((resolve, reject) => {
    callee1.ontrack = async e => {
      const receiverStreams = e.receiver.createEncodedStreams();
      const receiverReader = receiverStreams.readable.getReader();
      const result = await receiverReader.read();
      const original = result.value;
      let clone = original.clone();
      assert_equals(original.timestamp, clone.timestamp);
      assert_array_equals(Array.from(original.data), Array.from(clone.data));
      await writer2.write(clone);
      resolve();
    }
  });

  await exchangeOfferAnswer(caller1, callee1);

  return framesReceivedCorrectly;
}, "Cloning before sending works");
</script>