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