diff --git a/pc/BUILD.gn b/pc/BUILD.gn index ff5b6d98a1..456f267daf 100644 --- a/pc/BUILD.gn +++ b/pc/BUILD.gn @@ -1854,14 +1854,13 @@ rtc_library("remote_audio_source") { "../api:media_stream_interface", "../api:scoped_refptr", "../api:sequence_checker", + "../api/task_queue", "../media:rtc_media_base", "../rtc_base", "../rtc_base:checks", - "../rtc_base:location", "../rtc_base:logging", "../rtc_base:safe_conversions", "../rtc_base:stringutils", - "../rtc_base:threading", "../rtc_base/synchronization:mutex", ] absl_deps = [ diff --git a/pc/remote_audio_source.cc b/pc/remote_audio_source.cc index 597d19977b..667312c38c 100644 --- a/pc/remote_audio_source.cc +++ b/pc/remote_audio_source.cc @@ -14,15 +14,15 @@ #include #include +#include #include "absl/algorithm/container.h" #include "api/scoped_refptr.h" #include "api/sequence_checker.h" +#include "api/task_queue/task_queue_base.h" #include "rtc_base/checks.h" -#include "rtc_base/location.h" #include "rtc_base/logging.h" #include "rtc_base/strings/string_format.h" -#include "rtc_base/thread.h" namespace webrtc { @@ -51,9 +51,9 @@ class RemoteAudioSource::AudioDataProxy : public AudioSinkInterface { }; RemoteAudioSource::RemoteAudioSource( - rtc::Thread* worker_thread, + TaskQueueBase* worker_thread, OnAudioChannelGoneAction on_audio_channel_gone_action) - : main_thread_(rtc::Thread::Current()), + : main_thread_(TaskQueueBase::Current()), worker_thread_(worker_thread), on_audio_channel_gone_action_(on_audio_channel_gone_action), state_(MediaSourceInterface::kInitializing) { @@ -163,24 +163,18 @@ void RemoteAudioSource::OnAudioChannelGone() { if (on_audio_channel_gone_action_ != OnAudioChannelGoneAction::kEnd) { return; } - // Called when the audio channel is deleted. It may be the worker thread - // in libjingle or may be a different worker thread. - // This object needs to live long enough for the cleanup logic in OnMessage to - // run, so take a reference to it as the data. Sometimes the message may not - // be processed (because the thread was destroyed shortly after this call), - // but that is fine because the thread destructor will take care of destroying - // the message data which will release the reference on RemoteAudioSource. - main_thread_->Post(RTC_FROM_HERE, this, 0, - new rtc::ScopedRefMessageData(this)); -} - -void RemoteAudioSource::OnMessage(rtc::Message* msg) { - RTC_DCHECK_RUN_ON(main_thread_); - sinks_.clear(); - SetState(MediaSourceInterface::kEnded); - // Will possibly delete this RemoteAudioSource since it is reference counted - // in the message. - delete msg->pdata; + // Called when the audio channel is deleted. It may be the worker thread or + // may be a different task queue. + // This object needs to live long enough for the cleanup logic in the posted + // task to run, so take a reference to it. Sometimes the task may not be + // processed (because the task queue was destroyed shortly after this call), + // but that is fine because the task queue destructor will take care of + // destroying task which will release the reference on RemoteAudioSource. + rtc::scoped_refptr thiz(this); + main_thread_->PostTask([thiz = std::move(thiz)] { + thiz->sinks_.clear(); + thiz->SetState(MediaSourceInterface::kEnded); + }); } } // namespace webrtc diff --git a/pc/remote_audio_source.h b/pc/remote_audio_source.h index 89af4db714..d294a0f0fb 100644 --- a/pc/remote_audio_source.h +++ b/pc/remote_audio_source.h @@ -20,24 +20,16 @@ #include "api/call/audio_sink.h" #include "api/media_stream_interface.h" #include "api/notifier.h" +#include "api/task_queue/task_queue_base.h" #include "media/base/media_channel.h" -#include "rtc_base/message_handler.h" #include "rtc_base/synchronization/mutex.h" -#include "rtc_base/thread.h" -#include "rtc_base/thread_message.h" - -namespace rtc { -struct Message; -class Thread; -} // namespace rtc namespace webrtc { // This class implements the audio source used by the remote audio track. // This class works by configuring itself as a sink with the underlying media // engine, then when receiving data will fan out to all added sinks. -class RemoteAudioSource : public Notifier, - rtc::MessageHandler { +class RemoteAudioSource : public Notifier { public: // In Unified Plan, receivers map to m= sections and their tracks and sources // survive SSRCs being reconfigured. The life cycle of the remote audio source @@ -52,7 +44,7 @@ class RemoteAudioSource : public Notifier, }; explicit RemoteAudioSource( - rtc::Thread* worker_thread, + TaskQueueBase* worker_thread, OnAudioChannelGoneAction on_audio_channel_gone_action); // Register and unregister remote audio source with the underlying media @@ -85,10 +77,8 @@ class RemoteAudioSource : public Notifier, void OnData(const AudioSinkInterface::Data& audio); void OnAudioChannelGone(); - void OnMessage(rtc::Message* msg) override; - - rtc::Thread* const main_thread_; - rtc::Thread* const worker_thread_; + TaskQueueBase* const main_thread_; + TaskQueueBase* const worker_thread_; const OnAudioChannelGoneAction on_audio_channel_gone_action_; std::list audio_observers_; Mutex sink_lock_;