blob: bb49ca4a36fe8c2c6fcc3628469358593228c5ca [file] [log] [blame]
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +00003 *
Donald E Curtisa8736442015-08-05 15:48:13 -07004 * 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.orgef2a5dd2015-01-15 22:38:21 +00009 */
10
11#import "ARDVideoCallViewController.h"
12
tkchin0ce3bf92016-03-12 16:52:04 -080013#import "webrtc/modules/audio_device/ios/objc/RTCAudioSession.h"
14
tkchin9eeb6242016-04-27 01:54:20 -070015#import "WebRTC/RTCAVFoundationVideoSource.h"
16#import "WebRTC/RTCDispatcher.h"
17#import "WebRTC/RTCLogging.h"
Zeke Chin57cc74e2015-05-05 07:52:31 -070018
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000019#import "ARDAppClient.h"
20#import "ARDVideoCallView.h"
21
22@interface ARDVideoCallViewController () <ARDAppClientDelegate,
23 ARDVideoCallViewDelegate>
Zeke Chin57cc74e2015-05-05 07:52:31 -070024@property(nonatomic, strong) RTCVideoTrack *localVideoTrack;
25@property(nonatomic, strong) RTCVideoTrack *remoteVideoTrack;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000026@property(nonatomic, readonly) ARDVideoCallView *videoCallView;
27@end
28
29@implementation ARDVideoCallViewController {
30 ARDAppClient *_client;
31 RTCVideoTrack *_remoteVideoTrack;
32 RTCVideoTrack *_localVideoTrack;
tkchin0ce3bf92016-03-12 16:52:04 -080033 AVAudioSessionPortOverride _portOverride;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000034}
35
36@synthesize videoCallView = _videoCallView;
tkchind2511962016-05-06 18:54:15 -070037@synthesize delegate = _delegate;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000038
haysc913e6452015-10-02 11:44:03 -070039- (instancetype)initForRoom:(NSString *)room
40 isLoopback:(BOOL)isLoopback
tkchind2511962016-05-06 18:54:15 -070041 isAudioOnly:(BOOL)isAudioOnly
peah5085b0c2016-08-25 22:15:14 -070042 shouldMakeAecDump:(BOOL)shouldMakeAecDump
tkchind2511962016-05-06 18:54:15 -070043 delegate:(id<ARDVideoCallViewControllerDelegate>)delegate {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000044 if (self = [super init]) {
tkchind2511962016-05-06 18:54:15 -070045 _delegate = delegate;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000046 _client = [[ARDAppClient alloc] initWithDelegate:self];
haysc913e6452015-10-02 11:44:03 -070047 [_client connectToRoomWithId:room
48 isLoopback:isLoopback
peah5085b0c2016-08-25 22:15:14 -070049 isAudioOnly:isAudioOnly
50 shouldMakeAecDump:shouldMakeAecDump];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000051 }
52 return self;
53}
54
55- (void)loadView {
56 _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero];
57 _videoCallView.delegate = self;
58 _videoCallView.statusLabel.text =
hjon79858f82016-03-13 22:08:26 -070059 [self statusTextForState:RTCIceConnectionStateNew];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000060 self.view = _videoCallView;
61}
62
63#pragma mark - ARDAppClientDelegate
64
65- (void)appClient:(ARDAppClient *)client
66 didChangeState:(ARDAppClientState)state {
67 switch (state) {
68 case kARDAppClientStateConnected:
tkchinc3f46a92015-07-23 12:50:55 -070069 RTCLog(@"Client connected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000070 break;
71 case kARDAppClientStateConnecting:
tkchinc3f46a92015-07-23 12:50:55 -070072 RTCLog(@"Client connecting.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000073 break;
74 case kARDAppClientStateDisconnected:
tkchinc3f46a92015-07-23 12:50:55 -070075 RTCLog(@"Client disconnected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000076 [self hangup];
77 break;
78 }
79}
80
81- (void)appClient:(ARDAppClient *)client
hjon79858f82016-03-13 22:08:26 -070082 didChangeConnectionState:(RTCIceConnectionState)state {
83 RTCLog(@"ICE state changed: %ld", (long)state);
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000084 __weak ARDVideoCallViewController *weakSelf = self;
85 dispatch_async(dispatch_get_main_queue(), ^{
86 ARDVideoCallViewController *strongSelf = weakSelf;
87 strongSelf.videoCallView.statusLabel.text =
88 [strongSelf statusTextForState:state];
89 });
90}
91
92- (void)appClient:(ARDAppClient *)client
93 didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -070094 self.localVideoTrack = localVideoTrack;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000095}
96
97- (void)appClient:(ARDAppClient *)client
98 didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -070099 self.remoteVideoTrack = remoteVideoTrack;
100 _videoCallView.statusLabel.hidden = YES;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000101}
102
103- (void)appClient:(ARDAppClient *)client
Zeke Chind3325802015-08-14 11:00:02 -0700104 didGetStats:(NSArray *)stats {
105 _videoCallView.statsView.stats = stats;
106 [_videoCallView setNeedsLayout];
107}
108
109- (void)appClient:(ARDAppClient *)client
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000110 didError:(NSError *)error {
111 NSString *message =
112 [NSString stringWithFormat:@"%@", error.localizedDescription];
113 [self showAlertWithMessage:message];
114 [self hangup];
115}
116
117#pragma mark - ARDVideoCallViewDelegate
118
119- (void)videoCallViewDidHangup:(ARDVideoCallView *)view {
120 [self hangup];
121}
122
Zeke Chin57cc74e2015-05-05 07:52:31 -0700123- (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view {
124 // TODO(tkchin): Rate limit this so you can't tap continously on it.
125 // Probably through an animation.
126 [self switchCamera];
127}
128
tkchin0ce3bf92016-03-12 16:52:04 -0800129- (void)videoCallViewDidChangeRoute:(ARDVideoCallView *)view {
130 AVAudioSessionPortOverride override = AVAudioSessionPortOverrideNone;
131 if (_portOverride == AVAudioSessionPortOverrideNone) {
132 override = AVAudioSessionPortOverrideSpeaker;
133 }
134 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeAudioSession
135 block:^{
136 RTCAudioSession *session = [RTCAudioSession sharedInstance];
137 [session lockForConfiguration];
138 NSError *error = nil;
139 if ([session overrideOutputAudioPort:override error:&error]) {
140 _portOverride = override;
141 } else {
142 RTCLogError(@"Error overriding output port: %@",
143 error.localizedDescription);
144 }
145 [session unlockForConfiguration];
146 }];
147}
148
Zeke Chind3325802015-08-14 11:00:02 -0700149- (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view {
150 _client.shouldGetStats = YES;
151 _videoCallView.statsView.hidden = NO;
152}
153
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000154#pragma mark - Private
155
Zeke Chin57cc74e2015-05-05 07:52:31 -0700156- (void)setLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
157 if (_localVideoTrack == localVideoTrack) {
158 return;
159 }
Zeke Chin57cc74e2015-05-05 07:52:31 -0700160 _localVideoTrack = nil;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700161 _localVideoTrack = localVideoTrack;
hayscedd8fef2015-12-08 11:08:39 -0800162 RTCAVFoundationVideoSource *source = nil;
163 if ([localVideoTrack.source
164 isKindOfClass:[RTCAVFoundationVideoSource class]]) {
165 source = (RTCAVFoundationVideoSource*)localVideoTrack.source;
166 }
167 _videoCallView.localVideoView.captureSession = source.captureSession;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700168}
169
170- (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
171 if (_remoteVideoTrack == remoteVideoTrack) {
172 return;
173 }
hayscedd8fef2015-12-08 11:08:39 -0800174 [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700175 _remoteVideoTrack = nil;
176 [_videoCallView.remoteVideoView renderFrame:nil];
177 _remoteVideoTrack = remoteVideoTrack;
178 [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView];
179}
180
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000181- (void)hangup {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700182 self.remoteVideoTrack = nil;
183 self.localVideoTrack = nil;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000184 [_client disconnect];
tkchind2511962016-05-06 18:54:15 -0700185 [_delegate viewControllerDidFinish:self];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000186}
187
Zeke Chin57cc74e2015-05-05 07:52:31 -0700188- (void)switchCamera {
189 RTCVideoSource* source = self.localVideoTrack.source;
190 if ([source isKindOfClass:[RTCAVFoundationVideoSource class]]) {
191 RTCAVFoundationVideoSource* avSource = (RTCAVFoundationVideoSource*)source;
192 avSource.useBackCamera = !avSource.useBackCamera;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700193 }
194}
195
hjon79858f82016-03-13 22:08:26 -0700196- (NSString *)statusTextForState:(RTCIceConnectionState)state {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000197 switch (state) {
hjon79858f82016-03-13 22:08:26 -0700198 case RTCIceConnectionStateNew:
199 case RTCIceConnectionStateChecking:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000200 return @"Connecting...";
hjon79858f82016-03-13 22:08:26 -0700201 case RTCIceConnectionStateConnected:
202 case RTCIceConnectionStateCompleted:
203 case RTCIceConnectionStateFailed:
204 case RTCIceConnectionStateDisconnected:
205 case RTCIceConnectionStateClosed:
hjon8bbbf2c2016-03-14 13:15:44 -0700206 case RTCIceConnectionStateCount:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000207 return nil;
208 }
209}
210
211- (void)showAlertWithMessage:(NSString*)message {
212 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil
213 message:message
214 delegate:nil
215 cancelButtonTitle:@"OK"
216 otherButtonTitles:nil];
217 [alertView show];
218}
219
220@end