diff --git a/pc/BUILD.gn b/pc/BUILD.gn index bc44bbbebd..001bf02512 100644 --- a/pc/BUILD.gn +++ b/pc/BUILD.gn @@ -87,6 +87,7 @@ rtc_library("rtc_pc_base") { "../api:array_view", "../api:audio_options_api", "../api:call_api", + "../api:function_view", "../api:ice_transport_factory", "../api:libjingle_peerconnection_api", "../api:rtc_error", diff --git a/pc/channel.h b/pc/channel.h index c2b9e40dec..f59a204a95 100644 --- a/pc/channel.h +++ b/pc/channel.h @@ -19,6 +19,7 @@ #include #include "api/call/audio_sink.h" +#include "api/function_view.h" #include "api/jsep.h" #include "api/rtp_receiver_interface.h" #include "api/transport/media/media_transport_config.h" @@ -259,8 +260,9 @@ class BaseChannel : public ChannelInterface, void OnMessage(rtc::Message* pmsg) override; // Helper function template for invoking methods on the worker thread. - template - T InvokeOnWorker(const rtc::Location& posted_from, const FunctorT& functor) { + template + T InvokeOnWorker(const rtc::Location& posted_from, + rtc::FunctionView functor) { return worker_thread_->Invoke(posted_from, functor); } diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index 41e553c7bc..2de8c4f22d 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -785,6 +785,7 @@ rtc_library("rtc_base") { ":checks", ":stringutils", "../api:array_view", + "../api:function_view", "../api:scoped_refptr", "../api/task_queue", "network:sent_packet", diff --git a/rtc_base/thread.cc b/rtc_base/thread.cc index 0b8905e922..c16c39bb53 100644 --- a/rtc_base/thread.cc +++ b/rtc_base/thread.cc @@ -469,11 +469,22 @@ bool Thread::PopSendMessageFromThread(const Thread* source, _SendMessage* msg) { } void Thread::InvokeInternal(const Location& posted_from, - MessageHandler* handler) { + rtc::FunctionView functor) { TRACE_EVENT2("webrtc", "Thread::Invoke", "src_file_and_line", posted_from.file_and_line(), "src_func", posted_from.function_name()); - Send(posted_from, handler); + + class FunctorMessageHandler : public MessageHandler { + public: + explicit FunctorMessageHandler(rtc::FunctionView functor) + : functor_(functor) {} + void OnMessage(Message* msg) override { functor_(); } + + private: + rtc::FunctionView functor_; + } handler(functor); + + Send(posted_from, &handler); } void Thread::QueuedTaskHandler::OnMessage(Message* msg) { diff --git a/rtc_base/thread.h b/rtc_base/thread.h index 186d7f4c4d..f433bab1ba 100644 --- a/rtc_base/thread.h +++ b/rtc_base/thread.h @@ -21,6 +21,7 @@ #if defined(WEBRTC_POSIX) #include #endif +#include "api/function_view.h" #include "api/task_queue/queued_task.h" #include "api/task_queue/task_queue_base.h" #include "rtc_base/constructor_magic.h" @@ -214,12 +215,20 @@ class RTC_LOCKABLE RTC_EXPORT Thread : public MessageQueue, // See ScopedDisallowBlockingCalls for details. // NOTE: Blocking invokes are DISCOURAGED, consider if what you're doing can // be achieved with PostTask() and callbacks instead. - template - ReturnT Invoke(const Location& posted_from, FunctorT&& functor) { - FunctorMessageHandler handler( - std::forward(functor)); - InvokeInternal(posted_from, &handler); - return handler.MoveResult(); + template < + class ReturnT, + typename = typename std::enable_if::value>::type> + ReturnT Invoke(const Location& posted_from, FunctionView functor) { + ReturnT result; + InvokeInternal(posted_from, [functor, &result] { result = functor(); }); + return result; + } + + template < + class ReturnT, + typename = typename std::enable_if::value>::type> + void Invoke(const Location& posted_from, FunctionView functor) { + InvokeInternal(posted_from, functor); } // Posts a task to invoke the functor on |this| thread asynchronously, i.e. @@ -369,7 +378,8 @@ class RTC_LOCKABLE RTC_EXPORT Thread : public MessageQueue, // Returns true if there is such a message. bool PopSendMessageFromThread(const Thread* source, _SendMessage* msg); - void InvokeInternal(const Location& posted_from, MessageHandler* handler); + void InvokeInternal(const Location& posted_from, + rtc::FunctionView functor); std::list<_SendMessage> sendlist_; std::string name_;