1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/*
* Copyright 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// This file contains mock implementations of observers used in PeerConnection.
#ifndef WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_
#define WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_
#include <memory>
#include <string>
#include "webrtc/api/datachannelinterface.h"
#include "webrtc/base/checks.h"
namespace webrtc {
class MockCreateSessionDescriptionObserver
: public webrtc::CreateSessionDescriptionObserver {
public:
MockCreateSessionDescriptionObserver()
: called_(false),
result_(false) {}
virtual ~MockCreateSessionDescriptionObserver() {}
virtual void OnSuccess(SessionDescriptionInterface* desc) {
called_ = true;
result_ = true;
desc_.reset(desc);
}
virtual void OnFailure(const std::string& error) {
called_ = true;
result_ = false;
}
bool called() const { return called_; }
bool result() const { return result_; }
SessionDescriptionInterface* release_desc() {
return desc_.release();
}
private:
bool called_;
bool result_;
std::unique_ptr<SessionDescriptionInterface> desc_;
};
class MockSetSessionDescriptionObserver
: public webrtc::SetSessionDescriptionObserver {
public:
MockSetSessionDescriptionObserver()
: called_(false),
result_(false) {}
virtual ~MockSetSessionDescriptionObserver() {}
virtual void OnSuccess() {
called_ = true;
result_ = true;
}
virtual void OnFailure(const std::string& error) {
called_ = true;
result_ = false;
}
bool called() const { return called_; }
bool result() const { return result_; }
private:
bool called_;
bool result_;
};
class MockDataChannelObserver : public webrtc::DataChannelObserver {
public:
explicit MockDataChannelObserver(webrtc::DataChannelInterface* channel)
: channel_(channel) {
channel_->RegisterObserver(this);
state_ = channel_->state();
}
virtual ~MockDataChannelObserver() {
channel_->UnregisterObserver();
}
void OnBufferedAmountChange(uint64_t previous_amount) override {}
void OnStateChange() override { state_ = channel_->state(); }
void OnMessage(const DataBuffer& buffer) override {
messages_.push_back(
std::string(buffer.data.data<char>(), buffer.data.size()));
}
bool IsOpen() const { return state_ == DataChannelInterface::kOpen; }
std::vector<std::string> messages() const { return messages_; }
std::string last_message() const {
return messages_.empty() ? std::string() : messages_.back();
}
size_t received_message_count() const { return messages_.size(); }
private:
rtc::scoped_refptr<webrtc::DataChannelInterface> channel_;
DataChannelInterface::DataState state_;
std::vector<std::string> messages_;
};
class MockStatsObserver : public webrtc::StatsObserver {
public:
MockStatsObserver() : called_(false), stats_() {}
virtual ~MockStatsObserver() {}
virtual void OnComplete(const StatsReports& reports) {
RTC_CHECK(!called_);
called_ = true;
stats_.Clear();
stats_.number_of_reports = reports.size();
for (const auto* r : reports) {
if (r->type() == StatsReport::kStatsReportTypeSsrc) {
stats_.timestamp = r->timestamp();
GetIntValue(r, StatsReport::kStatsValueNameAudioOutputLevel,
&stats_.audio_output_level);
GetIntValue(r, StatsReport::kStatsValueNameAudioInputLevel,
&stats_.audio_input_level);
GetIntValue(r, StatsReport::kStatsValueNameBytesReceived,
&stats_.bytes_received);
GetIntValue(r, StatsReport::kStatsValueNameBytesSent,
&stats_.bytes_sent);
} else if (r->type() == StatsReport::kStatsReportTypeBwe) {
stats_.timestamp = r->timestamp();
GetIntValue(r, StatsReport::kStatsValueNameAvailableReceiveBandwidth,
&stats_.available_receive_bandwidth);
} else if (r->type() == StatsReport::kStatsReportTypeComponent) {
stats_.timestamp = r->timestamp();
GetStringValue(r, StatsReport::kStatsValueNameDtlsCipher,
&stats_.dtls_cipher);
GetStringValue(r, StatsReport::kStatsValueNameSrtpCipher,
&stats_.srtp_cipher);
}
}
}
bool called() const { return called_; }
size_t number_of_reports() const { return stats_.number_of_reports; }
double timestamp() const { return stats_.timestamp; }
int AudioOutputLevel() const {
RTC_CHECK(called_);
return stats_.audio_output_level;
}
int AudioInputLevel() const {
RTC_CHECK(called_);
return stats_.audio_input_level;
}
int BytesReceived() const {
RTC_CHECK(called_);
return stats_.bytes_received;
}
int BytesSent() const {
RTC_CHECK(called_);
return stats_.bytes_sent;
}
int AvailableReceiveBandwidth() const {
RTC_CHECK(called_);
return stats_.available_receive_bandwidth;
}
std::string DtlsCipher() const {
RTC_CHECK(called_);
return stats_.dtls_cipher;
}
std::string SrtpCipher() const {
RTC_CHECK(called_);
return stats_.srtp_cipher;
}
private:
bool GetIntValue(const StatsReport* report,
StatsReport::StatsValueName name,
int* value) {
const StatsReport::Value* v = report->FindValue(name);
if (v) {
// TODO(tommi): We should really just be using an int here :-/
*value = rtc::FromString<int>(v->ToString());
}
return v != nullptr;
}
bool GetStringValue(const StatsReport* report,
StatsReport::StatsValueName name,
std::string* value) {
const StatsReport::Value* v = report->FindValue(name);
if (v)
*value = v->ToString();
return v != nullptr;
}
bool called_;
struct {
void Clear() {
number_of_reports = 0;
timestamp = 0;
audio_output_level = 0;
audio_input_level = 0;
bytes_received = 0;
bytes_sent = 0;
available_receive_bandwidth = 0;
dtls_cipher.clear();
srtp_cipher.clear();
}
size_t number_of_reports;
double timestamp;
int audio_output_level;
int audio_input_level;
int bytes_received;
int bytes_sent;
int available_receive_bandwidth;
std::string dtls_cipher;
std::string srtp_cipher;
} stats_;
};
} // namespace webrtc
#endif // WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_
|