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