blob: f7f4a877ef0aa1ed131a71229b4aff207dbacc2a [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
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020013#import <WebRTC/RTCAudioSession.h>
14#import <WebRTC/RTCCameraVideoCapturer.h>
15#import <WebRTC/RTCDispatcher.h>
16#import <WebRTC/RTCLogging.h>
17#import <WebRTC/RTCMediaConstraints.h>
tkchin0ce3bf92016-03-12 16:52:04 -080018
denicijad17d5362016-11-02 02:56:09 -070019#import "ARDAppClient.h"
sakalc522e752017-04-05 12:17:48 -070020#import "ARDCaptureController.h"
Daniela012b56b2017-11-15 13:15:24 +010021#import "ARDFileCaptureController.h"
denicija2256e042016-11-09 06:26:18 -080022#import "ARDSettingsModel.h"
denicijad17d5362016-11-02 02:56:09 -070023#import "ARDVideoCallView.h"
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000024
25@interface ARDVideoCallViewController () <ARDAppClientDelegate,
Anders Carlsson121ea322017-06-26 15:34:47 +020026 ARDVideoCallViewDelegate,
27 RTCAudioSessionDelegate>
Zeke Chin57cc74e2015-05-05 07:52:31 -070028@property(nonatomic, strong) RTCVideoTrack *remoteVideoTrack;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000029@property(nonatomic, readonly) ARDVideoCallView *videoCallView;
Jiawei Ou4aeb35b2018-11-09 13:55:45 -080030@property(nonatomic, assign) AVAudioSessionPortOverride portOverride;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000031@end
32
33@implementation ARDVideoCallViewController {
34 ARDAppClient *_client;
35 RTCVideoTrack *_remoteVideoTrack;
sakalc522e752017-04-05 12:17:48 -070036 ARDCaptureController *_captureController;
Daniela012b56b2017-11-15 13:15:24 +010037 ARDFileCaptureController *_fileCaptureController NS_AVAILABLE_IOS(10);
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000038}
39
40@synthesize videoCallView = _videoCallView;
kthelgasonb13237b2017-03-30 04:56:05 -070041@synthesize remoteVideoTrack = _remoteVideoTrack;
tkchind2511962016-05-06 18:54:15 -070042@synthesize delegate = _delegate;
Jiawei Ou4aeb35b2018-11-09 13:55:45 -080043@synthesize portOverride = _portOverride;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000044
haysc913e6452015-10-02 11:44:03 -070045- (instancetype)initForRoom:(NSString *)room
46 isLoopback:(BOOL)isLoopback
tkchind2511962016-05-06 18:54:15 -070047 delegate:(id<ARDVideoCallViewControllerDelegate>)delegate {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000048 if (self = [super init]) {
denicija2256e042016-11-09 06:26:18 -080049 ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init];
sakal68b5df92017-03-17 09:01:59 -070050 _delegate = delegate;
sakalc522e752017-04-05 12:17:48 -070051
sakalc4adacf2017-03-28 01:22:48 -070052 _client = [[ARDAppClient alloc] initWithDelegate:self];
Anders Carlssone1500582017-06-15 16:05:13 +020053 [_client connectToRoomWithId:room settings:settingsModel isLoopback:isLoopback];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000054 }
55 return self;
56}
57
58- (void)loadView {
59 _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero];
60 _videoCallView.delegate = self;
61 _videoCallView.statusLabel.text =
hjon79858f82016-03-13 22:08:26 -070062 [self statusTextForState:RTCIceConnectionStateNew];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000063 self.view = _videoCallView;
Anders Carlsson121ea322017-06-26 15:34:47 +020064
65 RTCAudioSession *session = [RTCAudioSession sharedInstance];
66 [session addDelegate:self];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000067}
68
Gustavo Garcia gustavo@lifeonair.com19d77c12017-11-15 16:03:18 +020069- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
70 return UIInterfaceOrientationMaskAll;
71}
72
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000073#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
sakalc522e752017-04-05 12:17:48 -0700103 didCreateLocalCapturer:(RTCCameraVideoCapturer *)localCapturer {
104 _videoCallView.localVideoView.captureSession = localCapturer.captureSession;
105 ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init];
106 _captureController =
107 [[ARDCaptureController alloc] initWithCapturer:localCapturer settings:settingsModel];
108 [_captureController startCapture];
109}
110
111- (void)appClient:(ARDAppClient *)client
Daniela012b56b2017-11-15 13:15:24 +0100112 didCreateLocalFileCapturer:(RTCFileVideoCapturer *)fileCapturer {
113#if defined(__IPHONE_11_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0)
114 if (@available(iOS 10, *)) {
115 _fileCaptureController = [[ARDFileCaptureController alloc] initWithCapturer:fileCapturer];
116 [_fileCaptureController startCapture];
117 }
118#endif
119}
120
121- (void)appClient:(ARDAppClient *)client
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000122 didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000123}
124
125- (void)appClient:(ARDAppClient *)client
126 didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700127 self.remoteVideoTrack = remoteVideoTrack;
Peter Hanspersa1f566b2018-05-02 13:07:53 +0200128 __weak ARDVideoCallViewController *weakSelf = self;
129 dispatch_async(dispatch_get_main_queue(), ^{
130 ARDVideoCallViewController *strongSelf = weakSelf;
131 strongSelf.videoCallView.statusLabel.hidden = YES;
132 });
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000133}
134
135- (void)appClient:(ARDAppClient *)client
Zeke Chind3325802015-08-14 11:00:02 -0700136 didGetStats:(NSArray *)stats {
137 _videoCallView.statsView.stats = stats;
138 [_videoCallView setNeedsLayout];
139}
140
141- (void)appClient:(ARDAppClient *)client
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000142 didError:(NSError *)error {
143 NSString *message =
144 [NSString stringWithFormat:@"%@", error.localizedDescription];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000145 [self hangup];
hewwatt7cc881d2017-05-18 01:33:34 -0700146 [self showAlertWithMessage:message];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000147}
148
149#pragma mark - ARDVideoCallViewDelegate
150
151- (void)videoCallViewDidHangup:(ARDVideoCallView *)view {
152 [self hangup];
153}
154
Zeke Chin57cc74e2015-05-05 07:52:31 -0700155- (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view {
156 // TODO(tkchin): Rate limit this so you can't tap continously on it.
157 // Probably through an animation.
sakalc522e752017-04-05 12:17:48 -0700158 [_captureController switchCamera];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700159}
160
tkchin0ce3bf92016-03-12 16:52:04 -0800161- (void)videoCallViewDidChangeRoute:(ARDVideoCallView *)view {
162 AVAudioSessionPortOverride override = AVAudioSessionPortOverrideNone;
163 if (_portOverride == AVAudioSessionPortOverrideNone) {
164 override = AVAudioSessionPortOverrideSpeaker;
165 }
166 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeAudioSession
167 block:^{
168 RTCAudioSession *session = [RTCAudioSession sharedInstance];
169 [session lockForConfiguration];
170 NSError *error = nil;
171 if ([session overrideOutputAudioPort:override error:&error]) {
Jiawei Ou4aeb35b2018-11-09 13:55:45 -0800172 self.portOverride = override;
tkchin0ce3bf92016-03-12 16:52:04 -0800173 } else {
174 RTCLogError(@"Error overriding output port: %@",
175 error.localizedDescription);
176 }
177 [session unlockForConfiguration];
178 }];
179}
180
Zeke Chind3325802015-08-14 11:00:02 -0700181- (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view {
182 _client.shouldGetStats = YES;
183 _videoCallView.statsView.hidden = NO;
184}
185
Anders Carlsson121ea322017-06-26 15:34:47 +0200186#pragma mark - RTCAudioSessionDelegate
187
188- (void)audioSession:(RTCAudioSession *)audioSession
189 didDetectPlayoutGlitch:(int64_t)totalNumberOfGlitches {
190 RTCLog(@"Audio session detected glitch, total: %lld", totalNumberOfGlitches);
191}
192
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000193#pragma mark - Private
194
Zeke Chin57cc74e2015-05-05 07:52:31 -0700195- (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
196 if (_remoteVideoTrack == remoteVideoTrack) {
197 return;
198 }
hayscedd8fef2015-12-08 11:08:39 -0800199 [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700200 _remoteVideoTrack = nil;
201 [_videoCallView.remoteVideoView renderFrame:nil];
202 _remoteVideoTrack = remoteVideoTrack;
203 [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView];
204}
205
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000206- (void)hangup {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700207 self.remoteVideoTrack = nil;
sakalc522e752017-04-05 12:17:48 -0700208 _videoCallView.localVideoView.captureSession = nil;
209 [_captureController stopCapture];
210 _captureController = nil;
Daniela012b56b2017-11-15 13:15:24 +0100211 [_fileCaptureController stopCapture];
212 _fileCaptureController = nil;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000213 [_client disconnect];
tkchind2511962016-05-06 18:54:15 -0700214 [_delegate viewControllerDidFinish:self];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000215}
216
hjon79858f82016-03-13 22:08:26 -0700217- (NSString *)statusTextForState:(RTCIceConnectionState)state {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000218 switch (state) {
hjon79858f82016-03-13 22:08:26 -0700219 case RTCIceConnectionStateNew:
220 case RTCIceConnectionStateChecking:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000221 return @"Connecting...";
hjon79858f82016-03-13 22:08:26 -0700222 case RTCIceConnectionStateConnected:
223 case RTCIceConnectionStateCompleted:
224 case RTCIceConnectionStateFailed:
225 case RTCIceConnectionStateDisconnected:
226 case RTCIceConnectionStateClosed:
hjon8bbbf2c2016-03-14 13:15:44 -0700227 case RTCIceConnectionStateCount:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000228 return nil;
229 }
230}
231
232- (void)showAlertWithMessage:(NSString*)message {
kthelgasonb13237b2017-03-30 04:56:05 -0700233 UIAlertController *alert =
234 [UIAlertController alertControllerWithTitle:nil
235 message:message
236 preferredStyle:UIAlertControllerStyleAlert];
237
238 UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK"
239 style:UIAlertActionStyleDefault
240 handler:^(UIAlertAction *action){
241 }];
242
243 [alert addAction:defaultAction];
244 [self presentViewController:alert animated:YES completion:nil];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000245}
246
247@end