blob: 20cf7433555aa2262ff08f7cc41a0d6e43dc3f97 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +000027
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028#include "talk/app/webrtc/datachannel.h"
29
30#include <string>
31
wu@webrtc.org78187522013-10-07 23:32:02 +000032#include "talk/app/webrtc/mediastreamprovider.h"
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000033#include "talk/app/webrtc/sctputils.h"
deadbeefab9b2d12015-10-14 11:33:11 -070034#include "talk/media/sctp/sctpdataengine.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035#include "webrtc/base/logging.h"
36#include "webrtc/base/refcount.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037
38namespace webrtc {
39
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000040static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024;
41static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000043enum {
44 MSG_CHANNELREADY,
45};
46
deadbeefab9b2d12015-10-14 11:33:11 -070047bool SctpSidAllocator::AllocateSid(rtc::SSLRole role, int* sid) {
48 int potential_sid = (role == rtc::SSL_CLIENT) ? 0 : 1;
49 while (!IsSidAvailable(potential_sid)) {
50 potential_sid += 2;
51 if (potential_sid > static_cast<int>(cricket::kMaxSctpSid)) {
52 return false;
53 }
54 }
55
56 *sid = potential_sid;
57 used_sids_.insert(potential_sid);
58 return true;
59}
60
61bool SctpSidAllocator::ReserveSid(int sid) {
62 if (!IsSidAvailable(sid)) {
63 return false;
64 }
65 used_sids_.insert(sid);
66 return true;
67}
68
69void SctpSidAllocator::ReleaseSid(int sid) {
70 auto it = used_sids_.find(sid);
71 if (it != used_sids_.end()) {
72 used_sids_.erase(it);
73 }
74}
75
76bool SctpSidAllocator::IsSidAvailable(int sid) const {
77 if (sid < 0 || sid > static_cast<int>(cricket::kMaxSctpSid)) {
78 return false;
79 }
80 return used_sids_.find(sid) == used_sids_.end();
81}
82
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000083DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {}
84
85DataChannel::PacketQueue::~PacketQueue() {
86 Clear();
87}
88
89bool DataChannel::PacketQueue::Empty() const {
90 return packets_.empty();
91}
92
93DataBuffer* DataChannel::PacketQueue::Front() {
94 return packets_.front();
95}
96
97void DataChannel::PacketQueue::Pop() {
98 if (packets_.empty()) {
99 return;
100 }
101
102 byte_count_ -= packets_.front()->size();
103 packets_.pop_front();
104}
105
106void DataChannel::PacketQueue::Push(DataBuffer* packet) {
107 byte_count_ += packet->size();
108 packets_.push_back(packet);
109}
110
111void DataChannel::PacketQueue::Clear() {
112 while (!packets_.empty()) {
113 delete packets_.front();
114 packets_.pop_front();
115 }
116 byte_count_ = 0;
117}
118
119void DataChannel::PacketQueue::Swap(PacketQueue* other) {
120 size_t other_byte_count = other->byte_count_;
121 other->byte_count_ = byte_count_;
122 byte_count_ = other_byte_count;
123
124 other->packets_.swap(packets_);
125}
126
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000127rtc::scoped_refptr<DataChannel> DataChannel::Create(
wu@webrtc.org78187522013-10-07 23:32:02 +0000128 DataChannelProviderInterface* provider,
129 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000131 const InternalDataChannelInit& config) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000132 rtc::scoped_refptr<DataChannel> channel(
133 new rtc::RefCountedObject<DataChannel>(provider, dct, label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 if (!channel->Init(config)) {
135 return NULL;
136 }
137 return channel;
138}
139
wu@webrtc.org78187522013-10-07 23:32:02 +0000140DataChannel::DataChannel(
141 DataChannelProviderInterface* provider,
142 cricket::DataChannelType dct,
143 const std::string& label)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 : label_(label),
145 observer_(NULL),
146 state_(kConnecting),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000147 data_channel_type_(dct),
148 provider_(provider),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400149 handshake_state_(kHandshakeInit),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000150 connected_to_provider_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 send_ssrc_set_(false),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000152 receive_ssrc_set_(false),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400153 writable_(false),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000154 send_ssrc_(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 receive_ssrc_(0) {
156}
157
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000158bool DataChannel::Init(const InternalDataChannelInit& config) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400159 if (data_channel_type_ == cricket::DCT_RTP) {
160 if (config.reliable ||
161 config.id != -1 ||
162 config.maxRetransmits != -1 ||
163 config.maxRetransmitTime != -1) {
164 LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
165 << "invalid DataChannelInit.";
166 return false;
167 }
168 handshake_state_ = kHandshakeReady;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000169 } else if (data_channel_type_ == cricket::DCT_SCTP) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000170 if (config.id < -1 ||
171 config.maxRetransmits < -1 ||
172 config.maxRetransmitTime < -1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000173 LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 << "invalid DataChannelInit.";
175 return false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000176 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000177 if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000178 LOG(LS_ERROR) <<
179 "maxRetransmits and maxRetransmitTime should not be both set.";
180 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000182 config_ = config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183
Lally Singh5c6c6e02015-05-29 11:52:39 -0400184 switch (config_.open_handshake_role) {
185 case webrtc::InternalDataChannelInit::kNone: // pre-negotiated
186 handshake_state_ = kHandshakeReady;
187 break;
188 case webrtc::InternalDataChannelInit::kOpener:
189 handshake_state_ = kHandshakeShouldSendOpen;
190 break;
191 case webrtc::InternalDataChannelInit::kAcker:
192 handshake_state_ = kHandshakeShouldSendAck;
193 break;
194 };
195
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000196 // Try to connect to the transport in case the transport channel already
197 // exists.
198 OnTransportChannelCreated();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000199
200 // Checks if the transport is ready to send because the initial channel
201 // ready signal may have been sent before the DataChannel creation.
202 // This has to be done async because the upper layer objects (e.g.
203 // Chrome glue and WebKit) are not wired up properly until after this
204 // function returns.
205 if (provider_->ReadyToSendData()) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000206 rtc::Thread::Current()->Post(this, MSG_CHANNELREADY, NULL);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000207 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000208 }
209
210 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211}
212
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000213DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214
215void DataChannel::RegisterObserver(DataChannelObserver* observer) {
216 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000217 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218}
219
220void DataChannel::UnregisterObserver() {
221 observer_ = NULL;
222}
223
224bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02 +0000225 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 return false;
227 } else {
228 return config_.maxRetransmits == -1 &&
229 config_.maxRetransmitTime == -1;
230 }
231}
232
Peter Boström0c4e06b2015-10-07 12:23:21 +0200233uint64_t DataChannel::buffered_amount() const {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000234 return queued_send_data_.byte_count();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235}
236
237void DataChannel::Close() {
238 if (state_ == kClosed)
239 return;
240 send_ssrc_ = 0;
241 send_ssrc_set_ = false;
242 SetState(kClosing);
243 UpdateState();
244}
245
246bool DataChannel::Send(const DataBuffer& buffer) {
247 if (state_ != kOpen) {
248 return false;
249 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000250
251 // TODO(jiayl): the spec is unclear about if the remote side should get the
252 // onmessage event. We need to figure out the expected behavior and change the
253 // code accordingly.
254 if (buffer.size() == 0) {
255 return true;
256 }
257
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000258 // If the queue is non-empty, we're waiting for SignalReadyToSend,
259 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000260 if (!queued_send_data_.Empty()) {
261 // Only SCTP DataChannel queues the outgoing data when the transport is
262 // blocked.
263 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
264 if (!QueueSendDataMessage(buffer)) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000265 Close();
266 }
267 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000270 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000271 if (data_channel_type_ == cricket::DCT_RTP) {
272 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000273 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000274
275 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000276 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277}
278
Peter Boström0c4e06b2015-10-07 12:23:21 +0200279void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000280 ASSERT(data_channel_type_ == cricket::DCT_RTP);
281
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 return;
284 }
285 receive_ssrc_ = receive_ssrc;
286 receive_ssrc_set_ = true;
287 UpdateState();
288}
289
290// The remote peer request that this channel shall be closed.
291void DataChannel::RemotePeerRequestClose() {
292 DoClose();
293}
294
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000295void DataChannel::SetSctpSid(int sid) {
296 ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP);
deadbeefab9b2d12015-10-14 11:33:11 -0700297 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000298 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700299 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000300
301 config_.id = sid;
302 provider_->AddSctpDataStream(sid);
303}
304
305void DataChannel::OnTransportChannelCreated() {
306 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
307 if (!connected_to_provider_) {
308 connected_to_provider_ = provider_->ConnectDataChannel(this);
309 }
310 // The sid may have been unassigned when provider_->ConnectDataChannel was
311 // done. So always add the streams even if connected_to_provider_ is true.
312 if (config_.id >= 0) {
313 provider_->AddSctpDataStream(config_.id);
314 }
315}
316
deadbeefab9b2d12015-10-14 11:33:11 -0700317// The underlying transport channel was destroyed.
318// This function makes sure the DataChannel is disconnected and changes state to
319// kClosed.
320void DataChannel::OnTransportChannelDestroyed() {
321 DoClose();
322}
323
Peter Boström0c4e06b2015-10-07 12:23:21 +0200324void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000325 ASSERT(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 return;
328 }
329 send_ssrc_ = send_ssrc;
330 send_ssrc_set_ = true;
331 UpdateState();
332}
333
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000334void DataChannel::OnMessage(rtc::Message* msg) {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000335 switch (msg->message_id) {
336 case MSG_CHANNELREADY:
337 OnChannelReady(true);
338 break;
339 }
340}
341
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000342void DataChannel::OnDataReceived(cricket::DataChannel* channel,
343 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000344 const rtc::Buffer& payload) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200345 uint32_t expected_ssrc =
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000346 (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id;
347 if (params.ssrc != expected_ssrc) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000348 return;
349 }
350
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000351 if (params.type == cricket::DMT_CONTROL) {
352 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400353 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000354 // Ignore it if we are not expecting an ACK message.
355 LOG(LS_WARNING) << "DataChannel received unexpected CONTROL message, "
356 << "sid = " << params.ssrc;
357 return;
358 }
359 if (ParseDataChannelOpenAckMessage(payload)) {
360 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400361 handshake_state_ = kHandshakeReady;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000362 LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
363 << params.ssrc;
364 } else {
365 LOG(LS_WARNING) << "DataChannel failed to parse OPEN_ACK message, sid = "
366 << params.ssrc;
367 }
368 return;
369 }
370
371 ASSERT(params.type == cricket::DMT_BINARY ||
372 params.type == cricket::DMT_TEXT);
373
374 LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " << params.ssrc;
375 // We can send unordered as soon as we receive any DATA message since the
376 // remote side must have received the OPEN (and old clients do not send
377 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400378 if (handshake_state_ == kHandshakeWaitingForAck) {
379 handshake_state_ = kHandshakeReady;
380 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000381
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000382 bool binary = (params.type == cricket::DMT_BINARY);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000383 rtc::scoped_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400384 if (state_ == kOpen && observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000385 observer_->OnMessage(*buffer.get());
386 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000387 if (queued_received_data_.byte_count() + payload.size() >
388 kMaxQueuedReceivedDataBytes) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000389 LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
390
391 queued_received_data_.Clear();
392 if (data_channel_type_ != cricket::DCT_RTP) {
393 Close();
394 }
395
396 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000397 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000398 queued_received_data_.Push(buffer.release());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000399 }
400}
401
deadbeefab9b2d12015-10-14 11:33:11 -0700402void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
403 if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id) {
404 Close();
405 }
406}
407
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000408void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400409 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000410 if (!writable) {
411 return;
412 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000413
Lally Singh5c6c6e02015-05-29 11:52:39 -0400414 SendQueuedControlMessages();
415 SendQueuedDataMessages();
416 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000417}
418
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419void DataChannel::DoClose() {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000420 if (state_ == kClosed)
421 return;
422
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 receive_ssrc_set_ = false;
424 send_ssrc_set_ = false;
425 SetState(kClosing);
426 UpdateState();
427}
428
429void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400430 // UpdateState determines what to do from a few state variables. Include
431 // all conditions required for each state transition here for
432 // clarity. OnChannelReady(true) will send any queued data and then invoke
433 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434 switch (state_) {
435 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000436 if (send_ssrc_set_ == receive_ssrc_set_) {
437 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
438 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400440 if (connected_to_provider_) {
441 if (handshake_state_ == kHandshakeShouldSendOpen) {
442 rtc::Buffer payload;
443 WriteDataChannelOpenMessage(label_, config_, &payload);
444 SendControlMessage(payload);
445 } else if (handshake_state_ == kHandshakeShouldSendAck) {
446 rtc::Buffer payload;
447 WriteDataChannelOpenAckMessage(&payload);
448 SendControlMessage(payload);
449 }
450 if (writable_ &&
451 (handshake_state_ == kHandshakeReady ||
452 handshake_state_ == kHandshakeWaitingForAck)) {
453 SetState(kOpen);
454 // If we have received buffers before the channel got writable.
455 // Deliver them now.
456 DeliverQueuedReceivedData();
457 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 }
459 }
460 break;
461 }
462 case kOpen: {
463 break;
464 }
465 case kClosing: {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400466 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
467 if (connected_to_provider_) {
468 DisconnectFromProvider();
469 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000470
Lally Singh5c6c6e02015-05-29 11:52:39 -0400471 if (!connected_to_provider_ && !send_ssrc_set_ && !receive_ssrc_set_) {
472 SetState(kClosed);
473 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 }
475 break;
476 }
477 case kClosed:
478 break;
479 }
480}
481
482void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700483 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000484 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700485 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000486
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 state_ = state;
488 if (observer_) {
489 observer_->OnStateChange();
490 }
deadbeefab9b2d12015-10-14 11:33:11 -0700491 if (state_ == kClosed) {
492 SignalClosed(this);
493 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494}
495
Lally Singh5c6c6e02015-05-29 11:52:39 -0400496void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000497 if (!connected_to_provider_)
498 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499
wu@webrtc.org78187522013-10-07 23:32:02 +0000500 provider_->DisconnectDataChannel(this);
501 connected_to_provider_ = false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000502
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +0000503 if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000504 provider_->RemoveSctpDataStream(config_.id);
505 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506}
507
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000508void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400509 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000510 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000513 while (!queued_received_data_.Empty()) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000514 rtc::scoped_ptr<DataBuffer> buffer(queued_received_data_.Front());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000515 observer_->OnMessage(*buffer);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000516 queued_received_data_.Pop();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 }
518}
519
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000520void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400521 if (queued_send_data_.Empty()) {
522 return;
523 }
524
525 ASSERT(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000526
Peter Boström0c4e06b2015-10-07 12:23:21 +0200527 uint64_t start_buffered_amount = buffered_amount();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000528 while (!queued_send_data_.Empty()) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000529 DataBuffer* buffer = queued_send_data_.Front();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000530 if (!SendDataMessage(*buffer, false)) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000531 // Leave the message in the queue if sending is aborted.
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000532 break;
533 }
534 queued_send_data_.Pop();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000535 delete buffer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000536 }
bemasc0edd50c2015-07-01 13:34:33 -0700537
538 if (observer_ && buffered_amount() < start_buffered_amount) {
539 observer_->OnBufferedAmountChange(start_buffered_amount);
540 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000541}
542
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000543bool DataChannel::SendDataMessage(const DataBuffer& buffer,
544 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000545 cricket::SendDataParams send_params;
546
wu@webrtc.org78187522013-10-07 23:32:02 +0000547 if (data_channel_type_ == cricket::DCT_SCTP) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000548 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400549 // Send as ordered if it is still going through OPEN/ACK signaling.
550 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000551 send_params.ordered = true;
552 LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel "
553 << "because the OPEN_ACK message has not been received.";
554 }
555
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000556 send_params.max_rtx_count = config_.maxRetransmits;
557 send_params.max_rtx_ms = config_.maxRetransmitTime;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000558 send_params.ssrc = config_.id;
559 } else {
560 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000561 }
562 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
563
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000564 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
565 bool success = provider_->SendData(send_params, buffer.data, &send_result);
566
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000567 if (success) {
568 return true;
569 }
570
571 if (data_channel_type_ != cricket::DCT_SCTP) {
572 return false;
573 }
574
575 if (send_result == cricket::SDR_BLOCK) {
576 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
577 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000578 }
579 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000580 // Close the channel if the error is not SDR_BLOCK, or if queuing the
581 // message failed.
582 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
583 << "send_result = " << send_result;
584 Close();
585
586 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000587}
588
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000589bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
bemasc0edd50c2015-07-01 13:34:33 -0700590 size_t start_buffered_amount = buffered_amount();
591 if (start_buffered_amount >= kMaxQueuedSendDataBytes) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000592 LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000593 return false;
594 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000595 queued_send_data_.Push(new DataBuffer(buffer));
bemasc0edd50c2015-07-01 13:34:33 -0700596
597 // The buffer can have length zero, in which case there is no change.
598 if (observer_ && buffered_amount() > start_buffered_amount) {
599 observer_->OnBufferedAmountChange(start_buffered_amount);
600 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000601 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602}
603
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000604void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000605 PacketQueue control_packets;
606 control_packets.Swap(&queued_control_data_);
607
608 while (!control_packets.Empty()) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000609 rtc::scoped_ptr<DataBuffer> buf(control_packets.Front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000610 SendControlMessage(buf->data);
611 control_packets.Pop();
612 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000613}
614
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000615void DataChannel::QueueControlMessage(const rtc::Buffer& buffer) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000616 queued_control_data_.Push(new DataBuffer(buffer, true));
617}
618
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000619bool DataChannel::SendControlMessage(const rtc::Buffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400620 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000621
622 ASSERT(data_channel_type_ == cricket::DCT_SCTP &&
Lally Singh5c6c6e02015-05-29 11:52:39 -0400623 writable_ &&
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000624 config_.id >= 0 &&
625 (!is_open_message || !config_.negotiated));
626
627 cricket::SendDataParams send_params;
628 send_params.ssrc = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400629 // Send data as ordered before we receive any message from the remote peer to
630 // make sure the remote peer will not receive any data before it receives the
631 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000632 send_params.ordered = config_.ordered || is_open_message;
633 send_params.type = cricket::DMT_CONTROL;
634
635 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
636 bool retval = provider_->SendData(send_params, buffer, &send_result);
637 if (retval) {
638 LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
639
Lally Singh5c6c6e02015-05-29 11:52:39 -0400640 if (handshake_state_ == kHandshakeShouldSendAck) {
641 handshake_state_ = kHandshakeReady;
642 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
643 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000644 }
645 } else if (send_result == cricket::SDR_BLOCK) {
646 QueueControlMessage(buffer);
647 } else {
648 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
649 << " the CONTROL message, send_result = " << send_result;
650 Close();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000651 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000652 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000653}
654
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655} // namespace webrtc