blob: 385992e65943b37dd419e4112cc6669a46f013e4 [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
15//
16// Example usage:
17//
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018// class TestInterface : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019// public:
20// std::string FooA() = 0;
21// std::string FooB(bool arg1) const = 0;
nisse72c8d2b2016-04-15 03:49:07 -070022// std::string FooC(bool arg1) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023// };
24//
25// Note that return types can not be a const reference.
26//
27// class Test : public TestInterface {
28// ... implementation of the interface.
29// };
30//
31// BEGIN_PROXY_MAP(Test)
deadbeefd99a2002017-01-18 08:55:23 -080032// PROXY_SIGNALING_THREAD_DESTRUCTOR()
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033// PROXY_METHOD0(std::string, FooA)
34// PROXY_CONSTMETHOD1(std::string, FooB, arg1)
nisse72c8d2b2016-04-15 03:49:07 -070035// PROXY_WORKER_METHOD1(std::string, FooC, arg1)
deadbeefd99a2002017-01-18 08:55:23 -080036// END_PROXY_MAP()
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037//
deadbeefd99a2002017-01-18 08:55:23 -080038// Where the destructor and first two methods are invoked on the signaling
39// thread, and the third is invoked on the worker thread.
nisse72c8d2b2016-04-15 03:49:07 -070040//
41// The proxy can be created using
42//
43// TestProxy::Create(Thread* signaling_thread, Thread* worker_thread,
44// TestInterface*).
45//
46// The variant defined with BEGIN_SIGNALING_PROXY_MAP is unaware of
47// the worker thread, and invokes all methods on the signaling thread.
deadbeefd99a2002017-01-18 08:55:23 -080048//
49// The variant defined with BEGIN_OWNED_PROXY_MAP does not use
50// refcounting, and instead just takes ownership of the object being proxied.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#ifndef API_PROXY_H_
53#define API_PROXY_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054
kwibergd1fe2812016-04-27 06:47:29 -070055#include <memory>
Yves Gerey3e707812018-11-28 16:47:49 +010056#include <string>
Steve Antonc3639822019-11-26 15:27:50 -080057#include <tuple>
oprypin803dc292017-02-01 01:55:59 -080058#include <utility>
kwibergd1fe2812016-04-27 06:47:29 -070059
Mirko Bonadeid9708072019-01-25 20:26:48 +010060#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020061#include "rtc_base/event.h"
Steve Anton10542f22019-01-11 09:11:00 -080062#include "rtc_base/message_handler.h"
Steve Anton10542f22019-01-11 09:11:00 -080063#include "rtc_base/ref_counted_object.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020064#include "rtc_base/system/rtc_export.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020065#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
Yves Gerey3e707812018-11-28 16:47:49 +010067namespace rtc {
68class Location;
69}
70
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071namespace webrtc {
72
Guido Urdanetaccab06f2020-01-15 11:30:29 +000073template <typename R>
74class ReturnType {
75 public:
76 template <typename C, typename M, typename... Args>
77 void Invoke(C* c, M m, Args&&... args) {
78 r_ = (c->*m)(std::forward<Args>(args)...);
79 }
80
81 R moved_result() { return std::move(r_); }
82
83 private:
84 R r_;
85};
86
87template <>
88class ReturnType<void> {
89 public:
90 template <typename C, typename M, typename... Args>
91 void Invoke(C* c, M m, Args&&... args) {
92 (c->*m)(std::forward<Args>(args)...);
93 }
94
95 void moved_result() {}
96};
97
98namespace internal {
99
100class RTC_EXPORT SynchronousMethodCall : public rtc::MessageData,
101 public rtc::MessageHandler {
102 public:
103 explicit SynchronousMethodCall(rtc::MessageHandler* proxy);
104 ~SynchronousMethodCall() override;
105
106 void Invoke(const rtc::Location& posted_from, rtc::Thread* t);
107
108 private:
109 void OnMessage(rtc::Message*) override;
110
111 rtc::Event e_;
112 rtc::MessageHandler* proxy_;
113};
114
115} // namespace internal
116
117template <typename C, typename R, typename... Args>
118class MethodCall : public rtc::Message, public rtc::MessageHandler {
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(const rtc::Location& posted_from, rtc::Thread* t) {
127 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
128 return r_.moved_result();
129 }
130
131 private:
132 void OnMessage(rtc::Message*) { Invoke(std::index_sequence_for<Args...>()); }
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};
144
145template <typename C, typename R, typename... Args>
146class ConstMethodCall : public rtc::Message, public rtc::MessageHandler {
147 public:
148 typedef R (C::*Method)(Args...) const;
149 ConstMethodCall(const C* c, Method m, Args&&... args)
150 : c_(c),
151 m_(m),
152 args_(std::forward_as_tuple(std::forward<Args>(args)...)) {}
153
154 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
155 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
156 return r_.moved_result();
157 }
158
159 private:
160 void OnMessage(rtc::Message*) { Invoke(std::index_sequence_for<Args...>()); }
161
162 template <size_t... Is>
163 void Invoke(std::index_sequence<Is...>) {
164 r_.Invoke(c_, m_, std::move(std::get<Is>(args_))...);
165 }
166
167 const C* c_;
168 Method m_;
169 ReturnType<R> r_;
170 std::tuple<Args&&...> args_;
171};
172
deadbeefd99a2002017-01-18 08:55:23 -0800173// Helper macros to reduce code duplication.
deadbeefe814a0d2017-02-25 18:15:09 -0800174#define PROXY_MAP_BOILERPLATE(c) \
175 template <class INTERNAL_CLASS> \
176 class c##ProxyWithInternal; \
177 typedef c##ProxyWithInternal<c##Interface> c##Proxy; \
178 template <class INTERNAL_CLASS> \
179 class c##ProxyWithInternal : public c##Interface { \
180 protected: \
181 typedef c##Interface C; \
182 \
183 public: \
184 const INTERNAL_CLASS* internal() const { return c_; } \
185 INTERNAL_CLASS* internal() { return c_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186
Yves Gerey665174f2018-06-19 15:03:05 +0200187// clang-format off
188// clang-format would put the semicolon alone,
189// leading to a presubmit error (cpplint.py)
oprypin803dc292017-02-01 01:55:59 -0800190#define END_PROXY_MAP() \
191 };
Yves Gerey665174f2018-06-19 15:03:05 +0200192// clang-format on
oprypin803dc292017-02-01 01:55:59 -0800193
deadbeefd99a2002017-01-18 08:55:23 -0800194#define SIGNALING_PROXY_MAP_BOILERPLATE(c) \
195 protected: \
196 c##ProxyWithInternal(rtc::Thread* signaling_thread, INTERNAL_CLASS* c) \
197 : signaling_thread_(signaling_thread), c_(c) {} \
198 \
199 private: \
200 mutable rtc::Thread* signaling_thread_;
201
202#define WORKER_PROXY_MAP_BOILERPLATE(c) \
203 protected: \
204 c##ProxyWithInternal(rtc::Thread* signaling_thread, \
205 rtc::Thread* worker_thread, INTERNAL_CLASS* c) \
206 : signaling_thread_(signaling_thread), \
207 worker_thread_(worker_thread), \
208 c_(c) {} \
209 \
210 private: \
211 mutable rtc::Thread* signaling_thread_; \
212 mutable rtc::Thread* worker_thread_;
213
214// Note that the destructor is protected so that the proxy can only be
215// destroyed via RefCountInterface.
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000216#define REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \
217 protected: \
218 ~c##ProxyWithInternal() { \
219 MethodCall<c##ProxyWithInternal, void> call( \
220 this, &c##ProxyWithInternal::DestroyInternal); \
221 call.Marshal(RTC_FROM_HERE, destructor_thread()); \
222 } \
223 \
224 private: \
225 void DestroyInternal() { c_ = nullptr; } \
deadbeefd99a2002017-01-18 08:55:23 -0800226 rtc::scoped_refptr<INTERNAL_CLASS> c_;
227
deadbeefe814a0d2017-02-25 18:15:09 -0800228// Note: This doesn't use a unique_ptr, because it intends to handle a corner
229// case where an object's deletion triggers a callback that calls back into
230// this proxy object. If relying on a unique_ptr to delete the object, its
231// inner pointer would be set to null before this reentrant callback would have
232// a chance to run, resulting in a segfault.
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000233#define OWNED_PROXY_MAP_BOILERPLATE(c) \
234 public: \
235 ~c##ProxyWithInternal() { \
236 MethodCall<c##ProxyWithInternal, void> call( \
237 this, &c##ProxyWithInternal::DestroyInternal); \
238 call.Marshal(RTC_FROM_HERE, destructor_thread()); \
239 } \
240 \
241 private: \
242 void DestroyInternal() { delete c_; } \
deadbeefe814a0d2017-02-25 18:15:09 -0800243 INTERNAL_CLASS* c_;
deadbeefd99a2002017-01-18 08:55:23 -0800244
245#define BEGIN_SIGNALING_PROXY_MAP(c) \
246 PROXY_MAP_BOILERPLATE(c) \
247 SIGNALING_PROXY_MAP_BOILERPLATE(c) \
248 REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \
249 public: \
250 static rtc::scoped_refptr<c##ProxyWithInternal> Create( \
251 rtc::Thread* signaling_thread, INTERNAL_CLASS* c) { \
252 return new rtc::RefCountedObject<c##ProxyWithInternal>(signaling_thread, \
253 c); \
254 }
255
256#define BEGIN_PROXY_MAP(c) \
257 PROXY_MAP_BOILERPLATE(c) \
258 WORKER_PROXY_MAP_BOILERPLATE(c) \
259 REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \
260 public: \
261 static rtc::scoped_refptr<c##ProxyWithInternal> Create( \
262 rtc::Thread* signaling_thread, rtc::Thread* worker_thread, \
263 INTERNAL_CLASS* c) { \
264 return new rtc::RefCountedObject<c##ProxyWithInternal>(signaling_thread, \
265 worker_thread, c); \
266 }
267
deadbeefe814a0d2017-02-25 18:15:09 -0800268#define BEGIN_OWNED_PROXY_MAP(c) \
269 PROXY_MAP_BOILERPLATE(c) \
270 WORKER_PROXY_MAP_BOILERPLATE(c) \
271 OWNED_PROXY_MAP_BOILERPLATE(c) \
272 public: \
273 static std::unique_ptr<c##Interface> Create( \
274 rtc::Thread* signaling_thread, rtc::Thread* worker_thread, \
275 std::unique_ptr<INTERNAL_CLASS> c) { \
276 return std::unique_ptr<c##Interface>(new c##ProxyWithInternal( \
277 signaling_thread, worker_thread, c.release())); \
deadbeefd99a2002017-01-18 08:55:23 -0800278 }
279
280#define PROXY_SIGNALING_THREAD_DESTRUCTOR() \
281 private: \
282 rtc::Thread* destructor_thread() const { return signaling_thread_; } \
283 \
oprypin803dc292017-02-01 01:55:59 -0800284 public: // NOLINTNEXTLINE
deadbeefd99a2002017-01-18 08:55:23 -0800285
286#define PROXY_WORKER_THREAD_DESTRUCTOR() \
287 private: \
288 rtc::Thread* destructor_thread() const { return worker_thread_; } \
289 \
oprypin803dc292017-02-01 01:55:59 -0800290 public: // NOLINTNEXTLINE
deadbeefd99a2002017-01-18 08:55:23 -0800291
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000292#define PROXY_METHOD0(r, method) \
293 r method() override { \
294 MethodCall<C, r> call(c_, &C::method); \
295 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000296 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000298#define PROXY_CONSTMETHOD0(r, method) \
299 r method() const override { \
300 ConstMethodCall<C, r> call(c_, &C::method); \
301 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000302 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000304#define PROXY_METHOD1(r, method, t1) \
305 r method(t1 a1) override { \
306 MethodCall<C, r, t1> call(c_, &C::method, std::move(a1)); \
307 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000308 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000310#define PROXY_CONSTMETHOD1(r, method, t1) \
311 r method(t1 a1) const override { \
312 ConstMethodCall<C, r, t1> call(c_, &C::method, std::move(a1)); \
313 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000314 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000316#define PROXY_METHOD2(r, method, t1, t2) \
317 r method(t1 a1, t2 a2) override { \
318 MethodCall<C, r, t1, t2> call(c_, &C::method, std::move(a1), \
319 std::move(a2)); \
320 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000321 }
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000322
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000323#define PROXY_METHOD3(r, method, t1, t2, t3) \
324 r method(t1 a1, t2 a2, t3 a3) override { \
325 MethodCall<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \
326 std::move(a2), std::move(a3)); \
327 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
deadbeefd99a2002017-01-18 08:55:23 -0800328 }
329
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000330#define PROXY_METHOD4(r, method, t1, t2, t3, t4) \
331 r method(t1 a1, t2 a2, t3 a3, t4 a4) override { \
332 MethodCall<C, r, t1, t2, t3, t4> call(c_, &C::method, std::move(a1), \
333 std::move(a2), std::move(a3), \
334 std::move(a4)); \
335 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
336 }
deadbeefd99a2002017-01-18 08:55:23 -0800337
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000338#define PROXY_METHOD5(r, method, t1, t2, t3, t4, t5) \
339 r method(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5) override { \
340 MethodCall<C, r, t1, t2, t3, t4, t5> call(c_, &C::method, std::move(a1), \
341 std::move(a2), std::move(a3), \
342 std::move(a4), std::move(a5)); \
343 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
344 }
nisse5b68ab52016-04-07 07:45:54 -0700345
346// Define methods which should be invoked on the worker thread.
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000347#define PROXY_WORKER_METHOD0(r, method) \
348 r method() override { \
349 MethodCall<C, r> call(c_, &C::method); \
350 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
351 }
nisse5b68ab52016-04-07 07:45:54 -0700352
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000353#define PROXY_WORKER_CONSTMETHOD0(r, method) \
354 r method() const override { \
355 ConstMethodCall<C, r> call(c_, &C::method); \
356 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
357 }
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000358
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000359#define PROXY_WORKER_METHOD1(r, method, t1) \
360 r method(t1 a1) override { \
361 MethodCall<C, r, t1> call(c_, &C::method, std::move(a1)); \
362 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
363 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000365#define PROXY_WORKER_CONSTMETHOD1(r, method, t1) \
366 r method(t1 a1) const override { \
367 ConstMethodCall<C, r, t1> call(c_, &C::method, std::move(a1)); \
368 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
369 }
deadbeefd99a2002017-01-18 08:55:23 -0800370
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000371#define PROXY_WORKER_METHOD2(r, method, t1, t2) \
372 r method(t1 a1, t2 a2) override { \
373 MethodCall<C, r, t1, t2> call(c_, &C::method, std::move(a1), \
374 std::move(a2)); \
375 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
376 }
deadbeefd99a2002017-01-18 08:55:23 -0800377
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000378#define PROXY_WORKER_CONSTMETHOD2(r, method, t1, t2) \
379 r method(t1 a1, t2 a2) const override { \
380 ConstMethodCall<C, r, t1, t2> call(c_, &C::method, std::move(a1), \
381 std::move(a2)); \
382 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
383 }
deadbeefe814a0d2017-02-25 18:15:09 -0800384
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000385#define PROXY_WORKER_METHOD3(r, method, t1, t2, t3) \
386 r method(t1 a1, t2 a2, t3 a3) override { \
387 MethodCall<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \
388 std::move(a2), std::move(a3)); \
389 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
390 }
Steve Antonc3639822019-11-26 15:27:50 -0800391
Guido Urdanetaccab06f2020-01-15 11:30:29 +0000392#define PROXY_WORKER_CONSTMETHOD3(r, method, t1, t2) \
393 r method(t1 a1, t2 a2, t3 a3) const override { \
394 ConstMethodCall<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \
395 std::move(a2), std::move(a3)); \
396 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
397 }
deadbeefd99a2002017-01-18 08:55:23 -0800398
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399} // namespace webrtc
400
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200401#endif // API_PROXY_H_