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"
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"
16 FakeVideoDecoder::FakeVideoDecoder(int decoding_delay
,
17 int max_parallel_decoding_requests
)
18 : decoding_delay_(decoding_delay
),
19 max_parallel_decoding_requests_(max_parallel_decoding_requests
),
20 state_(STATE_UNINITIALIZED
),
22 total_bytes_decoded_(0),
24 DCHECK_GE(decoding_delay
, 0);
27 FakeVideoDecoder::~FakeVideoDecoder() {
28 DCHECK_EQ(state_
, STATE_UNINITIALIZED
);
31 void FakeVideoDecoder::Initialize(const VideoDecoderConfig
& config
,
33 const PipelineStatusCB
& status_cb
,
34 const OutputCB
& output_cb
) {
35 DCHECK(thread_checker_
.CalledOnValidThread());
36 DCHECK(config
.IsValidConfig());
37 DCHECK(held_decode_callbacks_
.empty())
38 << "No reinitialization during pending decode.";
39 DCHECK(reset_cb_
.IsNull()) << "No reinitialization during pending reset.";
41 current_config_
= config
;
42 init_cb_
.SetCallback(BindToCurrentLoop(status_cb
));
44 // Don't need BindToCurrentLoop() because |output_cb_| is only called from
45 // RunDecodeCallback() which is posted from Decode().
46 output_cb_
= output_cb
;
48 if (!decoded_frames_
.empty()) {
49 DVLOG(1) << "Decoded frames dropped during reinitialization.";
50 decoded_frames_
.clear();
53 state_
= STATE_NORMAL
;
54 init_cb_
.RunOrHold(PIPELINE_OK
);
57 void FakeVideoDecoder::Decode(const scoped_refptr
<DecoderBuffer
>& buffer
,
58 const DecodeCB
& decode_cb
) {
59 DCHECK(thread_checker_
.CalledOnValidThread());
60 DCHECK(reset_cb_
.IsNull());
61 DCHECK_LE(decoded_frames_
.size(),
62 decoding_delay_
+ held_decode_callbacks_
.size());
63 DCHECK_LT(static_cast<int>(held_decode_callbacks_
.size()),
64 max_parallel_decoding_requests_
);
66 int buffer_size
= buffer
->end_of_stream() ? 0 : buffer
->data_size();
67 DecodeCB wrapped_decode_cb
=
68 BindToCurrentLoop(base::Bind(&FakeVideoDecoder::OnFrameDecoded
,
69 weak_factory_
.GetWeakPtr(),
70 buffer_size
, decode_cb
));
72 if (state_
== STATE_ERROR
) {
73 wrapped_decode_cb
.Run(kDecodeError
);
77 if (buffer
->end_of_stream()) {
78 state_
= STATE_END_OF_STREAM
;
80 DCHECK(VerifyFakeVideoBufferForTest(buffer
, current_config_
));
81 scoped_refptr
<VideoFrame
> video_frame
= VideoFrame::CreateColorFrame(
82 current_config_
.coded_size(), 0, 0, 0, buffer
->timestamp());
83 decoded_frames_
.push_back(video_frame
);
86 RunOrHoldDecode(wrapped_decode_cb
);
89 void FakeVideoDecoder::Reset(const base::Closure
& closure
) {
90 DCHECK(thread_checker_
.CalledOnValidThread());
91 DCHECK(reset_cb_
.IsNull());
93 reset_cb_
.SetCallback(BindToCurrentLoop(closure
));
94 decoded_frames_
.clear();
96 // Defer the reset if a decode is pending.
97 if (!held_decode_callbacks_
.empty())
103 void FakeVideoDecoder::Stop() {
104 DCHECK(thread_checker_
.CalledOnValidThread());
106 if (!init_cb_
.IsNull())
108 if (!held_decode_callbacks_
.empty())
110 if (!reset_cb_
.IsNull())
113 decoded_frames_
.clear();
114 state_
= STATE_UNINITIALIZED
;
117 void FakeVideoDecoder::HoldNextInit() {
118 DCHECK(thread_checker_
.CalledOnValidThread());
119 init_cb_
.HoldCallback();
122 void FakeVideoDecoder::HoldDecode() {
123 DCHECK(thread_checker_
.CalledOnValidThread());
127 void FakeVideoDecoder::HoldNextReset() {
128 DCHECK(thread_checker_
.CalledOnValidThread());
129 reset_cb_
.HoldCallback();
132 void FakeVideoDecoder::SatisfyInit() {
133 DCHECK(thread_checker_
.CalledOnValidThread());
134 DCHECK(held_decode_callbacks_
.empty());
135 DCHECK(reset_cb_
.IsNull());
137 init_cb_
.RunHeldCallback();
140 void FakeVideoDecoder::SatisfyDecode() {
141 DCHECK(thread_checker_
.CalledOnValidThread());
142 DCHECK(hold_decode_
);
144 hold_decode_
= false;
146 while (!held_decode_callbacks_
.empty()) {
147 SatisfySingleDecode();
151 void FakeVideoDecoder::SatisfySingleDecode() {
152 DCHECK(thread_checker_
.CalledOnValidThread());
153 DCHECK(!held_decode_callbacks_
.empty());
155 DecodeCB decode_cb
= held_decode_callbacks_
.front();
156 held_decode_callbacks_
.pop_front();
157 RunDecodeCallback(decode_cb
);
159 if (!reset_cb_
.IsNull() && held_decode_callbacks_
.empty())
163 void FakeVideoDecoder::SatisfyReset() {
164 DCHECK(thread_checker_
.CalledOnValidThread());
165 DCHECK(held_decode_callbacks_
.empty());
166 reset_cb_
.RunHeldCallback();
169 void FakeVideoDecoder::SimulateError() {
170 DCHECK(thread_checker_
.CalledOnValidThread());
172 state_
= STATE_ERROR
;
173 while (!held_decode_callbacks_
.empty()) {
174 held_decode_callbacks_
.front().Run(kDecodeError
);
175 held_decode_callbacks_
.pop_front();
177 decoded_frames_
.clear();
180 int FakeVideoDecoder::GetMaxDecodeRequests() const {
181 return max_parallel_decoding_requests_
;
184 void FakeVideoDecoder::OnFrameDecoded(int buffer_size
,
185 const DecodeCB
& decode_cb
,
187 DCHECK(thread_checker_
.CalledOnValidThread());
190 total_bytes_decoded_
+= buffer_size
;
191 decode_cb
.Run(status
);
194 void FakeVideoDecoder::RunOrHoldDecode(const DecodeCB
& decode_cb
) {
195 DCHECK(thread_checker_
.CalledOnValidThread());
198 held_decode_callbacks_
.push_back(decode_cb
);
200 DCHECK(held_decode_callbacks_
.empty());
201 RunDecodeCallback(decode_cb
);
205 void FakeVideoDecoder::RunDecodeCallback(const DecodeCB
& decode_cb
) {
206 DCHECK(thread_checker_
.CalledOnValidThread());
208 if (!reset_cb_
.IsNull()) {
209 DCHECK(decoded_frames_
.empty());
210 decode_cb
.Run(kAborted
);
214 // Make sure we leave decoding_delay_ frames in the queue and also frames for
215 // all pending decode callbacks, except the current one.
216 if (decoded_frames_
.size() >
217 decoding_delay_
+ held_decode_callbacks_
.size()) {
218 output_cb_
.Run(decoded_frames_
.front());
219 decoded_frames_
.pop_front();
220 } else if (state_
== STATE_END_OF_STREAM
) {
221 // Drain the queue if this was the last request in the stream, otherwise
222 // just pop the last frame from the queue.
223 if (held_decode_callbacks_
.empty()) {
224 while (!decoded_frames_
.empty()) {
225 output_cb_
.Run(decoded_frames_
.front());
226 decoded_frames_
.pop_front();
228 } else if (!decoded_frames_
.empty()) {
229 output_cb_
.Run(decoded_frames_
.front());
230 decoded_frames_
.pop_front();
237 void FakeVideoDecoder::DoReset() {
238 DCHECK(thread_checker_
.CalledOnValidThread());
239 DCHECK(held_decode_callbacks_
.empty());
240 DCHECK(!reset_cb_
.IsNull());
242 reset_cb_
.RunOrHold();