ISBX apprtc 在iOS的使用
1. 把WebRTC视频聊天集成到你的app中
项目目录 - 终端中输入 pod install AppRTC
2. 从这里你可以找到ARTCVideoChatViewController
类 ,接下来集成的详细步骤:
1. 初始化SSL peer 连接
WebRTC可以通过SSL安全通信. 如果你要测试https,这是必须的. 你要修改你的appDelegate.m
如下:
#import "RTCPeerConnectionFactory.h"
- 在 didFinishLaunchingWithOption:方法中加入 :
[RTCPeerConnectionFactory initializeSSL];
- 在applicationWillTerminate: 方法中:
[RTCPeerConnectionFactory deinitializeSSL];
2. 添加视频聊天
需要两个控件:
- Local Video View : 显示设备相机
- Remote Video View : 显示远程相机.
步骤:
- 1.在你的ViewController或无论什么要放这两个视图的类中 ,引入:
#import <libjingle_peerconnection/RTCEAGLVideoView.h>
#import <AppRTC/ARDAppClient.h>
- 2.这个类要实现ARDAppClientDelegate 和 RTCEAGLVideoViewDelegate 协议.
@interface ARTCVideoChatViewController : UIViewController <ARDAppClientDelegate, RTCEAGLVideoViewDelegate>
//ARDAppClientDelegate: 管理当远程客户端连接和断开实践. 同时处理收到本地和远程视频提要.
//RTCEAGLVideoViewDelegate : 负责确定视频帧大小.
- 3.定义这些属性:
//执行AppRTC服务器连接并加入聊天室
@property (strong, nonatomic) ARDAppClient *client;
//视图中显示的远程视频
@property (strong, nonatomic) IBOutlet RTCEAGLVideoView *remoteView;
//显示的本地视频
@property (strong, nonatomic) IBOutlet RTCEAGLVideoView *localView;
@property (strong, nonatomic) RTCVideoTrack *localVideoTrack;
@property (strong, nonatomic) RTCVideoTrack *remoteVideoTrack;
- 4.初始化这些属性,并设置代理
/* Initializes the ARDAppClient with the delegate assignment */
self.client = [[ARDAppClient alloc] initWithDelegate:self];
/* RTCEAGLVideoViewDelegate provides notifications on video frame dimensions */
[self.remoteView setDelegate:self];
[self.localView setDelegate:self];
- 5.接入一个视频聊天室
// 设置Url
[self.client setServerHostUrl:@"https://apprtc.appspot.com"];
[self.client connectToRoomWithId:@"room123" options:nil];
主要内容在connectRoomWithId: option:里面:
- 首先把state 更改为kARDAppClientStateConnecting.
- 发送TURN Request.
注册room服务器
-
- 处理ARDAPPClientDelegate方法事件
-
//连接状态.
- (void)appClient:(ARDAppClient *)client didChangeState:(ARDAppClientState)state {
switch (state) {
case kARDAppClientStateConnected:
NSLog(@"Client connected.");
break;
case kARDAppClientStateConnecting:
NSLog(@"Client connecting.");
break;
case kARDAppClientStateDisconnected:
NSLog(@"Client disconnected.");
[self remoteDisconnected];
break;
}
}
//接收本地视频轨迹
- (void)appClient:(ARDAppClient *)client didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
self.localVideoTrack = localVideoTrack;
[self.localVideoTrack addRenderer:self.localView];
}
//接收远程视频轨迹
- (void)appClient:(ARDAppClient *)client didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
self.remoteVideoTrack = remoteVideoTrack;
[self.remoteVideoTrack addRenderer:self.remoteView];
}
- (void)appClient:(ARDAppClient *)client didError:(NSError *)error {
/* Handle the error */
}
- 7.处理RTCEAGLVideoViewDelegate回调
- (void)videoView:(RTCEAGLVideoView *)videoView didChangeVideoSize:(CGSize)size {
//根据返回的大小调整localView或remoteView
}