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 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 13 | #import "RTCAVFoundationVideoSource.h" |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 14 | #import "RTCLogging.h" |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 15 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 16 | #import "ARDAppClient.h" |
| 17 | #import "ARDVideoCallView.h" |
| 18 | |
| 19 | @interface ARDVideoCallViewController () <ARDAppClientDelegate, |
| 20 | ARDVideoCallViewDelegate> |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 21 | @property(nonatomic, strong) RTCVideoTrack *localVideoTrack; |
| 22 | @property(nonatomic, strong) RTCVideoTrack *remoteVideoTrack; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 23 | @property(nonatomic, readonly) ARDVideoCallView *videoCallView; |
| 24 | @end |
| 25 | |
| 26 | @implementation ARDVideoCallViewController { |
| 27 | ARDAppClient *_client; |
| 28 | RTCVideoTrack *_remoteVideoTrack; |
| 29 | RTCVideoTrack *_localVideoTrack; |
| 30 | } |
| 31 | |
| 32 | @synthesize videoCallView = _videoCallView; |
| 33 | |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 34 | - (instancetype)initForRoom:(NSString *)room |
| 35 | isLoopback:(BOOL)isLoopback |
| 36 | isAudioOnly:(BOOL)isAudioOnly { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 37 | if (self = [super init]) { |
| 38 | _client = [[ARDAppClient alloc] initWithDelegate:self]; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 39 | [_client connectToRoomWithId:room |
| 40 | isLoopback:isLoopback |
| 41 | isAudioOnly:isAudioOnly]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 42 | } |
| 43 | return self; |
| 44 | } |
| 45 | |
| 46 | - (void)loadView { |
| 47 | _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero]; |
| 48 | _videoCallView.delegate = self; |
| 49 | _videoCallView.statusLabel.text = |
| 50 | [self statusTextForState:RTCICEConnectionNew]; |
| 51 | self.view = _videoCallView; |
| 52 | } |
| 53 | |
| 54 | #pragma mark - ARDAppClientDelegate |
| 55 | |
| 56 | - (void)appClient:(ARDAppClient *)client |
| 57 | didChangeState:(ARDAppClientState)state { |
| 58 | switch (state) { |
| 59 | case kARDAppClientStateConnected: |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 60 | RTCLog(@"Client connected."); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 61 | break; |
| 62 | case kARDAppClientStateConnecting: |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 63 | RTCLog(@"Client connecting."); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 64 | break; |
| 65 | case kARDAppClientStateDisconnected: |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 66 | RTCLog(@"Client disconnected."); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 67 | [self hangup]; |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | - (void)appClient:(ARDAppClient *)client |
| 73 | didChangeConnectionState:(RTCICEConnectionState)state { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 74 | RTCLog(@"ICE state changed: %d", state); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 75 | __weak ARDVideoCallViewController *weakSelf = self; |
| 76 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 77 | ARDVideoCallViewController *strongSelf = weakSelf; |
| 78 | strongSelf.videoCallView.statusLabel.text = |
| 79 | [strongSelf statusTextForState:state]; |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | - (void)appClient:(ARDAppClient *)client |
| 84 | didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack { |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 85 | self.localVideoTrack = localVideoTrack; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | - (void)appClient:(ARDAppClient *)client |
| 89 | didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack { |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 90 | self.remoteVideoTrack = remoteVideoTrack; |
| 91 | _videoCallView.statusLabel.hidden = YES; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | - (void)appClient:(ARDAppClient *)client |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 95 | didGetStats:(NSArray *)stats { |
| 96 | _videoCallView.statsView.stats = stats; |
| 97 | [_videoCallView setNeedsLayout]; |
| 98 | } |
| 99 | |
| 100 | - (void)appClient:(ARDAppClient *)client |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 101 | didError:(NSError *)error { |
| 102 | NSString *message = |
| 103 | [NSString stringWithFormat:@"%@", error.localizedDescription]; |
| 104 | [self showAlertWithMessage:message]; |
| 105 | [self hangup]; |
| 106 | } |
| 107 | |
| 108 | #pragma mark - ARDVideoCallViewDelegate |
| 109 | |
| 110 | - (void)videoCallViewDidHangup:(ARDVideoCallView *)view { |
| 111 | [self hangup]; |
| 112 | } |
| 113 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 114 | - (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view { |
| 115 | // TODO(tkchin): Rate limit this so you can't tap continously on it. |
| 116 | // Probably through an animation. |
| 117 | [self switchCamera]; |
| 118 | } |
| 119 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 120 | - (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view { |
| 121 | _client.shouldGetStats = YES; |
| 122 | _videoCallView.statsView.hidden = NO; |
| 123 | } |
| 124 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 125 | #pragma mark - Private |
| 126 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 127 | - (void)setLocalVideoTrack:(RTCVideoTrack *)localVideoTrack { |
| 128 | if (_localVideoTrack == localVideoTrack) { |
| 129 | return; |
| 130 | } |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 131 | _localVideoTrack = nil; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 132 | _localVideoTrack = localVideoTrack; |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 133 | RTCAVFoundationVideoSource *source = nil; |
| 134 | if ([localVideoTrack.source |
| 135 | isKindOfClass:[RTCAVFoundationVideoSource class]]) { |
| 136 | source = (RTCAVFoundationVideoSource*)localVideoTrack.source; |
| 137 | } |
| 138 | _videoCallView.localVideoView.captureSession = source.captureSession; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | - (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack { |
| 142 | if (_remoteVideoTrack == remoteVideoTrack) { |
| 143 | return; |
| 144 | } |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 145 | [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 146 | _remoteVideoTrack = nil; |
| 147 | [_videoCallView.remoteVideoView renderFrame:nil]; |
| 148 | _remoteVideoTrack = remoteVideoTrack; |
| 149 | [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView]; |
| 150 | } |
| 151 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 152 | - (void)hangup { |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 153 | self.remoteVideoTrack = nil; |
| 154 | self.localVideoTrack = nil; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 155 | [_client disconnect]; |
Jon Hjelle | 2f65ac1 | 2015-06-12 11:33:45 -0700 | [diff] [blame] | 156 | if (![self isBeingDismissed]) { |
| 157 | [self.presentingViewController dismissViewControllerAnimated:YES |
| 158 | completion:nil]; |
| 159 | } |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 162 | - (void)switchCamera { |
| 163 | RTCVideoSource* source = self.localVideoTrack.source; |
| 164 | if ([source isKindOfClass:[RTCAVFoundationVideoSource class]]) { |
| 165 | RTCAVFoundationVideoSource* avSource = (RTCAVFoundationVideoSource*)source; |
| 166 | avSource.useBackCamera = !avSource.useBackCamera; |
| 167 | _videoCallView.localVideoView.transform = avSource.useBackCamera ? |
| 168 | CGAffineTransformIdentity : CGAffineTransformMakeScale(-1, 1); |
| 169 | } |
| 170 | } |
| 171 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 172 | - (NSString *)statusTextForState:(RTCICEConnectionState)state { |
| 173 | switch (state) { |
| 174 | case RTCICEConnectionNew: |
| 175 | case RTCICEConnectionChecking: |
| 176 | return @"Connecting..."; |
| 177 | case RTCICEConnectionConnected: |
| 178 | case RTCICEConnectionCompleted: |
| 179 | case RTCICEConnectionFailed: |
| 180 | case RTCICEConnectionDisconnected: |
| 181 | case RTCICEConnectionClosed: |
hjon | abd0d1a | 2015-09-01 15:35:38 -0700 | [diff] [blame] | 182 | case RTCICEConnectionMax: |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 183 | return nil; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | - (void)showAlertWithMessage:(NSString*)message { |
| 188 | UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil |
| 189 | message:message |
| 190 | delegate:nil |
| 191 | cancelButtonTitle:@"OK" |
| 192 | otherButtonTitles:nil]; |
| 193 | [alertView show]; |
| 194 | } |
| 195 | |
| 196 | @end |