tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2014 Google Inc. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 28 | #import "ARDAppClient+Internal.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 29 | |
| 30 | #import <AVFoundation/AVFoundation.h> |
| 31 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 32 | #import "ARDAppEngineClient.h" |
| 33 | #import "ARDCEODTURNClient.h" |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 34 | #import "ARDJoinResponse.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 35 | #import "ARDMessageResponse.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 36 | #import "ARDSignalingMessage.h" |
| 37 | #import "ARDUtilities.h" |
| 38 | #import "ARDWebSocketChannel.h" |
| 39 | #import "RTCICECandidate+JSON.h" |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 40 | #import "RTCICEServer.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 41 | #import "RTCMediaConstraints.h" |
| 42 | #import "RTCMediaStream.h" |
| 43 | #import "RTCPair.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 44 | #import "RTCSessionDescription+JSON.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 45 | #import "RTCVideoCapturer.h" |
| 46 | #import "RTCVideoTrack.h" |
| 47 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 48 | static NSString * const kARDDefaultSTUNServerUrl = |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 49 | @"stun:stun.l.google.com:19302"; |
| 50 | // TODO(tkchin): figure out a better username for CEOD statistics. |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 51 | static NSString * const kARDTurnRequestUrl = |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 52 | @"https://computeengineondemand.appspot.com" |
| 53 | @"/turn?username=iapprtc&key=4080218913"; |
| 54 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 55 | static NSString * const kARDAppClientErrorDomain = @"ARDAppClient"; |
| 56 | static NSInteger const kARDAppClientErrorUnknown = -1; |
| 57 | static NSInteger const kARDAppClientErrorRoomFull = -2; |
| 58 | static NSInteger const kARDAppClientErrorCreateSDP = -3; |
| 59 | static NSInteger const kARDAppClientErrorSetSDP = -4; |
| 60 | static NSInteger const kARDAppClientErrorInvalidClient = -5; |
| 61 | static NSInteger const kARDAppClientErrorInvalidRoom = -6; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 62 | |
| 63 | @implementation ARDAppClient |
| 64 | |
| 65 | @synthesize delegate = _delegate; |
| 66 | @synthesize state = _state; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 67 | @synthesize roomServerClient = _roomServerClient; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 68 | @synthesize channel = _channel; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 69 | @synthesize turnClient = _turnClient; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 70 | @synthesize peerConnection = _peerConnection; |
| 71 | @synthesize factory = _factory; |
| 72 | @synthesize messageQueue = _messageQueue; |
| 73 | @synthesize isTurnComplete = _isTurnComplete; |
| 74 | @synthesize hasReceivedSdp = _hasReceivedSdp; |
| 75 | @synthesize roomId = _roomId; |
| 76 | @synthesize clientId = _clientId; |
| 77 | @synthesize isInitiator = _isInitiator; |
| 78 | @synthesize iceServers = _iceServers; |
| 79 | @synthesize webSocketURL = _websocketURL; |
| 80 | @synthesize webSocketRestURL = _websocketRestURL; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 81 | @synthesize defaultPeerConnectionConstraints = |
| 82 | _defaultPeerConnectionConstraints; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 83 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 84 | - (instancetype)init { |
| 85 | if (self = [super init]) { |
| 86 | _roomServerClient = [[ARDAppEngineClient alloc] init]; |
| 87 | NSURL *turnRequestURL = [NSURL URLWithString:kARDTurnRequestUrl]; |
| 88 | _turnClient = [[ARDCEODTURNClient alloc] initWithURL:turnRequestURL]; |
| 89 | [self configure]; |
| 90 | } |
| 91 | return self; |
| 92 | } |
| 93 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 94 | - (instancetype)initWithDelegate:(id<ARDAppClientDelegate>)delegate { |
| 95 | if (self = [super init]) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 96 | _roomServerClient = [[ARDAppEngineClient alloc] init]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 97 | _delegate = delegate; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 98 | NSURL *turnRequestURL = [NSURL URLWithString:kARDTurnRequestUrl]; |
| 99 | _turnClient = [[ARDCEODTURNClient alloc] initWithURL:turnRequestURL]; |
| 100 | [self configure]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 101 | } |
| 102 | return self; |
| 103 | } |
| 104 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 105 | // TODO(tkchin): Provide signaling channel factory interface so we can recreate |
| 106 | // channel if we need to on network failure. Also, make this the default public |
| 107 | // constructor. |
| 108 | - (instancetype)initWithRoomServerClient:(id<ARDRoomServerClient>)rsClient |
| 109 | signalingChannel:(id<ARDSignalingChannel>)channel |
| 110 | turnClient:(id<ARDTURNClient>)turnClient |
| 111 | delegate:(id<ARDAppClientDelegate>)delegate { |
| 112 | NSParameterAssert(rsClient); |
| 113 | NSParameterAssert(channel); |
| 114 | NSParameterAssert(turnClient); |
| 115 | if (self = [super init]) { |
| 116 | _roomServerClient = rsClient; |
| 117 | _channel = channel; |
| 118 | _turnClient = turnClient; |
| 119 | _delegate = delegate; |
| 120 | [self configure]; |
| 121 | } |
| 122 | return self; |
| 123 | } |
| 124 | |
| 125 | - (void)configure { |
| 126 | _factory = [[RTCPeerConnectionFactory alloc] init]; |
| 127 | _messageQueue = [NSMutableArray array]; |
| 128 | _iceServers = [NSMutableArray arrayWithObject:[self defaultSTUNServer]]; |
| 129 | } |
| 130 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 131 | - (void)dealloc { |
| 132 | [self disconnect]; |
| 133 | } |
| 134 | |
| 135 | - (void)setState:(ARDAppClientState)state { |
| 136 | if (_state == state) { |
| 137 | return; |
| 138 | } |
| 139 | _state = state; |
| 140 | [_delegate appClient:self didChangeState:_state]; |
| 141 | } |
| 142 | |
| 143 | - (void)connectToRoomWithId:(NSString *)roomId |
| 144 | options:(NSDictionary *)options { |
| 145 | NSParameterAssert(roomId.length); |
| 146 | NSParameterAssert(_state == kARDAppClientStateDisconnected); |
| 147 | self.state = kARDAppClientStateConnecting; |
| 148 | |
| 149 | // Request TURN. |
| 150 | __weak ARDAppClient *weakSelf = self; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 151 | [_turnClient requestServersWithCompletionHandler:^(NSArray *turnServers, |
| 152 | NSError *error) { |
| 153 | if (error) { |
| 154 | NSLog(@"Error retrieving TURN servers: %@", error); |
| 155 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 156 | ARDAppClient *strongSelf = weakSelf; |
| 157 | [strongSelf.iceServers addObjectsFromArray:turnServers]; |
| 158 | strongSelf.isTurnComplete = YES; |
| 159 | [strongSelf startSignalingIfReady]; |
| 160 | }]; |
jiayl@webrtc.org | 27f5317 | 2014-12-31 00:26:20 +0000 | [diff] [blame] | 161 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 162 | // Join room on room server. |
| 163 | [_roomServerClient joinRoomWithRoomId:roomId |
| 164 | completionHandler:^(ARDJoinResponse *response, NSError *error) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 165 | ARDAppClient *strongSelf = weakSelf; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 166 | if (error) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 167 | [strongSelf.delegate appClient:strongSelf didError:error]; |
| 168 | return; |
| 169 | } |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 170 | NSError *joinError = |
| 171 | [[strongSelf class] errorForJoinResultType:response.result]; |
| 172 | if (joinError) { |
| 173 | NSLog(@"Failed to join room:%@ on room server.", roomId); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 174 | [strongSelf disconnect]; |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 175 | [strongSelf.delegate appClient:strongSelf didError:joinError]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 176 | return; |
| 177 | } |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 178 | NSLog(@"Joined room:%@ on room server.", roomId); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 179 | strongSelf.roomId = response.roomId; |
| 180 | strongSelf.clientId = response.clientId; |
| 181 | strongSelf.isInitiator = response.isInitiator; |
| 182 | for (ARDSignalingMessage *message in response.messages) { |
| 183 | if (message.type == kARDSignalingMessageTypeOffer || |
| 184 | message.type == kARDSignalingMessageTypeAnswer) { |
| 185 | strongSelf.hasReceivedSdp = YES; |
| 186 | [strongSelf.messageQueue insertObject:message atIndex:0]; |
| 187 | } else { |
| 188 | [strongSelf.messageQueue addObject:message]; |
| 189 | } |
| 190 | } |
| 191 | strongSelf.webSocketURL = response.webSocketURL; |
| 192 | strongSelf.webSocketRestURL = response.webSocketRestURL; |
| 193 | [strongSelf registerWithColliderIfReady]; |
| 194 | [strongSelf startSignalingIfReady]; |
| 195 | }]; |
| 196 | } |
| 197 | |
| 198 | - (void)disconnect { |
| 199 | if (_state == kARDAppClientStateDisconnected) { |
| 200 | return; |
| 201 | } |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 202 | if (self.hasJoinedRoomServerRoom) { |
| 203 | [_roomServerClient leaveRoomWithRoomId:_roomId |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 204 | clientId:_clientId |
| 205 | completionHandler:nil]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 206 | } |
| 207 | if (_channel) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 208 | if (_channel.state == kARDSignalingChannelStateRegistered) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 209 | // Tell the other client we're hanging up. |
| 210 | ARDByeMessage *byeMessage = [[ARDByeMessage alloc] init]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 211 | [_channel sendMessage:byeMessage]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 212 | } |
| 213 | // Disconnect from collider. |
| 214 | _channel = nil; |
| 215 | } |
| 216 | _clientId = nil; |
| 217 | _roomId = nil; |
| 218 | _isInitiator = NO; |
| 219 | _hasReceivedSdp = NO; |
| 220 | _messageQueue = [NSMutableArray array]; |
| 221 | _peerConnection = nil; |
| 222 | self.state = kARDAppClientStateDisconnected; |
| 223 | } |
| 224 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 225 | #pragma mark - ARDSignalingChannelDelegate |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 226 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 227 | - (void)channel:(id<ARDSignalingChannel>)channel |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 228 | didReceiveMessage:(ARDSignalingMessage *)message { |
| 229 | switch (message.type) { |
| 230 | case kARDSignalingMessageTypeOffer: |
| 231 | case kARDSignalingMessageTypeAnswer: |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 232 | // Offers and answers must be processed before any other message, so we |
| 233 | // place them at the front of the queue. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 234 | _hasReceivedSdp = YES; |
| 235 | [_messageQueue insertObject:message atIndex:0]; |
| 236 | break; |
| 237 | case kARDSignalingMessageTypeCandidate: |
| 238 | [_messageQueue addObject:message]; |
| 239 | break; |
| 240 | case kARDSignalingMessageTypeBye: |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 241 | // Disconnects can be processed immediately. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 242 | [self processSignalingMessage:message]; |
| 243 | return; |
| 244 | } |
| 245 | [self drainMessageQueueIfReady]; |
| 246 | } |
| 247 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 248 | - (void)channel:(id<ARDSignalingChannel>)channel |
| 249 | didChangeState:(ARDSignalingChannelState)state { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 250 | switch (state) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 251 | case kARDSignalingChannelStateOpen: |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 252 | break; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 253 | case kARDSignalingChannelStateRegistered: |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 254 | break; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 255 | case kARDSignalingChannelStateClosed: |
| 256 | case kARDSignalingChannelStateError: |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 257 | // TODO(tkchin): reconnection scenarios. Right now we just disconnect |
| 258 | // completely if the websocket connection fails. |
| 259 | [self disconnect]; |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | #pragma mark - RTCPeerConnectionDelegate |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 265 | // Callbacks for this delegate occur on non-main thread and need to be |
| 266 | // dispatched back to main queue as needed. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 267 | |
| 268 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 269 | signalingStateChanged:(RTCSignalingState)stateChanged { |
| 270 | NSLog(@"Signaling state changed: %d", stateChanged); |
| 271 | } |
| 272 | |
| 273 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 274 | addedStream:(RTCMediaStream *)stream { |
| 275 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 276 | NSLog(@"Received %lu video tracks and %lu audio tracks", |
| 277 | (unsigned long)stream.videoTracks.count, |
| 278 | (unsigned long)stream.audioTracks.count); |
| 279 | if (stream.videoTracks.count) { |
| 280 | RTCVideoTrack *videoTrack = stream.videoTracks[0]; |
| 281 | [_delegate appClient:self didReceiveRemoteVideoTrack:videoTrack]; |
| 282 | } |
| 283 | }); |
| 284 | } |
| 285 | |
| 286 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 287 | removedStream:(RTCMediaStream *)stream { |
| 288 | NSLog(@"Stream was removed."); |
| 289 | } |
| 290 | |
| 291 | - (void)peerConnectionOnRenegotiationNeeded: |
| 292 | (RTCPeerConnection *)peerConnection { |
| 293 | NSLog(@"WARNING: Renegotiation needed but unimplemented."); |
| 294 | } |
| 295 | |
| 296 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 297 | iceConnectionChanged:(RTCICEConnectionState)newState { |
| 298 | NSLog(@"ICE state changed: %d", newState); |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 299 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 300 | [_delegate appClient:self didChangeConnectionState:newState]; |
| 301 | }); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 305 | iceGatheringChanged:(RTCICEGatheringState)newState { |
| 306 | NSLog(@"ICE gathering state changed: %d", newState); |
| 307 | } |
| 308 | |
| 309 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 310 | gotICECandidate:(RTCICECandidate *)candidate { |
| 311 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 312 | ARDICECandidateMessage *message = |
| 313 | [[ARDICECandidateMessage alloc] initWithCandidate:candidate]; |
| 314 | [self sendSignalingMessage:message]; |
| 315 | }); |
| 316 | } |
| 317 | |
| 318 | - (void)peerConnection:(RTCPeerConnection*)peerConnection |
| 319 | didOpenDataChannel:(RTCDataChannel*)dataChannel { |
| 320 | } |
| 321 | |
| 322 | #pragma mark - RTCSessionDescriptionDelegate |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 323 | // Callbacks for this delegate occur on non-main thread and need to be |
| 324 | // dispatched back to main queue as needed. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 325 | |
| 326 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 327 | didCreateSessionDescription:(RTCSessionDescription *)sdp |
| 328 | error:(NSError *)error { |
| 329 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 330 | if (error) { |
| 331 | NSLog(@"Failed to create session description. Error: %@", error); |
| 332 | [self disconnect]; |
| 333 | NSDictionary *userInfo = @{ |
| 334 | NSLocalizedDescriptionKey: @"Failed to create session description.", |
| 335 | }; |
| 336 | NSError *sdpError = |
| 337 | [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 338 | code:kARDAppClientErrorCreateSDP |
| 339 | userInfo:userInfo]; |
| 340 | [_delegate appClient:self didError:sdpError]; |
| 341 | return; |
| 342 | } |
| 343 | [_peerConnection setLocalDescriptionWithDelegate:self |
| 344 | sessionDescription:sdp]; |
| 345 | ARDSessionDescriptionMessage *message = |
| 346 | [[ARDSessionDescriptionMessage alloc] initWithDescription:sdp]; |
| 347 | [self sendSignalingMessage:message]; |
| 348 | }); |
| 349 | } |
| 350 | |
| 351 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 352 | didSetSessionDescriptionWithError:(NSError *)error { |
| 353 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 354 | if (error) { |
| 355 | NSLog(@"Failed to set session description. Error: %@", error); |
| 356 | [self disconnect]; |
| 357 | NSDictionary *userInfo = @{ |
| 358 | NSLocalizedDescriptionKey: @"Failed to set session description.", |
| 359 | }; |
| 360 | NSError *sdpError = |
| 361 | [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 362 | code:kARDAppClientErrorSetSDP |
| 363 | userInfo:userInfo]; |
| 364 | [_delegate appClient:self didError:sdpError]; |
| 365 | return; |
| 366 | } |
| 367 | // If we're answering and we've just set the remote offer we need to create |
| 368 | // an answer and set the local description. |
| 369 | if (!_isInitiator && !_peerConnection.localDescription) { |
| 370 | RTCMediaConstraints *constraints = [self defaultAnswerConstraints]; |
| 371 | [_peerConnection createAnswerWithDelegate:self |
| 372 | constraints:constraints]; |
| 373 | |
| 374 | } |
| 375 | }); |
| 376 | } |
| 377 | |
| 378 | #pragma mark - Private |
| 379 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 380 | - (BOOL)hasJoinedRoomServerRoom { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 381 | return _clientId.length; |
| 382 | } |
| 383 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 384 | // Begins the peer connection connection process if we have both joined a room |
| 385 | // on the room server and tried to obtain a TURN server. Otherwise does nothing. |
| 386 | // A peer connection object will be created with a stream that contains local |
| 387 | // audio and video capture. If this client is the caller, an offer is created as |
| 388 | // well, otherwise the client will wait for an offer to arrive. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 389 | - (void)startSignalingIfReady { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 390 | if (!_isTurnComplete || !self.hasJoinedRoomServerRoom) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 391 | return; |
| 392 | } |
| 393 | self.state = kARDAppClientStateConnected; |
| 394 | |
| 395 | // Create peer connection. |
| 396 | RTCMediaConstraints *constraints = [self defaultPeerConnectionConstraints]; |
| 397 | _peerConnection = [_factory peerConnectionWithICEServers:_iceServers |
| 398 | constraints:constraints |
| 399 | delegate:self]; |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 400 | // Create AV media stream and add it to the peer connection. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 401 | RTCMediaStream *localStream = [self createLocalMediaStream]; |
| 402 | [_peerConnection addStream:localStream]; |
| 403 | if (_isInitiator) { |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 404 | // Send offer. |
| 405 | [_peerConnection createOfferWithDelegate:self |
| 406 | constraints:[self defaultOfferConstraints]]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 407 | } else { |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 408 | // Check if we've received an offer. |
| 409 | [self drainMessageQueueIfReady]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 413 | // Processes the messages that we've received from the room server and the |
| 414 | // signaling channel. The offer or answer message must be processed before other |
| 415 | // signaling messages, however they can arrive out of order. Hence, this method |
| 416 | // only processes pending messages if there is a peer connection object and |
| 417 | // if we have received either an offer or answer. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 418 | - (void)drainMessageQueueIfReady { |
| 419 | if (!_peerConnection || !_hasReceivedSdp) { |
| 420 | return; |
| 421 | } |
| 422 | for (ARDSignalingMessage *message in _messageQueue) { |
| 423 | [self processSignalingMessage:message]; |
| 424 | } |
| 425 | [_messageQueue removeAllObjects]; |
| 426 | } |
| 427 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 428 | // Processes the given signaling message based on its type. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 429 | - (void)processSignalingMessage:(ARDSignalingMessage *)message { |
| 430 | NSParameterAssert(_peerConnection || |
| 431 | message.type == kARDSignalingMessageTypeBye); |
| 432 | switch (message.type) { |
| 433 | case kARDSignalingMessageTypeOffer: |
| 434 | case kARDSignalingMessageTypeAnswer: { |
| 435 | ARDSessionDescriptionMessage *sdpMessage = |
| 436 | (ARDSessionDescriptionMessage *)message; |
| 437 | RTCSessionDescription *description = sdpMessage.sessionDescription; |
| 438 | [_peerConnection setRemoteDescriptionWithDelegate:self |
| 439 | sessionDescription:description]; |
| 440 | break; |
| 441 | } |
| 442 | case kARDSignalingMessageTypeCandidate: { |
| 443 | ARDICECandidateMessage *candidateMessage = |
| 444 | (ARDICECandidateMessage *)message; |
| 445 | [_peerConnection addICECandidate:candidateMessage.candidate]; |
| 446 | break; |
| 447 | } |
| 448 | case kARDSignalingMessageTypeBye: |
| 449 | // Other client disconnected. |
| 450 | // TODO(tkchin): support waiting in room for next client. For now just |
| 451 | // disconnect. |
| 452 | [self disconnect]; |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 457 | // Sends a signaling message to the other client. The caller will send messages |
| 458 | // through the room server, whereas the callee will send messages over the |
| 459 | // signaling channel. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 460 | - (void)sendSignalingMessage:(ARDSignalingMessage *)message { |
| 461 | if (_isInitiator) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 462 | __weak ARDAppClient *weakSelf = self; |
| 463 | [_roomServerClient sendMessage:message |
| 464 | forRoomId:_roomId |
| 465 | clientId:_clientId |
| 466 | completionHandler:^(ARDMessageResponse *response, |
| 467 | NSError *error) { |
| 468 | ARDAppClient *strongSelf = weakSelf; |
| 469 | if (error) { |
| 470 | [strongSelf.delegate appClient:strongSelf didError:error]; |
| 471 | return; |
| 472 | } |
| 473 | NSError *messageError = |
| 474 | [[strongSelf class] errorForMessageResultType:response.result]; |
| 475 | if (messageError) { |
| 476 | [strongSelf.delegate appClient:strongSelf didError:messageError]; |
| 477 | return; |
| 478 | } |
| 479 | }]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 480 | } else { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 481 | [_channel sendMessage:message]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | - (RTCMediaStream *)createLocalMediaStream { |
| 486 | RTCMediaStream* localStream = [_factory mediaStreamWithLabel:@"ARDAMS"]; |
| 487 | RTCVideoTrack* localVideoTrack = nil; |
| 488 | |
| 489 | // The iOS simulator doesn't provide any sort of camera capture |
| 490 | // support or emulation (http://goo.gl/rHAnC1) so don't bother |
| 491 | // trying to open a local stream. |
| 492 | // TODO(tkchin): local video capture for OSX. See |
| 493 | // https://code.google.com/p/webrtc/issues/detail?id=3417. |
| 494 | #if !TARGET_IPHONE_SIMULATOR && TARGET_OS_IPHONE |
| 495 | NSString *cameraID = nil; |
| 496 | for (AVCaptureDevice *captureDevice in |
| 497 | [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) { |
| 498 | if (captureDevice.position == AVCaptureDevicePositionFront) { |
| 499 | cameraID = [captureDevice localizedName]; |
| 500 | break; |
| 501 | } |
| 502 | } |
| 503 | NSAssert(cameraID, @"Unable to get the front camera id"); |
| 504 | |
| 505 | RTCVideoCapturer *capturer = |
| 506 | [RTCVideoCapturer capturerWithDeviceName:cameraID]; |
| 507 | RTCMediaConstraints *mediaConstraints = [self defaultMediaStreamConstraints]; |
| 508 | RTCVideoSource *videoSource = |
| 509 | [_factory videoSourceWithCapturer:capturer |
| 510 | constraints:mediaConstraints]; |
| 511 | localVideoTrack = |
| 512 | [_factory videoTrackWithID:@"ARDAMSv0" source:videoSource]; |
| 513 | if (localVideoTrack) { |
| 514 | [localStream addVideoTrack:localVideoTrack]; |
| 515 | } |
| 516 | [_delegate appClient:self didReceiveLocalVideoTrack:localVideoTrack]; |
| 517 | #endif |
| 518 | [localStream addAudioTrack:[_factory audioTrackWithID:@"ARDAMSa0"]]; |
| 519 | return localStream; |
| 520 | } |
| 521 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 522 | #pragma mark - Collider methods |
| 523 | |
| 524 | - (void)registerWithColliderIfReady { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 525 | if (!self.hasJoinedRoomServerRoom) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 526 | return; |
| 527 | } |
| 528 | // Open WebSocket connection. |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 529 | if (!_channel) { |
| 530 | _channel = |
| 531 | [[ARDWebSocketChannel alloc] initWithURL:_websocketURL |
| 532 | restURL:_websocketRestURL |
| 533 | delegate:self]; |
| 534 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 535 | [_channel registerForRoomId:_roomId clientId:_clientId]; |
| 536 | } |
| 537 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 538 | #pragma mark - Defaults |
| 539 | |
| 540 | - (RTCMediaConstraints *)defaultMediaStreamConstraints { |
| 541 | RTCMediaConstraints* constraints = |
| 542 | [[RTCMediaConstraints alloc] |
| 543 | initWithMandatoryConstraints:nil |
| 544 | optionalConstraints:nil]; |
| 545 | return constraints; |
| 546 | } |
| 547 | |
| 548 | - (RTCMediaConstraints *)defaultAnswerConstraints { |
| 549 | return [self defaultOfferConstraints]; |
| 550 | } |
| 551 | |
| 552 | - (RTCMediaConstraints *)defaultOfferConstraints { |
| 553 | NSArray *mandatoryConstraints = @[ |
| 554 | [[RTCPair alloc] initWithKey:@"OfferToReceiveAudio" value:@"true"], |
| 555 | [[RTCPair alloc] initWithKey:@"OfferToReceiveVideo" value:@"true"] |
| 556 | ]; |
| 557 | RTCMediaConstraints* constraints = |
| 558 | [[RTCMediaConstraints alloc] |
| 559 | initWithMandatoryConstraints:mandatoryConstraints |
| 560 | optionalConstraints:nil]; |
| 561 | return constraints; |
| 562 | } |
| 563 | |
| 564 | - (RTCMediaConstraints *)defaultPeerConnectionConstraints { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 565 | if (_defaultPeerConnectionConstraints) { |
| 566 | return _defaultPeerConnectionConstraints; |
| 567 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 568 | NSArray *optionalConstraints = @[ |
| 569 | [[RTCPair alloc] initWithKey:@"DtlsSrtpKeyAgreement" value:@"true"] |
| 570 | ]; |
| 571 | RTCMediaConstraints* constraints = |
| 572 | [[RTCMediaConstraints alloc] |
| 573 | initWithMandatoryConstraints:nil |
| 574 | optionalConstraints:optionalConstraints]; |
| 575 | return constraints; |
| 576 | } |
| 577 | |
| 578 | - (RTCICEServer *)defaultSTUNServer { |
| 579 | NSURL *defaultSTUNServerURL = [NSURL URLWithString:kARDDefaultSTUNServerUrl]; |
| 580 | return [[RTCICEServer alloc] initWithURI:defaultSTUNServerURL |
| 581 | username:@"" |
| 582 | password:@""]; |
| 583 | } |
| 584 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 585 | #pragma mark - Errors |
| 586 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 587 | + (NSError *)errorForJoinResultType:(ARDJoinResultType)resultType { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 588 | NSError *error = nil; |
| 589 | switch (resultType) { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 590 | case kARDJoinResultTypeSuccess: |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 591 | break; |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 592 | case kARDJoinResultTypeUnknown: { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 593 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 594 | code:kARDAppClientErrorUnknown |
| 595 | userInfo:@{ |
| 596 | NSLocalizedDescriptionKey: @"Unknown error.", |
| 597 | }]; |
| 598 | break; |
| 599 | } |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 600 | case kARDJoinResultTypeFull: { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 601 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 602 | code:kARDAppClientErrorRoomFull |
| 603 | userInfo:@{ |
| 604 | NSLocalizedDescriptionKey: @"Room is full.", |
| 605 | }]; |
| 606 | break; |
| 607 | } |
| 608 | } |
| 609 | return error; |
| 610 | } |
| 611 | |
| 612 | + (NSError *)errorForMessageResultType:(ARDMessageResultType)resultType { |
| 613 | NSError *error = nil; |
| 614 | switch (resultType) { |
| 615 | case kARDMessageResultTypeSuccess: |
| 616 | break; |
| 617 | case kARDMessageResultTypeUnknown: |
| 618 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 619 | code:kARDAppClientErrorUnknown |
| 620 | userInfo:@{ |
| 621 | NSLocalizedDescriptionKey: @"Unknown error.", |
| 622 | }]; |
| 623 | break; |
| 624 | case kARDMessageResultTypeInvalidClient: |
| 625 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 626 | code:kARDAppClientErrorInvalidClient |
| 627 | userInfo:@{ |
| 628 | NSLocalizedDescriptionKey: @"Invalid client.", |
| 629 | }]; |
| 630 | break; |
| 631 | case kARDMessageResultTypeInvalidRoom: |
| 632 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 633 | code:kARDAppClientErrorInvalidRoom |
| 634 | userInfo:@{ |
| 635 | NSLocalizedDescriptionKey: @"Invalid room.", |
| 636 | }]; |
| 637 | break; |
| 638 | } |
| 639 | return error; |
| 640 | } |
| 641 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 642 | @end |