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