test_getUserMedia_basicAudio_loopback.html (3989B)
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 createHTML({ 11 title: "getUserMedia Basic Audio Test Loopback", 12 bug: "1406350", 13 visible: true 14 }); 15 16 /** 17 * Run a test to verify the use of LoopbackTone as audio input. 18 */ 19 runTest(async () => { 20 if (!SpecialPowers.getCharPref("media.audio_loopback_dev", "")) { 21 todo(false, "No loopback device set by framework. Try --use-test-media-devices"); 22 return; 23 } 24 25 // Start a tone so that the gUM call will record something. 26 const audioContext = new AudioContext(); 27 let tone = new LoopbackTone(audioContext, TEST_AUDIO_FREQ); 28 tone.start(); 29 // At this point a tone has been instantiated on frequency 30 // TEST_AUDIO_FREQ (1 kHz). Verify that a tone is detected on that 31 // frequency. 32 info("Capturing at default frequency"); 33 const stream = await getUserMedia({audio: true}); 34 35 try { 36 const analyser = new AudioStreamAnalyser(audioContext, stream); 37 analyser.enableDebugCanvas(); 38 await analyser.waitForAnalysisSuccess(array => { 39 // High energy on 1000 Hz low energy around that 40 const freg_50Hz = array[analyser.binIndexForFrequency(50)]; 41 const freq = array[analyser.binIndexForFrequency(TEST_AUDIO_FREQ)]; 42 const freq_2000Hz = array[analyser.binIndexForFrequency(2000)]; 43 44 info("Analysing audio frequency - low:target:high = " 45 + freg_50Hz + ':' + freq + ':' + freq_2000Hz); 46 return freg_50Hz < 50 && freq > 200 && freq_2000Hz < 50; 47 }); 48 49 // Use the LoopbackTone API to change the frequency of the tone. 50 // Verify that a tone is detected on the new frequency (800 Hz). 51 info("Change loopback tone frequency"); 52 tone.changeFrequency(800); 53 await analyser.waitForAnalysisSuccess(array => { 54 const freg_50Hz = array[analyser.binIndexForFrequency(50)]; 55 const freq = array[analyser.binIndexForFrequency(800)]; 56 const freq_2000Hz = array[analyser.binIndexForFrequency(2000)]; 57 58 info("Analysing audio frequency - low:target:high = " 59 + freg_50Hz + ':' + freq + ':' + freq_2000Hz); 60 return freg_50Hz < 50 && freq > 200 && freq_2000Hz < 50; 61 }); 62 63 // Create a second tone at a different frequency. 64 // Verify that both tones are detected. 65 info("Multiple loopback tones"); 66 tone.changeFrequency(TEST_AUDIO_FREQ); 67 const second_tone = new LoopbackTone(audioContext, 2000); 68 second_tone.start(); 69 await analyser.waitForAnalysisSuccess(array => { 70 const freg_50Hz = array[analyser.binIndexForFrequency(50)]; 71 const freq = array[analyser.binIndexForFrequency(TEST_AUDIO_FREQ)]; 72 const freq_2000Hz = array[analyser.binIndexForFrequency(2000)]; 73 const freq_4000Hz = array[analyser.binIndexForFrequency(4000)]; 74 75 info("Analysing audio frequency - low:target1:target2:high = " 76 + freg_50Hz + ':' + freq + ':' + freq_2000Hz + ':' + freq_4000Hz); 77 return freg_50Hz < 50 && freq > 200 && freq_2000Hz > 200 && freq_4000Hz < 50; 78 }); 79 80 // Stop all tones and verify that there is no audio on the given frequencies. 81 info("Stop all loopback tones"); 82 tone.stop(); 83 second_tone.stop() 84 await analyser.waitForAnalysisSuccess(array => { 85 const freg_50Hz = array[analyser.binIndexForFrequency(50)]; 86 const freq = array[analyser.binIndexForFrequency(TEST_AUDIO_FREQ)]; 87 const freq_2000Hz = array[analyser.binIndexForFrequency(2000)]; 88 89 info("Analysing audio frequency - low:target:high = " 90 + freg_50Hz + ':' + freq + ':' + freq_2000Hz); 91 return freg_50Hz < 50 && freq < 50 && freq_2000Hz < 50; 92 }); 93 } finally { 94 for (let t of stream.getTracks()) { 95 t.stop(); 96 } 97 } 98 }); 99 </script> 100 </pre> 101 </body> 102 </html>