Add @VisibleForTesting to fix ChromePublic release build.
[chromium-blink-merge.git] / media / filters / fake_video_decoder.cc
blob4c6fa9a7712b6ee605f1bf18e7bb3c53c611b547
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/filters/fake_video_decoder.h"
7 #include "base/location.h"
8 #include "base/message_loop/message_loop_proxy.h"
9 #include "media/base/bind_to_current_loop.h"
10 #include "media/base/test_helpers.h"
12 namespace media {
14 FakeVideoDecoder::FakeVideoDecoder(int decoding_delay,
15 int max_parallel_decoding_requests,
16 const BytesDecodedCB& bytes_decoded_cb)
17 : decoding_delay_(decoding_delay),
18 max_parallel_decoding_requests_(max_parallel_decoding_requests),
19 bytes_decoded_cb_(bytes_decoded_cb),
20 state_(STATE_UNINITIALIZED),
21 hold_decode_(false),
22 total_bytes_decoded_(0),
23 fail_to_initialize_(false),
24 weak_factory_(this) {
25 DCHECK_GE(decoding_delay, 0);
28 FakeVideoDecoder::~FakeVideoDecoder() {
29 DCHECK(thread_checker_.CalledOnValidThread());
31 if (state_ == STATE_UNINITIALIZED)
32 return;
34 if (!init_cb_.IsNull())
35 SatisfyInit();
36 if (!held_decode_callbacks_.empty())
37 SatisfyDecode();
38 if (!reset_cb_.IsNull())
39 SatisfyReset();
41 decoded_frames_.clear();
44 std::string FakeVideoDecoder::GetDisplayName() const {
45 return "FakeVideoDecoder";
48 void FakeVideoDecoder::Initialize(const VideoDecoderConfig& config,
49 bool low_delay,
50 const PipelineStatusCB& status_cb,
51 const OutputCB& output_cb) {
52 DCHECK(thread_checker_.CalledOnValidThread());
53 DCHECK(config.IsValidConfig());
54 DCHECK(held_decode_callbacks_.empty())
55 << "No reinitialization during pending decode.";
56 DCHECK(reset_cb_.IsNull()) << "No reinitialization during pending reset.";
58 current_config_ = config;
59 init_cb_.SetCallback(BindToCurrentLoop(status_cb));
61 // Don't need BindToCurrentLoop() because |output_cb_| is only called from
62 // RunDecodeCallback() which is posted from Decode().
63 output_cb_ = output_cb;
65 if (!decoded_frames_.empty()) {
66 DVLOG(1) << "Decoded frames dropped during reinitialization.";
67 decoded_frames_.clear();
70 if (fail_to_initialize_) {
71 state_ = STATE_ERROR;
72 init_cb_.RunOrHold(DECODER_ERROR_NOT_SUPPORTED);
73 } else {
74 state_ = STATE_NORMAL;
75 init_cb_.RunOrHold(PIPELINE_OK);
79 void FakeVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
80 const DecodeCB& decode_cb) {
81 DCHECK(thread_checker_.CalledOnValidThread());
82 DCHECK(reset_cb_.IsNull());
83 DCHECK_LE(decoded_frames_.size(),
84 decoding_delay_ + held_decode_callbacks_.size());
85 DCHECK_LT(static_cast<int>(held_decode_callbacks_.size()),
86 max_parallel_decoding_requests_);
87 DCHECK_NE(state_, STATE_END_OF_STREAM);
89 int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size();
90 DecodeCB wrapped_decode_cb = base::Bind(&FakeVideoDecoder::OnFrameDecoded,
91 weak_factory_.GetWeakPtr(),
92 buffer_size,
93 BindToCurrentLoop(decode_cb));
95 if (state_ == STATE_ERROR) {
96 wrapped_decode_cb.Run(kDecodeError);
97 return;
100 if (buffer->end_of_stream()) {
101 state_ = STATE_END_OF_STREAM;
102 } else {
103 DCHECK(VerifyFakeVideoBufferForTest(buffer, current_config_));
104 scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame(
105 current_config_.coded_size(), 0, 0, 0, buffer->timestamp());
106 decoded_frames_.push_back(video_frame);
109 RunOrHoldDecode(wrapped_decode_cb);
112 void FakeVideoDecoder::Reset(const base::Closure& closure) {
113 DCHECK(thread_checker_.CalledOnValidThread());
114 DCHECK(reset_cb_.IsNull());
116 reset_cb_.SetCallback(BindToCurrentLoop(closure));
117 decoded_frames_.clear();
119 // Defer the reset if a decode is pending.
120 if (!held_decode_callbacks_.empty())
121 return;
123 DoReset();
126 void FakeVideoDecoder::HoldNextInit() {
127 DCHECK(thread_checker_.CalledOnValidThread());
128 init_cb_.HoldCallback();
131 void FakeVideoDecoder::HoldDecode() {
132 DCHECK(thread_checker_.CalledOnValidThread());
133 hold_decode_ = true;
136 void FakeVideoDecoder::HoldNextReset() {
137 DCHECK(thread_checker_.CalledOnValidThread());
138 reset_cb_.HoldCallback();
141 void FakeVideoDecoder::SatisfyInit() {
142 DCHECK(thread_checker_.CalledOnValidThread());
143 DCHECK(held_decode_callbacks_.empty());
144 DCHECK(reset_cb_.IsNull());
146 init_cb_.RunHeldCallback();
149 void FakeVideoDecoder::SatisfyDecode() {
150 DCHECK(thread_checker_.CalledOnValidThread());
151 DCHECK(hold_decode_);
153 hold_decode_ = false;
155 while (!held_decode_callbacks_.empty()) {
156 SatisfySingleDecode();
160 void FakeVideoDecoder::SatisfySingleDecode() {
161 DCHECK(thread_checker_.CalledOnValidThread());
162 DCHECK(!held_decode_callbacks_.empty());
164 DecodeCB decode_cb = held_decode_callbacks_.front();
165 held_decode_callbacks_.pop_front();
166 RunDecodeCallback(decode_cb);
168 if (!reset_cb_.IsNull() && held_decode_callbacks_.empty())
169 DoReset();
172 void FakeVideoDecoder::SatisfyReset() {
173 DCHECK(thread_checker_.CalledOnValidThread());
174 DCHECK(held_decode_callbacks_.empty());
175 reset_cb_.RunHeldCallback();
178 void FakeVideoDecoder::SimulateError() {
179 DCHECK(thread_checker_.CalledOnValidThread());
181 state_ = STATE_ERROR;
182 while (!held_decode_callbacks_.empty()) {
183 held_decode_callbacks_.front().Run(kDecodeError);
184 held_decode_callbacks_.pop_front();
186 decoded_frames_.clear();
189 void FakeVideoDecoder::SimulateFailureToInit() {
190 fail_to_initialize_ = true;
193 int FakeVideoDecoder::GetMaxDecodeRequests() const {
194 return max_parallel_decoding_requests_;
197 void FakeVideoDecoder::OnFrameDecoded(int buffer_size,
198 const DecodeCB& decode_cb,
199 Status status) {
200 DCHECK(thread_checker_.CalledOnValidThread());
202 if (status == kOk) {
203 total_bytes_decoded_ += buffer_size;
204 bytes_decoded_cb_.Run(buffer_size);
206 decode_cb.Run(status);
209 void FakeVideoDecoder::RunOrHoldDecode(const DecodeCB& decode_cb) {
210 DCHECK(thread_checker_.CalledOnValidThread());
212 if (hold_decode_) {
213 held_decode_callbacks_.push_back(decode_cb);
214 } else {
215 DCHECK(held_decode_callbacks_.empty());
216 RunDecodeCallback(decode_cb);
220 void FakeVideoDecoder::RunDecodeCallback(const DecodeCB& decode_cb) {
221 DCHECK(thread_checker_.CalledOnValidThread());
223 if (!reset_cb_.IsNull()) {
224 DCHECK(decoded_frames_.empty());
225 decode_cb.Run(kAborted);
226 return;
229 // Make sure we leave decoding_delay_ frames in the queue and also frames for
230 // all pending decode callbacks, except the current one.
231 if (decoded_frames_.size() >
232 decoding_delay_ + held_decode_callbacks_.size()) {
233 output_cb_.Run(decoded_frames_.front());
234 decoded_frames_.pop_front();
235 } else if (state_ == STATE_END_OF_STREAM) {
236 // Drain the queue if this was the last request in the stream, otherwise
237 // just pop the last frame from the queue.
238 if (held_decode_callbacks_.empty()) {
239 while (!decoded_frames_.empty()) {
240 output_cb_.Run(decoded_frames_.front());
241 decoded_frames_.pop_front();
243 state_ = STATE_NORMAL;
244 } else if (!decoded_frames_.empty()) {
245 output_cb_.Run(decoded_frames_.front());
246 decoded_frames_.pop_front();
250 decode_cb.Run(kOk);
253 void FakeVideoDecoder::DoReset() {
254 DCHECK(thread_checker_.CalledOnValidThread());
255 DCHECK(held_decode_callbacks_.empty());
256 DCHECK(!reset_cb_.IsNull());
258 reset_cb_.RunOrHold();
261 } // namespace media