fake_data_channel_provider.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  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
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef PC_TEST_FAKE_DATA_CHANNEL_PROVIDER_H_
  11. #define PC_TEST_FAKE_DATA_CHANNEL_PROVIDER_H_
  12. #include <set>
  13. #include "pc/sctp_data_channel.h"
  14. #include "rtc_base/checks.h"
  15. class FakeDataChannelProvider
  16. : public webrtc::SctpDataChannelProviderInterface {
  17. public:
  18. FakeDataChannelProvider()
  19. : send_blocked_(false),
  20. transport_available_(false),
  21. ready_to_send_(false),
  22. transport_error_(false) {}
  23. virtual ~FakeDataChannelProvider() {}
  24. bool SendData(const cricket::SendDataParams& params,
  25. const rtc::CopyOnWriteBuffer& payload,
  26. cricket::SendDataResult* result) override {
  27. RTC_CHECK(ready_to_send_);
  28. RTC_CHECK(transport_available_);
  29. if (send_blocked_) {
  30. *result = cricket::SDR_BLOCK;
  31. return false;
  32. }
  33. if (transport_error_ || payload.size() == 0) {
  34. *result = cricket::SDR_ERROR;
  35. return false;
  36. }
  37. last_send_data_params_ = params;
  38. return true;
  39. }
  40. bool ConnectDataChannel(webrtc::SctpDataChannel* data_channel) override {
  41. RTC_CHECK(connected_channels_.find(data_channel) ==
  42. connected_channels_.end());
  43. if (!transport_available_) {
  44. return false;
  45. }
  46. RTC_LOG(LS_INFO) << "DataChannel connected " << data_channel;
  47. connected_channels_.insert(data_channel);
  48. return true;
  49. }
  50. void DisconnectDataChannel(webrtc::SctpDataChannel* data_channel) override {
  51. RTC_CHECK(connected_channels_.find(data_channel) !=
  52. connected_channels_.end());
  53. RTC_LOG(LS_INFO) << "DataChannel disconnected " << data_channel;
  54. connected_channels_.erase(data_channel);
  55. }
  56. void AddSctpDataStream(int sid) override {
  57. RTC_CHECK(sid >= 0);
  58. if (!transport_available_) {
  59. return;
  60. }
  61. send_ssrcs_.insert(sid);
  62. recv_ssrcs_.insert(sid);
  63. }
  64. void RemoveSctpDataStream(int sid) override {
  65. RTC_CHECK(sid >= 0);
  66. send_ssrcs_.erase(sid);
  67. recv_ssrcs_.erase(sid);
  68. // Unlike the real SCTP transport, act like the closing procedure finished
  69. // instantly, doing the same snapshot thing as below.
  70. for (webrtc::SctpDataChannel* ch : std::set<webrtc::SctpDataChannel*>(
  71. connected_channels_.begin(), connected_channels_.end())) {
  72. if (connected_channels_.count(ch)) {
  73. ch->OnClosingProcedureComplete(sid);
  74. }
  75. }
  76. }
  77. bool ReadyToSendData() const override { return ready_to_send_; }
  78. // Set true to emulate the SCTP stream being blocked by congestion control.
  79. void set_send_blocked(bool blocked) {
  80. send_blocked_ = blocked;
  81. if (!blocked) {
  82. // Take a snapshot of the connected channels and check to see whether
  83. // each value is still in connected_channels_ before calling
  84. // OnTransportReady(). This avoids problems where the set gets modified
  85. // in response to OnTransportReady().
  86. for (webrtc::SctpDataChannel* ch : std::set<webrtc::SctpDataChannel*>(
  87. connected_channels_.begin(), connected_channels_.end())) {
  88. if (connected_channels_.count(ch)) {
  89. ch->OnTransportReady(true);
  90. }
  91. }
  92. }
  93. }
  94. // Set true to emulate the transport channel creation, e.g. after
  95. // setLocalDescription/setRemoteDescription called with data content.
  96. void set_transport_available(bool available) {
  97. transport_available_ = available;
  98. }
  99. // Set true to emulate the transport ReadyToSendData signal when the transport
  100. // becomes writable for the first time.
  101. void set_ready_to_send(bool ready) {
  102. RTC_CHECK(transport_available_);
  103. ready_to_send_ = ready;
  104. if (ready) {
  105. std::set<webrtc::SctpDataChannel*>::iterator it;
  106. for (it = connected_channels_.begin(); it != connected_channels_.end();
  107. ++it) {
  108. (*it)->OnTransportReady(true);
  109. }
  110. }
  111. }
  112. void set_transport_error() { transport_error_ = true; }
  113. cricket::SendDataParams last_send_data_params() const {
  114. return last_send_data_params_;
  115. }
  116. bool IsConnected(webrtc::SctpDataChannel* data_channel) const {
  117. return connected_channels_.find(data_channel) != connected_channels_.end();
  118. }
  119. bool IsSendStreamAdded(uint32_t stream) const {
  120. return send_ssrcs_.find(stream) != send_ssrcs_.end();
  121. }
  122. bool IsRecvStreamAdded(uint32_t stream) const {
  123. return recv_ssrcs_.find(stream) != recv_ssrcs_.end();
  124. }
  125. private:
  126. cricket::SendDataParams last_send_data_params_;
  127. bool send_blocked_;
  128. bool transport_available_;
  129. bool ready_to_send_;
  130. bool transport_error_;
  131. std::set<webrtc::SctpDataChannel*> connected_channels_;
  132. std::set<uint32_t> send_ssrcs_;
  133. std::set<uint32_t> recv_ssrcs_;
  134. };
  135. #endif // PC_TEST_FAKE_DATA_CHANNEL_PROVIDER_H_