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