blob: e989586607d77bf3a41f9283d524d3ad3f73b0e1 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/datachannel.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>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "media/sctp/sctptransportinternal.h"
17#include "pc/sctputils.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
20#include "rtc_base/refcount.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace webrtc {
23
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000024static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024;
25static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026
deadbeefab9b2d12015-10-14 11:33:11 -070027bool SctpSidAllocator::AllocateSid(rtc::SSLRole role, int* sid) {
28 int potential_sid = (role == rtc::SSL_CLIENT) ? 0 : 1;
29 while (!IsSidAvailable(potential_sid)) {
30 potential_sid += 2;
31 if (potential_sid > static_cast<int>(cricket::kMaxSctpSid)) {
32 return false;
33 }
34 }
35
36 *sid = potential_sid;
37 used_sids_.insert(potential_sid);
38 return true;
39}
40
41bool SctpSidAllocator::ReserveSid(int sid) {
42 if (!IsSidAvailable(sid)) {
43 return false;
44 }
45 used_sids_.insert(sid);
46 return true;
47}
48
49void SctpSidAllocator::ReleaseSid(int sid) {
50 auto it = used_sids_.find(sid);
51 if (it != used_sids_.end()) {
52 used_sids_.erase(it);
53 }
54}
55
56bool SctpSidAllocator::IsSidAvailable(int sid) const {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -070057 if (sid < static_cast<int>(cricket::kMinSctpSid) ||
58 sid > static_cast<int>(cricket::kMaxSctpSid)) {
deadbeefab9b2d12015-10-14 11:33:11 -070059 return false;
60 }
61 return used_sids_.find(sid) == used_sids_.end();
62}
63
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000064DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {}
65
66DataChannel::PacketQueue::~PacketQueue() {
67 Clear();
68}
69
70bool DataChannel::PacketQueue::Empty() const {
71 return packets_.empty();
72}
73
74DataBuffer* DataChannel::PacketQueue::Front() {
75 return packets_.front();
76}
77
78void DataChannel::PacketQueue::Pop() {
79 if (packets_.empty()) {
80 return;
81 }
82
83 byte_count_ -= packets_.front()->size();
84 packets_.pop_front();
85}
86
87void DataChannel::PacketQueue::Push(DataBuffer* packet) {
88 byte_count_ += packet->size();
89 packets_.push_back(packet);
90}
91
92void DataChannel::PacketQueue::Clear() {
93 while (!packets_.empty()) {
94 delete packets_.front();
95 packets_.pop_front();
96 }
97 byte_count_ = 0;
98}
99
100void DataChannel::PacketQueue::Swap(PacketQueue* other) {
101 size_t other_byte_count = other->byte_count_;
102 other->byte_count_ = byte_count_;
103 byte_count_ = other_byte_count;
104
105 other->packets_.swap(packets_);
106}
107
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000108rtc::scoped_refptr<DataChannel> DataChannel::Create(
wu@webrtc.org78187522013-10-07 23:32:02 +0000109 DataChannelProviderInterface* provider,
110 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000112 const InternalDataChannelInit& config) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000113 rtc::scoped_refptr<DataChannel> channel(
114 new rtc::RefCountedObject<DataChannel>(provider, dct, label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 if (!channel->Init(config)) {
116 return NULL;
117 }
118 return channel;
119}
120
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800121bool DataChannel::IsSctpLike(cricket::DataChannelType type) {
122 return type == cricket::DCT_SCTP || type == cricket::DCT_MEDIA_TRANSPORT;
123}
124
Yves Gerey665174f2018-06-19 15:03:05 +0200125DataChannel::DataChannel(DataChannelProviderInterface* provider,
126 cricket::DataChannelType dct,
127 const std::string& label)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 : label_(label),
hbos84ffdee2016-10-12 14:14:39 -0700129 observer_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 state_(kConnecting),
hbos84ffdee2016-10-12 14:14:39 -0700131 messages_sent_(0),
132 bytes_sent_(0),
133 messages_received_(0),
134 bytes_received_(0),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000135 data_channel_type_(dct),
136 provider_(provider),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400137 handshake_state_(kHandshakeInit),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000138 connected_to_provider_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 send_ssrc_set_(false),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000140 receive_ssrc_set_(false),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400141 writable_(false),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000142 send_ssrc_(0),
Yves Gerey665174f2018-06-19 15:03:05 +0200143 receive_ssrc_(0) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000145bool DataChannel::Init(const InternalDataChannelInit& config) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400146 if (data_channel_type_ == cricket::DCT_RTP) {
Yves Gerey665174f2018-06-19 15:03:05 +0200147 if (config.reliable || config.id != -1 || config.maxRetransmits != -1 ||
Lally Singh5c6c6e02015-05-29 11:52:39 -0400148 config.maxRetransmitTime != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100149 RTC_LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100150 "invalid DataChannelInit.";
Lally Singh5c6c6e02015-05-29 11:52:39 -0400151 return false;
152 }
153 handshake_state_ = kHandshakeReady;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800154 } else if (IsSctpLike(data_channel_type_)) {
Yves Gerey665174f2018-06-19 15:03:05 +0200155 if (config.id < -1 || config.maxRetransmits < -1 ||
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000156 config.maxRetransmitTime < -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100157 RTC_LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100158 "invalid DataChannelInit.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 return false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000160 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000161 if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100162 RTC_LOG(LS_ERROR)
163 << "maxRetransmits and maxRetransmitTime should not be both set.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000164 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000166 config_ = config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167
Lally Singh5c6c6e02015-05-29 11:52:39 -0400168 switch (config_.open_handshake_role) {
Yves Gerey665174f2018-06-19 15:03:05 +0200169 case webrtc::InternalDataChannelInit::kNone: // pre-negotiated
170 handshake_state_ = kHandshakeReady;
171 break;
172 case webrtc::InternalDataChannelInit::kOpener:
173 handshake_state_ = kHandshakeShouldSendOpen;
174 break;
175 case webrtc::InternalDataChannelInit::kAcker:
176 handshake_state_ = kHandshakeShouldSendAck;
177 break;
Steve Anton36b29d12017-10-30 09:57:42 -0700178 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400179
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000180 // Try to connect to the transport in case the transport channel already
181 // exists.
182 OnTransportChannelCreated();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000183
184 // Checks if the transport is ready to send because the initial channel
185 // ready signal may have been sent before the DataChannel creation.
186 // This has to be done async because the upper layer objects (e.g.
187 // Chrome glue and WebKit) are not wired up properly until after this
188 // function returns.
189 if (provider_->ReadyToSendData()) {
Steve Anton044a04d2018-08-31 13:51:19 -0700190 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, rtc::Thread::Current(),
191 [this] { OnChannelReady(true); });
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000192 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000193 }
194
195 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196}
197
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000198DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199
200void DataChannel::RegisterObserver(DataChannelObserver* observer) {
201 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000202 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203}
204
205void DataChannel::UnregisterObserver() {
206 observer_ = NULL;
207}
208
209bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02 +0000210 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 return false;
212 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200213 return config_.maxRetransmits == -1 && config_.maxRetransmitTime == -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 }
215}
216
Peter Boström0c4e06b2015-10-07 12:23:21 +0200217uint64_t DataChannel::buffered_amount() const {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000218 return queued_send_data_.byte_count();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219}
220
221void DataChannel::Close() {
222 if (state_ == kClosed)
223 return;
224 send_ssrc_ = 0;
225 send_ssrc_set_ = false;
226 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700227 // Will send queued data before beginning the underlying closing procedure.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 UpdateState();
229}
230
231bool DataChannel::Send(const DataBuffer& buffer) {
232 if (state_ != kOpen) {
233 return false;
234 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000235
236 // TODO(jiayl): the spec is unclear about if the remote side should get the
237 // onmessage event. We need to figure out the expected behavior and change the
238 // code accordingly.
239 if (buffer.size() == 0) {
240 return true;
241 }
242
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000243 // If the queue is non-empty, we're waiting for SignalReadyToSend,
244 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000245 if (!queued_send_data_.Empty()) {
246 // Only SCTP DataChannel queues the outgoing data when the transport is
247 // blocked.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800248 RTC_DCHECK(IsSctpLike(data_channel_type_));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000249 if (!QueueSendDataMessage(buffer)) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700250 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to queue "
251 "additional data.";
252 CloseAbruptly();
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000253 }
254 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000257 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000258 if (data_channel_type_ == cricket::DCT_RTP) {
259 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000260 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000261
262 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000263 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264}
265
Peter Boström0c4e06b2015-10-07 12:23:21 +0200266void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800267 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000268
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 return;
271 }
272 receive_ssrc_ = receive_ssrc;
273 receive_ssrc_set_ = true;
274 UpdateState();
275}
276
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000277void DataChannel::SetSctpSid(int sid) {
kwibergee89e782017-08-09 17:22:01 -0700278 RTC_DCHECK_LT(config_.id, 0);
279 RTC_DCHECK_GE(sid, 0);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800280 RTC_DCHECK(IsSctpLike(data_channel_type_));
deadbeefab9b2d12015-10-14 11:33:11 -0700281 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000282 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700283 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000284
285 config_.id = sid;
286 provider_->AddSctpDataStream(sid);
287}
288
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700289void DataChannel::OnClosingProcedureStartedRemotely(int sid) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800290 if (IsSctpLike(data_channel_type_) && sid == config_.id &&
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700291 state_ != kClosing && state_ != kClosed) {
292 // Don't bother sending queued data since the side that initiated the
293 // closure wouldn't receive it anyway. See crbug.com/559394 for a lengthy
294 // discussion about this.
295 queued_send_data_.Clear();
296 queued_control_data_.Clear();
297 // Just need to change state to kClosing, SctpTransport will handle the
298 // rest of the closing procedure and OnClosingProcedureComplete will be
299 // called later.
300 started_closing_procedure_ = true;
301 SetState(kClosing);
302 }
303}
304
305void DataChannel::OnClosingProcedureComplete(int sid) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800306 if (IsSctpLike(data_channel_type_) && sid == config_.id) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700307 // If the closing procedure is complete, we should have finished sending
308 // all pending data and transitioned to kClosing already.
309 RTC_DCHECK_EQ(state_, kClosing);
310 RTC_DCHECK(queued_send_data_.Empty());
311 DisconnectFromProvider();
312 SetState(kClosed);
313 }
314}
315
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000316void DataChannel::OnTransportChannelCreated() {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800317 RTC_DCHECK(IsSctpLike(data_channel_type_));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000318 if (!connected_to_provider_) {
319 connected_to_provider_ = provider_->ConnectDataChannel(this);
320 }
321 // The sid may have been unassigned when provider_->ConnectDataChannel was
322 // done. So always add the streams even if connected_to_provider_ is true.
323 if (config_.id >= 0) {
324 provider_->AddSctpDataStream(config_.id);
325 }
326}
327
deadbeefab9b2d12015-10-14 11:33:11 -0700328void DataChannel::OnTransportChannelDestroyed() {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700329 // The SctpTransport is going away (for example, because the SCTP m= section
330 // was rejected), so we need to close abruptly.
331 CloseAbruptly();
332}
333
334// The remote peer request that this channel shall be closed.
335void DataChannel::RemotePeerRequestClose() {
336 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
337 CloseAbruptly();
deadbeefab9b2d12015-10-14 11:33:11 -0700338}
339
Peter Boström0c4e06b2015-10-07 12:23:21 +0200340void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800341 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 return;
344 }
345 send_ssrc_ = send_ssrc;
346 send_ssrc_set_ = true;
347 UpdateState();
348}
349
deadbeef953c2ce2017-01-09 14:53:41 -0800350void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700351 const rtc::CopyOnWriteBuffer& payload) {
deadbeef953c2ce2017-01-09 14:53:41 -0800352 if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) {
353 return;
354 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800355 if (IsSctpLike(data_channel_type_) && params.sid != config_.id) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000356 return;
357 }
358
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000359 if (params.type == cricket::DMT_CONTROL) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800360 RTC_DCHECK(IsSctpLike(data_channel_type_));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400361 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000362 // Ignore it if we are not expecting an ACK message.
Jonas Olsson45cc8902018-02-13 10:37:07 +0100363 RTC_LOG(LS_WARNING)
364 << "DataChannel received unexpected CONTROL message, sid = "
365 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000366 return;
367 }
368 if (ParseDataChannelOpenAckMessage(payload)) {
369 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400370 handshake_state_ = kHandshakeReady;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100371 RTC_LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
372 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000373 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100374 RTC_LOG(LS_WARNING)
375 << "DataChannel failed to parse OPEN_ACK message, sid = "
376 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000377 }
378 return;
379 }
380
nisseede5da42017-01-12 05:15:36 -0800381 RTC_DCHECK(params.type == cricket::DMT_BINARY ||
382 params.type == cricket::DMT_TEXT);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000383
Mirko Bonadei675513b2017-11-09 11:09:25 +0100384 RTC_LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = "
385 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000386 // We can send unordered as soon as we receive any DATA message since the
387 // remote side must have received the OPEN (and old clients do not send
388 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400389 if (handshake_state_ == kHandshakeWaitingForAck) {
390 handshake_state_ = kHandshakeReady;
391 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000392
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000393 bool binary = (params.type == cricket::DMT_BINARY);
kwibergd1fe2812016-04-27 06:47:29 -0700394 std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400395 if (state_ == kOpen && observer_) {
hbos84ffdee2016-10-12 14:14:39 -0700396 ++messages_received_;
397 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000398 observer_->OnMessage(*buffer.get());
399 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000400 if (queued_received_data_.byte_count() + payload.size() >
401 kMaxQueuedReceivedDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100402 RTC_LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000403
404 queued_received_data_.Clear();
405 if (data_channel_type_ != cricket::DCT_RTP) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700406 CloseAbruptly();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000407 }
408
409 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000410 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000411 queued_received_data_.Push(buffer.release());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000412 }
413}
414
415void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400416 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000417 if (!writable) {
418 return;
419 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000420
Lally Singh5c6c6e02015-05-29 11:52:39 -0400421 SendQueuedControlMessages();
422 SendQueuedDataMessages();
423 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000424}
425
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700426void DataChannel::CloseAbruptly() {
427 if (state_ == kClosed) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000428 return;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700429 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000430
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700431 if (connected_to_provider_) {
432 DisconnectFromProvider();
433 }
434
435 // Closing abruptly means any queued data gets thrown away.
436 queued_send_data_.Clear();
437 queued_control_data_.Clear();
438
439 // Still go to "kClosing" before "kClosed", since observers may be expecting
440 // that.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700442 SetState(kClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443}
444
445void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400446 // UpdateState determines what to do from a few state variables. Include
447 // all conditions required for each state transition here for
448 // clarity. OnChannelReady(true) will send any queued data and then invoke
449 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 switch (state_) {
451 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000452 if (send_ssrc_set_ == receive_ssrc_set_) {
453 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
454 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400456 if (connected_to_provider_) {
457 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 06:15:43 -0700458 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400459 WriteDataChannelOpenMessage(label_, config_, &payload);
460 SendControlMessage(payload);
461 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 06:15:43 -0700462 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400463 WriteDataChannelOpenAckMessage(&payload);
464 SendControlMessage(payload);
465 }
Yves Gerey665174f2018-06-19 15:03:05 +0200466 if (writable_ && (handshake_state_ == kHandshakeReady ||
467 handshake_state_ == kHandshakeWaitingForAck)) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400468 SetState(kOpen);
469 // If we have received buffers before the channel got writable.
470 // Deliver them now.
471 DeliverQueuedReceivedData();
472 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473 }
474 }
475 break;
476 }
477 case kOpen: {
478 break;
479 }
480 case kClosing: {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700481 // Wait for all queued data to be sent before beginning the closing
482 // procedure.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400483 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700484 if (data_channel_type_ == cricket::DCT_RTP) {
485 // For RTP data channels, we can go to "closed" after we finish
486 // sending data and the send/recv SSRCs are unset.
487 if (connected_to_provider_) {
488 DisconnectFromProvider();
489 }
490 if (!send_ssrc_set_ && !receive_ssrc_set_) {
491 SetState(kClosed);
492 }
493 } else {
494 // For SCTP data channels, we need to wait for the closing procedure
495 // to complete; after calling RemoveSctpDataStream,
496 // OnClosingProcedureComplete will end up called asynchronously
497 // afterwards.
498 if (connected_to_provider_ && !started_closing_procedure_ &&
499 config_.id >= 0) {
500 started_closing_procedure_ = true;
501 provider_->RemoveSctpDataStream(config_.id);
502 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400503 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 }
505 break;
506 }
507 case kClosed:
508 break;
509 }
510}
511
512void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700513 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000514 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700515 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000516
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 state_ = state;
518 if (observer_) {
519 observer_->OnStateChange();
520 }
hbos82ebe022016-11-14 01:41:09 -0800521 if (state_ == kOpen) {
522 SignalOpened(this);
523 } else if (state_ == kClosed) {
deadbeefab9b2d12015-10-14 11:33:11 -0700524 SignalClosed(this);
525 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526}
527
Lally Singh5c6c6e02015-05-29 11:52:39 -0400528void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000529 if (!connected_to_provider_)
530 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531
wu@webrtc.org78187522013-10-07 23:32:02 +0000532 provider_->DisconnectDataChannel(this);
533 connected_to_provider_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534}
535
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000536void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400537 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000538 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000541 while (!queued_received_data_.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700542 std::unique_ptr<DataBuffer> buffer(queued_received_data_.Front());
hbos84ffdee2016-10-12 14:14:39 -0700543 ++messages_received_;
544 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000545 observer_->OnMessage(*buffer);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000546 queued_received_data_.Pop();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 }
548}
549
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000550void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400551 if (queued_send_data_.Empty()) {
552 return;
553 }
554
nisseede5da42017-01-12 05:15:36 -0800555 RTC_DCHECK(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000556
Peter Boström0c4e06b2015-10-07 12:23:21 +0200557 uint64_t start_buffered_amount = buffered_amount();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000558 while (!queued_send_data_.Empty()) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000559 DataBuffer* buffer = queued_send_data_.Front();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000560 if (!SendDataMessage(*buffer, false)) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000561 // Leave the message in the queue if sending is aborted.
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000562 break;
563 }
564 queued_send_data_.Pop();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000565 delete buffer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000566 }
bemasc0edd50c2015-07-01 13:34:33 -0700567
568 if (observer_ && buffered_amount() < start_buffered_amount) {
569 observer_->OnBufferedAmountChange(start_buffered_amount);
570 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000571}
572
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000573bool DataChannel::SendDataMessage(const DataBuffer& buffer,
574 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000575 cricket::SendDataParams send_params;
576
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800577 if (IsSctpLike(data_channel_type_)) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000578 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400579 // Send as ordered if it is still going through OPEN/ACK signaling.
580 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000581 send_params.ordered = true;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100582 RTC_LOG(LS_VERBOSE)
583 << "Sending data as ordered for unordered DataChannel "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100584 "because the OPEN_ACK message has not been received.";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000585 }
586
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000587 send_params.max_rtx_count = config_.maxRetransmits;
588 send_params.max_rtx_ms = config_.maxRetransmitTime;
deadbeef953c2ce2017-01-09 14:53:41 -0800589 send_params.sid = config_.id;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000590 } else {
591 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000592 }
593 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
594
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000595 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
596 bool success = provider_->SendData(send_params, buffer.data, &send_result);
597
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000598 if (success) {
hbos84ffdee2016-10-12 14:14:39 -0700599 ++messages_sent_;
600 bytes_sent_ += buffer.size();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000601 return true;
602 }
603
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800604 if (!IsSctpLike(data_channel_type_)) {
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000605 return false;
606 }
607
608 if (send_result == cricket::SDR_BLOCK) {
609 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
610 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000611 }
612 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000613 // Close the channel if the error is not SDR_BLOCK, or if queuing the
614 // message failed.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100615 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100616 "send_result = "
617 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700618 CloseAbruptly();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000619
620 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000621}
622
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000623bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
bemasc0edd50c2015-07-01 13:34:33 -0700624 size_t start_buffered_amount = buffered_amount();
625 if (start_buffered_amount >= kMaxQueuedSendDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100626 RTC_LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000627 return false;
628 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000629 queued_send_data_.Push(new DataBuffer(buffer));
bemasc0edd50c2015-07-01 13:34:33 -0700630
631 // The buffer can have length zero, in which case there is no change.
632 if (observer_ && buffered_amount() > start_buffered_amount) {
633 observer_->OnBufferedAmountChange(start_buffered_amount);
634 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000635 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636}
637
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000638void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000639 PacketQueue control_packets;
640 control_packets.Swap(&queued_control_data_);
641
642 while (!control_packets.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700643 std::unique_ptr<DataBuffer> buf(control_packets.Front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000644 SendControlMessage(buf->data);
645 control_packets.Pop();
646 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000647}
648
jbaucheec21bd2016-03-20 06:15:43 -0700649void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000650 queued_control_data_.Push(new DataBuffer(buffer, true));
651}
652
jbaucheec21bd2016-03-20 06:15:43 -0700653bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400654 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000655
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800656 RTC_DCHECK(IsSctpLike(data_channel_type_));
kwibergee89e782017-08-09 17:22:01 -0700657 RTC_DCHECK(writable_);
658 RTC_DCHECK_GE(config_.id, 0);
659 RTC_DCHECK(!is_open_message || !config_.negotiated);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000660
661 cricket::SendDataParams send_params;
deadbeef953c2ce2017-01-09 14:53:41 -0800662 send_params.sid = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400663 // Send data as ordered before we receive any message from the remote peer to
664 // make sure the remote peer will not receive any data before it receives the
665 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000666 send_params.ordered = config_.ordered || is_open_message;
667 send_params.type = cricket::DMT_CONTROL;
668
669 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
670 bool retval = provider_->SendData(send_params, buffer, &send_result);
671 if (retval) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100672 RTC_LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000673
Lally Singh5c6c6e02015-05-29 11:52:39 -0400674 if (handshake_state_ == kHandshakeShouldSendAck) {
675 handshake_state_ = kHandshakeReady;
676 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
677 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000678 }
679 } else if (send_result == cricket::SDR_BLOCK) {
680 QueueControlMessage(buffer);
681 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100682 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
Jonas Olsson45cc8902018-02-13 10:37:07 +0100683 " the CONTROL message, send_result = "
684 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700685 CloseAbruptly();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000686 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000687 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000688}
689
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690} // namespace webrtc