tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 1 | /* |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 3 | * |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 4 | * 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.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #import "ARDVideoCallViewController.h" |
| 12 | |
denicija | 59ee91b | 2017-06-05 05:48:47 -0700 | [diff] [blame] | 13 | #import "WebRTC/RTCAudioSession.h" |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 14 | |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 15 | #import "ARDAppClient.h" |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 16 | #import "ARDCaptureController.h" |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 17 | #import "ARDSettingsModel.h" |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 18 | #import "ARDVideoCallView.h" |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 19 | #import "WebRTC/RTCAVFoundationVideoSource.h" |
| 20 | #import "WebRTC/RTCDispatcher.h" |
| 21 | #import "WebRTC/RTCLogging.h" |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 22 | #import "WebRTC/RTCMediaConstraints.h" |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 23 | |
| 24 | @interface ARDVideoCallViewController () <ARDAppClientDelegate, |
Anders Carlsson | 121ea32 | 2017-06-26 15:34:47 +0200 | [diff] [blame^] | 25 | ARDVideoCallViewDelegate, |
| 26 | RTCAudioSessionDelegate> |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 27 | @property(nonatomic, strong) RTCVideoTrack *remoteVideoTrack; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 28 | @property(nonatomic, readonly) ARDVideoCallView *videoCallView; |
| 29 | @end |
| 30 | |
| 31 | @implementation ARDVideoCallViewController { |
| 32 | ARDAppClient *_client; |
| 33 | RTCVideoTrack *_remoteVideoTrack; |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 34 | ARDCaptureController *_captureController; |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 35 | AVAudioSessionPortOverride _portOverride; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | @synthesize videoCallView = _videoCallView; |
kthelgason | b13237b | 2017-03-30 04:56:05 -0700 | [diff] [blame] | 39 | @synthesize remoteVideoTrack = _remoteVideoTrack; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 40 | @synthesize delegate = _delegate; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 41 | |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 42 | - (instancetype)initForRoom:(NSString *)room |
| 43 | isLoopback:(BOOL)isLoopback |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 44 | delegate:(id<ARDVideoCallViewControllerDelegate>)delegate { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 45 | if (self = [super init]) { |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 46 | ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init]; |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame] | 47 | _delegate = delegate; |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 48 | |
sakal | c4adacf | 2017-03-28 01:22:48 -0700 | [diff] [blame] | 49 | _client = [[ARDAppClient alloc] initWithDelegate:self]; |
Anders Carlsson | e150058 | 2017-06-15 16:05:13 +0200 | [diff] [blame] | 50 | [_client connectToRoomWithId:room settings:settingsModel isLoopback:isLoopback]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 51 | } |
| 52 | return self; |
| 53 | } |
| 54 | |
| 55 | - (void)loadView { |
| 56 | _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero]; |
| 57 | _videoCallView.delegate = self; |
| 58 | _videoCallView.statusLabel.text = |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 59 | [self statusTextForState:RTCIceConnectionStateNew]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 60 | self.view = _videoCallView; |
Anders Carlsson | 121ea32 | 2017-06-26 15:34:47 +0200 | [diff] [blame^] | 61 | |
| 62 | RTCAudioSession *session = [RTCAudioSession sharedInstance]; |
| 63 | [session addDelegate:self]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | #pragma mark - ARDAppClientDelegate |
| 67 | |
| 68 | - (void)appClient:(ARDAppClient *)client |
| 69 | didChangeState:(ARDAppClientState)state { |
| 70 | switch (state) { |
| 71 | case kARDAppClientStateConnected: |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 72 | RTCLog(@"Client connected."); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 73 | break; |
| 74 | case kARDAppClientStateConnecting: |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 75 | RTCLog(@"Client connecting."); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 76 | break; |
| 77 | case kARDAppClientStateDisconnected: |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 78 | RTCLog(@"Client disconnected."); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 79 | [self hangup]; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | - (void)appClient:(ARDAppClient *)client |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 85 | didChangeConnectionState:(RTCIceConnectionState)state { |
| 86 | RTCLog(@"ICE state changed: %ld", (long)state); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 87 | __weak ARDVideoCallViewController *weakSelf = self; |
| 88 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 89 | ARDVideoCallViewController *strongSelf = weakSelf; |
| 90 | strongSelf.videoCallView.statusLabel.text = |
| 91 | [strongSelf statusTextForState:state]; |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | - (void)appClient:(ARDAppClient *)client |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 96 | didCreateLocalCapturer:(RTCCameraVideoCapturer *)localCapturer { |
| 97 | _videoCallView.localVideoView.captureSession = localCapturer.captureSession; |
| 98 | ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init]; |
| 99 | _captureController = |
| 100 | [[ARDCaptureController alloc] initWithCapturer:localCapturer settings:settingsModel]; |
| 101 | [_captureController startCapture]; |
| 102 | } |
| 103 | |
| 104 | - (void)appClient:(ARDAppClient *)client |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 105 | didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | - (void)appClient:(ARDAppClient *)client |
| 109 | didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack { |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 110 | self.remoteVideoTrack = remoteVideoTrack; |
| 111 | _videoCallView.statusLabel.hidden = YES; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | - (void)appClient:(ARDAppClient *)client |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 115 | didGetStats:(NSArray *)stats { |
| 116 | _videoCallView.statsView.stats = stats; |
| 117 | [_videoCallView setNeedsLayout]; |
| 118 | } |
| 119 | |
| 120 | - (void)appClient:(ARDAppClient *)client |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 121 | didError:(NSError *)error { |
| 122 | NSString *message = |
| 123 | [NSString stringWithFormat:@"%@", error.localizedDescription]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 124 | [self hangup]; |
hewwatt | 7cc881d | 2017-05-18 01:33:34 -0700 | [diff] [blame] | 125 | [self showAlertWithMessage:message]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | #pragma mark - ARDVideoCallViewDelegate |
| 129 | |
| 130 | - (void)videoCallViewDidHangup:(ARDVideoCallView *)view { |
| 131 | [self hangup]; |
| 132 | } |
| 133 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 134 | - (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view { |
| 135 | // TODO(tkchin): Rate limit this so you can't tap continously on it. |
| 136 | // Probably through an animation. |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 137 | [_captureController switchCamera]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 138 | } |
| 139 | |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 140 | - (void)videoCallViewDidChangeRoute:(ARDVideoCallView *)view { |
| 141 | AVAudioSessionPortOverride override = AVAudioSessionPortOverrideNone; |
| 142 | if (_portOverride == AVAudioSessionPortOverrideNone) { |
| 143 | override = AVAudioSessionPortOverrideSpeaker; |
| 144 | } |
| 145 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeAudioSession |
| 146 | block:^{ |
| 147 | RTCAudioSession *session = [RTCAudioSession sharedInstance]; |
| 148 | [session lockForConfiguration]; |
| 149 | NSError *error = nil; |
| 150 | if ([session overrideOutputAudioPort:override error:&error]) { |
| 151 | _portOverride = override; |
| 152 | } else { |
| 153 | RTCLogError(@"Error overriding output port: %@", |
| 154 | error.localizedDescription); |
| 155 | } |
| 156 | [session unlockForConfiguration]; |
| 157 | }]; |
| 158 | } |
| 159 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 160 | - (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view { |
| 161 | _client.shouldGetStats = YES; |
| 162 | _videoCallView.statsView.hidden = NO; |
| 163 | } |
| 164 | |
Anders Carlsson | 121ea32 | 2017-06-26 15:34:47 +0200 | [diff] [blame^] | 165 | #pragma mark - RTCAudioSessionDelegate |
| 166 | |
| 167 | - (void)audioSession:(RTCAudioSession *)audioSession |
| 168 | didDetectPlayoutGlitch:(int64_t)totalNumberOfGlitches { |
| 169 | RTCLog(@"Audio session detected glitch, total: %lld", totalNumberOfGlitches); |
| 170 | } |
| 171 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 172 | #pragma mark - Private |
| 173 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 174 | - (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack { |
| 175 | if (_remoteVideoTrack == remoteVideoTrack) { |
| 176 | return; |
| 177 | } |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 178 | [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 179 | _remoteVideoTrack = nil; |
| 180 | [_videoCallView.remoteVideoView renderFrame:nil]; |
| 181 | _remoteVideoTrack = remoteVideoTrack; |
| 182 | [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView]; |
| 183 | } |
| 184 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 185 | - (void)hangup { |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 186 | self.remoteVideoTrack = nil; |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 187 | _videoCallView.localVideoView.captureSession = nil; |
| 188 | [_captureController stopCapture]; |
| 189 | _captureController = nil; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 190 | [_client disconnect]; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 191 | [_delegate viewControllerDidFinish:self]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 192 | } |
| 193 | |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 194 | - (NSString *)statusTextForState:(RTCIceConnectionState)state { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 195 | switch (state) { |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 196 | case RTCIceConnectionStateNew: |
| 197 | case RTCIceConnectionStateChecking: |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 198 | return @"Connecting..."; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 199 | case RTCIceConnectionStateConnected: |
| 200 | case RTCIceConnectionStateCompleted: |
| 201 | case RTCIceConnectionStateFailed: |
| 202 | case RTCIceConnectionStateDisconnected: |
| 203 | case RTCIceConnectionStateClosed: |
hjon | 8bbbf2c | 2016-03-14 13:15:44 -0700 | [diff] [blame] | 204 | case RTCIceConnectionStateCount: |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 205 | return nil; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | - (void)showAlertWithMessage:(NSString*)message { |
kthelgason | b13237b | 2017-03-30 04:56:05 -0700 | [diff] [blame] | 210 | UIAlertController *alert = |
| 211 | [UIAlertController alertControllerWithTitle:nil |
| 212 | message:message |
| 213 | preferredStyle:UIAlertControllerStyleAlert]; |
| 214 | |
| 215 | UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" |
| 216 | style:UIAlertActionStyleDefault |
| 217 | handler:^(UIAlertAction *action){ |
| 218 | }]; |
| 219 | |
| 220 | [alert addAction:defaultAction]; |
| 221 | [self presentViewController:alert animated:YES completion:nil]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @end |