tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 1 | /* |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 2 | * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 3 | * |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 4 | * 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.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 11 | #import "ARDAppClient+Internal.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 12 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 13 | #if defined(WEBRTC_IOS) |
tkchin | d1fb26d | 2016-02-03 01:51:18 -0800 | [diff] [blame] | 14 | #import "webrtc/base/objc/RTCTracing.h" |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 15 | #import "webrtc/api/objc/RTCAVFoundationVideoSource.h" |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 16 | #endif |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 17 | #import "webrtc/api/objc/RTCAudioTrack.h" |
| 18 | #import "webrtc/api/objc/RTCConfiguration.h" |
| 19 | #import "webrtc/api/objc/RTCIceServer.h" |
| 20 | #import "webrtc/api/objc/RTCMediaConstraints.h" |
| 21 | #import "webrtc/api/objc/RTCMediaStream.h" |
| 22 | #import "webrtc/api/objc/RTCPeerConnectionFactory.h" |
| 23 | #import "webrtc/base/objc/RTCFileLogger.h" |
| 24 | #import "webrtc/base/objc/RTCLogging.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 25 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 26 | #import "ARDAppEngineClient.h" |
| 27 | #import "ARDCEODTURNClient.h" |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 28 | #import "ARDJoinResponse.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 29 | #import "ARDMessageResponse.h" |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 30 | #import "ARDSDPUtils.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 31 | #import "ARDSignalingMessage.h" |
| 32 | #import "ARDUtilities.h" |
| 33 | #import "ARDWebSocketChannel.h" |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 34 | #import "RTCIceCandidate+JSON.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 35 | #import "RTCSessionDescription+JSON.h" |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 36 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 37 | static NSString * const kARDDefaultSTUNServerUrl = |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 38 | @"stun:stun.l.google.com:19302"; |
| 39 | // TODO(tkchin): figure out a better username for CEOD statistics. |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 40 | static NSString * const kARDTurnRequestUrl = |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 41 | @"https://computeengineondemand.appspot.com" |
| 42 | @"/turn?username=iapprtc&key=4080218913"; |
| 43 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 44 | static NSString * const kARDAppClientErrorDomain = @"ARDAppClient"; |
| 45 | static NSInteger const kARDAppClientErrorUnknown = -1; |
| 46 | static NSInteger const kARDAppClientErrorRoomFull = -2; |
| 47 | static NSInteger const kARDAppClientErrorCreateSDP = -3; |
| 48 | static NSInteger const kARDAppClientErrorSetSDP = -4; |
| 49 | static NSInteger const kARDAppClientErrorInvalidClient = -5; |
| 50 | static NSInteger const kARDAppClientErrorInvalidRoom = -6; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 51 | |
tkchin | d1fb26d | 2016-02-03 01:51:18 -0800 | [diff] [blame] | 52 | // TODO(tkchin): Remove guard once rtc_base_objc compiles on Mac. |
| 53 | #if defined(WEBRTC_IOS) |
| 54 | // TODO(tkchin): Add this as a UI option. |
| 55 | static BOOL const kARDAppClientEnableTracing = NO; |
| 56 | #endif |
| 57 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 58 | // We need a proxy to NSTimer because it causes a strong retain cycle. When |
| 59 | // using the proxy, |invalidate| must be called before it properly deallocs. |
| 60 | @interface ARDTimerProxy : NSObject |
| 61 | |
| 62 | - (instancetype)initWithInterval:(NSTimeInterval)interval |
| 63 | repeats:(BOOL)repeats |
| 64 | timerHandler:(void (^)(void))timerHandler; |
| 65 | - (void)invalidate; |
| 66 | |
| 67 | @end |
| 68 | |
| 69 | @implementation ARDTimerProxy { |
| 70 | NSTimer *_timer; |
| 71 | void (^_timerHandler)(void); |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 72 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 73 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 74 | - (instancetype)initWithInterval:(NSTimeInterval)interval |
| 75 | repeats:(BOOL)repeats |
| 76 | timerHandler:(void (^)(void))timerHandler { |
| 77 | NSParameterAssert(timerHandler); |
| 78 | if (self = [super init]) { |
| 79 | _timerHandler = timerHandler; |
| 80 | _timer = [NSTimer scheduledTimerWithTimeInterval:interval |
| 81 | target:self |
| 82 | selector:@selector(timerDidFire:) |
| 83 | userInfo:nil |
| 84 | repeats:repeats]; |
| 85 | } |
| 86 | return self; |
| 87 | } |
| 88 | |
| 89 | - (void)invalidate { |
| 90 | [_timer invalidate]; |
| 91 | } |
| 92 | |
| 93 | - (void)timerDidFire:(NSTimer *)timer { |
| 94 | _timerHandler(); |
| 95 | } |
| 96 | |
| 97 | @end |
| 98 | |
| 99 | @implementation ARDAppClient { |
| 100 | RTCFileLogger *_fileLogger; |
| 101 | ARDTimerProxy *_statsTimer; |
| 102 | } |
| 103 | |
| 104 | @synthesize shouldGetStats = _shouldGetStats; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 105 | @synthesize state = _state; |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 106 | @synthesize delegate = _delegate; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 107 | @synthesize roomServerClient = _roomServerClient; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 108 | @synthesize channel = _channel; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 109 | @synthesize loopbackChannel = _loopbackChannel; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 110 | @synthesize turnClient = _turnClient; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 111 | @synthesize peerConnection = _peerConnection; |
| 112 | @synthesize factory = _factory; |
| 113 | @synthesize messageQueue = _messageQueue; |
| 114 | @synthesize isTurnComplete = _isTurnComplete; |
| 115 | @synthesize hasReceivedSdp = _hasReceivedSdp; |
| 116 | @synthesize roomId = _roomId; |
| 117 | @synthesize clientId = _clientId; |
| 118 | @synthesize isInitiator = _isInitiator; |
| 119 | @synthesize iceServers = _iceServers; |
| 120 | @synthesize webSocketURL = _websocketURL; |
| 121 | @synthesize webSocketRestURL = _websocketRestURL; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 122 | @synthesize defaultPeerConnectionConstraints = |
| 123 | _defaultPeerConnectionConstraints; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 124 | @synthesize isLoopback = _isLoopback; |
| 125 | @synthesize isAudioOnly = _isAudioOnly; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 126 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 127 | - (instancetype)init { |
| 128 | if (self = [super init]) { |
| 129 | _roomServerClient = [[ARDAppEngineClient alloc] init]; |
| 130 | NSURL *turnRequestURL = [NSURL URLWithString:kARDTurnRequestUrl]; |
| 131 | _turnClient = [[ARDCEODTURNClient alloc] initWithURL:turnRequestURL]; |
| 132 | [self configure]; |
| 133 | } |
| 134 | return self; |
| 135 | } |
| 136 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 137 | - (instancetype)initWithDelegate:(id<ARDAppClientDelegate>)delegate { |
| 138 | if (self = [super init]) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 139 | _roomServerClient = [[ARDAppEngineClient alloc] init]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 140 | _delegate = delegate; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 141 | NSURL *turnRequestURL = [NSURL URLWithString:kARDTurnRequestUrl]; |
| 142 | _turnClient = [[ARDCEODTURNClient alloc] initWithURL:turnRequestURL]; |
| 143 | [self configure]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 144 | } |
| 145 | return self; |
| 146 | } |
| 147 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 148 | // TODO(tkchin): Provide signaling channel factory interface so we can recreate |
| 149 | // channel if we need to on network failure. Also, make this the default public |
| 150 | // constructor. |
| 151 | - (instancetype)initWithRoomServerClient:(id<ARDRoomServerClient>)rsClient |
| 152 | signalingChannel:(id<ARDSignalingChannel>)channel |
| 153 | turnClient:(id<ARDTURNClient>)turnClient |
| 154 | delegate:(id<ARDAppClientDelegate>)delegate { |
| 155 | NSParameterAssert(rsClient); |
| 156 | NSParameterAssert(channel); |
| 157 | NSParameterAssert(turnClient); |
| 158 | if (self = [super init]) { |
| 159 | _roomServerClient = rsClient; |
| 160 | _channel = channel; |
| 161 | _turnClient = turnClient; |
| 162 | _delegate = delegate; |
| 163 | [self configure]; |
| 164 | } |
| 165 | return self; |
| 166 | } |
| 167 | |
| 168 | - (void)configure { |
| 169 | _factory = [[RTCPeerConnectionFactory alloc] init]; |
| 170 | _messageQueue = [NSMutableArray array]; |
| 171 | _iceServers = [NSMutableArray arrayWithObject:[self defaultSTUNServer]]; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 172 | _fileLogger = [[RTCFileLogger alloc] init]; |
| 173 | [_fileLogger start]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 174 | } |
| 175 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 176 | - (void)dealloc { |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 177 | self.shouldGetStats = NO; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 178 | [self disconnect]; |
| 179 | } |
| 180 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 181 | - (void)setShouldGetStats:(BOOL)shouldGetStats { |
| 182 | if (_shouldGetStats == shouldGetStats) { |
| 183 | return; |
| 184 | } |
| 185 | if (shouldGetStats) { |
| 186 | __weak ARDAppClient *weakSelf = self; |
| 187 | _statsTimer = [[ARDTimerProxy alloc] initWithInterval:1 |
| 188 | repeats:YES |
| 189 | timerHandler:^{ |
| 190 | ARDAppClient *strongSelf = weakSelf; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 191 | [strongSelf.peerConnection statsForTrack:nil |
| 192 | statsOutputLevel:RTCStatsOutputLevelDebug |
| 193 | completionHandler:^(NSArray *stats) { |
| 194 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 195 | ARDAppClient *strongSelf = weakSelf; |
| 196 | [strongSelf.delegate appClient:strongSelf didGetStats:stats]; |
| 197 | }); |
| 198 | }]; |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 199 | }]; |
| 200 | } else { |
| 201 | [_statsTimer invalidate]; |
| 202 | _statsTimer = nil; |
| 203 | } |
| 204 | _shouldGetStats = shouldGetStats; |
| 205 | } |
| 206 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 207 | - (void)setState:(ARDAppClientState)state { |
| 208 | if (_state == state) { |
| 209 | return; |
| 210 | } |
| 211 | _state = state; |
| 212 | [_delegate appClient:self didChangeState:_state]; |
| 213 | } |
| 214 | |
| 215 | - (void)connectToRoomWithId:(NSString *)roomId |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 216 | isLoopback:(BOOL)isLoopback |
| 217 | isAudioOnly:(BOOL)isAudioOnly { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 218 | NSParameterAssert(roomId.length); |
| 219 | NSParameterAssert(_state == kARDAppClientStateDisconnected); |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 220 | _isLoopback = isLoopback; |
| 221 | _isAudioOnly = isAudioOnly; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 222 | self.state = kARDAppClientStateConnecting; |
| 223 | |
tkchin | d1fb26d | 2016-02-03 01:51:18 -0800 | [diff] [blame] | 224 | #if defined(WEBRTC_IOS) |
| 225 | if (kARDAppClientEnableTracing) { |
| 226 | NSArray *paths = NSSearchPathForDirectoriesInDomains( |
| 227 | NSDocumentDirectory, NSUserDomainMask, YES); |
| 228 | NSString *documentsDirPath = paths.firstObject; |
| 229 | NSString *filePath = |
| 230 | [documentsDirPath stringByAppendingPathComponent:@"webrtc-trace.txt"]; |
| 231 | RTCStartInternalCapture(filePath); |
| 232 | } |
| 233 | #endif |
| 234 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 235 | // Request TURN. |
| 236 | __weak ARDAppClient *weakSelf = self; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 237 | [_turnClient requestServersWithCompletionHandler:^(NSArray *turnServers, |
| 238 | NSError *error) { |
| 239 | if (error) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 240 | RTCLogError("Error retrieving TURN servers: %@", |
| 241 | error.localizedDescription); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 242 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 243 | ARDAppClient *strongSelf = weakSelf; |
| 244 | [strongSelf.iceServers addObjectsFromArray:turnServers]; |
| 245 | strongSelf.isTurnComplete = YES; |
| 246 | [strongSelf startSignalingIfReady]; |
| 247 | }]; |
jiayl@webrtc.org | 27f5317 | 2014-12-31 00:26:20 +0000 | [diff] [blame] | 248 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 249 | // Join room on room server. |
| 250 | [_roomServerClient joinRoomWithRoomId:roomId |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 251 | isLoopback:isLoopback |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 252 | completionHandler:^(ARDJoinResponse *response, NSError *error) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 253 | ARDAppClient *strongSelf = weakSelf; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 254 | if (error) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 255 | [strongSelf.delegate appClient:strongSelf didError:error]; |
| 256 | return; |
| 257 | } |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 258 | NSError *joinError = |
| 259 | [[strongSelf class] errorForJoinResultType:response.result]; |
| 260 | if (joinError) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 261 | RTCLogError(@"Failed to join room:%@ on room server.", roomId); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 262 | [strongSelf disconnect]; |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 263 | [strongSelf.delegate appClient:strongSelf didError:joinError]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 264 | return; |
| 265 | } |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 266 | RTCLog(@"Joined room:%@ on room server.", roomId); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 267 | strongSelf.roomId = response.roomId; |
| 268 | strongSelf.clientId = response.clientId; |
| 269 | strongSelf.isInitiator = response.isInitiator; |
| 270 | for (ARDSignalingMessage *message in response.messages) { |
| 271 | if (message.type == kARDSignalingMessageTypeOffer || |
| 272 | message.type == kARDSignalingMessageTypeAnswer) { |
| 273 | strongSelf.hasReceivedSdp = YES; |
| 274 | [strongSelf.messageQueue insertObject:message atIndex:0]; |
| 275 | } else { |
| 276 | [strongSelf.messageQueue addObject:message]; |
| 277 | } |
| 278 | } |
| 279 | strongSelf.webSocketURL = response.webSocketURL; |
| 280 | strongSelf.webSocketRestURL = response.webSocketRestURL; |
| 281 | [strongSelf registerWithColliderIfReady]; |
| 282 | [strongSelf startSignalingIfReady]; |
| 283 | }]; |
| 284 | } |
| 285 | |
| 286 | - (void)disconnect { |
| 287 | if (_state == kARDAppClientStateDisconnected) { |
| 288 | return; |
| 289 | } |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 290 | if (self.hasJoinedRoomServerRoom) { |
| 291 | [_roomServerClient leaveRoomWithRoomId:_roomId |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 292 | clientId:_clientId |
| 293 | completionHandler:nil]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 294 | } |
| 295 | if (_channel) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 296 | if (_channel.state == kARDSignalingChannelStateRegistered) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 297 | // Tell the other client we're hanging up. |
| 298 | ARDByeMessage *byeMessage = [[ARDByeMessage alloc] init]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 299 | [_channel sendMessage:byeMessage]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 300 | } |
| 301 | // Disconnect from collider. |
| 302 | _channel = nil; |
| 303 | } |
| 304 | _clientId = nil; |
| 305 | _roomId = nil; |
| 306 | _isInitiator = NO; |
| 307 | _hasReceivedSdp = NO; |
| 308 | _messageQueue = [NSMutableArray array]; |
| 309 | _peerConnection = nil; |
| 310 | self.state = kARDAppClientStateDisconnected; |
tkchin | d1fb26d | 2016-02-03 01:51:18 -0800 | [diff] [blame] | 311 | #if defined(WEBRTC_IOS) |
| 312 | RTCStopInternalCapture(); |
| 313 | #endif |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 314 | } |
| 315 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 316 | #pragma mark - ARDSignalingChannelDelegate |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 317 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 318 | - (void)channel:(id<ARDSignalingChannel>)channel |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 319 | didReceiveMessage:(ARDSignalingMessage *)message { |
| 320 | switch (message.type) { |
| 321 | case kARDSignalingMessageTypeOffer: |
| 322 | case kARDSignalingMessageTypeAnswer: |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 323 | // Offers and answers must be processed before any other message, so we |
| 324 | // place them at the front of the queue. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 325 | _hasReceivedSdp = YES; |
| 326 | [_messageQueue insertObject:message atIndex:0]; |
| 327 | break; |
| 328 | case kARDSignalingMessageTypeCandidate: |
| 329 | [_messageQueue addObject:message]; |
| 330 | break; |
| 331 | case kARDSignalingMessageTypeBye: |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 332 | // Disconnects can be processed immediately. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 333 | [self processSignalingMessage:message]; |
| 334 | return; |
| 335 | } |
| 336 | [self drainMessageQueueIfReady]; |
| 337 | } |
| 338 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 339 | - (void)channel:(id<ARDSignalingChannel>)channel |
| 340 | didChangeState:(ARDSignalingChannelState)state { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 341 | switch (state) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 342 | case kARDSignalingChannelStateOpen: |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 343 | break; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 344 | case kARDSignalingChannelStateRegistered: |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 345 | break; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 346 | case kARDSignalingChannelStateClosed: |
| 347 | case kARDSignalingChannelStateError: |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 348 | // TODO(tkchin): reconnection scenarios. Right now we just disconnect |
| 349 | // completely if the websocket connection fails. |
| 350 | [self disconnect]; |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | #pragma mark - RTCPeerConnectionDelegate |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 356 | // Callbacks for this delegate occur on non-main thread and need to be |
| 357 | // dispatched back to main queue as needed. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 358 | |
| 359 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 360 | didChangeSignalingState:(RTCSignalingState)stateChanged { |
| 361 | RTCLog(@"Signaling state changed: %ld", (long)stateChanged); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 365 | didAddStream:(RTCMediaStream *)stream { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 366 | dispatch_async(dispatch_get_main_queue(), ^{ |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 367 | RTCLog(@"Received %lu video tracks and %lu audio tracks", |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 368 | (unsigned long)stream.videoTracks.count, |
| 369 | (unsigned long)stream.audioTracks.count); |
| 370 | if (stream.videoTracks.count) { |
| 371 | RTCVideoTrack *videoTrack = stream.videoTracks[0]; |
| 372 | [_delegate appClient:self didReceiveRemoteVideoTrack:videoTrack]; |
| 373 | } |
| 374 | }); |
| 375 | } |
| 376 | |
| 377 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 378 | didRemoveStream:(RTCMediaStream *)stream { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 379 | RTCLog(@"Stream was removed."); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 380 | } |
| 381 | |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 382 | - (void)peerConnectionShouldNegotiate:(RTCPeerConnection *)peerConnection { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 383 | RTCLog(@"WARNING: Renegotiation needed but unimplemented."); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 387 | didChangeIceConnectionState:(RTCIceConnectionState)newState { |
| 388 | RTCLog(@"ICE state changed: %ld", (long)newState); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 389 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 390 | [_delegate appClient:self didChangeConnectionState:newState]; |
| 391 | }); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 395 | didChangeIceGatheringState:(RTCIceGatheringState)newState { |
| 396 | RTCLog(@"ICE gathering state changed: %ld", (long)newState); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 400 | didGenerateIceCandidate:(RTCIceCandidate *)candidate { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 401 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 402 | ARDICECandidateMessage *message = |
| 403 | [[ARDICECandidateMessage alloc] initWithCandidate:candidate]; |
| 404 | [self sendSignalingMessage:message]; |
| 405 | }); |
| 406 | } |
| 407 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 408 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 409 | didOpenDataChannel:(RTCDataChannel *)dataChannel { |
| 410 | } |
| 411 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 412 | #pragma mark - RTCSessionDescriptionDelegate |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 413 | // Callbacks for this delegate occur on non-main thread and need to be |
| 414 | // dispatched back to main queue as needed. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 415 | |
| 416 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 417 | didCreateSessionDescription:(RTCSessionDescription *)sdp |
| 418 | error:(NSError *)error { |
| 419 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 420 | if (error) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 421 | RTCLogError(@"Failed to create session description. Error: %@", error); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 422 | [self disconnect]; |
| 423 | NSDictionary *userInfo = @{ |
| 424 | NSLocalizedDescriptionKey: @"Failed to create session description.", |
| 425 | }; |
| 426 | NSError *sdpError = |
| 427 | [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 428 | code:kARDAppClientErrorCreateSDP |
| 429 | userInfo:userInfo]; |
| 430 | [_delegate appClient:self didError:sdpError]; |
| 431 | return; |
| 432 | } |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 433 | // Prefer H264 if available. |
| 434 | RTCSessionDescription *sdpPreferringH264 = |
| 435 | [ARDSDPUtils descriptionForDescription:sdp |
| 436 | preferredVideoCodec:@"H264"]; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 437 | __weak ARDAppClient *weakSelf = self; |
| 438 | [_peerConnection setLocalDescription:sdpPreferringH264 |
| 439 | completionHandler:^(NSError *error) { |
| 440 | ARDAppClient *strongSelf = weakSelf; |
| 441 | [strongSelf peerConnection:strongSelf.peerConnection |
| 442 | didSetSessionDescriptionWithError:error]; |
| 443 | }]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 444 | ARDSessionDescriptionMessage *message = |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 445 | [[ARDSessionDescriptionMessage alloc] |
| 446 | initWithDescription:sdpPreferringH264]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 447 | [self sendSignalingMessage:message]; |
| 448 | }); |
| 449 | } |
| 450 | |
| 451 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 452 | didSetSessionDescriptionWithError:(NSError *)error { |
| 453 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 454 | if (error) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 455 | RTCLogError(@"Failed to set session description. Error: %@", error); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 456 | [self disconnect]; |
| 457 | NSDictionary *userInfo = @{ |
| 458 | NSLocalizedDescriptionKey: @"Failed to set session description.", |
| 459 | }; |
| 460 | NSError *sdpError = |
| 461 | [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 462 | code:kARDAppClientErrorSetSDP |
| 463 | userInfo:userInfo]; |
| 464 | [_delegate appClient:self didError:sdpError]; |
| 465 | return; |
| 466 | } |
| 467 | // If we're answering and we've just set the remote offer we need to create |
| 468 | // an answer and set the local description. |
| 469 | if (!_isInitiator && !_peerConnection.localDescription) { |
| 470 | RTCMediaConstraints *constraints = [self defaultAnswerConstraints]; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 471 | __weak ARDAppClient *weakSelf = self; |
| 472 | [_peerConnection answerForConstraints:constraints |
| 473 | completionHandler:^(RTCSessionDescription *sdp, |
| 474 | NSError *error) { |
| 475 | ARDAppClient *strongSelf = weakSelf; |
| 476 | [strongSelf peerConnection:strongSelf.peerConnection |
| 477 | didCreateSessionDescription:sdp |
| 478 | error:error]; |
| 479 | }]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 480 | } |
| 481 | }); |
| 482 | } |
| 483 | |
| 484 | #pragma mark - Private |
| 485 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 486 | - (BOOL)hasJoinedRoomServerRoom { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 487 | return _clientId.length; |
| 488 | } |
| 489 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 490 | // Begins the peer connection connection process if we have both joined a room |
| 491 | // on the room server and tried to obtain a TURN server. Otherwise does nothing. |
| 492 | // A peer connection object will be created with a stream that contains local |
| 493 | // audio and video capture. If this client is the caller, an offer is created as |
| 494 | // well, otherwise the client will wait for an offer to arrive. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 495 | - (void)startSignalingIfReady { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 496 | if (!_isTurnComplete || !self.hasJoinedRoomServerRoom) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 497 | return; |
| 498 | } |
| 499 | self.state = kARDAppClientStateConnected; |
| 500 | |
| 501 | // Create peer connection. |
| 502 | RTCMediaConstraints *constraints = [self defaultPeerConnectionConstraints]; |
Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 503 | RTCConfiguration *config = [[RTCConfiguration alloc] init]; |
| 504 | config.iceServers = _iceServers; |
Tze Kwang Chin | f3cb49f | 2016-03-22 10:57:40 -0700 | [diff] [blame^] | 505 | _peerConnection = [_factory peerConnectionWithConfiguration:config |
| 506 | constraints:constraints |
| 507 | delegate:self]; |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 508 | // Create AV media stream and add it to the peer connection. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 509 | RTCMediaStream *localStream = [self createLocalMediaStream]; |
| 510 | [_peerConnection addStream:localStream]; |
| 511 | if (_isInitiator) { |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 512 | // Send offer. |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 513 | __weak ARDAppClient *weakSelf = self; |
| 514 | [_peerConnection offerForConstraints:[self defaultOfferConstraints] |
| 515 | completionHandler:^(RTCSessionDescription *sdp, |
| 516 | NSError *error) { |
| 517 | ARDAppClient *strongSelf = weakSelf; |
| 518 | [strongSelf peerConnection:strongSelf.peerConnection |
| 519 | didCreateSessionDescription:sdp |
| 520 | error:error]; |
| 521 | }]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 522 | } else { |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 523 | // Check if we've received an offer. |
| 524 | [self drainMessageQueueIfReady]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 528 | // Processes the messages that we've received from the room server and the |
| 529 | // signaling channel. The offer or answer message must be processed before other |
| 530 | // signaling messages, however they can arrive out of order. Hence, this method |
| 531 | // only processes pending messages if there is a peer connection object and |
| 532 | // if we have received either an offer or answer. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 533 | - (void)drainMessageQueueIfReady { |
| 534 | if (!_peerConnection || !_hasReceivedSdp) { |
| 535 | return; |
| 536 | } |
| 537 | for (ARDSignalingMessage *message in _messageQueue) { |
| 538 | [self processSignalingMessage:message]; |
| 539 | } |
| 540 | [_messageQueue removeAllObjects]; |
| 541 | } |
| 542 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 543 | // Processes the given signaling message based on its type. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 544 | - (void)processSignalingMessage:(ARDSignalingMessage *)message { |
| 545 | NSParameterAssert(_peerConnection || |
| 546 | message.type == kARDSignalingMessageTypeBye); |
| 547 | switch (message.type) { |
| 548 | case kARDSignalingMessageTypeOffer: |
| 549 | case kARDSignalingMessageTypeAnswer: { |
| 550 | ARDSessionDescriptionMessage *sdpMessage = |
| 551 | (ARDSessionDescriptionMessage *)message; |
| 552 | RTCSessionDescription *description = sdpMessage.sessionDescription; |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 553 | // Prefer H264 if available. |
| 554 | RTCSessionDescription *sdpPreferringH264 = |
| 555 | [ARDSDPUtils descriptionForDescription:description |
| 556 | preferredVideoCodec:@"H264"]; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 557 | __weak ARDAppClient *weakSelf = self; |
| 558 | [_peerConnection setRemoteDescription:sdpPreferringH264 |
| 559 | completionHandler:^(NSError *error) { |
| 560 | ARDAppClient *strongSelf = weakSelf; |
| 561 | [strongSelf peerConnection:strongSelf.peerConnection |
| 562 | didSetSessionDescriptionWithError:error]; |
| 563 | }]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 564 | break; |
| 565 | } |
| 566 | case kARDSignalingMessageTypeCandidate: { |
| 567 | ARDICECandidateMessage *candidateMessage = |
| 568 | (ARDICECandidateMessage *)message; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 569 | [_peerConnection addIceCandidate:candidateMessage.candidate]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 570 | break; |
| 571 | } |
| 572 | case kARDSignalingMessageTypeBye: |
| 573 | // Other client disconnected. |
| 574 | // TODO(tkchin): support waiting in room for next client. For now just |
| 575 | // disconnect. |
| 576 | [self disconnect]; |
| 577 | break; |
| 578 | } |
| 579 | } |
| 580 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 581 | // Sends a signaling message to the other client. The caller will send messages |
| 582 | // through the room server, whereas the callee will send messages over the |
| 583 | // signaling channel. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 584 | - (void)sendSignalingMessage:(ARDSignalingMessage *)message { |
| 585 | if (_isInitiator) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 586 | __weak ARDAppClient *weakSelf = self; |
| 587 | [_roomServerClient sendMessage:message |
| 588 | forRoomId:_roomId |
| 589 | clientId:_clientId |
| 590 | completionHandler:^(ARDMessageResponse *response, |
| 591 | NSError *error) { |
| 592 | ARDAppClient *strongSelf = weakSelf; |
| 593 | if (error) { |
| 594 | [strongSelf.delegate appClient:strongSelf didError:error]; |
| 595 | return; |
| 596 | } |
| 597 | NSError *messageError = |
| 598 | [[strongSelf class] errorForMessageResultType:response.result]; |
| 599 | if (messageError) { |
| 600 | [strongSelf.delegate appClient:strongSelf didError:messageError]; |
| 601 | return; |
| 602 | } |
| 603 | }]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 604 | } else { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 605 | [_channel sendMessage:message]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
| 609 | - (RTCMediaStream *)createLocalMediaStream { |
Tze Kwang Chin | f3cb49f | 2016-03-22 10:57:40 -0700 | [diff] [blame^] | 610 | RTCMediaStream *localStream = [_factory mediaStreamWithStreamId:@"ARDAMS"]; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 611 | RTCVideoTrack *localVideoTrack = [self createLocalVideoTrack]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 612 | if (localVideoTrack) { |
| 613 | [localStream addVideoTrack:localVideoTrack]; |
| 614 | [_delegate appClient:self didReceiveLocalVideoTrack:localVideoTrack]; |
| 615 | } |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 616 | RTCAudioTrack *localAudioTrack = |
Tze Kwang Chin | f3cb49f | 2016-03-22 10:57:40 -0700 | [diff] [blame^] | 617 | [_factory audioTrackWithTrackId:@"ARDAMSa0"]; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 618 | [localStream addAudioTrack:localAudioTrack]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 619 | return localStream; |
| 620 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 621 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 622 | - (RTCVideoTrack *)createLocalVideoTrack { |
| 623 | RTCVideoTrack* localVideoTrack = nil; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 624 | // The iOS simulator doesn't provide any sort of camera capture |
| 625 | // support or emulation (http://goo.gl/rHAnC1) so don't bother |
| 626 | // trying to open a local stream. |
| 627 | // TODO(tkchin): local video capture for OSX. See |
| 628 | // https://code.google.com/p/webrtc/issues/detail?id=3417. |
| 629 | #if !TARGET_IPHONE_SIMULATOR && TARGET_OS_IPHONE |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 630 | if (!_isAudioOnly) { |
| 631 | RTCMediaConstraints *mediaConstraints = |
| 632 | [self defaultMediaStreamConstraints]; |
| 633 | RTCAVFoundationVideoSource *source = |
Tze Kwang Chin | f3cb49f | 2016-03-22 10:57:40 -0700 | [diff] [blame^] | 634 | [_factory avFoundationVideoSourceWithConstraints:mediaConstraints]; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 635 | localVideoTrack = |
Tze Kwang Chin | f3cb49f | 2016-03-22 10:57:40 -0700 | [diff] [blame^] | 636 | [_factory videoTrackWithSource:source |
| 637 | trackId:@"ARDAMSv0"]; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 638 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 639 | #endif |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 640 | return localVideoTrack; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 641 | } |
| 642 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 643 | #pragma mark - Collider methods |
| 644 | |
| 645 | - (void)registerWithColliderIfReady { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 646 | if (!self.hasJoinedRoomServerRoom) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 647 | return; |
| 648 | } |
| 649 | // Open WebSocket connection. |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 650 | if (!_channel) { |
| 651 | _channel = |
| 652 | [[ARDWebSocketChannel alloc] initWithURL:_websocketURL |
| 653 | restURL:_websocketRestURL |
| 654 | delegate:self]; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 655 | if (_isLoopback) { |
| 656 | _loopbackChannel = |
| 657 | [[ARDLoopbackWebSocketChannel alloc] initWithURL:_websocketURL |
| 658 | restURL:_websocketRestURL]; |
| 659 | } |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 660 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 661 | [_channel registerForRoomId:_roomId clientId:_clientId]; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 662 | if (_isLoopback) { |
| 663 | [_loopbackChannel registerForRoomId:_roomId clientId:@"LOOPBACK_CLIENT_ID"]; |
| 664 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 665 | } |
| 666 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 667 | #pragma mark - Defaults |
| 668 | |
| 669 | - (RTCMediaConstraints *)defaultMediaStreamConstraints { |
| 670 | RTCMediaConstraints* constraints = |
| 671 | [[RTCMediaConstraints alloc] |
| 672 | initWithMandatoryConstraints:nil |
| 673 | optionalConstraints:nil]; |
| 674 | return constraints; |
| 675 | } |
| 676 | |
| 677 | - (RTCMediaConstraints *)defaultAnswerConstraints { |
| 678 | return [self defaultOfferConstraints]; |
| 679 | } |
| 680 | |
| 681 | - (RTCMediaConstraints *)defaultOfferConstraints { |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 682 | NSDictionary *mandatoryConstraints = @{ |
| 683 | @"OfferToReceiveAudio" : @"true", |
| 684 | @"OfferToReceiveVideo" : @"true" |
| 685 | }; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 686 | RTCMediaConstraints* constraints = |
| 687 | [[RTCMediaConstraints alloc] |
| 688 | initWithMandatoryConstraints:mandatoryConstraints |
| 689 | optionalConstraints:nil]; |
| 690 | return constraints; |
| 691 | } |
| 692 | |
| 693 | - (RTCMediaConstraints *)defaultPeerConnectionConstraints { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 694 | if (_defaultPeerConnectionConstraints) { |
| 695 | return _defaultPeerConnectionConstraints; |
| 696 | } |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 697 | NSString *value = _isLoopback ? @"false" : @"true"; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 698 | NSDictionary *optionalConstraints = @{ @"DtlsSrtpKeyAgreement" : value }; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 699 | RTCMediaConstraints* constraints = |
| 700 | [[RTCMediaConstraints alloc] |
| 701 | initWithMandatoryConstraints:nil |
| 702 | optionalConstraints:optionalConstraints]; |
| 703 | return constraints; |
| 704 | } |
| 705 | |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 706 | - (RTCIceServer *)defaultSTUNServer { |
| 707 | return [[RTCIceServer alloc] initWithURLStrings:@[kARDDefaultSTUNServerUrl] |
| 708 | username:@"" |
| 709 | credential:@""]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 710 | } |
| 711 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 712 | #pragma mark - Errors |
| 713 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 714 | + (NSError *)errorForJoinResultType:(ARDJoinResultType)resultType { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 715 | NSError *error = nil; |
| 716 | switch (resultType) { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 717 | case kARDJoinResultTypeSuccess: |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 718 | break; |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 719 | case kARDJoinResultTypeUnknown: { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 720 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 721 | code:kARDAppClientErrorUnknown |
| 722 | userInfo:@{ |
| 723 | NSLocalizedDescriptionKey: @"Unknown error.", |
| 724 | }]; |
| 725 | break; |
| 726 | } |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 727 | case kARDJoinResultTypeFull: { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 728 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 729 | code:kARDAppClientErrorRoomFull |
| 730 | userInfo:@{ |
| 731 | NSLocalizedDescriptionKey: @"Room is full.", |
| 732 | }]; |
| 733 | break; |
| 734 | } |
| 735 | } |
| 736 | return error; |
| 737 | } |
| 738 | |
| 739 | + (NSError *)errorForMessageResultType:(ARDMessageResultType)resultType { |
| 740 | NSError *error = nil; |
| 741 | switch (resultType) { |
| 742 | case kARDMessageResultTypeSuccess: |
| 743 | break; |
| 744 | case kARDMessageResultTypeUnknown: |
| 745 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 746 | code:kARDAppClientErrorUnknown |
| 747 | userInfo:@{ |
| 748 | NSLocalizedDescriptionKey: @"Unknown error.", |
| 749 | }]; |
| 750 | break; |
| 751 | case kARDMessageResultTypeInvalidClient: |
| 752 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 753 | code:kARDAppClientErrorInvalidClient |
| 754 | userInfo:@{ |
| 755 | NSLocalizedDescriptionKey: @"Invalid client.", |
| 756 | }]; |
| 757 | break; |
| 758 | case kARDMessageResultTypeInvalidRoom: |
| 759 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 760 | code:kARDAppClientErrorInvalidRoom |
| 761 | userInfo:@{ |
| 762 | NSLocalizedDescriptionKey: @"Invalid room.", |
| 763 | }]; |
| 764 | break; |
| 765 | } |
| 766 | return error; |
| 767 | } |
| 768 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 769 | @end |