Bug 1276156: Stop encoder ProcessThreads before deleting the channel r=pkerr
authorRandell Jesup <rjesup@jesup.org>
Tue, 31 May 2016 23:39:34 -0400
changeset 340869 68eb3a876ea584571f47f465c9916970e050b343
parent 340868 5d9b7a5387ed5342e37224ca07a083127458b0e2
child 340870 fa2962ff448e58356955538a876787e411d7b18f
push id1183
push userraliiev@mozilla.com
push dateMon, 05 Sep 2016 20:01:49 +0000
treeherdermozilla-release@3148731bed45 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspkerr
bugs1276156
milestone49.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 1276156: Stop encoder ProcessThreads before deleting the channel r=pkerr To avoid deadlocks between DeleteChannel() and Process() code MozReview-Commit-ID: G650SWDH8s0
media/webrtc/trunk/webrtc/video_engine/vie_channel_group.cc
media/webrtc/trunk/webrtc/video_engine/vie_channel_group.h
media/webrtc/trunk/webrtc/video_engine/vie_channel_manager.cc
media/webrtc/trunk/webrtc/video_engine/vie_encoder.cc
--- a/media/webrtc/trunk/webrtc/video_engine/vie_channel_group.cc
+++ b/media/webrtc/trunk/webrtc/video_engine/vie_channel_group.cc
@@ -276,16 +276,28 @@ bool ChannelGroup::CreateChannel(int cha
     vie_encoder_map_[channel_id] = vie_encoder;
     if (sender)
       send_encoders_[channel_id] = vie_encoder;
   }
 
   return true;
 }
 
+void ChannelGroup::Stop(int channel_id) {
+  ViEEncoder* vie_encoder = GetEncoder(channel_id);
+  DCHECK(vie_encoder != NULL);
+
+  // If we're owning the encoder, remove the feedback and stop all encoding
+  // threads and processing. This must be done before deleting the channel.
+  if (vie_encoder->channel_id() == channel_id) {
+    encoder_state_feedback_->RemoveEncoder(vie_encoder);
+    vie_encoder->StopThreadsAndRemoveSharedMembers();
+  }
+}
+
 void ChannelGroup::DeleteChannel(int channel_id) {
   ViEChannel* vie_channel = PopChannel(channel_id);
 
   ViEEncoder* vie_encoder = GetEncoder(channel_id);
   DCHECK(vie_encoder != NULL);
 
   call_stats_->DeregisterStatsObserver(vie_channel->GetStatsObserver());
   SetChannelRembStatus(channel_id, false, false, vie_channel);
--- a/media/webrtc/trunk/webrtc/video_engine/vie_channel_group.h
+++ b/media/webrtc/trunk/webrtc/video_engine/vie_channel_group.h
@@ -47,16 +47,17 @@ class ChannelGroup : public BitrateObser
                          int engine_id,
                          int number_of_cores,
                          bool disable_default_encoder);
   bool CreateReceiveChannel(int channel_id,
                             int engine_id,
                             int base_channel_id,
                             int number_of_cores,
                             bool disable_default_encoder);
+  void Stop(int channel_id);
   void DeleteChannel(int channel_id);
   void AddChannel(int channel_id);
   void RemoveChannel(int channel_id);
   bool HasChannel(int channel_id) const;
   bool Empty() const;
   ViEChannel* GetChannel(int channel_id) const;
   ViEEncoder* GetEncoder(int channel_id) const;
   std::vector<int> GetChannelIds() const;
--- a/media/webrtc/trunk/webrtc/video_engine/vie_channel_manager.cc
+++ b/media/webrtc/trunk/webrtc/video_engine/vie_channel_manager.cc
@@ -143,16 +143,33 @@ int ViEChannelManager::CreateChannel(int
   *channel_id = new_channel_id;
   channel_group->AddChannel(*channel_id);
   return 0;
 }
 
 int ViEChannelManager::DeleteChannel(int channel_id) {
   ChannelGroup* group = NULL;
   {
+    // Read lock to make sure no one tries to delete it on us
+    ViEChannelManagerScoped(*this);
+
+    // Protect the maps.
+    CriticalSectionScoped cs(channel_id_critsect_);
+
+    group = FindGroup(channel_id);
+    if (group == NULL)
+      return -1;
+    group->Stop(channel_id);
+  }
+  // Now release the read lock, and get a write lock since we know the
+  // threads are stopped.  Otherwise we can deadlock if we hold a write
+  // lock and the thread is processing and needs a read lock, and we try to
+  // stop the thread (ProcessThread::DeRegisterModule().  See Mozilla
+  // bug 1276156.
+  {
     // Write lock to make sure no one is using the channel.
     ViEManagerWriteScoped wl(this);
 
     // Protect the maps.
     CriticalSectionScoped cs(channel_id_critsect_);
 
     group = FindGroup(channel_id);
     if (group == NULL)
--- a/media/webrtc/trunk/webrtc/video_engine/vie_encoder.cc
+++ b/media/webrtc/trunk/webrtc/video_engine/vie_encoder.cc
@@ -215,20 +215,23 @@ void ViEEncoder::StartThreadsAndSetShare
 
   send_payload_router_ = send_payload_router;
   vcm_protection_callback_ = vcm_protection_callback;
 
   module_process_thread_.RegisterModule(&vcm_);
 }
 
 void ViEEncoder::StopThreadsAndRemoveSharedMembers() {
-  vcm_.RegisterProtectionCallback(NULL);
-  vcm_protection_callback_ = NULL;
-  module_process_thread_.DeRegisterModule(&vcm_);
-  module_process_thread_.DeRegisterModule(&vpm_);
+  // Avoid trying to stop the threads/etc twice
+  if (vcm_protection_callback_) {
+    vcm_.RegisterProtectionCallback(NULL);
+    vcm_protection_callback_ = NULL;
+    module_process_thread_.DeRegisterModule(&vcm_);
+    module_process_thread_.DeRegisterModule(&vpm_);
+  }
 }
 
 void ViEEncoder::SetLoadManager(CPULoadStateCallbackInvoker* load_manager) {
   load_manager_ = load_manager;
   if (load_manager_) {
       load_manager_->AddObserver(loadstate_observer_.get());
   }
   vpm_.EnableContentAnalysis(load_manager != NULL);