blob: 9891d5025f4d61d5c1f8ebc5d4f30b2c37ca74d0 [file] [log] [blame]
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01001/*
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 Alvestrand05e4d082019-12-03 14:04:21 +010011#include "pc/data_channel_controller.h"
12
13#include <utility>
Harald Alvestrand00cf34c2019-12-02 09:56:02 +010014
15#include "pc/peer_connection.h"
16#include "pc/sctp_utils.h"
17
18namespace webrtc {
19
Harald Alvestrand05e4d082019-12-03 14:04:21 +010020bool DataChannelController::HasDataChannels() const {
21 RTC_DCHECK_RUN_ON(signaling_thread());
22 return !rtp_data_channels_.empty() || !sctp_data_channels_.empty();
23}
24
25bool DataChannelController::SendData(const cricket::SendDataParams& params,
26 const rtc::CopyOnWriteBuffer& payload,
27 cricket::SendDataResult* result) {
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +020028 if (data_channel_transport())
29 return DataChannelSendData(params, payload, result);
30 if (rtp_data_channel())
Harald Alvestrand00cf34c2019-12-02 09:56:02 +010031 return rtp_data_channel()->SendData(params, payload, result);
Harald Alvestrand00cf34c2019-12-02 09:56:02 +010032 RTC_LOG(LS_ERROR) << "SendData called before transport is ready";
33 return false;
34}
35
Harald Alvestrand05e4d082019-12-03 14:04:21 +010036bool DataChannelController::ConnectDataChannel(
Harald Alvestrand00cf34c2019-12-02 09:56:02 +010037 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 Alvestrand05e4d082019-12-03 14:04:21 +010064void DataChannelController::DisconnectDataChannel(
Harald Alvestrand00cf34c2019-12-02 09:56:02 +010065 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 Alvestrand05e4d082019-12-03 14:04:21 +010085void DataChannelController::AddSctpDataStream(int sid) {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +010086 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 Alvestrand05e4d082019-12-03 14:04:21 +010095void DataChannelController::RemoveSctpDataStream(int sid) {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +010096 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 Alvestrand05e4d082019-12-03 14:04:21 +0100105bool DataChannelController::ReadyToSendData() const {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100106 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 Alvestrand05e4d082019-12-03 14:04:21 +0100111void DataChannelController::OnDataReceived(
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100112 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 Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200122 // 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 Alvestrand00cf34c2019-12-02 09:56:02 +0100130 if (!HandleOpenMessage_s(params, buffer)) {
131 SignalDataChannelTransportReceivedData_s(params, buffer);
132 }
133 });
134}
135
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100136void DataChannelController::OnChannelClosing(int channel_id) {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100137 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 Alvestrand05e4d082019-12-03 14:04:21 +0100145void DataChannelController::OnChannelClosed(int channel_id) {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100146 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 Alvestrand05e4d082019-12-03 14:04:21 +0100154void DataChannelController::OnReadyToSend() {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100155 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 Alvestrand2697ac12019-12-16 10:37:04 +0100165void 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 Alvestrand05e4d082019-12-03 14:04:21 +0100174void DataChannelController::SetupDataChannelTransport_n() {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100175 RTC_DCHECK_RUN_ON(network_thread());
176 data_channel_transport_invoker_ = std::make_unique<rtc::AsyncInvoker>();
177}
178
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100179void DataChannelController::TeardownDataChannelTransport_n() {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100180 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 Alvestrand05e4d082019-12-03 14:04:21 +0100188void DataChannelController::OnTransportChanged(
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100189 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 Alvestrand05e4d082019-12-03 14:04:21 +0100205 RTC_DCHECK_RUN_ON(signaling_thread());
Mirko Bonadei16d0d372020-04-03 12:09:58 +0200206 for (const auto& channel : sctp_data_channels_) {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100207 channel->OnTransportChannelCreated();
208 }
209 });
210 }
211 }
212}
213
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100214bool DataChannelController::HandleOpenMessage_s(
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100215 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 Alvestrand05e4d082019-12-03 14:04:21 +0100235void DataChannelController::OnDataChannelOpenMessage(
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100236 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 Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200245 // TODO(bugs.webrtc.org/11547): Inject the network thread as well.
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100246 rtc::scoped_refptr<DataChannelInterface> proxy_channel =
247 DataChannelProxy::Create(signaling_thread(), channel);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100248 pc_->Observer()->OnDataChannel(std::move(proxy_channel));
249 pc_->NoteDataAddedEvent();
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100250}
251
252rtc::scoped_refptr<DataChannel>
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100253DataChannelController::InternalCreateDataChannel(
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100254 const std::string& label,
255 const InternalDataChannelInit* config) {
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100256 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100257 if (pc_->IsClosed()) {
258 return nullptr;
259 }
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100260 if (data_channel_type_ == cricket::DCT_NONE) {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100261 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 Alvestrand05e4d082019-12-03 14:04:21 +0100267 if (DataChannel::IsSctpLike(data_channel_type_)) {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100268 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 Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200284 DataChannel::Create(this, data_channel_type(), label, new_config,
285 signaling_thread(), network_thread()));
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100286 if (!channel) {
287 sid_allocator_.ReleaseSid(new_config.id);
288 return nullptr;
289 }
290
291 if (channel->data_channel_type() == cricket::DCT_RTP) {
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100292 if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100293 RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label()
294 << " already exists.";
295 return nullptr;
296 }
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100297 rtp_data_channels_[channel->label()] = channel;
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100298 } else {
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100299 RTC_DCHECK(DataChannel::IsSctpLike(data_channel_type_));
300 sctp_data_channels_.push_back(channel);
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100301 channel->SignalClosed.connect(pc_,
302 &PeerConnection::OnSctpDataChannelClosed);
303 }
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100304 SignalDataChannelCreated_(channel.get());
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100305 return channel;
306}
307
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100308void DataChannelController::AllocateSctpSids(rtc::SSLRole role) {
309 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100310 std::vector<rtc::scoped_refptr<DataChannel>> channels_to_close;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100311 for (const auto& channel : sctp_data_channels_) {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100312 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 Alvestranddfbfb462019-12-08 05:55:43 +0100325 channel->CloseAbruptlyWithDataChannelFailure("Failed to allocate SCTP SID");
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100326 }
327}
328
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100329void 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 Alvestrand00cf34c2019-12-02 09:56:02 +0100333 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 Alvestrand05e4d082019-12-03 14:04:21 +0100341 sctp_data_channels_to_free_.push_back(*it);
342 sctp_data_channels_.erase(it);
Harald Alvestrand246724b2019-12-03 22:31:42 +0100343 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 Alvestrand00cf34c2019-12-02 09:56:02 +0100350 return;
351 }
352 }
353}
354
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100355void 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
372DataChannel* 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
382void 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
408void DataChannelController::UpdateRemoteRtpDataChannels(
409 const cricket::StreamParamsVec& streams) {
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200410 RTC_DCHECK_RUN_ON(signaling_thread());
411
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100412 std::vector<std::string> existing_channels;
413
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100414 // Find new and active data channels.
415 for (const cricket::StreamParams& params : streams) {
Artem Titovf0a34f22020-03-16 17:52:04 +0000416 // 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 Alvestrand05e4d082019-12-03 14:04:21 +0100418 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 Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200434cricket::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
440void DataChannelController::set_data_channel_type(
441 cricket::DataChannelType type) {
442 RTC_DCHECK_RUN_ON(signaling_thread());
443 data_channel_type_ = type;
444}
445
446DataChannelTransportInterface* 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
454void DataChannelController::set_data_channel_transport(
455 DataChannelTransportInterface* transport) {
456 RTC_DCHECK_RUN_ON(network_thread());
457 data_channel_transport_ = transport;
458}
459
460const std::map<std::string, rtc::scoped_refptr<DataChannel>>*
461DataChannelController::rtp_data_channels() const {
462 RTC_DCHECK_RUN_ON(signaling_thread());
463 return &rtp_data_channels_;
464}
465
466const std::vector<rtc::scoped_refptr<DataChannel>>*
467DataChannelController::sctp_data_channels() const {
468 RTC_DCHECK_RUN_ON(signaling_thread());
469 return &sctp_data_channels_;
470}
471
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100472void 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
498void 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 Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200508 // TODO(bugs.webrtc.org/11547): Inject the network thread as well.
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100509 rtc::scoped_refptr<DataChannelInterface> proxy_channel =
510 DataChannelProxy::Create(signaling_thread(), channel);
511 pc_->Observer()->OnDataChannel(std::move(proxy_channel));
512}
513
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200514bool 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 Alvestrand05e4d082019-12-03 14:04:21 +0100552rtc::Thread* DataChannelController::network_thread() const {
553 return pc_->network_thread();
554}
555rtc::Thread* DataChannelController::signaling_thread() const {
556 return pc_->signaling_thread();
557}
558
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100559} // namespace webrtc