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