| OLD | NEW |
| (Empty) |
| 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 | |
| 11 #ifndef WEBRTC_API_TEST_FAKEDATACHANNELPROVIDER_H_ | |
| 12 #define WEBRTC_API_TEST_FAKEDATACHANNELPROVIDER_H_ | |
| 13 | |
| 14 #include "webrtc/api/datachannel.h" | |
| 15 | |
| 16 class FakeDataChannelProvider : public webrtc::DataChannelProviderInterface { | |
| 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 | |
| 25 bool SendData(const cricket::SendDataParams& params, | |
| 26 const rtc::CopyOnWriteBuffer& payload, | |
| 27 cricket::SendDataResult* result) override { | |
| 28 ASSERT(ready_to_send_ && transport_available_); | |
| 29 if (send_blocked_) { | |
| 30 *result = cricket::SDR_BLOCK; | |
| 31 return false; | |
| 32 } | |
| 33 | |
| 34 if (transport_error_ || payload.size() == 0) { | |
| 35 *result = cricket::SDR_ERROR; | |
| 36 return false; | |
| 37 } | |
| 38 | |
| 39 last_send_data_params_ = params; | |
| 40 return true; | |
| 41 } | |
| 42 | |
| 43 bool ConnectDataChannel(webrtc::DataChannel* data_channel) override { | |
| 44 ASSERT(connected_channels_.find(data_channel) == connected_channels_.end()); | |
| 45 if (!transport_available_) { | |
| 46 return false; | |
| 47 } | |
| 48 LOG(LS_INFO) << "DataChannel connected " << data_channel; | |
| 49 connected_channels_.insert(data_channel); | |
| 50 return true; | |
| 51 } | |
| 52 | |
| 53 void DisconnectDataChannel(webrtc::DataChannel* data_channel) override { | |
| 54 ASSERT(connected_channels_.find(data_channel) != connected_channels_.end()); | |
| 55 LOG(LS_INFO) << "DataChannel disconnected " << data_channel; | |
| 56 connected_channels_.erase(data_channel); | |
| 57 } | |
| 58 | |
| 59 void AddSctpDataStream(int sid) override { | |
| 60 ASSERT(sid >= 0); | |
| 61 if (!transport_available_) { | |
| 62 return; | |
| 63 } | |
| 64 send_ssrcs_.insert(sid); | |
| 65 recv_ssrcs_.insert(sid); | |
| 66 } | |
| 67 | |
| 68 void RemoveSctpDataStream(int sid) override { | |
| 69 ASSERT(sid >= 0); | |
| 70 send_ssrcs_.erase(sid); | |
| 71 recv_ssrcs_.erase(sid); | |
| 72 } | |
| 73 | |
| 74 bool ReadyToSendData() const override { return ready_to_send_; } | |
| 75 | |
| 76 // Set true to emulate the SCTP stream being blocked by congestion control. | |
| 77 void set_send_blocked(bool blocked) { | |
| 78 send_blocked_ = blocked; | |
| 79 if (!blocked) { | |
| 80 // Take a snapshot of the connected channels and check to see whether | |
| 81 // each value is still in connected_channels_ before calling | |
| 82 // OnChannelReady(). This avoids problems where the set gets modified | |
| 83 // in response to OnChannelReady(). | |
| 84 for (webrtc::DataChannel *ch : std::set<webrtc::DataChannel*>( | |
| 85 connected_channels_.begin(), connected_channels_.end())) { | |
| 86 if (connected_channels_.count(ch)) { | |
| 87 ch->OnChannelReady(true); | |
| 88 } | |
| 89 } | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 // Set true to emulate the transport channel creation, e.g. after | |
| 94 // setLocalDescription/setRemoteDescription called with data content. | |
| 95 void set_transport_available(bool available) { | |
| 96 transport_available_ = available; | |
| 97 } | |
| 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 ASSERT(transport_available_); | |
| 103 ready_to_send_ = ready; | |
| 104 if (ready) { | |
| 105 std::set<webrtc::DataChannel*>::iterator it; | |
| 106 for (it = connected_channels_.begin(); | |
| 107 it != connected_channels_.end(); | |
| 108 ++it) { | |
| 109 (*it)->OnChannelReady(true); | |
| 110 } | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void set_transport_error() { | |
| 115 transport_error_ = true; | |
| 116 } | |
| 117 | |
| 118 cricket::SendDataParams last_send_data_params() const { | |
| 119 return last_send_data_params_; | |
| 120 } | |
| 121 | |
| 122 bool IsConnected(webrtc::DataChannel* data_channel) const { | |
| 123 return connected_channels_.find(data_channel) != connected_channels_.end(); | |
| 124 } | |
| 125 | |
| 126 bool IsSendStreamAdded(uint32_t stream) const { | |
| 127 return send_ssrcs_.find(stream) != send_ssrcs_.end(); | |
| 128 } | |
| 129 | |
| 130 bool IsRecvStreamAdded(uint32_t stream) const { | |
| 131 return recv_ssrcs_.find(stream) != recv_ssrcs_.end(); | |
| 132 } | |
| 133 | |
| 134 private: | |
| 135 cricket::SendDataParams last_send_data_params_; | |
| 136 bool send_blocked_; | |
| 137 bool transport_available_; | |
| 138 bool ready_to_send_; | |
| 139 bool transport_error_; | |
| 140 std::set<webrtc::DataChannel*> connected_channels_; | |
| 141 std::set<uint32_t> send_ssrcs_; | |
| 142 std::set<uint32_t> recv_ssrcs_; | |
| 143 }; | |
| 144 #endif // WEBRTC_API_TEST_FAKEDATACHANNELPROVIDER_H_ | |
| OLD | NEW |