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