Adding "adapter" ORTC objects on top of ChannelManager/BaseChannel/etc.

This CL adds the following interfaces:
* RtpTransportController
* RtpTransport
* RtpSender
* RtpReceiver

They're implemented on top of the "BaseChannel" object, which is normally used
in a PeerConnection, and roughly corresponds to an SDP "m=" section. As a result
of this, there are several limitations:

* You can only have one of each type of sender and receiver (audio/video) on top
  of the same transport controller.
* The sender/receiver with the same media type must use the same RTP transport.
* You can't change the transport after creating the sender or receiver.
* Some of the parameters aren't supported.

Later, these "adapter" objects will be gradually replaced by real objects that don't
have these limitations, as "BaseChannel", "MediaChannel" and related code is
restructured. In this CL, we essentially have:

ORTC adapter objects -> BaseChannel -> Media engine
PeerConnection -> BaseChannel -> Media engine

And later we hope to have simply:

PeerConnection -> "Real" ORTC objects -> Media engine

See the linked bug for more context.

BUG=webrtc:7013
TBR=stefan@webrtc.org

Review-Url: https://codereview.webrtc.org/2675173003
Cr-Commit-Position: refs/heads/master@{#16842}
diff --git a/webrtc/api/proxy.h b/webrtc/api/proxy.h
index 5634cfe..46c424d 100644
--- a/webrtc/api/proxy.h
+++ b/webrtc/api/proxy.h
@@ -353,18 +353,18 @@
 
 
 // Helper macros to reduce code duplication.
-#define PROXY_MAP_BOILERPLATE(c)                                \
-  template <class INTERNAL_CLASS>                               \
-  class c##ProxyWithInternal;                                   \
-  typedef c##ProxyWithInternal<c##Interface> c##Proxy;          \
-  template <class INTERNAL_CLASS>                               \
-  class c##ProxyWithInternal : public c##Interface {            \
-   protected:                                                   \
-    typedef c##Interface C;                                     \
-                                                                \
-   public:                                                      \
-    const INTERNAL_CLASS* internal() const { return c_.get(); } \
-    INTERNAL_CLASS* internal() { return c_.get(); }
+#define PROXY_MAP_BOILERPLATE(c)                          \
+  template <class INTERNAL_CLASS>                         \
+  class c##ProxyWithInternal;                             \
+  typedef c##ProxyWithInternal<c##Interface> c##Proxy;    \
+  template <class INTERNAL_CLASS>                         \
+  class c##ProxyWithInternal : public c##Interface {      \
+   protected:                                             \
+    typedef c##Interface C;                               \
+                                                          \
+   public:                                                \
+    const INTERNAL_CLASS* internal() const { return c_; } \
+    INTERNAL_CLASS* internal() { return c_; }
 
 #define END_PROXY_MAP() \
   };
@@ -403,6 +403,11 @@
   void DestroyInternal() { c_ = nullptr; }             \
   rtc::scoped_refptr<INTERNAL_CLASS> c_;
 
+// Note: This doesn't use a unique_ptr, because it intends to handle a corner
+// case where an object's deletion triggers a callback that calls back into
+// this proxy object. If relying on a unique_ptr to delete the object, its
+// inner pointer would be set to null before this reentrant callback would have
+// a chance to run, resulting in a segfault.
 #define OWNED_PROXY_MAP_BOILERPLATE(c)                 \
  public:                                               \
   ~c##ProxyWithInternal() {                            \
@@ -412,8 +417,8 @@
   }                                                    \
                                                        \
  private:                                              \
-  void DestroyInternal() { c_.reset(nullptr); }        \
-  std::unique_ptr<INTERNAL_CLASS> c_;
+  void DestroyInternal() { delete c_; }                \
+  INTERNAL_CLASS* c_;
 
 #define BEGIN_SIGNALING_PROXY_MAP(c)                                         \
   PROXY_MAP_BOILERPLATE(c)                                                   \
@@ -438,16 +443,16 @@
                                                            worker_thread, c); \
   }
 
-#define BEGIN_OWNED_PROXY_MAP(c)                                       \
-  PROXY_MAP_BOILERPLATE(c)                                             \
-  WORKER_PROXY_MAP_BOILERPLATE(c)                                      \
-  OWNED_PROXY_MAP_BOILERPLATE(c)                                       \
- public:                                                               \
-  static std::unique_ptr<c##ProxyWithInternal> Create(                 \
-      rtc::Thread* signaling_thread, rtc::Thread* worker_thread,       \
-      INTERNAL_CLASS* c) {                                             \
-    return std::unique_ptr<c##ProxyWithInternal>(                      \
-        new c##ProxyWithInternal(signaling_thread, worker_thread, c)); \
+#define BEGIN_OWNED_PROXY_MAP(c)                                   \
+  PROXY_MAP_BOILERPLATE(c)                                         \
+  WORKER_PROXY_MAP_BOILERPLATE(c)                                  \
+  OWNED_PROXY_MAP_BOILERPLATE(c)                                   \
+ public:                                                           \
+  static std::unique_ptr<c##Interface> Create(                     \
+      rtc::Thread* signaling_thread, rtc::Thread* worker_thread,   \
+      std::unique_ptr<INTERNAL_CLASS> c) {                         \
+    return std::unique_ptr<c##Interface>(new c##ProxyWithInternal( \
+        signaling_thread, worker_thread, c.release()));            \
   }
 
 #define PROXY_SIGNALING_THREAD_DESTRUCTOR()                            \
