WebRtc 中的 Proxy

WebRtc中很多带有Proxy的类,字面上的意思看起来就是一个XX代理,这个代理与各种宏定义相互关联。因为各种宏定义,所以想要比较直观的看代理类与被代理类之间的关系是一件很痛苦的事。为此我以PeerConnectionProxy为例,将这种宏定义进行展开。
PeerConnectionProxy位于src/api/peer_connection_proxy.h文件里面。部分原文如下:

BEGIN_SIGNALING_PROXY_MAP(PeerConnection)
PROXY_SIGNALING_THREAD_DESTRUCTOR()
PROXY_METHOD0(rtc::scoped_refptr<StreamCollectionInterface>, local_streams)
END_PROXY_MAP()

因为原文件内容比较长,而且很多函数展开是类似的,所以选取典型的这四行进行展开。
展开内容如下:

template<class INTERNAL_CLASS>
class PeerConnectionProxyWithInternal;  
typedef PeerConnectionProxyWithInternal<PeerConnectionInterface> PeerConnectionProxy;
template <class INTERNAL_CLASS> 
class PeerConnectionProxyWithInternal : public PeerConnectionInterface{
  protected:
  typedef PeerConnectionInterface C;
  
  public:
  const INTERNAL_CLASS* internal()const {return c_;}
  INTERNAL_CLASS* internal() { return c_;}

  protected:
  PeerConnectionProxyWithInternal(rtc::Thread* signaling_thread, INTERNAL_CLASS* c)
: signaling_thread_(signaling_thread),  c_(c) {}  
  private:
  mutable rtc::Thread* signaling_thread_;

  protected:
  ~PeerConnectionProxyWithInternal(){
    MethodCall0<PeerConnectionProxyWithInternal, void> call(this, 
    &PeerConnectionProxyWithInternal::DestroyInternal);
    call.Marshal(RTC_FROM_HERE, destructor_thread());
  }

  private:
  void DestroyInternal() { c_ = nullptr; }   

  rtc::scoped_refptr<INTERNAL_CLASS> c_; 

  public:                                                                     
  static rtc::scoped_refptr<PeerConnectionProxyWithInternal> Create(                    
   rtc::Thread* signaling_thread, INTERNAL_CLASS* c) {                    
   return new rtc::RefCountedObject<PeerConnectionProxyWithInternal>(signaling_thread, c);               
  }
  
  private:                                                              
  rtc::Thread* destructor_thread() const { return signaling_thread_; } 
  public: 
rtc::scoped_refptr<StreamCollectionInterface> local_streams() override{
   MethodCall0<C, rtc::scoped_refptr<StreamCollectionInterface>> call(c_,  
          &C::local_streams);
   return call.Marshal( ::rtc::Location(function_name, 
                         __FILE__ ":" STRINGIZE(__LINE__)), signaling_thread_);
   }

}

在展开的类里面MethodCall0Location都是WebRtc 中定义的类。
其中c就是PeerConnection 在调用PeerConnectionProxy::Create(signaling_thread(), pc);被当作参数传入进来。调用过程:

rtc::scoped_refptr<PeerConnection> pc(
      new rtc::RefCountedObject<PeerConnection>(this, std::move(event_log),
                                                std::move(call)));
  ActionsBeforeInitializeForTesting(pc);
  if (!pc->Initialize(configuration, std::move(dependencies))) {
    return nullptr;
  }
  return PeerConnectionProxy::Create(signaling_thread(), pc);

从上面的展开代码中可以看到INTERNAL_CLASS* internal() { return c_;}也就是 PeerConnectionPeerConnectionProxyWithInternal父类PeerConnectionInterface的关联过程。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容