You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
8.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2013, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // This file contains Macros for creating proxies for webrtc MediaStream and
  28. // PeerConnection classes.
  29. //
  30. // Example usage:
  31. //
  32. // class TestInterface : public talk_base::RefCountInterface {
  33. // public:
  34. // std::string FooA() = 0;
  35. // std::string FooB(bool arg1) const = 0;
  36. // std::string FooC(bool arg1)= 0;
  37. // };
  38. //
  39. // Note that return types can not be a const reference.
  40. //
  41. // class Test : public TestInterface {
  42. // ... implementation of the interface.
  43. // };
  44. //
  45. // BEGIN_PROXY_MAP(Test)
  46. // PROXY_METHOD0(std::string, FooA)
  47. // PROXY_CONSTMETHOD1(std::string, FooB, arg1)
  48. // PROXY_METHOD1(std::string, FooC, arg1)
  49. // END_PROXY()
  50. //
  51. // The proxy can be created using TestProxy::Create(Thread*, TestInterface*).
  52. #ifndef TALK_APP_WEBRTC_PROXY_H_
  53. #define TALK_APP_WEBRTC_PROXY_H_
  54. #include "talk/base/thread.h"
  55. namespace webrtc {
  56. template <typename R>
  57. class ReturnType {
  58. public:
  59. template<typename C, typename M>
  60. void Invoke(C* c, M m) { r_ = (c->*m)(); }
  61. template<typename C, typename M, typename T1>
  62. void Invoke(C* c, M m, T1 a1) { r_ = (c->*m)(a1); }
  63. template<typename C, typename M, typename T1, typename T2>
  64. void Invoke(C* c, M m, T1 a1, T2 a2) { r_ = (c->*m)(a1, a2); }
  65. template<typename C, typename M, typename T1, typename T2, typename T3>
  66. void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3) { r_ = (c->*m)(a1, a2, a3); }
  67. R value() { return r_; }
  68. private:
  69. R r_;
  70. };
  71. template <>
  72. class ReturnType<void> {
  73. public:
  74. template<typename C, typename M>
  75. void Invoke(C* c, M m) { (c->*m)(); }
  76. template<typename C, typename M, typename T1>
  77. void Invoke(C* c, M m, T1 a1) { (c->*m)(a1); }
  78. template<typename C, typename M, typename T1, typename T2>
  79. void Invoke(C* c, M m, T1 a1, T2 a2) { (c->*m)(a1, a2); }
  80. template<typename C, typename M, typename T1, typename T2, typename T3>
  81. void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3) { (c->*m)(a1, a2, a3); }
  82. void value() {}
  83. };
  84. template <typename C, typename R>
  85. class MethodCall0 : public talk_base::Message,
  86. public talk_base::MessageHandler {
  87. public:
  88. typedef R (C::*Method)();
  89. MethodCall0(C* c, Method m) : c_(c), m_(m) {}
  90. R Marshal(talk_base::Thread* t) {
  91. t->Send(this, 0);
  92. return r_.value();
  93. }
  94. private:
  95. void OnMessage(talk_base::Message*) { r_.Invoke(c_, m_);}
  96. C* c_;
  97. Method m_;
  98. ReturnType<R> r_;
  99. };
  100. template <typename C, typename R>
  101. class ConstMethodCall0 : public talk_base::Message,
  102. public talk_base::MessageHandler {
  103. public:
  104. typedef R (C::*Method)() const;
  105. ConstMethodCall0(C* c, Method m) : c_(c), m_(m) {}
  106. R Marshal(talk_base::Thread* t) {
  107. t->Send(this, 0);
  108. return r_.value();
  109. }
  110. private:
  111. void OnMessage(talk_base::Message*) { r_.Invoke(c_, m_); }
  112. C* c_;
  113. Method m_;
  114. ReturnType<R> r_;
  115. };
  116. template <typename C, typename R, typename T1>
  117. class MethodCall1 : public talk_base::Message,
  118. public talk_base::MessageHandler {
  119. public:
  120. typedef R (C::*Method)(T1 a1);
  121. MethodCall1(C* c, Method m, T1 a1) : c_(c), m_(m), a1_(a1) {}
  122. R Marshal(talk_base::Thread* t) {
  123. t->Send(this, 0);
  124. return r_.value();
  125. }
  126. private:
  127. void OnMessage(talk_base::Message*) { r_.Invoke(c_, m_, a1_); }
  128. C* c_;
  129. Method m_;
  130. ReturnType<R> r_;
  131. T1 a1_;
  132. };
  133. template <typename C, typename R, typename T1>
  134. class ConstMethodCall1 : public talk_base::Message,
  135. public talk_base::MessageHandler {
  136. public:
  137. typedef R (C::*Method)(T1 a1) const;
  138. ConstMethodCall1(C* c, Method m, T1 a1) : c_(c), m_(m), a1_(a1) {}
  139. R Marshal(talk_base::Thread* t) {
  140. t->Send(this, 0);
  141. return r_.value();
  142. }
  143. private:
  144. void OnMessage(talk_base::Message*) { r_.Invoke(c_, m_, a1_); }
  145. C* c_;
  146. Method m_;
  147. ReturnType<R> r_;
  148. T1 a1_;
  149. };
  150. template <typename C, typename R, typename T1, typename T2>
  151. class MethodCall2 : public talk_base::Message,
  152. public talk_base::MessageHandler {
  153. public:
  154. typedef R (C::*Method)(T1 a1, T2 a2);
  155. MethodCall2(C* c, Method m, T1 a1, T2 a2) : c_(c), m_(m), a1_(a1), a2_(a2) {}
  156. R Marshal(talk_base::Thread* t) {
  157. t->Send(this, 0);
  158. return r_.value();
  159. }
  160. private:
  161. void OnMessage(talk_base::Message*) { r_.Invoke(c_, m_, a1_, a2_); }
  162. C* c_;
  163. Method m_;
  164. ReturnType<R> r_;
  165. T1 a1_;
  166. T2 a2_;
  167. };
  168. template <typename C, typename R, typename T1, typename T2, typename T3>
  169. class MethodCall3 : public talk_base::Message,
  170. public talk_base::MessageHandler {
  171. public:
  172. typedef R (C::*Method)(T1 a1, T2 a2, T3 a3);
  173. MethodCall3(C* c, Method m, T1 a1, T2 a2, T3 a3)
  174. : c_(c), m_(m), a1_(a1), a2_(a2), a3_(a3) {}
  175. R Marshal(talk_base::Thread* t) {
  176. t->Send(this, 0);
  177. return r_.value();
  178. }
  179. private:
  180. void OnMessage(talk_base::Message*) { r_.Invoke(c_, m_, a1_, a2_, a3_); }
  181. C* c_;
  182. Method m_;
  183. ReturnType<R> r_;
  184. T1 a1_;
  185. T2 a2_;
  186. T3 a3_;
  187. };
  188. #define BEGIN_PROXY_MAP(c) \
  189. class c##Proxy : public c##Interface {\
  190. protected:\
  191. typedef c##Interface C;\
  192. c##Proxy(talk_base::Thread* thread, C* c)\
  193. : owner_thread_(thread), \
  194. c_(c) {}\
  195. ~c##Proxy() {\
  196. MethodCall0<c##Proxy, void> call(this, &c##Proxy::Release_s);\
  197. call.Marshal(owner_thread_);\
  198. }\
  199. public:\
  200. static talk_base::scoped_refptr<C> Create(talk_base::Thread* thread, \
  201. C* c) {\
  202. return new talk_base::RefCountedObject<c##Proxy>(thread, c);\
  203. }\
  204. #define PROXY_METHOD0(r, method)\
  205. r method() OVERRIDE {\
  206. MethodCall0<C, r> call(c_.get(), &C::method);\
  207. return call.Marshal(owner_thread_);\
  208. }\
  209. #define PROXY_CONSTMETHOD0(r, method)\
  210. r method() const OVERRIDE {\
  211. ConstMethodCall0<C, r> call(c_.get(), &C::method);\
  212. return call.Marshal(owner_thread_);\
  213. }\
  214. #define PROXY_METHOD1(r, method, t1)\
  215. r method(t1 a1) OVERRIDE {\
  216. MethodCall1<C, r, t1> call(c_.get(), &C::method, a1);\
  217. return call.Marshal(owner_thread_);\
  218. }\
  219. #define PROXY_CONSTMETHOD1(r, method, t1)\
  220. r method(t1 a1) const OVERRIDE {\
  221. ConstMethodCall1<C, r, t1> call(c_.get(), &C::method, a1);\
  222. return call.Marshal(owner_thread_);\
  223. }\
  224. #define PROXY_METHOD2(r, method, t1, t2)\
  225. r method(t1 a1, t2 a2) OVERRIDE {\
  226. MethodCall2<C, r, t1, t2> call(c_.get(), &C::method, a1, a2);\
  227. return call.Marshal(owner_thread_);\
  228. }\
  229. #define PROXY_METHOD3(r, method, t1, t2, t3)\
  230. r method(t1 a1, t2 a2, t3 a3) OVERRIDE {\
  231. MethodCall3<C, r, t1, t2, t3> call(c_.get(), &C::method, a1, a2, a3);\
  232. return call.Marshal(owner_thread_);\
  233. }\
  234. #define END_PROXY() \
  235. private:\
  236. void Release_s() {\
  237. c_ = NULL;\
  238. }\
  239. mutable talk_base::Thread* owner_thread_;\
  240. talk_base::scoped_refptr<C> c_;\
  241. };\
  242. } // namespace webrtc
  243. #endif // TALK_APP_WEBRTC_PROXY_H_