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