| 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 API_PROXY_H_ |
| 60 | #define API_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/system/rtc_export.h" |
| 75 | #include "rtc_base/thread.h" |
| 76 |
| 77 | namespace rtc { |
| 78 | class Location; |
| 79 | } |
| 80 |
| 81 | namespace webrtc { |
| 82 |
| 83 | template <typename R> |
| 84 | class ReturnType { |
| 85 | public: |
| 86 | template <typename C, typename M, typename... Args> |
| 87 | void Invoke(C* c, M m, Args&&... args) { |
| 88 | r_ = (c->*m)(std::forward<Args>(args)...); |
| 89 | } |
| 90 |
| 91 | R moved_result() { return std::move(r_); } |
| 92 |
| 93 | private: |
| 94 | R r_; |
| 95 | }; |
| 96 |
| 97 | template <> |
| 98 | class ReturnType<void> { |
| 99 | public: |
| 100 | template <typename C, typename M, typename... Args> |
| 101 | void Invoke(C* c, M m, Args&&... args) { |
| 102 | (c->*m)(std::forward<Args>(args)...); |
| 103 | } |
| 104 |
| 105 | void moved_result() {} |
| 106 | }; |
| 107 |
| 108 | template <typename C, typename R, typename... Args> |
| 109 | class MethodCall : public QueuedTask { |
| 110 | public: |
| 111 | typedef R (C::*Method)(Args...); |
| 112 | MethodCall(C* c, Method m, Args&&... args) |
| 113 | : c_(c), |
| 114 | m_(m), |
| 115 | args_(std::forward_as_tuple(std::forward<Args>(args)...)) {} |
| 116 |
| 117 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 118 | if (t->IsCurrent()) { |
| 119 | Invoke(std::index_sequence_for<Args...>()); |
| 120 | } else { |
| 121 | t->PostTask(std::unique_ptr<QueuedTask>(this)); |
| 122 | event_.Wait(rtc::Event::kForever); |
| 123 | } |
| 124 | return r_.moved_result(); |
| 125 | } |
| 126 |
| 127 | private: |
| 128 | bool Run() override { |
| 129 | Invoke(std::index_sequence_for<Args...>()); |
| 130 | event_.Set(); |
| 131 | return false; |
| 132 | } |
| 133 |
| 134 | template <size_t... Is> |
| 135 | void Invoke(std::index_sequence<Is...>) { |
| 136 | r_.Invoke(c_, m_, std::move(std::get<Is>(args_))...); |
| 137 | } |
| 138 |
| 139 | C* c_; |
| 140 | Method m_; |
| 141 | ReturnType<R> r_; |
| 142 | std::tuple<Args&&...> args_; |
| 143 | rtc::Event event_; |
| 144 | }; |
| 145 |
| 146 | template <typename C, typename R, typename... Args> |
| 147 | class ConstMethodCall : public QueuedTask { |
| 148 | public: |
| 149 | typedef R (C::*Method)(Args...) const; |
| 150 | ConstMethodCall(const C* c, Method m, Args&&... args) |
| 151 | : c_(c), |
| 152 | m_(m), |
| 153 | args_(std::forward_as_tuple(std::forward<Args>(args)...)) {} |
| 154 |
| 155 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 156 | if (t->IsCurrent()) { |
| 157 | Invoke(std::index_sequence_for<Args...>()); |
| 158 | } else { |
| 159 | t->PostTask(std::unique_ptr<QueuedTask>(this)); |
| 160 | event_.Wait(rtc::Event::kForever); |
| 161 | } |
| 162 | return r_.moved_result(); |
| 163 | } |
| 164 |
| 165 | private: |
| 166 | bool Run() override { |
| 167 | Invoke(std::index_sequence_for<Args...>()); |
| 168 | event_.Set(); |
| 169 | return false; |
| 170 | } |
| 171 |
| 172 | template <size_t... Is> |
| 173 | void Invoke(std::index_sequence<Is...>) { |
| 174 | r_.Invoke(c_, m_, std::move(std::get<Is>(args_))...); |
| 175 | } |
| 176 |
| 177 | const C* c_; |
| 178 | Method m_; |
| 179 | ReturnType<R> r_; |
| 180 | std::tuple<Args&&...> args_; |
| 181 | rtc::Event event_; |
| 182 | }; |
| 183 |
| 184 | // Helper macros to reduce code duplication. |
| 185 | #define PROXY_MAP_BOILERPLATE(c) \ |
| 186 | template <class INTERNAL_CLASS> \ |
| 187 | class c##ProxyWithInternal; \ |
| 188 | typedef c##ProxyWithInternal<c##Interface> c##Proxy; \ |
| 189 | template <class INTERNAL_CLASS> \ |
| 190 | class c##ProxyWithInternal : public c##Interface { \ |
| 191 | protected: \ |
| 192 | typedef c##Interface C; \ |
| 193 | \ |
| 194 | public: \ |
| 195 | const INTERNAL_CLASS* internal() const { return c_; } \ |
| 196 | INTERNAL_CLASS* internal() { return c_; } |
| 197 |
| 198 | // clang-format off |
| 199 | // clang-format would put the semicolon alone, |
| 200 | // leading to a presubmit error (cpplint.py) |
| 201 | #define END_PROXY_MAP() \ |
| 202 | }; |
| 203 | // clang-format on |
| 204 |
| 205 | #define PRIMARY_PROXY_MAP_BOILERPLATE(c) \ |
| 206 | protected: \ |
| 207 | c##ProxyWithInternal(rtc::Thread* primary_thread, INTERNAL_CLASS* c) \ |
| 208 | : primary_thread_(primary_thread), c_(c) {} \ |
| 209 | \ |
| 210 | private: \ |
| 211 | mutable rtc::Thread* primary_thread_; |
| 212 |
| 213 | #define SECONDARY_PROXY_MAP_BOILERPLATE(c) \ |
| 214 | protected: \ |
| 215 | c##ProxyWithInternal(rtc::Thread* primary_thread, \ |
| 216 | rtc::Thread* secondary_thread, INTERNAL_CLASS* c) \ |
| 217 | : primary_thread_(primary_thread), \ |
| 218 | secondary_thread_(secondary_thread), \ |
| 219 | c_(c) {} \ |
| 220 | \ |
| 221 | private: \ |
| 222 | mutable rtc::Thread* primary_thread_; \ |
| 223 | mutable rtc::Thread* secondary_thread_; |
| 224 |
| 225 | // Note that the destructor is protected so that the proxy can only be |
| 226 | // destroyed via RefCountInterface. |
| 227 | #define REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \ |
| 228 | protected: \ |
| 229 | ~c##ProxyWithInternal() { \ |
| 230 | MethodCall<c##ProxyWithInternal, void> call( \ |
| 231 | this, &c##ProxyWithInternal::DestroyInternal); \ |
| 232 | call.Marshal(RTC_FROM_HERE, destructor_thread()); \ |
| 233 | } \ |
| 234 | \ |
| 235 | private: \ |
| 236 | void DestroyInternal() { c_ = nullptr; } \ |
| 237 | rtc::scoped_refptr<INTERNAL_CLASS> c_; |
| 238 |
| 239 | // Note: This doesn't use a unique_ptr, because it intends to handle a corner |
| 240 | // case where an object's deletion triggers a callback that calls back into |
| 241 | // this proxy object. If relying on a unique_ptr to delete the object, its |
| 242 | // inner pointer would be set to null before this reentrant callback would have |
| 243 | // a chance to run, resulting in a segfault. |
| 244 | #define OWNED_PROXY_MAP_BOILERPLATE(c) \ |
| 245 | public: \ |
| 246 | ~c##ProxyWithInternal() { \ |
| 247 | MethodCall<c##ProxyWithInternal, void> call( \ |
| 248 | this, &c##ProxyWithInternal::DestroyInternal); \ |
| 249 | call.Marshal(RTC_FROM_HERE, destructor_thread()); \ |
| 250 | } \ |
| 251 | \ |
| 252 | private: \ |
| 253 | void DestroyInternal() { delete c_; } \ |
| 254 | INTERNAL_CLASS* c_; |
| 255 |
| 256 | #define BEGIN_PRIMARY_PROXY_MAP(c) \ |
| 257 | PROXY_MAP_BOILERPLATE(c) \ |
| 258 | PRIMARY_PROXY_MAP_BOILERPLATE(c) \ |
| 259 | REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \ |
| 260 | public: \ |
| 261 | static rtc::scoped_refptr<c##ProxyWithInternal> Create( \ |
| 262 | rtc::Thread* primary_thread, INTERNAL_CLASS* c) { \ |
| 263 | return rtc::make_ref_counted<c##ProxyWithInternal>(primary_thread, c); \ |
| 264 | } |
| 265 |
| 266 | #define BEGIN_PROXY_MAP(c) \ |
| 267 | PROXY_MAP_BOILERPLATE(c) \ |
| 268 | SECONDARY_PROXY_MAP_BOILERPLATE(c) \ |
| 269 | REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \ |
| 270 | public: \ |
| 271 | static rtc::scoped_refptr<c##ProxyWithInternal> Create( \ |
| 272 | rtc::Thread* primary_thread, rtc::Thread* secondary_thread, \ |
| 273 | INTERNAL_CLASS* c) { \ |
| 274 | return rtc::make_ref_counted<c##ProxyWithInternal>(primary_thread, \ |
| 275 | secondary_thread, c); \ |
| 276 | } |
| 277 |
| 278 | #define BEGIN_OWNED_PROXY_MAP(c) \ |
| 279 | PROXY_MAP_BOILERPLATE(c) \ |
| 280 | SECONDARY_PROXY_MAP_BOILERPLATE(c) \ |
| 281 | OWNED_PROXY_MAP_BOILERPLATE(c) \ |
| 282 | public: \ |
| 283 | static std::unique_ptr<c##Interface> Create( \ |
| 284 | rtc::Thread* primary_thread, rtc::Thread* secondary_thread, \ |
| 285 | std::unique_ptr<INTERNAL_CLASS> c) { \ |
| 286 | return std::unique_ptr<c##Interface>(new c##ProxyWithInternal( \ |
| 287 | primary_thread, secondary_thread, c.release())); \ |
| 288 | } |
| 289 |
| 290 | #define PROXY_PRIMARY_THREAD_DESTRUCTOR() \ |
| 291 | private: \ |
| 292 | rtc::Thread* destructor_thread() const { return primary_thread_; } \ |
| 293 | \ |
| 294 | public: // NOLINTNEXTLINE |
| 295 |
| 296 | #define PROXY_SECONDARY_THREAD_DESTRUCTOR() \ |
| 297 | private: \ |
| 298 | rtc::Thread* destructor_thread() const { return secondary_thread_; } \ |
| 299 | \ |
| 300 | public: // NOLINTNEXTLINE |
| 301 |
| 302 | #define PROXY_METHOD0(r, method) \ |
| 303 | r method() override { \ |
| 304 | MethodCall<C, r> call(c_, &C::method); \ |
| 305 | return call.Marshal(RTC_FROM_HERE, primary_thread_); \ |
| 306 | } |
| 307 |
| 308 | #define PROXY_CONSTMETHOD0(r, method) \ |
| 309 | r method() const override { \ |
| 310 | ConstMethodCall<C, r> call(c_, &C::method); \ |
| 311 | return call.Marshal(RTC_FROM_HERE, primary_thread_); \ |
| 312 | } |
| 313 |
| 314 | #define PROXY_METHOD1(r, method, t1) \ |
| 315 | r method(t1 a1) override { \ |
| 316 | MethodCall<C, r, t1> call(c_, &C::method, std::move(a1)); \ |
| 317 | return call.Marshal(RTC_FROM_HERE, primary_thread_); \ |
| 318 | } |
| 319 |
| 320 | #define PROXY_CONSTMETHOD1(r, method, t1) \ |
| 321 | r method(t1 a1) const override { \ |
| 322 | ConstMethodCall<C, r, t1> call(c_, &C::method, std::move(a1)); \ |
| 323 | return call.Marshal(RTC_FROM_HERE, primary_thread_); \ |
| 324 | } |
| 325 |
| 326 | #define PROXY_METHOD2(r, method, t1, t2) \ |
| 327 | r method(t1 a1, t2 a2) override { \ |
| 328 | MethodCall<C, r, t1, t2> call(c_, &C::method, std::move(a1), \ |
| 329 | std::move(a2)); \ |
| 330 | return call.Marshal(RTC_FROM_HERE, primary_thread_); \ |
| 331 | } |
| 332 |
| 333 | #define PROXY_METHOD3(r, method, t1, t2, t3) \ |
| 334 | r method(t1 a1, t2 a2, t3 a3) override { \ |
| 335 | MethodCall<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \ |
| 336 | std::move(a2), std::move(a3)); \ |
| 337 | return call.Marshal(RTC_FROM_HERE, primary_thread_); \ |
| 338 | } |
| 339 |
| 340 | #define PROXY_METHOD4(r, method, t1, t2, t3, t4) \ |
| 341 | r method(t1 a1, t2 a2, t3 a3, t4 a4) override { \ |
| 342 | MethodCall<C, r, t1, t2, t3, t4> call(c_, &C::method, std::move(a1), \ |
| 343 | std::move(a2), std::move(a3), \ |
| 344 | std::move(a4)); \ |
| 345 | return call.Marshal(RTC_FROM_HERE, primary_thread_); \ |
| 346 | } |
| 347 |
| 348 | #define PROXY_METHOD5(r, method, t1, t2, t3, t4, t5) \ |
| 349 | r method(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5) override { \ |
| 350 | MethodCall<C, r, t1, t2, t3, t4, t5> call(c_, &C::method, std::move(a1), \ |
| 351 | std::move(a2), std::move(a3), \ |
| 352 | std::move(a4), std::move(a5)); \ |
| 353 | return call.Marshal(RTC_FROM_HERE, primary_thread_); \ |
| 354 | } |
| 355 |
| 356 | // Define methods which should be invoked on the secondary thread. |
| 357 | #define PROXY_SECONDARY_METHOD0(r, method) \ |
| 358 | r method() override { \ |
| 359 | MethodCall<C, r> call(c_, &C::method); \ |
| 360 | return call.Marshal(RTC_FROM_HERE, secondary_thread_); \ |
| 361 | } |
| 362 |
| 363 | #define PROXY_SECONDARY_CONSTMETHOD0(r, method) \ |
| 364 | r method() const override { \ |
| 365 | ConstMethodCall<C, r> call(c_, &C::method); \ |
| 366 | return call.Marshal(RTC_FROM_HERE, secondary_thread_); \ |
| 367 | } |
| 368 |
| 369 | #define PROXY_SECONDARY_METHOD1(r, method, t1) \ |
| 370 | r method(t1 a1) override { \ |
| 371 | MethodCall<C, r, t1> call(c_, &C::method, std::move(a1)); \ |
| 372 | return call.Marshal(RTC_FROM_HERE, secondary_thread_); \ |
| 373 | } |
| 374 |
| 375 | #define PROXY_SECONDARY_CONSTMETHOD1(r, method, t1) \ |
| 376 | r method(t1 a1) const override { \ |
| 377 | ConstMethodCall<C, r, t1> call(c_, &C::method, std::move(a1)); \ |
| 378 | return call.Marshal(RTC_FROM_HERE, secondary_thread_); \ |
| 379 | } |
| 380 |
| 381 | #define PROXY_SECONDARY_METHOD2(r, method, t1, t2) \ |
| 382 | r method(t1 a1, t2 a2) override { \ |
| 383 | MethodCall<C, r, t1, t2> call(c_, &C::method, std::move(a1), \ |
| 384 | std::move(a2)); \ |
| 385 | return call.Marshal(RTC_FROM_HERE, secondary_thread_); \ |
| 386 | } |
| 387 |
| 388 | #define PROXY_SECONDARY_CONSTMETHOD2(r, method, t1, t2) \ |
| 389 | r method(t1 a1, t2 a2) const override { \ |
| 390 | ConstMethodCall<C, r, t1, t2> call(c_, &C::method, std::move(a1), \ |
| 391 | std::move(a2)); \ |
| 392 | return call.Marshal(RTC_FROM_HERE, secondary_thread_); \ |
| 393 | } |
| 394 |
| 395 | #define PROXY_SECONDARY_METHOD3(r, method, t1, t2, t3) \ |
| 396 | r method(t1 a1, t2 a2, t3 a3) override { \ |
| 397 | MethodCall<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \ |
| 398 | std::move(a2), std::move(a3)); \ |
| 399 | return call.Marshal(RTC_FROM_HERE, secondary_thread_); \ |
| 400 | } |
| 401 |
| 402 | #define PROXY_SECONDARY_CONSTMETHOD3(r, method, t1, t2) \ |
| 403 | r method(t1 a1, t2 a2, t3 a3) const override { \ |
| 404 | ConstMethodCall<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \ |
| 405 | std::move(a2), std::move(a3)); \ |
| 406 | return call.Marshal(RTC_FROM_HERE, secondary_thread_); \ |
| 407 | } |
| 408 |
| 409 | // For use when returning purely const state (set during construction). |
| 410 | // Use with caution. This method should only be used when the return value will |
| 411 | // always be the same. |
| 412 | #define BYPASS_PROXY_CONSTMETHOD0(r, method) \ |
| 413 | r method() const override { \ |
| 414 | static_assert( \ |
| 415 | std::is_same<r, rtc::Thread*>::value || !std::is_pointer<r>::value, \ |
| 416 | "Type is a pointer"); \ |
| 417 | static_assert(!std::is_reference<r>::value, "Type is a reference"); \ |
| 418 | return c_->method(); \ |
| 419 | } |
| 420 |
| 421 | } // namespace webrtc |
| 422 |
| 423 | #endif // API_PROXY_H_ |
| 424 |