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