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 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 13 | #import "RTCConfiguration+Private.h" |
| 14 | #import "RTCDataChannel+Private.h" |
| 15 | #import "RTCIceCandidate+Private.h" |
hbos | bd3dda6 | 2016-09-09 01:36:28 -0700 | [diff] [blame] | 16 | #import "RTCLegacyStatsReport+Private.h" |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 17 | #import "RTCMediaConstraints+Private.h" |
| 18 | #import "RTCMediaStream+Private.h" |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 19 | #import "RTCMediaStreamTrack+Private.h" |
Magnus Jedvert | 0af86d1 | 2017-10-28 16:26:55 +0200 | [diff] [blame] | 20 | #import "RTCPeerConnection+Native.h" |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 21 | #import "RTCPeerConnectionFactory+Private.h" |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 22 | #import "RTCRtpReceiver+Private.h" |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 23 | #import "RTCRtpSender+Private.h" |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 24 | #import "RTCRtpTransceiver+Private.h" |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 25 | #import "RTCSessionDescription+Private.h" |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 26 | #import "base/RTCLogging.h" |
| 27 | #import "helpers/NSString+StdString.h" |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 28 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 29 | #include <memory> |
| 30 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 31 | #include "api/jsep_ice_candidate.h" |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 32 | #include "api/media_transport_interface.h" |
Niels Möller | 695cf6a | 2019-05-13 12:27:23 +0200 | [diff] [blame] | 33 | #include "api/rtc_event_log_output_file.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 34 | #include "rtc_base/checks.h" |
Niels Möller | 695cf6a | 2019-05-13 12:27:23 +0200 | [diff] [blame] | 35 | #include "rtc_base/numerics/safe_conversions.h" |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 36 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 37 | NSString * const kRTCPeerConnectionErrorDomain = |
| 38 | @"org.webrtc.RTCPeerConnection"; |
| 39 | int const kRTCPeerConnnectionSessionDescriptionError = -1; |
| 40 | |
| 41 | namespace webrtc { |
| 42 | |
| 43 | class CreateSessionDescriptionObserverAdapter |
| 44 | : public CreateSessionDescriptionObserver { |
| 45 | public: |
| 46 | CreateSessionDescriptionObserverAdapter( |
| 47 | void (^completionHandler)(RTCSessionDescription *sessionDescription, |
| 48 | NSError *error)) { |
| 49 | completion_handler_ = completionHandler; |
| 50 | } |
| 51 | |
Mirko Bonadei | 17aff35 | 2018-07-26 12:20:40 +0200 | [diff] [blame] | 52 | ~CreateSessionDescriptionObserverAdapter() override { completion_handler_ = nil; } |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 53 | |
| 54 | void OnSuccess(SessionDescriptionInterface *desc) override { |
| 55 | RTC_DCHECK(completion_handler_); |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 56 | std::unique_ptr<webrtc::SessionDescriptionInterface> description = |
| 57 | std::unique_ptr<webrtc::SessionDescriptionInterface>(desc); |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 58 | RTCSessionDescription* session = |
| 59 | [[RTCSessionDescription alloc] initWithNativeDescription: |
| 60 | description.get()]; |
| 61 | completion_handler_(session, nil); |
| 62 | completion_handler_ = nil; |
| 63 | } |
| 64 | |
Harald Alvestrand | 73771a8 | 2018-05-24 10:53:49 +0200 | [diff] [blame] | 65 | void OnFailure(RTCError error) override { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 66 | RTC_DCHECK(completion_handler_); |
Harald Alvestrand | 73771a8 | 2018-05-24 10:53:49 +0200 | [diff] [blame] | 67 | // TODO(hta): Add handling of error.type() |
| 68 | NSString *str = [NSString stringForStdString:error.message()]; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 69 | NSError* err = |
| 70 | [NSError errorWithDomain:kRTCPeerConnectionErrorDomain |
| 71 | code:kRTCPeerConnnectionSessionDescriptionError |
| 72 | userInfo:@{ NSLocalizedDescriptionKey : str }]; |
| 73 | completion_handler_(nil, err); |
| 74 | completion_handler_ = nil; |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | void (^completion_handler_) |
| 79 | (RTCSessionDescription *sessionDescription, NSError *error); |
| 80 | }; |
| 81 | |
| 82 | class SetSessionDescriptionObserverAdapter : |
| 83 | public SetSessionDescriptionObserver { |
| 84 | public: |
| 85 | SetSessionDescriptionObserverAdapter(void (^completionHandler) |
| 86 | (NSError *error)) { |
| 87 | completion_handler_ = completionHandler; |
| 88 | } |
| 89 | |
Mirko Bonadei | 17aff35 | 2018-07-26 12:20:40 +0200 | [diff] [blame] | 90 | ~SetSessionDescriptionObserverAdapter() override { completion_handler_ = nil; } |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 91 | |
| 92 | void OnSuccess() override { |
| 93 | RTC_DCHECK(completion_handler_); |
| 94 | completion_handler_(nil); |
| 95 | completion_handler_ = nil; |
| 96 | } |
| 97 | |
Harald Alvestrand | 73771a8 | 2018-05-24 10:53:49 +0200 | [diff] [blame] | 98 | void OnFailure(RTCError error) override { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 99 | RTC_DCHECK(completion_handler_); |
Harald Alvestrand | 73771a8 | 2018-05-24 10:53:49 +0200 | [diff] [blame] | 100 | // TODO(hta): Add handling of error.type() |
| 101 | NSString *str = [NSString stringForStdString:error.message()]; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 102 | NSError* err = |
| 103 | [NSError errorWithDomain:kRTCPeerConnectionErrorDomain |
| 104 | code:kRTCPeerConnnectionSessionDescriptionError |
| 105 | userInfo:@{ NSLocalizedDescriptionKey : str }]; |
| 106 | completion_handler_(err); |
| 107 | completion_handler_ = nil; |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | void (^completion_handler_)(NSError *error); |
| 112 | }; |
| 113 | |
| 114 | PeerConnectionDelegateAdapter::PeerConnectionDelegateAdapter( |
| 115 | RTCPeerConnection *peerConnection) { |
| 116 | peer_connection_ = peerConnection; |
| 117 | } |
| 118 | |
| 119 | PeerConnectionDelegateAdapter::~PeerConnectionDelegateAdapter() { |
| 120 | peer_connection_ = nil; |
| 121 | } |
| 122 | |
| 123 | void PeerConnectionDelegateAdapter::OnSignalingChange( |
| 124 | PeerConnectionInterface::SignalingState new_state) { |
| 125 | RTCSignalingState state = |
| 126 | [[RTCPeerConnection class] signalingStateForNativeState:new_state]; |
| 127 | RTCPeerConnection *peer_connection = peer_connection_; |
| 128 | [peer_connection.delegate peerConnection:peer_connection |
| 129 | didChangeSignalingState:state]; |
| 130 | } |
| 131 | |
| 132 | void PeerConnectionDelegateAdapter::OnAddStream( |
deadbeef | d5f41ce | 2016-06-08 13:31:45 -0700 | [diff] [blame] | 133 | rtc::scoped_refptr<MediaStreamInterface> stream) { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 134 | RTCPeerConnection *peer_connection = peer_connection_; |
Yura Yaroshevich | c806c1d | 2018-06-21 12:51:11 +0300 | [diff] [blame] | 135 | RTCMediaStream *mediaStream = |
| 136 | [[RTCMediaStream alloc] initWithFactory:peer_connection.factory nativeMediaStream:stream]; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 137 | [peer_connection.delegate peerConnection:peer_connection |
| 138 | didAddStream:mediaStream]; |
| 139 | } |
| 140 | |
| 141 | void PeerConnectionDelegateAdapter::OnRemoveStream( |
deadbeef | d5f41ce | 2016-06-08 13:31:45 -0700 | [diff] [blame] | 142 | rtc::scoped_refptr<MediaStreamInterface> stream) { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 143 | RTCPeerConnection *peer_connection = peer_connection_; |
Yura Yaroshevich | c806c1d | 2018-06-21 12:51:11 +0300 | [diff] [blame] | 144 | RTCMediaStream *mediaStream = |
| 145 | [[RTCMediaStream alloc] initWithFactory:peer_connection.factory nativeMediaStream:stream]; |
| 146 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 147 | [peer_connection.delegate peerConnection:peer_connection |
| 148 | didRemoveStream:mediaStream]; |
| 149 | } |
| 150 | |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 151 | void PeerConnectionDelegateAdapter::OnTrack( |
| 152 | rtc::scoped_refptr<RtpTransceiverInterface> nativeTransceiver) { |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 153 | RTCPeerConnection *peer_connection = peer_connection_; |
Yura Yaroshevich | 08f14dd | 2018-07-09 11:56:06 +0300 | [diff] [blame] | 154 | RTCRtpTransceiver *transceiver = |
| 155 | [[RTCRtpTransceiver alloc] initWithFactory:peer_connection.factory |
| 156 | nativeRtpTransceiver:nativeTransceiver]; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 157 | if ([peer_connection.delegate |
| 158 | respondsToSelector:@selector(peerConnection:didStartReceivingOnTransceiver:)]) { |
| 159 | [peer_connection.delegate peerConnection:peer_connection |
| 160 | didStartReceivingOnTransceiver:transceiver]; |
| 161 | } |
| 162 | } |
| 163 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 164 | void PeerConnectionDelegateAdapter::OnDataChannel( |
deadbeef | d5f41ce | 2016-06-08 13:31:45 -0700 | [diff] [blame] | 165 | rtc::scoped_refptr<DataChannelInterface> data_channel) { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 166 | RTCPeerConnection *peer_connection = peer_connection_; |
Yura Yaroshevich | c75b35a | 2018-06-27 17:09:14 +0300 | [diff] [blame] | 167 | RTCDataChannel *dataChannel = [[RTCDataChannel alloc] initWithFactory:peer_connection.factory |
| 168 | nativeDataChannel:data_channel]; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 169 | [peer_connection.delegate peerConnection:peer_connection |
| 170 | didOpenDataChannel:dataChannel]; |
| 171 | } |
| 172 | |
| 173 | void PeerConnectionDelegateAdapter::OnRenegotiationNeeded() { |
| 174 | RTCPeerConnection *peer_connection = peer_connection_; |
| 175 | [peer_connection.delegate peerConnectionShouldNegotiate:peer_connection]; |
| 176 | } |
| 177 | |
| 178 | void PeerConnectionDelegateAdapter::OnIceConnectionChange( |
| 179 | PeerConnectionInterface::IceConnectionState new_state) { |
Jonas Olsson | cfddbb7 | 2018-11-15 16:52:45 +0100 | [diff] [blame] | 180 | RTCIceConnectionState state = [RTCPeerConnection iceConnectionStateForNativeState:new_state]; |
| 181 | [peer_connection_.delegate peerConnection:peer_connection_ didChangeIceConnectionState:state]; |
| 182 | } |
| 183 | |
Qingsi Wang | 36e3147 | 2019-05-29 11:37:26 -0700 | [diff] [blame] | 184 | void PeerConnectionDelegateAdapter::OnStandardizedIceConnectionChange( |
| 185 | PeerConnectionInterface::IceConnectionState new_state) { |
| 186 | if ([peer_connection_.delegate |
| 187 | respondsToSelector:@selector(peerConnection:didChangeStandardizedIceConnectionState:)]) { |
| 188 | RTCIceConnectionState state = [RTCPeerConnection iceConnectionStateForNativeState:new_state]; |
| 189 | [peer_connection_.delegate peerConnection:peer_connection_ |
| 190 | didChangeStandardizedIceConnectionState:state]; |
| 191 | } |
| 192 | } |
| 193 | |
Jonas Olsson | cfddbb7 | 2018-11-15 16:52:45 +0100 | [diff] [blame] | 194 | void PeerConnectionDelegateAdapter::OnConnectionChange( |
| 195 | PeerConnectionInterface::PeerConnectionState new_state) { |
| 196 | if ([peer_connection_.delegate |
| 197 | respondsToSelector:@selector(peerConnection:didChangeConnectionState:)]) { |
| 198 | RTCPeerConnectionState state = [RTCPeerConnection connectionStateForNativeState:new_state]; |
| 199 | [peer_connection_.delegate peerConnection:peer_connection_ didChangeConnectionState:state]; |
| 200 | } |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void PeerConnectionDelegateAdapter::OnIceGatheringChange( |
| 204 | PeerConnectionInterface::IceGatheringState new_state) { |
| 205 | RTCIceGatheringState state = |
| 206 | [[RTCPeerConnection class] iceGatheringStateForNativeState:new_state]; |
| 207 | RTCPeerConnection *peer_connection = peer_connection_; |
| 208 | [peer_connection.delegate peerConnection:peer_connection |
| 209 | didChangeIceGatheringState:state]; |
| 210 | } |
| 211 | |
| 212 | void PeerConnectionDelegateAdapter::OnIceCandidate( |
| 213 | const IceCandidateInterface *candidate) { |
| 214 | RTCIceCandidate *iceCandidate = |
| 215 | [[RTCIceCandidate alloc] initWithNativeCandidate:candidate]; |
| 216 | RTCPeerConnection *peer_connection = peer_connection_; |
| 217 | [peer_connection.delegate peerConnection:peer_connection |
| 218 | didGenerateIceCandidate:iceCandidate]; |
| 219 | } |
Honghai Zhang | da2ba4d | 2016-05-23 11:53:14 -0700 | [diff] [blame] | 220 | |
| 221 | void PeerConnectionDelegateAdapter::OnIceCandidatesRemoved( |
| 222 | const std::vector<cricket::Candidate>& candidates) { |
| 223 | NSMutableArray* ice_candidates = |
| 224 | [NSMutableArray arrayWithCapacity:candidates.size()]; |
| 225 | for (const auto& candidate : candidates) { |
| 226 | std::unique_ptr<JsepIceCandidate> candidate_wrapper( |
| 227 | new JsepIceCandidate(candidate.transport_name(), -1, candidate)); |
| 228 | RTCIceCandidate* ice_candidate = [[RTCIceCandidate alloc] |
| 229 | initWithNativeCandidate:candidate_wrapper.get()]; |
| 230 | [ice_candidates addObject:ice_candidate]; |
| 231 | } |
| 232 | RTCPeerConnection* peer_connection = peer_connection_; |
| 233 | [peer_connection.delegate peerConnection:peer_connection |
| 234 | didRemoveIceCandidates:ice_candidates]; |
| 235 | } |
| 236 | |
Yura Yaroshevich | 546d7f9 | 2018-02-28 21:06:34 +0300 | [diff] [blame] | 237 | void PeerConnectionDelegateAdapter::OnAddTrack( |
| 238 | rtc::scoped_refptr<RtpReceiverInterface> receiver, |
| 239 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) { |
| 240 | RTCPeerConnection *peer_connection = peer_connection_; |
| 241 | if ([peer_connection.delegate |
| 242 | respondsToSelector:@selector(peerConnection:didAddReceiver:streams:)]) { |
| 243 | NSMutableArray *mediaStreams = [NSMutableArray arrayWithCapacity:streams.size()]; |
| 244 | for (const auto& nativeStream : streams) { |
Yura Yaroshevich | c806c1d | 2018-06-21 12:51:11 +0300 | [diff] [blame] | 245 | RTCMediaStream *mediaStream = [[RTCMediaStream alloc] initWithFactory:peer_connection.factory |
| 246 | nativeMediaStream:nativeStream]; |
Yura Yaroshevich | 546d7f9 | 2018-02-28 21:06:34 +0300 | [diff] [blame] | 247 | [mediaStreams addObject:mediaStream]; |
| 248 | } |
Yura Yaroshevich | 7a16c54 | 2018-07-11 12:55:04 +0300 | [diff] [blame] | 249 | RTCRtpReceiver *rtpReceiver = |
| 250 | [[RTCRtpReceiver alloc] initWithFactory:peer_connection.factory nativeRtpReceiver:receiver]; |
Yura Yaroshevich | 546d7f9 | 2018-02-28 21:06:34 +0300 | [diff] [blame] | 251 | |
| 252 | [peer_connection.delegate peerConnection:peer_connection |
| 253 | didAddReceiver:rtpReceiver |
| 254 | streams:mediaStreams]; |
| 255 | } |
| 256 | } |
| 257 | |
Zeke Chin | 8de502b | 2018-08-21 11:41:07 -0700 | [diff] [blame] | 258 | void PeerConnectionDelegateAdapter::OnRemoveTrack( |
| 259 | rtc::scoped_refptr<RtpReceiverInterface> receiver) { |
| 260 | RTCPeerConnection *peer_connection = peer_connection_; |
| 261 | if ([peer_connection.delegate respondsToSelector:@selector(peerConnection:didRemoveReceiver:)]) { |
| 262 | RTCRtpReceiver *rtpReceiver = |
| 263 | [[RTCRtpReceiver alloc] initWithFactory:peer_connection.factory nativeRtpReceiver:receiver]; |
| 264 | [peer_connection.delegate peerConnection:peer_connection didRemoveReceiver:rtpReceiver]; |
| 265 | } |
| 266 | } |
| 267 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 268 | } // namespace webrtc |
| 269 | |
| 270 | |
| 271 | @implementation RTCPeerConnection { |
Yura Yaroshevich | 5297bd2 | 2018-06-19 12:51:51 +0300 | [diff] [blame] | 272 | RTCPeerConnectionFactory *_factory; |
vopatop.skam | 96b6b83 | 2016-08-18 14:21:20 -0700 | [diff] [blame] | 273 | NSMutableArray<RTCMediaStream *> *_localStreams; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 274 | std::unique_ptr<webrtc::PeerConnectionDelegateAdapter> _observer; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 275 | rtc::scoped_refptr<webrtc::PeerConnectionInterface> _peerConnection; |
deadbeef | 5d0b6d8 | 2017-01-09 16:05:28 -0800 | [diff] [blame] | 276 | std::unique_ptr<webrtc::MediaConstraints> _nativeConstraints; |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 277 | BOOL _hasStartedRtcEventLog; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | @synthesize delegate = _delegate; |
Yura Yaroshevich | c806c1d | 2018-06-21 12:51:11 +0300 | [diff] [blame] | 281 | @synthesize factory = _factory; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 282 | |
| 283 | - (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory |
| 284 | configuration:(RTCConfiguration *)configuration |
| 285 | constraints:(RTCMediaConstraints *)constraints |
| 286 | delegate:(id<RTCPeerConnectionDelegate>)delegate { |
| 287 | NSParameterAssert(factory); |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 288 | std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration> config( |
hbos | a73ca56 | 2016-05-17 03:28:58 -0700 | [diff] [blame] | 289 | [configuration createNativeConfiguration]); |
| 290 | if (!config) { |
| 291 | return nil; |
| 292 | } |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 293 | if (self = [super init]) { |
| 294 | _observer.reset(new webrtc::PeerConnectionDelegateAdapter(self)); |
deadbeef | 5d0b6d8 | 2017-01-09 16:05:28 -0800 | [diff] [blame] | 295 | _nativeConstraints = constraints.nativeConstraints; |
| 296 | CopyConstraintsIntoRtcConfiguration(_nativeConstraints.get(), |
| 297 | config.get()); |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 298 | _peerConnection = |
hbos | d7973cc | 2016-05-27 06:08:53 -0700 | [diff] [blame] | 299 | factory.nativeFactory->CreatePeerConnection(*config, |
hbos | d7973cc | 2016-05-27 06:08:53 -0700 | [diff] [blame] | 300 | nullptr, |
| 301 | nullptr, |
| 302 | _observer.get()); |
skvlad | 588783a | 2016-08-11 14:29:25 -0700 | [diff] [blame] | 303 | if (!_peerConnection) { |
| 304 | return nil; |
| 305 | } |
Yura Yaroshevich | 5297bd2 | 2018-06-19 12:51:51 +0300 | [diff] [blame] | 306 | _factory = factory; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 307 | _localStreams = [[NSMutableArray alloc] init]; |
| 308 | _delegate = delegate; |
| 309 | } |
| 310 | return self; |
| 311 | } |
| 312 | |
vopatop.skam | 96b6b83 | 2016-08-18 14:21:20 -0700 | [diff] [blame] | 313 | - (NSArray<RTCMediaStream *> *)localStreams { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 314 | return [_localStreams copy]; |
| 315 | } |
| 316 | |
| 317 | - (RTCSessionDescription *)localDescription { |
| 318 | const webrtc::SessionDescriptionInterface *description = |
| 319 | _peerConnection->local_description(); |
| 320 | return description ? |
| 321 | [[RTCSessionDescription alloc] initWithNativeDescription:description] |
| 322 | : nil; |
| 323 | } |
| 324 | |
| 325 | - (RTCSessionDescription *)remoteDescription { |
| 326 | const webrtc::SessionDescriptionInterface *description = |
| 327 | _peerConnection->remote_description(); |
| 328 | return description ? |
| 329 | [[RTCSessionDescription alloc] initWithNativeDescription:description] |
| 330 | : nil; |
| 331 | } |
| 332 | |
| 333 | - (RTCSignalingState)signalingState { |
| 334 | return [[self class] |
| 335 | signalingStateForNativeState:_peerConnection->signaling_state()]; |
| 336 | } |
| 337 | |
| 338 | - (RTCIceConnectionState)iceConnectionState { |
| 339 | return [[self class] iceConnectionStateForNativeState: |
| 340 | _peerConnection->ice_connection_state()]; |
| 341 | } |
| 342 | |
Jonas Olsson | cfddbb7 | 2018-11-15 16:52:45 +0100 | [diff] [blame] | 343 | - (RTCPeerConnectionState)connectionState { |
| 344 | return [[self class] connectionStateForNativeState:_peerConnection->peer_connection_state()]; |
| 345 | } |
| 346 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 347 | - (RTCIceGatheringState)iceGatheringState { |
| 348 | return [[self class] iceGatheringStateForNativeState: |
| 349 | _peerConnection->ice_gathering_state()]; |
| 350 | } |
| 351 | |
tkchin | aac3eb2 | 2016-03-09 21:49:40 -0800 | [diff] [blame] | 352 | - (BOOL)setConfiguration:(RTCConfiguration *)configuration { |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 353 | std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration> config( |
hbos | a73ca56 | 2016-05-17 03:28:58 -0700 | [diff] [blame] | 354 | [configuration createNativeConfiguration]); |
| 355 | if (!config) { |
| 356 | return NO; |
| 357 | } |
deadbeef | 5d0b6d8 | 2017-01-09 16:05:28 -0800 | [diff] [blame] | 358 | CopyConstraintsIntoRtcConfiguration(_nativeConstraints.get(), |
| 359 | config.get()); |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 360 | return _peerConnection->SetConfiguration(*config); |
tkchin | aac3eb2 | 2016-03-09 21:49:40 -0800 | [diff] [blame] | 361 | } |
| 362 | |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 363 | - (RTCConfiguration *)configuration { |
| 364 | webrtc::PeerConnectionInterface::RTCConfiguration config = |
| 365 | _peerConnection->GetConfiguration(); |
jtteh | 465faf0 | 2017-04-04 14:00:16 -0700 | [diff] [blame] | 366 | return [[RTCConfiguration alloc] initWithNativeConfiguration:config]; |
jtteh | 4eeb537 | 2017-04-03 15:06:37 -0700 | [diff] [blame] | 367 | } |
| 368 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 369 | - (void)close { |
| 370 | _peerConnection->Close(); |
| 371 | } |
| 372 | |
| 373 | - (void)addIceCandidate:(RTCIceCandidate *)candidate { |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 374 | std::unique_ptr<const webrtc::IceCandidateInterface> iceCandidate( |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 375 | candidate.nativeCandidate); |
| 376 | _peerConnection->AddIceCandidate(iceCandidate.get()); |
| 377 | } |
| 378 | |
Honghai Zhang | da2ba4d | 2016-05-23 11:53:14 -0700 | [diff] [blame] | 379 | - (void)removeIceCandidates:(NSArray<RTCIceCandidate *> *)iceCandidates { |
| 380 | std::vector<cricket::Candidate> candidates; |
| 381 | for (RTCIceCandidate *iceCandidate in iceCandidates) { |
| 382 | std::unique_ptr<const webrtc::IceCandidateInterface> candidate( |
| 383 | iceCandidate.nativeCandidate); |
| 384 | if (candidate) { |
| 385 | candidates.push_back(candidate->candidate()); |
| 386 | // Need to fill the transport name from the sdp_mid. |
| 387 | candidates.back().set_transport_name(candidate->sdp_mid()); |
| 388 | } |
| 389 | } |
| 390 | if (!candidates.empty()) { |
| 391 | _peerConnection->RemoveIceCandidates(candidates); |
| 392 | } |
| 393 | } |
| 394 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 395 | - (void)addStream:(RTCMediaStream *)stream { |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 396 | if (!_peerConnection->AddStream(stream.nativeMediaStream)) { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 397 | RTCLogError(@"Failed to add stream: %@", stream); |
| 398 | return; |
| 399 | } |
| 400 | [_localStreams addObject:stream]; |
| 401 | } |
| 402 | |
| 403 | - (void)removeStream:(RTCMediaStream *)stream { |
| 404 | _peerConnection->RemoveStream(stream.nativeMediaStream); |
| 405 | [_localStreams removeObject:stream]; |
| 406 | } |
| 407 | |
Seth Hampson | 513449e | 2018-03-06 09:35:56 -0800 | [diff] [blame] | 408 | - (RTCRtpSender *)addTrack:(RTCMediaStreamTrack *)track streamIds:(NSArray<NSString *> *)streamIds { |
| 409 | std::vector<std::string> nativeStreamIds; |
| 410 | for (NSString *streamId in streamIds) { |
| 411 | nativeStreamIds.push_back([streamId UTF8String]); |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 412 | } |
| 413 | webrtc::RTCErrorOr<rtc::scoped_refptr<webrtc::RtpSenderInterface>> nativeSenderOrError = |
Seth Hampson | 513449e | 2018-03-06 09:35:56 -0800 | [diff] [blame] | 414 | _peerConnection->AddTrack(track.nativeTrack, nativeStreamIds); |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 415 | if (!nativeSenderOrError.ok()) { |
| 416 | RTCLogError(@"Failed to add track %@: %s", track, nativeSenderOrError.error().message()); |
| 417 | return nil; |
| 418 | } |
Yura Yaroshevich | ef43aaf | 2018-07-09 19:16:32 +0300 | [diff] [blame] | 419 | return [[RTCRtpSender alloc] initWithFactory:self.factory |
| 420 | nativeRtpSender:nativeSenderOrError.MoveValue()]; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | - (BOOL)removeTrack:(RTCRtpSender *)sender { |
| 424 | bool result = _peerConnection->RemoveTrack(sender.nativeRtpSender); |
| 425 | if (!result) { |
| 426 | RTCLogError(@"Failed to remote track %@", sender); |
| 427 | } |
| 428 | return result; |
| 429 | } |
| 430 | |
| 431 | - (RTCRtpTransceiver *)addTransceiverWithTrack:(RTCMediaStreamTrack *)track { |
| 432 | return [self addTransceiverWithTrack:track init:[[RTCRtpTransceiverInit alloc] init]]; |
| 433 | } |
| 434 | |
| 435 | - (RTCRtpTransceiver *)addTransceiverWithTrack:(RTCMediaStreamTrack *)track |
| 436 | init:(RTCRtpTransceiverInit *)init { |
| 437 | webrtc::RTCErrorOr<rtc::scoped_refptr<webrtc::RtpTransceiverInterface>> nativeTransceiverOrError = |
| 438 | _peerConnection->AddTransceiver(track.nativeTrack, init.nativeInit); |
| 439 | if (!nativeTransceiverOrError.ok()) { |
| 440 | RTCLogError( |
| 441 | @"Failed to add transceiver %@: %s", track, nativeTransceiverOrError.error().message()); |
| 442 | return nil; |
| 443 | } |
Yura Yaroshevich | 08f14dd | 2018-07-09 11:56:06 +0300 | [diff] [blame] | 444 | return [[RTCRtpTransceiver alloc] initWithFactory:self.factory |
| 445 | nativeRtpTransceiver:nativeTransceiverOrError.MoveValue()]; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | - (RTCRtpTransceiver *)addTransceiverOfType:(RTCRtpMediaType)mediaType { |
| 449 | return [self addTransceiverOfType:mediaType init:[[RTCRtpTransceiverInit alloc] init]]; |
| 450 | } |
| 451 | |
| 452 | - (RTCRtpTransceiver *)addTransceiverOfType:(RTCRtpMediaType)mediaType |
| 453 | init:(RTCRtpTransceiverInit *)init { |
| 454 | webrtc::RTCErrorOr<rtc::scoped_refptr<webrtc::RtpTransceiverInterface>> nativeTransceiverOrError = |
| 455 | _peerConnection->AddTransceiver([RTCRtpReceiver nativeMediaTypeForMediaType:mediaType], |
| 456 | init.nativeInit); |
| 457 | if (!nativeTransceiverOrError.ok()) { |
| 458 | RTCLogError(@"Failed to add transceiver %@: %s", |
| 459 | [RTCRtpReceiver stringForMediaType:mediaType], |
| 460 | nativeTransceiverOrError.error().message()); |
| 461 | return nil; |
| 462 | } |
Yura Yaroshevich | 08f14dd | 2018-07-09 11:56:06 +0300 | [diff] [blame] | 463 | return [[RTCRtpTransceiver alloc] initWithFactory:self.factory |
| 464 | nativeRtpTransceiver:nativeTransceiverOrError.MoveValue()]; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 465 | } |
| 466 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 467 | - (void)offerForConstraints:(RTCMediaConstraints *)constraints |
| 468 | completionHandler: |
| 469 | (void (^)(RTCSessionDescription *sessionDescription, |
| 470 | NSError *error))completionHandler { |
| 471 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserverAdapter> |
| 472 | observer(new rtc::RefCountedObject |
| 473 | <webrtc::CreateSessionDescriptionObserverAdapter>(completionHandler)); |
Niels Möller | f06f923 | 2018-08-07 12:32:18 +0200 | [diff] [blame] | 474 | webrtc::PeerConnectionInterface::RTCOfferAnswerOptions options; |
| 475 | CopyConstraintsIntoOfferAnswerOptions(constraints.nativeConstraints.get(), &options); |
| 476 | |
| 477 | _peerConnection->CreateOffer(observer, options); |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | - (void)answerForConstraints:(RTCMediaConstraints *)constraints |
| 481 | completionHandler: |
| 482 | (void (^)(RTCSessionDescription *sessionDescription, |
| 483 | NSError *error))completionHandler { |
| 484 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserverAdapter> |
| 485 | observer(new rtc::RefCountedObject |
| 486 | <webrtc::CreateSessionDescriptionObserverAdapter>(completionHandler)); |
Niels Möller | f06f923 | 2018-08-07 12:32:18 +0200 | [diff] [blame] | 487 | webrtc::PeerConnectionInterface::RTCOfferAnswerOptions options; |
| 488 | CopyConstraintsIntoOfferAnswerOptions(constraints.nativeConstraints.get(), &options); |
| 489 | |
| 490 | _peerConnection->CreateAnswer(observer, options); |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | - (void)setLocalDescription:(RTCSessionDescription *)sdp |
| 494 | completionHandler:(void (^)(NSError *error))completionHandler { |
| 495 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserverAdapter> observer( |
| 496 | new rtc::RefCountedObject<webrtc::SetSessionDescriptionObserverAdapter>( |
| 497 | completionHandler)); |
| 498 | _peerConnection->SetLocalDescription(observer, sdp.nativeDescription); |
| 499 | } |
| 500 | |
| 501 | - (void)setRemoteDescription:(RTCSessionDescription *)sdp |
| 502 | completionHandler:(void (^)(NSError *error))completionHandler { |
| 503 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserverAdapter> observer( |
| 504 | new rtc::RefCountedObject<webrtc::SetSessionDescriptionObserverAdapter>( |
| 505 | completionHandler)); |
| 506 | _peerConnection->SetRemoteDescription(observer, sdp.nativeDescription); |
| 507 | } |
| 508 | |
zstein | 8b47617 | 2017-09-05 14:43:03 -0700 | [diff] [blame] | 509 | - (BOOL)setBweMinBitrateBps:(nullable NSNumber *)minBitrateBps |
| 510 | currentBitrateBps:(nullable NSNumber *)currentBitrateBps |
| 511 | maxBitrateBps:(nullable NSNumber *)maxBitrateBps { |
zstein | 03adb7c | 2017-08-09 14:29:42 -0700 | [diff] [blame] | 512 | webrtc::PeerConnectionInterface::BitrateParameters params; |
| 513 | if (minBitrateBps != nil) { |
Danil Chapovalov | 196100e | 2018-06-21 10:17:24 +0200 | [diff] [blame] | 514 | params.min_bitrate_bps = absl::optional<int>(minBitrateBps.intValue); |
zstein | 03adb7c | 2017-08-09 14:29:42 -0700 | [diff] [blame] | 515 | } |
| 516 | if (currentBitrateBps != nil) { |
Danil Chapovalov | 196100e | 2018-06-21 10:17:24 +0200 | [diff] [blame] | 517 | params.current_bitrate_bps = absl::optional<int>(currentBitrateBps.intValue); |
zstein | 03adb7c | 2017-08-09 14:29:42 -0700 | [diff] [blame] | 518 | } |
| 519 | if (maxBitrateBps != nil) { |
Danil Chapovalov | 196100e | 2018-06-21 10:17:24 +0200 | [diff] [blame] | 520 | params.max_bitrate_bps = absl::optional<int>(maxBitrateBps.intValue); |
zstein | 03adb7c | 2017-08-09 14:29:42 -0700 | [diff] [blame] | 521 | } |
| 522 | return _peerConnection->SetBitrate(params).ok(); |
| 523 | } |
| 524 | |
Magnus Jedvert | 0af86d1 | 2017-10-28 16:26:55 +0200 | [diff] [blame] | 525 | - (void)setBitrateAllocationStrategy: |
| 526 | (std::unique_ptr<rtc::BitrateAllocationStrategy>)bitrateAllocationStrategy { |
| 527 | _peerConnection->SetBitrateAllocationStrategy(std::move(bitrateAllocationStrategy)); |
| 528 | } |
| 529 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 530 | - (BOOL)startRtcEventLogWithFilePath:(NSString *)filePath |
| 531 | maxSizeInBytes:(int64_t)maxSizeInBytes { |
| 532 | RTC_DCHECK(filePath.length); |
| 533 | RTC_DCHECK_GT(maxSizeInBytes, 0); |
| 534 | RTC_DCHECK(!_hasStartedRtcEventLog); |
| 535 | if (_hasStartedRtcEventLog) { |
| 536 | RTCLogError(@"Event logging already started."); |
| 537 | return NO; |
| 538 | } |
| 539 | int fd = open(filePath.UTF8String, O_WRONLY | O_CREAT | O_TRUNC, |
| 540 | S_IRUSR | S_IWUSR); |
| 541 | if (fd < 0) { |
| 542 | RTCLogError(@"Error opening file: %@. Error: %d", filePath, errno); |
| 543 | return NO; |
| 544 | } |
Niels Möller | 695cf6a | 2019-05-13 12:27:23 +0200 | [diff] [blame] | 545 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 546 | const size_t max_size = (maxSizeInBytes < 0) ? webrtc::RtcEventLog::kUnlimitedOutput : |
| 547 | rtc::saturated_cast<size_t>(maxSizeInBytes); |
| 548 | |
| 549 | _hasStartedRtcEventLog = _peerConnection->StartRtcEventLog( |
| 550 | absl::make_unique<webrtc::RtcEventLogOutputFile>(fd, max_size)); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 551 | return _hasStartedRtcEventLog; |
| 552 | } |
| 553 | |
| 554 | - (void)stopRtcEventLog { |
| 555 | _peerConnection->StopRtcEventLog(); |
| 556 | _hasStartedRtcEventLog = NO; |
| 557 | } |
| 558 | |
skvlad | f3569c8 | 2016-04-29 15:30:16 -0700 | [diff] [blame] | 559 | - (RTCRtpSender *)senderWithKind:(NSString *)kind |
| 560 | streamId:(NSString *)streamId { |
| 561 | std::string nativeKind = [NSString stdStringForString:kind]; |
| 562 | std::string nativeStreamId = [NSString stdStringForString:streamId]; |
| 563 | rtc::scoped_refptr<webrtc::RtpSenderInterface> nativeSender( |
| 564 | _peerConnection->CreateSender(nativeKind, nativeStreamId)); |
| 565 | return nativeSender ? |
Yura Yaroshevich | ef43aaf | 2018-07-09 19:16:32 +0300 | [diff] [blame] | 566 | [[RTCRtpSender alloc] initWithFactory:self.factory nativeRtpSender:nativeSender] : |
| 567 | nil; |
skvlad | f3569c8 | 2016-04-29 15:30:16 -0700 | [diff] [blame] | 568 | } |
| 569 | |
skvlad | 79b4b87 | 2016-04-08 17:28:55 -0700 | [diff] [blame] | 570 | - (NSArray<RTCRtpSender *> *)senders { |
| 571 | std::vector<rtc::scoped_refptr<webrtc::RtpSenderInterface>> nativeSenders( |
| 572 | _peerConnection->GetSenders()); |
| 573 | NSMutableArray *senders = [[NSMutableArray alloc] init]; |
| 574 | for (const auto &nativeSender : nativeSenders) { |
| 575 | RTCRtpSender *sender = |
Yura Yaroshevich | ef43aaf | 2018-07-09 19:16:32 +0300 | [diff] [blame] | 576 | [[RTCRtpSender alloc] initWithFactory:self.factory nativeRtpSender:nativeSender]; |
skvlad | 79b4b87 | 2016-04-08 17:28:55 -0700 | [diff] [blame] | 577 | [senders addObject:sender]; |
| 578 | } |
| 579 | return senders; |
| 580 | } |
| 581 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 582 | - (NSArray<RTCRtpReceiver *> *)receivers { |
| 583 | std::vector<rtc::scoped_refptr<webrtc::RtpReceiverInterface>> nativeReceivers( |
| 584 | _peerConnection->GetReceivers()); |
| 585 | NSMutableArray *receivers = [[NSMutableArray alloc] init]; |
| 586 | for (const auto &nativeReceiver : nativeReceivers) { |
| 587 | RTCRtpReceiver *receiver = |
Yura Yaroshevich | 7a16c54 | 2018-07-11 12:55:04 +0300 | [diff] [blame] | 588 | [[RTCRtpReceiver alloc] initWithFactory:self.factory nativeRtpReceiver:nativeReceiver]; |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 589 | [receivers addObject:receiver]; |
| 590 | } |
| 591 | return receivers; |
| 592 | } |
| 593 | |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 594 | - (NSArray<RTCRtpTransceiver *> *)transceivers { |
| 595 | std::vector<rtc::scoped_refptr<webrtc::RtpTransceiverInterface>> nativeTransceivers( |
| 596 | _peerConnection->GetTransceivers()); |
| 597 | NSMutableArray *transceivers = [[NSMutableArray alloc] init]; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 598 | for (const auto &nativeTransceiver : nativeTransceivers) { |
Yura Yaroshevich | 08f14dd | 2018-07-09 11:56:06 +0300 | [diff] [blame] | 599 | RTCRtpTransceiver *transceiver = [[RTCRtpTransceiver alloc] initWithFactory:self.factory |
| 600 | nativeRtpTransceiver:nativeTransceiver]; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 601 | [transceivers addObject:transceiver]; |
| 602 | } |
| 603 | return transceivers; |
| 604 | } |
| 605 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 606 | #pragma mark - Private |
| 607 | |
| 608 | + (webrtc::PeerConnectionInterface::SignalingState)nativeSignalingStateForState: |
| 609 | (RTCSignalingState)state { |
| 610 | switch (state) { |
| 611 | case RTCSignalingStateStable: |
| 612 | return webrtc::PeerConnectionInterface::kStable; |
| 613 | case RTCSignalingStateHaveLocalOffer: |
| 614 | return webrtc::PeerConnectionInterface::kHaveLocalOffer; |
| 615 | case RTCSignalingStateHaveLocalPrAnswer: |
| 616 | return webrtc::PeerConnectionInterface::kHaveLocalPrAnswer; |
| 617 | case RTCSignalingStateHaveRemoteOffer: |
| 618 | return webrtc::PeerConnectionInterface::kHaveRemoteOffer; |
| 619 | case RTCSignalingStateHaveRemotePrAnswer: |
| 620 | return webrtc::PeerConnectionInterface::kHaveRemotePrAnswer; |
| 621 | case RTCSignalingStateClosed: |
| 622 | return webrtc::PeerConnectionInterface::kClosed; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | + (RTCSignalingState)signalingStateForNativeState: |
| 627 | (webrtc::PeerConnectionInterface::SignalingState)nativeState { |
| 628 | switch (nativeState) { |
| 629 | case webrtc::PeerConnectionInterface::kStable: |
| 630 | return RTCSignalingStateStable; |
| 631 | case webrtc::PeerConnectionInterface::kHaveLocalOffer: |
| 632 | return RTCSignalingStateHaveLocalOffer; |
| 633 | case webrtc::PeerConnectionInterface::kHaveLocalPrAnswer: |
| 634 | return RTCSignalingStateHaveLocalPrAnswer; |
| 635 | case webrtc::PeerConnectionInterface::kHaveRemoteOffer: |
| 636 | return RTCSignalingStateHaveRemoteOffer; |
| 637 | case webrtc::PeerConnectionInterface::kHaveRemotePrAnswer: |
| 638 | return RTCSignalingStateHaveRemotePrAnswer; |
| 639 | case webrtc::PeerConnectionInterface::kClosed: |
| 640 | return RTCSignalingStateClosed; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | + (NSString *)stringForSignalingState:(RTCSignalingState)state { |
| 645 | switch (state) { |
| 646 | case RTCSignalingStateStable: |
| 647 | return @"STABLE"; |
| 648 | case RTCSignalingStateHaveLocalOffer: |
| 649 | return @"HAVE_LOCAL_OFFER"; |
| 650 | case RTCSignalingStateHaveLocalPrAnswer: |
| 651 | return @"HAVE_LOCAL_PRANSWER"; |
| 652 | case RTCSignalingStateHaveRemoteOffer: |
| 653 | return @"HAVE_REMOTE_OFFER"; |
| 654 | case RTCSignalingStateHaveRemotePrAnswer: |
| 655 | return @"HAVE_REMOTE_PRANSWER"; |
| 656 | case RTCSignalingStateClosed: |
| 657 | return @"CLOSED"; |
| 658 | } |
| 659 | } |
| 660 | |
Jonas Olsson | cfddbb7 | 2018-11-15 16:52:45 +0100 | [diff] [blame] | 661 | + (webrtc::PeerConnectionInterface::PeerConnectionState)nativeConnectionStateForState: |
| 662 | (RTCPeerConnectionState)state { |
| 663 | switch (state) { |
| 664 | case RTCPeerConnectionStateNew: |
| 665 | return webrtc::PeerConnectionInterface::PeerConnectionState::kNew; |
| 666 | case RTCPeerConnectionStateConnecting: |
| 667 | return webrtc::PeerConnectionInterface::PeerConnectionState::kConnecting; |
| 668 | case RTCPeerConnectionStateConnected: |
| 669 | return webrtc::PeerConnectionInterface::PeerConnectionState::kConnected; |
| 670 | case RTCPeerConnectionStateFailed: |
| 671 | return webrtc::PeerConnectionInterface::PeerConnectionState::kFailed; |
| 672 | case RTCPeerConnectionStateDisconnected: |
| 673 | return webrtc::PeerConnectionInterface::PeerConnectionState::kDisconnected; |
| 674 | case RTCPeerConnectionStateClosed: |
| 675 | return webrtc::PeerConnectionInterface::PeerConnectionState::kClosed; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | + (RTCPeerConnectionState)connectionStateForNativeState: |
| 680 | (webrtc::PeerConnectionInterface::PeerConnectionState)nativeState { |
| 681 | switch (nativeState) { |
| 682 | case webrtc::PeerConnectionInterface::PeerConnectionState::kNew: |
| 683 | return RTCPeerConnectionStateNew; |
| 684 | case webrtc::PeerConnectionInterface::PeerConnectionState::kConnecting: |
| 685 | return RTCPeerConnectionStateConnecting; |
| 686 | case webrtc::PeerConnectionInterface::PeerConnectionState::kConnected: |
| 687 | return RTCPeerConnectionStateConnected; |
| 688 | case webrtc::PeerConnectionInterface::PeerConnectionState::kFailed: |
| 689 | return RTCPeerConnectionStateFailed; |
| 690 | case webrtc::PeerConnectionInterface::PeerConnectionState::kDisconnected: |
| 691 | return RTCPeerConnectionStateDisconnected; |
| 692 | case webrtc::PeerConnectionInterface::PeerConnectionState::kClosed: |
| 693 | return RTCPeerConnectionStateClosed; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | + (NSString *)stringForConnectionState:(RTCPeerConnectionState)state { |
| 698 | switch (state) { |
| 699 | case RTCPeerConnectionStateNew: |
| 700 | return @"NEW"; |
| 701 | case RTCPeerConnectionStateConnecting: |
| 702 | return @"CONNECTING"; |
| 703 | case RTCPeerConnectionStateConnected: |
| 704 | return @"CONNECTED"; |
| 705 | case RTCPeerConnectionStateFailed: |
| 706 | return @"FAILED"; |
| 707 | case RTCPeerConnectionStateDisconnected: |
| 708 | return @"DISCONNECTED"; |
| 709 | case RTCPeerConnectionStateClosed: |
| 710 | return @"CLOSED"; |
| 711 | } |
| 712 | } |
| 713 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 714 | + (webrtc::PeerConnectionInterface::IceConnectionState) |
| 715 | nativeIceConnectionStateForState:(RTCIceConnectionState)state { |
| 716 | switch (state) { |
| 717 | case RTCIceConnectionStateNew: |
| 718 | return webrtc::PeerConnectionInterface::kIceConnectionNew; |
| 719 | case RTCIceConnectionStateChecking: |
| 720 | return webrtc::PeerConnectionInterface::kIceConnectionChecking; |
| 721 | case RTCIceConnectionStateConnected: |
| 722 | return webrtc::PeerConnectionInterface::kIceConnectionConnected; |
| 723 | case RTCIceConnectionStateCompleted: |
| 724 | return webrtc::PeerConnectionInterface::kIceConnectionCompleted; |
| 725 | case RTCIceConnectionStateFailed: |
| 726 | return webrtc::PeerConnectionInterface::kIceConnectionFailed; |
| 727 | case RTCIceConnectionStateDisconnected: |
| 728 | return webrtc::PeerConnectionInterface::kIceConnectionDisconnected; |
| 729 | case RTCIceConnectionStateClosed: |
| 730 | return webrtc::PeerConnectionInterface::kIceConnectionClosed; |
hjon | 8bbbf2c | 2016-03-14 13:15:44 -0700 | [diff] [blame] | 731 | case RTCIceConnectionStateCount: |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 732 | return webrtc::PeerConnectionInterface::kIceConnectionMax; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | + (RTCIceConnectionState)iceConnectionStateForNativeState: |
| 737 | (webrtc::PeerConnectionInterface::IceConnectionState)nativeState { |
| 738 | switch (nativeState) { |
| 739 | case webrtc::PeerConnectionInterface::kIceConnectionNew: |
| 740 | return RTCIceConnectionStateNew; |
| 741 | case webrtc::PeerConnectionInterface::kIceConnectionChecking: |
| 742 | return RTCIceConnectionStateChecking; |
| 743 | case webrtc::PeerConnectionInterface::kIceConnectionConnected: |
| 744 | return RTCIceConnectionStateConnected; |
| 745 | case webrtc::PeerConnectionInterface::kIceConnectionCompleted: |
| 746 | return RTCIceConnectionStateCompleted; |
| 747 | case webrtc::PeerConnectionInterface::kIceConnectionFailed: |
| 748 | return RTCIceConnectionStateFailed; |
| 749 | case webrtc::PeerConnectionInterface::kIceConnectionDisconnected: |
| 750 | return RTCIceConnectionStateDisconnected; |
| 751 | case webrtc::PeerConnectionInterface::kIceConnectionClosed: |
| 752 | return RTCIceConnectionStateClosed; |
| 753 | case webrtc::PeerConnectionInterface::kIceConnectionMax: |
hjon | 8bbbf2c | 2016-03-14 13:15:44 -0700 | [diff] [blame] | 754 | return RTCIceConnectionStateCount; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | + (NSString *)stringForIceConnectionState:(RTCIceConnectionState)state { |
| 759 | switch (state) { |
| 760 | case RTCIceConnectionStateNew: |
| 761 | return @"NEW"; |
| 762 | case RTCIceConnectionStateChecking: |
| 763 | return @"CHECKING"; |
| 764 | case RTCIceConnectionStateConnected: |
| 765 | return @"CONNECTED"; |
| 766 | case RTCIceConnectionStateCompleted: |
| 767 | return @"COMPLETED"; |
| 768 | case RTCIceConnectionStateFailed: |
| 769 | return @"FAILED"; |
| 770 | case RTCIceConnectionStateDisconnected: |
| 771 | return @"DISCONNECTED"; |
| 772 | case RTCIceConnectionStateClosed: |
| 773 | return @"CLOSED"; |
hjon | 8bbbf2c | 2016-03-14 13:15:44 -0700 | [diff] [blame] | 774 | case RTCIceConnectionStateCount: |
| 775 | return @"COUNT"; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
| 779 | + (webrtc::PeerConnectionInterface::IceGatheringState) |
| 780 | nativeIceGatheringStateForState:(RTCIceGatheringState)state { |
| 781 | switch (state) { |
| 782 | case RTCIceGatheringStateNew: |
| 783 | return webrtc::PeerConnectionInterface::kIceGatheringNew; |
| 784 | case RTCIceGatheringStateGathering: |
| 785 | return webrtc::PeerConnectionInterface::kIceGatheringGathering; |
| 786 | case RTCIceGatheringStateComplete: |
| 787 | return webrtc::PeerConnectionInterface::kIceGatheringComplete; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | + (RTCIceGatheringState)iceGatheringStateForNativeState: |
| 792 | (webrtc::PeerConnectionInterface::IceGatheringState)nativeState { |
| 793 | switch (nativeState) { |
| 794 | case webrtc::PeerConnectionInterface::kIceGatheringNew: |
| 795 | return RTCIceGatheringStateNew; |
| 796 | case webrtc::PeerConnectionInterface::kIceGatheringGathering: |
| 797 | return RTCIceGatheringStateGathering; |
| 798 | case webrtc::PeerConnectionInterface::kIceGatheringComplete: |
| 799 | return RTCIceGatheringStateComplete; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | + (NSString *)stringForIceGatheringState:(RTCIceGatheringState)state { |
| 804 | switch (state) { |
| 805 | case RTCIceGatheringStateNew: |
| 806 | return @"NEW"; |
| 807 | case RTCIceGatheringStateGathering: |
| 808 | return @"GATHERING"; |
| 809 | case RTCIceGatheringStateComplete: |
| 810 | return @"COMPLETE"; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | + (webrtc::PeerConnectionInterface::StatsOutputLevel) |
| 815 | nativeStatsOutputLevelForLevel:(RTCStatsOutputLevel)level { |
| 816 | switch (level) { |
| 817 | case RTCStatsOutputLevelStandard: |
| 818 | return webrtc::PeerConnectionInterface::kStatsOutputLevelStandard; |
| 819 | case RTCStatsOutputLevelDebug: |
| 820 | return webrtc::PeerConnectionInterface::kStatsOutputLevelDebug; |
| 821 | } |
| 822 | } |
| 823 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 824 | - (rtc::scoped_refptr<webrtc::PeerConnectionInterface>)nativePeerConnection { |
| 825 | return _peerConnection; |
| 826 | } |
| 827 | |
| 828 | @end |