blob: 9f6195167084ccd265778472cb5369b4bc315d57 [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
Zeke Chin57cc74e2015-05-05 07:52:31 -070013#if defined(WEBRTC_IOS)
tkchin9eeb6242016-04-27 01:54:20 -070014#import "WebRTC/RTCAVFoundationVideoSource.h"
Zeke Chin57cc74e2015-05-05 07:52:31 -070015#endif
tkchin9eeb6242016-04-27 01:54:20 -070016#import "WebRTC/RTCAudioTrack.h"
17#import "WebRTC/RTCConfiguration.h"
18#import "WebRTC/RTCFileLogger.h"
19#import "WebRTC/RTCIceServer.h"
20#import "WebRTC/RTCLogging.h"
21#import "WebRTC/RTCMediaConstraints.h"
22#import "WebRTC/RTCMediaStream.h"
23#import "WebRTC/RTCPeerConnectionFactory.h"
skvladf3569c82016-04-29 15:30:16 -070024#import "WebRTC/RTCRtpSender.h"
tkchin204177f2016-06-14 15:03:11 -070025#import "WebRTC/RTCTracing.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000026
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000027#import "ARDAppEngineClient.h"
28#import "ARDCEODTURNClient.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"
Zeke Chin71f6f442015-06-29 14:34:58 -070031#import "ARDSDPUtils.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000032#import "ARDSignalingMessage.h"
33#import "ARDUtilities.h"
34#import "ARDWebSocketChannel.h"
hjon79858f82016-03-13 22:08:26 -070035#import "RTCIceCandidate+JSON.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000036#import "RTCSessionDescription+JSON.h"
Zeke Chin57cc74e2015-05-05 07:52:31 -070037
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000038static NSString * const kARDDefaultSTUNServerUrl =
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000039 @"stun:stun.l.google.com:19302";
40// TODO(tkchin): figure out a better username for CEOD statistics.
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000041static NSString * const kARDTurnRequestUrl =
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000042 @"https://computeengineondemand.appspot.com"
43 @"/turn?username=iapprtc&key=4080218913";
44
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000045static NSString * const kARDAppClientErrorDomain = @"ARDAppClient";
46static NSInteger const kARDAppClientErrorUnknown = -1;
47static NSInteger const kARDAppClientErrorRoomFull = -2;
48static NSInteger const kARDAppClientErrorCreateSDP = -3;
49static NSInteger const kARDAppClientErrorSetSDP = -4;
50static NSInteger const kARDAppClientErrorInvalidClient = -5;
51static NSInteger const kARDAppClientErrorInvalidRoom = -6;
skvladf3569c82016-04-29 15:30:16 -070052static NSString * const kARDMediaStreamId = @"ARDAMS";
53static NSString * const kARDAudioTrackId = @"ARDAMSa0";
54static NSString * const kARDVideoTrackId = @"ARDAMSv0";
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000055
tkchin9eeb6242016-04-27 01:54:20 -070056// TODO(tkchin): Remove guard once rtc_sdk_common_objc compiles on Mac.
tkchind1fb26d2016-02-03 01:51:18 -080057#if defined(WEBRTC_IOS)
tkchin204177f2016-06-14 15:03:11 -070058// TODO(tkchin): Add these as UI options.
tkchind1fb26d2016-02-03 01:51:18 -080059static BOOL const kARDAppClientEnableTracing = NO;
tkchin204177f2016-06-14 15:03:11 -070060static BOOL const kARDAppClientEnableRtcEventLog = YES;
tkchinfce0e2c2016-08-30 12:58:11 -070061static int64_t const kARDAppClientAecDumpMaxSizeInBytes = 5e6; // 5 MB.
tkchin204177f2016-06-14 15:03:11 -070062static int64_t const kARDAppClientRtcEventLogMaxSizeInBytes = 5e6; // 5 MB.
tkchind1fb26d2016-02-03 01:51:18 -080063#endif
64
Zeke Chind3325802015-08-14 11:00:02 -070065// We need a proxy to NSTimer because it causes a strong retain cycle. When
66// using the proxy, |invalidate| must be called before it properly deallocs.
67@interface ARDTimerProxy : NSObject
68
69- (instancetype)initWithInterval:(NSTimeInterval)interval
70 repeats:(BOOL)repeats
71 timerHandler:(void (^)(void))timerHandler;
72- (void)invalidate;
73
74@end
75
76@implementation ARDTimerProxy {
77 NSTimer *_timer;
78 void (^_timerHandler)(void);
Zeke Chin2d3b7e22015-07-14 12:55:44 -070079}
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000080
Zeke Chind3325802015-08-14 11:00:02 -070081- (instancetype)initWithInterval:(NSTimeInterval)interval
82 repeats:(BOOL)repeats
83 timerHandler:(void (^)(void))timerHandler {
84 NSParameterAssert(timerHandler);
85 if (self = [super init]) {
86 _timerHandler = timerHandler;
87 _timer = [NSTimer scheduledTimerWithTimeInterval:interval
88 target:self
89 selector:@selector(timerDidFire:)
90 userInfo:nil
91 repeats:repeats];
92 }
93 return self;
94}
95
96- (void)invalidate {
97 [_timer invalidate];
98}
99
100- (void)timerDidFire:(NSTimer *)timer {
101 _timerHandler();
102}
103
104@end
105
106@implementation ARDAppClient {
107 RTCFileLogger *_fileLogger;
108 ARDTimerProxy *_statsTimer;
109}
110
111@synthesize shouldGetStats = _shouldGetStats;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000112@synthesize state = _state;
Zeke Chind3325802015-08-14 11:00:02 -0700113@synthesize delegate = _delegate;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000114@synthesize roomServerClient = _roomServerClient;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000115@synthesize channel = _channel;
haysc913e6452015-10-02 11:44:03 -0700116@synthesize loopbackChannel = _loopbackChannel;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000117@synthesize turnClient = _turnClient;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000118@synthesize peerConnection = _peerConnection;
119@synthesize factory = _factory;
120@synthesize messageQueue = _messageQueue;
121@synthesize isTurnComplete = _isTurnComplete;
122@synthesize hasReceivedSdp = _hasReceivedSdp;
123@synthesize roomId = _roomId;
124@synthesize clientId = _clientId;
125@synthesize isInitiator = _isInitiator;
126@synthesize iceServers = _iceServers;
127@synthesize webSocketURL = _websocketURL;
128@synthesize webSocketRestURL = _websocketRestURL;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000129@synthesize defaultPeerConnectionConstraints =
130 _defaultPeerConnectionConstraints;
haysc913e6452015-10-02 11:44:03 -0700131@synthesize isLoopback = _isLoopback;
132@synthesize isAudioOnly = _isAudioOnly;
peah5085b0c2016-08-25 22:15:14 -0700133@synthesize shouldMakeAecDump = _shouldMakeAecDump;
tkchinab1293a2016-08-30 12:35:05 -0700134@synthesize shouldUseLevelControl = _shouldUseLevelControl;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000135
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000136- (instancetype)init {
137 if (self = [super init]) {
138 _roomServerClient = [[ARDAppEngineClient alloc] init];
139 NSURL *turnRequestURL = [NSURL URLWithString:kARDTurnRequestUrl];
140 _turnClient = [[ARDCEODTURNClient alloc] initWithURL:turnRequestURL];
141 [self configure];
142 }
143 return self;
144}
145
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000146- (instancetype)initWithDelegate:(id<ARDAppClientDelegate>)delegate {
147 if (self = [super init]) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000148 _roomServerClient = [[ARDAppEngineClient alloc] init];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000149 _delegate = delegate;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000150 NSURL *turnRequestURL = [NSURL URLWithString:kARDTurnRequestUrl];
151 _turnClient = [[ARDCEODTURNClient alloc] initWithURL:turnRequestURL];
152 [self configure];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000153 }
154 return self;
155}
156
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000157// TODO(tkchin): Provide signaling channel factory interface so we can recreate
158// channel if we need to on network failure. Also, make this the default public
159// constructor.
160- (instancetype)initWithRoomServerClient:(id<ARDRoomServerClient>)rsClient
161 signalingChannel:(id<ARDSignalingChannel>)channel
162 turnClient:(id<ARDTURNClient>)turnClient
163 delegate:(id<ARDAppClientDelegate>)delegate {
164 NSParameterAssert(rsClient);
165 NSParameterAssert(channel);
166 NSParameterAssert(turnClient);
167 if (self = [super init]) {
168 _roomServerClient = rsClient;
169 _channel = channel;
170 _turnClient = turnClient;
171 _delegate = delegate;
172 [self configure];
173 }
174 return self;
175}
176
177- (void)configure {
178 _factory = [[RTCPeerConnectionFactory alloc] init];
179 _messageQueue = [NSMutableArray array];
180 _iceServers = [NSMutableArray arrayWithObject:[self defaultSTUNServer]];
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700181 _fileLogger = [[RTCFileLogger alloc] init];
182 [_fileLogger start];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000183}
184
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000185- (void)dealloc {
Zeke Chind3325802015-08-14 11:00:02 -0700186 self.shouldGetStats = NO;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000187 [self disconnect];
188}
189
Zeke Chind3325802015-08-14 11:00:02 -0700190- (void)setShouldGetStats:(BOOL)shouldGetStats {
191 if (_shouldGetStats == shouldGetStats) {
192 return;
193 }
194 if (shouldGetStats) {
195 __weak ARDAppClient *weakSelf = self;
196 _statsTimer = [[ARDTimerProxy alloc] initWithInterval:1
197 repeats:YES
198 timerHandler:^{
199 ARDAppClient *strongSelf = weakSelf;
hjon79858f82016-03-13 22:08:26 -0700200 [strongSelf.peerConnection statsForTrack:nil
201 statsOutputLevel:RTCStatsOutputLevelDebug
202 completionHandler:^(NSArray *stats) {
203 dispatch_async(dispatch_get_main_queue(), ^{
204 ARDAppClient *strongSelf = weakSelf;
205 [strongSelf.delegate appClient:strongSelf didGetStats:stats];
206 });
207 }];
Zeke Chind3325802015-08-14 11:00:02 -0700208 }];
209 } else {
210 [_statsTimer invalidate];
211 _statsTimer = nil;
212 }
213 _shouldGetStats = shouldGetStats;
214}
215
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000216- (void)setState:(ARDAppClientState)state {
217 if (_state == state) {
218 return;
219 }
220 _state = state;
221 [_delegate appClient:self didChangeState:_state];
222}
223
224- (void)connectToRoomWithId:(NSString *)roomId
haysc913e6452015-10-02 11:44:03 -0700225 isLoopback:(BOOL)isLoopback
peah5085b0c2016-08-25 22:15:14 -0700226 isAudioOnly:(BOOL)isAudioOnly
tkchinab1293a2016-08-30 12:35:05 -0700227 shouldMakeAecDump:(BOOL)shouldMakeAecDump
228 shouldUseLevelControl:(BOOL)shouldUseLevelControl {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000229 NSParameterAssert(roomId.length);
230 NSParameterAssert(_state == kARDAppClientStateDisconnected);
haysc913e6452015-10-02 11:44:03 -0700231 _isLoopback = isLoopback;
232 _isAudioOnly = isAudioOnly;
peah5085b0c2016-08-25 22:15:14 -0700233 _shouldMakeAecDump = shouldMakeAecDump;
tkchinab1293a2016-08-30 12:35:05 -0700234 _shouldUseLevelControl = shouldUseLevelControl;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000235 self.state = kARDAppClientStateConnecting;
236
tkchind1fb26d2016-02-03 01:51:18 -0800237#if defined(WEBRTC_IOS)
238 if (kARDAppClientEnableTracing) {
tkchin204177f2016-06-14 15:03:11 -0700239 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-trace.txt"];
tkchind1fb26d2016-02-03 01:51:18 -0800240 RTCStartInternalCapture(filePath);
241 }
242#endif
243
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000244 // Request TURN.
245 __weak ARDAppClient *weakSelf = self;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000246 [_turnClient requestServersWithCompletionHandler:^(NSArray *turnServers,
247 NSError *error) {
248 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700249 RTCLogError("Error retrieving TURN servers: %@",
250 error.localizedDescription);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000251 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000252 ARDAppClient *strongSelf = weakSelf;
253 [strongSelf.iceServers addObjectsFromArray:turnServers];
254 strongSelf.isTurnComplete = YES;
255 [strongSelf startSignalingIfReady];
256 }];
jiayl@webrtc.org27f53172014-12-31 00:26:20 +0000257
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000258 // Join room on room server.
259 [_roomServerClient joinRoomWithRoomId:roomId
haysc913e6452015-10-02 11:44:03 -0700260 isLoopback:isLoopback
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000261 completionHandler:^(ARDJoinResponse *response, NSError *error) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000262 ARDAppClient *strongSelf = weakSelf;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000263 if (error) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000264 [strongSelf.delegate appClient:strongSelf didError:error];
265 return;
266 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000267 NSError *joinError =
268 [[strongSelf class] errorForJoinResultType:response.result];
269 if (joinError) {
tkchinc3f46a92015-07-23 12:50:55 -0700270 RTCLogError(@"Failed to join room:%@ on room server.", roomId);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000271 [strongSelf disconnect];
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000272 [strongSelf.delegate appClient:strongSelf didError:joinError];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000273 return;
274 }
tkchinc3f46a92015-07-23 12:50:55 -0700275 RTCLog(@"Joined room:%@ on room server.", roomId);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000276 strongSelf.roomId = response.roomId;
277 strongSelf.clientId = response.clientId;
278 strongSelf.isInitiator = response.isInitiator;
279 for (ARDSignalingMessage *message in response.messages) {
280 if (message.type == kARDSignalingMessageTypeOffer ||
281 message.type == kARDSignalingMessageTypeAnswer) {
282 strongSelf.hasReceivedSdp = YES;
283 [strongSelf.messageQueue insertObject:message atIndex:0];
284 } else {
285 [strongSelf.messageQueue addObject:message];
286 }
287 }
288 strongSelf.webSocketURL = response.webSocketURL;
289 strongSelf.webSocketRestURL = response.webSocketRestURL;
290 [strongSelf registerWithColliderIfReady];
291 [strongSelf startSignalingIfReady];
292 }];
293}
294
295- (void)disconnect {
296 if (_state == kARDAppClientStateDisconnected) {
297 return;
298 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000299 if (self.hasJoinedRoomServerRoom) {
300 [_roomServerClient leaveRoomWithRoomId:_roomId
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000301 clientId:_clientId
302 completionHandler:nil];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000303 }
304 if (_channel) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000305 if (_channel.state == kARDSignalingChannelStateRegistered) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000306 // Tell the other client we're hanging up.
307 ARDByeMessage *byeMessage = [[ARDByeMessage alloc] init];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000308 [_channel sendMessage:byeMessage];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000309 }
310 // Disconnect from collider.
311 _channel = nil;
312 }
313 _clientId = nil;
314 _roomId = nil;
315 _isInitiator = NO;
316 _hasReceivedSdp = NO;
317 _messageQueue = [NSMutableArray array];
ivoc14d5dbe2016-07-04 07:06:55 -0700318#if defined(WEBRTC_IOS)
tkchinfce0e2c2016-08-30 12:58:11 -0700319 [_factory stopAecDump];
ivoc14d5dbe2016-07-04 07:06:55 -0700320 [_peerConnection stopRtcEventLog];
321#endif
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000322 _peerConnection = nil;
323 self.state = kARDAppClientStateDisconnected;
tkchind1fb26d2016-02-03 01:51:18 -0800324#if defined(WEBRTC_IOS)
325 RTCStopInternalCapture();
326#endif
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000327}
328
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000329#pragma mark - ARDSignalingChannelDelegate
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000330
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000331- (void)channel:(id<ARDSignalingChannel>)channel
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000332 didReceiveMessage:(ARDSignalingMessage *)message {
333 switch (message.type) {
334 case kARDSignalingMessageTypeOffer:
335 case kARDSignalingMessageTypeAnswer:
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000336 // Offers and answers must be processed before any other message, so we
337 // place them at the front of the queue.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000338 _hasReceivedSdp = YES;
339 [_messageQueue insertObject:message atIndex:0];
340 break;
341 case kARDSignalingMessageTypeCandidate:
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700342 case kARDSignalingMessageTypeCandidateRemoval:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000343 [_messageQueue addObject:message];
344 break;
345 case kARDSignalingMessageTypeBye:
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000346 // Disconnects can be processed immediately.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000347 [self processSignalingMessage:message];
348 return;
349 }
350 [self drainMessageQueueIfReady];
351}
352
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000353- (void)channel:(id<ARDSignalingChannel>)channel
354 didChangeState:(ARDSignalingChannelState)state {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000355 switch (state) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000356 case kARDSignalingChannelStateOpen:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000357 break;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000358 case kARDSignalingChannelStateRegistered:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000359 break;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000360 case kARDSignalingChannelStateClosed:
361 case kARDSignalingChannelStateError:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000362 // TODO(tkchin): reconnection scenarios. Right now we just disconnect
363 // completely if the websocket connection fails.
364 [self disconnect];
365 break;
366 }
367}
368
369#pragma mark - RTCPeerConnectionDelegate
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000370// Callbacks for this delegate occur on non-main thread and need to be
371// dispatched back to main queue as needed.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000372
373- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700374 didChangeSignalingState:(RTCSignalingState)stateChanged {
375 RTCLog(@"Signaling state changed: %ld", (long)stateChanged);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000376}
377
378- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700379 didAddStream:(RTCMediaStream *)stream {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000380 dispatch_async(dispatch_get_main_queue(), ^{
tkchinc3f46a92015-07-23 12:50:55 -0700381 RTCLog(@"Received %lu video tracks and %lu audio tracks",
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000382 (unsigned long)stream.videoTracks.count,
383 (unsigned long)stream.audioTracks.count);
384 if (stream.videoTracks.count) {
385 RTCVideoTrack *videoTrack = stream.videoTracks[0];
386 [_delegate appClient:self didReceiveRemoteVideoTrack:videoTrack];
387 }
388 });
389}
390
391- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700392 didRemoveStream:(RTCMediaStream *)stream {
tkchinc3f46a92015-07-23 12:50:55 -0700393 RTCLog(@"Stream was removed.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000394}
395
hjon79858f82016-03-13 22:08:26 -0700396- (void)peerConnectionShouldNegotiate:(RTCPeerConnection *)peerConnection {
tkchinc3f46a92015-07-23 12:50:55 -0700397 RTCLog(@"WARNING: Renegotiation needed but unimplemented.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000398}
399
400- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700401 didChangeIceConnectionState:(RTCIceConnectionState)newState {
402 RTCLog(@"ICE state changed: %ld", (long)newState);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000403 dispatch_async(dispatch_get_main_queue(), ^{
404 [_delegate appClient:self didChangeConnectionState:newState];
405 });
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000406}
407
408- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700409 didChangeIceGatheringState:(RTCIceGatheringState)newState {
410 RTCLog(@"ICE gathering state changed: %ld", (long)newState);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000411}
412
413- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700414 didGenerateIceCandidate:(RTCIceCandidate *)candidate {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000415 dispatch_async(dispatch_get_main_queue(), ^{
416 ARDICECandidateMessage *message =
417 [[ARDICECandidateMessage alloc] initWithCandidate:candidate];
418 [self sendSignalingMessage:message];
419 });
420}
421
Zeke Chind3325802015-08-14 11:00:02 -0700422- (void)peerConnection:(RTCPeerConnection *)peerConnection
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700423 didRemoveIceCandidates:(NSArray<RTCIceCandidate *> *)candidates {
424 dispatch_async(dispatch_get_main_queue(), ^{
425 ARDICECandidateRemovalMessage *message =
426 [[ARDICECandidateRemovalMessage alloc]
427 initWithRemovedCandidates:candidates];
428 [self sendSignalingMessage:message];
429 });
430}
431
432- (void)peerConnection:(RTCPeerConnection *)peerConnection
Zeke Chind3325802015-08-14 11:00:02 -0700433 didOpenDataChannel:(RTCDataChannel *)dataChannel {
434}
435
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000436#pragma mark - RTCSessionDescriptionDelegate
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000437// Callbacks for this delegate occur on non-main thread and need to be
438// dispatched back to main queue as needed.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000439
440- (void)peerConnection:(RTCPeerConnection *)peerConnection
441 didCreateSessionDescription:(RTCSessionDescription *)sdp
442 error:(NSError *)error {
443 dispatch_async(dispatch_get_main_queue(), ^{
444 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700445 RTCLogError(@"Failed to create session description. Error: %@", error);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000446 [self disconnect];
447 NSDictionary *userInfo = @{
448 NSLocalizedDescriptionKey: @"Failed to create session description.",
449 };
450 NSError *sdpError =
451 [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
452 code:kARDAppClientErrorCreateSDP
453 userInfo:userInfo];
454 [_delegate appClient:self didError:sdpError];
455 return;
456 }
Zeke Chin71f6f442015-06-29 14:34:58 -0700457 // Prefer H264 if available.
458 RTCSessionDescription *sdpPreferringH264 =
459 [ARDSDPUtils descriptionForDescription:sdp
460 preferredVideoCodec:@"H264"];
hjon79858f82016-03-13 22:08:26 -0700461 __weak ARDAppClient *weakSelf = self;
462 [_peerConnection setLocalDescription:sdpPreferringH264
463 completionHandler:^(NSError *error) {
464 ARDAppClient *strongSelf = weakSelf;
465 [strongSelf peerConnection:strongSelf.peerConnection
466 didSetSessionDescriptionWithError:error];
467 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000468 ARDSessionDescriptionMessage *message =
Zeke Chin71f6f442015-06-29 14:34:58 -0700469 [[ARDSessionDescriptionMessage alloc]
470 initWithDescription:sdpPreferringH264];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000471 [self sendSignalingMessage:message];
472 });
473}
474
475- (void)peerConnection:(RTCPeerConnection *)peerConnection
476 didSetSessionDescriptionWithError:(NSError *)error {
477 dispatch_async(dispatch_get_main_queue(), ^{
478 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700479 RTCLogError(@"Failed to set session description. Error: %@", error);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000480 [self disconnect];
481 NSDictionary *userInfo = @{
482 NSLocalizedDescriptionKey: @"Failed to set session description.",
483 };
484 NSError *sdpError =
485 [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
486 code:kARDAppClientErrorSetSDP
487 userInfo:userInfo];
488 [_delegate appClient:self didError:sdpError];
489 return;
490 }
491 // If we're answering and we've just set the remote offer we need to create
492 // an answer and set the local description.
493 if (!_isInitiator && !_peerConnection.localDescription) {
494 RTCMediaConstraints *constraints = [self defaultAnswerConstraints];
hjon79858f82016-03-13 22:08:26 -0700495 __weak ARDAppClient *weakSelf = self;
496 [_peerConnection answerForConstraints:constraints
497 completionHandler:^(RTCSessionDescription *sdp,
498 NSError *error) {
499 ARDAppClient *strongSelf = weakSelf;
500 [strongSelf peerConnection:strongSelf.peerConnection
501 didCreateSessionDescription:sdp
502 error:error];
503 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000504 }
505 });
506}
507
508#pragma mark - Private
509
tkchin204177f2016-06-14 15:03:11 -0700510#if defined(WEBRTC_IOS)
511
512- (NSString *)documentsFilePathForFileName:(NSString *)fileName {
513 NSParameterAssert(fileName.length);
514 NSArray *paths = NSSearchPathForDirectoriesInDomains(
515 NSDocumentDirectory, NSUserDomainMask, YES);
516 NSString *documentsDirPath = paths.firstObject;
517 NSString *filePath =
518 [documentsDirPath stringByAppendingPathComponent:fileName];
519 return filePath;
520}
521
522#endif
523
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000524- (BOOL)hasJoinedRoomServerRoom {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000525 return _clientId.length;
526}
527
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000528// Begins the peer connection connection process if we have both joined a room
529// on the room server and tried to obtain a TURN server. Otherwise does nothing.
530// A peer connection object will be created with a stream that contains local
531// audio and video capture. If this client is the caller, an offer is created as
532// well, otherwise the client will wait for an offer to arrive.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000533- (void)startSignalingIfReady {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000534 if (!_isTurnComplete || !self.hasJoinedRoomServerRoom) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000535 return;
536 }
537 self.state = kARDAppClientStateConnected;
538
539 // Create peer connection.
540 RTCMediaConstraints *constraints = [self defaultPeerConnectionConstraints];
Zeke Chinbc7dd7e2015-05-29 14:59:14 -0700541 RTCConfiguration *config = [[RTCConfiguration alloc] init];
542 config.iceServers = _iceServers;
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700543 _peerConnection = [_factory peerConnectionWithConfiguration:config
544 constraints:constraints
545 delegate:self];
skvladf3569c82016-04-29 15:30:16 -0700546 // Create AV senders.
547 [self createAudioSender];
548 [self createVideoSender];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000549 if (_isInitiator) {
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000550 // Send offer.
hjon79858f82016-03-13 22:08:26 -0700551 __weak ARDAppClient *weakSelf = self;
552 [_peerConnection offerForConstraints:[self defaultOfferConstraints]
553 completionHandler:^(RTCSessionDescription *sdp,
554 NSError *error) {
555 ARDAppClient *strongSelf = weakSelf;
556 [strongSelf peerConnection:strongSelf.peerConnection
557 didCreateSessionDescription:sdp
558 error:error];
559 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000560 } else {
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000561 // Check if we've received an offer.
562 [self drainMessageQueueIfReady];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000563 }
ivoc14d5dbe2016-07-04 07:06:55 -0700564#if defined(WEBRTC_IOS)
565 // Start event log.
566 if (kARDAppClientEnableRtcEventLog) {
567 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-rtceventlog"];
568 if (![_peerConnection startRtcEventLogWithFilePath:filePath
569 maxSizeInBytes:kARDAppClientRtcEventLogMaxSizeInBytes]) {
570 RTCLogError(@"Failed to start event logging.");
571 }
572 }
peah5085b0c2016-08-25 22:15:14 -0700573
574 // Start aecdump diagnostic recording.
575 if (_shouldMakeAecDump) {
tkchinfce0e2c2016-08-30 12:58:11 -0700576 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-audio.aecdump"];
577 if (![_factory startAecDumpWithFilePath:filePath
578 maxSizeInBytes:kARDAppClientAecDumpMaxSizeInBytes]) {
579 RTCLogError(@"Failed to start aec dump.");
peah5085b0c2016-08-25 22:15:14 -0700580 }
581 }
ivoc14d5dbe2016-07-04 07:06:55 -0700582#endif
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000583}
584
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000585// Processes the messages that we've received from the room server and the
586// signaling channel. The offer or answer message must be processed before other
587// signaling messages, however they can arrive out of order. Hence, this method
588// only processes pending messages if there is a peer connection object and
589// if we have received either an offer or answer.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000590- (void)drainMessageQueueIfReady {
591 if (!_peerConnection || !_hasReceivedSdp) {
592 return;
593 }
594 for (ARDSignalingMessage *message in _messageQueue) {
595 [self processSignalingMessage:message];
596 }
597 [_messageQueue removeAllObjects];
598}
599
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000600// Processes the given signaling message based on its type.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000601- (void)processSignalingMessage:(ARDSignalingMessage *)message {
602 NSParameterAssert(_peerConnection ||
603 message.type == kARDSignalingMessageTypeBye);
604 switch (message.type) {
605 case kARDSignalingMessageTypeOffer:
606 case kARDSignalingMessageTypeAnswer: {
607 ARDSessionDescriptionMessage *sdpMessage =
608 (ARDSessionDescriptionMessage *)message;
609 RTCSessionDescription *description = sdpMessage.sessionDescription;
Zeke Chin71f6f442015-06-29 14:34:58 -0700610 // Prefer H264 if available.
611 RTCSessionDescription *sdpPreferringH264 =
612 [ARDSDPUtils descriptionForDescription:description
613 preferredVideoCodec:@"H264"];
hjon79858f82016-03-13 22:08:26 -0700614 __weak ARDAppClient *weakSelf = self;
615 [_peerConnection setRemoteDescription:sdpPreferringH264
616 completionHandler:^(NSError *error) {
617 ARDAppClient *strongSelf = weakSelf;
618 [strongSelf peerConnection:strongSelf.peerConnection
619 didSetSessionDescriptionWithError:error];
620 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000621 break;
622 }
623 case kARDSignalingMessageTypeCandidate: {
624 ARDICECandidateMessage *candidateMessage =
625 (ARDICECandidateMessage *)message;
hjon79858f82016-03-13 22:08:26 -0700626 [_peerConnection addIceCandidate:candidateMessage.candidate];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000627 break;
628 }
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700629 case kARDSignalingMessageTypeCandidateRemoval: {
630 ARDICECandidateRemovalMessage *candidateMessage =
631 (ARDICECandidateRemovalMessage *)message;
632 [_peerConnection removeIceCandidates:candidateMessage.candidates];
633 break;
634 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000635 case kARDSignalingMessageTypeBye:
636 // Other client disconnected.
637 // TODO(tkchin): support waiting in room for next client. For now just
638 // disconnect.
639 [self disconnect];
640 break;
641 }
642}
643
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000644// Sends a signaling message to the other client. The caller will send messages
645// through the room server, whereas the callee will send messages over the
646// signaling channel.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000647- (void)sendSignalingMessage:(ARDSignalingMessage *)message {
648 if (_isInitiator) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000649 __weak ARDAppClient *weakSelf = self;
650 [_roomServerClient sendMessage:message
651 forRoomId:_roomId
652 clientId:_clientId
653 completionHandler:^(ARDMessageResponse *response,
654 NSError *error) {
655 ARDAppClient *strongSelf = weakSelf;
656 if (error) {
657 [strongSelf.delegate appClient:strongSelf didError:error];
658 return;
659 }
660 NSError *messageError =
661 [[strongSelf class] errorForMessageResultType:response.result];
662 if (messageError) {
663 [strongSelf.delegate appClient:strongSelf didError:messageError];
664 return;
665 }
666 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000667 } else {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000668 [_channel sendMessage:message];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000669 }
670}
671
skvladf3569c82016-04-29 15:30:16 -0700672- (RTCRtpSender *)createVideoSender {
673 RTCRtpSender *sender =
674 [_peerConnection senderWithKind:kRTCMediaStreamTrackKindVideo
675 streamId:kARDMediaStreamId];
676 RTCVideoTrack *track = [self createLocalVideoTrack];
677 if (track) {
678 sender.track = track;
679 [_delegate appClient:self didReceiveLocalVideoTrack:track];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700680 }
skvladf3569c82016-04-29 15:30:16 -0700681 return sender;
682}
683
684- (RTCRtpSender *)createAudioSender {
tkchinab1293a2016-08-30 12:35:05 -0700685 RTCMediaConstraints *constraints = [self defaultMediaAudioConstraints];
686 RTCAudioSource *source = [_factory audioSourceWithConstraints:constraints];
687 RTCAudioTrack *track = [_factory audioTrackWithSource:source
688 trackId:kARDAudioTrackId];
skvladf3569c82016-04-29 15:30:16 -0700689 RTCRtpSender *sender =
690 [_peerConnection senderWithKind:kRTCMediaStreamTrackKindAudio
691 streamId:kARDMediaStreamId];
skvladf3569c82016-04-29 15:30:16 -0700692 sender.track = track;
693 return sender;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700694}
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000695
Zeke Chin57cc74e2015-05-05 07:52:31 -0700696- (RTCVideoTrack *)createLocalVideoTrack {
697 RTCVideoTrack* localVideoTrack = nil;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000698 // The iOS simulator doesn't provide any sort of camera capture
699 // support or emulation (http://goo.gl/rHAnC1) so don't bother
700 // trying to open a local stream.
701 // TODO(tkchin): local video capture for OSX. See
702 // https://code.google.com/p/webrtc/issues/detail?id=3417.
703#if !TARGET_IPHONE_SIMULATOR && TARGET_OS_IPHONE
haysc913e6452015-10-02 11:44:03 -0700704 if (!_isAudioOnly) {
705 RTCMediaConstraints *mediaConstraints =
706 [self defaultMediaStreamConstraints];
707 RTCAVFoundationVideoSource *source =
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700708 [_factory avFoundationVideoSourceWithConstraints:mediaConstraints];
haysc913e6452015-10-02 11:44:03 -0700709 localVideoTrack =
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700710 [_factory videoTrackWithSource:source
skvladf3569c82016-04-29 15:30:16 -0700711 trackId:kARDVideoTrackId];
haysc913e6452015-10-02 11:44:03 -0700712 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000713#endif
Zeke Chin57cc74e2015-05-05 07:52:31 -0700714 return localVideoTrack;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000715}
716
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000717#pragma mark - Collider methods
718
719- (void)registerWithColliderIfReady {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000720 if (!self.hasJoinedRoomServerRoom) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000721 return;
722 }
723 // Open WebSocket connection.
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000724 if (!_channel) {
725 _channel =
726 [[ARDWebSocketChannel alloc] initWithURL:_websocketURL
727 restURL:_websocketRestURL
728 delegate:self];
haysc913e6452015-10-02 11:44:03 -0700729 if (_isLoopback) {
730 _loopbackChannel =
731 [[ARDLoopbackWebSocketChannel alloc] initWithURL:_websocketURL
732 restURL:_websocketRestURL];
733 }
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000734 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000735 [_channel registerForRoomId:_roomId clientId:_clientId];
haysc913e6452015-10-02 11:44:03 -0700736 if (_isLoopback) {
737 [_loopbackChannel registerForRoomId:_roomId clientId:@"LOOPBACK_CLIENT_ID"];
738 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000739}
740
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000741#pragma mark - Defaults
742
tkchinab1293a2016-08-30 12:35:05 -0700743 - (RTCMediaConstraints *)defaultMediaAudioConstraints {
744 NSString *valueLevelControl = _shouldUseLevelControl ?
745 kRTCMediaConstraintsValueTrue : kRTCMediaConstraintsValueFalse;
746 NSDictionary *mandatoryConstraints = @{ kRTCMediaConstraintsLevelControl : valueLevelControl };
747 RTCMediaConstraints* constraints =
748 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstraints
749 optionalConstraints:nil];
750 return constraints;
751}
752
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000753- (RTCMediaConstraints *)defaultMediaStreamConstraints {
754 RTCMediaConstraints* constraints =
755 [[RTCMediaConstraints alloc]
756 initWithMandatoryConstraints:nil
757 optionalConstraints:nil];
758 return constraints;
759}
760
761- (RTCMediaConstraints *)defaultAnswerConstraints {
762 return [self defaultOfferConstraints];
763}
764
765- (RTCMediaConstraints *)defaultOfferConstraints {
hjon79858f82016-03-13 22:08:26 -0700766 NSDictionary *mandatoryConstraints = @{
767 @"OfferToReceiveAudio" : @"true",
768 @"OfferToReceiveVideo" : @"true"
769 };
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000770 RTCMediaConstraints* constraints =
771 [[RTCMediaConstraints alloc]
772 initWithMandatoryConstraints:mandatoryConstraints
773 optionalConstraints:nil];
774 return constraints;
775}
776
777- (RTCMediaConstraints *)defaultPeerConnectionConstraints {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000778 if (_defaultPeerConnectionConstraints) {
779 return _defaultPeerConnectionConstraints;
780 }
haysc913e6452015-10-02 11:44:03 -0700781 NSString *value = _isLoopback ? @"false" : @"true";
hjon79858f82016-03-13 22:08:26 -0700782 NSDictionary *optionalConstraints = @{ @"DtlsSrtpKeyAgreement" : value };
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000783 RTCMediaConstraints* constraints =
784 [[RTCMediaConstraints alloc]
785 initWithMandatoryConstraints:nil
786 optionalConstraints:optionalConstraints];
787 return constraints;
788}
789
hjon79858f82016-03-13 22:08:26 -0700790- (RTCIceServer *)defaultSTUNServer {
791 return [[RTCIceServer alloc] initWithURLStrings:@[kARDDefaultSTUNServerUrl]
792 username:@""
793 credential:@""];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000794}
795
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000796#pragma mark - Errors
797
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000798+ (NSError *)errorForJoinResultType:(ARDJoinResultType)resultType {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000799 NSError *error = nil;
800 switch (resultType) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000801 case kARDJoinResultTypeSuccess:
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000802 break;
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000803 case kARDJoinResultTypeUnknown: {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000804 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
805 code:kARDAppClientErrorUnknown
806 userInfo:@{
807 NSLocalizedDescriptionKey: @"Unknown error.",
808 }];
809 break;
810 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000811 case kARDJoinResultTypeFull: {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000812 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
813 code:kARDAppClientErrorRoomFull
814 userInfo:@{
815 NSLocalizedDescriptionKey: @"Room is full.",
816 }];
817 break;
818 }
819 }
820 return error;
821}
822
823+ (NSError *)errorForMessageResultType:(ARDMessageResultType)resultType {
824 NSError *error = nil;
825 switch (resultType) {
826 case kARDMessageResultTypeSuccess:
827 break;
828 case kARDMessageResultTypeUnknown:
829 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
830 code:kARDAppClientErrorUnknown
831 userInfo:@{
832 NSLocalizedDescriptionKey: @"Unknown error.",
833 }];
834 break;
835 case kARDMessageResultTypeInvalidClient:
836 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
837 code:kARDAppClientErrorInvalidClient
838 userInfo:@{
839 NSLocalizedDescriptionKey: @"Invalid client.",
840 }];
841 break;
842 case kARDMessageResultTypeInvalidRoom:
843 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
844 code:kARDAppClientErrorInvalidRoom
845 userInfo:@{
846 NSLocalizedDescriptionKey: @"Invalid room.",
847 }];
848 break;
849 }
850 return error;
851}
852
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000853@end