henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/datachannel.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 13 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | #include <string> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "media/sctp/sctptransportinternal.h" |
| 17 | #include "pc/sctputils.h" |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/logging.h" |
| 20 | #include "rtc_base/refcount.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 24 | static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024; |
| 25 | static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 26 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 27 | bool SctpSidAllocator::AllocateSid(rtc::SSLRole role, int* sid) { |
| 28 | int potential_sid = (role == rtc::SSL_CLIENT) ? 0 : 1; |
| 29 | while (!IsSidAvailable(potential_sid)) { |
| 30 | potential_sid += 2; |
| 31 | if (potential_sid > static_cast<int>(cricket::kMaxSctpSid)) { |
| 32 | return false; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | *sid = potential_sid; |
| 37 | used_sids_.insert(potential_sid); |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | bool SctpSidAllocator::ReserveSid(int sid) { |
| 42 | if (!IsSidAvailable(sid)) { |
| 43 | return false; |
| 44 | } |
| 45 | used_sids_.insert(sid); |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | void SctpSidAllocator::ReleaseSid(int sid) { |
| 50 | auto it = used_sids_.find(sid); |
| 51 | if (it != used_sids_.end()) { |
| 52 | used_sids_.erase(it); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | bool SctpSidAllocator::IsSidAvailable(int sid) const { |
Taylor Brandstetter | 1d7a637 | 2016-08-24 13:15:27 -0700 | [diff] [blame] | 57 | if (sid < static_cast<int>(cricket::kMinSctpSid) || |
| 58 | sid > static_cast<int>(cricket::kMaxSctpSid)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | return used_sids_.find(sid) == used_sids_.end(); |
| 62 | } |
| 63 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 64 | DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {} |
| 65 | |
| 66 | DataChannel::PacketQueue::~PacketQueue() { |
| 67 | Clear(); |
| 68 | } |
| 69 | |
| 70 | bool DataChannel::PacketQueue::Empty() const { |
| 71 | return packets_.empty(); |
| 72 | } |
| 73 | |
| 74 | DataBuffer* DataChannel::PacketQueue::Front() { |
| 75 | return packets_.front(); |
| 76 | } |
| 77 | |
| 78 | void DataChannel::PacketQueue::Pop() { |
| 79 | if (packets_.empty()) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | byte_count_ -= packets_.front()->size(); |
| 84 | packets_.pop_front(); |
| 85 | } |
| 86 | |
| 87 | void DataChannel::PacketQueue::Push(DataBuffer* packet) { |
| 88 | byte_count_ += packet->size(); |
| 89 | packets_.push_back(packet); |
| 90 | } |
| 91 | |
| 92 | void DataChannel::PacketQueue::Clear() { |
| 93 | while (!packets_.empty()) { |
| 94 | delete packets_.front(); |
| 95 | packets_.pop_front(); |
| 96 | } |
| 97 | byte_count_ = 0; |
| 98 | } |
| 99 | |
| 100 | void DataChannel::PacketQueue::Swap(PacketQueue* other) { |
| 101 | size_t other_byte_count = other->byte_count_; |
| 102 | other->byte_count_ = byte_count_; |
| 103 | byte_count_ = other_byte_count; |
| 104 | |
| 105 | other->packets_.swap(packets_); |
| 106 | } |
| 107 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 108 | rtc::scoped_refptr<DataChannel> DataChannel::Create( |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 109 | DataChannelProviderInterface* provider, |
| 110 | cricket::DataChannelType dct, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 111 | const std::string& label, |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 112 | const InternalDataChannelInit& config) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 113 | rtc::scoped_refptr<DataChannel> channel( |
| 114 | new rtc::RefCountedObject<DataChannel>(provider, dct, label)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | if (!channel->Init(config)) { |
| 116 | return NULL; |
| 117 | } |
| 118 | return channel; |
| 119 | } |
| 120 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 121 | DataChannel::DataChannel(DataChannelProviderInterface* provider, |
| 122 | cricket::DataChannelType dct, |
| 123 | const std::string& label) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 124 | : label_(label), |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 125 | observer_(nullptr), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 126 | state_(kConnecting), |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 127 | messages_sent_(0), |
| 128 | bytes_sent_(0), |
| 129 | messages_received_(0), |
| 130 | bytes_received_(0), |
henrika@webrtc.org | 44461fa | 2014-01-13 09:35:02 +0000 | [diff] [blame] | 131 | data_channel_type_(dct), |
| 132 | provider_(provider), |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 133 | handshake_state_(kHandshakeInit), |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 134 | connected_to_provider_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 135 | send_ssrc_set_(false), |
henrika@webrtc.org | 44461fa | 2014-01-13 09:35:02 +0000 | [diff] [blame] | 136 | receive_ssrc_set_(false), |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 137 | writable_(false), |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 138 | send_ssrc_(0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 139 | receive_ssrc_(0) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 140 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 141 | bool DataChannel::Init(const InternalDataChannelInit& config) { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 142 | if (data_channel_type_ == cricket::DCT_RTP) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 143 | if (config.reliable || config.id != -1 || config.maxRetransmits != -1 || |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 144 | config.maxRetransmitTime != -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 145 | RTC_LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 146 | "invalid DataChannelInit."; |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | handshake_state_ = kHandshakeReady; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 150 | } else if (data_channel_type_ == cricket::DCT_SCTP) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 151 | if (config.id < -1 || config.maxRetransmits < -1 || |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 152 | config.maxRetransmitTime < -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 153 | RTC_LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 154 | "invalid DataChannelInit."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | return false; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 156 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 157 | if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 158 | RTC_LOG(LS_ERROR) |
| 159 | << "maxRetransmits and maxRetransmitTime should not be both set."; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 160 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 161 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 162 | config_ = config; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 163 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 164 | switch (config_.open_handshake_role) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 165 | case webrtc::InternalDataChannelInit::kNone: // pre-negotiated |
| 166 | handshake_state_ = kHandshakeReady; |
| 167 | break; |
| 168 | case webrtc::InternalDataChannelInit::kOpener: |
| 169 | handshake_state_ = kHandshakeShouldSendOpen; |
| 170 | break; |
| 171 | case webrtc::InternalDataChannelInit::kAcker: |
| 172 | handshake_state_ = kHandshakeShouldSendAck; |
| 173 | break; |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 174 | } |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 175 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 176 | // Try to connect to the transport in case the transport channel already |
| 177 | // exists. |
| 178 | OnTransportChannelCreated(); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 179 | |
| 180 | // Checks if the transport is ready to send because the initial channel |
| 181 | // ready signal may have been sent before the DataChannel creation. |
| 182 | // This has to be done async because the upper layer objects (e.g. |
| 183 | // Chrome glue and WebKit) are not wired up properly until after this |
| 184 | // function returns. |
| 185 | if (provider_->ReadyToSendData()) { |
Steve Anton | 044a04d | 2018-08-31 13:51:19 -0700 | [diff] [blame^] | 186 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, rtc::Thread::Current(), |
| 187 | [this] { OnChannelReady(true); }); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 188 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 192 | } |
| 193 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 194 | DataChannel::~DataChannel() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 195 | |
| 196 | void DataChannel::RegisterObserver(DataChannelObserver* observer) { |
| 197 | observer_ = observer; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 198 | DeliverQueuedReceivedData(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void DataChannel::UnregisterObserver() { |
| 202 | observer_ = NULL; |
| 203 | } |
| 204 | |
| 205 | bool DataChannel::reliable() const { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 206 | if (data_channel_type_ == cricket::DCT_RTP) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 207 | return false; |
| 208 | } else { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 209 | return config_.maxRetransmits == -1 && config_.maxRetransmitTime == -1; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 213 | uint64_t DataChannel::buffered_amount() const { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 214 | return queued_send_data_.byte_count(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void DataChannel::Close() { |
| 218 | if (state_ == kClosed) |
| 219 | return; |
| 220 | send_ssrc_ = 0; |
| 221 | send_ssrc_set_ = false; |
| 222 | SetState(kClosing); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 223 | // Will send queued data before beginning the underlying closing procedure. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 224 | UpdateState(); |
| 225 | } |
| 226 | |
| 227 | bool DataChannel::Send(const DataBuffer& buffer) { |
| 228 | if (state_ != kOpen) { |
| 229 | return false; |
| 230 | } |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 +0000 | [diff] [blame] | 231 | |
| 232 | // TODO(jiayl): the spec is unclear about if the remote side should get the |
| 233 | // onmessage event. We need to figure out the expected behavior and change the |
| 234 | // code accordingly. |
| 235 | if (buffer.size() == 0) { |
| 236 | return true; |
| 237 | } |
| 238 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 239 | // If the queue is non-empty, we're waiting for SignalReadyToSend, |
| 240 | // so just add to the end of the queue and keep waiting. |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 241 | if (!queued_send_data_.Empty()) { |
| 242 | // Only SCTP DataChannel queues the outgoing data when the transport is |
| 243 | // blocked. |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 244 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 245 | if (!QueueSendDataMessage(buffer)) { |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 246 | RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to queue " |
| 247 | "additional data."; |
| 248 | CloseAbruptly(); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 249 | } |
| 250 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 251 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 252 | |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 253 | bool success = SendDataMessage(buffer, true); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 254 | if (data_channel_type_ == cricket::DCT_RTP) { |
| 255 | return success; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 256 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 257 | |
| 258 | // Always return true for SCTP DataChannel per the spec. |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 259 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 262 | void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 263 | RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 264 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 265 | if (receive_ssrc_set_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | receive_ssrc_ = receive_ssrc; |
| 269 | receive_ssrc_set_ = true; |
| 270 | UpdateState(); |
| 271 | } |
| 272 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 273 | void DataChannel::SetSctpSid(int sid) { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 274 | RTC_DCHECK_LT(config_.id, 0); |
| 275 | RTC_DCHECK_GE(sid, 0); |
| 276 | RTC_DCHECK_EQ(data_channel_type_, cricket::DCT_SCTP); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 277 | if (config_.id == sid) { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 278 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 279 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 280 | |
| 281 | config_.id = sid; |
| 282 | provider_->AddSctpDataStream(sid); |
| 283 | } |
| 284 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 285 | void DataChannel::OnClosingProcedureStartedRemotely(int sid) { |
| 286 | if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id && |
| 287 | state_ != kClosing && state_ != kClosed) { |
| 288 | // Don't bother sending queued data since the side that initiated the |
| 289 | // closure wouldn't receive it anyway. See crbug.com/559394 for a lengthy |
| 290 | // discussion about this. |
| 291 | queued_send_data_.Clear(); |
| 292 | queued_control_data_.Clear(); |
| 293 | // Just need to change state to kClosing, SctpTransport will handle the |
| 294 | // rest of the closing procedure and OnClosingProcedureComplete will be |
| 295 | // called later. |
| 296 | started_closing_procedure_ = true; |
| 297 | SetState(kClosing); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | void DataChannel::OnClosingProcedureComplete(int sid) { |
| 302 | if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id) { |
| 303 | // If the closing procedure is complete, we should have finished sending |
| 304 | // all pending data and transitioned to kClosing already. |
| 305 | RTC_DCHECK_EQ(state_, kClosing); |
| 306 | RTC_DCHECK(queued_send_data_.Empty()); |
| 307 | DisconnectFromProvider(); |
| 308 | SetState(kClosed); |
| 309 | } |
| 310 | } |
| 311 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 312 | void DataChannel::OnTransportChannelCreated() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 313 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 314 | if (!connected_to_provider_) { |
| 315 | connected_to_provider_ = provider_->ConnectDataChannel(this); |
| 316 | } |
| 317 | // The sid may have been unassigned when provider_->ConnectDataChannel was |
| 318 | // done. So always add the streams even if connected_to_provider_ is true. |
| 319 | if (config_.id >= 0) { |
| 320 | provider_->AddSctpDataStream(config_.id); |
| 321 | } |
| 322 | } |
| 323 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 324 | void DataChannel::OnTransportChannelDestroyed() { |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 325 | // The SctpTransport is going away (for example, because the SCTP m= section |
| 326 | // was rejected), so we need to close abruptly. |
| 327 | CloseAbruptly(); |
| 328 | } |
| 329 | |
| 330 | // The remote peer request that this channel shall be closed. |
| 331 | void DataChannel::RemotePeerRequestClose() { |
| 332 | RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP); |
| 333 | CloseAbruptly(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 336 | void DataChannel::SetSendSsrc(uint32_t send_ssrc) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 337 | RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 338 | if (send_ssrc_set_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 339 | return; |
| 340 | } |
| 341 | send_ssrc_ = send_ssrc; |
| 342 | send_ssrc_set_ = true; |
| 343 | UpdateState(); |
| 344 | } |
| 345 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 346 | void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params, |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 347 | const rtc::CopyOnWriteBuffer& payload) { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 348 | if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) { |
| 349 | return; |
| 350 | } |
| 351 | if (data_channel_type_ == cricket::DCT_SCTP && params.sid != config_.id) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 352 | return; |
| 353 | } |
| 354 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 355 | if (params.type == cricket::DMT_CONTROL) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 356 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 357 | if (handshake_state_ != kHandshakeWaitingForAck) { |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 358 | // Ignore it if we are not expecting an ACK message. |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 359 | RTC_LOG(LS_WARNING) |
| 360 | << "DataChannel received unexpected CONTROL message, sid = " |
| 361 | << params.sid; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 362 | return; |
| 363 | } |
| 364 | if (ParseDataChannelOpenAckMessage(payload)) { |
| 365 | // We can send unordered as soon as we receive the ACK message. |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 366 | handshake_state_ = kHandshakeReady; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 367 | RTC_LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = " |
| 368 | << params.sid; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 369 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 370 | RTC_LOG(LS_WARNING) |
| 371 | << "DataChannel failed to parse OPEN_ACK message, sid = " |
| 372 | << params.sid; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 373 | } |
| 374 | return; |
| 375 | } |
| 376 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 377 | RTC_DCHECK(params.type == cricket::DMT_BINARY || |
| 378 | params.type == cricket::DMT_TEXT); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 379 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 380 | RTC_LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " |
| 381 | << params.sid; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 382 | // We can send unordered as soon as we receive any DATA message since the |
| 383 | // remote side must have received the OPEN (and old clients do not send |
| 384 | // OPEN_ACK). |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 385 | if (handshake_state_ == kHandshakeWaitingForAck) { |
| 386 | handshake_state_ = kHandshakeReady; |
| 387 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 388 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 389 | bool binary = (params.type == cricket::DMT_BINARY); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 390 | std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary)); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 391 | if (state_ == kOpen && observer_) { |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 392 | ++messages_received_; |
| 393 | bytes_received_ += buffer->size(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 394 | observer_->OnMessage(*buffer.get()); |
| 395 | } else { |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 396 | if (queued_received_data_.byte_count() + payload.size() > |
| 397 | kMaxQueuedReceivedDataBytes) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 398 | RTC_LOG(LS_ERROR) << "Queued received data exceeds the max buffer size."; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 399 | |
| 400 | queued_received_data_.Clear(); |
| 401 | if (data_channel_type_ != cricket::DCT_RTP) { |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 402 | CloseAbruptly(); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | return; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 406 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 407 | queued_received_data_.Push(buffer.release()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
| 411 | void DataChannel::OnChannelReady(bool writable) { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 412 | writable_ = writable; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 413 | if (!writable) { |
| 414 | return; |
| 415 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 416 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 417 | SendQueuedControlMessages(); |
| 418 | SendQueuedDataMessages(); |
| 419 | UpdateState(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 422 | void DataChannel::CloseAbruptly() { |
| 423 | if (state_ == kClosed) { |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 424 | return; |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 425 | } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 426 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 427 | if (connected_to_provider_) { |
| 428 | DisconnectFromProvider(); |
| 429 | } |
| 430 | |
| 431 | // Closing abruptly means any queued data gets thrown away. |
| 432 | queued_send_data_.Clear(); |
| 433 | queued_control_data_.Clear(); |
| 434 | |
| 435 | // Still go to "kClosing" before "kClosed", since observers may be expecting |
| 436 | // that. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 437 | SetState(kClosing); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 438 | SetState(kClosed); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | void DataChannel::UpdateState() { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 442 | // UpdateState determines what to do from a few state variables. Include |
| 443 | // all conditions required for each state transition here for |
| 444 | // clarity. OnChannelReady(true) will send any queued data and then invoke |
| 445 | // UpdateState(). |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 446 | switch (state_) { |
| 447 | case kConnecting: { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 448 | if (send_ssrc_set_ == receive_ssrc_set_) { |
| 449 | if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) { |
| 450 | connected_to_provider_ = provider_->ConnectDataChannel(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 451 | } |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 452 | if (connected_to_provider_) { |
| 453 | if (handshake_state_ == kHandshakeShouldSendOpen) { |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 454 | rtc::CopyOnWriteBuffer payload; |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 455 | WriteDataChannelOpenMessage(label_, config_, &payload); |
| 456 | SendControlMessage(payload); |
| 457 | } else if (handshake_state_ == kHandshakeShouldSendAck) { |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 458 | rtc::CopyOnWriteBuffer payload; |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 459 | WriteDataChannelOpenAckMessage(&payload); |
| 460 | SendControlMessage(payload); |
| 461 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 462 | if (writable_ && (handshake_state_ == kHandshakeReady || |
| 463 | handshake_state_ == kHandshakeWaitingForAck)) { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 464 | SetState(kOpen); |
| 465 | // If we have received buffers before the channel got writable. |
| 466 | // Deliver them now. |
| 467 | DeliverQueuedReceivedData(); |
| 468 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | break; |
| 472 | } |
| 473 | case kOpen: { |
| 474 | break; |
| 475 | } |
| 476 | case kClosing: { |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 477 | // Wait for all queued data to be sent before beginning the closing |
| 478 | // procedure. |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 479 | if (queued_send_data_.Empty() && queued_control_data_.Empty()) { |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 480 | if (data_channel_type_ == cricket::DCT_RTP) { |
| 481 | // For RTP data channels, we can go to "closed" after we finish |
| 482 | // sending data and the send/recv SSRCs are unset. |
| 483 | if (connected_to_provider_) { |
| 484 | DisconnectFromProvider(); |
| 485 | } |
| 486 | if (!send_ssrc_set_ && !receive_ssrc_set_) { |
| 487 | SetState(kClosed); |
| 488 | } |
| 489 | } else { |
| 490 | // For SCTP data channels, we need to wait for the closing procedure |
| 491 | // to complete; after calling RemoveSctpDataStream, |
| 492 | // OnClosingProcedureComplete will end up called asynchronously |
| 493 | // afterwards. |
| 494 | if (connected_to_provider_ && !started_closing_procedure_ && |
| 495 | config_.id >= 0) { |
| 496 | started_closing_procedure_ = true; |
| 497 | provider_->RemoveSctpDataStream(config_.id); |
| 498 | } |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 499 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 500 | } |
| 501 | break; |
| 502 | } |
| 503 | case kClosed: |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | void DataChannel::SetState(DataState state) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 509 | if (state_ == state) { |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 510 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 511 | } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 512 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 513 | state_ = state; |
| 514 | if (observer_) { |
| 515 | observer_->OnStateChange(); |
| 516 | } |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 517 | if (state_ == kOpen) { |
| 518 | SignalOpened(this); |
| 519 | } else if (state_ == kClosed) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 520 | SignalClosed(this); |
| 521 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 524 | void DataChannel::DisconnectFromProvider() { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 525 | if (!connected_to_provider_) |
| 526 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 527 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 528 | provider_->DisconnectDataChannel(this); |
| 529 | connected_to_provider_ = false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 530 | } |
| 531 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 532 | void DataChannel::DeliverQueuedReceivedData() { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 533 | if (!observer_) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 534 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 535 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 536 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 537 | while (!queued_received_data_.Empty()) { |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 538 | std::unique_ptr<DataBuffer> buffer(queued_received_data_.Front()); |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 539 | ++messages_received_; |
| 540 | bytes_received_ += buffer->size(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 541 | observer_->OnMessage(*buffer); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 542 | queued_received_data_.Pop(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 546 | void DataChannel::SendQueuedDataMessages() { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 547 | if (queued_send_data_.Empty()) { |
| 548 | return; |
| 549 | } |
| 550 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 551 | RTC_DCHECK(state_ == kOpen || state_ == kClosing); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 552 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 553 | uint64_t start_buffered_amount = buffered_amount(); |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 554 | while (!queued_send_data_.Empty()) { |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 555 | DataBuffer* buffer = queued_send_data_.Front(); |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 556 | if (!SendDataMessage(*buffer, false)) { |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 557 | // Leave the message in the queue if sending is aborted. |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 558 | break; |
| 559 | } |
| 560 | queued_send_data_.Pop(); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 561 | delete buffer; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 562 | } |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 563 | |
| 564 | if (observer_ && buffered_amount() < start_buffered_amount) { |
| 565 | observer_->OnBufferedAmountChange(start_buffered_amount); |
| 566 | } |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 567 | } |
| 568 | |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 569 | bool DataChannel::SendDataMessage(const DataBuffer& buffer, |
| 570 | bool queue_if_blocked) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 571 | cricket::SendDataParams send_params; |
| 572 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 573 | if (data_channel_type_ == cricket::DCT_SCTP) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 574 | send_params.ordered = config_.ordered; |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 575 | // Send as ordered if it is still going through OPEN/ACK signaling. |
| 576 | if (handshake_state_ != kHandshakeReady && !config_.ordered) { |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 577 | send_params.ordered = true; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 578 | RTC_LOG(LS_VERBOSE) |
| 579 | << "Sending data as ordered for unordered DataChannel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 580 | "because the OPEN_ACK message has not been received."; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 581 | } |
| 582 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 583 | send_params.max_rtx_count = config_.maxRetransmits; |
| 584 | send_params.max_rtx_ms = config_.maxRetransmitTime; |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 585 | send_params.sid = config_.id; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 586 | } else { |
| 587 | send_params.ssrc = send_ssrc_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 588 | } |
| 589 | send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT; |
| 590 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 591 | cricket::SendDataResult send_result = cricket::SDR_SUCCESS; |
| 592 | bool success = provider_->SendData(send_params, buffer.data, &send_result); |
| 593 | |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 594 | if (success) { |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 595 | ++messages_sent_; |
| 596 | bytes_sent_ += buffer.size(); |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 597 | return true; |
| 598 | } |
| 599 | |
| 600 | if (data_channel_type_ != cricket::DCT_SCTP) { |
| 601 | return false; |
| 602 | } |
| 603 | |
| 604 | if (send_result == cricket::SDR_BLOCK) { |
| 605 | if (!queue_if_blocked || QueueSendDataMessage(buffer)) { |
| 606 | return false; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 609 | // Close the channel if the error is not SDR_BLOCK, or if queuing the |
| 610 | // message failed. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 611 | RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 612 | "send_result = " |
| 613 | << send_result; |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 614 | CloseAbruptly(); |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 615 | |
| 616 | return false; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 617 | } |
| 618 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 619 | bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 620 | size_t start_buffered_amount = buffered_amount(); |
| 621 | if (start_buffered_amount >= kMaxQueuedSendDataBytes) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 622 | RTC_LOG(LS_ERROR) << "Can't buffer any more data for the data channel."; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 623 | return false; |
| 624 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 625 | queued_send_data_.Push(new DataBuffer(buffer)); |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 626 | |
| 627 | // The buffer can have length zero, in which case there is no change. |
| 628 | if (observer_ && buffered_amount() > start_buffered_amount) { |
| 629 | observer_->OnBufferedAmountChange(start_buffered_amount); |
| 630 | } |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 631 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 632 | } |
| 633 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 634 | void DataChannel::SendQueuedControlMessages() { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 635 | PacketQueue control_packets; |
| 636 | control_packets.Swap(&queued_control_data_); |
| 637 | |
| 638 | while (!control_packets.Empty()) { |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 639 | std::unique_ptr<DataBuffer> buf(control_packets.Front()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 640 | SendControlMessage(buf->data); |
| 641 | control_packets.Pop(); |
| 642 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 643 | } |
| 644 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 645 | void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 646 | queued_control_data_.Push(new DataBuffer(buffer, true)); |
| 647 | } |
| 648 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 649 | bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 650 | bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 651 | |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 652 | RTC_DCHECK_EQ(data_channel_type_, cricket::DCT_SCTP); |
| 653 | RTC_DCHECK(writable_); |
| 654 | RTC_DCHECK_GE(config_.id, 0); |
| 655 | RTC_DCHECK(!is_open_message || !config_.negotiated); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 656 | |
| 657 | cricket::SendDataParams send_params; |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 658 | send_params.sid = config_.id; |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 659 | // Send data as ordered before we receive any message from the remote peer to |
| 660 | // make sure the remote peer will not receive any data before it receives the |
| 661 | // OPEN message. |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 662 | send_params.ordered = config_.ordered || is_open_message; |
| 663 | send_params.type = cricket::DMT_CONTROL; |
| 664 | |
| 665 | cricket::SendDataResult send_result = cricket::SDR_SUCCESS; |
| 666 | bool retval = provider_->SendData(send_params, buffer, &send_result); |
| 667 | if (retval) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 668 | RTC_LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 669 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 670 | if (handshake_state_ == kHandshakeShouldSendAck) { |
| 671 | handshake_state_ = kHandshakeReady; |
| 672 | } else if (handshake_state_ == kHandshakeShouldSendOpen) { |
| 673 | handshake_state_ = kHandshakeWaitingForAck; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 674 | } |
| 675 | } else if (send_result == cricket::SDR_BLOCK) { |
| 676 | QueueControlMessage(buffer); |
| 677 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 678 | RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 679 | " the CONTROL message, send_result = " |
| 680 | << send_result; |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 681 | CloseAbruptly(); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 682 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 683 | return retval; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 684 | } |
| 685 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 686 | } // namespace webrtc |