blob: 03ce6262669780a18e42fb61ef228e39cbd2492c [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,
25 ARDVideoCallViewDelegate>
Zeke Chin57cc74e2015-05-05 07:52:31 -070026@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;
sakalc522e752017-04-05 12:17:48 -070033 ARDCaptureController *_captureController;
tkchin0ce3bf92016-03-12 16:52:04 -080034 AVAudioSessionPortOverride _portOverride;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000035}
36
37@synthesize videoCallView = _videoCallView;
kthelgasonb13237b2017-03-30 04:56:05 -070038@synthesize remoteVideoTrack = _remoteVideoTrack;
tkchind2511962016-05-06 18:54:15 -070039@synthesize delegate = _delegate;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000040
haysc913e6452015-10-02 11:44:03 -070041- (instancetype)initForRoom:(NSString *)room
42 isLoopback:(BOOL)isLoopback
tkchind2511962016-05-06 18:54:15 -070043 delegate:(id<ARDVideoCallViewControllerDelegate>)delegate {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000044 if (self = [super init]) {
denicija2256e042016-11-09 06:26:18 -080045 ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init];
sakal68b5df92017-03-17 09:01:59 -070046 _delegate = delegate;
sakalc522e752017-04-05 12:17:48 -070047
sakalc4adacf2017-03-28 01:22:48 -070048 _client = [[ARDAppClient alloc] initWithDelegate:self];
Anders Carlssone1500582017-06-15 16:05:13 +020049 [_client connectToRoomWithId:room settings:settingsModel isLoopback:isLoopback];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000050 }
51 return self;
52}
53
54- (void)loadView {
55 _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero];
56 _videoCallView.delegate = self;
57 _videoCallView.statusLabel.text =
hjon79858f82016-03-13 22:08:26 -070058 [self statusTextForState:RTCIceConnectionStateNew];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000059 self.view = _videoCallView;
60}
61
62#pragma mark - ARDAppClientDelegate
63
64- (void)appClient:(ARDAppClient *)client
65 didChangeState:(ARDAppClientState)state {
66 switch (state) {
67 case kARDAppClientStateConnected:
tkchinc3f46a92015-07-23 12:50:55 -070068 RTCLog(@"Client connected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000069 break;
70 case kARDAppClientStateConnecting:
tkchinc3f46a92015-07-23 12:50:55 -070071 RTCLog(@"Client connecting.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000072 break;
73 case kARDAppClientStateDisconnected:
tkchinc3f46a92015-07-23 12:50:55 -070074 RTCLog(@"Client disconnected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000075 [self hangup];
76 break;
77 }
78}
79
80- (void)appClient:(ARDAppClient *)client
hjon79858f82016-03-13 22:08:26 -070081 didChangeConnectionState:(RTCIceConnectionState)state {
82 RTCLog(@"ICE state changed: %ld", (long)state);
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000083 __weak ARDVideoCallViewController *weakSelf = self;
84 dispatch_async(dispatch_get_main_queue(), ^{
85 ARDVideoCallViewController *strongSelf = weakSelf;
86 strongSelf.videoCallView.statusLabel.text =
87 [strongSelf statusTextForState:state];
88 });
89}
90
91- (void)appClient:(ARDAppClient *)client
sakalc522e752017-04-05 12:17:48 -070092 didCreateLocalCapturer:(RTCCameraVideoCapturer *)localCapturer {
93 _videoCallView.localVideoView.captureSession = localCapturer.captureSession;
94 ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init];
95 _captureController =
96 [[ARDCaptureController alloc] initWithCapturer:localCapturer settings:settingsModel];
97 [_captureController startCapture];
98}
99
100- (void)appClient:(ARDAppClient *)client
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000101 didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000102}
103
104- (void)appClient:(ARDAppClient *)client
105 didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700106 self.remoteVideoTrack = remoteVideoTrack;
107 _videoCallView.statusLabel.hidden = YES;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000108}
109
110- (void)appClient:(ARDAppClient *)client
Zeke Chind3325802015-08-14 11:00:02 -0700111 didGetStats:(NSArray *)stats {
112 _videoCallView.statsView.stats = stats;
113 [_videoCallView setNeedsLayout];
114}
115
116- (void)appClient:(ARDAppClient *)client
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000117 didError:(NSError *)error {
118 NSString *message =
119 [NSString stringWithFormat:@"%@", error.localizedDescription];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000120 [self hangup];
hewwatt7cc881d2017-05-18 01:33:34 -0700121 [self showAlertWithMessage:message];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000122}
123
124#pragma mark - ARDVideoCallViewDelegate
125
126- (void)videoCallViewDidHangup:(ARDVideoCallView *)view {
127 [self hangup];
128}
129
Zeke Chin57cc74e2015-05-05 07:52:31 -0700130- (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view {
131 // TODO(tkchin): Rate limit this so you can't tap continously on it.
132 // Probably through an animation.
sakalc522e752017-04-05 12:17:48 -0700133 [_captureController switchCamera];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700134}
135
tkchin0ce3bf92016-03-12 16:52:04 -0800136- (void)videoCallViewDidChangeRoute:(ARDVideoCallView *)view {
137 AVAudioSessionPortOverride override = AVAudioSessionPortOverrideNone;
138 if (_portOverride == AVAudioSessionPortOverrideNone) {
139 override = AVAudioSessionPortOverrideSpeaker;
140 }
141 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeAudioSession
142 block:^{
143 RTCAudioSession *session = [RTCAudioSession sharedInstance];
144 [session lockForConfiguration];
145 NSError *error = nil;
146 if ([session overrideOutputAudioPort:override error:&error]) {
147 _portOverride = override;
148 } else {
149 RTCLogError(@"Error overriding output port: %@",
150 error.localizedDescription);
151 }
152 [session unlockForConfiguration];
153 }];
154}
155
Zeke Chind3325802015-08-14 11:00:02 -0700156- (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view {
157 _client.shouldGetStats = YES;
158 _videoCallView.statsView.hidden = NO;
159}
160
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000161#pragma mark - Private
162
Zeke Chin57cc74e2015-05-05 07:52:31 -0700163- (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
164 if (_remoteVideoTrack == remoteVideoTrack) {
165 return;
166 }
hayscedd8fef2015-12-08 11:08:39 -0800167 [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700168 _remoteVideoTrack = nil;
169 [_videoCallView.remoteVideoView renderFrame:nil];
170 _remoteVideoTrack = remoteVideoTrack;
171 [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView];
172}
173
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000174- (void)hangup {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700175 self.remoteVideoTrack = nil;
sakalc522e752017-04-05 12:17:48 -0700176 _videoCallView.localVideoView.captureSession = nil;
177 [_captureController stopCapture];
178 _captureController = nil;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000179 [_client disconnect];
tkchind2511962016-05-06 18:54:15 -0700180 [_delegate viewControllerDidFinish:self];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000181}
182
hjon79858f82016-03-13 22:08:26 -0700183- (NSString *)statusTextForState:(RTCIceConnectionState)state {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000184 switch (state) {
hjon79858f82016-03-13 22:08:26 -0700185 case RTCIceConnectionStateNew:
186 case RTCIceConnectionStateChecking:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000187 return @"Connecting...";
hjon79858f82016-03-13 22:08:26 -0700188 case RTCIceConnectionStateConnected:
189 case RTCIceConnectionStateCompleted:
190 case RTCIceConnectionStateFailed:
191 case RTCIceConnectionStateDisconnected:
192 case RTCIceConnectionStateClosed:
hjon8bbbf2c2016-03-14 13:15:44 -0700193 case RTCIceConnectionStateCount:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000194 return nil;
195 }
196}
197
198- (void)showAlertWithMessage:(NSString*)message {
kthelgasonb13237b2017-03-30 04:56:05 -0700199 UIAlertController *alert =
200 [UIAlertController alertControllerWithTitle:nil
201 message:message
202 preferredStyle:UIAlertControllerStyleAlert];
203
204 UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK"
205 style:UIAlertActionStyleDefault
206 handler:^(UIAlertAction *action){
207 }];
208
209 [alert addAction:defaultAction];
210 [self presentViewController:alert animated:YES completion:nil];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000211}
212
213@end