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.
4
5 #include "media/filters/fake_video_decoder.h"
6
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"
13
14 namespace media {
15
FakeVideoDecoder(int decoding_delay,int max_parallel_decoding_requests)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),
21 hold_decode_(false),
22 total_bytes_decoded_(0),
23 weak_factory_(this) {
24 DCHECK_GE(decoding_delay, 0);
25 }
26
~FakeVideoDecoder()27 FakeVideoDecoder::~FakeVideoDecoder() {
28 DCHECK_EQ(state_, STATE_UNINITIALIZED);
29 }
30
Initialize(const VideoDecoderConfig & config,bool low_delay,const PipelineStatusCB & status_cb,const OutputCB & output_cb)31 void FakeVideoDecoder::Initialize(const VideoDecoderConfig& config,
32 bool low_delay,
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.";
40
41 current_config_ = config;
42 init_cb_.SetCallback(BindToCurrentLoop(status_cb));
43
44 // Don't need BindToCurrentLoop() because |output_cb_| is only called from
45 // RunDecodeCallback() which is posted from Decode().
46 output_cb_ = output_cb;
47
48 if (!decoded_frames_.empty()) {
49 DVLOG(1) << "Decoded frames dropped during reinitialization.";
50 decoded_frames_.clear();
51 }
52
53 state_ = STATE_NORMAL;
54 init_cb_.RunOrHold(PIPELINE_OK);
55 }
56
Decode(const scoped_refptr<DecoderBuffer> & buffer,const DecodeCB & decode_cb)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_);
65
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));
71
72 if (state_ == STATE_ERROR) {
73 wrapped_decode_cb.Run(kDecodeError);
74 return;
75 }
76
77 if (buffer->end_of_stream()) {
78 state_ = STATE_END_OF_STREAM;
79 } else {
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);
84 }
85
86 RunOrHoldDecode(wrapped_decode_cb);
87 }
88
Reset(const base::Closure & closure)89 void FakeVideoDecoder::Reset(const base::Closure& closure) {
90 DCHECK(thread_checker_.CalledOnValidThread());
91 DCHECK(reset_cb_.IsNull());
92
93 reset_cb_.SetCallback(BindToCurrentLoop(closure));
94 decoded_frames_.clear();
95
96 // Defer the reset if a decode is pending.
97 if (!held_decode_callbacks_.empty())
98 return;
99
100 DoReset();
101 }
102
Stop()103 void FakeVideoDecoder::Stop() {
104 DCHECK(thread_checker_.CalledOnValidThread());
105
106 if (!init_cb_.IsNull())
107 SatisfyInit();
108 if (!held_decode_callbacks_.empty())
109 SatisfyDecode();
110 if (!reset_cb_.IsNull())
111 SatisfyReset();
112
113 decoded_frames_.clear();
114 state_ = STATE_UNINITIALIZED;
115 }
116
HoldNextInit()117 void FakeVideoDecoder::HoldNextInit() {
118 DCHECK(thread_checker_.CalledOnValidThread());
119 init_cb_.HoldCallback();
120 }
121
HoldDecode()122 void FakeVideoDecoder::HoldDecode() {
123 DCHECK(thread_checker_.CalledOnValidThread());
124 hold_decode_ = true;
125 }
126
HoldNextReset()127 void FakeVideoDecoder::HoldNextReset() {
128 DCHECK(thread_checker_.CalledOnValidThread());
129 reset_cb_.HoldCallback();
130 }
131
SatisfyInit()132 void FakeVideoDecoder::SatisfyInit() {
133 DCHECK(thread_checker_.CalledOnValidThread());
134 DCHECK(held_decode_callbacks_.empty());
135 DCHECK(reset_cb_.IsNull());
136
137 init_cb_.RunHeldCallback();
138 }
139
SatisfyDecode()140 void FakeVideoDecoder::SatisfyDecode() {
141 DCHECK(thread_checker_.CalledOnValidThread());
142 DCHECK(hold_decode_);
143
144 hold_decode_ = false;
145
146 while (!held_decode_callbacks_.empty()) {
147 SatisfySingleDecode();
148 }
149 }
150
SatisfySingleDecode()151 void FakeVideoDecoder::SatisfySingleDecode() {
152 DCHECK(thread_checker_.CalledOnValidThread());
153 DCHECK(!held_decode_callbacks_.empty());
154
155 DecodeCB decode_cb = held_decode_callbacks_.front();
156 held_decode_callbacks_.pop_front();
157 RunDecodeCallback(decode_cb);
158
159 if (!reset_cb_.IsNull() && held_decode_callbacks_.empty())
160 DoReset();
161 }
162
SatisfyReset()163 void FakeVideoDecoder::SatisfyReset() {
164 DCHECK(thread_checker_.CalledOnValidThread());
165 DCHECK(held_decode_callbacks_.empty());
166 reset_cb_.RunHeldCallback();
167 }
168
SimulateError()169 void FakeVideoDecoder::SimulateError() {
170 DCHECK(thread_checker_.CalledOnValidThread());
171
172 state_ = STATE_ERROR;
173 while (!held_decode_callbacks_.empty()) {
174 held_decode_callbacks_.front().Run(kDecodeError);
175 held_decode_callbacks_.pop_front();
176 }
177 decoded_frames_.clear();
178 }
179
GetMaxDecodeRequests() const180 int FakeVideoDecoder::GetMaxDecodeRequests() const {
181 return max_parallel_decoding_requests_;
182 }
183
OnFrameDecoded(int buffer_size,const DecodeCB & decode_cb,Status status)184 void FakeVideoDecoder::OnFrameDecoded(int buffer_size,
185 const DecodeCB& decode_cb,
186 Status status) {
187 DCHECK(thread_checker_.CalledOnValidThread());
188
189 if (status == kOk)
190 total_bytes_decoded_ += buffer_size;
191 decode_cb.Run(status);
192 }
193
RunOrHoldDecode(const DecodeCB & decode_cb)194 void FakeVideoDecoder::RunOrHoldDecode(const DecodeCB& decode_cb) {
195 DCHECK(thread_checker_.CalledOnValidThread());
196
197 if (hold_decode_) {
198 held_decode_callbacks_.push_back(decode_cb);
199 } else {
200 DCHECK(held_decode_callbacks_.empty());
201 RunDecodeCallback(decode_cb);
202 }
203 }
204
RunDecodeCallback(const DecodeCB & decode_cb)205 void FakeVideoDecoder::RunDecodeCallback(const DecodeCB& decode_cb) {
206 DCHECK(thread_checker_.CalledOnValidThread());
207
208 if (!reset_cb_.IsNull()) {
209 DCHECK(decoded_frames_.empty());
210 decode_cb.Run(kAborted);
211 return;
212 }
213
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();
227 }
228 } else if (!decoded_frames_.empty()) {
229 output_cb_.Run(decoded_frames_.front());
230 decoded_frames_.pop_front();
231 }
232 }
233
234 decode_cb.Run(kOk);
235 }
236
DoReset()237 void FakeVideoDecoder::DoReset() {
238 DCHECK(thread_checker_.CalledOnValidThread());
239 DCHECK(held_decode_callbacks_.empty());
240 DCHECK(!reset_cb_.IsNull());
241
242 reset_cb_.RunOrHold();
243 }
244
245 } // namespace media
246