blob: cd4ddedf34340d15decb8a05733e182fc5cd3fb6 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +000010
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/data_channel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
kwibergd1fe2812016-04-27 06:47:29 -070013#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <string>
Steve Anton944c7552018-12-13 14:19:10 -080015#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
Steve Anton944c7552018-12-13 14:19:10 -080017#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "media/sctp/sctp_transport_internal.h"
19#include "pc/sctp_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/location.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/ref_counted_object.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
26namespace webrtc {
27
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000028static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024;
29static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020031InternalDataChannelInit::InternalDataChannelInit(const DataChannelInit& base)
32 : DataChannelInit(base), open_handshake_role(kOpener) {
33 // If the channel is externally negotiated, do not send the OPEN message.
34 if (base.negotiated) {
35 open_handshake_role = kNone;
36 } else {
37 // Datachannel is externally negotiated. Ignore the id value.
38 // Specified in createDataChannel, WebRTC spec section 6.1 bullet 13.
39 id = -1;
40 }
41 // Backwards compatibility: If base.maxRetransmits or base.maxRetransmitTime
42 // have been set to -1, unset them.
43 if (maxRetransmits && *maxRetransmits == -1) {
44 RTC_LOG(LS_ERROR)
45 << "Accepting maxRetransmits = -1 for backwards compatibility";
46 maxRetransmits = absl::nullopt;
47 }
48 if (maxRetransmitTime && *maxRetransmitTime == -1) {
49 RTC_LOG(LS_ERROR)
50 << "Accepting maxRetransmitTime = -1 for backwards compatibility";
51 maxRetransmitTime = absl::nullopt;
52 }
53}
54
deadbeefab9b2d12015-10-14 11:33:11 -070055bool SctpSidAllocator::AllocateSid(rtc::SSLRole role, int* sid) {
56 int potential_sid = (role == rtc::SSL_CLIENT) ? 0 : 1;
57 while (!IsSidAvailable(potential_sid)) {
58 potential_sid += 2;
59 if (potential_sid > static_cast<int>(cricket::kMaxSctpSid)) {
60 return false;
61 }
62 }
63
64 *sid = potential_sid;
65 used_sids_.insert(potential_sid);
66 return true;
67}
68
69bool SctpSidAllocator::ReserveSid(int sid) {
70 if (!IsSidAvailable(sid)) {
71 return false;
72 }
73 used_sids_.insert(sid);
74 return true;
75}
76
77void SctpSidAllocator::ReleaseSid(int sid) {
78 auto it = used_sids_.find(sid);
79 if (it != used_sids_.end()) {
80 used_sids_.erase(it);
81 }
82}
83
84bool SctpSidAllocator::IsSidAvailable(int sid) const {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -070085 if (sid < static_cast<int>(cricket::kMinSctpSid) ||
86 sid > static_cast<int>(cricket::kMaxSctpSid)) {
deadbeefab9b2d12015-10-14 11:33:11 -070087 return false;
88 }
89 return used_sids_.find(sid) == used_sids_.end();
90}
91
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000092bool DataChannel::PacketQueue::Empty() const {
93 return packets_.empty();
94}
95
Steve Anton944c7552018-12-13 14:19:10 -080096std::unique_ptr<DataBuffer> DataChannel::PacketQueue::PopFront() {
97 RTC_DCHECK(!packets_.empty());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000098 byte_count_ -= packets_.front()->size();
Steve Anton944c7552018-12-13 14:19:10 -080099 std::unique_ptr<DataBuffer> packet = std::move(packets_.front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000100 packets_.pop_front();
Steve Anton944c7552018-12-13 14:19:10 -0800101 return packet;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000102}
103
Steve Anton944c7552018-12-13 14:19:10 -0800104void DataChannel::PacketQueue::PushFront(std::unique_ptr<DataBuffer> packet) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000105 byte_count_ += packet->size();
Steve Anton944c7552018-12-13 14:19:10 -0800106 packets_.push_front(std::move(packet));
107}
108
109void DataChannel::PacketQueue::PushBack(std::unique_ptr<DataBuffer> packet) {
110 byte_count_ += packet->size();
111 packets_.push_back(std::move(packet));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000112}
113
114void DataChannel::PacketQueue::Clear() {
Steve Anton944c7552018-12-13 14:19:10 -0800115 packets_.clear();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000116 byte_count_ = 0;
117}
118
119void DataChannel::PacketQueue::Swap(PacketQueue* other) {
120 size_t other_byte_count = other->byte_count_;
121 other->byte_count_ = byte_count_;
122 byte_count_ = other_byte_count;
123
124 other->packets_.swap(packets_);
125}
126
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000127rtc::scoped_refptr<DataChannel> DataChannel::Create(
wu@webrtc.org78187522013-10-07 23:32:02 +0000128 DataChannelProviderInterface* provider,
129 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000131 const InternalDataChannelInit& config) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000132 rtc::scoped_refptr<DataChannel> channel(
133 new rtc::RefCountedObject<DataChannel>(provider, dct, label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 if (!channel->Init(config)) {
135 return NULL;
136 }
137 return channel;
138}
139
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800140bool DataChannel::IsSctpLike(cricket::DataChannelType type) {
141 return type == cricket::DCT_SCTP || type == cricket::DCT_MEDIA_TRANSPORT;
142}
143
Yves Gerey665174f2018-06-19 15:03:05 +0200144DataChannel::DataChannel(DataChannelProviderInterface* provider,
145 cricket::DataChannelType dct,
146 const std::string& label)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 : label_(label),
hbos84ffdee2016-10-12 14:14:39 -0700148 observer_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 state_(kConnecting),
hbos84ffdee2016-10-12 14:14:39 -0700150 messages_sent_(0),
151 bytes_sent_(0),
152 messages_received_(0),
153 bytes_received_(0),
Marina Cioceae448a3f2019-03-04 15:52:21 +0100154 buffered_amount_(0),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000155 data_channel_type_(dct),
156 provider_(provider),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400157 handshake_state_(kHandshakeInit),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000158 connected_to_provider_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 send_ssrc_set_(false),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000160 receive_ssrc_set_(false),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400161 writable_(false),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000162 send_ssrc_(0),
Yves Gerey665174f2018-06-19 15:03:05 +0200163 receive_ssrc_(0) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000165bool DataChannel::Init(const InternalDataChannelInit& config) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400166 if (data_channel_type_ == cricket::DCT_RTP) {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200167 if (config.reliable || config.id != -1 || config.maxRetransmits ||
168 config.maxRetransmitTime) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100169 RTC_LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100170 "invalid DataChannelInit.";
Lally Singh5c6c6e02015-05-29 11:52:39 -0400171 return false;
172 }
173 handshake_state_ = kHandshakeReady;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800174 } else if (IsSctpLike(data_channel_type_)) {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200175 if (config.id < -1 ||
176 (config.maxRetransmits && *config.maxRetransmits < 0) ||
177 (config.maxRetransmitTime && *config.maxRetransmitTime < 0)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100178 RTC_LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100179 "invalid DataChannelInit.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 return false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000181 }
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200182 if (config.maxRetransmits && config.maxRetransmitTime) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100183 RTC_LOG(LS_ERROR)
184 << "maxRetransmits and maxRetransmitTime should not be both set.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000185 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000187 config_ = config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188
Lally Singh5c6c6e02015-05-29 11:52:39 -0400189 switch (config_.open_handshake_role) {
Yves Gerey665174f2018-06-19 15:03:05 +0200190 case webrtc::InternalDataChannelInit::kNone: // pre-negotiated
191 handshake_state_ = kHandshakeReady;
192 break;
193 case webrtc::InternalDataChannelInit::kOpener:
194 handshake_state_ = kHandshakeShouldSendOpen;
195 break;
196 case webrtc::InternalDataChannelInit::kAcker:
197 handshake_state_ = kHandshakeShouldSendAck;
198 break;
Steve Anton36b29d12017-10-30 09:57:42 -0700199 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400200
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000201 // Try to connect to the transport in case the transport channel already
202 // exists.
203 OnTransportChannelCreated();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000204
205 // Checks if the transport is ready to send because the initial channel
206 // ready signal may have been sent before the DataChannel creation.
207 // This has to be done async because the upper layer objects (e.g.
208 // Chrome glue and WebKit) are not wired up properly until after this
209 // function returns.
210 if (provider_->ReadyToSendData()) {
Steve Anton044a04d2018-08-31 13:51:19 -0700211 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, rtc::Thread::Current(),
212 [this] { OnChannelReady(true); });
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000213 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000214 }
215
216 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217}
218
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000219DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220
221void DataChannel::RegisterObserver(DataChannelObserver* observer) {
222 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000223 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224}
225
226void DataChannel::UnregisterObserver() {
227 observer_ = NULL;
228}
229
230bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02 +0000231 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 return false;
233 } else {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200234 return !config_.maxRetransmits && !config_.maxRetransmitTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235 }
236}
237
Peter Boström0c4e06b2015-10-07 12:23:21 +0200238uint64_t DataChannel::buffered_amount() const {
Marina Cioceae448a3f2019-03-04 15:52:21 +0100239 return buffered_amount_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240}
241
242void DataChannel::Close() {
243 if (state_ == kClosed)
244 return;
245 send_ssrc_ = 0;
246 send_ssrc_set_ = false;
247 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700248 // Will send queued data before beginning the underlying closing procedure.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 UpdateState();
250}
251
252bool DataChannel::Send(const DataBuffer& buffer) {
Marina Cioceae448a3f2019-03-04 15:52:21 +0100253 buffered_amount_ += buffer.size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 if (state_ != kOpen) {
255 return false;
256 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000257
258 // TODO(jiayl): the spec is unclear about if the remote side should get the
259 // onmessage event. We need to figure out the expected behavior and change the
260 // code accordingly.
261 if (buffer.size() == 0) {
262 return true;
263 }
264
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000265 // If the queue is non-empty, we're waiting for SignalReadyToSend,
266 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000267 if (!queued_send_data_.Empty()) {
268 // Only SCTP DataChannel queues the outgoing data when the transport is
269 // blocked.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800270 RTC_DCHECK(IsSctpLike(data_channel_type_));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000271 if (!QueueSendDataMessage(buffer)) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700272 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to queue "
273 "additional data.";
274 CloseAbruptly();
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000275 }
276 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000279 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000280 if (data_channel_type_ == cricket::DCT_RTP) {
281 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000282 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000283
284 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000285 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286}
287
Peter Boström0c4e06b2015-10-07 12:23:21 +0200288void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800289 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000290
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 return;
293 }
294 receive_ssrc_ = receive_ssrc;
295 receive_ssrc_set_ = true;
296 UpdateState();
297}
298
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000299void DataChannel::SetSctpSid(int sid) {
kwibergee89e782017-08-09 17:22:01 -0700300 RTC_DCHECK_LT(config_.id, 0);
301 RTC_DCHECK_GE(sid, 0);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800302 RTC_DCHECK(IsSctpLike(data_channel_type_));
deadbeefab9b2d12015-10-14 11:33:11 -0700303 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000304 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700305 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000306
307 config_.id = sid;
308 provider_->AddSctpDataStream(sid);
309}
310
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700311void DataChannel::OnClosingProcedureStartedRemotely(int sid) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800312 if (IsSctpLike(data_channel_type_) && sid == config_.id &&
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700313 state_ != kClosing && state_ != kClosed) {
314 // Don't bother sending queued data since the side that initiated the
315 // closure wouldn't receive it anyway. See crbug.com/559394 for a lengthy
316 // discussion about this.
317 queued_send_data_.Clear();
318 queued_control_data_.Clear();
319 // Just need to change state to kClosing, SctpTransport will handle the
320 // rest of the closing procedure and OnClosingProcedureComplete will be
321 // called later.
322 started_closing_procedure_ = true;
323 SetState(kClosing);
324 }
325}
326
327void DataChannel::OnClosingProcedureComplete(int sid) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800328 if (IsSctpLike(data_channel_type_) && sid == config_.id) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700329 // If the closing procedure is complete, we should have finished sending
330 // all pending data and transitioned to kClosing already.
331 RTC_DCHECK_EQ(state_, kClosing);
332 RTC_DCHECK(queued_send_data_.Empty());
333 DisconnectFromProvider();
334 SetState(kClosed);
335 }
336}
337
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000338void DataChannel::OnTransportChannelCreated() {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800339 RTC_DCHECK(IsSctpLike(data_channel_type_));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000340 if (!connected_to_provider_) {
341 connected_to_provider_ = provider_->ConnectDataChannel(this);
342 }
343 // The sid may have been unassigned when provider_->ConnectDataChannel was
344 // done. So always add the streams even if connected_to_provider_ is true.
345 if (config_.id >= 0) {
346 provider_->AddSctpDataStream(config_.id);
347 }
348}
349
deadbeefab9b2d12015-10-14 11:33:11 -0700350void DataChannel::OnTransportChannelDestroyed() {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700351 // The SctpTransport is going away (for example, because the SCTP m= section
352 // was rejected), so we need to close abruptly.
353 CloseAbruptly();
354}
355
356// The remote peer request that this channel shall be closed.
357void DataChannel::RemotePeerRequestClose() {
358 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
359 CloseAbruptly();
deadbeefab9b2d12015-10-14 11:33:11 -0700360}
361
Peter Boström0c4e06b2015-10-07 12:23:21 +0200362void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800363 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365 return;
366 }
367 send_ssrc_ = send_ssrc;
368 send_ssrc_set_ = true;
369 UpdateState();
370}
371
deadbeef953c2ce2017-01-09 14:53:41 -0800372void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700373 const rtc::CopyOnWriteBuffer& payload) {
deadbeef953c2ce2017-01-09 14:53:41 -0800374 if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) {
375 return;
376 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800377 if (IsSctpLike(data_channel_type_) && params.sid != config_.id) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000378 return;
379 }
380
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000381 if (params.type == cricket::DMT_CONTROL) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800382 RTC_DCHECK(IsSctpLike(data_channel_type_));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400383 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000384 // Ignore it if we are not expecting an ACK message.
Jonas Olsson45cc8902018-02-13 10:37:07 +0100385 RTC_LOG(LS_WARNING)
386 << "DataChannel received unexpected CONTROL message, sid = "
387 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000388 return;
389 }
390 if (ParseDataChannelOpenAckMessage(payload)) {
391 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400392 handshake_state_ = kHandshakeReady;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100393 RTC_LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
394 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000395 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100396 RTC_LOG(LS_WARNING)
397 << "DataChannel failed to parse OPEN_ACK message, sid = "
398 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000399 }
400 return;
401 }
402
nisseede5da42017-01-12 05:15:36 -0800403 RTC_DCHECK(params.type == cricket::DMT_BINARY ||
404 params.type == cricket::DMT_TEXT);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000405
Mirko Bonadei675513b2017-11-09 11:09:25 +0100406 RTC_LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = "
407 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000408 // We can send unordered as soon as we receive any DATA message since the
409 // remote side must have received the OPEN (and old clients do not send
410 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400411 if (handshake_state_ == kHandshakeWaitingForAck) {
412 handshake_state_ = kHandshakeReady;
413 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000414
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000415 bool binary = (params.type == cricket::DMT_BINARY);
Steve Anton944c7552018-12-13 14:19:10 -0800416 auto buffer = absl::make_unique<DataBuffer>(payload, binary);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400417 if (state_ == kOpen && observer_) {
hbos84ffdee2016-10-12 14:14:39 -0700418 ++messages_received_;
419 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000420 observer_->OnMessage(*buffer.get());
421 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000422 if (queued_received_data_.byte_count() + payload.size() >
423 kMaxQueuedReceivedDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100424 RTC_LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000425
426 queued_received_data_.Clear();
427 if (data_channel_type_ != cricket::DCT_RTP) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700428 CloseAbruptly();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000429 }
430
431 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000432 }
Steve Anton944c7552018-12-13 14:19:10 -0800433 queued_received_data_.PushBack(std::move(buffer));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000434 }
435}
436
437void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400438 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000439 if (!writable) {
440 return;
441 }
Harald Alvestrandb9bb3712019-03-27 07:23:31 +0000442
Lally Singh5c6c6e02015-05-29 11:52:39 -0400443 SendQueuedControlMessages();
444 SendQueuedDataMessages();
445 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000446}
447
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700448void DataChannel::CloseAbruptly() {
449 if (state_ == kClosed) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000450 return;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700451 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000452
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700453 if (connected_to_provider_) {
454 DisconnectFromProvider();
455 }
456
457 // Closing abruptly means any queued data gets thrown away.
458 queued_send_data_.Clear();
Marina Cioceae448a3f2019-03-04 15:52:21 +0100459 buffered_amount_ = 0;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700460 queued_control_data_.Clear();
461
462 // Still go to "kClosing" before "kClosed", since observers may be expecting
463 // that.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700465 SetState(kClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466}
467
468void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400469 // UpdateState determines what to do from a few state variables. Include
470 // all conditions required for each state transition here for
471 // clarity. OnChannelReady(true) will send any queued data and then invoke
472 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473 switch (state_) {
474 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000475 if (send_ssrc_set_ == receive_ssrc_set_) {
476 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
477 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400479 if (connected_to_provider_) {
480 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 06:15:43 -0700481 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400482 WriteDataChannelOpenMessage(label_, config_, &payload);
483 SendControlMessage(payload);
484 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 06:15:43 -0700485 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400486 WriteDataChannelOpenAckMessage(&payload);
487 SendControlMessage(payload);
488 }
Yves Gerey665174f2018-06-19 15:03:05 +0200489 if (writable_ && (handshake_state_ == kHandshakeReady ||
490 handshake_state_ == kHandshakeWaitingForAck)) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400491 SetState(kOpen);
492 // If we have received buffers before the channel got writable.
493 // Deliver them now.
494 DeliverQueuedReceivedData();
495 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 }
497 }
498 break;
499 }
500 case kOpen: {
501 break;
502 }
503 case kClosing: {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700504 // Wait for all queued data to be sent before beginning the closing
505 // procedure.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400506 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700507 if (data_channel_type_ == cricket::DCT_RTP) {
508 // For RTP data channels, we can go to "closed" after we finish
509 // sending data and the send/recv SSRCs are unset.
510 if (connected_to_provider_) {
511 DisconnectFromProvider();
512 }
513 if (!send_ssrc_set_ && !receive_ssrc_set_) {
514 SetState(kClosed);
515 }
516 } else {
517 // For SCTP data channels, we need to wait for the closing procedure
518 // to complete; after calling RemoveSctpDataStream,
519 // OnClosingProcedureComplete will end up called asynchronously
520 // afterwards.
521 if (connected_to_provider_ && !started_closing_procedure_ &&
522 config_.id >= 0) {
523 started_closing_procedure_ = true;
524 provider_->RemoveSctpDataStream(config_.id);
525 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400526 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 }
528 break;
529 }
530 case kClosed:
531 break;
532 }
533}
534
535void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700536 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000537 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700538 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000539
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 state_ = state;
541 if (observer_) {
542 observer_->OnStateChange();
543 }
hbos82ebe022016-11-14 01:41:09 -0800544 if (state_ == kOpen) {
545 SignalOpened(this);
546 } else if (state_ == kClosed) {
deadbeefab9b2d12015-10-14 11:33:11 -0700547 SignalClosed(this);
548 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549}
550
Lally Singh5c6c6e02015-05-29 11:52:39 -0400551void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000552 if (!connected_to_provider_)
553 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554
wu@webrtc.org78187522013-10-07 23:32:02 +0000555 provider_->DisconnectDataChannel(this);
556 connected_to_provider_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557}
558
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000559void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400560 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000561 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000564 while (!queued_received_data_.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800565 std::unique_ptr<DataBuffer> buffer = queued_received_data_.PopFront();
hbos84ffdee2016-10-12 14:14:39 -0700566 ++messages_received_;
567 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000568 observer_->OnMessage(*buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 }
570}
571
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000572void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400573 if (queued_send_data_.Empty()) {
574 return;
575 }
576
nisseede5da42017-01-12 05:15:36 -0800577 RTC_DCHECK(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000578
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000579 while (!queued_send_data_.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800580 std::unique_ptr<DataBuffer> buffer = queued_send_data_.PopFront();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000581 if (!SendDataMessage(*buffer, false)) {
Steve Anton944c7552018-12-13 14:19:10 -0800582 // Return the message to the front of the queue if sending is aborted.
583 queued_send_data_.PushFront(std::move(buffer));
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000584 break;
585 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000586 }
587}
588
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000589bool DataChannel::SendDataMessage(const DataBuffer& buffer,
590 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000591 cricket::SendDataParams send_params;
592
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800593 if (IsSctpLike(data_channel_type_)) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000594 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400595 // Send as ordered if it is still going through OPEN/ACK signaling.
596 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000597 send_params.ordered = true;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100598 RTC_LOG(LS_VERBOSE)
599 << "Sending data as ordered for unordered DataChannel "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100600 "because the OPEN_ACK message has not been received.";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000601 }
602
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200603 send_params.max_rtx_count =
604 config_.maxRetransmits ? *config_.maxRetransmits : -1;
605 send_params.max_rtx_ms =
606 config_.maxRetransmitTime ? *config_.maxRetransmitTime : -1;
deadbeef953c2ce2017-01-09 14:53:41 -0800607 send_params.sid = config_.id;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000608 } else {
609 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000610 }
611 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
612
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000613 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
614 bool success = provider_->SendData(send_params, buffer.data, &send_result);
615
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000616 if (success) {
hbos84ffdee2016-10-12 14:14:39 -0700617 ++messages_sent_;
618 bytes_sent_ += buffer.size();
Marina Cioceae448a3f2019-03-04 15:52:21 +0100619
620 RTC_DCHECK(buffered_amount_ >= buffer.size());
621 buffered_amount_ -= buffer.size();
622 if (observer_ && buffer.size() > 0) {
623 observer_->OnBufferedAmountChange(buffer.size());
624 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000625 return true;
626 }
627
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800628 if (!IsSctpLike(data_channel_type_)) {
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000629 return false;
630 }
631
632 if (send_result == cricket::SDR_BLOCK) {
633 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
634 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000635 }
636 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000637 // Close the channel if the error is not SDR_BLOCK, or if queuing the
638 // message failed.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100639 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100640 "send_result = "
641 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700642 CloseAbruptly();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000643
644 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000645}
646
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000647bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
Marina Cioceae448a3f2019-03-04 15:52:21 +0100648 size_t start_buffered_amount = queued_send_data_.byte_count();
649 if (start_buffered_amount + buffer.size() > kMaxQueuedSendDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100650 RTC_LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000651 return false;
652 }
Steve Anton944c7552018-12-13 14:19:10 -0800653 queued_send_data_.PushBack(absl::make_unique<DataBuffer>(buffer));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000654 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655}
656
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000657void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000658 PacketQueue control_packets;
659 control_packets.Swap(&queued_control_data_);
660
661 while (!control_packets.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800662 std::unique_ptr<DataBuffer> buf = control_packets.PopFront();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000663 SendControlMessage(buf->data);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000664 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000665}
666
jbaucheec21bd2016-03-20 06:15:43 -0700667void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Steve Anton944c7552018-12-13 14:19:10 -0800668 queued_control_data_.PushBack(absl::make_unique<DataBuffer>(buffer, true));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000669}
670
jbaucheec21bd2016-03-20 06:15:43 -0700671bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400672 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000673
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800674 RTC_DCHECK(IsSctpLike(data_channel_type_));
kwibergee89e782017-08-09 17:22:01 -0700675 RTC_DCHECK(writable_);
676 RTC_DCHECK_GE(config_.id, 0);
677 RTC_DCHECK(!is_open_message || !config_.negotiated);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000678
679 cricket::SendDataParams send_params;
deadbeef953c2ce2017-01-09 14:53:41 -0800680 send_params.sid = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400681 // Send data as ordered before we receive any message from the remote peer to
682 // make sure the remote peer will not receive any data before it receives the
683 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000684 send_params.ordered = config_.ordered || is_open_message;
685 send_params.type = cricket::DMT_CONTROL;
686
687 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
688 bool retval = provider_->SendData(send_params, buffer, &send_result);
689 if (retval) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100690 RTC_LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000691
Lally Singh5c6c6e02015-05-29 11:52:39 -0400692 if (handshake_state_ == kHandshakeShouldSendAck) {
693 handshake_state_ = kHandshakeReady;
694 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
695 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000696 }
697 } else if (send_result == cricket::SDR_BLOCK) {
698 QueueControlMessage(buffer);
699 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100700 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
Jonas Olsson45cc8902018-02-13 10:37:07 +0100701 " the CONTROL message, send_result = "
702 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700703 CloseAbruptly();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000704 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000705 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000706}
707
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708} // namespace webrtc