summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webrtc-extensions/RTCRtpReceiver-jitterBufferTarget-stats.html
blob: 33f71800bd8ed53eb7b0446e6535997bccb7b01a (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="timeout" content="long">
<title>Tests RTCRtpReceiver-jitterBufferTarget verified with stats</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/webrtc/RTCPeerConnection-helper.js"></script>
<body>
<script>
'use strict'

function async_promise_test(func, name, properties) {
  async_test(t => {
    Promise.resolve(func(t))
      .catch(t.step_func(e => { throw e; }))
      .then(() => t.done());
  }, name, properties);
}

async_promise_test(t => applyJitterBufferTarget(t, "video", 4000),
                    "measure raising and lowering video jitterBufferTarget");
async_promise_test(t => applyJitterBufferTarget(t, "audio", 4000),
                    "measure raising and lowering audio jitterBufferTarget");

async function applyJitterBufferTarget(t, kind, target) {
  const caller = new RTCPeerConnection();
  t.add_cleanup(() => caller.close());
  const callee = new RTCPeerConnection();
  t.add_cleanup(() => callee.close());

  const stream = await getNoiseStream({[kind]:true});
  t.add_cleanup(() => stream.getTracks().forEach(track => track.stop()));
  caller.addTransceiver(stream.getTracks()[0], {streams: [stream]});
  caller.addTransceiver(stream.getTracks()[0], {streams: [stream]});

  exchangeIceCandidates(caller, callee);
  await exchangeOffer(caller, callee);
  const [unconstrainedReceiver, constrainedReceiver] = callee.getReceivers();
  const haveRtp = Promise.all([
    new Promise(r => constrainedReceiver.track.onunmute = r),
    new Promise(r => unconstrainedReceiver.track.onunmute = r)
  ]);
  await exchangeAnswer(caller, callee);
  const chromeTimeout = new Promise(r => t.step_timeout(r, 1000)); // crbug.com/1295295
  await Promise.race([haveRtp, chromeTimeout]);

  // Allow some data to be processed to let the jitter buffer to stabilize a bit before measuring
  await new Promise(r => t.step_timeout(r, 5000));

  t.step(() => assert_equals(constrainedReceiver.jitterBufferTarget, null,
   `jitterBufferTarget supported for ${kind}`));

  constrainedReceiver.jitterBufferTarget = target;
  t.step(() => assert_equals(constrainedReceiver.jitterBufferTarget, target,
    `jitterBufferTarget increase target for ${kind}`));

  const [increased, base] = await Promise.all([
    measureDelayFromStats(t, constrainedReceiver, 20),
    measureDelayFromStats(t, unconstrainedReceiver, 20)
  ]);

  t.step(() => assert_greater_than(increased , base,
    `${kind} increased delay ${increased} ` +
    ` greater than base delay ${base}`));

  constrainedReceiver.jitterBufferTarget = 0;

  // Allow the jitter buffer to stabilize a bit before measuring
  await new Promise(r => t.step_timeout(r, 5000));
  t.step(() => assert_equals(constrainedReceiver.jitterBufferTarget, 0,
      `jitterBufferTarget decrease target for ${kind}`));

  const decreased = await measureDelayFromStats(t, constrainedReceiver, 20);

  t.step(() => assert_less_than(decreased, increased,
    `${kind} decreasedDelay ${decreased} ` +
    `less than increased delay ${increased}`));
}

async function measureDelayFromStats(t, receiver, cycles) {

  let statsReport = await receiver.getStats();
  const oldInboundStats = [...statsReport.values()].find(({type}) => type == "inbound-rtp");

  await new Promise(r => t.step_timeout(r, 1000 * cycles));

  statsReport = await receiver.getStats();
  const inboundStats = [...statsReport.values()].find(({type}) => type == "inbound-rtp");

  const delay = ((inboundStats.jitterBufferDelay - oldInboundStats.jitterBufferDelay) /
            (inboundStats.jitterBufferEmittedCount - oldInboundStats.jitterBufferEmittedCount) * 1000);

  return delay;
}
</script>
</body>