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