Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [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 14:04:21 +0100 | [diff] [blame] | 11 | #include "pc/data_channel_controller.h" |
| 12 | |
| 13 | #include <utility> |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 14 | |
| 15 | #include "pc/peer_connection.h" |
| 16 | #include "pc/sctp_utils.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 20 | bool DataChannelController::HasDataChannels() const { |
| 21 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 22 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 23 | } |
| 24 | |
| 25 | bool DataChannelController::SendData(const cricket::SendDataParams& params, |
| 26 | const rtc::CopyOnWriteBuffer& payload, |
| 27 | cricket::SendDataResult* result) { |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 13:47:42 +0200 | [diff] [blame^] | 28 | if (data_channel_transport()) |
| 29 | return DataChannelSendData(params, payload, result); |
| 30 | if (rtp_data_channel()) |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 31 | return rtp_data_channel()->SendData(params, payload, result); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 32 | RTC_LOG(LS_ERROR) << "SendData called before transport is ready"; |
| 33 | return false; |
| 34 | } |
| 35 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 36 | bool DataChannelController::ConnectDataChannel( |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 37 | DataChannel* webrtc_data_channel) { |
| 38 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 39 | if (!rtp_data_channel() && !data_channel_transport()) { |
| 40 | // Don't log an error here, because DataChannels are expected to call |
| 41 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 42 | // whether or not the underlying transport is ready. |
| 43 | return false; |
| 44 | } |
| 45 | if (data_channel_transport()) { |
| 46 | SignalDataChannelTransportWritable_s.connect(webrtc_data_channel, |
| 47 | &DataChannel::OnChannelReady); |
| 48 | SignalDataChannelTransportReceivedData_s.connect( |
| 49 | webrtc_data_channel, &DataChannel::OnDataReceived); |
| 50 | SignalDataChannelTransportChannelClosing_s.connect( |
| 51 | webrtc_data_channel, &DataChannel::OnClosingProcedureStartedRemotely); |
| 52 | SignalDataChannelTransportChannelClosed_s.connect( |
| 53 | webrtc_data_channel, &DataChannel::OnClosingProcedureComplete); |
| 54 | } |
| 55 | if (rtp_data_channel()) { |
| 56 | rtp_data_channel()->SignalReadyToSendData.connect( |
| 57 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 58 | rtp_data_channel()->SignalDataReceived.connect( |
| 59 | webrtc_data_channel, &DataChannel::OnDataReceived); |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 64 | void DataChannelController::DisconnectDataChannel( |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 65 | DataChannel* webrtc_data_channel) { |
| 66 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 67 | if (!rtp_data_channel() && !data_channel_transport()) { |
| 68 | RTC_LOG(LS_ERROR) |
| 69 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 70 | "sctp_transport_ are NULL."; |
| 71 | return; |
| 72 | } |
| 73 | if (data_channel_transport()) { |
| 74 | SignalDataChannelTransportWritable_s.disconnect(webrtc_data_channel); |
| 75 | SignalDataChannelTransportReceivedData_s.disconnect(webrtc_data_channel); |
| 76 | SignalDataChannelTransportChannelClosing_s.disconnect(webrtc_data_channel); |
| 77 | SignalDataChannelTransportChannelClosed_s.disconnect(webrtc_data_channel); |
| 78 | } |
| 79 | if (rtp_data_channel()) { |
| 80 | rtp_data_channel()->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 81 | rtp_data_channel()->SignalDataReceived.disconnect(webrtc_data_channel); |
| 82 | } |
| 83 | } |
| 84 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 85 | void DataChannelController::AddSctpDataStream(int sid) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 86 | if (data_channel_transport()) { |
| 87 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this, sid] { |
| 88 | if (data_channel_transport()) { |
| 89 | data_channel_transport()->OpenChannel(sid); |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | } |
| 94 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 95 | void DataChannelController::RemoveSctpDataStream(int sid) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 96 | if (data_channel_transport()) { |
| 97 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this, sid] { |
| 98 | if (data_channel_transport()) { |
| 99 | data_channel_transport()->CloseChannel(sid); |
| 100 | } |
| 101 | }); |
| 102 | } |
| 103 | } |
| 104 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 105 | bool DataChannelController::ReadyToSendData() const { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 106 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 107 | return (rtp_data_channel() && rtp_data_channel()->ready_to_send_data()) || |
| 108 | (data_channel_transport() && data_channel_transport_ready_to_send_); |
| 109 | } |
| 110 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 111 | void DataChannelController::OnDataReceived( |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 112 | int channel_id, |
| 113 | DataMessageType type, |
| 114 | const rtc::CopyOnWriteBuffer& buffer) { |
| 115 | RTC_DCHECK_RUN_ON(network_thread()); |
| 116 | cricket::ReceiveDataParams params; |
| 117 | params.sid = channel_id; |
| 118 | params.type = ToCricketDataMessageType(type); |
| 119 | data_channel_transport_invoker_->AsyncInvoke<void>( |
| 120 | RTC_FROM_HERE, signaling_thread(), [this, params, buffer] { |
| 121 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 13:47:42 +0200 | [diff] [blame^] | 122 | // TODO(bugs.webrtc.org/11547): The data being received should be |
| 123 | // delivered on the network thread. The way HandleOpenMessage_s works |
| 124 | // right now is that it's called for all types of buffers and operates |
| 125 | // as a selector function. Change this so that it's only called for |
| 126 | // buffers that it should be able to handle. Once we do that, we can |
| 127 | // deliver all other buffers on the network thread (change |
| 128 | // SignalDataChannelTransportReceivedData_s to |
| 129 | // SignalDataChannelTransportReceivedData_n). |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 130 | if (!HandleOpenMessage_s(params, buffer)) { |
| 131 | SignalDataChannelTransportReceivedData_s(params, buffer); |
| 132 | } |
| 133 | }); |
| 134 | } |
| 135 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 136 | void DataChannelController::OnChannelClosing(int channel_id) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 137 | RTC_DCHECK_RUN_ON(network_thread()); |
| 138 | data_channel_transport_invoker_->AsyncInvoke<void>( |
| 139 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 140 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 141 | SignalDataChannelTransportChannelClosing_s(channel_id); |
| 142 | }); |
| 143 | } |
| 144 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 145 | void DataChannelController::OnChannelClosed(int channel_id) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 146 | RTC_DCHECK_RUN_ON(network_thread()); |
| 147 | data_channel_transport_invoker_->AsyncInvoke<void>( |
| 148 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 149 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 150 | SignalDataChannelTransportChannelClosed_s(channel_id); |
| 151 | }); |
| 152 | } |
| 153 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 154 | void DataChannelController::OnReadyToSend() { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 155 | RTC_DCHECK_RUN_ON(network_thread()); |
| 156 | data_channel_transport_invoker_->AsyncInvoke<void>( |
| 157 | RTC_FROM_HERE, signaling_thread(), [this] { |
| 158 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 159 | data_channel_transport_ready_to_send_ = true; |
| 160 | SignalDataChannelTransportWritable_s( |
| 161 | data_channel_transport_ready_to_send_); |
| 162 | }); |
| 163 | } |
| 164 | |
Harald Alvestrand | 2697ac1 | 2019-12-16 10:37:04 +0100 | [diff] [blame] | 165 | void DataChannelController::OnTransportClosed() { |
| 166 | RTC_DCHECK_RUN_ON(network_thread()); |
| 167 | data_channel_transport_invoker_->AsyncInvoke<void>( |
| 168 | RTC_FROM_HERE, signaling_thread(), [this] { |
| 169 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 170 | OnTransportChannelClosed(); |
| 171 | }); |
| 172 | } |
| 173 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 174 | void DataChannelController::SetupDataChannelTransport_n() { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 175 | RTC_DCHECK_RUN_ON(network_thread()); |
| 176 | data_channel_transport_invoker_ = std::make_unique<rtc::AsyncInvoker>(); |
| 177 | } |
| 178 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 179 | void DataChannelController::TeardownDataChannelTransport_n() { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 180 | RTC_DCHECK_RUN_ON(network_thread()); |
| 181 | data_channel_transport_invoker_ = nullptr; |
| 182 | if (data_channel_transport()) { |
| 183 | data_channel_transport()->SetDataSink(nullptr); |
| 184 | } |
| 185 | set_data_channel_transport(nullptr); |
| 186 | } |
| 187 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 188 | void DataChannelController::OnTransportChanged( |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 189 | DataChannelTransportInterface* new_data_channel_transport) { |
| 190 | RTC_DCHECK_RUN_ON(network_thread()); |
| 191 | if (data_channel_transport() && |
| 192 | data_channel_transport() != new_data_channel_transport) { |
| 193 | // Changed which data channel transport is used for |sctp_mid_| (eg. now |
| 194 | // it's bundled). |
| 195 | data_channel_transport()->SetDataSink(nullptr); |
| 196 | set_data_channel_transport(new_data_channel_transport); |
| 197 | if (new_data_channel_transport) { |
| 198 | new_data_channel_transport->SetDataSink(this); |
| 199 | |
| 200 | // There's a new data channel transport. This needs to be signaled to the |
| 201 | // |sctp_data_channels_| so that they can reopen and reconnect. This is |
| 202 | // necessary when bundling is applied. |
| 203 | data_channel_transport_invoker_->AsyncInvoke<void>( |
| 204 | RTC_FROM_HERE, signaling_thread(), [this] { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 205 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Mirko Bonadei | 16d0d37 | 2020-04-03 12:09:58 +0200 | [diff] [blame] | 206 | for (const auto& channel : sctp_data_channels_) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 207 | channel->OnTransportChannelCreated(); |
| 208 | } |
| 209 | }); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 214 | bool DataChannelController::HandleOpenMessage_s( |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 215 | const cricket::ReceiveDataParams& params, |
| 216 | const rtc::CopyOnWriteBuffer& buffer) { |
| 217 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(buffer)) { |
| 218 | // Received OPEN message; parse and signal that a new data channel should |
| 219 | // be created. |
| 220 | std::string label; |
| 221 | InternalDataChannelInit config; |
| 222 | config.id = params.ssrc; |
| 223 | if (!ParseDataChannelOpenMessage(buffer, &label, &config)) { |
| 224 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for ssrc " |
| 225 | << params.ssrc; |
| 226 | return true; |
| 227 | } |
| 228 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 229 | OnDataChannelOpenMessage(label, config); |
| 230 | return true; |
| 231 | } |
| 232 | return false; |
| 233 | } |
| 234 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 235 | void DataChannelController::OnDataChannelOpenMessage( |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 236 | const std::string& label, |
| 237 | const InternalDataChannelInit& config) { |
| 238 | rtc::scoped_refptr<DataChannel> channel( |
| 239 | InternalCreateDataChannel(label, &config)); |
| 240 | if (!channel.get()) { |
| 241 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
| 242 | return; |
| 243 | } |
| 244 | |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 13:47:42 +0200 | [diff] [blame^] | 245 | // TODO(bugs.webrtc.org/11547): Inject the network thread as well. |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 246 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 247 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 248 | pc_->Observer()->OnDataChannel(std::move(proxy_channel)); |
| 249 | pc_->NoteDataAddedEvent(); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | rtc::scoped_refptr<DataChannel> |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 253 | DataChannelController::InternalCreateDataChannel( |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 254 | const std::string& label, |
| 255 | const InternalDataChannelInit* config) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 256 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 257 | if (pc_->IsClosed()) { |
| 258 | return nullptr; |
| 259 | } |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 260 | if (data_channel_type_ == cricket::DCT_NONE) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 261 | RTC_LOG(LS_ERROR) |
| 262 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 263 | return nullptr; |
| 264 | } |
| 265 | InternalDataChannelInit new_config = |
| 266 | config ? (*config) : InternalDataChannelInit(); |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 267 | if (DataChannel::IsSctpLike(data_channel_type_)) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 268 | if (new_config.id < 0) { |
| 269 | rtc::SSLRole role; |
| 270 | if ((pc_->GetSctpSslRole(&role)) && |
| 271 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
| 272 | RTC_LOG(LS_ERROR) |
| 273 | << "No id can be allocated for the SCTP data channel."; |
| 274 | return nullptr; |
| 275 | } |
| 276 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
| 277 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
| 278 | "because the id is already in use or out of range."; |
| 279 | return nullptr; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | rtc::scoped_refptr<DataChannel> channel( |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 13:47:42 +0200 | [diff] [blame^] | 284 | DataChannel::Create(this, data_channel_type(), label, new_config, |
| 285 | signaling_thread(), network_thread())); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 286 | if (!channel) { |
| 287 | sid_allocator_.ReleaseSid(new_config.id); |
| 288 | return nullptr; |
| 289 | } |
| 290 | |
| 291 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 292 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 293 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 294 | << " already exists."; |
| 295 | return nullptr; |
| 296 | } |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 297 | rtp_data_channels_[channel->label()] = channel; |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 298 | } else { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 299 | RTC_DCHECK(DataChannel::IsSctpLike(data_channel_type_)); |
| 300 | sctp_data_channels_.push_back(channel); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 301 | channel->SignalClosed.connect(pc_, |
| 302 | &PeerConnection::OnSctpDataChannelClosed); |
| 303 | } |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 304 | SignalDataChannelCreated_(channel.get()); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 305 | return channel; |
| 306 | } |
| 307 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 308 | void DataChannelController::AllocateSctpSids(rtc::SSLRole role) { |
| 309 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 310 | std::vector<rtc::scoped_refptr<DataChannel>> channels_to_close; |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 311 | for (const auto& channel : sctp_data_channels_) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 312 | if (channel->id() < 0) { |
| 313 | int sid; |
| 314 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
| 315 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid, closing channel."; |
| 316 | channels_to_close.push_back(channel); |
| 317 | continue; |
| 318 | } |
| 319 | channel->SetSctpSid(sid); |
| 320 | } |
| 321 | } |
| 322 | // Since closing modifies the list of channels, we have to do the actual |
| 323 | // closing outside the loop. |
| 324 | for (const auto& channel : channels_to_close) { |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 325 | channel->CloseAbruptlyWithDataChannelFailure("Failed to allocate SCTP SID"); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 329 | void DataChannelController::OnSctpDataChannelClosed(DataChannel* channel) { |
| 330 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 331 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 332 | ++it) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 333 | if (it->get() == channel) { |
| 334 | if (channel->id() >= 0) { |
| 335 | // After the closing procedure is done, it's safe to use this ID for |
| 336 | // another data channel. |
| 337 | sid_allocator_.ReleaseSid(channel->id()); |
| 338 | } |
| 339 | // Since this method is triggered by a signal from the DataChannel, |
| 340 | // we can't free it directly here; we need to free it asynchronously. |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 341 | sctp_data_channels_to_free_.push_back(*it); |
| 342 | sctp_data_channels_.erase(it); |
Harald Alvestrand | 246724b | 2019-12-03 22:31:42 +0100 | [diff] [blame] | 343 | signaling_thread()->PostTask( |
| 344 | RTC_FROM_HERE, [self = weak_factory_.GetWeakPtr()] { |
| 345 | if (self) { |
| 346 | RTC_DCHECK_RUN_ON(self->signaling_thread()); |
| 347 | self->sctp_data_channels_to_free_.clear(); |
| 348 | } |
| 349 | }); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 350 | return; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 355 | void DataChannelController::OnTransportChannelClosed() { |
| 356 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 357 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 358 | // DataChannel may callback to us and try to modify the list. |
| 359 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 360 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 361 | for (const auto& kv : temp_rtp_dcs) { |
| 362 | kv.second->OnTransportChannelClosed(); |
| 363 | } |
| 364 | |
| 365 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 366 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 367 | for (const auto& channel : temp_sctp_dcs) { |
| 368 | channel->OnTransportChannelClosed(); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | DataChannel* DataChannelController::FindDataChannelBySid(int sid) const { |
| 373 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 374 | for (const auto& channel : sctp_data_channels_) { |
| 375 | if (channel->id() == sid) { |
| 376 | return channel; |
| 377 | } |
| 378 | } |
| 379 | return nullptr; |
| 380 | } |
| 381 | |
| 382 | void DataChannelController::UpdateLocalRtpDataChannels( |
| 383 | const cricket::StreamParamsVec& streams) { |
| 384 | std::vector<std::string> existing_channels; |
| 385 | |
| 386 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 387 | // Find new and active data channels. |
| 388 | for (const cricket::StreamParams& params : streams) { |
| 389 | // |it->sync_label| is actually the data channel label. The reason is that |
| 390 | // we use the same naming of data channels as we do for |
| 391 | // MediaStreams and Tracks. |
| 392 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 393 | // track label is the same as |streamid|. |
| 394 | const std::string& channel_label = params.first_stream_id(); |
| 395 | auto data_channel_it = rtp_data_channels()->find(channel_label); |
| 396 | if (data_channel_it == rtp_data_channels()->end()) { |
| 397 | RTC_LOG(LS_ERROR) << "channel label not found"; |
| 398 | continue; |
| 399 | } |
| 400 | // Set the SSRC the data channel should use for sending. |
| 401 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 402 | existing_channels.push_back(data_channel_it->first); |
| 403 | } |
| 404 | |
| 405 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 406 | } |
| 407 | |
| 408 | void DataChannelController::UpdateRemoteRtpDataChannels( |
| 409 | const cricket::StreamParamsVec& streams) { |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 13:47:42 +0200 | [diff] [blame^] | 410 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 411 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 412 | std::vector<std::string> existing_channels; |
| 413 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 414 | // Find new and active data channels. |
| 415 | for (const cricket::StreamParams& params : streams) { |
Artem Titov | f0a34f2 | 2020-03-16 17:52:04 +0000 | [diff] [blame] | 416 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 417 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 418 | std::string label = params.first_stream_id().empty() |
| 419 | ? rtc::ToString(params.first_ssrc()) |
| 420 | : params.first_stream_id(); |
| 421 | auto data_channel_it = rtp_data_channels()->find(label); |
| 422 | if (data_channel_it == rtp_data_channels()->end()) { |
| 423 | // This is a new data channel. |
| 424 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 425 | } else { |
| 426 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 427 | } |
| 428 | existing_channels.push_back(label); |
| 429 | } |
| 430 | |
| 431 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 432 | } |
| 433 | |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 13:47:42 +0200 | [diff] [blame^] | 434 | cricket::DataChannelType DataChannelController::data_channel_type() const { |
| 435 | // TODO(bugs.webrtc.org/9987): Should be restricted to the signaling thread. |
| 436 | // RTC_DCHECK_RUN_ON(signaling_thread()); |
| 437 | return data_channel_type_; |
| 438 | } |
| 439 | |
| 440 | void DataChannelController::set_data_channel_type( |
| 441 | cricket::DataChannelType type) { |
| 442 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 443 | data_channel_type_ = type; |
| 444 | } |
| 445 | |
| 446 | DataChannelTransportInterface* DataChannelController::data_channel_transport() |
| 447 | const { |
| 448 | // TODO(bugs.webrtc.org/11547): Only allow this accessor to be called on the |
| 449 | // network thread. |
| 450 | // RTC_DCHECK_RUN_ON(network_thread()); |
| 451 | return data_channel_transport_; |
| 452 | } |
| 453 | |
| 454 | void DataChannelController::set_data_channel_transport( |
| 455 | DataChannelTransportInterface* transport) { |
| 456 | RTC_DCHECK_RUN_ON(network_thread()); |
| 457 | data_channel_transport_ = transport; |
| 458 | } |
| 459 | |
| 460 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>* |
| 461 | DataChannelController::rtp_data_channels() const { |
| 462 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 463 | return &rtp_data_channels_; |
| 464 | } |
| 465 | |
| 466 | const std::vector<rtc::scoped_refptr<DataChannel>>* |
| 467 | DataChannelController::sctp_data_channels() const { |
| 468 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 469 | return &sctp_data_channels_; |
| 470 | } |
| 471 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 472 | void DataChannelController::UpdateClosingRtpDataChannels( |
| 473 | const std::vector<std::string>& active_channels, |
| 474 | bool is_local_update) { |
| 475 | auto it = rtp_data_channels_.begin(); |
| 476 | while (it != rtp_data_channels_.end()) { |
| 477 | DataChannel* data_channel = it->second; |
| 478 | if (absl::c_linear_search(active_channels, data_channel->label())) { |
| 479 | ++it; |
| 480 | continue; |
| 481 | } |
| 482 | |
| 483 | if (is_local_update) { |
| 484 | data_channel->SetSendSsrc(0); |
| 485 | } else { |
| 486 | data_channel->RemotePeerRequestClose(); |
| 487 | } |
| 488 | |
| 489 | if (data_channel->state() == DataChannel::kClosed) { |
| 490 | rtp_data_channels_.erase(it); |
| 491 | it = rtp_data_channels_.begin(); |
| 492 | } else { |
| 493 | ++it; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | void DataChannelController::CreateRemoteRtpDataChannel(const std::string& label, |
| 499 | uint32_t remote_ssrc) { |
| 500 | rtc::scoped_refptr<DataChannel> channel( |
| 501 | InternalCreateDataChannel(label, nullptr)); |
| 502 | if (!channel.get()) { |
| 503 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
| 504 | "CreateDataChannel failed."; |
| 505 | return; |
| 506 | } |
| 507 | channel->SetReceiveSsrc(remote_ssrc); |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 13:47:42 +0200 | [diff] [blame^] | 508 | // TODO(bugs.webrtc.org/11547): Inject the network thread as well. |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 509 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 510 | DataChannelProxy::Create(signaling_thread(), channel); |
| 511 | pc_->Observer()->OnDataChannel(std::move(proxy_channel)); |
| 512 | } |
| 513 | |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 13:47:42 +0200 | [diff] [blame^] | 514 | bool DataChannelController::DataChannelSendData( |
| 515 | const cricket::SendDataParams& params, |
| 516 | const rtc::CopyOnWriteBuffer& payload, |
| 517 | cricket::SendDataResult* result) { |
| 518 | // TODO(bugs.webrtc.org/11547): Expect method to be called on the network |
| 519 | // thread instead. Remove the Invoke() below and move assocated state to |
| 520 | // the network thread. |
| 521 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 522 | RTC_DCHECK(data_channel_transport()); |
| 523 | |
| 524 | SendDataParams send_params; |
| 525 | send_params.type = ToWebrtcDataMessageType(params.type); |
| 526 | send_params.ordered = params.ordered; |
| 527 | if (params.max_rtx_count >= 0) { |
| 528 | send_params.max_rtx_count = params.max_rtx_count; |
| 529 | } else if (params.max_rtx_ms >= 0) { |
| 530 | send_params.max_rtx_ms = params.max_rtx_ms; |
| 531 | } |
| 532 | |
| 533 | RTCError error = network_thread()->Invoke<RTCError>( |
| 534 | RTC_FROM_HERE, [this, params, send_params, payload] { |
| 535 | return data_channel_transport()->SendData(params.sid, send_params, |
| 536 | payload); |
| 537 | }); |
| 538 | |
| 539 | if (error.ok()) { |
| 540 | *result = cricket::SendDataResult::SDR_SUCCESS; |
| 541 | return true; |
| 542 | } else if (error.type() == RTCErrorType::RESOURCE_EXHAUSTED) { |
| 543 | // SCTP transport uses RESOURCE_EXHAUSTED when it's blocked. |
| 544 | // TODO(mellem): Stop using RTCError here and get rid of the mapping. |
| 545 | *result = cricket::SendDataResult::SDR_BLOCK; |
| 546 | return false; |
| 547 | } |
| 548 | *result = cricket::SendDataResult::SDR_ERROR; |
| 549 | return false; |
| 550 | } |
| 551 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 552 | rtc::Thread* DataChannelController::network_thread() const { |
| 553 | return pc_->network_thread(); |
| 554 | } |
| 555 | rtc::Thread* DataChannelController::signaling_thread() const { |
| 556 | return pc_->signaling_thread(); |
| 557 | } |
| 558 | |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 559 | } // namespace webrtc |