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