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_);
}
}
在展开的类里面MethodCall0
和Location
都是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_;}
也就是 PeerConnection
与PeerConnectionProxyWithInternal
父类PeerConnectionInterface
的关联过程。