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>(); |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 16:54:10 +0200 | [diff] [blame] | 177 | |
| 178 | // There's a new data channel transport. This needs to be signaled to the |
| 179 | // |sctp_data_channels_| so that they can reopen and reconnect. This is |
| 180 | // necessary when bundling is applied. |
| 181 | NotifyDataChannelsOfTransportCreated(); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 184 | void DataChannelController::TeardownDataChannelTransport_n() { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 185 | RTC_DCHECK_RUN_ON(network_thread()); |
| 186 | data_channel_transport_invoker_ = nullptr; |
| 187 | if (data_channel_transport()) { |
| 188 | data_channel_transport()->SetDataSink(nullptr); |
| 189 | } |
| 190 | set_data_channel_transport(nullptr); |
| 191 | } |
| 192 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 193 | void DataChannelController::OnTransportChanged( |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 194 | DataChannelTransportInterface* new_data_channel_transport) { |
| 195 | RTC_DCHECK_RUN_ON(network_thread()); |
| 196 | if (data_channel_transport() && |
| 197 | data_channel_transport() != new_data_channel_transport) { |
| 198 | // Changed which data channel transport is used for |sctp_mid_| (eg. now |
| 199 | // it's bundled). |
| 200 | data_channel_transport()->SetDataSink(nullptr); |
| 201 | set_data_channel_transport(new_data_channel_transport); |
| 202 | if (new_data_channel_transport) { |
| 203 | new_data_channel_transport->SetDataSink(this); |
| 204 | |
| 205 | // There's a new data channel transport. This needs to be signaled to the |
| 206 | // |sctp_data_channels_| so that they can reopen and reconnect. This is |
| 207 | // necessary when bundling is applied. |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 16:54:10 +0200 | [diff] [blame] | 208 | NotifyDataChannelsOfTransportCreated(); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 16:54:10 +0200 | [diff] [blame] | 213 | std::vector<DataChannel::Stats> DataChannelController::GetDataChannelStats() |
| 214 | const { |
| 215 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 216 | std::vector<DataChannel::Stats> stats; |
| 217 | stats.reserve(sctp_data_channels_.size()); |
| 218 | for (const auto& channel : sctp_data_channels_) |
| 219 | stats.push_back(channel->GetStats()); |
| 220 | return stats; |
| 221 | } |
| 222 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 223 | bool DataChannelController::HandleOpenMessage_s( |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 224 | const cricket::ReceiveDataParams& params, |
| 225 | const rtc::CopyOnWriteBuffer& buffer) { |
| 226 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(buffer)) { |
| 227 | // Received OPEN message; parse and signal that a new data channel should |
| 228 | // be created. |
| 229 | std::string label; |
| 230 | InternalDataChannelInit config; |
| 231 | config.id = params.ssrc; |
| 232 | if (!ParseDataChannelOpenMessage(buffer, &label, &config)) { |
| 233 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for ssrc " |
| 234 | << params.ssrc; |
| 235 | return true; |
| 236 | } |
| 237 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 238 | OnDataChannelOpenMessage(label, config); |
| 239 | return true; |
| 240 | } |
| 241 | return false; |
| 242 | } |
| 243 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 244 | void DataChannelController::OnDataChannelOpenMessage( |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 245 | const std::string& label, |
| 246 | const InternalDataChannelInit& config) { |
| 247 | rtc::scoped_refptr<DataChannel> channel( |
| 248 | InternalCreateDataChannel(label, &config)); |
| 249 | if (!channel.get()) { |
| 250 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
Tomas Gunnarsson | 6476d0b | 2020-06-16 18:39:50 +0200 | [diff] [blame] | 255 | DataChannel::CreateProxy(std::move(channel)); |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 256 | pc_->Observer()->OnDataChannel(std::move(proxy_channel)); |
| 257 | pc_->NoteDataAddedEvent(); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | rtc::scoped_refptr<DataChannel> |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 261 | DataChannelController::InternalCreateDataChannel( |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 262 | const std::string& label, |
| 263 | const InternalDataChannelInit* config) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 264 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 265 | if (pc_->IsClosed()) { |
| 266 | return nullptr; |
| 267 | } |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 268 | if (data_channel_type_ == cricket::DCT_NONE) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 269 | RTC_LOG(LS_ERROR) |
| 270 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 271 | return nullptr; |
| 272 | } |
| 273 | InternalDataChannelInit new_config = |
| 274 | config ? (*config) : InternalDataChannelInit(); |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 275 | if (DataChannel::IsSctpLike(data_channel_type_)) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 276 | if (new_config.id < 0) { |
| 277 | rtc::SSLRole role; |
| 278 | if ((pc_->GetSctpSslRole(&role)) && |
| 279 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
| 280 | RTC_LOG(LS_ERROR) |
| 281 | << "No id can be allocated for the SCTP data channel."; |
| 282 | return nullptr; |
| 283 | } |
| 284 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
| 285 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
| 286 | "because the id is already in use or out of range."; |
| 287 | return nullptr; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | rtc::scoped_refptr<DataChannel> channel( |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 13:47:42 +0200 | [diff] [blame] | 292 | DataChannel::Create(this, data_channel_type(), label, new_config, |
| 293 | signaling_thread(), network_thread())); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 294 | if (!channel) { |
| 295 | sid_allocator_.ReleaseSid(new_config.id); |
| 296 | return nullptr; |
| 297 | } |
| 298 | |
| 299 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 300 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 301 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 302 | << " already exists."; |
| 303 | return nullptr; |
| 304 | } |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 305 | rtp_data_channels_[channel->label()] = channel; |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 306 | } else { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 307 | RTC_DCHECK(DataChannel::IsSctpLike(data_channel_type_)); |
| 308 | sctp_data_channels_.push_back(channel); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 309 | channel->SignalClosed.connect(pc_, |
| 310 | &PeerConnection::OnSctpDataChannelClosed); |
| 311 | } |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 312 | SignalDataChannelCreated_(channel.get()); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 313 | return channel; |
| 314 | } |
| 315 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 316 | void DataChannelController::AllocateSctpSids(rtc::SSLRole role) { |
| 317 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 318 | std::vector<rtc::scoped_refptr<DataChannel>> channels_to_close; |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 319 | for (const auto& channel : sctp_data_channels_) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 320 | if (channel->id() < 0) { |
| 321 | int sid; |
| 322 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
| 323 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid, closing channel."; |
| 324 | channels_to_close.push_back(channel); |
| 325 | continue; |
| 326 | } |
| 327 | channel->SetSctpSid(sid); |
| 328 | } |
| 329 | } |
| 330 | // Since closing modifies the list of channels, we have to do the actual |
| 331 | // closing outside the loop. |
| 332 | for (const auto& channel : channels_to_close) { |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 333 | channel->CloseAbruptlyWithDataChannelFailure("Failed to allocate SCTP SID"); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 337 | void DataChannelController::OnSctpDataChannelClosed(DataChannel* channel) { |
| 338 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 339 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 340 | ++it) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 341 | if (it->get() == channel) { |
| 342 | if (channel->id() >= 0) { |
| 343 | // After the closing procedure is done, it's safe to use this ID for |
| 344 | // another data channel. |
| 345 | sid_allocator_.ReleaseSid(channel->id()); |
| 346 | } |
| 347 | // Since this method is triggered by a signal from the DataChannel, |
| 348 | // 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] | 349 | sctp_data_channels_to_free_.push_back(*it); |
| 350 | sctp_data_channels_.erase(it); |
Harald Alvestrand | 246724b | 2019-12-03 22:31:42 +0100 | [diff] [blame] | 351 | signaling_thread()->PostTask( |
| 352 | RTC_FROM_HERE, [self = weak_factory_.GetWeakPtr()] { |
| 353 | if (self) { |
| 354 | RTC_DCHECK_RUN_ON(self->signaling_thread()); |
| 355 | self->sctp_data_channels_to_free_.clear(); |
| 356 | } |
| 357 | }); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 358 | return; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 363 | void DataChannelController::OnTransportChannelClosed() { |
| 364 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 365 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 366 | // DataChannel may callback to us and try to modify the list. |
| 367 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 368 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 369 | for (const auto& kv : temp_rtp_dcs) { |
| 370 | kv.second->OnTransportChannelClosed(); |
| 371 | } |
| 372 | |
| 373 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 374 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 375 | for (const auto& channel : temp_sctp_dcs) { |
| 376 | channel->OnTransportChannelClosed(); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | DataChannel* DataChannelController::FindDataChannelBySid(int sid) const { |
| 381 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 382 | for (const auto& channel : sctp_data_channels_) { |
| 383 | if (channel->id() == sid) { |
| 384 | return channel; |
| 385 | } |
| 386 | } |
| 387 | return nullptr; |
| 388 | } |
| 389 | |
| 390 | void DataChannelController::UpdateLocalRtpDataChannels( |
| 391 | const cricket::StreamParamsVec& streams) { |
| 392 | std::vector<std::string> existing_channels; |
| 393 | |
| 394 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 395 | // Find new and active data channels. |
| 396 | for (const cricket::StreamParams& params : streams) { |
| 397 | // |it->sync_label| is actually the data channel label. The reason is that |
| 398 | // we use the same naming of data channels as we do for |
| 399 | // MediaStreams and Tracks. |
| 400 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 401 | // track label is the same as |streamid|. |
| 402 | const std::string& channel_label = params.first_stream_id(); |
| 403 | auto data_channel_it = rtp_data_channels()->find(channel_label); |
| 404 | if (data_channel_it == rtp_data_channels()->end()) { |
| 405 | RTC_LOG(LS_ERROR) << "channel label not found"; |
| 406 | continue; |
| 407 | } |
| 408 | // Set the SSRC the data channel should use for sending. |
| 409 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 410 | existing_channels.push_back(data_channel_it->first); |
| 411 | } |
| 412 | |
| 413 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 414 | } |
| 415 | |
| 416 | void DataChannelController::UpdateRemoteRtpDataChannels( |
| 417 | const cricket::StreamParamsVec& streams) { |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 13:47:42 +0200 | [diff] [blame] | 418 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 419 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 420 | std::vector<std::string> existing_channels; |
| 421 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 422 | // Find new and active data channels. |
| 423 | for (const cricket::StreamParams& params : streams) { |
Artem Titov | f0a34f2 | 2020-03-16 17:52:04 +0000 | [diff] [blame] | 424 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 425 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 426 | std::string label = params.first_stream_id().empty() |
| 427 | ? rtc::ToString(params.first_ssrc()) |
| 428 | : params.first_stream_id(); |
| 429 | auto data_channel_it = rtp_data_channels()->find(label); |
| 430 | if (data_channel_it == rtp_data_channels()->end()) { |
| 431 | // This is a new data channel. |
| 432 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 433 | } else { |
| 434 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 435 | } |
| 436 | existing_channels.push_back(label); |
| 437 | } |
| 438 | |
| 439 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 440 | } |
| 441 | |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 13:47:42 +0200 | [diff] [blame] | 442 | cricket::DataChannelType DataChannelController::data_channel_type() const { |
| 443 | // TODO(bugs.webrtc.org/9987): Should be restricted to the signaling thread. |
| 444 | // RTC_DCHECK_RUN_ON(signaling_thread()); |
| 445 | return data_channel_type_; |
| 446 | } |
| 447 | |
| 448 | void DataChannelController::set_data_channel_type( |
| 449 | cricket::DataChannelType type) { |
| 450 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 451 | data_channel_type_ = type; |
| 452 | } |
| 453 | |
| 454 | DataChannelTransportInterface* DataChannelController::data_channel_transport() |
| 455 | const { |
| 456 | // TODO(bugs.webrtc.org/11547): Only allow this accessor to be called on the |
| 457 | // network thread. |
| 458 | // RTC_DCHECK_RUN_ON(network_thread()); |
| 459 | return data_channel_transport_; |
| 460 | } |
| 461 | |
| 462 | void DataChannelController::set_data_channel_transport( |
| 463 | DataChannelTransportInterface* transport) { |
| 464 | RTC_DCHECK_RUN_ON(network_thread()); |
| 465 | data_channel_transport_ = transport; |
| 466 | } |
| 467 | |
| 468 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>* |
| 469 | DataChannelController::rtp_data_channels() const { |
| 470 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 471 | return &rtp_data_channels_; |
| 472 | } |
| 473 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 474 | void DataChannelController::UpdateClosingRtpDataChannels( |
| 475 | const std::vector<std::string>& active_channels, |
| 476 | bool is_local_update) { |
| 477 | auto it = rtp_data_channels_.begin(); |
| 478 | while (it != rtp_data_channels_.end()) { |
| 479 | DataChannel* data_channel = it->second; |
| 480 | if (absl::c_linear_search(active_channels, data_channel->label())) { |
| 481 | ++it; |
| 482 | continue; |
| 483 | } |
| 484 | |
| 485 | if (is_local_update) { |
| 486 | data_channel->SetSendSsrc(0); |
| 487 | } else { |
| 488 | data_channel->RemotePeerRequestClose(); |
| 489 | } |
| 490 | |
| 491 | if (data_channel->state() == DataChannel::kClosed) { |
| 492 | rtp_data_channels_.erase(it); |
| 493 | it = rtp_data_channels_.begin(); |
| 494 | } else { |
| 495 | ++it; |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | void DataChannelController::CreateRemoteRtpDataChannel(const std::string& label, |
| 501 | uint32_t remote_ssrc) { |
| 502 | rtc::scoped_refptr<DataChannel> channel( |
| 503 | InternalCreateDataChannel(label, nullptr)); |
| 504 | if (!channel.get()) { |
| 505 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
| 506 | "CreateDataChannel failed."; |
| 507 | return; |
| 508 | } |
| 509 | channel->SetReceiveSsrc(remote_ssrc); |
| 510 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
Tomas Gunnarsson | 6476d0b | 2020-06-16 18:39:50 +0200 | [diff] [blame] | 511 | DataChannel::CreateProxy(std::move(channel)); |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 512 | pc_->Observer()->OnDataChannel(std::move(proxy_channel)); |
| 513 | } |
| 514 | |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 13:47:42 +0200 | [diff] [blame] | 515 | bool DataChannelController::DataChannelSendData( |
| 516 | const cricket::SendDataParams& params, |
| 517 | const rtc::CopyOnWriteBuffer& payload, |
| 518 | cricket::SendDataResult* result) { |
| 519 | // TODO(bugs.webrtc.org/11547): Expect method to be called on the network |
| 520 | // thread instead. Remove the Invoke() below and move assocated state to |
| 521 | // the network thread. |
| 522 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 523 | RTC_DCHECK(data_channel_transport()); |
| 524 | |
| 525 | SendDataParams send_params; |
| 526 | send_params.type = ToWebrtcDataMessageType(params.type); |
| 527 | send_params.ordered = params.ordered; |
| 528 | if (params.max_rtx_count >= 0) { |
| 529 | send_params.max_rtx_count = params.max_rtx_count; |
| 530 | } else if (params.max_rtx_ms >= 0) { |
| 531 | send_params.max_rtx_ms = params.max_rtx_ms; |
| 532 | } |
| 533 | |
| 534 | RTCError error = network_thread()->Invoke<RTCError>( |
| 535 | RTC_FROM_HERE, [this, params, send_params, payload] { |
| 536 | return data_channel_transport()->SendData(params.sid, send_params, |
| 537 | payload); |
| 538 | }); |
| 539 | |
| 540 | if (error.ok()) { |
| 541 | *result = cricket::SendDataResult::SDR_SUCCESS; |
| 542 | return true; |
| 543 | } else if (error.type() == RTCErrorType::RESOURCE_EXHAUSTED) { |
| 544 | // SCTP transport uses RESOURCE_EXHAUSTED when it's blocked. |
| 545 | // TODO(mellem): Stop using RTCError here and get rid of the mapping. |
| 546 | *result = cricket::SendDataResult::SDR_BLOCK; |
| 547 | return false; |
| 548 | } |
| 549 | *result = cricket::SendDataResult::SDR_ERROR; |
| 550 | return false; |
| 551 | } |
| 552 | |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 16:54:10 +0200 | [diff] [blame] | 553 | void DataChannelController::NotifyDataChannelsOfTransportCreated() { |
| 554 | RTC_DCHECK_RUN_ON(network_thread()); |
| 555 | data_channel_transport_invoker_->AsyncInvoke<void>( |
| 556 | RTC_FROM_HERE, signaling_thread(), [this] { |
| 557 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 558 | for (const auto& channel : sctp_data_channels_) { |
| 559 | channel->OnTransportChannelCreated(); |
| 560 | } |
| 561 | }); |
| 562 | } |
| 563 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 564 | rtc::Thread* DataChannelController::network_thread() const { |
| 565 | return pc_->network_thread(); |
| 566 | } |
| 567 | rtc::Thread* DataChannelController::signaling_thread() const { |
| 568 | return pc_->signaling_thread(); |
| 569 | } |
| 570 | |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 571 | } // namespace webrtc |