Bug 1300446 - Check return value from GetCubebContext - r?kinetik draft
authorGerald Squelart <gsquelart@mozilla.com>
Tue, 30 Aug 2016 17:20:10 -0700
changeset 409740 33c4cf75c32f78c4b2ce0baddb785cf30e4a0d00
parent 409739 13efd4025f11f0091ee30cb94295a2f421863191
child 530394 2f1f66726dbd23dab05afa3bb31f7cd3f9d75749
push id28527
push usergsquelart@mozilla.com
push dateMon, 05 Sep 2016 01:07:03 +0000
reviewerskinetik
bugs1300446
milestone51.0a1
Bug 1300446 - Check return value from GetCubebContext - r?kinetik MozReview-Commit-ID: Y9b5Aq2RZE
dom/media/CubebUtils.cpp
dom/media/GraphDriver.cpp
dom/media/webrtc/MediaEngineWebRTC.cpp
--- a/dom/media/CubebUtils.cpp
+++ b/dom/media/CubebUtils.cpp
@@ -308,18 +308,21 @@ cubeb_stream_type ConvertChannelToCubebT
       NS_ERROR("The value of AudioChannel is invalid");
       return CUBEB_STREAM_TYPE_MAX;
   }
 }
 #endif
 
 void GetCurrentBackend(nsAString& aBackend)
 {
-  const char* backend = cubeb_get_backend_id(GetCubebContext());
-  if (!backend) {
-    aBackend.AssignLiteral("unknown");
-    return;
+  cubeb* cubebContext = GetCubebContext();
+  if (cubebContext) {
+    const char* backend = cubeb_get_backend_id(cubebContext);
+    if (backend) {
+      aBackend.AssignASCII(backend);
+      return;
+    }
   }
-  aBackend.AssignASCII(backend);
+  aBackend.AssignLiteral("unknown");
 }
 
 } // namespace CubebUtils
 } // namespace mozilla
--- a/dom/media/GraphDriver.cpp
+++ b/dom/media/GraphDriver.cpp
@@ -583,16 +583,22 @@ AudioCallbackDriver::AudioCallbackDriver
 AudioCallbackDriver::~AudioCallbackDriver()
 {
   MOZ_ASSERT(mPromisesForOperation.IsEmpty());
 }
 
 void
 AudioCallbackDriver::Init()
 {
+  cubeb* cubebContext = CubebUtils::GetCubebContext();
+  if (!cubebContext) {
+    NS_WARNING("Could not get cubeb context.");
+    return;
+  }
+
   cubeb_stream_params output;
   cubeb_stream_params input;
   uint32_t latency_frames;
   bool firstStream = CubebUtils::GetFirstStream();
 
   MOZ_ASSERT(!NS_IsMainThread(),
       "This is blocking and should never run on the main thread.");
 
@@ -614,17 +620,17 @@ AudioCallbackDriver::Init()
 
   output.channels = mGraphImpl->AudioChannelCount();
   if (AUDIO_OUTPUT_FORMAT == AUDIO_FORMAT_S16) {
     output.format = CUBEB_SAMPLE_S16NE;
   } else {
     output.format = CUBEB_SAMPLE_FLOAT32NE;
   }
 
-  if (cubeb_get_min_latency(CubebUtils::GetCubebContext(), output, &latency_frames) != CUBEB_OK) {
+  if (cubeb_get_min_latency(cubebContext, output, &latency_frames) != CUBEB_OK) {
     NS_WARNING("Could not get minimal latency from cubeb.");
     return;
   }
 
   input = output;
   input.channels = mInputChannels; // change to support optional stereo capture
 
   cubeb_stream* stream = nullptr;
@@ -645,17 +651,17 @@ AudioCallbackDriver::Init()
          // XXX we should figure out how we would use a deviceID for output without webrtc.
          // Currently we don't set this though, so it's ok
          || AudioInputCubeb::GetDeviceID(mGraphImpl->mOutputDeviceID, output_id)
 #endif
          ) &&
         // XXX Only pass input input if we have an input listener.  Always
         // set up output because it's easier, and it will just get silence.
         // XXX Add support for adding/removing an input listener later.
-        cubeb_stream_init(CubebUtils::GetCubebContext(), &stream,
+        cubeb_stream_init(cubebContext, &stream,
                           "AudioCallbackDriver",
                           input_id,
                           mGraphImpl->mInputWanted ? &input : nullptr,
                           output_id,
                           mGraphImpl->mOutputWanted ? &output : nullptr, latency_frames,
                           DataCallback_s, StateCallback_s, this) == CUBEB_OK) {
       mAudioStream.own(stream);
       DebugOnly<int> rv = cubeb_stream_set_volume(mAudioStream, CubebUtils::GetVolumeScale());
--- a/dom/media/webrtc/MediaEngineWebRTC.cpp
+++ b/dom/media/webrtc/MediaEngineWebRTC.cpp
@@ -49,19 +49,24 @@ cubeb_device_collection* AudioInputCubeb
 bool AudioInputCubeb::mAnyInUse = false;
 StaticMutex AudioInputCubeb::sMutex;
 
 // AudioDeviceID is an annoying opaque value that's really a string
 // pointer, and is freed when the cubeb_device_collection is destroyed
 
 void AudioInputCubeb::UpdateDeviceList()
 {
+  cubeb* cubebContext = CubebUtils::GetCubebContext();
+  if (!cubebContext) {
+    return;
+  }
+
   cubeb_device_collection *devices = nullptr;
 
-  if (CUBEB_OK != cubeb_enumerate_devices(CubebUtils::GetCubebContext(),
+  if (CUBEB_OK != cubeb_enumerate_devices(cubebContext,
                                           CUBEB_DEVICE_TYPE_INPUT,
                                           &devices)) {
     return;
   }
 
   for (auto& device_index : (*mDeviceIndexes)) {
     device_index = -1; // unmapped
   }