[MD settings] moving attached() code
[chromium-blink-merge.git] / media / filters / fake_video_decoder.cc
blob00264ea5309cb9819fc426edc2f6edd636b3a876
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 "media/base/bind_to_current_loop.h"
9 #include "media/base/test_helpers.h"
11 namespace media {
13 FakeVideoDecoder::FakeVideoDecoder(int decoding_delay,
14 int max_parallel_decoding_requests,
15 const BytesDecodedCB& bytes_decoded_cb)
16 : decoding_delay_(decoding_delay),
17 max_parallel_decoding_requests_(max_parallel_decoding_requests),
18 bytes_decoded_cb_(bytes_decoded_cb),
19 state_(STATE_UNINITIALIZED),
20 hold_decode_(false),
21 total_bytes_decoded_(0),
22 fail_to_initialize_(false),
23 weak_factory_(this) {
24 DCHECK_GE(decoding_delay, 0);
27 FakeVideoDecoder::~FakeVideoDecoder() {
28 DCHECK(thread_checker_.CalledOnValidThread());
30 if (state_ == STATE_UNINITIALIZED)
31 return;
33 if (!init_cb_.IsNull())
34 SatisfyInit();
35 if (!held_decode_callbacks_.empty())
36 SatisfyDecode();
37 if (!reset_cb_.IsNull())
38 SatisfyReset();
40 decoded_frames_.clear();
43 std::string FakeVideoDecoder::GetDisplayName() const {
44 return "FakeVideoDecoder";
47 void FakeVideoDecoder::Initialize(const VideoDecoderConfig& config,
48 bool low_delay,
49 const InitCB& init_cb,
50 const OutputCB& output_cb) {
51 DCHECK(thread_checker_.CalledOnValidThread());
52 DCHECK(config.IsValidConfig());
53 DCHECK(held_decode_callbacks_.empty())
54 << "No reinitialization during pending decode.";
55 DCHECK(reset_cb_.IsNull()) << "No reinitialization during pending reset.";
57 current_config_ = config;
58 init_cb_.SetCallback(BindToCurrentLoop(init_cb));
60 // Don't need BindToCurrentLoop() because |output_cb_| is only called from
61 // RunDecodeCallback() which is posted from Decode().
62 output_cb_ = output_cb;
64 if (!decoded_frames_.empty()) {
65 DVLOG(1) << "Decoded frames dropped during reinitialization.";
66 decoded_frames_.clear();
69 if (fail_to_initialize_) {
70 state_ = STATE_ERROR;
71 init_cb_.RunOrHold(false);
72 } else {
73 state_ = STATE_NORMAL;
74 init_cb_.RunOrHold(true);
78 void FakeVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
79 const DecodeCB& decode_cb) {
80 DCHECK(thread_checker_.CalledOnValidThread());
81 DCHECK(reset_cb_.IsNull());
82 DCHECK_LE(decoded_frames_.size(),
83 decoding_delay_ + held_decode_callbacks_.size());
84 DCHECK_LT(static_cast<int>(held_decode_callbacks_.size()),
85 max_parallel_decoding_requests_);
86 DCHECK_NE(state_, STATE_END_OF_STREAM);
88 int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size();
89 DecodeCB wrapped_decode_cb = base::Bind(&FakeVideoDecoder::OnFrameDecoded,
90 weak_factory_.GetWeakPtr(),
91 buffer_size,
92 BindToCurrentLoop(decode_cb));
94 if (state_ == STATE_ERROR) {
95 wrapped_decode_cb.Run(kDecodeError);
96 return;
99 if (buffer->end_of_stream()) {
100 state_ = STATE_END_OF_STREAM;
101 } else {
102 DCHECK(VerifyFakeVideoBufferForTest(buffer, current_config_));
103 scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame(
104 current_config_.coded_size(), 0, 0, 0, buffer->timestamp());
105 decoded_frames_.push_back(video_frame);
108 RunOrHoldDecode(wrapped_decode_cb);
111 void FakeVideoDecoder::Reset(const base::Closure& closure) {
112 DCHECK(thread_checker_.CalledOnValidThread());
113 DCHECK(reset_cb_.IsNull());
115 reset_cb_.SetCallback(BindToCurrentLoop(closure));
116 decoded_frames_.clear();
118 // Defer the reset if a decode is pending.
119 if (!held_decode_callbacks_.empty())
120 return;
122 DoReset();
125 void FakeVideoDecoder::HoldNextInit() {
126 DCHECK(thread_checker_.CalledOnValidThread());
127 init_cb_.HoldCallback();
130 void FakeVideoDecoder::HoldDecode() {
131 DCHECK(thread_checker_.CalledOnValidThread());
132 hold_decode_ = true;
135 void FakeVideoDecoder::HoldNextReset() {
136 DCHECK(thread_checker_.CalledOnValidThread());
137 reset_cb_.HoldCallback();
140 void FakeVideoDecoder::SatisfyInit() {
141 DCHECK(thread_checker_.CalledOnValidThread());
142 DCHECK(held_decode_callbacks_.empty());
143 DCHECK(reset_cb_.IsNull());
145 init_cb_.RunHeldCallback();
148 void FakeVideoDecoder::SatisfyDecode() {
149 DCHECK(thread_checker_.CalledOnValidThread());
150 DCHECK(hold_decode_);
152 hold_decode_ = false;
154 while (!held_decode_callbacks_.empty()) {
155 SatisfySingleDecode();
159 void FakeVideoDecoder::SatisfySingleDecode() {
160 DCHECK(thread_checker_.CalledOnValidThread());
161 DCHECK(!held_decode_callbacks_.empty());
163 DecodeCB decode_cb = held_decode_callbacks_.front();
164 held_decode_callbacks_.pop_front();
165 RunDecodeCallback(decode_cb);
167 if (!reset_cb_.IsNull() && held_decode_callbacks_.empty())
168 DoReset();
171 void FakeVideoDecoder::SatisfyReset() {
172 DCHECK(thread_checker_.CalledOnValidThread());
173 DCHECK(held_decode_callbacks_.empty());
174 reset_cb_.RunHeldCallback();
177 void FakeVideoDecoder::SimulateError() {
178 DCHECK(thread_checker_.CalledOnValidThread());
180 state_ = STATE_ERROR;
181 while (!held_decode_callbacks_.empty()) {
182 held_decode_callbacks_.front().Run(kDecodeError);
183 held_decode_callbacks_.pop_front();
185 decoded_frames_.clear();
188 void FakeVideoDecoder::SimulateFailureToInit() {
189 fail_to_initialize_ = true;
192 int FakeVideoDecoder::GetMaxDecodeRequests() const {
193 return max_parallel_decoding_requests_;
196 void FakeVideoDecoder::OnFrameDecoded(int buffer_size,
197 const DecodeCB& decode_cb,
198 Status status) {
199 DCHECK(thread_checker_.CalledOnValidThread());
201 if (status == kOk) {
202 total_bytes_decoded_ += buffer_size;
203 bytes_decoded_cb_.Run(buffer_size);
205 decode_cb.Run(status);
208 void FakeVideoDecoder::RunOrHoldDecode(const DecodeCB& decode_cb) {
209 DCHECK(thread_checker_.CalledOnValidThread());
211 if (hold_decode_) {
212 held_decode_callbacks_.push_back(decode_cb);
213 } else {
214 DCHECK(held_decode_callbacks_.empty());
215 RunDecodeCallback(decode_cb);
219 void FakeVideoDecoder::RunDecodeCallback(const DecodeCB& decode_cb) {
220 DCHECK(thread_checker_.CalledOnValidThread());
222 if (!reset_cb_.IsNull()) {
223 DCHECK(decoded_frames_.empty());
224 decode_cb.Run(kAborted);
225 return;
228 // Make sure we leave decoding_delay_ frames in the queue and also frames for
229 // all pending decode callbacks, except the current one.
230 if (decoded_frames_.size() >
231 decoding_delay_ + held_decode_callbacks_.size()) {
232 output_cb_.Run(decoded_frames_.front());
233 decoded_frames_.pop_front();
234 } else if (state_ == STATE_END_OF_STREAM) {
235 // Drain the queue if this was the last request in the stream, otherwise
236 // just pop the last frame from the queue.
237 if (held_decode_callbacks_.empty()) {
238 while (!decoded_frames_.empty()) {
239 output_cb_.Run(decoded_frames_.front());
240 decoded_frames_.pop_front();
242 state_ = STATE_NORMAL;
243 } else if (!decoded_frames_.empty()) {
244 output_cb_.Run(decoded_frames_.front());
245 decoded_frames_.pop_front();
249 decode_cb.Run(kOk);
252 void FakeVideoDecoder::DoReset() {
253 DCHECK(thread_checker_.CalledOnValidThread());
254 DCHECK(held_decode_callbacks_.empty());
255 DCHECK(!reset_cb_.IsNull());
257 reset_cb_.RunOrHold();
260 } // namespace media