blob: b7a717c34e5a786a849b441b0dde2534d2db0476 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +000010
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/datachannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
kwibergd1fe2812016-04-27 06:47:29 -070013#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <string>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "media/sctp/sctptransportinternal.h"
17#include "pc/sctputils.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
20#include "rtc_base/refcount.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace webrtc {
23
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000024static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024;
25static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000027enum {
28 MSG_CHANNELREADY,
29};
30
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 +000068DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {}
69
70DataChannel::PacketQueue::~PacketQueue() {
71 Clear();
72}
73
74bool DataChannel::PacketQueue::Empty() const {
75 return packets_.empty();
76}
77
78DataBuffer* DataChannel::PacketQueue::Front() {
79 return packets_.front();
80}
81
82void DataChannel::PacketQueue::Pop() {
83 if (packets_.empty()) {
84 return;
85 }
86
87 byte_count_ -= packets_.front()->size();
88 packets_.pop_front();
89}
90
91void DataChannel::PacketQueue::Push(DataBuffer* packet) {
92 byte_count_ += packet->size();
93 packets_.push_back(packet);
94}
95
96void DataChannel::PacketQueue::Clear() {
97 while (!packets_.empty()) {
98 delete packets_.front();
99 packets_.pop_front();
100 }
101 byte_count_ = 0;
102}
103
104void DataChannel::PacketQueue::Swap(PacketQueue* other) {
105 size_t other_byte_count = other->byte_count_;
106 other->byte_count_ = byte_count_;
107 byte_count_ = other_byte_count;
108
109 other->packets_.swap(packets_);
110}
111
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000112rtc::scoped_refptr<DataChannel> DataChannel::Create(
wu@webrtc.org78187522013-10-07 23:32:02 +0000113 DataChannelProviderInterface* provider,
114 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000116 const InternalDataChannelInit& config) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000117 rtc::scoped_refptr<DataChannel> channel(
118 new rtc::RefCountedObject<DataChannel>(provider, dct, label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 if (!channel->Init(config)) {
120 return NULL;
121 }
122 return channel;
123}
124
wu@webrtc.org78187522013-10-07 23:32:02 +0000125DataChannel::DataChannel(
126 DataChannelProviderInterface* provider,
127 cricket::DataChannelType dct,
128 const std::string& label)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 : label_(label),
hbos84ffdee2016-10-12 14:14:39 -0700130 observer_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 state_(kConnecting),
hbos84ffdee2016-10-12 14:14:39 -0700132 messages_sent_(0),
133 bytes_sent_(0),
134 messages_received_(0),
135 bytes_received_(0),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000136 data_channel_type_(dct),
137 provider_(provider),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400138 handshake_state_(kHandshakeInit),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000139 connected_to_provider_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 send_ssrc_set_(false),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000141 receive_ssrc_set_(false),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400142 writable_(false),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000143 send_ssrc_(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 receive_ssrc_(0) {
145}
146
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000147bool DataChannel::Init(const InternalDataChannelInit& config) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400148 if (data_channel_type_ == cricket::DCT_RTP) {
149 if (config.reliable ||
150 config.id != -1 ||
151 config.maxRetransmits != -1 ||
152 config.maxRetransmitTime != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100153 RTC_LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100154 "invalid DataChannelInit.";
Lally Singh5c6c6e02015-05-29 11:52:39 -0400155 return false;
156 }
157 handshake_state_ = kHandshakeReady;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000158 } else if (data_channel_type_ == cricket::DCT_SCTP) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000159 if (config.id < -1 ||
160 config.maxRetransmits < -1 ||
161 config.maxRetransmitTime < -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100162 RTC_LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100163 "invalid DataChannelInit.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 return false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000165 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000166 if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100167 RTC_LOG(LS_ERROR)
168 << "maxRetransmits and maxRetransmitTime should not be both set.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000169 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000171 config_ = config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172
Lally Singh5c6c6e02015-05-29 11:52:39 -0400173 switch (config_.open_handshake_role) {
174 case webrtc::InternalDataChannelInit::kNone: // pre-negotiated
175 handshake_state_ = kHandshakeReady;
176 break;
177 case webrtc::InternalDataChannelInit::kOpener:
178 handshake_state_ = kHandshakeShouldSendOpen;
179 break;
180 case webrtc::InternalDataChannelInit::kAcker:
181 handshake_state_ = kHandshakeShouldSendAck;
182 break;
Steve Anton36b29d12017-10-30 09:57:42 -0700183 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400184
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000185 // Try to connect to the transport in case the transport channel already
186 // exists.
187 OnTransportChannelCreated();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000188
189 // Checks if the transport is ready to send because the initial channel
190 // ready signal may have been sent before the DataChannel creation.
191 // This has to be done async because the upper layer objects (e.g.
192 // Chrome glue and WebKit) are not wired up properly until after this
193 // function returns.
194 if (provider_->ReadyToSendData()) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700195 rtc::Thread::Current()->Post(RTC_FROM_HERE, this, MSG_CHANNELREADY, NULL);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000196 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000197 }
198
199 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200}
201
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000202DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203
204void DataChannel::RegisterObserver(DataChannelObserver* observer) {
205 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000206 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207}
208
209void DataChannel::UnregisterObserver() {
210 observer_ = NULL;
211}
212
213bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02 +0000214 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 return false;
216 } else {
217 return config_.maxRetransmits == -1 &&
218 config_.maxRetransmitTime == -1;
219 }
220}
221
Peter Boström0c4e06b2015-10-07 12:23:21 +0200222uint64_t DataChannel::buffered_amount() const {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000223 return queued_send_data_.byte_count();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224}
225
226void DataChannel::Close() {
227 if (state_ == kClosed)
228 return;
229 send_ssrc_ = 0;
230 send_ssrc_set_ = false;
231 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700232 // Will send queued data before beginning the underlying closing procedure.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 UpdateState();
234}
235
236bool DataChannel::Send(const DataBuffer& buffer) {
237 if (state_ != kOpen) {
238 return false;
239 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000240
241 // TODO(jiayl): the spec is unclear about if the remote side should get the
242 // onmessage event. We need to figure out the expected behavior and change the
243 // code accordingly.
244 if (buffer.size() == 0) {
245 return true;
246 }
247
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000248 // If the queue is non-empty, we're waiting for SignalReadyToSend,
249 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000250 if (!queued_send_data_.Empty()) {
251 // Only SCTP DataChannel queues the outgoing data when the transport is
252 // blocked.
nisseede5da42017-01-12 05:15:36 -0800253 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000254 if (!QueueSendDataMessage(buffer)) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700255 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to queue "
256 "additional data.";
257 CloseAbruptly();
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000258 }
259 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000262 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000263 if (data_channel_type_ == cricket::DCT_RTP) {
264 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000265 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000266
267 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000268 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269}
270
Peter Boström0c4e06b2015-10-07 12:23:21 +0200271void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800272 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000273
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 return;
276 }
277 receive_ssrc_ = receive_ssrc;
278 receive_ssrc_set_ = true;
279 UpdateState();
280}
281
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000282void DataChannel::SetSctpSid(int sid) {
kwibergee89e782017-08-09 17:22:01 -0700283 RTC_DCHECK_LT(config_.id, 0);
284 RTC_DCHECK_GE(sid, 0);
285 RTC_DCHECK_EQ(data_channel_type_, cricket::DCT_SCTP);
deadbeefab9b2d12015-10-14 11:33:11 -0700286 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000287 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700288 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000289
290 config_.id = sid;
291 provider_->AddSctpDataStream(sid);
292}
293
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700294void DataChannel::OnClosingProcedureStartedRemotely(int sid) {
295 if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id &&
296 state_ != kClosing && state_ != kClosed) {
297 // Don't bother sending queued data since the side that initiated the
298 // closure wouldn't receive it anyway. See crbug.com/559394 for a lengthy
299 // discussion about this.
300 queued_send_data_.Clear();
301 queued_control_data_.Clear();
302 // Just need to change state to kClosing, SctpTransport will handle the
303 // rest of the closing procedure and OnClosingProcedureComplete will be
304 // called later.
305 started_closing_procedure_ = true;
306 SetState(kClosing);
307 }
308}
309
310void DataChannel::OnClosingProcedureComplete(int sid) {
311 if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id) {
312 // If the closing procedure is complete, we should have finished sending
313 // all pending data and transitioned to kClosing already.
314 RTC_DCHECK_EQ(state_, kClosing);
315 RTC_DCHECK(queued_send_data_.Empty());
316 DisconnectFromProvider();
317 SetState(kClosed);
318 }
319}
320
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000321void DataChannel::OnTransportChannelCreated() {
nisseede5da42017-01-12 05:15:36 -0800322 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000323 if (!connected_to_provider_) {
324 connected_to_provider_ = provider_->ConnectDataChannel(this);
325 }
326 // The sid may have been unassigned when provider_->ConnectDataChannel was
327 // done. So always add the streams even if connected_to_provider_ is true.
328 if (config_.id >= 0) {
329 provider_->AddSctpDataStream(config_.id);
330 }
331}
332
deadbeefab9b2d12015-10-14 11:33:11 -0700333void DataChannel::OnTransportChannelDestroyed() {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700334 // The SctpTransport is going away (for example, because the SCTP m= section
335 // was rejected), so we need to close abruptly.
336 CloseAbruptly();
337}
338
339// The remote peer request that this channel shall be closed.
340void DataChannel::RemotePeerRequestClose() {
341 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
342 CloseAbruptly();
deadbeefab9b2d12015-10-14 11:33:11 -0700343}
344
Peter Boström0c4e06b2015-10-07 12:23:21 +0200345void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800346 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348 return;
349 }
350 send_ssrc_ = send_ssrc;
351 send_ssrc_set_ = true;
352 UpdateState();
353}
354
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000355void DataChannel::OnMessage(rtc::Message* msg) {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000356 switch (msg->message_id) {
357 case MSG_CHANNELREADY:
358 OnChannelReady(true);
359 break;
360 }
361}
362
deadbeef953c2ce2017-01-09 14:53:41 -0800363void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700364 const rtc::CopyOnWriteBuffer& payload) {
deadbeef953c2ce2017-01-09 14:53:41 -0800365 if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) {
366 return;
367 }
368 if (data_channel_type_ == cricket::DCT_SCTP && params.sid != config_.id) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000369 return;
370 }
371
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000372 if (params.type == cricket::DMT_CONTROL) {
nisseede5da42017-01-12 05:15:36 -0800373 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400374 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000375 // Ignore it if we are not expecting an ACK message.
Jonas Olsson45cc8902018-02-13 10:37:07 +0100376 RTC_LOG(LS_WARNING)
377 << "DataChannel received unexpected CONTROL message, sid = "
378 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000379 return;
380 }
381 if (ParseDataChannelOpenAckMessage(payload)) {
382 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400383 handshake_state_ = kHandshakeReady;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100384 RTC_LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
385 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000386 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100387 RTC_LOG(LS_WARNING)
388 << "DataChannel failed to parse OPEN_ACK message, sid = "
389 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000390 }
391 return;
392 }
393
nisseede5da42017-01-12 05:15:36 -0800394 RTC_DCHECK(params.type == cricket::DMT_BINARY ||
395 params.type == cricket::DMT_TEXT);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000396
Mirko Bonadei675513b2017-11-09 11:09:25 +0100397 RTC_LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = "
398 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000399 // We can send unordered as soon as we receive any DATA message since the
400 // remote side must have received the OPEN (and old clients do not send
401 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400402 if (handshake_state_ == kHandshakeWaitingForAck) {
403 handshake_state_ = kHandshakeReady;
404 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000405
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000406 bool binary = (params.type == cricket::DMT_BINARY);
kwibergd1fe2812016-04-27 06:47:29 -0700407 std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400408 if (state_ == kOpen && observer_) {
hbos84ffdee2016-10-12 14:14:39 -0700409 ++messages_received_;
410 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000411 observer_->OnMessage(*buffer.get());
412 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000413 if (queued_received_data_.byte_count() + payload.size() >
414 kMaxQueuedReceivedDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100415 RTC_LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000416
417 queued_received_data_.Clear();
418 if (data_channel_type_ != cricket::DCT_RTP) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700419 CloseAbruptly();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000420 }
421
422 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000423 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000424 queued_received_data_.Push(buffer.release());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000425 }
426}
427
428void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400429 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000430 if (!writable) {
431 return;
432 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000433
Lally Singh5c6c6e02015-05-29 11:52:39 -0400434 SendQueuedControlMessages();
435 SendQueuedDataMessages();
436 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000437}
438
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700439void DataChannel::CloseAbruptly() {
440 if (state_ == kClosed) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000441 return;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700442 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000443
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700444 if (connected_to_provider_) {
445 DisconnectFromProvider();
446 }
447
448 // Closing abruptly means any queued data gets thrown away.
449 queued_send_data_.Clear();
450 queued_control_data_.Clear();
451
452 // Still go to "kClosing" before "kClosed", since observers may be expecting
453 // that.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 SetState(kClosing);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700455 SetState(kClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456}
457
458void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400459 // UpdateState determines what to do from a few state variables. Include
460 // all conditions required for each state transition here for
461 // clarity. OnChannelReady(true) will send any queued data and then invoke
462 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 switch (state_) {
464 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000465 if (send_ssrc_set_ == receive_ssrc_set_) {
466 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
467 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400469 if (connected_to_provider_) {
470 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 06:15:43 -0700471 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400472 WriteDataChannelOpenMessage(label_, config_, &payload);
473 SendControlMessage(payload);
474 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 06:15:43 -0700475 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400476 WriteDataChannelOpenAckMessage(&payload);
477 SendControlMessage(payload);
478 }
479 if (writable_ &&
480 (handshake_state_ == kHandshakeReady ||
481 handshake_state_ == kHandshakeWaitingForAck)) {
482 SetState(kOpen);
483 // If we have received buffers before the channel got writable.
484 // Deliver them now.
485 DeliverQueuedReceivedData();
486 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 }
488 }
489 break;
490 }
491 case kOpen: {
492 break;
493 }
494 case kClosing: {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700495 // Wait for all queued data to be sent before beginning the closing
496 // procedure.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400497 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700498 if (data_channel_type_ == cricket::DCT_RTP) {
499 // For RTP data channels, we can go to "closed" after we finish
500 // sending data and the send/recv SSRCs are unset.
501 if (connected_to_provider_) {
502 DisconnectFromProvider();
503 }
504 if (!send_ssrc_set_ && !receive_ssrc_set_) {
505 SetState(kClosed);
506 }
507 } else {
508 // For SCTP data channels, we need to wait for the closing procedure
509 // to complete; after calling RemoveSctpDataStream,
510 // OnClosingProcedureComplete will end up called asynchronously
511 // afterwards.
512 if (connected_to_provider_ && !started_closing_procedure_ &&
513 config_.id >= 0) {
514 started_closing_procedure_ = true;
515 provider_->RemoveSctpDataStream(config_.id);
516 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400517 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 }
519 break;
520 }
521 case kClosed:
522 break;
523 }
524}
525
526void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700527 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000528 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700529 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000530
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 state_ = state;
532 if (observer_) {
533 observer_->OnStateChange();
534 }
hbos82ebe022016-11-14 01:41:09 -0800535 if (state_ == kOpen) {
536 SignalOpened(this);
537 } else if (state_ == kClosed) {
deadbeefab9b2d12015-10-14 11:33:11 -0700538 SignalClosed(this);
539 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540}
541
Lally Singh5c6c6e02015-05-29 11:52:39 -0400542void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000543 if (!connected_to_provider_)
544 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545
wu@webrtc.org78187522013-10-07 23:32:02 +0000546 provider_->DisconnectDataChannel(this);
547 connected_to_provider_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548}
549
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000550void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400551 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000552 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000555 while (!queued_received_data_.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700556 std::unique_ptr<DataBuffer> buffer(queued_received_data_.Front());
hbos84ffdee2016-10-12 14:14:39 -0700557 ++messages_received_;
558 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000559 observer_->OnMessage(*buffer);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000560 queued_received_data_.Pop();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 }
562}
563
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000564void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400565 if (queued_send_data_.Empty()) {
566 return;
567 }
568
nisseede5da42017-01-12 05:15:36 -0800569 RTC_DCHECK(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000570
Peter Boström0c4e06b2015-10-07 12:23:21 +0200571 uint64_t start_buffered_amount = buffered_amount();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000572 while (!queued_send_data_.Empty()) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000573 DataBuffer* buffer = queued_send_data_.Front();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000574 if (!SendDataMessage(*buffer, false)) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000575 // Leave the message in the queue if sending is aborted.
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000576 break;
577 }
578 queued_send_data_.Pop();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000579 delete buffer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000580 }
bemasc0edd50c2015-07-01 13:34:33 -0700581
582 if (observer_ && buffered_amount() < start_buffered_amount) {
583 observer_->OnBufferedAmountChange(start_buffered_amount);
584 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000585}
586
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000587bool DataChannel::SendDataMessage(const DataBuffer& buffer,
588 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000589 cricket::SendDataParams send_params;
590
wu@webrtc.org78187522013-10-07 23:32:02 +0000591 if (data_channel_type_ == cricket::DCT_SCTP) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000592 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400593 // Send as ordered if it is still going through OPEN/ACK signaling.
594 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000595 send_params.ordered = true;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100596 RTC_LOG(LS_VERBOSE)
597 << "Sending data as ordered for unordered DataChannel "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100598 "because the OPEN_ACK message has not been received.";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000599 }
600
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000601 send_params.max_rtx_count = config_.maxRetransmits;
602 send_params.max_rtx_ms = config_.maxRetransmitTime;
deadbeef953c2ce2017-01-09 14:53:41 -0800603 send_params.sid = config_.id;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000604 } else {
605 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000606 }
607 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
608
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000609 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
610 bool success = provider_->SendData(send_params, buffer.data, &send_result);
611
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000612 if (success) {
hbos84ffdee2016-10-12 14:14:39 -0700613 ++messages_sent_;
614 bytes_sent_ += buffer.size();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000615 return true;
616 }
617
618 if (data_channel_type_ != cricket::DCT_SCTP) {
619 return false;
620 }
621
622 if (send_result == cricket::SDR_BLOCK) {
623 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
624 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000625 }
626 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000627 // Close the channel if the error is not SDR_BLOCK, or if queuing the
628 // message failed.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100629 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100630 "send_result = "
631 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700632 CloseAbruptly();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000633
634 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000635}
636
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000637bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
bemasc0edd50c2015-07-01 13:34:33 -0700638 size_t start_buffered_amount = buffered_amount();
639 if (start_buffered_amount >= kMaxQueuedSendDataBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100640 RTC_LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000641 return false;
642 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000643 queued_send_data_.Push(new DataBuffer(buffer));
bemasc0edd50c2015-07-01 13:34:33 -0700644
645 // The buffer can have length zero, in which case there is no change.
646 if (observer_ && buffered_amount() > start_buffered_amount) {
647 observer_->OnBufferedAmountChange(start_buffered_amount);
648 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000649 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650}
651
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000652void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000653 PacketQueue control_packets;
654 control_packets.Swap(&queued_control_data_);
655
656 while (!control_packets.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700657 std::unique_ptr<DataBuffer> buf(control_packets.Front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000658 SendControlMessage(buf->data);
659 control_packets.Pop();
660 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000661}
662
jbaucheec21bd2016-03-20 06:15:43 -0700663void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000664 queued_control_data_.Push(new DataBuffer(buffer, true));
665}
666
jbaucheec21bd2016-03-20 06:15:43 -0700667bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400668 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000669
kwibergee89e782017-08-09 17:22:01 -0700670 RTC_DCHECK_EQ(data_channel_type_, cricket::DCT_SCTP);
671 RTC_DCHECK(writable_);
672 RTC_DCHECK_GE(config_.id, 0);
673 RTC_DCHECK(!is_open_message || !config_.negotiated);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000674
675 cricket::SendDataParams send_params;
deadbeef953c2ce2017-01-09 14:53:41 -0800676 send_params.sid = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400677 // Send data as ordered before we receive any message from the remote peer to
678 // make sure the remote peer will not receive any data before it receives the
679 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000680 send_params.ordered = config_.ordered || is_open_message;
681 send_params.type = cricket::DMT_CONTROL;
682
683 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
684 bool retval = provider_->SendData(send_params, buffer, &send_result);
685 if (retval) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100686 RTC_LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000687
Lally Singh5c6c6e02015-05-29 11:52:39 -0400688 if (handshake_state_ == kHandshakeShouldSendAck) {
689 handshake_state_ = kHandshakeReady;
690 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
691 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000692 }
693 } else if (send_result == cricket::SDR_BLOCK) {
694 QueueControlMessage(buffer);
695 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100696 RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
Jonas Olsson45cc8902018-02-13 10:37:07 +0100697 " the CONTROL message, send_result = "
698 << send_result;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700699 CloseAbruptly();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000700 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000701 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000702}
703
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704} // namespace webrtc