blob: 2ac4f9430996f1a16fc9e8a37a3d95a052612c5c [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef PC_TEST_FAKEDATACHANNELPROVIDER_H_
12#define PC_TEST_FAKEDATACHANNELPROVIDER_H_
stefanc1aeaf02015-10-15 07:26:07 -070013
Steve Anton36b29d12017-10-30 09:57:42 -070014#include <set>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "pc/datachannel.h"
17#include "rtc_base/checks.h"
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000018
19class FakeDataChannelProvider : public webrtc::DataChannelProviderInterface {
20 public:
21 FakeDataChannelProvider()
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000022 : send_blocked_(false),
23 transport_available_(false),
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000024 ready_to_send_(false),
25 transport_error_(false) {}
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000026 virtual ~FakeDataChannelProvider() {}
27
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000028 bool SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -070029 const rtc::CopyOnWriteBuffer& payload,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000030 cricket::SendDataResult* result) override {
kwibergee89e782017-08-09 17:22:01 -070031 RTC_CHECK(ready_to_send_);
32 RTC_CHECK(transport_available_);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000033 if (send_blocked_) {
34 *result = cricket::SDR_BLOCK;
35 return false;
36 }
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000037
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +000038 if (transport_error_ || payload.size() == 0) {
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000039 *result = cricket::SDR_ERROR;
40 return false;
41 }
42
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000043 last_send_data_params_ = params;
44 return true;
45 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000046
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000047 bool ConnectDataChannel(webrtc::DataChannel* data_channel) override {
nissec8ee8822017-01-18 07:20:55 -080048 RTC_CHECK(connected_channels_.find(data_channel) ==
49 connected_channels_.end());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000050 if (!transport_available_) {
51 return false;
52 }
53 LOG(LS_INFO) << "DataChannel connected " << data_channel;
54 connected_channels_.insert(data_channel);
55 return true;
56 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000057
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000058 void DisconnectDataChannel(webrtc::DataChannel* data_channel) override {
nissec8ee8822017-01-18 07:20:55 -080059 RTC_CHECK(connected_channels_.find(data_channel) !=
60 connected_channels_.end());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000061 LOG(LS_INFO) << "DataChannel disconnected " << data_channel;
62 connected_channels_.erase(data_channel);
63 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000064
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000065 void AddSctpDataStream(int sid) override {
nissec8ee8822017-01-18 07:20:55 -080066 RTC_CHECK(sid >= 0);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000067 if (!transport_available_) {
68 return;
69 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000070 send_ssrcs_.insert(sid);
71 recv_ssrcs_.insert(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000072 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000073
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000074 void RemoveSctpDataStream(int sid) override {
nissec8ee8822017-01-18 07:20:55 -080075 RTC_CHECK(sid >= 0);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000076 send_ssrcs_.erase(sid);
77 recv_ssrcs_.erase(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000078 }
79
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000080 bool ReadyToSendData() const override { return ready_to_send_; }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000081
82 // Set true to emulate the SCTP stream being blocked by congestion control.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000083 void set_send_blocked(bool blocked) {
84 send_blocked_ = blocked;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000085 if (!blocked) {
Lally Singh5c6c6e02015-05-29 11:52:39 -040086 // Take a snapshot of the connected channels and check to see whether
87 // each value is still in connected_channels_ before calling
88 // OnChannelReady(). This avoids problems where the set gets modified
89 // in response to OnChannelReady().
90 for (webrtc::DataChannel *ch : std::set<webrtc::DataChannel*>(
91 connected_channels_.begin(), connected_channels_.end())) {
92 if (connected_channels_.count(ch)) {
93 ch->OnChannelReady(true);
94 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000095 }
96 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000097 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000098
99 // Set true to emulate the transport channel creation, e.g. after
100 // setLocalDescription/setRemoteDescription called with data content.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000101 void set_transport_available(bool available) {
102 transport_available_ = available;
103 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000104
105 // Set true to emulate the transport ReadyToSendData signal when the transport
106 // becomes writable for the first time.
107 void set_ready_to_send(bool ready) {
nissec8ee8822017-01-18 07:20:55 -0800108 RTC_CHECK(transport_available_);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000109 ready_to_send_ = ready;
110 if (ready) {
111 std::set<webrtc::DataChannel*>::iterator it;
112 for (it = connected_channels_.begin();
113 it != connected_channels_.end();
114 ++it) {
115 (*it)->OnChannelReady(true);
116 }
117 }
118 }
119
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000120 void set_transport_error() {
121 transport_error_ = true;
122 }
123
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000124 cricket::SendDataParams last_send_data_params() const {
125 return last_send_data_params_;
126 }
127
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000128 bool IsConnected(webrtc::DataChannel* data_channel) const {
129 return connected_channels_.find(data_channel) != connected_channels_.end();
130 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000131
Peter Boström0c4e06b2015-10-07 12:23:21 +0200132 bool IsSendStreamAdded(uint32_t stream) const {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000133 return send_ssrcs_.find(stream) != send_ssrcs_.end();
134 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000135
Peter Boström0c4e06b2015-10-07 12:23:21 +0200136 bool IsRecvStreamAdded(uint32_t stream) const {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000137 return recv_ssrcs_.find(stream) != recv_ssrcs_.end();
138 }
139
140 private:
141 cricket::SendDataParams last_send_data_params_;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000142 bool send_blocked_;
143 bool transport_available_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000144 bool ready_to_send_;
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000145 bool transport_error_;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000146 std::set<webrtc::DataChannel*> connected_channels_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200147 std::set<uint32_t> send_ssrcs_;
148 std::set<uint32_t> recv_ssrcs_;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000149};
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200150#endif // PC_TEST_FAKEDATACHANNELPROVIDER_H_