blob: a554829e4f2e556efd06bebb6332319ecd78120c [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 Alvestrand77c442c2019-03-25 16:47:02 +0100417 // If the datachannel has not been assigned an ID, ignore update.
418 if (id() < 0) {
419 return;
420 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400421 SendQueuedControlMessages();
422 SendQueuedDataMessages();
423 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000424}
425
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700426void DataChannel::CloseAbruptly() {
427 if (state_ == kClosed) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000428 return;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700429 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000430
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700431 if (connected_to_provider_) {
432 DisconnectFromProvider();
433 }
434
435 // Closing abruptly means any queued data gets thrown away.
436 queued_send_data_.Clear();
Marina Cioceae448a3f2019-03-04 15:52:21 +0100437 buffered_amount_ = 0;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700438 queued_control_data_.Clear();
439
440 // Still go to "kClosing" before "kClosed", since observers may be expecting
441 // that.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700443 SetState(kClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444}
445
446void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400447 // UpdateState determines what to do from a few state variables. Include
448 // all conditions required for each state transition here for
449 // clarity. OnChannelReady(true) will send any queued data and then invoke
450 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 switch (state_) {
452 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000453 if (send_ssrc_set_ == receive_ssrc_set_) {
454 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
455 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400457 if (connected_to_provider_) {
458 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 06:15:43 -0700459 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400460 WriteDataChannelOpenMessage(label_, config_, &payload);
461 SendControlMessage(payload);
462 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 06:15:43 -0700463 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400464 WriteDataChannelOpenAckMessage(&payload);
465 SendControlMessage(payload);
466 }
Yves Gerey665174f2018-06-19 15:03:05 +0200467 if (writable_ && (handshake_state_ == kHandshakeReady ||
468 handshake_state_ == kHandshakeWaitingForAck)) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400469 SetState(kOpen);
470 // If we have received buffers before the channel got writable.
471 // Deliver them now.
472 DeliverQueuedReceivedData();
473 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 }
475 }
476 break;
477 }
478 case kOpen: {
479 break;
480 }
481 case kClosing: {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700482 // Wait for all queued data to be sent before beginning the closing
483 // procedure.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400484 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700485 if (data_channel_type_ == cricket::DCT_RTP) {
486 // For RTP data channels, we can go to "closed" after we finish
487 // sending data and the send/recv SSRCs are unset.
488 if (connected_to_provider_) {
489 DisconnectFromProvider();
490 }
491 if (!send_ssrc_set_ && !receive_ssrc_set_) {
492 SetState(kClosed);
493 }
494 } else {
495 // For SCTP data channels, we need to wait for the closing procedure
496 // to complete; after calling RemoveSctpDataStream,
497 // OnClosingProcedureComplete will end up called asynchronously
498 // afterwards.
499 if (connected_to_provider_ && !started_closing_procedure_ &&
500 config_.id >= 0) {
501 started_closing_procedure_ = true;
502 provider_->RemoveSctpDataStream(config_.id);
503 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400504 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 }
506 break;
507 }
508 case kClosed:
509 break;
510 }
511}
512
513void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700514 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000515 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700516 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000517
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 state_ = state;
519 if (observer_) {
520 observer_->OnStateChange();
521 }
hbos82ebe022016-11-14 01:41:09 -0800522 if (state_ == kOpen) {
523 SignalOpened(this);
524 } else if (state_ == kClosed) {
deadbeefab9b2d12015-10-14 11:33:11 -0700525 SignalClosed(this);
526 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527}
528
Lally Singh5c6c6e02015-05-29 11:52:39 -0400529void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000530 if (!connected_to_provider_)
531 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532
wu@webrtc.org78187522013-10-07 23:32:02 +0000533 provider_->DisconnectDataChannel(this);
534 connected_to_provider_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535}
536
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000537void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400538 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000539 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000542 while (!queued_received_data_.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800543 std::unique_ptr<DataBuffer> buffer = queued_received_data_.PopFront();
hbos84ffdee2016-10-12 14:14:39 -0700544 ++messages_received_;
545 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000546 observer_->OnMessage(*buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 }
548}
549
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000550void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400551 if (queued_send_data_.Empty()) {
552 return;
553 }
554
nisseede5da42017-01-12 05:15:36 -0800555 RTC_DCHECK(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000556
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000557 while (!queued_send_data_.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800558 std::unique_ptr<DataBuffer> buffer = queued_send_data_.PopFront();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000559 if (!SendDataMessage(*buffer, false)) {
Steve Anton944c7552018-12-13 14:19:10 -0800560 // Return the message to the front of the queue if sending is aborted.
561 queued_send_data_.PushFront(std::move(buffer));
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000562 break;
563 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000564 }
565}
566
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000567bool DataChannel::SendDataMessage(const DataBuffer& buffer,
568 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000569 cricket::SendDataParams send_params;
570
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800571 if (IsSctpLike(data_channel_type_)) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000572 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400573 // Send as ordered if it is still going through OPEN/ACK signaling.
574 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000575 send_params.ordered = true;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100576 RTC_LOG(LS_VERBOSE)
577 << "Sending data as ordered for unordered DataChannel "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100578 "because the OPEN_ACK message has not been received.";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000579 }
580
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000581 send_params.max_rtx_count = config_.maxRetransmits;
582 send_params.max_rtx_ms = config_.maxRetransmitTime;
deadbeef953c2ce2017-01-09 14:53:41 -0800583 send_params.sid = config_.id;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000584 } else {
585 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000586 }
587 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
588
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000589 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
590 bool success = provider_->SendData(send_params, buffer.data, &send_result);
591
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000592 if (success) {
hbos84ffdee2016-10-12 14:14:39 -0700593 ++messages_sent_;
594 bytes_sent_ += buffer.size();
Marina Cioceae448a3f2019-03-04 15:52:21 +0100595
596 RTC_DCHECK(buffered_amount_ >= buffer.size());
597 buffered_amount_ -= buffer.size();
598 if (observer_ && buffer.size() > 0) {
599 observer_->OnBufferedAmountChange(buffer.size());
600 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000601 return true;
602 }
603
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800604 if (!IsSctpLike(data_channel_type_)) {
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000605 return false;
606 }
607
608 if (send_result == cricket::SDR_BLOCK) {
609 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
610 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000611 }
612 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000613 // Close the channel if the error is not SDR_BLOCK, or if queuing the
614 // message failed.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100615 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100616 "send_result = "
617 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700618 CloseAbruptly();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000619
620 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000621}
622
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000623bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
Marina Cioceae448a3f2019-03-04 15:52:21 +0100624 size_t start_buffered_amount = queued_send_data_.byte_count();
625 if (start_buffered_amount + buffer.size() > kMaxQueuedSendDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100626 RTC_LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000627 return false;
628 }
Steve Anton944c7552018-12-13 14:19:10 -0800629 queued_send_data_.PushBack(absl::make_unique<DataBuffer>(buffer));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000630 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631}
632
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000633void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000634 PacketQueue control_packets;
635 control_packets.Swap(&queued_control_data_);
636
637 while (!control_packets.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800638 std::unique_ptr<DataBuffer> buf = control_packets.PopFront();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000639 SendControlMessage(buf->data);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000640 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000641}
642
jbaucheec21bd2016-03-20 06:15:43 -0700643void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Steve Anton944c7552018-12-13 14:19:10 -0800644 queued_control_data_.PushBack(absl::make_unique<DataBuffer>(buffer, true));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000645}
646
jbaucheec21bd2016-03-20 06:15:43 -0700647bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400648 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000649
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800650 RTC_DCHECK(IsSctpLike(data_channel_type_));
kwibergee89e782017-08-09 17:22:01 -0700651 RTC_DCHECK(writable_);
652 RTC_DCHECK_GE(config_.id, 0);
653 RTC_DCHECK(!is_open_message || !config_.negotiated);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000654
655 cricket::SendDataParams send_params;
deadbeef953c2ce2017-01-09 14:53:41 -0800656 send_params.sid = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400657 // Send data as ordered before we receive any message from the remote peer to
658 // make sure the remote peer will not receive any data before it receives the
659 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000660 send_params.ordered = config_.ordered || is_open_message;
661 send_params.type = cricket::DMT_CONTROL;
662
663 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
664 bool retval = provider_->SendData(send_params, buffer, &send_result);
665 if (retval) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100666 RTC_LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000667
Lally Singh5c6c6e02015-05-29 11:52:39 -0400668 if (handshake_state_ == kHandshakeShouldSendAck) {
669 handshake_state_ = kHandshakeReady;
670 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
671 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000672 }
673 } else if (send_result == cricket::SDR_BLOCK) {
674 QueueControlMessage(buffer);
675 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100676 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
Jonas Olsson45cc8902018-02-13 10:37:07 +0100677 " the CONTROL message, send_result = "
678 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700679 CloseAbruptly();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000680 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000681 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000682}
683
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684} // namespace webrtc