Cleanup unused variables in AwContents
[chromium-blink-merge.git] / media / filters / fake_video_decoder.cc
blobf7f1abd8540b6af9ffbc7b22b3fbfefafb918325
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 : 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);
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 PipelineStatusCB& status_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(status_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 state_ = STATE_NORMAL;
70 init_cb_.RunOrHold(PIPELINE_OK);
73 void FakeVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
74 const DecodeCB& decode_cb) {
75 DCHECK(thread_checker_.CalledOnValidThread());
76 DCHECK(reset_cb_.IsNull());
77 DCHECK_LE(decoded_frames_.size(),
78 decoding_delay_ + held_decode_callbacks_.size());
79 DCHECK_LT(static_cast<int>(held_decode_callbacks_.size()),
80 max_parallel_decoding_requests_);
81 DCHECK_NE(state_, STATE_END_OF_STREAM);
83 int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size();
84 DecodeCB wrapped_decode_cb = base::Bind(&FakeVideoDecoder::OnFrameDecoded,
85 weak_factory_.GetWeakPtr(),
86 buffer_size,
87 BindToCurrentLoop(decode_cb));
89 if (state_ == STATE_ERROR) {
90 wrapped_decode_cb.Run(kDecodeError);
91 return;
94 if (buffer->end_of_stream()) {
95 state_ = STATE_END_OF_STREAM;
96 } else {
97 DCHECK(VerifyFakeVideoBufferForTest(buffer, current_config_));
98 scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame(
99 current_config_.coded_size(), 0, 0, 0, buffer->timestamp());
100 decoded_frames_.push_back(video_frame);
103 RunOrHoldDecode(wrapped_decode_cb);
106 void FakeVideoDecoder::Reset(const base::Closure& closure) {
107 DCHECK(thread_checker_.CalledOnValidThread());
108 DCHECK(reset_cb_.IsNull());
110 reset_cb_.SetCallback(BindToCurrentLoop(closure));
111 decoded_frames_.clear();
113 // Defer the reset if a decode is pending.
114 if (!held_decode_callbacks_.empty())
115 return;
117 DoReset();
120 void FakeVideoDecoder::HoldNextInit() {
121 DCHECK(thread_checker_.CalledOnValidThread());
122 init_cb_.HoldCallback();
125 void FakeVideoDecoder::HoldDecode() {
126 DCHECK(thread_checker_.CalledOnValidThread());
127 hold_decode_ = true;
130 void FakeVideoDecoder::HoldNextReset() {
131 DCHECK(thread_checker_.CalledOnValidThread());
132 reset_cb_.HoldCallback();
135 void FakeVideoDecoder::SatisfyInit() {
136 DCHECK(thread_checker_.CalledOnValidThread());
137 DCHECK(held_decode_callbacks_.empty());
138 DCHECK(reset_cb_.IsNull());
140 init_cb_.RunHeldCallback();
143 void FakeVideoDecoder::SatisfyDecode() {
144 DCHECK(thread_checker_.CalledOnValidThread());
145 DCHECK(hold_decode_);
147 hold_decode_ = false;
149 while (!held_decode_callbacks_.empty()) {
150 SatisfySingleDecode();
154 void FakeVideoDecoder::SatisfySingleDecode() {
155 DCHECK(thread_checker_.CalledOnValidThread());
156 DCHECK(!held_decode_callbacks_.empty());
158 DecodeCB decode_cb = held_decode_callbacks_.front();
159 held_decode_callbacks_.pop_front();
160 RunDecodeCallback(decode_cb);
162 if (!reset_cb_.IsNull() && held_decode_callbacks_.empty())
163 DoReset();
166 void FakeVideoDecoder::SatisfyReset() {
167 DCHECK(thread_checker_.CalledOnValidThread());
168 DCHECK(held_decode_callbacks_.empty());
169 reset_cb_.RunHeldCallback();
172 void FakeVideoDecoder::SimulateError() {
173 DCHECK(thread_checker_.CalledOnValidThread());
175 state_ = STATE_ERROR;
176 while (!held_decode_callbacks_.empty()) {
177 held_decode_callbacks_.front().Run(kDecodeError);
178 held_decode_callbacks_.pop_front();
180 decoded_frames_.clear();
183 int FakeVideoDecoder::GetMaxDecodeRequests() const {
184 return max_parallel_decoding_requests_;
187 void FakeVideoDecoder::OnFrameDecoded(int buffer_size,
188 const DecodeCB& decode_cb,
189 Status status) {
190 DCHECK(thread_checker_.CalledOnValidThread());
192 if (status == kOk)
193 total_bytes_decoded_ += buffer_size;
194 decode_cb.Run(status);
197 void FakeVideoDecoder::RunOrHoldDecode(const DecodeCB& decode_cb) {
198 DCHECK(thread_checker_.CalledOnValidThread());
200 if (hold_decode_) {
201 held_decode_callbacks_.push_back(decode_cb);
202 } else {
203 DCHECK(held_decode_callbacks_.empty());
204 RunDecodeCallback(decode_cb);
208 void FakeVideoDecoder::RunDecodeCallback(const DecodeCB& decode_cb) {
209 DCHECK(thread_checker_.CalledOnValidThread());
211 if (!reset_cb_.IsNull()) {
212 DCHECK(decoded_frames_.empty());
213 decode_cb.Run(kAborted);
214 return;
217 // Make sure we leave decoding_delay_ frames in the queue and also frames for
218 // all pending decode callbacks, except the current one.
219 if (decoded_frames_.size() >
220 decoding_delay_ + held_decode_callbacks_.size()) {
221 output_cb_.Run(decoded_frames_.front());
222 decoded_frames_.pop_front();
223 } else if (state_ == STATE_END_OF_STREAM) {
224 // Drain the queue if this was the last request in the stream, otherwise
225 // just pop the last frame from the queue.
226 if (held_decode_callbacks_.empty()) {
227 while (!decoded_frames_.empty()) {
228 output_cb_.Run(decoded_frames_.front());
229 decoded_frames_.pop_front();
231 state_ = STATE_NORMAL;
232 } else if (!decoded_frames_.empty()) {
233 output_cb_.Run(decoded_frames_.front());
234 decoded_frames_.pop_front();
238 decode_cb.Run(kOk);
241 void FakeVideoDecoder::DoReset() {
242 DCHECK(thread_checker_.CalledOnValidThread());
243 DCHECK(held_decode_callbacks_.empty());
244 DCHECK(!reset_cb_.IsNull());
246 reset_cb_.RunOrHold();
249 } // namespace media