blob: 586520b6e0be136c703107fd1c41fbb97ffe8b90 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +000010
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/data_channel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
kwibergd1fe2812016-04-27 06:47:29 -070013#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <string>
Steve Anton944c7552018-12-13 14:19:10 -080015#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
Steve Anton944c7552018-12-13 14:19:10 -080017#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "media/sctp/sctp_transport_internal.h"
19#include "pc/sctp_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/location.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/ref_counted_object.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
26namespace webrtc {
27
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000028static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024;
29static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030
Harald Alvestrand928e7a32019-07-31 07:16:45 -040031namespace {
32
33static std::atomic<int> g_unique_id{0};
34
35int GenerateUniqueId() {
36 return ++g_unique_id;
37}
38
39} // namespace
40
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020041InternalDataChannelInit::InternalDataChannelInit(const DataChannelInit& base)
42 : DataChannelInit(base), open_handshake_role(kOpener) {
43 // If the channel is externally negotiated, do not send the OPEN message.
44 if (base.negotiated) {
45 open_handshake_role = kNone;
46 } else {
47 // Datachannel is externally negotiated. Ignore the id value.
48 // Specified in createDataChannel, WebRTC spec section 6.1 bullet 13.
49 id = -1;
50 }
51 // Backwards compatibility: If base.maxRetransmits or base.maxRetransmitTime
52 // have been set to -1, unset them.
53 if (maxRetransmits && *maxRetransmits == -1) {
54 RTC_LOG(LS_ERROR)
55 << "Accepting maxRetransmits = -1 for backwards compatibility";
56 maxRetransmits = absl::nullopt;
57 }
58 if (maxRetransmitTime && *maxRetransmitTime == -1) {
59 RTC_LOG(LS_ERROR)
60 << "Accepting maxRetransmitTime = -1 for backwards compatibility";
61 maxRetransmitTime = absl::nullopt;
62 }
63}
64
deadbeefab9b2d12015-10-14 11:33:11 -070065bool SctpSidAllocator::AllocateSid(rtc::SSLRole role, int* sid) {
66 int potential_sid = (role == rtc::SSL_CLIENT) ? 0 : 1;
67 while (!IsSidAvailable(potential_sid)) {
68 potential_sid += 2;
69 if (potential_sid > static_cast<int>(cricket::kMaxSctpSid)) {
70 return false;
71 }
72 }
73
74 *sid = potential_sid;
75 used_sids_.insert(potential_sid);
76 return true;
77}
78
79bool SctpSidAllocator::ReserveSid(int sid) {
80 if (!IsSidAvailable(sid)) {
81 return false;
82 }
83 used_sids_.insert(sid);
84 return true;
85}
86
87void SctpSidAllocator::ReleaseSid(int sid) {
88 auto it = used_sids_.find(sid);
89 if (it != used_sids_.end()) {
90 used_sids_.erase(it);
91 }
92}
93
94bool SctpSidAllocator::IsSidAvailable(int sid) const {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -070095 if (sid < static_cast<int>(cricket::kMinSctpSid) ||
96 sid > static_cast<int>(cricket::kMaxSctpSid)) {
deadbeefab9b2d12015-10-14 11:33:11 -070097 return false;
98 }
99 return used_sids_.find(sid) == used_sids_.end();
100}
101
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000102bool DataChannel::PacketQueue::Empty() const {
103 return packets_.empty();
104}
105
Steve Anton944c7552018-12-13 14:19:10 -0800106std::unique_ptr<DataBuffer> DataChannel::PacketQueue::PopFront() {
107 RTC_DCHECK(!packets_.empty());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000108 byte_count_ -= packets_.front()->size();
Steve Anton944c7552018-12-13 14:19:10 -0800109 std::unique_ptr<DataBuffer> packet = std::move(packets_.front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000110 packets_.pop_front();
Steve Anton944c7552018-12-13 14:19:10 -0800111 return packet;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000112}
113
Steve Anton944c7552018-12-13 14:19:10 -0800114void DataChannel::PacketQueue::PushFront(std::unique_ptr<DataBuffer> packet) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000115 byte_count_ += packet->size();
Steve Anton944c7552018-12-13 14:19:10 -0800116 packets_.push_front(std::move(packet));
117}
118
119void DataChannel::PacketQueue::PushBack(std::unique_ptr<DataBuffer> packet) {
120 byte_count_ += packet->size();
121 packets_.push_back(std::move(packet));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000122}
123
124void DataChannel::PacketQueue::Clear() {
Steve Anton944c7552018-12-13 14:19:10 -0800125 packets_.clear();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000126 byte_count_ = 0;
127}
128
129void DataChannel::PacketQueue::Swap(PacketQueue* other) {
130 size_t other_byte_count = other->byte_count_;
131 other->byte_count_ = byte_count_;
132 byte_count_ = other_byte_count;
133
134 other->packets_.swap(packets_);
135}
136
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000137rtc::scoped_refptr<DataChannel> DataChannel::Create(
wu@webrtc.org78187522013-10-07 23:32:02 +0000138 DataChannelProviderInterface* provider,
139 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000141 const InternalDataChannelInit& config) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000142 rtc::scoped_refptr<DataChannel> channel(
143 new rtc::RefCountedObject<DataChannel>(provider, dct, label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 if (!channel->Init(config)) {
145 return NULL;
146 }
147 return channel;
148}
149
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800150bool DataChannel::IsSctpLike(cricket::DataChannelType type) {
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700151 return type == cricket::DCT_SCTP || type == cricket::DCT_MEDIA_TRANSPORT ||
152 type == cricket::DCT_DATA_CHANNEL_TRANSPORT ||
153 type == cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800154}
155
Yves Gerey665174f2018-06-19 15:03:05 +0200156DataChannel::DataChannel(DataChannelProviderInterface* provider,
157 cricket::DataChannelType dct,
158 const std::string& label)
Harald Alvestrand928e7a32019-07-31 07:16:45 -0400159 : internal_id_(GenerateUniqueId()),
160 label_(label),
hbos84ffdee2016-10-12 14:14:39 -0700161 observer_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 state_(kConnecting),
hbos84ffdee2016-10-12 14:14:39 -0700163 messages_sent_(0),
164 bytes_sent_(0),
165 messages_received_(0),
166 bytes_received_(0),
Marina Cioceae448a3f2019-03-04 15:52:21 +0100167 buffered_amount_(0),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000168 data_channel_type_(dct),
169 provider_(provider),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400170 handshake_state_(kHandshakeInit),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000171 connected_to_provider_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 send_ssrc_set_(false),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000173 receive_ssrc_set_(false),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400174 writable_(false),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000175 send_ssrc_(0),
Yves Gerey665174f2018-06-19 15:03:05 +0200176 receive_ssrc_(0) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000178bool DataChannel::Init(const InternalDataChannelInit& config) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400179 if (data_channel_type_ == cricket::DCT_RTP) {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200180 if (config.reliable || config.id != -1 || config.maxRetransmits ||
181 config.maxRetransmitTime) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100182 RTC_LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100183 "invalid DataChannelInit.";
Lally Singh5c6c6e02015-05-29 11:52:39 -0400184 return false;
185 }
186 handshake_state_ = kHandshakeReady;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800187 } else if (IsSctpLike(data_channel_type_)) {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200188 if (config.id < -1 ||
189 (config.maxRetransmits && *config.maxRetransmits < 0) ||
190 (config.maxRetransmitTime && *config.maxRetransmitTime < 0)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100191 RTC_LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100192 "invalid DataChannelInit.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 return false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000194 }
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200195 if (config.maxRetransmits && config.maxRetransmitTime) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100196 RTC_LOG(LS_ERROR)
197 << "maxRetransmits and maxRetransmitTime should not be both set.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000198 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000200 config_ = config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201
Lally Singh5c6c6e02015-05-29 11:52:39 -0400202 switch (config_.open_handshake_role) {
Yves Gerey665174f2018-06-19 15:03:05 +0200203 case webrtc::InternalDataChannelInit::kNone: // pre-negotiated
204 handshake_state_ = kHandshakeReady;
205 break;
206 case webrtc::InternalDataChannelInit::kOpener:
207 handshake_state_ = kHandshakeShouldSendOpen;
208 break;
209 case webrtc::InternalDataChannelInit::kAcker:
210 handshake_state_ = kHandshakeShouldSendAck;
211 break;
Steve Anton36b29d12017-10-30 09:57:42 -0700212 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400213
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000214 // Try to connect to the transport in case the transport channel already
215 // exists.
216 OnTransportChannelCreated();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000217
218 // Checks if the transport is ready to send because the initial channel
219 // ready signal may have been sent before the DataChannel creation.
220 // This has to be done async because the upper layer objects (e.g.
221 // Chrome glue and WebKit) are not wired up properly until after this
222 // function returns.
223 if (provider_->ReadyToSendData()) {
Steve Anton044a04d2018-08-31 13:51:19 -0700224 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, rtc::Thread::Current(),
225 [this] { OnChannelReady(true); });
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000226 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000227 }
228
229 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230}
231
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000232DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233
234void DataChannel::RegisterObserver(DataChannelObserver* observer) {
235 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000236 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237}
238
239void DataChannel::UnregisterObserver() {
240 observer_ = NULL;
241}
242
243bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02 +0000244 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245 return false;
246 } else {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200247 return !config_.maxRetransmits && !config_.maxRetransmitTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 }
249}
250
Peter Boström0c4e06b2015-10-07 12:23:21 +0200251uint64_t DataChannel::buffered_amount() const {
Marina Cioceae448a3f2019-03-04 15:52:21 +0100252 return buffered_amount_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253}
254
255void DataChannel::Close() {
256 if (state_ == kClosed)
257 return;
258 send_ssrc_ = 0;
259 send_ssrc_set_ = false;
260 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700261 // Will send queued data before beginning the underlying closing procedure.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 UpdateState();
263}
264
265bool DataChannel::Send(const DataBuffer& buffer) {
Marina Cioceae448a3f2019-03-04 15:52:21 +0100266 buffered_amount_ += buffer.size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 if (state_ != kOpen) {
268 return false;
269 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000270
271 // TODO(jiayl): the spec is unclear about if the remote side should get the
272 // onmessage event. We need to figure out the expected behavior and change the
273 // code accordingly.
274 if (buffer.size() == 0) {
275 return true;
276 }
277
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000278 // If the queue is non-empty, we're waiting for SignalReadyToSend,
279 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000280 if (!queued_send_data_.Empty()) {
281 // Only SCTP DataChannel queues the outgoing data when the transport is
282 // blocked.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800283 RTC_DCHECK(IsSctpLike(data_channel_type_));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000284 if (!QueueSendDataMessage(buffer)) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700285 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to queue "
286 "additional data.";
287 CloseAbruptly();
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000288 }
289 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000292 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000293 if (data_channel_type_ == cricket::DCT_RTP) {
294 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000295 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000296
297 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000298 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299}
300
Peter Boström0c4e06b2015-10-07 12:23:21 +0200301void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800302 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000303
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 return;
306 }
307 receive_ssrc_ = receive_ssrc;
308 receive_ssrc_set_ = true;
309 UpdateState();
310}
311
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000312void DataChannel::SetSctpSid(int sid) {
kwibergee89e782017-08-09 17:22:01 -0700313 RTC_DCHECK_LT(config_.id, 0);
314 RTC_DCHECK_GE(sid, 0);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800315 RTC_DCHECK(IsSctpLike(data_channel_type_));
deadbeefab9b2d12015-10-14 11:33:11 -0700316 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000317 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700318 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000319
320 config_.id = sid;
321 provider_->AddSctpDataStream(sid);
322}
323
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700324void DataChannel::OnClosingProcedureStartedRemotely(int sid) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800325 if (IsSctpLike(data_channel_type_) && sid == config_.id &&
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700326 state_ != kClosing && state_ != kClosed) {
327 // Don't bother sending queued data since the side that initiated the
328 // closure wouldn't receive it anyway. See crbug.com/559394 for a lengthy
329 // discussion about this.
330 queued_send_data_.Clear();
331 queued_control_data_.Clear();
332 // Just need to change state to kClosing, SctpTransport will handle the
333 // rest of the closing procedure and OnClosingProcedureComplete will be
334 // called later.
335 started_closing_procedure_ = true;
336 SetState(kClosing);
337 }
338}
339
340void DataChannel::OnClosingProcedureComplete(int sid) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800341 if (IsSctpLike(data_channel_type_) && sid == config_.id) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700342 // If the closing procedure is complete, we should have finished sending
343 // all pending data and transitioned to kClosing already.
344 RTC_DCHECK_EQ(state_, kClosing);
345 RTC_DCHECK(queued_send_data_.Empty());
346 DisconnectFromProvider();
347 SetState(kClosed);
348 }
349}
350
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000351void DataChannel::OnTransportChannelCreated() {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800352 RTC_DCHECK(IsSctpLike(data_channel_type_));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000353 if (!connected_to_provider_) {
354 connected_to_provider_ = provider_->ConnectDataChannel(this);
355 }
356 // The sid may have been unassigned when provider_->ConnectDataChannel was
357 // done. So always add the streams even if connected_to_provider_ is true.
358 if (config_.id >= 0) {
359 provider_->AddSctpDataStream(config_.id);
360 }
361}
362
deadbeefab9b2d12015-10-14 11:33:11 -0700363void DataChannel::OnTransportChannelDestroyed() {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700364 // The SctpTransport is going away (for example, because the SCTP m= section
365 // was rejected), so we need to close abruptly.
366 CloseAbruptly();
367}
368
369// The remote peer request that this channel shall be closed.
370void DataChannel::RemotePeerRequestClose() {
371 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
372 CloseAbruptly();
deadbeefab9b2d12015-10-14 11:33:11 -0700373}
374
Peter Boström0c4e06b2015-10-07 12:23:21 +0200375void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800376 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378 return;
379 }
380 send_ssrc_ = send_ssrc;
381 send_ssrc_set_ = true;
382 UpdateState();
383}
384
deadbeef953c2ce2017-01-09 14:53:41 -0800385void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700386 const rtc::CopyOnWriteBuffer& payload) {
deadbeef953c2ce2017-01-09 14:53:41 -0800387 if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) {
388 return;
389 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800390 if (IsSctpLike(data_channel_type_) && params.sid != config_.id) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000391 return;
392 }
393
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000394 if (params.type == cricket::DMT_CONTROL) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800395 RTC_DCHECK(IsSctpLike(data_channel_type_));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400396 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000397 // Ignore it if we are not expecting an ACK message.
Jonas Olsson45cc8902018-02-13 10:37:07 +0100398 RTC_LOG(LS_WARNING)
399 << "DataChannel received unexpected CONTROL message, sid = "
400 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000401 return;
402 }
403 if (ParseDataChannelOpenAckMessage(payload)) {
404 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400405 handshake_state_ = kHandshakeReady;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100406 RTC_LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
407 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000408 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100409 RTC_LOG(LS_WARNING)
410 << "DataChannel failed to parse OPEN_ACK message, sid = "
411 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000412 }
413 return;
414 }
415
nisseede5da42017-01-12 05:15:36 -0800416 RTC_DCHECK(params.type == cricket::DMT_BINARY ||
417 params.type == cricket::DMT_TEXT);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000418
Mirko Bonadei675513b2017-11-09 11:09:25 +0100419 RTC_LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = "
420 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000421 // We can send unordered as soon as we receive any DATA message since the
422 // remote side must have received the OPEN (and old clients do not send
423 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400424 if (handshake_state_ == kHandshakeWaitingForAck) {
425 handshake_state_ = kHandshakeReady;
426 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000427
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000428 bool binary = (params.type == cricket::DMT_BINARY);
Steve Anton944c7552018-12-13 14:19:10 -0800429 auto buffer = absl::make_unique<DataBuffer>(payload, binary);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400430 if (state_ == kOpen && observer_) {
hbos84ffdee2016-10-12 14:14:39 -0700431 ++messages_received_;
432 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000433 observer_->OnMessage(*buffer.get());
434 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000435 if (queued_received_data_.byte_count() + payload.size() >
436 kMaxQueuedReceivedDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100437 RTC_LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000438
439 queued_received_data_.Clear();
440 if (data_channel_type_ != cricket::DCT_RTP) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700441 CloseAbruptly();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000442 }
443
444 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000445 }
Steve Anton944c7552018-12-13 14:19:10 -0800446 queued_received_data_.PushBack(std::move(buffer));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000447 }
448}
449
450void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400451 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000452 if (!writable) {
453 return;
454 }
Harald Alvestrandb9bb3712019-03-27 07:23:31 +0000455
Lally Singh5c6c6e02015-05-29 11:52:39 -0400456 SendQueuedControlMessages();
457 SendQueuedDataMessages();
458 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000459}
460
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700461void DataChannel::CloseAbruptly() {
462 if (state_ == kClosed) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000463 return;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700464 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000465
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700466 if (connected_to_provider_) {
467 DisconnectFromProvider();
468 }
469
470 // Closing abruptly means any queued data gets thrown away.
471 queued_send_data_.Clear();
Marina Cioceae448a3f2019-03-04 15:52:21 +0100472 buffered_amount_ = 0;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700473 queued_control_data_.Clear();
474
475 // Still go to "kClosing" before "kClosed", since observers may be expecting
476 // that.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700478 SetState(kClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479}
480
481void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400482 // UpdateState determines what to do from a few state variables. Include
483 // all conditions required for each state transition here for
484 // clarity. OnChannelReady(true) will send any queued data and then invoke
485 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 switch (state_) {
487 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000488 if (send_ssrc_set_ == receive_ssrc_set_) {
489 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
490 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400492 if (connected_to_provider_) {
493 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 06:15:43 -0700494 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400495 WriteDataChannelOpenMessage(label_, config_, &payload);
496 SendControlMessage(payload);
497 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 06:15:43 -0700498 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400499 WriteDataChannelOpenAckMessage(&payload);
500 SendControlMessage(payload);
501 }
Yves Gerey665174f2018-06-19 15:03:05 +0200502 if (writable_ && (handshake_state_ == kHandshakeReady ||
503 handshake_state_ == kHandshakeWaitingForAck)) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400504 SetState(kOpen);
505 // If we have received buffers before the channel got writable.
506 // Deliver them now.
507 DeliverQueuedReceivedData();
508 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 }
510 }
511 break;
512 }
513 case kOpen: {
514 break;
515 }
516 case kClosing: {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700517 // Wait for all queued data to be sent before beginning the closing
518 // procedure.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400519 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700520 if (data_channel_type_ == cricket::DCT_RTP) {
521 // For RTP data channels, we can go to "closed" after we finish
522 // sending data and the send/recv SSRCs are unset.
523 if (connected_to_provider_) {
524 DisconnectFromProvider();
525 }
526 if (!send_ssrc_set_ && !receive_ssrc_set_) {
527 SetState(kClosed);
528 }
529 } else {
530 // For SCTP data channels, we need to wait for the closing procedure
531 // to complete; after calling RemoveSctpDataStream,
532 // OnClosingProcedureComplete will end up called asynchronously
533 // afterwards.
534 if (connected_to_provider_ && !started_closing_procedure_ &&
535 config_.id >= 0) {
536 started_closing_procedure_ = true;
537 provider_->RemoveSctpDataStream(config_.id);
538 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400539 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 }
541 break;
542 }
543 case kClosed:
544 break;
545 }
546}
547
548void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700549 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000550 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700551 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000552
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 state_ = state;
554 if (observer_) {
555 observer_->OnStateChange();
556 }
hbos82ebe022016-11-14 01:41:09 -0800557 if (state_ == kOpen) {
558 SignalOpened(this);
559 } else if (state_ == kClosed) {
deadbeefab9b2d12015-10-14 11:33:11 -0700560 SignalClosed(this);
561 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562}
563
Lally Singh5c6c6e02015-05-29 11:52:39 -0400564void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000565 if (!connected_to_provider_)
566 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567
wu@webrtc.org78187522013-10-07 23:32:02 +0000568 provider_->DisconnectDataChannel(this);
569 connected_to_provider_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570}
571
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000572void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400573 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000574 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000577 while (!queued_received_data_.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800578 std::unique_ptr<DataBuffer> buffer = queued_received_data_.PopFront();
hbos84ffdee2016-10-12 14:14:39 -0700579 ++messages_received_;
580 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000581 observer_->OnMessage(*buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 }
583}
584
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000585void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400586 if (queued_send_data_.Empty()) {
587 return;
588 }
589
nisseede5da42017-01-12 05:15:36 -0800590 RTC_DCHECK(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000591
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000592 while (!queued_send_data_.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800593 std::unique_ptr<DataBuffer> buffer = queued_send_data_.PopFront();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000594 if (!SendDataMessage(*buffer, false)) {
Steve Anton944c7552018-12-13 14:19:10 -0800595 // Return the message to the front of the queue if sending is aborted.
596 queued_send_data_.PushFront(std::move(buffer));
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000597 break;
598 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000599 }
600}
601
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000602bool DataChannel::SendDataMessage(const DataBuffer& buffer,
603 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000604 cricket::SendDataParams send_params;
605
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800606 if (IsSctpLike(data_channel_type_)) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000607 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400608 // Send as ordered if it is still going through OPEN/ACK signaling.
609 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000610 send_params.ordered = true;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100611 RTC_LOG(LS_VERBOSE)
612 << "Sending data as ordered for unordered DataChannel "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100613 "because the OPEN_ACK message has not been received.";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000614 }
615
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200616 send_params.max_rtx_count =
617 config_.maxRetransmits ? *config_.maxRetransmits : -1;
618 send_params.max_rtx_ms =
619 config_.maxRetransmitTime ? *config_.maxRetransmitTime : -1;
deadbeef953c2ce2017-01-09 14:53:41 -0800620 send_params.sid = config_.id;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000621 } else {
622 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000623 }
624 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
625
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000626 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
627 bool success = provider_->SendData(send_params, buffer.data, &send_result);
628
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000629 if (success) {
hbos84ffdee2016-10-12 14:14:39 -0700630 ++messages_sent_;
631 bytes_sent_ += buffer.size();
Marina Cioceae448a3f2019-03-04 15:52:21 +0100632
633 RTC_DCHECK(buffered_amount_ >= buffer.size());
634 buffered_amount_ -= buffer.size();
635 if (observer_ && buffer.size() > 0) {
636 observer_->OnBufferedAmountChange(buffer.size());
637 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000638 return true;
639 }
640
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800641 if (!IsSctpLike(data_channel_type_)) {
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000642 return false;
643 }
644
645 if (send_result == cricket::SDR_BLOCK) {
646 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
647 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000648 }
649 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000650 // Close the channel if the error is not SDR_BLOCK, or if queuing the
651 // message failed.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100652 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100653 "send_result = "
654 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700655 CloseAbruptly();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000656
657 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000658}
659
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000660bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
Marina Cioceae448a3f2019-03-04 15:52:21 +0100661 size_t start_buffered_amount = queued_send_data_.byte_count();
662 if (start_buffered_amount + buffer.size() > kMaxQueuedSendDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100663 RTC_LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000664 return false;
665 }
Steve Anton944c7552018-12-13 14:19:10 -0800666 queued_send_data_.PushBack(absl::make_unique<DataBuffer>(buffer));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000667 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668}
669
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000670void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000671 PacketQueue control_packets;
672 control_packets.Swap(&queued_control_data_);
673
674 while (!control_packets.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800675 std::unique_ptr<DataBuffer> buf = control_packets.PopFront();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000676 SendControlMessage(buf->data);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000677 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000678}
679
jbaucheec21bd2016-03-20 06:15:43 -0700680void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Steve Anton944c7552018-12-13 14:19:10 -0800681 queued_control_data_.PushBack(absl::make_unique<DataBuffer>(buffer, true));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000682}
683
jbaucheec21bd2016-03-20 06:15:43 -0700684bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400685 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000686
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800687 RTC_DCHECK(IsSctpLike(data_channel_type_));
kwibergee89e782017-08-09 17:22:01 -0700688 RTC_DCHECK(writable_);
689 RTC_DCHECK_GE(config_.id, 0);
690 RTC_DCHECK(!is_open_message || !config_.negotiated);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000691
692 cricket::SendDataParams send_params;
deadbeef953c2ce2017-01-09 14:53:41 -0800693 send_params.sid = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400694 // Send data as ordered before we receive any message from the remote peer to
695 // make sure the remote peer will not receive any data before it receives the
696 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000697 send_params.ordered = config_.ordered || is_open_message;
698 send_params.type = cricket::DMT_CONTROL;
699
700 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
701 bool retval = provider_->SendData(send_params, buffer, &send_result);
702 if (retval) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100703 RTC_LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000704
Lally Singh5c6c6e02015-05-29 11:52:39 -0400705 if (handshake_state_ == kHandshakeShouldSendAck) {
706 handshake_state_ = kHandshakeReady;
707 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
708 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000709 }
710 } else if (send_result == cricket::SDR_BLOCK) {
711 QueueControlMessage(buffer);
712 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100713 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
Jonas Olsson45cc8902018-02-13 10:37:07 +0100714 " the CONTROL message, send_result = "
715 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700716 CloseAbruptly();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000717 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000718 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000719}
720
Harald Alvestrand928e7a32019-07-31 07:16:45 -0400721// static
722void DataChannel::ResetInternalIdAllocatorForTesting(int new_value) {
723 g_unique_id = new_value;
724}
725
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726} // namespace webrtc