blob: ccd5bb066227beef6f0bda89e086b88eeb29f9ca [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
Mirko Bonadei19640aa2020-10-19 16:12:43 +020013#import "sdk/objc/api/peerconnection/RTCAudioTrack.h"
14#import "sdk/objc/api/peerconnection/RTCConfiguration.h"
15#import "sdk/objc/api/peerconnection/RTCFileLogger.h"
16#import "sdk/objc/api/peerconnection/RTCIceServer.h"
17#import "sdk/objc/api/peerconnection/RTCMediaConstraints.h"
18#import "sdk/objc/api/peerconnection/RTCMediaStream.h"
19#import "sdk/objc/api/peerconnection/RTCPeerConnectionFactory.h"
20#import "sdk/objc/api/peerconnection/RTCRtpSender.h"
21#import "sdk/objc/api/peerconnection/RTCRtpTransceiver.h"
22#import "sdk/objc/api/peerconnection/RTCTracing.h"
23#import "sdk/objc/api/peerconnection/RTCVideoSource.h"
24#import "sdk/objc/api/peerconnection/RTCVideoTrack.h"
25#import "sdk/objc/base/RTCLogging.h"
26#import "sdk/objc/components/capturer/RTCCameraVideoCapturer.h"
27#import "sdk/objc/components/capturer/RTCFileVideoCapturer.h"
28#import "sdk/objc/components/video_codec/RTCDefaultVideoDecoderFactory.h"
29#import "sdk/objc/components/video_codec/RTCDefaultVideoEncoderFactory.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 {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200108 RTC_OBJC_TYPE(RTCFileLogger) * _fileLogger;
Zeke Chind3325802015-08-14 11:00:02 -0700109 ARDTimerProxy *_statsTimer;
sakalc4adacf2017-03-28 01:22:48 -0700110 ARDSettingsModel *_settings;
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200111 RTC_OBJC_TYPE(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];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200175 _fileLogger = [[RTC_OBJC_TYPE(RTCFileLogger) alloc] init];
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700176 [_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;
Mirko Bonadei924a2752021-04-30 09:25:47 +0000215 [_delegate appClient:self didChangeState:_state];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000216}
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
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200227 RTC_OBJC_TYPE(RTCDefaultVideoDecoderFactory) *decoderFactory =
228 [[RTC_OBJC_TYPE(RTCDefaultVideoDecoderFactory) alloc] init];
229 RTC_OBJC_TYPE(RTCDefaultVideoEncoderFactory) *encoderFactory =
230 [[RTC_OBJC_TYPE(RTCDefaultVideoEncoderFactory) alloc] init];
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200231 encoderFactory.preferredCodec = [settings currentVideoCodecSettingFromStore];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200232 _factory =
233 [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] initWithEncoderFactory:encoderFactory
234 decoderFactory:decoderFactory];
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200235
tkchind1fb26d2016-02-03 01:51:18 -0800236#if defined(WEBRTC_IOS)
237 if (kARDAppClientEnableTracing) {
tkchin204177f2016-06-14 15:03:11 -0700238 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-trace.txt"];
tkchind1fb26d2016-02-03 01:51:18 -0800239 RTCStartInternalCapture(filePath);
240 }
241#endif
242
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000243 // Request TURN.
244 __weak ARDAppClient *weakSelf = self;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000245 [_turnClient requestServersWithCompletionHandler:^(NSArray *turnServers,
246 NSError *error) {
247 if (error) {
Anders Carlsson358f2e02018-06-04 10:24:37 +0200248 RTCLogError(@"Error retrieving TURN servers: %@", error.localizedDescription);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000249 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000250 ARDAppClient *strongSelf = weakSelf;
251 [strongSelf.iceServers addObjectsFromArray:turnServers];
252 strongSelf.isTurnComplete = YES;
253 [strongSelf startSignalingIfReady];
254 }];
jiayl@webrtc.org27f53172014-12-31 00:26:20 +0000255
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000256 // Join room on room server.
257 [_roomServerClient joinRoomWithRoomId:roomId
haysc913e6452015-10-02 11:44:03 -0700258 isLoopback:isLoopback
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000259 completionHandler:^(ARDJoinResponse *response, NSError *error) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000260 ARDAppClient *strongSelf = weakSelf;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000261 if (error) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000262 [strongSelf.delegate appClient:strongSelf didError:error];
263 return;
264 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000265 NSError *joinError =
266 [[strongSelf class] errorForJoinResultType:response.result];
267 if (joinError) {
tkchinc3f46a92015-07-23 12:50:55 -0700268 RTCLogError(@"Failed to join room:%@ on room server.", roomId);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000269 [strongSelf disconnect];
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000270 [strongSelf.delegate appClient:strongSelf didError:joinError];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000271 return;
272 }
tkchinc3f46a92015-07-23 12:50:55 -0700273 RTCLog(@"Joined room:%@ on room server.", roomId);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000274 strongSelf.roomId = response.roomId;
275 strongSelf.clientId = response.clientId;
276 strongSelf.isInitiator = response.isInitiator;
277 for (ARDSignalingMessage *message in response.messages) {
278 if (message.type == kARDSignalingMessageTypeOffer ||
279 message.type == kARDSignalingMessageTypeAnswer) {
280 strongSelf.hasReceivedSdp = YES;
281 [strongSelf.messageQueue insertObject:message atIndex:0];
282 } else {
283 [strongSelf.messageQueue addObject:message];
284 }
285 }
286 strongSelf.webSocketURL = response.webSocketURL;
287 strongSelf.webSocketRestURL = response.webSocketRestURL;
288 [strongSelf registerWithColliderIfReady];
289 [strongSelf startSignalingIfReady];
290 }];
291}
292
293- (void)disconnect {
294 if (_state == kARDAppClientStateDisconnected) {
295 return;
296 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000297 if (self.hasJoinedRoomServerRoom) {
298 [_roomServerClient leaveRoomWithRoomId:_roomId
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000299 clientId:_clientId
300 completionHandler:nil];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000301 }
302 if (_channel) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000303 if (_channel.state == kARDSignalingChannelStateRegistered) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000304 // Tell the other client we're hanging up.
305 ARDByeMessage *byeMessage = [[ARDByeMessage alloc] init];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000306 [_channel sendMessage:byeMessage];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000307 }
308 // Disconnect from collider.
309 _channel = nil;
310 }
311 _clientId = nil;
312 _roomId = nil;
313 _isInitiator = NO;
314 _hasReceivedSdp = NO;
315 _messageQueue = [NSMutableArray array];
sakalc522e752017-04-05 12:17:48 -0700316 _localVideoTrack = nil;
ivoc14d5dbe2016-07-04 07:06:55 -0700317#if defined(WEBRTC_IOS)
tkchinfce0e2c2016-08-30 12:58:11 -0700318 [_factory stopAecDump];
ivoc14d5dbe2016-07-04 07:06:55 -0700319 [_peerConnection stopRtcEventLog];
320#endif
magjedcc8b9062017-07-24 07:32:33 -0700321 [_peerConnection close];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000322 _peerConnection = nil;
323 self.state = kARDAppClientStateDisconnected;
tkchind1fb26d2016-02-03 01:51:18 -0800324#if defined(WEBRTC_IOS)
adam.fedorbcc5d872016-11-07 14:53:28 -0800325 if (kARDAppClientEnableTracing) {
326 RTCStopInternalCapture();
327 }
tkchind1fb26d2016-02-03 01:51:18 -0800328#endif
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000329}
330
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000331#pragma mark - ARDSignalingChannelDelegate
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000332
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000333- (void)channel:(id<ARDSignalingChannel>)channel
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000334 didReceiveMessage:(ARDSignalingMessage *)message {
335 switch (message.type) {
336 case kARDSignalingMessageTypeOffer:
337 case kARDSignalingMessageTypeAnswer:
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000338 // Offers and answers must be processed before any other message, so we
339 // place them at the front of the queue.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000340 _hasReceivedSdp = YES;
341 [_messageQueue insertObject:message atIndex:0];
342 break;
343 case kARDSignalingMessageTypeCandidate:
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700344 case kARDSignalingMessageTypeCandidateRemoval:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000345 [_messageQueue addObject:message];
346 break;
347 case kARDSignalingMessageTypeBye:
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000348 // Disconnects can be processed immediately.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000349 [self processSignalingMessage:message];
350 return;
351 }
352 [self drainMessageQueueIfReady];
353}
354
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000355- (void)channel:(id<ARDSignalingChannel>)channel
356 didChangeState:(ARDSignalingChannelState)state {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000357 switch (state) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000358 case kARDSignalingChannelStateOpen:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000359 break;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000360 case kARDSignalingChannelStateRegistered:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000361 break;
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000362 case kARDSignalingChannelStateClosed:
363 case kARDSignalingChannelStateError:
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000364 // TODO(tkchin): reconnection scenarios. Right now we just disconnect
365 // completely if the websocket connection fails.
366 [self disconnect];
367 break;
368 }
369}
370
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200371#pragma mark - RTC_OBJC_TYPE(RTCPeerConnectionDelegate)
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000372// Callbacks for this delegate occur on non-main thread and need to be
373// dispatched back to main queue as needed.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000374
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200375- (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700376 didChangeSignalingState:(RTCSignalingState)stateChanged {
377 RTCLog(@"Signaling state changed: %ld", (long)stateChanged);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000378}
379
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200380- (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection
381 didAddStream:(RTC_OBJC_TYPE(RTCMediaStream) *)stream {
Steve Antonaf23b752018-03-01 09:22:48 -0800382 RTCLog(@"Stream with %lu video tracks and %lu audio tracks was added.",
383 (unsigned long)stream.videoTracks.count,
384 (unsigned long)stream.audioTracks.count);
385}
386
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200387- (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection
388 didStartReceivingOnTransceiver:(RTC_OBJC_TYPE(RTCRtpTransceiver) *)transceiver {
389 RTC_OBJC_TYPE(RTCMediaStreamTrack) *track = transceiver.receiver.track;
Steve Antonaf23b752018-03-01 09:22:48 -0800390 RTCLog(@"Now receiving %@ on track %@.", track.kind, track.trackId);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000391}
392
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200393- (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection
394 didRemoveStream:(RTC_OBJC_TYPE(RTCMediaStream) *)stream {
tkchinc3f46a92015-07-23 12:50:55 -0700395 RTCLog(@"Stream was removed.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000396}
397
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200398- (void)peerConnectionShouldNegotiate:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection {
tkchinc3f46a92015-07-23 12:50:55 -0700399 RTCLog(@"WARNING: Renegotiation needed but unimplemented.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000400}
401
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200402- (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700403 didChangeIceConnectionState:(RTCIceConnectionState)newState {
404 RTCLog(@"ICE state changed: %ld", (long)newState);
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000405 dispatch_async(dispatch_get_main_queue(), ^{
Jiawei Ou4aeb35b2018-11-09 13:55:45 -0800406 [self.delegate appClient:self didChangeConnectionState:newState];
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000407 });
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000408}
409
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200410- (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection
Jonas Olssoncfddbb72018-11-15 16:52:45 +0100411 didChangeConnectionState:(RTCPeerConnectionState)newState {
412 RTCLog(@"ICE+DTLS state changed: %ld", (long)newState);
413}
414
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200415- (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection
hjon79858f82016-03-13 22:08:26 -0700416 didChangeIceGatheringState:(RTCIceGatheringState)newState {
417 RTCLog(@"ICE gathering state changed: %ld", (long)newState);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000418}
419
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200420- (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection
421 didGenerateIceCandidate:(RTC_OBJC_TYPE(RTCIceCandidate) *)candidate {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000422 dispatch_async(dispatch_get_main_queue(), ^{
423 ARDICECandidateMessage *message =
424 [[ARDICECandidateMessage alloc] initWithCandidate:candidate];
425 [self sendSignalingMessage:message];
426 });
427}
428
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200429- (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection
430 didRemoveIceCandidates:(NSArray<RTC_OBJC_TYPE(RTCIceCandidate) *> *)candidates {
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700431 dispatch_async(dispatch_get_main_queue(), ^{
432 ARDICECandidateRemovalMessage *message =
433 [[ARDICECandidateRemovalMessage alloc]
434 initWithRemovedCandidates:candidates];
435 [self sendSignalingMessage:message];
436 });
437}
438
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200439- (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection
440 didChangeLocalCandidate:(RTC_OBJC_TYPE(RTCIceCandidate) *)local
441 didChangeRemoteCandidate:(RTC_OBJC_TYPE(RTCIceCandidate) *)remote
Alex Drake43faee02019-08-12 16:27:34 -0700442 lastReceivedMs:(int)lastDataReceivedMs
443 didHaveReason:(NSString *)reason {
444 RTCLog(@"ICE candidate pair changed because: %@", reason);
445}
446
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200447- (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection
448 didOpenDataChannel:(RTC_OBJC_TYPE(RTCDataChannel) *)dataChannel {
Zeke Chind3325802015-08-14 11:00:02 -0700449}
450
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000451#pragma mark - RTCSessionDescriptionDelegate
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000452// Callbacks for this delegate occur on non-main thread and need to be
453// dispatched back to main queue as needed.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000454
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200455- (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection
456 didCreateSessionDescription:(RTC_OBJC_TYPE(RTCSessionDescription) *)sdp
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000457 error:(NSError *)error {
458 dispatch_async(dispatch_get_main_queue(), ^{
459 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700460 RTCLogError(@"Failed to create session description. Error: %@", error);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000461 [self disconnect];
462 NSDictionary *userInfo = @{
463 NSLocalizedDescriptionKey: @"Failed to create session description.",
464 };
465 NSError *sdpError =
466 [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
467 code:kARDAppClientErrorCreateSDP
468 userInfo:userInfo];
Jiawei Ou4aeb35b2018-11-09 13:55:45 -0800469 [self.delegate appClient:self didError:sdpError];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000470 return;
471 }
hjon79858f82016-03-13 22:08:26 -0700472 __weak ARDAppClient *weakSelf = self;
Jiawei Ou4aeb35b2018-11-09 13:55:45 -0800473 [self.peerConnection setLocalDescription:sdp
474 completionHandler:^(NSError *error) {
475 ARDAppClient *strongSelf = weakSelf;
476 [strongSelf peerConnection:strongSelf.peerConnection
477 didSetSessionDescriptionWithError:error];
478 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000479 ARDSessionDescriptionMessage *message =
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200480 [[ARDSessionDescriptionMessage alloc] initWithDescription:sdp];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000481 [self sendSignalingMessage:message];
denicija9af2b602016-11-17 00:43:43 -0800482 [self setMaxBitrateForPeerConnectionVideoSender];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000483 });
484}
485
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200486- (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000487 didSetSessionDescriptionWithError:(NSError *)error {
488 dispatch_async(dispatch_get_main_queue(), ^{
489 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -0700490 RTCLogError(@"Failed to set session description. Error: %@", error);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000491 [self disconnect];
492 NSDictionary *userInfo = @{
493 NSLocalizedDescriptionKey: @"Failed to set session description.",
494 };
495 NSError *sdpError =
496 [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
497 code:kARDAppClientErrorSetSDP
498 userInfo:userInfo];
Jiawei Ou4aeb35b2018-11-09 13:55:45 -0800499 [self.delegate appClient:self didError:sdpError];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000500 return;
501 }
502 // If we're answering and we've just set the remote offer we need to create
503 // an answer and set the local description.
Jiawei Ou4aeb35b2018-11-09 13:55:45 -0800504 if (!self.isInitiator && !self.peerConnection.localDescription) {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200505 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints = [self defaultAnswerConstraints];
hjon79858f82016-03-13 22:08:26 -0700506 __weak ARDAppClient *weakSelf = self;
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200507 [self.peerConnection
508 answerForConstraints:constraints
509 completionHandler:^(RTC_OBJC_TYPE(RTCSessionDescription) * sdp, NSError * error) {
510 ARDAppClient *strongSelf = weakSelf;
511 [strongSelf peerConnection:strongSelf.peerConnection
512 didCreateSessionDescription:sdp
513 error:error];
514 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000515 }
516 });
517}
518
519#pragma mark - Private
520
tkchin204177f2016-06-14 15:03:11 -0700521#if defined(WEBRTC_IOS)
522
523- (NSString *)documentsFilePathForFileName:(NSString *)fileName {
524 NSParameterAssert(fileName.length);
525 NSArray *paths = NSSearchPathForDirectoriesInDomains(
526 NSDocumentDirectory, NSUserDomainMask, YES);
527 NSString *documentsDirPath = paths.firstObject;
528 NSString *filePath =
529 [documentsDirPath stringByAppendingPathComponent:fileName];
530 return filePath;
531}
532
533#endif
534
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000535- (BOOL)hasJoinedRoomServerRoom {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000536 return _clientId.length;
537}
538
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000539// Begins the peer connection connection process if we have both joined a room
540// on the room server and tried to obtain a TURN server. Otherwise does nothing.
541// A peer connection object will be created with a stream that contains local
542// audio and video capture. If this client is the caller, an offer is created as
543// well, otherwise the client will wait for an offer to arrive.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000544- (void)startSignalingIfReady {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000545 if (!_isTurnComplete || !self.hasJoinedRoomServerRoom) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000546 return;
547 }
548 self.state = kARDAppClientStateConnected;
549
550 // Create peer connection.
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200551 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints = [self defaultPeerConnectionConstraints];
552 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
553 RTC_OBJC_TYPE(RTCCertificate) *pcert = [RTC_OBJC_TYPE(RTCCertificate)
554 generateCertificateWithParams:@{@"expires" : @100000, @"name" : @"RSASSA-PKCS1-v1_5"}];
Zeke Chinbc7dd7e2015-05-29 14:59:14 -0700555 config.iceServers = _iceServers;
Steve Antonaf23b752018-03-01 09:22:48 -0800556 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
Michael Iedemaccee56b2018-07-05 15:28:24 +0200557 config.certificate = pcert;
558
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700559 _peerConnection = [_factory peerConnectionWithConfiguration:config
560 constraints:constraints
561 delegate:self];
skvladf3569c82016-04-29 15:30:16 -0700562 // Create AV senders.
Alex Narestb3944f02017-10-13 14:56:18 +0200563 [self createMediaSenders];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000564 if (_isInitiator) {
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000565 // Send offer.
hjon79858f82016-03-13 22:08:26 -0700566 __weak ARDAppClient *weakSelf = self;
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200567 [_peerConnection
568 offerForConstraints:[self defaultOfferConstraints]
569 completionHandler:^(RTC_OBJC_TYPE(RTCSessionDescription) * sdp, NSError * error) {
570 ARDAppClient *strongSelf = weakSelf;
571 [strongSelf peerConnection:strongSelf.peerConnection
572 didCreateSessionDescription:sdp
573 error:error];
574 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000575 } else {
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000576 // Check if we've received an offer.
577 [self drainMessageQueueIfReady];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000578 }
ivoc14d5dbe2016-07-04 07:06:55 -0700579#if defined(WEBRTC_IOS)
580 // Start event log.
581 if (kARDAppClientEnableRtcEventLog) {
582 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-rtceventlog"];
583 if (![_peerConnection startRtcEventLogWithFilePath:filePath
584 maxSizeInBytes:kARDAppClientRtcEventLogMaxSizeInBytes]) {
585 RTCLogError(@"Failed to start event logging.");
586 }
587 }
peah5085b0c2016-08-25 22:15:14 -0700588
589 // Start aecdump diagnostic recording.
Anders Carlssone1500582017-06-15 16:05:13 +0200590 if ([_settings currentCreateAecDumpSettingFromStore]) {
tkchinfce0e2c2016-08-30 12:58:11 -0700591 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-audio.aecdump"];
592 if (![_factory startAecDumpWithFilePath:filePath
593 maxSizeInBytes:kARDAppClientAecDumpMaxSizeInBytes]) {
594 RTCLogError(@"Failed to start aec dump.");
peah5085b0c2016-08-25 22:15:14 -0700595 }
596 }
ivoc14d5dbe2016-07-04 07:06:55 -0700597#endif
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000598}
599
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000600// Processes the messages that we've received from the room server and the
601// signaling channel. The offer or answer message must be processed before other
602// signaling messages, however they can arrive out of order. Hence, this method
603// only processes pending messages if there is a peer connection object and
604// if we have received either an offer or answer.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000605- (void)drainMessageQueueIfReady {
606 if (!_peerConnection || !_hasReceivedSdp) {
607 return;
608 }
609 for (ARDSignalingMessage *message in _messageQueue) {
610 [self processSignalingMessage:message];
611 }
612 [_messageQueue removeAllObjects];
613}
614
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000615// Processes the given signaling message based on its type.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000616- (void)processSignalingMessage:(ARDSignalingMessage *)message {
617 NSParameterAssert(_peerConnection ||
618 message.type == kARDSignalingMessageTypeBye);
619 switch (message.type) {
620 case kARDSignalingMessageTypeOffer:
621 case kARDSignalingMessageTypeAnswer: {
622 ARDSessionDescriptionMessage *sdpMessage =
623 (ARDSessionDescriptionMessage *)message;
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200624 RTC_OBJC_TYPE(RTCSessionDescription) *description = sdpMessage.sessionDescription;
hjon79858f82016-03-13 22:08:26 -0700625 __weak ARDAppClient *weakSelf = self;
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200626 [_peerConnection setRemoteDescription:description
hjon79858f82016-03-13 22:08:26 -0700627 completionHandler:^(NSError *error) {
Anders Carlsson6bf43d22017-10-16 13:51:43 +0200628 ARDAppClient *strongSelf = weakSelf;
629 [strongSelf peerConnection:strongSelf.peerConnection
630 didSetSessionDescriptionWithError:error];
631 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000632 break;
633 }
634 case kARDSignalingMessageTypeCandidate: {
635 ARDICECandidateMessage *candidateMessage =
636 (ARDICECandidateMessage *)message;
Yura Yaroshevich2d9f53c2021-03-10 13:03:00 +0300637 __weak ARDAppClient *weakSelf = self;
638 [_peerConnection addIceCandidate:candidateMessage.candidate
639 completionHandler:^(NSError *error) {
640 ARDAppClient *strongSelf = weakSelf;
641 if (error) {
642 [strongSelf.delegate appClient:strongSelf didError:error];
643 }
644 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000645 break;
646 }
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700647 case kARDSignalingMessageTypeCandidateRemoval: {
648 ARDICECandidateRemovalMessage *candidateMessage =
649 (ARDICECandidateRemovalMessage *)message;
650 [_peerConnection removeIceCandidates:candidateMessage.candidates];
651 break;
652 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000653 case kARDSignalingMessageTypeBye:
654 // Other client disconnected.
655 // TODO(tkchin): support waiting in room for next client. For now just
656 // disconnect.
657 [self disconnect];
658 break;
659 }
660}
661
tkchin@webrtc.org8cc47e92015-03-18 23:38:04 +0000662// Sends a signaling message to the other client. The caller will send messages
663// through the room server, whereas the callee will send messages over the
664// signaling channel.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000665- (void)sendSignalingMessage:(ARDSignalingMessage *)message {
666 if (_isInitiator) {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000667 __weak ARDAppClient *weakSelf = self;
668 [_roomServerClient sendMessage:message
669 forRoomId:_roomId
670 clientId:_clientId
671 completionHandler:^(ARDMessageResponse *response,
672 NSError *error) {
673 ARDAppClient *strongSelf = weakSelf;
674 if (error) {
675 [strongSelf.delegate appClient:strongSelf didError:error];
676 return;
677 }
678 NSError *messageError =
679 [[strongSelf class] errorForMessageResultType:response.result];
680 if (messageError) {
681 [strongSelf.delegate appClient:strongSelf didError:messageError];
682 return;
683 }
684 }];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000685 } else {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000686 [_channel sendMessage:message];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000687 }
688}
689
denicija9af2b602016-11-17 00:43:43 -0800690- (void)setMaxBitrateForPeerConnectionVideoSender {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200691 for (RTC_OBJC_TYPE(RTCRtpSender) * sender in _peerConnection.senders) {
denicija9af2b602016-11-17 00:43:43 -0800692 if (sender.track != nil) {
693 if ([sender.track.kind isEqualToString:kARDVideoTrackKind]) {
sakalc4adacf2017-03-28 01:22:48 -0700694 [self setMaxBitrate:[_settings currentMaxBitrateSettingFromStore] forVideoSender:sender];
denicija9af2b602016-11-17 00:43:43 -0800695 }
696 }
denicija8c375de2016-11-08 06:28:17 -0800697 }
698}
699
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200700- (void)setMaxBitrate:(NSNumber *)maxBitrate forVideoSender:(RTC_OBJC_TYPE(RTCRtpSender) *)sender {
denicija9af2b602016-11-17 00:43:43 -0800701 if (maxBitrate.intValue <= 0) {
702 return;
703 }
704
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200705 RTC_OBJC_TYPE(RTCRtpParameters) *parametersToModify = sender.parameters;
706 for (RTC_OBJC_TYPE(RTCRtpEncodingParameters) * encoding in parametersToModify.encodings) {
denicija9af2b602016-11-17 00:43:43 -0800707 encoding.maxBitrateBps = @(maxBitrate.intValue * kKbpsMultiplier);
708 }
709 [sender setParameters:parametersToModify];
710}
711
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200712- (RTC_OBJC_TYPE(RTCRtpTransceiver) *)videoTransceiver {
713 for (RTC_OBJC_TYPE(RTCRtpTransceiver) * transceiver in _peerConnection.transceivers) {
Steve Antonaf23b752018-03-01 09:22:48 -0800714 if (transceiver.mediaType == RTCRtpMediaTypeVideo) {
715 return transceiver;
716 }
717 }
718 return nil;
719}
720
Alex Narestb3944f02017-10-13 14:56:18 +0200721- (void)createMediaSenders {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200722 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints = [self defaultMediaAudioConstraints];
723 RTC_OBJC_TYPE(RTCAudioSource) *source = [_factory audioSourceWithConstraints:constraints];
724 RTC_OBJC_TYPE(RTCAudioTrack) *track = [_factory audioTrackWithSource:source
725 trackId:kARDAudioTrackId];
Seth Hampson513449e2018-03-06 09:35:56 -0800726 [_peerConnection addTrack:track streamIds:@[ kARDMediaStreamId ]];
Alex Narestb3944f02017-10-13 14:56:18 +0200727 _localVideoTrack = [self createLocalVideoTrack];
728 if (_localVideoTrack) {
Seth Hampson513449e2018-03-06 09:35:56 -0800729 [_peerConnection addTrack:_localVideoTrack streamIds:@[ kARDMediaStreamId ]];
Piasy Xue7e06022018-05-30 23:31:07 +0800730 [_delegate appClient:self didReceiveLocalVideoTrack:_localVideoTrack];
Steve Antonaf23b752018-03-01 09:22:48 -0800731 // We can set up rendering for the remote track right away since the transceiver already has an
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200732 // RTC_OBJC_TYPE(RTCRtpReceiver) with a track. The track will automatically get unmuted and
733 // produce frames once RTP is received.
734 RTC_OBJC_TYPE(RTCVideoTrack) *track =
735 (RTC_OBJC_TYPE(RTCVideoTrack) *)([self videoTransceiver].receiver.track);
Steve Antonaf23b752018-03-01 09:22:48 -0800736 [_delegate appClient:self didReceiveRemoteVideoTrack:track];
Alex Narestb3944f02017-10-13 14:56:18 +0200737 }
Zeke Chin57cc74e2015-05-05 07:52:31 -0700738}
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000739
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200740- (RTC_OBJC_TYPE(RTCVideoTrack) *)createLocalVideoTrack {
Daniela012b56b2017-11-15 13:15:24 +0100741 if ([_settings currentAudioOnlySettingFromStore]) {
742 return nil;
743 }
744
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200745 RTC_OBJC_TYPE(RTCVideoSource) *source = [_factory videoSource];
Daniela012b56b2017-11-15 13:15:24 +0100746
kthelgason314bc5f2016-08-31 10:23:27 -0700747#if !TARGET_IPHONE_SIMULATOR
Anders Carlsson358f2e02018-06-04 10:24:37 +0200748 if (self.isBroadcast) {
749 ARDExternalSampleCapturer *capturer =
750 [[ARDExternalSampleCapturer alloc] initWithDelegate:source];
751 [_delegate appClient:self didCreateLocalExternalSampleCapturer:capturer];
752 } else {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200753 RTC_OBJC_TYPE(RTCCameraVideoCapturer) *capturer =
754 [[RTC_OBJC_TYPE(RTCCameraVideoCapturer) alloc] initWithDelegate:source];
Anders Carlsson358f2e02018-06-04 10:24:37 +0200755 [_delegate appClient:self didCreateLocalCapturer:capturer];
756 }
Daniela012b56b2017-11-15 13:15:24 +0100757#else
758#if defined(__IPHONE_11_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0)
759 if (@available(iOS 10, *)) {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200760 RTC_OBJC_TYPE(RTCFileVideoCapturer) *fileCapturer =
761 [[RTC_OBJC_TYPE(RTCFileVideoCapturer) alloc] initWithDelegate:source];
Daniela012b56b2017-11-15 13:15:24 +0100762 [_delegate appClient:self didCreateLocalFileCapturer:fileCapturer];
haysc913e6452015-10-02 11:44:03 -0700763 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000764#endif
Daniela012b56b2017-11-15 13:15:24 +0100765#endif
766
767 return [_factory videoTrackWithSource:source trackId:kARDVideoTrackId];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000768}
769
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000770#pragma mark - Collider methods
771
772- (void)registerWithColliderIfReady {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000773 if (!self.hasJoinedRoomServerRoom) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000774 return;
775 }
776 // Open WebSocket connection.
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000777 if (!_channel) {
778 _channel =
779 [[ARDWebSocketChannel alloc] initWithURL:_websocketURL
780 restURL:_websocketRestURL
781 delegate:self];
haysc913e6452015-10-02 11:44:03 -0700782 if (_isLoopback) {
783 _loopbackChannel =
784 [[ARDLoopbackWebSocketChannel alloc] initWithURL:_websocketURL
785 restURL:_websocketRestURL];
786 }
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000787 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000788 [_channel registerForRoomId:_roomId clientId:_clientId];
haysc913e6452015-10-02 11:44:03 -0700789 if (_isLoopback) {
790 [_loopbackChannel registerForRoomId:_roomId clientId:@"LOOPBACK_CLIENT_ID"];
791 }
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000792}
793
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000794#pragma mark - Defaults
795
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200796- (RTC_OBJC_TYPE(RTCMediaConstraints) *)defaultMediaAudioConstraints {
797 NSDictionary *mandatoryConstraints = @{};
798 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
799 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:mandatoryConstraints
800 optionalConstraints:nil];
801 return constraints;
tkchinab1293a2016-08-30 12:35:05 -0700802}
803
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200804- (RTC_OBJC_TYPE(RTCMediaConstraints) *)defaultAnswerConstraints {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000805 return [self defaultOfferConstraints];
806}
807
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200808- (RTC_OBJC_TYPE(RTCMediaConstraints) *)defaultOfferConstraints {
hjon79858f82016-03-13 22:08:26 -0700809 NSDictionary *mandatoryConstraints = @{
810 @"OfferToReceiveAudio" : @"true",
811 @"OfferToReceiveVideo" : @"true"
812 };
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200813 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
814 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:mandatoryConstraints
815 optionalConstraints:nil];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000816 return constraints;
817}
818
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200819- (RTC_OBJC_TYPE(RTCMediaConstraints) *)defaultPeerConnectionConstraints {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000820 if (_defaultPeerConnectionConstraints) {
821 return _defaultPeerConnectionConstraints;
822 }
haysc913e6452015-10-02 11:44:03 -0700823 NSString *value = _isLoopback ? @"false" : @"true";
hjon79858f82016-03-13 22:08:26 -0700824 NSDictionary *optionalConstraints = @{ @"DtlsSrtpKeyAgreement" : value };
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200825 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
826 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:nil
827 optionalConstraints:optionalConstraints];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000828 return constraints;
829}
830
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000831#pragma mark - Errors
832
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000833+ (NSError *)errorForJoinResultType:(ARDJoinResultType)resultType {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000834 NSError *error = nil;
835 switch (resultType) {
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000836 case kARDJoinResultTypeSuccess:
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000837 break;
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000838 case kARDJoinResultTypeUnknown: {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000839 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
840 code:kARDAppClientErrorUnknown
841 userInfo:@{
842 NSLocalizedDescriptionKey: @"Unknown error.",
843 }];
844 break;
845 }
tkchin@webrtc.org36401ab2015-01-27 21:34:39 +0000846 case kARDJoinResultTypeFull: {
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000847 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
848 code:kARDAppClientErrorRoomFull
849 userInfo:@{
850 NSLocalizedDescriptionKey: @"Room is full.",
851 }];
852 break;
853 }
854 }
855 return error;
856}
857
858+ (NSError *)errorForMessageResultType:(ARDMessageResultType)resultType {
859 NSError *error = nil;
860 switch (resultType) {
861 case kARDMessageResultTypeSuccess:
862 break;
863 case kARDMessageResultTypeUnknown:
864 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
865 code:kARDAppClientErrorUnknown
866 userInfo:@{
867 NSLocalizedDescriptionKey: @"Unknown error.",
868 }];
869 break;
870 case kARDMessageResultTypeInvalidClient:
871 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
872 code:kARDAppClientErrorInvalidClient
873 userInfo:@{
874 NSLocalizedDescriptionKey: @"Invalid client.",
875 }];
876 break;
877 case kARDMessageResultTypeInvalidRoom:
878 error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain
879 code:kARDAppClientErrorInvalidRoom
880 userInfo:@{
881 NSLocalizedDescriptionKey: @"Invalid room.",
882 }];
883 break;
884 }
885 return error;
886}
887
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000888@end