Bug 1904964: Collect audio device information lighter. r=media-playback-reviewers,padenot

Differential Revision: https://phabricator.services.mozilla.com/D215031
This commit is contained in:
Fatih 2024-08-07 00:31:46 +00:00
parent 7670d63768
commit 2983c483d9
5 changed files with 69 additions and 75 deletions

View File

@ -114,6 +114,12 @@ static bool IsWindowAllowedToPlayByTraits(nsPIDOMWindowInner* aWindow) {
return true;
}
if (currentDoc->GetPrincipal()->Equals(
nsContentUtils::GetFingerprintingProtectionPrincipal())) {
AUTOPLAY_LOG("Allow autoplay as in fingerprinting protection document.");
return true;
}
return false;
}

View File

@ -424,6 +424,9 @@ export class UserCharacteristicsPageService {
"oscpu",
"pdfViewer",
"platform",
"audioFrames",
"audioRate",
"audioChannels",
],
};

View File

@ -1182,6 +1182,22 @@ async function populateMathML() {
}, {});
}
async function populateAudioDeviceProperties() {
const ctx = new AudioContext();
await ctx.resume();
// Give firefox some time to calculate latency
await new Promise(resolve => setTimeout(resolve, 2000));
// All the other properties (min/max decibels, smoothingTimeConstant,
// fftSize, frequencyBinCount, baseLatency) are hardcoded.
return {
audioFrames: ctx.outputLatency * ctx.sampleRate,
audioRate: ctx.sampleRate,
audioChannels: ctx.destination.maxChannelCount,
};
}
// A helper function to generate an array of asynchronous functions to populate
// canvases using both software and hardware rendering.
function getCanvasSources() {
@ -1252,6 +1268,7 @@ async function startPopulating() {
populateMathML,
populateCSSQueries,
populateNavigatorProperties,
populateAudioDeviceProperties,
];
// Catches errors in promise-creating functions. E.g. if populateVoiceList
// throws an error before returning any of its `key: (Promise<any> | any)`

View File

@ -2028,10 +2028,11 @@ characteristics:
# is more like 'technical' (per the Data Review), I'm marking highly sensitive.
- highly_sensitive
audio_devices:
type: text
audio_frames:
type: quantity
unit: int
description: >
A JSON object containing sample rate and max channel count of every audio device, and output latency of default device
Audio frames
lifetime: application
send_in_pings:
- user-characteristics
@ -2044,7 +2045,45 @@ characteristics:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1896490#c3
expires: never
data_sensitivity:
- highly_sensitive
- technical
audio_rate:
type: quantity
unit: int
description: >
Audio sampling rate
lifetime: application
send_in_pings:
- user-characteristics
notification_emails:
- tom@mozilla.com
bugs:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1879151
- https://bugzilla.mozilla.org/show_bug.cgi?id=1896490
data_reviews:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1896490#c3
expires: never
data_sensitivity:
- technical
audio_channels:
type: quantity
unit: int
description: >
Audio sampling rate
lifetime: application
send_in_pings:
- user-characteristics
notification_emails:
- tom@mozilla.com
bugs:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1879151
- https://bugzilla.mozilla.org/show_bug.cgi?id=1896490
data_reviews:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1896490#c3
expires: never
data_sensitivity:
- technical
languages:
type: string

View File

@ -46,8 +46,6 @@
#include "mozilla/dom/MediaDeviceInfoBinding.h"
#include "mozilla/MozPromise.h"
#include "nsThreadUtils.h"
#include "CubebDeviceEnumerator.h"
#include "mozilla/media/MediaUtils.h"
#include "mozilla/dom/Navigator.h"
#include "nsIGSettingsService.h"
#include "nsITimer.h"
@ -481,74 +479,6 @@ RefPtr<PopulatePromise> PopulateMediaDevices() {
return populatePromise;
}
RefPtr<PopulatePromise> PopulateAudioDeviceProperties() {
RefPtr<PopulatePromise> populatePromise = new PopulatePromise(__func__);
NS_DispatchBackgroundTask(
NS_NewRunnableFunction("PopulateAudioDeviceProperties", [=]() {
RefPtr<CubebDeviceEnumerator> enumerator =
CubebDeviceEnumerator::GetInstance();
RefPtr<const CubebDeviceEnumerator::AudioDeviceSet> devices;
nsCString output = "{"_ns;
nsCString list = "["_ns;
devices = enumerator->EnumerateAudioInputDevices();
for (const auto& deviceInfo : *devices) {
uint32_t maxChannels;
deviceInfo->GetMaxChannels(&maxChannels);
list.AppendPrintf(R"({"rate":%d,"channels":%d)",
deviceInfo->DefaultRate(), maxChannels);
if (deviceInfo->Preferred()) {
list.Append(",\"default\":1");
}
list.Append("}");
if (&deviceInfo != &devices->LastElement()) {
list.Append(',');
}
}
list.Append(']');
output.AppendPrintf(R"("devices":%s,)", list.get());
double inputMean, inputStdDev, outputMean, outputStdDev;
CubebUtils::EstimatedLatencyDefaultDevices(&inputMean, &inputStdDev,
CubebUtils::Side::Input);
CubebUtils::EstimatedLatencyDefaultDevices(&outputMean, &outputStdDev,
CubebUtils::Side::Output);
cubeb_stream_params output_params;
output_params.format = CUBEB_SAMPLE_FLOAT32NE;
output_params.rate = CubebUtils::PreferredSampleRate(false);
output_params.channels = 2;
output_params.layout = CUBEB_LAYOUT_UNDEFINED;
output_params.prefs =
CubebUtils::GetDefaultStreamPrefs(CUBEB_DEVICE_TYPE_OUTPUT);
uint32_t latencyFrames =
CubebUtils::GetCubebMTGLatencyInFrames(&output_params);
RefPtr<AudioDeviceInfo> defaultOutputDevice =
enumerator->DefaultDevice(CubebDeviceEnumerator::Side::OUTPUT);
output.AppendPrintf(
R"("latency":[%f,%f,%f,%f],"latFrames":%d,"rate":%u,"channels":%u)",
inputMean, inputStdDev, outputMean, outputStdDev, latencyFrames,
defaultOutputDevice->DefaultRate(),
defaultOutputDevice->MaxChannels());
output.Append("}");
glean::characteristics::audio_devices.Set(output);
NS_DispatchToMainThread(NS_NewRunnableFunction(
"PopulateAudioDeviceProperties",
[=]() { populatePromise->Resolve(void_t(), __func__); }));
}));
return populatePromise;
}
void PopulateLanguages() {
// All navigator.languages, navigator.language, and Accept-Languages header
// use Navigator::GetAcceptLanguages to create a language list. It is
@ -957,7 +887,6 @@ void nsUserCharacteristics::PopulateDataAndEventuallySubmit(
// ------------------------------------------------------------------------
promises.AppendElement(PopulateMediaDevices());
promises.AppendElement(PopulateAudioDeviceProperties());
promises.AppendElement(PopulateTimeZone());
promises.AppendElement(TimoutPromise(PopulatePointerInfo(), 5 * 60 * 1000,
"PopulatePointerInfo"_ns));