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) |
| 14 | #import "RTCAVFoundationVideoSource.h" |
| 15 | #endif |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 16 | #import "RTCFileLogger.h" |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 17 | #import "RTCICEServer.h" |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 18 | #import "RTCLogging.h" |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 19 | #import "RTCMediaConstraints.h" |
| 20 | #import "RTCMediaStream.h" |
| 21 | #import "RTCPair.h" |
Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 22 | #import "RTCPeerConnectionInterface.h" |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 23 | #import "RTCVideoCapturer.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" |
| 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; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 50 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 51 | // We need a proxy to NSTimer because it causes a strong retain cycle. When |
| 52 | // using the proxy, |invalidate| must be called before it properly deallocs. |
| 53 | @interface ARDTimerProxy : NSObject |
| 54 | |
| 55 | - (instancetype)initWithInterval:(NSTimeInterval)interval |
| 56 | repeats:(BOOL)repeats |
| 57 | timerHandler:(void (^)(void))timerHandler; |
| 58 | - (void)invalidate; |
| 59 | |
| 60 | @end |
| 61 | |
| 62 | @implementation ARDTimerProxy { |
| 63 | NSTimer *_timer; |
| 64 | void (^_timerHandler)(void); |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 65 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 66 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 67 | - (instancetype)initWithInterval:(NSTimeInterval)interval |
| 68 | repeats:(BOOL)repeats |
| 69 | timerHandler:(void (^)(void))timerHandler { |
| 70 | NSParameterAssert(timerHandler); |
| 71 | if (self = [super init]) { |
| 72 | _timerHandler = timerHandler; |
| 73 | _timer = [NSTimer scheduledTimerWithTimeInterval:interval |
| 74 | target:self |
| 75 | selector:@selector(timerDidFire:) |
| 76 | userInfo:nil |
| 77 | repeats:repeats]; |
| 78 | } |
| 79 | return self; |
| 80 | } |
| 81 | |
| 82 | - (void)invalidate { |
| 83 | [_timer invalidate]; |
| 84 | } |
| 85 | |
| 86 | - (void)timerDidFire:(NSTimer *)timer { |
| 87 | _timerHandler(); |
| 88 | } |
| 89 | |
| 90 | @end |
| 91 | |
| 92 | @implementation ARDAppClient { |
| 93 | RTCFileLogger *_fileLogger; |
| 94 | ARDTimerProxy *_statsTimer; |
| 95 | } |
| 96 | |
| 97 | @synthesize shouldGetStats = _shouldGetStats; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 98 | @synthesize state = _state; |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 99 | @synthesize delegate = _delegate; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 100 | @synthesize roomServerClient = _roomServerClient; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 101 | @synthesize channel = _channel; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 102 | @synthesize loopbackChannel = _loopbackChannel; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 103 | @synthesize turnClient = _turnClient; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 104 | @synthesize peerConnection = _peerConnection; |
| 105 | @synthesize factory = _factory; |
| 106 | @synthesize messageQueue = _messageQueue; |
| 107 | @synthesize isTurnComplete = _isTurnComplete; |
| 108 | @synthesize hasReceivedSdp = _hasReceivedSdp; |
| 109 | @synthesize roomId = _roomId; |
| 110 | @synthesize clientId = _clientId; |
| 111 | @synthesize isInitiator = _isInitiator; |
| 112 | @synthesize iceServers = _iceServers; |
| 113 | @synthesize webSocketURL = _websocketURL; |
| 114 | @synthesize webSocketRestURL = _websocketRestURL; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 115 | @synthesize defaultPeerConnectionConstraints = |
| 116 | _defaultPeerConnectionConstraints; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 117 | @synthesize isLoopback = _isLoopback; |
| 118 | @synthesize isAudioOnly = _isAudioOnly; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 119 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 120 | - (instancetype)init { |
| 121 | if (self = [super init]) { |
| 122 | _roomServerClient = [[ARDAppEngineClient alloc] init]; |
| 123 | NSURL *turnRequestURL = [NSURL URLWithString:kARDTurnRequestUrl]; |
| 124 | _turnClient = [[ARDCEODTURNClient alloc] initWithURL:turnRequestURL]; |
| 125 | [self configure]; |
| 126 | } |
| 127 | return self; |
| 128 | } |
| 129 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 130 | - (instancetype)initWithDelegate:(id<ARDAppClientDelegate>)delegate { |
| 131 | if (self = [super init]) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 132 | _roomServerClient = [[ARDAppEngineClient alloc] init]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 133 | _delegate = delegate; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 134 | NSURL *turnRequestURL = [NSURL URLWithString:kARDTurnRequestUrl]; |
| 135 | _turnClient = [[ARDCEODTURNClient alloc] initWithURL:turnRequestURL]; |
| 136 | [self configure]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 137 | } |
| 138 | return self; |
| 139 | } |
| 140 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 141 | // TODO(tkchin): Provide signaling channel factory interface so we can recreate |
| 142 | // channel if we need to on network failure. Also, make this the default public |
| 143 | // constructor. |
| 144 | - (instancetype)initWithRoomServerClient:(id<ARDRoomServerClient>)rsClient |
| 145 | signalingChannel:(id<ARDSignalingChannel>)channel |
| 146 | turnClient:(id<ARDTURNClient>)turnClient |
| 147 | delegate:(id<ARDAppClientDelegate>)delegate { |
| 148 | NSParameterAssert(rsClient); |
| 149 | NSParameterAssert(channel); |
| 150 | NSParameterAssert(turnClient); |
| 151 | if (self = [super init]) { |
| 152 | _roomServerClient = rsClient; |
| 153 | _channel = channel; |
| 154 | _turnClient = turnClient; |
| 155 | _delegate = delegate; |
| 156 | [self configure]; |
| 157 | } |
| 158 | return self; |
| 159 | } |
| 160 | |
| 161 | - (void)configure { |
| 162 | _factory = [[RTCPeerConnectionFactory alloc] init]; |
| 163 | _messageQueue = [NSMutableArray array]; |
| 164 | _iceServers = [NSMutableArray arrayWithObject:[self defaultSTUNServer]]; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 165 | _fileLogger = [[RTCFileLogger alloc] init]; |
| 166 | [_fileLogger start]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 167 | } |
| 168 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 169 | - (void)dealloc { |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 170 | self.shouldGetStats = NO; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 171 | [self disconnect]; |
| 172 | } |
| 173 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 174 | - (void)setShouldGetStats:(BOOL)shouldGetStats { |
| 175 | if (_shouldGetStats == shouldGetStats) { |
| 176 | return; |
| 177 | } |
| 178 | if (shouldGetStats) { |
| 179 | __weak ARDAppClient *weakSelf = self; |
| 180 | _statsTimer = [[ARDTimerProxy alloc] initWithInterval:1 |
| 181 | repeats:YES |
| 182 | timerHandler:^{ |
| 183 | ARDAppClient *strongSelf = weakSelf; |
| 184 | [strongSelf.peerConnection getStatsWithDelegate:strongSelf |
| 185 | mediaStreamTrack:nil |
| 186 | statsOutputLevel:RTCStatsOutputLevelDebug]; |
| 187 | }]; |
| 188 | } else { |
| 189 | [_statsTimer invalidate]; |
| 190 | _statsTimer = nil; |
| 191 | } |
| 192 | _shouldGetStats = shouldGetStats; |
| 193 | } |
| 194 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 195 | - (void)setState:(ARDAppClientState)state { |
| 196 | if (_state == state) { |
| 197 | return; |
| 198 | } |
| 199 | _state = state; |
| 200 | [_delegate appClient:self didChangeState:_state]; |
| 201 | } |
| 202 | |
| 203 | - (void)connectToRoomWithId:(NSString *)roomId |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 204 | isLoopback:(BOOL)isLoopback |
| 205 | isAudioOnly:(BOOL)isAudioOnly { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 206 | NSParameterAssert(roomId.length); |
| 207 | NSParameterAssert(_state == kARDAppClientStateDisconnected); |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 208 | _isLoopback = isLoopback; |
| 209 | _isAudioOnly = isAudioOnly; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 210 | self.state = kARDAppClientStateConnecting; |
| 211 | |
| 212 | // Request TURN. |
| 213 | __weak ARDAppClient *weakSelf = self; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 214 | [_turnClient requestServersWithCompletionHandler:^(NSArray *turnServers, |
| 215 | NSError *error) { |
| 216 | if (error) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 217 | RTCLogError("Error retrieving TURN servers: %@", |
| 218 | error.localizedDescription); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 219 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 220 | ARDAppClient *strongSelf = weakSelf; |
| 221 | [strongSelf.iceServers addObjectsFromArray:turnServers]; |
| 222 | strongSelf.isTurnComplete = YES; |
| 223 | [strongSelf startSignalingIfReady]; |
| 224 | }]; |
jiayl@webrtc.org | 27f5317 | 2014-12-31 00:26:20 +0000 | [diff] [blame] | 225 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 226 | // Join room on room server. |
| 227 | [_roomServerClient joinRoomWithRoomId:roomId |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 228 | isLoopback:isLoopback |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 229 | completionHandler:^(ARDJoinResponse *response, NSError *error) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 230 | ARDAppClient *strongSelf = weakSelf; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 231 | if (error) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 232 | [strongSelf.delegate appClient:strongSelf didError:error]; |
| 233 | return; |
| 234 | } |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 235 | NSError *joinError = |
| 236 | [[strongSelf class] errorForJoinResultType:response.result]; |
| 237 | if (joinError) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 238 | RTCLogError(@"Failed to join room:%@ on room server.", roomId); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 239 | [strongSelf disconnect]; |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 240 | [strongSelf.delegate appClient:strongSelf didError:joinError]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 241 | return; |
| 242 | } |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 243 | RTCLog(@"Joined room:%@ on room server.", roomId); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 244 | strongSelf.roomId = response.roomId; |
| 245 | strongSelf.clientId = response.clientId; |
| 246 | strongSelf.isInitiator = response.isInitiator; |
| 247 | for (ARDSignalingMessage *message in response.messages) { |
| 248 | if (message.type == kARDSignalingMessageTypeOffer || |
| 249 | message.type == kARDSignalingMessageTypeAnswer) { |
| 250 | strongSelf.hasReceivedSdp = YES; |
| 251 | [strongSelf.messageQueue insertObject:message atIndex:0]; |
| 252 | } else { |
| 253 | [strongSelf.messageQueue addObject:message]; |
| 254 | } |
| 255 | } |
| 256 | strongSelf.webSocketURL = response.webSocketURL; |
| 257 | strongSelf.webSocketRestURL = response.webSocketRestURL; |
| 258 | [strongSelf registerWithColliderIfReady]; |
| 259 | [strongSelf startSignalingIfReady]; |
| 260 | }]; |
| 261 | } |
| 262 | |
| 263 | - (void)disconnect { |
| 264 | if (_state == kARDAppClientStateDisconnected) { |
| 265 | return; |
| 266 | } |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 267 | if (self.hasJoinedRoomServerRoom) { |
| 268 | [_roomServerClient leaveRoomWithRoomId:_roomId |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 269 | clientId:_clientId |
| 270 | completionHandler:nil]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 271 | } |
| 272 | if (_channel) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 273 | if (_channel.state == kARDSignalingChannelStateRegistered) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 274 | // Tell the other client we're hanging up. |
| 275 | ARDByeMessage *byeMessage = [[ARDByeMessage alloc] init]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 276 | [_channel sendMessage:byeMessage]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 277 | } |
| 278 | // Disconnect from collider. |
| 279 | _channel = nil; |
| 280 | } |
| 281 | _clientId = nil; |
| 282 | _roomId = nil; |
| 283 | _isInitiator = NO; |
| 284 | _hasReceivedSdp = NO; |
| 285 | _messageQueue = [NSMutableArray array]; |
| 286 | _peerConnection = nil; |
| 287 | self.state = kARDAppClientStateDisconnected; |
| 288 | } |
| 289 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 290 | #pragma mark - ARDSignalingChannelDelegate |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 291 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 292 | - (void)channel:(id<ARDSignalingChannel>)channel |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 293 | didReceiveMessage:(ARDSignalingMessage *)message { |
| 294 | switch (message.type) { |
| 295 | case kARDSignalingMessageTypeOffer: |
| 296 | case kARDSignalingMessageTypeAnswer: |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 297 | // Offers and answers must be processed before any other message, so we |
| 298 | // place them at the front of the queue. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 299 | _hasReceivedSdp = YES; |
| 300 | [_messageQueue insertObject:message atIndex:0]; |
| 301 | break; |
| 302 | case kARDSignalingMessageTypeCandidate: |
| 303 | [_messageQueue addObject:message]; |
| 304 | break; |
| 305 | case kARDSignalingMessageTypeBye: |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 306 | // Disconnects can be processed immediately. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 307 | [self processSignalingMessage:message]; |
| 308 | return; |
| 309 | } |
| 310 | [self drainMessageQueueIfReady]; |
| 311 | } |
| 312 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 313 | - (void)channel:(id<ARDSignalingChannel>)channel |
| 314 | didChangeState:(ARDSignalingChannelState)state { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 315 | switch (state) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 316 | case kARDSignalingChannelStateOpen: |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 317 | break; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 318 | case kARDSignalingChannelStateRegistered: |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 319 | break; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 320 | case kARDSignalingChannelStateClosed: |
| 321 | case kARDSignalingChannelStateError: |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 322 | // TODO(tkchin): reconnection scenarios. Right now we just disconnect |
| 323 | // completely if the websocket connection fails. |
| 324 | [self disconnect]; |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | #pragma mark - RTCPeerConnectionDelegate |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 330 | // Callbacks for this delegate occur on non-main thread and need to be |
| 331 | // dispatched back to main queue as needed. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 332 | |
| 333 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 334 | signalingStateChanged:(RTCSignalingState)stateChanged { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 335 | RTCLog(@"Signaling state changed: %d", stateChanged); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 339 | addedStream:(RTCMediaStream *)stream { |
| 340 | dispatch_async(dispatch_get_main_queue(), ^{ |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 341 | RTCLog(@"Received %lu video tracks and %lu audio tracks", |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 342 | (unsigned long)stream.videoTracks.count, |
| 343 | (unsigned long)stream.audioTracks.count); |
| 344 | if (stream.videoTracks.count) { |
| 345 | RTCVideoTrack *videoTrack = stream.videoTracks[0]; |
| 346 | [_delegate appClient:self didReceiveRemoteVideoTrack:videoTrack]; |
| 347 | } |
| 348 | }); |
| 349 | } |
| 350 | |
| 351 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 352 | removedStream:(RTCMediaStream *)stream { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 353 | RTCLog(@"Stream was removed."); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | - (void)peerConnectionOnRenegotiationNeeded: |
| 357 | (RTCPeerConnection *)peerConnection { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 358 | RTCLog(@"WARNING: Renegotiation needed but unimplemented."); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 362 | iceConnectionChanged:(RTCICEConnectionState)newState { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 363 | RTCLog(@"ICE state changed: %d", newState); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 364 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 365 | [_delegate appClient:self didChangeConnectionState:newState]; |
| 366 | }); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 370 | iceGatheringChanged:(RTCICEGatheringState)newState { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 371 | RTCLog(@"ICE gathering state changed: %d", newState); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 375 | gotICECandidate:(RTCICECandidate *)candidate { |
| 376 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 377 | ARDICECandidateMessage *message = |
| 378 | [[ARDICECandidateMessage alloc] initWithCandidate:candidate]; |
| 379 | [self sendSignalingMessage:message]; |
| 380 | }); |
| 381 | } |
| 382 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 383 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 384 | didOpenDataChannel:(RTCDataChannel *)dataChannel { |
| 385 | } |
| 386 | |
| 387 | #pragma mark - RTCStatsDelegate |
| 388 | |
| 389 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 390 | didGetStats:(NSArray *)stats { |
| 391 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 392 | [_delegate appClient:self didGetStats:stats]; |
| 393 | }); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | #pragma mark - RTCSessionDescriptionDelegate |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 397 | // Callbacks for this delegate occur on non-main thread and need to be |
| 398 | // dispatched back to main queue as needed. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 399 | |
| 400 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 401 | didCreateSessionDescription:(RTCSessionDescription *)sdp |
| 402 | error:(NSError *)error { |
| 403 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 404 | if (error) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 405 | RTCLogError(@"Failed to create session description. Error: %@", error); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 406 | [self disconnect]; |
| 407 | NSDictionary *userInfo = @{ |
| 408 | NSLocalizedDescriptionKey: @"Failed to create session description.", |
| 409 | }; |
| 410 | NSError *sdpError = |
| 411 | [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 412 | code:kARDAppClientErrorCreateSDP |
| 413 | userInfo:userInfo]; |
| 414 | [_delegate appClient:self didError:sdpError]; |
| 415 | return; |
| 416 | } |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 417 | // Prefer H264 if available. |
| 418 | RTCSessionDescription *sdpPreferringH264 = |
| 419 | [ARDSDPUtils descriptionForDescription:sdp |
| 420 | preferredVideoCodec:@"H264"]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 421 | [_peerConnection setLocalDescriptionWithDelegate:self |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 422 | sessionDescription:sdpPreferringH264]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 423 | ARDSessionDescriptionMessage *message = |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 424 | [[ARDSessionDescriptionMessage alloc] |
| 425 | initWithDescription:sdpPreferringH264]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 426 | [self sendSignalingMessage:message]; |
| 427 | }); |
| 428 | } |
| 429 | |
| 430 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 431 | didSetSessionDescriptionWithError:(NSError *)error { |
| 432 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 433 | if (error) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 434 | RTCLogError(@"Failed to set session description. Error: %@", error); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 435 | [self disconnect]; |
| 436 | NSDictionary *userInfo = @{ |
| 437 | NSLocalizedDescriptionKey: @"Failed to set session description.", |
| 438 | }; |
| 439 | NSError *sdpError = |
| 440 | [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 441 | code:kARDAppClientErrorSetSDP |
| 442 | userInfo:userInfo]; |
| 443 | [_delegate appClient:self didError:sdpError]; |
| 444 | return; |
| 445 | } |
| 446 | // If we're answering and we've just set the remote offer we need to create |
| 447 | // an answer and set the local description. |
| 448 | if (!_isInitiator && !_peerConnection.localDescription) { |
| 449 | RTCMediaConstraints *constraints = [self defaultAnswerConstraints]; |
| 450 | [_peerConnection createAnswerWithDelegate:self |
| 451 | constraints:constraints]; |
| 452 | |
| 453 | } |
| 454 | }); |
| 455 | } |
| 456 | |
| 457 | #pragma mark - Private |
| 458 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 459 | - (BOOL)hasJoinedRoomServerRoom { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 460 | return _clientId.length; |
| 461 | } |
| 462 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 463 | // Begins the peer connection connection process if we have both joined a room |
| 464 | // on the room server and tried to obtain a TURN server. Otherwise does nothing. |
| 465 | // A peer connection object will be created with a stream that contains local |
| 466 | // audio and video capture. If this client is the caller, an offer is created as |
| 467 | // well, otherwise the client will wait for an offer to arrive. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 468 | - (void)startSignalingIfReady { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 469 | if (!_isTurnComplete || !self.hasJoinedRoomServerRoom) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 470 | return; |
| 471 | } |
| 472 | self.state = kARDAppClientStateConnected; |
| 473 | |
| 474 | // Create peer connection. |
| 475 | RTCMediaConstraints *constraints = [self defaultPeerConnectionConstraints]; |
Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 476 | RTCConfiguration *config = [[RTCConfiguration alloc] init]; |
| 477 | config.iceServers = _iceServers; |
| 478 | _peerConnection = [_factory peerConnectionWithConfiguration:config |
| 479 | constraints:constraints |
| 480 | delegate:self]; |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 481 | // Create AV media stream and add it to the peer connection. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 482 | RTCMediaStream *localStream = [self createLocalMediaStream]; |
| 483 | [_peerConnection addStream:localStream]; |
| 484 | if (_isInitiator) { |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 485 | // Send offer. |
| 486 | [_peerConnection createOfferWithDelegate:self |
| 487 | constraints:[self defaultOfferConstraints]]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 488 | } else { |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 489 | // Check if we've received an offer. |
| 490 | [self drainMessageQueueIfReady]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 494 | // Processes the messages that we've received from the room server and the |
| 495 | // signaling channel. The offer or answer message must be processed before other |
| 496 | // signaling messages, however they can arrive out of order. Hence, this method |
| 497 | // only processes pending messages if there is a peer connection object and |
| 498 | // if we have received either an offer or answer. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 499 | - (void)drainMessageQueueIfReady { |
| 500 | if (!_peerConnection || !_hasReceivedSdp) { |
| 501 | return; |
| 502 | } |
| 503 | for (ARDSignalingMessage *message in _messageQueue) { |
| 504 | [self processSignalingMessage:message]; |
| 505 | } |
| 506 | [_messageQueue removeAllObjects]; |
| 507 | } |
| 508 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 509 | // Processes the given signaling message based on its type. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 510 | - (void)processSignalingMessage:(ARDSignalingMessage *)message { |
| 511 | NSParameterAssert(_peerConnection || |
| 512 | message.type == kARDSignalingMessageTypeBye); |
| 513 | switch (message.type) { |
| 514 | case kARDSignalingMessageTypeOffer: |
| 515 | case kARDSignalingMessageTypeAnswer: { |
| 516 | ARDSessionDescriptionMessage *sdpMessage = |
| 517 | (ARDSessionDescriptionMessage *)message; |
| 518 | RTCSessionDescription *description = sdpMessage.sessionDescription; |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 519 | // Prefer H264 if available. |
| 520 | RTCSessionDescription *sdpPreferringH264 = |
| 521 | [ARDSDPUtils descriptionForDescription:description |
| 522 | preferredVideoCodec:@"H264"]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 523 | [_peerConnection setRemoteDescriptionWithDelegate:self |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 524 | sessionDescription:sdpPreferringH264]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 525 | break; |
| 526 | } |
| 527 | case kARDSignalingMessageTypeCandidate: { |
| 528 | ARDICECandidateMessage *candidateMessage = |
| 529 | (ARDICECandidateMessage *)message; |
| 530 | [_peerConnection addICECandidate:candidateMessage.candidate]; |
| 531 | break; |
| 532 | } |
| 533 | case kARDSignalingMessageTypeBye: |
| 534 | // Other client disconnected. |
| 535 | // TODO(tkchin): support waiting in room for next client. For now just |
| 536 | // disconnect. |
| 537 | [self disconnect]; |
| 538 | break; |
| 539 | } |
| 540 | } |
| 541 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 542 | // Sends a signaling message to the other client. The caller will send messages |
| 543 | // through the room server, whereas the callee will send messages over the |
| 544 | // signaling channel. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 545 | - (void)sendSignalingMessage:(ARDSignalingMessage *)message { |
| 546 | if (_isInitiator) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 547 | __weak ARDAppClient *weakSelf = self; |
| 548 | [_roomServerClient sendMessage:message |
| 549 | forRoomId:_roomId |
| 550 | clientId:_clientId |
| 551 | completionHandler:^(ARDMessageResponse *response, |
| 552 | NSError *error) { |
| 553 | ARDAppClient *strongSelf = weakSelf; |
| 554 | if (error) { |
| 555 | [strongSelf.delegate appClient:strongSelf didError:error]; |
| 556 | return; |
| 557 | } |
| 558 | NSError *messageError = |
| 559 | [[strongSelf class] errorForMessageResultType:response.result]; |
| 560 | if (messageError) { |
| 561 | [strongSelf.delegate appClient:strongSelf didError:messageError]; |
| 562 | return; |
| 563 | } |
| 564 | }]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 565 | } else { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 566 | [_channel sendMessage:message]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
| 570 | - (RTCMediaStream *)createLocalMediaStream { |
| 571 | RTCMediaStream* localStream = [_factory mediaStreamWithLabel:@"ARDAMS"]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 572 | RTCVideoTrack* localVideoTrack = [self createLocalVideoTrack]; |
| 573 | if (localVideoTrack) { |
| 574 | [localStream addVideoTrack:localVideoTrack]; |
| 575 | [_delegate appClient:self didReceiveLocalVideoTrack:localVideoTrack]; |
| 576 | } |
| 577 | [localStream addAudioTrack:[_factory audioTrackWithID:@"ARDAMSa0"]]; |
| 578 | return localStream; |
| 579 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 580 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 581 | - (RTCVideoTrack *)createLocalVideoTrack { |
| 582 | RTCVideoTrack* localVideoTrack = nil; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 583 | // The iOS simulator doesn't provide any sort of camera capture |
| 584 | // support or emulation (http://goo.gl/rHAnC1) so don't bother |
| 585 | // trying to open a local stream. |
| 586 | // TODO(tkchin): local video capture for OSX. See |
| 587 | // https://code.google.com/p/webrtc/issues/detail?id=3417. |
| 588 | #if !TARGET_IPHONE_SIMULATOR && TARGET_OS_IPHONE |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 589 | if (!_isAudioOnly) { |
| 590 | RTCMediaConstraints *mediaConstraints = |
| 591 | [self defaultMediaStreamConstraints]; |
| 592 | RTCAVFoundationVideoSource *source = |
| 593 | [[RTCAVFoundationVideoSource alloc] initWithFactory:_factory |
| 594 | constraints:mediaConstraints]; |
| 595 | localVideoTrack = |
| 596 | [[RTCVideoTrack alloc] initWithFactory:_factory |
| 597 | source:source |
| 598 | trackId:@"ARDAMSv0"]; |
| 599 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 600 | #endif |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 601 | return localVideoTrack; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 602 | } |
| 603 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 604 | #pragma mark - Collider methods |
| 605 | |
| 606 | - (void)registerWithColliderIfReady { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 607 | if (!self.hasJoinedRoomServerRoom) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 608 | return; |
| 609 | } |
| 610 | // Open WebSocket connection. |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 611 | if (!_channel) { |
| 612 | _channel = |
| 613 | [[ARDWebSocketChannel alloc] initWithURL:_websocketURL |
| 614 | restURL:_websocketRestURL |
| 615 | delegate:self]; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 616 | if (_isLoopback) { |
| 617 | _loopbackChannel = |
| 618 | [[ARDLoopbackWebSocketChannel alloc] initWithURL:_websocketURL |
| 619 | restURL:_websocketRestURL]; |
| 620 | } |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 621 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 622 | [_channel registerForRoomId:_roomId clientId:_clientId]; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 623 | if (_isLoopback) { |
| 624 | [_loopbackChannel registerForRoomId:_roomId clientId:@"LOOPBACK_CLIENT_ID"]; |
| 625 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 626 | } |
| 627 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 628 | #pragma mark - Defaults |
| 629 | |
| 630 | - (RTCMediaConstraints *)defaultMediaStreamConstraints { |
| 631 | RTCMediaConstraints* constraints = |
| 632 | [[RTCMediaConstraints alloc] |
| 633 | initWithMandatoryConstraints:nil |
| 634 | optionalConstraints:nil]; |
| 635 | return constraints; |
| 636 | } |
| 637 | |
| 638 | - (RTCMediaConstraints *)defaultAnswerConstraints { |
| 639 | return [self defaultOfferConstraints]; |
| 640 | } |
| 641 | |
| 642 | - (RTCMediaConstraints *)defaultOfferConstraints { |
| 643 | NSArray *mandatoryConstraints = @[ |
| 644 | [[RTCPair alloc] initWithKey:@"OfferToReceiveAudio" value:@"true"], |
| 645 | [[RTCPair alloc] initWithKey:@"OfferToReceiveVideo" value:@"true"] |
| 646 | ]; |
| 647 | RTCMediaConstraints* constraints = |
| 648 | [[RTCMediaConstraints alloc] |
| 649 | initWithMandatoryConstraints:mandatoryConstraints |
| 650 | optionalConstraints:nil]; |
| 651 | return constraints; |
| 652 | } |
| 653 | |
| 654 | - (RTCMediaConstraints *)defaultPeerConnectionConstraints { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 655 | if (_defaultPeerConnectionConstraints) { |
| 656 | return _defaultPeerConnectionConstraints; |
| 657 | } |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 658 | NSString *value = _isLoopback ? @"false" : @"true"; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 659 | NSArray *optionalConstraints = @[ |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 660 | [[RTCPair alloc] initWithKey:@"DtlsSrtpKeyAgreement" value:value] |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 661 | ]; |
| 662 | RTCMediaConstraints* constraints = |
| 663 | [[RTCMediaConstraints alloc] |
| 664 | initWithMandatoryConstraints:nil |
| 665 | optionalConstraints:optionalConstraints]; |
| 666 | return constraints; |
| 667 | } |
| 668 | |
| 669 | - (RTCICEServer *)defaultSTUNServer { |
| 670 | NSURL *defaultSTUNServerURL = [NSURL URLWithString:kARDDefaultSTUNServerUrl]; |
| 671 | return [[RTCICEServer alloc] initWithURI:defaultSTUNServerURL |
| 672 | username:@"" |
| 673 | password:@""]; |
| 674 | } |
| 675 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 676 | #pragma mark - Errors |
| 677 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 678 | + (NSError *)errorForJoinResultType:(ARDJoinResultType)resultType { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 679 | NSError *error = nil; |
| 680 | switch (resultType) { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 681 | case kARDJoinResultTypeSuccess: |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 682 | break; |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 683 | case kARDJoinResultTypeUnknown: { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 684 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 685 | code:kARDAppClientErrorUnknown |
| 686 | userInfo:@{ |
| 687 | NSLocalizedDescriptionKey: @"Unknown error.", |
| 688 | }]; |
| 689 | break; |
| 690 | } |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 691 | case kARDJoinResultTypeFull: { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 692 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 693 | code:kARDAppClientErrorRoomFull |
| 694 | userInfo:@{ |
| 695 | NSLocalizedDescriptionKey: @"Room is full.", |
| 696 | }]; |
| 697 | break; |
| 698 | } |
| 699 | } |
| 700 | return error; |
| 701 | } |
| 702 | |
| 703 | + (NSError *)errorForMessageResultType:(ARDMessageResultType)resultType { |
| 704 | NSError *error = nil; |
| 705 | switch (resultType) { |
| 706 | case kARDMessageResultTypeSuccess: |
| 707 | break; |
| 708 | case kARDMessageResultTypeUnknown: |
| 709 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 710 | code:kARDAppClientErrorUnknown |
| 711 | userInfo:@{ |
| 712 | NSLocalizedDescriptionKey: @"Unknown error.", |
| 713 | }]; |
| 714 | break; |
| 715 | case kARDMessageResultTypeInvalidClient: |
| 716 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 717 | code:kARDAppClientErrorInvalidClient |
| 718 | userInfo:@{ |
| 719 | NSLocalizedDescriptionKey: @"Invalid client.", |
| 720 | }]; |
| 721 | break; |
| 722 | case kARDMessageResultTypeInvalidRoom: |
| 723 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 724 | code:kARDAppClientErrorInvalidRoom |
| 725 | userInfo:@{ |
| 726 | NSLocalizedDescriptionKey: @"Invalid room.", |
| 727 | }]; |
| 728 | break; |
| 729 | } |
| 730 | return error; |
| 731 | } |
| 732 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 733 | @end |