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