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