blob: f819d26695760515d08fe194ba0131520370408d [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
Yves Gerey665174f2018-06-19 15:03:05 +0200121DataChannel::DataChannel(DataChannelProviderInterface* provider,
122 cricket::DataChannelType dct,
123 const std::string& label)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 : label_(label),
hbos84ffdee2016-10-12 14:14:39 -0700125 observer_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 state_(kConnecting),
hbos84ffdee2016-10-12 14:14:39 -0700127 messages_sent_(0),
128 bytes_sent_(0),
129 messages_received_(0),
130 bytes_received_(0),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000131 data_channel_type_(dct),
132 provider_(provider),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400133 handshake_state_(kHandshakeInit),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000134 connected_to_provider_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 send_ssrc_set_(false),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000136 receive_ssrc_set_(false),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400137 writable_(false),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000138 send_ssrc_(0),
Yves Gerey665174f2018-06-19 15:03:05 +0200139 receive_ssrc_(0) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000141bool DataChannel::Init(const InternalDataChannelInit& config) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400142 if (data_channel_type_ == cricket::DCT_RTP) {
Yves Gerey665174f2018-06-19 15:03:05 +0200143 if (config.reliable || config.id != -1 || config.maxRetransmits != -1 ||
Lally Singh5c6c6e02015-05-29 11:52:39 -0400144 config.maxRetransmitTime != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100145 RTC_LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100146 "invalid DataChannelInit.";
Lally Singh5c6c6e02015-05-29 11:52:39 -0400147 return false;
148 }
149 handshake_state_ = kHandshakeReady;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000150 } else if (data_channel_type_ == cricket::DCT_SCTP) {
Yves Gerey665174f2018-06-19 15:03:05 +0200151 if (config.id < -1 || config.maxRetransmits < -1 ||
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000152 config.maxRetransmitTime < -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100153 RTC_LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100154 "invalid DataChannelInit.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 return false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000156 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000157 if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100158 RTC_LOG(LS_ERROR)
159 << "maxRetransmits and maxRetransmitTime should not be both set.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000160 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000162 config_ = config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163
Lally Singh5c6c6e02015-05-29 11:52:39 -0400164 switch (config_.open_handshake_role) {
Yves Gerey665174f2018-06-19 15:03:05 +0200165 case webrtc::InternalDataChannelInit::kNone: // pre-negotiated
166 handshake_state_ = kHandshakeReady;
167 break;
168 case webrtc::InternalDataChannelInit::kOpener:
169 handshake_state_ = kHandshakeShouldSendOpen;
170 break;
171 case webrtc::InternalDataChannelInit::kAcker:
172 handshake_state_ = kHandshakeShouldSendAck;
173 break;
Steve Anton36b29d12017-10-30 09:57:42 -0700174 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400175
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000176 // Try to connect to the transport in case the transport channel already
177 // exists.
178 OnTransportChannelCreated();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000179
180 // Checks if the transport is ready to send because the initial channel
181 // ready signal may have been sent before the DataChannel creation.
182 // This has to be done async because the upper layer objects (e.g.
183 // Chrome glue and WebKit) are not wired up properly until after this
184 // function returns.
185 if (provider_->ReadyToSendData()) {
Steve Anton044a04d2018-08-31 13:51:19 -0700186 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, rtc::Thread::Current(),
187 [this] { OnChannelReady(true); });
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000188 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000189 }
190
191 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192}
193
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000194DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195
196void DataChannel::RegisterObserver(DataChannelObserver* observer) {
197 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000198 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199}
200
201void DataChannel::UnregisterObserver() {
202 observer_ = NULL;
203}
204
205bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02 +0000206 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 return false;
208 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200209 return config_.maxRetransmits == -1 && config_.maxRetransmitTime == -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 }
211}
212
Peter Boström0c4e06b2015-10-07 12:23:21 +0200213uint64_t DataChannel::buffered_amount() const {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000214 return queued_send_data_.byte_count();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215}
216
217void DataChannel::Close() {
218 if (state_ == kClosed)
219 return;
220 send_ssrc_ = 0;
221 send_ssrc_set_ = false;
222 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700223 // Will send queued data before beginning the underlying closing procedure.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 UpdateState();
225}
226
227bool DataChannel::Send(const DataBuffer& buffer) {
228 if (state_ != kOpen) {
229 return false;
230 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000231
232 // TODO(jiayl): the spec is unclear about if the remote side should get the
233 // onmessage event. We need to figure out the expected behavior and change the
234 // code accordingly.
235 if (buffer.size() == 0) {
236 return true;
237 }
238
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000239 // If the queue is non-empty, we're waiting for SignalReadyToSend,
240 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000241 if (!queued_send_data_.Empty()) {
242 // Only SCTP DataChannel queues the outgoing data when the transport is
243 // blocked.
nisseede5da42017-01-12 05:15:36 -0800244 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000245 if (!QueueSendDataMessage(buffer)) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700246 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to queue "
247 "additional data.";
248 CloseAbruptly();
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000249 }
250 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000253 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000254 if (data_channel_type_ == cricket::DCT_RTP) {
255 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000256 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000257
258 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000259 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260}
261
Peter Boström0c4e06b2015-10-07 12:23:21 +0200262void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800263 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000264
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 return;
267 }
268 receive_ssrc_ = receive_ssrc;
269 receive_ssrc_set_ = true;
270 UpdateState();
271}
272
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000273void DataChannel::SetSctpSid(int sid) {
kwibergee89e782017-08-09 17:22:01 -0700274 RTC_DCHECK_LT(config_.id, 0);
275 RTC_DCHECK_GE(sid, 0);
276 RTC_DCHECK_EQ(data_channel_type_, cricket::DCT_SCTP);
deadbeefab9b2d12015-10-14 11:33:11 -0700277 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000278 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700279 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000280
281 config_.id = sid;
282 provider_->AddSctpDataStream(sid);
283}
284
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700285void DataChannel::OnClosingProcedureStartedRemotely(int sid) {
286 if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id &&
287 state_ != kClosing && state_ != kClosed) {
288 // Don't bother sending queued data since the side that initiated the
289 // closure wouldn't receive it anyway. See crbug.com/559394 for a lengthy
290 // discussion about this.
291 queued_send_data_.Clear();
292 queued_control_data_.Clear();
293 // Just need to change state to kClosing, SctpTransport will handle the
294 // rest of the closing procedure and OnClosingProcedureComplete will be
295 // called later.
296 started_closing_procedure_ = true;
297 SetState(kClosing);
298 }
299}
300
301void DataChannel::OnClosingProcedureComplete(int sid) {
302 if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id) {
303 // If the closing procedure is complete, we should have finished sending
304 // all pending data and transitioned to kClosing already.
305 RTC_DCHECK_EQ(state_, kClosing);
306 RTC_DCHECK(queued_send_data_.Empty());
307 DisconnectFromProvider();
308 SetState(kClosed);
309 }
310}
311
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000312void DataChannel::OnTransportChannelCreated() {
nisseede5da42017-01-12 05:15:36 -0800313 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000314 if (!connected_to_provider_) {
315 connected_to_provider_ = provider_->ConnectDataChannel(this);
316 }
317 // The sid may have been unassigned when provider_->ConnectDataChannel was
318 // done. So always add the streams even if connected_to_provider_ is true.
319 if (config_.id >= 0) {
320 provider_->AddSctpDataStream(config_.id);
321 }
322}
323
deadbeefab9b2d12015-10-14 11:33:11 -0700324void DataChannel::OnTransportChannelDestroyed() {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700325 // The SctpTransport is going away (for example, because the SCTP m= section
326 // was rejected), so we need to close abruptly.
327 CloseAbruptly();
328}
329
330// The remote peer request that this channel shall be closed.
331void DataChannel::RemotePeerRequestClose() {
332 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
333 CloseAbruptly();
deadbeefab9b2d12015-10-14 11:33:11 -0700334}
335
Peter Boström0c4e06b2015-10-07 12:23:21 +0200336void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800337 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339 return;
340 }
341 send_ssrc_ = send_ssrc;
342 send_ssrc_set_ = true;
343 UpdateState();
344}
345
deadbeef953c2ce2017-01-09 14:53:41 -0800346void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700347 const rtc::CopyOnWriteBuffer& payload) {
deadbeef953c2ce2017-01-09 14:53:41 -0800348 if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) {
349 return;
350 }
351 if (data_channel_type_ == cricket::DCT_SCTP && params.sid != config_.id) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000352 return;
353 }
354
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000355 if (params.type == cricket::DMT_CONTROL) {
nisseede5da42017-01-12 05:15:36 -0800356 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400357 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000358 // Ignore it if we are not expecting an ACK message.
Jonas Olsson45cc8902018-02-13 10:37:07 +0100359 RTC_LOG(LS_WARNING)
360 << "DataChannel received unexpected CONTROL message, sid = "
361 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000362 return;
363 }
364 if (ParseDataChannelOpenAckMessage(payload)) {
365 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400366 handshake_state_ = kHandshakeReady;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100367 RTC_LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
368 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000369 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100370 RTC_LOG(LS_WARNING)
371 << "DataChannel failed to parse OPEN_ACK message, sid = "
372 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000373 }
374 return;
375 }
376
nisseede5da42017-01-12 05:15:36 -0800377 RTC_DCHECK(params.type == cricket::DMT_BINARY ||
378 params.type == cricket::DMT_TEXT);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000379
Mirko Bonadei675513b2017-11-09 11:09:25 +0100380 RTC_LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = "
381 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000382 // We can send unordered as soon as we receive any DATA message since the
383 // remote side must have received the OPEN (and old clients do not send
384 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400385 if (handshake_state_ == kHandshakeWaitingForAck) {
386 handshake_state_ = kHandshakeReady;
387 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000388
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000389 bool binary = (params.type == cricket::DMT_BINARY);
kwibergd1fe2812016-04-27 06:47:29 -0700390 std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400391 if (state_ == kOpen && observer_) {
hbos84ffdee2016-10-12 14:14:39 -0700392 ++messages_received_;
393 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000394 observer_->OnMessage(*buffer.get());
395 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000396 if (queued_received_data_.byte_count() + payload.size() >
397 kMaxQueuedReceivedDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100398 RTC_LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000399
400 queued_received_data_.Clear();
401 if (data_channel_type_ != cricket::DCT_RTP) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700402 CloseAbruptly();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000403 }
404
405 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000406 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000407 queued_received_data_.Push(buffer.release());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000408 }
409}
410
411void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400412 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000413 if (!writable) {
414 return;
415 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000416
Lally Singh5c6c6e02015-05-29 11:52:39 -0400417 SendQueuedControlMessages();
418 SendQueuedDataMessages();
419 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000420}
421
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700422void DataChannel::CloseAbruptly() {
423 if (state_ == kClosed) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000424 return;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700425 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000426
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700427 if (connected_to_provider_) {
428 DisconnectFromProvider();
429 }
430
431 // Closing abruptly means any queued data gets thrown away.
432 queued_send_data_.Clear();
433 queued_control_data_.Clear();
434
435 // Still go to "kClosing" before "kClosed", since observers may be expecting
436 // that.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700438 SetState(kClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439}
440
441void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400442 // UpdateState determines what to do from a few state variables. Include
443 // all conditions required for each state transition here for
444 // clarity. OnChannelReady(true) will send any queued data and then invoke
445 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 switch (state_) {
447 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000448 if (send_ssrc_set_ == receive_ssrc_set_) {
449 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
450 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400452 if (connected_to_provider_) {
453 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 06:15:43 -0700454 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400455 WriteDataChannelOpenMessage(label_, config_, &payload);
456 SendControlMessage(payload);
457 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 06:15:43 -0700458 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400459 WriteDataChannelOpenAckMessage(&payload);
460 SendControlMessage(payload);
461 }
Yves Gerey665174f2018-06-19 15:03:05 +0200462 if (writable_ && (handshake_state_ == kHandshakeReady ||
463 handshake_state_ == kHandshakeWaitingForAck)) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400464 SetState(kOpen);
465 // If we have received buffers before the channel got writable.
466 // Deliver them now.
467 DeliverQueuedReceivedData();
468 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469 }
470 }
471 break;
472 }
473 case kOpen: {
474 break;
475 }
476 case kClosing: {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700477 // Wait for all queued data to be sent before beginning the closing
478 // procedure.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400479 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700480 if (data_channel_type_ == cricket::DCT_RTP) {
481 // For RTP data channels, we can go to "closed" after we finish
482 // sending data and the send/recv SSRCs are unset.
483 if (connected_to_provider_) {
484 DisconnectFromProvider();
485 }
486 if (!send_ssrc_set_ && !receive_ssrc_set_) {
487 SetState(kClosed);
488 }
489 } else {
490 // For SCTP data channels, we need to wait for the closing procedure
491 // to complete; after calling RemoveSctpDataStream,
492 // OnClosingProcedureComplete will end up called asynchronously
493 // afterwards.
494 if (connected_to_provider_ && !started_closing_procedure_ &&
495 config_.id >= 0) {
496 started_closing_procedure_ = true;
497 provider_->RemoveSctpDataStream(config_.id);
498 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400499 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 }
501 break;
502 }
503 case kClosed:
504 break;
505 }
506}
507
508void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700509 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000510 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700511 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000512
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 state_ = state;
514 if (observer_) {
515 observer_->OnStateChange();
516 }
hbos82ebe022016-11-14 01:41:09 -0800517 if (state_ == kOpen) {
518 SignalOpened(this);
519 } else if (state_ == kClosed) {
deadbeefab9b2d12015-10-14 11:33:11 -0700520 SignalClosed(this);
521 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522}
523
Lally Singh5c6c6e02015-05-29 11:52:39 -0400524void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000525 if (!connected_to_provider_)
526 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527
wu@webrtc.org78187522013-10-07 23:32:02 +0000528 provider_->DisconnectDataChannel(this);
529 connected_to_provider_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530}
531
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000532void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400533 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000534 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000537 while (!queued_received_data_.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700538 std::unique_ptr<DataBuffer> buffer(queued_received_data_.Front());
hbos84ffdee2016-10-12 14:14:39 -0700539 ++messages_received_;
540 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000541 observer_->OnMessage(*buffer);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000542 queued_received_data_.Pop();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 }
544}
545
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000546void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400547 if (queued_send_data_.Empty()) {
548 return;
549 }
550
nisseede5da42017-01-12 05:15:36 -0800551 RTC_DCHECK(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000552
Peter Boström0c4e06b2015-10-07 12:23:21 +0200553 uint64_t start_buffered_amount = buffered_amount();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000554 while (!queued_send_data_.Empty()) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000555 DataBuffer* buffer = queued_send_data_.Front();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000556 if (!SendDataMessage(*buffer, false)) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000557 // Leave the message in the queue if sending is aborted.
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000558 break;
559 }
560 queued_send_data_.Pop();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000561 delete buffer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000562 }
bemasc0edd50c2015-07-01 13:34:33 -0700563
564 if (observer_ && buffered_amount() < start_buffered_amount) {
565 observer_->OnBufferedAmountChange(start_buffered_amount);
566 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000567}
568
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000569bool DataChannel::SendDataMessage(const DataBuffer& buffer,
570 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000571 cricket::SendDataParams send_params;
572
wu@webrtc.org78187522013-10-07 23:32:02 +0000573 if (data_channel_type_ == cricket::DCT_SCTP) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000574 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400575 // Send as ordered if it is still going through OPEN/ACK signaling.
576 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000577 send_params.ordered = true;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100578 RTC_LOG(LS_VERBOSE)
579 << "Sending data as ordered for unordered DataChannel "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100580 "because the OPEN_ACK message has not been received.";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000581 }
582
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000583 send_params.max_rtx_count = config_.maxRetransmits;
584 send_params.max_rtx_ms = config_.maxRetransmitTime;
deadbeef953c2ce2017-01-09 14:53:41 -0800585 send_params.sid = config_.id;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000586 } else {
587 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000588 }
589 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
590
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000591 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
592 bool success = provider_->SendData(send_params, buffer.data, &send_result);
593
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000594 if (success) {
hbos84ffdee2016-10-12 14:14:39 -0700595 ++messages_sent_;
596 bytes_sent_ += buffer.size();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000597 return true;
598 }
599
600 if (data_channel_type_ != cricket::DCT_SCTP) {
601 return false;
602 }
603
604 if (send_result == cricket::SDR_BLOCK) {
605 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
606 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000607 }
608 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000609 // Close the channel if the error is not SDR_BLOCK, or if queuing the
610 // message failed.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100611 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100612 "send_result = "
613 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700614 CloseAbruptly();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000615
616 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000617}
618
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000619bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
bemasc0edd50c2015-07-01 13:34:33 -0700620 size_t start_buffered_amount = buffered_amount();
621 if (start_buffered_amount >= kMaxQueuedSendDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100622 RTC_LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000623 return false;
624 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000625 queued_send_data_.Push(new DataBuffer(buffer));
bemasc0edd50c2015-07-01 13:34:33 -0700626
627 // The buffer can have length zero, in which case there is no change.
628 if (observer_ && buffered_amount() > start_buffered_amount) {
629 observer_->OnBufferedAmountChange(start_buffered_amount);
630 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000631 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632}
633
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000634void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000635 PacketQueue control_packets;
636 control_packets.Swap(&queued_control_data_);
637
638 while (!control_packets.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700639 std::unique_ptr<DataBuffer> buf(control_packets.Front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000640 SendControlMessage(buf->data);
641 control_packets.Pop();
642 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000643}
644
jbaucheec21bd2016-03-20 06:15:43 -0700645void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000646 queued_control_data_.Push(new DataBuffer(buffer, true));
647}
648
jbaucheec21bd2016-03-20 06:15:43 -0700649bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400650 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000651
kwibergee89e782017-08-09 17:22:01 -0700652 RTC_DCHECK_EQ(data_channel_type_, cricket::DCT_SCTP);
653 RTC_DCHECK(writable_);
654 RTC_DCHECK_GE(config_.id, 0);
655 RTC_DCHECK(!is_open_message || !config_.negotiated);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000656
657 cricket::SendDataParams send_params;
deadbeef953c2ce2017-01-09 14:53:41 -0800658 send_params.sid = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400659 // Send data as ordered before we receive any message from the remote peer to
660 // make sure the remote peer will not receive any data before it receives the
661 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000662 send_params.ordered = config_.ordered || is_open_message;
663 send_params.type = cricket::DMT_CONTROL;
664
665 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
666 bool retval = provider_->SendData(send_params, buffer, &send_result);
667 if (retval) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100668 RTC_LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000669
Lally Singh5c6c6e02015-05-29 11:52:39 -0400670 if (handshake_state_ == kHandshakeShouldSendAck) {
671 handshake_state_ = kHandshakeReady;
672 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
673 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000674 }
675 } else if (send_result == cricket::SDR_BLOCK) {
676 QueueControlMessage(buffer);
677 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100678 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
Jonas Olsson45cc8902018-02-13 10:37:07 +0100679 " the CONTROL message, send_result = "
680 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700681 CloseAbruptly();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000682 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000683 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000684}
685
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686} // namespace webrtc