blob: 452e4b39611f7f0d99d6e0813f39124eeaf5aaf5 [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/mediastreamprovider.h"
17#include "webrtc/api/sctputils.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018#include "webrtc/base/logging.h"
19#include "webrtc/base/refcount.h"
kjellandera96e2d72016-02-04 23:52:28 -080020#include "webrtc/media/sctp/sctpdataengine.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 {
61 if (sid < 0 || sid > static_cast<int>(cricket::kMaxSctpSid)) {
62 return false;
63 }
64 return used_sids_.find(sid) == used_sids_.end();
65}
66
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000067DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {}
68
69DataChannel::PacketQueue::~PacketQueue() {
70 Clear();
71}
72
73bool DataChannel::PacketQueue::Empty() const {
74 return packets_.empty();
75}
76
77DataBuffer* DataChannel::PacketQueue::Front() {
78 return packets_.front();
79}
80
81void DataChannel::PacketQueue::Pop() {
82 if (packets_.empty()) {
83 return;
84 }
85
86 byte_count_ -= packets_.front()->size();
87 packets_.pop_front();
88}
89
90void DataChannel::PacketQueue::Push(DataBuffer* packet) {
91 byte_count_ += packet->size();
92 packets_.push_back(packet);
93}
94
95void DataChannel::PacketQueue::Clear() {
96 while (!packets_.empty()) {
97 delete packets_.front();
98 packets_.pop_front();
99 }
100 byte_count_ = 0;
101}
102
103void DataChannel::PacketQueue::Swap(PacketQueue* other) {
104 size_t other_byte_count = other->byte_count_;
105 other->byte_count_ = byte_count_;
106 byte_count_ = other_byte_count;
107
108 other->packets_.swap(packets_);
109}
110
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000111rtc::scoped_refptr<DataChannel> DataChannel::Create(
wu@webrtc.org78187522013-10-07 23:32:02 +0000112 DataChannelProviderInterface* provider,
113 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000115 const InternalDataChannelInit& config) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000116 rtc::scoped_refptr<DataChannel> channel(
117 new rtc::RefCountedObject<DataChannel>(provider, dct, label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 if (!channel->Init(config)) {
119 return NULL;
120 }
121 return channel;
122}
123
wu@webrtc.org78187522013-10-07 23:32:02 +0000124DataChannel::DataChannel(
125 DataChannelProviderInterface* provider,
126 cricket::DataChannelType dct,
127 const std::string& label)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 : label_(label),
129 observer_(NULL),
130 state_(kConnecting),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000131 data_channel_type_(dct),
132 provider_(provider),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400133 handshake_state_(kHandshakeInit),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000134 connected_to_provider_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 send_ssrc_set_(false),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000136 receive_ssrc_set_(false),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400137 writable_(false),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000138 send_ssrc_(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 receive_ssrc_(0) {
140}
141
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000142bool DataChannel::Init(const InternalDataChannelInit& config) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400143 if (data_channel_type_ == cricket::DCT_RTP) {
144 if (config.reliable ||
145 config.id != -1 ||
146 config.maxRetransmits != -1 ||
147 config.maxRetransmitTime != -1) {
148 LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
149 << "invalid DataChannelInit.";
150 return false;
151 }
152 handshake_state_ = kHandshakeReady;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000153 } else if (data_channel_type_ == cricket::DCT_SCTP) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000154 if (config.id < -1 ||
155 config.maxRetransmits < -1 ||
156 config.maxRetransmitTime < -1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000157 LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 << "invalid DataChannelInit.";
159 return false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000160 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000161 if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000162 LOG(LS_ERROR) <<
163 "maxRetransmits and maxRetransmitTime should not be both set.";
164 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000166 config_ = config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167
Lally Singh5c6c6e02015-05-29 11:52:39 -0400168 switch (config_.open_handshake_role) {
169 case webrtc::InternalDataChannelInit::kNone: // pre-negotiated
170 handshake_state_ = kHandshakeReady;
171 break;
172 case webrtc::InternalDataChannelInit::kOpener:
173 handshake_state_ = kHandshakeShouldSendOpen;
174 break;
175 case webrtc::InternalDataChannelInit::kAcker:
176 handshake_state_ = kHandshakeShouldSendAck;
177 break;
178 };
179
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000180 // Try to connect to the transport in case the transport channel already
181 // exists.
182 OnTransportChannelCreated();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000183
184 // Checks if the transport is ready to send because the initial channel
185 // ready signal may have been sent before the DataChannel creation.
186 // This has to be done async because the upper layer objects (e.g.
187 // Chrome glue and WebKit) are not wired up properly until after this
188 // function returns.
189 if (provider_->ReadyToSendData()) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000190 rtc::Thread::Current()->Post(this, MSG_CHANNELREADY, NULL);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000191 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000192 }
193
194 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195}
196
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000197DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198
199void DataChannel::RegisterObserver(DataChannelObserver* observer) {
200 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000201 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202}
203
204void DataChannel::UnregisterObserver() {
205 observer_ = NULL;
206}
207
208bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02 +0000209 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 return false;
211 } else {
212 return config_.maxRetransmits == -1 &&
213 config_.maxRetransmitTime == -1;
214 }
215}
216
Peter Boström0c4e06b2015-10-07 12:23:21 +0200217uint64_t DataChannel::buffered_amount() const {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000218 return queued_send_data_.byte_count();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219}
220
221void DataChannel::Close() {
222 if (state_ == kClosed)
223 return;
224 send_ssrc_ = 0;
225 send_ssrc_set_ = false;
226 SetState(kClosing);
227 UpdateState();
228}
229
230bool DataChannel::Send(const DataBuffer& buffer) {
231 if (state_ != kOpen) {
232 return false;
233 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000234
235 // TODO(jiayl): the spec is unclear about if the remote side should get the
236 // onmessage event. We need to figure out the expected behavior and change the
237 // code accordingly.
238 if (buffer.size() == 0) {
239 return true;
240 }
241
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000242 // If the queue is non-empty, we're waiting for SignalReadyToSend,
243 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000244 if (!queued_send_data_.Empty()) {
245 // Only SCTP DataChannel queues the outgoing data when the transport is
246 // blocked.
247 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
248 if (!QueueSendDataMessage(buffer)) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000249 Close();
250 }
251 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000254 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000255 if (data_channel_type_ == cricket::DCT_RTP) {
256 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000257 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000258
259 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000260 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261}
262
Peter Boström0c4e06b2015-10-07 12:23:21 +0200263void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000264 ASSERT(data_channel_type_ == cricket::DCT_RTP);
265
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 return;
268 }
269 receive_ssrc_ = receive_ssrc;
270 receive_ssrc_set_ = true;
271 UpdateState();
272}
273
274// The remote peer request that this channel shall be closed.
275void DataChannel::RemotePeerRequestClose() {
276 DoClose();
277}
278
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000279void DataChannel::SetSctpSid(int sid) {
280 ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP);
deadbeefab9b2d12015-10-14 11:33:11 -0700281 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000282 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700283 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000284
285 config_.id = sid;
286 provider_->AddSctpDataStream(sid);
287}
288
289void DataChannel::OnTransportChannelCreated() {
290 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
291 if (!connected_to_provider_) {
292 connected_to_provider_ = provider_->ConnectDataChannel(this);
293 }
294 // The sid may have been unassigned when provider_->ConnectDataChannel was
295 // done. So always add the streams even if connected_to_provider_ is true.
296 if (config_.id >= 0) {
297 provider_->AddSctpDataStream(config_.id);
298 }
299}
300
deadbeefab9b2d12015-10-14 11:33:11 -0700301// The underlying transport channel was destroyed.
302// This function makes sure the DataChannel is disconnected and changes state to
303// kClosed.
304void DataChannel::OnTransportChannelDestroyed() {
305 DoClose();
306}
307
Peter Boström0c4e06b2015-10-07 12:23:21 +0200308void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000309 ASSERT(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 return;
312 }
313 send_ssrc_ = send_ssrc;
314 send_ssrc_set_ = true;
315 UpdateState();
316}
317
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000318void DataChannel::OnMessage(rtc::Message* msg) {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000319 switch (msg->message_id) {
320 case MSG_CHANNELREADY:
321 OnChannelReady(true);
322 break;
323 }
324}
325
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000326void DataChannel::OnDataReceived(cricket::DataChannel* channel,
327 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700328 const rtc::CopyOnWriteBuffer& payload) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200329 uint32_t expected_ssrc =
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000330 (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id;
331 if (params.ssrc != expected_ssrc) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000332 return;
333 }
334
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000335 if (params.type == cricket::DMT_CONTROL) {
336 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400337 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000338 // Ignore it if we are not expecting an ACK message.
339 LOG(LS_WARNING) << "DataChannel received unexpected CONTROL message, "
340 << "sid = " << params.ssrc;
341 return;
342 }
343 if (ParseDataChannelOpenAckMessage(payload)) {
344 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400345 handshake_state_ = kHandshakeReady;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000346 LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
347 << params.ssrc;
348 } else {
349 LOG(LS_WARNING) << "DataChannel failed to parse OPEN_ACK message, sid = "
350 << params.ssrc;
351 }
352 return;
353 }
354
355 ASSERT(params.type == cricket::DMT_BINARY ||
356 params.type == cricket::DMT_TEXT);
357
358 LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " << params.ssrc;
359 // We can send unordered as soon as we receive any DATA message since the
360 // remote side must have received the OPEN (and old clients do not send
361 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400362 if (handshake_state_ == kHandshakeWaitingForAck) {
363 handshake_state_ = kHandshakeReady;
364 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000365
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000366 bool binary = (params.type == cricket::DMT_BINARY);
kwibergd1fe2812016-04-27 06:47:29 -0700367 std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400368 if (state_ == kOpen && observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000369 observer_->OnMessage(*buffer.get());
370 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000371 if (queued_received_data_.byte_count() + payload.size() >
372 kMaxQueuedReceivedDataBytes) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000373 LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
374
375 queued_received_data_.Clear();
376 if (data_channel_type_ != cricket::DCT_RTP) {
377 Close();
378 }
379
380 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000381 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000382 queued_received_data_.Push(buffer.release());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000383 }
384}
385
deadbeefab9b2d12015-10-14 11:33:11 -0700386void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
387 if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id) {
388 Close();
389 }
390}
391
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000392void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400393 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000394 if (!writable) {
395 return;
396 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000397
Lally Singh5c6c6e02015-05-29 11:52:39 -0400398 SendQueuedControlMessages();
399 SendQueuedDataMessages();
400 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000401}
402
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403void DataChannel::DoClose() {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000404 if (state_ == kClosed)
405 return;
406
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 receive_ssrc_set_ = false;
408 send_ssrc_set_ = false;
409 SetState(kClosing);
410 UpdateState();
411}
412
413void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400414 // UpdateState determines what to do from a few state variables. Include
415 // all conditions required for each state transition here for
416 // clarity. OnChannelReady(true) will send any queued data and then invoke
417 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418 switch (state_) {
419 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000420 if (send_ssrc_set_ == receive_ssrc_set_) {
421 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
422 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400424 if (connected_to_provider_) {
425 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 06:15:43 -0700426 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400427 WriteDataChannelOpenMessage(label_, config_, &payload);
428 SendControlMessage(payload);
429 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 06:15:43 -0700430 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400431 WriteDataChannelOpenAckMessage(&payload);
432 SendControlMessage(payload);
433 }
434 if (writable_ &&
435 (handshake_state_ == kHandshakeReady ||
436 handshake_state_ == kHandshakeWaitingForAck)) {
437 SetState(kOpen);
438 // If we have received buffers before the channel got writable.
439 // Deliver them now.
440 DeliverQueuedReceivedData();
441 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442 }
443 }
444 break;
445 }
446 case kOpen: {
447 break;
448 }
449 case kClosing: {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400450 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
451 if (connected_to_provider_) {
452 DisconnectFromProvider();
453 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000454
Lally Singh5c6c6e02015-05-29 11:52:39 -0400455 if (!connected_to_provider_ && !send_ssrc_set_ && !receive_ssrc_set_) {
456 SetState(kClosed);
457 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 }
459 break;
460 }
461 case kClosed:
462 break;
463 }
464}
465
466void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700467 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000468 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700469 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000470
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471 state_ = state;
472 if (observer_) {
473 observer_->OnStateChange();
474 }
deadbeefab9b2d12015-10-14 11:33:11 -0700475 if (state_ == kClosed) {
476 SignalClosed(this);
477 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478}
479
Lally Singh5c6c6e02015-05-29 11:52:39 -0400480void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000481 if (!connected_to_provider_)
482 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483
wu@webrtc.org78187522013-10-07 23:32:02 +0000484 provider_->DisconnectDataChannel(this);
485 connected_to_provider_ = false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000486
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +0000487 if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000488 provider_->RemoveSctpDataStream(config_.id);
489 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490}
491
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000492void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400493 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000494 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000497 while (!queued_received_data_.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700498 std::unique_ptr<DataBuffer> buffer(queued_received_data_.Front());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000499 observer_->OnMessage(*buffer);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000500 queued_received_data_.Pop();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 }
502}
503
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000504void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400505 if (queued_send_data_.Empty()) {
506 return;
507 }
508
509 ASSERT(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000510
Peter Boström0c4e06b2015-10-07 12:23:21 +0200511 uint64_t start_buffered_amount = buffered_amount();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000512 while (!queued_send_data_.Empty()) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000513 DataBuffer* buffer = queued_send_data_.Front();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000514 if (!SendDataMessage(*buffer, false)) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000515 // Leave the message in the queue if sending is aborted.
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000516 break;
517 }
518 queued_send_data_.Pop();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000519 delete buffer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000520 }
bemasc0edd50c2015-07-01 13:34:33 -0700521
522 if (observer_ && buffered_amount() < start_buffered_amount) {
523 observer_->OnBufferedAmountChange(start_buffered_amount);
524 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000525}
526
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000527bool DataChannel::SendDataMessage(const DataBuffer& buffer,
528 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000529 cricket::SendDataParams send_params;
530
wu@webrtc.org78187522013-10-07 23:32:02 +0000531 if (data_channel_type_ == cricket::DCT_SCTP) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000532 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400533 // Send as ordered if it is still going through OPEN/ACK signaling.
534 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000535 send_params.ordered = true;
536 LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel "
537 << "because the OPEN_ACK message has not been received.";
538 }
539
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000540 send_params.max_rtx_count = config_.maxRetransmits;
541 send_params.max_rtx_ms = config_.maxRetransmitTime;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000542 send_params.ssrc = config_.id;
543 } else {
544 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000545 }
546 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
547
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000548 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
549 bool success = provider_->SendData(send_params, buffer.data, &send_result);
550
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000551 if (success) {
552 return true;
553 }
554
555 if (data_channel_type_ != cricket::DCT_SCTP) {
556 return false;
557 }
558
559 if (send_result == cricket::SDR_BLOCK) {
560 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
561 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000562 }
563 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000564 // Close the channel if the error is not SDR_BLOCK, or if queuing the
565 // message failed.
566 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
567 << "send_result = " << send_result;
568 Close();
569
570 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000571}
572
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000573bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
bemasc0edd50c2015-07-01 13:34:33 -0700574 size_t start_buffered_amount = buffered_amount();
575 if (start_buffered_amount >= kMaxQueuedSendDataBytes) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000576 LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000577 return false;
578 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000579 queued_send_data_.Push(new DataBuffer(buffer));
bemasc0edd50c2015-07-01 13:34:33 -0700580
581 // The buffer can have length zero, in which case there is no change.
582 if (observer_ && buffered_amount() > start_buffered_amount) {
583 observer_->OnBufferedAmountChange(start_buffered_amount);
584 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000585 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586}
587
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000588void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000589 PacketQueue control_packets;
590 control_packets.Swap(&queued_control_data_);
591
592 while (!control_packets.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700593 std::unique_ptr<DataBuffer> buf(control_packets.Front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000594 SendControlMessage(buf->data);
595 control_packets.Pop();
596 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000597}
598
jbaucheec21bd2016-03-20 06:15:43 -0700599void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000600 queued_control_data_.Push(new DataBuffer(buffer, true));
601}
602
jbaucheec21bd2016-03-20 06:15:43 -0700603bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400604 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000605
606 ASSERT(data_channel_type_ == cricket::DCT_SCTP &&
Lally Singh5c6c6e02015-05-29 11:52:39 -0400607 writable_ &&
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000608 config_.id >= 0 &&
609 (!is_open_message || !config_.negotiated));
610
611 cricket::SendDataParams send_params;
612 send_params.ssrc = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400613 // Send data as ordered before we receive any message from the remote peer to
614 // make sure the remote peer will not receive any data before it receives the
615 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000616 send_params.ordered = config_.ordered || is_open_message;
617 send_params.type = cricket::DMT_CONTROL;
618
619 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
620 bool retval = provider_->SendData(send_params, buffer, &send_result);
621 if (retval) {
622 LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
623
Lally Singh5c6c6e02015-05-29 11:52:39 -0400624 if (handshake_state_ == kHandshakeShouldSendAck) {
625 handshake_state_ = kHandshakeReady;
626 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
627 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000628 }
629 } else if (send_result == cricket::SDR_BLOCK) {
630 QueueControlMessage(buffer);
631 } else {
632 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
633 << " the CONTROL message, send_result = " << send_result;
634 Close();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000635 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000636 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000637}
638
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639} // namespace webrtc