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