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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*
* Copyright (c) 2010 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.
*/
#ifndef WEBRTC_MEDIA_ENGINE_FAKEWEBRTCVIDEOENGINE_H_
#define WEBRTC_MEDIA_ENGINE_FAKEWEBRTCVIDEOENGINE_H_
#include <map>
#include <set>
#include <vector>
#include "webrtc/base/basictypes.h"
#include "webrtc/base/criticalsection.h"
#include "webrtc/base/gunit.h"
#include "webrtc/base/stringutils.h"
#include "webrtc/base/thread_annotations.h"
#include "webrtc/media/base/codec.h"
#include "webrtc/media/engine/webrtcvideodecoderfactory.h"
#include "webrtc/media/engine/webrtcvideoencoderfactory.h"
#include "webrtc/modules/video_coding/include/video_error_codes.h"
#include "webrtc/video_decoder.h"
#include "webrtc/video_encoder.h"
namespace cricket {
static const int kMinVideoBitrate = 100;
static const int kStartVideoBitrate = 300;
static const int kMaxVideoBitrate = 1000;
// WebRtc channel id and capture id share the same number space.
// This is how AddRenderer(renderId, ...) is able to tell if it is adding a
// renderer for a channel or it is adding a renderer for a capturer.
static const int kViEChannelIdBase = 0;
static const int kViEChannelIdMax = 1000;
static const int kEventTimeoutMs = 10000;
// Fake class for mocking out webrtc::VideoDecoder
class FakeWebRtcVideoDecoder : public webrtc::VideoDecoder {
public:
FakeWebRtcVideoDecoder() : num_frames_received_(0) {}
virtual int32_t InitDecode(const webrtc::VideoCodec*, int32_t) {
return WEBRTC_VIDEO_CODEC_OK;
}
virtual int32_t Decode(const webrtc::EncodedImage&,
bool,
const webrtc::RTPFragmentationHeader*,
const webrtc::CodecSpecificInfo*,
int64_t) {
num_frames_received_++;
return WEBRTC_VIDEO_CODEC_OK;
}
virtual int32_t RegisterDecodeCompleteCallback(
webrtc::DecodedImageCallback*) {
return WEBRTC_VIDEO_CODEC_OK;
}
virtual int32_t Release() { return WEBRTC_VIDEO_CODEC_OK; }
int GetNumFramesReceived() const {
return num_frames_received_;
}
private:
int num_frames_received_;
};
// Fake class for mocking out WebRtcVideoDecoderFactory.
class FakeWebRtcVideoDecoderFactory : public WebRtcVideoDecoderFactory {
public:
FakeWebRtcVideoDecoderFactory()
: num_created_decoders_(0) {
}
virtual webrtc::VideoDecoder* CreateVideoDecoder(
webrtc::VideoCodecType type) {
if (supported_codec_types_.count(type) == 0) {
return NULL;
}
FakeWebRtcVideoDecoder* decoder = new FakeWebRtcVideoDecoder();
decoders_.push_back(decoder);
num_created_decoders_++;
return decoder;
}
virtual webrtc::VideoDecoder* CreateVideoDecoderWithParams(
webrtc::VideoCodecType type,
VideoDecoderParams params) {
params_.push_back(params);
return CreateVideoDecoder(type);
}
virtual void DestroyVideoDecoder(webrtc::VideoDecoder* decoder) {
decoders_.erase(
std::remove(decoders_.begin(), decoders_.end(), decoder),
decoders_.end());
delete decoder;
}
void AddSupportedVideoCodecType(webrtc::VideoCodecType type) {
supported_codec_types_.insert(type);
}
int GetNumCreatedDecoders() {
return num_created_decoders_;
}
const std::vector<FakeWebRtcVideoDecoder*>& decoders() {
return decoders_;
}
const std::vector<VideoDecoderParams>& params() { return params_; }
private:
std::set<webrtc::VideoCodecType> supported_codec_types_;
std::vector<FakeWebRtcVideoDecoder*> decoders_;
int num_created_decoders_;
std::vector<VideoDecoderParams> params_;
};
// Fake class for mocking out webrtc::VideoEnoder
class FakeWebRtcVideoEncoder : public webrtc::VideoEncoder {
public:
FakeWebRtcVideoEncoder()
: init_encode_event_(false, false), num_frames_encoded_(0) {}
int32_t InitEncode(const webrtc::VideoCodec* codecSettings,
int32_t numberOfCores,
size_t maxPayloadSize) override {
rtc::CritScope lock(&crit_);
codec_settings_ = *codecSettings;
init_encode_event_.Set();
return WEBRTC_VIDEO_CODEC_OK;
}
bool WaitForInitEncode() { return init_encode_event_.Wait(kEventTimeoutMs); }
webrtc::VideoCodec GetCodecSettings() {
rtc::CritScope lock(&crit_);
return codec_settings_;
}
int32_t Encode(const webrtc::VideoFrame& inputImage,
const webrtc::CodecSpecificInfo* codecSpecificInfo,
const std::vector<webrtc::FrameType>* frame_types) override {
rtc::CritScope lock(&crit_);
++num_frames_encoded_;
init_encode_event_.Set();
return WEBRTC_VIDEO_CODEC_OK;
}
int32_t RegisterEncodeCompleteCallback(
webrtc::EncodedImageCallback* callback) override {
return WEBRTC_VIDEO_CODEC_OK;
}
int32_t Release() override { return WEBRTC_VIDEO_CODEC_OK; }
int32_t SetChannelParameters(uint32_t packetLoss, int64_t rtt) override {
return WEBRTC_VIDEO_CODEC_OK;
}
int32_t SetRateAllocation(const webrtc::BitrateAllocation& allocation,
uint32_t framerate) override {
return WEBRTC_VIDEO_CODEC_OK;
}
int GetNumEncodedFrames() {
rtc::CritScope lock(&crit_);
return num_frames_encoded_;
}
private:
rtc::CriticalSection crit_;
rtc::Event init_encode_event_;
int num_frames_encoded_ GUARDED_BY(crit_);
webrtc::VideoCodec codec_settings_ GUARDED_BY(crit_);
};
// Fake class for mocking out WebRtcVideoEncoderFactory.
class FakeWebRtcVideoEncoderFactory : public WebRtcVideoEncoderFactory {
public:
FakeWebRtcVideoEncoderFactory()
: created_video_encoder_event_(false, false),
num_created_encoders_(0),
encoders_have_internal_sources_(false) {}
webrtc::VideoEncoder* CreateVideoEncoder(
const cricket::VideoCodec& codec) override {
rtc::CritScope lock(&crit_);
if (!FindMatchingCodec(codecs_, codec))
return nullptr;
FakeWebRtcVideoEncoder* encoder = new FakeWebRtcVideoEncoder();
encoders_.push_back(encoder);
num_created_encoders_++;
created_video_encoder_event_.Set();
return encoder;
}
bool WaitForCreatedVideoEncoders(int num_encoders) {
while (created_video_encoder_event_.Wait(kEventTimeoutMs)) {
if (GetNumCreatedEncoders() >= num_encoders)
return true;
}
return false;
}
void DestroyVideoEncoder(webrtc::VideoEncoder* encoder) override {
rtc::CritScope lock(&crit_);
encoders_.erase(
std::remove(encoders_.begin(), encoders_.end(), encoder),
encoders_.end());
delete encoder;
}
const std::vector<cricket::VideoCodec>& supported_codecs() const override {
return codecs_;
}
bool EncoderTypeHasInternalSource(
webrtc::VideoCodecType type) const override {
return encoders_have_internal_sources_;
}
void set_encoders_have_internal_sources(bool internal_source) {
encoders_have_internal_sources_ = internal_source;
}
void AddSupportedVideoCodec(const cricket::VideoCodec& codec) {
codecs_.push_back(codec);
}
void AddSupportedVideoCodecType(const std::string& name) {
codecs_.push_back(cricket::VideoCodec(name));
}
int GetNumCreatedEncoders() {
rtc::CritScope lock(&crit_);
return num_created_encoders_;
}
const std::vector<FakeWebRtcVideoEncoder*> encoders() {
rtc::CritScope lock(&crit_);
return encoders_;
}
private:
// Disable overloaded virtual function warning. TODO(magjed): Remove once
// http://crbug/webrtc/6402 is fixed.
using cricket::WebRtcVideoEncoderFactory::CreateVideoEncoder;
rtc::CriticalSection crit_;
rtc::Event created_video_encoder_event_;
std::vector<cricket::VideoCodec> codecs_;
std::vector<FakeWebRtcVideoEncoder*> encoders_ GUARDED_BY(crit_);
int num_created_encoders_ GUARDED_BY(crit_);
bool encoders_have_internal_sources_;
};
} // namespace cricket
#endif // WEBRTC_MEDIA_ENGINE_FAKEWEBRTCVIDEOENGINE_H_
|