@@ -464,95 +469,109 @@
 
 #define PROXY_METHOD0(r, method)                           \
   r method() override {                                    \
-    MethodCall0<C, r> call(c_.get(), &C::method);          \
+    MethodCall0<C, r> call(c_, &C::method);                \
     return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
   }
 
 #define PROXY_CONSTMETHOD0(r, method)                      \
   r method() const override {                              \
-    ConstMethodCall0<C, r> call(c_.get(), &C::method);     \
+    ConstMethodCall0<C, r> call(c_, &C::method);           \
     return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
   }
 
-#define PROXY_METHOD1(r, method, t1)                                 \
-  r method(t1 a1) override {                                         \
-    MethodCall1<C, r, t1> call(c_.get(), &C::method, std::move(a1)); \
-    return call.Marshal(RTC_FROM_HERE, signaling_thread_);           \
+#define PROXY_METHOD1(r, method, t1)                           \
+  r method(t1 a1) override {                                   \
+    MethodCall1<C, r, t1> call(c_, &C::method, std::move(a1)); \
+    return call.Marshal(RTC_FROM_HERE, signaling_thread_);     \
   }
 
-#define PROXY_CONSTMETHOD1(r, method, t1)                                 \
-  r method(t1 a1) const override {                                        \
-    ConstMethodCall1<C, r, t1> call(c_.get(), &C::method, std::move(a1)); \
-    return call.Marshal(RTC_FROM_HERE, signaling_thread_);                \
+#define PROXY_CONSTMETHOD1(r, method, t1)                           \
+  r method(t1 a1) const override {                                  \
+    ConstMethodCall1<C, r, t1> call(c_, &C::method, std::move(a1)); \
+    return call.Marshal(RTC_FROM_HERE, signaling_thread_);          \
   }
 
-#define PROXY_METHOD2(r, method, t1, t2)                                \
-  r method(t1 a1, t2 a2) override {                                     \
-    MethodCall2<C, r, t1, t2> call(c_.get(), &C::method, std::move(a1), \
-                                   std::move(a2));                      \
-    return call.Marshal(RTC_FROM_HERE, signaling_thread_);              \
+#define PROXY_METHOD2(r, method, t1, t2)                          \
+  r method(t1 a1, t2 a2) override {                               \
+    MethodCall2<C, r, t1, t2> call(c_, &C::method, std::move(a1), \
+                                   std::move(a2));                \
+    return call.Marshal(RTC_FROM_HERE, signaling_thread_);        \
   }
 
-#define PROXY_METHOD3(r, method, t1, t2, t3)                                \
-  r method(t1 a1, t2 a2, t3 a3) override {                                  \
-    MethodCall3<C, r, t1, t2, t3> call(c_.get(), &C::method, std::move(a1), \
-                                       std::move(a2), std::move(a3));       \
-    return call.Marshal(RTC_FROM_HERE, signaling_thread_);                  \
+#define PROXY_METHOD3(r, method, t1, t2, t3)                          \
+  r method(t1 a1, t2 a2, t3 a3) override {                            \
+    MethodCall3<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \
+                                       std::move(a2), std::move(a3)); \
+    return call.Marshal(RTC_FROM_HERE, signaling_thread_);            \
   }
 
 #define PROXY_METHOD4(r, method, t1, t2, t3, t4)                          \
   r method(t1 a1, t2 a2, t3 a3, t4 a4) override {                         \
-    MethodCall4<C, r, t1, t2, t3, t4> call(c_.get(), &C::method,          \
-                                           std::move(a1), std::move(a2),  \
-                                           std::move(a3), std::move(a4)); \
+    MethodCall4<C, r, t1, t2, t3, t4> call(c_, &C::method, std::move(a1), \
+                                           std::move(a2), std::move(a3),  \
+                                           std::move(a4));                \
     return call.Marshal(RTC_FROM_HERE, signaling_thread_);                \
   }
 
-#define PROXY_METHOD5(r, method, t1, t2, t3, t4, t5)                       \
-  r method(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5) override {                   \
-    MethodCall5<C, r, t1, t2, t3, t4, t5> call(                            \
-        c_.get(), &C::method, std::move(a1), std::move(a2), std::move(a3), \
-        std::move(a4), std::move(a5));                                     \
-    return call.Marshal(RTC_FROM_HERE, signaling_thread_);                 \
+#define PROXY_METHOD5(r, method, t1, t2, t3, t4, t5)                          \
+  r method(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5) override {                      \
+    MethodCall5<C, r, t1, t2, t3, t4, t5> call(c_, &C::method, std::move(a1), \
+                                               std::move(a2), std::move(a3),  \
+                                               std::move(a4), std::move(a5)); \
+    return call.Marshal(RTC_FROM_HERE, signaling_thread_);                    \
   }
 
 // Define methods which should be invoked on the worker thread.
 #define PROXY_WORKER_METHOD0(r, method)                 \
   r method() override {                                 \
-    MethodCall0<C, r> call(c_.get(), &C::method);       \
+    MethodCall0<C, r> call(c_, &C::method);             \
     return call.Marshal(RTC_FROM_HERE, worker_thread_); \
   }
 
 #define PROXY_WORKER_CONSTMETHOD0(r, method)            \
   r method() const override {                           \
-    ConstMethodCall0<C, r> call(c_.get(), &C::method);  \
+    ConstMethodCall0<C, r> call(c_, &C::method);        \
     return call.Marshal(RTC_FROM_HERE, worker_thread_); \
   }
 
-#define PROXY_WORKER_METHOD1(r, method, t1)                          \
-  r method(t1 a1) override {                                         \
-    MethodCall1<C, r, t1> call(c_.get(), &C::method, std::move(a1)); \
-    return call.Marshal(RTC_FROM_HERE, worker_thread_);              \
+#define PROXY_WORKER_METHOD1(r, method, t1)                    \
+  r method(t1 a1) override {                                   \
+    MethodCall1<C, r, t1> call(c_, &C::method, std::move(a1)); \
+    return call.Marshal(RTC_FROM_HERE, worker_thread_);        \
   }
 
-#define PROXY_WORKER_CONSTMETHOD1(r, method, t1)                          \
-  r method(t1 a1) const override {                                        \
-    ConstMethodCall1<C, r, t1> call(c_.get(), &C::method, std::move(a1)); \
-    return call.Marshal(RTC_FROM_HERE, worker_thread_);                   \
+#define PROXY_WORKER_CONSTMETHOD1(r, method, t1)                    \
+  r method(t1 a1) const override {                                  \
+    ConstMethodCall1<C, r, t1> call(c_, &C::method, std::move(a1)); \
+    return call.Marshal(RTC_FROM_HERE, worker_thread_);             \
   }
 
-#define PROXY_WORKER_METHOD2(r, method, t1, t2)                         \
-  r method(t1 a1, t2 a2) override {                                     \
-    MethodCall2<C, r, t1, t2> call(c_.get(), &C::method, std::move(a1), \
-                                   std::move(a2));                      \
-    return call.Marshal(RTC_FROM_HERE, worker_thread_);                 \
+#define PROXY_WORKER_METHOD2(r, method, t1, t2)                   \
+  r method(t1 a1, t2 a2) override {                               \
+    MethodCall2<C, r, t1, t2> call(c_, &C::method, std::move(a1), \
+                                   std::move(a2));                \
+    return call.Marshal(RTC_FROM_HERE, worker_thread_);           \
   }
 
-#define PROXY_WORKER_CONSTMETHOD2(r, method, t1, t2)                         \
-  r method(t1 a1, t2 a2) const override {                                    \
-    ConstMethodCall2<C, r, t1, t2> call(c_.get(), &C::method, std::move(a1), \
-                                        std::move(a2));                      \
-    return call.Marshal(RTC_FROM_HERE, worker_thread_);                      \
+#define PROXY_WORKER_CONSTMETHOD2(r, method, t1, t2)                   \
+  r method(t1 a1, t2 a2) const override {                              \
+    ConstMethodCall2<C, r, t1, t2> call(c_, &C::method, std::move(a1), \
+                                        std::move(a2));                \
+    return call.Marshal(RTC_FROM_HERE, worker_thread_);                \
+  }
+
+#define PROXY_WORKER_METHOD3(r, method, t1, t2, t3)                   \
+  r method(t1 a1, t2 a2, t3 a3) override {                            \
+    MethodCall3<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \
+                                       std::move(a2), std::move(a3)); \
+    return call.Marshal(RTC_FROM_HERE, worker_thread_);               \
+  }
+
+#define PROXY_WORKER_CONSTMETHOD3(r, method, t1, t2)                       \
+  r method(t1 a1, t2 a2, t3 a3) const override {                           \
+    ConstMethodCall3<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \
+                                            std::move(a2), std::move(a3)); \
+    return call.Marshal(RTC_FROM_HERE, worker_thread_);                    \
   }
 
 }  // namespace webrtc