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" |
| 17 | #import "RTCMediaConstraints+Private.h" |
| 18 | #import "RTCMediaStream+Private.h" |
| 19 | #import "RTCPeerConnectionFactory+Private.h" |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 20 | #import "RTCRtpReceiver+Private.h" |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 21 | #import "RTCRtpSender+Private.h" |
| 22 | #import "RTCSessionDescription+Private.h" |
| 23 | #import "RTCStatsReport+Private.h" |
| 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 | |
Honghai Zhang | da2ba4d | 2016-05-23 11:53:14 -0700 | [diff] [blame] | 28 | #include "webrtc/api/jsepicecandidate.h" |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 29 | #include "webrtc/base/checks.h" |
| 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) { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 130 | RTCMediaStream *mediaStream = |
| 131 | [[RTCMediaStream alloc] initWithNativeMediaStream:stream]; |
| 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 { |
| 207 | NSMutableArray *_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; |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 210 | BOOL _hasStartedRtcEventLog; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @synthesize delegate = _delegate; |
| 214 | |
| 215 | - (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory |
| 216 | configuration:(RTCConfiguration *)configuration |
| 217 | constraints:(RTCMediaConstraints *)constraints |
| 218 | delegate:(id<RTCPeerConnectionDelegate>)delegate { |
| 219 | NSParameterAssert(factory); |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 220 | std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration> config( |
hbos | a73ca56 | 2016-05-17 03:28:58 -0700 | [diff] [blame] | 221 | [configuration createNativeConfiguration]); |
| 222 | if (!config) { |
| 223 | return nil; |
| 224 | } |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 225 | if (self = [super init]) { |
| 226 | _observer.reset(new webrtc::PeerConnectionDelegateAdapter(self)); |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 227 | std::unique_ptr<webrtc::MediaConstraints> nativeConstraints = |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 228 | constraints.nativeConstraints; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 229 | _peerConnection = |
hbos | d7973cc | 2016-05-27 06:08:53 -0700 | [diff] [blame] | 230 | factory.nativeFactory->CreatePeerConnection(*config, |
| 231 | nativeConstraints.get(), |
| 232 | nullptr, |
| 233 | nullptr, |
| 234 | _observer.get()); |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 235 | _localStreams = [[NSMutableArray alloc] init]; |
| 236 | _delegate = delegate; |
| 237 | } |
| 238 | return self; |
| 239 | } |
| 240 | |
| 241 | - (NSArray *)localStreams { |
| 242 | return [_localStreams copy]; |
| 243 | } |
| 244 | |
| 245 | - (RTCSessionDescription *)localDescription { |
| 246 | const webrtc::SessionDescriptionInterface *description = |
| 247 | _peerConnection->local_description(); |
| 248 | return description ? |
| 249 | [[RTCSessionDescription alloc] initWithNativeDescription:description] |
| 250 | : nil; |
| 251 | } |
| 252 | |
| 253 | - (RTCSessionDescription *)remoteDescription { |
| 254 | const webrtc::SessionDescriptionInterface *description = |
| 255 | _peerConnection->remote_description(); |
| 256 | return description ? |
| 257 | [[RTCSessionDescription alloc] initWithNativeDescription:description] |
| 258 | : nil; |
| 259 | } |
| 260 | |
| 261 | - (RTCSignalingState)signalingState { |
| 262 | return [[self class] |
| 263 | signalingStateForNativeState:_peerConnection->signaling_state()]; |
| 264 | } |
| 265 | |
| 266 | - (RTCIceConnectionState)iceConnectionState { |
| 267 | return [[self class] iceConnectionStateForNativeState: |
| 268 | _peerConnection->ice_connection_state()]; |
| 269 | } |
| 270 | |
| 271 | - (RTCIceGatheringState)iceGatheringState { |
| 272 | return [[self class] iceGatheringStateForNativeState: |
| 273 | _peerConnection->ice_gathering_state()]; |
| 274 | } |
| 275 | |
tkchin | aac3eb2 | 2016-03-09 21:49:40 -0800 | [diff] [blame] | 276 | - (BOOL)setConfiguration:(RTCConfiguration *)configuration { |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 277 | std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration> config( |
hbos | a73ca56 | 2016-05-17 03:28:58 -0700 | [diff] [blame] | 278 | [configuration createNativeConfiguration]); |
| 279 | if (!config) { |
| 280 | return NO; |
| 281 | } |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 282 | return _peerConnection->SetConfiguration(*config); |
tkchin | aac3eb2 | 2016-03-09 21:49:40 -0800 | [diff] [blame] | 283 | } |
| 284 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 285 | - (void)close { |
| 286 | _peerConnection->Close(); |
| 287 | } |
| 288 | |
| 289 | - (void)addIceCandidate:(RTCIceCandidate *)candidate { |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 290 | std::unique_ptr<const webrtc::IceCandidateInterface> iceCandidate( |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 291 | candidate.nativeCandidate); |
| 292 | _peerConnection->AddIceCandidate(iceCandidate.get()); |
| 293 | } |
| 294 | |
Honghai Zhang | da2ba4d | 2016-05-23 11:53:14 -0700 | [diff] [blame] | 295 | - (void)removeIceCandidates:(NSArray<RTCIceCandidate *> *)iceCandidates { |
| 296 | std::vector<cricket::Candidate> candidates; |
| 297 | for (RTCIceCandidate *iceCandidate in iceCandidates) { |
| 298 | std::unique_ptr<const webrtc::IceCandidateInterface> candidate( |
| 299 | iceCandidate.nativeCandidate); |
| 300 | if (candidate) { |
| 301 | candidates.push_back(candidate->candidate()); |
| 302 | // Need to fill the transport name from the sdp_mid. |
| 303 | candidates.back().set_transport_name(candidate->sdp_mid()); |
| 304 | } |
| 305 | } |
| 306 | if (!candidates.empty()) { |
| 307 | _peerConnection->RemoveIceCandidates(candidates); |
| 308 | } |
| 309 | } |
| 310 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 311 | - (void)addStream:(RTCMediaStream *)stream { |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 312 | if (!_peerConnection->AddStream(stream.nativeMediaStream)) { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 313 | RTCLogError(@"Failed to add stream: %@", stream); |
| 314 | return; |
| 315 | } |
| 316 | [_localStreams addObject:stream]; |
| 317 | } |
| 318 | |
| 319 | - (void)removeStream:(RTCMediaStream *)stream { |
| 320 | _peerConnection->RemoveStream(stream.nativeMediaStream); |
| 321 | [_localStreams removeObject:stream]; |
| 322 | } |
| 323 | |
| 324 | - (void)offerForConstraints:(RTCMediaConstraints *)constraints |
| 325 | completionHandler: |
| 326 | (void (^)(RTCSessionDescription *sessionDescription, |
| 327 | NSError *error))completionHandler { |
| 328 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserverAdapter> |
| 329 | observer(new rtc::RefCountedObject |
| 330 | <webrtc::CreateSessionDescriptionObserverAdapter>(completionHandler)); |
| 331 | _peerConnection->CreateOffer(observer, constraints.nativeConstraints.get()); |
| 332 | } |
| 333 | |
| 334 | - (void)answerForConstraints:(RTCMediaConstraints *)constraints |
| 335 | completionHandler: |
| 336 | (void (^)(RTCSessionDescription *sessionDescription, |
| 337 | NSError *error))completionHandler { |
| 338 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserverAdapter> |
| 339 | observer(new rtc::RefCountedObject |
| 340 | <webrtc::CreateSessionDescriptionObserverAdapter>(completionHandler)); |
| 341 | _peerConnection->CreateAnswer(observer, constraints.nativeConstraints.get()); |
| 342 | } |
| 343 | |
| 344 | - (void)setLocalDescription:(RTCSessionDescription *)sdp |
| 345 | completionHandler:(void (^)(NSError *error))completionHandler { |
| 346 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserverAdapter> observer( |
| 347 | new rtc::RefCountedObject<webrtc::SetSessionDescriptionObserverAdapter>( |
| 348 | completionHandler)); |
| 349 | _peerConnection->SetLocalDescription(observer, sdp.nativeDescription); |
| 350 | } |
| 351 | |
| 352 | - (void)setRemoteDescription:(RTCSessionDescription *)sdp |
| 353 | completionHandler:(void (^)(NSError *error))completionHandler { |
| 354 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserverAdapter> observer( |
| 355 | new rtc::RefCountedObject<webrtc::SetSessionDescriptionObserverAdapter>( |
| 356 | completionHandler)); |
| 357 | _peerConnection->SetRemoteDescription(observer, sdp.nativeDescription); |
| 358 | } |
| 359 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 360 | - (BOOL)startRtcEventLogWithFilePath:(NSString *)filePath |
| 361 | maxSizeInBytes:(int64_t)maxSizeInBytes { |
| 362 | RTC_DCHECK(filePath.length); |
| 363 | RTC_DCHECK_GT(maxSizeInBytes, 0); |
| 364 | RTC_DCHECK(!_hasStartedRtcEventLog); |
| 365 | if (_hasStartedRtcEventLog) { |
| 366 | RTCLogError(@"Event logging already started."); |
| 367 | return NO; |
| 368 | } |
| 369 | int fd = open(filePath.UTF8String, O_WRONLY | O_CREAT | O_TRUNC, |
| 370 | S_IRUSR | S_IWUSR); |
| 371 | if (fd < 0) { |
| 372 | RTCLogError(@"Error opening file: %@. Error: %d", filePath, errno); |
| 373 | return NO; |
| 374 | } |
| 375 | _hasStartedRtcEventLog = |
| 376 | _peerConnection->StartRtcEventLog(fd, maxSizeInBytes); |
| 377 | return _hasStartedRtcEventLog; |
| 378 | } |
| 379 | |
| 380 | - (void)stopRtcEventLog { |
| 381 | _peerConnection->StopRtcEventLog(); |
| 382 | _hasStartedRtcEventLog = NO; |
| 383 | } |
| 384 | |
skvlad | f3569c8 | 2016-04-29 15:30:16 -0700 | [diff] [blame] | 385 | - (RTCRtpSender *)senderWithKind:(NSString *)kind |
| 386 | streamId:(NSString *)streamId { |
| 387 | std::string nativeKind = [NSString stdStringForString:kind]; |
| 388 | std::string nativeStreamId = [NSString stdStringForString:streamId]; |
| 389 | rtc::scoped_refptr<webrtc::RtpSenderInterface> nativeSender( |
| 390 | _peerConnection->CreateSender(nativeKind, nativeStreamId)); |
| 391 | return nativeSender ? |
| 392 | [[RTCRtpSender alloc] initWithNativeRtpSender:nativeSender] |
| 393 | : nil; |
| 394 | } |
| 395 | |
skvlad | 79b4b87 | 2016-04-08 17:28:55 -0700 | [diff] [blame] | 396 | - (NSArray<RTCRtpSender *> *)senders { |
| 397 | std::vector<rtc::scoped_refptr<webrtc::RtpSenderInterface>> nativeSenders( |
| 398 | _peerConnection->GetSenders()); |
| 399 | NSMutableArray *senders = [[NSMutableArray alloc] init]; |
| 400 | for (const auto &nativeSender : nativeSenders) { |
| 401 | RTCRtpSender *sender = |
| 402 | [[RTCRtpSender alloc] initWithNativeRtpSender:nativeSender]; |
| 403 | [senders addObject:sender]; |
| 404 | } |
| 405 | return senders; |
| 406 | } |
| 407 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 408 | - (NSArray<RTCRtpReceiver *> *)receivers { |
| 409 | std::vector<rtc::scoped_refptr<webrtc::RtpReceiverInterface>> nativeReceivers( |
| 410 | _peerConnection->GetReceivers()); |
| 411 | NSMutableArray *receivers = [[NSMutableArray alloc] init]; |
| 412 | for (const auto &nativeReceiver : nativeReceivers) { |
| 413 | RTCRtpReceiver *receiver = |
| 414 | [[RTCRtpReceiver alloc] initWithNativeRtpReceiver:nativeReceiver]; |
| 415 | [receivers addObject:receiver]; |
| 416 | } |
| 417 | return receivers; |
| 418 | } |
| 419 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 420 | #pragma mark - Private |
| 421 | |
| 422 | + (webrtc::PeerConnectionInterface::SignalingState)nativeSignalingStateForState: |
| 423 | (RTCSignalingState)state { |
| 424 | switch (state) { |
| 425 | case RTCSignalingStateStable: |
| 426 | return webrtc::PeerConnectionInterface::kStable; |
| 427 | case RTCSignalingStateHaveLocalOffer: |
| 428 | return webrtc::PeerConnectionInterface::kHaveLocalOffer; |
| 429 | case RTCSignalingStateHaveLocalPrAnswer: |
| 430 | return webrtc::PeerConnectionInterface::kHaveLocalPrAnswer; |
| 431 | case RTCSignalingStateHaveRemoteOffer: |
| 432 | return webrtc::PeerConnectionInterface::kHaveRemoteOffer; |
| 433 | case RTCSignalingStateHaveRemotePrAnswer: |
| 434 | return webrtc::PeerConnectionInterface::kHaveRemotePrAnswer; |
| 435 | case RTCSignalingStateClosed: |
| 436 | return webrtc::PeerConnectionInterface::kClosed; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | + (RTCSignalingState)signalingStateForNativeState: |
| 441 | (webrtc::PeerConnectionInterface::SignalingState)nativeState { |
| 442 | switch (nativeState) { |
| 443 | case webrtc::PeerConnectionInterface::kStable: |
| 444 | return RTCSignalingStateStable; |
| 445 | case webrtc::PeerConnectionInterface::kHaveLocalOffer: |
| 446 | return RTCSignalingStateHaveLocalOffer; |
| 447 | case webrtc::PeerConnectionInterface::kHaveLocalPrAnswer: |
| 448 | return RTCSignalingStateHaveLocalPrAnswer; |
| 449 | case webrtc::PeerConnectionInterface::kHaveRemoteOffer: |
| 450 | return RTCSignalingStateHaveRemoteOffer; |
| 451 | case webrtc::PeerConnectionInterface::kHaveRemotePrAnswer: |
| 452 | return RTCSignalingStateHaveRemotePrAnswer; |
| 453 | case webrtc::PeerConnectionInterface::kClosed: |
| 454 | return RTCSignalingStateClosed; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | + (NSString *)stringForSignalingState:(RTCSignalingState)state { |
| 459 | switch (state) { |
| 460 | case RTCSignalingStateStable: |
| 461 | return @"STABLE"; |
| 462 | case RTCSignalingStateHaveLocalOffer: |
| 463 | return @"HAVE_LOCAL_OFFER"; |
| 464 | case RTCSignalingStateHaveLocalPrAnswer: |
| 465 | return @"HAVE_LOCAL_PRANSWER"; |
| 466 | case RTCSignalingStateHaveRemoteOffer: |
| 467 | return @"HAVE_REMOTE_OFFER"; |
| 468 | case RTCSignalingStateHaveRemotePrAnswer: |
| 469 | return @"HAVE_REMOTE_PRANSWER"; |
| 470 | case RTCSignalingStateClosed: |
| 471 | return @"CLOSED"; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | + (webrtc::PeerConnectionInterface::IceConnectionState) |
| 476 | nativeIceConnectionStateForState:(RTCIceConnectionState)state { |
| 477 | switch (state) { |
| 478 | case RTCIceConnectionStateNew: |
| 479 | return webrtc::PeerConnectionInterface::kIceConnectionNew; |
| 480 | case RTCIceConnectionStateChecking: |
| 481 | return webrtc::PeerConnectionInterface::kIceConnectionChecking; |
| 482 | case RTCIceConnectionStateConnected: |
| 483 | return webrtc::PeerConnectionInterface::kIceConnectionConnected; |
| 484 | case RTCIceConnectionStateCompleted: |
| 485 | return webrtc::PeerConnectionInterface::kIceConnectionCompleted; |
| 486 | case RTCIceConnectionStateFailed: |
| 487 | return webrtc::PeerConnectionInterface::kIceConnectionFailed; |
| 488 | case RTCIceConnectionStateDisconnected: |
| 489 | return webrtc::PeerConnectionInterface::kIceConnectionDisconnected; |
| 490 | case RTCIceConnectionStateClosed: |
| 491 | return webrtc::PeerConnectionInterface::kIceConnectionClosed; |
hjon | 8bbbf2c | 2016-03-14 13:15:44 -0700 | [diff] [blame] | 492 | case RTCIceConnectionStateCount: |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 493 | return webrtc::PeerConnectionInterface::kIceConnectionMax; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | + (RTCIceConnectionState)iceConnectionStateForNativeState: |
| 498 | (webrtc::PeerConnectionInterface::IceConnectionState)nativeState { |
| 499 | switch (nativeState) { |
| 500 | case webrtc::PeerConnectionInterface::kIceConnectionNew: |
| 501 | return RTCIceConnectionStateNew; |
| 502 | case webrtc::PeerConnectionInterface::kIceConnectionChecking: |
| 503 | return RTCIceConnectionStateChecking; |
| 504 | case webrtc::PeerConnectionInterface::kIceConnectionConnected: |
| 505 | return RTCIceConnectionStateConnected; |
| 506 | case webrtc::PeerConnectionInterface::kIceConnectionCompleted: |
| 507 | return RTCIceConnectionStateCompleted; |
| 508 | case webrtc::PeerConnectionInterface::kIceConnectionFailed: |
| 509 | return RTCIceConnectionStateFailed; |
| 510 | case webrtc::PeerConnectionInterface::kIceConnectionDisconnected: |
| 511 | return RTCIceConnectionStateDisconnected; |
| 512 | case webrtc::PeerConnectionInterface::kIceConnectionClosed: |
| 513 | return RTCIceConnectionStateClosed; |
| 514 | case webrtc::PeerConnectionInterface::kIceConnectionMax: |
hjon | 8bbbf2c | 2016-03-14 13:15:44 -0700 | [diff] [blame] | 515 | return RTCIceConnectionStateCount; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | + (NSString *)stringForIceConnectionState:(RTCIceConnectionState)state { |
| 520 | switch (state) { |
| 521 | case RTCIceConnectionStateNew: |
| 522 | return @"NEW"; |
| 523 | case RTCIceConnectionStateChecking: |
| 524 | return @"CHECKING"; |
| 525 | case RTCIceConnectionStateConnected: |
| 526 | return @"CONNECTED"; |
| 527 | case RTCIceConnectionStateCompleted: |
| 528 | return @"COMPLETED"; |
| 529 | case RTCIceConnectionStateFailed: |
| 530 | return @"FAILED"; |
| 531 | case RTCIceConnectionStateDisconnected: |
| 532 | return @"DISCONNECTED"; |
| 533 | case RTCIceConnectionStateClosed: |
| 534 | return @"CLOSED"; |
hjon | 8bbbf2c | 2016-03-14 13:15:44 -0700 | [diff] [blame] | 535 | case RTCIceConnectionStateCount: |
| 536 | return @"COUNT"; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | + (webrtc::PeerConnectionInterface::IceGatheringState) |
| 541 | nativeIceGatheringStateForState:(RTCIceGatheringState)state { |
| 542 | switch (state) { |
| 543 | case RTCIceGatheringStateNew: |
| 544 | return webrtc::PeerConnectionInterface::kIceGatheringNew; |
| 545 | case RTCIceGatheringStateGathering: |
| 546 | return webrtc::PeerConnectionInterface::kIceGatheringGathering; |
| 547 | case RTCIceGatheringStateComplete: |
| 548 | return webrtc::PeerConnectionInterface::kIceGatheringComplete; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | + (RTCIceGatheringState)iceGatheringStateForNativeState: |
| 553 | (webrtc::PeerConnectionInterface::IceGatheringState)nativeState { |
| 554 | switch (nativeState) { |
| 555 | case webrtc::PeerConnectionInterface::kIceGatheringNew: |
| 556 | return RTCIceGatheringStateNew; |
| 557 | case webrtc::PeerConnectionInterface::kIceGatheringGathering: |
| 558 | return RTCIceGatheringStateGathering; |
| 559 | case webrtc::PeerConnectionInterface::kIceGatheringComplete: |
| 560 | return RTCIceGatheringStateComplete; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | + (NSString *)stringForIceGatheringState:(RTCIceGatheringState)state { |
| 565 | switch (state) { |
| 566 | case RTCIceGatheringStateNew: |
| 567 | return @"NEW"; |
| 568 | case RTCIceGatheringStateGathering: |
| 569 | return @"GATHERING"; |
| 570 | case RTCIceGatheringStateComplete: |
| 571 | return @"COMPLETE"; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | + (webrtc::PeerConnectionInterface::StatsOutputLevel) |
| 576 | nativeStatsOutputLevelForLevel:(RTCStatsOutputLevel)level { |
| 577 | switch (level) { |
| 578 | case RTCStatsOutputLevelStandard: |
| 579 | return webrtc::PeerConnectionInterface::kStatsOutputLevelStandard; |
| 580 | case RTCStatsOutputLevelDebug: |
| 581 | return webrtc::PeerConnectionInterface::kStatsOutputLevelDebug; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | - (rtc::scoped_refptr<webrtc::PeerConnectionInterface>)nativePeerConnection { |
| 586 | return _peerConnection; |
| 587 | } |
| 588 | |
| 589 | @end |