Bug 1346880 - Test that a muted tab that plays audio via the Web Audio API is really muted using PulseAudio monitoring devices. r=pehrsons,alwu

Differential Revision: https://phabricator.services.mozilla.com/D93653
This commit is contained in:
Paul Adenot 2020-10-19 08:45:04 +00:00
Родитель fb3387d796
Коммит 307c6e1733
2 изменённых файлов: 95 добавлений и 0 удалений

Просмотреть файл

@ -253,4 +253,7 @@ skip-if = (os == "win" && processor == "aarch64") # aarch64 due to 1538360
[test_waveShaperInvalidLengthCurve.html]
[test_WebAudioMemoryReporting.html]
[test_audioContextParams_sampleRate.html]
[test_webAudio_muteTab.html]
scheme = https
skip-if = os == 'mac' || os == 'win' || toolkit == 'android' # Bug 1404995, no loopback devices on some platforms
[test_audioContextParams_recordNonDefaultSampleRate.html]

Просмотреть файл

@ -0,0 +1,92 @@
<!DOCTYPE HTML>
<html>
<head>
<script type="application/javascript" src="mediaStreamPlayback.js"></script>
</head>
<body>
<pre id="test">
<script>
(async function() {
await createHTML({
title: "Check tab muting when the tab plays audio via the Web Audio API",
bug: "1346880",
visible: false
});
/**
* Check that muting a tab results in no audible audio: mute a tab, in which
* an OscillatorNode is playing. The default audio output device is a
* pulseaudio null-sink. Simulateously, record the other side of the null
* sink, and check that no audio has been written to the sink, because the tab
* was muted. Then, umute the tab and check that audio is being sent to the
* null-sink. */
await runTestWhenReady(async () => {
let audioDevice = SpecialPowers.getCharPref("media.audio_loopback_dev", "");
if (!audioDevice) {
todo(false, "No loopback device set by framework. Try --use-test-media-devices");
return;
}
// Mute the tab
await SpecialPowers.toggleMuteState(true, window.top);
// Don't use a loopback tone, the loopback device is here to check that
// nothing is output because the tab is muted.
DISABLE_LOOPBACK_TONE = true;
let stream = await getUserMedia({audio: true});
let ac = new AudioContext();
let osc = new OscillatorNode(ac);
osc.connect(ac.destination);
osc.start();
let analyser = new AudioStreamAnalyser(ac, stream);
// Wait for some time, checking there is only ever silent audio in the
// loopback stream. `waitForAnalysisSuccess` runs off requestAnimationFrame
let silenceFor = 3 / (1 / 60);
await analyser.waitForAnalysisSuccess(array => {
// `array` has values between 0 and 255, 0 being silence.
let sum = array.reduce((acc, v) => { return acc + v; });
if (sum == 0) {
silenceFor--;
} else {
info(`Sum of the array values ${sum}`);
ok(false, `Found non-silent data in the loopback stream while the tab was muted.`);
return true;
}
if (silenceFor == 0) {
ok(true, "Muting the tab was effective");
}
return silenceFor == 0;
});
// Unmute the tab
await SpecialPowers.toggleMuteState(false, window.top);
await analyser.waitForAnalysisSuccess(array => {
// `array` has values between 0 and 255, 0 being silence.
let sum = array.reduce((acc, v) => { return acc + v; });
if (sum != 0) {
info(`Sum after unmuting ${sum}`);
ok(true, "Unmuting the tab was effective");
return true;
} else {
// Increment again if we find silence.
silenceFor++;
if (silenceFor > 100) {
ok(false, "Unmuting wasn't effective")
return true;
}
return false;
}
});
});
finish()
})();
</script>
</pre>
</body>
</html>