test_webAudio_muteTab.html (2900B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <script type="application/javascript" src="mediaStreamPlayback.js"></script> 5 </head> 6 <body> 7 <pre id="test"> 8 9 <script> 10 /* import-globals-from ../../webrtc/tests/mochitests/mediaStreamPlayback.js */ 11 createHTML({ 12 title: "Check tab muting when the tab plays audio via the Web Audio API", 13 bug: "1346880", 14 visible: false 15 }); 16 17 /** 18 * Check that muting a tab results in no audible audio: mute a tab, in which 19 * an OscillatorNode is playing. The default audio output device is a 20 * pulseaudio null-sink. Simulateously, record the other side of the null 21 * sink, and check that no audio has been written to the sink, because the tab 22 * was muted. Then, umute the tab and check that audio is being sent to the 23 * null-sink. 24 */ 25 runTest(async () => { 26 if (!SpecialPowers.getCharPref("media.audio_loopback_dev", "")) { 27 todo(false, "No loopback device set by framework. Try --use-test-media-devices"); 28 return; 29 } 30 31 // Mute the tab 32 await SpecialPowers.toggleMuteState(true, window.top); 33 34 const stream = await getUserMedia({audio: { 35 noiseSuppression: false, 36 echoCancellation: false, 37 autoGainControl: false, 38 }}); 39 try { 40 const ac = new AudioContext(); 41 const osc = new OscillatorNode(ac); 42 osc.connect(ac.destination); 43 osc.start(); 44 45 const analyser = new AudioStreamAnalyser(ac, stream); 46 // Wait for some time, checking there is only ever silent audio in the 47 // loopback stream. `waitForAnalysisSuccess` runs off requestAnimationFrame 48 let silenceFor = 3 / (1 / 60); 49 await analyser.waitForAnalysisSuccess(array => { 50 // `array` has values between 0 and 255, 0 being silence. 51 const sum = array.reduce((acc, v) => { return acc + v; }); 52 if (sum == 0) { 53 silenceFor--; 54 } else { 55 info(`Sum of the array values ${sum}`); 56 ok(false, `Found non-silent data in the loopback stream while the tab was muted.`); 57 return true; 58 } 59 if (silenceFor == 0) { 60 ok(true, "Muting the tab was effective"); 61 } 62 return silenceFor == 0; 63 }); 64 65 // Unmute the tab 66 await SpecialPowers.toggleMuteState(false, window.top); 67 68 await analyser.waitForAnalysisSuccess(array => { 69 // `array` has values between 0 and 255, 0 being silence. 70 const sum = array.reduce((acc, v) => { return acc + v; }); 71 if (sum != 0) { 72 info(`Sum after unmuting ${sum}`); 73 ok(true, "Unmuting the tab was effective"); 74 return true; 75 } else { 76 // Increment again if we find silence. 77 silenceFor++; 78 if (silenceFor > 100) { 79 ok(false, "Unmuting wasn't effective") 80 return true; 81 } 82 return false; 83 } 84 }); 85 } finally { 86 for (let t of stream.getTracks()) { 87 t.stop(); 88 } 89 } 90 }); 91 </script> 92 </pre> 93 </body> 94 </html>