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" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | #include "talk/base/logging.h" |
| 33 | #include "talk/base/refcount.h" |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 34 | #include "talk/media/sctp/sctputils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 35 | |
| 36 | namespace webrtc { |
| 37 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 38 | static size_t kMaxQueuedReceivedDataPackets = 100; |
| 39 | static size_t kMaxQueuedSendDataPackets = 100; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 40 | |
| 41 | talk_base::scoped_refptr<DataChannel> DataChannel::Create( |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 42 | DataChannelProviderInterface* provider, |
| 43 | cricket::DataChannelType dct, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | const std::string& label, |
| 45 | const DataChannelInit* config) { |
| 46 | talk_base::scoped_refptr<DataChannel> channel( |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 47 | new talk_base::RefCountedObject<DataChannel>(provider, dct, label)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 48 | if (!channel->Init(config)) { |
| 49 | return NULL; |
| 50 | } |
| 51 | return channel; |
| 52 | } |
| 53 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 54 | DataChannel::DataChannel( |
| 55 | DataChannelProviderInterface* provider, |
| 56 | cricket::DataChannelType dct, |
| 57 | const std::string& label) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | : label_(label), |
| 59 | observer_(NULL), |
| 60 | state_(kConnecting), |
| 61 | was_ever_writable_(false), |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 62 | connected_to_provider_(false), |
| 63 | data_channel_type_(dct), |
| 64 | provider_(provider), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | send_ssrc_set_(false), |
| 66 | send_ssrc_(0), |
| 67 | receive_ssrc_set_(false), |
| 68 | receive_ssrc_(0) { |
| 69 | } |
| 70 | |
| 71 | bool DataChannel::Init(const DataChannelInit* config) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 72 | if (data_channel_type_ == cricket::DCT_RTP && |
| 73 | (config->reliable || |
| 74 | config->id != -1 || |
| 75 | config->maxRetransmits != -1 || |
| 76 | config->maxRetransmitTime != -1)) { |
| 77 | LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to " |
| 78 | << "invalid DataChannelInit."; |
| 79 | return false; |
| 80 | } else if (data_channel_type_ == cricket::DCT_SCTP) { |
| 81 | if (config->id < -1 || |
| 82 | config->maxRetransmits < -1 || |
| 83 | config->maxRetransmitTime < -1) { |
| 84 | 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] | 85 | << "invalid DataChannelInit."; |
| 86 | return false; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 87 | } |
| 88 | if (config->maxRetransmits != -1 && config->maxRetransmitTime != -1) { |
| 89 | LOG(LS_ERROR) << |
| 90 | "maxRetransmits and maxRetransmitTime should not be both set."; |
| 91 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | } |
| 93 | config_ = *config; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 95 | // Try to connect to the transport in case the transport channel already |
| 96 | // exists. |
| 97 | OnTransportChannelCreated(); |
| 98 | } |
| 99 | |
| 100 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | DataChannel::~DataChannel() { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 104 | ClearQueuedReceivedData(); |
| 105 | ClearQueuedSendData(); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 106 | ClearQueuedControlData(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void DataChannel::RegisterObserver(DataChannelObserver* observer) { |
| 110 | observer_ = observer; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 111 | DeliverQueuedReceivedData(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void DataChannel::UnregisterObserver() { |
| 115 | observer_ = NULL; |
| 116 | } |
| 117 | |
| 118 | bool DataChannel::reliable() const { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 119 | if (data_channel_type_ == cricket::DCT_RTP) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 120 | return false; |
| 121 | } else { |
| 122 | return config_.maxRetransmits == -1 && |
| 123 | config_.maxRetransmitTime == -1; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | uint64 DataChannel::buffered_amount() const { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 128 | uint64 buffered_amount = 0; |
| 129 | for (std::deque<DataBuffer*>::const_iterator it = queued_send_data_.begin(); |
| 130 | it != queued_send_data_.end(); |
| 131 | ++it) { |
| 132 | buffered_amount += (*it)->size(); |
| 133 | } |
| 134 | return buffered_amount; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void DataChannel::Close() { |
| 138 | if (state_ == kClosed) |
| 139 | return; |
| 140 | send_ssrc_ = 0; |
| 141 | send_ssrc_set_ = false; |
| 142 | SetState(kClosing); |
| 143 | UpdateState(); |
| 144 | } |
| 145 | |
| 146 | bool DataChannel::Send(const DataBuffer& buffer) { |
| 147 | if (state_ != kOpen) { |
| 148 | return false; |
| 149 | } |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 150 | // If the queue is non-empty, we're waiting for SignalReadyToSend, |
| 151 | // so just add to the end of the queue and keep waiting. |
| 152 | if (!queued_send_data_.empty()) { |
| 153 | return QueueSendData(buffer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 154 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | |
| 156 | cricket::SendDataResult send_result; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 157 | if (!InternalSendWithoutQueueing(buffer, &send_result)) { |
| 158 | if (send_result == cricket::SDR_BLOCK) { |
| 159 | return QueueSendData(buffer); |
| 160 | } |
| 161 | // Fail for other results. |
| 162 | // TODO(jiayl): We should close the data channel in this case. |
| 163 | return false; |
| 164 | } |
| 165 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 166 | } |
| 167 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 168 | void DataChannel::QueueControl(const talk_base::Buffer* buffer) { |
| 169 | queued_control_data_.push(buffer); |
| 170 | } |
| 171 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 172 | bool DataChannel::SendOpenMessage(const talk_base::Buffer* raw_buffer) { |
| 173 | ASSERT(data_channel_type_ == cricket::DCT_SCTP && |
| 174 | was_ever_writable_ && |
| 175 | config_.id >= 0 && |
| 176 | !config_.negotiated); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 177 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 178 | talk_base::scoped_ptr<const talk_base::Buffer> buffer(raw_buffer); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 179 | |
| 180 | cricket::SendDataParams send_params; |
| 181 | send_params.ssrc = config_.id; |
| 182 | send_params.ordered = true; |
| 183 | send_params.type = cricket::DMT_CONTROL; |
| 184 | |
| 185 | cricket::SendDataResult send_result; |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 186 | bool retval = provider_->SendData(send_params, *buffer, &send_result); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 187 | if (!retval && send_result == cricket::SDR_BLOCK) { |
| 188 | // Link is congested. Queue for later. |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 189 | QueueControl(buffer.release()); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 190 | } |
| 191 | return retval; |
| 192 | } |
| 193 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 194 | void DataChannel::SetReceiveSsrc(uint32 receive_ssrc) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 195 | ASSERT(data_channel_type_ == cricket::DCT_RTP); |
| 196 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 197 | if (receive_ssrc_set_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | receive_ssrc_ = receive_ssrc; |
| 201 | receive_ssrc_set_ = true; |
| 202 | UpdateState(); |
| 203 | } |
| 204 | |
| 205 | // The remote peer request that this channel shall be closed. |
| 206 | void DataChannel::RemotePeerRequestClose() { |
| 207 | DoClose(); |
| 208 | } |
| 209 | |
| 210 | void DataChannel::SetSendSsrc(uint32 send_ssrc) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 211 | ASSERT(data_channel_type_ == cricket::DCT_RTP); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 212 | if (send_ssrc_set_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 213 | return; |
| 214 | } |
| 215 | send_ssrc_ = send_ssrc; |
| 216 | send_ssrc_set_ = true; |
| 217 | UpdateState(); |
| 218 | } |
| 219 | |
| 220 | // The underlaying data engine is closing. |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 221 | // 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] | 222 | // kClosed. |
| 223 | void DataChannel::OnDataEngineClose() { |
| 224 | DoClose(); |
| 225 | } |
| 226 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 227 | void DataChannel::OnDataReceived(cricket::DataChannel* channel, |
| 228 | const cricket::ReceiveDataParams& params, |
| 229 | const talk_base::Buffer& payload) { |
| 230 | if (params.ssrc != receive_ssrc_) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | bool binary = (params.type == cricket::DMT_BINARY); |
| 235 | talk_base::scoped_ptr<DataBuffer> buffer(new DataBuffer(payload, binary)); |
| 236 | if (was_ever_writable_ && observer_) { |
| 237 | observer_->OnMessage(*buffer.get()); |
| 238 | } else { |
| 239 | if (queued_received_data_.size() > kMaxQueuedReceivedDataPackets) { |
| 240 | // TODO(jiayl): We should close the data channel in this case. |
| 241 | LOG(LS_ERROR) |
| 242 | << "Queued received data exceeds the max number of packes."; |
| 243 | ClearQueuedReceivedData(); |
| 244 | } |
| 245 | queued_received_data_.push(buffer.release()); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void DataChannel::OnChannelReady(bool writable) { |
| 250 | if (!writable) { |
| 251 | return; |
| 252 | } |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 253 | // Update the readyState and send the queued control message if the channel |
| 254 | // is writable for the first time; otherwise it means the channel was blocked |
| 255 | // for sending and now unblocked, so send the queued data now. |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 256 | if (!was_ever_writable_) { |
| 257 | was_ever_writable_ = true; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 258 | |
| 259 | if (data_channel_type_ == cricket::DCT_SCTP && !config_.negotiated) { |
| 260 | talk_base::Buffer* payload = new talk_base::Buffer; |
| 261 | if (!cricket::WriteDataChannelOpenMessage(label_, config_, payload)) { |
| 262 | // TODO(jiayl): close the data channel on this error. |
| 263 | LOG(LS_ERROR) << "Could not write data channel OPEN message"; |
| 264 | return; |
| 265 | } |
| 266 | SendOpenMessage(payload); |
| 267 | } |
| 268 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 269 | UpdateState(); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 270 | ASSERT(queued_send_data_.empty()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 271 | } else if (state_ == kOpen) { |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 272 | DeliverQueuedSendData(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 276 | void DataChannel::DoClose() { |
| 277 | receive_ssrc_set_ = false; |
| 278 | send_ssrc_set_ = false; |
| 279 | SetState(kClosing); |
| 280 | UpdateState(); |
| 281 | } |
| 282 | |
| 283 | void DataChannel::UpdateState() { |
| 284 | switch (state_) { |
| 285 | case kConnecting: { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 286 | if (send_ssrc_set_ == receive_ssrc_set_) { |
| 287 | if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) { |
| 288 | connected_to_provider_ = provider_->ConnectDataChannel(this); |
| 289 | provider_->AddRtpDataStream(send_ssrc_, receive_ssrc_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 290 | } |
| 291 | if (was_ever_writable_) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 292 | // TODO(jiayl): Do not transition to kOpen if we failed to send the |
| 293 | // OPEN message. |
| 294 | DeliverQueuedControlData(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 295 | SetState(kOpen); |
| 296 | // If we have received buffers before the channel got writable. |
| 297 | // Deliver them now. |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 298 | DeliverQueuedReceivedData(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | break; |
| 302 | } |
| 303 | case kOpen: { |
| 304 | break; |
| 305 | } |
| 306 | case kClosing: { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 307 | DisconnectFromTransport(); |
| 308 | |
| 309 | if (!send_ssrc_set_ && !receive_ssrc_set_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 310 | SetState(kClosed); |
| 311 | } |
| 312 | break; |
| 313 | } |
| 314 | case kClosed: |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void DataChannel::SetState(DataState state) { |
| 320 | state_ = state; |
| 321 | if (observer_) { |
| 322 | observer_->OnStateChange(); |
| 323 | } |
| 324 | } |
| 325 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 326 | void DataChannel::DisconnectFromTransport() { |
| 327 | if (!connected_to_provider_) |
| 328 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 329 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 330 | provider_->DisconnectDataChannel(this); |
| 331 | connected_to_provider_ = false; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 332 | |
| 333 | if (data_channel_type_ == cricket::DCT_RTP) { |
| 334 | provider_->RemoveRtpDataStream(send_ssrc_, receive_ssrc_); |
| 335 | } else { |
| 336 | provider_->RemoveSctpDataStream(config_.id); |
| 337 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 338 | } |
| 339 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 340 | void DataChannel::DeliverQueuedReceivedData() { |
| 341 | if (!was_ever_writable_ || !observer_) { |
| 342 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 343 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 344 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 345 | while (!queued_received_data_.empty()) { |
| 346 | DataBuffer* buffer = queued_received_data_.front(); |
| 347 | observer_->OnMessage(*buffer); |
| 348 | queued_received_data_.pop(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 349 | delete buffer; |
| 350 | } |
| 351 | } |
| 352 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 353 | void DataChannel::ClearQueuedReceivedData() { |
| 354 | while (!queued_received_data_.empty()) { |
| 355 | DataBuffer* buffer = queued_received_data_.front(); |
| 356 | queued_received_data_.pop(); |
| 357 | delete buffer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 361 | void DataChannel::DeliverQueuedSendData() { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 362 | ASSERT(was_ever_writable_ && state_ == kOpen); |
| 363 | |
| 364 | // TODO(jiayl): Sending OPEN message here contradicts with the pre-condition |
| 365 | // that the readyState is open. According to the standard, the channel should |
| 366 | // not become open before the OPEN message is sent. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 367 | DeliverQueuedControlData(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 368 | |
| 369 | while (!queued_send_data_.empty()) { |
| 370 | DataBuffer* buffer = queued_send_data_.front(); |
| 371 | cricket::SendDataResult send_result; |
| 372 | if (!InternalSendWithoutQueueing(*buffer, &send_result)) { |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 373 | LOG(LS_WARNING) << "DeliverQueuedSendData aborted due to send_result " |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 374 | << send_result; |
| 375 | break; |
| 376 | } |
| 377 | queued_send_data_.pop_front(); |
| 378 | delete buffer; |
| 379 | } |
| 380 | } |
| 381 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 382 | void DataChannel::ClearQueuedControlData() { |
| 383 | while (!queued_control_data_.empty()) { |
| 384 | const talk_base::Buffer *buf = queued_control_data_.front(); |
| 385 | queued_control_data_.pop(); |
| 386 | delete buf; |
| 387 | } |
| 388 | } |
| 389 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 390 | void DataChannel::DeliverQueuedControlData() { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 391 | ASSERT(was_ever_writable_); |
| 392 | while (!queued_control_data_.empty()) { |
| 393 | const talk_base::Buffer* buf = queued_control_data_.front(); |
| 394 | queued_control_data_.pop(); |
| 395 | SendOpenMessage(buf); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 399 | void DataChannel::ClearQueuedSendData() { |
wu@webrtc.org | a054569 | 2013-08-01 22:08:14 +0000 | [diff] [blame] | 400 | while (!queued_send_data_.empty()) { |
| 401 | DataBuffer* buffer = queued_send_data_.front(); |
| 402 | queued_send_data_.pop_front(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 403 | delete buffer; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | bool DataChannel::InternalSendWithoutQueueing( |
| 408 | const DataBuffer& buffer, cricket::SendDataResult* send_result) { |
| 409 | cricket::SendDataParams send_params; |
| 410 | |
| 411 | send_params.ssrc = send_ssrc_; |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 412 | if (data_channel_type_ == cricket::DCT_SCTP) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 413 | send_params.ordered = config_.ordered; |
| 414 | send_params.max_rtx_count = config_.maxRetransmits; |
| 415 | send_params.max_rtx_ms = config_.maxRetransmitTime; |
| 416 | } |
| 417 | send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT; |
| 418 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 419 | return provider_->SendData(send_params, buffer.data, send_result); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | bool DataChannel::QueueSendData(const DataBuffer& buffer) { |
| 423 | if (queued_send_data_.size() > kMaxQueuedSendDataPackets) { |
| 424 | LOG(LS_ERROR) << "Can't buffer any more data in the data channel."; |
| 425 | return false; |
| 426 | } |
| 427 | queued_send_data_.push_back(new DataBuffer(buffer)); |
| 428 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 429 | } |
| 430 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 431 | void DataChannel::SetSctpSid(int sid) { |
| 432 | ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP); |
| 433 | config_.id = sid; |
| 434 | provider_->AddSctpDataStream(sid); |
| 435 | } |
| 436 | |
| 437 | void DataChannel::OnTransportChannelCreated() { |
| 438 | ASSERT(data_channel_type_ == cricket::DCT_SCTP); |
| 439 | if (!connected_to_provider_) { |
| 440 | connected_to_provider_ = provider_->ConnectDataChannel(this); |
| 441 | } |
| 442 | // The sid may have been unassigned when provider_->ConnectDataChannel was |
| 443 | // done. So always add the streams even if connected_to_provider_ is true. |
| 444 | if (config_.id >= 0) { |
| 445 | provider_->AddSctpDataStream(config_.id); |
| 446 | } |
| 447 | } |
| 448 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 449 | } // namespace webrtc |