blob: c0bc3dc42b3e63c341d8bca7107dee0fefb347bb [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"
nisseede5da42017-01-12 05:15:36 -080017#include "webrtc/base/checks.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018#include "webrtc/base/logging.h"
19#include "webrtc/base/refcount.h"
deadbeef953c2ce2017-01-09 14:53:41 -080020#include "webrtc/media/sctp/sctptransportinternal.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) {
153 LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
154 << "invalid DataChannelInit.";
155 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) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000162 LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 << "invalid DataChannelInit.";
164 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) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000167 LOG(LS_ERROR) <<
168 "maxRetransmits and maxRetransmitTime should not be both set.";
169 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;
183 };
184
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);
232 UpdateState();
233}
234
235bool DataChannel::Send(const DataBuffer& buffer) {
236 if (state_ != kOpen) {
237 return false;
238 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000239
240 // TODO(jiayl): the spec is unclear about if the remote side should get the
241 // onmessage event. We need to figure out the expected behavior and change the
242 // code accordingly.
243 if (buffer.size() == 0) {
244 return true;
245 }
246
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000247 // If the queue is non-empty, we're waiting for SignalReadyToSend,
248 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000249 if (!queued_send_data_.Empty()) {
250 // Only SCTP DataChannel queues the outgoing data when the transport is
251 // blocked.
nisseede5da42017-01-12 05:15:36 -0800252 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000253 if (!QueueSendDataMessage(buffer)) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000254 Close();
255 }
256 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000259 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000260 if (data_channel_type_ == cricket::DCT_RTP) {
261 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000262 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000263
264 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000265 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266}
267
Peter Boström0c4e06b2015-10-07 12:23:21 +0200268void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800269 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000270
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 return;
273 }
274 receive_ssrc_ = receive_ssrc;
275 receive_ssrc_set_ = true;
276 UpdateState();
277}
278
279// The remote peer request that this channel shall be closed.
280void DataChannel::RemotePeerRequestClose() {
281 DoClose();
282}
283
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000284void DataChannel::SetSctpSid(int sid) {
nisseede5da42017-01-12 05:15:36 -0800285 RTC_DCHECK(config_.id < 0 && sid >= 0 &&
286 data_channel_type_ == cricket::DCT_SCTP);
deadbeefab9b2d12015-10-14 11:33:11 -0700287 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000288 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700289 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000290
291 config_.id = sid;
292 provider_->AddSctpDataStream(sid);
293}
294
295void DataChannel::OnTransportChannelCreated() {
nisseede5da42017-01-12 05:15:36 -0800296 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000297 if (!connected_to_provider_) {
298 connected_to_provider_ = provider_->ConnectDataChannel(this);
299 }
300 // The sid may have been unassigned when provider_->ConnectDataChannel was
301 // done. So always add the streams even if connected_to_provider_ is true.
302 if (config_.id >= 0) {
303 provider_->AddSctpDataStream(config_.id);
304 }
305}
306
deadbeefab9b2d12015-10-14 11:33:11 -0700307void DataChannel::OnTransportChannelDestroyed() {
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700308 // This method needs to synchronously close the data channel, which means any
309 // queued data needs to be discarded.
310 queued_send_data_.Clear();
311 queued_control_data_.Clear();
deadbeefab9b2d12015-10-14 11:33:11 -0700312 DoClose();
313}
314
Peter Boström0c4e06b2015-10-07 12:23:21 +0200315void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
nisseede5da42017-01-12 05:15:36 -0800316 RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 return;
319 }
320 send_ssrc_ = send_ssrc;
321 send_ssrc_set_ = true;
322 UpdateState();
323}
324
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000325void DataChannel::OnMessage(rtc::Message* msg) {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000326 switch (msg->message_id) {
327 case MSG_CHANNELREADY:
328 OnChannelReady(true);
329 break;
330 }
331}
332
deadbeef953c2ce2017-01-09 14:53:41 -0800333void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700334 const rtc::CopyOnWriteBuffer& payload) {
deadbeef953c2ce2017-01-09 14:53:41 -0800335 if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) {
336 return;
337 }
338 if (data_channel_type_ == cricket::DCT_SCTP && params.sid != config_.id) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000339 return;
340 }
341
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000342 if (params.type == cricket::DMT_CONTROL) {
nisseede5da42017-01-12 05:15:36 -0800343 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400344 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000345 // Ignore it if we are not expecting an ACK message.
346 LOG(LS_WARNING) << "DataChannel received unexpected CONTROL message, "
deadbeef953c2ce2017-01-09 14:53:41 -0800347 << "sid = " << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000348 return;
349 }
350 if (ParseDataChannelOpenAckMessage(payload)) {
351 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400352 handshake_state_ = kHandshakeReady;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000353 LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
deadbeef953c2ce2017-01-09 14:53:41 -0800354 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000355 } else {
356 LOG(LS_WARNING) << "DataChannel failed to parse OPEN_ACK message, sid = "
deadbeef953c2ce2017-01-09 14:53:41 -0800357 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000358 }
359 return;
360 }
361
nisseede5da42017-01-12 05:15:36 -0800362 RTC_DCHECK(params.type == cricket::DMT_BINARY ||
363 params.type == cricket::DMT_TEXT);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000364
deadbeef953c2ce2017-01-09 14:53:41 -0800365 LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000366 // We can send unordered as soon as we receive any DATA message since the
367 // remote side must have received the OPEN (and old clients do not send
368 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400369 if (handshake_state_ == kHandshakeWaitingForAck) {
370 handshake_state_ = kHandshakeReady;
371 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000372
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000373 bool binary = (params.type == cricket::DMT_BINARY);
kwibergd1fe2812016-04-27 06:47:29 -0700374 std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400375 if (state_ == kOpen && observer_) {
hbos84ffdee2016-10-12 14:14:39 -0700376 ++messages_received_;
377 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000378 observer_->OnMessage(*buffer.get());
379 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000380 if (queued_received_data_.byte_count() + payload.size() >
381 kMaxQueuedReceivedDataBytes) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000382 LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
383
384 queued_received_data_.Clear();
385 if (data_channel_type_ != cricket::DCT_RTP) {
386 Close();
387 }
388
389 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000390 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000391 queued_received_data_.Push(buffer.release());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000392 }
393}
394
deadbeef953c2ce2017-01-09 14:53:41 -0800395void DataChannel::OnStreamClosedRemotely(int sid) {
396 if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id) {
deadbeefab9b2d12015-10-14 11:33:11 -0700397 Close();
398 }
399}
400
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000401void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400402 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000403 if (!writable) {
404 return;
405 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000406
Lally Singh5c6c6e02015-05-29 11:52:39 -0400407 SendQueuedControlMessages();
408 SendQueuedDataMessages();
409 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000410}
411
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412void DataChannel::DoClose() {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000413 if (state_ == kClosed)
414 return;
415
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 receive_ssrc_set_ = false;
417 send_ssrc_set_ = false;
418 SetState(kClosing);
419 UpdateState();
420}
421
422void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400423 // UpdateState determines what to do from a few state variables. Include
424 // all conditions required for each state transition here for
425 // clarity. OnChannelReady(true) will send any queued data and then invoke
426 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427 switch (state_) {
428 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000429 if (send_ssrc_set_ == receive_ssrc_set_) {
430 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
431 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400433 if (connected_to_provider_) {
434 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 06:15:43 -0700435 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400436 WriteDataChannelOpenMessage(label_, config_, &payload);
437 SendControlMessage(payload);
438 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 06:15:43 -0700439 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400440 WriteDataChannelOpenAckMessage(&payload);
441 SendControlMessage(payload);
442 }
443 if (writable_ &&
444 (handshake_state_ == kHandshakeReady ||
445 handshake_state_ == kHandshakeWaitingForAck)) {
446 SetState(kOpen);
447 // If we have received buffers before the channel got writable.
448 // Deliver them now.
449 DeliverQueuedReceivedData();
450 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 }
452 }
453 break;
454 }
455 case kOpen: {
456 break;
457 }
458 case kClosing: {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400459 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
460 if (connected_to_provider_) {
461 DisconnectFromProvider();
462 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000463
Lally Singh5c6c6e02015-05-29 11:52:39 -0400464 if (!connected_to_provider_ && !send_ssrc_set_ && !receive_ssrc_set_) {
465 SetState(kClosed);
466 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 }
468 break;
469 }
470 case kClosed:
471 break;
472 }
473}
474
475void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700476 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000477 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700478 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000479
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 state_ = state;
481 if (observer_) {
482 observer_->OnStateChange();
483 }
hbos82ebe022016-11-14 01:41:09 -0800484 if (state_ == kOpen) {
485 SignalOpened(this);
486 } else if (state_ == kClosed) {
deadbeefab9b2d12015-10-14 11:33:11 -0700487 SignalClosed(this);
488 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489}
490
Lally Singh5c6c6e02015-05-29 11:52:39 -0400491void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000492 if (!connected_to_provider_)
493 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494
wu@webrtc.org78187522013-10-07 23:32:02 +0000495 provider_->DisconnectDataChannel(this);
496 connected_to_provider_ = false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000497
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +0000498 if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000499 provider_->RemoveSctpDataStream(config_.id);
500 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501}
502
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000503void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400504 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000505 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000508 while (!queued_received_data_.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700509 std::unique_ptr<DataBuffer> buffer(queued_received_data_.Front());
hbos84ffdee2016-10-12 14:14:39 -0700510 ++messages_received_;
511 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000512 observer_->OnMessage(*buffer);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000513 queued_received_data_.Pop();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 }
515}
516
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000517void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400518 if (queued_send_data_.Empty()) {
519 return;
520 }
521
nisseede5da42017-01-12 05:15:36 -0800522 RTC_DCHECK(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000523
Peter Boström0c4e06b2015-10-07 12:23:21 +0200524 uint64_t start_buffered_amount = buffered_amount();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000525 while (!queued_send_data_.Empty()) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000526 DataBuffer* buffer = queued_send_data_.Front();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000527 if (!SendDataMessage(*buffer, false)) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000528 // Leave the message in the queue if sending is aborted.
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000529 break;
530 }
531 queued_send_data_.Pop();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000532 delete buffer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000533 }
bemasc0edd50c2015-07-01 13:34:33 -0700534
535 if (observer_ && buffered_amount() < start_buffered_amount) {
536 observer_->OnBufferedAmountChange(start_buffered_amount);
537 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000538}
539
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000540bool DataChannel::SendDataMessage(const DataBuffer& buffer,
541 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000542 cricket::SendDataParams send_params;
543
wu@webrtc.org78187522013-10-07 23:32:02 +0000544 if (data_channel_type_ == cricket::DCT_SCTP) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000545 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400546 // Send as ordered if it is still going through OPEN/ACK signaling.
547 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000548 send_params.ordered = true;
549 LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel "
550 << "because the OPEN_ACK message has not been received.";
551 }
552
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000553 send_params.max_rtx_count = config_.maxRetransmits;
554 send_params.max_rtx_ms = config_.maxRetransmitTime;
deadbeef953c2ce2017-01-09 14:53:41 -0800555 send_params.sid = config_.id;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000556 } else {
557 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000558 }
559 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
560
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000561 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
562 bool success = provider_->SendData(send_params, buffer.data, &send_result);
563
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000564 if (success) {
hbos84ffdee2016-10-12 14:14:39 -0700565 ++messages_sent_;
566 bytes_sent_ += buffer.size();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000567 return true;
568 }
569
570 if (data_channel_type_ != cricket::DCT_SCTP) {
571 return false;
572 }
573
574 if (send_result == cricket::SDR_BLOCK) {
575 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
576 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000577 }
578 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000579 // Close the channel if the error is not SDR_BLOCK, or if queuing the
580 // message failed.
581 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
582 << "send_result = " << send_result;
583 Close();
584
585 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000586}
587
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000588bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
bemasc0edd50c2015-07-01 13:34:33 -0700589 size_t start_buffered_amount = buffered_amount();
590 if (start_buffered_amount >= kMaxQueuedSendDataBytes) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000591 LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000592 return false;
593 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000594 queued_send_data_.Push(new DataBuffer(buffer));
bemasc0edd50c2015-07-01 13:34:33 -0700595
596 // The buffer can have length zero, in which case there is no change.
597 if (observer_ && buffered_amount() > start_buffered_amount) {
598 observer_->OnBufferedAmountChange(start_buffered_amount);
599 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000600 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601}
602
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000603void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000604 PacketQueue control_packets;
605 control_packets.Swap(&queued_control_data_);
606
607 while (!control_packets.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700608 std::unique_ptr<DataBuffer> buf(control_packets.Front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000609 SendControlMessage(buf->data);
610 control_packets.Pop();
611 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000612}
613
jbaucheec21bd2016-03-20 06:15:43 -0700614void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000615 queued_control_data_.Push(new DataBuffer(buffer, true));
616}
617
jbaucheec21bd2016-03-20 06:15:43 -0700618bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400619 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000620
nisseede5da42017-01-12 05:15:36 -0800621 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP && writable_ &&
622 config_.id >= 0 && (!is_open_message || !config_.negotiated));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000623
624 cricket::SendDataParams send_params;
deadbeef953c2ce2017-01-09 14:53:41 -0800625 send_params.sid = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400626 // Send data as ordered before we receive any message from the remote peer to
627 // make sure the remote peer will not receive any data before it receives the
628 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000629 send_params.ordered = config_.ordered || is_open_message;
630 send_params.type = cricket::DMT_CONTROL;
631
632 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
633 bool retval = provider_->SendData(send_params, buffer, &send_result);
634 if (retval) {
635 LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
636
Lally Singh5c6c6e02015-05-29 11:52:39 -0400637 if (handshake_state_ == kHandshakeShouldSendAck) {
638 handshake_state_ = kHandshakeReady;
639 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
640 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000641 }
642 } else if (send_result == cricket::SDR_BLOCK) {
643 QueueControlMessage(buffer);
644 } else {
645 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
646 << " the CONTROL message, send_result = " << send_result;
647 Close();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000648 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000649 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000650}
651
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652} // namespace webrtc