blob: 36c0902981910d7cbac5c413ad9110f118f0e19d [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
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 Chin57cc74e2015-05-05 07:52:31 -0700104- (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.orgef2a5dd2015-01-15 22:38:21 +0000110#pragma mark - Private
111
Zeke Chin57cc74e2015-05-05 07:52:31 -0700112- (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.orgef2a5dd2015-01-15 22:38:21 +0000134- (void)hangup {
Zeke Chin57cc74e2015-05-05 07:52:31 -0700135 self.remoteVideoTrack = nil;
136 self.localVideoTrack = nil;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000137 [_client disconnect];
Jon Hjelle2f65ac12015-06-12 11:33:45 -0700138 if (![self isBeingDismissed]) {
139 [self.presentingViewController dismissViewControllerAnimated:YES
140 completion:nil];
141 }
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000142}
143
Zeke Chin57cc74e2015-05-05 07:52:31 -0700144- (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.orgef2a5dd2015-01-15 22:38:21 +0000154- (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