blob: d35e1e6b2d0274792d9da8526ca65032731cc065 [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
42 delegate:(id<ARDVideoCallViewControllerDelegate>)delegate {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000043 if (self = [super init]) {
tkchind2511962016-05-06 18:54:15 -070044 _delegate = delegate;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000045 _client = [[ARDAppClient alloc] initWithDelegate:self];
haysc913e6452015-10-02 11:44:03 -070046 [_client connectToRoomWithId:room
47 isLoopback:isLoopback
48 isAudioOnly:isAudioOnly];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000049 }
50 return self;
51}
52
53- (void)loadView {
54 _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero];
55 _videoCallView.delegate = self;
56 _videoCallView.statusLabel.text =
hjon79858f82016-03-13 22:08:26 -070057 [self statusTextForState:RTCIceConnectionStateNew];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000058 self.view = _videoCallView;
59}
60
61#pragma mark - ARDAppClientDelegate
62
63- (void)appClient:(ARDAppClient *)client
64 didChangeState:(ARDAppClientState)state {
65 switch (state) {
66 case kARDAppClientStateConnected:
tkchinc3f46a92015-07-23 12:50:55 -070067 RTCLog(@"Client connected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000068 break;
69 case kARDAppClientStateConnecting:
tkchinc3f46a92015-07-23 12:50:55 -070070 RTCLog(@"Client connecting.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000071 break;
72 case kARDAppClientStateDisconnected:
tkchinc3f46a92015-07-23 12:50:55 -070073 RTCLog(@"Client disconnected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000074 [self hangup];
75 break;
76 }
77}
78
79- (void)appClient:(ARDAppClient *)client
hjon79858f82016-03-13 22:08:26 -070080 didChangeConnectionState:(RTCIceConnectionState)state {
81 RTCLog(@"ICE state changed: %ld", (long)state);
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000082 __weak ARDVideoCallViewController *weakSelf = self;
83 dispatch_async(dispatch_get_main_queue(), ^{
84 ARDVideoCallViewController *strongSelf = weakSelf;
85 strongSelf.videoCallView.statusLabel.text =
86 [strongSelf statusTextForState:state];
87 });
88}
89
90- (void)appClient:(ARDAppClient *)client
91 didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -070092 self.localVideoTrack = localVideoTrack;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000093}
94
95- (void)appClient:(ARDAppClient *)client
96 didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -070097 self.remoteVideoTrack = remoteVideoTrack;
98 _videoCallView.statusLabel.hidden = YES;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000099}
100
101- (void)appClient:(ARDAppClient *)client
Zeke Chind3325802015-08-14 11:00:02 -0700102 didGetStats:(NSArray *)stats {
103 _videoCallView.statsView.stats = stats;
104 [_videoCallView setNeedsLayout];
105}
106
107- (void)appClient:(ARDAppClient *)client
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000108 didError:(NSError *)error {
109 NSString *message =
110 [NSString stringWithFormat:@"%@", error.localizedDescription];
111 [self showAlertWithMessage:message];
112 [self hangup];
113}
114
115#pragma mark - ARDVideoCallViewDelegate
116
117- (void)videoCallViewDidHangup:(ARDVideoCallView *)view {
118 [self hangup];
119}
120
Zeke Chin57cc74e2015-05-05 07:52:31 -0700121- (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view {
122 // TODO(tkchin): Rate limit this so you can't tap continously on it.
123 // Probably through an animation.
124 [self switchCamera];
125}
126
tkchin0ce3bf92016-03-12 16:52:04 -0800127- (void)videoCallViewDidChangeRoute:(ARDVideoCallView *)view {
128 AVAudioSessionPortOverride override = AVAudioSessionPortOverrideNone;
129 if (_portOverride == AVAudioSessionPortOverrideNone) {
130 override = AVAudioSessionPortOverrideSpeaker;
131 }
132 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeAudioSession
133 block:^{
134 RTCAudioSession *session = [RTCAudioSession sharedInstance];
135 [session lockForConfiguration];
136 NSError *error = nil;
137 if ([session overrideOutputAudioPort:override error:&error]) {
138 _portOverride = override;
139 } else {
140 RTCLogError(@"Error overriding output port: %@",
141 error.localizedDescription);
142 }
143 [session unlockForConfiguration];
144 }];
145}
146
Zeke Chind3325802015-08-14 11:00:02 -0700147- (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view {
148 _client.shouldGetStats = YES;
149 _videoCallView.statsView.hidden = NO;
150}
151
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000152#pragma mark - Private
153
Zeke Chin57cc74e2015-05-05 07:52:31 -0700154- (void)setLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
155 if (_localVideoTrack == localVideoTrack) {
156 return;
157 }
Zeke Chin57cc74e2015-05-05 07:52:31 -0700158 _localVideoTrack = nil;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700159 _localVideoTrack = localVideoTrack;
hayscedd8fef2015-12-08 11:08:39 -0800160 RTCAVFoundationVideoSource *source = nil;
161 if ([localVideoTrack.source
162 isKindOfClass:[RTCAVFoundationVideoSource class]]) {
163 source = (RTCAVFoundationVideoSource*)localVideoTrack.source;
164 }
165 _videoCallView.localVideoView.captureSession = source.captureSession;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700166}
167
168- (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
169 if (_remoteVideoTrack == remoteVideoTrack) {
170 return;
171 }
hayscedd8fef2015-12-08 11:08:39 -0800172 [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700173 _remoteVideoTrack = nil;
174 [_videoCallView.remoteVideoView renderFrame:nil];
175 _remoteVideoTrack = remoteVideoTrack;
176 [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView];
177}
178
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000179- (void)hangup {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700180 self.remoteVideoTrack = nil;
181 self.localVideoTrack = nil;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000182 [_client disconnect];
tkchind2511962016-05-06 18:54:15 -0700183 [_delegate viewControllerDidFinish:self];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000184}
185
Zeke Chin57cc74e2015-05-05 07:52:31 -0700186- (void)switchCamera {
187 RTCVideoSource* source = self.localVideoTrack.source;
188 if ([source isKindOfClass:[RTCAVFoundationVideoSource class]]) {
189 RTCAVFoundationVideoSource* avSource = (RTCAVFoundationVideoSource*)source;
190 avSource.useBackCamera = !avSource.useBackCamera;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700191 }
192}
193
hjon79858f82016-03-13 22:08:26 -0700194- (NSString *)statusTextForState:(RTCIceConnectionState)state {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000195 switch (state) {
hjon79858f82016-03-13 22:08:26 -0700196 case RTCIceConnectionStateNew:
197 case RTCIceConnectionStateChecking:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000198 return @"Connecting...";
hjon79858f82016-03-13 22:08:26 -0700199 case RTCIceConnectionStateConnected:
200 case RTCIceConnectionStateCompleted:
201 case RTCIceConnectionStateFailed:
202 case RTCIceConnectionStateDisconnected:
203 case RTCIceConnectionStateClosed:
hjon8bbbf2c2016-03-14 13:15:44 -0700204 case RTCIceConnectionStateCount:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000205 return nil;
206 }
207}
208
209- (void)showAlertWithMessage:(NSString*)message {
210 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil
211 message:message
212 delegate:nil
213 cancelButtonTitle:@"OK"
214 otherButtonTitles:nil];
215 [alertView show];
216}
217
218@end