hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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. |
| 9 | */ |
| 10 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 11 | #import "RTCPeerConnection+Private.h" |
| 12 | |
| 13 | #import "NSString+StdString.h" |
| 14 | #import "RTCConfiguration+Private.h" |
| 15 | #import "RTCDataChannel+Private.h" |
| 16 | #import "RTCIceCandidate+Private.h" |
hbos | bd3dda6 | 2016-09-09 01:36:28 -0700 | [diff] [blame] | 17 | #import "RTCLegacyStatsReport+Private.h" |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 18 | #import "RTCMediaConstraints+Private.h" |
| 19 | #import "RTCMediaStream+Private.h" |
| 20 | #import "RTCPeerConnectionFactory+Private.h" |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 21 | #import "RTCRtpReceiver+Private.h" |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 22 | #import "RTCRtpSender+Private.h" |
| 23 | #import "RTCSessionDescription+Private.h" |
Alex Narest | a5fbc23 | 2017-10-18 18:31:07 +0200 | [diff] [blame^] | 24 | #import "WebRTC/RTCBitrateAllocationStrategy.h" |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 25 | #import "WebRTC/RTCLogging.h" |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 26 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 27 | #include <memory> |
| 28 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "api/jsepicecandidate.h" |
| 30 | #include "rtc_base/checks.h" |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 31 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 32 | NSString * const kRTCPeerConnectionErrorDomain = |
| 33 | @"org.webrtc.RTCPeerConnection"; |
| 34 | int const kRTCPeerConnnectionSessionDescriptionError = -1; |
| 35 | |
| 36 | namespace webrtc { |
| 37 | |
| 38 | class CreateSessionDescriptionObserverAdapter |
| 39 | : public CreateSessionDescriptionObserver { |
| 40 | public: |
| 41 | CreateSessionDescriptionObserverAdapter( |
| 42 | void (^completionHandler)(RTCSessionDescription *sessionDescription, |
| 43 | NSError *error)) { |
| 44 | completion_handler_ = completionHandler; |
| 45 | } |
| 46 | |
| 47 | ~CreateSessionDescriptionObserverAdapter() { |
| 48 | completion_handler_ = nil; |
| 49 | } |
| 50 | |
| 51 | void OnSuccess(SessionDescriptionInterface *desc) override { |
| 52 | RTC_DCHECK(completion_handler_); |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 53 | std::unique_ptr<webrtc::SessionDescriptionInterface> description = |
| 54 | std::unique_ptr<webrtc::SessionDescriptionInterface>(desc); |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 55 | RTCSessionDescription* session = |
| 56 | [[RTCSessionDescription alloc] initWithNativeDescription: |
| 57 | description.get()]; |
| 58 | completion_handler_(session, nil); |
| 59 | completion_handler_ = nil; |
| 60 | } |
| 61 | |
| 62 | void OnFailure(const std::string& error) override { |
| 63 | RTC_DCHECK(completion_handler_); |
| 64 | NSString* str = [NSString stringForStdString:error]; |
| 65 | NSError* err = |
| 66 | [NSError errorWithDomain:kRTCPeerConnectionErrorDomain |
| 67 | code:kRTCPeerConnnectionSessionDescriptionError |
| 68 | userInfo:@{ NSLocalizedDescriptionKey : str }]; |
| 69 | completion_handler_(nil, err); |
| 70 | completion_handler_ = nil; |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | void (^completion_handler_) |
| 75 | (RTCSessionDescription *sessionDescription, NSError *error); |
| 76 | }; |
| 77 | |
| 78 | class SetSessionDescriptionObserverAdapter : |
| 79 | public SetSessionDescriptionObserver { |
| 80 | public: |
| 81 | SetSessionDescriptionObserverAdapter(void (^completionHandler) |
| 82 | (NSError *error)) { |
| 83 | completion_handler_ = completionHandler; |
| 84 | } |
| 85 | |
| 86 | ~SetSessionDescriptionObserverAdapter() { |
| 87 | completion_handler_ = nil; |
| 88 | } |
| 89 | |
| 90 | void OnSuccess() override { |
| 91 | RTC_DCHECK(completion_handler_); |
| 92 | completion_handler_(nil); |
| 93 | completion_handler_ = nil; |
| 94 | } |
| 95 | |
| 96 | void OnFailure(const std::string& error) override { |
| 97 | RTC_DCHECK(completion_handler_); |
| 98 | NSString* str = [NSString stringForStdString:error]; |
| 99 | NSError* err = |
| 100 | [NSError errorWithDomain:kRTCPeerConnectionErrorDomain |
| 101 | code:kRTCPeerConnnectionSessionDescriptionError |
| 102 | userInfo:@{ NSLocalizedDescriptionKey : str }]; |
| 103 | completion_handler_(err); |
| 104 | completion_handler_ = nil; |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | void (^completion_handler_)(NSError *error); |
| 109 | }; |
| 110 | |
| 111 | PeerConnectionDelegateAdapter::PeerConnectionDelegateAdapter( |
| 112 | RTCPeerConnection *peerConnection) { |
| 113 | peer_connection_ = peerConnection; |
| 114 | } |
| 115 | |
| 116 | PeerConnectionDelegateAdapter::~PeerConnectionDelegateAdapter() { |
| 117 | peer_connection_ = nil; |
| 118 | } |
| 119 | |
| 120 | void PeerConnectionDelegateAdapter::OnSignalingChange( |
| 121 | PeerConnectionInterface::SignalingState new_state) { |
| 122 | RTCSignalingState state = |
| 123 | [[RTCPeerConnection class] signalingStateForNativeState:new_state]; |
| 124 | RTCPeerConnection *peer_connection = peer_connection_; |
| 125 | [peer_connection.delegate peerConnection:peer_connection |
| 126 | didChangeSignalingState:state]; |
| 127 | } |
| 128 | |
| 129 | void PeerConnectionDelegateAdapter::OnAddStream( |
deadbeef | d5f41ce | 2016-06-08 13:31:45 -0700 | [diff] [blame] | 130 | rtc::scoped_refptr<MediaStreamInterface> stream) { |
magjed | 63bafd6 | 2017-02-27 07:04:25 -0800 | [diff] [blame] | 131 | RTCMediaStream *mediaStream = |
| 132 | [[RTCMediaStream alloc] initWithNativeMediaStream:stream]; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 133 | RTCPeerConnection *peer_connection = peer_connection_; |
| 134 | [peer_connection.delegate peerConnection:peer_connection |
| 135 | didAddStream:mediaStream]; |
| 136 | } |
| 137 | |
| 138 | void PeerConnectionDelegateAdapter::OnRemoveStream( |
deadbeef | d5f41ce | 2016-06-08 13:31:45 -0700 | [diff] [blame] | 139 | rtc::scoped_refptr<MediaStreamInterface> stream) { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 140 | RTCMediaStream *mediaStream = |
| 141 | [[RTCMediaStream alloc] initWithNativeMediaStream:stream]; |
| 142 | RTCPeerConnection *peer_connection = peer_connection_; |
| 143 | [peer_connection.delegate peerConnection:peer_connection |
| 144 | didRemoveStream:mediaStream]; |
| 145 | } |
| 146 | |
| 147 | void PeerConnectionDelegateAdapter::OnDataChannel( |
deadbeef | d5f41ce | 2016-06-08 13:31:45 -0700 | [diff] [blame] | 148 | rtc::scoped_refptr<DataChannelInterface> data_channel) { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 149 | RTCDataChannel *dataChannel = |
| 150 | [[RTCDataChannel alloc] initWithNativeDataChannel:data_channel]; |
| 151 | RTCPeerConnection *peer_connection = peer_connection_; |
| 152 | [peer_connection.delegate peerConnection:peer_connection |
| 153 | didOpenDataChannel:dataChannel]; |
| 154 | } |
| 155 | |
| 156 | void PeerConnectionDelegateAdapter::OnRenegotiationNeeded() { |
| 157 | RTCPeerConnection *peer_connection = peer_connection_; |
| 158 | [peer_connection.delegate peerConnectionShouldNegotiate:peer_connection]; |
| 159 | } |
| 160 | |
| 161 | void PeerConnectionDelegateAdapter::OnIceConnectionChange( |
| 162 | PeerConnectionInterface::IceConnectionState new_state) { |
| 163 | RTCIceConnectionState state = |
| 164 | [[RTCPeerConnection class] iceConnectionStateForNativeState:new_state]; |
| 165 | RTCPeerConnection *peer_connection = peer_connection_; |
| 166 | [peer_connection.delegate peerConnection:peer_connection |
| 167 | didChangeIceConnectionState:state]; |
| 168 | } |
| 169 | |
| 170 | void PeerConnectionDelegateAdapter::OnIceGatheringChange( |
| 171 | PeerConnectionInterface::IceGatheringState new_state) { |
| 172 | RTCIceGatheringState state = |
| 173 | [[RTCPeerConnection class] iceGatheringStateForNativeState:new_state]; |
| 174 | RTCPeerConnection *peer_connection = peer_connection_; |
| 175 | [peer_connection.delegate peerConnection:peer_connection |
| 176 | didChangeIceGatheringState:state]; |
| 177 | } |
| 178 | |
| 179 | void PeerConnectionDelegateAdapter::OnIceCandidate( |
| 180 | const IceCandidateInterface *candidate) { |
| 181 | RTCIceCandidate *iceCandidate = |
| 182 | [[RTCIceCandidate alloc] initWithNativeCandidate:candidate]; |
| 183 | RTCPeerConnection *peer_connection = peer_connection_; |
| 184 | [peer_connection.delegate peerConnection:peer_connection |
| 185 | didGenerateIceCandidate:iceCandidate]; |
| 186 | } |
Honghai Zhang | da2ba4d | 2016-05-23 11:53:14 -0700 | [diff] [blame] | 187 | |
| 188 | void PeerConnectionDelegateAdapter::OnIceCandidatesRemoved( |
| 189 | const std::vector<cricket::Candidate>& candidates) { |
| 190 | NSMutableArray* ice_candidates = |
| 191 | [NSMutableArray arrayWithCapacity:candidates.size()]; |
| 192 | for (const auto& candidate : candidates) { |
| 193 | std::unique_ptr<JsepIceCandidate> candidate_wrapper( |
| 194 | new JsepIceCandidate(candidate.transport_name(), -1, candidate)); |
| 195 | RTCIceCandidate* ice_candidate = [[RTCIceCandidate alloc] |
| 196 | initWithNativeCandidate:candidate_wrapper.get()]; |
| 197 | [ice_candidates addObject:ice_candidate]; |
| 198 | } |
| 199 | RTCPeerConnection* peer_connection = peer_connection_; |
| 200 | [peer_connection.delegate peerConnection:peer_connection |
| 201 | didRemoveIceCandidates:ice_candidates]; |
| 202 | } |
| 203 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 204 | } // namespace webrtc |
| 205 | |
| 206 | |
| 207 | @implementation RTCPeerConnection { |
vopatop.skam | 96b6b83 | 2016-08-18 14:21:20 -0700 | [diff] [blame] | 208 | NSMutableArray<RTCMediaStream *> *_localStreams; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 209 | std::unique_ptr<webrtc::PeerConnectionDelegateAdapter> _observer; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 210 | rtc::scoped_refptr<webrtc::PeerConnectionInterface> _peerConnection; |
deadbeef | 5d0b6d8 | 2017-01-09 16:05:28 -0800 | [diff] [blame] | 211 | std::unique_ptr<webrtc::MediaConstraints> _nativeConstraints; |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 212 | BOOL _hasStartedRtcEventLog; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @synthesize delegate = _delegate; |
| 216 | |
| 217 | - (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory |
| 218 | configuration:(RTCConfiguration *)configuration |
| 219 | constraints:(RTCMediaConstraints *)constraints |
| 220 | delegate:(id<RTCPeerConnectionDelegate>)delegate { |
| 221 | NSParameterAssert(factory); |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 222 | std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration> config( |
hbos | a73ca56 | 2016-05-17 03:28:58 -0700 | [diff] [blame] | 223 | [configuration createNativeConfiguration]); |
| 224 | if (!config) { |
| 225 | return nil; |
| 226 | } |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 227 | if (self = [super init]) { |
| 228 | _observer.reset(new webrtc::PeerConnectionDelegateAdapter(self)); |
deadbeef | 5d0b6d8 | 2017-01-09 16:05:28 -0800 | [diff] [blame] | 229 | _nativeConstraints = constraints.nativeConstraints; |
| 230 | CopyConstraintsIntoRtcConfiguration(_nativeConstraints.get(), |
| 231 | config.get()); |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 232 | _peerConnection = |
hbos | d7973cc | 2016-05-27 06:08:53 -0700 | [diff] [blame] | 233 | factory.nativeFactory->CreatePeerConnection(*config, |
hbos | d7973cc | 2016-05-27 06:08:53 -0700 | [diff] [blame] | 234 | nullptr, |
| 235 | nullptr, |
| 236 | _observer.get()); |
skvlad | 588783a | 2016-08-11 14:29:25 -0700 | [diff] [blame] | 237 | if (!_peerConnection) { |
| 238 | return nil; |
| 239 | } |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 240 | _localStreams = [[NSMutableArray alloc] init]; |
| 241 | _delegate = delegate; |
| 242 | } |
| 243 | return self; |
| 244 | } |
| 245 | |
vopatop.skam | 96b6b83 | 2016-08-18 14:21:20 -0700 | [diff] [blame] | 246 | - (NSArray<RTCMediaStream *> *)localStreams { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 247 | return [_localStreams copy]; |
| 248 | } |
| 249 | |
| 250 | - (RTCSessionDescription *)localDescription { |
| 251 | const webrtc::SessionDescriptionInterface *description = |
| 252 | _peerConnection->local_description(); |
| 253 | return description ? |
| 254 | [[RTCSessionDescription alloc] initWithNativeDescription:description] |
| 255 | : nil; |
| 256 | } |
| 257 | |
| 258 | - (RTCSessionDescription *)remoteDescription { |
| 259 | const webrtc::SessionDescriptionInterface *description = |
| 260 | _peerConnection->remote_description(); |
| 261 | return description ? |
| 262 | [[RTCSessionDescription alloc] initWithNativeDescription:description] |
| 263 | : nil; |
| 264 | } |
| 265 | |
| 266 | - (RTCSignalingState)signalingState { |
| 267 | return [[self class] |
| 268 | signalingStateForNativeState:_peerConnection->signaling_state()]; |
| 269 | } |
| 270 | |
| 271 | - (RTCIceConnectionState)iceConnectionState { |
| 272 | return [[self class] iceConnectionStateForNativeState: |
| 273 | _peerConnection->ice_connection_state()]; |
| 274 | } |
| 275 | |
| 276 | - (RTCIceGatheringState)iceGatheringState { |
| 277 | return [[self class] iceGatheringStateForNativeState: |
| 278 | _peerConnection->ice_gathering_state()]; |
| 279 | } |
| 280 | |
tkchin | aac3eb2 | 2016-03-09 21:49:40 -0800 | [diff] [blame] | 281 | - (BOOL)setConfiguration:(RTCConfiguration *)configuration { |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 282 | std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration> config( |
hbos | a73ca56 | 2016-05-17 03:28:58 -0700 | [diff] [blame] | 283 | [configuration createNativeConfiguration]); |
| 284 | if (!config) { |
| 285 | return NO; |
| 286 | } |
deadbeef | 5d0b6d8 | 2017-01-09 16:05:28 -0800 | [diff] [blame] | 287 | CopyConstraintsIntoRtcConfiguration(_nativeConstraints.get(), |
| 288 | config.get()); |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 289 | return _peerConnection->SetConfiguration(*config); |
tkchin | aac3eb2 | 2016-03-09 21:49:40 -0800 | [diff] [blame] | 290 | } |
| 291 | |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 292 | - (RTCConfiguration *)configuration { |
| 293 | webrtc::PeerConnectionInterface::RTCConfiguration config = |
| 294 | _peerConnection->GetConfiguration(); |
jtteh | 465faf0 | 2017-04-04 14:00:16 -0700 | [diff] [blame] | 295 | return [[RTCConfiguration alloc] initWithNativeConfiguration:config]; |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 296 | } |
| 297 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 298 | - (void)close { |
| 299 | _peerConnection->Close(); |
| 300 | } |
| 301 | |
| 302 | - (void)addIceCandidate:(RTCIceCandidate *)candidate { |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 303 | std::unique_ptr<const webrtc::IceCandidateInterface> iceCandidate( |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 304 | candidate.nativeCandidate); |
| 305 | _peerConnection->AddIceCandidate(iceCandidate.get()); |
| 306 | } |
| 307 | |
Honghai Zhang | da2ba4d | 2016-05-23 11:53:14 -0700 | [diff] [blame] | 308 | - (void)removeIceCandidates:(NSArray<RTCIceCandidate *> *)iceCandidates { |
| 309 | std::vector<cricket::Candidate> candidates; |
| 310 | for (RTCIceCandidate *iceCandidate in iceCandidates) { |
| 311 | std::unique_ptr<const webrtc::IceCandidateInterface> candidate( |
| 312 | iceCandidate.nativeCandidate); |
| 313 | if (candidate) { |
| 314 | candidates.push_back(candidate->candidate()); |
| 315 | // Need to fill the transport name from the sdp_mid. |
| 316 | candidates.back().set_transport_name(candidate->sdp_mid()); |
| 317 | } |
| 318 | } |
| 319 | if (!candidates.empty()) { |
| 320 | _peerConnection->RemoveIceCandidates(candidates); |
| 321 | } |
| 322 | } |
| 323 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 324 | - (void)addStream:(RTCMediaStream *)stream { |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 325 | if (!_peerConnection->AddStream(stream.nativeMediaStream)) { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 326 | RTCLogError(@"Failed to add stream: %@", stream); |
| 327 | return; |
| 328 | } |
| 329 | [_localStreams addObject:stream]; |
| 330 | } |
| 331 | |
| 332 | - (void)removeStream:(RTCMediaStream *)stream { |
| 333 | _peerConnection->RemoveStream(stream.nativeMediaStream); |
| 334 | [_localStreams removeObject:stream]; |
| 335 | } |
| 336 | |
| 337 | - (void)offerForConstraints:(RTCMediaConstraints *)constraints |
| 338 | completionHandler: |
| 339 | (void (^)(RTCSessionDescription *sessionDescription, |
| 340 | NSError *error))completionHandler { |
| 341 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserverAdapter> |
| 342 | observer(new rtc::RefCountedObject |
| 343 | <webrtc::CreateSessionDescriptionObserverAdapter>(completionHandler)); |
| 344 | _peerConnection->CreateOffer(observer, constraints.nativeConstraints.get()); |
| 345 | } |
| 346 | |
| 347 | - (void)answerForConstraints:(RTCMediaConstraints *)constraints |
| 348 | completionHandler: |
| 349 | (void (^)(RTCSessionDescription *sessionDescription, |
| 350 | NSError *error))completionHandler { |
| 351 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserverAdapter> |
| 352 | observer(new rtc::RefCountedObject |
| 353 | <webrtc::CreateSessionDescriptionObserverAdapter>(completionHandler)); |
| 354 | _peerConnection->CreateAnswer(observer, constraints.nativeConstraints.get()); |
| 355 | } |
| 356 | |
| 357 | - (void)setLocalDescription:(RTCSessionDescription *)sdp |
| 358 | completionHandler:(void (^)(NSError *error))completionHandler { |
| 359 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserverAdapter> observer( |
| 360 | new rtc::RefCountedObject<webrtc::SetSessionDescriptionObserverAdapter>( |
| 361 | completionHandler)); |
| 362 | _peerConnection->SetLocalDescription(observer, sdp.nativeDescription); |
| 363 | } |
| 364 | |
| 365 | - (void)setRemoteDescription:(RTCSessionDescription *)sdp |
| 366 | completionHandler:(void (^)(NSError *error))completionHandler { |
| 367 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserverAdapter> observer( |
| 368 | new rtc::RefCountedObject<webrtc::SetSessionDescriptionObserverAdapter>( |
| 369 | completionHandler)); |
| 370 | _peerConnection->SetRemoteDescription(observer, sdp.nativeDescription); |
| 371 | } |
| 372 | |
zstein | 8b47617 | 2017-09-05 14:43:03 -0700 | [diff] [blame] | 373 | - (BOOL)setBweMinBitrateBps:(nullable NSNumber *)minBitrateBps |
| 374 | currentBitrateBps:(nullable NSNumber *)currentBitrateBps |
| 375 | maxBitrateBps:(nullable NSNumber *)maxBitrateBps { |
zstein | 03adb7c | 2017-08-09 14:29:42 -0700 | [diff] [blame] | 376 | webrtc::PeerConnectionInterface::BitrateParameters params; |
| 377 | if (minBitrateBps != nil) { |
| 378 | params.min_bitrate_bps = rtc::Optional<int>(minBitrateBps.intValue); |
| 379 | } |
| 380 | if (currentBitrateBps != nil) { |
| 381 | params.current_bitrate_bps = rtc::Optional<int>(currentBitrateBps.intValue); |
| 382 | } |
| 383 | if (maxBitrateBps != nil) { |
| 384 | params.max_bitrate_bps = rtc::Optional<int>(maxBitrateBps.intValue); |
| 385 | } |
| 386 | return _peerConnection->SetBitrate(params).ok(); |
| 387 | } |
| 388 | |
Alex Narest | a5fbc23 | 2017-10-18 18:31:07 +0200 | [diff] [blame^] | 389 | - (void)setBitrateAllocationStrategy: |
| 390 | (RTCBitrateAllocationStrategy *_Nullable)bitrateAllocationStrategy { |
| 391 | if (bitrateAllocationStrategy) |
| 392 | _peerConnection->SetBitrateAllocationStrategy( |
| 393 | std::unique_ptr<rtc::BitrateAllocationStrategy>(bitrateAllocationStrategy.strategy)); |
| 394 | else |
| 395 | _peerConnection->SetBitrateAllocationStrategy(nullptr); |
| 396 | } |
| 397 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 398 | - (BOOL)startRtcEventLogWithFilePath:(NSString *)filePath |
| 399 | maxSizeInBytes:(int64_t)maxSizeInBytes { |
| 400 | RTC_DCHECK(filePath.length); |
| 401 | RTC_DCHECK_GT(maxSizeInBytes, 0); |
| 402 | RTC_DCHECK(!_hasStartedRtcEventLog); |
| 403 | if (_hasStartedRtcEventLog) { |
| 404 | RTCLogError(@"Event logging already started."); |
| 405 | return NO; |
| 406 | } |
| 407 | int fd = open(filePath.UTF8String, O_WRONLY | O_CREAT | O_TRUNC, |
| 408 | S_IRUSR | S_IWUSR); |
| 409 | if (fd < 0) { |
| 410 | RTCLogError(@"Error opening file: %@. Error: %d", filePath, errno); |
| 411 | return NO; |
| 412 | } |
| 413 | _hasStartedRtcEventLog = |
| 414 | _peerConnection->StartRtcEventLog(fd, maxSizeInBytes); |
| 415 | return _hasStartedRtcEventLog; |
| 416 | } |
| 417 | |
| 418 | - (void)stopRtcEventLog { |
| 419 | _peerConnection->StopRtcEventLog(); |
| 420 | _hasStartedRtcEventLog = NO; |
| 421 | } |
| 422 | |
skvlad | f3569c8 | 2016-04-29 15:30:16 -0700 | [diff] [blame] | 423 | - (RTCRtpSender *)senderWithKind:(NSString *)kind |
| 424 | streamId:(NSString *)streamId { |
| 425 | std::string nativeKind = [NSString stdStringForString:kind]; |
| 426 | std::string nativeStreamId = [NSString stdStringForString:streamId]; |
| 427 | rtc::scoped_refptr<webrtc::RtpSenderInterface> nativeSender( |
| 428 | _peerConnection->CreateSender(nativeKind, nativeStreamId)); |
| 429 | return nativeSender ? |
| 430 | [[RTCRtpSender alloc] initWithNativeRtpSender:nativeSender] |
| 431 | : nil; |
| 432 | } |
| 433 | |
skvlad | 79b4b87 | 2016-04-08 17:28:55 -0700 | [diff] [blame] | 434 | - (NSArray<RTCRtpSender *> *)senders { |
| 435 | std::vector<rtc::scoped_refptr<webrtc::RtpSenderInterface>> nativeSenders( |
| 436 | _peerConnection->GetSenders()); |
| 437 | NSMutableArray *senders = [[NSMutableArray alloc] init]; |
| 438 | for (const auto &nativeSender : nativeSenders) { |
| 439 | RTCRtpSender *sender = |
| 440 | [[RTCRtpSender alloc] initWithNativeRtpSender:nativeSender]; |
| 441 | [senders addObject:sender]; |
| 442 | } |
| 443 | return senders; |
| 444 | } |
| 445 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 446 | - (NSArray<RTCRtpReceiver *> *)receivers { |
| 447 | std::vector<rtc::scoped_refptr<webrtc::RtpReceiverInterface>> nativeReceivers( |
| 448 | _peerConnection->GetReceivers()); |
| 449 | NSMutableArray *receivers = [[NSMutableArray alloc] init]; |
| 450 | for (const auto &nativeReceiver : nativeReceivers) { |
| 451 | RTCRtpReceiver *receiver = |
| 452 | [[RTCRtpReceiver alloc] initWithNativeRtpReceiver:nativeReceiver]; |
| 453 | [receivers addObject:receiver]; |
| 454 | } |
| 455 | return receivers; |
| 456 | } |
| 457 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 458 | #pragma mark - Private |
| 459 | |
| 460 | + (webrtc::PeerConnectionInterface::SignalingState)nativeSignalingStateForState: |
| 461 | (RTCSignalingState)state { |
| 462 | switch (state) { |
| 463 | case RTCSignalingStateStable: |
| 464 | return webrtc::PeerConnectionInterface::kStable; |
| 465 | case RTCSignalingStateHaveLocalOffer: |
| 466 | return webrtc::PeerConnectionInterface::kHaveLocalOffer; |
| 467 | case RTCSignalingStateHaveLocalPrAnswer: |
| 468 | return webrtc::PeerConnectionInterface::kHaveLocalPrAnswer; |
| 469 | case RTCSignalingStateHaveRemoteOffer: |
| 470 | return webrtc::PeerConnectionInterface::kHaveRemoteOffer; |
| 471 | case RTCSignalingStateHaveRemotePrAnswer: |
| 472 | return webrtc::PeerConnectionInterface::kHaveRemotePrAnswer; |
| 473 | case RTCSignalingStateClosed: |
| 474 | return webrtc::PeerConnectionInterface::kClosed; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | + (RTCSignalingState)signalingStateForNativeState: |
| 479 | (webrtc::PeerConnectionInterface::SignalingState)nativeState { |
| 480 | switch (nativeState) { |
| 481 | case webrtc::PeerConnectionInterface::kStable: |
| 482 | return RTCSignalingStateStable; |
| 483 | case webrtc::PeerConnectionInterface::kHaveLocalOffer: |
| 484 | return RTCSignalingStateHaveLocalOffer; |
| 485 | case webrtc::PeerConnectionInterface::kHaveLocalPrAnswer: |
| 486 | return RTCSignalingStateHaveLocalPrAnswer; |
| 487 | case webrtc::PeerConnectionInterface::kHaveRemoteOffer: |
| 488 | return RTCSignalingStateHaveRemoteOffer; |
| 489 | case webrtc::PeerConnectionInterface::kHaveRemotePrAnswer: |
| 490 | return RTCSignalingStateHaveRemotePrAnswer; |
| 491 | case webrtc::PeerConnectionInterface::kClosed: |
| 492 | return RTCSignalingStateClosed; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | + (NSString *)stringForSignalingState:(RTCSignalingState)state { |
| 497 | switch (state) { |
| 498 | case RTCSignalingStateStable: |
| 499 | return @"STABLE"; |
| 500 | case RTCSignalingStateHaveLocalOffer: |
| 501 | return @"HAVE_LOCAL_OFFER"; |
| 502 | case RTCSignalingStateHaveLocalPrAnswer: |
| 503 | return @"HAVE_LOCAL_PRANSWER"; |
| 504 | case RTCSignalingStateHaveRemoteOffer: |
| 505 | return @"HAVE_REMOTE_OFFER"; |
| 506 | case RTCSignalingStateHaveRemotePrAnswer: |
| 507 | return @"HAVE_REMOTE_PRANSWER"; |
| 508 | case RTCSignalingStateClosed: |
| 509 | return @"CLOSED"; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | + (webrtc::PeerConnectionInterface::IceConnectionState) |
| 514 | nativeIceConnectionStateForState:(RTCIceConnectionState)state { |
| 515 | switch (state) { |
| 516 | case RTCIceConnectionStateNew: |
| 517 | return webrtc::PeerConnectionInterface::kIceConnectionNew; |
| 518 | case RTCIceConnectionStateChecking: |
| 519 | return webrtc::PeerConnectionInterface::kIceConnectionChecking; |
| 520 | case RTCIceConnectionStateConnected: |
| 521 | return webrtc::PeerConnectionInterface::kIceConnectionConnected; |
| 522 | case RTCIceConnectionStateCompleted: |
| 523 | return webrtc::PeerConnectionInterface::kIceConnectionCompleted; |
| 524 | case RTCIceConnectionStateFailed: |
| 525 | return webrtc::PeerConnectionInterface::kIceConnectionFailed; |
| 526 | case RTCIceConnectionStateDisconnected: |
| 527 | return webrtc::PeerConnectionInterface::kIceConnectionDisconnected; |
| 528 | case RTCIceConnectionStateClosed: |
| 529 | return webrtc::PeerConnectionInterface::kIceConnectionClosed; |
hjon | 8bbbf2c | 2016-03-14 13:15:44 -0700 | [diff] [blame] | 530 | case RTCIceConnectionStateCount: |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 531 | return webrtc::PeerConnectionInterface::kIceConnectionMax; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | + (RTCIceConnectionState)iceConnectionStateForNativeState: |
| 536 | (webrtc::PeerConnectionInterface::IceConnectionState)nativeState { |
| 537 | switch (nativeState) { |
| 538 | case webrtc::PeerConnectionInterface::kIceConnectionNew: |
| 539 | return RTCIceConnectionStateNew; |
| 540 | case webrtc::PeerConnectionInterface::kIceConnectionChecking: |
| 541 | return RTCIceConnectionStateChecking; |
| 542 | case webrtc::PeerConnectionInterface::kIceConnectionConnected: |
| 543 | return RTCIceConnectionStateConnected; |
| 544 | case webrtc::PeerConnectionInterface::kIceConnectionCompleted: |
| 545 | return RTCIceConnectionStateCompleted; |
| 546 | case webrtc::PeerConnectionInterface::kIceConnectionFailed: |
| 547 | return RTCIceConnectionStateFailed; |
| 548 | case webrtc::PeerConnectionInterface::kIceConnectionDisconnected: |
| 549 | return RTCIceConnectionStateDisconnected; |
| 550 | case webrtc::PeerConnectionInterface::kIceConnectionClosed: |
| 551 | return RTCIceConnectionStateClosed; |
| 552 | case webrtc::PeerConnectionInterface::kIceConnectionMax: |
hjon | 8bbbf2c | 2016-03-14 13:15:44 -0700 | [diff] [blame] | 553 | return RTCIceConnectionStateCount; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | + (NSString *)stringForIceConnectionState:(RTCIceConnectionState)state { |
| 558 | switch (state) { |
| 559 | case RTCIceConnectionStateNew: |
| 560 | return @"NEW"; |
| 561 | case RTCIceConnectionStateChecking: |
| 562 | return @"CHECKING"; |
| 563 | case RTCIceConnectionStateConnected: |
| 564 | return @"CONNECTED"; |
| 565 | case RTCIceConnectionStateCompleted: |
| 566 | return @"COMPLETED"; |
| 567 | case RTCIceConnectionStateFailed: |
| 568 | return @"FAILED"; |
| 569 | case RTCIceConnectionStateDisconnected: |
| 570 | return @"DISCONNECTED"; |
| 571 | case RTCIceConnectionStateClosed: |
| 572 | return @"CLOSED"; |
hjon | 8bbbf2c | 2016-03-14 13:15:44 -0700 | [diff] [blame] | 573 | case RTCIceConnectionStateCount: |
| 574 | return @"COUNT"; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | |
| 578 | + (webrtc::PeerConnectionInterface::IceGatheringState) |
| 579 | nativeIceGatheringStateForState:(RTCIceGatheringState)state { |
| 580 | switch (state) { |
| 581 | case RTCIceGatheringStateNew: |
| 582 | return webrtc::PeerConnectionInterface::kIceGatheringNew; |
| 583 | case RTCIceGatheringStateGathering: |
| 584 | return webrtc::PeerConnectionInterface::kIceGatheringGathering; |
| 585 | case RTCIceGatheringStateComplete: |
| 586 | return webrtc::PeerConnectionInterface::kIceGatheringComplete; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | + (RTCIceGatheringState)iceGatheringStateForNativeState: |
| 591 | (webrtc::PeerConnectionInterface::IceGatheringState)nativeState { |
| 592 | switch (nativeState) { |
| 593 | case webrtc::PeerConnectionInterface::kIceGatheringNew: |
| 594 | return RTCIceGatheringStateNew; |
| 595 | case webrtc::PeerConnectionInterface::kIceGatheringGathering: |
| 596 | return RTCIceGatheringStateGathering; |
| 597 | case webrtc::PeerConnectionInterface::kIceGatheringComplete: |
| 598 | return RTCIceGatheringStateComplete; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | + (NSString *)stringForIceGatheringState:(RTCIceGatheringState)state { |
| 603 | switch (state) { |
| 604 | case RTCIceGatheringStateNew: |
| 605 | return @"NEW"; |
| 606 | case RTCIceGatheringStateGathering: |
| 607 | return @"GATHERING"; |
| 608 | case RTCIceGatheringStateComplete: |
| 609 | return @"COMPLETE"; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | + (webrtc::PeerConnectionInterface::StatsOutputLevel) |
| 614 | nativeStatsOutputLevelForLevel:(RTCStatsOutputLevel)level { |
| 615 | switch (level) { |
| 616 | case RTCStatsOutputLevelStandard: |
| 617 | return webrtc::PeerConnectionInterface::kStatsOutputLevelStandard; |
| 618 | case RTCStatsOutputLevelDebug: |
| 619 | return webrtc::PeerConnectionInterface::kStatsOutputLevelDebug; |
| 620 | } |
| 621 | } |
| 622 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 623 | - (rtc::scoped_refptr<webrtc::PeerConnectionInterface>)nativePeerConnection { |
| 624 | return _peerConnection; |
| 625 | } |
| 626 | |
| 627 | @end |