| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | // This file contains Macros for creating proxies for webrtc MediaStream and |
| 12 | // PeerConnection classes. |
| 13 |
| 14 | // The proxied objects are initialized with either one or two thread |
| 15 | // objects that operations can be proxied to: The primary and secondary |
| 16 | // threads. |
| 17 | // In common usage, the primary thread will be the PeerConnection's |
| 18 | // signaling thread, and the secondary thread will be either the |
| 19 | // PeerConnection's worker thread or the PeerConnection's network thread. |
| 20 |
| 21 | // |
| 22 | // Example usage: |
| 23 | // |
| 24 | // class TestInterface : public rtc::RefCountInterface { |
| 25 | // public: |
| 26 | // std::string FooA() = 0; |
| 27 | // std::string FooB(bool arg1) const = 0; |
| 28 | // std::string FooC(bool arg1) = 0; |
| 29 | // }; |
| 30 | // |
| 31 | // Note that return types can not be a const reference. |
| 32 | // |
| 33 | // class Test : public TestInterface { |
| 34 | // ... implementation of the interface. |
| 35 | // }; |
| 36 | // |
| 37 | // BEGIN_PROXY_MAP(Test) |
| 38 | // PROXY_PRIMARY_THREAD_DESTRUCTOR() |
| 39 | // PROXY_METHOD0(std::string, FooA) |
| 40 | // PROXY_CONSTMETHOD1(std::string, FooB, arg1) |
| 41 | // PROXY_SECONDARY_METHOD1(std::string, FooC, arg1) |
| 42 | // END_PROXY_MAP() |
| 43 | // |
| 44 | // Where the destructor and first two methods are invoked on the primary |
| 45 | // thread, and the third is invoked on the secondary thread. |
| 46 | // |
| 47 | // The proxy can be created using |
| 48 | // |
| 49 | // TestProxy::Create(Thread* signaling_thread, Thread* worker_thread, |
| 50 | // TestInterface*). |
| 51 | // |
| 52 | // The variant defined with BEGIN_PRIMARY_PROXY_MAP is unaware of |
| 53 | // the secondary thread, and invokes all methods on the primary thread. |
| 54 | // |
| 55 |
| 56 | #ifndef PC_PROXY_H_ |
| 57 | #define PC_PROXY_H_ |
| 58 |
| 59 | #include <stddef.h> |
| 60 |
| 61 | #include <memory> |
| 62 | #include <string> |
| 63 | #include <tuple> |
| 64 | #include <type_traits> |
| 65 | #include <utility> |
| 66 |
| 67 | #include "api/scoped_refptr.h" |
| 68 | #include "api/task_queue/task_queue_base.h" |
| 69 | #include "rtc_base/event.h" |
| 70 | #include "rtc_base/string_utils.h" |
| 71 | #include "rtc_base/system/rtc_export.h" |
| 72 | #include "rtc_base/thread.h" |
| 73 |
| 74 | #if !defined(RTC_DISABLE_PROXY_TRACE_EVENTS) && !defined(WEBRTC_CHROMIUM_BUILD) |
| 75 | #define RTC_DISABLE_PROXY_TRACE_EVENTS |
| 76 | #endif |
| 77 |
| 78 | namespace webrtc { |
| 79 | namespace proxy_internal { |
| 80 |
| 81 | // Class for tracing the lifetime of MethodCall::Marshal. |
| 82 | class ScopedTrace { |
| 83 | public: |
| 84 | explicit ScopedTrace(const char* class_and_method_name); |
| 85 | ~ScopedTrace(); |
| 86 |
| 87 | private: |
| 88 | [[maybe_unused]] const char* const class_and_method_name_; |
| 89 | }; |
| 90 | } // namespace proxy_internal |
| 91 |
| 92 | template <typename R> |
| 93 | class ReturnType { |
| 94 | public: |
| 95 | template <typename C, typename M, typename... Args> |
| 96 | void Invoke(C* c, M m, Args&&... args) { |
| 97 | r_ = (c->*m)(std::forward<Args>(args)...); |
| 98 | } |
| 99 |
| 100 | R moved_result() { return std::move(r_); } |
| 101 |
| 102 | private: |
| 103 | R r_; |
| 104 | }; |
| 105 |
| 106 | template <> |
| 107 | class ReturnType<void> { |
| 108 | public: |
| 109 | template <typename C, typename M, typename... Args> |
| 110 | void Invoke(C* c, M m, Args&&... args) { |
| 111 | (c->*m)(std::forward<Args>(args)...); |
| 112 | } |
| 113 |
| 114 | void moved_result() {} |
| 115 | }; |
| 116 |
| 117 | template <typename C, typename R, typename... Args> |
| 118 | class MethodCall { |
| 119 | public: |
| 120 | typedef R (C::*Method)(Args...); |
| 121 | MethodCall(C* c, Method m, Args&&... args) |
| 122 | : c_(c), |
| 123 | m_(m), |
| 124 | args_(std::forward_as_tuple(std::forward<Args>(args)...)) {} |
| 125 |
| 126 | R Marshal(rtc::Thread* t) { |
| 127 | if (t->IsCurrent()) { |
| 128 | Invoke(std::index_sequence_for<Args...>()); |
| 129 | } else { |
| 130 | t->PostTask([this] { |
| 131 | Invoke(std::index_sequence_for<Args...>()); |
| 132 | event_.Set(); |
| 133 | }); |
| 134 | event_.Wait(rtc::Event::kForever); |
| 135 | } |
| 136 | return r_.moved_result(); |
| 137 | } |
| 138 |
| 139 | private: |
| 140 | template <size_t... Is> |
| 141 | void Invoke(std::index_sequence<Is...>) { |
| 142 | r_.Invoke(c_, m_, std::move(std::get<Is>(args_))...); |
| 143 | } |
| 144 |
| 145 | C* c_; |
| 146 | Method m_; |
| 147 | ReturnType<R> r_; |
| 148 | std::tuple<Args&&...> args_; |
| 149 | rtc::Event event_; |
| 150 | }; |
| 151 |
| 152 | template <typename C, typename R, typename... Args> |
| 153 | class ConstMethodCall { |
| 154 | public: |
| 155 | typedef R (C::*Method)(Args...) const; |
| 156 | ConstMethodCall(const C* c, Method m, Args&&... args) |
| 157 | : c_(c), |
| 158 | m_(m), |
| 159 | args_(std::forward_as_tuple(std::forward<Args>(args)...)) {} |
| 160 |
| 161 | R Marshal(rtc::Thread* t) { |
| 162 | if (t->IsCurrent()) { |
| 163 | Invoke(std::index_sequence_for<Args...>()); |
| 164 | } else { |
| 165 | t->PostTask([this] { |
| 166 | Invoke(std::index_sequence_for<Args...>()); |
| 167 | event_.Set(); |
| 168 | }); |
| 169 | event_.Wait(rtc::Event::kForever); |
| 170 | } |
| 171 | return r_.moved_result(); |
| 172 | } |
| 173 |
| 174 | private: |
| 175 | template <size_t... Is> |
| 176 | void Invoke(std::index_sequence<Is...>) { |
| 177 | r_.Invoke(c_, m_, std::move(std::get<Is>(args_))...); |
| 178 | } |
| 179 |
| 180 | const C* c_; |
| 181 | Method m_; |
| 182 | ReturnType<R> r_; |
| 183 | std::tuple<Args&&...> args_; |
| 184 | rtc::Event event_; |
| 185 | }; |
| 186 |
| 187 | #define PROXY_STRINGIZE_IMPL(x) #x |
| 188 | #define PROXY_STRINGIZE(x) PROXY_STRINGIZE_IMPL(x) |
| 189 |
| 190 | // Helper macros to reduce code duplication. |
| 191 | #define PROXY_MAP_BOILERPLATE(class_name) \ |
| 192 | template <class INTERNAL_CLASS> \ |
| 193 | class class_name##ProxyWithInternal; \ |
| 194 | typedef class_name##ProxyWithInternal<class_name##Interface> \ |
| 195 | class_name##Proxy; \ |
| 196 | template <class INTERNAL_CLASS> \ |
| 197 | class class_name##ProxyWithInternal : public class_name##Interface { \ |
| 198 | protected: \ |
| 199 | static constexpr char proxy_name_[] = #class_name "Proxy"; \ |
| 200 | typedef class_name##Interface C; \ |
| 201 | \ |
| 202 | public: \ |
| 203 | const INTERNAL_CLASS* internal() const { \ |
| 204 | return c(); \ |
| 205 | } \ |
| 206 | INTERNAL_CLASS* internal() { \ |
| 207 | return c(); \ |
| 208 | } |
| 209 |
| 210 | // clang-format off |
| 211 | // clang-format would put the semicolon alone, |
| 212 | // leading to a presubmit error (cpplint.py) |
| 213 | #define END_PROXY_MAP(class_name) \ |
| 214 | }; \ |
| 215 | template <class INTERNAL_CLASS> \ |
| 216 | constexpr char class_name##ProxyWithInternal<INTERNAL_CLASS>::proxy_name_[]; |
| 217 | // clang-format on |
| 218 |
| 219 | #define PRIMARY_PROXY_MAP_BOILERPLATE(class_name) \ |
| 220 | protected: \ |
| 221 | class_name##ProxyWithInternal(rtc::Thread* primary_thread, \ |
| 222 | rtc::scoped_refptr<INTERNAL_CLASS> c) \ |
| 223 | : primary_thread_(primary_thread), c_(std::move(c)) {} \ |
| 224 | \ |
| 225 | private: \ |
| 226 | mutable rtc::Thread* primary_thread_; |
| 227 |
| 228 | #define SECONDARY_PROXY_MAP_BOILERPLATE(class_name) \ |
| 229 | protected: \ |
| 230 | class_name##ProxyWithInternal(rtc::Thread* primary_thread, \ |
| 231 | rtc::Thread* secondary_thread, \ |
| 232 | rtc::scoped_refptr<INTERNAL_CLASS> c) \ |
| 233 | : primary_thread_(primary_thread), \ |
| 234 | secondary_thread_(secondary_thread), \ |
| 235 | c_(std::move(c)) {} \ |
| 236 | \ |
| 237 | private: \ |
| 238 | mutable rtc::Thread* primary_thread_; \ |
| 239 | mutable rtc::Thread* secondary_thread_; |
| 240 |
| 241 | // Note that the destructor is protected so that the proxy can only be |
| 242 | // destroyed via RefCountInterface. |
| 243 | #define REFCOUNTED_PROXY_MAP_BOILERPLATE(class_name) \ |
| 244 | protected: \ |
| 245 | ~class_name##ProxyWithInternal() { \ |
| 246 | MethodCall<class_name##ProxyWithInternal, void> call( \ |
| 247 | this, &class_name##ProxyWithInternal::DestroyInternal); \ |
| 248 | call.Marshal(destructor_thread()); \ |
| 249 | } \ |
| 250 | \ |
| 251 | private: \ |
| 252 | const INTERNAL_CLASS* c() const { \ |
| 253 | return c_.get(); \ |
| 254 | } \ |
| 255 | INTERNAL_CLASS* c() { \ |
| 256 | return c_.get(); \ |
| 257 | } \ |
| 258 | void DestroyInternal() { \ |
| 259 | c_ = nullptr; \ |
| 260 | } \ |
| 261 | rtc::scoped_refptr<INTERNAL_CLASS> c_; |
| 262 |
| 263 | // Note: This doesn't use a unique_ptr, because it intends to handle a corner |
| 264 | // case where an object's deletion triggers a callback that calls back into |
| 265 | // this proxy object. If relying on a unique_ptr to delete the object, its |
| 266 | // inner pointer would be set to null before this reentrant callback would have |
| 267 | // a chance to run, resulting in a segfault. |
| 268 | #define OWNED_PROXY_MAP_BOILERPLATE(class_name) \ |
| 269 | public: \ |
| 270 | ~class_name##ProxyWithInternal() { \ |
| 271 | MethodCall<class_name##ProxyWithInternal, void> call( \ |
| 272 | this, &class_name##ProxyWithInternal::DestroyInternal); \ |
| 273 | call.Marshal(destructor_thread()); \ |
| 274 | } \ |
| 275 | \ |
| 276 | private: \ |
| 277 | const INTERNAL_CLASS* c() const { \ |
| 278 | return c_; \ |
| 279 | } \ |
| 280 | INTERNAL_CLASS* c() { \ |
| 281 | return c_; \ |
| 282 | } \ |
| 283 | void DestroyInternal() { \ |
| 284 | delete c_; \ |
| 285 | } \ |
| 286 | INTERNAL_CLASS* c_; |
| 287 |
| 288 | #define BEGIN_PRIMARY_PROXY_MAP(class_name) \ |
| 289 | PROXY_MAP_BOILERPLATE(class_name) \ |
| 290 | PRIMARY_PROXY_MAP_BOILERPLATE(class_name) \ |
| 291 | REFCOUNTED_PROXY_MAP_BOILERPLATE(class_name) \ |
| 292 | public: \ |
| 293 | static rtc::scoped_refptr<class_name##ProxyWithInternal> Create( \ |
| 294 | rtc::Thread* primary_thread, rtc::scoped_refptr<INTERNAL_CLASS> c) { \ |
| 295 | return rtc::make_ref_counted<class_name##ProxyWithInternal>( \ |
| 296 | primary_thread, std::move(c)); \ |
| 297 | } |
| 298 |
| 299 | #define BEGIN_PROXY_MAP(class_name) \ |
| 300 | PROXY_MAP_BOILERPLATE(class_name) \ |
| 301 | SECONDARY_PROXY_MAP_BOILERPLATE(class_name) \ |
| 302 | REFCOUNTED_PROXY_MAP_BOILERPLATE(class_name) \ |
| 303 | public: \ |
| 304 | static rtc::scoped_refptr<class_name##ProxyWithInternal> Create( \ |
| 305 | rtc::Thread* primary_thread, rtc::Thread* secondary_thread, \ |
| 306 | rtc::scoped_refptr<INTERNAL_CLASS> c) { \ |
| 307 | return rtc::make_ref_counted<class_name##ProxyWithInternal>( \ |
| 308 | primary_thread, secondary_thread, std::move(c)); \ |
| 309 | } |
| 310 |
| 311 | #define PROXY_PRIMARY_THREAD_DESTRUCTOR() \ |
| 312 | private: \ |
| 313 | rtc::Thread* destructor_thread() const { \ |
| 314 | return primary_thread_; \ |
| 315 | } \ |
| 316 | \ |
| 317 | public: // NOLINTNEXTLINE |
| 318 |
| 319 | #define PROXY_SECONDARY_THREAD_DESTRUCTOR() \ |
| 320 | private: \ |
| 321 | rtc::Thread* destructor_thread() const { \ |
| 322 | return secondary_thread_; \ |
| 323 | } \ |
| 324 | \ |
| 325 | public: // NOLINTNEXTLINE |
| 326 |
| 327 | #if defined(RTC_DISABLE_PROXY_TRACE_EVENTS) |
| 328 | #define TRACE_BOILERPLATE(method) \ |
| 329 | do { \ |
| 330 | } while (0) |
| 331 | #else // if defined(RTC_DISABLE_PROXY_TRACE_EVENTS) |
| 332 | #define TRACE_BOILERPLATE(method) \ |
| 333 | static constexpr auto class_and_method_name = \ |
| 334 | rtc::MakeCompileTimeString(proxy_name_) \ |
| 335 | .Concat(rtc::MakeCompileTimeString("::")) \ |
| 336 | .Concat(rtc::MakeCompileTimeString(#method)); \ |
| 337 | proxy_internal::ScopedTrace scoped_trace(class_and_method_name.string) |
| 338 |
| 339 | #endif // if defined(RTC_DISABLE_PROXY_TRACE_EVENTS) |
| 340 |
| 341 | #define PROXY_METHOD0(r, method) \ |
| 342 | r method() override { \ |
| 343 | TRACE_BOILERPLATE(method); \ |
| 344 | MethodCall<C, r> call(c(), &C::method); \ |
| 345 | return call.Marshal(primary_thread_); \ |
| 346 | } |
| 347 |
| 348 | #define PROXY_CONSTMETHOD0(r, method) \ |
| 349 | r method() const override { \ |
| 350 | TRACE_BOILERPLATE(method); \ |
| 351 | ConstMethodCall<C, r> call(c(), &C::method); \ |
| 352 | return call.Marshal(primary_thread_); \ |
| 353 | } |
| 354 |
| 355 | #define PROXY_METHOD1(r, method, t1) \ |
| 356 | r method(t1 a1) override { \ |
| 357 | TRACE_BOILERPLATE(method); \ |
| 358 | MethodCall<C, r, t1> call(c(), &C::method, std::move(a1)); \ |
| 359 | return call.Marshal(primary_thread_); \ |
| 360 | } |
| 361 |
| 362 | #define PROXY_CONSTMETHOD1(r, method, t1) \ |
| 363 | r method(t1 a1) const override { \ |
| 364 | TRACE_BOILERPLATE(method); \ |
| 365 | ConstMethodCall<C, r, t1> call(c(), &C::method, std::move(a1)); \ |
| 366 | return call.Marshal(primary_thread_); \ |
| 367 | } |
| 368 |
| 369 | #define PROXY_METHOD2(r, method, t1, t2) \ |
| 370 | r method(t1 a1, t2 a2) override { \ |
| 371 | TRACE_BOILERPLATE(method); \ |
| 372 | MethodCall<C, r, t1, t2> call(c(), &C::method, std::move(a1), \ |
| 373 | std::move(a2)); \ |
| 374 | return call.Marshal(primary_thread_); \ |
| 375 | } |
| 376 |
| 377 | #define PROXY_METHOD3(r, method, t1, t2, t3) \ |
| 378 | r method(t1 a1, t2 a2, t3 a3) override { \ |
| 379 | TRACE_BOILERPLATE(method); \ |
| 380 | MethodCall<C, r, t1, t2, t3> call(c(), &C::method, std::move(a1), \ |
| 381 | std::move(a2), std::move(a3)); \ |
| 382 | return call.Marshal(primary_thread_); \ |
| 383 | } |
| 384 |
| 385 | #define PROXY_METHOD4(r, method, t1, t2, t3, t4) \ |
| 386 | r method(t1 a1, t2 a2, t3 a3, t4 a4) override { \ |
| 387 | TRACE_BOILERPLATE(method); \ |
| 388 | MethodCall<C, r, t1, t2, t3, t4> call(c(), &C::method, std::move(a1), \ |
| 389 | std::move(a2), std::move(a3), \ |
| 390 | std::move(a4)); \ |
| 391 | return call.Marshal(primary_thread_); \ |
| 392 | } |
| 393 |
| 394 | #define PROXY_METHOD5(r, method, t1, t2, t3, t4, t5) \ |
| 395 | r method(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5) override { \ |
| 396 | TRACE_BOILERPLATE(method); \ |
| 397 | MethodCall<C, r, t1, t2, t3, t4, t5> call(c(), &C::method, std::move(a1), \ |
| 398 | std::move(a2), std::move(a3), \ |
| 399 | std::move(a4), std::move(a5)); \ |
| 400 | return call.Marshal(primary_thread_); \ |
| 401 | } |
| 402 |
| 403 | // Define methods which should be invoked on the secondary thread. |
| 404 | #define PROXY_SECONDARY_METHOD0(r, method) \ |
| 405 | r method() override { \ |
| 406 | TRACE_BOILERPLATE(method); \ |
| 407 | MethodCall<C, r> call(c(), &C::method); \ |
| 408 | return call.Marshal(secondary_thread_); \ |
| 409 | } |
| 410 |
| 411 | #define PROXY_SECONDARY_CONSTMETHOD0(r, method) \ |
| 412 | r method() const override { \ |
| 413 | TRACE_BOILERPLATE(method); \ |
| 414 | ConstMethodCall<C, r> call(c(), &C::method); \ |
| 415 | return call.Marshal(secondary_thread_); \ |
| 416 | } |
| 417 |
| 418 | #define PROXY_SECONDARY_METHOD1(r, method, t1) \ |
| 419 | r method(t1 a1) override { \ |
| 420 | TRACE_BOILERPLATE(method); \ |
| 421 | MethodCall<C, r, t1> call(c(), &C::method, std::move(a1)); \ |
| 422 | return call.Marshal(secondary_thread_); \ |
| 423 | } |
| 424 |
| 425 | #define PROXY_SECONDARY_CONSTMETHOD1(r, method, t1) \ |
| 426 | r method(t1 a1) const override { \ |
| 427 | TRACE_BOILERPLATE(method); \ |
| 428 | ConstMethodCall<C, r, t1> call(c(), &C::method, std::move(a1)); \ |
| 429 | return call.Marshal(secondary_thread_); \ |
| 430 | } |
| 431 |
| 432 | #define PROXY_SECONDARY_METHOD2(r, method, t1, t2) \ |
| 433 | r method(t1 a1, t2 a2) override { \ |
| 434 | TRACE_BOILERPLATE(method); \ |
| 435 | MethodCall<C, r, t1, t2> call(c(), &C::method, std::move(a1), \ |
| 436 | std::move(a2)); \ |
| 437 | return call.Marshal(secondary_thread_); \ |
| 438 | } |
| 439 |
| 440 | #define PROXY_SECONDARY_CONSTMETHOD2(r, method, t1, t2) \ |
| 441 | r method(t1 a1, t2 a2) const override { \ |
| 442 | TRACE_BOILERPLATE(method); \ |
| 443 | ConstMethodCall<C, r, t1, t2> call(c(), &C::method, std::move(a1), \ |
| 444 | std::move(a2)); \ |
| 445 | return call.Marshal(secondary_thread_); \ |
| 446 | } |
| 447 |
| 448 | #define PROXY_SECONDARY_METHOD3(r, method, t1, t2, t3) \ |
| 449 | r method(t1 a1, t2 a2, t3 a3) override { \ |
| 450 | TRACE_BOILERPLATE(method); \ |
| 451 | MethodCall<C, r, t1, t2, t3> call(c(), &C::method, std::move(a1), \ |
| 452 | std::move(a2), std::move(a3)); \ |
| 453 | return call.Marshal(secondary_thread_); \ |
| 454 | } |
| 455 |
| 456 | #define PROXY_SECONDARY_CONSTMETHOD3(r, method, t1, t2) \ |
| 457 | r method(t1 a1, t2 a2, t3 a3) const override { \ |
| 458 | TRACE_BOILERPLATE(method); \ |
| 459 | ConstMethodCall<C, r, t1, t2, t3> call(c(), &C::method, std::move(a1), \ |
| 460 | std::move(a2), std::move(a3)); \ |
| 461 | return call.Marshal(secondary_thread_); \ |
| 462 | } |
| 463 |
| 464 | // For use when returning purely const state (set during construction). |
| 465 | // Use with caution. This method should only be used when the return value will |
| 466 | // always be the same. |
| 467 | #define BYPASS_PROXY_CONSTMETHOD0(r, method) \ |
| 468 | r method() const override { \ |
| 469 | TRACE_BOILERPLATE(method); \ |
| 470 | return c_->method(); \ |
| 471 | } |
| 472 | // Allows a custom implementation of a method where the otherwise proxied |
| 473 | // implementation can do a more efficient, yet thread-safe, job than the proxy |
| 474 | // can do by default or when more flexibility is needed than can be provided |
| 475 | // by a proxy. |
| 476 | // Note that calls to these methods should be expected to be made from unknown |
| 477 | // threads. |
| 478 | #define BYPASS_PROXY_METHOD0(r, method) \ |
| 479 | r method() override { \ |
| 480 | TRACE_BOILERPLATE(method); \ |
| 481 | return c_->method(); \ |
| 482 | } |
| 483 |
| 484 | // The 1 argument version of `BYPASS_PROXY_METHOD0`. |
| 485 | #define BYPASS_PROXY_METHOD1(r, method, t1) \ |
| 486 | r method(t1 a1) override { \ |
| 487 | TRACE_BOILERPLATE(method); \ |
| 488 | return c_->method(std::move(a1)); \ |
| 489 | } |
| 490 |
| 491 | // The 2 argument version of `BYPASS_PROXY_METHOD0`. |
| 492 | #define BYPASS_PROXY_METHOD2(r, method, t1, t2) \ |
| 493 | r method(t1 a1, t2 a2) override { \ |
| 494 | TRACE_BOILERPLATE(method); \ |
| 495 | return c_->method(std::move(a1), std::move(a2)); \ |
| 496 | } |
| 497 | } // namespace webrtc |
| 498 |
| 499 | #endif // PC_PROXY_H_ |
| 500 |