blob: a26068f213d7724895e48f9c356094e98bc1af6a [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
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:
tkchinc3f46a92015-07-23 12:50:55 -070056 RTCLog(@"Client connected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000057 break;
58 case kARDAppClientStateConnecting:
tkchinc3f46a92015-07-23 12:50:55 -070059 RTCLog(@"Client connecting.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000060 break;
61 case kARDAppClientStateDisconnected:
tkchinc3f46a92015-07-23 12:50:55 -070062 RTCLog(@"Client disconnected.");
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000063 [self hangup];
64 break;
65 }
66}
67
68- (void)appClient:(ARDAppClient *)client
69 didChangeConnectionState:(RTCICEConnectionState)state {
tkchinc3f46a92015-07-23 12:50:55 -070070 RTCLog(@"ICE state changed: %d", state);
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000071 __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 Chin57cc74e2015-05-05 07:52:31 -070081 self.localVideoTrack = localVideoTrack;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000082}
83
84- (void)appClient:(ARDAppClient *)client
85 didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
Zeke Chin57cc74e2015-05-05 07:52:31 -070086 self.remoteVideoTrack = remoteVideoTrack;
87 _videoCallView.statusLabel.hidden = YES;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000088}
89
90- (void)appClient:(ARDAppClient *)client
Zeke Chind3325802015-08-14 11:00:02 -070091 didGetStats:(NSArray *)stats {
92 _videoCallView.statsView.stats = stats;
93 [_videoCallView setNeedsLayout];
94}
95
96- (void)appClient:(ARDAppClient *)client
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000097 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 Chin57cc74e2015-05-05 07:52:31 -0700110- (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 Chind3325802015-08-14 11:00:02 -0700116- (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view {
117 _client.shouldGetStats = YES;
118 _videoCallView.statsView.hidden = NO;
119}
120
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000121#pragma mark - Private
122
Zeke Chin57cc74e2015-05-05 07:52:31 -0700123- (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.orgef2a5dd2015-01-15 22:38:21 +0000145- (void)hangup {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700146 self.remoteVideoTrack = nil;
147 self.localVideoTrack = nil;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000148 [_client disconnect];
Jon Hjelle2f65ac12015-06-12 11:33:45 -0700149 if (![self isBeingDismissed]) {
150 [self.presentingViewController dismissViewControllerAnimated:YES
151 completion:nil];
152 }
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000153}
154
Zeke Chin57cc74e2015-05-05 07:52:31 -0700155- (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.orgef2a5dd2015-01-15 22:38:21 +0000165- (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:
175 return nil;
176 }
177}
178
179- (void)showAlertWithMessage:(NSString*)message {
180 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil
181 message:message
182 delegate:nil
183 cancelButtonTitle:@"OK"
184 otherButtonTitles:nil];
185 [alertView show];
186}
187
188@end