blob: 6971ca8f66c41fed25155731baca4b57439a445a [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
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000027enum {
28 MSG_CHANNELREADY,
29};
30
deadbeefab9b2d12015-10-14 11:33:11 -070031bool SctpSidAllocator::AllocateSid(rtc::SSLRole role, int* sid) {
32 int potential_sid = (role == rtc::SSL_CLIENT) ? 0 : 1;
33 while (!IsSidAvailable(potential_sid)) {
34 potential_sid += 2;
35 if (potential_sid > static_cast<int>(cricket::kMaxSctpSid)) {
36 return false;
37 }
38 }
39
40 *sid = potential_sid;
41 used_sids_.insert(potential_sid);
42 return true;
43}
44
45bool SctpSidAllocator::ReserveSid(int sid) {
46 if (!IsSidAvailable(sid)) {
47 return false;
48 }
49 used_sids_.insert(sid);
50 return true;
51}
52
53void SctpSidAllocator::ReleaseSid(int sid) {
54 auto it = used_sids_.find(sid);
55 if (it != used_sids_.end()) {
56 used_sids_.erase(it);
57 }
58}
59
60bool SctpSidAllocator::IsSidAvailable(int sid) const {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -070061 if (sid < static_cast<int>(cricket::kMinSctpSid) ||
62 sid > static_cast<int>(cricket::kMaxSctpSid)) {
deadbeefab9b2d12015-10-14 11:33:11 -070063 return false;
64 }
65 return used_sids_.find(sid) == used_sids_.end();
66}
67
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000068DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {}
69
70DataChannel::PacketQueue::~PacketQueue() {
71 Clear();
72}
73
74bool DataChannel::PacketQueue::Empty() const {
75 return packets_.empty();
76}
77
78DataBuffer* DataChannel::PacketQueue::Front() {
79 return packets_.front();
80}
81
82void DataChannel::PacketQueue::Pop() {
83 if (packets_.empty()) {
84 return;
85 }
86
87 byte_count_ -= packets_.front()->size();
88 packets_.pop_front();
89}
90
91void DataChannel::PacketQueue::Push(DataBuffer* packet) {
92 byte_count_ += packet->size();
93 packets_.push_back(packet);
94}
95
96void DataChannel::PacketQueue::Clear() {
97 while (!packets_.empty()) {
98 delete packets_.front();
99 packets_.pop_front();
100 }
101 byte_count_ = 0;
102}
103
104void DataChannel::PacketQueue::Swap(PacketQueue* other) {
105 size_t other_byte_count = other->byte_count_;
106 other->byte_count_ = byte_count_;
107 byte_count_ = other_byte_count;
108
109 other->packets_.swap(packets_);
110}
111
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000112rtc::scoped_refptr<DataChannel> DataChannel::Create(
wu@webrtc.org78187522013-10-07 23:32:02 +0000113 DataChannelProviderInterface* provider,
114 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000116 const InternalDataChannelInit& config) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000117 rtc::scoped_refptr<DataChannel> channel(
118 new rtc::RefCountedObject<DataChannel>(provider, dct, label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 if (!channel->Init(config)) {
120 return NULL;
121 }
122 return channel;
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;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000154 } else if (data_channel_type_ == cricket::DCT_SCTP) {
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()) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700190 rtc::Thread::Current()->Post(RTC_FROM_HERE, this, MSG_CHANNELREADY, NULL);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000191 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000192 }
193
194 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195}
196
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000197DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198
199void DataChannel::RegisterObserver(DataChannelObserver* observer) {
200 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000201 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202}
203
204void DataChannel::UnregisterObserver() {
205 observer_ = NULL;
206}
207
208bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02 +0000209 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 return false;
211 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200212 return config_.maxRetransmits == -1 && config_.maxRetransmitTime == -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 }
214}
215
Peter Boström0c4e06b2015-10-07 12:23:21 +0200216uint64_t DataChannel::buffered_amount() const {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000217 return queued_send_data_.byte_count();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218}
219
220void DataChannel::Close() {
221 if (state_ == kClosed)
222 return;
223 send_ssrc_ = 0;
224 send_ssrc_set_ = false;
225 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700226 // Will send queued data before beginning the underlying closing procedure.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227 UpdateState();
228}
229
230bool DataChannel::Send(const DataBuffer& buffer) {
231 if (state_ != kOpen) {
232 return false;
233 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000234
235 // TODO(jiayl): the spec is unclear about if the remote side should get the
236 // onmessage event. We need to figure out the expected behavior and change the
237 // code accordingly.
238 if (buffer.size() == 0) {
239 return true;
240 }
241
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000242 // If the queue is non-empty, we're waiting for SignalReadyToSend,
243 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000244 if (!queued_send_data_.Empty()) {
245 // Only SCTP DataChannel queues the outgoing data when the transport is
246 // blocked.
nisseede5da42017-01-12 05:15:36 -0800247 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000248 if (!QueueSendDataMessage(buffer)) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700249 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to queue "
250 "additional data.";
251 CloseAbruptly();
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000252 }
253 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000256 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000257 if (data_channel_type_ == cricket::DCT_RTP) {
258 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000259 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000260
261 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000262 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263}
264
Peter Boström0c4e06b2015-10-07 12:23:21 +0200265void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800266 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000267
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 return;
270 }
271 receive_ssrc_ = receive_ssrc;
272 receive_ssrc_set_ = true;
273 UpdateState();
274}
275
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000276void DataChannel::SetSctpSid(int sid) {
kwibergee89e782017-08-09 17:22:01 -0700277 RTC_DCHECK_LT(config_.id, 0);
278 RTC_DCHECK_GE(sid, 0);
279 RTC_DCHECK_EQ(data_channel_type_, cricket::DCT_SCTP);
deadbeefab9b2d12015-10-14 11:33:11 -0700280 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000281 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700282 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000283
284 config_.id = sid;
285 provider_->AddSctpDataStream(sid);
286}
287
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700288void DataChannel::OnClosingProcedureStartedRemotely(int sid) {
289 if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id &&
290 state_ != kClosing && state_ != kClosed) {
291 // Don't bother sending queued data since the side that initiated the
292 // closure wouldn't receive it anyway. See crbug.com/559394 for a lengthy
293 // discussion about this.
294 queued_send_data_.Clear();
295 queued_control_data_.Clear();
296 // Just need to change state to kClosing, SctpTransport will handle the
297 // rest of the closing procedure and OnClosingProcedureComplete will be
298 // called later.
299 started_closing_procedure_ = true;
300 SetState(kClosing);
301 }
302}
303
304void DataChannel::OnClosingProcedureComplete(int sid) {
305 if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id) {
306 // If the closing procedure is complete, we should have finished sending
307 // all pending data and transitioned to kClosing already.
308 RTC_DCHECK_EQ(state_, kClosing);
309 RTC_DCHECK(queued_send_data_.Empty());
310 DisconnectFromProvider();
311 SetState(kClosed);
312 }
313}
314
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000315void DataChannel::OnTransportChannelCreated() {
nisseede5da42017-01-12 05:15:36 -0800316 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000317 if (!connected_to_provider_) {
318 connected_to_provider_ = provider_->ConnectDataChannel(this);
319 }
320 // The sid may have been unassigned when provider_->ConnectDataChannel was
321 // done. So always add the streams even if connected_to_provider_ is true.
322 if (config_.id >= 0) {
323 provider_->AddSctpDataStream(config_.id);
324 }
325}
326
deadbeefab9b2d12015-10-14 11:33:11 -0700327void DataChannel::OnTransportChannelDestroyed() {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700328 // The SctpTransport is going away (for example, because the SCTP m= section
329 // was rejected), so we need to close abruptly.
330 CloseAbruptly();
331}
332
333// The remote peer request that this channel shall be closed.
334void DataChannel::RemotePeerRequestClose() {
335 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
336 CloseAbruptly();
deadbeefab9b2d12015-10-14 11:33:11 -0700337}
338
Peter Boström0c4e06b2015-10-07 12:23:21 +0200339void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800340 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 return;
343 }
344 send_ssrc_ = send_ssrc;
345 send_ssrc_set_ = true;
346 UpdateState();
347}
348
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000349void DataChannel::OnMessage(rtc::Message* msg) {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000350 switch (msg->message_id) {
351 case MSG_CHANNELREADY:
352 OnChannelReady(true);
353 break;
354 }
355}
356
deadbeef953c2ce2017-01-09 14:53:41 -0800357void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700358 const rtc::CopyOnWriteBuffer& payload) {
deadbeef953c2ce2017-01-09 14:53:41 -0800359 if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) {
360 return;
361 }
362 if (data_channel_type_ == cricket::DCT_SCTP && params.sid != config_.id) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000363 return;
364 }
365
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000366 if (params.type == cricket::DMT_CONTROL) {
nisseede5da42017-01-12 05:15:36 -0800367 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400368 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000369 // Ignore it if we are not expecting an ACK message.
Jonas Olsson45cc8902018-02-13 10:37:07 +0100370 RTC_LOG(LS_WARNING)
371 << "DataChannel received unexpected CONTROL message, sid = "
372 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000373 return;
374 }
375 if (ParseDataChannelOpenAckMessage(payload)) {
376 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400377 handshake_state_ = kHandshakeReady;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100378 RTC_LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
379 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000380 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100381 RTC_LOG(LS_WARNING)
382 << "DataChannel failed to parse OPEN_ACK message, sid = "
383 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000384 }
385 return;
386 }
387
nisseede5da42017-01-12 05:15:36 -0800388 RTC_DCHECK(params.type == cricket::DMT_BINARY ||
389 params.type == cricket::DMT_TEXT);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000390
Mirko Bonadei675513b2017-11-09 11:09:25 +0100391 RTC_LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = "
392 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000393 // We can send unordered as soon as we receive any DATA message since the
394 // remote side must have received the OPEN (and old clients do not send
395 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400396 if (handshake_state_ == kHandshakeWaitingForAck) {
397 handshake_state_ = kHandshakeReady;
398 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000399
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000400 bool binary = (params.type == cricket::DMT_BINARY);
kwibergd1fe2812016-04-27 06:47:29 -0700401 std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400402 if (state_ == kOpen && observer_) {
hbos84ffdee2016-10-12 14:14:39 -0700403 ++messages_received_;
404 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000405 observer_->OnMessage(*buffer.get());
406 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000407 if (queued_received_data_.byte_count() + payload.size() >
408 kMaxQueuedReceivedDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100409 RTC_LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000410
411 queued_received_data_.Clear();
412 if (data_channel_type_ != cricket::DCT_RTP) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700413 CloseAbruptly();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000414 }
415
416 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000417 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000418 queued_received_data_.Push(buffer.release());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000419 }
420}
421
422void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400423 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000424 if (!writable) {
425 return;
426 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000427
Lally Singh5c6c6e02015-05-29 11:52:39 -0400428 SendQueuedControlMessages();
429 SendQueuedDataMessages();
430 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000431}
432
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700433void DataChannel::CloseAbruptly() {
434 if (state_ == kClosed) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000435 return;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700436 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000437
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700438 if (connected_to_provider_) {
439 DisconnectFromProvider();
440 }
441
442 // Closing abruptly means any queued data gets thrown away.
443 queued_send_data_.Clear();
444 queued_control_data_.Clear();
445
446 // Still go to "kClosing" before "kClosed", since observers may be expecting
447 // that.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700449 SetState(kClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450}
451
452void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400453 // UpdateState determines what to do from a few state variables. Include
454 // all conditions required for each state transition here for
455 // clarity. OnChannelReady(true) will send any queued data and then invoke
456 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457 switch (state_) {
458 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000459 if (send_ssrc_set_ == receive_ssrc_set_) {
460 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
461 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400463 if (connected_to_provider_) {
464 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 06:15:43 -0700465 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400466 WriteDataChannelOpenMessage(label_, config_, &payload);
467 SendControlMessage(payload);
468 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 06:15:43 -0700469 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400470 WriteDataChannelOpenAckMessage(&payload);
471 SendControlMessage(payload);
472 }
Yves Gerey665174f2018-06-19 15:03:05 +0200473 if (writable_ && (handshake_state_ == kHandshakeReady ||
474 handshake_state_ == kHandshakeWaitingForAck)) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400475 SetState(kOpen);
476 // If we have received buffers before the channel got writable.
477 // Deliver them now.
478 DeliverQueuedReceivedData();
479 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 }
481 }
482 break;
483 }
484 case kOpen: {
485 break;
486 }
487 case kClosing: {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700488 // Wait for all queued data to be sent before beginning the closing
489 // procedure.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400490 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700491 if (data_channel_type_ == cricket::DCT_RTP) {
492 // For RTP data channels, we can go to "closed" after we finish
493 // sending data and the send/recv SSRCs are unset.
494 if (connected_to_provider_) {
495 DisconnectFromProvider();
496 }
497 if (!send_ssrc_set_ && !receive_ssrc_set_) {
498 SetState(kClosed);
499 }
500 } else {
501 // For SCTP data channels, we need to wait for the closing procedure
502 // to complete; after calling RemoveSctpDataStream,
503 // OnClosingProcedureComplete will end up called asynchronously
504 // afterwards.
505 if (connected_to_provider_ && !started_closing_procedure_ &&
506 config_.id >= 0) {
507 started_closing_procedure_ = true;
508 provider_->RemoveSctpDataStream(config_.id);
509 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400510 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 }
512 break;
513 }
514 case kClosed:
515 break;
516 }
517}
518
519void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700520 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000521 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700522 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000523
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 state_ = state;
525 if (observer_) {
526 observer_->OnStateChange();
527 }
hbos82ebe022016-11-14 01:41:09 -0800528 if (state_ == kOpen) {
529 SignalOpened(this);
530 } else if (state_ == kClosed) {
deadbeefab9b2d12015-10-14 11:33:11 -0700531 SignalClosed(this);
532 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533}
534
Lally Singh5c6c6e02015-05-29 11:52:39 -0400535void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000536 if (!connected_to_provider_)
537 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538
wu@webrtc.org78187522013-10-07 23:32:02 +0000539 provider_->DisconnectDataChannel(this);
540 connected_to_provider_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541}
542
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000543void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400544 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000545 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000548 while (!queued_received_data_.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700549 std::unique_ptr<DataBuffer> buffer(queued_received_data_.Front());
hbos84ffdee2016-10-12 14:14:39 -0700550 ++messages_received_;
551 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000552 observer_->OnMessage(*buffer);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000553 queued_received_data_.Pop();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 }
555}
556
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000557void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400558 if (queued_send_data_.Empty()) {
559 return;
560 }
561
nisseede5da42017-01-12 05:15:36 -0800562 RTC_DCHECK(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000563
Peter Boström0c4e06b2015-10-07 12:23:21 +0200564 uint64_t start_buffered_amount = buffered_amount();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000565 while (!queued_send_data_.Empty()) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000566 DataBuffer* buffer = queued_send_data_.Front();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000567 if (!SendDataMessage(*buffer, false)) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000568 // Leave the message in the queue if sending is aborted.
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000569 break;
570 }
571 queued_send_data_.Pop();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000572 delete buffer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000573 }
bemasc0edd50c2015-07-01 13:34:33 -0700574
575 if (observer_ && buffered_amount() < start_buffered_amount) {
576 observer_->OnBufferedAmountChange(start_buffered_amount);
577 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000578}
579
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000580bool DataChannel::SendDataMessage(const DataBuffer& buffer,
581 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000582 cricket::SendDataParams send_params;
583
wu@webrtc.org78187522013-10-07 23:32:02 +0000584 if (data_channel_type_ == cricket::DCT_SCTP) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000585 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400586 // Send as ordered if it is still going through OPEN/ACK signaling.
587 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000588 send_params.ordered = true;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100589 RTC_LOG(LS_VERBOSE)
590 << "Sending data as ordered for unordered DataChannel "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100591 "because the OPEN_ACK message has not been received.";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000592 }
593
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000594 send_params.max_rtx_count = config_.maxRetransmits;
595 send_params.max_rtx_ms = config_.maxRetransmitTime;
deadbeef953c2ce2017-01-09 14:53:41 -0800596 send_params.sid = config_.id;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000597 } else {
598 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000599 }
600 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
601
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000602 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
603 bool success = provider_->SendData(send_params, buffer.data, &send_result);
604
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000605 if (success) {
hbos84ffdee2016-10-12 14:14:39 -0700606 ++messages_sent_;
607 bytes_sent_ += buffer.size();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000608 return true;
609 }
610
611 if (data_channel_type_ != cricket::DCT_SCTP) {
612 return false;
613 }
614
615 if (send_result == cricket::SDR_BLOCK) {
616 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
617 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000618 }
619 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000620 // Close the channel if the error is not SDR_BLOCK, or if queuing the
621 // message failed.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100622 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100623 "send_result = "
624 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700625 CloseAbruptly();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000626
627 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000628}
629
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000630bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
bemasc0edd50c2015-07-01 13:34:33 -0700631 size_t start_buffered_amount = buffered_amount();
632 if (start_buffered_amount >= kMaxQueuedSendDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100633 RTC_LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000634 return false;
635 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000636 queued_send_data_.Push(new DataBuffer(buffer));
bemasc0edd50c2015-07-01 13:34:33 -0700637
638 // The buffer can have length zero, in which case there is no change.
639 if (observer_ && buffered_amount() > start_buffered_amount) {
640 observer_->OnBufferedAmountChange(start_buffered_amount);
641 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000642 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643}
644
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000645void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000646 PacketQueue control_packets;
647 control_packets.Swap(&queued_control_data_);
648
649 while (!control_packets.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700650 std::unique_ptr<DataBuffer> buf(control_packets.Front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000651 SendControlMessage(buf->data);
652 control_packets.Pop();
653 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000654}
655
jbaucheec21bd2016-03-20 06:15:43 -0700656void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000657 queued_control_data_.Push(new DataBuffer(buffer, true));
658}
659
jbaucheec21bd2016-03-20 06:15:43 -0700660bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400661 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000662
kwibergee89e782017-08-09 17:22:01 -0700663 RTC_DCHECK_EQ(data_channel_type_, cricket::DCT_SCTP);
664 RTC_DCHECK(writable_);
665 RTC_DCHECK_GE(config_.id, 0);
666 RTC_DCHECK(!is_open_message || !config_.negotiated);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000667
668 cricket::SendDataParams send_params;
deadbeef953c2ce2017-01-09 14:53:41 -0800669 send_params.sid = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400670 // Send data as ordered before we receive any message from the remote peer to
671 // make sure the remote peer will not receive any data before it receives the
672 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000673 send_params.ordered = config_.ordered || is_open_message;
674 send_params.type = cricket::DMT_CONTROL;
675
676 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
677 bool retval = provider_->SendData(send_params, buffer, &send_result);
678 if (retval) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100679 RTC_LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000680
Lally Singh5c6c6e02015-05-29 11:52:39 -0400681 if (handshake_state_ == kHandshakeShouldSendAck) {
682 handshake_state_ = kHandshakeReady;
683 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
684 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000685 }
686 } else if (send_result == cricket::SDR_BLOCK) {
687 QueueControlMessage(buffer);
688 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100689 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
Jonas Olsson45cc8902018-02-13 10:37:07 +0100690 " the CONTROL message, send_result = "
691 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700692 CloseAbruptly();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000693 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000694 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000695}
696
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697} // namespace webrtc