Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / filters / fake_video_decoder.cc
blob9b9663335352791cd1c4a9ae756beb79eb859c85
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/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/location.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "media/base/bind_to_current_loop.h"
12 #include "media/base/test_helpers.h"
14 namespace media {
16 FakeVideoDecoder::FakeVideoDecoder(int decoding_delay,
17 int max_parallel_decoding_requests,
18 const BytesDecodedCB& bytes_decoded_cb)
19 : decoding_delay_(decoding_delay),
20 max_parallel_decoding_requests_(max_parallel_decoding_requests),
21 bytes_decoded_cb_(bytes_decoded_cb),
22 state_(STATE_UNINITIALIZED),
23 hold_decode_(false),
24 total_bytes_decoded_(0),
25 fail_to_initialize_(false),
26 weak_factory_(this) {
27 DCHECK_GE(decoding_delay, 0);
30 FakeVideoDecoder::~FakeVideoDecoder() {
31 DCHECK(thread_checker_.CalledOnValidThread());
33 if (state_ == STATE_UNINITIALIZED)
34 return;
36 if (!init_cb_.IsNull())
37 SatisfyInit();
38 if (!held_decode_callbacks_.empty())
39 SatisfyDecode();
40 if (!reset_cb_.IsNull())
41 SatisfyReset();
43 decoded_frames_.clear();
46 std::string FakeVideoDecoder::GetDisplayName() const {
47 return "FakeVideoDecoder";
50 void FakeVideoDecoder::Initialize(const VideoDecoderConfig& config,
51 bool low_delay,
52 const PipelineStatusCB& status_cb,
53 const OutputCB& output_cb) {
54 DCHECK(thread_checker_.CalledOnValidThread());
55 DCHECK(config.IsValidConfig());
56 DCHECK(held_decode_callbacks_.empty())
57 << "No reinitialization during pending decode.";
58 DCHECK(reset_cb_.IsNull()) << "No reinitialization during pending reset.";
60 current_config_ = config;
61 init_cb_.SetCallback(BindToCurrentLoop(status_cb));
63 // Don't need BindToCurrentLoop() because |output_cb_| is only called from
64 // RunDecodeCallback() which is posted from Decode().
65 output_cb_ = output_cb;
67 if (!decoded_frames_.empty()) {
68 DVLOG(1) << "Decoded frames dropped during reinitialization.";
69 decoded_frames_.clear();
72 if (fail_to_initialize_) {
73 state_ = STATE_ERROR;
74 init_cb_.RunOrHold(DECODER_ERROR_NOT_SUPPORTED);
75 } else {
76 state_ = STATE_NORMAL;
77 init_cb_.RunOrHold(PIPELINE_OK);
81 void FakeVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
82 const DecodeCB& decode_cb) {
83 DCHECK(thread_checker_.CalledOnValidThread());
84 DCHECK(reset_cb_.IsNull());
85 DCHECK_LE(decoded_frames_.size(),
86 decoding_delay_ + held_decode_callbacks_.size());
87 DCHECK_LT(static_cast<int>(held_decode_callbacks_.size()),
88 max_parallel_decoding_requests_);
89 DCHECK_NE(state_, STATE_END_OF_STREAM);
91 int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size();
92 DecodeCB wrapped_decode_cb = base::Bind(&FakeVideoDecoder::OnFrameDecoded,
93 weak_factory_.GetWeakPtr(),
94 buffer_size,
95 BindToCurrentLoop(decode_cb));
97 if (state_ == STATE_ERROR) {
98 wrapped_decode_cb.Run(kDecodeError);
99 return;
102 if (buffer->end_of_stream()) {
103 state_ = STATE_END_OF_STREAM;
104 } else {
105 DCHECK(VerifyFakeVideoBufferForTest(buffer, current_config_));
106 scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame(
107 current_config_.coded_size(), 0, 0, 0, buffer->timestamp());
108 decoded_frames_.push_back(video_frame);
111 RunOrHoldDecode(wrapped_decode_cb);
114 void FakeVideoDecoder::Reset(const base::Closure& closure) {
115 DCHECK(thread_checker_.CalledOnValidThread());
116 DCHECK(reset_cb_.IsNull());
118 reset_cb_.SetCallback(BindToCurrentLoop(closure));
119 decoded_frames_.clear();
121 // Defer the reset if a decode is pending.
122 if (!held_decode_callbacks_.empty())
123 return;
125 DoReset();
128 void FakeVideoDecoder::HoldNextInit() {
129 DCHECK(thread_checker_.CalledOnValidThread());
130 init_cb_.HoldCallback();
133 void FakeVideoDecoder::HoldDecode() {
134 DCHECK(thread_checker_.CalledOnValidThread());
135 hold_decode_ = true;
138 void FakeVideoDecoder::HoldNextReset() {
139 DCHECK(thread_checker_.CalledOnValidThread());
140 reset_cb_.HoldCallback();
143 void FakeVideoDecoder::SatisfyInit() {
144 DCHECK(thread_checker_.CalledOnValidThread());
145 DCHECK(held_decode_callbacks_.empty());
146 DCHECK(reset_cb_.IsNull());
148 init_cb_.RunHeldCallback();
151 void FakeVideoDecoder::SatisfyDecode() {
152 DCHECK(thread_checker_.CalledOnValidThread());
153 DCHECK(hold_decode_);
155 hold_decode_ = false;
157 while (!held_decode_callbacks_.empty()) {
158 SatisfySingleDecode();
162 void FakeVideoDecoder::SatisfySingleDecode() {
163 DCHECK(thread_checker_.CalledOnValidThread());
164 DCHECK(!held_decode_callbacks_.empty());
166 DecodeCB decode_cb = held_decode_callbacks_.front();
167 held_decode_callbacks_.pop_front();
168 RunDecodeCallback(decode_cb);
170 if (!reset_cb_.IsNull() && held_decode_callbacks_.empty())
171 DoReset();
174 void FakeVideoDecoder::SatisfyReset() {
175 DCHECK(thread_checker_.CalledOnValidThread());
176 DCHECK(held_decode_callbacks_.empty());
177 reset_cb_.RunHeldCallback();
180 void FakeVideoDecoder::SimulateError() {
181 DCHECK(thread_checker_.CalledOnValidThread());
183 state_ = STATE_ERROR;
184 while (!held_decode_callbacks_.empty()) {
185 held_decode_callbacks_.front().Run(kDecodeError);
186 held_decode_callbacks_.pop_front();
188 decoded_frames_.clear();
191 void FakeVideoDecoder::SimulateFailureToInit() {
192 fail_to_initialize_ = true;
195 int FakeVideoDecoder::GetMaxDecodeRequests() const {
196 return max_parallel_decoding_requests_;
199 void FakeVideoDecoder::OnFrameDecoded(int buffer_size,
200 const DecodeCB& decode_cb,
201 Status status) {
202 DCHECK(thread_checker_.CalledOnValidThread());
204 if (status == kOk) {
205 total_bytes_decoded_ += buffer_size;
206 bytes_decoded_cb_.Run(buffer_size);
208 decode_cb.Run(status);
211 void FakeVideoDecoder::RunOrHoldDecode(const DecodeCB& decode_cb) {
212 DCHECK(thread_checker_.CalledOnValidThread());
214 if (hold_decode_) {
215 held_decode_callbacks_.push_back(decode_cb);
216 } else {
217 DCHECK(held_decode_callbacks_.empty());
218 RunDecodeCallback(decode_cb);
222 void FakeVideoDecoder::RunDecodeCallback(const DecodeCB& decode_cb) {
223 DCHECK(thread_checker_.CalledOnValidThread());
225 if (!reset_cb_.IsNull()) {
226 DCHECK(decoded_frames_.empty());
227 decode_cb.Run(kAborted);
228 return;
231 // Make sure we leave decoding_delay_ frames in the queue and also frames for
232 // all pending decode callbacks, except the current one.
233 if (decoded_frames_.size() >
234 decoding_delay_ + held_decode_callbacks_.size()) {
235 output_cb_.Run(decoded_frames_.front());
236 decoded_frames_.pop_front();
237 } else if (state_ == STATE_END_OF_STREAM) {
238 // Drain the queue if this was the last request in the stream, otherwise
239 // just pop the last frame from the queue.
240 if (held_decode_callbacks_.empty()) {
241 while (!decoded_frames_.empty()) {
242 output_cb_.Run(decoded_frames_.front());
243 decoded_frames_.pop_front();
245 state_ = STATE_NORMAL;
246 } else if (!decoded_frames_.empty()) {
247 output_cb_.Run(decoded_frames_.front());
248 decoded_frames_.pop_front();
252 decode_cb.Run(kOk);
255 void FakeVideoDecoder::DoReset() {
256 DCHECK(thread_checker_.CalledOnValidThread());
257 DCHECK(held_decode_callbacks_.empty());
258 DCHECK(!reset_cb_.IsNull());
260 reset_cb_.RunOrHold();
263 } // namespace media