blob: 19f0e6855a386cae0e96d012f02a79a897e14b68 [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"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "rtc_base/location.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/refcountedobject.h"
22#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023
24namespace webrtc {
25
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000026static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024;
27static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
deadbeefab9b2d12015-10-14 11:33:11 -070029bool SctpSidAllocator::AllocateSid(rtc::SSLRole role, int* sid) {
30 int potential_sid = (role == rtc::SSL_CLIENT) ? 0 : 1;
31 while (!IsSidAvailable(potential_sid)) {
32 potential_sid += 2;
33 if (potential_sid > static_cast<int>(cricket::kMaxSctpSid)) {
34 return false;
35 }
36 }
37
38 *sid = potential_sid;
39 used_sids_.insert(potential_sid);
40 return true;
41}
42
43bool SctpSidAllocator::ReserveSid(int sid) {
44 if (!IsSidAvailable(sid)) {
45 return false;
46 }
47 used_sids_.insert(sid);
48 return true;
49}
50
51void SctpSidAllocator::ReleaseSid(int sid) {
52 auto it = used_sids_.find(sid);
53 if (it != used_sids_.end()) {
54 used_sids_.erase(it);
55 }
56}
57
58bool SctpSidAllocator::IsSidAvailable(int sid) const {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -070059 if (sid < static_cast<int>(cricket::kMinSctpSid) ||
60 sid > static_cast<int>(cricket::kMaxSctpSid)) {
deadbeefab9b2d12015-10-14 11:33:11 -070061 return false;
62 }
63 return used_sids_.find(sid) == used_sids_.end();
64}
65
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000066DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {}
67
68DataChannel::PacketQueue::~PacketQueue() {
69 Clear();
70}
71
72bool DataChannel::PacketQueue::Empty() const {
73 return packets_.empty();
74}
75
76DataBuffer* DataChannel::PacketQueue::Front() {
77 return packets_.front();
78}
79
80void DataChannel::PacketQueue::Pop() {
81 if (packets_.empty()) {
82 return;
83 }
84
85 byte_count_ -= packets_.front()->size();
86 packets_.pop_front();
87}
88
89void DataChannel::PacketQueue::Push(DataBuffer* packet) {
90 byte_count_ += packet->size();
91 packets_.push_back(packet);
92}
93
94void DataChannel::PacketQueue::Clear() {
95 while (!packets_.empty()) {
96 delete packets_.front();
97 packets_.pop_front();
98 }
99 byte_count_ = 0;
100}
101
102void DataChannel::PacketQueue::Swap(PacketQueue* other) {
103 size_t other_byte_count = other->byte_count_;
104 other->byte_count_ = byte_count_;
105 byte_count_ = other_byte_count;
106
107 other->packets_.swap(packets_);
108}
109
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000110rtc::scoped_refptr<DataChannel> DataChannel::Create(
wu@webrtc.org78187522013-10-07 23:32:02 +0000111 DataChannelProviderInterface* provider,
112 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000114 const InternalDataChannelInit& config) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000115 rtc::scoped_refptr<DataChannel> channel(
116 new rtc::RefCountedObject<DataChannel>(provider, dct, label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 if (!channel->Init(config)) {
118 return NULL;
119 }
120 return channel;
121}
122
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800123bool DataChannel::IsSctpLike(cricket::DataChannelType type) {
124 return type == cricket::DCT_SCTP || type == cricket::DCT_MEDIA_TRANSPORT;
125}
126
Yves Gerey665174f2018-06-19 15:03:05 +0200127DataChannel::DataChannel(DataChannelProviderInterface* provider,
128 cricket::DataChannelType dct,
129 const std::string& label)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 : label_(label),
hbos84ffdee2016-10-12 14:14:39 -0700131 observer_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 state_(kConnecting),
hbos84ffdee2016-10-12 14:14:39 -0700133 messages_sent_(0),
134 bytes_sent_(0),
135 messages_received_(0),
136 bytes_received_(0),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000137 data_channel_type_(dct),
138 provider_(provider),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400139 handshake_state_(kHandshakeInit),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000140 connected_to_provider_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 send_ssrc_set_(false),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000142 receive_ssrc_set_(false),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400143 writable_(false),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000144 send_ssrc_(0),
Yves Gerey665174f2018-06-19 15:03:05 +0200145 receive_ssrc_(0) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000147bool DataChannel::Init(const InternalDataChannelInit& config) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400148 if (data_channel_type_ == cricket::DCT_RTP) {
Yves Gerey665174f2018-06-19 15:03:05 +0200149 if (config.reliable || config.id != -1 || config.maxRetransmits != -1 ||
Lally Singh5c6c6e02015-05-29 11:52:39 -0400150 config.maxRetransmitTime != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100151 RTC_LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100152 "invalid DataChannelInit.";
Lally Singh5c6c6e02015-05-29 11:52:39 -0400153 return false;
154 }
155 handshake_state_ = kHandshakeReady;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800156 } else if (IsSctpLike(data_channel_type_)) {
Yves Gerey665174f2018-06-19 15:03:05 +0200157 if (config.id < -1 || config.maxRetransmits < -1 ||
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000158 config.maxRetransmitTime < -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100159 RTC_LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100160 "invalid DataChannelInit.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 return false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000162 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000163 if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100164 RTC_LOG(LS_ERROR)
165 << "maxRetransmits and maxRetransmitTime should not be both set.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000166 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000168 config_ = config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169
Lally Singh5c6c6e02015-05-29 11:52:39 -0400170 switch (config_.open_handshake_role) {
Yves Gerey665174f2018-06-19 15:03:05 +0200171 case webrtc::InternalDataChannelInit::kNone: // pre-negotiated
172 handshake_state_ = kHandshakeReady;
173 break;
174 case webrtc::InternalDataChannelInit::kOpener:
175 handshake_state_ = kHandshakeShouldSendOpen;
176 break;
177 case webrtc::InternalDataChannelInit::kAcker:
178 handshake_state_ = kHandshakeShouldSendAck;
179 break;
Steve Anton36b29d12017-10-30 09:57:42 -0700180 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400181
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000182 // Try to connect to the transport in case the transport channel already
183 // exists.
184 OnTransportChannelCreated();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000185
186 // Checks if the transport is ready to send because the initial channel
187 // ready signal may have been sent before the DataChannel creation.
188 // This has to be done async because the upper layer objects (e.g.
189 // Chrome glue and WebKit) are not wired up properly until after this
190 // function returns.
191 if (provider_->ReadyToSendData()) {
Steve Anton044a04d2018-08-31 13:51:19 -0700192 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, rtc::Thread::Current(),
193 [this] { OnChannelReady(true); });
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000194 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000195 }
196
197 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198}
199
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000200DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201
202void DataChannel::RegisterObserver(DataChannelObserver* observer) {
203 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000204 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205}
206
207void DataChannel::UnregisterObserver() {
208 observer_ = NULL;
209}
210
211bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02 +0000212 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 return false;
214 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200215 return config_.maxRetransmits == -1 && config_.maxRetransmitTime == -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 }
217}
218
Peter Boström0c4e06b2015-10-07 12:23:21 +0200219uint64_t DataChannel::buffered_amount() const {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000220 return queued_send_data_.byte_count();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221}
222
223void DataChannel::Close() {
224 if (state_ == kClosed)
225 return;
226 send_ssrc_ = 0;
227 send_ssrc_set_ = false;
228 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700229 // Will send queued data before beginning the underlying closing procedure.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 UpdateState();
231}
232
233bool DataChannel::Send(const DataBuffer& buffer) {
234 if (state_ != kOpen) {
235 return false;
236 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000237
238 // TODO(jiayl): the spec is unclear about if the remote side should get the
239 // onmessage event. We need to figure out the expected behavior and change the
240 // code accordingly.
241 if (buffer.size() == 0) {
242 return true;
243 }
244
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000245 // If the queue is non-empty, we're waiting for SignalReadyToSend,
246 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000247 if (!queued_send_data_.Empty()) {
248 // Only SCTP DataChannel queues the outgoing data when the transport is
249 // blocked.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800250 RTC_DCHECK(IsSctpLike(data_channel_type_));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000251 if (!QueueSendDataMessage(buffer)) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700252 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to queue "
253 "additional data.";
254 CloseAbruptly();
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000255 }
256 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000259 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000260 if (data_channel_type_ == cricket::DCT_RTP) {
261 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000262 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000263
264 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000265 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266}
267
Peter Boström0c4e06b2015-10-07 12:23:21 +0200268void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800269 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000270
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 return;
273 }
274 receive_ssrc_ = receive_ssrc;
275 receive_ssrc_set_ = true;
276 UpdateState();
277}
278
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000279void DataChannel::SetSctpSid(int sid) {
kwibergee89e782017-08-09 17:22:01 -0700280 RTC_DCHECK_LT(config_.id, 0);
281 RTC_DCHECK_GE(sid, 0);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800282 RTC_DCHECK(IsSctpLike(data_channel_type_));
deadbeefab9b2d12015-10-14 11:33:11 -0700283 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000284 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700285 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000286
287 config_.id = sid;
288 provider_->AddSctpDataStream(sid);
289}
290
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700291void DataChannel::OnClosingProcedureStartedRemotely(int sid) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800292 if (IsSctpLike(data_channel_type_) && sid == config_.id &&
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700293 state_ != kClosing && state_ != kClosed) {
294 // Don't bother sending queued data since the side that initiated the
295 // closure wouldn't receive it anyway. See crbug.com/559394 for a lengthy
296 // discussion about this.
297 queued_send_data_.Clear();
298 queued_control_data_.Clear();
299 // Just need to change state to kClosing, SctpTransport will handle the
300 // rest of the closing procedure and OnClosingProcedureComplete will be
301 // called later.
302 started_closing_procedure_ = true;
303 SetState(kClosing);
304 }
305}
306
307void DataChannel::OnClosingProcedureComplete(int sid) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800308 if (IsSctpLike(data_channel_type_) && sid == config_.id) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700309 // If the closing procedure is complete, we should have finished sending
310 // all pending data and transitioned to kClosing already.
311 RTC_DCHECK_EQ(state_, kClosing);
312 RTC_DCHECK(queued_send_data_.Empty());
313 DisconnectFromProvider();
314 SetState(kClosed);
315 }
316}
317
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000318void DataChannel::OnTransportChannelCreated() {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800319 RTC_DCHECK(IsSctpLike(data_channel_type_));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000320 if (!connected_to_provider_) {
321 connected_to_provider_ = provider_->ConnectDataChannel(this);
322 }
323 // The sid may have been unassigned when provider_->ConnectDataChannel was
324 // done. So always add the streams even if connected_to_provider_ is true.
325 if (config_.id >= 0) {
326 provider_->AddSctpDataStream(config_.id);
327 }
328}
329
deadbeefab9b2d12015-10-14 11:33:11 -0700330void DataChannel::OnTransportChannelDestroyed() {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700331 // The SctpTransport is going away (for example, because the SCTP m= section
332 // was rejected), so we need to close abruptly.
333 CloseAbruptly();
334}
335
336// The remote peer request that this channel shall be closed.
337void DataChannel::RemotePeerRequestClose() {
338 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
339 CloseAbruptly();
deadbeefab9b2d12015-10-14 11:33:11 -0700340}
341
Peter Boström0c4e06b2015-10-07 12:23:21 +0200342void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800343 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 return;
346 }
347 send_ssrc_ = send_ssrc;
348 send_ssrc_set_ = true;
349 UpdateState();
350}
351
deadbeef953c2ce2017-01-09 14:53:41 -0800352void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700353 const rtc::CopyOnWriteBuffer& payload) {
deadbeef953c2ce2017-01-09 14:53:41 -0800354 if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) {
355 return;
356 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800357 if (IsSctpLike(data_channel_type_) && params.sid != config_.id) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000358 return;
359 }
360
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000361 if (params.type == cricket::DMT_CONTROL) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800362 RTC_DCHECK(IsSctpLike(data_channel_type_));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400363 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000364 // Ignore it if we are not expecting an ACK message.
Jonas Olsson45cc8902018-02-13 10:37:07 +0100365 RTC_LOG(LS_WARNING)
366 << "DataChannel received unexpected CONTROL message, sid = "
367 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000368 return;
369 }
370 if (ParseDataChannelOpenAckMessage(payload)) {
371 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400372 handshake_state_ = kHandshakeReady;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100373 RTC_LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
374 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000375 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100376 RTC_LOG(LS_WARNING)
377 << "DataChannel failed to parse OPEN_ACK message, sid = "
378 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000379 }
380 return;
381 }
382
nisseede5da42017-01-12 05:15:36 -0800383 RTC_DCHECK(params.type == cricket::DMT_BINARY ||
384 params.type == cricket::DMT_TEXT);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000385
Mirko Bonadei675513b2017-11-09 11:09:25 +0100386 RTC_LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = "
387 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000388 // We can send unordered as soon as we receive any DATA message since the
389 // remote side must have received the OPEN (and old clients do not send
390 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400391 if (handshake_state_ == kHandshakeWaitingForAck) {
392 handshake_state_ = kHandshakeReady;
393 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000394
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000395 bool binary = (params.type == cricket::DMT_BINARY);
kwibergd1fe2812016-04-27 06:47:29 -0700396 std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400397 if (state_ == kOpen && observer_) {
hbos84ffdee2016-10-12 14:14:39 -0700398 ++messages_received_;
399 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000400 observer_->OnMessage(*buffer.get());
401 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000402 if (queued_received_data_.byte_count() + payload.size() >
403 kMaxQueuedReceivedDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100404 RTC_LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000405
406 queued_received_data_.Clear();
407 if (data_channel_type_ != cricket::DCT_RTP) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700408 CloseAbruptly();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000409 }
410
411 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000412 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000413 queued_received_data_.Push(buffer.release());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000414 }
415}
416
417void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400418 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000419 if (!writable) {
420 return;
421 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000422
Lally Singh5c6c6e02015-05-29 11:52:39 -0400423 SendQueuedControlMessages();
424 SendQueuedDataMessages();
425 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000426}
427
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700428void DataChannel::CloseAbruptly() {
429 if (state_ == kClosed) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000430 return;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700431 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000432
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700433 if (connected_to_provider_) {
434 DisconnectFromProvider();
435 }
436
437 // Closing abruptly means any queued data gets thrown away.
438 queued_send_data_.Clear();
439 queued_control_data_.Clear();
440
441 // Still go to "kClosing" before "kClosed", since observers may be expecting
442 // that.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700444 SetState(kClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445}
446
447void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400448 // UpdateState determines what to do from a few state variables. Include
449 // all conditions required for each state transition here for
450 // clarity. OnChannelReady(true) will send any queued data and then invoke
451 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 switch (state_) {
453 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000454 if (send_ssrc_set_ == receive_ssrc_set_) {
455 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
456 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400458 if (connected_to_provider_) {
459 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 06:15:43 -0700460 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400461 WriteDataChannelOpenMessage(label_, config_, &payload);
462 SendControlMessage(payload);
463 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 06:15:43 -0700464 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400465 WriteDataChannelOpenAckMessage(&payload);
466 SendControlMessage(payload);
467 }
Yves Gerey665174f2018-06-19 15:03:05 +0200468 if (writable_ && (handshake_state_ == kHandshakeReady ||
469 handshake_state_ == kHandshakeWaitingForAck)) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400470 SetState(kOpen);
471 // If we have received buffers before the channel got writable.
472 // Deliver them now.
473 DeliverQueuedReceivedData();
474 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475 }
476 }
477 break;
478 }
479 case kOpen: {
480 break;
481 }
482 case kClosing: {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700483 // Wait for all queued data to be sent before beginning the closing
484 // procedure.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400485 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700486 if (data_channel_type_ == cricket::DCT_RTP) {
487 // For RTP data channels, we can go to "closed" after we finish
488 // sending data and the send/recv SSRCs are unset.
489 if (connected_to_provider_) {
490 DisconnectFromProvider();
491 }
492 if (!send_ssrc_set_ && !receive_ssrc_set_) {
493 SetState(kClosed);
494 }
495 } else {
496 // For SCTP data channels, we need to wait for the closing procedure
497 // to complete; after calling RemoveSctpDataStream,
498 // OnClosingProcedureComplete will end up called asynchronously
499 // afterwards.
500 if (connected_to_provider_ && !started_closing_procedure_ &&
501 config_.id >= 0) {
502 started_closing_procedure_ = true;
503 provider_->RemoveSctpDataStream(config_.id);
504 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400505 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 }
507 break;
508 }
509 case kClosed:
510 break;
511 }
512}
513
514void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700515 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000516 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700517 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000518
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 state_ = state;
520 if (observer_) {
521 observer_->OnStateChange();
522 }
hbos82ebe022016-11-14 01:41:09 -0800523 if (state_ == kOpen) {
524 SignalOpened(this);
525 } else if (state_ == kClosed) {
deadbeefab9b2d12015-10-14 11:33:11 -0700526 SignalClosed(this);
527 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528}
529
Lally Singh5c6c6e02015-05-29 11:52:39 -0400530void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000531 if (!connected_to_provider_)
532 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533
wu@webrtc.org78187522013-10-07 23:32:02 +0000534 provider_->DisconnectDataChannel(this);
535 connected_to_provider_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536}
537
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000538void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400539 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000540 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000543 while (!queued_received_data_.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700544 std::unique_ptr<DataBuffer> buffer(queued_received_data_.Front());
hbos84ffdee2016-10-12 14:14:39 -0700545 ++messages_received_;
546 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000547 observer_->OnMessage(*buffer);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000548 queued_received_data_.Pop();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 }
550}
551
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000552void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400553 if (queued_send_data_.Empty()) {
554 return;
555 }
556
nisseede5da42017-01-12 05:15:36 -0800557 RTC_DCHECK(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000558
Peter Boström0c4e06b2015-10-07 12:23:21 +0200559 uint64_t start_buffered_amount = buffered_amount();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000560 while (!queued_send_data_.Empty()) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000561 DataBuffer* buffer = queued_send_data_.Front();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000562 if (!SendDataMessage(*buffer, false)) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000563 // Leave the message in the queue if sending is aborted.
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000564 break;
565 }
566 queued_send_data_.Pop();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000567 delete buffer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000568 }
bemasc0edd50c2015-07-01 13:34:33 -0700569
570 if (observer_ && buffered_amount() < start_buffered_amount) {
571 observer_->OnBufferedAmountChange(start_buffered_amount);
572 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000573}
574
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000575bool DataChannel::SendDataMessage(const DataBuffer& buffer,
576 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000577 cricket::SendDataParams send_params;
578
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800579 if (IsSctpLike(data_channel_type_)) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000580 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400581 // Send as ordered if it is still going through OPEN/ACK signaling.
582 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000583 send_params.ordered = true;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100584 RTC_LOG(LS_VERBOSE)
585 << "Sending data as ordered for unordered DataChannel "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100586 "because the OPEN_ACK message has not been received.";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000587 }
588
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000589 send_params.max_rtx_count = config_.maxRetransmits;
590 send_params.max_rtx_ms = config_.maxRetransmitTime;
deadbeef953c2ce2017-01-09 14:53:41 -0800591 send_params.sid = config_.id;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000592 } else {
593 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000594 }
595 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
596
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000597 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
598 bool success = provider_->SendData(send_params, buffer.data, &send_result);
599
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000600 if (success) {
hbos84ffdee2016-10-12 14:14:39 -0700601 ++messages_sent_;
602 bytes_sent_ += buffer.size();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000603 return true;
604 }
605
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800606 if (!IsSctpLike(data_channel_type_)) {
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000607 return false;
608 }
609
610 if (send_result == cricket::SDR_BLOCK) {
611 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
612 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000613 }
614 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000615 // Close the channel if the error is not SDR_BLOCK, or if queuing the
616 // message failed.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100617 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100618 "send_result = "
619 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700620 CloseAbruptly();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000621
622 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000623}
624
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000625bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
bemasc0edd50c2015-07-01 13:34:33 -0700626 size_t start_buffered_amount = buffered_amount();
627 if (start_buffered_amount >= kMaxQueuedSendDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100628 RTC_LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000629 return false;
630 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000631 queued_send_data_.Push(new DataBuffer(buffer));
bemasc0edd50c2015-07-01 13:34:33 -0700632
633 // The buffer can have length zero, in which case there is no change.
634 if (observer_ && buffered_amount() > start_buffered_amount) {
635 observer_->OnBufferedAmountChange(start_buffered_amount);
636 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000637 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638}
639
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000640void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000641 PacketQueue control_packets;
642 control_packets.Swap(&queued_control_data_);
643
644 while (!control_packets.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700645 std::unique_ptr<DataBuffer> buf(control_packets.Front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000646 SendControlMessage(buf->data);
647 control_packets.Pop();
648 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000649}
650
jbaucheec21bd2016-03-20 06:15:43 -0700651void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000652 queued_control_data_.Push(new DataBuffer(buffer, true));
653}
654
jbaucheec21bd2016-03-20 06:15:43 -0700655bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400656 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000657
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800658 RTC_DCHECK(IsSctpLike(data_channel_type_));
kwibergee89e782017-08-09 17:22:01 -0700659 RTC_DCHECK(writable_);
660 RTC_DCHECK_GE(config_.id, 0);
661 RTC_DCHECK(!is_open_message || !config_.negotiated);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000662
663 cricket::SendDataParams send_params;
deadbeef953c2ce2017-01-09 14:53:41 -0800664 send_params.sid = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400665 // Send data as ordered before we receive any message from the remote peer to
666 // make sure the remote peer will not receive any data before it receives the
667 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000668 send_params.ordered = config_.ordered || is_open_message;
669 send_params.type = cricket::DMT_CONTROL;
670
671 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
672 bool retval = provider_->SendData(send_params, buffer, &send_result);
673 if (retval) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100674 RTC_LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000675
Lally Singh5c6c6e02015-05-29 11:52:39 -0400676 if (handshake_state_ == kHandshakeShouldSendAck) {
677 handshake_state_ = kHandshakeReady;
678 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
679 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000680 }
681 } else if (send_result == cricket::SDR_BLOCK) {
682 QueueControlMessage(buffer);
683 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100684 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
Jonas Olsson45cc8902018-02-13 10:37:07 +0100685 " the CONTROL message, send_result = "
686 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700687 CloseAbruptly();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000688 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000689 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000690}
691
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692} // namespace webrtc