blob: d1ae3f94e05bb93c7f991441539b57f8be4a4448 [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]) {
denicija2256e042016-11-09 06:26:18 -080047 ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init];
sakal68b5df92017-03-17 09:01:59 -070048 _delegate = delegate;
sakalc4adacf2017-03-28 01:22:48 -070049 _client = [[ARDAppClient alloc] initWithDelegate:self];
haysc913e6452015-10-02 11:44:03 -070050 [_client connectToRoomWithId:room
sakalc4adacf2017-03-28 01:22:48 -070051 settings:settingsModel
haysc913e6452015-10-02 11:44:03 -070052 isLoopback:isLoopback
peah5085b0c2016-08-25 22:15:14 -070053 isAudioOnly:isAudioOnly
tkchinab1293a2016-08-30 12:35:05 -070054 shouldMakeAecDump:shouldMakeAecDump
55 shouldUseLevelControl:shouldUseLevelControl];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000056 }
57 return self;
58}
59
60- (void)loadView {
61 _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero];
62 _videoCallView.delegate = self;
63 _videoCallView.statusLabel.text =
hjon79858f82016-03-13 22:08:26 -070064 [self statusTextForState:RTCIceConnectionStateNew];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000065 self.view = _videoCallView;
66}
67
68#pragma mark - ARDAppClientDelegate
69
70- (void)appClient:(ARDAppClient *)client
71 didChangeState:(ARDAppClientState)state {
72 switch (state) {
73 case kARDAppClientStateConnected:
tkchinc3f46a92015-07-23 12:50:55 -070074 RTCLog(@"Client connected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000075 break;
76 case kARDAppClientStateConnecting:
tkchinc3f46a92015-07-23 12:50:55 -070077 RTCLog(@"Client connecting.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000078 break;
79 case kARDAppClientStateDisconnected:
tkchinc3f46a92015-07-23 12:50:55 -070080 RTCLog(@"Client disconnected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000081 [self hangup];
82 break;
83 }
84}
85
86- (void)appClient:(ARDAppClient *)client
hjon79858f82016-03-13 22:08:26 -070087 didChangeConnectionState:(RTCIceConnectionState)state {
88 RTCLog(@"ICE state changed: %ld", (long)state);
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000089 __weak ARDVideoCallViewController *weakSelf = self;
90 dispatch_async(dispatch_get_main_queue(), ^{
91 ARDVideoCallViewController *strongSelf = weakSelf;
92 strongSelf.videoCallView.statusLabel.text =
93 [strongSelf statusTextForState:state];
94 });
95}
96
97- (void)appClient:(ARDAppClient *)client
98 didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -070099 self.localVideoTrack = localVideoTrack;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000100}
101
102- (void)appClient:(ARDAppClient *)client
103 didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700104 self.remoteVideoTrack = remoteVideoTrack;
105 _videoCallView.statusLabel.hidden = YES;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000106}
107
108- (void)appClient:(ARDAppClient *)client
Zeke Chind3325802015-08-14 11:00:02 -0700109 didGetStats:(NSArray *)stats {
110 _videoCallView.statsView.stats = stats;
111 [_videoCallView setNeedsLayout];
112}
113
114- (void)appClient:(ARDAppClient *)client
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000115 didError:(NSError *)error {
116 NSString *message =
117 [NSString stringWithFormat:@"%@", error.localizedDescription];
118 [self showAlertWithMessage:message];
119 [self hangup];
120}
121
122#pragma mark - ARDVideoCallViewDelegate
123
124- (void)videoCallViewDidHangup:(ARDVideoCallView *)view {
125 [self hangup];
126}
127
Zeke Chin57cc74e2015-05-05 07:52:31 -0700128- (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view {
129 // TODO(tkchin): Rate limit this so you can't tap continously on it.
130 // Probably through an animation.
131 [self switchCamera];
132}
133
tkchin0ce3bf92016-03-12 16:52:04 -0800134- (void)videoCallViewDidChangeRoute:(ARDVideoCallView *)view {
135 AVAudioSessionPortOverride override = AVAudioSessionPortOverrideNone;
136 if (_portOverride == AVAudioSessionPortOverrideNone) {
137 override = AVAudioSessionPortOverrideSpeaker;
138 }
139 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeAudioSession
140 block:^{
141 RTCAudioSession *session = [RTCAudioSession sharedInstance];
142 [session lockForConfiguration];
143 NSError *error = nil;
144 if ([session overrideOutputAudioPort:override error:&error]) {
145 _portOverride = override;
146 } else {
147 RTCLogError(@"Error overriding output port: %@",
148 error.localizedDescription);
149 }
150 [session unlockForConfiguration];
151 }];
152}
153
Zeke Chind3325802015-08-14 11:00:02 -0700154- (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view {
155 _client.shouldGetStats = YES;
156 _videoCallView.statsView.hidden = NO;
157}
158
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000159#pragma mark - Private
160
Zeke Chin57cc74e2015-05-05 07:52:31 -0700161- (void)setLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
162 if (_localVideoTrack == localVideoTrack) {
163 return;
164 }
Zeke Chin57cc74e2015-05-05 07:52:31 -0700165 _localVideoTrack = nil;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700166 _localVideoTrack = localVideoTrack;
hayscedd8fef2015-12-08 11:08:39 -0800167 RTCAVFoundationVideoSource *source = nil;
168 if ([localVideoTrack.source
169 isKindOfClass:[RTCAVFoundationVideoSource class]]) {
170 source = (RTCAVFoundationVideoSource*)localVideoTrack.source;
171 }
172 _videoCallView.localVideoView.captureSession = source.captureSession;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700173}
174
175- (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
176 if (_remoteVideoTrack == remoteVideoTrack) {
177 return;
178 }
hayscedd8fef2015-12-08 11:08:39 -0800179 [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700180 _remoteVideoTrack = nil;
181 [_videoCallView.remoteVideoView renderFrame:nil];
182 _remoteVideoTrack = remoteVideoTrack;
183 [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView];
184}
185
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000186- (void)hangup {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700187 self.remoteVideoTrack = nil;
188 self.localVideoTrack = nil;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000189 [_client disconnect];
tkchind2511962016-05-06 18:54:15 -0700190 [_delegate viewControllerDidFinish:self];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000191}
192
Zeke Chin57cc74e2015-05-05 07:52:31 -0700193- (void)switchCamera {
194 RTCVideoSource* source = self.localVideoTrack.source;
195 if ([source isKindOfClass:[RTCAVFoundationVideoSource class]]) {
196 RTCAVFoundationVideoSource* avSource = (RTCAVFoundationVideoSource*)source;
197 avSource.useBackCamera = !avSource.useBackCamera;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700198 }
199}
200
hjon79858f82016-03-13 22:08:26 -0700201- (NSString *)statusTextForState:(RTCIceConnectionState)state {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000202 switch (state) {
hjon79858f82016-03-13 22:08:26 -0700203 case RTCIceConnectionStateNew:
204 case RTCIceConnectionStateChecking:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000205 return @"Connecting...";
hjon79858f82016-03-13 22:08:26 -0700206 case RTCIceConnectionStateConnected:
207 case RTCIceConnectionStateCompleted:
208 case RTCIceConnectionStateFailed:
209 case RTCIceConnectionStateDisconnected:
210 case RTCIceConnectionStateClosed:
hjon8bbbf2c2016-03-14 13:15:44 -0700211 case RTCIceConnectionStateCount:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000212 return nil;
213 }
214}
215
216- (void)showAlertWithMessage:(NSString*)message {
217 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil
218 message:message
219 delegate:nil
220 cancelButtonTitle:@"OK"
221 otherButtonTitles:nil];
222 [alertView show];
223}
224
225@end