馥琳 发表于 2022-3-26 10:29:51

iOS下WebRTC音视频通话(二)-局域网内音视频通话

这里是iOS 下WebRTC音视频通话开发的第二篇,在这一篇会利用一个局域网内音视频通话的例子介绍WebRTC中常用的API。
如果你下载并编译完成之后,会看到一个iOS 版的WebRTC Demo。但是那个demo涉及到外网的通讯需要翻墙,而且还有对信令消息的封装理解起来非常的困难。
但是,我将要写的这个demo去掉了STUN服务器、TURN服务器配置,以及信令的包装,基本上是用WebRTC进行音视频通话的最精简主干了,非常容易理解。
准备

因为这个Demo用到了我之前写的另外两个工程:
一个XMPP聊天的Demo
音视频通话的UI效果视图
如果你对在本地搭建OpenFire服务以及开发一个基于XMPP的聊天小程序感兴趣
教程在这里:
XMPP系列(一):OpenFire环境搭建
XMPP系列(二)—-用户注册和用户登录功能
XMPP系列(三)—获取好友列表、添加好友
XMPP系列(四)—发送和接收文字消息,获取历史消息功能
XMPP系列(五)—文件传输
所以只需要下载上面两个工程,然后把一些控件合并下,然后配置好你的XMPP服务器的IP和端口号,就可以继续做音视频功能的开发了。
开始着手开发

首先我在聊天介绍导航栏上加了两个按钮【视频】【语音】(主要是太懒,不想在输入框做更多功能)。如下图:

