blob: 559eec5343f15e3102d5fcd7dbbad0632fdcf847 [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),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400112 handshake_state_(kHandshakeInit),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000113 connected_to_provider_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 send_ssrc_set_(false),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000115 receive_ssrc_set_(false),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400116 writable_(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) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400122 if (data_channel_type_ == cricket::DCT_RTP) {
123 if (config.reliable ||
124 config.id != -1 ||
125 config.maxRetransmits != -1 ||
126 config.maxRetransmitTime != -1) {
127 LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
128 << "invalid DataChannelInit.";
129 return false;
130 }
131 handshake_state_ = kHandshakeReady;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000132 } else if (data_channel_type_ == cricket::DCT_SCTP) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000133 if (config.id < -1 ||
134 config.maxRetransmits < -1 ||
135 config.maxRetransmitTime < -1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000136 LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 << "invalid DataChannelInit.";
138 return false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000139 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000140 if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000141 LOG(LS_ERROR) <<
142 "maxRetransmits and maxRetransmitTime should not be both set.";
143 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000145 config_ = config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146
Lally Singh5c6c6e02015-05-29 11:52:39 -0400147 switch (config_.open_handshake_role) {
148 case webrtc::InternalDataChannelInit::kNone: // pre-negotiated
149 handshake_state_ = kHandshakeReady;
150 break;
151 case webrtc::InternalDataChannelInit::kOpener:
152 handshake_state_ = kHandshakeShouldSendOpen;
153 break;
154 case webrtc::InternalDataChannelInit::kAcker:
155 handshake_state_ = kHandshakeShouldSendAck;
156 break;
157 };
158
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000159 // Try to connect to the transport in case the transport channel already
160 // exists.
161 OnTransportChannelCreated();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000162
163 // Checks if the transport is ready to send because the initial channel
164 // ready signal may have been sent before the DataChannel creation.
165 // This has to be done async because the upper layer objects (e.g.
166 // Chrome glue and WebKit) are not wired up properly until after this
167 // function returns.
168 if (provider_->ReadyToSendData()) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000169 rtc::Thread::Current()->Post(this, MSG_CHANNELREADY, NULL);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000170 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000171 }
172
173 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174}
175
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000176DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177
178void DataChannel::RegisterObserver(DataChannelObserver* observer) {
179 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000180 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181}
182
183void DataChannel::UnregisterObserver() {
184 observer_ = NULL;
185}
186
187bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02 +0000188 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 return false;
190 } else {
191 return config_.maxRetransmits == -1 &&
192 config_.maxRetransmitTime == -1;
193 }
194}
195
196uint64 DataChannel::buffered_amount() const {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000197 return queued_send_data_.byte_count();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198}
199
200void DataChannel::Close() {
201 if (state_ == kClosed)
202 return;
203 send_ssrc_ = 0;
204 send_ssrc_set_ = false;
205 SetState(kClosing);
206 UpdateState();
207}
208
209bool DataChannel::Send(const DataBuffer& buffer) {
210 if (state_ != kOpen) {
211 return false;
212 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000213
214 // TODO(jiayl): the spec is unclear about if the remote side should get the
215 // onmessage event. We need to figure out the expected behavior and change the
216 // code accordingly.
217 if (buffer.size() == 0) {
218 return true;
219 }
220
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000221 // If the queue is non-empty, we're waiting for SignalReadyToSend,
222 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000223 if (!queued_send_data_.Empty()) {
224 // Only SCTP DataChannel queues the outgoing data when the transport is
225 // blocked.
226 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
227 if (!QueueSendDataMessage(buffer)) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000228 Close();
229 }
230 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000233 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000234 if (data_channel_type_ == cricket::DCT_RTP) {
235 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000236 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000237
238 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000239 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240}
241
242void DataChannel::SetReceiveSsrc(uint32 receive_ssrc) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000243 ASSERT(data_channel_type_ == cricket::DCT_RTP);
244
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246 return;
247 }
248 receive_ssrc_ = receive_ssrc;
249 receive_ssrc_set_ = true;
250 UpdateState();
251}
252
253// The remote peer request that this channel shall be closed.
254void DataChannel::RemotePeerRequestClose() {
255 DoClose();
256}
257
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000258void DataChannel::SetSctpSid(int sid) {
259 ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP);
260 if (config_.id == sid)
261 return;
262
263 config_.id = sid;
264 provider_->AddSctpDataStream(sid);
265}
266
267void DataChannel::OnTransportChannelCreated() {
268 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
269 if (!connected_to_provider_) {
270 connected_to_provider_ = provider_->ConnectDataChannel(this);
271 }
272 // The sid may have been unassigned when provider_->ConnectDataChannel was
273 // done. So always add the streams even if connected_to_provider_ is true.
274 if (config_.id >= 0) {
275 provider_->AddSctpDataStream(config_.id);
276 }
277}
278
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279void DataChannel::SetSendSsrc(uint32 send_ssrc) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000280 ASSERT(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 return;
283 }
284 send_ssrc_ = send_ssrc;
285 send_ssrc_set_ = true;
286 UpdateState();
287}
288
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000289void DataChannel::OnMessage(rtc::Message* msg) {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000290 switch (msg->message_id) {
291 case MSG_CHANNELREADY:
292 OnChannelReady(true);
293 break;
294 }
295}
296
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297// The underlaying data engine is closing.
wu@webrtc.org78187522013-10-07 23:32:02 +0000298// This function makes sure the DataChannel is disconnected and changes state to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299// kClosed.
300void DataChannel::OnDataEngineClose() {
301 DoClose();
302}
303
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000304void DataChannel::OnDataReceived(cricket::DataChannel* channel,
305 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000306 const rtc::Buffer& payload) {
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000307 uint32 expected_ssrc =
308 (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id;
309 if (params.ssrc != expected_ssrc) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000310 return;
311 }
312
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000313 if (params.type == cricket::DMT_CONTROL) {
314 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400315 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000316 // Ignore it if we are not expecting an ACK message.
317 LOG(LS_WARNING) << "DataChannel received unexpected CONTROL message, "
318 << "sid = " << params.ssrc;
319 return;
320 }
321 if (ParseDataChannelOpenAckMessage(payload)) {
322 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400323 handshake_state_ = kHandshakeReady;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000324 LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
325 << params.ssrc;
326 } else {
327 LOG(LS_WARNING) << "DataChannel failed to parse OPEN_ACK message, sid = "
328 << params.ssrc;
329 }
330 return;
331 }
332
333 ASSERT(params.type == cricket::DMT_BINARY ||
334 params.type == cricket::DMT_TEXT);
335
336 LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " << params.ssrc;
337 // We can send unordered as soon as we receive any DATA message since the
338 // remote side must have received the OPEN (and old clients do not send
339 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400340 if (handshake_state_ == kHandshakeWaitingForAck) {
341 handshake_state_ = kHandshakeReady;
342 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000343
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000344 bool binary = (params.type == cricket::DMT_BINARY);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000345 rtc::scoped_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400346 if (state_ == kOpen && observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000347 observer_->OnMessage(*buffer.get());
348 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000349 if (queued_received_data_.byte_count() + payload.size() >
350 kMaxQueuedReceivedDataBytes) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000351 LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
352
353 queued_received_data_.Clear();
354 if (data_channel_type_ != cricket::DCT_RTP) {
355 Close();
356 }
357
358 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000359 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000360 queued_received_data_.Push(buffer.release());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000361 }
362}
363
364void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400365 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000366 if (!writable) {
367 return;
368 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000369
Lally Singh5c6c6e02015-05-29 11:52:39 -0400370 SendQueuedControlMessages();
371 SendQueuedDataMessages();
372 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000373}
374
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375void DataChannel::DoClose() {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000376 if (state_ == kClosed)
377 return;
378
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379 receive_ssrc_set_ = false;
380 send_ssrc_set_ = false;
381 SetState(kClosing);
382 UpdateState();
383}
384
385void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400386 // UpdateState determines what to do from a few state variables. Include
387 // all conditions required for each state transition here for
388 // clarity. OnChannelReady(true) will send any queued data and then invoke
389 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000390 switch (state_) {
391 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000392 if (send_ssrc_set_ == receive_ssrc_set_) {
393 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
394 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400396 if (connected_to_provider_) {
397 if (handshake_state_ == kHandshakeShouldSendOpen) {
398 rtc::Buffer payload;
399 WriteDataChannelOpenMessage(label_, config_, &payload);
400 SendControlMessage(payload);
401 } else if (handshake_state_ == kHandshakeShouldSendAck) {
402 rtc::Buffer payload;
403 WriteDataChannelOpenAckMessage(&payload);
404 SendControlMessage(payload);
405 }
406 if (writable_ &&
407 (handshake_state_ == kHandshakeReady ||
408 handshake_state_ == kHandshakeWaitingForAck)) {
409 SetState(kOpen);
410 // If we have received buffers before the channel got writable.
411 // Deliver them now.
412 DeliverQueuedReceivedData();
413 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414 }
415 }
416 break;
417 }
418 case kOpen: {
419 break;
420 }
421 case kClosing: {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400422 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
423 if (connected_to_provider_) {
424 DisconnectFromProvider();
425 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000426
Lally Singh5c6c6e02015-05-29 11:52:39 -0400427 if (!connected_to_provider_ && !send_ssrc_set_ && !receive_ssrc_set_) {
428 SetState(kClosed);
429 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 }
431 break;
432 }
433 case kClosed:
434 break;
435 }
436}
437
438void DataChannel::SetState(DataState state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000439 if (state_ == state)
440 return;
441
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442 state_ = state;
443 if (observer_) {
444 observer_->OnStateChange();
445 }
446}
447
Lally Singh5c6c6e02015-05-29 11:52:39 -0400448void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000449 if (!connected_to_provider_)
450 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451
wu@webrtc.org78187522013-10-07 23:32:02 +0000452 provider_->DisconnectDataChannel(this);
453 connected_to_provider_ = false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000454
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +0000455 if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000456 provider_->RemoveSctpDataStream(config_.id);
457 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458}
459
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000460void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400461 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000462 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000465 while (!queued_received_data_.Empty()) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000466 rtc::scoped_ptr<DataBuffer> buffer(queued_received_data_.Front());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000467 observer_->OnMessage(*buffer);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000468 queued_received_data_.Pop();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469 }
470}
471
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000472void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400473 if (queued_send_data_.Empty()) {
474 return;
475 }
476
477 ASSERT(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000478
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000479 while (!queued_send_data_.Empty()) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000480 DataBuffer* buffer = queued_send_data_.Front();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000481 if (!SendDataMessage(*buffer, false)) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000482 // Leave the message in the queue if sending is aborted.
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000483 break;
484 }
485 queued_send_data_.Pop();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000486 delete buffer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000487 }
488}
489
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000490bool DataChannel::SendDataMessage(const DataBuffer& buffer,
491 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000492 cricket::SendDataParams send_params;
493
wu@webrtc.org78187522013-10-07 23:32:02 +0000494 if (data_channel_type_ == cricket::DCT_SCTP) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000495 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400496 // Send as ordered if it is still going through OPEN/ACK signaling.
497 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000498 send_params.ordered = true;
499 LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel "
500 << "because the OPEN_ACK message has not been received.";
501 }
502
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000503 send_params.max_rtx_count = config_.maxRetransmits;
504 send_params.max_rtx_ms = config_.maxRetransmitTime;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000505 send_params.ssrc = config_.id;
506 } else {
507 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000508 }
509 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
510
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000511 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
512 bool success = provider_->SendData(send_params, buffer.data, &send_result);
513
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000514 if (success) {
515 return true;
516 }
517
518 if (data_channel_type_ != cricket::DCT_SCTP) {
519 return false;
520 }
521
522 if (send_result == cricket::SDR_BLOCK) {
523 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
524 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000525 }
526 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000527 // Close the channel if the error is not SDR_BLOCK, or if queuing the
528 // message failed.
529 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
530 << "send_result = " << send_result;
531 Close();
532
533 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000534}
535
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000536bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
537 if (queued_send_data_.byte_count() >= kMaxQueuedSendDataBytes) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000538 LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000539 return false;
540 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000541 queued_send_data_.Push(new DataBuffer(buffer));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000542 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543}
544
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000545void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000546 PacketQueue control_packets;
547 control_packets.Swap(&queued_control_data_);
548
549 while (!control_packets.Empty()) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000550 rtc::scoped_ptr<DataBuffer> buf(control_packets.Front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000551 SendControlMessage(buf->data);
552 control_packets.Pop();
553 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000554}
555
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000556void DataChannel::QueueControlMessage(const rtc::Buffer& buffer) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000557 queued_control_data_.Push(new DataBuffer(buffer, true));
558}
559
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000560bool DataChannel::SendControlMessage(const rtc::Buffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400561 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000562
563 ASSERT(data_channel_type_ == cricket::DCT_SCTP &&
Lally Singh5c6c6e02015-05-29 11:52:39 -0400564 writable_ &&
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000565 config_.id >= 0 &&
566 (!is_open_message || !config_.negotiated));
567
568 cricket::SendDataParams send_params;
569 send_params.ssrc = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400570 // Send data as ordered before we receive any message from the remote peer to
571 // make sure the remote peer will not receive any data before it receives the
572 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000573 send_params.ordered = config_.ordered || is_open_message;
574 send_params.type = cricket::DMT_CONTROL;
575
576 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
577 bool retval = provider_->SendData(send_params, buffer, &send_result);
578 if (retval) {
579 LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
580
Lally Singh5c6c6e02015-05-29 11:52:39 -0400581 if (handshake_state_ == kHandshakeShouldSendAck) {
582 handshake_state_ = kHandshakeReady;
583 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
584 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000585 }
586 } else if (send_result == cricket::SDR_BLOCK) {
587 QueueControlMessage(buffer);
588 } else {
589 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
590 << " the CONTROL message, send_result = " << send_result;
591 Close();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000592 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000593 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000594}
595
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596} // namespace webrtc