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