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