然后为视频按钮添加点击事件,在这个点击事件里需要做几件事:
1、弹出一个拨打的界面。
2、播放拨打视频通话的声音。
3、做WebRTC的配置。
- (void)videoAction{    NSLog(@"%s",__func__);    ;}- (void)startCommunication:(BOOL)isVideo{    WebRTCClient *client = ;    ;    client.myJID = .xmppStream.myJID.full;    client.remoteJID = self.chatJID.full;    ;} 所有与WebRTC相关的操作都先封装在WebRTCClient内,然后再根据功能做拆分,后面你可以拆分到不同的类里。
下面开始介绍WebRTC的相关配置:
- (void)startEngine{//如果你需要安全一点,用到SSL验证,那就加上这句话。    ;    //set RTCPeerConnection's constraints    self.peerConnectionFactory = [ init];    NSArray *mandatoryConstraints = @[[ initWithKey:@"OfferToReceiveAudio" value:@"true"],                                    [ initWithKey:@"OfferToReceiveVideo" value:@"true"]                                    ];    NSArray *optionalConstraints = @[[ initWithKey:@"DtlsSrtpKeyAgreement" value:@"false"]];    self.pcConstraints = [ initWithMandatoryConstraints:mandatoryConstraints optionalConstraints:optionalConstraints];    //set SDP's Constraints in order to (offer/answer)    NSArray *sdpMandatoryConstraints = @[[ initWithKey:@"OfferToReceiveAudio" value:@"true"],                                       [ initWithKey:@"OfferToReceiveVideo" value:@"true"]                                       ];    self.sdpConstraints = [ initWithMandatoryConstraints:sdpMandatoryConstraints optionalConstraints:nil];    //set RTCVideoSource's(localVideoSource) constraints    self.videoConstraints = [ initWithMandatoryConstraints:nil optionalConstraints:nil];} 上面的三个约束,只有pcConstraints是必须的,其他的都不是必须的。这些约束主要是控制音视频的采集,以及PeerConnection的设置。
其他RTC相关的配置是在显示拨打界面后做的操作:
- (void)showRTCViewByRemoteName:(NSString *)remoteName isVideo:(BOOL)isVideo isCaller:(BOOL)isCaller{    // 1.显示视图    self.rtcView = [ initWithIsVideo:isVideo isCallee:!isCaller];    self.rtcView.nickName = remoteName;    self.rtcView.connectText = @"等待对方接听";    self.rtcView.netTipText = @"网络状况良好";    ;    // 2.播放声音    NSURL *audioURL;    if (isCaller) {      audioURL = [ URLForResource:@"AVChat_waitingForAnswer.mp3" withExtension:nil];    } else {      audioURL = [ URLForResource:@"AVChat_incoming.mp3" withExtension:nil];    }    _audioPlayer = [ initWithContentsOfURL:audioURL error:nil];    _audioPlayer.numberOfLoops = -1;    ;    ;    // 3.拨打时,禁止黑屏    .idleTimerDisabled = YES;    // 4.监听系统电话    ;    // 5.做RTC必要设置    if (isCaller) {      ;      // 如果是发起者,创建一个offer信令      ;    } else {      // 如果是接收者,就要处理信令信息,创建一个answer,但是设置和创建answer应该在点击接听后才开始      NSLog(@"如果是接收者,就要处理信令信息");      self.rtcView.connectText = isVideo ? @"视频通话":@"语音通话";    }} 上面的注释已经很明白了。主要内容在中。
* 1.已ICE服务器地址、pc约束、代理作为参数创建RTCPeerConnection对象。
self.peerConnection = ;

[*]2.创建本地多媒体流
RTCMediaStream *mediaStream = ;

[*]3.为多媒体流添加音频轨迹
RTCAudioTrack *localAudioTrack = ; ; 音频的采集,已经封装在peerConnectionFactory工厂内。
* 4.为多媒体流添加视频轨迹
RTCAVFoundationVideoSource *source = [ initWithFactory:self.peerConnectionFactory constraints:self.videoConstraints];RTCVideoTrack *localVideoTrack = [ initWithFactory:self.peerConnectionFactory source:source trackId:@"AVAMSv0"]; ; 随着WebRTC的更新,API也被替换了很多,现在视频的采集多了一个新的类RTCAVFoundationVideoSource
* 5.为视频流添加渲染视图
    RTCEAGLVideoView *localVideoView = [ initWithFrame:self.rtcView.ownImageView.bounds];    localVideoView.transform = CGAffineTransformMakeScale(-1, 1);    localVideoView.delegate = self;    ;    self.localVideoView = localVideoView;    // 添加渲染视图    ; RTCEAGLVideoView也是新增的一个视图类,我们可以直接将这个视图添加到某个视图上。
* 6.将多媒体流绑定到peerConnection上
; 至此发起方的RTC 设置完毕,只用在创建一个Offer,然后将Offer发送给对方。
* 7.创建Offer
;

[*]8.在createSession的回调里,为peerConnection设置localDescription,并发送信令给对方—(其实在setSession的代理方法中发送信令更合适,但是那样就得保存sdp,所以这里偷了个懒)。
- (void)peerConnection:(RTCPeerConnection *)peerConnectiondidCreateSessionDescription:(RTCSessionDescription *)sdp               error:(NSError *)error{    if (error) {      NSLog(@"创建SessionDescription 失败");#warning 这里创建 创建SessionDescription 失败    } else {      NSLog(@"创建SessionDescription 成功");      // 这里将SessionDescription转换成H264的sdp,因为默认的是V8格式的视频。      RTCSessionDescription *sdpH264 = ;      ;      NSDictionary *jsonDict = @{ @"type" : sdp.type, @"sdp" : sdp.description };      NSData *jsonData = ;      NSString *jsonStr = [ initWithData:jsonData encoding:NSUTF8StringEncoding];      // 将offer信令消息通过XMPP发送给对方      [ sendSignalingMessage:jsonStr toUser:self.remoteJID];    }}

[*]9.等待对方返回Answer信令消息,当接听后,发送Answer信令消息回来后,将其设置为peerConnection的RemoteDescription即可。然后RTC在处理完成后就开始像对方发送多媒体流啦。
补充:
RTCPeerConnection有很多个回调,他们分别是在不同的时机触发

在为peerConnection添加RTCMediaStream之后就会触发下面这个代理方法:
- (void)peerConnectionOnRenegotiationNeeded:(RTCPeerConnection *)peerConnection 设置完LocalDescription之后,ICE框架才会开始去进行流数据传输,才会触发下面这几个方法
- (void)peerConnection:(RTCPeerConnection *)peerConnection signalingStateChanged:(RTCSignalingState)stateChanged - (void)peerConnection:(RTCPeerConnection *)peerConnection   iceGatheringChanged:(RTCICEGatheringState)newState// 而局域网内,因为没有涉及到穿墙,这个代理方法并没有回调。 - (void)peerConnection:(RTCPeerConnection *)peerConnectioniceConnectionChanged:(RTCICEConnectionState)newState 其中各种不同的状态的枚举值含义,在这篇文中里有英文解释:中间部分有各种枚举值的解释
而搜索到ICECandidate之后,会回调:
- (void)peerConnection:(RTCPeerConnection *)peerConnection       gotICECandidate:(RTCICECandidate *)candidate 我们需要在上面这个回调中,将候选信息发送给对方,然后对方讲接收到的候选添加到peerConnection中。
完整代码:
- (void)peerConnection:(RTCPeerConnection *)peerConnection       gotICECandidate:(RTCICECandidate *)candidate{    if (self.HaveSentCandidate) {      return;    }    NSLog(@"新的 Ice candidate 被发现.");    NSDictionary *jsonDict = @{@"type":@"candidate",                               @"label":,                               @"id":candidate.sdpMid,                               @"sdp":candidate.sdp                               };    NSData *jsonData = ;    if (jsonData.length > 0) {      NSString *jsonStr = [ initWithData:jsonData encoding:NSUTF8StringEncoding];      [ sendSignalingMessage:jsonStr toUser:self.remoteJID];      self.HaveSentCandidate = YES;    }} 接收方

接收方在收到发起方通过XMPP发送过来的信令(可能会有Offer信令,Candidate信令,bye信令)后,先将其保存到数组中,同时展示音视频通话界面,并播放声音。
   这里需要注意:要将收到的Offer信令消息插入到第一个,Offer信令消息必须先处理。
当点击接听按钮时,初始化RTC的设置,即上面的方法。然后处理之前保存的信令消息。
- (void)acceptAction{    ;    ;    for (NSDictionary *dict in self.messages) {      ;    }    ;} 如何处理之前的信令消息呢?
处理Offer信令消息:
将收到的Offer信令设置为peerConnection的RemoteDescription,并创建一个Answer信令发送给对方。
处理Candidate信令消息
将收到的信令消息包装成RTCICECandidate对象,然后添加到peerConnection上。
具体代码:
- (void)processMessageDict:(NSDictionary *)dict{    NSString *type = dict[@"type"];    if () {      RTCSessionDescription *remoteSdp = [ initWithType:type sdp:dict[@"sdp"]];      ;      ;    } else if () {      RTCSessionDescription *remoteSdp = [ initWithType:type sdp:dict[@"sdp"]];      ;    } else if () {      NSString *mid = ;      NSNumber *sdpLineIndex = ;      NSString *sdp = ;      RTCICECandidate *candidate = [ initWithMid:mid index:sdpLineIndex.intValue sdp:sdp];      ;    } else if () {      if (self.rtcView) {            NSData *jsonData = ;            NSString *jsonStr = [ initWithData:jsonData encoding:NSUTF8StringEncoding];            if (jsonStr.length > 0) {                [ sendSignalingMessage:jsonStr toUser:self.remoteJID];            }            ;            ;      }    }} 需要注意的是因为没有用到ICE穿墙,所以必须在同一个路由器下,否则可能无法进行点对点传输多媒体流。至此,局域网内音视频通话的小程序就完成了。
完整的Demo地址:局域网内WebRTC音视频通话
Demo中用到的WebRTC静态库已放到:百度网盘

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: iOS下WebRTC音视频通话(二)-局域网内音视频通话