WebRTC发布流过程

发布流程

  • 初始化peer connection
_peerConnection = [_factory peerConnectionWithConfiguration:config
                                                    constraints:constraints
                                                       delegate:self];
  • create offer
    • 这里注册了回调函数
    [_peerConnection offerForConstraints:[self defaultPublishOfferConstraints]
                       completionHandler:^(RTCSessionDescription * _Nullable sdp, NSError * _Nullable error) {
                           WebRTCClient *strongSelf = weakSelf;
                           [strongSelf peerConnection:strongSelf.peerConnection didCreateSessionDescription:sdp error:error];
    }];
  • 回调函数
 (void)peerConnection:(RTCPeerConnection *)peerConnection didCreateSessionDescription:(RTCSessionDescription *)sdp
                 error:(NSError *)error {
//这里要判断error,是否成功

//设置本地sdp
[_peerConnection setLocalDescription:newSDP completionHandler:^(NSError * _Nullable error) {
//这里是回调函数
}];
}
  • 然后就开始收集本地的candidate
  • 收集结束回调
- (void)peerConnection:(RTCPeerConnection *)peerConnection
                didChangeIceGatheringState:(RTCIceGatheringState)newState {
    __weak RTCPeerConnection * pc = peerConnection;
    if (newState == RTCIceGatheringStateComplete) {
        RunOnRtcEngineKitQueue(^{
            if(pc != self.peerConnection){
                return;
            }

            NSTimeInterval nowtime = [[NSDate date] timeIntervalSince1970]*1000;
            NSInteger elapse = [[NSNumber numberWithDouble:nowtime] longLongValue] - setLocalDescriptionStart;
            RTCSessionDescription* sdp = [_peerConnection localDescription];
            LOGD(@"didChangeIceGatheringState:%@ %ld sdp:%@", _userId, (long)elapse, sdp.sdp);
            [self publish:sdp.sdp];
        }, NO);
    }
}

这里get到description,准备发给server
RTCSessionDescription* sdp = [_peerConnection localDescription];
通过信令转发给webRTC server
[self publish:sdp.sdp];

  • 收到信令转发的answer sdp
- (void)signalingChannel:(SignalingChannel *)channel ReceiveMessage:(NSDictionary *)message {
    if ([type isEqualToString:@"answer"]) {
        return [self processAnswer:internalMessage];
    }
}

- (void)processAnswer:(NSDictionary*) message {
    [_peerConnection setRemoteDescription:sdp
                        completionHandler:^(NSError * _Nullable error) {
    }];
}
  • 开始ice建联
    • 这里是ice过程的事件回调函数
- (void)peerConnection:(RTCPeerConnection *)peerConnection
                                didChangeIceConnectionState:(RTCIceConnectionState)newState {
//主要有checking、connected、completed事件
//当然还是disconnected、close
//当收到connected时,就说明建联成功,开始收发媒体数据了
}

你可能感兴趣的:(WebRTC发布流过程)