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