Bug 1435673 - Inline Alloc/FreeChannel and fix access per threading model. r?padenot draft
authorAndreas Pehrson <pehrsons@mozilla.com>
Mon, 05 Feb 2018 10:50:47 +0100
changeset 751089 ef7e7c44906b4c319e21eb9903feb41db820b537
parent 751088 c8853d369e04372bd4e97af14df93dc428afd2b7
child 751090 1ba19c27797cac2c72ae422c6cffc50e434e4d9e
push id97847
push userbmo:apehrson@mozilla.com
push dateMon, 05 Feb 2018 10:00:19 +0000
reviewerspadenot
bugs1435673
milestone60.0a1
Bug 1435673 - Inline Alloc/FreeChannel and fix access per threading model. r?padenot MozReview-Commit-ID: 1hpt8tAFuKW
dom/media/webrtc/MediaEngineWebRTC.h
dom/media/webrtc/MediaEngineWebRTCAudio.cpp
--- a/dom/media/webrtc/MediaEngineWebRTC.h
+++ b/dom/media/webrtc/MediaEngineWebRTC.h
@@ -518,20 +518,16 @@ private:
 
 
   void UpdateAECSettingsIfNeeded(bool aEnable, webrtc::EcModes aMode);
   void UpdateAGCSettingsIfNeeded(bool aEnable, webrtc::AgcModes aMode);
   void UpdateNSSettingsIfNeeded(bool aEnable, webrtc::NsModes aMode);
 
   void SetLastPrefs(const MediaEnginePrefs& aPrefs);
 
-  // These allocate/configure and release the channel
-  bool AllocChannel();
-  void FreeChannel();
-
   bool HasEnabledTrack() const;
 
   template<typename T>
   void InsertInGraph(const T* aBuffer,
                      size_t aFrames,
                      uint32_t aChannels);
 
   void PacketizeAndProcess(MediaStreamGraph* aGraph,
@@ -547,17 +543,17 @@ private:
   bool PassThrough() const;
 
   // Graph thread only.
   void SetPassThrough(bool aPassThrough);
 
   // Owning thread only.
   RefPtr<WebRTCAudioDataListener> mListener;
 
-  // Note: shared across all microphone sources
+  // Note: shared across all microphone sources. Owning thread only.
   static int sChannelsOpen;
 
   const RefPtr<mozilla::AudioInput> mAudioInput;
   const UniquePtr<webrtc::AudioProcessing> mAudioProcessing;
 
   // accessed from the GraphDriver thread except for deletion.
   nsAutoPtr<AudioPacketizer<AudioDataValue, float>> mPacketizerInput;
   nsAutoPtr<AudioPacketizer<AudioDataValue, float>> mPacketizerOutput;
--- a/dom/media/webrtc/MediaEngineWebRTCAudio.cpp
+++ b/dom/media/webrtc/MediaEngineWebRTCAudio.cpp
@@ -439,21 +439,21 @@ MediaEngineWebRTCMicrophoneSource::Updat
         // Until we fix (or wallpaper) support for multiple mic input
         // (Bug 1238038) fail allocation for a second device
         return NS_ERROR_FAILURE;
       }
       if (mAudioInput->SetRecordingDevice(mCapIndex)) {
          return NS_ERROR_FAILURE;
       }
       mAudioInput->SetUserChannelCount(prefs.mChannels);
-      if (!AllocChannel()) {
-        FreeChannel();
-        LOG(("Audio device is not initalized"));
-        return NS_ERROR_FAILURE;
+      {
+        MutexAutoLock lock(mMutex);
+        mState = kAllocated;
       }
+      sChannelsOpen++;
       LOG(("Audio device %d allocated", mCapIndex));
       {
         // Update with the actual applied channelCount in order
         // to store it in settings.
         uint32_t channelCount = 0;
         mAudioInput->GetChannelCount(channelCount);
         MOZ_ASSERT(channelCount > 0);
         prefs.mChannels = channelCount;
@@ -608,17 +608,21 @@ MediaEngineWebRTCMicrophoneSource::Deall
     MutexAutoLock lock(mMutex);
     mAllocations.RemoveElementAt(i);
   }
 
   if (mAllocations.IsEmpty()) {
     // If empty, no callbacks to deliver data should be occuring
     MOZ_ASSERT(mState != kReleased, "Source not allocated");
     MOZ_ASSERT(mState != kStarted, "Source not stopped");
-    FreeChannel();
+    MOZ_ASSERT(sChannelsOpen > 0);
+    --sChannelsOpen;
+
+    MutexAutoLock lock(mMutex);
+    mState = kReleased;
     LOG(("Audio device %d deallocated", mCapIndex));
   } else {
     LOG(("Audio device %d deallocated but still in use", mCapIndex));
   }
   return NS_OK;
 }
 
 nsresult
@@ -1123,36 +1127,16 @@ void
 MediaEngineWebRTCMicrophoneSource::DeviceChanged()
 {
   // Reset some processing
   ResetProcessingIfNeeded(gain_control);
   ResetProcessingIfNeeded(echo_cancellation);
   ResetProcessingIfNeeded(noise_suppression);
 }
 
-// mState records if a channel is allocated (slightly redundantly to mChannel)
-void
-MediaEngineWebRTCMicrophoneSource::FreeChannel()
-{
-  if (mState != kReleased) {
-    mState = kReleased;
-
-    MOZ_ASSERT(sChannelsOpen > 0);
-    --sChannelsOpen;
-  }
-}
-
-bool
-MediaEngineWebRTCMicrophoneSource::AllocChannel()
-{
-  mState = kAllocated;
-  sChannelsOpen++;
-  return true;
-}
-
 void
 MediaEngineWebRTCMicrophoneSource::Shutdown()
 {
   AssertIsOnOwningThread();
 
   if (mListener) {
     // breaks a cycle, since the WebRTCAudioDataListener has a RefPtr to us
     mListener->Shutdown();
@@ -1166,17 +1150,16 @@ MediaEngineWebRTCMicrophoneSource::Shutd
         Stop(allocation.mHandle);
       }
     }
     MOZ_ASSERT(mState == kStopped);
   }
 
   while (!mAllocations.IsEmpty()) {
     MOZ_ASSERT(mState == kAllocated || mState == kStopped);
-    // on last Deallocate(), FreeChannel()s and DeInit()s if all channels are released
     Deallocate(mAllocations[0].mHandle);
   }
   MOZ_ASSERT(mState == kReleased);
 }
 
 nsString
 MediaEngineWebRTCAudioCaptureSource::GetName() const
 {