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 bool supports_get_decode_output
,
18 int max_parallel_decoding_requests
)
19 : decoding_delay_(decoding_delay
),
20 supports_get_decode_output_(supports_get_decode_output
),
21 max_parallel_decoding_requests_(max_parallel_decoding_requests
),
22 state_(STATE_UNINITIALIZED
),
24 total_bytes_decoded_(0),
26 DCHECK_GE(decoding_delay
, 0);
29 FakeVideoDecoder::~FakeVideoDecoder() {
30 DCHECK_EQ(state_
, STATE_UNINITIALIZED
);
33 void FakeVideoDecoder::Initialize(const VideoDecoderConfig
& config
,
35 const PipelineStatusCB
& status_cb
) {
36 DCHECK(thread_checker_
.CalledOnValidThread());
37 DCHECK(config
.IsValidConfig());
38 DCHECK(held_decode_callbacks_
.empty())
39 << "No reinitialization during pending decode.";
40 DCHECK(reset_cb_
.IsNull()) << "No reinitialization during pending reset.";
42 current_config_
= config
;
43 init_cb_
.SetCallback(BindToCurrentLoop(status_cb
));
45 if (!decoded_frames_
.empty()) {
46 DVLOG(1) << "Decoded frames dropped during reinitialization.";
47 decoded_frames_
.clear();
50 state_
= STATE_NORMAL
;
51 init_cb_
.RunOrHold(PIPELINE_OK
);
54 void FakeVideoDecoder::Decode(const scoped_refptr
<DecoderBuffer
>& buffer
,
55 const DecodeCB
& decode_cb
) {
56 DCHECK(thread_checker_
.CalledOnValidThread());
57 DCHECK(reset_cb_
.IsNull());
58 DCHECK_LE(decoded_frames_
.size(),
59 decoding_delay_
+ held_decode_callbacks_
.size());
60 DCHECK_LT(static_cast<int>(held_decode_callbacks_
.size()),
61 max_parallel_decoding_requests_
);
63 int buffer_size
= buffer
->end_of_stream() ? 0 : buffer
->data_size();
64 DecodeCB wrapped_decode_cb
=
65 BindToCurrentLoop(base::Bind(&FakeVideoDecoder::OnFrameDecoded
,
66 weak_factory_
.GetWeakPtr(),
70 if (state_
== STATE_ERROR
) {
71 wrapped_decode_cb
.Run(kDecodeError
, scoped_refptr
<VideoFrame
>());
75 if (buffer
->end_of_stream()) {
76 state_
= STATE_END_OF_STREAM
;
78 DCHECK(VerifyFakeVideoBufferForTest(buffer
, current_config_
));
79 scoped_refptr
<VideoFrame
> video_frame
= VideoFrame::CreateColorFrame(
80 current_config_
.coded_size(), 0, 0, 0, buffer
->timestamp());
81 decoded_frames_
.push_back(video_frame
);
84 RunOrHoldDecode(wrapped_decode_cb
);
87 void FakeVideoDecoder::Reset(const base::Closure
& closure
) {
88 DCHECK(thread_checker_
.CalledOnValidThread());
89 DCHECK(reset_cb_
.IsNull());
91 reset_cb_
.SetCallback(BindToCurrentLoop(closure
));
92 decoded_frames_
.clear();
94 // Defer the reset if a decode is pending.
95 if (!held_decode_callbacks_
.empty())
101 void FakeVideoDecoder::Stop() {
102 DCHECK(thread_checker_
.CalledOnValidThread());
104 if (!init_cb_
.IsNull())
106 if (!held_decode_callbacks_
.empty())
108 if (!reset_cb_
.IsNull())
111 decoded_frames_
.clear();
112 state_
= STATE_UNINITIALIZED
;
115 scoped_refptr
<VideoFrame
> FakeVideoDecoder::GetDecodeOutput() {
116 DCHECK(thread_checker_
.CalledOnValidThread());
117 if (!supports_get_decode_output_
|| decoded_frames_
.empty())
119 scoped_refptr
<VideoFrame
> out
= decoded_frames_
.front();
120 decoded_frames_
.pop_front();
124 void FakeVideoDecoder::HoldNextInit() {
125 DCHECK(thread_checker_
.CalledOnValidThread());
126 init_cb_
.HoldCallback();
129 void FakeVideoDecoder::HoldDecode() {
130 DCHECK(thread_checker_
.CalledOnValidThread());
134 void FakeVideoDecoder::HoldNextReset() {
135 DCHECK(thread_checker_
.CalledOnValidThread());
136 reset_cb_
.HoldCallback();
139 void FakeVideoDecoder::SatisfyInit() {
140 DCHECK(thread_checker_
.CalledOnValidThread());
141 DCHECK(held_decode_callbacks_
.empty());
142 DCHECK(reset_cb_
.IsNull());
144 init_cb_
.RunHeldCallback();
147 void FakeVideoDecoder::SatisfyDecode() {
148 DCHECK(thread_checker_
.CalledOnValidThread());
149 DCHECK(hold_decode_
);
151 hold_decode_
= false;
153 while (!held_decode_callbacks_
.empty()) {
154 SatisfySingleDecode();
158 void FakeVideoDecoder::SatisfySingleDecode() {
159 DCHECK(thread_checker_
.CalledOnValidThread());
160 DCHECK(!held_decode_callbacks_
.empty());
162 DecodeCB decode_cb
= held_decode_callbacks_
.front();
163 held_decode_callbacks_
.pop_front();
164 RunDecodeCallback(decode_cb
);
166 if (!reset_cb_
.IsNull() && held_decode_callbacks_
.empty())
170 void FakeVideoDecoder::SatisfyReset() {
171 DCHECK(thread_checker_
.CalledOnValidThread());
172 DCHECK(held_decode_callbacks_
.empty());
173 reset_cb_
.RunHeldCallback();
176 void FakeVideoDecoder::SimulateError() {
177 DCHECK(thread_checker_
.CalledOnValidThread());
179 state_
= STATE_ERROR
;
180 while (!held_decode_callbacks_
.empty()) {
181 held_decode_callbacks_
.front().Run(kDecodeError
,
182 scoped_refptr
<VideoFrame
>());
183 held_decode_callbacks_
.pop_front();
185 decoded_frames_
.clear();
188 int FakeVideoDecoder::GetMaxDecodeRequests() const {
189 return max_parallel_decoding_requests_
;
192 void FakeVideoDecoder::OnFrameDecoded(
194 const DecodeCB
& decode_cb
,
196 const scoped_refptr
<VideoFrame
>& video_frame
) {
197 DCHECK(thread_checker_
.CalledOnValidThread());
199 if (status
== kOk
|| status
== kNotEnoughData
)
200 total_bytes_decoded_
+= buffer_size
;
201 decode_cb
.Run(status
, video_frame
);
204 void FakeVideoDecoder::RunOrHoldDecode(const DecodeCB
& decode_cb
) {
205 DCHECK(thread_checker_
.CalledOnValidThread());
208 held_decode_callbacks_
.push_back(decode_cb
);
210 DCHECK(held_decode_callbacks_
.empty());
211 RunDecodeCallback(decode_cb
);
215 void FakeVideoDecoder::RunDecodeCallback(const DecodeCB
& decode_cb
) {
216 DCHECK(thread_checker_
.CalledOnValidThread());
218 if (!reset_cb_
.IsNull()) {
219 DCHECK(decoded_frames_
.empty());
220 decode_cb
.Run(kAborted
, scoped_refptr
<VideoFrame
>());
224 // Make sure we leave decoding_delay_ frames in the queue and also frames for
225 // all pending decode callbacks, except the current one.
226 if (decoded_frames_
.size() <=
227 decoding_delay_
+ held_decode_callbacks_
.size() &&
228 state_
!= STATE_END_OF_STREAM
) {
229 decode_cb
.Run(kNotEnoughData
, scoped_refptr
<VideoFrame
>());
233 scoped_refptr
<VideoFrame
> frame
;
234 if (decoded_frames_
.empty()) {
235 DCHECK_EQ(state_
, STATE_END_OF_STREAM
);
236 frame
= VideoFrame::CreateEOSFrame();
238 frame
= decoded_frames_
.front();
239 decoded_frames_
.pop_front();
241 decode_cb
.Run(kOk
, frame
);
244 void FakeVideoDecoder::DoReset() {
245 DCHECK(thread_checker_
.CalledOnValidThread());
246 DCHECK(held_decode_callbacks_
.empty());
247 DCHECK(!reset_cb_
.IsNull());
249 reset_cb_
.RunOrHold();