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