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