您的位置:首页 > 其它

WebRTC 学习笔记(5)--线程模型

2015-06-23 12:04 435 查看
1, 基本线程

webrtc中主要有两个线程:

signaling_thread-->处理小工作量方法。要求此线程内的方法都必须快速返回。

worker_thread-->处理大工作量的方法。此线程内的方法可能会处理很长时间,如网络消息等。

2, 线程实现

webrtc中的线程包含了一个消息队列。当需要在此线程中运行逻辑时,仅需要向线程对象发送一个消息即可。其中消息包含消息的处理类。



thread主要方法:
virtual void Post(MessageHandler *phandler, uint32 id = 0, MessageData *pdata = NULL, bool time_sensitive = false);

发送一个异步请求,调用会立即返回。

virtual void PostDelayed(int cmsDelay, MessageHandler *phandler, uint32 id = 0, MessageData *pdata = NULL);

发送一个异步请求,调用会立即返回,但是此请求会在cmsDelay后被处理。相当于集成了个计时器。

virtual void Send(MessageHandler *phandler, uint32 id = 0, MessageData *pdata = NULL);

发送一个同步请求,消息处理完成后调用才会返回。

3, 线程调用实现

3.1 发送一个消息。

thread->send()

thread->post()

3.2 调用Invoke

worker_thread_->Invoke<bool>(Bind(&MediaEngineInterface::SetOutputVolume, media_engine_, level));

以上调用表示在worker_thread_内同步调用media_engine_类的MediaEngineInterface::SetOutputVolume方法,方法有一个参数是level

3.3 用proxy宏

如下用PROXY宏定义对应类的proxy类,此proxy类的所有方法都会在woker_thread_线程内调用

BEGIN_PROXY_MAP(PeerConnection)

PROXY_METHOD0(talk_base::scoped_refptr<StreamCollectionInterface>,

local_streams)

PROXY_METHOD0(talk_base::scoped_refptr<StreamCollectionInterface>,

remote_streams)

PROXY_METHOD2(bool, AddStream, MediaStreamInterface*,

const MediaConstraintsInterface*)

PROXY_METHOD1(void, RemoveStream, MediaStreamInterface*)

PROXY_METHOD1(talk_base::scoped_refptr<DtmfSenderInterface>,

CreateDtmfSender, AudioTrackInterface*)

PROXY_METHOD2(bool, GetStats, StatsObserver*, MediaStreamTrackInterface*)

PROXY_METHOD2(talk_base::scoped_refptr<DataChannelInterface>,

CreateDataChannel, const std::string&, const DataChannelInit*)

PROXY_CONSTMETHOD0(const SessionDescriptionInterface*, local_description)

PROXY_CONSTMETHOD0(const SessionDescriptionInterface*, remote_description)

PROXY_METHOD2(void, CreateOffer, CreateSessionDescriptionObserver*,

const MediaConstraintsInterface*)

PROXY_METHOD2(void, CreateAnswer, CreateSessionDescriptionObserver*,

const MediaConstraintsInterface*)

PROXY_METHOD2(void, SetLocalDescription, SetSessionDescriptionObserver*,

SessionDescriptionInterface*)

PROXY_METHOD2(void, SetRemoteDescription, SetSessionDescriptionObserver*,

SessionDescriptionInterface*)

PROXY_METHOD2(bool, UpdateIce, const IceServers&,

const MediaConstraintsInterface*)

PROXY_METHOD1(bool, AddIceCandidate, const IceCandidateInterface*)

PROXY_METHOD0(SignalingState, signaling_state)

PROXY_METHOD0(IceState, ice_state)

PROXY_METHOD0(IceConnectionState, ice_connection_state)

PROXY_METHOD0(IceGatheringState, ice_gathering_state)

PROXY_METHOD0(void, Close)

END_PROXY()

PeerConnectionProxy(woker_thread_, peer_connection);

/article/9284681.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: