1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "CubebUtils.h"
8
9 #include "audio_thread_priority.h"
10 #include "MediaInfo.h"
11 #include "mozilla/AbstractThread.h"
12 #include "mozilla/dom/ContentChild.h"
13 #include "mozilla/dom/AudioDeviceInfo.h"
14 #include "mozilla/ipc/FileDescriptor.h"
15 #include "mozilla/Logging.h"
16 #include "mozilla/Preferences.h"
17 #include "mozilla/Components.h"
18 #include "mozilla/Sprintf.h"
19 #include "mozilla/StaticMutex.h"
20 #include "mozilla/StaticPtr.h"
21 #include "mozilla/Telemetry.h"
22 #include "mozilla/UnderrunHandler.h"
23 #include "nsAutoRef.h"
24 #include "nsDebug.h"
25 #include "nsIStringBundle.h"
26 #include "nsString.h"
27 #include "nsThreadUtils.h"
28 #include "prdtoa.h"
29 #include <algorithm>
30 #include <stdint.h>
31 #ifdef MOZ_WIDGET_ANDROID
32 # include "mozilla/java/GeckoAppShellWrappers.h"
33 #endif
34 #ifdef XP_WIN
35 # include "mozilla/mscom/EnsureMTA.h"
36 #endif
37 #include "audioipc_server_ffi_generated.h"
38 #include "audioipc_client_ffi_generated.h"
39 #include <cmath>
40 #include <thread>
41 #include "AudioThreadRegistry.h"
42 #include "mozilla/StaticPrefs_media.h"
43
44 #define AUDIOIPC_POOL_SIZE_DEFAULT 1
45 #define AUDIOIPC_STACK_SIZE_DEFAULT (64 * 4096)
46
47 #define PREF_VOLUME_SCALE "media.volume_scale"
48 #define PREF_CUBEB_BACKEND "media.cubeb.backend"
49 #define PREF_CUBEB_OUTPUT_DEVICE "media.cubeb.output_device"
50 #define PREF_CUBEB_LATENCY_PLAYBACK "media.cubeb_latency_playback_ms"
51 #define PREF_CUBEB_LATENCY_MTG "media.cubeb_latency_mtg_frames"
52 // Allows to get something non-default for the preferred sample-rate, to allow
53 // troubleshooting in the field and testing.
54 #define PREF_CUBEB_FORCE_SAMPLE_RATE "media.cubeb.force_sample_rate"
55 #define PREF_CUBEB_LOGGING_LEVEL "media.cubeb.logging_level"
56 // Hidden pref used by tests to force failure to obtain cubeb context
57 #define PREF_CUBEB_FORCE_NULL_CONTEXT "media.cubeb.force_null_context"
58 #define PREF_CUBEB_OUTPUT_VOICE_ROUTING "media.cubeb.output_voice_routing"
59 #define PREF_CUBEB_SANDBOX "media.cubeb.sandbox"
60 #define PREF_AUDIOIPC_POOL_SIZE "media.audioipc.pool_size"
61 #define PREF_AUDIOIPC_STACK_SIZE "media.audioipc.stack_size"
62
63 #if (defined(XP_LINUX) && !defined(MOZ_WIDGET_ANDROID)) || \
64 defined(XP_MACOSX) || (defined(XP_WIN) && !defined(_ARM64_))
65 # define MOZ_CUBEB_REMOTING
66 #endif
67
68 namespace mozilla {
69
70 namespace {
71
72 using Telemetry::LABELS_MEDIA_AUDIO_BACKEND;
73 using Telemetry::LABELS_MEDIA_AUDIO_INIT_FAILURE;
74
75 LazyLogModule gCubebLog("cubeb");
76
CubebLogCallback(const char * aFmt,...)77 void CubebLogCallback(const char* aFmt, ...) {
78 char buffer[1024];
79
80 va_list arglist;
81 va_start(arglist, aFmt);
82 VsprintfLiteral(buffer, aFmt, arglist);
83 MOZ_LOG(gCubebLog, LogLevel::Error, ("%s", buffer));
84 va_end(arglist);
85 }
86
87 // This mutex protects the variables below.
88 StaticMutex sMutex;
89 enum class CubebState {
90 Uninitialized = 0,
91 Initialized,
92 Shutdown
93 } sCubebState = CubebState::Uninitialized;
94 cubeb* sCubebContext;
95 double sVolumeScale = 1.0;
96 uint32_t sCubebPlaybackLatencyInMilliseconds = 100;
97 uint32_t sCubebMTGLatencyInFrames = 512;
98 // If sCubebForcedSampleRate is zero, PreferredSampleRate will return the
99 // preferred sample-rate for the audio backend in use. Otherwise, it will be
100 // used as the preferred sample-rate.
101 uint32_t sCubebForcedSampleRate = 0;
102 bool sCubebPlaybackLatencyPrefSet = false;
103 bool sCubebMTGLatencyPrefSet = false;
104 bool sAudioStreamInitEverSucceeded = false;
105 bool sCubebForceNullContext = false;
106 bool sRouteOutputAsVoice = false;
107 #ifdef MOZ_CUBEB_REMOTING
108 bool sCubebSandbox = false;
109 size_t sAudioIPCPoolSize;
110 size_t sAudioIPCStackSize;
111 #endif
112 StaticAutoPtr<char> sBrandName;
113 StaticAutoPtr<char> sCubebBackendName;
114 StaticAutoPtr<char> sCubebOutputDeviceName;
115 StaticAutoPtr<AudioThreadRegistry> sAudioThreadRegistry;
116 #ifdef MOZ_WIDGET_ANDROID
117 // Counts the number of time a request for switching to global "communication
118 // mode" has been received. If this is > 0, global communication mode is to be
119 // enabled. If it is 0, the global communication mode is to be disabled.
120 // This allows to correctly track the global behaviour to adopt accross
121 // asynchronous GraphDriver changes, on Android.
122 int sInCommunicationCount = 0;
123 #endif
124
125 const char kBrandBundleURL[] = "chrome://branding/locale/brand.properties";
126
127 std::unordered_map<std::string, LABELS_MEDIA_AUDIO_BACKEND>
128 kTelemetryBackendLabel = {
129 {"audiounit", LABELS_MEDIA_AUDIO_BACKEND::audiounit},
130 {"audiounit-rust", LABELS_MEDIA_AUDIO_BACKEND::audiounit_rust},
131 {"aaudio", LABELS_MEDIA_AUDIO_BACKEND::aaudio},
132 {"opensl", LABELS_MEDIA_AUDIO_BACKEND::opensl},
133 {"wasapi", LABELS_MEDIA_AUDIO_BACKEND::wasapi},
134 {"winmm", LABELS_MEDIA_AUDIO_BACKEND::winmm},
135 {"alsa", LABELS_MEDIA_AUDIO_BACKEND::alsa},
136 {"jack", LABELS_MEDIA_AUDIO_BACKEND::jack},
137 {"oss", LABELS_MEDIA_AUDIO_BACKEND::oss},
138 {"pulse", LABELS_MEDIA_AUDIO_BACKEND::pulse},
139 {"pulse-rust", LABELS_MEDIA_AUDIO_BACKEND::pulse_rust},
140 {"sndio", LABELS_MEDIA_AUDIO_BACKEND::sndio},
141 {"sun", LABELS_MEDIA_AUDIO_BACKEND::sunaudio},
142 };
143
144 // Prefered samplerate, in Hz (characteristic of the hardware, mixer, platform,
145 // and API used).
146 //
147 // sMutex protects *initialization* of this, which must be performed from each
148 // thread before fetching, after which it is safe to fetch without holding the
149 // mutex because it is only written once per process execution (by the first
150 // initialization to complete). Since the init must have been called on a
151 // given thread before fetching the value, it's guaranteed (via the mutex) that
152 // sufficient memory barriers have occurred to ensure the correct value is
153 // visible on the querying thread/CPU.
154 uint32_t sPreferredSampleRate;
155
156 #ifdef MOZ_CUBEB_REMOTING
157 // AudioIPC server handle
158 void* sServerHandle = nullptr;
159
160 // Initialized during early startup, protected by sMutex.
161 StaticAutoPtr<ipc::FileDescriptor> sIPCConnection;
162
StartAudioIPCServer()163 static bool StartAudioIPCServer() {
164 sServerHandle =
165 audioipc::audioipc_server_start(sBrandName, sCubebBackendName);
166 return sServerHandle != nullptr;
167 }
168
ShutdownAudioIPCServer()169 static void ShutdownAudioIPCServer() {
170 if (!sServerHandle) {
171 return;
172 }
173
174 audioipc::audioipc_server_stop(sServerHandle);
175 sServerHandle = nullptr;
176 }
177 #endif // MOZ_CUBEB_REMOTING
178 } // namespace
179
180 static const uint32_t CUBEB_NORMAL_LATENCY_MS = 100;
181 // Consevative default that can work on all platforms.
182 static const uint32_t CUBEB_NORMAL_LATENCY_FRAMES = 1024;
183
184 namespace CubebUtils {
185 cubeb* GetCubebContextUnlocked();
186
GetPrefAndSetString(const char * aPref,StaticAutoPtr<char> & aStorage)187 void GetPrefAndSetString(const char* aPref, StaticAutoPtr<char>& aStorage) {
188 nsAutoCString value;
189 Preferences::GetCString(aPref, value);
190 if (value.IsEmpty()) {
191 aStorage = nullptr;
192 } else {
193 aStorage = new char[value.Length() + 1];
194 PodCopy(aStorage.get(), value.get(), value.Length());
195 aStorage[value.Length()] = 0;
196 }
197 }
198
PrefChanged(const char * aPref,void * aClosure)199 void PrefChanged(const char* aPref, void* aClosure) {
200 if (strcmp(aPref, PREF_VOLUME_SCALE) == 0) {
201 nsAutoCString value;
202 Preferences::GetCString(aPref, value);
203 StaticMutexAutoLock lock(sMutex);
204 if (value.IsEmpty()) {
205 sVolumeScale = 1.0;
206 } else {
207 sVolumeScale = std::max<double>(0, PR_strtod(value.get(), nullptr));
208 }
209 } else if (strcmp(aPref, PREF_CUBEB_LATENCY_PLAYBACK) == 0) {
210 StaticMutexAutoLock lock(sMutex);
211 // Arbitrary default stream latency of 100ms. The higher this
212 // value, the longer stream volume changes will take to become
213 // audible.
214 sCubebPlaybackLatencyPrefSet = Preferences::HasUserValue(aPref);
215 uint32_t value = Preferences::GetUint(aPref, CUBEB_NORMAL_LATENCY_MS);
216 sCubebPlaybackLatencyInMilliseconds =
217 std::min<uint32_t>(std::max<uint32_t>(value, 1), 1000);
218 } else if (strcmp(aPref, PREF_CUBEB_LATENCY_MTG) == 0) {
219 StaticMutexAutoLock lock(sMutex);
220 sCubebMTGLatencyPrefSet = Preferences::HasUserValue(aPref);
221 uint32_t value = Preferences::GetUint(aPref, CUBEB_NORMAL_LATENCY_FRAMES);
222 // 128 is the block size for the Web Audio API, which limits how low the
223 // latency can be here.
224 // We don't want to limit the upper limit too much, so that people can
225 // experiment.
226 sCubebMTGLatencyInFrames =
227 std::min<uint32_t>(std::max<uint32_t>(value, 128), 1e6);
228 } else if (strcmp(aPref, PREF_CUBEB_FORCE_SAMPLE_RATE) == 0) {
229 StaticMutexAutoLock lock(sMutex);
230 sCubebForcedSampleRate = Preferences::GetUint(aPref);
231 } else if (strcmp(aPref, PREF_CUBEB_LOGGING_LEVEL) == 0) {
232 nsAutoCString value;
233 Preferences::GetCString(aPref, value);
234 LogModule* cubebLog = LogModule::Get("cubeb");
235 if (value.EqualsLiteral("verbose")) {
236 cubeb_set_log_callback(CUBEB_LOG_VERBOSE, CubebLogCallback);
237 cubebLog->SetLevel(LogLevel::Verbose);
238 } else if (value.EqualsLiteral("normal")) {
239 cubeb_set_log_callback(CUBEB_LOG_NORMAL, CubebLogCallback);
240 cubebLog->SetLevel(LogLevel::Error);
241 } else if (value.IsEmpty()) {
242 cubeb_set_log_callback(CUBEB_LOG_DISABLED, nullptr);
243 cubebLog->SetLevel(LogLevel::Disabled);
244 }
245 } else if (strcmp(aPref, PREF_CUBEB_BACKEND) == 0) {
246 StaticMutexAutoLock lock(sMutex);
247 GetPrefAndSetString(aPref, sCubebBackendName);
248 } else if (strcmp(aPref, PREF_CUBEB_OUTPUT_DEVICE) == 0) {
249 StaticMutexAutoLock lock(sMutex);
250 GetPrefAndSetString(aPref, sCubebOutputDeviceName);
251 } else if (strcmp(aPref, PREF_CUBEB_FORCE_NULL_CONTEXT) == 0) {
252 StaticMutexAutoLock lock(sMutex);
253 sCubebForceNullContext = Preferences::GetBool(aPref, false);
254 MOZ_LOG(gCubebLog, LogLevel::Verbose,
255 ("%s: %s", PREF_CUBEB_FORCE_NULL_CONTEXT,
256 sCubebForceNullContext ? "true" : "false"));
257 }
258 #ifdef MOZ_CUBEB_REMOTING
259 else if (strcmp(aPref, PREF_CUBEB_SANDBOX) == 0) {
260 StaticMutexAutoLock lock(sMutex);
261 sCubebSandbox = Preferences::GetBool(aPref);
262 MOZ_LOG(gCubebLog, LogLevel::Verbose,
263 ("%s: %s", PREF_CUBEB_SANDBOX, sCubebSandbox ? "true" : "false"));
264 } else if (strcmp(aPref, PREF_AUDIOIPC_POOL_SIZE) == 0) {
265 StaticMutexAutoLock lock(sMutex);
266 sAudioIPCPoolSize = Preferences::GetUint(PREF_AUDIOIPC_POOL_SIZE,
267 AUDIOIPC_POOL_SIZE_DEFAULT);
268 } else if (strcmp(aPref, PREF_AUDIOIPC_STACK_SIZE) == 0) {
269 StaticMutexAutoLock lock(sMutex);
270 sAudioIPCStackSize = Preferences::GetUint(PREF_AUDIOIPC_STACK_SIZE,
271 AUDIOIPC_STACK_SIZE_DEFAULT);
272 }
273 #endif
274 else if (strcmp(aPref, PREF_CUBEB_OUTPUT_VOICE_ROUTING) == 0) {
275 StaticMutexAutoLock lock(sMutex);
276 sRouteOutputAsVoice = Preferences::GetBool(aPref);
277 MOZ_LOG(gCubebLog, LogLevel::Verbose,
278 ("%s: %s", PREF_CUBEB_OUTPUT_VOICE_ROUTING,
279 sRouteOutputAsVoice ? "true" : "false"));
280 }
281 }
282
GetFirstStream()283 bool GetFirstStream() {
284 static bool sFirstStream = true;
285
286 StaticMutexAutoLock lock(sMutex);
287 bool result = sFirstStream;
288 sFirstStream = false;
289 return result;
290 }
291
GetVolumeScale()292 double GetVolumeScale() {
293 StaticMutexAutoLock lock(sMutex);
294 return sVolumeScale;
295 }
296
GetCubebContext()297 cubeb* GetCubebContext() {
298 StaticMutexAutoLock lock(sMutex);
299 return GetCubebContextUnlocked();
300 }
301
302 // This is only exported when running tests.
ForceSetCubebContext(cubeb * aCubebContext)303 void ForceSetCubebContext(cubeb* aCubebContext) {
304 StaticMutexAutoLock lock(sMutex);
305 sCubebContext = aCubebContext;
306 sCubebState = CubebState::Initialized;
307 }
308
SetInCommunication(bool aInCommunication)309 void SetInCommunication(bool aInCommunication) {
310 #ifdef MOZ_WIDGET_ANDROID
311 StaticMutexAutoLock lock(sMutex);
312 if (aInCommunication) {
313 sInCommunicationCount++;
314 } else {
315 MOZ_ASSERT(sInCommunicationCount > 0);
316 sInCommunicationCount--;
317 }
318
319 if (sInCommunicationCount == 1) {
320 java::GeckoAppShell::SetCommunicationAudioModeOn(true);
321 } else if (sInCommunicationCount == 0) {
322 java::GeckoAppShell::SetCommunicationAudioModeOn(false);
323 }
324 #endif
325 }
326
InitPreferredSampleRate()327 bool InitPreferredSampleRate() {
328 StaticMutexAutoLock lock(sMutex);
329 if (sPreferredSampleRate != 0) {
330 return true;
331 }
332 #ifdef MOZ_WIDGET_ANDROID
333 sPreferredSampleRate = AndroidGetAudioOutputSampleRate();
334 #else
335 cubeb* context = GetCubebContextUnlocked();
336 if (!context) {
337 return false;
338 }
339 if (cubeb_get_preferred_sample_rate(context, &sPreferredSampleRate) !=
340 CUBEB_OK) {
341 return false;
342 }
343 #endif
344 MOZ_ASSERT(sPreferredSampleRate);
345 return true;
346 }
347
PreferredSampleRate()348 uint32_t PreferredSampleRate() {
349 if (sCubebForcedSampleRate) {
350 return sCubebForcedSampleRate;
351 }
352 if (StaticPrefs::privacy_resistFingerprinting()) {
353 return 44100;
354 }
355 if (!InitPreferredSampleRate()) {
356 return 44100;
357 }
358 MOZ_ASSERT(sPreferredSampleRate);
359 return sPreferredSampleRate;
360 }
361
InitBrandName()362 void InitBrandName() {
363 if (sBrandName) {
364 return;
365 }
366 nsAutoString brandName;
367 nsCOMPtr<nsIStringBundleService> stringBundleService =
368 mozilla::components::StringBundle::Service();
369 if (stringBundleService) {
370 nsCOMPtr<nsIStringBundle> brandBundle;
371 nsresult rv = stringBundleService->CreateBundle(
372 kBrandBundleURL, getter_AddRefs(brandBundle));
373 if (NS_SUCCEEDED(rv)) {
374 rv = brandBundle->GetStringFromName("brandShortName", brandName);
375 NS_WARNING_ASSERTION(
376 NS_SUCCEEDED(rv),
377 "Could not get the program name for a cubeb stream.");
378 }
379 }
380 NS_LossyConvertUTF16toASCII ascii(brandName);
381 sBrandName = new char[ascii.Length() + 1];
382 PodCopy(sBrandName.get(), ascii.get(), ascii.Length());
383 sBrandName[ascii.Length()] = 0;
384 }
385
386 #ifdef MOZ_CUBEB_REMOTING
InitAudioIPCConnection()387 void InitAudioIPCConnection() {
388 MOZ_ASSERT(NS_IsMainThread());
389 auto contentChild = dom::ContentChild::GetSingleton();
390 auto promise = contentChild->SendCreateAudioIPCConnection();
391 promise->Then(
392 AbstractThread::MainThread(), __func__,
393 [](dom::FileDescOrError&& aFD) {
394 StaticMutexAutoLock lock(sMutex);
395 MOZ_ASSERT(!sIPCConnection);
396 if (aFD.type() == dom::FileDescOrError::Type::TFileDescriptor) {
397 sIPCConnection = new ipc::FileDescriptor(std::move(aFD));
398 } else {
399 MOZ_LOG(gCubebLog, LogLevel::Error,
400 ("SendCreateAudioIPCConnection failed: invalid FD"));
401 }
402 },
403 [](mozilla::ipc::ResponseRejectReason&& aReason) {
404 MOZ_LOG(gCubebLog, LogLevel::Error,
405 ("SendCreateAudioIPCConnection rejected: %d", int(aReason)));
406 });
407 }
408 #endif
409
CreateAudioIPCConnection()410 ipc::FileDescriptor CreateAudioIPCConnection() {
411 #ifdef MOZ_CUBEB_REMOTING
412 MOZ_ASSERT(sCubebSandbox && XRE_IsParentProcess());
413 if (!sServerHandle) {
414 MOZ_LOG(gCubebLog, LogLevel::Debug, ("Starting cubeb server..."));
415 if (!StartAudioIPCServer()) {
416 MOZ_LOG(gCubebLog, LogLevel::Error, ("audioipc_server_start failed"));
417 return ipc::FileDescriptor();
418 }
419 }
420 MOZ_ASSERT(sServerHandle);
421 ipc::FileDescriptor::PlatformHandleType rawFD =
422 audioipc::audioipc_server_new_client(sServerHandle);
423 ipc::FileDescriptor fd(rawFD);
424 if (!fd.IsValid()) {
425 MOZ_LOG(gCubebLog, LogLevel::Error, ("audioipc_server_new_client failed"));
426 return ipc::FileDescriptor();
427 }
428 // Close rawFD since FileDescriptor's ctor cloned it.
429 // TODO: Find cleaner cross-platform way to close rawFD.
430 # ifdef XP_WIN
431 CloseHandle(rawFD);
432 # else
433 close(rawFD);
434 # endif
435 return fd;
436 #else
437 return ipc::FileDescriptor();
438 #endif
439 }
440
GetCubebContextUnlocked()441 cubeb* GetCubebContextUnlocked() {
442 sMutex.AssertCurrentThreadOwns();
443 if (sCubebForceNullContext) {
444 // Pref set such that we should return a null context
445 MOZ_LOG(gCubebLog, LogLevel::Debug,
446 ("%s: returning null context due to %s!", __func__,
447 PREF_CUBEB_FORCE_NULL_CONTEXT));
448 return nullptr;
449 }
450 if (sCubebState != CubebState::Uninitialized) {
451 // If we have already passed the initialization point (below), just return
452 // the current context, which may be null (e.g., after error or shutdown.)
453 return sCubebContext;
454 }
455
456 if (!sBrandName && NS_IsMainThread()) {
457 InitBrandName();
458 } else {
459 NS_WARNING_ASSERTION(
460 sBrandName,
461 "Did not initialize sbrandName, and not on the main thread?");
462 }
463
464 int rv = CUBEB_ERROR;
465 #ifdef MOZ_CUBEB_REMOTING
466 MOZ_LOG(gCubebLog, LogLevel::Info,
467 ("%s: %s", PREF_CUBEB_SANDBOX, sCubebSandbox ? "true" : "false"));
468
469 if (sCubebSandbox) {
470 if (XRE_IsParentProcess() && !sIPCConnection) {
471 // TODO: Don't use audio IPC when within the same process.
472 auto fd = CreateAudioIPCConnection();
473 if (fd.IsValid()) {
474 sIPCConnection = new ipc::FileDescriptor(fd);
475 }
476 }
477 if (NS_WARN_IF(!sIPCConnection)) {
478 // Either the IPC connection failed to init or we're still waiting for
479 // InitAudioIPCConnection to complete (bug 1454782).
480 return nullptr;
481 }
482
483 audioipc::AudioIpcInitParams initParams;
484 initParams.mPoolSize = sAudioIPCPoolSize;
485 initParams.mStackSize = sAudioIPCStackSize;
486 initParams.mServerConnection =
487 sIPCConnection->ClonePlatformHandle().release();
488 initParams.mThreadCreateCallback = [](const char* aName) {
489 PROFILER_REGISTER_THREAD(aName);
490 };
491 initParams.mThreadDestroyCallback = []() { PROFILER_UNREGISTER_THREAD(); };
492
493 MOZ_LOG(gCubebLog, LogLevel::Debug,
494 ("%s: %d", PREF_AUDIOIPC_POOL_SIZE, (int)initParams.mPoolSize));
495 MOZ_LOG(gCubebLog, LogLevel::Debug,
496 ("%s: %d", PREF_AUDIOIPC_STACK_SIZE, (int)initParams.mStackSize));
497
498 rv =
499 audioipc::audioipc_client_init(&sCubebContext, sBrandName, &initParams);
500 } else {
501 #endif // MOZ_CUBEB_REMOTING
502 #ifdef XP_WIN
503 mozilla::mscom::EnsureMTA([&]() -> void {
504 #endif
505 rv = cubeb_init(&sCubebContext, sBrandName, sCubebBackendName);
506 #ifdef XP_WIN
507 });
508 #endif
509 #ifdef MOZ_CUBEB_REMOTING
510 }
511 sIPCConnection = nullptr;
512 #endif // MOZ_CUBEB_REMOTING
513 NS_WARNING_ASSERTION(rv == CUBEB_OK, "Could not get a cubeb context.");
514 sCubebState =
515 (rv == CUBEB_OK) ? CubebState::Initialized : CubebState::Uninitialized;
516
517 return sCubebContext;
518 }
519
ReportCubebBackendUsed()520 void ReportCubebBackendUsed() {
521 StaticMutexAutoLock lock(sMutex);
522
523 sAudioStreamInitEverSucceeded = true;
524
525 LABELS_MEDIA_AUDIO_BACKEND label = LABELS_MEDIA_AUDIO_BACKEND::unknown;
526 auto backend =
527 kTelemetryBackendLabel.find(cubeb_get_backend_id(sCubebContext));
528 if (backend != kTelemetryBackendLabel.end()) {
529 label = backend->second;
530 }
531 AccumulateCategorical(label);
532 }
533
ReportCubebStreamInitFailure(bool aIsFirst)534 void ReportCubebStreamInitFailure(bool aIsFirst) {
535 StaticMutexAutoLock lock(sMutex);
536 if (!aIsFirst && !sAudioStreamInitEverSucceeded) {
537 // This machine has no audio hardware, or it's in really bad shape, don't
538 // send this info, since we want CUBEB_BACKEND_INIT_FAILURE_OTHER to detect
539 // failures to open multiple streams in a process over time.
540 return;
541 }
542 AccumulateCategorical(aIsFirst ? LABELS_MEDIA_AUDIO_INIT_FAILURE::first
543 : LABELS_MEDIA_AUDIO_INIT_FAILURE::other);
544 }
545
GetCubebPlaybackLatencyInMilliseconds()546 uint32_t GetCubebPlaybackLatencyInMilliseconds() {
547 StaticMutexAutoLock lock(sMutex);
548 return sCubebPlaybackLatencyInMilliseconds;
549 }
550
CubebPlaybackLatencyPrefSet()551 bool CubebPlaybackLatencyPrefSet() {
552 StaticMutexAutoLock lock(sMutex);
553 return sCubebPlaybackLatencyPrefSet;
554 }
555
CubebMTGLatencyPrefSet()556 bool CubebMTGLatencyPrefSet() {
557 StaticMutexAutoLock lock(sMutex);
558 return sCubebMTGLatencyPrefSet;
559 }
560
GetCubebMTGLatencyInFrames(cubeb_stream_params * params)561 uint32_t GetCubebMTGLatencyInFrames(cubeb_stream_params* params) {
562 StaticMutexAutoLock lock(sMutex);
563 if (sCubebMTGLatencyPrefSet) {
564 MOZ_ASSERT(sCubebMTGLatencyInFrames > 0);
565 return sCubebMTGLatencyInFrames;
566 }
567
568 #ifdef MOZ_WIDGET_ANDROID
569 return AndroidGetAudioOutputFramesPerBuffer();
570 #else
571 cubeb* context = GetCubebContextUnlocked();
572 if (!context) {
573 return sCubebMTGLatencyInFrames; // default 512
574 }
575 uint32_t latency_frames = 0;
576 if (cubeb_get_min_latency(context, params, &latency_frames) != CUBEB_OK) {
577 NS_WARNING("Could not get minimal latency from cubeb.");
578 return sCubebMTGLatencyInFrames; // default 512
579 }
580 return latency_frames;
581 #endif
582 }
583
584 static const char* gInitCallbackPrefs[] = {
585 PREF_VOLUME_SCALE, PREF_CUBEB_OUTPUT_DEVICE,
586 PREF_CUBEB_LATENCY_PLAYBACK, PREF_CUBEB_LATENCY_MTG,
587 PREF_CUBEB_BACKEND, PREF_CUBEB_FORCE_NULL_CONTEXT,
588 PREF_CUBEB_SANDBOX, PREF_AUDIOIPC_POOL_SIZE,
589 PREF_AUDIOIPC_STACK_SIZE, nullptr,
590 };
591 static const char* gCallbackPrefs[] = {
592 PREF_CUBEB_FORCE_SAMPLE_RATE,
593 // We don't want to call the callback on startup, because the pref is the
594 // empty string by default ("", which means "logging disabled"). Because the
595 // logging can be enabled via environment variables (MOZ_LOG="module:5"),
596 // calling this callback on init would immediately re-disable the logging.
597 PREF_CUBEB_LOGGING_LEVEL,
598 nullptr,
599 };
600
InitLibrary()601 void InitLibrary() {
602 Preferences::RegisterCallbacksAndCall(PrefChanged, gInitCallbackPrefs);
603 Preferences::RegisterCallbacks(PrefChanged, gCallbackPrefs);
604
605 if (MOZ_LOG_TEST(gCubebLog, LogLevel::Verbose)) {
606 cubeb_set_log_callback(CUBEB_LOG_VERBOSE, CubebLogCallback);
607 } else if (MOZ_LOG_TEST(gCubebLog, LogLevel::Error)) {
608 cubeb_set_log_callback(CUBEB_LOG_NORMAL, CubebLogCallback);
609 }
610
611 #ifndef MOZ_WIDGET_ANDROID
612 NS_DispatchToMainThread(
613 NS_NewRunnableFunction("CubebUtils::InitLibrary", &InitBrandName));
614 #endif
615 #ifdef MOZ_CUBEB_REMOTING
616 if (sCubebSandbox && XRE_IsContentProcess()) {
617 # ifdef XP_LINUX
618 if (atp_set_real_time_limit(0, 48000)) {
619 NS_WARNING("could not set real-time limit in CubebUtils::InitLibrary");
620 }
621 InstallSoftRealTimeLimitHandler();
622 # endif
623 InitAudioIPCConnection();
624 }
625 #endif
626
627 sAudioThreadRegistry = new AudioThreadRegistry;
628 }
629
ShutdownLibrary()630 void ShutdownLibrary() {
631 Preferences::UnregisterCallbacks(PrefChanged, gInitCallbackPrefs);
632 Preferences::UnregisterCallbacks(PrefChanged, gCallbackPrefs);
633
634 StaticMutexAutoLock lock(sMutex);
635 if (sCubebContext) {
636 cubeb_destroy(sCubebContext);
637 sCubebContext = nullptr;
638 }
639 sBrandName = nullptr;
640 sCubebBackendName = nullptr;
641 // This will ensure we don't try to re-create a context.
642 sCubebState = CubebState::Shutdown;
643
644 #ifdef MOZ_CUBEB_REMOTING
645 sIPCConnection = nullptr;
646 ShutdownAudioIPCServer();
647 #endif
648 sAudioThreadRegistry = nullptr;
649 }
650
SandboxEnabled()651 bool SandboxEnabled() {
652 #ifdef MOZ_CUBEB_REMOTING
653 StaticMutexAutoLock lock(sMutex);
654 return !!sCubebSandbox;
655 #else
656 return false;
657 #endif
658 }
659
GetAudioThreadRegistry()660 AudioThreadRegistry* GetAudioThreadRegistry() { return sAudioThreadRegistry; }
661
MaxNumberOfChannels()662 uint32_t MaxNumberOfChannels() {
663 cubeb* cubebContext = GetCubebContext();
664 uint32_t maxNumberOfChannels;
665 if (cubebContext && cubeb_get_max_channel_count(
666 cubebContext, &maxNumberOfChannels) == CUBEB_OK) {
667 return maxNumberOfChannels;
668 }
669
670 return 0;
671 }
672
GetCurrentBackend(nsAString & aBackend)673 void GetCurrentBackend(nsAString& aBackend) {
674 cubeb* cubebContext = GetCubebContext();
675 if (cubebContext) {
676 const char* backend = cubeb_get_backend_id(cubebContext);
677 if (backend) {
678 aBackend.AssignASCII(backend);
679 return;
680 }
681 }
682 aBackend.AssignLiteral("unknown");
683 }
684
GetForcedOutputDevice()685 char* GetForcedOutputDevice() {
686 StaticMutexAutoLock lock(sMutex);
687 return sCubebOutputDeviceName;
688 }
689
GetDefaultStreamPrefs(cubeb_device_type aType)690 cubeb_stream_prefs GetDefaultStreamPrefs(cubeb_device_type aType) {
691 cubeb_stream_prefs prefs = CUBEB_STREAM_PREF_NONE;
692 #ifdef XP_WIN
693 if (StaticPrefs::media_cubeb_wasapi_raw() & static_cast<uint32_t>(aType)) {
694 prefs |= CUBEB_STREAM_PREF_RAW;
695 }
696 #endif
697 return prefs;
698 }
699
RouteOutputAsVoice()700 bool RouteOutputAsVoice() { return sRouteOutputAsVoice; }
701
datacb(cubeb_stream *,void *,const void *,void * out_buffer,long nframes)702 long datacb(cubeb_stream*, void*, const void*, void* out_buffer, long nframes) {
703 PodZero(static_cast<float*>(out_buffer), nframes * 2);
704 return nframes;
705 }
706
statecb(cubeb_stream *,void *,cubeb_state)707 void statecb(cubeb_stream*, void*, cubeb_state) {}
708
EstimatedRoundTripLatencyDefaultDevices(double * aMean,double * aStdDev)709 bool EstimatedRoundTripLatencyDefaultDevices(double* aMean, double* aStdDev) {
710 nsTArray<double> roundtripLatencies;
711 // Create a cubeb stream with the correct latency and default input/output
712 // devices (mono/stereo channels). Wait for two seconds, get the latency a few
713 // times.
714 int rv;
715 uint32_t rate;
716 uint32_t latencyFrames;
717 rv = cubeb_get_preferred_sample_rate(GetCubebContext(), &rate);
718 if (rv != CUBEB_OK) {
719 MOZ_LOG(gCubebLog, LogLevel::Error, ("Could not get preferred rate"));
720 return false;
721 }
722
723 cubeb_stream_params output_params;
724 output_params.format = CUBEB_SAMPLE_FLOAT32NE;
725 output_params.rate = rate;
726 output_params.channels = 2;
727 output_params.layout = CUBEB_LAYOUT_UNDEFINED;
728 output_params.prefs = GetDefaultStreamPrefs(CUBEB_DEVICE_TYPE_OUTPUT);
729
730 latencyFrames = GetCubebMTGLatencyInFrames(&output_params);
731
732 cubeb_stream_params input_params;
733 input_params.format = CUBEB_SAMPLE_FLOAT32NE;
734 input_params.rate = rate;
735 input_params.channels = 1;
736 input_params.layout = CUBEB_LAYOUT_UNDEFINED;
737 input_params.prefs = GetDefaultStreamPrefs(CUBEB_DEVICE_TYPE_INPUT);
738
739 cubeb_stream* stm;
740 rv = cubeb_stream_init(GetCubebContext(), &stm,
741 "about:support latency estimation", NULL,
742 &input_params, NULL, &output_params, latencyFrames,
743 datacb, statecb, NULL);
744 if (rv != CUBEB_OK) {
745 MOZ_LOG(gCubebLog, LogLevel::Error, ("Could not get init stream"));
746 return false;
747 }
748
749 rv = cubeb_stream_start(stm);
750 if (rv != CUBEB_OK) {
751 MOZ_LOG(gCubebLog, LogLevel::Error, ("Could not start stream"));
752 return false;
753 }
754 // +-2s
755 for (uint32_t i = 0; i < 40; i++) {
756 std::this_thread::sleep_for(std::chrono::milliseconds(50));
757 uint32_t inputLatency, outputLatency, rvIn, rvOut;
758 rvOut = cubeb_stream_get_latency(stm, &outputLatency);
759 if (rvOut) {
760 MOZ_LOG(gCubebLog, LogLevel::Error, ("Could not get output latency"));
761 }
762 rvIn = cubeb_stream_get_input_latency(stm, &inputLatency);
763 if (rvIn) {
764 MOZ_LOG(gCubebLog, LogLevel::Error, ("Could not get input latency"));
765 }
766 if (rvIn != CUBEB_OK || rvOut != CUBEB_OK) {
767 continue;
768 }
769
770 double roundTrip = static_cast<double>(outputLatency + inputLatency) / rate;
771 roundtripLatencies.AppendElement(roundTrip);
772 }
773 rv = cubeb_stream_stop(stm);
774 if (rv != CUBEB_OK) {
775 MOZ_LOG(gCubebLog, LogLevel::Error, ("Could not stop the stream"));
776 }
777
778 *aMean = 0.0;
779 *aStdDev = 0.0;
780 double variance = 0.0;
781 for (uint32_t i = 0; i < roundtripLatencies.Length(); i++) {
782 *aMean += roundtripLatencies[i];
783 }
784
785 *aMean /= roundtripLatencies.Length();
786
787 for (uint32_t i = 0; i < roundtripLatencies.Length(); i++) {
788 variance += pow(roundtripLatencies[i] - *aMean, 2.);
789 }
790 variance /= roundtripLatencies.Length();
791
792 *aStdDev = sqrt(variance);
793
794 MOZ_LOG(gCubebLog, LogLevel::Debug,
795 ("Default device roundtrip latency in seconds %lf (stddev: %lf)",
796 *aMean, *aStdDev));
797
798 cubeb_stream_destroy(stm);
799
800 return true;
801 }
802
803 #ifdef MOZ_WIDGET_ANDROID
AndroidGetAudioOutputSampleRate()804 uint32_t AndroidGetAudioOutputSampleRate() {
805 int32_t sample_rate = java::GeckoAppShell::GetAudioOutputSampleRate();
806 MOZ_ASSERT(sample_rate > 0);
807 return sample_rate;
808 }
AndroidGetAudioOutputFramesPerBuffer()809 uint32_t AndroidGetAudioOutputFramesPerBuffer() {
810 int32_t frames = java::GeckoAppShell::GetAudioOutputFramesPerBuffer();
811 MOZ_ASSERT(frames > 0);
812 return frames;
813 }
814 #endif
815
816 } // namespace CubebUtils
817 } // namespace mozilla
818