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" |
Anders Carlsson | 1d4c152 | 2017-10-30 13:07:07 +0100 | [diff] [blame^] | 25 | #import "WebRTC/RTCVideoCodecFactory.h" |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 26 | #import "WebRTC/RTCVideoTrack.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 27 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 28 | #import "ARDAppEngineClient.h" |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 29 | #import "ARDJoinResponse.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 30 | #import "ARDMessageResponse.h" |
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; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 129 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 130 | - (instancetype)init { |
sakal | c4adacf | 2017-03-28 01:22:48 -0700 | [diff] [blame] | 131 | return [self initWithDelegate:nil]; |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 132 | } |
| 133 | |
sakal | c4adacf | 2017-03-28 01:22:48 -0700 | [diff] [blame] | 134 | - (instancetype)initWithDelegate:(id<ARDAppClientDelegate>)delegate { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 135 | if (self = [super init]) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 136 | _roomServerClient = [[ARDAppEngineClient alloc] init]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 137 | _delegate = delegate; |
kthelgason | cc882af | 2017-01-13 05:59:46 -0800 | [diff] [blame] | 138 | NSURL *turnRequestURL = [NSURL URLWithString:kARDIceServerRequestUrl]; |
| 139 | _turnClient = [[ARDTURNClient alloc] initWithURL:turnRequestURL]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 140 | [self configure]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 141 | } |
| 142 | return self; |
| 143 | } |
| 144 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 145 | // TODO(tkchin): Provide signaling channel factory interface so we can recreate |
| 146 | // channel if we need to on network failure. Also, make this the default public |
| 147 | // constructor. |
| 148 | - (instancetype)initWithRoomServerClient:(id<ARDRoomServerClient>)rsClient |
| 149 | signalingChannel:(id<ARDSignalingChannel>)channel |
| 150 | turnClient:(id<ARDTURNClient>)turnClient |
| 151 | delegate:(id<ARDAppClientDelegate>)delegate { |
| 152 | NSParameterAssert(rsClient); |
| 153 | NSParameterAssert(channel); |
| 154 | NSParameterAssert(turnClient); |
| 155 | if (self = [super init]) { |
| 156 | _roomServerClient = rsClient; |
| 157 | _channel = channel; |
| 158 | _turnClient = turnClient; |
| 159 | _delegate = delegate; |
| 160 | [self configure]; |
| 161 | } |
| 162 | return self; |
| 163 | } |
| 164 | |
| 165 | - (void)configure { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 166 | _messageQueue = [NSMutableArray array]; |
kthelgason | cc882af | 2017-01-13 05:59:46 -0800 | [diff] [blame] | 167 | _iceServers = [NSMutableArray array]; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 168 | _fileLogger = [[RTCFileLogger alloc] init]; |
| 169 | [_fileLogger start]; |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 170 | } |
| 171 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 172 | - (void)dealloc { |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 173 | self.shouldGetStats = NO; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 174 | [self disconnect]; |
| 175 | } |
| 176 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 177 | - (void)setShouldGetStats:(BOOL)shouldGetStats { |
| 178 | if (_shouldGetStats == shouldGetStats) { |
| 179 | return; |
| 180 | } |
| 181 | if (shouldGetStats) { |
| 182 | __weak ARDAppClient *weakSelf = self; |
| 183 | _statsTimer = [[ARDTimerProxy alloc] initWithInterval:1 |
| 184 | repeats:YES |
| 185 | timerHandler:^{ |
| 186 | ARDAppClient *strongSelf = weakSelf; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 187 | [strongSelf.peerConnection statsForTrack:nil |
| 188 | statsOutputLevel:RTCStatsOutputLevelDebug |
| 189 | completionHandler:^(NSArray *stats) { |
| 190 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 191 | ARDAppClient *strongSelf = weakSelf; |
| 192 | [strongSelf.delegate appClient:strongSelf didGetStats:stats]; |
| 193 | }); |
| 194 | }]; |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 195 | }]; |
| 196 | } else { |
| 197 | [_statsTimer invalidate]; |
| 198 | _statsTimer = nil; |
| 199 | } |
| 200 | _shouldGetStats = shouldGetStats; |
| 201 | } |
| 202 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 203 | - (void)setState:(ARDAppClientState)state { |
| 204 | if (_state == state) { |
| 205 | return; |
| 206 | } |
| 207 | _state = state; |
| 208 | [_delegate appClient:self didChangeState:_state]; |
| 209 | } |
| 210 | |
| 211 | - (void)connectToRoomWithId:(NSString *)roomId |
sakal | c4adacf | 2017-03-28 01:22:48 -0700 | [diff] [blame] | 212 | settings:(ARDSettingsModel *)settings |
Anders Carlsson | e150058 | 2017-06-15 16:05:13 +0200 | [diff] [blame] | 213 | isLoopback:(BOOL)isLoopback { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 214 | NSParameterAssert(roomId.length); |
| 215 | NSParameterAssert(_state == kARDAppClientStateDisconnected); |
sakal | c4adacf | 2017-03-28 01:22:48 -0700 | [diff] [blame] | 216 | _settings = settings; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 217 | _isLoopback = isLoopback; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 218 | self.state = kARDAppClientStateConnecting; |
| 219 | |
Anders Carlsson | 1d4c152 | 2017-10-30 13:07:07 +0100 | [diff] [blame^] | 220 | RTCDefaultVideoDecoderFactory *decoderFactory = [[RTCDefaultVideoDecoderFactory alloc] init]; |
| 221 | RTCDefaultVideoEncoderFactory *encoderFactory = [[RTCDefaultVideoEncoderFactory alloc] init]; |
Anders Carlsson | 6bf43d2 | 2017-10-16 13:51:43 +0200 | [diff] [blame] | 222 | encoderFactory.preferredCodec = [settings currentVideoCodecSettingFromStore]; |
| 223 | _factory = [[RTCPeerConnectionFactory alloc] initWithEncoderFactory:encoderFactory |
| 224 | decoderFactory:decoderFactory]; |
| 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 | } |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 450 | __weak ARDAppClient *weakSelf = self; |
Anders Carlsson | 6bf43d2 | 2017-10-16 13:51:43 +0200 | [diff] [blame] | 451 | [_peerConnection setLocalDescription:sdp |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 452 | completionHandler:^(NSError *error) { |
Anders Carlsson | 6bf43d2 | 2017-10-16 13:51:43 +0200 | [diff] [blame] | 453 | ARDAppClient *strongSelf = weakSelf; |
| 454 | [strongSelf peerConnection:strongSelf.peerConnection |
| 455 | didSetSessionDescriptionWithError:error]; |
| 456 | }]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 457 | ARDSessionDescriptionMessage *message = |
Anders Carlsson | 6bf43d2 | 2017-10-16 13:51:43 +0200 | [diff] [blame] | 458 | [[ARDSessionDescriptionMessage alloc] initWithDescription:sdp]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 459 | [self sendSignalingMessage:message]; |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 460 | [self setMaxBitrateForPeerConnectionVideoSender]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 461 | }); |
| 462 | } |
| 463 | |
| 464 | - (void)peerConnection:(RTCPeerConnection *)peerConnection |
| 465 | didSetSessionDescriptionWithError:(NSError *)error { |
| 466 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 467 | if (error) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 468 | RTCLogError(@"Failed to set session description. Error: %@", error); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 469 | [self disconnect]; |
| 470 | NSDictionary *userInfo = @{ |
| 471 | NSLocalizedDescriptionKey: @"Failed to set session description.", |
| 472 | }; |
| 473 | NSError *sdpError = |
| 474 | [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 475 | code:kARDAppClientErrorSetSDP |
| 476 | userInfo:userInfo]; |
| 477 | [_delegate appClient:self didError:sdpError]; |
| 478 | return; |
| 479 | } |
| 480 | // If we're answering and we've just set the remote offer we need to create |
| 481 | // an answer and set the local description. |
| 482 | if (!_isInitiator && !_peerConnection.localDescription) { |
| 483 | RTCMediaConstraints *constraints = [self defaultAnswerConstraints]; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 484 | __weak ARDAppClient *weakSelf = self; |
| 485 | [_peerConnection answerForConstraints:constraints |
| 486 | completionHandler:^(RTCSessionDescription *sdp, |
| 487 | NSError *error) { |
| 488 | ARDAppClient *strongSelf = weakSelf; |
| 489 | [strongSelf peerConnection:strongSelf.peerConnection |
| 490 | didCreateSessionDescription:sdp |
| 491 | error:error]; |
| 492 | }]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 493 | } |
| 494 | }); |
| 495 | } |
| 496 | |
| 497 | #pragma mark - Private |
| 498 | |
tkchin | 204177f | 2016-06-14 15:03:11 -0700 | [diff] [blame] | 499 | #if defined(WEBRTC_IOS) |
| 500 | |
| 501 | - (NSString *)documentsFilePathForFileName:(NSString *)fileName { |
| 502 | NSParameterAssert(fileName.length); |
| 503 | NSArray *paths = NSSearchPathForDirectoriesInDomains( |
| 504 | NSDocumentDirectory, NSUserDomainMask, YES); |
| 505 | NSString *documentsDirPath = paths.firstObject; |
| 506 | NSString *filePath = |
| 507 | [documentsDirPath stringByAppendingPathComponent:fileName]; |
| 508 | return filePath; |
| 509 | } |
| 510 | |
| 511 | #endif |
| 512 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 513 | - (BOOL)hasJoinedRoomServerRoom { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 514 | return _clientId.length; |
| 515 | } |
| 516 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 517 | // Begins the peer connection connection process if we have both joined a room |
| 518 | // on the room server and tried to obtain a TURN server. Otherwise does nothing. |
| 519 | // A peer connection object will be created with a stream that contains local |
| 520 | // audio and video capture. If this client is the caller, an offer is created as |
| 521 | // well, otherwise the client will wait for an offer to arrive. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 522 | - (void)startSignalingIfReady { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 523 | if (!_isTurnComplete || !self.hasJoinedRoomServerRoom) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 524 | return; |
| 525 | } |
| 526 | self.state = kARDAppClientStateConnected; |
| 527 | |
| 528 | // Create peer connection. |
| 529 | RTCMediaConstraints *constraints = [self defaultPeerConnectionConstraints]; |
Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 530 | RTCConfiguration *config = [[RTCConfiguration alloc] init]; |
| 531 | config.iceServers = _iceServers; |
Tze Kwang Chin | f3cb49f | 2016-03-22 10:57:40 -0700 | [diff] [blame] | 532 | _peerConnection = [_factory peerConnectionWithConfiguration:config |
| 533 | constraints:constraints |
| 534 | delegate:self]; |
skvlad | f3569c8 | 2016-04-29 15:30:16 -0700 | [diff] [blame] | 535 | // Create AV senders. |
Alex Narest | b3944f0 | 2017-10-13 14:56:18 +0200 | [diff] [blame] | 536 | [self createMediaSenders]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 537 | if (_isInitiator) { |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 538 | // Send offer. |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 539 | __weak ARDAppClient *weakSelf = self; |
| 540 | [_peerConnection offerForConstraints:[self defaultOfferConstraints] |
| 541 | completionHandler:^(RTCSessionDescription *sdp, |
| 542 | NSError *error) { |
| 543 | ARDAppClient *strongSelf = weakSelf; |
| 544 | [strongSelf peerConnection:strongSelf.peerConnection |
| 545 | didCreateSessionDescription:sdp |
| 546 | error:error]; |
| 547 | }]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 548 | } else { |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 549 | // Check if we've received an offer. |
| 550 | [self drainMessageQueueIfReady]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 551 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 552 | #if defined(WEBRTC_IOS) |
| 553 | // Start event log. |
| 554 | if (kARDAppClientEnableRtcEventLog) { |
| 555 | NSString *filePath = [self documentsFilePathForFileName:@"webrtc-rtceventlog"]; |
| 556 | if (![_peerConnection startRtcEventLogWithFilePath:filePath |
| 557 | maxSizeInBytes:kARDAppClientRtcEventLogMaxSizeInBytes]) { |
| 558 | RTCLogError(@"Failed to start event logging."); |
| 559 | } |
| 560 | } |
peah | 5085b0c | 2016-08-25 22:15:14 -0700 | [diff] [blame] | 561 | |
| 562 | // Start aecdump diagnostic recording. |
Anders Carlsson | e150058 | 2017-06-15 16:05:13 +0200 | [diff] [blame] | 563 | if ([_settings currentCreateAecDumpSettingFromStore]) { |
tkchin | fce0e2c | 2016-08-30 12:58:11 -0700 | [diff] [blame] | 564 | NSString *filePath = [self documentsFilePathForFileName:@"webrtc-audio.aecdump"]; |
| 565 | if (![_factory startAecDumpWithFilePath:filePath |
| 566 | maxSizeInBytes:kARDAppClientAecDumpMaxSizeInBytes]) { |
| 567 | RTCLogError(@"Failed to start aec dump."); |
peah | 5085b0c | 2016-08-25 22:15:14 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 570 | #endif |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 571 | } |
| 572 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 573 | // Processes the messages that we've received from the room server and the |
| 574 | // signaling channel. The offer or answer message must be processed before other |
| 575 | // signaling messages, however they can arrive out of order. Hence, this method |
| 576 | // only processes pending messages if there is a peer connection object and |
| 577 | // if we have received either an offer or answer. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 578 | - (void)drainMessageQueueIfReady { |
| 579 | if (!_peerConnection || !_hasReceivedSdp) { |
| 580 | return; |
| 581 | } |
| 582 | for (ARDSignalingMessage *message in _messageQueue) { |
| 583 | [self processSignalingMessage:message]; |
| 584 | } |
| 585 | [_messageQueue removeAllObjects]; |
| 586 | } |
| 587 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 588 | // Processes the given signaling message based on its type. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 589 | - (void)processSignalingMessage:(ARDSignalingMessage *)message { |
| 590 | NSParameterAssert(_peerConnection || |
| 591 | message.type == kARDSignalingMessageTypeBye); |
| 592 | switch (message.type) { |
| 593 | case kARDSignalingMessageTypeOffer: |
| 594 | case kARDSignalingMessageTypeAnswer: { |
| 595 | ARDSessionDescriptionMessage *sdpMessage = |
| 596 | (ARDSessionDescriptionMessage *)message; |
| 597 | RTCSessionDescription *description = sdpMessage.sessionDescription; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 598 | __weak ARDAppClient *weakSelf = self; |
Anders Carlsson | 6bf43d2 | 2017-10-16 13:51:43 +0200 | [diff] [blame] | 599 | [_peerConnection setRemoteDescription:description |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 600 | completionHandler:^(NSError *error) { |
Anders Carlsson | 6bf43d2 | 2017-10-16 13:51:43 +0200 | [diff] [blame] | 601 | ARDAppClient *strongSelf = weakSelf; |
| 602 | [strongSelf peerConnection:strongSelf.peerConnection |
| 603 | didSetSessionDescriptionWithError:error]; |
| 604 | }]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 605 | break; |
| 606 | } |
| 607 | case kARDSignalingMessageTypeCandidate: { |
| 608 | ARDICECandidateMessage *candidateMessage = |
| 609 | (ARDICECandidateMessage *)message; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 610 | [_peerConnection addIceCandidate:candidateMessage.candidate]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 611 | break; |
| 612 | } |
Honghai Zhang | da2ba4d | 2016-05-23 11:53:14 -0700 | [diff] [blame] | 613 | case kARDSignalingMessageTypeCandidateRemoval: { |
| 614 | ARDICECandidateRemovalMessage *candidateMessage = |
| 615 | (ARDICECandidateRemovalMessage *)message; |
| 616 | [_peerConnection removeIceCandidates:candidateMessage.candidates]; |
| 617 | break; |
| 618 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 619 | case kARDSignalingMessageTypeBye: |
| 620 | // Other client disconnected. |
| 621 | // TODO(tkchin): support waiting in room for next client. For now just |
| 622 | // disconnect. |
| 623 | [self disconnect]; |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | |
tkchin@webrtc.org | 8cc47e9 | 2015-03-18 23:38:04 +0000 | [diff] [blame] | 628 | // Sends a signaling message to the other client. The caller will send messages |
| 629 | // through the room server, whereas the callee will send messages over the |
| 630 | // signaling channel. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 631 | - (void)sendSignalingMessage:(ARDSignalingMessage *)message { |
| 632 | if (_isInitiator) { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 633 | __weak ARDAppClient *weakSelf = self; |
| 634 | [_roomServerClient sendMessage:message |
| 635 | forRoomId:_roomId |
| 636 | clientId:_clientId |
| 637 | completionHandler:^(ARDMessageResponse *response, |
| 638 | NSError *error) { |
| 639 | ARDAppClient *strongSelf = weakSelf; |
| 640 | if (error) { |
| 641 | [strongSelf.delegate appClient:strongSelf didError:error]; |
| 642 | return; |
| 643 | } |
| 644 | NSError *messageError = |
| 645 | [[strongSelf class] errorForMessageResultType:response.result]; |
| 646 | if (messageError) { |
| 647 | [strongSelf.delegate appClient:strongSelf didError:messageError]; |
| 648 | return; |
| 649 | } |
| 650 | }]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 651 | } else { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 652 | [_channel sendMessage:message]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 656 | - (void)setMaxBitrateForPeerConnectionVideoSender { |
| 657 | for (RTCRtpSender *sender in _peerConnection.senders) { |
| 658 | if (sender.track != nil) { |
| 659 | if ([sender.track.kind isEqualToString:kARDVideoTrackKind]) { |
sakal | c4adacf | 2017-03-28 01:22:48 -0700 | [diff] [blame] | 660 | [self setMaxBitrate:[_settings currentMaxBitrateSettingFromStore] forVideoSender:sender]; |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 661 | } |
| 662 | } |
denicija | 8c375de | 2016-11-08 06:28:17 -0800 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 666 | - (void)setMaxBitrate:(NSNumber *)maxBitrate forVideoSender:(RTCRtpSender *)sender { |
| 667 | if (maxBitrate.intValue <= 0) { |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | RTCRtpParameters *parametersToModify = sender.parameters; |
| 672 | for (RTCRtpEncodingParameters *encoding in parametersToModify.encodings) { |
| 673 | encoding.maxBitrateBps = @(maxBitrate.intValue * kKbpsMultiplier); |
| 674 | } |
| 675 | [sender setParameters:parametersToModify]; |
| 676 | } |
| 677 | |
Alex Narest | b3944f0 | 2017-10-13 14:56:18 +0200 | [diff] [blame] | 678 | - (void)createMediaSenders { |
tkchin | ab1293a | 2016-08-30 12:35:05 -0700 | [diff] [blame] | 679 | RTCMediaConstraints *constraints = [self defaultMediaAudioConstraints]; |
| 680 | RTCAudioSource *source = [_factory audioSourceWithConstraints:constraints]; |
| 681 | RTCAudioTrack *track = [_factory audioTrackWithSource:source |
| 682 | trackId:kARDAudioTrackId]; |
Alex Narest | b3944f0 | 2017-10-13 14:56:18 +0200 | [diff] [blame] | 683 | RTCMediaStream *stream = [_factory mediaStreamWithStreamId:kARDMediaStreamId]; |
| 684 | [stream addAudioTrack:track]; |
| 685 | _localVideoTrack = [self createLocalVideoTrack]; |
| 686 | if (_localVideoTrack) { |
| 687 | [stream addVideoTrack:_localVideoTrack]; |
| 688 | } |
| 689 | [_peerConnection addStream:stream]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 690 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 691 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 692 | - (RTCVideoTrack *)createLocalVideoTrack { |
| 693 | RTCVideoTrack* localVideoTrack = nil; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 694 | // The iOS simulator doesn't provide any sort of camera capture |
| 695 | // support or emulation (http://goo.gl/rHAnC1) so don't bother |
| 696 | // trying to open a local stream. |
kthelgason | 314bc5f | 2016-08-31 10:23:27 -0700 | [diff] [blame] | 697 | #if !TARGET_IPHONE_SIMULATOR |
Anders Carlsson | e150058 | 2017-06-15 16:05:13 +0200 | [diff] [blame] | 698 | if (![_settings currentAudioOnlySettingFromStore]) { |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 699 | RTCVideoSource *source = [_factory videoSource]; |
| 700 | RTCCameraVideoCapturer *capturer = [[RTCCameraVideoCapturer alloc] initWithDelegate:source]; |
| 701 | [_delegate appClient:self didCreateLocalCapturer:capturer]; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 702 | localVideoTrack = |
Tze Kwang Chin | f3cb49f | 2016-03-22 10:57:40 -0700 | [diff] [blame] | 703 | [_factory videoTrackWithSource:source |
skvlad | f3569c8 | 2016-04-29 15:30:16 -0700 | [diff] [blame] | 704 | trackId:kARDVideoTrackId]; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 705 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 706 | #endif |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 707 | return localVideoTrack; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 708 | } |
| 709 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 710 | #pragma mark - Collider methods |
| 711 | |
| 712 | - (void)registerWithColliderIfReady { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 713 | if (!self.hasJoinedRoomServerRoom) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 714 | return; |
| 715 | } |
| 716 | // Open WebSocket connection. |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 717 | if (!_channel) { |
| 718 | _channel = |
| 719 | [[ARDWebSocketChannel alloc] initWithURL:_websocketURL |
| 720 | restURL:_websocketRestURL |
| 721 | delegate:self]; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 722 | if (_isLoopback) { |
| 723 | _loopbackChannel = |
| 724 | [[ARDLoopbackWebSocketChannel alloc] initWithURL:_websocketURL |
| 725 | restURL:_websocketRestURL]; |
| 726 | } |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 727 | } |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 728 | [_channel registerForRoomId:_roomId clientId:_clientId]; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 729 | if (_isLoopback) { |
| 730 | [_loopbackChannel registerForRoomId:_roomId clientId:@"LOOPBACK_CLIENT_ID"]; |
| 731 | } |
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 - Defaults |
| 735 | |
tkchin | ab1293a | 2016-08-30 12:35:05 -0700 | [diff] [blame] | 736 | - (RTCMediaConstraints *)defaultMediaAudioConstraints { |
Anders Carlsson | e150058 | 2017-06-15 16:05:13 +0200 | [diff] [blame] | 737 | NSString *valueLevelControl = [_settings currentUseLevelControllerSettingFromStore] ? |
| 738 | kRTCMediaConstraintsValueTrue : |
| 739 | kRTCMediaConstraintsValueFalse; |
tkchin | ab1293a | 2016-08-30 12:35:05 -0700 | [diff] [blame] | 740 | NSDictionary *mandatoryConstraints = @{ kRTCMediaConstraintsLevelControl : valueLevelControl }; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 741 | RTCMediaConstraints *constraints = |
| 742 | [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstraints |
| 743 | optionalConstraints:nil]; |
tkchin | ab1293a | 2016-08-30 12:35:05 -0700 | [diff] [blame] | 744 | return constraints; |
| 745 | } |
| 746 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 747 | - (RTCMediaConstraints *)defaultAnswerConstraints { |
| 748 | return [self defaultOfferConstraints]; |
| 749 | } |
| 750 | |
| 751 | - (RTCMediaConstraints *)defaultOfferConstraints { |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 752 | NSDictionary *mandatoryConstraints = @{ |
| 753 | @"OfferToReceiveAudio" : @"true", |
| 754 | @"OfferToReceiveVideo" : @"true" |
| 755 | }; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 756 | RTCMediaConstraints* constraints = |
| 757 | [[RTCMediaConstraints alloc] |
| 758 | initWithMandatoryConstraints:mandatoryConstraints |
| 759 | optionalConstraints:nil]; |
| 760 | return constraints; |
| 761 | } |
| 762 | |
| 763 | - (RTCMediaConstraints *)defaultPeerConnectionConstraints { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 764 | if (_defaultPeerConnectionConstraints) { |
| 765 | return _defaultPeerConnectionConstraints; |
| 766 | } |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 767 | NSString *value = _isLoopback ? @"false" : @"true"; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 768 | NSDictionary *optionalConstraints = @{ @"DtlsSrtpKeyAgreement" : value }; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 769 | RTCMediaConstraints* constraints = |
| 770 | [[RTCMediaConstraints alloc] |
| 771 | initWithMandatoryConstraints:nil |
| 772 | optionalConstraints:optionalConstraints]; |
| 773 | return constraints; |
| 774 | } |
| 775 | |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 776 | #pragma mark - Errors |
| 777 | |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 778 | + (NSError *)errorForJoinResultType:(ARDJoinResultType)resultType { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 779 | NSError *error = nil; |
| 780 | switch (resultType) { |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 781 | case kARDJoinResultTypeSuccess: |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 782 | break; |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 783 | case kARDJoinResultTypeUnknown: { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 784 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 785 | code:kARDAppClientErrorUnknown |
| 786 | userInfo:@{ |
| 787 | NSLocalizedDescriptionKey: @"Unknown error.", |
| 788 | }]; |
| 789 | break; |
| 790 | } |
tkchin@webrtc.org | 36401ab | 2015-01-27 21:34:39 +0000 | [diff] [blame] | 791 | case kARDJoinResultTypeFull: { |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 792 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 793 | code:kARDAppClientErrorRoomFull |
| 794 | userInfo:@{ |
| 795 | NSLocalizedDescriptionKey: @"Room is full.", |
| 796 | }]; |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | return error; |
| 801 | } |
| 802 | |
| 803 | + (NSError *)errorForMessageResultType:(ARDMessageResultType)resultType { |
| 804 | NSError *error = nil; |
| 805 | switch (resultType) { |
| 806 | case kARDMessageResultTypeSuccess: |
| 807 | break; |
| 808 | case kARDMessageResultTypeUnknown: |
| 809 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 810 | code:kARDAppClientErrorUnknown |
| 811 | userInfo:@{ |
| 812 | NSLocalizedDescriptionKey: @"Unknown error.", |
| 813 | }]; |
| 814 | break; |
| 815 | case kARDMessageResultTypeInvalidClient: |
| 816 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 817 | code:kARDAppClientErrorInvalidClient |
| 818 | userInfo:@{ |
| 819 | NSLocalizedDescriptionKey: @"Invalid client.", |
| 820 | }]; |
| 821 | break; |
| 822 | case kARDMessageResultTypeInvalidRoom: |
| 823 | error = [[NSError alloc] initWithDomain:kARDAppClientErrorDomain |
| 824 | code:kARDAppClientErrorInvalidRoom |
| 825 | userInfo:@{ |
| 826 | NSLocalizedDescriptionKey: @"Invalid room.", |
| 827 | }]; |
| 828 | break; |
| 829 | } |
| 830 | return error; |
| 831 | } |
| 832 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 833 | @end |