test_getUserMedia_audioConstraints_concurrentStreams.html (4896B)
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 type="application/javascript"> 9 createHTML({ 10 title: "getUserMedia multiple times, concurrently, and with different constraints", 11 bug: "1404977" 12 }); 13 /** 14 * Verify that we can successfully call getUserMedia multiple times for the 15 * same device, concurrently. This is checked by calling getUserMedia a number 16 * of times with different constraints. We verify that the stream returned by 17 * that call has the same constraints as requested both immediately after the 18 * call and after all gUM calls have been made. The test then verifies the 19 * streams can be played. 20 */ 21 runTest(async function() { 22 // Compare constraints and return a string with the differences in 23 // echoCancellation, autoGainControl, and noiseSuppression. The string 24 // will be empty if there are no differences. 25 function getConstraintDifferenceString(constraints, otherConstraints) { 26 let diffString = ""; 27 if (constraints.echoCancellation != otherConstraints.echoCancellation) { 28 diffString += "echoCancellation different: " + 29 `${constraints.echoCancellation} != ${otherConstraints.echoCancellation}, `; 30 } 31 if (constraints.autoGainControl != otherConstraints.autoGainControl) { 32 diffString += "autoGainControl different: " + 33 `${constraints.autoGainControl} != ${otherConstraints.autoGainControl}, `; 34 } 35 if (constraints.noiseSuppression != otherConstraints.noiseSuppression) { 36 diffString += "noiseSuppression different: " + 37 `${constraints.noiseSuppression} != ${otherConstraints.noiseSuppression}, `; 38 } 39 // Replace trailing comma and space if any 40 return diffString.replace(/, $/, ""); 41 } 42 43 // We need a real device to get a MediaEngine supporting constraints 44 let audioDevice = SpecialPowers.getCharPref("media.audio_loopback_dev", ""); 45 if (!audioDevice) { 46 todo(false, "No device set by framework. Try --use-test-media-devices"); 47 return; 48 } 49 50 let egn = (e, g, n) => ({ 51 echoCancellation: e, 52 autoGainControl: g, 53 noiseSuppression: n 54 }); 55 56 let constraintCombinations = [ 57 egn(false, false, false), 58 egn(true, false, false), 59 egn(false, true, false), 60 egn(false, false, true), 61 egn(true, true, false), 62 egn(true, false, true), 63 egn(false, true, true), 64 egn(true, true, true), 65 ]; 66 67 // Array to store objects that associate information used in our test such as 68 // constraints, gum streams, and various promises. 69 let testCases = []; 70 71 for (let constraints of constraintCombinations) { 72 let testCase = {requestedConstraints: constraints}; 73 // Provide an id for logging, labeling related elements. 74 testCase.id = `testCase.` + 75 `e=${constraints.echoCancellation}.` + 76 `g=${constraints.noiseSuppression}.` + 77 `n=${constraints.noiseSuppression}`; 78 testCases.push(testCase); 79 testCase.gumStream = 80 await getUserMedia({audio: testCase.requestedConstraints}) 81 .catch(e => Promise.reject(`getUserMedia calls should not fail! Failed at ${testCase.id} with: ${e}!`)); 82 let differenceString = getConstraintDifferenceString( 83 testCase.requestedConstraints, 84 testCase.gumStream.getAudioTracks()[0].getSettings()); 85 ok(!differenceString, 86 `gUM stream for ${testCase.id} should have the same constraints as were ` + 87 `requested from gUM. Differences: ${differenceString}`); 88 } 89 is(testCases.length, 90 constraintCombinations.length, 91 "Should have a stream for each constraint"); 92 93 // Once all streams are collected, make sure the constraints haven't been 94 // mutated by another gUM call. 95 for (let testCase of testCases) { 96 let differenceString = getConstraintDifferenceString( 97 testCase.requestedConstraints, 98 testCase.gumStream.getAudioTracks()[0].getSettings()); 99 ok(!differenceString, 100 `gUM stream for ${testCase.id} should not have had constraints altered after ` + 101 `all gUM calls are done. Differences: ${differenceString}`); 102 } 103 104 // We do not currently have tests to verify the behaviour of the different 105 // constraints. Once we do we should do further verificaiton here. See 106 // bug 1406372, bug 1406376, and bug 1406377. 107 108 for (let testCase of testCases) { 109 let testAudio = createMediaElement("audio", `testAudio.${testCase.id}`); 110 let playback = new MediaStreamPlayback(testAudio, testCase.gumStream); 111 await playback.playMediaWithoutStoppingTracks(false); 112 } 113 114 // Stop the tracks for each stream, we left them running above via 115 // playMediaWithoutStoppingTracks to make sure they can play concurrently. 116 for (let testCase of testCases) { 117 testCase.gumStream.getTracks().map(t => t.stop()); 118 } 119 }); 120 </script> 121 </pre> 122 </body> 123 </html>