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