blob: 9e17951f07f587d71d43372e8dd2a730c93e6155 [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"
denicija2256e042016-11-09 06:26:18 -080016#import "ARDSettingsModel.h"
denicijad17d5362016-11-02 02:56:09 -070017#import "ARDVideoCallView.h"
tkchin9eeb6242016-04-27 01:54:20 -070018#import "WebRTC/RTCAVFoundationVideoSource.h"
19#import "WebRTC/RTCDispatcher.h"
20#import "WebRTC/RTCLogging.h"
denicijad17d5362016-11-02 02:56:09 -070021#import "WebRTC/RTCMediaConstraints.h"
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000022
23@interface ARDVideoCallViewController () <ARDAppClientDelegate,
24 ARDVideoCallViewDelegate>
Zeke Chin57cc74e2015-05-05 07:52:31 -070025@property(nonatomic, strong) RTCVideoTrack *localVideoTrack;
26@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;
33 RTCVideoTrack *_localVideoTrack;
tkchin0ce3bf92016-03-12 16:52:04 -080034 AVAudioSessionPortOverride _portOverride;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000035}
36
37@synthesize videoCallView = _videoCallView;
tkchind2511962016-05-06 18:54:15 -070038@synthesize delegate = _delegate;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000039
haysc913e6452015-10-02 11:44:03 -070040- (instancetype)initForRoom:(NSString *)room
41 isLoopback:(BOOL)isLoopback
tkchind2511962016-05-06 18:54:15 -070042 isAudioOnly:(BOOL)isAudioOnly
peah5085b0c2016-08-25 22:15:14 -070043 shouldMakeAecDump:(BOOL)shouldMakeAecDump
tkchinab1293a2016-08-30 12:35:05 -070044 shouldUseLevelControl:(BOOL)shouldUseLevelControl
tkchind2511962016-05-06 18:54:15 -070045 delegate:(id<ARDVideoCallViewControllerDelegate>)delegate {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000046 if (self = [super init]) {
tkchind2511962016-05-06 18:54:15 -070047 _delegate = delegate;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000048 _client = [[ARDAppClient alloc] initWithDelegate:self];
denicija2256e042016-11-09 06:26:18 -080049 ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init];
denicijad17d5362016-11-02 02:56:09 -070050 RTCMediaConstraints *cameraConstraints = [[RTCMediaConstraints alloc]
51 initWithMandatoryConstraints:nil
denicija2256e042016-11-09 06:26:18 -080052 optionalConstraints:[settingsModel
denicijad17d5362016-11-02 02:56:09 -070053 currentMediaConstraintFromStoreAsRTCDictionary]];
denicija9af2b602016-11-17 00:43:43 -080054 [_client setMaxBitrate:[settingsModel currentMaxBitrateSettingFromStore]];
denicijad17d5362016-11-02 02:56:09 -070055 [_client setCameraConstraints:cameraConstraints];
haysc913e6452015-10-02 11:44:03 -070056 [_client connectToRoomWithId:room
57 isLoopback:isLoopback
peah5085b0c2016-08-25 22:15:14 -070058 isAudioOnly:isAudioOnly
tkchinab1293a2016-08-30 12:35:05 -070059 shouldMakeAecDump:shouldMakeAecDump
60 shouldUseLevelControl:shouldUseLevelControl];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000061 }
62 return self;
63}
64
65- (void)loadView {
66 _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero];
67 _videoCallView.delegate = self;
68 _videoCallView.statusLabel.text =
hjon79858f82016-03-13 22:08:26 -070069 [self statusTextForState:RTCIceConnectionStateNew];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000070 self.view = _videoCallView;
71}
72
73#pragma mark - ARDAppClientDelegate
74
75- (void)appClient:(ARDAppClient *)client
76 didChangeState:(ARDAppClientState)state {
77 switch (state) {
78 case kARDAppClientStateConnected:
tkchinc3f46a92015-07-23 12:50:55 -070079 RTCLog(@"Client connected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000080 break;
81 case kARDAppClientStateConnecting:
tkchinc3f46a92015-07-23 12:50:55 -070082 RTCLog(@"Client connecting.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000083 break;
84 case kARDAppClientStateDisconnected:
tkchinc3f46a92015-07-23 12:50:55 -070085 RTCLog(@"Client disconnected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000086 [self hangup];
87 break;
88 }
89}
90
91- (void)appClient:(ARDAppClient *)client
hjon79858f82016-03-13 22:08:26 -070092 didChangeConnectionState:(RTCIceConnectionState)state {
93 RTCLog(@"ICE state changed: %ld", (long)state);
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000094 __weak ARDVideoCallViewController *weakSelf = self;
95 dispatch_async(dispatch_get_main_queue(), ^{
96 ARDVideoCallViewController *strongSelf = weakSelf;
97 strongSelf.videoCallView.statusLabel.text =
98 [strongSelf statusTextForState:state];
99 });
100}
101
102- (void)appClient:(ARDAppClient *)client
103 didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700104 self.localVideoTrack = localVideoTrack;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000105}
106
107- (void)appClient:(ARDAppClient *)client
108 didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700109 self.remoteVideoTrack = remoteVideoTrack;
110 _videoCallView.statusLabel.hidden = YES;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000111}
112
113- (void)appClient:(ARDAppClient *)client
Zeke Chind3325802015-08-14 11:00:02 -0700114 didGetStats:(NSArray *)stats {
115 _videoCallView.statsView.stats = stats;
116 [_videoCallView setNeedsLayout];
117}
118
119- (void)appClient:(ARDAppClient *)client
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000120 didError:(NSError *)error {
121 NSString *message =
122 [NSString stringWithFormat:@"%@", error.localizedDescription];
123 [self showAlertWithMessage:message];
124 [self hangup];
125}
126
127#pragma mark - ARDVideoCallViewDelegate
128
129- (void)videoCallViewDidHangup:(ARDVideoCallView *)view {
130 [self hangup];
131}
132
Zeke Chin57cc74e2015-05-05 07:52:31 -0700133- (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view {
134 // TODO(tkchin): Rate limit this so you can't tap continously on it.
135 // Probably through an animation.
136 [self switchCamera];
137}
138
tkchin0ce3bf92016-03-12 16:52:04 -0800139- (void)videoCallViewDidChangeRoute:(ARDVideoCallView *)view {
140 AVAudioSessionPortOverride override = AVAudioSessionPortOverrideNone;
141 if (_portOverride == AVAudioSessionPortOverrideNone) {
142 override = AVAudioSessionPortOverrideSpeaker;
143 }
144 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeAudioSession
145 block:^{
146 RTCAudioSession *session = [RTCAudioSession sharedInstance];
147 [session lockForConfiguration];
148 NSError *error = nil;
149 if ([session overrideOutputAudioPort:override error:&error]) {
150 _portOverride = override;
151 } else {
152 RTCLogError(@"Error overriding output port: %@",
153 error.localizedDescription);
154 }
155 [session unlockForConfiguration];
156 }];
157}
158
Zeke Chind3325802015-08-14 11:00:02 -0700159- (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view {
160 _client.shouldGetStats = YES;
161 _videoCallView.statsView.hidden = NO;
162}
163
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000164#pragma mark - Private
165
Zeke Chin57cc74e2015-05-05 07:52:31 -0700166- (void)setLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
167 if (_localVideoTrack == localVideoTrack) {
168 return;
169 }
Zeke Chin57cc74e2015-05-05 07:52:31 -0700170 _localVideoTrack = nil;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700171 _localVideoTrack = localVideoTrack;
hayscedd8fef2015-12-08 11:08:39 -0800172 RTCAVFoundationVideoSource *source = nil;
173 if ([localVideoTrack.source
174 isKindOfClass:[RTCAVFoundationVideoSource class]]) {
175 source = (RTCAVFoundationVideoSource*)localVideoTrack.source;
176 }
177 _videoCallView.localVideoView.captureSession = source.captureSession;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700178}
179
180- (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
181 if (_remoteVideoTrack == remoteVideoTrack) {
182 return;
183 }
hayscedd8fef2015-12-08 11:08:39 -0800184 [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700185 _remoteVideoTrack = nil;
186 [_videoCallView.remoteVideoView renderFrame:nil];
187 _remoteVideoTrack = remoteVideoTrack;
188 [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView];
189}
190
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000191- (void)hangup {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700192 self.remoteVideoTrack = nil;
193 self.localVideoTrack = nil;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000194 [_client disconnect];
tkchind2511962016-05-06 18:54:15 -0700195 [_delegate viewControllerDidFinish:self];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000196}
197
Zeke Chin57cc74e2015-05-05 07:52:31 -0700198- (void)switchCamera {
199 RTCVideoSource* source = self.localVideoTrack.source;
200 if ([source isKindOfClass:[RTCAVFoundationVideoSource class]]) {
201 RTCAVFoundationVideoSource* avSource = (RTCAVFoundationVideoSource*)source;
202 avSource.useBackCamera = !avSource.useBackCamera;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700203 }
204}
205
hjon79858f82016-03-13 22:08:26 -0700206- (NSString *)statusTextForState:(RTCIceConnectionState)state {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000207 switch (state) {
hjon79858f82016-03-13 22:08:26 -0700208 case RTCIceConnectionStateNew:
209 case RTCIceConnectionStateChecking:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000210 return @"Connecting...";
hjon79858f82016-03-13 22:08:26 -0700211 case RTCIceConnectionStateConnected:
212 case RTCIceConnectionStateCompleted:
213 case RTCIceConnectionStateFailed:
214 case RTCIceConnectionStateDisconnected:
215 case RTCIceConnectionStateClosed:
hjon8bbbf2c2016-03-14 13:15:44 -0700216 case RTCIceConnectionStateCount:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000217 return nil;
218 }
219}
220
221- (void)showAlertWithMessage:(NSString*)message {
222 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil
223 message:message
224 delegate:nil
225 cancelButtonTitle:@"OK"
226 otherButtonTitles:nil];
227 [alertView show];
228}
229
230@end