blob: e4dbad416eb8a74faad109ab1ff0e4e8ad63a356 [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/RTCAudioTrack.h"
sakalc522e752017-04-05 12:17:48 -070014#import "WebRTC/RTCCameraVideoCapturer.h"
tkchin9eeb6242016-04-27 01:54:20 -070015#import "WebRTC/RTCConfiguration.h"
16#import "WebRTC/RTCFileLogger.h"
Daniela012b56b2017-11-15 13:15:24 +010017#import "WebRTC/RTCFileVideoCapturer.h"
tkchin9eeb6242016-04-27 01:54:20 -070018#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"
Steve Antonaf23b752018-03-01 09:22:48 -080024#import "WebRTC/RTCRtpTransceiver.h"
tkchin204177f2016-06-14 15:03:11 -070025#import "WebRTC/RTCTracing.h"
Anders Carlsson1d4c1522017-10-30 13:07:07 +010026#import "WebRTC/RTCVideoCodecFactory.h"
Kári Tristan Helgason0d3c9a32018-02-08 20:17:34 +010027#import "WebRTC/RTCVideoSource.h"
sakalc522e752017-04-05 12:17:48 -070028#import "WebRTC/RTCVideoTrack.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000029
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +000030#import "ARDAppEngineClient.h"
Anders Carlsson358f2e02018-06-04 10:24:37 +020031#import "ARDExternalSampleCapturer.h"
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +000032#import "ARDJoinResponse.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000033#import "ARDMessageResponse.h"
sakalc4adacf2017-03-28 01:22:48 -070034#import "ARDSettingsModel.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000035#import "ARDSignalingMessage.h"
sakalc4adacf2017-03-28 01:22:48 -070036#import "ARDTURNClient+Internal.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000037#import "ARDUtilities.h"
38#import "ARDWebSocketChannel.h"
hjon79858f82016-03-13 22:08:26 -070039#import "RTCIceCandidate+JSON.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000040#import "RTCSessionDescription+JSON.h"
Zeke Chin57cc74e2015-05-05 07:52:31 -070041
kthelgasoncc882af2017-01-13 05:59:46 -080042static NSString * const kARDIceServerRequestUrl = @"https://appr.tc/params";
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000043
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +000044static NSString * const kARDAppClientErrorDomain = @"ARDAppClient";
45static NSInteger const kARDAppClientErrorUnknown = -1;
46static NSInteger const kARDAppClientErrorRoomFull = -2;
47static NSInteger const kARDAppClientErrorCreateSDP = -3;
48static NSInteger const kARDAppClientErrorSetSDP = -4;
49static NSInteger const kARDAppClientErrorInvalidClient = -5;
50static NSInteger const kARDAppClientErrorInvalidRoom = -6;
skvladf3569c82016-04-29 15:30:16 -070051static NSString * const kARDMediaStreamId = @"ARDAMS";
52static NSString * const kARDAudioTrackId = @"ARDAMSa0";
53static NSString * const kARDVideoTrackId = @"ARDAMSv0";
denicija9af2b602016-11-17 00:43:43 -080054static NSString * const kARDVideoTrackKind = @"video";
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000055
tkchin204177f2016-06-14 15:03:11 -070056// TODO(tkchin): Add these as UI options.
Mirko Bonadei3e603ec2018-07-10 14:21:23 +020057#if defined(WEBRTC_IOS)
tkchind1fb26d2016-02-03 01:51:18 -080058static BOOL const kARDAppClientEnableTracing = NO;
tkchin204177f2016-06-14 15:03:11 -070059static BOOL const kARDAppClientEnableRtcEventLog = YES;
tkchinfce0e2c2016-08-30 12:58:11 -070060static int64_t const kARDAppClientAecDumpMaxSizeInBytes = 5e6; // 5 MB.
tkchin204177f2016-06-14 15:03:11 -070061static int64_t const kARDAppClientRtcEventLogMaxSizeInBytes = 5e6; // 5 MB.
Mirko Bonadei3e603ec2018-07-10 14:21:23 +020062#endif
denicija9af2b602016-11-17 00:43:43 -080063static int const kKbpsMultiplier = 1000;
tkchind1fb26d2016-02-03 01:51:18 -080064
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;
sakalc4adacf2017-03-28 01:22:48 -0700109 ARDSettingsModel *_settings;
sakalc522e752017-04-05 12:17:48 -0700110 RTCVideoTrack *_localVideoTrack;
Zeke Chind3325802015-08-14 11:00:02 -0700111}
112
113@synthesize shouldGetStats = _shouldGetStats;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000114@synthesize state = _state;
Zeke Chind3325802015-08-14 11:00:02 -0700115@synthesize delegate = _delegate;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000116@synthesize roomServerClient = _roomServerClient;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000117@synthesize channel = _channel;
haysc913e6452015-10-02 11:44:03 -0700118@synthesize loopbackChannel = _loopbackChannel;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000119@synthesize turnClient = _turnClient;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000120@synthesize peerConnection = _peerConnection;
121@synthesize factory = _factory;
122@synthesize messageQueue = _messageQueue;
123@synthesize isTurnComplete = _isTurnComplete;
124@synthesize hasReceivedSdp = _hasReceivedSdp;
125@synthesize roomId = _roomId;
126@synthesize clientId = _clientId;
127@synthesize isInitiator = _isInitiator;
128@synthesize iceServers = _iceServers;
129@synthesize webSocketURL = _websocketURL;
130@synthesize webSocketRestURL = _websocketRestURL;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000131@synthesize defaultPeerConnectionConstraints =
132 _defaultPeerConnectionConstraints;
haysc913e6452015-10-02 11:44:03 -0700133@synthesize isLoopback = _isLoopback;
Anders Carlsson358f2e02018-06-04 10:24:37 +0200134@synthesize broadcast = _broadcast;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000135
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000136- (instancetype)init {
sakalc4adacf2017-03-28 01:22:48 -0700137 return [self initWithDelegate:nil];
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000138}
139
sakalc4adacf2017-03-28 01:22:48 -0700140- (instancetype)initWithDelegate:(id<ARDAppClientDelegate>)delegate {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000141 if (self = [super init]) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000142 _roomServerClient = [[ARDAppEngineClient alloc] init];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000143 _delegate = delegate;
kthelgasoncc882af2017-01-13 05:59:46 -0800144 NSURL *turnRequestURL = [NSURL URLWithString:kARDIceServerRequestUrl];
145 _turnClient = [[ARDTURNClient alloc] initWithURL:turnRequestURL];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000146 [self configure];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000147 }
148 return self;
149}
150
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000151// TODO(tkchin): Provide signaling channel factory interface so we can recreate
152// channel if we need to on network failure. Also, make this the default public
153// constructor.
154- (instancetype)initWithRoomServerClient:(id<ARDRoomServerClient>)rsClient
155 signalingChannel:(id<ARDSignalingChannel>)channel
156 turnClient:(id<ARDTURNClient>)turnClient
157 delegate:(id<ARDAppClientDelegate>)delegate {
158 NSParameterAssert(rsClient);
159 NSParameterAssert(channel);
160 NSParameterAssert(turnClient);
161 if (self = [super init]) {
162 _roomServerClient = rsClient;
163 _channel = channel;
164 _turnClient = turnClient;
165 _delegate = delegate;
166 [self configure];
167 }
168 return self;
169}
170
171- (void)configure {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000172 _messageQueue = [NSMutableArray array];
kthelgasoncc882af2017-01-13 05:59:46 -0800173 _iceServers = [NSMutableArray array];
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700174 _fileLogger = [[RTCFileLogger alloc] init];
175 [_fileLogger start];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000176}
177
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000178- (void)dealloc {
Zeke Chind3325802015-08-14 11:00:02 -0700179 self.shouldGetStats = NO;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000180 [self disconnect];
181}
182
Zeke Chind3325802015-08-14 11:00:02 -0700183- (void)setShouldGetStats:(BOOL)shouldGetStats {
184 if (_shouldGetStats == shouldGetStats) {
185 return;
186 }
187 if (shouldGetStats) {
188 __weak ARDAppClient *weakSelf = self;
189 _statsTimer = [[ARDTimerProxy alloc] initWithInterval:1
190 repeats:YES
191 timerHandler:^{
192 ARDAppClient *strongSelf = weakSelf;
hjon79858f82016-03-13 22:08:26 -0700193 [strongSelf.peerConnection statsForTrack:nil
194 statsOutputLevel:RTCStatsOutputLevelDebug
195 completionHandler:^(NSArray *stats) {
196 dispatch_async(dispatch_get_main_queue(), ^{
197 ARDAppClient *strongSelf = weakSelf;
198 [strongSelf.delegate appClient:strongSelf didGetStats:stats];
199 });
200 }];
Zeke Chind3325802015-08-14 11:00:02 -0700201 }];
202 } else {
203 [_statsTimer invalidate];
204 _statsTimer = nil;
205 }
206 _shouldGetStats = shouldGetStats;
207}
208
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000209- (void)setState:(ARDAppClientState)state {
210 if (_state == state) {
211 return;
212 }
213 _state = state;
214 [_delegate appClient:self didChangeState:_state];
215}
216
217- (void)connectToRoomWithId:(NSString *)roomId
sakalc4adacf2017-03-28 01:22:48 -0700218 settings:(ARDSettingsModel *)settings
Anders Carlssone1500582017-06-15 16:05:13 +0200219 isLoopback:(BOOL)isLoopback {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000220 NSParameterAssert(roomId.length);
221 NSParameterAssert(_state == kARDAppClientStateDisconnected);
sakalc4adacf2017-03-28 01:22:48 -0700222 _settings = settings;
haysc913e6452015-10-02 11:44:03 -0700223 _isLoopback = isLoopback;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000224 self.state = kARDAppClientStateConnecting;
225
Anders Carlsson1d4c1522017-10-30 13:07:07 +0100226 RTCDefaultVideoDecoderFactory *decoderFactory = [[RTCDefaultVideoDecoderFactory alloc] init];
227 RTCDefaultVideoEncoderFactory *encoderFactory = [[RTCDefaultVideoEncoderFactory alloc] init];
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200228 encoderFactory.preferredCodec = [settings currentVideoCodecSettingFromStore];
229 _factory = [[RTCPeerConnectionFactory alloc] initWithEncoderFactory:encoderFactory
230 decoderFactory:decoderFactory];
231
tkchind1fb26d2016-02-03 01:51:18 -0800232#if defined(WEBRTC_IOS)
233 if (kARDAppClientEnableTracing) {
tkchin204177f2016-06-14 15:03:11 -0700234 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-trace.txt"];
tkchind1fb26d2016-02-03 01:51:18 -0800235 RTCStartInternalCapture(filePath);
236 }
237#endif
238
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000239 // Request TURN.
240 __weak ARDAppClient *weakSelf = self;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000241 [_turnClient requestServersWithCompletionHandler:^(NSArray *turnServers,
242 NSError *error) {
243 if (error) {
Anders Carlsson358f2e02018-06-04 10:24:37 +0200244 RTCLogError(@"Error retrieving TURN servers: %@", error.localizedDescription);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000245 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000246 ARDAppClient *strongSelf = weakSelf;
247 [strongSelf.iceServers addObjectsFromArray:turnServers];
248 strongSelf.isTurnComplete = YES;
249 [strongSelf startSignalingIfReady];
250 }];
jiayl@webrtc.org27f53172014-12-31 00:26:20 +0000251
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000252 // Join room on room server.
253 [_roomServerClient joinRoomWithRoomId:roomId
haysc913e6452015-10-02 11:44:03 -0700254 isLoopback:isLoopback
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000255 completionHandler:^(ARDJoinResponse *response, NSError *error) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000256 ARDAppClient *strongSelf = weakSelf;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000257 if (error) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000258 [strongSelf.delegate appClient:strongSelf didError:error];
259 return;
260 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000261 NSError *joinError =
262 [[strongSelf class] errorForJoinResultType:response.result];
263 if (joinError) {
tkchinc3f46a92015-07-23 12:50:55 -0700264 RTCLogError(@"Failed to join room:%@ on room server.", roomId);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000265 [strongSelf disconnect];
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000266 [strongSelf.delegate appClient:strongSelf didError:joinError];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000267 return;
268 }
tkchinc3f46a92015-07-23 12:50:55 -0700269 RTCLog(@"Joined room:%@ on room server.", roomId);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000270 strongSelf.roomId = response.roomId;
271 strongSelf.clientId = response.clientId;
272 strongSelf.isInitiator = response.isInitiator;
273 for (ARDSignalingMessage *message in response.messages) {
274 if (message.type == kARDSignalingMessageTypeOffer ||
275 message.type == kARDSignalingMessageTypeAnswer) {
276 strongSelf.hasReceivedSdp = YES;
277 [strongSelf.messageQueue insertObject:message atIndex:0];
278 } else {
279 [strongSelf.messageQueue addObject:message];
280 }
281 }
282 strongSelf.webSocketURL = response.webSocketURL;
283 strongSelf.webSocketRestURL = response.webSocketRestURL;
284 [strongSelf registerWithColliderIfReady];
285 [strongSelf startSignalingIfReady];
286 }];
287}
288
289- (void)disconnect {
290 if (_state == kARDAppClientStateDisconnected) {
291 return;
292 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000293 if (self.hasJoinedRoomServerRoom) {
294 [_roomServerClient leaveRoomWithRoomId:_roomId
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000295 clientId:_clientId
296 completionHandler:nil];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000297 }
298 if (_channel) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000299 if (_channel.state == kARDSignalingChannelStateRegistered) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000300 // Tell the other client we're hanging up.
301 ARDByeMessage *byeMessage = [[ARDByeMessage alloc] init];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000302 [_channel sendMessage:byeMessage];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000303 }
304 // Disconnect from collider.
305 _channel = nil;
306 }
307 _clientId = nil;
308 _roomId = nil;
309 _isInitiator = NO;
310 _hasReceivedSdp = NO;
311 _messageQueue = [NSMutableArray array];
sakalc522e752017-04-05 12:17:48 -0700312 _localVideoTrack = nil;
ivoc14d5dbe2016-07-04 07:06:55 -0700313#if defined(WEBRTC_IOS)
tkchinfce0e2c2016-08-30 12:58:11 -0700314 [_factory stopAecDump];
ivoc14d5dbe2016-07-04 07:06:55 -0700315 [_peerConnection stopRtcEventLog];
316#endif
magjedcc8b9062017-07-24 07:32:33 -0700317 [_peerConnection close];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000318 _peerConnection = nil;
319 self.state = kARDAppClientStateDisconnected;
tkchind1fb26d2016-02-03 01:51:18 -0800320#if defined(WEBRTC_IOS)
adam.fedorbcc5d872016-11-07 14:53:28 -0800321 if (kARDAppClientEnableTracing) {
322 RTCStopInternalCapture();
323 }
tkchind1fb26d2016-02-03 01:51:18 -0800324#endif
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000325}
326
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000327#pragma mark - ARDSignalingChannelDelegate
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000328
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000329- (void)channel:(id<ARDSignalingChannel>)channel
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000330 didReceiveMessage:(ARDSignalingMessage *)message {
331 switch (message.type) {
332 case kARDSignalingMessageTypeOffer:
333 case kARDSignalingMessageTypeAnswer:
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000334 // Offers and answers must be processed before any other message, so we
335 // place them at the front of the queue.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000336 _hasReceivedSdp = YES;
337 [_messageQueue insertObject:message atIndex:0];
338 break;
339 case kARDSignalingMessageTypeCandidate:
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700340 case kARDSignalingMessageTypeCandidateRemoval:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000341 [_messageQueue addObject:message];
342 break;
343 case kARDSignalingMessageTypeBye:
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000344 // Disconnects can be processed immediately.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000345 [self processSignalingMessage:message];
346 return;
347 }
348 [self drainMessageQueueIfReady];
349}
350
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000351- (void)channel:(id<ARDSignalingChannel>)channel
352 didChangeState:(ARDSignalingChannelState)state {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000353 switch (state) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000354 case kARDSignalingChannelStateOpen:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000355 break;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000356 case kARDSignalingChannelStateRegistered:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000357 break;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000358 case kARDSignalingChannelStateClosed:
359 case kARDSignalingChannelStateError:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000360 // TODO(tkchin): reconnection scenarios. Right now we just disconnect
361 // completely if the websocket connection fails.
362 [self disconnect];
363 break;
364 }
365}
366
367#pragma mark - RTCPeerConnectionDelegate
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000368// Callbacks for this delegate occur on non-main thread and need to be
369// dispatched back to main queue as needed.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000370
371- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700372 didChangeSignalingState:(RTCSignalingState)stateChanged {
373 RTCLog(@"Signaling state changed: %ld", (long)stateChanged);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000374}
375
376- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700377 didAddStream:(RTCMediaStream *)stream {
Steve Antonaf23b752018-03-01 09:22:48 -0800378 RTCLog(@"Stream with %lu video tracks and %lu audio tracks was added.",
379 (unsigned long)stream.videoTracks.count,
380 (unsigned long)stream.audioTracks.count);
381}
382
383- (void)peerConnection:(RTCPeerConnection *)peerConnection
384 didStartReceivingOnTransceiver:(RTCRtpTransceiver *)transceiver {
385 RTCMediaStreamTrack *track = transceiver.receiver.track;
386 RTCLog(@"Now receiving %@ on track %@.", track.kind, track.trackId);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000387}
388
389- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700390 didRemoveStream:(RTCMediaStream *)stream {
tkchinc3f46a92015-07-23 12:50:55 -0700391 RTCLog(@"Stream was removed.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000392}
393
hjon79858f82016-03-13 22:08:26 -0700394- (void)peerConnectionShouldNegotiate:(RTCPeerConnection *)peerConnection {
tkchinc3f46a92015-07-23 12:50:55 -0700395 RTCLog(@"WARNING: Renegotiation needed but unimplemented.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000396}
397
398- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700399 didChangeIceConnectionState:(RTCIceConnectionState)newState {
400 RTCLog(@"ICE state changed: %ld", (long)newState);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000401 dispatch_async(dispatch_get_main_queue(), ^{
402 [_delegate appClient:self didChangeConnectionState:newState];
403 });
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000404}
405
406- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700407 didChangeIceGatheringState:(RTCIceGatheringState)newState {
408 RTCLog(@"ICE gathering state changed: %ld", (long)newState);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000409}
410
411- (void)peerConnection:(RTCPeerConnection *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700412 didGenerateIceCandidate:(RTCIceCandidate *)candidate {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000413 dispatch_async(dispatch_get_main_queue(), ^{
414 ARDICECandidateMessage *message =
415 [[ARDICECandidateMessage alloc] initWithCandidate:candidate];
416 [self sendSignalingMessage:message];
417 });
418}
419
Zeke Chind3325802015-08-14 11:00:02 -0700420- (void)peerConnection:(RTCPeerConnection *)peerConnection
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700421 didRemoveIceCandidates:(NSArray<RTCIceCandidate *> *)candidates {
422 dispatch_async(dispatch_get_main_queue(), ^{
423 ARDICECandidateRemovalMessage *message =
424 [[ARDICECandidateRemovalMessage alloc]
425 initWithRemovedCandidates:candidates];
426 [self sendSignalingMessage:message];
427 });
428}
429
430- (void)peerConnection:(RTCPeerConnection *)peerConnection
Zeke Chind3325802015-08-14 11:00:02 -0700431 didOpenDataChannel:(RTCDataChannel *)dataChannel {
432}
433
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000434#pragma mark - RTCSessionDescriptionDelegate
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000435// Callbacks for this delegate occur on non-main thread and need to be
436// dispatched back to main queue as needed.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000437
438- (void)peerConnection:(RTCPeerConnection *)peerConnection
439 didCreateSessionDescription:(RTCSessionDescription *)sdp
440 error:(NSError *)error {
441 dispatch_async(dispatch_get_main_queue(), ^{
442 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700443 RTCLogError(@"Failed to create session description. Error: %@", error);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000444 [self disconnect];
445 NSDictionary *userInfo = @{
446 NSLocalizedDescriptionKey: @"Failed to create session description.",
447 };
448 NSError *sdpError =
449 [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
450 code:kARDAppClientErrorCreateSDP
451 userInfo:userInfo];
452 [_delegate appClient:self didError:sdpError];
453 return;
454 }
hjon79858f82016-03-13 22:08:26 -0700455 __weak ARDAppClient *weakSelf = self;
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200456 [_peerConnection setLocalDescription:sdp
hjon79858f82016-03-13 22:08:26 -0700457 completionHandler:^(NSError *error) {
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200458 ARDAppClient *strongSelf = weakSelf;
459 [strongSelf peerConnection:strongSelf.peerConnection
460 didSetSessionDescriptionWithError:error];
461 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000462 ARDSessionDescriptionMessage *message =
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200463 [[ARDSessionDescriptionMessage alloc] initWithDescription:sdp];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000464 [self sendSignalingMessage:message];
denicija9af2b602016-11-17 00:43:43 -0800465 [self setMaxBitrateForPeerConnectionVideoSender];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000466 });
467}
468
469- (void)peerConnection:(RTCPeerConnection *)peerConnection
470 didSetSessionDescriptionWithError:(NSError *)error {
471 dispatch_async(dispatch_get_main_queue(), ^{
472 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700473 RTCLogError(@"Failed to set session description. Error: %@", error);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000474 [self disconnect];
475 NSDictionary *userInfo = @{
476 NSLocalizedDescriptionKey: @"Failed to set session description.",
477 };
478 NSError *sdpError =
479 [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
480 code:kARDAppClientErrorSetSDP
481 userInfo:userInfo];
482 [_delegate appClient:self didError:sdpError];
483 return;
484 }
485 // If we're answering and we've just set the remote offer we need to create
486 // an answer and set the local description.
487 if (!_isInitiator && !_peerConnection.localDescription) {
488 RTCMediaConstraints *constraints = [self defaultAnswerConstraints];
hjon79858f82016-03-13 22:08:26 -0700489 __weak ARDAppClient *weakSelf = self;
490 [_peerConnection answerForConstraints:constraints
491 completionHandler:^(RTCSessionDescription *sdp,
492 NSError *error) {
493 ARDAppClient *strongSelf = weakSelf;
494 [strongSelf peerConnection:strongSelf.peerConnection
495 didCreateSessionDescription:sdp
496 error:error];
497 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000498 }
499 });
500}
501
502#pragma mark - Private
503
tkchin204177f2016-06-14 15:03:11 -0700504#if defined(WEBRTC_IOS)
505
506- (NSString *)documentsFilePathForFileName:(NSString *)fileName {
507 NSParameterAssert(fileName.length);
508 NSArray *paths = NSSearchPathForDirectoriesInDomains(
509 NSDocumentDirectory, NSUserDomainMask, YES);
510 NSString *documentsDirPath = paths.firstObject;
511 NSString *filePath =
512 [documentsDirPath stringByAppendingPathComponent:fileName];
513 return filePath;
514}
515
516#endif
517
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000518- (BOOL)hasJoinedRoomServerRoom {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000519 return _clientId.length;
520}
521
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000522// Begins the peer connection connection process if we have both joined a room
523// on the room server and tried to obtain a TURN server. Otherwise does nothing.
524// A peer connection object will be created with a stream that contains local
525// audio and video capture. If this client is the caller, an offer is created as
526// well, otherwise the client will wait for an offer to arrive.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000527- (void)startSignalingIfReady {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000528 if (!_isTurnComplete || !self.hasJoinedRoomServerRoom) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000529 return;
530 }
531 self.state = kARDAppClientStateConnected;
532
533 // Create peer connection.
534 RTCMediaConstraints *constraints = [self defaultPeerConnectionConstraints];
Zeke Chinbc7dd7e2015-05-29 14:59:14 -0700535 RTCConfiguration *config = [[RTCConfiguration alloc] init];
536 config.iceServers = _iceServers;
Steve Antonaf23b752018-03-01 09:22:48 -0800537 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700538 _peerConnection = [_factory peerConnectionWithConfiguration:config
539 constraints:constraints
540 delegate:self];
skvladf3569c82016-04-29 15:30:16 -0700541 // Create AV senders.
Alex Narestb3944f02017-10-13 14:56:18 +0200542 [self createMediaSenders];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000543 if (_isInitiator) {
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000544 // Send offer.
hjon79858f82016-03-13 22:08:26 -0700545 __weak ARDAppClient *weakSelf = self;
546 [_peerConnection offerForConstraints:[self defaultOfferConstraints]
547 completionHandler:^(RTCSessionDescription *sdp,
548 NSError *error) {
549 ARDAppClient *strongSelf = weakSelf;
550 [strongSelf peerConnection:strongSelf.peerConnection
551 didCreateSessionDescription:sdp
552 error:error];
553 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000554 } else {
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000555 // Check if we've received an offer.
556 [self drainMessageQueueIfReady];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000557 }
ivoc14d5dbe2016-07-04 07:06:55 -0700558#if defined(WEBRTC_IOS)
559 // Start event log.
560 if (kARDAppClientEnableRtcEventLog) {
561 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-rtceventlog"];
562 if (![_peerConnection startRtcEventLogWithFilePath:filePath
563 maxSizeInBytes:kARDAppClientRtcEventLogMaxSizeInBytes]) {
564 RTCLogError(@"Failed to start event logging.");
565 }
566 }
peah5085b0c2016-08-25 22:15:14 -0700567
568 // Start aecdump diagnostic recording.
Anders Carlssone1500582017-06-15 16:05:13 +0200569 if ([_settings currentCreateAecDumpSettingFromStore]) {
tkchinfce0e2c2016-08-30 12:58:11 -0700570 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-audio.aecdump"];
571 if (![_factory startAecDumpWithFilePath:filePath
572 maxSizeInBytes:kARDAppClientAecDumpMaxSizeInBytes]) {
573 RTCLogError(@"Failed to start aec dump.");
peah5085b0c2016-08-25 22:15:14 -0700574 }
575 }
ivoc14d5dbe2016-07-04 07:06:55 -0700576#endif
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000577}
578
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000579// Processes the messages that we've received from the room server and the
580// signaling channel. The offer or answer message must be processed before other
581// signaling messages, however they can arrive out of order. Hence, this method
582// only processes pending messages if there is a peer connection object and
583// if we have received either an offer or answer.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000584- (void)drainMessageQueueIfReady {
585 if (!_peerConnection || !_hasReceivedSdp) {
586 return;
587 }
588 for (ARDSignalingMessage *message in _messageQueue) {
589 [self processSignalingMessage:message];
590 }
591 [_messageQueue removeAllObjects];
592}
593
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000594// Processes the given signaling message based on its type.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000595- (void)processSignalingMessage:(ARDSignalingMessage *)message {
596 NSParameterAssert(_peerConnection ||
597 message.type == kARDSignalingMessageTypeBye);
598 switch (message.type) {
599 case kARDSignalingMessageTypeOffer:
600 case kARDSignalingMessageTypeAnswer: {
601 ARDSessionDescriptionMessage *sdpMessage =
602 (ARDSessionDescriptionMessage *)message;
603 RTCSessionDescription *description = sdpMessage.sessionDescription;
hjon79858f82016-03-13 22:08:26 -0700604 __weak ARDAppClient *weakSelf = self;
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200605 [_peerConnection setRemoteDescription:description
hjon79858f82016-03-13 22:08:26 -0700606 completionHandler:^(NSError *error) {
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200607 ARDAppClient *strongSelf = weakSelf;
608 [strongSelf peerConnection:strongSelf.peerConnection
609 didSetSessionDescriptionWithError:error];
610 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000611 break;
612 }
613 case kARDSignalingMessageTypeCandidate: {
614 ARDICECandidateMessage *candidateMessage =
615 (ARDICECandidateMessage *)message;
hjon79858f82016-03-13 22:08:26 -0700616 [_peerConnection addIceCandidate:candidateMessage.candidate];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000617 break;
618 }
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700619 case kARDSignalingMessageTypeCandidateRemoval: {
620 ARDICECandidateRemovalMessage *candidateMessage =
621 (ARDICECandidateRemovalMessage *)message;
622 [_peerConnection removeIceCandidates:candidateMessage.candidates];
623 break;
624 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000625 case kARDSignalingMessageTypeBye:
626 // Other client disconnected.
627 // TODO(tkchin): support waiting in room for next client. For now just
628 // disconnect.
629 [self disconnect];
630 break;
631 }
632}
633
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000634// Sends a signaling message to the other client. The caller will send messages
635// through the room server, whereas the callee will send messages over the
636// signaling channel.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000637- (void)sendSignalingMessage:(ARDSignalingMessage *)message {
638 if (_isInitiator) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000639 __weak ARDAppClient *weakSelf = self;
640 [_roomServerClient sendMessage:message
641 forRoomId:_roomId
642 clientId:_clientId
643 completionHandler:^(ARDMessageResponse *response,
644 NSError *error) {
645 ARDAppClient *strongSelf = weakSelf;
646 if (error) {
647 [strongSelf.delegate appClient:strongSelf didError:error];
648 return;
649 }
650 NSError *messageError =
651 [[strongSelf class] errorForMessageResultType:response.result];
652 if (messageError) {
653 [strongSelf.delegate appClient:strongSelf didError:messageError];
654 return;
655 }
656 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000657 } else {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000658 [_channel sendMessage:message];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000659 }
660}
661
denicija9af2b602016-11-17 00:43:43 -0800662- (void)setMaxBitrateForPeerConnectionVideoSender {
663 for (RTCRtpSender *sender in _peerConnection.senders) {
664 if (sender.track != nil) {
665 if ([sender.track.kind isEqualToString:kARDVideoTrackKind]) {
sakalc4adacf2017-03-28 01:22:48 -0700666 [self setMaxBitrate:[_settings currentMaxBitrateSettingFromStore] forVideoSender:sender];
denicija9af2b602016-11-17 00:43:43 -0800667 }
668 }
denicija8c375de2016-11-08 06:28:17 -0800669 }
670}
671
denicija9af2b602016-11-17 00:43:43 -0800672- (void)setMaxBitrate:(NSNumber *)maxBitrate forVideoSender:(RTCRtpSender *)sender {
673 if (maxBitrate.intValue <= 0) {
674 return;
675 }
676
677 RTCRtpParameters *parametersToModify = sender.parameters;
678 for (RTCRtpEncodingParameters *encoding in parametersToModify.encodings) {
679 encoding.maxBitrateBps = @(maxBitrate.intValue * kKbpsMultiplier);
680 }
681 [sender setParameters:parametersToModify];
682}
683
Steve Antonaf23b752018-03-01 09:22:48 -0800684- (RTCRtpTransceiver *)videoTransceiver {
685 for (RTCRtpTransceiver *transceiver in _peerConnection.transceivers) {
686 if (transceiver.mediaType == RTCRtpMediaTypeVideo) {
687 return transceiver;
688 }
689 }
690 return nil;
691}
692
Alex Narestb3944f02017-10-13 14:56:18 +0200693- (void)createMediaSenders {
tkchinab1293a2016-08-30 12:35:05 -0700694 RTCMediaConstraints *constraints = [self defaultMediaAudioConstraints];
695 RTCAudioSource *source = [_factory audioSourceWithConstraints:constraints];
696 RTCAudioTrack *track = [_factory audioTrackWithSource:source
697 trackId:kARDAudioTrackId];
Seth Hampson513449e2018-03-06 09:35:56 -0800698 [_peerConnection addTrack:track streamIds:@[ kARDMediaStreamId ]];
Alex Narestb3944f02017-10-13 14:56:18 +0200699 _localVideoTrack = [self createLocalVideoTrack];
700 if (_localVideoTrack) {
Seth Hampson513449e2018-03-06 09:35:56 -0800701 [_peerConnection addTrack:_localVideoTrack streamIds:@[ kARDMediaStreamId ]];
Piasy Xue7e06022018-05-30 23:31:07 +0800702 [_delegate appClient:self didReceiveLocalVideoTrack:_localVideoTrack];
Steve Antonaf23b752018-03-01 09:22:48 -0800703 // We can set up rendering for the remote track right away since the transceiver already has an
704 // RTCRtpReceiver with a track. The track will automatically get unmuted and produce frames
705 // once RTP is received.
706 RTCVideoTrack *track = (RTCVideoTrack *)([self videoTransceiver].receiver.track);
707 [_delegate appClient:self didReceiveRemoteVideoTrack:track];
Alex Narestb3944f02017-10-13 14:56:18 +0200708 }
Zeke Chin57cc74e2015-05-05 07:52:31 -0700709}
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000710
Zeke Chin57cc74e2015-05-05 07:52:31 -0700711- (RTCVideoTrack *)createLocalVideoTrack {
Daniela012b56b2017-11-15 13:15:24 +0100712 if ([_settings currentAudioOnlySettingFromStore]) {
713 return nil;
714 }
715
716 RTCVideoSource *source = [_factory videoSource];
717
kthelgason314bc5f2016-08-31 10:23:27 -0700718#if !TARGET_IPHONE_SIMULATOR
Anders Carlsson358f2e02018-06-04 10:24:37 +0200719 if (self.isBroadcast) {
720 ARDExternalSampleCapturer *capturer =
721 [[ARDExternalSampleCapturer alloc] initWithDelegate:source];
722 [_delegate appClient:self didCreateLocalExternalSampleCapturer:capturer];
723 } else {
724 RTCCameraVideoCapturer *capturer = [[RTCCameraVideoCapturer alloc] initWithDelegate:source];
725 [_delegate appClient:self didCreateLocalCapturer:capturer];
726 }
Daniela012b56b2017-11-15 13:15:24 +0100727#else
728#if defined(__IPHONE_11_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0)
729 if (@available(iOS 10, *)) {
730 RTCFileVideoCapturer *fileCapturer = [[RTCFileVideoCapturer alloc] initWithDelegate:source];
731 [_delegate appClient:self didCreateLocalFileCapturer:fileCapturer];
haysc913e6452015-10-02 11:44:03 -0700732 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000733#endif
Daniela012b56b2017-11-15 13:15:24 +0100734#endif
735
736 return [_factory videoTrackWithSource:source trackId:kARDVideoTrackId];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000737}
738
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000739#pragma mark - Collider methods
740
741- (void)registerWithColliderIfReady {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000742 if (!self.hasJoinedRoomServerRoom) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000743 return;
744 }
745 // Open WebSocket connection.
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000746 if (!_channel) {
747 _channel =
748 [[ARDWebSocketChannel alloc] initWithURL:_websocketURL
749 restURL:_websocketRestURL
750 delegate:self];
haysc913e6452015-10-02 11:44:03 -0700751 if (_isLoopback) {
752 _loopbackChannel =
753 [[ARDLoopbackWebSocketChannel alloc] initWithURL:_websocketURL
754 restURL:_websocketRestURL];
755 }
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000756 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000757 [_channel registerForRoomId:_roomId clientId:_clientId];
haysc913e6452015-10-02 11:44:03 -0700758 if (_isLoopback) {
759 [_loopbackChannel registerForRoomId:_roomId clientId:@"LOOPBACK_CLIENT_ID"];
760 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000761}
762
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000763#pragma mark - Defaults
764
tkchinab1293a2016-08-30 12:35:05 -0700765 - (RTCMediaConstraints *)defaultMediaAudioConstraints {
Sam Zackrisson9e981f02018-02-28 16:20:58 +0100766 NSDictionary *mandatoryConstraints = @{};
denicijad17d5362016-11-02 02:56:09 -0700767 RTCMediaConstraints *constraints =
768 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstraints
769 optionalConstraints:nil];
tkchinab1293a2016-08-30 12:35:05 -0700770 return constraints;
771}
772
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000773- (RTCMediaConstraints *)defaultAnswerConstraints {
774 return [self defaultOfferConstraints];
775}
776
777- (RTCMediaConstraints *)defaultOfferConstraints {
hjon79858f82016-03-13 22:08:26 -0700778 NSDictionary *mandatoryConstraints = @{
779 @"OfferToReceiveAudio" : @"true",
780 @"OfferToReceiveVideo" : @"true"
781 };
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000782 RTCMediaConstraints* constraints =
783 [[RTCMediaConstraints alloc]
784 initWithMandatoryConstraints:mandatoryConstraints
785 optionalConstraints:nil];
786 return constraints;
787}
788
789- (RTCMediaConstraints *)defaultPeerConnectionConstraints {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000790 if (_defaultPeerConnectionConstraints) {
791 return _defaultPeerConnectionConstraints;
792 }
haysc913e6452015-10-02 11:44:03 -0700793 NSString *value = _isLoopback ? @"false" : @"true";
hjon79858f82016-03-13 22:08:26 -0700794 NSDictionary *optionalConstraints = @{ @"DtlsSrtpKeyAgreement" : value };
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000795 RTCMediaConstraints* constraints =
796 [[RTCMediaConstraints alloc]
797 initWithMandatoryConstraints:nil
798 optionalConstraints:optionalConstraints];
799 return constraints;
800}
801
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000802#pragma mark - Errors
803
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000804+ (NSError *)errorForJoinResultType:(ARDJoinResultType)resultType {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000805 NSError *error = nil;
806 switch (resultType) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000807 case kARDJoinResultTypeSuccess:
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000808 break;
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000809 case kARDJoinResultTypeUnknown: {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000810 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
811 code:kARDAppClientErrorUnknown
812 userInfo:@{
813 NSLocalizedDescriptionKey: @"Unknown error.",
814 }];
815 break;
816 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000817 case kARDJoinResultTypeFull: {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000818 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
819 code:kARDAppClientErrorRoomFull
820 userInfo:@{
821 NSLocalizedDescriptionKey: @"Room is full.",
822 }];
823 break;
824 }
825 }
826 return error;
827}
828
829+ (NSError *)errorForMessageResultType:(ARDMessageResultType)resultType {
830 NSError *error = nil;
831 switch (resultType) {
832 case kARDMessageResultTypeSuccess:
833 break;
834 case kARDMessageResultTypeUnknown:
835 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
836 code:kARDAppClientErrorUnknown
837 userInfo:@{
838 NSLocalizedDescriptionKey: @"Unknown error.",
839 }];
840 break;
841 case kARDMessageResultTypeInvalidClient:
842 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
843 code:kARDAppClientErrorInvalidClient
844 userInfo:@{
845 NSLocalizedDescriptionKey: @"Invalid client.",
846 }];
847 break;
848 case kARDMessageResultTypeInvalidRoom:
849 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
850 code:kARDAppClientErrorInvalidRoom
851 userInfo:@{
852 NSLocalizedDescriptionKey: @"Invalid room.",
853 }];
854 break;
855 }
856 return error;
857}
858
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000859@end