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 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 28 | #include "talk/app/webrtc/datachannel.h" |
| 29 | |
| 30 | #include <string> |
| 31 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 32 | #include "talk/app/webrtc/mediastreamprovider.h" |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 33 | #include "talk/app/webrtc/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" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | |
| 37 | namespace webrtc { |
| 38 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 39 | static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024; |
| 40 | static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 41 | |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 42 | enum { |
| 43 | MSG_CHANNELREADY, |
| 44 | }; |
| 45 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 46 | DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {} |
| 47 | |
| 48 | DataChannel::PacketQueue::~PacketQueue() { |
| 49 | Clear(); |
| 50 | } |
| 51 | |
| 52 | bool DataChannel::PacketQueue::Empty() const { |
| 53 | return packets_.empty(); |
| 54 | } |
| 55 | |
| 56 | DataBuffer* DataChannel::PacketQueue::Front() { |
| 57 | return packets_.front(); |
| 58 | } |
| 59 | |
| 60 | void DataChannel::PacketQueue::Pop() { |
| 61 | if (packets_.empty()) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | byte_count_ -= packets_.front()->size(); |
| 66 | packets_.pop_front(); |
| 67 | } |
| 68 | |
| 69 | void DataChannel::PacketQueue::Push(DataBuffer* packet) { |
| 70 | byte_count_ += packet->size(); |
| 71 | packets_.push_back(packet); |
| 72 | } |
| 73 | |
| 74 | void DataChannel::PacketQueue::Clear() { |
| 75 | while (!packets_.empty()) { |
| 76 | delete packets_.front(); |
| 77 | packets_.pop_front(); |
| 78 | } |
| 79 | byte_count_ = 0; |
| 80 | } |
| 81 | |
| 82 | void DataChannel::PacketQueue::Swap(PacketQueue* other) { |
| 83 | size_t other_byte_count = other->byte_count_; |
| 84 | other->byte_count_ = byte_count_; |
| 85 | byte_count_ = other_byte_count; |
| 86 | |
| 87 | other->packets_.swap(packets_); |
| 88 | } |
| 89 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 90 | rtc::scoped_refptr<DataChannel> DataChannel::Create( |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 91 | DataChannelProviderInterface* provider, |
| 92 | cricket::DataChannelType dct, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 93 | const std::string& label, |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 94 | const InternalDataChannelInit& config) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 95 | rtc::scoped_refptr<DataChannel> channel( |
| 96 | new rtc::RefCountedObject<DataChannel>(provider, dct, label)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | if (!channel->Init(config)) { |
| 98 | return NULL; |
| 99 | } |
| 100 | return channel; |
| 101 | } |
| 102 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 103 | DataChannel::DataChannel( |
| 104 | DataChannelProviderInterface* provider, |
| 105 | cricket::DataChannelType dct, |
| 106 | const std::string& label) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | : label_(label), |
| 108 | observer_(NULL), |
| 109 | state_(kConnecting), |
henrika@webrtc.org | 44461fa | 2014-01-13 09:35:02 +0000 | [diff] [blame] | 110 | data_channel_type_(dct), |
| 111 | provider_(provider), |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 112 | waiting_for_open_ack_(false), |
| 113 | was_ever_writable_(false), |
| 114 | connected_to_provider_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | send_ssrc_set_(false), |
henrika@webrtc.org | 44461fa | 2014-01-13 09:35:02 +0000 | [diff] [blame] | 116 | receive_ssrc_set_(false), |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 117 | send_ssrc_(0), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | receive_ssrc_(0) { |
| 119 | } |
| 120 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 121 | bool DataChannel::Init(const InternalDataChannelInit& config) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 122 | if (data_channel_type_ == cricket::DCT_RTP && |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 123 | (config.reliable || |
| 124 | config.id != -1 || |
| 125 | config.maxRetransmits != -1 || |
| 126 | config.maxRetransmitTime != -1)) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 127 | LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to " |
| 128 | << "invalid DataChannelInit."; |
| 129 | return false; |
| 130 | } else if (data_channel_type_ == cricket::DCT_SCTP) { |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 131 | if (config.id < -1 || |
| 132 | config.maxRetransmits < -1 || |
| 133 | config.maxRetransmitTime < -1) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 134 | 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] | 135 | << "invalid DataChannelInit."; |
| 136 | return false; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 137 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 138 | if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 139 | LOG(LS_ERROR) << |
| 140 | "maxRetransmits and maxRetransmitTime should not be both set."; |
| 141 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 142 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 143 | config_ = config; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 144 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 145 | // Try to connect to the transport in case the transport channel already |
| 146 | // exists. |
| 147 | OnTransportChannelCreated(); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 148 | |
| 149 | // Checks if the transport is ready to send because the initial channel |
| 150 | // ready signal may have been sent before the DataChannel creation. |
| 151 | // This has to be done async because the upper layer objects (e.g. |
| 152 | // Chrome glue and WebKit) are not wired up properly until after this |
| 153 | // function returns. |
| 154 | if (provider_->ReadyToSendData()) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 155 | rtc::Thread::Current()->Post(this, MSG_CHANNELREADY, NULL); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 156 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 160 | } |
| 161 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 162 | DataChannel::~DataChannel() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 163 | |
| 164 | void DataChannel::RegisterObserver(DataChannelObserver* observer) { |
| 165 | observer_ = observer; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 166 | DeliverQueuedReceivedData(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void DataChannel::UnregisterObserver() { |
| 170 | observer_ = NULL; |
| 171 | } |
| 172 | |
| 173 | bool DataChannel::reliable() const { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 174 | if (data_channel_type_ == cricket::DCT_RTP) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 175 | return false; |
| 176 | } else { |
| 177 | return config_.maxRetransmits == -1 && |
| 178 | config_.maxRetransmitTime == -1; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | uint64 DataChannel::buffered_amount() const { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 183 | return queued_send_data_.byte_count(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void DataChannel::Close() { |
| 187 | if (state_ == kClosed) |
| 188 | return; |
| 189 | send_ssrc_ = 0; |
| 190 | send_ssrc_set_ = false; |
| 191 | SetState(kClosing); |
| 192 | UpdateState(); |
| 193 | } |
| 194 | |
| 195 | bool DataChannel::Send(const DataBuffer& buffer) { |
| 196 | if (state_ != kOpen) { |
| 197 | return false; |
| 198 | } |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 +0000 | [diff] [blame] | 199 | |
| 200 | // TODO(jiayl): the spec is unclear about if the remote side should get the |
| 201 | // onmessage event. We need to figure out the expected behavior and change the |
| 202 | // code accordingly. |
| 203 | if (buffer.size() == 0) { |
| 204 | return true; |
| 205 | } |
| 206 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 207 | // If the queue is non-empty, we're waiting for SignalReadyToSend, |
| 208 | // 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] | 209 | if (!queued_send_data_.Empty()) { |
| 210 | // Only SCTP DataChannel queues the outgoing data when the transport is |
| 211 | // blocked. |
| 212 | ASSERT(data_channel_type_ == cricket::DCT_SCTP); |
| 213 | if (!QueueSendDataMessage(buffer)) { |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 214 | Close(); |
| 215 | } |
| 216 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 217 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 218 | |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 219 | bool success = SendDataMessage(buffer, true); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 220 | if (data_channel_type_ == cricket::DCT_RTP) { |
| 221 | return success; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 222 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 223 | |
| 224 | // Always return true for SCTP DataChannel per the spec. |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 225 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void DataChannel::SetReceiveSsrc(uint32 receive_ssrc) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 229 | ASSERT(data_channel_type_ == cricket::DCT_RTP); |
| 230 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 231 | if (receive_ssrc_set_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | receive_ssrc_ = receive_ssrc; |
| 235 | receive_ssrc_set_ = true; |
| 236 | UpdateState(); |
| 237 | } |
| 238 | |
| 239 | // The remote peer request that this channel shall be closed. |
| 240 | void DataChannel::RemotePeerRequestClose() { |
| 241 | DoClose(); |
| 242 | } |
| 243 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 244 | void DataChannel::SetSctpSid(int sid) { |
| 245 | ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP); |
| 246 | if (config_.id == sid) |
| 247 | return; |
| 248 | |
| 249 | config_.id = sid; |
| 250 | provider_->AddSctpDataStream(sid); |
| 251 | } |
| 252 | |
| 253 | void DataChannel::OnTransportChannelCreated() { |
| 254 | ASSERT(data_channel_type_ == cricket::DCT_SCTP); |
| 255 | if (!connected_to_provider_) { |
| 256 | connected_to_provider_ = provider_->ConnectDataChannel(this); |
| 257 | } |
| 258 | // The sid may have been unassigned when provider_->ConnectDataChannel was |
| 259 | // done. So always add the streams even if connected_to_provider_ is true. |
| 260 | if (config_.id >= 0) { |
| 261 | provider_->AddSctpDataStream(config_.id); |
| 262 | } |
| 263 | } |
| 264 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 265 | void DataChannel::SetSendSsrc(uint32 send_ssrc) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 266 | ASSERT(data_channel_type_ == cricket::DCT_RTP); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 267 | if (send_ssrc_set_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 268 | return; |
| 269 | } |
| 270 | send_ssrc_ = send_ssrc; |
| 271 | send_ssrc_set_ = true; |
| 272 | UpdateState(); |
| 273 | } |
| 274 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 275 | void DataChannel::OnMessage(rtc::Message* msg) { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 276 | switch (msg->message_id) { |
| 277 | case MSG_CHANNELREADY: |
| 278 | OnChannelReady(true); |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 283 | // The underlaying data engine is closing. |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 284 | // This function makes sure the DataChannel is disconnected and changes state to |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 285 | // kClosed. |
| 286 | void DataChannel::OnDataEngineClose() { |
| 287 | DoClose(); |
| 288 | } |
| 289 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 290 | void DataChannel::OnDataReceived(cricket::DataChannel* channel, |
| 291 | const cricket::ReceiveDataParams& params, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 292 | const rtc::Buffer& payload) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 293 | uint32 expected_ssrc = |
| 294 | (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id; |
| 295 | if (params.ssrc != expected_ssrc) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 296 | return; |
| 297 | } |
| 298 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 299 | if (params.type == cricket::DMT_CONTROL) { |
| 300 | ASSERT(data_channel_type_ == cricket::DCT_SCTP); |
| 301 | if (!waiting_for_open_ack_) { |
| 302 | // Ignore it if we are not expecting an ACK message. |
| 303 | LOG(LS_WARNING) << "DataChannel received unexpected CONTROL message, " |
| 304 | << "sid = " << params.ssrc; |
| 305 | return; |
| 306 | } |
| 307 | if (ParseDataChannelOpenAckMessage(payload)) { |
| 308 | // We can send unordered as soon as we receive the ACK message. |
| 309 | waiting_for_open_ack_ = false; |
| 310 | LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = " |
| 311 | << params.ssrc; |
| 312 | } else { |
| 313 | LOG(LS_WARNING) << "DataChannel failed to parse OPEN_ACK message, sid = " |
| 314 | << params.ssrc; |
| 315 | } |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | ASSERT(params.type == cricket::DMT_BINARY || |
| 320 | params.type == cricket::DMT_TEXT); |
| 321 | |
| 322 | LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " << params.ssrc; |
| 323 | // We can send unordered as soon as we receive any DATA message since the |
| 324 | // remote side must have received the OPEN (and old clients do not send |
| 325 | // OPEN_ACK). |
| 326 | waiting_for_open_ack_ = false; |
| 327 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 328 | bool binary = (params.type == cricket::DMT_BINARY); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 329 | rtc::scoped_ptr<DataBuffer> buffer(new DataBuffer(payload, binary)); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 330 | if (was_ever_writable_ && observer_) { |
| 331 | observer_->OnMessage(*buffer.get()); |
| 332 | } else { |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame^] | 333 | if (queued_received_data_.byte_count() + payload.size() > |
| 334 | kMaxQueuedReceivedDataBytes) { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 335 | LOG(LS_ERROR) << "Queued received data exceeds the max buffer size."; |
| 336 | |
| 337 | queued_received_data_.Clear(); |
| 338 | if (data_channel_type_ != cricket::DCT_RTP) { |
| 339 | Close(); |
| 340 | } |
| 341 | |
| 342 | return; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 343 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 344 | queued_received_data_.Push(buffer.release()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | void DataChannel::OnChannelReady(bool writable) { |
| 349 | if (!writable) { |
| 350 | return; |
| 351 | } |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 352 | // Update the readyState and send the queued control message if the channel |
| 353 | // is writable for the first time; otherwise it means the channel was blocked |
| 354 | // for sending and now unblocked, so send the queued data now. |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 355 | if (!was_ever_writable_) { |
| 356 | was_ever_writable_ = true; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 357 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 358 | if (data_channel_type_ == cricket::DCT_SCTP) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 359 | rtc::Buffer payload; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 360 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 361 | if (config_.open_handshake_role == InternalDataChannelInit::kOpener) { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 362 | WriteDataChannelOpenMessage(label_, config_, &payload); |
| 363 | SendControlMessage(payload); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 364 | } else if (config_.open_handshake_role == |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 365 | InternalDataChannelInit::kAcker) { |
| 366 | WriteDataChannelOpenAckMessage(&payload); |
| 367 | SendControlMessage(payload); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 368 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 369 | } |
| 370 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 371 | UpdateState(); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 372 | ASSERT(queued_send_data_.Empty()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 373 | } else if (state_ == kOpen) { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 374 | // TODO(jiayl): Sending OPEN message here contradicts with the pre-condition |
| 375 | // that the readyState is open. According to the standard, the channel |
| 376 | // should not become open before the OPEN message is sent. |
| 377 | SendQueuedControlMessages(); |
| 378 | |
| 379 | SendQueuedDataMessages(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 383 | void DataChannel::DoClose() { |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 384 | if (state_ == kClosed) |
| 385 | return; |
| 386 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 387 | receive_ssrc_set_ = false; |
| 388 | send_ssrc_set_ = false; |
| 389 | SetState(kClosing); |
| 390 | UpdateState(); |
| 391 | } |
| 392 | |
| 393 | void DataChannel::UpdateState() { |
| 394 | switch (state_) { |
| 395 | case kConnecting: { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 396 | if (send_ssrc_set_ == receive_ssrc_set_) { |
| 397 | if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) { |
| 398 | connected_to_provider_ = provider_->ConnectDataChannel(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 399 | } |
| 400 | if (was_ever_writable_) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 401 | // TODO(jiayl): Do not transition to kOpen if we failed to send the |
| 402 | // OPEN message. |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 403 | SendQueuedControlMessages(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 404 | SetState(kOpen); |
| 405 | // If we have received buffers before the channel got writable. |
| 406 | // Deliver them now. |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 407 | DeliverQueuedReceivedData(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | break; |
| 411 | } |
| 412 | case kOpen: { |
| 413 | break; |
| 414 | } |
| 415 | case kClosing: { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 416 | DisconnectFromTransport(); |
| 417 | |
| 418 | if (!send_ssrc_set_ && !receive_ssrc_set_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 419 | SetState(kClosed); |
| 420 | } |
| 421 | break; |
| 422 | } |
| 423 | case kClosed: |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | void DataChannel::SetState(DataState state) { |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 429 | if (state_ == state) |
| 430 | return; |
| 431 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 432 | state_ = state; |
| 433 | if (observer_) { |
| 434 | observer_->OnStateChange(); |
| 435 | } |
| 436 | } |
| 437 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 438 | void DataChannel::DisconnectFromTransport() { |
| 439 | if (!connected_to_provider_) |
| 440 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 441 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 442 | provider_->DisconnectDataChannel(this); |
| 443 | connected_to_provider_ = false; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 444 | |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 +0000 | [diff] [blame] | 445 | if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 446 | provider_->RemoveSctpDataStream(config_.id); |
| 447 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 448 | } |
| 449 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 450 | void DataChannel::DeliverQueuedReceivedData() { |
| 451 | if (!was_ever_writable_ || !observer_) { |
| 452 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 453 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 454 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 455 | while (!queued_received_data_.Empty()) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 456 | rtc::scoped_ptr<DataBuffer> buffer(queued_received_data_.Front()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 457 | observer_->OnMessage(*buffer); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 458 | queued_received_data_.Pop(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 462 | void DataChannel::SendQueuedDataMessages() { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 463 | ASSERT(was_ever_writable_ && state_ == kOpen); |
| 464 | |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 465 | while (!queued_send_data_.Empty()) { |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 466 | DataBuffer* buffer = queued_send_data_.Front(); |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 467 | if (!SendDataMessage(*buffer, false)) { |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 468 | // Leave the message in the queue if sending is aborted. |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 469 | break; |
| 470 | } |
| 471 | queued_send_data_.Pop(); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 472 | delete buffer; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 476 | bool DataChannel::SendDataMessage(const DataBuffer& buffer, |
| 477 | bool queue_if_blocked) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 478 | cricket::SendDataParams send_params; |
| 479 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 480 | if (data_channel_type_ == cricket::DCT_SCTP) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 481 | send_params.ordered = config_.ordered; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 482 | // Send as ordered if it is waiting for the OPEN_ACK message. |
| 483 | if (waiting_for_open_ack_ && !config_.ordered) { |
| 484 | send_params.ordered = true; |
| 485 | LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel " |
| 486 | << "because the OPEN_ACK message has not been received."; |
| 487 | } |
| 488 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 489 | send_params.max_rtx_count = config_.maxRetransmits; |
| 490 | send_params.max_rtx_ms = config_.maxRetransmitTime; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 491 | send_params.ssrc = config_.id; |
| 492 | } else { |
| 493 | send_params.ssrc = send_ssrc_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 494 | } |
| 495 | send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT; |
| 496 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 497 | cricket::SendDataResult send_result = cricket::SDR_SUCCESS; |
| 498 | bool success = provider_->SendData(send_params, buffer.data, &send_result); |
| 499 | |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 500 | if (success) { |
| 501 | return true; |
| 502 | } |
| 503 | |
| 504 | if (data_channel_type_ != cricket::DCT_SCTP) { |
| 505 | return false; |
| 506 | } |
| 507 | |
| 508 | if (send_result == cricket::SDR_BLOCK) { |
| 509 | if (!queue_if_blocked || QueueSendDataMessage(buffer)) { |
| 510 | return false; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 511 | } |
| 512 | } |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 513 | // Close the channel if the error is not SDR_BLOCK, or if queuing the |
| 514 | // message failed. |
| 515 | LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, " |
| 516 | << "send_result = " << send_result; |
| 517 | Close(); |
| 518 | |
| 519 | return false; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 520 | } |
| 521 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 522 | bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) { |
| 523 | if (queued_send_data_.byte_count() >= kMaxQueuedSendDataBytes) { |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 524 | 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] | 525 | return false; |
| 526 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 527 | queued_send_data_.Push(new DataBuffer(buffer)); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 528 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 529 | } |
| 530 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 531 | void DataChannel::SendQueuedControlMessages() { |
| 532 | ASSERT(was_ever_writable_); |
| 533 | |
| 534 | PacketQueue control_packets; |
| 535 | control_packets.Swap(&queued_control_data_); |
| 536 | |
| 537 | while (!control_packets.Empty()) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 538 | rtc::scoped_ptr<DataBuffer> buf(control_packets.Front()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 539 | SendControlMessage(buf->data); |
| 540 | control_packets.Pop(); |
| 541 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 542 | } |
| 543 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 544 | void DataChannel::QueueControlMessage(const rtc::Buffer& buffer) { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 545 | queued_control_data_.Push(new DataBuffer(buffer, true)); |
| 546 | } |
| 547 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 548 | bool DataChannel::SendControlMessage(const rtc::Buffer& buffer) { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 549 | bool is_open_message = |
| 550 | (config_.open_handshake_role == InternalDataChannelInit::kOpener); |
| 551 | |
| 552 | ASSERT(data_channel_type_ == cricket::DCT_SCTP && |
| 553 | was_ever_writable_ && |
| 554 | config_.id >= 0 && |
| 555 | (!is_open_message || !config_.negotiated)); |
| 556 | |
| 557 | cricket::SendDataParams send_params; |
| 558 | send_params.ssrc = config_.id; |
| 559 | send_params.ordered = config_.ordered || is_open_message; |
| 560 | send_params.type = cricket::DMT_CONTROL; |
| 561 | |
| 562 | cricket::SendDataResult send_result = cricket::SDR_SUCCESS; |
| 563 | bool retval = provider_->SendData(send_params, buffer, &send_result); |
| 564 | if (retval) { |
| 565 | LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id; |
| 566 | |
| 567 | if (is_open_message) { |
| 568 | // Send data as ordered before we receive any message from the remote peer |
| 569 | // to make sure the remote peer will not receive any data before it |
| 570 | // receives the OPEN message. |
| 571 | waiting_for_open_ack_ = true; |
| 572 | } |
| 573 | } else if (send_result == cricket::SDR_BLOCK) { |
| 574 | QueueControlMessage(buffer); |
| 575 | } else { |
| 576 | LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send" |
| 577 | << " the CONTROL message, send_result = " << send_result; |
| 578 | Close(); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 579 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 580 | return retval; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 581 | } |
| 582 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 583 | } // namespace webrtc |