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