Migrate away from the PrefMetricsService-based device ID in PrefHashCalculator.
[chromium-blink-merge.git] / media / filters / fake_video_decoder.cc
blob05dc410f870bc1a6fad5ddbd7da11f6f7955b191
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 void FakeVideoDecoder::Initialize(const VideoDecoderConfig& config,
44 bool low_delay,
45 const PipelineStatusCB& status_cb,
46 const OutputCB& output_cb) {
47 DCHECK(thread_checker_.CalledOnValidThread());
48 DCHECK(config.IsValidConfig());
49 DCHECK(held_decode_callbacks_.empty())
50 << "No reinitialization during pending decode.";
51 DCHECK(reset_cb_.IsNull()) << "No reinitialization during pending reset.";
53 current_config_ = config;
54 init_cb_.SetCallback(BindToCurrentLoop(status_cb));
56 // Don't need BindToCurrentLoop() because |output_cb_| is only called from
57 // RunDecodeCallback() which is posted from Decode().
58 output_cb_ = output_cb;
60 if (!decoded_frames_.empty()) {
61 DVLOG(1) << "Decoded frames dropped during reinitialization.";
62 decoded_frames_.clear();
65 state_ = STATE_NORMAL;
66 init_cb_.RunOrHold(PIPELINE_OK);
69 void FakeVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
70 const DecodeCB& decode_cb) {
71 DCHECK(thread_checker_.CalledOnValidThread());
72 DCHECK(reset_cb_.IsNull());
73 DCHECK_LE(decoded_frames_.size(),
74 decoding_delay_ + held_decode_callbacks_.size());
75 DCHECK_LT(static_cast<int>(held_decode_callbacks_.size()),
76 max_parallel_decoding_requests_);
78 int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size();
79 DecodeCB wrapped_decode_cb = base::Bind(&FakeVideoDecoder::OnFrameDecoded,
80 weak_factory_.GetWeakPtr(),
81 buffer_size,
82 BindToCurrentLoop(decode_cb));
84 if (state_ == STATE_ERROR) {
85 wrapped_decode_cb.Run(kDecodeError);
86 return;
89 if (buffer->end_of_stream()) {
90 state_ = STATE_END_OF_STREAM;
91 } else {
92 DCHECK(VerifyFakeVideoBufferForTest(buffer, current_config_));
93 scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame(
94 current_config_.coded_size(), 0, 0, 0, buffer->timestamp());
95 decoded_frames_.push_back(video_frame);
98 RunOrHoldDecode(wrapped_decode_cb);
101 void FakeVideoDecoder::Reset(const base::Closure& closure) {
102 DCHECK(thread_checker_.CalledOnValidThread());
103 DCHECK(reset_cb_.IsNull());
105 reset_cb_.SetCallback(BindToCurrentLoop(closure));
106 decoded_frames_.clear();
108 // Defer the reset if a decode is pending.
109 if (!held_decode_callbacks_.empty())
110 return;
112 DoReset();
115 void FakeVideoDecoder::HoldNextInit() {
116 DCHECK(thread_checker_.CalledOnValidThread());
117 init_cb_.HoldCallback();
120 void FakeVideoDecoder::HoldDecode() {
121 DCHECK(thread_checker_.CalledOnValidThread());
122 hold_decode_ = true;
125 void FakeVideoDecoder::HoldNextReset() {
126 DCHECK(thread_checker_.CalledOnValidThread());
127 reset_cb_.HoldCallback();
130 void FakeVideoDecoder::SatisfyInit() {
131 DCHECK(thread_checker_.CalledOnValidThread());
132 DCHECK(held_decode_callbacks_.empty());
133 DCHECK(reset_cb_.IsNull());
135 init_cb_.RunHeldCallback();
138 void FakeVideoDecoder::SatisfyDecode() {
139 DCHECK(thread_checker_.CalledOnValidThread());
140 DCHECK(hold_decode_);
142 hold_decode_ = false;
144 while (!held_decode_callbacks_.empty()) {
145 SatisfySingleDecode();
149 void FakeVideoDecoder::SatisfySingleDecode() {
150 DCHECK(thread_checker_.CalledOnValidThread());
151 DCHECK(!held_decode_callbacks_.empty());
153 DecodeCB decode_cb = held_decode_callbacks_.front();
154 held_decode_callbacks_.pop_front();
155 RunDecodeCallback(decode_cb);
157 if (!reset_cb_.IsNull() && held_decode_callbacks_.empty())
158 DoReset();
161 void FakeVideoDecoder::SatisfyReset() {
162 DCHECK(thread_checker_.CalledOnValidThread());
163 DCHECK(held_decode_callbacks_.empty());
164 reset_cb_.RunHeldCallback();
167 void FakeVideoDecoder::SimulateError() {
168 DCHECK(thread_checker_.CalledOnValidThread());
170 state_ = STATE_ERROR;
171 while (!held_decode_callbacks_.empty()) {
172 held_decode_callbacks_.front().Run(kDecodeError);
173 held_decode_callbacks_.pop_front();
175 decoded_frames_.clear();
178 int FakeVideoDecoder::GetMaxDecodeRequests() const {
179 return max_parallel_decoding_requests_;
182 void FakeVideoDecoder::OnFrameDecoded(int buffer_size,
183 const DecodeCB& decode_cb,
184 Status status) {
185 DCHECK(thread_checker_.CalledOnValidThread());
187 if (status == kOk)
188 total_bytes_decoded_ += buffer_size;
189 decode_cb.Run(status);
192 void FakeVideoDecoder::RunOrHoldDecode(const DecodeCB& decode_cb) {
193 DCHECK(thread_checker_.CalledOnValidThread());
195 if (hold_decode_) {
196 held_decode_callbacks_.push_back(decode_cb);
197 } else {
198 DCHECK(held_decode_callbacks_.empty());
199 RunDecodeCallback(decode_cb);
203 void FakeVideoDecoder::RunDecodeCallback(const DecodeCB& decode_cb) {
204 DCHECK(thread_checker_.CalledOnValidThread());
206 if (!reset_cb_.IsNull()) {
207 DCHECK(decoded_frames_.empty());
208 decode_cb.Run(kAborted);
209 return;
212 // Make sure we leave decoding_delay_ frames in the queue and also frames for
213 // all pending decode callbacks, except the current one.
214 if (decoded_frames_.size() >
215 decoding_delay_ + held_decode_callbacks_.size()) {
216 output_cb_.Run(decoded_frames_.front());
217 decoded_frames_.pop_front();
218 } else if (state_ == STATE_END_OF_STREAM) {
219 // Drain the queue if this was the last request in the stream, otherwise
220 // just pop the last frame from the queue.
221 if (held_decode_callbacks_.empty()) {
222 while (!decoded_frames_.empty()) {
223 output_cb_.Run(decoded_frames_.front());
224 decoded_frames_.pop_front();
226 } else if (!decoded_frames_.empty()) {
227 output_cb_.Run(decoded_frames_.front());
228 decoded_frames_.pop_front();
232 decode_cb.Run(kOk);
235 void FakeVideoDecoder::DoReset() {
236 DCHECK(thread_checker_.CalledOnValidThread());
237 DCHECK(held_decode_callbacks_.empty());
238 DCHECK(!reset_cb_.IsNull());
240 reset_cb_.RunOrHold();
243 } // namespace media