blob: 3deb067a68f223b90612077a783448d618f19147 [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
denicija59ee91b2017-06-05 05:48:47 -070013#import "WebRTC/RTCAudioSession.h"
tkchin0ce3bf92016-03-12 16:52:04 -080014
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,
Anders Carlsson121ea322017-06-26 15:34:47 +020025 ARDVideoCallViewDelegate,
26 RTCAudioSessionDelegate>
Zeke Chin57cc74e2015-05-05 07:52:31 -070027@property(nonatomic, strong) RTCVideoTrack *remoteVideoTrack;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000028@property(nonatomic, readonly) ARDVideoCallView *videoCallView;
29@end
30
31@implementation ARDVideoCallViewController {
32 ARDAppClient *_client;
33 RTCVideoTrack *_remoteVideoTrack;
sakalc522e752017-04-05 12:17:48 -070034 ARDCaptureController *_captureController;
tkchin0ce3bf92016-03-12 16:52:04 -080035 AVAudioSessionPortOverride _portOverride;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000036}
37
38@synthesize videoCallView = _videoCallView;
kthelgasonb13237b2017-03-30 04:56:05 -070039@synthesize remoteVideoTrack = _remoteVideoTrack;
tkchind2511962016-05-06 18:54:15 -070040@synthesize delegate = _delegate;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000041
haysc913e6452015-10-02 11:44:03 -070042- (instancetype)initForRoom:(NSString *)room
43 isLoopback:(BOOL)isLoopback
tkchind2511962016-05-06 18:54:15 -070044 delegate:(id<ARDVideoCallViewControllerDelegate>)delegate {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000045 if (self = [super init]) {
denicija2256e042016-11-09 06:26:18 -080046 ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init];
sakal68b5df92017-03-17 09:01:59 -070047 _delegate = delegate;
sakalc522e752017-04-05 12:17:48 -070048
sakalc4adacf2017-03-28 01:22:48 -070049 _client = [[ARDAppClient alloc] initWithDelegate:self];
Anders Carlssone1500582017-06-15 16:05:13 +020050 [_client connectToRoomWithId:room settings:settingsModel isLoopback:isLoopback];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000051 }
52 return self;
53}
54
55- (void)loadView {
56 _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero];
57 _videoCallView.delegate = self;
58 _videoCallView.statusLabel.text =
hjon79858f82016-03-13 22:08:26 -070059 [self statusTextForState:RTCIceConnectionStateNew];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000060 self.view = _videoCallView;
Anders Carlsson121ea322017-06-26 15:34:47 +020061
62 RTCAudioSession *session = [RTCAudioSession sharedInstance];
63 [session addDelegate:self];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000064}
65
Gustavo Garcia gustavo@lifeonair.com19d77c12017-11-15 16:03:18 +020066- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
67 return UIInterfaceOrientationMaskAll;
68}
69
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000070#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];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000128 [self hangup];
hewwatt7cc881d2017-05-18 01:33:34 -0700129 [self showAlertWithMessage:message];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000130}
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
Anders Carlsson121ea322017-06-26 15:34:47 +0200169#pragma mark - RTCAudioSessionDelegate
170
171- (void)audioSession:(RTCAudioSession *)audioSession
172 didDetectPlayoutGlitch:(int64_t)totalNumberOfGlitches {
173 RTCLog(@"Audio session detected glitch, total: %lld", totalNumberOfGlitches);
174}
175
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000176#pragma mark - Private
177
Zeke Chin57cc74e2015-05-05 07:52:31 -0700178- (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
179 if (_remoteVideoTrack == remoteVideoTrack) {
180 return;
181 }
hayscedd8fef2015-12-08 11:08:39 -0800182 [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700183 _remoteVideoTrack = nil;
184 [_videoCallView.remoteVideoView renderFrame:nil];
185 _remoteVideoTrack = remoteVideoTrack;
186 [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView];
187}
188
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000189- (void)hangup {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700190 self.remoteVideoTrack = nil;
sakalc522e752017-04-05 12:17:48 -0700191 _videoCallView.localVideoView.captureSession = nil;
192 [_captureController stopCapture];
193 _captureController = 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
hjon79858f82016-03-13 22:08:26 -0700198- (NSString *)statusTextForState:(RTCIceConnectionState)state {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000199 switch (state) {
hjon79858f82016-03-13 22:08:26 -0700200 case RTCIceConnectionStateNew:
201 case RTCIceConnectionStateChecking:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000202 return @"Connecting...";
hjon79858f82016-03-13 22:08:26 -0700203 case RTCIceConnectionStateConnected:
204 case RTCIceConnectionStateCompleted:
205 case RTCIceConnectionStateFailed:
206 case RTCIceConnectionStateDisconnected:
207 case RTCIceConnectionStateClosed:
hjon8bbbf2c2016-03-14 13:15:44 -0700208 case RTCIceConnectionStateCount:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000209 return nil;
210 }
211}
212
213- (void)showAlertWithMessage:(NSString*)message {
kthelgasonb13237b2017-03-30 04:56:05 -0700214 UIAlertController *alert =
215 [UIAlertController alertControllerWithTitle:nil
216 message:message
217 preferredStyle:UIAlertControllerStyleAlert];
218
219 UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK"
220 style:UIAlertActionStyleDefault
221 handler:^(UIAlertAction *action){
222 }];
223
224 [alert addAction:defaultAction];
225 [self presentViewController:alert animated:YES completion:nil];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000226}
227
228@end