blob: 1897b73fc88e0ed0b091bf87c4c1b52113346a30 [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"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/logging.h"
35#include "webrtc/base/refcount.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37namespace webrtc {
38
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000039static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024;
40static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000042enum {
43 MSG_CHANNELREADY,
44};
45
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000046DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {}
47
48DataChannel::PacketQueue::~PacketQueue() {
49 Clear();
50}
51
52bool DataChannel::PacketQueue::Empty() const {
53 return packets_.empty();
54}
55
56DataBuffer* DataChannel::PacketQueue::Front() {
57 return packets_.front();
58}
59
60void DataChannel::PacketQueue::Pop() {
61 if (packets_.empty()) {
62 return;
63 }
64
65 byte_count_ -= packets_.front()->size();
66 packets_.pop_front();
67}
68
69void DataChannel::PacketQueue::Push(DataBuffer* packet) {
70 byte_count_ += packet->size();
71 packets_.push_back(packet);
72}
73
74void DataChannel::PacketQueue::Clear() {
75 while (!packets_.empty()) {
76 delete packets_.front();
77 packets_.pop_front();
78 }
79 byte_count_ = 0;
80}
81
82void DataChannel::PacketQueue::Swap(PacketQueue* other) {
83 size_t other_byte_count = other->byte_count_;
84 other->byte_count_ = byte_count_;
85 byte_count_ = other_byte_count;
86
87 other->packets_.swap(packets_);
88}
89
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000090rtc::scoped_refptr<DataChannel> DataChannel::Create(
wu@webrtc.org78187522013-10-07 23:32:02 +000091 DataChannelProviderInterface* provider,
92 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000094 const InternalDataChannelInit& config) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000095 rtc::scoped_refptr<DataChannel> channel(
96 new rtc::RefCountedObject<DataChannel>(provider, dct, label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 if (!channel->Init(config)) {
98 return NULL;
99 }
100 return channel;
101}
102
wu@webrtc.org78187522013-10-07 23:32:02 +0000103DataChannel::DataChannel(
104 DataChannelProviderInterface* provider,
105 cricket::DataChannelType dct,
106 const std::string& label)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 : label_(label),
108 observer_(NULL),
109 state_(kConnecting),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000110 data_channel_type_(dct),
111 provider_(provider),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000112 waiting_for_open_ack_(false),
113 was_ever_writable_(false),
114 connected_to_provider_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 send_ssrc_set_(false),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000116 receive_ssrc_set_(false),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000117 send_ssrc_(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 receive_ssrc_(0) {
119}
120
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000121bool DataChannel::Init(const InternalDataChannelInit& config) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000122 if (data_channel_type_ == cricket::DCT_RTP &&
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000123 (config.reliable ||
124 config.id != -1 ||
125 config.maxRetransmits != -1 ||
126 config.maxRetransmitTime != -1)) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000127 LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
128 << "invalid DataChannelInit.";
129 return false;
130 } else if (data_channel_type_ == cricket::DCT_SCTP) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000131 if (config.id < -1 ||
132 config.maxRetransmits < -1 ||
133 config.maxRetransmitTime < -1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000134 LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 << "invalid DataChannelInit.";
136 return false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000137 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000138 if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000139 LOG(LS_ERROR) <<
140 "maxRetransmits and maxRetransmitTime should not be both set.";
141 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000143 config_ = config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000145 // Try to connect to the transport in case the transport channel already
146 // exists.
147 OnTransportChannelCreated();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000148
149 // Checks if the transport is ready to send because the initial channel
150 // ready signal may have been sent before the DataChannel creation.
151 // This has to be done async because the upper layer objects (e.g.
152 // Chrome glue and WebKit) are not wired up properly until after this
153 // function returns.
154 if (provider_->ReadyToSendData()) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000155 rtc::Thread::Current()->Post(this, MSG_CHANNELREADY, NULL);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000156 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000157 }
158
159 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160}
161
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000162DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163
164void DataChannel::RegisterObserver(DataChannelObserver* observer) {
165 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000166 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167}
168
169void DataChannel::UnregisterObserver() {
170 observer_ = NULL;
171}
172
173bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02 +0000174 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 return false;
176 } else {
177 return config_.maxRetransmits == -1 &&
178 config_.maxRetransmitTime == -1;
179 }
180}
181
182uint64 DataChannel::buffered_amount() const {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000183 return queued_send_data_.byte_count();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184}
185
186void DataChannel::Close() {
187 if (state_ == kClosed)
188 return;
189 send_ssrc_ = 0;
190 send_ssrc_set_ = false;
191 SetState(kClosing);
192 UpdateState();
193}
194
195bool DataChannel::Send(const DataBuffer& buffer) {
196 if (state_ != kOpen) {
197 return false;
198 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000199
200 // TODO(jiayl): the spec is unclear about if the remote side should get the
201 // onmessage event. We need to figure out the expected behavior and change the
202 // code accordingly.
203 if (buffer.size() == 0) {
204 return true;
205 }
206
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000207 // If the queue is non-empty, we're waiting for SignalReadyToSend,
208 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000209 if (!queued_send_data_.Empty()) {
210 // Only SCTP DataChannel queues the outgoing data when the transport is
211 // blocked.
212 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
213 if (!QueueSendDataMessage(buffer)) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000214 Close();
215 }
216 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000219 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000220 if (data_channel_type_ == cricket::DCT_RTP) {
221 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000222 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000223
224 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000225 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226}
227
228void DataChannel::SetReceiveSsrc(uint32 receive_ssrc) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000229 ASSERT(data_channel_type_ == cricket::DCT_RTP);
230
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 return;
233 }
234 receive_ssrc_ = receive_ssrc;
235 receive_ssrc_set_ = true;
236 UpdateState();
237}
238
239// The remote peer request that this channel shall be closed.
240void DataChannel::RemotePeerRequestClose() {
241 DoClose();
242}
243
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000244void DataChannel::SetSctpSid(int sid) {
245 ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP);
246 if (config_.id == sid)
247 return;
248
249 config_.id = sid;
250 provider_->AddSctpDataStream(sid);
251}
252
253void DataChannel::OnTransportChannelCreated() {
254 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
255 if (!connected_to_provider_) {
256 connected_to_provider_ = provider_->ConnectDataChannel(this);
257 }
258 // The sid may have been unassigned when provider_->ConnectDataChannel was
259 // done. So always add the streams even if connected_to_provider_ is true.
260 if (config_.id >= 0) {
261 provider_->AddSctpDataStream(config_.id);
262 }
263}
264
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265void DataChannel::SetSendSsrc(uint32 send_ssrc) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000266 ASSERT(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 return;
269 }
270 send_ssrc_ = send_ssrc;
271 send_ssrc_set_ = true;
272 UpdateState();
273}
274
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000275void DataChannel::OnMessage(rtc::Message* msg) {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000276 switch (msg->message_id) {
277 case MSG_CHANNELREADY:
278 OnChannelReady(true);
279 break;
280 }
281}
282
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283// The underlaying data engine is closing.
wu@webrtc.org78187522013-10-07 23:32:02 +0000284// This function makes sure the DataChannel is disconnected and changes state to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285// kClosed.
286void DataChannel::OnDataEngineClose() {
287 DoClose();
288}
289
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000290void DataChannel::OnDataReceived(cricket::DataChannel* channel,
291 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000292 const rtc::Buffer& payload) {
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000293 uint32 expected_ssrc =
294 (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id;
295 if (params.ssrc != expected_ssrc) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000296 return;
297 }
298
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000299 if (params.type == cricket::DMT_CONTROL) {
300 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
301 if (!waiting_for_open_ack_) {
302 // Ignore it if we are not expecting an ACK message.
303 LOG(LS_WARNING) << "DataChannel received unexpected CONTROL message, "
304 << "sid = " << params.ssrc;
305 return;
306 }
307 if (ParseDataChannelOpenAckMessage(payload)) {
308 // We can send unordered as soon as we receive the ACK message.
309 waiting_for_open_ack_ = false;
310 LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
311 << params.ssrc;
312 } else {
313 LOG(LS_WARNING) << "DataChannel failed to parse OPEN_ACK message, sid = "
314 << params.ssrc;
315 }
316 return;
317 }
318
319 ASSERT(params.type == cricket::DMT_BINARY ||
320 params.type == cricket::DMT_TEXT);
321
322 LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " << params.ssrc;
323 // We can send unordered as soon as we receive any DATA message since the
324 // remote side must have received the OPEN (and old clients do not send
325 // OPEN_ACK).
326 waiting_for_open_ack_ = false;
327
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000328 bool binary = (params.type == cricket::DMT_BINARY);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000329 rtc::scoped_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000330 if (was_ever_writable_ && observer_) {
331 observer_->OnMessage(*buffer.get());
332 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000333 if (queued_received_data_.byte_count() + payload.size() >
334 kMaxQueuedReceivedDataBytes) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000335 LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
336
337 queued_received_data_.Clear();
338 if (data_channel_type_ != cricket::DCT_RTP) {
339 Close();
340 }
341
342 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000343 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000344 queued_received_data_.Push(buffer.release());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000345 }
346}
347
348void DataChannel::OnChannelReady(bool writable) {
349 if (!writable) {
350 return;
351 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000352 // Update the readyState and send the queued control message if the channel
353 // is writable for the first time; otherwise it means the channel was blocked
354 // for sending and now unblocked, so send the queued data now.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000355 if (!was_ever_writable_) {
356 was_ever_writable_ = true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000357
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000358 if (data_channel_type_ == cricket::DCT_SCTP) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000359 rtc::Buffer payload;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000360
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000361 if (config_.open_handshake_role == InternalDataChannelInit::kOpener) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000362 WriteDataChannelOpenMessage(label_, config_, &payload);
363 SendControlMessage(payload);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000364 } else if (config_.open_handshake_role ==
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000365 InternalDataChannelInit::kAcker) {
366 WriteDataChannelOpenAckMessage(&payload);
367 SendControlMessage(payload);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000368 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000369 }
370
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000371 UpdateState();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000372 ASSERT(queued_send_data_.Empty());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000373 } else if (state_ == kOpen) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000374 // TODO(jiayl): Sending OPEN message here contradicts with the pre-condition
375 // that the readyState is open. According to the standard, the channel
376 // should not become open before the OPEN message is sent.
377 SendQueuedControlMessages();
378
379 SendQueuedDataMessages();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000380 }
381}
382
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383void DataChannel::DoClose() {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000384 if (state_ == kClosed)
385 return;
386
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 receive_ssrc_set_ = false;
388 send_ssrc_set_ = false;
389 SetState(kClosing);
390 UpdateState();
391}
392
393void DataChannel::UpdateState() {
394 switch (state_) {
395 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000396 if (send_ssrc_set_ == receive_ssrc_set_) {
397 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
398 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399 }
400 if (was_ever_writable_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000401 // TODO(jiayl): Do not transition to kOpen if we failed to send the
402 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000403 SendQueuedControlMessages();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 SetState(kOpen);
405 // If we have received buffers before the channel got writable.
406 // Deliver them now.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000407 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 }
409 }
410 break;
411 }
412 case kOpen: {
413 break;
414 }
415 case kClosing: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000416 DisconnectFromTransport();
417
418 if (!send_ssrc_set_ && !receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 SetState(kClosed);
420 }
421 break;
422 }
423 case kClosed:
424 break;
425 }
426}
427
428void DataChannel::SetState(DataState state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000429 if (state_ == state)
430 return;
431
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432 state_ = state;
433 if (observer_) {
434 observer_->OnStateChange();
435 }
436}
437
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000438void DataChannel::DisconnectFromTransport() {
439 if (!connected_to_provider_)
440 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441
wu@webrtc.org78187522013-10-07 23:32:02 +0000442 provider_->DisconnectDataChannel(this);
443 connected_to_provider_ = false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000444
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +0000445 if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000446 provider_->RemoveSctpDataStream(config_.id);
447 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448}
449
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000450void DataChannel::DeliverQueuedReceivedData() {
451 if (!was_ever_writable_ || !observer_) {
452 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000455 while (!queued_received_data_.Empty()) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000456 rtc::scoped_ptr<DataBuffer> buffer(queued_received_data_.Front());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000457 observer_->OnMessage(*buffer);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000458 queued_received_data_.Pop();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459 }
460}
461
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000462void DataChannel::SendQueuedDataMessages() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000463 ASSERT(was_ever_writable_ && state_ == kOpen);
464
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000465 while (!queued_send_data_.Empty()) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000466 DataBuffer* buffer = queued_send_data_.Front();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000467 if (!SendDataMessage(*buffer, false)) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000468 // Leave the message in the queue if sending is aborted.
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000469 break;
470 }
471 queued_send_data_.Pop();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000472 delete buffer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000473 }
474}
475
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000476bool DataChannel::SendDataMessage(const DataBuffer& buffer,
477 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000478 cricket::SendDataParams send_params;
479
wu@webrtc.org78187522013-10-07 23:32:02 +0000480 if (data_channel_type_ == cricket::DCT_SCTP) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000481 send_params.ordered = config_.ordered;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000482 // Send as ordered if it is waiting for the OPEN_ACK message.
483 if (waiting_for_open_ack_ && !config_.ordered) {
484 send_params.ordered = true;
485 LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel "
486 << "because the OPEN_ACK message has not been received.";
487 }
488
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000489 send_params.max_rtx_count = config_.maxRetransmits;
490 send_params.max_rtx_ms = config_.maxRetransmitTime;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000491 send_params.ssrc = config_.id;
492 } else {
493 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000494 }
495 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
496
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000497 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
498 bool success = provider_->SendData(send_params, buffer.data, &send_result);
499
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000500 if (success) {
501 return true;
502 }
503
504 if (data_channel_type_ != cricket::DCT_SCTP) {
505 return false;
506 }
507
508 if (send_result == cricket::SDR_BLOCK) {
509 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
510 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000511 }
512 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000513 // Close the channel if the error is not SDR_BLOCK, or if queuing the
514 // message failed.
515 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
516 << "send_result = " << send_result;
517 Close();
518
519 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000520}
521
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000522bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
523 if (queued_send_data_.byte_count() >= kMaxQueuedSendDataBytes) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000524 LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000525 return false;
526 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000527 queued_send_data_.Push(new DataBuffer(buffer));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000528 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529}
530
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000531void DataChannel::SendQueuedControlMessages() {
532 ASSERT(was_ever_writable_);
533
534 PacketQueue control_packets;
535 control_packets.Swap(&queued_control_data_);
536
537 while (!control_packets.Empty()) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000538 rtc::scoped_ptr<DataBuffer> buf(control_packets.Front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000539 SendControlMessage(buf->data);
540 control_packets.Pop();
541 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000542}
543
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000544void DataChannel::QueueControlMessage(const rtc::Buffer& buffer) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000545 queued_control_data_.Push(new DataBuffer(buffer, true));
546}
547
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000548bool DataChannel::SendControlMessage(const rtc::Buffer& buffer) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000549 bool is_open_message =
550 (config_.open_handshake_role == InternalDataChannelInit::kOpener);
551
552 ASSERT(data_channel_type_ == cricket::DCT_SCTP &&
553 was_ever_writable_ &&
554 config_.id >= 0 &&
555 (!is_open_message || !config_.negotiated));
556
557 cricket::SendDataParams send_params;
558 send_params.ssrc = config_.id;
559 send_params.ordered = config_.ordered || is_open_message;
560 send_params.type = cricket::DMT_CONTROL;
561
562 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
563 bool retval = provider_->SendData(send_params, buffer, &send_result);
564 if (retval) {
565 LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
566
567 if (is_open_message) {
568 // Send data as ordered before we receive any message from the remote peer
569 // to make sure the remote peer will not receive any data before it
570 // receives the OPEN message.
571 waiting_for_open_ack_ = true;
572 }
573 } else if (send_result == cricket::SDR_BLOCK) {
574 QueueControlMessage(buffer);
575 } else {
576 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
577 << " the CONTROL message, send_result = " << send_result;
578 Close();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000579 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000580 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000581}
582
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583} // namespace webrtc