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