中国太空网站,网站建设费用能否计入广告费,赣州品牌网站建设,小说手机网站建设文章目录 基本流程状态回调类sdp的中媒体行pc对象 基本流程
webrtc native的接口#xff0c;主要就是围绕着PeerConnection对象#xff0c;一个PeerConnection对象它代表了一次音视频会话。 那么通过PeerConnection对象建立音视频通话#xff0c;包括如下步骤#xff1a; … 文章目录 基本流程状态回调类sdp的中媒体行pc对象 基本流程
webrtc native的接口主要就是围绕着PeerConnection对象一个PeerConnection对象它代表了一次音视频会话。 那么通过PeerConnection对象建立音视频通话包括如下步骤
创建PeerConnectionFactory通过工厂方法webrtc::CreatePeerConnectionFactory。创建PeerConnection通过它的PeerConnectionFactory的CreatePeerConnection方法。调用PeerConnectionFactory的CreateAudioTrack和CreateVideoTrack方法创建Track。调用PeerConncetion的 AddTrack方法添加Tracktrack最终会反映到sdp中的m行中。协商成功的Track在随后会通过onAddTrack回调告知应用层根据Track的类型来确定是回放视频还是音频。如果是本地为发起端则需调用PeerConnection的CreateOffer方法产生本地sdp信息(抽象为了webrtc::SessionDescriptionInterface**对象)触发 **CreateSessionDescriptionObserver**中 **OnSuccess方法在OnSuccess方法中调用PeerConnection的SetLocalDescription方法获取sdp字符串后通过信令协议给到远端。如果远端是发起在收到信令服务传来的远端sdp消息调用PeerConnection的SetRemoteDescription方法(需将sdp字符转换为webrtc::SessionDescriptionInterface对象)。然后调用PeerConnection的CreateAnswer方法(产生的local sdp应该还是会通过CreateSessionDescriptionObserver的OnSuccess回调给出最终还是通过PeerConnection的SetLocalDescription方法设置需要调试一下追踪流程)。
整个过程本质就是获取本地sdp信息和远端sdp信息再进行协商流程可以概括为如下图。
状态回调类
上述流程都是异步所以会有状态回调来告知应用状态。主要的两个Observer就是CreateSessionDescriptionObserver和PeerConnectionObserver前者是告知sdp创建协商的状态。后者是PC对象的状态。如下图。 Conductor类在examples/peerconnection/client/conductor.h是webrtc native的pc对象封装示例代码。 实现一个pc client可以参照它的实现首先是要继承CreateSessionDescriptionObserver和PeerConnectionObserver再是有PeerConnectionInterface(PC对象)和PeerConnectionFactoryInterface(用于创建PC对象)的成员变量。
sdp的中媒体行
sdp中核心的信息就是描述媒体信息的内容简称m行或媒体行。通过pc对象的AddTrack或AddTransceiver方法添加track会直接反映到sdp中。
如下代码添加了两个VideoTrack最终反映到sdp中为两个sendrecv的m行。
rtc::scoped_refptrCapturerTrackSource video_device CapturerTrackSource::Create();if (video_device) {rtc::scoped_refptrwebrtc::VideoTrackInterface video_track_(peer_connection_factory_-CreateVideoTrack(kVideoLabel, video_device));main_wnd_-StartLocalRenderer(video_track_);result_or_error peer_connection_-AddTrack(video_track_, {kStreamId});if (!result_or_error.ok()) {RTC_LOG(LS_ERROR) Failed to add video track to PeerConnection: result_or_error.error().message();}//添加第二个video trackrtc::scoped_refptrwebrtc::VideoTrackInterface video_track_1(peer_connection_factory_-CreateVideoTrack(video_track_1, video_device));result_or_error peer_connection_-AddTrack(video_track_1, {kStreamId});if (!result_or_error.ok()) {RTC_LOG(LS_ERROR) Failed to add video track to PeerConnection: result_or_error.error().message();}} else {RTC_LOG(LS_ERROR) OpenVideoCaptureDevice failed;}如下代码添加了一个VieoTrack方向为recvonly。
//video recvonly
webrtc::RtpTransceiverInit init;
init.direction webrtc::RtpTransceiverDirection::kRecvOnly;peer_connection_-AddTransceiver(cricket::MediaType::MEDIA_TYPE_VIDEO, init);pc对象
一个PC对象表示一次P2P会话它包括sdp handlecall。可以产生多个PC对象PC对象之间相互不关联。 std::unique_ptrSdpOfferAnswerHandler sdp_handler_;用以处理sdp协商。 std::unique_ptrCall call_;管理PC sdp中对应的stream如下类图为它提供的核心方法 一个PC对象中video/audio send/receive stream都被webrtc::interal::call对象管理也是通过它的接口进行创建。
sdp中一个m行对应一个RtpTransceiver对象最终被映射成MediaChannel和 video/audio的各种send/receive stream(比如webrtc::internal::VideoSendStream)而这些stream就是直接管理编解码器。