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狀態的雙重驅動控制。