blob: 6b13bf2c705ae7152c9ce4fa6edef00f9ce92340 [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/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
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/sctputils.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000017#include "webrtc/base/logging.h"
18#include "webrtc/base/refcount.h"
kjellandera96e2d72016-02-04 23:52:28 -080019#include "webrtc/media/sctp/sctpdataengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
21namespace webrtc {
22
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000023static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024;
24static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000026enum {
27 MSG_CHANNELREADY,
28};
29
deadbeefab9b2d12015-10-14 11:33:11 -070030bool SctpSidAllocator::AllocateSid(rtc::SSLRole role, int* sid) {
31 int potential_sid = (role == rtc::SSL_CLIENT) ? 0 : 1;
32 while (!IsSidAvailable(potential_sid)) {
33 potential_sid += 2;
34 if (potential_sid > static_cast<int>(cricket::kMaxSctpSid)) {
35 return false;
36 }
37 }
38
39 *sid = potential_sid;
40 used_sids_.insert(potential_sid);
41 return true;
42}
43
44bool SctpSidAllocator::ReserveSid(int sid) {
45 if (!IsSidAvailable(sid)) {
46 return false;
47 }
48 used_sids_.insert(sid);
49 return true;
50}
51
52void SctpSidAllocator::ReleaseSid(int sid) {
53 auto it = used_sids_.find(sid);
54 if (it != used_sids_.end()) {
55 used_sids_.erase(it);
56 }
57}
58
59bool SctpSidAllocator::IsSidAvailable(int sid) const {
60 if (sid < 0 || sid > static_cast<int>(cricket::kMaxSctpSid)) {
61 return false;
62 }
63 return used_sids_.find(sid) == used_sids_.end();
64}
65
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000066DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {}
67
68DataChannel::PacketQueue::~PacketQueue() {
69 Clear();
70}
71
72bool DataChannel::PacketQueue::Empty() const {
73 return packets_.empty();
74}
75
76DataBuffer* DataChannel::PacketQueue::Front() {
77 return packets_.front();
78}
79
80void DataChannel::PacketQueue::Pop() {
81 if (packets_.empty()) {
82 return;
83 }
84
85 byte_count_ -= packets_.front()->size();
86 packets_.pop_front();
87}
88
89void DataChannel::PacketQueue::Push(DataBuffer* packet) {
90 byte_count_ += packet->size();
91 packets_.push_back(packet);
92}
93
94void DataChannel::PacketQueue::Clear() {
95 while (!packets_.empty()) {
96 delete packets_.front();
97 packets_.pop_front();
98 }
99 byte_count_ = 0;
100}
101
102void DataChannel::PacketQueue::Swap(PacketQueue* other) {
103 size_t other_byte_count = other->byte_count_;
104 other->byte_count_ = byte_count_;
105 byte_count_ = other_byte_count;
106
107 other->packets_.swap(packets_);
108}
109
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000110rtc::scoped_refptr<DataChannel> DataChannel::Create(
wu@webrtc.org78187522013-10-07 23:32:02 +0000111 DataChannelProviderInterface* provider,
112 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000114 const InternalDataChannelInit& config) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000115 rtc::scoped_refptr<DataChannel> channel(
116 new rtc::RefCountedObject<DataChannel>(provider, dct, label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 if (!channel->Init(config)) {
118 return NULL;
119 }
120 return channel;
121}
122
wu@webrtc.org78187522013-10-07 23:32:02 +0000123DataChannel::DataChannel(
124 DataChannelProviderInterface* provider,
125 cricket::DataChannelType dct,
126 const std::string& label)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 : label_(label),
128 observer_(NULL),
129 state_(kConnecting),
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),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 receive_ssrc_(0) {
139}
140
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) {
143 if (config.reliable ||
144 config.id != -1 ||
145 config.maxRetransmits != -1 ||
146 config.maxRetransmitTime != -1) {
147 LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
148 << "invalid DataChannelInit.";
149 return false;
150 }
151 handshake_state_ = kHandshakeReady;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000152 } else if (data_channel_type_ == cricket::DCT_SCTP) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000153 if (config.id < -1 ||
154 config.maxRetransmits < -1 ||
155 config.maxRetransmitTime < -1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000156 LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 << "invalid DataChannelInit.";
158 return false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000159 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000160 if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000161 LOG(LS_ERROR) <<
162 "maxRetransmits and maxRetransmitTime should not be both set.";
163 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000165 config_ = config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166
Lally Singh5c6c6e02015-05-29 11:52:39 -0400167 switch (config_.open_handshake_role) {
168 case webrtc::InternalDataChannelInit::kNone: // pre-negotiated
169 handshake_state_ = kHandshakeReady;
170 break;
171 case webrtc::InternalDataChannelInit::kOpener:
172 handshake_state_ = kHandshakeShouldSendOpen;
173 break;
174 case webrtc::InternalDataChannelInit::kAcker:
175 handshake_state_ = kHandshakeShouldSendAck;
176 break;
177 };
178
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000179 // Try to connect to the transport in case the transport channel already
180 // exists.
181 OnTransportChannelCreated();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000182
183 // Checks if the transport is ready to send because the initial channel
184 // ready signal may have been sent before the DataChannel creation.
185 // This has to be done async because the upper layer objects (e.g.
186 // Chrome glue and WebKit) are not wired up properly until after this
187 // function returns.
188 if (provider_->ReadyToSendData()) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700189 rtc::Thread::Current()->Post(RTC_FROM_HERE, this, MSG_CHANNELREADY, NULL);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000190 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000191 }
192
193 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194}
195
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000196DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197
198void DataChannel::RegisterObserver(DataChannelObserver* observer) {
199 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000200 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201}
202
203void DataChannel::UnregisterObserver() {
204 observer_ = NULL;
205}
206
207bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02 +0000208 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 return false;
210 } else {
211 return config_.maxRetransmits == -1 &&
212 config_.maxRetransmitTime == -1;
213 }
214}
215
Peter Boström0c4e06b2015-10-07 12:23:21 +0200216uint64_t DataChannel::buffered_amount() const {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000217 return queued_send_data_.byte_count();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218}
219
220void DataChannel::Close() {
221 if (state_ == kClosed)
222 return;
223 send_ssrc_ = 0;
224 send_ssrc_set_ = false;
225 SetState(kClosing);
226 UpdateState();
227}
228
229bool DataChannel::Send(const DataBuffer& buffer) {
230 if (state_ != kOpen) {
231 return false;
232 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000233
234 // TODO(jiayl): the spec is unclear about if the remote side should get the
235 // onmessage event. We need to figure out the expected behavior and change the
236 // code accordingly.
237 if (buffer.size() == 0) {
238 return true;
239 }
240
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000241 // If the queue is non-empty, we're waiting for SignalReadyToSend,
242 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000243 if (!queued_send_data_.Empty()) {
244 // Only SCTP DataChannel queues the outgoing data when the transport is
245 // blocked.
246 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
247 if (!QueueSendDataMessage(buffer)) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000248 Close();
249 }
250 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000253 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000254 if (data_channel_type_ == cricket::DCT_RTP) {
255 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000256 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000257
258 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000259 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260}
261
Peter Boström0c4e06b2015-10-07 12:23:21 +0200262void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000263 ASSERT(data_channel_type_ == cricket::DCT_RTP);
264
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 return;
267 }
268 receive_ssrc_ = receive_ssrc;
269 receive_ssrc_set_ = true;
270 UpdateState();
271}
272
273// The remote peer request that this channel shall be closed.
274void DataChannel::RemotePeerRequestClose() {
275 DoClose();
276}
277
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000278void DataChannel::SetSctpSid(int sid) {
279 ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP);
deadbeefab9b2d12015-10-14 11:33:11 -0700280 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000281 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700282 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000283
284 config_.id = sid;
285 provider_->AddSctpDataStream(sid);
286}
287
288void DataChannel::OnTransportChannelCreated() {
289 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
290 if (!connected_to_provider_) {
291 connected_to_provider_ = provider_->ConnectDataChannel(this);
292 }
293 // The sid may have been unassigned when provider_->ConnectDataChannel was
294 // done. So always add the streams even if connected_to_provider_ is true.
295 if (config_.id >= 0) {
296 provider_->AddSctpDataStream(config_.id);
297 }
298}
299
deadbeefab9b2d12015-10-14 11:33:11 -0700300void DataChannel::OnTransportChannelDestroyed() {
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700301 // This method needs to synchronously close the data channel, which means any
302 // queued data needs to be discarded.
303 queued_send_data_.Clear();
304 queued_control_data_.Clear();
deadbeefab9b2d12015-10-14 11:33:11 -0700305 DoClose();
306}
307
Peter Boström0c4e06b2015-10-07 12:23:21 +0200308void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000309 ASSERT(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 return;
312 }
313 send_ssrc_ = send_ssrc;
314 send_ssrc_set_ = true;
315 UpdateState();
316}
317
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000318void DataChannel::OnMessage(rtc::Message* msg) {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000319 switch (msg->message_id) {
320 case MSG_CHANNELREADY:
321 OnChannelReady(true);
322 break;
323 }
324}
325
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000326void DataChannel::OnDataReceived(cricket::DataChannel* channel,
327 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700328 const rtc::CopyOnWriteBuffer& payload) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200329 uint32_t expected_ssrc =
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000330 (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id;
331 if (params.ssrc != expected_ssrc) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000332 return;
333 }
334
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000335 if (params.type == cricket::DMT_CONTROL) {
336 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400337 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000338 // Ignore it if we are not expecting an ACK message.
339 LOG(LS_WARNING) << "DataChannel received unexpected CONTROL message, "
340 << "sid = " << params.ssrc;
341 return;
342 }
343 if (ParseDataChannelOpenAckMessage(payload)) {
344 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400345 handshake_state_ = kHandshakeReady;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000346 LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
347 << params.ssrc;
348 } else {
349 LOG(LS_WARNING) << "DataChannel failed to parse OPEN_ACK message, sid = "
350 << params.ssrc;
351 }
352 return;
353 }
354
355 ASSERT(params.type == cricket::DMT_BINARY ||
356 params.type == cricket::DMT_TEXT);
357
358 LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " << params.ssrc;
359 // We can send unordered as soon as we receive any DATA message since the
360 // remote side must have received the OPEN (and old clients do not send
361 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400362 if (handshake_state_ == kHandshakeWaitingForAck) {
363 handshake_state_ = kHandshakeReady;
364 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000365
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000366 bool binary = (params.type == cricket::DMT_BINARY);
kwibergd1fe2812016-04-27 06:47:29 -0700367 std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400368 if (state_ == kOpen && observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000369 observer_->OnMessage(*buffer.get());
370 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000371 if (queued_received_data_.byte_count() + payload.size() >
372 kMaxQueuedReceivedDataBytes) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000373 LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
374
375 queued_received_data_.Clear();
376 if (data_channel_type_ != cricket::DCT_RTP) {
377 Close();
378 }
379
380 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000381 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000382 queued_received_data_.Push(buffer.release());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000383 }
384}
385
deadbeefab9b2d12015-10-14 11:33:11 -0700386void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
kjellander3e33bfe2016-06-20 07:04:09 -0700387 if (data_channel_type_ == cricket::DCT_SCTP &&
388 sid == static_cast<uint32_t>(config_.id)) {
deadbeefab9b2d12015-10-14 11:33:11 -0700389 Close();
390 }
391}
392
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000393void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400394 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000395 if (!writable) {
396 return;
397 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000398
Lally Singh5c6c6e02015-05-29 11:52:39 -0400399 SendQueuedControlMessages();
400 SendQueuedDataMessages();
401 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000402}
403
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404void DataChannel::DoClose() {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000405 if (state_ == kClosed)
406 return;
407
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 receive_ssrc_set_ = false;
409 send_ssrc_set_ = false;
410 SetState(kClosing);
411 UpdateState();
412}
413
414void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400415 // UpdateState determines what to do from a few state variables. Include
416 // all conditions required for each state transition here for
417 // clarity. OnChannelReady(true) will send any queued data and then invoke
418 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 switch (state_) {
420 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000421 if (send_ssrc_set_ == receive_ssrc_set_) {
422 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
423 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400425 if (connected_to_provider_) {
426 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 06:15:43 -0700427 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400428 WriteDataChannelOpenMessage(label_, config_, &payload);
429 SendControlMessage(payload);
430 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 06:15:43 -0700431 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400432 WriteDataChannelOpenAckMessage(&payload);
433 SendControlMessage(payload);
434 }
435 if (writable_ &&
436 (handshake_state_ == kHandshakeReady ||
437 handshake_state_ == kHandshakeWaitingForAck)) {
438 SetState(kOpen);
439 // If we have received buffers before the channel got writable.
440 // Deliver them now.
441 DeliverQueuedReceivedData();
442 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443 }
444 }
445 break;
446 }
447 case kOpen: {
448 break;
449 }
450 case kClosing: {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400451 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
452 if (connected_to_provider_) {
453 DisconnectFromProvider();
454 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000455
Lally Singh5c6c6e02015-05-29 11:52:39 -0400456 if (!connected_to_provider_ && !send_ssrc_set_ && !receive_ssrc_set_) {
457 SetState(kClosed);
458 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459 }
460 break;
461 }
462 case kClosed:
463 break;
464 }
465}
466
467void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700468 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000469 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700470 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000471
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 state_ = state;
473 if (observer_) {
474 observer_->OnStateChange();
475 }
deadbeefab9b2d12015-10-14 11:33:11 -0700476 if (state_ == kClosed) {
477 SignalClosed(this);
478 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479}
480
Lally Singh5c6c6e02015-05-29 11:52:39 -0400481void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000482 if (!connected_to_provider_)
483 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484
wu@webrtc.org78187522013-10-07 23:32:02 +0000485 provider_->DisconnectDataChannel(this);
486 connected_to_provider_ = false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000487
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +0000488 if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000489 provider_->RemoveSctpDataStream(config_.id);
490 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491}
492
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000493void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400494 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000495 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000498 while (!queued_received_data_.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700499 std::unique_ptr<DataBuffer> buffer(queued_received_data_.Front());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000500 observer_->OnMessage(*buffer);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000501 queued_received_data_.Pop();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 }
503}
504
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000505void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400506 if (queued_send_data_.Empty()) {
507 return;
508 }
509
510 ASSERT(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000511
Peter Boström0c4e06b2015-10-07 12:23:21 +0200512 uint64_t start_buffered_amount = buffered_amount();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000513 while (!queued_send_data_.Empty()) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000514 DataBuffer* buffer = queued_send_data_.Front();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000515 if (!SendDataMessage(*buffer, false)) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000516 // Leave the message in the queue if sending is aborted.
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000517 break;
518 }
519 queued_send_data_.Pop();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000520 delete buffer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000521 }
bemasc0edd50c2015-07-01 13:34:33 -0700522
523 if (observer_ && buffered_amount() < start_buffered_amount) {
524 observer_->OnBufferedAmountChange(start_buffered_amount);
525 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000526}
527
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000528bool DataChannel::SendDataMessage(const DataBuffer& buffer,
529 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000530 cricket::SendDataParams send_params;
531
wu@webrtc.org78187522013-10-07 23:32:02 +0000532 if (data_channel_type_ == cricket::DCT_SCTP) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000533 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400534 // Send as ordered if it is still going through OPEN/ACK signaling.
535 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000536 send_params.ordered = true;
537 LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel "
538 << "because the OPEN_ACK message has not been received.";
539 }
540
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000541 send_params.max_rtx_count = config_.maxRetransmits;
542 send_params.max_rtx_ms = config_.maxRetransmitTime;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000543 send_params.ssrc = config_.id;
544 } else {
545 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000546 }
547 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
548
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000549 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
550 bool success = provider_->SendData(send_params, buffer.data, &send_result);
551
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000552 if (success) {
553 return true;
554 }
555
556 if (data_channel_type_ != cricket::DCT_SCTP) {
557 return false;
558 }
559
560 if (send_result == cricket::SDR_BLOCK) {
561 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
562 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000563 }
564 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000565 // Close the channel if the error is not SDR_BLOCK, or if queuing the
566 // message failed.
567 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
568 << "send_result = " << send_result;
569 Close();
570
571 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000572}
573
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000574bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
bemasc0edd50c2015-07-01 13:34:33 -0700575 size_t start_buffered_amount = buffered_amount();
576 if (start_buffered_amount >= kMaxQueuedSendDataBytes) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000577 LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000578 return false;
579 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000580 queued_send_data_.Push(new DataBuffer(buffer));
bemasc0edd50c2015-07-01 13:34:33 -0700581
582 // The buffer can have length zero, in which case there is no change.
583 if (observer_ && buffered_amount() > start_buffered_amount) {
584 observer_->OnBufferedAmountChange(start_buffered_amount);
585 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000586 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587}
588
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000589void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000590 PacketQueue control_packets;
591 control_packets.Swap(&queued_control_data_);
592
593 while (!control_packets.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700594 std::unique_ptr<DataBuffer> buf(control_packets.Front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000595 SendControlMessage(buf->data);
596 control_packets.Pop();
597 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000598}
599
jbaucheec21bd2016-03-20 06:15:43 -0700600void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000601 queued_control_data_.Push(new DataBuffer(buffer, true));
602}
603
jbaucheec21bd2016-03-20 06:15:43 -0700604bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400605 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000606
607 ASSERT(data_channel_type_ == cricket::DCT_SCTP &&
Lally Singh5c6c6e02015-05-29 11:52:39 -0400608 writable_ &&
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000609 config_.id >= 0 &&
610 (!is_open_message || !config_.negotiated));
611
612 cricket::SendDataParams send_params;
613 send_params.ssrc = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400614 // Send data as ordered before we receive any message from the remote peer to
615 // make sure the remote peer will not receive any data before it receives the
616 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000617 send_params.ordered = config_.ordered || is_open_message;
618 send_params.type = cricket::DMT_CONTROL;
619
620 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
621 bool retval = provider_->SendData(send_params, buffer, &send_result);
622 if (retval) {
623 LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
624
Lally Singh5c6c6e02015-05-29 11:52:39 -0400625 if (handshake_state_ == kHandshakeShouldSendAck) {
626 handshake_state_ = kHandshakeReady;
627 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
628 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000629 }
630 } else if (send_result == cricket::SDR_BLOCK) {
631 QueueControlMessage(buffer);
632 } else {
633 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
634 << " the CONTROL message, send_result = " << send_result;
635 Close();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000636 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000637 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000638}
639
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640} // namespace webrtc