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