| 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 |
| 11 | #include "pc/sctp_transport.h" |
| 12 |
| 13 | #include <algorithm> |
| 14 | #include <utility> |
| 15 |
| 16 | #include "absl/types/optional.h" |
| 17 | #include "api/dtls_transport_interface.h" |
| 18 | #include "api/sequence_checker.h" |
| 19 | #include "rtc_base/checks.h" |
| 20 | #include "rtc_base/logging.h" |
| 21 |
| 22 | namespace webrtc { |
| 23 |
| 24 | SctpTransport::SctpTransport( |
| 25 | std::unique_ptr<cricket::SctpTransportInternal> internal) |
| 26 | : owner_thread_(rtc::Thread::Current()), |
| 27 | info_(SctpTransportState::kNew), |
| 28 | internal_sctp_transport_(std::move(internal)) { |
| 29 | RTC_DCHECK(internal_sctp_transport_.get()); |
| 30 | internal_sctp_transport_->SetOnConnectedCallback( |
| 31 | [this]() { OnAssociationChangeCommunicationUp(); }); |
| 32 |
| 33 | if (dtls_transport_) { |
| 34 | UpdateInformation(SctpTransportState::kConnecting); |
| 35 | } else { |
| 36 | UpdateInformation(SctpTransportState::kNew); |
| 37 | } |
| 38 | } |
| 39 |
| 40 | SctpTransport::~SctpTransport() { |
| 41 | // We depend on the network thread to call Clear() before dropping |
| 42 | // its last reference to this object. |
| 43 | RTC_DCHECK(owner_thread_->IsCurrent() || !internal_sctp_transport_); |
| 44 | } |
| 45 |
| 46 | SctpTransportInformation SctpTransport::Information() const { |
| 47 | // TODO(tommi): Update PeerConnection::GetSctpTransport to hand out a proxy |
| 48 | // to the transport so that we can be sure that methods get called on the |
| 49 | // expected thread. Chromium currently calls this method from |
| 50 | // TransceiverStateSurfacer. |
| 51 | if (!owner_thread_->IsCurrent()) { |
| 52 | return owner_thread_->BlockingCall([this] { return Information(); }); |
| 53 | } |
| 54 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 55 | return info_; |
| 56 | } |
| 57 |
| 58 | void SctpTransport::RegisterObserver(SctpTransportObserverInterface* observer) { |
| 59 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 60 | RTC_DCHECK(observer); |
| 61 | RTC_DCHECK(!observer_); |
| 62 | observer_ = observer; |
| 63 | } |
| 64 |
| 65 | void SctpTransport::UnregisterObserver() { |
| 66 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 67 | observer_ = nullptr; |
| 68 | } |
| 69 |
| 70 | RTCError SctpTransport::OpenChannel(int channel_id) { |
| 71 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 72 | RTC_DCHECK(internal_sctp_transport_); |
| 73 | internal_sctp_transport_->OpenStream(channel_id); |
| 74 | return RTCError::OK(); |
| 75 | } |
| 76 |
| 77 | RTCError SctpTransport::SendData(int channel_id, |
| 78 | const SendDataParams& params, |
| 79 | const rtc::CopyOnWriteBuffer& buffer) { |
| 80 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 81 | return internal_sctp_transport_->SendData(channel_id, params, buffer); |
| 82 | } |
| 83 |
| 84 | RTCError SctpTransport::CloseChannel(int channel_id) { |
| 85 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 86 | RTC_DCHECK(internal_sctp_transport_); |
| 87 | internal_sctp_transport_->ResetStream(channel_id); |
| 88 | return RTCError::OK(); |
| 89 | } |
| 90 |
| 91 | void SctpTransport::SetDataSink(DataChannelSink* sink) { |
| 92 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 93 | RTC_DCHECK(internal_sctp_transport_); |
| 94 | internal_sctp_transport_->SetDataChannelSink(sink); |
| 95 | } |
| 96 |
| 97 | bool SctpTransport::IsReadyToSend() const { |
| 98 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 99 | RTC_DCHECK(internal_sctp_transport_); |
| 100 | return internal_sctp_transport_->ReadyToSendData(); |
| 101 | } |
| 102 |
| 103 | rtc::scoped_refptr<DtlsTransportInterface> SctpTransport::dtls_transport() |
| 104 | const { |
| 105 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 106 | return dtls_transport_; |
| 107 | } |
| 108 |
| 109 | // Internal functions |
| 110 | void SctpTransport::Clear() { |
| 111 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 112 | RTC_DCHECK(internal()); |
| 113 | // Note that we delete internal_sctp_transport_, but |
| 114 | // only drop the reference to dtls_transport_. |
| 115 | dtls_transport_ = nullptr; |
| 116 | internal_sctp_transport_ = nullptr; |
| 117 | UpdateInformation(SctpTransportState::kClosed); |
| 118 | } |
| 119 |
| 120 | void SctpTransport::SetDtlsTransport( |
| 121 | rtc::scoped_refptr<DtlsTransport> transport) { |
| 122 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 123 | SctpTransportState next_state = info_.state(); |
| 124 | dtls_transport_ = transport; |
| 125 | if (internal_sctp_transport_) { |
| 126 | if (transport) { |
| 127 | internal_sctp_transport_->SetDtlsTransport(transport->internal()); |
| 128 |
| 129 | transport->internal()->SubscribeDtlsTransportState( |
| 130 | [this](cricket::DtlsTransportInternal* transport, |
| 131 | DtlsTransportState state) { |
| 132 | OnDtlsStateChange(transport, state); |
| 133 | }); |
| 134 | if (info_.state() == SctpTransportState::kNew) { |
| 135 | next_state = SctpTransportState::kConnecting; |
| 136 | } |
| 137 | } else { |
| 138 | internal_sctp_transport_->SetDtlsTransport(nullptr); |
| 139 | } |
| 140 | } |
| 141 |
| 142 | UpdateInformation(next_state); |
| 143 | } |
| 144 |
| 145 | void SctpTransport::Start(int local_port, |
| 146 | int remote_port, |
| 147 | int max_message_size) { |
| 148 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 149 | info_ = SctpTransportInformation(info_.state(), info_.dtls_transport(), |
| 150 | max_message_size, info_.MaxChannels()); |
| 151 |
| 152 | if (!internal()->Start(local_port, remote_port, max_message_size)) { |
| 153 | RTC_LOG(LS_ERROR) << "Failed to push down SCTP parameters, closing."; |
| 154 | UpdateInformation(SctpTransportState::kClosed); |
| 155 | } |
| 156 | } |
| 157 |
| 158 | void SctpTransport::UpdateInformation(SctpTransportState state) { |
| 159 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 160 | bool must_send_update = (state != info_.state()); |
| 161 | // TODO(https://bugs.webrtc.org/10358): Update max channels from internal |
| 162 | // SCTP transport when available. |
| 163 | if (internal_sctp_transport_) { |
| 164 | info_ = SctpTransportInformation( |
| 165 | state, dtls_transport_, info_.MaxMessageSize(), info_.MaxChannels()); |
| 166 | } else { |
| 167 | info_ = SctpTransportInformation( |
| 168 | state, dtls_transport_, info_.MaxMessageSize(), info_.MaxChannels()); |
| 169 | } |
| 170 |
| 171 | if (observer_ && must_send_update) { |
| 172 | observer_->OnStateChange(info_); |
| 173 | } |
| 174 | } |
| 175 |
| 176 | void SctpTransport::OnAssociationChangeCommunicationUp() { |
| 177 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 178 | RTC_DCHECK(internal_sctp_transport_); |
| 179 | if (internal_sctp_transport_->max_outbound_streams() && |
| 180 | internal_sctp_transport_->max_inbound_streams()) { |
| 181 | int max_channels = |
| 182 | std::min(*(internal_sctp_transport_->max_outbound_streams()), |
| 183 | *(internal_sctp_transport_->max_inbound_streams())); |
| 184 | // Record max channels. |
| 185 | info_ = SctpTransportInformation(info_.state(), info_.dtls_transport(), |
| 186 | info_.MaxMessageSize(), max_channels); |
| 187 | } |
| 188 |
| 189 | UpdateInformation(SctpTransportState::kConnected); |
| 190 | } |
| 191 |
| 192 | void SctpTransport::OnDtlsStateChange(cricket::DtlsTransportInternal* transport, |
| 193 | DtlsTransportState state) { |
| 194 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 195 | RTC_CHECK(transport == dtls_transport_->internal()); |
| 196 | if (state == DtlsTransportState::kClosed || |
| 197 | state == DtlsTransportState::kFailed) { |
| 198 | UpdateInformation(SctpTransportState::kClosed); |
| 199 | // TODO(http://bugs.webrtc.org/11090): Close all the data channels |
| 200 | } |
| 201 | } |
| 202 |
| 203 | } // namespace webrtc |
| 204 |