iOS WebRTC語音視訊通話實現與demo
阿新 • • 發佈:2019-01-01
pod install AppRTC
從那裡你可以看到在這個回購ARTCVideoChatViewController類。以下步驟詳細具體更改您將需要在您的應用程式新增視訊聊天。
Initialize SSL Peer Connection
WebRTC可以通過SSL安全通訊。這是必需的,如果你想測試在https://apprtc.appspot.com。您需要修改您的AppDelegate。m類如下:- Import the RTCPeerConnectionFactory.h
#import "RTCPeerConnectionFactory.h"
- Add the following to your
application:didFinishLaunchingWithOptions:
[RTCPeerConnectionFactory initializeSSL];
- Add the following to your
applicationWillTerminate:
method:
[RTCPeerConnectionFactory deinitializeSSL];
Add Video Chat
嚮應用程式新增視訊聊天你需要2檢視:1.本地視訊檢視——從你的裝置攝像頭視訊呈現
2.遠端視訊呈現檢視——視訊遠端相機
為此,執行以下:
1.ViewController或使用的任何類,其中包含上面定義的兩個檢視新增以下標題:進口
#import <2.類應該實現ARDAppClientDelegate和RTCEAGLVideoViewDelegate協議:libjingle_peerconnection/RTCEAGLVideoView.h> #import <AppRTC/ARDAppClient.h>
@interface ARTCVideoChatViewController : UIViewController <ARDAppClientDelegate, RTCEAGLVideoViewDelegate>
3.在您的類定義以下屬性:* `ARDAppClientDelegate` - Handles events when remote client connects and disconnect states. Also, handles events when local and remote video feeds are received. * `RTCEAGLVideoViewDelegate` - Handles event for determining the video frame size.
@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;
* *ARDAppClient* - Performs the connection to the AppRTC Server and joins the chat room
* *remoteView* - Renders the Remote Video in the view
* *localView* - Renders the Local Video in the view
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.連線到一個視訊聊天室
[self.client setServerHostUrl:@"https://apprtc.appspot.com"]; [self.client connectToRoomWithId:@"room123" options:nil];6.處理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 { /* resize self.localView or self.remoteView based on the size returned */ }
Known Issues
以下是已知問題正在處理,應該不久公佈:- None at this time
有需要程式碼的可以聯絡加群。群號:622492955 哈哈哈!!!