blob: 5e730d9b5f37a1f094d02e28e24118347be070d1 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11// This file contains Macros for creating proxies for webrtc MediaStream and
12// PeerConnection classes.
deadbeefb10f32f2017-02-08 01:38:21 -080013// TODO(deadbeef): Move this to pc/; this is part of the implementation.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +010015// 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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022//
23// Example usage:
24//
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000025// class TestInterface : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026// public:
27// std::string FooA() = 0;
28// std::string FooB(bool arg1) const = 0;
nisse72c8d2b2016-04-15 03:49:07 -070029// std::string FooC(bool arg1) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030// };
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)
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +010039// PROXY_PRIMARY_THREAD_DESTRUCTOR()
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040// PROXY_METHOD0(std::string, FooA)
41// PROXY_CONSTMETHOD1(std::string, FooB, arg1)
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +010042// PROXY_SECONDARY_METHOD1(std::string, FooC, arg1)
deadbeefd99a2002017-01-18 08:55:23 -080043// END_PROXY_MAP()
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044//
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +010045// Where the destructor and first two methods are invoked on the primary
46// thread, and the third is invoked on the secondary thread.
nisse72c8d2b2016-04-15 03:49:07 -070047//
48// The proxy can be created using
49//
50// TestProxy::Create(Thread* signaling_thread, Thread* worker_thread,
51// TestInterface*).
52//
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +010053// The variant defined with BEGIN_PRIMARY_PROXY_MAP is unaware of
54// the secondary thread, and invokes all methods on the primary thread.
deadbeefd99a2002017-01-18 08:55:23 -080055//
56// The variant defined with BEGIN_OWNED_PROXY_MAP does not use
57// refcounting, and instead just takes ownership of the object being proxied.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#ifndef API_PROXY_H_
60#define API_PROXY_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061
kwibergd1fe2812016-04-27 06:47:29 -070062#include <memory>
Yves Gerey3e707812018-11-28 16:47:49 +010063#include <string>
Steve Antonc3639822019-11-26 15:27:50 -080064#include <tuple>
Tomas Gunnarsson0ca13d92020-06-10 12:17:50 +020065#include <type_traits>
oprypin803dc292017-02-01 01:55:59 -080066#include <utility>
kwibergd1fe2812016-04-27 06:47:29 -070067
Mirko Bonadeid9708072019-01-25 20:26:48 +010068#include "api/scoped_refptr.h"
Tomas Gunnarssonabdb4702020-09-05 18:43:36 +020069#include "api/task_queue/queued_task.h"
70#include "api/task_queue/task_queue_base.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020071#include "rtc_base/event.h"
Steve Anton10542f22019-01-11 09:11:00 -080072#include "rtc_base/message_handler.h"
Steve Anton10542f22019-01-11 09:11:00 -080073#include "rtc_base/ref_counted_object.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020074#include "rtc_base/system/rtc_export.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020075#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076
Yves Gerey3e707812018-11-28 16:47:49 +010077namespace rtc {
78class Location;
79}
80
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081namespace webrtc {
Tommi1cb796f2021-05-21 08:50:09 +020082namespace proxy_internal {
83RTC_EXPORT void TraceApiCall(const char* class_name, const char* method_name);
84} // namespace proxy_internal
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085
Guido Urdanetaccab06f2020-01-15 11:30:29 +000086template <typename R>
87class ReturnType {
88 public:
89 template <typename C, typename M, typename... Args>
90 void Invoke(C* c, M m, Args&&... args) {
91 r_ = (c->*m)(std::forward<Args>(args)...);
92 }
93
94 R moved_result() { return std::move(r_); }
95
96 private:
97 R r_;
98};
99
100template <>
101class ReturnType<void> {
102 public:
103 template <typename C, typename M, typename... Args>
104 void Invoke(C* c, M m, Args&&... args) {
105 (c->*m)(std::forward<Args>(args)...);
106 }
107
108 void moved_result() {}
109};
110
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000111template <typename C, typename R, typename... Args>
Tomas Gunnarssonabdb4702020-09-05 18:43:36 +0200112class MethodCall : public QueuedTask {
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000113 public:
114 typedef R (C::*Method)(Args...);
115 MethodCall(C* c, Method m, Args&&... args)
116 : c_(c),
117 m_(m),
118 args_(std::forward_as_tuple(std::forward<Args>(args)...)) {}
119
120 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
Tomas Gunnarssonabdb4702020-09-05 18:43:36 +0200121 if (t->IsCurrent()) {
122 Invoke(std::index_sequence_for<Args...>());
123 } else {
124 t->PostTask(std::unique_ptr<QueuedTask>(this));
125 event_.Wait(rtc::Event::kForever);
126 }
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000127 return r_.moved_result();
128 }
129
130 private:
Tomas Gunnarssonabdb4702020-09-05 18:43:36 +0200131 bool Run() override {
132 Invoke(std::index_sequence_for<Args...>());
133 event_.Set();
134 return false;
135 }
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000136
137 template <size_t... Is>
138 void Invoke(std::index_sequence<Is...>) {
139 r_.Invoke(c_, m_, std::move(std::get<Is>(args_))...);
140 }
141
142 C* c_;
143 Method m_;
144 ReturnType<R> r_;
145 std::tuple<Args&&...> args_;
Tomas Gunnarssonabdb4702020-09-05 18:43:36 +0200146 rtc::Event event_;
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000147};
148
149template <typename C, typename R, typename... Args>
Tomas Gunnarssonabdb4702020-09-05 18:43:36 +0200150class ConstMethodCall : public QueuedTask {
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000151 public:
152 typedef R (C::*Method)(Args...) const;
153 ConstMethodCall(const C* c, Method m, Args&&... args)
154 : c_(c),
155 m_(m),
156 args_(std::forward_as_tuple(std::forward<Args>(args)...)) {}
157
158 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
Tomas Gunnarssonabdb4702020-09-05 18:43:36 +0200159 if (t->IsCurrent()) {
160 Invoke(std::index_sequence_for<Args...>());
161 } else {
162 t->PostTask(std::unique_ptr<QueuedTask>(this));
163 event_.Wait(rtc::Event::kForever);
164 }
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000165 return r_.moved_result();
166 }
167
168 private:
Tomas Gunnarssonabdb4702020-09-05 18:43:36 +0200169 bool Run() override {
170 Invoke(std::index_sequence_for<Args...>());
171 event_.Set();
172 return false;
173 }
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000174
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_;
Tomas Gunnarssonabdb4702020-09-05 18:43:36 +0200184 rtc::Event event_;
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000185};
186
Tommi1cb796f2021-05-21 08:50:09 +0200187#define PROXY_STRINGIZE_IMPL(x) #x
188#define PROXY_STRINGIZE(x) PROXY_STRINGIZE_IMPL(x)
189
deadbeefd99a2002017-01-18 08:55:23 -0800190// Helper macros to reduce code duplication.
deadbeefe814a0d2017-02-25 18:15:09 -0800191#define PROXY_MAP_BOILERPLATE(c) \
192 template <class INTERNAL_CLASS> \
193 class c##ProxyWithInternal; \
194 typedef c##ProxyWithInternal<c##Interface> c##Proxy; \
195 template <class INTERNAL_CLASS> \
196 class c##ProxyWithInternal : public c##Interface { \
197 protected: \
198 typedef c##Interface C; \
Tommi1cb796f2021-05-21 08:50:09 +0200199 const char* class_name_ = PROXY_STRINGIZE(c); \
deadbeefe814a0d2017-02-25 18:15:09 -0800200 \
201 public: \
202 const INTERNAL_CLASS* internal() const { return c_; } \
203 INTERNAL_CLASS* internal() { return c_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204
Yves Gerey665174f2018-06-19 15:03:05 +0200205// clang-format off
206// clang-format would put the semicolon alone,
207// leading to a presubmit error (cpplint.py)
oprypin803dc292017-02-01 01:55:59 -0800208#define END_PROXY_MAP() \
209 };
Yves Gerey665174f2018-06-19 15:03:05 +0200210// clang-format on
oprypin803dc292017-02-01 01:55:59 -0800211
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100212#define PRIMARY_PROXY_MAP_BOILERPLATE(c) \
213 protected: \
214 c##ProxyWithInternal(rtc::Thread* primary_thread, INTERNAL_CLASS* c) \
215 : primary_thread_(primary_thread), c_(c) {} \
216 \
217 private: \
218 mutable rtc::Thread* primary_thread_;
219
220#define SECONDARY_PROXY_MAP_BOILERPLATE(c) \
deadbeefd99a2002017-01-18 08:55:23 -0800221 protected: \
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100222 c##ProxyWithInternal(rtc::Thread* primary_thread, \
223 rtc::Thread* secondary_thread, INTERNAL_CLASS* c) \
224 : primary_thread_(primary_thread), \
225 secondary_thread_(secondary_thread), \
226 c_(c) {} \
deadbeefd99a2002017-01-18 08:55:23 -0800227 \
228 private: \
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100229 mutable rtc::Thread* primary_thread_; \
230 mutable rtc::Thread* secondary_thread_;
deadbeefd99a2002017-01-18 08:55:23 -0800231
232// Note that the destructor is protected so that the proxy can only be
233// destroyed via RefCountInterface.
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000234#define REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \
235 protected: \
236 ~c##ProxyWithInternal() { \
237 MethodCall<c##ProxyWithInternal, void> call( \
238 this, &c##ProxyWithInternal::DestroyInternal); \
239 call.Marshal(RTC_FROM_HERE, destructor_thread()); \
240 } \
241 \
242 private: \
243 void DestroyInternal() { c_ = nullptr; } \
deadbeefd99a2002017-01-18 08:55:23 -0800244 rtc::scoped_refptr<INTERNAL_CLASS> c_;
245
deadbeefe814a0d2017-02-25 18:15:09 -0800246// Note: This doesn't use a unique_ptr, because it intends to handle a corner
247// case where an object's deletion triggers a callback that calls back into
248// this proxy object. If relying on a unique_ptr to delete the object, its
249// inner pointer would be set to null before this reentrant callback would have
250// a chance to run, resulting in a segfault.
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000251#define OWNED_PROXY_MAP_BOILERPLATE(c) \
252 public: \
253 ~c##ProxyWithInternal() { \
254 MethodCall<c##ProxyWithInternal, void> call( \
255 this, &c##ProxyWithInternal::DestroyInternal); \
256 call.Marshal(RTC_FROM_HERE, destructor_thread()); \
257 } \
258 \
259 private: \
260 void DestroyInternal() { delete c_; } \
deadbeefe814a0d2017-02-25 18:15:09 -0800261 INTERNAL_CLASS* c_;
deadbeefd99a2002017-01-18 08:55:23 -0800262
Tomas Gunnarssonc1d58912021-04-22 19:21:43 +0200263#define BEGIN_PRIMARY_PROXY_MAP(c) \
264 PROXY_MAP_BOILERPLATE(c) \
265 PRIMARY_PROXY_MAP_BOILERPLATE(c) \
266 REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \
267 public: \
268 static rtc::scoped_refptr<c##ProxyWithInternal> Create( \
269 rtc::Thread* primary_thread, INTERNAL_CLASS* c) { \
270 return rtc::make_ref_counted<c##ProxyWithInternal>(primary_thread, c); \
deadbeefd99a2002017-01-18 08:55:23 -0800271 }
272
Tomas Gunnarssonc1d58912021-04-22 19:21:43 +0200273#define BEGIN_PROXY_MAP(c) \
274 PROXY_MAP_BOILERPLATE(c) \
275 SECONDARY_PROXY_MAP_BOILERPLATE(c) \
276 REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \
277 public: \
278 static rtc::scoped_refptr<c##ProxyWithInternal> Create( \
279 rtc::Thread* primary_thread, rtc::Thread* secondary_thread, \
280 INTERNAL_CLASS* c) { \
281 return rtc::make_ref_counted<c##ProxyWithInternal>(primary_thread, \
282 secondary_thread, c); \
deadbeefd99a2002017-01-18 08:55:23 -0800283 }
284
deadbeefe814a0d2017-02-25 18:15:09 -0800285#define BEGIN_OWNED_PROXY_MAP(c) \
286 PROXY_MAP_BOILERPLATE(c) \
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100287 SECONDARY_PROXY_MAP_BOILERPLATE(c) \
deadbeefe814a0d2017-02-25 18:15:09 -0800288 OWNED_PROXY_MAP_BOILERPLATE(c) \
289 public: \
290 static std::unique_ptr<c##Interface> Create( \
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100291 rtc::Thread* primary_thread, rtc::Thread* secondary_thread, \
deadbeefe814a0d2017-02-25 18:15:09 -0800292 std::unique_ptr<INTERNAL_CLASS> c) { \
293 return std::unique_ptr<c##Interface>(new c##ProxyWithInternal( \
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100294 primary_thread, secondary_thread, c.release())); \
deadbeefd99a2002017-01-18 08:55:23 -0800295 }
296
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100297#define PROXY_PRIMARY_THREAD_DESTRUCTOR() \
298 private: \
299 rtc::Thread* destructor_thread() const { return primary_thread_; } \
300 \
301 public: // NOLINTNEXTLINE
302
303#define PROXY_SECONDARY_THREAD_DESTRUCTOR() \
deadbeefd99a2002017-01-18 08:55:23 -0800304 private: \
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100305 rtc::Thread* destructor_thread() const { return secondary_thread_; } \
deadbeefd99a2002017-01-18 08:55:23 -0800306 \
oprypin803dc292017-02-01 01:55:59 -0800307 public: // NOLINTNEXTLINE
deadbeefd99a2002017-01-18 08:55:23 -0800308
Tommi1cb796f2021-05-21 08:50:09 +0200309#define PROXY_METHOD0(r, method) \
310 r method() override { \
311 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
312 MethodCall<C, r> call(c_, &C::method); \
313 return call.Marshal(RTC_FROM_HERE, primary_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000314 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315
Tommi1cb796f2021-05-21 08:50:09 +0200316#define PROXY_CONSTMETHOD0(r, method) \
317 r method() const override { \
318 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
319 ConstMethodCall<C, r> call(c_, &C::method); \
320 return call.Marshal(RTC_FROM_HERE, primary_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000321 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322
Tommi1cb796f2021-05-21 08:50:09 +0200323#define PROXY_METHOD1(r, method, t1) \
324 r method(t1 a1) override { \
325 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
326 MethodCall<C, r, t1> call(c_, &C::method, std::move(a1)); \
327 return call.Marshal(RTC_FROM_HERE, primary_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000328 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329
Tommi1cb796f2021-05-21 08:50:09 +0200330#define PROXY_CONSTMETHOD1(r, method, t1) \
331 r method(t1 a1) const override { \
332 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
333 ConstMethodCall<C, r, t1> call(c_, &C::method, std::move(a1)); \
334 return call.Marshal(RTC_FROM_HERE, primary_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000335 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336
Tommi1cb796f2021-05-21 08:50:09 +0200337#define PROXY_METHOD2(r, method, t1, t2) \
338 r method(t1 a1, t2 a2) override { \
339 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
340 MethodCall<C, r, t1, t2> call(c_, &C::method, std::move(a1), \
341 std::move(a2)); \
342 return call.Marshal(RTC_FROM_HERE, primary_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000343 }
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000344
Tommi1cb796f2021-05-21 08:50:09 +0200345#define PROXY_METHOD3(r, method, t1, t2, t3) \
346 r method(t1 a1, t2 a2, t3 a3) override { \
347 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
348 MethodCall<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \
349 std::move(a2), std::move(a3)); \
350 return call.Marshal(RTC_FROM_HERE, primary_thread_); \
deadbeefd99a2002017-01-18 08:55:23 -0800351 }
352
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000353#define PROXY_METHOD4(r, method, t1, t2, t3, t4) \
354 r method(t1 a1, t2 a2, t3 a3, t4 a4) override { \
Tommi1cb796f2021-05-21 08:50:09 +0200355 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000356 MethodCall<C, r, t1, t2, t3, t4> call(c_, &C::method, std::move(a1), \
357 std::move(a2), std::move(a3), \
358 std::move(a4)); \
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100359 return call.Marshal(RTC_FROM_HERE, primary_thread_); \
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000360 }
deadbeefd99a2002017-01-18 08:55:23 -0800361
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000362#define PROXY_METHOD5(r, method, t1, t2, t3, t4, t5) \
363 r method(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5) override { \
Tommi1cb796f2021-05-21 08:50:09 +0200364 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000365 MethodCall<C, r, t1, t2, t3, t4, t5> call(c_, &C::method, std::move(a1), \
366 std::move(a2), std::move(a3), \
367 std::move(a4), std::move(a5)); \
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100368 return call.Marshal(RTC_FROM_HERE, primary_thread_); \
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000369 }
nisse5b68ab52016-04-07 07:45:54 -0700370
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100371// Define methods which should be invoked on the secondary thread.
Tommi1cb796f2021-05-21 08:50:09 +0200372#define PROXY_SECONDARY_METHOD0(r, method) \
373 r method() override { \
374 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
375 MethodCall<C, r> call(c_, &C::method); \
376 return call.Marshal(RTC_FROM_HERE, secondary_thread_); \
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000377 }
nisse5b68ab52016-04-07 07:45:54 -0700378
Tommi1cb796f2021-05-21 08:50:09 +0200379#define PROXY_SECONDARY_CONSTMETHOD0(r, method) \
380 r method() const override { \
381 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
382 ConstMethodCall<C, r> call(c_, &C::method); \
383 return call.Marshal(RTC_FROM_HERE, secondary_thread_); \
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000384 }
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000385
Tommi1cb796f2021-05-21 08:50:09 +0200386#define PROXY_SECONDARY_METHOD1(r, method, t1) \
387 r method(t1 a1) override { \
388 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
389 MethodCall<C, r, t1> call(c_, &C::method, std::move(a1)); \
390 return call.Marshal(RTC_FROM_HERE, secondary_thread_); \
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000391 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392
Tommi1cb796f2021-05-21 08:50:09 +0200393#define PROXY_SECONDARY_CONSTMETHOD1(r, method, t1) \
394 r method(t1 a1) const override { \
395 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
396 ConstMethodCall<C, r, t1> call(c_, &C::method, std::move(a1)); \
397 return call.Marshal(RTC_FROM_HERE, secondary_thread_); \
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000398 }
deadbeefd99a2002017-01-18 08:55:23 -0800399
Tommi1cb796f2021-05-21 08:50:09 +0200400#define PROXY_SECONDARY_METHOD2(r, method, t1, t2) \
401 r method(t1 a1, t2 a2) override { \
402 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
403 MethodCall<C, r, t1, t2> call(c_, &C::method, std::move(a1), \
404 std::move(a2)); \
405 return call.Marshal(RTC_FROM_HERE, secondary_thread_); \
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000406 }
deadbeefd99a2002017-01-18 08:55:23 -0800407
Tommi1cb796f2021-05-21 08:50:09 +0200408#define PROXY_SECONDARY_CONSTMETHOD2(r, method, t1, t2) \
409 r method(t1 a1, t2 a2) const override { \
410 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
411 ConstMethodCall<C, r, t1, t2> call(c_, &C::method, std::move(a1), \
412 std::move(a2)); \
413 return call.Marshal(RTC_FROM_HERE, secondary_thread_); \
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000414 }
deadbeefe814a0d2017-02-25 18:15:09 -0800415
Tommi1cb796f2021-05-21 08:50:09 +0200416#define PROXY_SECONDARY_METHOD3(r, method, t1, t2, t3) \
417 r method(t1 a1, t2 a2, t3 a3) override { \
418 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
419 MethodCall<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \
420 std::move(a2), std::move(a3)); \
421 return call.Marshal(RTC_FROM_HERE, secondary_thread_); \
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000422 }
Steve Antonc3639822019-11-26 15:27:50 -0800423
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100424#define PROXY_SECONDARY_CONSTMETHOD3(r, method, t1, t2) \
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000425 r method(t1 a1, t2 a2, t3 a3) const override { \
Tommi1cb796f2021-05-21 08:50:09 +0200426 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000427 ConstMethodCall<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \
428 std::move(a2), std::move(a3)); \
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100429 return call.Marshal(RTC_FROM_HERE, secondary_thread_); \
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000430 }
deadbeefd99a2002017-01-18 08:55:23 -0800431
Tomas Gunnarsson0ca13d92020-06-10 12:17:50 +0200432// For use when returning purely const state (set during construction).
433// Use with caution. This method should only be used when the return value will
434// always be the same.
Taylor Brandstetterc88fe702020-08-03 16:36:16 -0700435#define BYPASS_PROXY_CONSTMETHOD0(r, method) \
436 r method() const override { \
Tommi1cb796f2021-05-21 08:50:09 +0200437 proxy_internal::TraceApiCall(class_name_, PROXY_STRINGIZE(method)); \
Taylor Brandstetterc88fe702020-08-03 16:36:16 -0700438 static_assert( \
439 std::is_same<r, rtc::Thread*>::value || !std::is_pointer<r>::value, \
440 "Type is a pointer"); \
441 static_assert(!std::is_reference<r>::value, "Type is a reference"); \
442 return c_->method(); \
Tomas Gunnarsson0ca13d92020-06-10 12:17:50 +0200443 }
444
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445} // namespace webrtc
446
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200447#endif // API_PROXY_H_