diff --git a/media/BUILD.gn b/media/BUILD.gn index c12b08ff5b..e4a0d89c3a 100644 --- a/media/BUILD.gn +++ b/media/BUILD.gn @@ -494,6 +494,8 @@ if (rtc_include_tests) { "../api:call_api", "../api:fec_controller_api", "../api:scoped_refptr", + "../api/task_queue", + "../api/task_queue:pending_task_safety_flag", "../api/transport:field_trial_based_config", "../api/video:encoded_image", "../api/video:video_bitrate_allocation", diff --git a/media/base/fake_network_interface.h b/media/base/fake_network_interface.h index 043e559f28..099b7cad38 100644 --- a/media/base/fake_network_interface.h +++ b/media/base/fake_network_interface.h @@ -13,8 +13,11 @@ #include #include +#include #include +#include "api/task_queue/pending_task_safety_flag.h" +#include "api/task_queue/task_queue_base.h" #include "media/base/media_channel.h" #include "media/base/rtp_utils.h" #include "modules/rtp_rtcp/source/rtp_util.h" @@ -22,15 +25,13 @@ #include "rtc_base/checks.h" #include "rtc_base/copy_on_write_buffer.h" #include "rtc_base/dscp.h" -#include "rtc_base/message_handler.h" #include "rtc_base/synchronization/mutex.h" #include "rtc_base/thread.h" namespace cricket { // Fake NetworkInterface that sends/receives RTP/RTCP packets. -class FakeNetworkInterface : public MediaChannel::NetworkInterface, - public rtc::MessageHandlerAutoCleanup { +class FakeNetworkInterface : public MediaChannel::NetworkInterface { public: FakeNetworkInterface() : thread_(rtc::Thread::Current()), @@ -129,10 +130,10 @@ class FakeNetworkInterface : public MediaChannel::NetworkInterface, if (conf_) { for (size_t i = 0; i < conf_sent_ssrcs_.size(); ++i) { SetRtpSsrc(conf_sent_ssrcs_[i], *packet); - PostMessage(ST_RTP, *packet); + PostPacket(*packet); } } else { - PostMessage(ST_RTP, *packet); + PostPacket(*packet); } return true; } @@ -145,7 +146,8 @@ class FakeNetworkInterface : public MediaChannel::NetworkInterface, options_ = options; if (!conf_) { // don't worry about RTCP in conf mode for now - PostMessage(ST_RTCP, *packet); + RTC_LOG(LS_VERBOSE) << "Dropping RTCP packet, they are not handled by " + "MediaChannel anymore."; } return true; } @@ -161,22 +163,13 @@ class FakeNetworkInterface : public MediaChannel::NetworkInterface, return 0; } - void PostMessage(int id, const rtc::CopyOnWriteBuffer& packet) { - thread_->Post(RTC_FROM_HERE, this, id, rtc::WrapMessageData(packet)); - } - - virtual void OnMessage(rtc::Message* msg) { - rtc::TypedMessageData* msg_data = - static_cast*>(msg->pdata); - if (dest_) { - if (msg->message_id == ST_RTP) { - dest_->OnPacketReceived(msg_data->data(), rtc::TimeMicros()); - } else { - RTC_LOG(LS_VERBOSE) << "Dropping RTCP packet, they not handled by " - "MediaChannel anymore."; - } - } - delete msg_data; + void PostPacket(rtc::CopyOnWriteBuffer packet) { + thread_->PostTask( + SafeTask(safety_.flag(), [this, packet = std::move(packet)]() mutable { + if (dest_) { + dest_->OnPacketReceived(std::move(packet), rtc::TimeMicros()); + } + })); } private: @@ -204,7 +197,7 @@ class FakeNetworkInterface : public MediaChannel::NetworkInterface, } } - rtc::Thread* thread_; + webrtc::TaskQueueBase* thread_; MediaChannel* dest_; bool conf_; // The ssrcs used in sending out packets in conference mode. @@ -222,6 +215,7 @@ class FakeNetworkInterface : public MediaChannel::NetworkInterface, rtc::DiffServCodePoint dscp_; // Options of the most recently sent packet. rtc::PacketOptions options_; + webrtc::ScopedTaskSafety safety_; }; } // namespace cricket