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 |
| 91 | didError:(NSError *)error { |
| 92 | NSString *message = |
| 93 | [NSString stringWithFormat:@"%@", error.localizedDescription]; |
| 94 | [self showAlertWithMessage:message]; |
| 95 | [self hangup]; |
| 96 | } |
| 97 | |
| 98 | #pragma mark - ARDVideoCallViewDelegate |
| 99 | |
| 100 | - (void)videoCallViewDidHangup:(ARDVideoCallView *)view { |
| 101 | [self hangup]; |
| 102 | } |
| 103 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 104 | - (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view { |
| 105 | // TODO(tkchin): Rate limit this so you can't tap continously on it. |
| 106 | // Probably through an animation. |
| 107 | [self switchCamera]; |
| 108 | } |
| 109 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 110 | #pragma mark - Private |
| 111 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 112 | - (void)setLocalVideoTrack:(RTCVideoTrack *)localVideoTrack { |
| 113 | if (_localVideoTrack == localVideoTrack) { |
| 114 | return; |
| 115 | } |
| 116 | [_localVideoTrack removeRenderer:_videoCallView.localVideoView]; |
| 117 | _localVideoTrack = nil; |
| 118 | [_videoCallView.localVideoView renderFrame:nil]; |
| 119 | _localVideoTrack = localVideoTrack; |
| 120 | [_localVideoTrack addRenderer:_videoCallView.localVideoView]; |
| 121 | } |
| 122 | |
| 123 | - (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack { |
| 124 | if (_remoteVideoTrack == remoteVideoTrack) { |
| 125 | return; |
| 126 | } |
| 127 | [_remoteVideoTrack removeRenderer:_videoCallView.localVideoView]; |
| 128 | _remoteVideoTrack = nil; |
| 129 | [_videoCallView.remoteVideoView renderFrame:nil]; |
| 130 | _remoteVideoTrack = remoteVideoTrack; |
| 131 | [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView]; |
| 132 | } |
| 133 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 134 | - (void)hangup { |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 135 | self.remoteVideoTrack = nil; |
| 136 | self.localVideoTrack = nil; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 137 | [_client disconnect]; |
Jon Hjelle | 2f65ac1 | 2015-06-12 11:33:45 -0700 | [diff] [blame] | 138 | if (![self isBeingDismissed]) { |
| 139 | [self.presentingViewController dismissViewControllerAnimated:YES |
| 140 | completion:nil]; |
| 141 | } |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 144 | - (void)switchCamera { |
| 145 | RTCVideoSource* source = self.localVideoTrack.source; |
| 146 | if ([source isKindOfClass:[RTCAVFoundationVideoSource class]]) { |
| 147 | RTCAVFoundationVideoSource* avSource = (RTCAVFoundationVideoSource*)source; |
| 148 | avSource.useBackCamera = !avSource.useBackCamera; |
| 149 | _videoCallView.localVideoView.transform = avSource.useBackCamera ? |
| 150 | CGAffineTransformIdentity : CGAffineTransformMakeScale(-1, 1); |
| 151 | } |
| 152 | } |
| 153 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 154 | - (NSString *)statusTextForState:(RTCICEConnectionState)state { |
| 155 | switch (state) { |
| 156 | case RTCICEConnectionNew: |
| 157 | case RTCICEConnectionChecking: |
| 158 | return @"Connecting..."; |
| 159 | case RTCICEConnectionConnected: |
| 160 | case RTCICEConnectionCompleted: |
| 161 | case RTCICEConnectionFailed: |
| 162 | case RTCICEConnectionDisconnected: |
| 163 | case RTCICEConnectionClosed: |
| 164 | return nil; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | - (void)showAlertWithMessage:(NSString*)message { |
| 169 | UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil |
| 170 | message:message |
| 171 | delegate:nil |
| 172 | cancelButtonTitle:@"OK" |
| 173 | otherButtonTitles:nil]; |
| 174 | [alertView show]; |
| 175 | } |
| 176 | |
| 177 | @end |