blob: 732e4b6511ef281db7ab763f55aa1ae7995486f5 [file] [log] [blame]
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +00003 *
Donald E Curtisa8736442015-08-05 15:48:13 -07004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +00009 */
10
11#import "ARDVideoCallViewController.h"
12
tkchin0ce3bf92016-03-12 16:52:04 -080013#import "webrtc/modules/audio_device/ios/objc/RTCAudioSession.h"
14
denicijad17d5362016-11-02 02:56:09 -070015#import "ARDAppClient.h"
sakalc522e752017-04-05 12:17:48 -070016#import "ARDCaptureController.h"
denicija2256e042016-11-09 06:26:18 -080017#import "ARDSettingsModel.h"
denicijad17d5362016-11-02 02:56:09 -070018#import "ARDVideoCallView.h"
tkchin9eeb6242016-04-27 01:54:20 -070019#import "WebRTC/RTCAVFoundationVideoSource.h"
20#import "WebRTC/RTCDispatcher.h"
21#import "WebRTC/RTCLogging.h"
denicijad17d5362016-11-02 02:56:09 -070022#import "WebRTC/RTCMediaConstraints.h"
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000023
24@interface ARDVideoCallViewController () <ARDAppClientDelegate,
25 ARDVideoCallViewDelegate>
Zeke Chin57cc74e2015-05-05 07:52:31 -070026@property(nonatomic, strong) RTCVideoTrack *remoteVideoTrack;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000027@property(nonatomic, readonly) ARDVideoCallView *videoCallView;
28@end
29
30@implementation ARDVideoCallViewController {
31 ARDAppClient *_client;
32 RTCVideoTrack *_remoteVideoTrack;
sakalc522e752017-04-05 12:17:48 -070033 ARDCaptureController *_captureController;
tkchin0ce3bf92016-03-12 16:52:04 -080034 AVAudioSessionPortOverride _portOverride;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000035}
36
37@synthesize videoCallView = _videoCallView;
kthelgasonb13237b2017-03-30 04:56:05 -070038@synthesize remoteVideoTrack = _remoteVideoTrack;
tkchind2511962016-05-06 18:54:15 -070039@synthesize delegate = _delegate;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000040
haysc913e6452015-10-02 11:44:03 -070041- (instancetype)initForRoom:(NSString *)room
42 isLoopback:(BOOL)isLoopback
tkchind2511962016-05-06 18:54:15 -070043 isAudioOnly:(BOOL)isAudioOnly
peah5085b0c2016-08-25 22:15:14 -070044 shouldMakeAecDump:(BOOL)shouldMakeAecDump
tkchinab1293a2016-08-30 12:35:05 -070045 shouldUseLevelControl:(BOOL)shouldUseLevelControl
tkchind2511962016-05-06 18:54:15 -070046 delegate:(id<ARDVideoCallViewControllerDelegate>)delegate {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000047 if (self = [super init]) {
denicija2256e042016-11-09 06:26:18 -080048 ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init];
sakal68b5df92017-03-17 09:01:59 -070049 _delegate = delegate;
sakalc522e752017-04-05 12:17:48 -070050
sakalc4adacf2017-03-28 01:22:48 -070051 _client = [[ARDAppClient alloc] initWithDelegate:self];
haysc913e6452015-10-02 11:44:03 -070052 [_client connectToRoomWithId:room
sakalc4adacf2017-03-28 01:22:48 -070053 settings:settingsModel
haysc913e6452015-10-02 11:44:03 -070054 isLoopback:isLoopback
peah5085b0c2016-08-25 22:15:14 -070055 isAudioOnly:isAudioOnly
tkchinab1293a2016-08-30 12:35:05 -070056 shouldMakeAecDump:shouldMakeAecDump
57 shouldUseLevelControl:shouldUseLevelControl];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000058 }
59 return self;
60}
61
62- (void)loadView {
63 _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero];
64 _videoCallView.delegate = self;
65 _videoCallView.statusLabel.text =
hjon79858f82016-03-13 22:08:26 -070066 [self statusTextForState:RTCIceConnectionStateNew];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000067 self.view = _videoCallView;
68}
69
70#pragma mark - ARDAppClientDelegate
71
72- (void)appClient:(ARDAppClient *)client
73 didChangeState:(ARDAppClientState)state {
74 switch (state) {
75 case kARDAppClientStateConnected:
tkchinc3f46a92015-07-23 12:50:55 -070076 RTCLog(@"Client connected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000077 break;
78 case kARDAppClientStateConnecting:
tkchinc3f46a92015-07-23 12:50:55 -070079 RTCLog(@"Client connecting.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000080 break;
81 case kARDAppClientStateDisconnected:
tkchinc3f46a92015-07-23 12:50:55 -070082 RTCLog(@"Client disconnected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000083 [self hangup];
84 break;
85 }
86}
87
88- (void)appClient:(ARDAppClient *)client
hjon79858f82016-03-13 22:08:26 -070089 didChangeConnectionState:(RTCIceConnectionState)state {
90 RTCLog(@"ICE state changed: %ld", (long)state);
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000091 __weak ARDVideoCallViewController *weakSelf = self;
92 dispatch_async(dispatch_get_main_queue(), ^{
93 ARDVideoCallViewController *strongSelf = weakSelf;
94 strongSelf.videoCallView.statusLabel.text =
95 [strongSelf statusTextForState:state];
96 });
97}
98
99- (void)appClient:(ARDAppClient *)client
sakalc522e752017-04-05 12:17:48 -0700100 didCreateLocalCapturer:(RTCCameraVideoCapturer *)localCapturer {
101 _videoCallView.localVideoView.captureSession = localCapturer.captureSession;
102 ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init];
103 _captureController =
104 [[ARDCaptureController alloc] initWithCapturer:localCapturer settings:settingsModel];
105 [_captureController startCapture];
106}
107
108- (void)appClient:(ARDAppClient *)client
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000109 didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000110}
111
112- (void)appClient:(ARDAppClient *)client
113 didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700114 self.remoteVideoTrack = remoteVideoTrack;
115 _videoCallView.statusLabel.hidden = YES;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000116}
117
118- (void)appClient:(ARDAppClient *)client
Zeke Chind3325802015-08-14 11:00:02 -0700119 didGetStats:(NSArray *)stats {
120 _videoCallView.statsView.stats = stats;
121 [_videoCallView setNeedsLayout];
122}
123
124- (void)appClient:(ARDAppClient *)client
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000125 didError:(NSError *)error {
126 NSString *message =
127 [NSString stringWithFormat:@"%@", error.localizedDescription];
128 [self showAlertWithMessage:message];
129 [self hangup];
130}
131
132#pragma mark - ARDVideoCallViewDelegate
133
134- (void)videoCallViewDidHangup:(ARDVideoCallView *)view {
135 [self hangup];
136}
137
Zeke Chin57cc74e2015-05-05 07:52:31 -0700138- (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view {
139 // TODO(tkchin): Rate limit this so you can't tap continously on it.
140 // Probably through an animation.
sakalc522e752017-04-05 12:17:48 -0700141 [_captureController switchCamera];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700142}
143
tkchin0ce3bf92016-03-12 16:52:04 -0800144- (void)videoCallViewDidChangeRoute:(ARDVideoCallView *)view {
145 AVAudioSessionPortOverride override = AVAudioSessionPortOverrideNone;
146 if (_portOverride == AVAudioSessionPortOverrideNone) {
147 override = AVAudioSessionPortOverrideSpeaker;
148 }
149 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeAudioSession
150 block:^{
151 RTCAudioSession *session = [RTCAudioSession sharedInstance];
152 [session lockForConfiguration];
153 NSError *error = nil;
154 if ([session overrideOutputAudioPort:override error:&error]) {
155 _portOverride = override;
156 } else {
157 RTCLogError(@"Error overriding output port: %@",
158 error.localizedDescription);
159 }
160 [session unlockForConfiguration];
161 }];
162}
163
Zeke Chind3325802015-08-14 11:00:02 -0700164- (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view {
165 _client.shouldGetStats = YES;
166 _videoCallView.statsView.hidden = NO;
167}
168
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000169#pragma mark - Private
170
Zeke Chin57cc74e2015-05-05 07:52:31 -0700171- (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
172 if (_remoteVideoTrack == remoteVideoTrack) {
173 return;
174 }
hayscedd8fef2015-12-08 11:08:39 -0800175 [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700176 _remoteVideoTrack = nil;
177 [_videoCallView.remoteVideoView renderFrame:nil];
178 _remoteVideoTrack = remoteVideoTrack;
179 [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView];
180}
181
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000182- (void)hangup {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700183 self.remoteVideoTrack = nil;
sakalc522e752017-04-05 12:17:48 -0700184 _videoCallView.localVideoView.captureSession = nil;
185 [_captureController stopCapture];
186 _captureController = nil;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000187 [_client disconnect];
tkchind2511962016-05-06 18:54:15 -0700188 [_delegate viewControllerDidFinish:self];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000189}
190
hjon79858f82016-03-13 22:08:26 -0700191- (NSString *)statusTextForState:(RTCIceConnectionState)state {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000192 switch (state) {
hjon79858f82016-03-13 22:08:26 -0700193 case RTCIceConnectionStateNew:
194 case RTCIceConnectionStateChecking:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000195 return @"Connecting...";
hjon79858f82016-03-13 22:08:26 -0700196 case RTCIceConnectionStateConnected:
197 case RTCIceConnectionStateCompleted:
198 case RTCIceConnectionStateFailed:
199 case RTCIceConnectionStateDisconnected:
200 case RTCIceConnectionStateClosed:
hjon8bbbf2c2016-03-14 13:15:44 -0700201 case RTCIceConnectionStateCount:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000202 return nil;
203 }
204}
205
206- (void)showAlertWithMessage:(NSString*)message {
kthelgasonb13237b2017-03-30 04:56:05 -0700207 UIAlertController *alert =
208 [UIAlertController alertControllerWithTitle:nil
209 message:message
210 preferredStyle:UIAlertControllerStyleAlert];
211
212 UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK"
213 style:UIAlertActionStyleDefault
214 handler:^(UIAlertAction *action){
215 }];
216
217 [alert addAction:defaultAction];
218 [self presentViewController:alert animated:YES completion:nil];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000219}
220
221@end