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, |
| 25 | ARDVideoCallViewDelegate> |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 26 | @property(nonatomic, strong) RTCVideoTrack *remoteVideoTrack; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 27 | @property(nonatomic, readonly) ARDVideoCallView *videoCallView; |
| 28 | @end |
| 29 | |
| 30 | @implementation ARDVideoCallViewController { |
| 31 | ARDAppClient *_client; |
| 32 | RTCVideoTrack *_remoteVideoTrack; |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 33 | ARDCaptureController *_captureController; |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 34 | AVAudioSessionPortOverride _portOverride; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | @synthesize videoCallView = _videoCallView; |
kthelgason | b13237b | 2017-03-30 04:56:05 -0700 | [diff] [blame] | 38 | @synthesize remoteVideoTrack = _remoteVideoTrack; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 39 | @synthesize delegate = _delegate; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 40 | |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 41 | - (instancetype)initForRoom:(NSString *)room |
| 42 | isLoopback:(BOOL)isLoopback |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 43 | delegate:(id<ARDVideoCallViewControllerDelegate>)delegate { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 44 | if (self = [super init]) { |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 45 | ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init]; |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame] | 46 | _delegate = delegate; |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 47 | |
sakal | c4adacf | 2017-03-28 01:22:48 -0700 | [diff] [blame] | 48 | _client = [[ARDAppClient alloc] initWithDelegate:self]; |
Anders Carlsson | e150058 | 2017-06-15 16:05:13 +0200 | [diff] [blame] | 49 | [_client connectToRoomWithId:room settings:settingsModel isLoopback:isLoopback]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 50 | } |
| 51 | return self; |
| 52 | } |
| 53 | |
| 54 | - (void)loadView { |
| 55 | _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero]; |
| 56 | _videoCallView.delegate = self; |
| 57 | _videoCallView.statusLabel.text = |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 58 | [self statusTextForState:RTCIceConnectionStateNew]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 59 | 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: |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 68 | RTCLog(@"Client connected."); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 69 | break; |
| 70 | case kARDAppClientStateConnecting: |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 71 | RTCLog(@"Client connecting."); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 72 | break; |
| 73 | case kARDAppClientStateDisconnected: |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 74 | RTCLog(@"Client disconnected."); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 75 | [self hangup]; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | - (void)appClient:(ARDAppClient *)client |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 81 | didChangeConnectionState:(RTCIceConnectionState)state { |
| 82 | RTCLog(@"ICE state changed: %ld", (long)state); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 83 | __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 |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 92 | 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.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 101 | didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | - (void)appClient:(ARDAppClient *)client |
| 105 | didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack { |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 106 | self.remoteVideoTrack = remoteVideoTrack; |
| 107 | _videoCallView.statusLabel.hidden = YES; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | - (void)appClient:(ARDAppClient *)client |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 111 | didGetStats:(NSArray *)stats { |
| 112 | _videoCallView.statsView.stats = stats; |
| 113 | [_videoCallView setNeedsLayout]; |
| 114 | } |
| 115 | |
| 116 | - (void)appClient:(ARDAppClient *)client |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 117 | didError:(NSError *)error { |
| 118 | NSString *message = |
| 119 | [NSString stringWithFormat:@"%@", error.localizedDescription]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 120 | [self hangup]; |
hewwatt | 7cc881d | 2017-05-18 01:33:34 -0700 | [diff] [blame] | 121 | [self showAlertWithMessage:message]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | #pragma mark - ARDVideoCallViewDelegate |
| 125 | |
| 126 | - (void)videoCallViewDidHangup:(ARDVideoCallView *)view { |
| 127 | [self hangup]; |
| 128 | } |
| 129 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 130 | - (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view { |
| 131 | // TODO(tkchin): Rate limit this so you can't tap continously on it. |
| 132 | // Probably through an animation. |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 133 | [_captureController switchCamera]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 134 | } |
| 135 | |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 136 | - (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 Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 156 | - (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view { |
| 157 | _client.shouldGetStats = YES; |
| 158 | _videoCallView.statsView.hidden = NO; |
| 159 | } |
| 160 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 161 | #pragma mark - Private |
| 162 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 163 | - (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack { |
| 164 | if (_remoteVideoTrack == remoteVideoTrack) { |
| 165 | return; |
| 166 | } |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 167 | [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 168 | _remoteVideoTrack = nil; |
| 169 | [_videoCallView.remoteVideoView renderFrame:nil]; |
| 170 | _remoteVideoTrack = remoteVideoTrack; |
| 171 | [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView]; |
| 172 | } |
| 173 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 174 | - (void)hangup { |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 175 | self.remoteVideoTrack = nil; |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 176 | _videoCallView.localVideoView.captureSession = nil; |
| 177 | [_captureController stopCapture]; |
| 178 | _captureController = nil; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 179 | [_client disconnect]; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 180 | [_delegate viewControllerDidFinish:self]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 181 | } |
| 182 | |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 183 | - (NSString *)statusTextForState:(RTCIceConnectionState)state { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 184 | switch (state) { |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 185 | case RTCIceConnectionStateNew: |
| 186 | case RTCIceConnectionStateChecking: |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 187 | return @"Connecting..."; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 188 | case RTCIceConnectionStateConnected: |
| 189 | case RTCIceConnectionStateCompleted: |
| 190 | case RTCIceConnectionStateFailed: |
| 191 | case RTCIceConnectionStateDisconnected: |
| 192 | case RTCIceConnectionStateClosed: |
hjon | 8bbbf2c | 2016-03-14 13:15:44 -0700 | [diff] [blame] | 193 | case RTCIceConnectionStateCount: |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 194 | return nil; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | - (void)showAlertWithMessage:(NSString*)message { |
kthelgason | b13237b | 2017-03-30 04:56:05 -0700 | [diff] [blame] | 199 | 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.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @end |