Bug 1539225 - Pass cubeb backend name to AudioIPC server during init. r=chunmin
authorMatthew Gregan <kinetik@flim.org>
Tue, 23 Jul 2019 19:41:37 +0000
changeset 483912 e3dcc1a1bfcbe9ed11923096a2d5e12cc68f2ccb
parent 483911 e7d500f650ccfe119d46a36392fa641b47c142a2
child 483913 d9eed0a390132dc20e2d0aae58de5eb9ca4110e8
push id90693
push usermgregan@mozilla.com
push dateTue, 23 Jul 2019 21:21:02 +0000
treeherderautoland@790684b8a27c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerschunmin
bugs1539225
milestone70.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1539225 - Pass cubeb backend name to AudioIPC server during init. r=chunmin Differential Revision: https://phabricator.services.mozilla.com/D37833
dom/media/CubebUtils.cpp
--- a/dom/media/CubebUtils.cpp
+++ b/dom/media/CubebUtils.cpp
@@ -69,55 +69,32 @@ extern "C" {
 struct AudioIpcInitParams {
   mozilla::ipc::FileDescriptor::PlatformHandleType mServerConnection;
   size_t mPoolSize;
   size_t mStackSize;
   void (*mThreadCreateCallback)(const char*);
 };
 
 // These functions are provided by audioipc-server crate
-extern void* audioipc_server_start();
+extern void* audioipc_server_start(const char*, const char*);
 extern mozilla::ipc::FileDescriptor::PlatformHandleType
 audioipc_server_new_client(void*);
 extern void audioipc_server_stop(void*);
 // These functions are provided by audioipc-client crate
 extern int audioipc_client_init(cubeb**, const char*,
                                 const AudioIpcInitParams*);
 #ifdef XP_LINUX
 extern void audioipc_init_threads(const AudioIpcInitParams*);
 #endif
 }
 
 namespace mozilla {
 
 namespace {
 
-#ifdef MOZ_CUBEB_REMOTING
-////////////////////////////////////////////////////////////////////////////////
-// Cubeb Sound Server Thread
-void* sServerHandle = nullptr;
-
-// Initialized during early startup, protected by sMutex.
-StaticAutoPtr<ipc::FileDescriptor> sIPCConnection;
-
-static bool StartSoundServer() {
-  sServerHandle = audioipc_server_start();
-  return sServerHandle != nullptr;
-}
-
-static void ShutdownSoundServer() {
-  if (!sServerHandle) return;
-
-  audioipc_server_stop(sServerHandle);
-  sServerHandle = nullptr;
-}
-#endif  // MOZ_CUBEB_REMOTING
-
-////////////////////////////////////////////////////////////////////////////////
-
 LazyLogModule gCubebLog("cubeb");
 
 void CubebLogCallback(const char* aFmt, ...) {
   char buffer[256];
 
   va_list arglist;
   va_start(arglist, aFmt);
   VsprintfLiteral(buffer, aFmt, arglist);
@@ -183,16 +160,37 @@ const int CUBEB_BACKEND_UNKNOWN = CUBEB_
 // thread before fetching, after which it is safe to fetch without holding the
 // mutex because it is only written once per process execution (by the first
 // initialization to complete).  Since the init must have been called on a
 // given thread before fetching the value, it's guaranteed (via the mutex) that
 // sufficient memory barriers have occurred to ensure the correct value is
 // visible on the querying thread/CPU.
 uint32_t sPreferredSampleRate;
 
+#ifdef MOZ_CUBEB_REMOTING
+// AudioIPC server handle
+void* sServerHandle = nullptr;
+
+// Initialized during early startup, protected by sMutex.
+StaticAutoPtr<ipc::FileDescriptor> sIPCConnection;
+
+static bool StartAudioIPCServer() {
+  sServerHandle = audioipc_server_start(sBrandName, sCubebBackendName);
+  return sServerHandle != nullptr;
+}
+
+static void ShutdownAudioIPCServer() {
+  if (!sServerHandle) {
+    return;
+  }
+
+  audioipc_server_stop(sServerHandle);
+  sServerHandle = nullptr;
+}
+#endif  // MOZ_CUBEB_REMOTING
 }  // namespace
 
 static const uint32_t CUBEB_NORMAL_LATENCY_MS = 100;
 // Consevative default that can work on all platforms.
 static const uint32_t CUBEB_NORMAL_LATENCY_FRAMES = 1024;
 
 namespace CubebUtils {
 cubeb* GetCubebContextUnlocked();
@@ -275,21 +273,16 @@ void PrefChanged(const char* aPref, void
              sCubebDisableDeviceSwitching ? "true" : "false"));
   }
 #ifdef MOZ_CUBEB_REMOTING
   else if (strcmp(aPref, PREF_CUBEB_SANDBOX) == 0) {
     StaticMutexAutoLock lock(sMutex);
     sCubebSandbox = Preferences::GetBool(aPref);
     MOZ_LOG(gCubebLog, LogLevel::Verbose,
             ("%s: %s", PREF_CUBEB_SANDBOX, sCubebSandbox ? "true" : "false"));
-
-    if (sCubebSandbox && !sServerHandle && XRE_IsParentProcess()) {
-      MOZ_LOG(gCubebLog, LogLevel::Debug, ("Starting cubeb server..."));
-      StartSoundServer();
-    }
   } else if (strcmp(aPref, PREF_AUDIOIPC_POOL_SIZE) == 0) {
     StaticMutexAutoLock lock(sMutex);
     sAudioIPCPoolSize = Preferences::GetUint(PREF_AUDIOIPC_POOL_SIZE,
                                              AUDIOIPC_POOL_SIZE_DEFAULT);
   } else if (strcmp(aPref, PREF_AUDIOIPC_STACK_SIZE) == 0) {
     StaticMutexAutoLock lock(sMutex);
     sAudioIPCStackSize = Preferences::GetUint(PREF_AUDIOIPC_STACK_SIZE,
                                               AUDIOIPC_STACK_SIZE_DEFAULT);
@@ -418,16 +411,24 @@ void InitAudioIPCConnection() {
         MOZ_LOG(gCubebLog, LogLevel::Error,
                 ("SendCreateAudioIPCConnection rejected: %d", int(aReason)));
       });
 }
 #endif
 
 ipc::FileDescriptor CreateAudioIPCConnection() {
 #ifdef MOZ_CUBEB_REMOTING
+  MOZ_ASSERT(sCubebSandbox && XRE_IsParentProcess());
+  if (!sServerHandle) {
+    MOZ_LOG(gCubebLog, LogLevel::Debug, ("Starting cubeb server..."));
+    if (!StartAudioIPCServer()) {
+      MOZ_LOG(gCubebLog, LogLevel::Error, ("audioipc_server_start failed"));
+      return ipc::FileDescriptor();
+    }
+  }
   MOZ_ASSERT(sServerHandle);
   ipc::FileDescriptor::PlatformHandleType rawFD =
       audioipc_server_new_client(sServerHandle);
   ipc::FileDescriptor fd(rawFD);
   if (!fd.IsValid()) {
     MOZ_LOG(gCubebLog, LogLevel::Error, ("audioipc_server_new_client failed"));
     return ipc::FileDescriptor();
   }
@@ -517,17 +518,17 @@ cubeb* GetCubebContextUnlocked() {
             ("%s: %d", PREF_AUDIOIPC_STACK_SIZE, (int)initParams.mStackSize));
 
     rv = audioipc_client_init(&sCubebContext, sBrandName, &initParams);
   } else {
 #endif  // MOZ_CUBEB_REMOTING
 #ifdef XP_WIN
     mozilla::mscom::EnsureMTA([&]() -> void {
 #endif
-      rv = cubeb_init(&sCubebContext, sBrandName, sCubebBackendName.get());
+      rv = cubeb_init(&sCubebContext, sBrandName, sCubebBackendName);
 #ifdef XP_WIN
     });
 #endif
 #ifdef MOZ_CUBEB_REMOTING
   }
   sIPCConnection = nullptr;
 #endif  // MOZ_CUBEB_REMOTING
   NS_WARNING_ASSERTION(rv == CUBEB_OK, "Could not get a cubeb context.");
@@ -661,17 +662,17 @@ void ShutdownLibrary() {
   }
   sBrandName = nullptr;
   sCubebBackendName = nullptr;
   // This will ensure we don't try to re-create a context.
   sCubebState = CubebState::Shutdown;
 
 #ifdef MOZ_CUBEB_REMOTING
   sIPCConnection = nullptr;
-  ShutdownSoundServer();
+  ShutdownAudioIPCServer();
 #endif
 }
 
 uint32_t MaxNumberOfChannels() {
   cubeb* cubebContext = GetCubebContext();
   uint32_t maxNumberOfChannels;
   if (cubebContext && cubeb_get_max_channel_count(
                           cubebContext, &maxNumberOfChannels) == CUBEB_OK) {