blob: e4727f25de919fbb21ba3358e0116c2d4918674d [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
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 +000068bool DataChannel::PacketQueue::Empty() const {
69 return packets_.empty();
70}
71
Steve Anton944c7552018-12-13 14:19:10 -080072std::unique_ptr<DataBuffer> DataChannel::PacketQueue::PopFront() {
73 RTC_DCHECK(!packets_.empty());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000074 byte_count_ -= packets_.front()->size();
Steve Anton944c7552018-12-13 14:19:10 -080075 std::unique_ptr<DataBuffer> packet = std::move(packets_.front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000076 packets_.pop_front();
Steve Anton944c7552018-12-13 14:19:10 -080077 return packet;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000078}
79
Steve Anton944c7552018-12-13 14:19:10 -080080void DataChannel::PacketQueue::PushFront(std::unique_ptr<DataBuffer> packet) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000081 byte_count_ += packet->size();
Steve Anton944c7552018-12-13 14:19:10 -080082 packets_.push_front(std::move(packet));
83}
84
85void DataChannel::PacketQueue::PushBack(std::unique_ptr<DataBuffer> packet) {
86 byte_count_ += packet->size();
87 packets_.push_back(std::move(packet));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000088}
89
90void DataChannel::PacketQueue::Clear() {
Steve Anton944c7552018-12-13 14:19:10 -080091 packets_.clear();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000092 byte_count_ = 0;
93}
94
95void DataChannel::PacketQueue::Swap(PacketQueue* other) {
96 size_t other_byte_count = other->byte_count_;
97 other->byte_count_ = byte_count_;
98 byte_count_ = other_byte_count;
99
100 other->packets_.swap(packets_);
101}
102
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000103rtc::scoped_refptr<DataChannel> DataChannel::Create(
wu@webrtc.org78187522013-10-07 23:32:02 +0000104 DataChannelProviderInterface* provider,
105 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000107 const InternalDataChannelInit& config) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000108 rtc::scoped_refptr<DataChannel> channel(
109 new rtc::RefCountedObject<DataChannel>(provider, dct, label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 if (!channel->Init(config)) {
111 return NULL;
112 }
113 return channel;
114}
115
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800116bool DataChannel::IsSctpLike(cricket::DataChannelType type) {
117 return type == cricket::DCT_SCTP || type == cricket::DCT_MEDIA_TRANSPORT;
118}
119
Yves Gerey665174f2018-06-19 15:03:05 +0200120DataChannel::DataChannel(DataChannelProviderInterface* provider,
121 cricket::DataChannelType dct,
122 const std::string& label)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 : label_(label),
hbos84ffdee2016-10-12 14:14:39 -0700124 observer_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 state_(kConnecting),
hbos84ffdee2016-10-12 14:14:39 -0700126 messages_sent_(0),
127 bytes_sent_(0),
128 messages_received_(0),
129 bytes_received_(0),
Marina Cioceae448a3f2019-03-04 15:52:21 +0100130 buffered_amount_(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;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800150 } else if (IsSctpLike(data_channel_type_)) {
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 {
Marina Cioceae448a3f2019-03-04 15:52:21 +0100214 return buffered_amount_;
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) {
Marina Cioceae448a3f2019-03-04 15:52:21 +0100228 buffered_amount_ += buffer.size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 if (state_ != kOpen) {
230 return false;
231 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000232
233 // TODO(jiayl): the spec is unclear about if the remote side should get the
234 // onmessage event. We need to figure out the expected behavior and change the
235 // code accordingly.
236 if (buffer.size() == 0) {
237 return true;
238 }
239
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000240 // If the queue is non-empty, we're waiting for SignalReadyToSend,
241 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000242 if (!queued_send_data_.Empty()) {
243 // Only SCTP DataChannel queues the outgoing data when the transport is
244 // blocked.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800245 RTC_DCHECK(IsSctpLike(data_channel_type_));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000246 if (!QueueSendDataMessage(buffer)) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700247 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to queue "
248 "additional data.";
249 CloseAbruptly();
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000250 }
251 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000254 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000255 if (data_channel_type_ == cricket::DCT_RTP) {
256 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000257 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000258
259 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000260 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261}
262
Peter Boström0c4e06b2015-10-07 12:23:21 +0200263void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800264 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000265
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 return;
268 }
269 receive_ssrc_ = receive_ssrc;
270 receive_ssrc_set_ = true;
271 UpdateState();
272}
273
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000274void DataChannel::SetSctpSid(int sid) {
kwibergee89e782017-08-09 17:22:01 -0700275 RTC_DCHECK_LT(config_.id, 0);
276 RTC_DCHECK_GE(sid, 0);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800277 RTC_DCHECK(IsSctpLike(data_channel_type_));
deadbeefab9b2d12015-10-14 11:33:11 -0700278 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000279 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700280 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000281
282 config_.id = sid;
283 provider_->AddSctpDataStream(sid);
284}
285
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700286void DataChannel::OnClosingProcedureStartedRemotely(int sid) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800287 if (IsSctpLike(data_channel_type_) && sid == config_.id &&
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700288 state_ != kClosing && state_ != kClosed) {
289 // Don't bother sending queued data since the side that initiated the
290 // closure wouldn't receive it anyway. See crbug.com/559394 for a lengthy
291 // discussion about this.
292 queued_send_data_.Clear();
293 queued_control_data_.Clear();
294 // Just need to change state to kClosing, SctpTransport will handle the
295 // rest of the closing procedure and OnClosingProcedureComplete will be
296 // called later.
297 started_closing_procedure_ = true;
298 SetState(kClosing);
299 }
300}
301
302void DataChannel::OnClosingProcedureComplete(int sid) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800303 if (IsSctpLike(data_channel_type_) && sid == config_.id) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700304 // If the closing procedure is complete, we should have finished sending
305 // all pending data and transitioned to kClosing already.
306 RTC_DCHECK_EQ(state_, kClosing);
307 RTC_DCHECK(queued_send_data_.Empty());
308 DisconnectFromProvider();
309 SetState(kClosed);
310 }
311}
312
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000313void DataChannel::OnTransportChannelCreated() {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800314 RTC_DCHECK(IsSctpLike(data_channel_type_));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000315 if (!connected_to_provider_) {
316 connected_to_provider_ = provider_->ConnectDataChannel(this);
317 }
318 // The sid may have been unassigned when provider_->ConnectDataChannel was
319 // done. So always add the streams even if connected_to_provider_ is true.
320 if (config_.id >= 0) {
321 provider_->AddSctpDataStream(config_.id);
322 }
323}
324
deadbeefab9b2d12015-10-14 11:33:11 -0700325void DataChannel::OnTransportChannelDestroyed() {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700326 // The SctpTransport is going away (for example, because the SCTP m= section
327 // was rejected), so we need to close abruptly.
328 CloseAbruptly();
329}
330
331// The remote peer request that this channel shall be closed.
332void DataChannel::RemotePeerRequestClose() {
333 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
334 CloseAbruptly();
deadbeefab9b2d12015-10-14 11:33:11 -0700335}
336
Peter Boström0c4e06b2015-10-07 12:23:21 +0200337void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800338 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 return;
341 }
342 send_ssrc_ = send_ssrc;
343 send_ssrc_set_ = true;
344 UpdateState();
345}
346
deadbeef953c2ce2017-01-09 14:53:41 -0800347void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700348 const rtc::CopyOnWriteBuffer& payload) {
deadbeef953c2ce2017-01-09 14:53:41 -0800349 if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) {
350 return;
351 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800352 if (IsSctpLike(data_channel_type_) && params.sid != config_.id) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000353 return;
354 }
355
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000356 if (params.type == cricket::DMT_CONTROL) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800357 RTC_DCHECK(IsSctpLike(data_channel_type_));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400358 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000359 // Ignore it if we are not expecting an ACK message.
Jonas Olsson45cc8902018-02-13 10:37:07 +0100360 RTC_LOG(LS_WARNING)
361 << "DataChannel received unexpected CONTROL message, sid = "
362 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000363 return;
364 }
365 if (ParseDataChannelOpenAckMessage(payload)) {
366 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400367 handshake_state_ = kHandshakeReady;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100368 RTC_LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
369 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000370 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100371 RTC_LOG(LS_WARNING)
372 << "DataChannel failed to parse OPEN_ACK message, sid = "
373 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000374 }
375 return;
376 }
377
nisseede5da42017-01-12 05:15:36 -0800378 RTC_DCHECK(params.type == cricket::DMT_BINARY ||
379 params.type == cricket::DMT_TEXT);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000380
Mirko Bonadei675513b2017-11-09 11:09:25 +0100381 RTC_LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = "
382 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000383 // We can send unordered as soon as we receive any DATA message since the
384 // remote side must have received the OPEN (and old clients do not send
385 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400386 if (handshake_state_ == kHandshakeWaitingForAck) {
387 handshake_state_ = kHandshakeReady;
388 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000389
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000390 bool binary = (params.type == cricket::DMT_BINARY);
Steve Anton944c7552018-12-13 14:19:10 -0800391 auto buffer = absl::make_unique<DataBuffer>(payload, binary);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400392 if (state_ == kOpen && observer_) {
hbos84ffdee2016-10-12 14:14:39 -0700393 ++messages_received_;
394 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000395 observer_->OnMessage(*buffer.get());
396 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000397 if (queued_received_data_.byte_count() + payload.size() >
398 kMaxQueuedReceivedDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100399 RTC_LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000400
401 queued_received_data_.Clear();
402 if (data_channel_type_ != cricket::DCT_RTP) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700403 CloseAbruptly();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000404 }
405
406 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000407 }
Steve Anton944c7552018-12-13 14:19:10 -0800408 queued_received_data_.PushBack(std::move(buffer));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000409 }
410}
411
412void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400413 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000414 if (!writable) {
415 return;
416 }
Harald Alvestrandb9bb3712019-03-27 07:23:31 +0000417
Lally Singh5c6c6e02015-05-29 11:52:39 -0400418 SendQueuedControlMessages();
419 SendQueuedDataMessages();
420 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000421}
422
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700423void DataChannel::CloseAbruptly() {
424 if (state_ == kClosed) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000425 return;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700426 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000427
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700428 if (connected_to_provider_) {
429 DisconnectFromProvider();
430 }
431
432 // Closing abruptly means any queued data gets thrown away.
433 queued_send_data_.Clear();
Marina Cioceae448a3f2019-03-04 15:52:21 +0100434 buffered_amount_ = 0;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700435 queued_control_data_.Clear();
436
437 // Still go to "kClosing" before "kClosed", since observers may be expecting
438 // that.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700440 SetState(kClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441}
442
443void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400444 // UpdateState determines what to do from a few state variables. Include
445 // all conditions required for each state transition here for
446 // clarity. OnChannelReady(true) will send any queued data and then invoke
447 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 switch (state_) {
449 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000450 if (send_ssrc_set_ == receive_ssrc_set_) {
451 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
452 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400454 if (connected_to_provider_) {
455 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 06:15:43 -0700456 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400457 WriteDataChannelOpenMessage(label_, config_, &payload);
458 SendControlMessage(payload);
459 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 06:15:43 -0700460 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400461 WriteDataChannelOpenAckMessage(&payload);
462 SendControlMessage(payload);
463 }
Yves Gerey665174f2018-06-19 15:03:05 +0200464 if (writable_ && (handshake_state_ == kHandshakeReady ||
465 handshake_state_ == kHandshakeWaitingForAck)) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400466 SetState(kOpen);
467 // If we have received buffers before the channel got writable.
468 // Deliver them now.
469 DeliverQueuedReceivedData();
470 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471 }
472 }
473 break;
474 }
475 case kOpen: {
476 break;
477 }
478 case kClosing: {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700479 // Wait for all queued data to be sent before beginning the closing
480 // procedure.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400481 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700482 if (data_channel_type_ == cricket::DCT_RTP) {
483 // For RTP data channels, we can go to "closed" after we finish
484 // sending data and the send/recv SSRCs are unset.
485 if (connected_to_provider_) {
486 DisconnectFromProvider();
487 }
488 if (!send_ssrc_set_ && !receive_ssrc_set_) {
489 SetState(kClosed);
490 }
491 } else {
492 // For SCTP data channels, we need to wait for the closing procedure
493 // to complete; after calling RemoveSctpDataStream,
494 // OnClosingProcedureComplete will end up called asynchronously
495 // afterwards.
496 if (connected_to_provider_ && !started_closing_procedure_ &&
497 config_.id >= 0) {
498 started_closing_procedure_ = true;
499 provider_->RemoveSctpDataStream(config_.id);
500 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400501 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 }
503 break;
504 }
505 case kClosed:
506 break;
507 }
508}
509
510void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700511 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000512 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700513 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000514
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 state_ = state;
516 if (observer_) {
517 observer_->OnStateChange();
518 }
hbos82ebe022016-11-14 01:41:09 -0800519 if (state_ == kOpen) {
520 SignalOpened(this);
521 } else if (state_ == kClosed) {
deadbeefab9b2d12015-10-14 11:33:11 -0700522 SignalClosed(this);
523 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524}
525
Lally Singh5c6c6e02015-05-29 11:52:39 -0400526void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000527 if (!connected_to_provider_)
528 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529
wu@webrtc.org78187522013-10-07 23:32:02 +0000530 provider_->DisconnectDataChannel(this);
531 connected_to_provider_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532}
533
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000534void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400535 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000536 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000539 while (!queued_received_data_.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800540 std::unique_ptr<DataBuffer> buffer = queued_received_data_.PopFront();
hbos84ffdee2016-10-12 14:14:39 -0700541 ++messages_received_;
542 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000543 observer_->OnMessage(*buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 }
545}
546
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000547void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400548 if (queued_send_data_.Empty()) {
549 return;
550 }
551
nisseede5da42017-01-12 05:15:36 -0800552 RTC_DCHECK(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000553
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000554 while (!queued_send_data_.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800555 std::unique_ptr<DataBuffer> buffer = queued_send_data_.PopFront();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000556 if (!SendDataMessage(*buffer, false)) {
Steve Anton944c7552018-12-13 14:19:10 -0800557 // Return the message to the front of the queue if sending is aborted.
558 queued_send_data_.PushFront(std::move(buffer));
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000559 break;
560 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000561 }
562}
563
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000564bool DataChannel::SendDataMessage(const DataBuffer& buffer,
565 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000566 cricket::SendDataParams send_params;
567
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800568 if (IsSctpLike(data_channel_type_)) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000569 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400570 // Send as ordered if it is still going through OPEN/ACK signaling.
571 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000572 send_params.ordered = true;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100573 RTC_LOG(LS_VERBOSE)
574 << "Sending data as ordered for unordered DataChannel "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100575 "because the OPEN_ACK message has not been received.";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000576 }
577
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000578 send_params.max_rtx_count = config_.maxRetransmits;
579 send_params.max_rtx_ms = config_.maxRetransmitTime;
deadbeef953c2ce2017-01-09 14:53:41 -0800580 send_params.sid = config_.id;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000581 } else {
582 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000583 }
584 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
585
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000586 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
587 bool success = provider_->SendData(send_params, buffer.data, &send_result);
588
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000589 if (success) {
hbos84ffdee2016-10-12 14:14:39 -0700590 ++messages_sent_;
591 bytes_sent_ += buffer.size();
Marina Cioceae448a3f2019-03-04 15:52:21 +0100592
593 RTC_DCHECK(buffered_amount_ >= buffer.size());
594 buffered_amount_ -= buffer.size();
595 if (observer_ && buffer.size() > 0) {
596 observer_->OnBufferedAmountChange(buffer.size());
597 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000598 return true;
599 }
600
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800601 if (!IsSctpLike(data_channel_type_)) {
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000602 return false;
603 }
604
605 if (send_result == cricket::SDR_BLOCK) {
606 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
607 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000608 }
609 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000610 // Close the channel if the error is not SDR_BLOCK, or if queuing the
611 // message failed.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100612 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100613 "send_result = "
614 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700615 CloseAbruptly();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000616
617 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000618}
619
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000620bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
Marina Cioceae448a3f2019-03-04 15:52:21 +0100621 size_t start_buffered_amount = queued_send_data_.byte_count();
622 if (start_buffered_amount + buffer.size() > kMaxQueuedSendDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100623 RTC_LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000624 return false;
625 }
Steve Anton944c7552018-12-13 14:19:10 -0800626 queued_send_data_.PushBack(absl::make_unique<DataBuffer>(buffer));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000627 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628}
629
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000630void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000631 PacketQueue control_packets;
632 control_packets.Swap(&queued_control_data_);
633
634 while (!control_packets.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800635 std::unique_ptr<DataBuffer> buf = control_packets.PopFront();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000636 SendControlMessage(buf->data);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000637 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000638}
639
jbaucheec21bd2016-03-20 06:15:43 -0700640void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Steve Anton944c7552018-12-13 14:19:10 -0800641 queued_control_data_.PushBack(absl::make_unique<DataBuffer>(buffer, true));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000642}
643
jbaucheec21bd2016-03-20 06:15:43 -0700644bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400645 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000646
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800647 RTC_DCHECK(IsSctpLike(data_channel_type_));
kwibergee89e782017-08-09 17:22:01 -0700648 RTC_DCHECK(writable_);
649 RTC_DCHECK_GE(config_.id, 0);
650 RTC_DCHECK(!is_open_message || !config_.negotiated);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000651
652 cricket::SendDataParams send_params;
deadbeef953c2ce2017-01-09 14:53:41 -0800653 send_params.sid = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400654 // Send data as ordered before we receive any message from the remote peer to
655 // make sure the remote peer will not receive any data before it receives the
656 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000657 send_params.ordered = config_.ordered || is_open_message;
658 send_params.type = cricket::DMT_CONTROL;
659
660 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
661 bool retval = provider_->SendData(send_params, buffer, &send_result);
662 if (retval) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100663 RTC_LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000664
Lally Singh5c6c6e02015-05-29 11:52:39 -0400665 if (handshake_state_ == kHandshakeShouldSendAck) {
666 handshake_state_ = kHandshakeReady;
667 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
668 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000669 }
670 } else if (send_result == cricket::SDR_BLOCK) {
671 QueueControlMessage(buffer);
672 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100673 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
Jonas Olsson45cc8902018-02-13 10:37:07 +0100674 " the CONTROL message, send_result = "
675 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700676 CloseAbruptly();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000677 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000678 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000679}
680
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681} // namespace webrtc