|
| 1 | +<!doctype html> |
| 2 | +<meta charset=utf-8> |
| 3 | +<script src="/resources/testharness.js"></script> |
| 4 | +<script src="/resources/testharnessreport.js"></script> |
| 5 | +<script src="RTCPeerConnection-helper.js"></script> |
| 6 | +<script> |
| 7 | +'use strict'; |
| 8 | + |
| 9 | +async function hasStats(pc, type) { |
| 10 | + const report = await pc.getStats(); |
| 11 | + for (const stats of report.values()) { |
| 12 | + if (stats.type == type) { |
| 13 | + return true; |
| 14 | + } |
| 15 | + } |
| 16 | + return false; |
| 17 | +} |
| 18 | + |
| 19 | +async function getInboundRtpPollUntilItExists(pc, kTimeoutMs = 10000) { |
| 20 | + const t0 = performance.now(); |
| 21 | + while (performance.now() - t0 < kTimeoutMs) { |
| 22 | + const report = await pc.getStats(); |
| 23 | + for (const stats of report.values()) { |
| 24 | + if (stats.type == 'inbound-rtp') { |
| 25 | + return stats; |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + return null; |
| 30 | +} |
| 31 | + |
| 32 | +promise_test(async t => { |
| 33 | + const pc = new RTCPeerConnection(); |
| 34 | + t.add_cleanup(() => pc.close()); |
| 35 | + |
| 36 | + pc.addTransceiver('video'); |
| 37 | + assert_false(await hasStats(pc, 'outbound-rtp'), |
| 38 | + 'outbound-rtp does not exist after addTransceiver'); |
| 39 | + await pc.setLocalDescription(); |
| 40 | + assert_false(await hasStats(pc, 'outbound-rtp'), |
| 41 | + 'outbound-rtp does not exist in have-local-offer'); |
| 42 | +}, `RTCOutboundRtpStreamStats does not exist as early as have-local-offer`); |
| 43 | + |
| 44 | +// This test does not exchange ICE candidates, meaning no packets are sent. |
| 45 | +// We should still see outbound-rtp stats because they are created by the O/A. |
| 46 | +promise_test(async t => { |
| 47 | + const pc1 = new RTCPeerConnection(); |
| 48 | + t.add_cleanup(() => pc1.close()); |
| 49 | + const pc2 = new RTCPeerConnection(); |
| 50 | + t.add_cleanup(() => pc1.close()); |
| 51 | + |
| 52 | + // Offer to send. See previous test for assertions that the outbound-rtp is |
| 53 | + // not created this early, which this test does not care about. |
| 54 | + pc1.addTransceiver('video'); |
| 55 | + await pc1.setLocalDescription(); |
| 56 | + |
| 57 | + // Answer to send. |
| 58 | + await pc2.setRemoteDescription(pc1.localDescription); |
| 59 | + const [transceiver] = pc2.getTransceivers(); |
| 60 | + transceiver.direction = 'sendrecv'; |
| 61 | + assert_false(await hasStats(pc2, 'outbound-rtp'), |
| 62 | + 'outbound-rtp does not exist in has-remote-offer'); |
| 63 | + await pc2.setLocalDescription(); |
| 64 | + assert_true(await hasStats(pc2, 'outbound-rtp'), |
| 65 | + 'outbound-rtp exists after answerer returns to stable'); |
| 66 | + |
| 67 | + // Complete offerer negotiation. |
| 68 | + await pc1.setRemoteDescription(pc2.localDescription); |
| 69 | + assert_true(await hasStats(pc1, 'outbound-rtp'), |
| 70 | + 'outbound-rtp exists after offerer returns to stable'); |
| 71 | +}, `RTCOutboundRtpStreamStats exists after returning to stable`); |
| 72 | + |
| 73 | +promise_test(async t => { |
| 74 | + const pc1 = new RTCPeerConnection(); |
| 75 | + t.add_cleanup(() => pc1.close()); |
| 76 | + const pc2 = new RTCPeerConnection(); |
| 77 | + t.add_cleanup(() => pc1.close()); |
| 78 | + pc1.onicecandidate = e => pc2.addIceCandidate(e.candidate); |
| 79 | + pc2.onicecandidate = e => pc1.addIceCandidate(e.candidate); |
| 80 | + |
| 81 | + // Negotaite to send, but don't send anything yet (track is null). |
| 82 | + const {sender} = pc1.addTransceiver('video'); |
| 83 | + await pc1.setLocalDescription(); |
| 84 | + await pc2.setRemoteDescription(pc1.localDescription); |
| 85 | + await pc2.setLocalDescription(); |
| 86 | + await pc1.setRemoteDescription(pc2.localDescription); |
| 87 | + assert_false(await hasStats(pc2, 'inbound-rtp'), |
| 88 | + 'inbound-rtp does not exist before packets are received'); |
| 89 | + |
| 90 | + // Start sending. This results in inbound-rtp being created. |
| 91 | + const stream = await getNoiseStream({video:true}); |
| 92 | + const [track] = stream.getTracks(); |
| 93 | + await sender.replaceTrack(track); |
| 94 | + const inboundRtp = await getInboundRtpPollUntilItExists(pc2); |
| 95 | + assert_not_equals( |
| 96 | + inboundRtp, null, |
| 97 | + 'inbound-rtp should be created in response to the sender having a track'); |
| 98 | + assert_greater_than( |
| 99 | + inboundRtp.packetsReceived, 0, |
| 100 | + 'inbound-rtp must only exist after packets have been received'); |
| 101 | +}, `RTCInboundRtpStreamStats are created by packet reception`); |
| 102 | +</script> |
0 commit comments