3 * Copyright 2012, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
27 #include "talk/app/webrtc/datachannel.h"
31 #include "talk/app/webrtc/mediastreamprovider.h"
32 #include "talk/app/webrtc/sctputils.h"
33 #include "webrtc/base/logging.h"
34 #include "webrtc/base/refcount.h"
38 static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024;
39 static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024;
45 DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {}
47 DataChannel::PacketQueue::~PacketQueue() {
51 bool DataChannel::PacketQueue::Empty() const {
52 return packets_.empty();
55 DataBuffer* DataChannel::PacketQueue::Front() {
56 return packets_.front();
59 void DataChannel::PacketQueue::Pop() {
60 if (packets_.empty()) {
64 byte_count_ -= packets_.front()->size();
68 void DataChannel::PacketQueue::Push(DataBuffer* packet) {
69 byte_count_ += packet->size();
70 packets_.push_back(packet);
73 void DataChannel::PacketQueue::Clear() {
74 while (!packets_.empty()) {
75 delete packets_.front();
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;
86 other->packets_.swap(packets_);
89 rtc::scoped_refptr<DataChannel> DataChannel::Create(
90 DataChannelProviderInterface* provider,
91 cricket::DataChannelType dct,
92 const std::string& label,
93 const InternalDataChannelInit& config) {
94 rtc::scoped_refptr<DataChannel> channel(
95 new rtc::RefCountedObject<DataChannel>(provider, dct, label));
96 if (!channel->Init(config)) {
102 DataChannel::DataChannel(
103 DataChannelProviderInterface* provider,
104 cricket::DataChannelType dct,
105 const std::string& label)
109 data_channel_type_(dct),
111 waiting_for_open_ack_(false),
112 was_ever_writable_(false),
113 connected_to_provider_(false),
114 send_ssrc_set_(false),
115 receive_ssrc_set_(false),
120 bool DataChannel::Init(const InternalDataChannelInit& config) {
121 if (data_channel_type_ == cricket::DCT_RTP &&
124 config.maxRetransmits != -1 ||
125 config.maxRetransmitTime != -1)) {
126 LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to "
127 << "invalid DataChannelInit.";
129 } else if (data_channel_type_ == cricket::DCT_SCTP) {
130 if (config.id < -1 ||
131 config.maxRetransmits < -1 ||
132 config.maxRetransmitTime < -1) {
133 LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to "
134 << "invalid DataChannelInit.";
137 if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) {
139 "maxRetransmits and maxRetransmitTime should not be both set.";
144 // Try to connect to the transport in case the transport channel already
146 OnTransportChannelCreated();
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
153 if (provider_->ReadyToSendData()) {
154 rtc::Thread::Current()->Post(this, MSG_CHANNELREADY, NULL);
161 DataChannel::~DataChannel() {}
163 void DataChannel::RegisterObserver(DataChannelObserver* observer) {
164 observer_ = observer;
165 DeliverQueuedReceivedData();
168 void DataChannel::UnregisterObserver() {
172 bool DataChannel::reliable() const {
173 if (data_channel_type_ == cricket::DCT_RTP) {
176 return config_.maxRetransmits == -1 &&
177 config_.maxRetransmitTime == -1;
181 uint64 DataChannel::buffered_amount() const {
182 return queued_send_data_.byte_count();
185 void DataChannel::Close() {
186 if (state_ == kClosed)
189 send_ssrc_set_ = false;
194 bool DataChannel::Send(const DataBuffer& buffer) {
195 if (state_ != kOpen) {
199 // TODO(jiayl): the spec is unclear about if the remote side should get the
200 // onmessage event. We need to figure out the expected behavior and change the
202 if (buffer.size() == 0) {
206 // If the queue is non-empty, we're waiting for SignalReadyToSend,
207 // so just add to the end of the queue and keep waiting.
208 if (!queued_send_data_.Empty()) {
209 // Only SCTP DataChannel queues the outgoing data when the transport is
211 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
212 if (!QueueSendDataMessage(buffer)) {
218 bool success = SendDataMessage(buffer);
219 if (data_channel_type_ == cricket::DCT_RTP) {
223 // Always return true for SCTP DataChannel per the spec.
227 void DataChannel::SetReceiveSsrc(uint32 receive_ssrc) {
228 ASSERT(data_channel_type_ == cricket::DCT_RTP);
230 if (receive_ssrc_set_) {
233 receive_ssrc_ = receive_ssrc;
234 receive_ssrc_set_ = true;
238 // The remote peer request that this channel shall be closed.
239 void DataChannel::RemotePeerRequestClose() {
243 void DataChannel::SetSctpSid(int sid) {
244 ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP);
245 if (config_.id == sid)
249 provider_->AddSctpDataStream(sid);
252 void DataChannel::OnTransportChannelCreated() {
253 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
254 if (!connected_to_provider_) {
255 connected_to_provider_ = provider_->ConnectDataChannel(this);
257 // The sid may have been unassigned when provider_->ConnectDataChannel was
258 // done. So always add the streams even if connected_to_provider_ is true.
259 if (config_.id >= 0) {
260 provider_->AddSctpDataStream(config_.id);
264 void DataChannel::SetSendSsrc(uint32 send_ssrc) {
265 ASSERT(data_channel_type_ == cricket::DCT_RTP);
266 if (send_ssrc_set_) {
269 send_ssrc_ = send_ssrc;
270 send_ssrc_set_ = true;
274 void DataChannel::OnMessage(rtc::Message* msg) {
275 switch (msg->message_id) {
276 case MSG_CHANNELREADY:
277 OnChannelReady(true);
282 // The underlaying data engine is closing.
283 // This function makes sure the DataChannel is disconnected and changes state to
285 void DataChannel::OnDataEngineClose() {
289 void DataChannel::OnDataReceived(cricket::DataChannel* channel,
290 const cricket::ReceiveDataParams& params,
291 const rtc::Buffer& payload) {
292 uint32 expected_ssrc =
293 (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id;
294 if (params.ssrc != expected_ssrc) {
298 if (params.type == cricket::DMT_CONTROL) {
299 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
300 if (!waiting_for_open_ack_) {
301 // Ignore it if we are not expecting an ACK message.
302 LOG(LS_WARNING) << "DataChannel received unexpected CONTROL message, "
303 << "sid = " << params.ssrc;
306 if (ParseDataChannelOpenAckMessage(payload)) {
307 // We can send unordered as soon as we receive the ACK message.
308 waiting_for_open_ack_ = false;
309 LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = "
312 LOG(LS_WARNING) << "DataChannel failed to parse OPEN_ACK message, sid = "
318 ASSERT(params.type == cricket::DMT_BINARY ||
319 params.type == cricket::DMT_TEXT);
321 LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " << params.ssrc;
322 // We can send unordered as soon as we receive any DATA message since the
323 // remote side must have received the OPEN (and old clients do not send
325 waiting_for_open_ack_ = false;
327 bool binary = (params.type == cricket::DMT_BINARY);
328 rtc::scoped_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
329 if (was_ever_writable_ && observer_) {
330 observer_->OnMessage(*buffer.get());
332 if (queued_received_data_.byte_count() + payload.length() >
333 kMaxQueuedReceivedDataBytes) {
334 LOG(LS_ERROR) << "Queued received data exceeds the max buffer size.";
336 queued_received_data_.Clear();
337 if (data_channel_type_ != cricket::DCT_RTP) {
343 queued_received_data_.Push(buffer.release());
347 void DataChannel::OnChannelReady(bool writable) {
351 // Update the readyState and send the queued control message if the channel
352 // is writable for the first time; otherwise it means the channel was blocked
353 // for sending and now unblocked, so send the queued data now.
354 if (!was_ever_writable_) {
355 was_ever_writable_ = true;
357 if (data_channel_type_ == cricket::DCT_SCTP) {
360 if (config_.open_handshake_role == InternalDataChannelInit::kOpener) {
361 WriteDataChannelOpenMessage(label_, config_, &payload);
362 SendControlMessage(payload);
363 } else if (config_.open_handshake_role ==
364 InternalDataChannelInit::kAcker) {
365 WriteDataChannelOpenAckMessage(&payload);
366 SendControlMessage(payload);
371 ASSERT(queued_send_data_.Empty());
372 } else if (state_ == kOpen) {
373 // TODO(jiayl): Sending OPEN message here contradicts with the pre-condition
374 // that the readyState is open. According to the standard, the channel
375 // should not become open before the OPEN message is sent.
376 SendQueuedControlMessages();
378 SendQueuedDataMessages();
382 void DataChannel::DoClose() {
383 if (state_ == kClosed)
386 receive_ssrc_set_ = false;
387 send_ssrc_set_ = false;
392 void DataChannel::UpdateState() {
395 if (send_ssrc_set_ == receive_ssrc_set_) {
396 if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) {
397 connected_to_provider_ = provider_->ConnectDataChannel(this);
399 if (was_ever_writable_) {
400 // TODO(jiayl): Do not transition to kOpen if we failed to send the
402 SendQueuedControlMessages();
404 // If we have received buffers before the channel got writable.
406 DeliverQueuedReceivedData();
415 DisconnectFromTransport();
417 if (!send_ssrc_set_ && !receive_ssrc_set_) {
427 void DataChannel::SetState(DataState state) {
433 observer_->OnStateChange();
437 void DataChannel::DisconnectFromTransport() {
438 if (!connected_to_provider_)
441 provider_->DisconnectDataChannel(this);
442 connected_to_provider_ = false;
444 if (data_channel_type_ == cricket::DCT_SCTP) {
445 provider_->RemoveSctpDataStream(config_.id);
449 void DataChannel::DeliverQueuedReceivedData() {
450 if (!was_ever_writable_ || !observer_) {
454 while (!queued_received_data_.Empty()) {
455 rtc::scoped_ptr<DataBuffer> buffer(queued_received_data_.Front());
456 observer_->OnMessage(*buffer);
457 queued_received_data_.Pop();
461 void DataChannel::SendQueuedDataMessages() {
462 ASSERT(was_ever_writable_ && state_ == kOpen);
464 PacketQueue packet_buffer;
465 packet_buffer.Swap(&queued_send_data_);
467 while (!packet_buffer.Empty()) {
468 rtc::scoped_ptr<DataBuffer> buffer(packet_buffer.Front());
469 SendDataMessage(*buffer);
474 bool DataChannel::SendDataMessage(const DataBuffer& buffer) {
475 cricket::SendDataParams send_params;
477 if (data_channel_type_ == cricket::DCT_SCTP) {
478 send_params.ordered = config_.ordered;
479 // Send as ordered if it is waiting for the OPEN_ACK message.
480 if (waiting_for_open_ack_ && !config_.ordered) {
481 send_params.ordered = true;
482 LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel "
483 << "because the OPEN_ACK message has not been received.";
486 send_params.max_rtx_count = config_.maxRetransmits;
487 send_params.max_rtx_ms = config_.maxRetransmitTime;
488 send_params.ssrc = config_.id;
490 send_params.ssrc = send_ssrc_;
492 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT;
494 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
495 bool success = provider_->SendData(send_params, buffer.data, &send_result);
497 if (!success && data_channel_type_ == cricket::DCT_SCTP) {
498 if (send_result != cricket::SDR_BLOCK || !QueueSendDataMessage(buffer)) {
499 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, "
500 << "send_result = " << send_result;
507 bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
508 if (queued_send_data_.byte_count() >= kMaxQueuedSendDataBytes) {
509 LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
512 queued_send_data_.Push(new DataBuffer(buffer));
516 void DataChannel::SendQueuedControlMessages() {
517 ASSERT(was_ever_writable_);
519 PacketQueue control_packets;
520 control_packets.Swap(&queued_control_data_);
522 while (!control_packets.Empty()) {
523 rtc::scoped_ptr<DataBuffer> buf(control_packets.Front());
524 SendControlMessage(buf->data);
525 control_packets.Pop();
529 void DataChannel::QueueControlMessage(const rtc::Buffer& buffer) {
530 queued_control_data_.Push(new DataBuffer(buffer, true));
533 bool DataChannel::SendControlMessage(const rtc::Buffer& buffer) {
534 bool is_open_message =
535 (config_.open_handshake_role == InternalDataChannelInit::kOpener);
537 ASSERT(data_channel_type_ == cricket::DCT_SCTP &&
538 was_ever_writable_ &&
540 (!is_open_message || !config_.negotiated));
542 cricket::SendDataParams send_params;
543 send_params.ssrc = config_.id;
544 send_params.ordered = config_.ordered || is_open_message;
545 send_params.type = cricket::DMT_CONTROL;
547 cricket::SendDataResult send_result = cricket::SDR_SUCCESS;
548 bool retval = provider_->SendData(send_params, buffer, &send_result);
550 LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id;
552 if (is_open_message) {
553 // Send data as ordered before we receive any message from the remote peer
554 // to make sure the remote peer will not receive any data before it
555 // receives the OPEN message.
556 waiting_for_open_ack_ = true;
558 } else if (send_result == cricket::SDR_BLOCK) {
559 QueueControlMessage(buffer);
561 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send"
562 << " the CONTROL message, send_result = " << send_result;
568 } // namespace webrtc