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