blob: 7e9fbdac75dcf6eaa6610bfbe1bcf47b7dfd54bb [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"
sakalc522e752017-04-05 12:17:48 -070025#import "WebRTC/RTCVideoTrack.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000026
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000027#import "ARDAppEngineClient.h"
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000028#import "ARDJoinResponse.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000029#import "ARDMessageResponse.h"
Zeke Chin71f6f442015-06-29 14:34:58 -070030#import "ARDSDPUtils.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 {
166 _factory = [[RTCPeerConnectionFactory alloc] init];
167 _messageQueue = [NSMutableArray array];
kthelgasoncc882af2017-01-13 05:59:46 -0800168 _iceServers = [NSMutableArray array];
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700169 _fileLogger = [[RTCFileLogger alloc] init];
170 [_fileLogger start];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000171}
172
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000173- (void)dealloc {
Zeke Chind3325802015-08-14 11:00:02 -0700174 self.shouldGetStats = NO;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000175 [self disconnect];
176}
177
Zeke Chind3325802015-08-14 11:00:02 -0700178- (void)setShouldGetStats:(BOOL)shouldGetStats {
179 if (_shouldGetStats == shouldGetStats) {
180 return;
181 }
182 if (shouldGetStats) {
183 __weak ARDAppClient *weakSelf = self;
184 _statsTimer = [[ARDTimerProxy alloc] initWithInterval:1
185 repeats:YES
186 timerHandler:^{
187 ARDAppClient *strongSelf = weakSelf;
hjon79858f82016-03-13 22:08:26 -0700188 [strongSelf.peerConnection statsForTrack:nil
189 statsOutputLevel:RTCStatsOutputLevelDebug
190 completionHandler:^(NSArray *stats) {
191 dispatch_async(dispatch_get_main_queue(), ^{
192 ARDAppClient *strongSelf = weakSelf;
193 [strongSelf.delegate appClient:strongSelf didGetStats:stats];
194 });
195 }];
Zeke Chind3325802015-08-14 11:00:02 -0700196 }];
197 } else {
198 [_statsTimer invalidate];
199 _statsTimer = nil;
200 }
201 _shouldGetStats = shouldGetStats;
202}
203
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000204- (void)setState:(ARDAppClientState)state {
205 if (_state == state) {
206 return;
207 }
208 _state = state;
209 [_delegate appClient:self didChangeState:_state];
210}
211
212- (void)connectToRoomWithId:(NSString *)roomId
sakalc4adacf2017-03-28 01:22:48 -0700213 settings:(ARDSettingsModel *)settings
Anders Carlssone1500582017-06-15 16:05:13 +0200214 isLoopback:(BOOL)isLoopback {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000215 NSParameterAssert(roomId.length);
216 NSParameterAssert(_state == kARDAppClientStateDisconnected);
sakalc4adacf2017-03-28 01:22:48 -0700217 _settings = settings;
haysc913e6452015-10-02 11:44:03 -0700218 _isLoopback = isLoopback;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000219 self.state = kARDAppClientStateConnecting;
220
tkchind1fb26d2016-02-03 01:51:18 -0800221#if defined(WEBRTC_IOS)
222 if (kARDAppClientEnableTracing) {
tkchin204177f2016-06-14 15:03:11 -0700223 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-trace.txt"];
tkchind1fb26d2016-02-03 01:51:18 -0800224 RTCStartInternalCapture(filePath);
225 }
226#endif
227
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000228 // Request TURN.
229 __weak ARDAppClient *weakSelf = self;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000230 [_turnClient requestServersWithCompletionHandler:^(NSArray *turnServers,
231 NSError *error) {
232 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700233 RTCLogError("Error retrieving TURN servers: %@",
234 error.localizedDescription);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000235 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000236 ARDAppClient *strongSelf = weakSelf;
237 [strongSelf.iceServers addObjectsFromArray:turnServers];
238 strongSelf.isTurnComplete = YES;
239 [strongSelf startSignalingIfReady];
240 }];
jiayl@webrtc.org27f53172014-12-31 00:26:20 +0000241
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000242 // Join room on room server.
243 [_roomServerClient joinRoomWithRoomId:roomId
haysc913e6452015-10-02 11:44:03 -0700244 isLoopback:isLoopback
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000245 completionHandler:^(ARDJoinResponse *response, NSError *error) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000246 ARDAppClient *strongSelf = weakSelf;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000247 if (error) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000248 [strongSelf.delegate appClient:strongSelf didError:error];
249 return;
250 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000251 NSError *joinError =
252 [[strongSelf class] errorForJoinResultType:response.result];
253 if (joinError) {
tkchinc3f46a92015-07-23 12:50:55 -0700254 RTCLogError(@"Failed to join room:%@ on room server.", roomId);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000255 [strongSelf disconnect];
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000256 [strongSelf.delegate appClient:strongSelf didError:joinError];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000257 return;
258 }
tkchinc3f46a92015-07-23 12:50:55 -0700259 RTCLog(@"Joined room:%@ on room server.", roomId);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000260 strongSelf.roomId = response.roomId;
261 strongSelf.clientId = response.clientId;
262 strongSelf.isInitiator = response.isInitiator;
263 for (ARDSignalingMessage *message in response.messages) {
264 if (message.type == kARDSignalingMessageTypeOffer ||
265 message.type == kARDSignalingMessageTypeAnswer) {
266 strongSelf.hasReceivedSdp = YES;
267 [strongSelf.messageQueue insertObject:message atIndex:0];
268 } else {
269 [strongSelf.messageQueue addObject:message];
270 }
271 }
272 strongSelf.webSocketURL = response.webSocketURL;
273 strongSelf.webSocketRestURL = response.webSocketRestURL;
274 [strongSelf registerWithColliderIfReady];
275 [strongSelf startSignalingIfReady];
276 }];
277}
278
279- (void)disconnect {
280 if (_state == kARDAppClientStateDisconnected) {
281 return;
282 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000283 if (self.hasJoinedRoomServerRoom) {
284 [_roomServerClient leaveRoomWithRoomId:_roomId
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000285 clientId:_clientId
286 completionHandler:nil];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000287 }
288 if (_channel) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000289 if (_channel.state == kARDSignalingChannelStateRegistered) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000290 // Tell the other client we're hanging up.
291 ARDByeMessage *byeMessage = [[ARDByeMessage alloc] init];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000292 [_channel sendMessage:byeMessage];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000293 }
294 // Disconnect from collider.
295 _channel = nil;
296 }
297 _clientId = nil;
298 _roomId = nil;
299 _isInitiator = NO;
300 _hasReceivedSdp = NO;
301 _messageQueue = [NSMutableArray array];
sakalc522e752017-04-05 12:17:48 -0700302 _localVideoTrack = nil;
ivoc14d5dbe2016-07-04 07:06:55 -0700303#if defined(WEBRTC_IOS)
tkchinfce0e2c2016-08-30 12:58:11 -0700304 [_factory stopAecDump];
ivoc14d5dbe2016-07-04 07:06:55 -0700305 [_peerConnection stopRtcEventLog];
306#endif
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000307 _peerConnection = nil;
308 self.state = kARDAppClientStateDisconnected;
tkchind1fb26d2016-02-03 01:51:18 -0800309#if defined(WEBRTC_IOS)
adam.fedorbcc5d872016-11-07 14:53:28 -0800310 if (kARDAppClientEnableTracing) {
311 RTCStopInternalCapture();
312 }
tkchind1fb26d2016-02-03 01:51:18 -0800313#endif
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000314}
315
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000316#pragma mark - ARDSignalingChannelDelegate
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000317
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000318- (void)channel:(id<ARDSignalingChannel>)channel
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000319 didReceiveMessage:(ARDSignalingMessage *)message {
320 switch (message.type) {
321 case kARDSignalingMessageTypeOffer:
322 case kARDSignalingMessageTypeAnswer:
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000323 // Offers and answers must be processed before any other message, so we
324 // place them at the front of the queue.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000325 _hasReceivedSdp = YES;
326 [_messageQueue insertObject:message atIndex:0];
327 break;
328 case kARDSignalingMessageTypeCandidate:
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700329 case kARDSignalingMessageTypeCandidateRemoval:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000330 [_messageQueue addObject:message];
331 break;
332 case kARDSignalingMessageTypeBye:
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000333 // Disconnects can be processed immediately.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000334 [self processSignalingMessage:message];
335 return;
336 }
337 [self drainMessageQueueIfReady];
338}
339
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000340- (void)channel:(id<ARDSignalingChannel>)channel
341 didChangeState:(ARDSignalingChannelState)state {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000342 switch (state) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000343 case kARDSignalingChannelStateOpen:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000344 break;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000345 case kARDSignalingChannelStateRegistered:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000346 break;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000347 case kARDSignalingChannelStateClosed:
348 case kARDSignalingChannelStateError:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000349 // TODO(tkchin): reconnection scenarios. Right now we just disconnect
350 // completely if the websocket connection fails.
351 [self disconnect];
352 break;
353 }
354}
355
356#pragma mark - RTCPeerConnectionDelegate
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000357// Callbacks for this delegate occur on non-main thread and need to be
358// dispatched back to main queue as needed.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000359
360- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700361 didChangeSignalingState:(RTCSignalingState)stateChanged {
362 RTCLog(@"Signaling state changed: %ld", (long)stateChanged);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000363}
364
365- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700366 didAddStream:(RTCMediaStream *)stream {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000367 dispatch_async(dispatch_get_main_queue(), ^{
tkchinc3f46a92015-07-23 12:50:55 -0700368 RTCLog(@"Received %lu video tracks and %lu audio tracks",
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000369 (unsigned long)stream.videoTracks.count,
370 (unsigned long)stream.audioTracks.count);
371 if (stream.videoTracks.count) {
372 RTCVideoTrack *videoTrack = stream.videoTracks[0];
373 [_delegate appClient:self didReceiveRemoteVideoTrack:videoTrack];
374 }
375 });
376}
377
378- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700379 didRemoveStream:(RTCMediaStream *)stream {
tkchinc3f46a92015-07-23 12:50:55 -0700380 RTCLog(@"Stream was removed.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000381}
382
hjon79858f82016-03-13 22:08:26 -0700383- (void)peerConnectionShouldNegotiate:(RTCPeerConnection *)peerConnection {
tkchinc3f46a92015-07-23 12:50:55 -0700384 RTCLog(@"WARNING: Renegotiation needed but unimplemented.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000385}
386
387- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700388 didChangeIceConnectionState:(RTCIceConnectionState)newState {
389 RTCLog(@"ICE state changed: %ld", (long)newState);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000390 dispatch_async(dispatch_get_main_queue(), ^{
391 [_delegate appClient:self didChangeConnectionState:newState];
392 });
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000393}
394
395- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700396 didChangeIceGatheringState:(RTCIceGatheringState)newState {
397 RTCLog(@"ICE gathering state changed: %ld", (long)newState);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000398}
399
400- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700401 didGenerateIceCandidate:(RTCIceCandidate *)candidate {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000402 dispatch_async(dispatch_get_main_queue(), ^{
403 ARDICECandidateMessage *message =
404 [[ARDICECandidateMessage alloc] initWithCandidate:candidate];
405 [self sendSignalingMessage:message];
406 });
407}
408
Zeke Chind3325802015-08-14 11:00:02 -0700409- (void)peerConnection:(RTCPeerConnection *)peerConnection
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700410 didRemoveIceCandidates:(NSArray<RTCIceCandidate *> *)candidates {
411 dispatch_async(dispatch_get_main_queue(), ^{
412 ARDICECandidateRemovalMessage *message =
413 [[ARDICECandidateRemovalMessage alloc]
414 initWithRemovedCandidates:candidates];
415 [self sendSignalingMessage:message];
416 });
417}
418
419- (void)peerConnection:(RTCPeerConnection *)peerConnection
Zeke Chind3325802015-08-14 11:00:02 -0700420 didOpenDataChannel:(RTCDataChannel *)dataChannel {
421}
422
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000423#pragma mark - RTCSessionDescriptionDelegate
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000424// Callbacks for this delegate occur on non-main thread and need to be
425// dispatched back to main queue as needed.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000426
427- (void)peerConnection:(RTCPeerConnection *)peerConnection
428 didCreateSessionDescription:(RTCSessionDescription *)sdp
429 error:(NSError *)error {
430 dispatch_async(dispatch_get_main_queue(), ^{
431 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700432 RTCLogError(@"Failed to create session description. Error: %@", error);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000433 [self disconnect];
434 NSDictionary *userInfo = @{
435 NSLocalizedDescriptionKey: @"Failed to create session description.",
436 };
437 NSError *sdpError =
438 [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
439 code:kARDAppClientErrorCreateSDP
440 userInfo:userInfo];
441 [_delegate appClient:self didError:sdpError];
442 return;
443 }
sakal68b5df92017-03-17 09:01:59 -0700444 // Prefer codec from settings if available.
445 RTCSessionDescription *sdpPreferringCodec =
Zeke Chin71f6f442015-06-29 14:34:58 -0700446 [ARDSDPUtils descriptionForDescription:sdp
sakalc4adacf2017-03-28 01:22:48 -0700447 preferredVideoCodec:[_settings currentVideoCodecSettingFromStore]];
hjon79858f82016-03-13 22:08:26 -0700448 __weak ARDAppClient *weakSelf = self;
sakal68b5df92017-03-17 09:01:59 -0700449 [_peerConnection setLocalDescription:sdpPreferringCodec
hjon79858f82016-03-13 22:08:26 -0700450 completionHandler:^(NSError *error) {
451 ARDAppClient *strongSelf = weakSelf;
452 [strongSelf peerConnection:strongSelf.peerConnection
453 didSetSessionDescriptionWithError:error];
454 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000455 ARDSessionDescriptionMessage *message =
Zeke Chin71f6f442015-06-29 14:34:58 -0700456 [[ARDSessionDescriptionMessage alloc]
sakal68b5df92017-03-17 09:01:59 -0700457 initWithDescription:sdpPreferringCodec];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000458 [self sendSignalingMessage:message];
denicija9af2b602016-11-17 00:43:43 -0800459 [self setMaxBitrateForPeerConnectionVideoSender];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000460 });
461}
462
463- (void)peerConnection:(RTCPeerConnection *)peerConnection
464 didSetSessionDescriptionWithError:(NSError *)error {
465 dispatch_async(dispatch_get_main_queue(), ^{
466 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700467 RTCLogError(@"Failed to set session description. Error: %@", error);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000468 [self disconnect];
469 NSDictionary *userInfo = @{
470 NSLocalizedDescriptionKey: @"Failed to set session description.",
471 };
472 NSError *sdpError =
473 [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
474 code:kARDAppClientErrorSetSDP
475 userInfo:userInfo];
476 [_delegate appClient:self didError:sdpError];
477 return;
478 }
479 // If we're answering and we've just set the remote offer we need to create
480 // an answer and set the local description.
481 if (!_isInitiator && !_peerConnection.localDescription) {
482 RTCMediaConstraints *constraints = [self defaultAnswerConstraints];
hjon79858f82016-03-13 22:08:26 -0700483 __weak ARDAppClient *weakSelf = self;
484 [_peerConnection answerForConstraints:constraints
485 completionHandler:^(RTCSessionDescription *sdp,
486 NSError *error) {
487 ARDAppClient *strongSelf = weakSelf;
488 [strongSelf peerConnection:strongSelf.peerConnection
489 didCreateSessionDescription:sdp
490 error:error];
491 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000492 }
493 });
494}
495
496#pragma mark - Private
497
tkchin204177f2016-06-14 15:03:11 -0700498#if defined(WEBRTC_IOS)
499
500- (NSString *)documentsFilePathForFileName:(NSString *)fileName {
501 NSParameterAssert(fileName.length);
502 NSArray *paths = NSSearchPathForDirectoriesInDomains(
503 NSDocumentDirectory, NSUserDomainMask, YES);
504 NSString *documentsDirPath = paths.firstObject;
505 NSString *filePath =
506 [documentsDirPath stringByAppendingPathComponent:fileName];
507 return filePath;
508}
509
510#endif
511
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000512- (BOOL)hasJoinedRoomServerRoom {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000513 return _clientId.length;
514}
515
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000516// Begins the peer connection connection process if we have both joined a room
517// on the room server and tried to obtain a TURN server. Otherwise does nothing.
518// A peer connection object will be created with a stream that contains local
519// audio and video capture. If this client is the caller, an offer is created as
520// well, otherwise the client will wait for an offer to arrive.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000521- (void)startSignalingIfReady {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000522 if (!_isTurnComplete || !self.hasJoinedRoomServerRoom) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000523 return;
524 }
525 self.state = kARDAppClientStateConnected;
526
527 // Create peer connection.
528 RTCMediaConstraints *constraints = [self defaultPeerConnectionConstraints];
Zeke Chinbc7dd7e2015-05-29 14:59:14 -0700529 RTCConfiguration *config = [[RTCConfiguration alloc] init];
530 config.iceServers = _iceServers;
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700531 _peerConnection = [_factory peerConnectionWithConfiguration:config
532 constraints:constraints
533 delegate:self];
skvladf3569c82016-04-29 15:30:16 -0700534 // Create AV senders.
535 [self createAudioSender];
536 [self createVideoSender];
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;
sakal68b5df92017-03-17 09:01:59 -0700598 // Prefer codec from settings if available.
599 RTCSessionDescription *sdpPreferringCodec =
Zeke Chin71f6f442015-06-29 14:34:58 -0700600 [ARDSDPUtils descriptionForDescription:description
sakalc4adacf2017-03-28 01:22:48 -0700601 preferredVideoCodec:[_settings currentVideoCodecSettingFromStore]];
hjon79858f82016-03-13 22:08:26 -0700602 __weak ARDAppClient *weakSelf = self;
sakal68b5df92017-03-17 09:01:59 -0700603 [_peerConnection setRemoteDescription:sdpPreferringCodec
hjon79858f82016-03-13 22:08:26 -0700604 completionHandler:^(NSError *error) {
605 ARDAppClient *strongSelf = weakSelf;
606 [strongSelf peerConnection:strongSelf.peerConnection
607 didSetSessionDescriptionWithError:error];
608 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000609 break;
610 }
611 case kARDSignalingMessageTypeCandidate: {
612 ARDICECandidateMessage *candidateMessage =
613 (ARDICECandidateMessage *)message;
hjon79858f82016-03-13 22:08:26 -0700614 [_peerConnection addIceCandidate:candidateMessage.candidate];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000615 break;
616 }
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700617 case kARDSignalingMessageTypeCandidateRemoval: {
618 ARDICECandidateRemovalMessage *candidateMessage =
619 (ARDICECandidateRemovalMessage *)message;
620 [_peerConnection removeIceCandidates:candidateMessage.candidates];
621 break;
622 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000623 case kARDSignalingMessageTypeBye:
624 // Other client disconnected.
625 // TODO(tkchin): support waiting in room for next client. For now just
626 // disconnect.
627 [self disconnect];
628 break;
629 }
630}
631
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000632// Sends a signaling message to the other client. The caller will send messages
633// through the room server, whereas the callee will send messages over the
634// signaling channel.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000635- (void)sendSignalingMessage:(ARDSignalingMessage *)message {
636 if (_isInitiator) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000637 __weak ARDAppClient *weakSelf = self;
638 [_roomServerClient sendMessage:message
639 forRoomId:_roomId
640 clientId:_clientId
641 completionHandler:^(ARDMessageResponse *response,
642 NSError *error) {
643 ARDAppClient *strongSelf = weakSelf;
644 if (error) {
645 [strongSelf.delegate appClient:strongSelf didError:error];
646 return;
647 }
648 NSError *messageError =
649 [[strongSelf class] errorForMessageResultType:response.result];
650 if (messageError) {
651 [strongSelf.delegate appClient:strongSelf didError:messageError];
652 return;
653 }
654 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000655 } else {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000656 [_channel sendMessage:message];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000657 }
658}
659
skvladf3569c82016-04-29 15:30:16 -0700660- (RTCRtpSender *)createVideoSender {
661 RTCRtpSender *sender =
662 [_peerConnection senderWithKind:kRTCMediaStreamTrackKindVideo
663 streamId:kARDMediaStreamId];
sakalc522e752017-04-05 12:17:48 -0700664 _localVideoTrack = [self createLocalVideoTrack];
665 if (_localVideoTrack) {
666 sender.track = _localVideoTrack;
667 [_delegate appClient:self didReceiveLocalVideoTrack:_localVideoTrack];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700668 }
denicija9af2b602016-11-17 00:43:43 -0800669
skvladf3569c82016-04-29 15:30:16 -0700670 return sender;
671}
672
denicija9af2b602016-11-17 00:43:43 -0800673- (void)setMaxBitrateForPeerConnectionVideoSender {
674 for (RTCRtpSender *sender in _peerConnection.senders) {
675 if (sender.track != nil) {
676 if ([sender.track.kind isEqualToString:kARDVideoTrackKind]) {
sakalc4adacf2017-03-28 01:22:48 -0700677 [self setMaxBitrate:[_settings currentMaxBitrateSettingFromStore] forVideoSender:sender];
denicija9af2b602016-11-17 00:43:43 -0800678 }
679 }
denicija8c375de2016-11-08 06:28:17 -0800680 }
681}
682
denicija9af2b602016-11-17 00:43:43 -0800683- (void)setMaxBitrate:(NSNumber *)maxBitrate forVideoSender:(RTCRtpSender *)sender {
684 if (maxBitrate.intValue <= 0) {
685 return;
686 }
687
688 RTCRtpParameters *parametersToModify = sender.parameters;
689 for (RTCRtpEncodingParameters *encoding in parametersToModify.encodings) {
690 encoding.maxBitrateBps = @(maxBitrate.intValue * kKbpsMultiplier);
691 }
692 [sender setParameters:parametersToModify];
693}
694
skvladf3569c82016-04-29 15:30:16 -0700695- (RTCRtpSender *)createAudioSender {
tkchinab1293a2016-08-30 12:35:05 -0700696 RTCMediaConstraints *constraints = [self defaultMediaAudioConstraints];
697 RTCAudioSource *source = [_factory audioSourceWithConstraints:constraints];
698 RTCAudioTrack *track = [_factory audioTrackWithSource:source
699 trackId:kARDAudioTrackId];
skvladf3569c82016-04-29 15:30:16 -0700700 RTCRtpSender *sender =
701 [_peerConnection senderWithKind:kRTCMediaStreamTrackKindAudio
702 streamId:kARDMediaStreamId];
skvladf3569c82016-04-29 15:30:16 -0700703 sender.track = track;
704 return sender;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700705}
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000706
Zeke Chin57cc74e2015-05-05 07:52:31 -0700707- (RTCVideoTrack *)createLocalVideoTrack {
708 RTCVideoTrack* localVideoTrack = nil;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000709 // The iOS simulator doesn't provide any sort of camera capture
710 // support or emulation (http://goo.gl/rHAnC1) so don't bother
711 // trying to open a local stream.
kthelgason314bc5f2016-08-31 10:23:27 -0700712#if !TARGET_IPHONE_SIMULATOR
Anders Carlssone1500582017-06-15 16:05:13 +0200713 if (![_settings currentAudioOnlySettingFromStore]) {
sakalc522e752017-04-05 12:17:48 -0700714 RTCVideoSource *source = [_factory videoSource];
715 RTCCameraVideoCapturer *capturer = [[RTCCameraVideoCapturer alloc] initWithDelegate:source];
716 [_delegate appClient:self didCreateLocalCapturer:capturer];
haysc913e6452015-10-02 11:44:03 -0700717 localVideoTrack =
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700718 [_factory videoTrackWithSource:source
skvladf3569c82016-04-29 15:30:16 -0700719 trackId:kARDVideoTrackId];
haysc913e6452015-10-02 11:44:03 -0700720 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000721#endif
Zeke Chin57cc74e2015-05-05 07:52:31 -0700722 return localVideoTrack;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000723}
724
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000725#pragma mark - Collider methods
726
727- (void)registerWithColliderIfReady {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000728 if (!self.hasJoinedRoomServerRoom) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000729 return;
730 }
731 // Open WebSocket connection.
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000732 if (!_channel) {
733 _channel =
734 [[ARDWebSocketChannel alloc] initWithURL:_websocketURL
735 restURL:_websocketRestURL
736 delegate:self];
haysc913e6452015-10-02 11:44:03 -0700737 if (_isLoopback) {
738 _loopbackChannel =
739 [[ARDLoopbackWebSocketChannel alloc] initWithURL:_websocketURL
740 restURL:_websocketRestURL];
741 }
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000742 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000743 [_channel registerForRoomId:_roomId clientId:_clientId];
haysc913e6452015-10-02 11:44:03 -0700744 if (_isLoopback) {
745 [_loopbackChannel registerForRoomId:_roomId clientId:@"LOOPBACK_CLIENT_ID"];
746 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000747}
748
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000749#pragma mark - Defaults
750
tkchinab1293a2016-08-30 12:35:05 -0700751 - (RTCMediaConstraints *)defaultMediaAudioConstraints {
Anders Carlssone1500582017-06-15 16:05:13 +0200752 NSString *valueLevelControl = [_settings currentUseLevelControllerSettingFromStore] ?
753 kRTCMediaConstraintsValueTrue :
754 kRTCMediaConstraintsValueFalse;
tkchinab1293a2016-08-30 12:35:05 -0700755 NSDictionary *mandatoryConstraints = @{ kRTCMediaConstraintsLevelControl : valueLevelControl };
denicijad17d5362016-11-02 02:56:09 -0700756 RTCMediaConstraints *constraints =
757 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstraints
758 optionalConstraints:nil];
tkchinab1293a2016-08-30 12:35:05 -0700759 return constraints;
760}
761
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000762- (RTCMediaConstraints *)defaultAnswerConstraints {
763 return [self defaultOfferConstraints];
764}
765
766- (RTCMediaConstraints *)defaultOfferConstraints {
hjon79858f82016-03-13 22:08:26 -0700767 NSDictionary *mandatoryConstraints = @{
768 @"OfferToReceiveAudio" : @"true",
769 @"OfferToReceiveVideo" : @"true"
770 };
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000771 RTCMediaConstraints* constraints =
772 [[RTCMediaConstraints alloc]
773 initWithMandatoryConstraints:mandatoryConstraints
774 optionalConstraints:nil];
775 return constraints;
776}
777
778- (RTCMediaConstraints *)defaultPeerConnectionConstraints {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000779 if (_defaultPeerConnectionConstraints) {
780 return _defaultPeerConnectionConstraints;
781 }
haysc913e6452015-10-02 11:44:03 -0700782 NSString *value = _isLoopback ? @"false" : @"true";
hjon79858f82016-03-13 22:08:26 -0700783 NSDictionary *optionalConstraints = @{ @"DtlsSrtpKeyAgreement" : value };
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000784 RTCMediaConstraints* constraints =
785 [[RTCMediaConstraints alloc]
786 initWithMandatoryConstraints:nil
787 optionalConstraints:optionalConstraints];
788 return constraints;
789}
790
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000791#pragma mark - Errors
792
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000793+ (NSError *)errorForJoinResultType:(ARDJoinResultType)resultType {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000794 NSError *error = nil;
795 switch (resultType) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000796 case kARDJoinResultTypeSuccess:
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000797 break;
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000798 case kARDJoinResultTypeUnknown: {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000799 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
800 code:kARDAppClientErrorUnknown
801 userInfo:@{
802 NSLocalizedDescriptionKey: @"Unknown error.",
803 }];
804 break;
805 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000806 case kARDJoinResultTypeFull: {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000807 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
808 code:kARDAppClientErrorRoomFull
809 userInfo:@{
810 NSLocalizedDescriptionKey: @"Room is full.",
811 }];
812 break;
813 }
814 }
815 return error;
816}
817
818+ (NSError *)errorForMessageResultType:(ARDMessageResultType)resultType {
819 NSError *error = nil;
820 switch (resultType) {
821 case kARDMessageResultTypeSuccess:
822 break;
823 case kARDMessageResultTypeUnknown:
824 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
825 code:kARDAppClientErrorUnknown
826 userInfo:@{
827 NSLocalizedDescriptionKey: @"Unknown error.",
828 }];
829 break;
830 case kARDMessageResultTypeInvalidClient:
831 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
832 code:kARDAppClientErrorInvalidClient
833 userInfo:@{
834 NSLocalizedDescriptionKey: @"Invalid client.",
835 }];
836 break;
837 case kARDMessageResultTypeInvalidRoom:
838 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
839 code:kARDAppClientErrorInvalidRoom
840 userInfo:@{
841 NSLocalizedDescriptionKey: @"Invalid room.",
842 }];
843 break;
844 }
845 return error;
846}
847
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000848@end