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/sctpdataengine.h" | 19 #include "webrtc/media/sctp/sctptransportinternal.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(cricket::DataChannel* channel, | 331 void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params, |
332 const cricket::ReceiveDataParams& params, | |
333 const rtc::CopyOnWriteBuffer& payload) { | 332 const rtc::CopyOnWriteBuffer& payload) { |
334 uint32_t expected_ssrc = | 333 if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) { |
335 (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id; | 334 return; |
336 if (params.ssrc != expected_ssrc) { | 335 } |
| 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.ssrc; | 345 << "sid = " << params.sid; |
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.ssrc; | 352 << params.sid; |
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.ssrc; | 355 << params.sid; |
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.ssrc; | 363 LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " << params.sid; |
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(uint32_t sid) { | 393 void DataChannel::OnStreamClosedRemotely(int sid) { |
394 if (data_channel_type_ == cricket::DCT_SCTP && | 394 if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id) { |
395 sid == static_cast<uint32_t>(config_.id)) { | |
396 Close(); | 395 Close(); |
397 } | 396 } |
398 } | 397 } |
399 | 398 |
400 void DataChannel::OnChannelReady(bool writable) { | 399 void DataChannel::OnChannelReady(bool writable) { |
401 writable_ = writable; | 400 writable_ = writable; |
402 if (!writable) { | 401 if (!writable) { |
403 return; | 402 return; |
404 } | 403 } |
405 | 404 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 send_params.ordered = config_.ordered; | 543 send_params.ordered = config_.ordered; |
545 // Send as ordered if it is still going through OPEN/ACK signaling. | 544 // Send as ordered if it is still going through OPEN/ACK signaling. |
546 if (handshake_state_ != kHandshakeReady && !config_.ordered) { | 545 if (handshake_state_ != kHandshakeReady && !config_.ordered) { |
547 send_params.ordered = true; | 546 send_params.ordered = true; |
548 LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel " | 547 LOG(LS_VERBOSE) << "Sending data as ordered for unordered DataChannel " |
549 << "because the OPEN_ACK message has not been received."; | 548 << "because the OPEN_ACK message has not been received."; |
550 } | 549 } |
551 | 550 |
552 send_params.max_rtx_count = config_.maxRetransmits; | 551 send_params.max_rtx_count = config_.maxRetransmits; |
553 send_params.max_rtx_ms = config_.maxRetransmitTime; | 552 send_params.max_rtx_ms = config_.maxRetransmitTime; |
554 send_params.ssrc = config_.id; | 553 send_params.sid = config_.id; |
555 } else { | 554 } else { |
556 send_params.ssrc = send_ssrc_; | 555 send_params.ssrc = send_ssrc_; |
557 } | 556 } |
558 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT; | 557 send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT; |
559 | 558 |
560 cricket::SendDataResult send_result = cricket::SDR_SUCCESS; | 559 cricket::SendDataResult send_result = cricket::SDR_SUCCESS; |
561 bool success = provider_->SendData(send_params, buffer.data, &send_result); | 560 bool success = provider_->SendData(send_params, buffer.data, &send_result); |
562 | 561 |
563 if (success) { | 562 if (success) { |
564 ++messages_sent_; | 563 ++messages_sent_; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 | 615 |
617 bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) { | 616 bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) { |
618 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen; | 617 bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen; |
619 | 618 |
620 ASSERT(data_channel_type_ == cricket::DCT_SCTP && | 619 ASSERT(data_channel_type_ == cricket::DCT_SCTP && |
621 writable_ && | 620 writable_ && |
622 config_.id >= 0 && | 621 config_.id >= 0 && |
623 (!is_open_message || !config_.negotiated)); | 622 (!is_open_message || !config_.negotiated)); |
624 | 623 |
625 cricket::SendDataParams send_params; | 624 cricket::SendDataParams send_params; |
626 send_params.ssrc = config_.id; | 625 send_params.sid = config_.id; |
627 // Send data as ordered before we receive any message from the remote peer to | 626 // Send data as ordered before we receive any message from the remote peer to |
628 // make sure the remote peer will not receive any data before it receives the | 627 // make sure the remote peer will not receive any data before it receives the |
629 // OPEN message. | 628 // OPEN message. |
630 send_params.ordered = config_.ordered || is_open_message; | 629 send_params.ordered = config_.ordered || is_open_message; |
631 send_params.type = cricket::DMT_CONTROL; | 630 send_params.type = cricket::DMT_CONTROL; |
632 | 631 |
633 cricket::SendDataResult send_result = cricket::SDR_SUCCESS; | 632 cricket::SendDataResult send_result = cricket::SDR_SUCCESS; |
634 bool retval = provider_->SendData(send_params, buffer, &send_result); | 633 bool retval = provider_->SendData(send_params, buffer, &send_result); |
635 if (retval) { | 634 if (retval) { |
636 LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id; | 635 LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id; |
637 | 636 |
638 if (handshake_state_ == kHandshakeShouldSendAck) { | 637 if (handshake_state_ == kHandshakeShouldSendAck) { |
639 handshake_state_ = kHandshakeReady; | 638 handshake_state_ = kHandshakeReady; |
640 } else if (handshake_state_ == kHandshakeShouldSendOpen) { | 639 } else if (handshake_state_ == kHandshakeShouldSendOpen) { |
641 handshake_state_ = kHandshakeWaitingForAck; | 640 handshake_state_ = kHandshakeWaitingForAck; |
642 } | 641 } |
643 } else if (send_result == cricket::SDR_BLOCK) { | 642 } else if (send_result == cricket::SDR_BLOCK) { |
644 QueueControlMessage(buffer); | 643 QueueControlMessage(buffer); |
645 } else { | 644 } else { |
646 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send" | 645 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send" |
647 << " the CONTROL message, send_result = " << send_result; | 646 << " the CONTROL message, send_result = " << send_result; |
648 Close(); | 647 Close(); |
649 } | 648 } |
650 return retval; | 649 return retval; |
651 } | 650 } |
652 | 651 |
653 } // namespace webrtc | 652 } // namespace webrtc |
OLD | NEW |