test_getUserMedia_mediaElementCapture_audio.html (4494B)
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 <script> 9 10 createHTML({ 11 bug: "1259788", 12 title: "Test CaptureStream audio content on HTMLMediaElement playing a gUM MediaStream", 13 visible: true 14 }); 15 16 var audioContext; 17 var gUMAudioElement; 18 var analyser; 19 let tone; 20 runTest(() => getUserMedia({audio: { echoCancellation: false }}) 21 .then(stream => { 22 gUMAudioElement = createMediaElement("audio", "gUMAudio"); 23 gUMAudioElement.srcObject = stream; 24 25 audioContext = new AudioContext(); 26 // Start a tone so that the gUM call will record something even with 27 // --use-test-media-devices. 28 tone = new LoopbackTone(audioContext, TEST_AUDIO_FREQ); 29 tone.start(); 30 31 info("Capturing"); 32 33 analyser = new AudioStreamAnalyser(audioContext, 34 gUMAudioElement.mozCaptureStream()); 35 analyser.enableDebugCanvas(); 36 return analyser.waitForAnalysisSuccess(array => 37 array[analyser.binIndexForFrequency(50)] < 50 && 38 array[analyser.binIndexForFrequency(TEST_AUDIO_FREQ)] > 200 && 39 array[analyser.binIndexForFrequency(2500)] < 50); 40 }) 41 .then(() => { 42 info("Audio flowing. Pausing."); 43 gUMAudioElement.pause(); 44 45 return analyser.waitForAnalysisSuccess(array => 46 array[analyser.binIndexForFrequency(50)] < 50 && 47 array[analyser.binIndexForFrequency(TEST_AUDIO_FREQ)] < 50 && 48 array[analyser.binIndexForFrequency(2500)] < 50); 49 }) 50 .then(() => { 51 info("Audio stopped flowing. Playing."); 52 gUMAudioElement.play(); 53 54 return analyser.waitForAnalysisSuccess(array => 55 array[analyser.binIndexForFrequency(50)] < 50 && 56 array[analyser.binIndexForFrequency(TEST_AUDIO_FREQ)] > 200 && 57 array[analyser.binIndexForFrequency(2500)] < 50); 58 }) 59 .then(() => { 60 info("Audio flowing. Removing source."); 61 var stream = gUMAudioElement.srcObject; 62 gUMAudioElement.srcObject = null; 63 64 return analyser.waitForAnalysisSuccess(array => 65 array[analyser.binIndexForFrequency(50)] < 50 && 66 array[analyser.binIndexForFrequency(TEST_AUDIO_FREQ)] < 50 && 67 array[analyser.binIndexForFrequency(2500)] < 50) 68 .then(() => stream); 69 }) 70 .then(stream => { 71 info("Audio stopped flowing. Setting source."); 72 gUMAudioElement.srcObject = stream; 73 74 return analyser.waitForAnalysisSuccess(array => 75 array[analyser.binIndexForFrequency(50)] < 50 && 76 array[analyser.binIndexForFrequency(TEST_AUDIO_FREQ)] > 200 && 77 array[analyser.binIndexForFrequency(2500)] < 50); 78 }) 79 .then(() => { 80 info("Audio flowing from new source. Adding a track."); 81 let oscillator = audioContext.createOscillator(); 82 oscillator.type = 'sine'; 83 oscillator.frequency.value = 2000; 84 oscillator.start(); 85 86 let oscOut = audioContext.createMediaStreamDestination(); 87 oscillator.connect(oscOut); 88 89 gUMAudioElement.srcObject.addTrack(oscOut.stream.getTracks()[0]); 90 91 return analyser.waitForAnalysisSuccess(array => 92 array[analyser.binIndexForFrequency(50)] < 50 && 93 array[analyser.binIndexForFrequency(TEST_AUDIO_FREQ)] > 200 && 94 array[analyser.binIndexForFrequency(1500)] < 50 && 95 array[analyser.binIndexForFrequency(2000)] > 200 && 96 array[analyser.binIndexForFrequency(2500)] < 50); 97 }) 98 .then(() => { 99 info("Audio flowing from new track. Removing a track."); 100 101 const gUMTrack = gUMAudioElement.srcObject.getTracks()[0]; 102 gUMAudioElement.srcObject.removeTrack(gUMTrack); 103 104 is(gUMAudioElement.srcObject.getTracks().length, 1, 105 "A track should have been removed"); 106 107 return analyser.waitForAnalysisSuccess(array => 108 array[analyser.binIndexForFrequency(50)] < 50 && 109 array[analyser.binIndexForFrequency(TEST_AUDIO_FREQ)] < 50 && 110 array[analyser.binIndexForFrequency(1500)] < 50 && 111 array[analyser.binIndexForFrequency(2000)] > 200 && 112 array[analyser.binIndexForFrequency(2500)] < 50) 113 .then(() => [gUMTrack, ...gUMAudioElement.srcObject.getTracks()] 114 .forEach(t => t.stop())); 115 }) 116 .then(() => ok(true, "Test passed.")) 117 .catch(e => ok(false, "Test failed: " + e + (e.stack ? "\n" + e.stack : ""))) 118 .finally(() => tone.stop())); 119 120 </script> 121 </pre> 122 </body> 123 </html>