Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 11 | #include "pc/data_channel_controller.h" |
| 12 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 13 | #include <algorithm> |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 14 | #include <utility> |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 15 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 16 | #include "absl/algorithm/container.h" |
| 17 | #include "absl/types/optional.h" |
| 18 | #include "api/peer_connection_interface.h" |
| 19 | #include "api/rtc_error.h" |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 20 | #include "pc/peer_connection.h" |
| 21 | #include "pc/sctp_utils.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 22 | #include "rtc_base/location.h" |
| 23 | #include "rtc_base/logging.h" |
Niels Möller | 236e36c | 2021-03-23 08:23:10 | [diff] [blame] | 24 | #include "rtc_base/task_utils/to_queued_task.h" |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 28 | bool DataChannelController::HasDataChannels() const { |
| 29 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 30 | return !sctp_data_channels_.empty(); |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 31 | } |
| 32 | |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 33 | bool DataChannelController::SendData(int sid, |
| 34 | const SendDataParams& params, |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 35 | const rtc::CopyOnWriteBuffer& payload, |
| 36 | cricket::SendDataResult* result) { |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 11:47:42 | [diff] [blame] | 37 | if (data_channel_transport()) |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 38 | return DataChannelSendData(sid, params, payload, result); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 39 | RTC_LOG(LS_ERROR) << "SendData called before transport is ready"; |
| 40 | return false; |
| 41 | } |
| 42 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 43 | bool DataChannelController::ConnectDataChannel( |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 44 | SctpDataChannel* webrtc_data_channel) { |
| 45 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 46 | if (!data_channel_transport()) { |
| 47 | // Don't log an error here, because DataChannels are expected to call |
| 48 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 49 | // whether or not the underlying transport is ready. |
| 50 | return false; |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 51 | } |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 52 | SignalDataChannelTransportWritable_s.connect( |
| 53 | webrtc_data_channel, &SctpDataChannel::OnTransportReady); |
| 54 | SignalDataChannelTransportReceivedData_s.connect( |
| 55 | webrtc_data_channel, &SctpDataChannel::OnDataReceived); |
| 56 | SignalDataChannelTransportChannelClosing_s.connect( |
| 57 | webrtc_data_channel, &SctpDataChannel::OnClosingProcedureStartedRemotely); |
| 58 | SignalDataChannelTransportChannelClosed_s.connect( |
| 59 | webrtc_data_channel, &SctpDataChannel::OnClosingProcedureComplete); |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | void DataChannelController::DisconnectDataChannel( |
| 64 | SctpDataChannel* webrtc_data_channel) { |
| 65 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 66 | if (!data_channel_transport()) { |
| 67 | RTC_LOG(LS_ERROR) |
| 68 | << "DisconnectDataChannel called when sctp_transport_ is NULL."; |
| 69 | return; |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 70 | } |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 71 | SignalDataChannelTransportWritable_s.disconnect(webrtc_data_channel); |
| 72 | SignalDataChannelTransportReceivedData_s.disconnect(webrtc_data_channel); |
| 73 | SignalDataChannelTransportChannelClosing_s.disconnect(webrtc_data_channel); |
| 74 | SignalDataChannelTransportChannelClosed_s.disconnect(webrtc_data_channel); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 75 | } |
| 76 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 77 | void DataChannelController::AddSctpDataStream(int sid) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 78 | if (data_channel_transport()) { |
| 79 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this, sid] { |
| 80 | if (data_channel_transport()) { |
| 81 | data_channel_transport()->OpenChannel(sid); |
| 82 | } |
| 83 | }); |
| 84 | } |
| 85 | } |
| 86 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 87 | void DataChannelController::RemoveSctpDataStream(int sid) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 88 | if (data_channel_transport()) { |
| 89 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this, sid] { |
| 90 | if (data_channel_transport()) { |
| 91 | data_channel_transport()->CloseChannel(sid); |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | } |
| 96 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 97 | bool DataChannelController::ReadyToSendData() const { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 98 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 99 | return (data_channel_transport() && data_channel_transport_ready_to_send_); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 100 | } |
| 101 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 102 | void DataChannelController::OnDataReceived( |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 103 | int channel_id, |
| 104 | DataMessageType type, |
| 105 | const rtc::CopyOnWriteBuffer& buffer) { |
| 106 | RTC_DCHECK_RUN_ON(network_thread()); |
| 107 | cricket::ReceiveDataParams params; |
| 108 | params.sid = channel_id; |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 109 | params.type = type; |
Niels Möller | 236e36c | 2021-03-23 08:23:10 | [diff] [blame] | 110 | signaling_thread()->PostTask( |
| 111 | ToQueuedTask([self = weak_factory_.GetWeakPtr(), params, buffer] { |
| 112 | if (self) { |
| 113 | RTC_DCHECK_RUN_ON(self->signaling_thread()); |
| 114 | // TODO(bugs.webrtc.org/11547): The data being received should be |
| 115 | // delivered on the network thread. The way HandleOpenMessage_s works |
| 116 | // right now is that it's called for all types of buffers and operates |
| 117 | // as a selector function. Change this so that it's only called for |
| 118 | // buffers that it should be able to handle. Once we do that, we can |
| 119 | // deliver all other buffers on the network thread (change |
| 120 | // SignalDataChannelTransportReceivedData_s to |
| 121 | // SignalDataChannelTransportReceivedData_n). |
| 122 | if (!self->HandleOpenMessage_s(params, buffer)) { |
| 123 | self->SignalDataChannelTransportReceivedData_s(params, buffer); |
| 124 | } |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 125 | } |
Niels Möller | 236e36c | 2021-03-23 08:23:10 | [diff] [blame] | 126 | })); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 127 | } |
| 128 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 129 | void DataChannelController::OnChannelClosing(int channel_id) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 130 | RTC_DCHECK_RUN_ON(network_thread()); |
Niels Möller | 236e36c | 2021-03-23 08:23:10 | [diff] [blame] | 131 | signaling_thread()->PostTask( |
| 132 | ToQueuedTask([self = weak_factory_.GetWeakPtr(), channel_id] { |
| 133 | if (self) { |
| 134 | RTC_DCHECK_RUN_ON(self->signaling_thread()); |
| 135 | self->SignalDataChannelTransportChannelClosing_s(channel_id); |
| 136 | } |
| 137 | })); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 138 | } |
| 139 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 140 | void DataChannelController::OnChannelClosed(int channel_id) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 141 | RTC_DCHECK_RUN_ON(network_thread()); |
Niels Möller | 236e36c | 2021-03-23 08:23:10 | [diff] [blame] | 142 | signaling_thread()->PostTask( |
| 143 | ToQueuedTask([self = weak_factory_.GetWeakPtr(), channel_id] { |
| 144 | if (self) { |
| 145 | RTC_DCHECK_RUN_ON(self->signaling_thread()); |
| 146 | self->SignalDataChannelTransportChannelClosed_s(channel_id); |
| 147 | } |
| 148 | })); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 149 | } |
| 150 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 151 | void DataChannelController::OnReadyToSend() { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 152 | RTC_DCHECK_RUN_ON(network_thread()); |
Niels Möller | 236e36c | 2021-03-23 08:23:10 | [diff] [blame] | 153 | signaling_thread()->PostTask( |
| 154 | ToQueuedTask([self = weak_factory_.GetWeakPtr()] { |
| 155 | if (self) { |
| 156 | RTC_DCHECK_RUN_ON(self->signaling_thread()); |
| 157 | self->data_channel_transport_ready_to_send_ = true; |
| 158 | self->SignalDataChannelTransportWritable_s( |
| 159 | self->data_channel_transport_ready_to_send_); |
| 160 | } |
| 161 | })); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 162 | } |
| 163 | |
Florent Castelli | dcb9ffc | 2021-06-29 12:58:23 | [diff] [blame] | 164 | void DataChannelController::OnTransportClosed(RTCError error) { |
Harald Alvestrand | 2697ac1 | 2019-12-16 09:37:04 | [diff] [blame] | 165 | RTC_DCHECK_RUN_ON(network_thread()); |
Niels Möller | 236e36c | 2021-03-23 08:23:10 | [diff] [blame] | 166 | signaling_thread()->PostTask( |
Florent Castelli | dcb9ffc | 2021-06-29 12:58:23 | [diff] [blame] | 167 | ToQueuedTask([self = weak_factory_.GetWeakPtr(), error] { |
Niels Möller | 236e36c | 2021-03-23 08:23:10 | [diff] [blame] | 168 | if (self) { |
| 169 | RTC_DCHECK_RUN_ON(self->signaling_thread()); |
Florent Castelli | dcb9ffc | 2021-06-29 12:58:23 | [diff] [blame] | 170 | self->OnTransportChannelClosed(error); |
Niels Möller | 236e36c | 2021-03-23 08:23:10 | [diff] [blame] | 171 | } |
| 172 | })); |
Harald Alvestrand | 2697ac1 | 2019-12-16 09:37:04 | [diff] [blame] | 173 | } |
| 174 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 175 | void DataChannelController::SetupDataChannelTransport_n() { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 176 | RTC_DCHECK_RUN_ON(network_thread()); |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 14:54:10 | [diff] [blame] | 177 | |
| 178 | // There's a new data channel transport. This needs to be signaled to the |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 179 | // `sctp_data_channels_` so that they can reopen and reconnect. This is |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 14:54:10 | [diff] [blame] | 180 | // necessary when bundling is applied. |
| 181 | NotifyDataChannelsOfTransportCreated(); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 182 | } |
| 183 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 184 | void DataChannelController::TeardownDataChannelTransport_n() { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 185 | RTC_DCHECK_RUN_ON(network_thread()); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 186 | if (data_channel_transport()) { |
| 187 | data_channel_transport()->SetDataSink(nullptr); |
| 188 | } |
| 189 | set_data_channel_transport(nullptr); |
| 190 | } |
| 191 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 192 | void DataChannelController::OnTransportChanged( |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 193 | DataChannelTransportInterface* new_data_channel_transport) { |
| 194 | RTC_DCHECK_RUN_ON(network_thread()); |
| 195 | if (data_channel_transport() && |
| 196 | data_channel_transport() != new_data_channel_transport) { |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 197 | // Changed which data channel transport is used for `sctp_mid_` (eg. now |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 198 | // it's bundled). |
| 199 | data_channel_transport()->SetDataSink(nullptr); |
| 200 | set_data_channel_transport(new_data_channel_transport); |
| 201 | if (new_data_channel_transport) { |
| 202 | new_data_channel_transport->SetDataSink(this); |
| 203 | |
| 204 | // There's a new data channel transport. This needs to be signaled to the |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 205 | // `sctp_data_channels_` so that they can reopen and reconnect. This is |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 206 | // necessary when bundling is applied. |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 14:54:10 | [diff] [blame] | 207 | NotifyDataChannelsOfTransportCreated(); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 212 | std::vector<DataChannelStats> DataChannelController::GetDataChannelStats() |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 14:54:10 | [diff] [blame] | 213 | const { |
| 214 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 215 | std::vector<DataChannelStats> stats; |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 14:54:10 | [diff] [blame] | 216 | stats.reserve(sctp_data_channels_.size()); |
| 217 | for (const auto& channel : sctp_data_channels_) |
| 218 | stats.push_back(channel->GetStats()); |
| 219 | return stats; |
| 220 | } |
| 221 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 222 | bool DataChannelController::HandleOpenMessage_s( |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 223 | const cricket::ReceiveDataParams& params, |
| 224 | const rtc::CopyOnWriteBuffer& buffer) { |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 225 | if (params.type == DataMessageType::kControl && IsOpenMessage(buffer)) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 226 | // Received OPEN message; parse and signal that a new data channel should |
| 227 | // be created. |
| 228 | std::string label; |
| 229 | InternalDataChannelInit config; |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 230 | config.id = params.sid; |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 231 | if (!ParseDataChannelOpenMessage(buffer, &label, &config)) { |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 232 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for sid " |
| 233 | << params.sid; |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 234 | return true; |
| 235 | } |
| 236 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 237 | OnDataChannelOpenMessage(label, config); |
| 238 | return true; |
| 239 | } |
| 240 | return false; |
| 241 | } |
| 242 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 243 | void DataChannelController::OnDataChannelOpenMessage( |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 244 | const std::string& label, |
| 245 | const InternalDataChannelInit& config) { |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 246 | rtc::scoped_refptr<DataChannelInterface> channel( |
| 247 | InternalCreateDataChannelWithProxy(label, &config)); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 248 | if (!channel.get()) { |
| 249 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
| 250 | return; |
| 251 | } |
| 252 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 253 | pc_->Observer()->OnDataChannel(std::move(channel)); |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 254 | pc_->NoteDataAddedEvent(); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 255 | } |
| 256 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 257 | rtc::scoped_refptr<DataChannelInterface> |
| 258 | DataChannelController::InternalCreateDataChannelWithProxy( |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 259 | const std::string& label, |
| 260 | const InternalDataChannelInit* config) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 261 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 262 | if (pc_->IsClosed()) { |
| 263 | return nullptr; |
| 264 | } |
Florent Castelli | 516e284 | 2021-04-19 13:29:50 | [diff] [blame] | 265 | |
| 266 | rtc::scoped_refptr<SctpDataChannel> channel = |
| 267 | InternalCreateSctpDataChannel(label, config); |
| 268 | if (channel) { |
| 269 | return SctpDataChannel::CreateProxy(channel); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 270 | } |
| 271 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 272 | return nullptr; |
| 273 | } |
| 274 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 275 | rtc::scoped_refptr<SctpDataChannel> |
| 276 | DataChannelController::InternalCreateSctpDataChannel( |
| 277 | const std::string& label, |
| 278 | const InternalDataChannelInit* config) { |
| 279 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 280 | InternalDataChannelInit new_config = |
| 281 | config ? (*config) : InternalDataChannelInit(); |
| 282 | if (new_config.id < 0) { |
| 283 | rtc::SSLRole role; |
| 284 | if ((pc_->GetSctpSslRole(&role)) && |
| 285 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
| 286 | RTC_LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel."; |
| 287 | return nullptr; |
| 288 | } |
| 289 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
| 290 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
| 291 | "because the id is already in use or out of range."; |
| 292 | return nullptr; |
| 293 | } |
| 294 | rtc::scoped_refptr<SctpDataChannel> channel(SctpDataChannel::Create( |
| 295 | this, label, new_config, signaling_thread(), network_thread())); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 296 | if (!channel) { |
| 297 | sid_allocator_.ReleaseSid(new_config.id); |
| 298 | return nullptr; |
| 299 | } |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 300 | sctp_data_channels_.push_back(channel); |
| 301 | channel->SignalClosed.connect(pc_, &PeerConnection::OnSctpDataChannelClosed); |
| 302 | SignalSctpDataChannelCreated_(channel.get()); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 303 | return channel; |
| 304 | } |
| 305 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 306 | void DataChannelController::AllocateSctpSids(rtc::SSLRole role) { |
| 307 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 308 | std::vector<rtc::scoped_refptr<SctpDataChannel>> channels_to_close; |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 309 | for (const auto& channel : sctp_data_channels_) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 310 | if (channel->id() < 0) { |
| 311 | int sid; |
| 312 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
| 313 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid, closing channel."; |
| 314 | channels_to_close.push_back(channel); |
| 315 | continue; |
| 316 | } |
| 317 | channel->SetSctpSid(sid); |
| 318 | } |
| 319 | } |
| 320 | // Since closing modifies the list of channels, we have to do the actual |
| 321 | // closing outside the loop. |
| 322 | for (const auto& channel : channels_to_close) { |
Harald Alvestrand | dfbfb46 | 2019-12-08 04:55:43 | [diff] [blame] | 323 | channel->CloseAbruptlyWithDataChannelFailure("Failed to allocate SCTP SID"); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 327 | void DataChannelController::OnSctpDataChannelClosed(SctpDataChannel* channel) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 328 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 329 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 330 | ++it) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 331 | if (it->get() == channel) { |
| 332 | if (channel->id() >= 0) { |
| 333 | // After the closing procedure is done, it's safe to use this ID for |
| 334 | // another data channel. |
| 335 | sid_allocator_.ReleaseSid(channel->id()); |
| 336 | } |
| 337 | // Since this method is triggered by a signal from the DataChannel, |
| 338 | // we can't free it directly here; we need to free it asynchronously. |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 339 | sctp_data_channels_to_free_.push_back(*it); |
| 340 | sctp_data_channels_.erase(it); |
Harald Alvestrand | 246724b | 2019-12-03 21:31:42 | [diff] [blame] | 341 | signaling_thread()->PostTask( |
Niels Möller | 236e36c | 2021-03-23 08:23:10 | [diff] [blame] | 342 | ToQueuedTask([self = weak_factory_.GetWeakPtr()] { |
Harald Alvestrand | 246724b | 2019-12-03 21:31:42 | [diff] [blame] | 343 | if (self) { |
| 344 | RTC_DCHECK_RUN_ON(self->signaling_thread()); |
| 345 | self->sctp_data_channels_to_free_.clear(); |
| 346 | } |
Niels Möller | 236e36c | 2021-03-23 08:23:10 | [diff] [blame] | 347 | })); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 348 | return; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
Florent Castelli | dcb9ffc | 2021-06-29 12:58:23 | [diff] [blame] | 353 | void DataChannelController::OnTransportChannelClosed(RTCError error) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 354 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 355 | // Use a temporary copy of the SCTP DataChannel list because the |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 356 | // DataChannel may callback to us and try to modify the list. |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 357 | std::vector<rtc::scoped_refptr<SctpDataChannel>> temp_sctp_dcs; |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 358 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 359 | for (const auto& channel : temp_sctp_dcs) { |
Florent Castelli | dcb9ffc | 2021-06-29 12:58:23 | [diff] [blame] | 360 | channel->OnTransportChannelClosed(error); |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 364 | SctpDataChannel* DataChannelController::FindDataChannelBySid(int sid) const { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 365 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 366 | for (const auto& channel : sctp_data_channels_) { |
| 367 | if (channel->id() == sid) { |
| 368 | return channel; |
| 369 | } |
| 370 | } |
| 371 | return nullptr; |
| 372 | } |
| 373 | |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 11:47:42 | [diff] [blame] | 374 | DataChannelTransportInterface* DataChannelController::data_channel_transport() |
| 375 | const { |
| 376 | // TODO(bugs.webrtc.org/11547): Only allow this accessor to be called on the |
| 377 | // network thread. |
| 378 | // RTC_DCHECK_RUN_ON(network_thread()); |
| 379 | return data_channel_transport_; |
| 380 | } |
| 381 | |
| 382 | void DataChannelController::set_data_channel_transport( |
| 383 | DataChannelTransportInterface* transport) { |
| 384 | RTC_DCHECK_RUN_ON(network_thread()); |
| 385 | data_channel_transport_ = transport; |
| 386 | } |
| 387 | |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 11:47:42 | [diff] [blame] | 388 | bool DataChannelController::DataChannelSendData( |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 389 | int sid, |
| 390 | const SendDataParams& params, |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 11:47:42 | [diff] [blame] | 391 | const rtc::CopyOnWriteBuffer& payload, |
| 392 | cricket::SendDataResult* result) { |
| 393 | // TODO(bugs.webrtc.org/11547): Expect method to be called on the network |
| 394 | // thread instead. Remove the Invoke() below and move assocated state to |
| 395 | // the network thread. |
| 396 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 397 | RTC_DCHECK(data_channel_transport()); |
| 398 | |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 11:47:42 | [diff] [blame] | 399 | RTCError error = network_thread()->Invoke<RTCError>( |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 400 | RTC_FROM_HERE, [this, sid, params, payload] { |
| 401 | return data_channel_transport()->SendData(sid, params, payload); |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 11:47:42 | [diff] [blame] | 402 | }); |
| 403 | |
| 404 | if (error.ok()) { |
| 405 | *result = cricket::SendDataResult::SDR_SUCCESS; |
| 406 | return true; |
| 407 | } else if (error.type() == RTCErrorType::RESOURCE_EXHAUSTED) { |
| 408 | // SCTP transport uses RESOURCE_EXHAUSTED when it's blocked. |
| 409 | // TODO(mellem): Stop using RTCError here and get rid of the mapping. |
| 410 | *result = cricket::SendDataResult::SDR_BLOCK; |
| 411 | return false; |
| 412 | } |
| 413 | *result = cricket::SendDataResult::SDR_ERROR; |
| 414 | return false; |
| 415 | } |
| 416 | |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 14:54:10 | [diff] [blame] | 417 | void DataChannelController::NotifyDataChannelsOfTransportCreated() { |
| 418 | RTC_DCHECK_RUN_ON(network_thread()); |
Niels Möller | 236e36c | 2021-03-23 08:23:10 | [diff] [blame] | 419 | signaling_thread()->PostTask( |
| 420 | ToQueuedTask([self = weak_factory_.GetWeakPtr()] { |
| 421 | if (self) { |
| 422 | RTC_DCHECK_RUN_ON(self->signaling_thread()); |
| 423 | for (const auto& channel : self->sctp_data_channels_) { |
| 424 | channel->OnTransportChannelCreated(); |
| 425 | } |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 14:54:10 | [diff] [blame] | 426 | } |
Niels Möller | 236e36c | 2021-03-23 08:23:10 | [diff] [blame] | 427 | })); |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 14:54:10 | [diff] [blame] | 428 | } |
| 429 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 430 | rtc::Thread* DataChannelController::network_thread() const { |
| 431 | return pc_->network_thread(); |
| 432 | } |
| 433 | rtc::Thread* DataChannelController::signaling_thread() const { |
| 434 | return pc_->signaling_thread(); |
| 435 | } |
| 436 | |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 437 | } // namespace webrtc |