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