OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #import "ARDAppClient+Internal.h" | 11 #import "ARDAppClient+Internal.h" |
12 | 12 |
13 #import "WebRTC/RTCAVFoundationVideoSource.h" | 13 #import "WebRTC/RTCAVFoundationVideoSource.h" |
14 #import "WebRTC/RTCAudioTrack.h" | 14 #import "WebRTC/RTCAudioTrack.h" |
15 #import "WebRTC/RTCConfiguration.h" | 15 #import "WebRTC/RTCConfiguration.h" |
16 #import "WebRTC/RTCFileLogger.h" | 16 #import "WebRTC/RTCFileLogger.h" |
17 #import "WebRTC/RTCIceServer.h" | 17 #import "WebRTC/RTCIceServer.h" |
18 #import "WebRTC/RTCLogging.h" | 18 #import "WebRTC/RTCLogging.h" |
19 #import "WebRTC/RTCMediaConstraints.h" | 19 #import "WebRTC/RTCMediaConstraints.h" |
20 #import "WebRTC/RTCMediaStream.h" | 20 #import "WebRTC/RTCMediaStream.h" |
21 #import "WebRTC/RTCPeerConnectionFactory.h" | 21 #import "WebRTC/RTCPeerConnectionFactory.h" |
22 #import "WebRTC/RTCRtpSender.h" | 22 #import "WebRTC/RTCRtpSender.h" |
23 #import "WebRTC/RTCTracing.h" | 23 #import "WebRTC/RTCTracing.h" |
24 | 24 |
25 #import "ARDAppEngineClient.h" | 25 #import "ARDAppEngineClient.h" |
26 #import "ARDTURNClient+Internal.h" | |
27 #import "ARDJoinResponse.h" | 26 #import "ARDJoinResponse.h" |
28 #import "ARDMessageResponse.h" | 27 #import "ARDMessageResponse.h" |
29 #import "ARDSDPUtils.h" | 28 #import "ARDSDPUtils.h" |
| 29 #import "ARDSettingsModel.h" |
30 #import "ARDSignalingMessage.h" | 30 #import "ARDSignalingMessage.h" |
| 31 #import "ARDTURNClient+Internal.h" |
31 #import "ARDUtilities.h" | 32 #import "ARDUtilities.h" |
32 #import "ARDWebSocketChannel.h" | 33 #import "ARDWebSocketChannel.h" |
33 #import "RTCIceCandidate+JSON.h" | 34 #import "RTCIceCandidate+JSON.h" |
34 #import "RTCSessionDescription+JSON.h" | 35 #import "RTCSessionDescription+JSON.h" |
35 | 36 |
36 static NSString * const kARDIceServerRequestUrl = @"https://appr.tc/params"; | 37 static NSString * const kARDIceServerRequestUrl = @"https://appr.tc/params"; |
37 | 38 |
38 static NSString * const kARDAppClientErrorDomain = @"ARDAppClient"; | 39 static NSString * const kARDAppClientErrorDomain = @"ARDAppClient"; |
39 static NSInteger const kARDAppClientErrorUnknown = -1; | 40 static NSInteger const kARDAppClientErrorUnknown = -1; |
40 static NSInteger const kARDAppClientErrorRoomFull = -2; | 41 static NSInteger const kARDAppClientErrorRoomFull = -2; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 92 |
92 - (void)timerDidFire:(NSTimer *)timer { | 93 - (void)timerDidFire:(NSTimer *)timer { |
93 _timerHandler(); | 94 _timerHandler(); |
94 } | 95 } |
95 | 96 |
96 @end | 97 @end |
97 | 98 |
98 @implementation ARDAppClient { | 99 @implementation ARDAppClient { |
99 RTCFileLogger *_fileLogger; | 100 RTCFileLogger *_fileLogger; |
100 ARDTimerProxy *_statsTimer; | 101 ARDTimerProxy *_statsTimer; |
101 RTCMediaConstraints *_cameraConstraints; | 102 ARDSettingsModel *_settings; |
102 NSNumber *_maxBitrate; | |
103 NSString *_videoCodec; | |
104 } | 103 } |
105 | 104 |
106 @synthesize shouldGetStats = _shouldGetStats; | 105 @synthesize shouldGetStats = _shouldGetStats; |
107 @synthesize state = _state; | 106 @synthesize state = _state; |
108 @synthesize delegate = _delegate; | 107 @synthesize delegate = _delegate; |
109 @synthesize roomServerClient = _roomServerClient; | 108 @synthesize roomServerClient = _roomServerClient; |
110 @synthesize channel = _channel; | 109 @synthesize channel = _channel; |
111 @synthesize loopbackChannel = _loopbackChannel; | 110 @synthesize loopbackChannel = _loopbackChannel; |
112 @synthesize turnClient = _turnClient; | 111 @synthesize turnClient = _turnClient; |
113 @synthesize peerConnection = _peerConnection; | 112 @synthesize peerConnection = _peerConnection; |
114 @synthesize factory = _factory; | 113 @synthesize factory = _factory; |
115 @synthesize messageQueue = _messageQueue; | 114 @synthesize messageQueue = _messageQueue; |
116 @synthesize isTurnComplete = _isTurnComplete; | 115 @synthesize isTurnComplete = _isTurnComplete; |
117 @synthesize hasReceivedSdp = _hasReceivedSdp; | 116 @synthesize hasReceivedSdp = _hasReceivedSdp; |
118 @synthesize roomId = _roomId; | 117 @synthesize roomId = _roomId; |
119 @synthesize clientId = _clientId; | 118 @synthesize clientId = _clientId; |
120 @synthesize isInitiator = _isInitiator; | 119 @synthesize isInitiator = _isInitiator; |
121 @synthesize iceServers = _iceServers; | 120 @synthesize iceServers = _iceServers; |
122 @synthesize webSocketURL = _websocketURL; | 121 @synthesize webSocketURL = _websocketURL; |
123 @synthesize webSocketRestURL = _websocketRestURL; | 122 @synthesize webSocketRestURL = _websocketRestURL; |
124 @synthesize defaultPeerConnectionConstraints = | 123 @synthesize defaultPeerConnectionConstraints = |
125 _defaultPeerConnectionConstraints; | 124 _defaultPeerConnectionConstraints; |
126 @synthesize isLoopback = _isLoopback; | 125 @synthesize isLoopback = _isLoopback; |
127 @synthesize isAudioOnly = _isAudioOnly; | 126 @synthesize isAudioOnly = _isAudioOnly; |
128 @synthesize shouldMakeAecDump = _shouldMakeAecDump; | 127 @synthesize shouldMakeAecDump = _shouldMakeAecDump; |
129 @synthesize shouldUseLevelControl = _shouldUseLevelControl; | 128 @synthesize shouldUseLevelControl = _shouldUseLevelControl; |
130 | 129 |
131 - (instancetype)init { | 130 - (instancetype)init { |
132 return [self initWithDelegate:nil preferVideoCodec:@"H264"]; | 131 return [self initWithDelegate:nil]; |
133 } | 132 } |
134 | 133 |
135 - (instancetype)initWithDelegate:(id<ARDAppClientDelegate>)delegate | 134 - (instancetype)initWithDelegate:(id<ARDAppClientDelegate>)delegate { |
136 preferVideoCodec:(NSString *)codec { | |
137 if (self = [super init]) { | 135 if (self = [super init]) { |
138 _roomServerClient = [[ARDAppEngineClient alloc] init]; | 136 _roomServerClient = [[ARDAppEngineClient alloc] init]; |
139 _delegate = delegate; | 137 _delegate = delegate; |
140 _videoCodec = codec; | |
141 NSURL *turnRequestURL = [NSURL URLWithString:kARDIceServerRequestUrl]; | 138 NSURL *turnRequestURL = [NSURL URLWithString:kARDIceServerRequestUrl]; |
142 _turnClient = [[ARDTURNClient alloc] initWithURL:turnRequestURL]; | 139 _turnClient = [[ARDTURNClient alloc] initWithURL:turnRequestURL]; |
143 [self configure]; | 140 [self configure]; |
144 } | 141 } |
145 return self; | 142 return self; |
146 } | 143 } |
147 | 144 |
148 // TODO(tkchin): Provide signaling channel factory interface so we can recreate | 145 // 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 | 146 // channel if we need to on network failure. Also, make this the default public |
150 // constructor. | 147 // constructor. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 | 203 |
207 - (void)setState:(ARDAppClientState)state { | 204 - (void)setState:(ARDAppClientState)state { |
208 if (_state == state) { | 205 if (_state == state) { |
209 return; | 206 return; |
210 } | 207 } |
211 _state = state; | 208 _state = state; |
212 [_delegate appClient:self didChangeState:_state]; | 209 [_delegate appClient:self didChangeState:_state]; |
213 } | 210 } |
214 | 211 |
215 - (void)connectToRoomWithId:(NSString *)roomId | 212 - (void)connectToRoomWithId:(NSString *)roomId |
| 213 settings:(ARDSettingsModel *)settings |
216 isLoopback:(BOOL)isLoopback | 214 isLoopback:(BOOL)isLoopback |
217 isAudioOnly:(BOOL)isAudioOnly | 215 isAudioOnly:(BOOL)isAudioOnly |
218 shouldMakeAecDump:(BOOL)shouldMakeAecDump | 216 shouldMakeAecDump:(BOOL)shouldMakeAecDump |
219 shouldUseLevelControl:(BOOL)shouldUseLevelControl { | 217 shouldUseLevelControl:(BOOL)shouldUseLevelControl { |
220 NSParameterAssert(roomId.length); | 218 NSParameterAssert(roomId.length); |
221 NSParameterAssert(_state == kARDAppClientStateDisconnected); | 219 NSParameterAssert(_state == kARDAppClientStateDisconnected); |
| 220 _settings = settings; |
222 _isLoopback = isLoopback; | 221 _isLoopback = isLoopback; |
223 _isAudioOnly = isAudioOnly; | 222 _isAudioOnly = isAudioOnly; |
224 _shouldMakeAecDump = shouldMakeAecDump; | 223 _shouldMakeAecDump = shouldMakeAecDump; |
225 _shouldUseLevelControl = shouldUseLevelControl; | 224 _shouldUseLevelControl = shouldUseLevelControl; |
226 self.state = kARDAppClientStateConnecting; | 225 self.state = kARDAppClientStateConnecting; |
227 | 226 |
228 #if defined(WEBRTC_IOS) | 227 #if defined(WEBRTC_IOS) |
229 if (kARDAppClientEnableTracing) { | 228 if (kARDAppClientEnableTracing) { |
230 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-trace.txt"]
; | 229 NSString *filePath = [self documentsFilePathForFileName:@"webrtc-trace.txt"]
; |
231 RTCStartInternalCapture(filePath); | 230 RTCStartInternalCapture(filePath); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 #endif | 311 #endif |
313 _peerConnection = nil; | 312 _peerConnection = nil; |
314 self.state = kARDAppClientStateDisconnected; | 313 self.state = kARDAppClientStateDisconnected; |
315 #if defined(WEBRTC_IOS) | 314 #if defined(WEBRTC_IOS) |
316 if (kARDAppClientEnableTracing) { | 315 if (kARDAppClientEnableTracing) { |
317 RTCStopInternalCapture(); | 316 RTCStopInternalCapture(); |
318 } | 317 } |
319 #endif | 318 #endif |
320 } | 319 } |
321 | 320 |
322 - (void)setCameraConstraints:(RTCMediaConstraints *)mediaConstraints { | |
323 _cameraConstraints = mediaConstraints; | |
324 } | |
325 | |
326 - (void)setMaxBitrate:(NSNumber *)maxBitrate { | |
327 _maxBitrate = maxBitrate; | |
328 } | |
329 | |
330 #pragma mark - ARDSignalingChannelDelegate | 321 #pragma mark - ARDSignalingChannelDelegate |
331 | 322 |
332 - (void)channel:(id<ARDSignalingChannel>)channel | 323 - (void)channel:(id<ARDSignalingChannel>)channel |
333 didReceiveMessage:(ARDSignalingMessage *)message { | 324 didReceiveMessage:(ARDSignalingMessage *)message { |
334 switch (message.type) { | 325 switch (message.type) { |
335 case kARDSignalingMessageTypeOffer: | 326 case kARDSignalingMessageTypeOffer: |
336 case kARDSignalingMessageTypeAnswer: | 327 case kARDSignalingMessageTypeAnswer: |
337 // Offers and answers must be processed before any other message, so we | 328 // Offers and answers must be processed before any other message, so we |
338 // place them at the front of the queue. | 329 // place them at the front of the queue. |
339 _hasReceivedSdp = YES; | 330 _hasReceivedSdp = YES; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 NSError *sdpError = | 442 NSError *sdpError = |
452 [[NSError alloc] initWithDomain:kARDAppClientErrorDomain | 443 [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
453 code:kARDAppClientErrorCreateSDP | 444 code:kARDAppClientErrorCreateSDP |
454 userInfo:userInfo]; | 445 userInfo:userInfo]; |
455 [_delegate appClient:self didError:sdpError]; | 446 [_delegate appClient:self didError:sdpError]; |
456 return; | 447 return; |
457 } | 448 } |
458 // Prefer codec from settings if available. | 449 // Prefer codec from settings if available. |
459 RTCSessionDescription *sdpPreferringCodec = | 450 RTCSessionDescription *sdpPreferringCodec = |
460 [ARDSDPUtils descriptionForDescription:sdp | 451 [ARDSDPUtils descriptionForDescription:sdp |
461 preferredVideoCodec:_videoCodec]; | 452 preferredVideoCodec:[_settings currentVideoCodecSetti
ngFromStore]]; |
462 __weak ARDAppClient *weakSelf = self; | 453 __weak ARDAppClient *weakSelf = self; |
463 [_peerConnection setLocalDescription:sdpPreferringCodec | 454 [_peerConnection setLocalDescription:sdpPreferringCodec |
464 completionHandler:^(NSError *error) { | 455 completionHandler:^(NSError *error) { |
465 ARDAppClient *strongSelf = weakSelf; | 456 ARDAppClient *strongSelf = weakSelf; |
466 [strongSelf peerConnection:strongSelf.peerConnection | 457 [strongSelf peerConnection:strongSelf.peerConnection |
467 didSetSessionDescriptionWithError:error]; | 458 didSetSessionDescriptionWithError:error]; |
468 }]; | 459 }]; |
469 ARDSessionDescriptionMessage *message = | 460 ARDSessionDescriptionMessage *message = |
470 [[ARDSessionDescriptionMessage alloc] | 461 [[ARDSessionDescriptionMessage alloc] |
471 initWithDescription:sdpPreferringCodec]; | 462 initWithDescription:sdpPreferringCodec]; |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
605 message.type == kARDSignalingMessageTypeBye); | 596 message.type == kARDSignalingMessageTypeBye); |
606 switch (message.type) { | 597 switch (message.type) { |
607 case kARDSignalingMessageTypeOffer: | 598 case kARDSignalingMessageTypeOffer: |
608 case kARDSignalingMessageTypeAnswer: { | 599 case kARDSignalingMessageTypeAnswer: { |
609 ARDSessionDescriptionMessage *sdpMessage = | 600 ARDSessionDescriptionMessage *sdpMessage = |
610 (ARDSessionDescriptionMessage *)message; | 601 (ARDSessionDescriptionMessage *)message; |
611 RTCSessionDescription *description = sdpMessage.sessionDescription; | 602 RTCSessionDescription *description = sdpMessage.sessionDescription; |
612 // Prefer codec from settings if available. | 603 // Prefer codec from settings if available. |
613 RTCSessionDescription *sdpPreferringCodec = | 604 RTCSessionDescription *sdpPreferringCodec = |
614 [ARDSDPUtils descriptionForDescription:description | 605 [ARDSDPUtils descriptionForDescription:description |
615 preferredVideoCodec:_videoCodec]; | 606 preferredVideoCodec:[_settings currentVideoCodecSet
tingFromStore]]; |
616 __weak ARDAppClient *weakSelf = self; | 607 __weak ARDAppClient *weakSelf = self; |
617 [_peerConnection setRemoteDescription:sdpPreferringCodec | 608 [_peerConnection setRemoteDescription:sdpPreferringCodec |
618 completionHandler:^(NSError *error) { | 609 completionHandler:^(NSError *error) { |
619 ARDAppClient *strongSelf = weakSelf; | 610 ARDAppClient *strongSelf = weakSelf; |
620 [strongSelf peerConnection:strongSelf.peerConnection | 611 [strongSelf peerConnection:strongSelf.peerConnection |
621 didSetSessionDescriptionWithError:error]; | 612 didSetSessionDescriptionWithError:error]; |
622 }]; | 613 }]; |
623 break; | 614 break; |
624 } | 615 } |
625 case kARDSignalingMessageTypeCandidate: { | 616 case kARDSignalingMessageTypeCandidate: { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 [_delegate appClient:self didReceiveLocalVideoTrack:track]; | 672 [_delegate appClient:self didReceiveLocalVideoTrack:track]; |
682 } | 673 } |
683 | 674 |
684 return sender; | 675 return sender; |
685 } | 676 } |
686 | 677 |
687 - (void)setMaxBitrateForPeerConnectionVideoSender { | 678 - (void)setMaxBitrateForPeerConnectionVideoSender { |
688 for (RTCRtpSender *sender in _peerConnection.senders) { | 679 for (RTCRtpSender *sender in _peerConnection.senders) { |
689 if (sender.track != nil) { | 680 if (sender.track != nil) { |
690 if ([sender.track.kind isEqualToString:kARDVideoTrackKind]) { | 681 if ([sender.track.kind isEqualToString:kARDVideoTrackKind]) { |
691 [self setMaxBitrate:_maxBitrate forVideoSender:sender]; | 682 [self setMaxBitrate:[_settings currentMaxBitrateSettingFromStore] forVid
eoSender:sender]; |
692 } | 683 } |
693 } | 684 } |
694 } | 685 } |
695 } | 686 } |
696 | 687 |
697 - (void)setMaxBitrate:(NSNumber *)maxBitrate forVideoSender:(RTCRtpSender *)send
er { | 688 - (void)setMaxBitrate:(NSNumber *)maxBitrate forVideoSender:(RTCRtpSender *)send
er { |
698 if (maxBitrate.intValue <= 0) { | 689 if (maxBitrate.intValue <= 0) { |
699 return; | 690 return; |
700 } | 691 } |
701 | 692 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
767 NSString *valueLevelControl = _shouldUseLevelControl ? | 758 NSString *valueLevelControl = _shouldUseLevelControl ? |
768 kRTCMediaConstraintsValueTrue : kRTCMediaConstraintsValueFalse; | 759 kRTCMediaConstraintsValueTrue : kRTCMediaConstraintsValueFalse; |
769 NSDictionary *mandatoryConstraints = @{ kRTCMediaConstraintsLevelControl : va
lueLevelControl }; | 760 NSDictionary *mandatoryConstraints = @{ kRTCMediaConstraintsLevelControl : va
lueLevelControl }; |
770 RTCMediaConstraints *constraints = | 761 RTCMediaConstraints *constraints = |
771 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstr
aints | 762 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstr
aints |
772 optionalConstraints:nil]; | 763 optionalConstraints:nil]; |
773 return constraints; | 764 return constraints; |
774 } | 765 } |
775 | 766 |
776 - (RTCMediaConstraints *)cameraConstraints { | 767 - (RTCMediaConstraints *)cameraConstraints { |
777 return _cameraConstraints; | 768 RTCMediaConstraints *cameraConstraints = [[RTCMediaConstraints alloc] |
| 769 initWithMandatoryConstraints:nil |
| 770 optionalConstraints:[_settings currentMediaConstraintFromStoreAsR
TCDictionary]]; |
| 771 return cameraConstraints; |
778 } | 772 } |
779 | 773 |
780 - (RTCMediaConstraints *)defaultAnswerConstraints { | 774 - (RTCMediaConstraints *)defaultAnswerConstraints { |
781 return [self defaultOfferConstraints]; | 775 return [self defaultOfferConstraints]; |
782 } | 776 } |
783 | 777 |
784 - (RTCMediaConstraints *)defaultOfferConstraints { | 778 - (RTCMediaConstraints *)defaultOfferConstraints { |
785 NSDictionary *mandatoryConstraints = @{ | 779 NSDictionary *mandatoryConstraints = @{ |
786 @"OfferToReceiveAudio" : @"true", | 780 @"OfferToReceiveAudio" : @"true", |
787 @"OfferToReceiveVideo" : @"true" | 781 @"OfferToReceiveVideo" : @"true" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
857 code:kARDAppClientErrorInvalidRoom | 851 code:kARDAppClientErrorInvalidRoom |
858 userInfo:@{ | 852 userInfo:@{ |
859 NSLocalizedDescriptionKey: @"Invalid room.", | 853 NSLocalizedDescriptionKey: @"Invalid room.", |
860 }]; | 854 }]; |
861 break; | 855 break; |
862 } | 856 } |
863 return error; | 857 return error; |
864 } | 858 } |
865 | 859 |
866 @end | 860 @end |
OLD | NEW |