blob: 9a0d3695f91615469c990d1122d1aeecdbff60e5 [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
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020013#import <WebRTC/RTCAudioTrack.h>
14#import <WebRTC/RTCCameraVideoCapturer.h>
15#import <WebRTC/RTCConfiguration.h>
16#import <WebRTC/RTCDefaultVideoDecoderFactory.h>
17#import <WebRTC/RTCDefaultVideoEncoderFactory.h>
18#import <WebRTC/RTCFileLogger.h>
19#import <WebRTC/RTCFileVideoCapturer.h>
20#import <WebRTC/RTCIceServer.h>
21#import <WebRTC/RTCLogging.h>
22#import <WebRTC/RTCMediaConstraints.h>
23#import <WebRTC/RTCMediaStream.h>
24#import <WebRTC/RTCPeerConnectionFactory.h>
25#import <WebRTC/RTCRtpSender.h>
26#import <WebRTC/RTCRtpTransceiver.h>
27#import <WebRTC/RTCTracing.h>
28#import <WebRTC/RTCVideoSource.h>
29#import <WebRTC/RTCVideoTrack.h>
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000030
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000031#import "ARDAppEngineClient.h"
Anders Carlsson358f2e02018-06-04 10:24:37 +020032#import "ARDExternalSampleCapturer.h"
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000033#import "ARDJoinResponse.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000034#import "ARDMessageResponse.h"
sakalc4adacf2017-03-28 01:22:48 -070035#import "ARDSettingsModel.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000036#import "ARDSignalingMessage.h"
sakalc4adacf2017-03-28 01:22:48 -070037#import "ARDTURNClient+Internal.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000038#import "ARDUtilities.h"
39#import "ARDWebSocketChannel.h"
hjon79858f82016-03-13 22:08:26 -070040#import "RTCIceCandidate+JSON.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000041#import "RTCSessionDescription+JSON.h"
Zeke Chin57cc74e2015-05-05 07:52:31 -070042
kthelgasoncc882af2017-01-13 05:59:46 -080043static NSString * const kARDIceServerRequestUrl = @"https://appr.tc/params";
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000044
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";
denicija9af2b602016-11-17 00:43:43 -080055static NSString * const kARDVideoTrackKind = @"video";
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000056
tkchin204177f2016-06-14 15:03:11 -070057// TODO(tkchin): Add these as UI options.
Mirko Bonadei3e603ec2018-07-10 14:21:23 +020058#if defined(WEBRTC_IOS)
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.
Mirko Bonadei3e603ec2018-07-10 14:21:23 +020063#endif
denicija9af2b602016-11-17 00:43:43 -080064static int const kKbpsMultiplier = 1000;
tkchind1fb26d2016-02-03 01:51:18 -080065
Zeke Chind3325802015-08-14 11:00:02 -070066// We need a proxy to NSTimer because it causes a strong retain cycle. When
67// using the proxy, |invalidate| must be called before it properly deallocs.
68@interface ARDTimerProxy : NSObject
69
70- (instancetype)initWithInterval:(NSTimeInterval)interval
71 repeats:(BOOL)repeats
72 timerHandler:(void (^)(void))timerHandler;
73- (void)invalidate;
74
75@end
76
77@implementation ARDTimerProxy {
78 NSTimer *_timer;
79 void (^_timerHandler)(void);
Zeke Chin2d3b7e22015-07-14 12:55:44 -070080}
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000081
Zeke Chind3325802015-08-14 11:00:02 -070082- (instancetype)initWithInterval:(NSTimeInterval)interval
83 repeats:(BOOL)repeats
84 timerHandler:(void (^)(void))timerHandler {
85 NSParameterAssert(timerHandler);
86 if (self = [super init]) {
87 _timerHandler = timerHandler;
88 _timer = [NSTimer scheduledTimerWithTimeInterval:interval
89 target:self
90 selector:@selector(timerDidFire:)
91 userInfo:nil
92 repeats:repeats];
93 }
94 return self;
95}
96
97- (void)invalidate {
98 [_timer invalidate];
99}
100
101- (void)timerDidFire:(NSTimer *)timer {
102 _timerHandler();
103}
104
105@end
106
107@implementation ARDAppClient {
108 RTCFileLogger *_fileLogger;
109 ARDTimerProxy *_statsTimer;
sakalc4adacf2017-03-28 01:22:48 -0700110 ARDSettingsModel *_settings;
sakalc522e752017-04-05 12:17:48 -0700111 RTCVideoTrack *_localVideoTrack;
Zeke Chind3325802015-08-14 11:00:02 -0700112}
113
114@synthesize shouldGetStats = _shouldGetStats;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000115@synthesize state = _state;
Zeke Chind3325802015-08-14 11:00:02 -0700116@synthesize delegate = _delegate;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000117@synthesize roomServerClient = _roomServerClient;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000118@synthesize channel = _channel;
haysc913e6452015-10-02 11:44:03 -0700119@synthesize loopbackChannel = _loopbackChannel;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000120@synthesize turnClient = _turnClient;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000121@synthesize peerConnection = _peerConnection;
122@synthesize factory = _factory;
123@synthesize messageQueue = _messageQueue;
124@synthesize isTurnComplete = _isTurnComplete;
125@synthesize hasReceivedSdp = _hasReceivedSdp;
126@synthesize roomId = _roomId;
127@synthesize clientId = _clientId;
128@synthesize isInitiator = _isInitiator;
129@synthesize iceServers = _iceServers;
130@synthesize webSocketURL = _websocketURL;
131@synthesize webSocketRestURL = _websocketRestURL;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000132@synthesize defaultPeerConnectionConstraints =
133 _defaultPeerConnectionConstraints;
haysc913e6452015-10-02 11:44:03 -0700134@synthesize isLoopback = _isLoopback;
Anders Carlsson358f2e02018-06-04 10:24:37 +0200135@synthesize broadcast = _broadcast;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000136
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000137- (instancetype)init {
sakalc4adacf2017-03-28 01:22:48 -0700138 return [self initWithDelegate:nil];
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000139}
140
sakalc4adacf2017-03-28 01:22:48 -0700141- (instancetype)initWithDelegate:(id<ARDAppClientDelegate>)delegate {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000142 if (self = [super init]) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000143 _roomServerClient = [[ARDAppEngineClient alloc] init];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000144 _delegate = delegate;
kthelgasoncc882af2017-01-13 05:59:46 -0800145 NSURL *turnRequestURL = [NSURL URLWithString:kARDIceServerRequestUrl];
146 _turnClient = [[ARDTURNClient alloc] initWithURL:turnRequestURL];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000147 [self configure];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000148 }
149 return self;
150}
151
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000152// TODO(tkchin): Provide signaling channel factory interface so we can recreate
153// channel if we need to on network failure. Also, make this the default public
154// constructor.
155- (instancetype)initWithRoomServerClient:(id<ARDRoomServerClient>)rsClient
156 signalingChannel:(id<ARDSignalingChannel>)channel
157 turnClient:(id<ARDTURNClient>)turnClient
158 delegate:(id<ARDAppClientDelegate>)delegate {
159 NSParameterAssert(rsClient);
160 NSParameterAssert(channel);
161 NSParameterAssert(turnClient);
162 if (self = [super init]) {
163 _roomServerClient = rsClient;
164 _channel = channel;
165 _turnClient = turnClient;
166 _delegate = delegate;
167 [self configure];
168 }
169 return self;
170}
171
172- (void)configure {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000173 _messageQueue = [NSMutableArray array];
kthelgasoncc882af2017-01-13 05:59:46 -0800174 _iceServers = [NSMutableArray array];
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700175 _fileLogger = [[RTCFileLogger alloc] init];
176 [_fileLogger start];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000177}
178
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000179- (void)dealloc {
Zeke Chind3325802015-08-14 11:00:02 -0700180 self.shouldGetStats = NO;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000181 [self disconnect];
182}
183
Zeke Chind3325802015-08-14 11:00:02 -0700184- (void)setShouldGetStats:(BOOL)shouldGetStats {
185 if (_shouldGetStats == shouldGetStats) {
186 return;
187 }
188 if (shouldGetStats) {
189 __weak ARDAppClient *weakSelf = self;
190 _statsTimer = [[ARDTimerProxy alloc] initWithInterval:1
191 repeats:YES
192 timerHandler:^{
193 ARDAppClient *strongSelf = weakSelf;
hjon79858f82016-03-13 22:08:26 -0700194 [strongSelf.peerConnection statsForTrack:nil
195 statsOutputLevel:RTCStatsOutputLevelDebug
196 completionHandler:^(NSArray *stats) {
197 dispatch_async(dispatch_get_main_queue(), ^{
198 ARDAppClient *strongSelf = weakSelf;
199 [strongSelf.delegate appClient:strongSelf didGetStats:stats];
200 });
201 }];
Zeke Chind3325802015-08-14 11:00:02 -0700202 }];
203 } else {
204 [_statsTimer invalidate];
205 _statsTimer = nil;
206 }
207 _shouldGetStats = shouldGetStats;
208}
209
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000210- (void)setState:(ARDAppClientState)state {
211 if (_state == state) {
212 return;
213 }
214 _state = state;
215 [_delegate appClient:self didChangeState:_state];
216}
217
218- (void)connectToRoomWithId:(NSString *)roomId
sakalc4adacf2017-03-28 01:22:48 -0700219 settings:(ARDSettingsModel *)settings
Anders Carlssone1500582017-06-15 16:05:13 +0200220 isLoopback:(BOOL)isLoopback {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000221 NSParameterAssert(roomId.length);
222 NSParameterAssert(_state == kARDAppClientStateDisconnected);
sakalc4adacf2017-03-28 01:22:48 -0700223 _settings = settings;
haysc913e6452015-10-02 11:44:03 -0700224 _isLoopback = isLoopback;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000225 self.state = kARDAppClientStateConnecting;
226
Anders Carlsson1d4c1522017-10-30 13:07:07 +0100227 RTCDefaultVideoDecoderFactory *decoderFactory = [[RTCDefaultVideoDecoderFactory alloc] init];
228 RTCDefaultVideoEncoderFactory *encoderFactory = [[RTCDefaultVideoEncoderFactory alloc] init];
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200229 encoderFactory.preferredCodec = [settings currentVideoCodecSettingFromStore];
230 _factory = [[RTCPeerConnectionFactory alloc] initWithEncoderFactory:encoderFactory
231 decoderFactory:decoderFactory];
232
tkchind1fb26d2016-02-03 01:51:18 -0800233#if defined(WEBRTC_IOS)
234 if (kARDAppClientEnableTracing) {
tkchin204177f2016-06-14 15:03:11 -0700235 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-trace.txt"];
tkchind1fb26d2016-02-03 01:51:18 -0800236 RTCStartInternalCapture(filePath);
237 }
238#endif
239
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000240 // Request TURN.
241 __weak ARDAppClient *weakSelf = self;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000242 [_turnClient requestServersWithCompletionHandler:^(NSArray *turnServers,
243 NSError *error) {
244 if (error) {
Anders Carlsson358f2e02018-06-04 10:24:37 +0200245 RTCLogError(@"Error retrieving TURN servers: %@", error.localizedDescription);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000246 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000247 ARDAppClient *strongSelf = weakSelf;
248 [strongSelf.iceServers addObjectsFromArray:turnServers];
249 strongSelf.isTurnComplete = YES;
250 [strongSelf startSignalingIfReady];
251 }];
jiayl@webrtc.org27f53172014-12-31 00:26:20 +0000252
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000253 // Join room on room server.
254 [_roomServerClient joinRoomWithRoomId:roomId
haysc913e6452015-10-02 11:44:03 -0700255 isLoopback:isLoopback
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000256 completionHandler:^(ARDJoinResponse *response, NSError *error) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000257 ARDAppClient *strongSelf = weakSelf;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000258 if (error) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000259 [strongSelf.delegate appClient:strongSelf didError:error];
260 return;
261 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000262 NSError *joinError =
263 [[strongSelf class] errorForJoinResultType:response.result];
264 if (joinError) {
tkchinc3f46a92015-07-23 12:50:55 -0700265 RTCLogError(@"Failed to join room:%@ on room server.", roomId);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000266 [strongSelf disconnect];
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000267 [strongSelf.delegate appClient:strongSelf didError:joinError];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000268 return;
269 }
tkchinc3f46a92015-07-23 12:50:55 -0700270 RTCLog(@"Joined room:%@ on room server.", roomId);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000271 strongSelf.roomId = response.roomId;
272 strongSelf.clientId = response.clientId;
273 strongSelf.isInitiator = response.isInitiator;
274 for (ARDSignalingMessage *message in response.messages) {
275 if (message.type == kARDSignalingMessageTypeOffer ||
276 message.type == kARDSignalingMessageTypeAnswer) {
277 strongSelf.hasReceivedSdp = YES;
278 [strongSelf.messageQueue insertObject:message atIndex:0];
279 } else {
280 [strongSelf.messageQueue addObject:message];
281 }
282 }
283 strongSelf.webSocketURL = response.webSocketURL;
284 strongSelf.webSocketRestURL = response.webSocketRestURL;
285 [strongSelf registerWithColliderIfReady];
286 [strongSelf startSignalingIfReady];
287 }];
288}
289
290- (void)disconnect {
291 if (_state == kARDAppClientStateDisconnected) {
292 return;
293 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000294 if (self.hasJoinedRoomServerRoom) {
295 [_roomServerClient leaveRoomWithRoomId:_roomId
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000296 clientId:_clientId
297 completionHandler:nil];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000298 }
299 if (_channel) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000300 if (_channel.state == kARDSignalingChannelStateRegistered) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000301 // Tell the other client we're hanging up.
302 ARDByeMessage *byeMessage = [[ARDByeMessage alloc] init];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000303 [_channel sendMessage:byeMessage];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000304 }
305 // Disconnect from collider.
306 _channel = nil;
307 }
308 _clientId = nil;
309 _roomId = nil;
310 _isInitiator = NO;
311 _hasReceivedSdp = NO;
312 _messageQueue = [NSMutableArray array];
sakalc522e752017-04-05 12:17:48 -0700313 _localVideoTrack = nil;
ivoc14d5dbe2016-07-04 07:06:55 -0700314#if defined(WEBRTC_IOS)
tkchinfce0e2c2016-08-30 12:58:11 -0700315 [_factory stopAecDump];
ivoc14d5dbe2016-07-04 07:06:55 -0700316 [_peerConnection stopRtcEventLog];
317#endif
magjedcc8b9062017-07-24 07:32:33 -0700318 [_peerConnection close];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000319 _peerConnection = nil;
320 self.state = kARDAppClientStateDisconnected;
tkchind1fb26d2016-02-03 01:51:18 -0800321#if defined(WEBRTC_IOS)
adam.fedorbcc5d872016-11-07 14:53:28 -0800322 if (kARDAppClientEnableTracing) {
323 RTCStopInternalCapture();
324 }
tkchind1fb26d2016-02-03 01:51:18 -0800325#endif
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000326}
327
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000328#pragma mark - ARDSignalingChannelDelegate
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000329
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000330- (void)channel:(id<ARDSignalingChannel>)channel
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000331 didReceiveMessage:(ARDSignalingMessage *)message {
332 switch (message.type) {
333 case kARDSignalingMessageTypeOffer:
334 case kARDSignalingMessageTypeAnswer:
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000335 // Offers and answers must be processed before any other message, so we
336 // place them at the front of the queue.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000337 _hasReceivedSdp = YES;
338 [_messageQueue insertObject:message atIndex:0];
339 break;
340 case kARDSignalingMessageTypeCandidate:
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700341 case kARDSignalingMessageTypeCandidateRemoval:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000342 [_messageQueue addObject:message];
343 break;
344 case kARDSignalingMessageTypeBye:
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000345 // Disconnects can be processed immediately.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000346 [self processSignalingMessage:message];
347 return;
348 }
349 [self drainMessageQueueIfReady];
350}
351
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000352- (void)channel:(id<ARDSignalingChannel>)channel
353 didChangeState:(ARDSignalingChannelState)state {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000354 switch (state) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000355 case kARDSignalingChannelStateOpen:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000356 break;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000357 case kARDSignalingChannelStateRegistered:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000358 break;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000359 case kARDSignalingChannelStateClosed:
360 case kARDSignalingChannelStateError:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000361 // TODO(tkchin): reconnection scenarios. Right now we just disconnect
362 // completely if the websocket connection fails.
363 [self disconnect];
364 break;
365 }
366}
367
368#pragma mark - RTCPeerConnectionDelegate
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000369// Callbacks for this delegate occur on non-main thread and need to be
370// dispatched back to main queue as needed.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000371
372- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700373 didChangeSignalingState:(RTCSignalingState)stateChanged {
374 RTCLog(@"Signaling state changed: %ld", (long)stateChanged);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000375}
376
377- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700378 didAddStream:(RTCMediaStream *)stream {
Steve Antonaf23b752018-03-01 09:22:48 -0800379 RTCLog(@"Stream with %lu video tracks and %lu audio tracks was added.",
380 (unsigned long)stream.videoTracks.count,
381 (unsigned long)stream.audioTracks.count);
382}
383
384- (void)peerConnection:(RTCPeerConnection *)peerConnection
385 didStartReceivingOnTransceiver:(RTCRtpTransceiver *)transceiver {
386 RTCMediaStreamTrack *track = transceiver.receiver.track;
387 RTCLog(@"Now receiving %@ on track %@.", track.kind, track.trackId);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000388}
389
390- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700391 didRemoveStream:(RTCMediaStream *)stream {
tkchinc3f46a92015-07-23 12:50:55 -0700392 RTCLog(@"Stream was removed.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000393}
394
hjon79858f82016-03-13 22:08:26 -0700395- (void)peerConnectionShouldNegotiate:(RTCPeerConnection *)peerConnection {
tkchinc3f46a92015-07-23 12:50:55 -0700396 RTCLog(@"WARNING: Renegotiation needed but unimplemented.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000397}
398
399- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700400 didChangeIceConnectionState:(RTCIceConnectionState)newState {
401 RTCLog(@"ICE state changed: %ld", (long)newState);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000402 dispatch_async(dispatch_get_main_queue(), ^{
403 [_delegate appClient:self didChangeConnectionState:newState];
404 });
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000405}
406
407- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700408 didChangeIceGatheringState:(RTCIceGatheringState)newState {
409 RTCLog(@"ICE gathering state changed: %ld", (long)newState);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000410}
411
412- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700413 didGenerateIceCandidate:(RTCIceCandidate *)candidate {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000414 dispatch_async(dispatch_get_main_queue(), ^{
415 ARDICECandidateMessage *message =
416 [[ARDICECandidateMessage alloc] initWithCandidate:candidate];
417 [self sendSignalingMessage:message];
418 });
419}
420
Zeke Chind3325802015-08-14 11:00:02 -0700421- (void)peerConnection:(RTCPeerConnection *)peerConnection
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700422 didRemoveIceCandidates:(NSArray<RTCIceCandidate *> *)candidates {
423 dispatch_async(dispatch_get_main_queue(), ^{
424 ARDICECandidateRemovalMessage *message =
425 [[ARDICECandidateRemovalMessage alloc]
426 initWithRemovedCandidates:candidates];
427 [self sendSignalingMessage:message];
428 });
429}
430
431- (void)peerConnection:(RTCPeerConnection *)peerConnection
Zeke Chind3325802015-08-14 11:00:02 -0700432 didOpenDataChannel:(RTCDataChannel *)dataChannel {
433}
434
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000435#pragma mark - RTCSessionDescriptionDelegate
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000436// Callbacks for this delegate occur on non-main thread and need to be
437// dispatched back to main queue as needed.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000438
439- (void)peerConnection:(RTCPeerConnection *)peerConnection
440 didCreateSessionDescription:(RTCSessionDescription *)sdp
441 error:(NSError *)error {
442 dispatch_async(dispatch_get_main_queue(), ^{
443 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700444 RTCLogError(@"Failed to create session description. Error: %@", error);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000445 [self disconnect];
446 NSDictionary *userInfo = @{
447 NSLocalizedDescriptionKey: @"Failed to create session description.",
448 };
449 NSError *sdpError =
450 [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
451 code:kARDAppClientErrorCreateSDP
452 userInfo:userInfo];
453 [_delegate appClient:self didError:sdpError];
454 return;
455 }
hjon79858f82016-03-13 22:08:26 -0700456 __weak ARDAppClient *weakSelf = self;
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200457 [_peerConnection setLocalDescription:sdp
hjon79858f82016-03-13 22:08:26 -0700458 completionHandler:^(NSError *error) {
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200459 ARDAppClient *strongSelf = weakSelf;
460 [strongSelf peerConnection:strongSelf.peerConnection
461 didSetSessionDescriptionWithError:error];
462 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000463 ARDSessionDescriptionMessage *message =
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200464 [[ARDSessionDescriptionMessage alloc] initWithDescription:sdp];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000465 [self sendSignalingMessage:message];
denicija9af2b602016-11-17 00:43:43 -0800466 [self setMaxBitrateForPeerConnectionVideoSender];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000467 });
468}
469
470- (void)peerConnection:(RTCPeerConnection *)peerConnection
471 didSetSessionDescriptionWithError:(NSError *)error {
472 dispatch_async(dispatch_get_main_queue(), ^{
473 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700474 RTCLogError(@"Failed to set session description. Error: %@", error);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000475 [self disconnect];
476 NSDictionary *userInfo = @{
477 NSLocalizedDescriptionKey: @"Failed to set session description.",
478 };
479 NSError *sdpError =
480 [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
481 code:kARDAppClientErrorSetSDP
482 userInfo:userInfo];
483 [_delegate appClient:self didError:sdpError];
484 return;
485 }
486 // If we're answering and we've just set the remote offer we need to create
487 // an answer and set the local description.
488 if (!_isInitiator && !_peerConnection.localDescription) {
489 RTCMediaConstraints *constraints = [self defaultAnswerConstraints];
hjon79858f82016-03-13 22:08:26 -0700490 __weak ARDAppClient *weakSelf = self;
491 [_peerConnection answerForConstraints:constraints
492 completionHandler:^(RTCSessionDescription *sdp,
493 NSError *error) {
494 ARDAppClient *strongSelf = weakSelf;
495 [strongSelf peerConnection:strongSelf.peerConnection
496 didCreateSessionDescription:sdp
497 error:error];
498 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000499 }
500 });
501}
502
503#pragma mark - Private
504
tkchin204177f2016-06-14 15:03:11 -0700505#if defined(WEBRTC_IOS)
506
507- (NSString *)documentsFilePathForFileName:(NSString *)fileName {
508 NSParameterAssert(fileName.length);
509 NSArray *paths = NSSearchPathForDirectoriesInDomains(
510 NSDocumentDirectory, NSUserDomainMask, YES);
511 NSString *documentsDirPath = paths.firstObject;
512 NSString *filePath =
513 [documentsDirPath stringByAppendingPathComponent:fileName];
514 return filePath;
515}
516
517#endif
518
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000519- (BOOL)hasJoinedRoomServerRoom {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000520 return _clientId.length;
521}
522
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000523// Begins the peer connection connection process if we have both joined a room
524// on the room server and tried to obtain a TURN server. Otherwise does nothing.
525// A peer connection object will be created with a stream that contains local
526// audio and video capture. If this client is the caller, an offer is created as
527// well, otherwise the client will wait for an offer to arrive.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000528- (void)startSignalingIfReady {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000529 if (!_isTurnComplete || !self.hasJoinedRoomServerRoom) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000530 return;
531 }
532 self.state = kARDAppClientStateConnected;
533
534 // Create peer connection.
535 RTCMediaConstraints *constraints = [self defaultPeerConnectionConstraints];
Zeke Chinbc7dd7e2015-05-29 14:59:14 -0700536 RTCConfiguration *config = [[RTCConfiguration alloc] init];
Michael Iedemaccee56b2018-07-05 15:28:24 +0200537 RTCCertificate *pcert = [RTCCertificate generateCertificateWithParams:@{
538 @"expires" : @100000,
539 @"name" : @"RSASSA-PKCS1-v1_5"
540 }];
Zeke Chinbc7dd7e2015-05-29 14:59:14 -0700541 config.iceServers = _iceServers;
Steve Antonaf23b752018-03-01 09:22:48 -0800542 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
Michael Iedemaccee56b2018-07-05 15:28:24 +0200543 config.certificate = pcert;
544
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700545 _peerConnection = [_factory peerConnectionWithConfiguration:config
546 constraints:constraints
547 delegate:self];
skvladf3569c82016-04-29 15:30:16 -0700548 // Create AV senders.
Alex Narestb3944f02017-10-13 14:56:18 +0200549 [self createMediaSenders];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000550 if (_isInitiator) {
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000551 // Send offer.
hjon79858f82016-03-13 22:08:26 -0700552 __weak ARDAppClient *weakSelf = self;
553 [_peerConnection offerForConstraints:[self defaultOfferConstraints]
554 completionHandler:^(RTCSessionDescription *sdp,
555 NSError *error) {
556 ARDAppClient *strongSelf = weakSelf;
557 [strongSelf peerConnection:strongSelf.peerConnection
558 didCreateSessionDescription:sdp
559 error:error];
560 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000561 } else {
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000562 // Check if we've received an offer.
563 [self drainMessageQueueIfReady];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000564 }
ivoc14d5dbe2016-07-04 07:06:55 -0700565#if defined(WEBRTC_IOS)
566 // Start event log.
567 if (kARDAppClientEnableRtcEventLog) {
568 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-rtceventlog"];
569 if (![_peerConnection startRtcEventLogWithFilePath:filePath
570 maxSizeInBytes:kARDAppClientRtcEventLogMaxSizeInBytes]) {
571 RTCLogError(@"Failed to start event logging.");
572 }
573 }
peah5085b0c2016-08-25 22:15:14 -0700574
575 // Start aecdump diagnostic recording.
Anders Carlssone1500582017-06-15 16:05:13 +0200576 if ([_settings currentCreateAecDumpSettingFromStore]) {
tkchinfce0e2c2016-08-30 12:58:11 -0700577 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-audio.aecdump"];
578 if (![_factory startAecDumpWithFilePath:filePath
579 maxSizeInBytes:kARDAppClientAecDumpMaxSizeInBytes]) {
580 RTCLogError(@"Failed to start aec dump.");
peah5085b0c2016-08-25 22:15:14 -0700581 }
582 }
ivoc14d5dbe2016-07-04 07:06:55 -0700583#endif
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000584}
585
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000586// Processes the messages that we've received from the room server and the
587// signaling channel. The offer or answer message must be processed before other
588// signaling messages, however they can arrive out of order. Hence, this method
589// only processes pending messages if there is a peer connection object and
590// if we have received either an offer or answer.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000591- (void)drainMessageQueueIfReady {
592 if (!_peerConnection || !_hasReceivedSdp) {
593 return;
594 }
595 for (ARDSignalingMessage *message in _messageQueue) {
596 [self processSignalingMessage:message];
597 }
598 [_messageQueue removeAllObjects];
599}
600
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000601// Processes the given signaling message based on its type.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000602- (void)processSignalingMessage:(ARDSignalingMessage *)message {
603 NSParameterAssert(_peerConnection ||
604 message.type == kARDSignalingMessageTypeBye);
605 switch (message.type) {
606 case kARDSignalingMessageTypeOffer:
607 case kARDSignalingMessageTypeAnswer: {
608 ARDSessionDescriptionMessage *sdpMessage =
609 (ARDSessionDescriptionMessage *)message;
610 RTCSessionDescription *description = sdpMessage.sessionDescription;
hjon79858f82016-03-13 22:08:26 -0700611 __weak ARDAppClient *weakSelf = self;
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200612 [_peerConnection setRemoteDescription:description
hjon79858f82016-03-13 22:08:26 -0700613 completionHandler:^(NSError *error) {
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200614 ARDAppClient *strongSelf = weakSelf;
615 [strongSelf peerConnection:strongSelf.peerConnection
616 didSetSessionDescriptionWithError:error];
617 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000618 break;
619 }
620 case kARDSignalingMessageTypeCandidate: {
621 ARDICECandidateMessage *candidateMessage =
622 (ARDICECandidateMessage *)message;
hjon79858f82016-03-13 22:08:26 -0700623 [_peerConnection addIceCandidate:candidateMessage.candidate];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000624 break;
625 }
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700626 case kARDSignalingMessageTypeCandidateRemoval: {
627 ARDICECandidateRemovalMessage *candidateMessage =
628 (ARDICECandidateRemovalMessage *)message;
629 [_peerConnection removeIceCandidates:candidateMessage.candidates];
630 break;
631 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000632 case kARDSignalingMessageTypeBye:
633 // Other client disconnected.
634 // TODO(tkchin): support waiting in room for next client. For now just
635 // disconnect.
636 [self disconnect];
637 break;
638 }
639}
640
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000641// Sends a signaling message to the other client. The caller will send messages
642// through the room server, whereas the callee will send messages over the
643// signaling channel.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000644- (void)sendSignalingMessage:(ARDSignalingMessage *)message {
645 if (_isInitiator) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000646 __weak ARDAppClient *weakSelf = self;
647 [_roomServerClient sendMessage:message
648 forRoomId:_roomId
649 clientId:_clientId
650 completionHandler:^(ARDMessageResponse *response,
651 NSError *error) {
652 ARDAppClient *strongSelf = weakSelf;
653 if (error) {
654 [strongSelf.delegate appClient:strongSelf didError:error];
655 return;
656 }
657 NSError *messageError =
658 [[strongSelf class] errorForMessageResultType:response.result];
659 if (messageError) {
660 [strongSelf.delegate appClient:strongSelf didError:messageError];
661 return;
662 }
663 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000664 } else {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000665 [_channel sendMessage:message];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000666 }
667}
668
denicija9af2b602016-11-17 00:43:43 -0800669- (void)setMaxBitrateForPeerConnectionVideoSender {
670 for (RTCRtpSender *sender in _peerConnection.senders) {
671 if (sender.track != nil) {
672 if ([sender.track.kind isEqualToString:kARDVideoTrackKind]) {
sakalc4adacf2017-03-28 01:22:48 -0700673 [self setMaxBitrate:[_settings currentMaxBitrateSettingFromStore] forVideoSender:sender];
denicija9af2b602016-11-17 00:43:43 -0800674 }
675 }
denicija8c375de2016-11-08 06:28:17 -0800676 }
677}
678
denicija9af2b602016-11-17 00:43:43 -0800679- (void)setMaxBitrate:(NSNumber *)maxBitrate forVideoSender:(RTCRtpSender *)sender {
680 if (maxBitrate.intValue <= 0) {
681 return;
682 }
683
684 RTCRtpParameters *parametersToModify = sender.parameters;
685 for (RTCRtpEncodingParameters *encoding in parametersToModify.encodings) {
686 encoding.maxBitrateBps = @(maxBitrate.intValue * kKbpsMultiplier);
687 }
688 [sender setParameters:parametersToModify];
689}
690
Steve Antonaf23b752018-03-01 09:22:48 -0800691- (RTCRtpTransceiver *)videoTransceiver {
692 for (RTCRtpTransceiver *transceiver in _peerConnection.transceivers) {
693 if (transceiver.mediaType == RTCRtpMediaTypeVideo) {
694 return transceiver;
695 }
696 }
697 return nil;
698}
699
Alex Narestb3944f02017-10-13 14:56:18 +0200700- (void)createMediaSenders {
tkchinab1293a2016-08-30 12:35:05 -0700701 RTCMediaConstraints *constraints = [self defaultMediaAudioConstraints];
702 RTCAudioSource *source = [_factory audioSourceWithConstraints:constraints];
703 RTCAudioTrack *track = [_factory audioTrackWithSource:source
704 trackId:kARDAudioTrackId];
Seth Hampson513449e2018-03-06 09:35:56 -0800705 [_peerConnection addTrack:track streamIds:@[ kARDMediaStreamId ]];
Alex Narestb3944f02017-10-13 14:56:18 +0200706 _localVideoTrack = [self createLocalVideoTrack];
707 if (_localVideoTrack) {
Seth Hampson513449e2018-03-06 09:35:56 -0800708 [_peerConnection addTrack:_localVideoTrack streamIds:@[ kARDMediaStreamId ]];
Piasy Xue7e06022018-05-30 23:31:07 +0800709 [_delegate appClient:self didReceiveLocalVideoTrack:_localVideoTrack];
Steve Antonaf23b752018-03-01 09:22:48 -0800710 // We can set up rendering for the remote track right away since the transceiver already has an
711 // RTCRtpReceiver with a track. The track will automatically get unmuted and produce frames
712 // once RTP is received.
713 RTCVideoTrack *track = (RTCVideoTrack *)([self videoTransceiver].receiver.track);
714 [_delegate appClient:self didReceiveRemoteVideoTrack:track];
Alex Narestb3944f02017-10-13 14:56:18 +0200715 }
Zeke Chin57cc74e2015-05-05 07:52:31 -0700716}
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000717
Zeke Chin57cc74e2015-05-05 07:52:31 -0700718- (RTCVideoTrack *)createLocalVideoTrack {
Daniela012b56b2017-11-15 13:15:24 +0100719 if ([_settings currentAudioOnlySettingFromStore]) {
720 return nil;
721 }
722
723 RTCVideoSource *source = [_factory videoSource];
724
kthelgason314bc5f2016-08-31 10:23:27 -0700725#if !TARGET_IPHONE_SIMULATOR
Anders Carlsson358f2e02018-06-04 10:24:37 +0200726 if (self.isBroadcast) {
727 ARDExternalSampleCapturer *capturer =
728 [[ARDExternalSampleCapturer alloc] initWithDelegate:source];
729 [_delegate appClient:self didCreateLocalExternalSampleCapturer:capturer];
730 } else {
731 RTCCameraVideoCapturer *capturer = [[RTCCameraVideoCapturer alloc] initWithDelegate:source];
732 [_delegate appClient:self didCreateLocalCapturer:capturer];
733 }
Daniela012b56b2017-11-15 13:15:24 +0100734#else
735#if defined(__IPHONE_11_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0)
736 if (@available(iOS 10, *)) {
737 RTCFileVideoCapturer *fileCapturer = [[RTCFileVideoCapturer alloc] initWithDelegate:source];
738 [_delegate appClient:self didCreateLocalFileCapturer:fileCapturer];
haysc913e6452015-10-02 11:44:03 -0700739 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000740#endif
Daniela012b56b2017-11-15 13:15:24 +0100741#endif
742
743 return [_factory videoTrackWithSource:source trackId:kARDVideoTrackId];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000744}
745
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000746#pragma mark - Collider methods
747
748- (void)registerWithColliderIfReady {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000749 if (!self.hasJoinedRoomServerRoom) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000750 return;
751 }
752 // Open WebSocket connection.
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000753 if (!_channel) {
754 _channel =
755 [[ARDWebSocketChannel alloc] initWithURL:_websocketURL
756 restURL:_websocketRestURL
757 delegate:self];
haysc913e6452015-10-02 11:44:03 -0700758 if (_isLoopback) {
759 _loopbackChannel =
760 [[ARDLoopbackWebSocketChannel alloc] initWithURL:_websocketURL
761 restURL:_websocketRestURL];
762 }
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000763 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000764 [_channel registerForRoomId:_roomId clientId:_clientId];
haysc913e6452015-10-02 11:44:03 -0700765 if (_isLoopback) {
766 [_loopbackChannel registerForRoomId:_roomId clientId:@"LOOPBACK_CLIENT_ID"];
767 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000768}
769
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000770#pragma mark - Defaults
771
tkchinab1293a2016-08-30 12:35:05 -0700772 - (RTCMediaConstraints *)defaultMediaAudioConstraints {
Sam Zackrisson9e981f02018-02-28 16:20:58 +0100773 NSDictionary *mandatoryConstraints = @{};
denicijad17d5362016-11-02 02:56:09 -0700774 RTCMediaConstraints *constraints =
775 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstraints
776 optionalConstraints:nil];
tkchinab1293a2016-08-30 12:35:05 -0700777 return constraints;
778}
779
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000780- (RTCMediaConstraints *)defaultAnswerConstraints {
781 return [self defaultOfferConstraints];
782}
783
784- (RTCMediaConstraints *)defaultOfferConstraints {
hjon79858f82016-03-13 22:08:26 -0700785 NSDictionary *mandatoryConstraints = @{
786 @"OfferToReceiveAudio" : @"true",
787 @"OfferToReceiveVideo" : @"true"
788 };
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000789 RTCMediaConstraints* constraints =
790 [[RTCMediaConstraints alloc]
791 initWithMandatoryConstraints:mandatoryConstraints
792 optionalConstraints:nil];
793 return constraints;
794}
795
796- (RTCMediaConstraints *)defaultPeerConnectionConstraints {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000797 if (_defaultPeerConnectionConstraints) {
798 return _defaultPeerConnectionConstraints;
799 }
haysc913e6452015-10-02 11:44:03 -0700800 NSString *value = _isLoopback ? @"false" : @"true";
hjon79858f82016-03-13 22:08:26 -0700801 NSDictionary *optionalConstraints = @{ @"DtlsSrtpKeyAgreement" : value };
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000802 RTCMediaConstraints* constraints =
803 [[RTCMediaConstraints alloc]
804 initWithMandatoryConstraints:nil
805 optionalConstraints:optionalConstraints];
806 return constraints;
807}
808
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000809#pragma mark - Errors
810
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000811+ (NSError *)errorForJoinResultType:(ARDJoinResultType)resultType {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000812 NSError *error = nil;
813 switch (resultType) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000814 case kARDJoinResultTypeSuccess:
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000815 break;
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000816 case kARDJoinResultTypeUnknown: {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000817 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
818 code:kARDAppClientErrorUnknown
819 userInfo:@{
820 NSLocalizedDescriptionKey: @"Unknown error.",
821 }];
822 break;
823 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000824 case kARDJoinResultTypeFull: {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000825 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
826 code:kARDAppClientErrorRoomFull
827 userInfo:@{
828 NSLocalizedDescriptionKey: @"Room is full.",
829 }];
830 break;
831 }
832 }
833 return error;
834}
835
836+ (NSError *)errorForMessageResultType:(ARDMessageResultType)resultType {
837 NSError *error = nil;
838 switch (resultType) {
839 case kARDMessageResultTypeSuccess:
840 break;
841 case kARDMessageResultTypeUnknown:
842 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
843 code:kARDAppClientErrorUnknown
844 userInfo:@{
845 NSLocalizedDescriptionKey: @"Unknown error.",
846 }];
847 break;
848 case kARDMessageResultTypeInvalidClient:
849 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
850 code:kARDAppClientErrorInvalidClient
851 userInfo:@{
852 NSLocalizedDescriptionKey: @"Invalid client.",
853 }];
854 break;
855 case kARDMessageResultTypeInvalidRoom:
856 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
857 code:kARDAppClientErrorInvalidRoom
858 userInfo:@{
859 NSLocalizedDescriptionKey: @"Invalid room.",
860 }];
861 break;
862 }
863 return error;
864}
865
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000866@end