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