blob: 51290a05b585bed47fe4074f17bf80b0cbec5026 [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
Zeke Chin57cc74e2015-05-05 07:52:31 -070013#import "RTCAVFoundationVideoSource.h"
tkchinc3f46a92015-07-23 12:50:55 -070014#import "RTCLogging.h"
Zeke Chin57cc74e2015-05-05 07:52:31 -070015
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000016#import "ARDAppClient.h"
17#import "ARDVideoCallView.h"
18
19@interface ARDVideoCallViewController () <ARDAppClientDelegate,
20 ARDVideoCallViewDelegate>
Zeke Chin57cc74e2015-05-05 07:52:31 -070021@property(nonatomic, strong) RTCVideoTrack *localVideoTrack;
22@property(nonatomic, strong) RTCVideoTrack *remoteVideoTrack;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000023@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
haysc913e6452015-10-02 11:44:03 -070034- (instancetype)initForRoom:(NSString *)room
35 isLoopback:(BOOL)isLoopback
36 isAudioOnly:(BOOL)isAudioOnly {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000037 if (self = [super init]) {
38 _client = [[ARDAppClient alloc] initWithDelegate:self];
haysc913e6452015-10-02 11:44:03 -070039 [_client connectToRoomWithId:room
40 isLoopback:isLoopback
41 isAudioOnly:isAudioOnly];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000042 }
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:
tkchinc3f46a92015-07-23 12:50:55 -070060 RTCLog(@"Client connected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000061 break;
62 case kARDAppClientStateConnecting:
tkchinc3f46a92015-07-23 12:50:55 -070063 RTCLog(@"Client connecting.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000064 break;
65 case kARDAppClientStateDisconnected:
tkchinc3f46a92015-07-23 12:50:55 -070066 RTCLog(@"Client disconnected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000067 [self hangup];
68 break;
69 }
70}
71
72- (void)appClient:(ARDAppClient *)client
73 didChangeConnectionState:(RTCICEConnectionState)state {
tkchinc3f46a92015-07-23 12:50:55 -070074 RTCLog(@"ICE state changed: %d", state);
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000075 __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 Chin57cc74e2015-05-05 07:52:31 -070085 self.localVideoTrack = localVideoTrack;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000086}
87
88- (void)appClient:(ARDAppClient *)client
89 didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -070090 self.remoteVideoTrack = remoteVideoTrack;
91 _videoCallView.statusLabel.hidden = YES;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000092}
93
94- (void)appClient:(ARDAppClient *)client
Zeke Chind3325802015-08-14 11:00:02 -070095 didGetStats:(NSArray *)stats {
96 _videoCallView.statsView.stats = stats;
97 [_videoCallView setNeedsLayout];
98}
99
100- (void)appClient:(ARDAppClient *)client
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000101 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 Chin57cc74e2015-05-05 07:52:31 -0700114- (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 Chind3325802015-08-14 11:00:02 -0700120- (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view {
121 _client.shouldGetStats = YES;
122 _videoCallView.statsView.hidden = NO;
123}
124
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000125#pragma mark - Private
126
Zeke Chin57cc74e2015-05-05 07:52:31 -0700127- (void)setLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
128 if (_localVideoTrack == localVideoTrack) {
129 return;
130 }
Zeke Chin57cc74e2015-05-05 07:52:31 -0700131 _localVideoTrack = nil;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700132 _localVideoTrack = localVideoTrack;
hayscedd8fef2015-12-08 11:08:39 -0800133 RTCAVFoundationVideoSource *source = nil;
134 if ([localVideoTrack.source
135 isKindOfClass:[RTCAVFoundationVideoSource class]]) {
136 source = (RTCAVFoundationVideoSource*)localVideoTrack.source;
137 }
138 _videoCallView.localVideoView.captureSession = source.captureSession;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700139}
140
141- (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
142 if (_remoteVideoTrack == remoteVideoTrack) {
143 return;
144 }
hayscedd8fef2015-12-08 11:08:39 -0800145 [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700146 _remoteVideoTrack = nil;
147 [_videoCallView.remoteVideoView renderFrame:nil];
148 _remoteVideoTrack = remoteVideoTrack;
149 [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView];
150}
151
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000152- (void)hangup {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700153 self.remoteVideoTrack = nil;
154 self.localVideoTrack = nil;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000155 [_client disconnect];
Jon Hjelle2f65ac12015-06-12 11:33:45 -0700156 if (![self isBeingDismissed]) {
157 [self.presentingViewController dismissViewControllerAnimated:YES
158 completion:nil];
159 }
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000160}
161
Zeke Chin57cc74e2015-05-05 07:52:31 -0700162- (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.orgef2a5dd2015-01-15 22:38:21 +0000172- (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:
hjonabd0d1a2015-09-01 15:35:38 -0700182 case RTCICEConnectionMax:
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000183 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