RTCPeerConnection-videoDetectorTest.html (3643B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <meta name="timeout" content="long"> 4 <title>RTCPeerConnection Video detector test</title> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src="RTCPeerConnection-helper.js"></script> 8 <script> 9 'use strict'; 10 11 // This test verifies that the helper function "detectSignal" from 12 // RTCPeerConnectionHelper, which is used to detect changes in a video 13 // signal, performs properly for a range of "signal" values. 14 15 // If it fails, it indicates that the video codec used in this particular 16 // browser at this time doesn't reproduce the luma signal reliably enough 17 // for this particular application, which may lead to other tests that 18 // use the "detectSignal" helper failing without an obvious cause. 19 20 // The most likely failure is timeout - which will happen if the 21 // luma value detected doesn't settle within the margin of error before 22 // the test times out. 23 24 async function signalSettlementTime(t, v, sender, signal, backgroundTrack) { 25 const detectionStream = await getNoiseStream({video: {signal}}); 26 const [detectionTrack] = detectionStream.getTracks(); 27 try { 28 await sender.replaceTrack(detectionTrack); 29 const framesBefore = v.getVideoPlaybackQuality().totalVideoFrames; 30 await detectSignal(t, v, signal); 31 const framesAfter = v.getVideoPlaybackQuality().totalVideoFrames; 32 await sender.replaceTrack(backgroundTrack); 33 await detectSignal(t, v, 100); 34 return framesAfter - framesBefore; 35 } finally { 36 detectionTrack.stop(); 37 } 38 } 39 40 promise_test(async t => { 41 const v = document.createElement('video'); 42 v.autoplay = true; 43 const pc1 = new RTCPeerConnection(); 44 const pc2 = new RTCPeerConnection(); 45 t.add_cleanup(() => pc1.close()); 46 t.add_cleanup(() => pc2.close()); 47 const stream1 = await getNoiseStream({video: {signal: 100}}); 48 const [track1] = stream1.getTracks(); 49 t.add_cleanup(() => track1.stop()); 50 51 const sender = pc1.addTrack(track1); 52 const haveTrackEvent = new Promise(r => pc2.ontrack = r); 53 exchangeIceCandidates(pc1, pc2); 54 await exchangeOfferAnswer(pc1, pc2); 55 v.srcObject = new MediaStream([(await haveTrackEvent).track]); 56 await new Promise(r => v.onloadedmetadata = r); 57 // The basic signal is a track with signal 100. We replace this 58 // with tracks with signal from 0 to 255 and see if they are all 59 // reliably detected. 60 await detectSignal(t, v, 100); 61 // A few buffered frames are received with the old content, and a few 62 // frames may not have settled on exactly the right value. In testing, 63 // this test passes with maxFrames = 3; give a little more margin. 64 const maxFrames = 7; 65 // Test values 0 and 255 66 let maxCount = await signalSettlementTime(t, v, sender, 0, track1); 67 assert_less_than(maxCount, maxFrames, 68 'Should get the black value within ' + maxFrames + ' frames'); 69 maxCount = Math.max( 70 await signalSettlementTime(t, v, sender, 255, track1), maxCount); 71 assert_less_than(maxCount, maxFrames, 72 'Should get the white value within ' + maxFrames + ' frames'); 73 // Test a set of other values - far enough apart to make the test fast. 74 for (let signal = 2; signal <= 255; signal += 47) { 75 if (Math.abs(signal - 100) > 10) { 76 const count = await signalSettlementTime(t, v, sender, signal, track1); 77 maxCount = Math.max(count, maxCount); 78 assert_less_than(maxCount, 10, 79 'Should get value ' + signal + ' within ' + maxFrames + ' frames'); 80 } 81 } 82 assert_less_than(maxCount, 10, 'Should get the right value within 10 frames'); 83 }, 'Signal detector detects track change within reasonable time'); 84 </script>