henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2012 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 27 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 28 | #include "webrtc/api/datachannel.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | |
| 30 | #include <string> |
| 31 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 32 | #include "webrtc/api/mediastreamprovider.h" |
| 33 | #include "webrtc/api/sctputils.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 34 | #include "webrtc/base/logging.h" |
| 35 | #include "webrtc/base/refcount.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 36 | #include "webrtc/media/sctp/sctpdataengine.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | |
| 38 | namespace webrtc { |
| 39 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 40 | static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024; |
| 41 | static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 43 | enum { |
| 44 | MSG_CHANNELREADY, |
| 45 | }; |
| 46 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 47 | bool SctpSidAllocator::AllocateSid(rtc::SSLRole role, int* sid) { |
| 48 | int potential_sid = (role == rtc::SSL_CLIENT) ? 0 : 1; |
| 49 | while (!IsSidAvailable(potential_sid)) { |
| 50 | potential_sid += 2; |
| 51 | if (potential_sid > static_cast<int>(cricket::kMaxSctpSid)) { |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | *sid = potential_sid; |
| 57 | used_sids_.insert(potential_sid); |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | bool SctpSidAllocator::ReserveSid(int sid) { |
| 62 | if (!IsSidAvailable(sid)) { |
| 63 | return false; |
| 64 | } |
| 65 | used_sids_.insert(sid); |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | void SctpSidAllocator::ReleaseSid(int sid) { |
| 70 | auto it = used_sids_.find(sid); |
| 71 | if (it != used_sids_.end()) { |
| 72 | used_sids_.erase(it); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | bool SctpSidAllocator::IsSidAvailable(int sid) const { |
| 77 | if (sid < 0 || sid > static_cast<int>(cricket::kMaxSctpSid)) { |
| 78 | return false; |
| 79 | } |
| 80 | return used_sids_.find(sid) == used_sids_.end(); |
| 81 | } |
| 82 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 83 | DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {} |
| 84 | |
| 85 | DataChannel::PacketQueue::~PacketQueue() { |
| 86 | Clear(); |
| 87 | } |
| 88 | |
| 89 | bool DataChannel::PacketQueue::Empty() const { |
| 90 | return packets_.empty(); |
| 91 | } |
| 92 | |
| 93 | DataBuffer* DataChannel::PacketQueue::Front() { |
| 94 | return packets_.front(); |
| 95 | } |
| 96 | |
| 97 | void DataChannel::PacketQueue::Pop() { |
| 98 | if (packets_.empty()) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | byte_count_ -= packets_.front()->size(); |
| 103 | packets_.pop_front(); |
| 104 | } |
| 105 | |
| 106 | void DataChannel::PacketQueue::Push(DataBuffer* packet) { |
| 107 | byte_count_ += packet->size(); |
| 108 | packets_.push_back(packet); |
| 109 | } |
| 110 | |
| 111 | void DataChannel::PacketQueue::Clear() { |
| 112 | while (!packets_.empty()) { |
| 113 | delete packets_.front(); |
| 114 | packets_.pop_front(); |
| 115 | } |
| 116 | byte_count_ = 0; |
| 117 | } |
| 118 | |
| 119 | void DataChannel::PacketQueue::Swap(PacketQueue* other) { |
| 120 | size_t other_byte_count = other->byte_count_; |
| 121 | other->byte_count_ = byte_count_; |
| 122 | byte_count_ = other_byte_count; |
| 123 | |
| 124 | other->packets_.swap(packets_); |
| 125 | } |
| 126 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 127 | rtc::scoped_refptr<DataChannel> DataChannel::Create( |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 128 | DataChannelProviderInterface* provider, |
| 129 | cricket::DataChannelType dct, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 130 | const std::string& label, |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 131 | const InternalDataChannelInit& config) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 132 | rtc::scoped_refptr<DataChannel> channel( |
| 133 | new rtc::RefCountedObject<DataChannel>(provider, dct, label)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | if (!channel->Init(config)) { |
| 135 | return NULL; |
| 136 | } |
| 137 | return channel; |
| 138 | } |
| 139 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 140 | DataChannel::DataChannel( |
| 141 | DataChannelProviderInterface* provider, |
| 142 | cricket::DataChannelType dct, |
| 143 | const std::string& label) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 144 | : label_(label), |
| 145 | observer_(NULL), |
| 146 | state_(kConnecting), |
henrika@webrtc.org | 44461fa | 2014-01-13 09:35:02 +0000 | [diff] [blame] | 147 | data_channel_type_(dct), |
| 148 | provider_(provider), |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 149 | handshake_state_(kHandshakeInit), |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 150 | connected_to_provider_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 151 | send_ssrc_set_(false), |
henrika@webrtc.org | 44461fa | 2014-01-13 09:35:02 +0000 | [diff] [blame] | 152 | receive_ssrc_set_(false), |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 153 | writable_(false), |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 154 | send_ssrc_(0), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | receive_ssrc_(0) { |
| 156 | } |
| 157 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 158 | bool DataChannel::Init(const InternalDataChannelInit& config) { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 159 | if (data_channel_type_ == cricket::DCT_RTP) { |
| 160 | if (config.reliable || |
| 161 | config.id != -1 || |
| 162 | config.maxRetransmits != -1 || |
| 163 | config.maxRetransmitTime != -1) { |
| 164 | LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to " |
| 165 | << "invalid DataChannelInit."; |
| 166 | return false; |
| 167 | } |
| 168 | handshake_state_ = kHandshakeReady; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 169 | } else if (data_channel_type_ == cricket::DCT_SCTP) { |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 170 | if (config.id < -1 || |
| 171 | config.maxRetransmits < -1 || |
| 172 | config.maxRetransmitTime < -1) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 173 | LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to " |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 174 | << "invalid DataChannelInit."; |
| 175 | return false; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 176 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 177 | if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 178 | LOG(LS_ERROR) << |
| 179 | "maxRetransmits and maxRetransmitTime should not be both set."; |
| 180 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 181 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 182 | config_ = config; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 183 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 184 | switch (config_.open_handshake_role) { |
| 185 | case webrtc::InternalDataChannelInit::kNone: // pre-negotiated |
| 186 | handshake_state_ = kHandshakeReady; |
| 187 | break; |
| 188 | case webrtc::InternalDataChannelInit::kOpener: |
| 189 | handshake_state_ = kHandshakeShouldSendOpen; |
| 190 | break; |
| 191 | case webrtc::InternalDataChannelInit::kAcker: |
| 192 | handshake_state_ = kHandshakeShouldSendAck; |
| 193 | break; |
| 194 | }; |
| 195 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 196 | // Try to connect to the transport in case the transport channel already |
| 197 | // exists. |
| 198 | OnTransportChannelCreated(); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 199 | |
| 200 | // Checks if the transport is ready to send because the initial channel |
| 201 | // ready signal may have been sent before the DataChannel creation. |
| 202 | // This has to be done async because the upper layer objects (e.g. |
| 203 | // Chrome glue and WebKit) are not wired up properly until after this |
| 204 | // function returns. |
| 205 | if (provider_->ReadyToSendData()) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 206 | rtc::Thread::Current()->Post(this, MSG_CHANNELREADY, NULL); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 207 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 211 | } |
| 212 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 213 | DataChannel::~DataChannel() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 214 | |
| 215 | void DataChannel::RegisterObserver(DataChannelObserver* observer) { |
| 216 | observer_ = observer; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 217 | DeliverQueuedReceivedData(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void DataChannel::UnregisterObserver() { |
| 221 | observer_ = NULL; |
| 222 | } |
| 223 | |
| 224 | bool DataChannel::reliable() const { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 225 | if (data_channel_type_ == cricket::DCT_RTP) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 226 | return false; |
| 227 | } else { |
| 228 | return config_.maxRetransmits == -1 && |
| 229 | config_.maxRetransmitTime == -1; |
| 230 | } |
| 231 | } |
| 232 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 233 | uint64_t DataChannel::buffered_amount() const { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 234 | return queued_send_data_.byte_count(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void DataChannel::Close() { |
| 238 | if (state_ == kClosed) |
| 239 | return; |
| 240 | send_ssrc_ = 0; |
| 241 | send_ssrc_set_ = false; |
| 242 | SetState(kClosing); |
| 243 | UpdateState(); |
| 244 | } |
| 245 | |
| 246 | bool DataChannel::Send(const DataBuffer& buffer) { |
| 247 | if (state_ != kOpen) { |
| 248 | return false; |
| 249 | } |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 +0000 | [diff] [blame] | 250 | |
| 251 | // TODO(jiayl): the spec is unclear about if the remote side should get the |
| 252 | // onmessage event. We need to figure out the expected behavior and change the |
| 253 | // code accordingly. |
| 254 | if (buffer.size() == 0) { |
| 255 | return true; |
| 256 | } |
| 257 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 258 | // If the queue is non-empty, we're waiting for SignalReadyToSend, |
| 259 | // 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] | 260 | if (!queued_send_data_.Empty()) { |
| 261 | // Only SCTP DataChannel queues the outgoing data when the transport is |
| 262 | // blocked. |
| 263 | ASSERT(data_channel_type_ == cricket::DCT_SCTP); |
| 264 | if (!QueueSendDataMessage(buffer)) { |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 265 | Close(); |
| 266 | } |
| 267 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 268 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 269 | |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 270 | bool success = SendDataMessage(buffer, true); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 271 | if (data_channel_type_ == cricket::DCT_RTP) { |
| 272 | return success; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 273 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 274 | |
| 275 | // Always return true for SCTP DataChannel per the spec. |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 276 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 279 | void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 280 | ASSERT(data_channel_type_ == cricket::DCT_RTP); |
| 281 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 282 | if (receive_ssrc_set_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 283 | return; |
| 284 | } |
| 285 | receive_ssrc_ = receive_ssrc; |
| 286 | receive_ssrc_set_ = true; |
| 287 | UpdateState(); |
| 288 | } |
| 289 | |
| 290 | // The remote peer request that this channel shall be closed. |
| 291 | void DataChannel::RemotePeerRequestClose() { |
| 292 | DoClose(); |
| 293 | } |
| 294 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 295 | void DataChannel::SetSctpSid(int sid) { |
| 296 | ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 297 | if (config_.id == sid) { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 298 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 299 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 300 | |
| 301 | config_.id = sid; |
| 302 | provider_->AddSctpDataStream(sid); |
| 303 | } |
| 304 | |
| 305 | void DataChannel::OnTransportChannelCreated() { |
| 306 | ASSERT(data_channel_type_ == cricket::DCT_SCTP); |
| 307 | if (!connected_to_provider_) { |
| 308 | connected_to_provider_ = provider_->ConnectDataChannel(this); |
| 309 | } |
| 310 | // The sid may have been unassigned when provider_->ConnectDataChannel was |
| 311 | // done. So always add the streams even if connected_to_provider_ is true. |
| 312 | if (config_.id >= 0) { |
| 313 | provider_->AddSctpDataStream(config_.id); |
| 314 | } |
| 315 | } |
| 316 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 317 | // The underlying transport channel was destroyed. |
| 318 | // This function makes sure the DataChannel is disconnected and changes state to |
| 319 | // kClosed. |
| 320 | void DataChannel::OnTransportChannelDestroyed() { |
| 321 | DoClose(); |
| 322 | } |
| 323 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 324 | void DataChannel::SetSendSsrc(uint32_t send_ssrc) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 325 | ASSERT(data_channel_type_ == cricket::DCT_RTP); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 326 | if (send_ssrc_set_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 327 | return; |
| 328 | } |
| 329 | send_ssrc_ = send_ssrc; |
| 330 | send_ssrc_set_ = true; |
| 331 | UpdateState(); |
| 332 | } |
| 333 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 334 | void DataChannel::OnMessage(rtc::Message* msg) { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 335 | switch (msg->message_id) { |
| 336 | case MSG_CHANNELREADY: |
| 337 | OnChannelReady(true); |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 342 | void DataChannel::OnDataReceived(cricket::DataChannel* channel, |
| 343 | const cricket::ReceiveDataParams& params, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 344 | const rtc::Buffer& payload) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 345 | uint32_t expected_ssrc = |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 346 | (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id; |
| 347 | if (params.ssrc != expected_ssrc) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 348 | return; |
| 349 | } |
| 350 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 351 | if (params.type == cricket::DMT_CONTROL) { |
| 352 | ASSERT(data_channel_type_ == cricket::DCT_SCTP); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 353 | if (handshake_state_ != kHandshakeWaitingForAck) { |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 354 | // Ignore it if we are not expecting an ACK message. |
| 355 | LOG(LS_WARNING) << "DataChannel received unexpected CONTROL message, " |
| 356 | << "sid = " << params.ssrc; |
| 357 | return; |
| 358 | } |
| 359 | if (ParseDataChannelOpenAckMessage(payload)) { |
| 360 | // We can send unordered as soon as we receive the ACK message. |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 361 | handshake_state_ = kHandshakeReady; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 362 | LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = " |
| 363 | << params.ssrc; |
| 364 | } else { |
| 365 | LOG(LS_WARNING) << "DataChannel failed to parse OPEN_ACK message, sid = " |
| 366 | << params.ssrc; |
| 367 | } |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | ASSERT(params.type == cricket::DMT_BINARY || |
| 372 | params.type == cricket::DMT_TEXT); |
| 373 | |
| 374 | LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " << params.ssrc; |
| 375 | // We can send unordered as soon as we receive any DATA message since the |
| 376 | // remote side must have received the OPEN (and old clients do not send |
| 377 | // OPEN_ACK). |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 378 | if (handshake_state_ == kHandshakeWaitingForAck) { |
| 379 | handshake_state_ = kHandshakeReady; |
| 380 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 381 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 382 | bool binary = (params.type == cricket::DMT_BINARY); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 383 | rtc::scoped_ptr<DataBuffer> buffer(new DataBuffer(payload, binary)); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 384 | if (state_ == kOpen && observer_) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 385 | observer_->OnMessage(*buffer.get()); |
| 386 | } else { |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 387 | if (queued_received_data_.byte_count() + payload.size() > |
| 388 | kMaxQueuedReceivedDataBytes) { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 389 | LOG(LS_ERROR) << "Queued received data exceeds the max buffer size."; |
| 390 | |
| 391 | queued_received_data_.Clear(); |
| 392 | if (data_channel_type_ != cricket::DCT_RTP) { |
| 393 | Close(); |
| 394 | } |
| 395 | |
| 396 | return; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 397 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 398 | queued_received_data_.Push(buffer.release()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 402 | void DataChannel::OnStreamClosedRemotely(uint32_t sid) { |
| 403 | if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id) { |
| 404 | Close(); |
| 405 | } |
| 406 | } |
| 407 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 408 | void DataChannel::OnChannelReady(bool writable) { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 409 | writable_ = writable; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 410 | if (!writable) { |
| 411 | return; |
| 412 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 413 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 414 | SendQueuedControlMessages(); |
| 415 | SendQueuedDataMessages(); |
| 416 | UpdateState(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 417 | } |
| 418 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 419 | void DataChannel::DoClose() { |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 420 | if (state_ == kClosed) |
| 421 | return; |
| 422 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 423 | receive_ssrc_set_ = false; |
| 424 | send_ssrc_set_ = false; |
| 425 | SetState(kClosing); |
| 426 | UpdateState(); |
| 427 | } |
| 428 | |
| 429 | void DataChannel::UpdateState() { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 430 | // UpdateState determines what to do from a few state variables. Include |
| 431 | // all conditions required for each state transition here for |
| 432 | // clarity. OnChannelReady(true) will send any queued data and then invoke |
| 433 | // UpdateState(). |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 434 | switch (state_) { |
| 435 | case kConnecting: { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 436 | if (send_ssrc_set_ == receive_ssrc_set_) { |
| 437 | if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) { |
| 438 | connected_to_provider_ = provider_->ConnectDataChannel(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 439 | } |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 440 | if (connected_to_provider_) { |
| 441 | if (handshake_state_ == kHandshakeShouldSendOpen) { |
| 442 | rtc::Buffer payload; |
| 443 | WriteDataChannelOpenMessage(label_, config_, &payload); |
| 444 | SendControlMessage(payload); |
| 445 | } else if (handshake_state_ == kHandshakeShouldSendAck) { |
| 446 | rtc::Buffer payload; |
| 447 | WriteDataChannelOpenAckMessage(&payload); |
| 448 | SendControlMessage(payload); |
| 449 | } |
| 450 | if (writable_ && |
| 451 | (handshake_state_ == kHandshakeReady || |
| 452 | handshake_state_ == kHandshakeWaitingForAck)) { |
| 453 | SetState(kOpen); |
| 454 | // If we have received buffers before the channel got writable. |
| 455 | // Deliver them now. |
| 456 | DeliverQueuedReceivedData(); |
| 457 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | break; |
| 461 | } |
| 462 | case kOpen: { |
| 463 | break; |
| 464 | } |
| 465 | case kClosing: { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 466 | if (queued_send_data_.Empty() && queued_control_data_.Empty()) { |
| 467 | if (connected_to_provider_) { |
| 468 | DisconnectFromProvider(); |
| 469 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 470 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 471 | if (!connected_to_provider_ && !send_ssrc_set_ && !receive_ssrc_set_) { |
| 472 | SetState(kClosed); |
| 473 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 474 | } |
| 475 | break; |
| 476 | } |
| 477 | case kClosed: |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | void DataChannel::SetState(DataState state) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 483 | if (state_ == state) { |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 484 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 485 | } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 486 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 487 | state_ = state; |
| 488 | if (observer_) { |
| 489 | observer_->OnStateChange(); |
| 490 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 491 | if (state_ == kClosed) { |
| 492 | SignalClosed(this); |
| 493 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 496 | void DataChannel::DisconnectFromProvider() { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 497 | if (!connected_to_provider_) |
| 498 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 499 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 500 | provider_->DisconnectDataChannel(this); |
| 501 | connected_to_provider_ = false; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 502 | |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 +0000 | [diff] [blame] | 503 | if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 504 | provider_->RemoveSctpDataStream(config_.id); |
| 505 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 506 | } |
| 507 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 508 | void DataChannel::DeliverQueuedReceivedData() { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 509 | if (!observer_) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 510 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 511 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 512 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 513 | while (!queued_received_data_.Empty()) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 514 | rtc::scoped_ptr<DataBuffer> buffer(queued_received_data_.Front()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 515 | observer_->OnMessage(*buffer); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 516 | queued_received_data_.Pop(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 520 | void DataChannel::SendQueuedDataMessages() { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 521 | if (queued_send_data_.Empty()) { |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | ASSERT(state_ == kOpen || state_ == kClosing); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 526 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 527 | uint64_t start_buffered_amount = buffered_amount(); |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 528 | while (!queued_send_data_.Empty()) { |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 529 | DataBuffer* buffer = queued_send_data_.Front(); |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 530 | if (!SendDataMessage(*buffer, false)) { |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 531 | // Leave the message in the queue if sending is aborted. |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 532 | break; |
| 533 | } |
| 534 | queued_send_data_.Pop(); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 535 | delete buffer; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 536 | } |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 537 | |
| 538 | if (observer_ && buffered_amount() < start_buffered_amount) { |
| 539 | observer_->OnBufferedAmountChange(start_buffered_amount); |
| 540 | } |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 541 | } |
| 542 | |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 543 | bool DataChannel::SendDataMessage(const DataBuffer& buffer, |
| 544 | bool queue_if_blocked) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 545 | cricket::SendDataParams send_params; |
| 546 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 547 | if (data_channel_type_ == cricket::DCT_SCTP) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 548 | send_params.ordered = config_.ordered; |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 549 | // Send as ordered if it is still going through OPEN/ACK signaling. |
| 550 | if (handshake_state_ != kHandshakeReady && !config_.ordered) { |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 551 | send_params.ordered = true; |
| 552 | LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel " |
| 553 | << "because the OPEN_ACK message has not been received."; |
| 554 | } |
| 555 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 556 | send_params.max_rtx_count = config_.maxRetransmits; |
| 557 | send_params.max_rtx_ms = config_.maxRetransmitTime; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 558 | send_params.ssrc = config_.id; |
| 559 | } else { |
| 560 | send_params.ssrc = send_ssrc_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 561 | } |
| 562 | send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT; |
| 563 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 564 | cricket::SendDataResult send_result = cricket::SDR_SUCCESS; |
| 565 | bool success = provider_->SendData(send_params, buffer.data, &send_result); |
| 566 | |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 567 | if (success) { |
| 568 | return true; |
| 569 | } |
| 570 | |
| 571 | if (data_channel_type_ != cricket::DCT_SCTP) { |
| 572 | return false; |
| 573 | } |
| 574 | |
| 575 | if (send_result == cricket::SDR_BLOCK) { |
| 576 | if (!queue_if_blocked || QueueSendDataMessage(buffer)) { |
| 577 | return false; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 578 | } |
| 579 | } |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 580 | // Close the channel if the error is not SDR_BLOCK, or if queuing the |
| 581 | // message failed. |
| 582 | LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, " |
| 583 | << "send_result = " << send_result; |
| 584 | Close(); |
| 585 | |
| 586 | return false; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 587 | } |
| 588 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 589 | bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 590 | size_t start_buffered_amount = buffered_amount(); |
| 591 | if (start_buffered_amount >= kMaxQueuedSendDataBytes) { |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 592 | 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] | 593 | return false; |
| 594 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 595 | queued_send_data_.Push(new DataBuffer(buffer)); |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 596 | |
| 597 | // The buffer can have length zero, in which case there is no change. |
| 598 | if (observer_ && buffered_amount() > start_buffered_amount) { |
| 599 | observer_->OnBufferedAmountChange(start_buffered_amount); |
| 600 | } |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 601 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 602 | } |
| 603 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 604 | void DataChannel::SendQueuedControlMessages() { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 605 | PacketQueue control_packets; |
| 606 | control_packets.Swap(&queued_control_data_); |
| 607 | |
| 608 | while (!control_packets.Empty()) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 609 | rtc::scoped_ptr<DataBuffer> buf(control_packets.Front()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 610 | SendControlMessage(buf->data); |
| 611 | control_packets.Pop(); |
| 612 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 613 | } |
| 614 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 615 | void DataChannel::QueueControlMessage(const rtc::Buffer& buffer) { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 616 | queued_control_data_.Push(new DataBuffer(buffer, true)); |
| 617 | } |
| 618 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 619 | bool DataChannel::SendControlMessage(const rtc::Buffer& buffer) { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 620 | bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 621 | |
| 622 | ASSERT(data_channel_type_ == cricket::DCT_SCTP && |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 623 | writable_ && |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 624 | config_.id >= 0 && |
| 625 | (!is_open_message || !config_.negotiated)); |
| 626 | |
| 627 | cricket::SendDataParams send_params; |
| 628 | send_params.ssrc = config_.id; |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 629 | // Send data as ordered before we receive any message from the remote peer to |
| 630 | // make sure the remote peer will not receive any data before it receives the |
| 631 | // OPEN message. |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 632 | send_params.ordered = config_.ordered || is_open_message; |
| 633 | send_params.type = cricket::DMT_CONTROL; |
| 634 | |
| 635 | cricket::SendDataResult send_result = cricket::SDR_SUCCESS; |
| 636 | bool retval = provider_->SendData(send_params, buffer, &send_result); |
| 637 | if (retval) { |
| 638 | LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id; |
| 639 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 640 | if (handshake_state_ == kHandshakeShouldSendAck) { |
| 641 | handshake_state_ = kHandshakeReady; |
| 642 | } else if (handshake_state_ == kHandshakeShouldSendOpen) { |
| 643 | handshake_state_ = kHandshakeWaitingForAck; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 644 | } |
| 645 | } else if (send_result == cricket::SDR_BLOCK) { |
| 646 | QueueControlMessage(buffer); |
| 647 | } else { |
| 648 | LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send" |
| 649 | << " the CONTROL message, send_result = " << send_result; |
| 650 | Close(); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 651 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 652 | return retval; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 653 | } |
| 654 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 655 | } // namespace webrtc |