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