blob: f854defd1b67f828994d576b737501485c811f53 [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),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000130 data_channel_type_(dct),
131 provider_(provider),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400132 handshake_state_(kHandshakeInit),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000133 connected_to_provider_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 send_ssrc_set_(false),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000135 receive_ssrc_set_(false),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400136 writable_(false),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000137 send_ssrc_(0),
Yves Gerey665174f2018-06-19 15:03:05 +0200138 receive_ssrc_(0) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000140bool DataChannel::Init(const InternalDataChannelInit& config) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400141 if (data_channel_type_ == cricket::DCT_RTP) {
Yves Gerey665174f2018-06-19 15:03:05 +0200142 if (config.reliable || config.id != -1 || config.maxRetransmits != -1 ||
Lally Singh5c6c6e02015-05-29 11:52:39 -0400143 config.maxRetransmitTime != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100144 RTC_LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100145 "invalid DataChannelInit.";
Lally Singh5c6c6e02015-05-29 11:52:39 -0400146 return false;
147 }
148 handshake_state_ = kHandshakeReady;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800149 } else if (IsSctpLike(data_channel_type_)) {
Yves Gerey665174f2018-06-19 15:03:05 +0200150 if (config.id < -1 || config.maxRetransmits < -1 ||
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000151 config.maxRetransmitTime < -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100152 RTC_LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100153 "invalid DataChannelInit.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 return false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000155 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000156 if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100157 RTC_LOG(LS_ERROR)
158 << "maxRetransmits and maxRetransmitTime should not be both set.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000159 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000161 config_ = config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162
Lally Singh5c6c6e02015-05-29 11:52:39 -0400163 switch (config_.open_handshake_role) {
Yves Gerey665174f2018-06-19 15:03:05 +0200164 case webrtc::InternalDataChannelInit::kNone: // pre-negotiated
165 handshake_state_ = kHandshakeReady;
166 break;
167 case webrtc::InternalDataChannelInit::kOpener:
168 handshake_state_ = kHandshakeShouldSendOpen;
169 break;
170 case webrtc::InternalDataChannelInit::kAcker:
171 handshake_state_ = kHandshakeShouldSendAck;
172 break;
Steve Anton36b29d12017-10-30 09:57:42 -0700173 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400174
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000175 // Try to connect to the transport in case the transport channel already
176 // exists.
177 OnTransportChannelCreated();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000178
179 // Checks if the transport is ready to send because the initial channel
180 // ready signal may have been sent before the DataChannel creation.
181 // This has to be done async because the upper layer objects (e.g.
182 // Chrome glue and WebKit) are not wired up properly until after this
183 // function returns.
184 if (provider_->ReadyToSendData()) {
Steve Anton044a04d2018-08-31 13:51:19 -0700185 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, rtc::Thread::Current(),
186 [this] { OnChannelReady(true); });
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000187 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000188 }
189
190 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191}
192
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000193DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194
195void DataChannel::RegisterObserver(DataChannelObserver* observer) {
196 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000197 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198}
199
200void DataChannel::UnregisterObserver() {
201 observer_ = NULL;
202}
203
204bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02 +0000205 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 return false;
207 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200208 return config_.maxRetransmits == -1 && config_.maxRetransmitTime == -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 }
210}
211
Peter Boström0c4e06b2015-10-07 12:23:21 +0200212uint64_t DataChannel::buffered_amount() const {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000213 return queued_send_data_.byte_count();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214}
215
216void DataChannel::Close() {
217 if (state_ == kClosed)
218 return;
219 send_ssrc_ = 0;
220 send_ssrc_set_ = false;
221 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700222 // Will send queued data before beginning the underlying closing procedure.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223 UpdateState();
224}
225
226bool DataChannel::Send(const DataBuffer& buffer) {
227 if (state_ != kOpen) {
228 return false;
229 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000230
231 // TODO(jiayl): the spec is unclear about if the remote side should get the
232 // onmessage event. We need to figure out the expected behavior and change the
233 // code accordingly.
234 if (buffer.size() == 0) {
235 return true;
236 }
237
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000238 // If the queue is non-empty, we're waiting for SignalReadyToSend,
239 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000240 if (!queued_send_data_.Empty()) {
241 // Only SCTP DataChannel queues the outgoing data when the transport is
242 // blocked.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800243 RTC_DCHECK(IsSctpLike(data_channel_type_));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000244 if (!QueueSendDataMessage(buffer)) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700245 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to queue "
246 "additional data.";
247 CloseAbruptly();
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000248 }
249 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000252 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000253 if (data_channel_type_ == cricket::DCT_RTP) {
254 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000255 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000256
257 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000258 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259}
260
Peter Boström0c4e06b2015-10-07 12:23:21 +0200261void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800262 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000263
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 return;
266 }
267 receive_ssrc_ = receive_ssrc;
268 receive_ssrc_set_ = true;
269 UpdateState();
270}
271
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000272void DataChannel::SetSctpSid(int sid) {
kwibergee89e782017-08-09 17:22:01 -0700273 RTC_DCHECK_LT(config_.id, 0);
274 RTC_DCHECK_GE(sid, 0);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800275 RTC_DCHECK(IsSctpLike(data_channel_type_));
deadbeefab9b2d12015-10-14 11:33:11 -0700276 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000277 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700278 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000279
280 config_.id = sid;
281 provider_->AddSctpDataStream(sid);
282}
283
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700284void DataChannel::OnClosingProcedureStartedRemotely(int sid) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800285 if (IsSctpLike(data_channel_type_) && sid == config_.id &&
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700286 state_ != kClosing && state_ != kClosed) {
287 // Don't bother sending queued data since the side that initiated the
288 // closure wouldn't receive it anyway. See crbug.com/559394 for a lengthy
289 // discussion about this.
290 queued_send_data_.Clear();
291 queued_control_data_.Clear();
292 // Just need to change state to kClosing, SctpTransport will handle the
293 // rest of the closing procedure and OnClosingProcedureComplete will be
294 // called later.
295 started_closing_procedure_ = true;
296 SetState(kClosing);
297 }
298}
299
300void DataChannel::OnClosingProcedureComplete(int sid) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800301 if (IsSctpLike(data_channel_type_) && sid == config_.id) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700302 // If the closing procedure is complete, we should have finished sending
303 // all pending data and transitioned to kClosing already.
304 RTC_DCHECK_EQ(state_, kClosing);
305 RTC_DCHECK(queued_send_data_.Empty());
306 DisconnectFromProvider();
307 SetState(kClosed);
308 }
309}
310
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000311void DataChannel::OnTransportChannelCreated() {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800312 RTC_DCHECK(IsSctpLike(data_channel_type_));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000313 if (!connected_to_provider_) {
314 connected_to_provider_ = provider_->ConnectDataChannel(this);
315 }
316 // The sid may have been unassigned when provider_->ConnectDataChannel was
317 // done. So always add the streams even if connected_to_provider_ is true.
318 if (config_.id >= 0) {
319 provider_->AddSctpDataStream(config_.id);
320 }
321}
322
deadbeefab9b2d12015-10-14 11:33:11 -0700323void DataChannel::OnTransportChannelDestroyed() {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700324 // The SctpTransport is going away (for example, because the SCTP m= section
325 // was rejected), so we need to close abruptly.
326 CloseAbruptly();
327}
328
329// The remote peer request that this channel shall be closed.
330void DataChannel::RemotePeerRequestClose() {
331 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
332 CloseAbruptly();
deadbeefab9b2d12015-10-14 11:33:11 -0700333}
334
Peter Boström0c4e06b2015-10-07 12:23:21 +0200335void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800336 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338 return;
339 }
340 send_ssrc_ = send_ssrc;
341 send_ssrc_set_ = true;
342 UpdateState();
343}
344
deadbeef953c2ce2017-01-09 14:53:41 -0800345void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700346 const rtc::CopyOnWriteBuffer& payload) {
deadbeef953c2ce2017-01-09 14:53:41 -0800347 if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) {
348 return;
349 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800350 if (IsSctpLike(data_channel_type_) && params.sid != config_.id) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000351 return;
352 }
353
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000354 if (params.type == cricket::DMT_CONTROL) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800355 RTC_DCHECK(IsSctpLike(data_channel_type_));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400356 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000357 // Ignore it if we are not expecting an ACK message.
Jonas Olsson45cc8902018-02-13 10:37:07 +0100358 RTC_LOG(LS_WARNING)
359 << "DataChannel received unexpected CONTROL message, sid = "
360 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000361 return;
362 }
363 if (ParseDataChannelOpenAckMessage(payload)) {
364 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400365 handshake_state_ = kHandshakeReady;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100366 RTC_LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
367 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000368 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100369 RTC_LOG(LS_WARNING)
370 << "DataChannel failed to parse OPEN_ACK message, sid = "
371 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000372 }
373 return;
374 }
375
nisseede5da42017-01-12 05:15:36 -0800376 RTC_DCHECK(params.type == cricket::DMT_BINARY ||
377 params.type == cricket::DMT_TEXT);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000378
Mirko Bonadei675513b2017-11-09 11:09:25 +0100379 RTC_LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = "
380 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000381 // We can send unordered as soon as we receive any DATA message since the
382 // remote side must have received the OPEN (and old clients do not send
383 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400384 if (handshake_state_ == kHandshakeWaitingForAck) {
385 handshake_state_ = kHandshakeReady;
386 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000387
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000388 bool binary = (params.type == cricket::DMT_BINARY);
Steve Anton944c7552018-12-13 14:19:10 -0800389 auto buffer = absl::make_unique<DataBuffer>(payload, binary);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400390 if (state_ == kOpen && observer_) {
hbos84ffdee2016-10-12 14:14:39 -0700391 ++messages_received_;
392 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000393 observer_->OnMessage(*buffer.get());
394 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000395 if (queued_received_data_.byte_count() + payload.size() >
396 kMaxQueuedReceivedDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100397 RTC_LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000398
399 queued_received_data_.Clear();
400 if (data_channel_type_ != cricket::DCT_RTP) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700401 CloseAbruptly();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000402 }
403
404 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000405 }
Steve Anton944c7552018-12-13 14:19:10 -0800406 queued_received_data_.PushBack(std::move(buffer));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000407 }
408}
409
410void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400411 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000412 if (!writable) {
413 return;
414 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000415
Lally Singh5c6c6e02015-05-29 11:52:39 -0400416 SendQueuedControlMessages();
417 SendQueuedDataMessages();
418 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000419}
420
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700421void DataChannel::CloseAbruptly() {
422 if (state_ == kClosed) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000423 return;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700424 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000425
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700426 if (connected_to_provider_) {
427 DisconnectFromProvider();
428 }
429
430 // Closing abruptly means any queued data gets thrown away.
431 queued_send_data_.Clear();
432 queued_control_data_.Clear();
433
434 // Still go to "kClosing" before "kClosed", since observers may be expecting
435 // that.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700437 SetState(kClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438}
439
440void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400441 // UpdateState determines what to do from a few state variables. Include
442 // all conditions required for each state transition here for
443 // clarity. OnChannelReady(true) will send any queued data and then invoke
444 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 switch (state_) {
446 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000447 if (send_ssrc_set_ == receive_ssrc_set_) {
448 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
449 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400451 if (connected_to_provider_) {
452 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 06:15:43 -0700453 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400454 WriteDataChannelOpenMessage(label_, config_, &payload);
455 SendControlMessage(payload);
456 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 06:15:43 -0700457 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400458 WriteDataChannelOpenAckMessage(&payload);
459 SendControlMessage(payload);
460 }
Yves Gerey665174f2018-06-19 15:03:05 +0200461 if (writable_ && (handshake_state_ == kHandshakeReady ||
462 handshake_state_ == kHandshakeWaitingForAck)) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400463 SetState(kOpen);
464 // If we have received buffers before the channel got writable.
465 // Deliver them now.
466 DeliverQueuedReceivedData();
467 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468 }
469 }
470 break;
471 }
472 case kOpen: {
473 break;
474 }
475 case kClosing: {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700476 // Wait for all queued data to be sent before beginning the closing
477 // procedure.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400478 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700479 if (data_channel_type_ == cricket::DCT_RTP) {
480 // For RTP data channels, we can go to "closed" after we finish
481 // sending data and the send/recv SSRCs are unset.
482 if (connected_to_provider_) {
483 DisconnectFromProvider();
484 }
485 if (!send_ssrc_set_ && !receive_ssrc_set_) {
486 SetState(kClosed);
487 }
488 } else {
489 // For SCTP data channels, we need to wait for the closing procedure
490 // to complete; after calling RemoveSctpDataStream,
491 // OnClosingProcedureComplete will end up called asynchronously
492 // afterwards.
493 if (connected_to_provider_ && !started_closing_procedure_ &&
494 config_.id >= 0) {
495 started_closing_procedure_ = true;
496 provider_->RemoveSctpDataStream(config_.id);
497 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400498 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 }
500 break;
501 }
502 case kClosed:
503 break;
504 }
505}
506
507void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700508 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000509 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700510 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000511
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 state_ = state;
513 if (observer_) {
514 observer_->OnStateChange();
515 }
hbos82ebe022016-11-14 01:41:09 -0800516 if (state_ == kOpen) {
517 SignalOpened(this);
518 } else if (state_ == kClosed) {
deadbeefab9b2d12015-10-14 11:33:11 -0700519 SignalClosed(this);
520 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521}
522
Lally Singh5c6c6e02015-05-29 11:52:39 -0400523void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000524 if (!connected_to_provider_)
525 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526
wu@webrtc.org78187522013-10-07 23:32:02 +0000527 provider_->DisconnectDataChannel(this);
528 connected_to_provider_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529}
530
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000531void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400532 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000533 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000536 while (!queued_received_data_.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800537 std::unique_ptr<DataBuffer> buffer = queued_received_data_.PopFront();
hbos84ffdee2016-10-12 14:14:39 -0700538 ++messages_received_;
539 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000540 observer_->OnMessage(*buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 }
542}
543
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000544void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400545 if (queued_send_data_.Empty()) {
546 return;
547 }
548
nisseede5da42017-01-12 05:15:36 -0800549 RTC_DCHECK(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000550
Peter Boström0c4e06b2015-10-07 12:23:21 +0200551 uint64_t start_buffered_amount = buffered_amount();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000552 while (!queued_send_data_.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800553 std::unique_ptr<DataBuffer> buffer = queued_send_data_.PopFront();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000554 if (!SendDataMessage(*buffer, false)) {
Steve Anton944c7552018-12-13 14:19:10 -0800555 // Return the message to the front of the queue if sending is aborted.
556 queued_send_data_.PushFront(std::move(buffer));
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000557 break;
558 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000559 }
bemasc0edd50c2015-07-01 13:34:33 -0700560
561 if (observer_ && buffered_amount() < start_buffered_amount) {
562 observer_->OnBufferedAmountChange(start_buffered_amount);
563 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000564}
565
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000566bool DataChannel::SendDataMessage(const DataBuffer& buffer,
567 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000568 cricket::SendDataParams send_params;
569
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800570 if (IsSctpLike(data_channel_type_)) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000571 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400572 // Send as ordered if it is still going through OPEN/ACK signaling.
573 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000574 send_params.ordered = true;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100575 RTC_LOG(LS_VERBOSE)
576 << "Sending data as ordered for unordered DataChannel "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100577 "because the OPEN_ACK message has not been received.";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000578 }
579
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000580 send_params.max_rtx_count = config_.maxRetransmits;
581 send_params.max_rtx_ms = config_.maxRetransmitTime;
deadbeef953c2ce2017-01-09 14:53:41 -0800582 send_params.sid = config_.id;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000583 } else {
584 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000585 }
586 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
587
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000588 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
589 bool success = provider_->SendData(send_params, buffer.data, &send_result);
590
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000591 if (success) {
hbos84ffdee2016-10-12 14:14:39 -0700592 ++messages_sent_;
593 bytes_sent_ += buffer.size();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000594 return true;
595 }
596
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800597 if (!IsSctpLike(data_channel_type_)) {
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000598 return false;
599 }
600
601 if (send_result == cricket::SDR_BLOCK) {
602 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
603 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000604 }
605 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000606 // Close the channel if the error is not SDR_BLOCK, or if queuing the
607 // message failed.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100608 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100609 "send_result = "
610 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700611 CloseAbruptly();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000612
613 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000614}
615
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000616bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
bemasc0edd50c2015-07-01 13:34:33 -0700617 size_t start_buffered_amount = buffered_amount();
618 if (start_buffered_amount >= kMaxQueuedSendDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100619 RTC_LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000620 return false;
621 }
Steve Anton944c7552018-12-13 14:19:10 -0800622 queued_send_data_.PushBack(absl::make_unique<DataBuffer>(buffer));
bemasc0edd50c2015-07-01 13:34:33 -0700623
624 // The buffer can have length zero, in which case there is no change.
625 if (observer_ && buffered_amount() > start_buffered_amount) {
626 observer_->OnBufferedAmountChange(start_buffered_amount);
627 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000628 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629}
630
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000631void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000632 PacketQueue control_packets;
633 control_packets.Swap(&queued_control_data_);
634
635 while (!control_packets.Empty()) {
Steve Anton944c7552018-12-13 14:19:10 -0800636 std::unique_ptr<DataBuffer> buf = control_packets.PopFront();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000637 SendControlMessage(buf->data);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000638 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000639}
640
jbaucheec21bd2016-03-20 06:15:43 -0700641void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Steve Anton944c7552018-12-13 14:19:10 -0800642 queued_control_data_.PushBack(absl::make_unique<DataBuffer>(buffer, true));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000643}
644
jbaucheec21bd2016-03-20 06:15:43 -0700645bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400646 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000647
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800648 RTC_DCHECK(IsSctpLike(data_channel_type_));
kwibergee89e782017-08-09 17:22:01 -0700649 RTC_DCHECK(writable_);
650 RTC_DCHECK_GE(config_.id, 0);
651 RTC_DCHECK(!is_open_message || !config_.negotiated);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000652
653 cricket::SendDataParams send_params;
deadbeef953c2ce2017-01-09 14:53:41 -0800654 send_params.sid = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400655 // Send data as ordered before we receive any message from the remote peer to
656 // make sure the remote peer will not receive any data before it receives the
657 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000658 send_params.ordered = config_.ordered || is_open_message;
659 send_params.type = cricket::DMT_CONTROL;
660
661 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
662 bool retval = provider_->SendData(send_params, buffer, &send_result);
663 if (retval) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100664 RTC_LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000665
Lally Singh5c6c6e02015-05-29 11:52:39 -0400666 if (handshake_state_ == kHandshakeShouldSendAck) {
667 handshake_state_ = kHandshakeReady;
668 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
669 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000670 }
671 } else if (send_result == cricket::SDR_BLOCK) {
672 QueueControlMessage(buffer);
673 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100674 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
Jonas Olsson45cc8902018-02-13 10:37:07 +0100675 " the CONTROL message, send_result = "
676 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700677 CloseAbruptly();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000678 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000679 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000680}
681
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682} // namespace webrtc