blob: 7899c3150a3764a64e0e62c56e81c175fb52d313 [file] [log] [blame]
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#ifndef WEBRTC_API_TEST_FAKEDATACHANNELPROVIDER_H_
12#define WEBRTC_API_TEST_FAKEDATACHANNELPROVIDER_H_
stefanc1aeaf02015-10-15 07:26:07 -070013
Henrik Kjellander15583c12016-02-10 10:53:12 +010014#include "webrtc/api/datachannel.h"
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000015
16class FakeDataChannelProvider : public webrtc::DataChannelProviderInterface {
17 public:
18 FakeDataChannelProvider()
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000019 : send_blocked_(false),
20 transport_available_(false),
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000021 ready_to_send_(false),
22 transport_error_(false) {}
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000023 virtual ~FakeDataChannelProvider() {}
24
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000025 bool SendData(const cricket::SendDataParams& params,
26 const rtc::Buffer& payload,
27 cricket::SendDataResult* result) override {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000028 ASSERT(ready_to_send_ && transport_available_);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000029 if (send_blocked_) {
30 *result = cricket::SDR_BLOCK;
31 return false;
32 }
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000033
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +000034 if (transport_error_ || payload.size() == 0) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000035 *result = cricket::SDR_ERROR;
36 return false;
37 }
38
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000039 last_send_data_params_ = params;
40 return true;
41 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000042
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000043 bool ConnectDataChannel(webrtc::DataChannel* data_channel) override {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000044 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 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000052
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000053 void DisconnectDataChannel(webrtc::DataChannel* data_channel) override {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000054 ASSERT(connected_channels_.find(data_channel) != connected_channels_.end());
55 LOG(LS_INFO) << "DataChannel disconnected " << data_channel;
56 connected_channels_.erase(data_channel);
57 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000058
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000059 void AddSctpDataStream(int sid) override {
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +000060 ASSERT(sid >= 0);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000061 if (!transport_available_) {
62 return;
63 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000064 send_ssrcs_.insert(sid);
65 recv_ssrcs_.insert(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000066 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000067
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000068 void RemoveSctpDataStream(int sid) override {
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +000069 ASSERT(sid >= 0);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000070 send_ssrcs_.erase(sid);
71 recv_ssrcs_.erase(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000072 }
73
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000074 bool ReadyToSendData() const override { return ready_to_send_; }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000075
76 // Set true to emulate the SCTP stream being blocked by congestion control.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000077 void set_send_blocked(bool blocked) {
78 send_blocked_ = blocked;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000079 if (!blocked) {
Lally Singh5c6c6e02015-05-29 11:52:39 -040080 // 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 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000089 }
90 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000091 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000092
93 // Set true to emulate the transport channel creation, e.g. after
94 // setLocalDescription/setRemoteDescription called with data content.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000095 void set_transport_available(bool available) {
96 transport_available_ = available;
97 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000098
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
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000114 void set_transport_error() {
115 transport_error_ = true;
116 }
117
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000118 cricket::SendDataParams last_send_data_params() const {
119 return last_send_data_params_;
120 }
121
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000122 bool IsConnected(webrtc::DataChannel* data_channel) const {
123 return connected_channels_.find(data_channel) != connected_channels_.end();
124 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000125
Peter Boström0c4e06b2015-10-07 12:23:21 +0200126 bool IsSendStreamAdded(uint32_t stream) const {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000127 return send_ssrcs_.find(stream) != send_ssrcs_.end();
128 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000129
Peter Boström0c4e06b2015-10-07 12:23:21 +0200130 bool IsRecvStreamAdded(uint32_t stream) const {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000131 return recv_ssrcs_.find(stream) != recv_ssrcs_.end();
132 }
133
134 private:
135 cricket::SendDataParams last_send_data_params_;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000136 bool send_blocked_;
137 bool transport_available_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000138 bool ready_to_send_;
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000139 bool transport_error_;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000140 std::set<webrtc::DataChannel*> connected_channels_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200141 std::set<uint32_t> send_ssrcs_;
142 std::set<uint32_t> recv_ssrcs_;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000143};
Henrik Kjellander15583c12016-02-10 10:53:12 +0100144#endif // WEBRTC_API_TEST_FAKEDATACHANNELPROVIDER_H_