blob: 5e058662b05d6ed14a8059e5a189a896222ed2b9 [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]];
54 [_client setCameraConstraints:cameraConstraints];
haysc913e6452015-10-02 11:44:03 -070055 [_client connectToRoomWithId:room
56 isLoopback:isLoopback
peah5085b0c2016-08-25 22:15:14 -070057 isAudioOnly:isAudioOnly
tkchinab1293a2016-08-30 12:35:05 -070058 shouldMakeAecDump:shouldMakeAecDump
59 shouldUseLevelControl:shouldUseLevelControl];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000060 }
61 return self;
62}
63
64- (void)loadView {
65 _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero];
66 _videoCallView.delegate = self;
67 _videoCallView.statusLabel.text =
hjon79858f82016-03-13 22:08:26 -070068 [self statusTextForState:RTCIceConnectionStateNew];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000069 self.view = _videoCallView;
70}
71
72#pragma mark - ARDAppClientDelegate
73
74- (void)appClient:(ARDAppClient *)client
75 didChangeState:(ARDAppClientState)state {
76 switch (state) {
77 case kARDAppClientStateConnected:
tkchinc3f46a92015-07-23 12:50:55 -070078 RTCLog(@"Client connected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000079 break;
80 case kARDAppClientStateConnecting:
tkchinc3f46a92015-07-23 12:50:55 -070081 RTCLog(@"Client connecting.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000082 break;
83 case kARDAppClientStateDisconnected:
tkchinc3f46a92015-07-23 12:50:55 -070084 RTCLog(@"Client disconnected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000085 [self hangup];
86 break;
87 }
88}
89
90- (void)appClient:(ARDAppClient *)client
hjon79858f82016-03-13 22:08:26 -070091 didChangeConnectionState:(RTCIceConnectionState)state {
92 RTCLog(@"ICE state changed: %ld", (long)state);
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000093 __weak ARDVideoCallViewController *weakSelf = self;
94 dispatch_async(dispatch_get_main_queue(), ^{
95 ARDVideoCallViewController *strongSelf = weakSelf;
96 strongSelf.videoCallView.statusLabel.text =
97 [strongSelf statusTextForState:state];
98 });
99}
100
101- (void)appClient:(ARDAppClient *)client
102 didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700103 self.localVideoTrack = localVideoTrack;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000104}
105
106- (void)appClient:(ARDAppClient *)client
107 didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700108 self.remoteVideoTrack = remoteVideoTrack;
109 _videoCallView.statusLabel.hidden = YES;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000110}
111
112- (void)appClient:(ARDAppClient *)client
Zeke Chind3325802015-08-14 11:00:02 -0700113 didGetStats:(NSArray *)stats {
114 _videoCallView.statsView.stats = stats;
115 [_videoCallView setNeedsLayout];
116}
117
118- (void)appClient:(ARDAppClient *)client
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000119 didError:(NSError *)error {
120 NSString *message =
121 [NSString stringWithFormat:@"%@", error.localizedDescription];
122 [self showAlertWithMessage:message];
123 [self hangup];
124}
125
126#pragma mark - ARDVideoCallViewDelegate
127
128- (void)videoCallViewDidHangup:(ARDVideoCallView *)view {
129 [self hangup];
130}
131
Zeke Chin57cc74e2015-05-05 07:52:31 -0700132- (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view {
133 // TODO(tkchin): Rate limit this so you can't tap continously on it.
134 // Probably through an animation.
135 [self switchCamera];
136}
137
tkchin0ce3bf92016-03-12 16:52:04 -0800138- (void)videoCallViewDidChangeRoute:(ARDVideoCallView *)view {
139 AVAudioSessionPortOverride override = AVAudioSessionPortOverrideNone;
140 if (_portOverride == AVAudioSessionPortOverrideNone) {
141 override = AVAudioSessionPortOverrideSpeaker;
142 }
143 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeAudioSession
144 block:^{
145 RTCAudioSession *session = [RTCAudioSession sharedInstance];
146 [session lockForConfiguration];
147 NSError *error = nil;
148 if ([session overrideOutputAudioPort:override error:&error]) {
149 _portOverride = override;
150 } else {
151 RTCLogError(@"Error overriding output port: %@",
152 error.localizedDescription);
153 }
154 [session unlockForConfiguration];
155 }];
156}
157
Zeke Chind3325802015-08-14 11:00:02 -0700158- (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view {
159 _client.shouldGetStats = YES;
160 _videoCallView.statsView.hidden = NO;
161}
162
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000163#pragma mark - Private
164
Zeke Chin57cc74e2015-05-05 07:52:31 -0700165- (void)setLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
166 if (_localVideoTrack == localVideoTrack) {
167 return;
168 }
Zeke Chin57cc74e2015-05-05 07:52:31 -0700169 _localVideoTrack = nil;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700170 _localVideoTrack = localVideoTrack;
hayscedd8fef2015-12-08 11:08:39 -0800171 RTCAVFoundationVideoSource *source = nil;
172 if ([localVideoTrack.source
173 isKindOfClass:[RTCAVFoundationVideoSource class]]) {
174 source = (RTCAVFoundationVideoSource*)localVideoTrack.source;
175 }
176 _videoCallView.localVideoView.captureSession = source.captureSession;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700177}
178
179- (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
180 if (_remoteVideoTrack == remoteVideoTrack) {
181 return;
182 }
hayscedd8fef2015-12-08 11:08:39 -0800183 [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700184 _remoteVideoTrack = nil;
185 [_videoCallView.remoteVideoView renderFrame:nil];
186 _remoteVideoTrack = remoteVideoTrack;
187 [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView];
188}
189
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000190- (void)hangup {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700191 self.remoteVideoTrack = nil;
192 self.localVideoTrack = nil;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000193 [_client disconnect];
tkchind2511962016-05-06 18:54:15 -0700194 [_delegate viewControllerDidFinish:self];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000195}
196
Zeke Chin57cc74e2015-05-05 07:52:31 -0700197- (void)switchCamera {
198 RTCVideoSource* source = self.localVideoTrack.source;
199 if ([source isKindOfClass:[RTCAVFoundationVideoSource class]]) {
200 RTCAVFoundationVideoSource* avSource = (RTCAVFoundationVideoSource*)source;
201 avSource.useBackCamera = !avSource.useBackCamera;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700202 }
203}
204
hjon79858f82016-03-13 22:08:26 -0700205- (NSString *)statusTextForState:(RTCIceConnectionState)state {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000206 switch (state) {
hjon79858f82016-03-13 22:08:26 -0700207 case RTCIceConnectionStateNew:
208 case RTCIceConnectionStateChecking:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000209 return @"Connecting...";
hjon79858f82016-03-13 22:08:26 -0700210 case RTCIceConnectionStateConnected:
211 case RTCIceConnectionStateCompleted:
212 case RTCIceConnectionStateFailed:
213 case RTCIceConnectionStateDisconnected:
214 case RTCIceConnectionStateClosed:
hjon8bbbf2c2016-03-14 13:15:44 -0700215 case RTCIceConnectionStateCount:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000216 return nil;
217 }
218}
219
220- (void)showAlertWithMessage:(NSString*)message {
221 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil
222 message:message
223 delegate:nil
224 cancelButtonTitle:@"OK"
225 otherButtonTitles:nil];
226 [alertView show];
227}
228
229@end