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