blob: ad7cb571990be3649cddba387fb2f1dd62e77973 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +000010
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/datachannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
kwibergd1fe2812016-04-27 06:47:29 -070013#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <string>
15
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/sctputils.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000017#include "webrtc/base/logging.h"
18#include "webrtc/base/refcount.h"
deadbeef953c2ce2017-01-09 14:53:41 -080019#include "webrtc/media/sctp/sctptransportinternal.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
21namespace webrtc {
22
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000023static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024;
24static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000026enum {
27 MSG_CHANNELREADY,
28};
29
deadbeefab9b2d12015-10-14 11:33:11 -070030bool SctpSidAllocator::AllocateSid(rtc::SSLRole role, int* sid) {
31 int potential_sid = (role == rtc::SSL_CLIENT) ? 0 : 1;
32 while (!IsSidAvailable(potential_sid)) {
33 potential_sid += 2;
34 if (potential_sid > static_cast<int>(cricket::kMaxSctpSid)) {
35 return false;
36 }
37 }
38
39 *sid = potential_sid;
40 used_sids_.insert(potential_sid);
41 return true;
42}
43
44bool SctpSidAllocator::ReserveSid(int sid) {
45 if (!IsSidAvailable(sid)) {
46 return false;
47 }
48 used_sids_.insert(sid);
49 return true;
50}
51
52void SctpSidAllocator::ReleaseSid(int sid) {
53 auto it = used_sids_.find(sid);
54 if (it != used_sids_.end()) {
55 used_sids_.erase(it);
56 }
57}
58
59bool SctpSidAllocator::IsSidAvailable(int sid) const {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -070060 if (sid < static_cast<int>(cricket::kMinSctpSid) ||
61 sid > static_cast<int>(cricket::kMaxSctpSid)) {
deadbeefab9b2d12015-10-14 11:33:11 -070062 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),
hbos84ffdee2016-10-12 14:14:39 -0700129 observer_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 state_(kConnecting),
hbos84ffdee2016-10-12 14:14:39 -0700131 messages_sent_(0),
132 bytes_sent_(0),
133 messages_received_(0),
134 bytes_received_(0),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000135 data_channel_type_(dct),
136 provider_(provider),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400137 handshake_state_(kHandshakeInit),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000138 connected_to_provider_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 send_ssrc_set_(false),
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000140 receive_ssrc_set_(false),
Lally Singh5c6c6e02015-05-29 11:52:39 -0400141 writable_(false),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000142 send_ssrc_(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 receive_ssrc_(0) {
144}
145
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000146bool DataChannel::Init(const InternalDataChannelInit& config) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400147 if (data_channel_type_ == cricket::DCT_RTP) {
148 if (config.reliable ||
149 config.id != -1 ||
150 config.maxRetransmits != -1 ||
151 config.maxRetransmitTime != -1) {
152 LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
153 << "invalid DataChannelInit.";
154 return false;
155 }
156 handshake_state_ = kHandshakeReady;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000157 } else if (data_channel_type_ == cricket::DCT_SCTP) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000158 if (config.id < -1 ||
159 config.maxRetransmits < -1 ||
160 config.maxRetransmitTime < -1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000161 LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 << "invalid DataChannelInit.";
163 return false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000164 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000165 if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000166 LOG(LS_ERROR) <<
167 "maxRetransmits and maxRetransmitTime should not be both set.";
168 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000170 config_ = config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171
Lally Singh5c6c6e02015-05-29 11:52:39 -0400172 switch (config_.open_handshake_role) {
173 case webrtc::InternalDataChannelInit::kNone: // pre-negotiated
174 handshake_state_ = kHandshakeReady;
175 break;
176 case webrtc::InternalDataChannelInit::kOpener:
177 handshake_state_ = kHandshakeShouldSendOpen;
178 break;
179 case webrtc::InternalDataChannelInit::kAcker:
180 handshake_state_ = kHandshakeShouldSendAck;
181 break;
182 };
183
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000184 // Try to connect to the transport in case the transport channel already
185 // exists.
186 OnTransportChannelCreated();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000187
188 // Checks if the transport is ready to send because the initial channel
189 // ready signal may have been sent before the DataChannel creation.
190 // This has to be done async because the upper layer objects (e.g.
191 // Chrome glue and WebKit) are not wired up properly until after this
192 // function returns.
193 if (provider_->ReadyToSendData()) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700194 rtc::Thread::Current()->Post(RTC_FROM_HERE, this, MSG_CHANNELREADY, NULL);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000195 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000196 }
197
198 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199}
200
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000201DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202
203void DataChannel::RegisterObserver(DataChannelObserver* observer) {
204 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000205 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206}
207
208void DataChannel::UnregisterObserver() {
209 observer_ = NULL;
210}
211
212bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02 +0000213 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 return false;
215 } else {
216 return config_.maxRetransmits == -1 &&
217 config_.maxRetransmitTime == -1;
218 }
219}
220
Peter Boström0c4e06b2015-10-07 12:23:21 +0200221uint64_t DataChannel::buffered_amount() const {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000222 return queued_send_data_.byte_count();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223}
224
225void DataChannel::Close() {
226 if (state_ == kClosed)
227 return;
228 send_ssrc_ = 0;
229 send_ssrc_set_ = false;
230 SetState(kClosing);
231 UpdateState();
232}
233
234bool DataChannel::Send(const DataBuffer& buffer) {
235 if (state_ != kOpen) {
236 return false;
237 }
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000238
239 // TODO(jiayl): the spec is unclear about if the remote side should get the
240 // onmessage event. We need to figure out the expected behavior and change the
241 // code accordingly.
242 if (buffer.size() == 0) {
243 return true;
244 }
245
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000246 // If the queue is non-empty, we're waiting for SignalReadyToSend,
247 // so just add to the end of the queue and keep waiting.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000248 if (!queued_send_data_.Empty()) {
249 // Only SCTP DataChannel queues the outgoing data when the transport is
250 // blocked.
251 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
252 if (!QueueSendDataMessage(buffer)) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000253 Close();
254 }
255 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000258 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000259 if (data_channel_type_ == cricket::DCT_RTP) {
260 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000261 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000262
263 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000264 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265}
266
Peter Boström0c4e06b2015-10-07 12:23:21 +0200267void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000268 ASSERT(data_channel_type_ == cricket::DCT_RTP);
269
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 return;
272 }
273 receive_ssrc_ = receive_ssrc;
274 receive_ssrc_set_ = true;
275 UpdateState();
276}
277
278// The remote peer request that this channel shall be closed.
279void DataChannel::RemotePeerRequestClose() {
280 DoClose();
281}
282
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000283void DataChannel::SetSctpSid(int sid) {
284 ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP);
deadbeefab9b2d12015-10-14 11:33:11 -0700285 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000286 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700287 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000288
289 config_.id = sid;
290 provider_->AddSctpDataStream(sid);
291}
292
293void DataChannel::OnTransportChannelCreated() {
294 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
295 if (!connected_to_provider_) {
296 connected_to_provider_ = provider_->ConnectDataChannel(this);
297 }
298 // The sid may have been unassigned when provider_->ConnectDataChannel was
299 // done. So always add the streams even if connected_to_provider_ is true.
300 if (config_.id >= 0) {
301 provider_->AddSctpDataStream(config_.id);
302 }
303}
304
deadbeefab9b2d12015-10-14 11:33:11 -0700305void DataChannel::OnTransportChannelDestroyed() {
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700306 // This method needs to synchronously close the data channel, which means any
307 // queued data needs to be discarded.
308 queued_send_data_.Clear();
309 queued_control_data_.Clear();
deadbeefab9b2d12015-10-14 11:33:11 -0700310 DoClose();
311}
312
Peter Boström0c4e06b2015-10-07 12:23:21 +0200313void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000314 ASSERT(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 return;
317 }
318 send_ssrc_ = send_ssrc;
319 send_ssrc_set_ = true;
320 UpdateState();
321}
322
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000323void DataChannel::OnMessage(rtc::Message* msg) {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000324 switch (msg->message_id) {
325 case MSG_CHANNELREADY:
326 OnChannelReady(true);
327 break;
328 }
329}
330
deadbeef953c2ce2017-01-09 14:53:41 -0800331void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700332 const rtc::CopyOnWriteBuffer& payload) {
deadbeef953c2ce2017-01-09 14:53:41 -0800333 if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) {
334 return;
335 }
336 if (data_channel_type_ == cricket::DCT_SCTP && params.sid != config_.id) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000337 return;
338 }
339
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000340 if (params.type == cricket::DMT_CONTROL) {
341 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400342 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000343 // Ignore it if we are not expecting an ACK message.
344 LOG(LS_WARNING) << "DataChannel received unexpected CONTROL message, "
deadbeef953c2ce2017-01-09 14:53:41 -0800345 << "sid = " << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000346 return;
347 }
348 if (ParseDataChannelOpenAckMessage(payload)) {
349 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 11:52:39 -0400350 handshake_state_ = kHandshakeReady;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000351 LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
deadbeef953c2ce2017-01-09 14:53:41 -0800352 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000353 } else {
354 LOG(LS_WARNING) << "DataChannel failed to parse OPEN_ACK message, sid = "
deadbeef953c2ce2017-01-09 14:53:41 -0800355 << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000356 }
357 return;
358 }
359
360 ASSERT(params.type == cricket::DMT_BINARY ||
361 params.type == cricket::DMT_TEXT);
362
deadbeef953c2ce2017-01-09 14:53:41 -0800363 LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " << params.sid;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000364 // We can send unordered as soon as we receive any DATA message since the
365 // remote side must have received the OPEN (and old clients do not send
366 // OPEN_ACK).
Lally Singh5c6c6e02015-05-29 11:52:39 -0400367 if (handshake_state_ == kHandshakeWaitingForAck) {
368 handshake_state_ = kHandshakeReady;
369 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000370
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000371 bool binary = (params.type == cricket::DMT_BINARY);
kwibergd1fe2812016-04-27 06:47:29 -0700372 std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
Lally Singh5c6c6e02015-05-29 11:52:39 -0400373 if (state_ == kOpen && observer_) {
hbos84ffdee2016-10-12 14:14:39 -0700374 ++messages_received_;
375 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000376 observer_->OnMessage(*buffer.get());
377 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000378 if (queued_received_data_.byte_count() + payload.size() >
379 kMaxQueuedReceivedDataBytes) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000380 LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
381
382 queued_received_data_.Clear();
383 if (data_channel_type_ != cricket::DCT_RTP) {
384 Close();
385 }
386
387 return;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000388 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000389 queued_received_data_.Push(buffer.release());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000390 }
391}
392
deadbeef953c2ce2017-01-09 14:53:41 -0800393void DataChannel::OnStreamClosedRemotely(int sid) {
394 if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id) {
deadbeefab9b2d12015-10-14 11:33:11 -0700395 Close();
396 }
397}
398
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000399void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400400 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000401 if (!writable) {
402 return;
403 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000404
Lally Singh5c6c6e02015-05-29 11:52:39 -0400405 SendQueuedControlMessages();
406 SendQueuedDataMessages();
407 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000408}
409
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410void DataChannel::DoClose() {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000411 if (state_ == kClosed)
412 return;
413
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414 receive_ssrc_set_ = false;
415 send_ssrc_set_ = false;
416 SetState(kClosing);
417 UpdateState();
418}
419
420void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400421 // UpdateState determines what to do from a few state variables. Include
422 // all conditions required for each state transition here for
423 // clarity. OnChannelReady(true) will send any queued data and then invoke
424 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425 switch (state_) {
426 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000427 if (send_ssrc_set_ == receive_ssrc_set_) {
428 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
429 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 }
Lally Singh5c6c6e02015-05-29 11:52:39 -0400431 if (connected_to_provider_) {
432 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 06:15:43 -0700433 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400434 WriteDataChannelOpenMessage(label_, config_, &payload);
435 SendControlMessage(payload);
436 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 06:15:43 -0700437 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400438 WriteDataChannelOpenAckMessage(&payload);
439 SendControlMessage(payload);
440 }
441 if (writable_ &&
442 (handshake_state_ == kHandshakeReady ||
443 handshake_state_ == kHandshakeWaitingForAck)) {
444 SetState(kOpen);
445 // If we have received buffers before the channel got writable.
446 // Deliver them now.
447 DeliverQueuedReceivedData();
448 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 }
450 }
451 break;
452 }
453 case kOpen: {
454 break;
455 }
456 case kClosing: {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400457 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
458 if (connected_to_provider_) {
459 DisconnectFromProvider();
460 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000461
Lally Singh5c6c6e02015-05-29 11:52:39 -0400462 if (!connected_to_provider_ && !send_ssrc_set_ && !receive_ssrc_set_) {
463 SetState(kClosed);
464 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 }
466 break;
467 }
468 case kClosed:
469 break;
470 }
471}
472
473void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 11:33:11 -0700474 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000475 return;
deadbeefab9b2d12015-10-14 11:33:11 -0700476 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000477
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478 state_ = state;
479 if (observer_) {
480 observer_->OnStateChange();
481 }
hbos82ebe022016-11-14 01:41:09 -0800482 if (state_ == kOpen) {
483 SignalOpened(this);
484 } else if (state_ == kClosed) {
deadbeefab9b2d12015-10-14 11:33:11 -0700485 SignalClosed(this);
486 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487}
488
Lally Singh5c6c6e02015-05-29 11:52:39 -0400489void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000490 if (!connected_to_provider_)
491 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492
wu@webrtc.org78187522013-10-07 23:32:02 +0000493 provider_->DisconnectDataChannel(this);
494 connected_to_provider_ = false;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000495
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +0000496 if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000497 provider_->RemoveSctpDataStream(config_.id);
498 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499}
500
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000501void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400502 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000503 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000506 while (!queued_received_data_.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700507 std::unique_ptr<DataBuffer> buffer(queued_received_data_.Front());
hbos84ffdee2016-10-12 14:14:39 -0700508 ++messages_received_;
509 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000510 observer_->OnMessage(*buffer);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000511 queued_received_data_.Pop();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 }
513}
514
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000515void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400516 if (queued_send_data_.Empty()) {
517 return;
518 }
519
520 ASSERT(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000521
Peter Boström0c4e06b2015-10-07 12:23:21 +0200522 uint64_t start_buffered_amount = buffered_amount();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000523 while (!queued_send_data_.Empty()) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000524 DataBuffer* buffer = queued_send_data_.Front();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000525 if (!SendDataMessage(*buffer, false)) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000526 // Leave the message in the queue if sending is aborted.
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000527 break;
528 }
529 queued_send_data_.Pop();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000530 delete buffer;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000531 }
bemasc0edd50c2015-07-01 13:34:33 -0700532
533 if (observer_ && buffered_amount() < start_buffered_amount) {
534 observer_->OnBufferedAmountChange(start_buffered_amount);
535 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000536}
537
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000538bool DataChannel::SendDataMessage(const DataBuffer& buffer,
539 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000540 cricket::SendDataParams send_params;
541
wu@webrtc.org78187522013-10-07 23:32:02 +0000542 if (data_channel_type_ == cricket::DCT_SCTP) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000543 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400544 // Send as ordered if it is still going through OPEN/ACK signaling.
545 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000546 send_params.ordered = true;
547 LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel "
548 << "because the OPEN_ACK message has not been received.";
549 }
550
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000551 send_params.max_rtx_count = config_.maxRetransmits;
552 send_params.max_rtx_ms = config_.maxRetransmitTime;
deadbeef953c2ce2017-01-09 14:53:41 -0800553 send_params.sid = config_.id;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000554 } else {
555 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000556 }
557 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
558
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000559 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
560 bool success = provider_->SendData(send_params, buffer.data, &send_result);
561
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000562 if (success) {
hbos84ffdee2016-10-12 14:14:39 -0700563 ++messages_sent_;
564 bytes_sent_ += buffer.size();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000565 return true;
566 }
567
568 if (data_channel_type_ != cricket::DCT_SCTP) {
569 return false;
570 }
571
572 if (send_result == cricket::SDR_BLOCK) {
573 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
574 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000575 }
576 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000577 // Close the channel if the error is not SDR_BLOCK, or if queuing the
578 // message failed.
579 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
580 << "send_result = " << send_result;
581 Close();
582
583 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000584}
585
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000586bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
bemasc0edd50c2015-07-01 13:34:33 -0700587 size_t start_buffered_amount = buffered_amount();
588 if (start_buffered_amount >= kMaxQueuedSendDataBytes) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000589 LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000590 return false;
591 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000592 queued_send_data_.Push(new DataBuffer(buffer));
bemasc0edd50c2015-07-01 13:34:33 -0700593
594 // The buffer can have length zero, in which case there is no change.
595 if (observer_ && buffered_amount() > start_buffered_amount) {
596 observer_->OnBufferedAmountChange(start_buffered_amount);
597 }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000598 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599}
600
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000601void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000602 PacketQueue control_packets;
603 control_packets.Swap(&queued_control_data_);
604
605 while (!control_packets.Empty()) {
kwibergd1fe2812016-04-27 06:47:29 -0700606 std::unique_ptr<DataBuffer> buf(control_packets.Front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000607 SendControlMessage(buf->data);
608 control_packets.Pop();
609 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000610}
611
jbaucheec21bd2016-03-20 06:15:43 -0700612void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000613 queued_control_data_.Push(new DataBuffer(buffer, true));
614}
615
jbaucheec21bd2016-03-20 06:15:43 -0700616bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 11:52:39 -0400617 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000618
619 ASSERT(data_channel_type_ == cricket::DCT_SCTP &&
Lally Singh5c6c6e02015-05-29 11:52:39 -0400620 writable_ &&
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000621 config_.id >= 0 &&
622 (!is_open_message || !config_.negotiated));
623
624 cricket::SendDataParams send_params;
deadbeef953c2ce2017-01-09 14:53:41 -0800625 send_params.sid = config_.id;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400626 // Send data as ordered before we receive any message from the remote peer to
627 // make sure the remote peer will not receive any data before it receives the
628 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000629 send_params.ordered = config_.ordered || is_open_message;
630 send_params.type = cricket::DMT_CONTROL;
631
632 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
633 bool retval = provider_->SendData(send_params, buffer, &send_result);
634 if (retval) {
635 LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
636
Lally Singh5c6c6e02015-05-29 11:52:39 -0400637 if (handshake_state_ == kHandshakeShouldSendAck) {
638 handshake_state_ = kHandshakeReady;
639 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
640 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000641 }
642 } else if (send_result == cricket::SDR_BLOCK) {
643 QueueControlMessage(buffer);
644 } else {
645 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
646 << " the CONTROL message, send_result = " << send_result;
647 Close();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000648 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000649 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000650}
651
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652} // namespace webrtc