blob: af694b7b1c4324d504aaf39947f3dd7536e96609 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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:369 */
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:1310
Henrik Kjellander15583c12016-02-10 09:53:1211#include "webrtc/api/datachannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:3612
kwibergd1fe2812016-04-27 13:47:2913#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:3614#include <string>
15
Henrik Kjellander15583c12016-02-10 09:53:1216#include "webrtc/api/sctputils.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5217#include "webrtc/base/logging.h"
18#include "webrtc/base/refcount.h"
kjellandera96e2d72016-02-05 07:52:2819#include "webrtc/media/sctp/sctpdataengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:3620
21namespace webrtc {
22
jiayl@webrtc.orgb43c99d2014-06-20 17:11:1423static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024;
24static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024;
henrike@webrtc.org28e20752013-07-10 00:45:3625
wu@webrtc.org07a6fbe2013-11-04 18:41:3426enum {
27 MSG_CHANNELREADY,
28};
29
deadbeefab9b2d12015-10-14 18:33:1130bool 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 20:15:2760 if (sid < static_cast<int>(cricket::kMinSctpSid) ||
61 sid > static_cast<int>(cricket::kMaxSctpSid)) {
deadbeefab9b2d12015-10-14 18:33:1162 return false;
63 }
64 return used_sids_.find(sid) == used_sids_.end();
65}
66
jiayl@webrtc.orgb43c99d2014-06-20 17:11:1467DataChannel::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:52111rtc::scoped_refptr<DataChannel> DataChannel::Create(
wu@webrtc.org78187522013-10-07 23:32:02112 DataChannelProviderInterface* provider,
113 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36114 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58115 const InternalDataChannelInit& config) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52116 rtc::scoped_refptr<DataChannel> channel(
117 new rtc::RefCountedObject<DataChannel>(provider, dct, label));
henrike@webrtc.org28e20752013-07-10 00:45:36118 if (!channel->Init(config)) {
119 return NULL;
120 }
121 return channel;
122}
123
wu@webrtc.org78187522013-10-07 23:32:02124DataChannel::DataChannel(
125 DataChannelProviderInterface* provider,
126 cricket::DataChannelType dct,
127 const std::string& label)
henrike@webrtc.org28e20752013-07-10 00:45:36128 : label_(label),
hbos84ffdee2016-10-12 21:14:39129 observer_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36130 state_(kConnecting),
hbos84ffdee2016-10-12 21:14:39131 messages_sent_(0),
132 bytes_sent_(0),
133 messages_received_(0),
134 bytes_received_(0),
henrika@webrtc.org44461fa2014-01-13 09:35:02135 data_channel_type_(dct),
136 provider_(provider),
Lally Singh5c6c6e02015-05-29 15:52:39137 handshake_state_(kHandshakeInit),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58138 connected_to_provider_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36139 send_ssrc_set_(false),
henrika@webrtc.org44461fa2014-01-13 09:35:02140 receive_ssrc_set_(false),
Lally Singh5c6c6e02015-05-29 15:52:39141 writable_(false),
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58142 send_ssrc_(0),
henrike@webrtc.org28e20752013-07-10 00:45:36143 receive_ssrc_(0) {
144}
145
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58146bool DataChannel::Init(const InternalDataChannelInit& config) {
Lally Singh5c6c6e02015-05-29 15:52:39147 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:12157 } else if (data_channel_type_ == cricket::DCT_SCTP) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58158 if (config.id < -1 ||
159 config.maxRetransmits < -1 ||
160 config.maxRetransmitTime < -1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12161 LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
henrike@webrtc.org28e20752013-07-10 00:45:36162 << "invalid DataChannelInit.";
163 return false;
wu@webrtc.orgcecfd182013-10-30 05:18:12164 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58165 if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12166 LOG(LS_ERROR) <<
167 "maxRetransmits and maxRetransmitTime should not be both set.";
168 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36169 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58170 config_ = config;
henrike@webrtc.org28e20752013-07-10 00:45:36171
Lally Singh5c6c6e02015-05-29 15:52:39172 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:12184 // Try to connect to the transport in case the transport channel already
185 // exists.
186 OnTransportChannelCreated();
wu@webrtc.org07a6fbe2013-11-04 18:41:34187
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 21:17:27194 rtc::Thread::Current()->Post(RTC_FROM_HERE, this, MSG_CHANNELREADY, NULL);
wu@webrtc.org07a6fbe2013-11-04 18:41:34195 }
wu@webrtc.orgcecfd182013-10-30 05:18:12196 }
197
198 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36199}
200
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14201DataChannel::~DataChannel() {}
henrike@webrtc.org28e20752013-07-10 00:45:36202
203void DataChannel::RegisterObserver(DataChannelObserver* observer) {
204 observer_ = observer;
wu@webrtc.orgd64719d2013-08-01 00:00:07205 DeliverQueuedReceivedData();
henrike@webrtc.org28e20752013-07-10 00:45:36206}
207
208void DataChannel::UnregisterObserver() {
209 observer_ = NULL;
210}
211
212bool DataChannel::reliable() const {
wu@webrtc.org78187522013-10-07 23:32:02213 if (data_channel_type_ == cricket::DCT_RTP) {
henrike@webrtc.org28e20752013-07-10 00:45:36214 return false;
215 } else {
216 return config_.maxRetransmits == -1 &&
217 config_.maxRetransmitTime == -1;
218 }
219}
220
Peter Boström0c4e06b2015-10-07 10:23:21221uint64_t DataChannel::buffered_amount() const {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14222 return queued_send_data_.byte_count();
henrike@webrtc.org28e20752013-07-10 00:45:36223}
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:50238
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:07246 // 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:14248 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:54253 Close();
254 }
255 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36256 }
henrike@webrtc.org28e20752013-07-10 00:45:36257
jiayl@webrtc.org6ca61902014-11-12 17:28:40258 bool success = SendDataMessage(buffer, true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14259 if (data_channel_type_ == cricket::DCT_RTP) {
260 return success;
wu@webrtc.orgd64719d2013-08-01 00:00:07261 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14262
263 // Always return true for SCTP DataChannel per the spec.
wu@webrtc.orgd64719d2013-08-01 00:00:07264 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36265}
266
Peter Boström0c4e06b2015-10-07 10:23:21267void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) {
wu@webrtc.orgcecfd182013-10-30 05:18:12268 ASSERT(data_channel_type_ == cricket::DCT_RTP);
269
henrike@webrtc.org28e20752013-07-10 00:45:36270 if (receive_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36271 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:14283void DataChannel::SetSctpSid(int sid) {
284 ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP);
deadbeefab9b2d12015-10-14 18:33:11285 if (config_.id == sid) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14286 return;
deadbeefab9b2d12015-10-14 18:33:11287 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14288
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 18:33:11305void DataChannel::OnTransportChannelDestroyed() {
Taylor Brandstetter4cb5b642016-08-12 17:10:31306 // 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 18:33:11310 DoClose();
311}
312
Peter Boström0c4e06b2015-10-07 10:23:21313void DataChannel::SetSendSsrc(uint32_t send_ssrc) {
wu@webrtc.orgcecfd182013-10-30 05:18:12314 ASSERT(data_channel_type_ == cricket::DCT_RTP);
henrike@webrtc.org28e20752013-07-10 00:45:36315 if (send_ssrc_set_) {
henrike@webrtc.org28e20752013-07-10 00:45:36316 return;
317 }
318 send_ssrc_ = send_ssrc;
319 send_ssrc_set_ = true;
320 UpdateState();
321}
322
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52323void DataChannel::OnMessage(rtc::Message* msg) {
wu@webrtc.org07a6fbe2013-11-04 18:41:34324 switch (msg->message_id) {
325 case MSG_CHANNELREADY:
326 OnChannelReady(true);
327 break;
328 }
329}
330
wu@webrtc.orgd64719d2013-08-01 00:00:07331void DataChannel::OnDataReceived(cricket::DataChannel* channel,
332 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 13:15:43333 const rtc::CopyOnWriteBuffer& payload) {
Peter Boström0c4e06b2015-10-07 10:23:21334 uint32_t expected_ssrc =
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52335 (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id;
336 if (params.ssrc != expected_ssrc) {
wu@webrtc.orgd64719d2013-08-01 00:00:07337 return;
338 }
339
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58340 if (params.type == cricket::DMT_CONTROL) {
341 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
Lally Singh5c6c6e02015-05-29 15:52:39342 if (handshake_state_ != kHandshakeWaitingForAck) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58343 // Ignore it if we are not expecting an ACK message.
344 LOG(LS_WARNING) << "DataChannel received unexpected CONTROL message, "
345 << "sid = " << params.ssrc;
346 return;
347 }
348 if (ParseDataChannelOpenAckMessage(payload)) {
349 // We can send unordered as soon as we receive the ACK message.
Lally Singh5c6c6e02015-05-29 15:52:39350 handshake_state_ = kHandshakeReady;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58351 LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
352 << params.ssrc;
353 } else {
354 LOG(LS_WARNING) << "DataChannel failed to parse OPEN_ACK message, sid = "
355 << params.ssrc;
356 }
357 return;
358 }
359
360 ASSERT(params.type == cricket::DMT_BINARY ||
361 params.type == cricket::DMT_TEXT);
362
363 LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " << params.ssrc;
364 // 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 15:52:39367 if (handshake_state_ == kHandshakeWaitingForAck) {
368 handshake_state_ = kHandshakeReady;
369 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58370
wu@webrtc.orgd64719d2013-08-01 00:00:07371 bool binary = (params.type == cricket::DMT_BINARY);
kwibergd1fe2812016-04-27 13:47:29372 std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
Lally Singh5c6c6e02015-05-29 15:52:39373 if (state_ == kOpen && observer_) {
hbos84ffdee2016-10-12 21:14:39374 ++messages_received_;
375 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07376 observer_->OnMessage(*buffer.get());
377 } else {
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06378 if (queued_received_data_.byte_count() + payload.size() >
379 kMaxQueuedReceivedDataBytes) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14380 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:07388 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14389 queued_received_data_.Push(buffer.release());
wu@webrtc.orgd64719d2013-08-01 00:00:07390 }
391}
392
deadbeefab9b2d12015-10-14 18:33:11393void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
kjellander3e33bfe2016-06-20 14:04:09394 if (data_channel_type_ == cricket::DCT_SCTP &&
395 sid == static_cast<uint32_t>(config_.id)) {
deadbeefab9b2d12015-10-14 18:33:11396 Close();
397 }
398}
399
wu@webrtc.orgd64719d2013-08-01 00:00:07400void DataChannel::OnChannelReady(bool writable) {
Lally Singh5c6c6e02015-05-29 15:52:39401 writable_ = writable;
wu@webrtc.orgd64719d2013-08-01 00:00:07402 if (!writable) {
403 return;
404 }
wu@webrtc.orgcecfd182013-10-30 05:18:12405
Lally Singh5c6c6e02015-05-29 15:52:39406 SendQueuedControlMessages();
407 SendQueuedDataMessages();
408 UpdateState();
wu@webrtc.orgd64719d2013-08-01 00:00:07409}
410
henrike@webrtc.org28e20752013-07-10 00:45:36411void DataChannel::DoClose() {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17412 if (state_ == kClosed)
413 return;
414
henrike@webrtc.org28e20752013-07-10 00:45:36415 receive_ssrc_set_ = false;
416 send_ssrc_set_ = false;
417 SetState(kClosing);
418 UpdateState();
419}
420
421void DataChannel::UpdateState() {
Lally Singh5c6c6e02015-05-29 15:52:39422 // UpdateState determines what to do from a few state variables. Include
423 // all conditions required for each state transition here for
424 // clarity. OnChannelReady(true) will send any queued data and then invoke
425 // UpdateState().
henrike@webrtc.org28e20752013-07-10 00:45:36426 switch (state_) {
427 case kConnecting: {
wu@webrtc.orgcecfd182013-10-30 05:18:12428 if (send_ssrc_set_ == receive_ssrc_set_) {
429 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
430 connected_to_provider_ = provider_->ConnectDataChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36431 }
Lally Singh5c6c6e02015-05-29 15:52:39432 if (connected_to_provider_) {
433 if (handshake_state_ == kHandshakeShouldSendOpen) {
jbaucheec21bd2016-03-20 13:15:43434 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 15:52:39435 WriteDataChannelOpenMessage(label_, config_, &payload);
436 SendControlMessage(payload);
437 } else if (handshake_state_ == kHandshakeShouldSendAck) {
jbaucheec21bd2016-03-20 13:15:43438 rtc::CopyOnWriteBuffer payload;
Lally Singh5c6c6e02015-05-29 15:52:39439 WriteDataChannelOpenAckMessage(&payload);
440 SendControlMessage(payload);
441 }
442 if (writable_ &&
443 (handshake_state_ == kHandshakeReady ||
444 handshake_state_ == kHandshakeWaitingForAck)) {
445 SetState(kOpen);
446 // If we have received buffers before the channel got writable.
447 // Deliver them now.
448 DeliverQueuedReceivedData();
449 }
henrike@webrtc.org28e20752013-07-10 00:45:36450 }
451 }
452 break;
453 }
454 case kOpen: {
455 break;
456 }
457 case kClosing: {
Lally Singh5c6c6e02015-05-29 15:52:39458 if (queued_send_data_.Empty() && queued_control_data_.Empty()) {
459 if (connected_to_provider_) {
460 DisconnectFromProvider();
461 }
wu@webrtc.orgcecfd182013-10-30 05:18:12462
Lally Singh5c6c6e02015-05-29 15:52:39463 if (!connected_to_provider_ && !send_ssrc_set_ && !receive_ssrc_set_) {
464 SetState(kClosed);
465 }
henrike@webrtc.org28e20752013-07-10 00:45:36466 }
467 break;
468 }
469 case kClosed:
470 break;
471 }
472}
473
474void DataChannel::SetState(DataState state) {
deadbeefab9b2d12015-10-14 18:33:11475 if (state_ == state) {
jiayl@webrtc.org9f8164c2014-05-30 21:53:17476 return;
deadbeefab9b2d12015-10-14 18:33:11477 }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17478
henrike@webrtc.org28e20752013-07-10 00:45:36479 state_ = state;
480 if (observer_) {
481 observer_->OnStateChange();
482 }
deadbeefab9b2d12015-10-14 18:33:11483 if (state_ == kClosed) {
484 SignalClosed(this);
485 }
henrike@webrtc.org28e20752013-07-10 00:45:36486}
487
Lally Singh5c6c6e02015-05-29 15:52:39488void DataChannel::DisconnectFromProvider() {
wu@webrtc.orgcecfd182013-10-30 05:18:12489 if (!connected_to_provider_)
490 return;
henrike@webrtc.org28e20752013-07-10 00:45:36491
wu@webrtc.org78187522013-10-07 23:32:02492 provider_->DisconnectDataChannel(this);
493 connected_to_provider_ = false;
wu@webrtc.orgcecfd182013-10-30 05:18:12494
bemasc@webrtc.org9b5467e2014-12-04 23:16:52495 if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) {
wu@webrtc.orgcecfd182013-10-30 05:18:12496 provider_->RemoveSctpDataStream(config_.id);
497 }
henrike@webrtc.org28e20752013-07-10 00:45:36498}
499
wu@webrtc.orgd64719d2013-08-01 00:00:07500void DataChannel::DeliverQueuedReceivedData() {
Lally Singh5c6c6e02015-05-29 15:52:39501 if (!observer_) {
wu@webrtc.orgd64719d2013-08-01 00:00:07502 return;
henrike@webrtc.org28e20752013-07-10 00:45:36503 }
henrike@webrtc.org28e20752013-07-10 00:45:36504
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14505 while (!queued_received_data_.Empty()) {
kwibergd1fe2812016-04-27 13:47:29506 std::unique_ptr<DataBuffer> buffer(queued_received_data_.Front());
hbos84ffdee2016-10-12 21:14:39507 ++messages_received_;
508 bytes_received_ += buffer->size();
wu@webrtc.orgd64719d2013-08-01 00:00:07509 observer_->OnMessage(*buffer);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14510 queued_received_data_.Pop();
henrike@webrtc.org28e20752013-07-10 00:45:36511 }
512}
513
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14514void DataChannel::SendQueuedDataMessages() {
Lally Singh5c6c6e02015-05-29 15:52:39515 if (queued_send_data_.Empty()) {
516 return;
517 }
518
519 ASSERT(state_ == kOpen || state_ == kClosing);
wu@webrtc.orgcecfd182013-10-30 05:18:12520
Peter Boström0c4e06b2015-10-07 10:23:21521 uint64_t start_buffered_amount = buffered_amount();
jiayl@webrtc.org6ca61902014-11-12 17:28:40522 while (!queued_send_data_.Empty()) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10523 DataBuffer* buffer = queued_send_data_.Front();
jiayl@webrtc.org6ca61902014-11-12 17:28:40524 if (!SendDataMessage(*buffer, false)) {
jiayl@webrtc.orgcceb1662015-01-22 00:55:10525 // Leave the message in the queue if sending is aborted.
jiayl@webrtc.org6ca61902014-11-12 17:28:40526 break;
527 }
528 queued_send_data_.Pop();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10529 delete buffer;
wu@webrtc.orgd64719d2013-08-01 00:00:07530 }
bemasc0edd50c2015-07-01 20:34:33531
532 if (observer_ && buffered_amount() < start_buffered_amount) {
533 observer_->OnBufferedAmountChange(start_buffered_amount);
534 }
wu@webrtc.orgd64719d2013-08-01 00:00:07535}
536
jiayl@webrtc.org6ca61902014-11-12 17:28:40537bool DataChannel::SendDataMessage(const DataBuffer& buffer,
538 bool queue_if_blocked) {
wu@webrtc.orgd64719d2013-08-01 00:00:07539 cricket::SendDataParams send_params;
540
wu@webrtc.org78187522013-10-07 23:32:02541 if (data_channel_type_ == cricket::DCT_SCTP) {
wu@webrtc.orgd64719d2013-08-01 00:00:07542 send_params.ordered = config_.ordered;
Lally Singh5c6c6e02015-05-29 15:52:39543 // Send as ordered if it is still going through OPEN/ACK signaling.
544 if (handshake_state_ != kHandshakeReady && !config_.ordered) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58545 send_params.ordered = true;
546 LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel "
547 << "because the OPEN_ACK message has not been received.";
548 }
549
wu@webrtc.orgd64719d2013-08-01 00:00:07550 send_params.max_rtx_count = config_.maxRetransmits;
551 send_params.max_rtx_ms = config_.maxRetransmitTime;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52552 send_params.ssrc = config_.id;
553 } else {
554 send_params.ssrc = send_ssrc_;
wu@webrtc.orgd64719d2013-08-01 00:00:07555 }
556 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
557
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14558 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
559 bool success = provider_->SendData(send_params, buffer.data, &send_result);
560
jiayl@webrtc.org6ca61902014-11-12 17:28:40561 if (success) {
hbos84ffdee2016-10-12 21:14:39562 ++messages_sent_;
563 bytes_sent_ += buffer.size();
jiayl@webrtc.org6ca61902014-11-12 17:28:40564 return true;
565 }
566
567 if (data_channel_type_ != cricket::DCT_SCTP) {
568 return false;
569 }
570
571 if (send_result == cricket::SDR_BLOCK) {
572 if (!queue_if_blocked || QueueSendDataMessage(buffer)) {
573 return false;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14574 }
575 }
jiayl@webrtc.org6ca61902014-11-12 17:28:40576 // Close the channel if the error is not SDR_BLOCK, or if queuing the
577 // message failed.
578 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
579 << "send_result = " << send_result;
580 Close();
581
582 return false;
wu@webrtc.orgd64719d2013-08-01 00:00:07583}
584
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14585bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
bemasc0edd50c2015-07-01 20:34:33586 size_t start_buffered_amount = buffered_amount();
587 if (start_buffered_amount >= kMaxQueuedSendDataBytes) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54588 LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
wu@webrtc.orgd64719d2013-08-01 00:00:07589 return false;
590 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14591 queued_send_data_.Push(new DataBuffer(buffer));
bemasc0edd50c2015-07-01 20:34:33592
593 // The buffer can have length zero, in which case there is no change.
594 if (observer_ && buffered_amount() > start_buffered_amount) {
595 observer_->OnBufferedAmountChange(start_buffered_amount);
596 }
wu@webrtc.orgd64719d2013-08-01 00:00:07597 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36598}
599
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14600void DataChannel::SendQueuedControlMessages() {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14601 PacketQueue control_packets;
602 control_packets.Swap(&queued_control_data_);
603
604 while (!control_packets.Empty()) {
kwibergd1fe2812016-04-27 13:47:29605 std::unique_ptr<DataBuffer> buf(control_packets.Front());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14606 SendControlMessage(buf->data);
607 control_packets.Pop();
608 }
wu@webrtc.orgcecfd182013-10-30 05:18:12609}
610
jbaucheec21bd2016-03-20 13:15:43611void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14612 queued_control_data_.Push(new DataBuffer(buffer, true));
613}
614
jbaucheec21bd2016-03-20 13:15:43615bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
Lally Singh5c6c6e02015-05-29 15:52:39616 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14617
618 ASSERT(data_channel_type_ == cricket::DCT_SCTP &&
Lally Singh5c6c6e02015-05-29 15:52:39619 writable_ &&
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14620 config_.id >= 0 &&
621 (!is_open_message || !config_.negotiated));
622
623 cricket::SendDataParams send_params;
624 send_params.ssrc = config_.id;
Lally Singh5c6c6e02015-05-29 15:52:39625 // Send data as ordered before we receive any message from the remote peer to
626 // make sure the remote peer will not receive any data before it receives the
627 // OPEN message.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14628 send_params.ordered = config_.ordered || is_open_message;
629 send_params.type = cricket::DMT_CONTROL;
630
631 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
632 bool retval = provider_->SendData(send_params, buffer, &send_result);
633 if (retval) {
634 LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
635
Lally Singh5c6c6e02015-05-29 15:52:39636 if (handshake_state_ == kHandshakeShouldSendAck) {
637 handshake_state_ = kHandshakeReady;
638 } else if (handshake_state_ == kHandshakeShouldSendOpen) {
639 handshake_state_ = kHandshakeWaitingForAck;
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14640 }
641 } else if (send_result == cricket::SDR_BLOCK) {
642 QueueControlMessage(buffer);
643 } else {
644 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
645 << " the CONTROL message, send_result = " << send_result;
646 Close();
wu@webrtc.orgcecfd182013-10-30 05:18:12647 }
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14648 return retval;
wu@webrtc.orgcecfd182013-10-30 05:18:12649}
650
henrike@webrtc.org28e20752013-07-10 00:45:36651} // namespace webrtc