blob: f753828f7537ad64ba5e7bf8603952b2b9fd0910 [file] [log] [blame]
tkchin@webrtc.org87776a82014-12-09 19:32:35 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +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.org87776a82014-12-09 19:32:35 +00009 */
10
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000011#import "ARDAppClient+Internal.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000012
tkchin9eeb6242016-04-27 01:54:20 -070013#import "WebRTC/RTCAVFoundationVideoSource.h"
tkchin9eeb6242016-04-27 01:54:20 -070014#import "WebRTC/RTCAudioTrack.h"
sakalc522e752017-04-05 12:17:48 -070015#import "WebRTC/RTCCameraVideoCapturer.h"
tkchin9eeb6242016-04-27 01:54:20 -070016#import "WebRTC/RTCConfiguration.h"
17#import "WebRTC/RTCFileLogger.h"
18#import "WebRTC/RTCIceServer.h"
19#import "WebRTC/RTCLogging.h"
20#import "WebRTC/RTCMediaConstraints.h"
21#import "WebRTC/RTCMediaStream.h"
22#import "WebRTC/RTCPeerConnectionFactory.h"
skvladf3569c82016-04-29 15:30:16 -070023#import "WebRTC/RTCRtpSender.h"
tkchin204177f2016-06-14 15:03:11 -070024#import "WebRTC/RTCTracing.h"
Anders Carlsson1d4c1522017-10-30 13:07:07 +010025#import "WebRTC/RTCVideoCodecFactory.h"
sakalc522e752017-04-05 12:17:48 -070026#import "WebRTC/RTCVideoTrack.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000027
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000028#import "ARDAppEngineClient.h"
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000029#import "ARDJoinResponse.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000030#import "ARDMessageResponse.h"
sakalc4adacf2017-03-28 01:22:48 -070031#import "ARDSettingsModel.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000032#import "ARDSignalingMessage.h"
sakalc4adacf2017-03-28 01:22:48 -070033#import "ARDTURNClient+Internal.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000034#import "ARDUtilities.h"
35#import "ARDWebSocketChannel.h"
hjon79858f82016-03-13 22:08:26 -070036#import "RTCIceCandidate+JSON.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000037#import "RTCSessionDescription+JSON.h"
Zeke Chin57cc74e2015-05-05 07:52:31 -070038
kthelgasoncc882af2017-01-13 05:59:46 -080039static NSString * const kARDIceServerRequestUrl = @"https://appr.tc/params";
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000040
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000041static NSString * const kARDAppClientErrorDomain = @"ARDAppClient";
42static NSInteger const kARDAppClientErrorUnknown = -1;
43static NSInteger const kARDAppClientErrorRoomFull = -2;
44static NSInteger const kARDAppClientErrorCreateSDP = -3;
45static NSInteger const kARDAppClientErrorSetSDP = -4;
46static NSInteger const kARDAppClientErrorInvalidClient = -5;
47static NSInteger const kARDAppClientErrorInvalidRoom = -6;
skvladf3569c82016-04-29 15:30:16 -070048static NSString * const kARDMediaStreamId = @"ARDAMS";
49static NSString * const kARDAudioTrackId = @"ARDAMSa0";
50static NSString * const kARDVideoTrackId = @"ARDAMSv0";
denicija9af2b602016-11-17 00:43:43 -080051static NSString * const kARDVideoTrackKind = @"video";
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000052
tkchin204177f2016-06-14 15:03:11 -070053// TODO(tkchin): Add these as UI options.
tkchind1fb26d2016-02-03 01:51:18 -080054static BOOL const kARDAppClientEnableTracing = NO;
tkchin204177f2016-06-14 15:03:11 -070055static BOOL const kARDAppClientEnableRtcEventLog = YES;
tkchinfce0e2c2016-08-30 12:58:11 -070056static int64_t const kARDAppClientAecDumpMaxSizeInBytes = 5e6; // 5 MB.
tkchin204177f2016-06-14 15:03:11 -070057static int64_t const kARDAppClientRtcEventLogMaxSizeInBytes = 5e6; // 5 MB.
denicija9af2b602016-11-17 00:43:43 -080058static int const kKbpsMultiplier = 1000;
tkchind1fb26d2016-02-03 01:51:18 -080059
Zeke Chind3325802015-08-14 11:00:02 -070060// We need a proxy to NSTimer because it causes a strong retain cycle. When
61// using the proxy, |invalidate| must be called before it properly deallocs.
62@interface ARDTimerProxy : NSObject
63
64- (instancetype)initWithInterval:(NSTimeInterval)interval
65 repeats:(BOOL)repeats
66 timerHandler:(void (^)(void))timerHandler;
67- (void)invalidate;
68
69@end
70
71@implementation ARDTimerProxy {
72 NSTimer *_timer;
73 void (^_timerHandler)(void);
Zeke Chin2d3b7e22015-07-14 12:55:44 -070074}
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000075
Zeke Chind3325802015-08-14 11:00:02 -070076- (instancetype)initWithInterval:(NSTimeInterval)interval
77 repeats:(BOOL)repeats
78 timerHandler:(void (^)(void))timerHandler {
79 NSParameterAssert(timerHandler);
80 if (self = [super init]) {
81 _timerHandler = timerHandler;
82 _timer = [NSTimer scheduledTimerWithTimeInterval:interval
83 target:self
84 selector:@selector(timerDidFire:)
85 userInfo:nil
86 repeats:repeats];
87 }
88 return self;
89}
90
91- (void)invalidate {
92 [_timer invalidate];
93}
94
95- (void)timerDidFire:(NSTimer *)timer {
96 _timerHandler();
97}
98
99@end
100
101@implementation ARDAppClient {
102 RTCFileLogger *_fileLogger;
103 ARDTimerProxy *_statsTimer;
sakalc4adacf2017-03-28 01:22:48 -0700104 ARDSettingsModel *_settings;
sakalc522e752017-04-05 12:17:48 -0700105 RTCVideoTrack *_localVideoTrack;
Zeke Chind3325802015-08-14 11:00:02 -0700106}
107
108@synthesize shouldGetStats = _shouldGetStats;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000109@synthesize state = _state;
Zeke Chind3325802015-08-14 11:00:02 -0700110@synthesize delegate = _delegate;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000111@synthesize roomServerClient = _roomServerClient;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000112@synthesize channel = _channel;
haysc913e6452015-10-02 11:44:03 -0700113@synthesize loopbackChannel = _loopbackChannel;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000114@synthesize turnClient = _turnClient;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000115@synthesize peerConnection = _peerConnection;
116@synthesize factory = _factory;
117@synthesize messageQueue = _messageQueue;
118@synthesize isTurnComplete = _isTurnComplete;
119@synthesize hasReceivedSdp = _hasReceivedSdp;
120@synthesize roomId = _roomId;
121@synthesize clientId = _clientId;
122@synthesize isInitiator = _isInitiator;
123@synthesize iceServers = _iceServers;
124@synthesize webSocketURL = _websocketURL;
125@synthesize webSocketRestURL = _websocketRestURL;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000126@synthesize defaultPeerConnectionConstraints =
127 _defaultPeerConnectionConstraints;
haysc913e6452015-10-02 11:44:03 -0700128@synthesize isLoopback = _isLoopback;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000129
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000130- (instancetype)init {
sakalc4adacf2017-03-28 01:22:48 -0700131 return [self initWithDelegate:nil];
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000132}
133
sakalc4adacf2017-03-28 01:22:48 -0700134- (instancetype)initWithDelegate:(id<ARDAppClientDelegate>)delegate {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000135 if (self = [super init]) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000136 _roomServerClient = [[ARDAppEngineClient alloc] init];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000137 _delegate = delegate;
kthelgasoncc882af2017-01-13 05:59:46 -0800138 NSURL *turnRequestURL = [NSURL URLWithString:kARDIceServerRequestUrl];
139 _turnClient = [[ARDTURNClient alloc] initWithURL:turnRequestURL];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000140 [self configure];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000141 }
142 return self;
143}
144
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000145// TODO(tkchin): Provide signaling channel factory interface so we can recreate
146// channel if we need to on network failure. Also, make this the default public
147// constructor.
148- (instancetype)initWithRoomServerClient:(id<ARDRoomServerClient>)rsClient
149 signalingChannel:(id<ARDSignalingChannel>)channel
150 turnClient:(id<ARDTURNClient>)turnClient
151 delegate:(id<ARDAppClientDelegate>)delegate {
152 NSParameterAssert(rsClient);
153 NSParameterAssert(channel);
154 NSParameterAssert(turnClient);
155 if (self = [super init]) {
156 _roomServerClient = rsClient;
157 _channel = channel;
158 _turnClient = turnClient;
159 _delegate = delegate;
160 [self configure];
161 }
162 return self;
163}
164
165- (void)configure {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000166 _messageQueue = [NSMutableArray array];
kthelgasoncc882af2017-01-13 05:59:46 -0800167 _iceServers = [NSMutableArray array];
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700168 _fileLogger = [[RTCFileLogger alloc] init];
169 [_fileLogger start];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000170}
171
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000172- (void)dealloc {
Zeke Chind3325802015-08-14 11:00:02 -0700173 self.shouldGetStats = NO;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000174 [self disconnect];
175}
176
Zeke Chind3325802015-08-14 11:00:02 -0700177- (void)setShouldGetStats:(BOOL)shouldGetStats {
178 if (_shouldGetStats == shouldGetStats) {
179 return;
180 }
181 if (shouldGetStats) {
182 __weak ARDAppClient *weakSelf = self;
183 _statsTimer = [[ARDTimerProxy alloc] initWithInterval:1
184 repeats:YES
185 timerHandler:^{
186 ARDAppClient *strongSelf = weakSelf;
hjon79858f82016-03-13 22:08:26 -0700187 [strongSelf.peerConnection statsForTrack:nil
188 statsOutputLevel:RTCStatsOutputLevelDebug
189 completionHandler:^(NSArray *stats) {
190 dispatch_async(dispatch_get_main_queue(), ^{
191 ARDAppClient *strongSelf = weakSelf;
192 [strongSelf.delegate appClient:strongSelf didGetStats:stats];
193 });
194 }];
Zeke Chind3325802015-08-14 11:00:02 -0700195 }];
196 } else {
197 [_statsTimer invalidate];
198 _statsTimer = nil;
199 }
200 _shouldGetStats = shouldGetStats;
201}
202
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000203- (void)setState:(ARDAppClientState)state {
204 if (_state == state) {
205 return;
206 }
207 _state = state;
208 [_delegate appClient:self didChangeState:_state];
209}
210
211- (void)connectToRoomWithId:(NSString *)roomId
sakalc4adacf2017-03-28 01:22:48 -0700212 settings:(ARDSettingsModel *)settings
Anders Carlssone1500582017-06-15 16:05:13 +0200213 isLoopback:(BOOL)isLoopback {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000214 NSParameterAssert(roomId.length);
215 NSParameterAssert(_state == kARDAppClientStateDisconnected);
sakalc4adacf2017-03-28 01:22:48 -0700216 _settings = settings;
haysc913e6452015-10-02 11:44:03 -0700217 _isLoopback = isLoopback;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000218 self.state = kARDAppClientStateConnecting;
219
Anders Carlsson1d4c1522017-10-30 13:07:07 +0100220 RTCDefaultVideoDecoderFactory *decoderFactory = [[RTCDefaultVideoDecoderFactory alloc] init];
221 RTCDefaultVideoEncoderFactory *encoderFactory = [[RTCDefaultVideoEncoderFactory alloc] init];
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200222 encoderFactory.preferredCodec = [settings currentVideoCodecSettingFromStore];
223 _factory = [[RTCPeerConnectionFactory alloc] initWithEncoderFactory:encoderFactory
224 decoderFactory:decoderFactory];
225
tkchind1fb26d2016-02-03 01:51:18 -0800226#if defined(WEBRTC_IOS)
227 if (kARDAppClientEnableTracing) {
tkchin204177f2016-06-14 15:03:11 -0700228 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-trace.txt"];
tkchind1fb26d2016-02-03 01:51:18 -0800229 RTCStartInternalCapture(filePath);
230 }
231#endif
232
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000233 // Request TURN.
234 __weak ARDAppClient *weakSelf = self;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000235 [_turnClient requestServersWithCompletionHandler:^(NSArray *turnServers,
236 NSError *error) {
237 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700238 RTCLogError("Error retrieving TURN servers: %@",
239 error.localizedDescription);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000240 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000241 ARDAppClient *strongSelf = weakSelf;
242 [strongSelf.iceServers addObjectsFromArray:turnServers];
243 strongSelf.isTurnComplete = YES;
244 [strongSelf startSignalingIfReady];
245 }];
jiayl@webrtc.org27f53172014-12-31 00:26:20 +0000246
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000247 // Join room on room server.
248 [_roomServerClient joinRoomWithRoomId:roomId
haysc913e6452015-10-02 11:44:03 -0700249 isLoopback:isLoopback
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000250 completionHandler:^(ARDJoinResponse *response, NSError *error) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000251 ARDAppClient *strongSelf = weakSelf;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000252 if (error) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000253 [strongSelf.delegate appClient:strongSelf didError:error];
254 return;
255 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000256 NSError *joinError =
257 [[strongSelf class] errorForJoinResultType:response.result];
258 if (joinError) {
tkchinc3f46a92015-07-23 12:50:55 -0700259 RTCLogError(@"Failed to join room:%@ on room server.", roomId);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000260 [strongSelf disconnect];
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000261 [strongSelf.delegate appClient:strongSelf didError:joinError];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000262 return;
263 }
tkchinc3f46a92015-07-23 12:50:55 -0700264 RTCLog(@"Joined room:%@ on room server.", roomId);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000265 strongSelf.roomId = response.roomId;
266 strongSelf.clientId = response.clientId;
267 strongSelf.isInitiator = response.isInitiator;
268 for (ARDSignalingMessage *message in response.messages) {
269 if (message.type == kARDSignalingMessageTypeOffer ||
270 message.type == kARDSignalingMessageTypeAnswer) {
271 strongSelf.hasReceivedSdp = YES;
272 [strongSelf.messageQueue insertObject:message atIndex:0];
273 } else {
274 [strongSelf.messageQueue addObject:message];
275 }
276 }
277 strongSelf.webSocketURL = response.webSocketURL;
278 strongSelf.webSocketRestURL = response.webSocketRestURL;
279 [strongSelf registerWithColliderIfReady];
280 [strongSelf startSignalingIfReady];
281 }];
282}
283
284- (void)disconnect {
285 if (_state == kARDAppClientStateDisconnected) {
286 return;
287 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000288 if (self.hasJoinedRoomServerRoom) {
289 [_roomServerClient leaveRoomWithRoomId:_roomId
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000290 clientId:_clientId
291 completionHandler:nil];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000292 }
293 if (_channel) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000294 if (_channel.state == kARDSignalingChannelStateRegistered) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000295 // Tell the other client we're hanging up.
296 ARDByeMessage *byeMessage = [[ARDByeMessage alloc] init];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000297 [_channel sendMessage:byeMessage];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000298 }
299 // Disconnect from collider.
300 _channel = nil;
301 }
302 _clientId = nil;
303 _roomId = nil;
304 _isInitiator = NO;
305 _hasReceivedSdp = NO;
306 _messageQueue = [NSMutableArray array];
sakalc522e752017-04-05 12:17:48 -0700307 _localVideoTrack = nil;
ivoc14d5dbe2016-07-04 07:06:55 -0700308#if defined(WEBRTC_IOS)
tkchinfce0e2c2016-08-30 12:58:11 -0700309 [_factory stopAecDump];
ivoc14d5dbe2016-07-04 07:06:55 -0700310 [_peerConnection stopRtcEventLog];
311#endif
magjedcc8b9062017-07-24 07:32:33 -0700312 [_peerConnection close];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000313 _peerConnection = nil;
314 self.state = kARDAppClientStateDisconnected;
tkchind1fb26d2016-02-03 01:51:18 -0800315#if defined(WEBRTC_IOS)
adam.fedorbcc5d872016-11-07 14:53:28 -0800316 if (kARDAppClientEnableTracing) {
317 RTCStopInternalCapture();
318 }
tkchind1fb26d2016-02-03 01:51:18 -0800319#endif
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000320}
321
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000322#pragma mark - ARDSignalingChannelDelegate
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000323
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000324- (void)channel:(id<ARDSignalingChannel>)channel
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000325 didReceiveMessage:(ARDSignalingMessage *)message {
326 switch (message.type) {
327 case kARDSignalingMessageTypeOffer:
328 case kARDSignalingMessageTypeAnswer:
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000329 // Offers and answers must be processed before any other message, so we
330 // place them at the front of the queue.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000331 _hasReceivedSdp = YES;
332 [_messageQueue insertObject:message atIndex:0];
333 break;
334 case kARDSignalingMessageTypeCandidate:
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700335 case kARDSignalingMessageTypeCandidateRemoval:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000336 [_messageQueue addObject:message];
337 break;
338 case kARDSignalingMessageTypeBye:
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000339 // Disconnects can be processed immediately.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000340 [self processSignalingMessage:message];
341 return;
342 }
343 [self drainMessageQueueIfReady];
344}
345
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000346- (void)channel:(id<ARDSignalingChannel>)channel
347 didChangeState:(ARDSignalingChannelState)state {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000348 switch (state) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000349 case kARDSignalingChannelStateOpen:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000350 break;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000351 case kARDSignalingChannelStateRegistered:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000352 break;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000353 case kARDSignalingChannelStateClosed:
354 case kARDSignalingChannelStateError:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000355 // TODO(tkchin): reconnection scenarios. Right now we just disconnect
356 // completely if the websocket connection fails.
357 [self disconnect];
358 break;
359 }
360}
361
362#pragma mark - RTCPeerConnectionDelegate
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000363// Callbacks for this delegate occur on non-main thread and need to be
364// dispatched back to main queue as needed.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000365
366- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700367 didChangeSignalingState:(RTCSignalingState)stateChanged {
368 RTCLog(@"Signaling state changed: %ld", (long)stateChanged);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000369}
370
371- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700372 didAddStream:(RTCMediaStream *)stream {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000373 dispatch_async(dispatch_get_main_queue(), ^{
tkchinc3f46a92015-07-23 12:50:55 -0700374 RTCLog(@"Received %lu video tracks and %lu audio tracks",
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000375 (unsigned long)stream.videoTracks.count,
376 (unsigned long)stream.audioTracks.count);
377 if (stream.videoTracks.count) {
378 RTCVideoTrack *videoTrack = stream.videoTracks[0];
379 [_delegate appClient:self didReceiveRemoteVideoTrack:videoTrack];
380 }
381 });
382}
383
384- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700385 didRemoveStream:(RTCMediaStream *)stream {
tkchinc3f46a92015-07-23 12:50:55 -0700386 RTCLog(@"Stream was removed.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000387}
388
hjon79858f82016-03-13 22:08:26 -0700389- (void)peerConnectionShouldNegotiate:(RTCPeerConnection *)peerConnection {
tkchinc3f46a92015-07-23 12:50:55 -0700390 RTCLog(@"WARNING: Renegotiation needed but unimplemented.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000391}
392
393- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700394 didChangeIceConnectionState:(RTCIceConnectionState)newState {
395 RTCLog(@"ICE state changed: %ld", (long)newState);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000396 dispatch_async(dispatch_get_main_queue(), ^{
397 [_delegate appClient:self didChangeConnectionState:newState];
398 });
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000399}
400
401- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700402 didChangeIceGatheringState:(RTCIceGatheringState)newState {
403 RTCLog(@"ICE gathering state changed: %ld", (long)newState);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000404}
405
406- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700407 didGenerateIceCandidate:(RTCIceCandidate *)candidate {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000408 dispatch_async(dispatch_get_main_queue(), ^{
409 ARDICECandidateMessage *message =
410 [[ARDICECandidateMessage alloc] initWithCandidate:candidate];
411 [self sendSignalingMessage:message];
412 });
413}
414
Zeke Chind3325802015-08-14 11:00:02 -0700415- (void)peerConnection:(RTCPeerConnection *)peerConnection
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700416 didRemoveIceCandidates:(NSArray<RTCIceCandidate *> *)candidates {
417 dispatch_async(dispatch_get_main_queue(), ^{
418 ARDICECandidateRemovalMessage *message =
419 [[ARDICECandidateRemovalMessage alloc]
420 initWithRemovedCandidates:candidates];
421 [self sendSignalingMessage:message];
422 });
423}
424
425- (void)peerConnection:(RTCPeerConnection *)peerConnection
Zeke Chind3325802015-08-14 11:00:02 -0700426 didOpenDataChannel:(RTCDataChannel *)dataChannel {
427}
428
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000429#pragma mark - RTCSessionDescriptionDelegate
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000430// Callbacks for this delegate occur on non-main thread and need to be
431// dispatched back to main queue as needed.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000432
433- (void)peerConnection:(RTCPeerConnection *)peerConnection
434 didCreateSessionDescription:(RTCSessionDescription *)sdp
435 error:(NSError *)error {
436 dispatch_async(dispatch_get_main_queue(), ^{
437 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700438 RTCLogError(@"Failed to create session description. Error: %@", error);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000439 [self disconnect];
440 NSDictionary *userInfo = @{
441 NSLocalizedDescriptionKey: @"Failed to create session description.",
442 };
443 NSError *sdpError =
444 [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
445 code:kARDAppClientErrorCreateSDP
446 userInfo:userInfo];
447 [_delegate appClient:self didError:sdpError];
448 return;
449 }
hjon79858f82016-03-13 22:08:26 -0700450 __weak ARDAppClient *weakSelf = self;
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200451 [_peerConnection setLocalDescription:sdp
hjon79858f82016-03-13 22:08:26 -0700452 completionHandler:^(NSError *error) {
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200453 ARDAppClient *strongSelf = weakSelf;
454 [strongSelf peerConnection:strongSelf.peerConnection
455 didSetSessionDescriptionWithError:error];
456 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000457 ARDSessionDescriptionMessage *message =
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200458 [[ARDSessionDescriptionMessage alloc] initWithDescription:sdp];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000459 [self sendSignalingMessage:message];
denicija9af2b602016-11-17 00:43:43 -0800460 [self setMaxBitrateForPeerConnectionVideoSender];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000461 });
462}
463
464- (void)peerConnection:(RTCPeerConnection *)peerConnection
465 didSetSessionDescriptionWithError:(NSError *)error {
466 dispatch_async(dispatch_get_main_queue(), ^{
467 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700468 RTCLogError(@"Failed to set session description. Error: %@", error);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000469 [self disconnect];
470 NSDictionary *userInfo = @{
471 NSLocalizedDescriptionKey: @"Failed to set session description.",
472 };
473 NSError *sdpError =
474 [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
475 code:kARDAppClientErrorSetSDP
476 userInfo:userInfo];
477 [_delegate appClient:self didError:sdpError];
478 return;
479 }
480 // If we're answering and we've just set the remote offer we need to create
481 // an answer and set the local description.
482 if (!_isInitiator && !_peerConnection.localDescription) {
483 RTCMediaConstraints *constraints = [self defaultAnswerConstraints];
hjon79858f82016-03-13 22:08:26 -0700484 __weak ARDAppClient *weakSelf = self;
485 [_peerConnection answerForConstraints:constraints
486 completionHandler:^(RTCSessionDescription *sdp,
487 NSError *error) {
488 ARDAppClient *strongSelf = weakSelf;
489 [strongSelf peerConnection:strongSelf.peerConnection
490 didCreateSessionDescription:sdp
491 error:error];
492 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000493 }
494 });
495}
496
497#pragma mark - Private
498
tkchin204177f2016-06-14 15:03:11 -0700499#if defined(WEBRTC_IOS)
500
501- (NSString *)documentsFilePathForFileName:(NSString *)fileName {
502 NSParameterAssert(fileName.length);
503 NSArray *paths = NSSearchPathForDirectoriesInDomains(
504 NSDocumentDirectory, NSUserDomainMask, YES);
505 NSString *documentsDirPath = paths.firstObject;
506 NSString *filePath =
507 [documentsDirPath stringByAppendingPathComponent:fileName];
508 return filePath;
509}
510
511#endif
512
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000513- (BOOL)hasJoinedRoomServerRoom {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000514 return _clientId.length;
515}
516
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000517// Begins the peer connection connection process if we have both joined a room
518// on the room server and tried to obtain a TURN server. Otherwise does nothing.
519// A peer connection object will be created with a stream that contains local
520// audio and video capture. If this client is the caller, an offer is created as
521// well, otherwise the client will wait for an offer to arrive.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000522- (void)startSignalingIfReady {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000523 if (!_isTurnComplete || !self.hasJoinedRoomServerRoom) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000524 return;
525 }
526 self.state = kARDAppClientStateConnected;
527
528 // Create peer connection.
529 RTCMediaConstraints *constraints = [self defaultPeerConnectionConstraints];
Zeke Chinbc7dd7e2015-05-29 14:59:14 -0700530 RTCConfiguration *config = [[RTCConfiguration alloc] init];
531 config.iceServers = _iceServers;
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700532 _peerConnection = [_factory peerConnectionWithConfiguration:config
533 constraints:constraints
534 delegate:self];
skvladf3569c82016-04-29 15:30:16 -0700535 // Create AV senders.
Alex Narestb3944f02017-10-13 14:56:18 +0200536 [self createMediaSenders];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000537 if (_isInitiator) {
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000538 // Send offer.
hjon79858f82016-03-13 22:08:26 -0700539 __weak ARDAppClient *weakSelf = self;
540 [_peerConnection offerForConstraints:[self defaultOfferConstraints]
541 completionHandler:^(RTCSessionDescription *sdp,
542 NSError *error) {
543 ARDAppClient *strongSelf = weakSelf;
544 [strongSelf peerConnection:strongSelf.peerConnection
545 didCreateSessionDescription:sdp
546 error:error];
547 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000548 } else {
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000549 // Check if we've received an offer.
550 [self drainMessageQueueIfReady];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000551 }
ivoc14d5dbe2016-07-04 07:06:55 -0700552#if defined(WEBRTC_IOS)
553 // Start event log.
554 if (kARDAppClientEnableRtcEventLog) {
555 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-rtceventlog"];
556 if (![_peerConnection startRtcEventLogWithFilePath:filePath
557 maxSizeInBytes:kARDAppClientRtcEventLogMaxSizeInBytes]) {
558 RTCLogError(@"Failed to start event logging.");
559 }
560 }
peah5085b0c2016-08-25 22:15:14 -0700561
562 // Start aecdump diagnostic recording.
Anders Carlssone1500582017-06-15 16:05:13 +0200563 if ([_settings currentCreateAecDumpSettingFromStore]) {
tkchinfce0e2c2016-08-30 12:58:11 -0700564 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-audio.aecdump"];
565 if (![_factory startAecDumpWithFilePath:filePath
566 maxSizeInBytes:kARDAppClientAecDumpMaxSizeInBytes]) {
567 RTCLogError(@"Failed to start aec dump.");
peah5085b0c2016-08-25 22:15:14 -0700568 }
569 }
ivoc14d5dbe2016-07-04 07:06:55 -0700570#endif
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000571}
572
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000573// Processes the messages that we've received from the room server and the
574// signaling channel. The offer or answer message must be processed before other
575// signaling messages, however they can arrive out of order. Hence, this method
576// only processes pending messages if there is a peer connection object and
577// if we have received either an offer or answer.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000578- (void)drainMessageQueueIfReady {
579 if (!_peerConnection || !_hasReceivedSdp) {
580 return;
581 }
582 for (ARDSignalingMessage *message in _messageQueue) {
583 [self processSignalingMessage:message];
584 }
585 [_messageQueue removeAllObjects];
586}
587
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000588// Processes the given signaling message based on its type.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000589- (void)processSignalingMessage:(ARDSignalingMessage *)message {
590 NSParameterAssert(_peerConnection ||
591 message.type == kARDSignalingMessageTypeBye);
592 switch (message.type) {
593 case kARDSignalingMessageTypeOffer:
594 case kARDSignalingMessageTypeAnswer: {
595 ARDSessionDescriptionMessage *sdpMessage =
596 (ARDSessionDescriptionMessage *)message;
597 RTCSessionDescription *description = sdpMessage.sessionDescription;
hjon79858f82016-03-13 22:08:26 -0700598 __weak ARDAppClient *weakSelf = self;
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200599 [_peerConnection setRemoteDescription:description
hjon79858f82016-03-13 22:08:26 -0700600 completionHandler:^(NSError *error) {
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200601 ARDAppClient *strongSelf = weakSelf;
602 [strongSelf peerConnection:strongSelf.peerConnection
603 didSetSessionDescriptionWithError:error];
604 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000605 break;
606 }
607 case kARDSignalingMessageTypeCandidate: {
608 ARDICECandidateMessage *candidateMessage =
609 (ARDICECandidateMessage *)message;
hjon79858f82016-03-13 22:08:26 -0700610 [_peerConnection addIceCandidate:candidateMessage.candidate];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000611 break;
612 }
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700613 case kARDSignalingMessageTypeCandidateRemoval: {
614 ARDICECandidateRemovalMessage *candidateMessage =
615 (ARDICECandidateRemovalMessage *)message;
616 [_peerConnection removeIceCandidates:candidateMessage.candidates];
617 break;
618 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000619 case kARDSignalingMessageTypeBye:
620 // Other client disconnected.
621 // TODO(tkchin): support waiting in room for next client. For now just
622 // disconnect.
623 [self disconnect];
624 break;
625 }
626}
627
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000628// Sends a signaling message to the other client. The caller will send messages
629// through the room server, whereas the callee will send messages over the
630// signaling channel.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000631- (void)sendSignalingMessage:(ARDSignalingMessage *)message {
632 if (_isInitiator) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000633 __weak ARDAppClient *weakSelf = self;
634 [_roomServerClient sendMessage:message
635 forRoomId:_roomId
636 clientId:_clientId
637 completionHandler:^(ARDMessageResponse *response,
638 NSError *error) {
639 ARDAppClient *strongSelf = weakSelf;
640 if (error) {
641 [strongSelf.delegate appClient:strongSelf didError:error];
642 return;
643 }
644 NSError *messageError =
645 [[strongSelf class] errorForMessageResultType:response.result];
646 if (messageError) {
647 [strongSelf.delegate appClient:strongSelf didError:messageError];
648 return;
649 }
650 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000651 } else {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000652 [_channel sendMessage:message];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000653 }
654}
655
denicija9af2b602016-11-17 00:43:43 -0800656- (void)setMaxBitrateForPeerConnectionVideoSender {
657 for (RTCRtpSender *sender in _peerConnection.senders) {
658 if (sender.track != nil) {
659 if ([sender.track.kind isEqualToString:kARDVideoTrackKind]) {
sakalc4adacf2017-03-28 01:22:48 -0700660 [self setMaxBitrate:[_settings currentMaxBitrateSettingFromStore] forVideoSender:sender];
denicija9af2b602016-11-17 00:43:43 -0800661 }
662 }
denicija8c375de2016-11-08 06:28:17 -0800663 }
664}
665
denicija9af2b602016-11-17 00:43:43 -0800666- (void)setMaxBitrate:(NSNumber *)maxBitrate forVideoSender:(RTCRtpSender *)sender {
667 if (maxBitrate.intValue <= 0) {
668 return;
669 }
670
671 RTCRtpParameters *parametersToModify = sender.parameters;
672 for (RTCRtpEncodingParameters *encoding in parametersToModify.encodings) {
673 encoding.maxBitrateBps = @(maxBitrate.intValue * kKbpsMultiplier);
674 }
675 [sender setParameters:parametersToModify];
676}
677
Alex Narestb3944f02017-10-13 14:56:18 +0200678- (void)createMediaSenders {
tkchinab1293a2016-08-30 12:35:05 -0700679 RTCMediaConstraints *constraints = [self defaultMediaAudioConstraints];
680 RTCAudioSource *source = [_factory audioSourceWithConstraints:constraints];
681 RTCAudioTrack *track = [_factory audioTrackWithSource:source
682 trackId:kARDAudioTrackId];
Alex Narestb3944f02017-10-13 14:56:18 +0200683 RTCMediaStream *stream = [_factory mediaStreamWithStreamId:kARDMediaStreamId];
684 [stream addAudioTrack:track];
685 _localVideoTrack = [self createLocalVideoTrack];
686 if (_localVideoTrack) {
687 [stream addVideoTrack:_localVideoTrack];
688 }
689 [_peerConnection addStream:stream];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700690}
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000691
Zeke Chin57cc74e2015-05-05 07:52:31 -0700692- (RTCVideoTrack *)createLocalVideoTrack {
693 RTCVideoTrack* localVideoTrack = nil;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000694 // The iOS simulator doesn't provide any sort of camera capture
695 // support or emulation (http://goo.gl/rHAnC1) so don't bother
696 // trying to open a local stream.
kthelgason314bc5f2016-08-31 10:23:27 -0700697#if !TARGET_IPHONE_SIMULATOR
Anders Carlssone1500582017-06-15 16:05:13 +0200698 if (![_settings currentAudioOnlySettingFromStore]) {
sakalc522e752017-04-05 12:17:48 -0700699 RTCVideoSource *source = [_factory videoSource];
700 RTCCameraVideoCapturer *capturer = [[RTCCameraVideoCapturer alloc] initWithDelegate:source];
701 [_delegate appClient:self didCreateLocalCapturer:capturer];
haysc913e6452015-10-02 11:44:03 -0700702 localVideoTrack =
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700703 [_factory videoTrackWithSource:source
skvladf3569c82016-04-29 15:30:16 -0700704 trackId:kARDVideoTrackId];
haysc913e6452015-10-02 11:44:03 -0700705 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000706#endif
Zeke Chin57cc74e2015-05-05 07:52:31 -0700707 return localVideoTrack;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000708}
709
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000710#pragma mark - Collider methods
711
712- (void)registerWithColliderIfReady {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000713 if (!self.hasJoinedRoomServerRoom) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000714 return;
715 }
716 // Open WebSocket connection.
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000717 if (!_channel) {
718 _channel =
719 [[ARDWebSocketChannel alloc] initWithURL:_websocketURL
720 restURL:_websocketRestURL
721 delegate:self];
haysc913e6452015-10-02 11:44:03 -0700722 if (_isLoopback) {
723 _loopbackChannel =
724 [[ARDLoopbackWebSocketChannel alloc] initWithURL:_websocketURL
725 restURL:_websocketRestURL];
726 }
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000727 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000728 [_channel registerForRoomId:_roomId clientId:_clientId];
haysc913e6452015-10-02 11:44:03 -0700729 if (_isLoopback) {
730 [_loopbackChannel registerForRoomId:_roomId clientId:@"LOOPBACK_CLIENT_ID"];
731 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000732}
733
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000734#pragma mark - Defaults
735
tkchinab1293a2016-08-30 12:35:05 -0700736 - (RTCMediaConstraints *)defaultMediaAudioConstraints {
Anders Carlssone1500582017-06-15 16:05:13 +0200737 NSString *valueLevelControl = [_settings currentUseLevelControllerSettingFromStore] ?
738 kRTCMediaConstraintsValueTrue :
739 kRTCMediaConstraintsValueFalse;
tkchinab1293a2016-08-30 12:35:05 -0700740 NSDictionary *mandatoryConstraints = @{ kRTCMediaConstraintsLevelControl : valueLevelControl };
denicijad17d5362016-11-02 02:56:09 -0700741 RTCMediaConstraints *constraints =
742 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstraints
743 optionalConstraints:nil];
tkchinab1293a2016-08-30 12:35:05 -0700744 return constraints;
745}
746
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000747- (RTCMediaConstraints *)defaultAnswerConstraints {
748 return [self defaultOfferConstraints];
749}
750
751- (RTCMediaConstraints *)defaultOfferConstraints {
hjon79858f82016-03-13 22:08:26 -0700752 NSDictionary *mandatoryConstraints = @{
753 @"OfferToReceiveAudio" : @"true",
754 @"OfferToReceiveVideo" : @"true"
755 };
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000756 RTCMediaConstraints* constraints =
757 [[RTCMediaConstraints alloc]
758 initWithMandatoryConstraints:mandatoryConstraints
759 optionalConstraints:nil];
760 return constraints;
761}
762
763- (RTCMediaConstraints *)defaultPeerConnectionConstraints {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000764 if (_defaultPeerConnectionConstraints) {
765 return _defaultPeerConnectionConstraints;
766 }
haysc913e6452015-10-02 11:44:03 -0700767 NSString *value = _isLoopback ? @"false" : @"true";
hjon79858f82016-03-13 22:08:26 -0700768 NSDictionary *optionalConstraints = @{ @"DtlsSrtpKeyAgreement" : value };
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000769 RTCMediaConstraints* constraints =
770 [[RTCMediaConstraints alloc]
771 initWithMandatoryConstraints:nil
772 optionalConstraints:optionalConstraints];
773 return constraints;
774}
775
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000776#pragma mark - Errors
777
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000778+ (NSError *)errorForJoinResultType:(ARDJoinResultType)resultType {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000779 NSError *error = nil;
780 switch (resultType) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000781 case kARDJoinResultTypeSuccess:
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000782 break;
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000783 case kARDJoinResultTypeUnknown: {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000784 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
785 code:kARDAppClientErrorUnknown
786 userInfo:@{
787 NSLocalizedDescriptionKey: @"Unknown error.",
788 }];
789 break;
790 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000791 case kARDJoinResultTypeFull: {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000792 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
793 code:kARDAppClientErrorRoomFull
794 userInfo:@{
795 NSLocalizedDescriptionKey: @"Room is full.",
796 }];
797 break;
798 }
799 }
800 return error;
801}
802
803+ (NSError *)errorForMessageResultType:(ARDMessageResultType)resultType {
804 NSError *error = nil;
805 switch (resultType) {
806 case kARDMessageResultTypeSuccess:
807 break;
808 case kARDMessageResultTypeUnknown:
809 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
810 code:kARDAppClientErrorUnknown
811 userInfo:@{
812 NSLocalizedDescriptionKey: @"Unknown error.",
813 }];
814 break;
815 case kARDMessageResultTypeInvalidClient:
816 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
817 code:kARDAppClientErrorInvalidClient
818 userInfo:@{
819 NSLocalizedDescriptionKey: @"Invalid client.",
820 }];
821 break;
822 case kARDMessageResultTypeInvalidRoom:
823 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
824 code:kARDAppClientErrorInvalidRoom
825 userInfo:@{
826 NSLocalizedDescriptionKey: @"Invalid room.",
827 }];
828 break;
829 }
830 return error;
831}
832
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000833@end