ios中使用webrtc流程


//创建RTCPeerConnectionFactory

_factory = [[RTCPeerConnectionFactory alloc] init];

//创建peerconnection,其中config定义turnserver constraints定义//音视频配置信息

_peerConnection = [[RTCPeerConnection alloc]

                          initWithFactory:_factory

                          configuration:config                                  

                          constraints:constraints

                            delegate:self];

//创建本地音视频stream,并加入到peerconnection

//本地视频track创建完成后,预览本地视频

RTCMediaStream *localStream = [self createLocalMediaStream];

[_peerConnection addStream:localStream];

//如果为发起方,获取本地offer,并通过命令通道发给对方

[_peerConnection offerForConstraints:[

                      self defaultOfferConstraints]

completionHandler:^(

        RTCSessionDescription    *sdp,

NSError *error) {

//回调函数中将offer发给对方 

//从命令通道收到对方的answer消息,设置到peerconnection

[_peerConnection setRemoteDescription:sdpPreferringH264

                          completionHandler:^(NSError *error) {

                          //回调函数中判断设置结果,如有错提示用户 

//从命令通道收到对方的candidate消息,设置到peerconnection

[_peerConnection addIceCandidate:candidateMessage.candidate];

//peerconnection回调函数收到candidate,通过命令通道发送给对方

(void)peerConnection:(RTCPeerConnection *)peerConnection

didGenerateIceCandidate:(RTCIceCandidate *)candidate {

//candidate发送给对方 }

//peerconnection回调函数收到对方视频

(void)peerConnection:(RTCPeerConnection *)peerConnection

         didAddStream:(RTCMediaStream *)stream {

        //显示远程视频  



猜你喜欢

转载自blog.csdn.net/lipku/article/details/78925239