//创建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 {
//显示远程视频 }