licode学习之erizo篇--WebrtcConnection
WebrtcConnection是erizo进行Webrtc交互的基础类
其主要成员有如下:
std::string connection_id_; //唯一的ID bool audio_enabled_; //如果主动发起请求,被createOffer函数赋值,否则被processRemote赋值,表示是否有音频交互 bool video_enabled_; //表示是否有视频交互 bool trickle_enabled_; //启用ice的trickle模式 bool slide_show_mode_; //没有用,应该是留作扩展或者是以前版本的残留 bool sending_;//发送数据开关 int bundle_;//ice 使用,是否使用合并端口收发数据 WebRtcConnectionEventListener* conn_event_listener_; //webrtc连接事件监听 IceConfig ice_config_;//ice配置 std::vector<RtpMap> rtp_mappings_; //支持的RtpMap,生成sdp使用 RtpExtensionProcessor extension_processor_; //支持的extension_processor,生成sdp使用 boost::condition_variable cond_; //没看到wait,只有notify,应该是被废弃的变量 std::shared_ptr<Transport> video_transport_, audio_transport_; //视频数据链路,音频数据链路 std::shared_ptr<Stats> stats_; //输出状态 WebRTCEvent global_state_; //webrtc事件状态枚举 boost::mutex update_state_mutex_; // boost::mutex event_listener_mutex_; std::shared_ptr<Worker> worker_; std::shared_ptr<IOWorker> io_worker_; std::vector<std::shared_ptr<MediaStream>> media_streams_; //这个connection使用的流 std::shared_ptr<SdpInfo> remote_sdp_; //对端的sdp std::shared_ptr<SdpInfo> local_sdp_; //本地的sdp bool audio_muted_; //应该是残留或者扩展,没看到有用 bool video_muted_; //应该是残留或者扩展,没看到有用 bool first_remote_sdp_processed_; //是否第一次处理sdp标识 std::unique_ptr<BandwidthDistributionAlgorithm> distributor_; //remb码率估计处理
从成员可以看出,webrtcconnection,主要控制的有链路transport,交互local_sdp remote_sdp, ice控制,事件监听回调,数据流media_streams。
先看交互流程
交互之主动发起流程:
交互之被动呼叫流程
提供一个webrtc终端(手机,谷歌浏览器,iphone)主动发起offer的例子
#include <WebRtcConnection.h> #include <thread/IOThreadPool.h> #include <thread/ThreadPool.h> using namespace erizo; class SendOfferEvtListener : public WebRtcConnectionEventListener { public: void notifyEvent(WebRTCEvent newEvent, const std::string& message, const std::string &stream_id) { switch (newEvent) { case CONN_GATHERED: std::string sdp = message; //send answer to the client break; } }; }; std::shared_ptr<WebRtcConnection> webrtcConn = nullptr; std::string remote_sdp; //webrtc client send offer, and erizo send answer void user_offer_sample_start() { std::shared_ptr<ThreadPool> workerPool = std::make_shared<ThreadPool>(2); std::shared_ptr<IOThreadPool> ioPool = std::make_shared<IOThreadPool>(2); std::string connid = "1"; IceConfig cfg;//you may need init the cfg value std::vector<RtpMap> rtp_mappings;//you may need to init the mappings std::vector<erizo::ExtMap> ext_mappings; //you may need to init the ext mappings WebRtcConnectionEventListener* listener = new SendOfferEvtListener; webrtcConn = std::make_shared<WebRtcConnection>(workerPool->getLessUsedWorker(), ioPool->getLessUsedIOWorker(), connid, cfg, rtp_mappings, ext_mappings, listener); } void user_offer_sample_recv_answer(const std::string& answerSdp) { webrtcConn->setRemoteSdp(answerSdp); remote_sdp = answerSdp; } void user_offer_sample_recv_candidate(const std::string& candidate) { //parse candidate std::string mid; //parse from candidate int mLineIndex; //parse from candidate std::string cand; //parse from candidate const std::string sdp = remote_sdp; sdp += "\r\na="; sdp += cand; webrtcConn->addRemoteCandidate(mid, mLineIndex, sdp); }
总结:erizo的webrtcconnection,其封装的交互流程,主要依托于webrtc的标准交互:offer,candidate,answer来进行。整体流程需要根据ice的相关事件进行驱动,实际上是程序控制与ice状态的双重驱动控制。