| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/api/datachannel.h" | 11 #include "webrtc/api/datachannel.h" |
| 12 | 12 |
| 13 #include <memory> | 13 #include <memory> |
| 14 #include <string> | 14 #include <string> |
| 15 | 15 |
| 16 #include "webrtc/api/sctputils.h" | 16 #include "webrtc/api/sctputils.h" |
| 17 #include "webrtc/base/logging.h" | 17 #include "webrtc/base/logging.h" |
| 18 #include "webrtc/base/refcount.h" | 18 #include "webrtc/base/refcount.h" |
| 19 #include "webrtc/media/sctp/sctptransportinternal.h" | 19 #include "webrtc/media/sctp/sctpdataengine.h" |
| 20 | 20 |
| 21 namespace webrtc { | 21 namespace webrtc { |
| 22 | 22 |
| 23 static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024; | 23 static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024; |
| 24 static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024; | 24 static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024; |
| 25 | 25 |
| 26 enum { | 26 enum { |
| 27 MSG_CHANNELREADY, | 27 MSG_CHANNELREADY, |
| 28 }; | 28 }; |
| 29 | 29 |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 } | 321 } |
| 322 | 322 |
| 323 void DataChannel::OnMessage(rtc::Message* msg) { | 323 void DataChannel::OnMessage(rtc::Message* msg) { |
| 324 switch (msg->message_id) { | 324 switch (msg->message_id) { |
| 325 case MSG_CHANNELREADY: | 325 case MSG_CHANNELREADY: |
| 326 OnChannelReady(true); | 326 OnChannelReady(true); |
| 327 break; | 327 break; |
| 328 } | 328 } |
| 329 } | 329 } |
| 330 | 330 |
| 331 void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params, | 331 void DataChannel::OnDataReceived(cricket::DataChannel* channel, |
| 332 const cricket::ReceiveDataParams& params, |
| 332 const rtc::CopyOnWriteBuffer& payload) { | 333 const rtc::CopyOnWriteBuffer& payload) { |
| 333 if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) { | 334 uint32_t expected_ssrc = |
| 334 return; | 335 (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id; |
| 335 } | 336 if (params.ssrc != expected_ssrc) { |
| 336 if (data_channel_type_ == cricket::DCT_SCTP && params.sid != config_.id) { | |
| 337 return; | 337 return; |
| 338 } | 338 } |
| 339 | 339 |
| 340 if (params.type == cricket::DMT_CONTROL) { | 340 if (params.type == cricket::DMT_CONTROL) { |
| 341 ASSERT(data_channel_type_ == cricket::DCT_SCTP); | 341 ASSERT(data_channel_type_ == cricket::DCT_SCTP); |
| 342 if (handshake_state_ != kHandshakeWaitingForAck) { | 342 if (handshake_state_ != kHandshakeWaitingForAck) { |
| 343 // Ignore it if we are not expecting an ACK message. | 343 // Ignore it if we are not expecting an ACK message. |
| 344 LOG(LS_WARNING) << "DataChannel received unexpected CONTROL message, " | 344 LOG(LS_WARNING) << "DataChannel received unexpected CONTROL message, " |
| 345 << "sid = " << params.sid; | 345 << "sid = " << params.ssrc; |
| 346 return; | 346 return; |
| 347 } | 347 } |
| 348 if (ParseDataChannelOpenAckMessage(payload)) { | 348 if (ParseDataChannelOpenAckMessage(payload)) { |
| 349 // We can send unordered as soon as we receive the ACK message. | 349 // We can send unordered as soon as we receive the ACK message. |
| 350 handshake_state_ = kHandshakeReady; | 350 handshake_state_ = kHandshakeReady; |
| 351 LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = " | 351 LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = " |
| 352 << params.sid; | 352 << params.ssrc; |
| 353 } else { | 353 } else { |
| 354 LOG(LS_WARNING) << "DataChannel failed to parse OPEN_ACK message, sid = " | 354 LOG(LS_WARNING) << "DataChannel failed to parse OPEN_ACK message, sid = " |
| 355 << params.sid; | 355 << params.ssrc; |
| 356 } | 356 } |
| 357 return; | 357 return; |
| 358 } | 358 } |
| 359 | 359 |
| 360 ASSERT(params.type == cricket::DMT_BINARY || | 360 ASSERT(params.type == cricket::DMT_BINARY || |
| 361 params.type == cricket::DMT_TEXT); | 361 params.type == cricket::DMT_TEXT); |
| 362 | 362 |
| 363 LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " << params.sid; | 363 LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " << params.ssrc; |
| 364 // We can send unordered as soon as we receive any DATA message since the | 364 // We can send unordered as soon as we receive any DATA message since the |
| 365 // remote side must have received the OPEN (and old clients do not send | 365 // remote side must have received the OPEN (and old clients do not send |
| 366 // OPEN_ACK). | 366 // OPEN_ACK). |
| 367 if (handshake_state_ == kHandshakeWaitingForAck) { | 367 if (handshake_state_ == kHandshakeWaitingForAck) { |
| 368 handshake_state_ = kHandshakeReady; | 368 handshake_state_ = kHandshakeReady; |
| 369 } | 369 } |
| 370 | 370 |
| 371 bool binary = (params.type == cricket::DMT_BINARY); | 371 bool binary = (params.type == cricket::DMT_BINARY); |
| 372 std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary)); | 372 std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary)); |
| 373 if (state_ == kOpen && observer_) { | 373 if (state_ == kOpen && observer_) { |
| 374 ++messages_received_; | 374 ++messages_received_; |
| 375 bytes_received_ += buffer->size(); | 375 bytes_received_ += buffer->size(); |
| 376 observer_->OnMessage(*buffer.get()); | 376 observer_->OnMessage(*buffer.get()); |
| 377 } else { | 377 } else { |
| 378 if (queued_received_data_.byte_count() + payload.size() > | 378 if (queued_received_data_.byte_count() + payload.size() > |
| 379 kMaxQueuedReceivedDataBytes) { | 379 kMaxQueuedReceivedDataBytes) { |
| 380 LOG(LS_ERROR) << "Queued received data exceeds the max buffer size."; | 380 LOG(LS_ERROR) << "Queued received data exceeds the max buffer size."; |
| 381 | 381 |
| 382 queued_received_data_.Clear(); | 382 queued_received_data_.Clear(); |
| 383 if (data_channel_type_ != cricket::DCT_RTP) { | 383 if (data_channel_type_ != cricket::DCT_RTP) { |
| 384 Close(); | 384 Close(); |
| 385 } | 385 } |
| 386 | 386 |
| 387 return; | 387 return; |
| 388 } | 388 } |
| 389 queued_received_data_.Push(buffer.release()); | 389 queued_received_data_.Push(buffer.release()); |
| 390 } | 390 } |
| 391 } | 391 } |
| 392 | 392 |
| 393 void DataChannel::OnStreamClosedRemotely(int sid) { | 393 void DataChannel::OnStreamClosedRemotely(uint32_t sid) { |
| 394 if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id) { | 394 if (data_channel_type_ == cricket::DCT_SCTP && |
| 395 sid == static_cast<uint32_t>(config_.id)) { |
| 395 Close(); | 396 Close(); |
| 396 } | 397 } |
| 397 } | 398 } |
| 398 | 399 |
| 399 void DataChannel::OnChannelReady(bool writable) { | 400 void DataChannel::OnChannelReady(bool writable) { |
| 400 writable_ = writable; | 401 writable_ = writable; |
| 401 if (!writable) { | 402 if (!writable) { |
| 402 return; | 403 return; |
| 403 } | 404 } |
| 404 | 405 |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 543 send_params.ordered = config_.ordered; | 544 send_params.ordered = config_.ordered; |
| 544 // Send as ordered if it is still going through OPEN/ACK signaling. | 545 // Send as ordered if it is still going through OPEN/ACK signaling. |
| 545 if (handshake_state_ != kHandshakeReady && !config_.ordered) { | 546 if (handshake_state_ != kHandshakeReady && !config_.ordered) { |
| 546 send_params.ordered = true; | 547 send_params.ordered = true; |
| 547 LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel " | 548 LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel " |
| 548 << "because the OPEN_ACK message has not been received."; | 549 << "because the OPEN_ACK message has not been received."; |
| 549 } | 550 } |
| 550 | 551 |
| 551 send_params.max_rtx_count = config_.maxRetransmits; | 552 send_params.max_rtx_count = config_.maxRetransmits; |
| 552 send_params.max_rtx_ms = config_.maxRetransmitTime; | 553 send_params.max_rtx_ms = config_.maxRetransmitTime; |
| 553 send_params.sid = config_.id; | 554 send_params.ssrc = config_.id; |
| 554 } else { | 555 } else { |
| 555 send_params.ssrc = send_ssrc_; | 556 send_params.ssrc = send_ssrc_; |
| 556 } | 557 } |
| 557 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT; | 558 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT; |
| 558 | 559 |
| 559 cricket::SendDataResult send_result = cricket::SDR_SUCCESS; | 560 cricket::SendDataResult send_result = cricket::SDR_SUCCESS; |
| 560 bool success = provider_->SendData(send_params, buffer.data, &send_result); | 561 bool success = provider_->SendData(send_params, buffer.data, &send_result); |
| 561 | 562 |
| 562 if (success) { | 563 if (success) { |
| 563 ++messages_sent_; | 564 ++messages_sent_; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 615 | 616 |
| 616 bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) { | 617 bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) { |
| 617 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen; | 618 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen; |
| 618 | 619 |
| 619 ASSERT(data_channel_type_ == cricket::DCT_SCTP && | 620 ASSERT(data_channel_type_ == cricket::DCT_SCTP && |
| 620 writable_ && | 621 writable_ && |
| 621 config_.id >= 0 && | 622 config_.id >= 0 && |
| 622 (!is_open_message || !config_.negotiated)); | 623 (!is_open_message || !config_.negotiated)); |
| 623 | 624 |
| 624 cricket::SendDataParams send_params; | 625 cricket::SendDataParams send_params; |
| 625 send_params.sid = config_.id; | 626 send_params.ssrc = config_.id; |
| 626 // Send data as ordered before we receive any message from the remote peer to | 627 // Send data as ordered before we receive any message from the remote peer to |
| 627 // make sure the remote peer will not receive any data before it receives the | 628 // make sure the remote peer will not receive any data before it receives the |
| 628 // OPEN message. | 629 // OPEN message. |
| 629 send_params.ordered = config_.ordered || is_open_message; | 630 send_params.ordered = config_.ordered || is_open_message; |
| 630 send_params.type = cricket::DMT_CONTROL; | 631 send_params.type = cricket::DMT_CONTROL; |
| 631 | 632 |
| 632 cricket::SendDataResult send_result = cricket::SDR_SUCCESS; | 633 cricket::SendDataResult send_result = cricket::SDR_SUCCESS; |
| 633 bool retval = provider_->SendData(send_params, buffer, &send_result); | 634 bool retval = provider_->SendData(send_params, buffer, &send_result); |
| 634 if (retval) { | 635 if (retval) { |
| 635 LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id; | 636 LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id; |
| 636 | 637 |
| 637 if (handshake_state_ == kHandshakeShouldSendAck) { | 638 if (handshake_state_ == kHandshakeShouldSendAck) { |
| 638 handshake_state_ = kHandshakeReady; | 639 handshake_state_ = kHandshakeReady; |
| 639 } else if (handshake_state_ == kHandshakeShouldSendOpen) { | 640 } else if (handshake_state_ == kHandshakeShouldSendOpen) { |
| 640 handshake_state_ = kHandshakeWaitingForAck; | 641 handshake_state_ = kHandshakeWaitingForAck; |
| 641 } | 642 } |
| 642 } else if (send_result == cricket::SDR_BLOCK) { | 643 } else if (send_result == cricket::SDR_BLOCK) { |
| 643 QueueControlMessage(buffer); | 644 QueueControlMessage(buffer); |
| 644 } else { | 645 } else { |
| 645 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send" | 646 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send" |
| 646 << " the CONTROL message, send_result = " << send_result; | 647 << " the CONTROL message, send_result = " << send_result; |
| 647 Close(); | 648 Close(); |
| 648 } | 649 } |
| 649 return retval; | 650 return retval; |
| 650 } | 651 } |
| 651 | 652 |
| 652 } // namespace webrtc | 653 } // namespace webrtc |
| OLD | NEW |