Bug 1337805 - Avoid deadlock in AudioStream::DataCallback. r?padenot draft
authorAlex Chronopoulos <achronop@gmail.com>
Fri, 10 Feb 2017 16:14:36 +0200
changeset 481851 4b3fa42b248e8fd698fa9b4f9180669138857e06
parent 481686 449bde3195cd552147dff7a17cd759a437a8c4a0
child 545293 b68acf643b4a59ac7b0bbcf8f5b8fc81058069b1
push id44927
push userachronop@gmail.com
push dateFri, 10 Feb 2017 14:13:56 +0000
reviewerspadenot
bugs1337805
milestone54.0a1
Bug 1337805 - Avoid deadlock in AudioStream::DataCallback. r?padenot MozReview-Commit-ID: IPCjepQ4dKt
dom/media/CubebUtils.cpp
--- a/dom/media/CubebUtils.cpp
+++ b/dom/media/CubebUtils.cpp
@@ -218,27 +218,37 @@ uint32_t PreferredSampleRate()
     return 44100;
   }
   MOZ_ASSERT(sPreferredSampleRate);
   return sPreferredSampleRate;
 }
 
 bool InitPreferredChannelLayout()
 {
-  StaticMutexAutoLock lock(sMutex);
-  if (sPreferredChannelLayout != 0) {
-    return true;
+  {
+    StaticMutexAutoLock lock(sMutex);
+    if (sPreferredChannelLayout != 0) {
+      return true;
+    }
   }
-  cubeb* context = GetCubebContextUnlocked();
+
+  cubeb* context = GetCubebContext();
   if (!context) {
     return false;
   }
-  return cubeb_get_preferred_channel_layout(context,
-                                            &sPreferredChannelLayout) == CUBEB_OK
-         ? true : false;
+
+  // Favor calling cubeb api with the mutex unlock, potential deadlock.
+  cubeb_channel_layout layout;
+  if (cubeb_get_preferred_channel_layout(context, &layout) != CUBEB_OK) {
+    return false;
+  }
+
+  StaticMutexAutoLock lock(sMutex);
+  sPreferredChannelLayout = layout;
+  return true;
 }
 
 uint32_t PreferredChannelMap(uint32_t aChannels)
 {
   // The first element of the following mapping table is channel counts,
   // and the second one is its bit mask. It will be used in many times,
   // so we shoule avoid to allocate it in stack, or it will be created
   // and removed repeatedly. Use static to allocate this local variable