blob: ecc0aa6c73b232967c7638b2c97df73600b88073 [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
ossu7bb87ee2017-01-23 04:56:25 -080011#ifndef WEBRTC_PC_TEST_FAKEDATACHANNELPROVIDER_H_
12#define WEBRTC_PC_TEST_FAKEDATACHANNELPROVIDER_H_
stefanc1aeaf02015-10-15 07:26:07 -070013
ossu7bb87ee2017-01-23 04:56:25 -080014#include "webrtc/pc/datachannel.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020015#include "webrtc/rtc_base/checks.h"
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000016
17class FakeDataChannelProvider : public webrtc::DataChannelProviderInterface {
18 public:
19 FakeDataChannelProvider()
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000020 : send_blocked_(false),
21 transport_available_(false),
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000022 ready_to_send_(false),
23 transport_error_(false) {}
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000024 virtual ~FakeDataChannelProvider() {}
25
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000026 bool SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -070027 const rtc::CopyOnWriteBuffer& payload,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000028 cricket::SendDataResult* result) override {
kwibergee89e782017-08-09 17:22:01 -070029 RTC_CHECK(ready_to_send_);
30 RTC_CHECK(transport_available_);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000031 if (send_blocked_) {
32 *result = cricket::SDR_BLOCK;
33 return false;
34 }
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000035
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +000036 if (transport_error_ || payload.size() == 0) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000037 *result = cricket::SDR_ERROR;
38 return false;
39 }
40
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000041 last_send_data_params_ = params;
42 return true;
43 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000044
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000045 bool ConnectDataChannel(webrtc::DataChannel* data_channel) override {
nissec8ee8822017-01-18 07:20:55 -080046 RTC_CHECK(connected_channels_.find(data_channel) ==
47 connected_channels_.end());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000048 if (!transport_available_) {
49 return false;
50 }
51 LOG(LS_INFO) << "DataChannel connected " << data_channel;
52 connected_channels_.insert(data_channel);
53 return true;
54 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000055
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000056 void DisconnectDataChannel(webrtc::DataChannel* data_channel) override {
nissec8ee8822017-01-18 07:20:55 -080057 RTC_CHECK(connected_channels_.find(data_channel) !=
58 connected_channels_.end());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000059 LOG(LS_INFO) << "DataChannel disconnected " << data_channel;
60 connected_channels_.erase(data_channel);
61 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000062
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000063 void AddSctpDataStream(int sid) override {
nissec8ee8822017-01-18 07:20:55 -080064 RTC_CHECK(sid >= 0);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000065 if (!transport_available_) {
66 return;
67 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000068 send_ssrcs_.insert(sid);
69 recv_ssrcs_.insert(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000070 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000071
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000072 void RemoveSctpDataStream(int sid) override {
nissec8ee8822017-01-18 07:20:55 -080073 RTC_CHECK(sid >= 0);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000074 send_ssrcs_.erase(sid);
75 recv_ssrcs_.erase(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000076 }
77
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000078 bool ReadyToSendData() const override { return ready_to_send_; }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000079
80 // Set true to emulate the SCTP stream being blocked by congestion control.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000081 void set_send_blocked(bool blocked) {
82 send_blocked_ = blocked;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000083 if (!blocked) {
Lally Singh5c6c6e02015-05-29 11:52:39 -040084 // Take a snapshot of the connected channels and check to see whether
85 // each value is still in connected_channels_ before calling
86 // OnChannelReady(). This avoids problems where the set gets modified
87 // in response to OnChannelReady().
88 for (webrtc::DataChannel *ch : std::set<webrtc::DataChannel*>(
89 connected_channels_.begin(), connected_channels_.end())) {
90 if (connected_channels_.count(ch)) {
91 ch->OnChannelReady(true);
92 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000093 }
94 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000095 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000096
97 // Set true to emulate the transport channel creation, e.g. after
98 // setLocalDescription/setRemoteDescription called with data content.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000099 void set_transport_available(bool available) {
100 transport_available_ = available;
101 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000102
103 // Set true to emulate the transport ReadyToSendData signal when the transport
104 // becomes writable for the first time.
105 void set_ready_to_send(bool ready) {
nissec8ee8822017-01-18 07:20:55 -0800106 RTC_CHECK(transport_available_);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000107 ready_to_send_ = ready;
108 if (ready) {
109 std::set<webrtc::DataChannel*>::iterator it;
110 for (it = connected_channels_.begin();
111 it != connected_channels_.end();
112 ++it) {
113 (*it)->OnChannelReady(true);
114 }
115 }
116 }
117
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000118 void set_transport_error() {
119 transport_error_ = true;
120 }
121
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000122 cricket::SendDataParams last_send_data_params() const {
123 return last_send_data_params_;
124 }
125
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000126 bool IsConnected(webrtc::DataChannel* data_channel) const {
127 return connected_channels_.find(data_channel) != connected_channels_.end();
128 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000129
Peter Boström0c4e06b2015-10-07 12:23:21 +0200130 bool IsSendStreamAdded(uint32_t stream) const {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000131 return send_ssrcs_.find(stream) != send_ssrcs_.end();
132 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000133
Peter Boström0c4e06b2015-10-07 12:23:21 +0200134 bool IsRecvStreamAdded(uint32_t stream) const {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000135 return recv_ssrcs_.find(stream) != recv_ssrcs_.end();
136 }
137
138 private:
139 cricket::SendDataParams last_send_data_params_;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000140 bool send_blocked_;
141 bool transport_available_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000142 bool ready_to_send_;
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000143 bool transport_error_;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000144 std::set<webrtc::DataChannel*> connected_channels_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200145 std::set<uint32_t> send_ssrcs_;
146 std::set<uint32_t> recv_ssrcs_;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000147};
ossu7bb87ee2017-01-23 04:56:25 -0800148#endif // WEBRTC_PC_TEST_FAKEDATACHANNELPROVIDER_H_