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 : task_runner_(base::MessageLoopProxy::current()),
19 decoding_delay_(decoding_delay),
20 supports_get_decode_output_(supports_get_decode_output),
21 state_(UNINITIALIZED),
22 total_bytes_decoded_(0),
24 DCHECK_GE(decoding_delay, 0);
27 FakeVideoDecoder::~FakeVideoDecoder() {
28 DCHECK_EQ(state_, UNINITIALIZED);
31 void FakeVideoDecoder::Initialize(const VideoDecoderConfig& config,
32 const PipelineStatusCB& status_cb) {
33 DCHECK(task_runner_->BelongsToCurrentThread());
34 DCHECK(config.IsValidConfig());
35 DCHECK(decode_cb_.IsNull()) << "No reinitialization during pending decode.";
36 DCHECK(reset_cb_.IsNull()) << "No reinitialization during pending reset.";
38 current_config_ = config;
39 init_cb_.SetCallback(BindToCurrentLoop(status_cb));
41 if (!decoded_frames_.empty()) {
42 DVLOG(1) << "Decoded frames dropped during reinitialization.";
43 decoded_frames_.clear();
47 init_cb_.RunOrHold(PIPELINE_OK);
50 void FakeVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
51 const DecodeCB& decode_cb) {
52 DCHECK(task_runner_->BelongsToCurrentThread());
53 DCHECK(decode_cb_.IsNull()) << "Overlapping decodes are not supported.";
54 DCHECK(reset_cb_.IsNull());
55 DCHECK_LE(decoded_frames_.size(), static_cast<size_t>(decoding_delay_));
57 int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size();
58 decode_cb_.SetCallback(
59 BindToCurrentLoop(base::Bind(&FakeVideoDecoder::OnFrameDecoded,
60 weak_factory_.GetWeakPtr(),
64 if (buffer->end_of_stream() && decoded_frames_.empty()) {
65 decode_cb_.RunOrHold(kOk, VideoFrame::CreateEOSFrame());
69 if (!buffer->end_of_stream()) {
70 DCHECK(VerifyFakeVideoBufferForTest(buffer, current_config_));
71 scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame(
72 current_config_.coded_size(), 0, 0, 0, buffer->timestamp());
73 decoded_frames_.push_back(video_frame);
75 if (decoded_frames_.size() <= static_cast<size_t>(decoding_delay_)) {
76 decode_cb_.RunOrHold(kNotEnoughData, scoped_refptr<VideoFrame>());
81 scoped_refptr<VideoFrame> frame = decoded_frames_.front();
82 decoded_frames_.pop_front();
83 decode_cb_.RunOrHold(kOk, frame);
86 void FakeVideoDecoder::Reset(const base::Closure& closure) {
87 DCHECK(task_runner_->BelongsToCurrentThread());
88 DCHECK(reset_cb_.IsNull());
89 reset_cb_.SetCallback(BindToCurrentLoop(closure));
91 // Defer the reset if a decode is pending.
92 if (!decode_cb_.IsNull())
98 void FakeVideoDecoder::Stop(const base::Closure& closure) {
99 DCHECK(task_runner_->BelongsToCurrentThread());
100 stop_cb_.SetCallback(BindToCurrentLoop(closure));
102 // Defer the stop if an init, a decode or a reset is pending.
103 if (!init_cb_.IsNull() || !decode_cb_.IsNull() || !reset_cb_.IsNull())
109 scoped_refptr<VideoFrame> FakeVideoDecoder::GetDecodeOutput() {
110 DCHECK(task_runner_->BelongsToCurrentThread());
111 if (!supports_get_decode_output_ || decoded_frames_.empty())
113 scoped_refptr<VideoFrame> out = decoded_frames_.front();
114 decoded_frames_.pop_front();
118 void FakeVideoDecoder::HoldNextInit() {
119 DCHECK(task_runner_->BelongsToCurrentThread());
120 init_cb_.HoldCallback();
123 void FakeVideoDecoder::HoldNextRead() {
124 DCHECK(task_runner_->BelongsToCurrentThread());
125 decode_cb_.HoldCallback();
128 void FakeVideoDecoder::HoldNextReset() {
129 DCHECK(task_runner_->BelongsToCurrentThread());
130 reset_cb_.HoldCallback();
133 void FakeVideoDecoder::HoldNextStop() {
134 DCHECK(task_runner_->BelongsToCurrentThread());
135 stop_cb_.HoldCallback();
138 void FakeVideoDecoder::SatisfyInit() {
139 DCHECK(task_runner_->BelongsToCurrentThread());
140 DCHECK(decode_cb_.IsNull());
141 DCHECK(reset_cb_.IsNull());
143 init_cb_.RunHeldCallback();
145 if (!stop_cb_.IsNull())
149 void FakeVideoDecoder::SatisfyRead() {
150 DCHECK(task_runner_->BelongsToCurrentThread());
151 decode_cb_.RunHeldCallback();
153 if (!reset_cb_.IsNull())
156 if (reset_cb_.IsNull() && !stop_cb_.IsNull())
160 void FakeVideoDecoder::SatisfyReset() {
161 DCHECK(task_runner_->BelongsToCurrentThread());
162 DCHECK(decode_cb_.IsNull());
163 reset_cb_.RunHeldCallback();
165 if (!stop_cb_.IsNull())
169 void FakeVideoDecoder::SatisfyStop() {
170 DCHECK(task_runner_->BelongsToCurrentThread());
171 DCHECK(decode_cb_.IsNull());
172 DCHECK(reset_cb_.IsNull());
173 stop_cb_.RunHeldCallback();
176 void FakeVideoDecoder::DoReset() {
177 DCHECK(task_runner_->BelongsToCurrentThread());
178 DCHECK(decode_cb_.IsNull());
179 DCHECK(!reset_cb_.IsNull());
181 decoded_frames_.clear();
182 reset_cb_.RunOrHold();
185 void FakeVideoDecoder::DoStop() {
186 DCHECK(task_runner_->BelongsToCurrentThread());
187 DCHECK(decode_cb_.IsNull());
188 DCHECK(reset_cb_.IsNull());
189 DCHECK(!stop_cb_.IsNull());
191 state_ = UNINITIALIZED;
192 decoded_frames_.clear();
193 stop_cb_.RunOrHold();
196 void FakeVideoDecoder::OnFrameDecoded(
198 const DecodeCB& decode_cb,
200 const scoped_refptr<VideoFrame>& video_frame) {
201 if (status == kOk || status == kNotEnoughData)
202 total_bytes_decoded_ += buffer_size;
203 decode_cb.Run(status, video_frame);