Chromium Code Reviews| Index: media/filters/fake_video_decoder.cc |
| diff --git a/media/filters/fake_video_decoder.cc b/media/filters/fake_video_decoder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0c81214e96d274a2d2397bd15a96174c5c84a42a |
| --- /dev/null |
| +++ b/media/filters/fake_video_decoder.cc |
| @@ -0,0 +1,216 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/filters/fake_video_decoder.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/callback_helpers.h" |
| +#include "base/location.h" |
| +#include "base/message_loop_proxy.h" |
| +#include "media/base/bind_to_loop.h" |
| +#include "media/base/demuxer_stream.h" |
| + |
| +namespace media { |
| + |
| +FakeVideoDecoder::FakeVideoDecoder(int decoding_delay) |
| + : message_loop_(base::MessageLoopProxy::current()), |
| + weak_factory_(this), |
| + decoding_delay_(decoding_delay), |
| + state_(UNINITIALIZED), |
| + demuxer_stream_(NULL) { |
| + DCHECK_GE(decoding_delay, 0); |
| +} |
| + |
| +FakeVideoDecoder::~FakeVideoDecoder() { |
| + DCHECK_EQ(state_, UNINITIALIZED); |
| +} |
| + |
| +void FakeVideoDecoder::Initialize(DemuxerStream* stream, |
| + const PipelineStatusCB& status_cb, |
| + const StatisticsCB& statistics_cb) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK(stream); |
| + DCHECK(stream->video_decoder_config().IsValidConfig()); |
| + DCHECK(read_cb_.is_null()) << "No reinitialization during pending read."; |
| + DCHECK(reset_cb_.is_null()) << "No reinitialization during pending reset."; |
| + |
| + weak_this_ = weak_factory_.GetWeakPtr(); |
| + |
| + demuxer_stream_ = stream; |
| + statistics_cb_ = statistics_cb; |
| + current_config_ = stream->video_decoder_config(); |
| + |
| + if (!decoded_frames_.empty()) { |
| + LOG(INFO) << "Decoded frames dropped during reinitialization."; |
|
scherkus (not reviewing)
2013/05/09 20:07:09
does this have to be LOG(INFO)?
xhwang
2013/05/22 04:29:42
Done.
|
| + decoded_frames_.clear(); |
| + } |
| + |
| + state_ = NORMAL; |
| + // TODO(xhwang): Make initialization callback holdable using CallbackHolder. |
| + message_loop_->PostTask(FROM_HERE, base::Bind(status_cb, PIPELINE_OK)); |
| +} |
| + |
| +void FakeVideoDecoder::Read(const ReadCB& read_cb) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; |
| + DCHECK(reset_cb_.is_null()); |
| + DCHECK_LE(decoded_frames_.size(), static_cast<size_t>(decoding_delay_)); |
| + |
| + read_cb_.SetCallback(BindToCurrentLoop(read_cb)); |
| + ReadFromDemuxerStream(); |
| +} |
| + |
| +void FakeVideoDecoder::Reset(const base::Closure& closure) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK(reset_cb_.is_null()); |
| + reset_cb_.SetCallback(BindToCurrentLoop(closure)); |
| + |
| + // Defer the reset if a read is pending. |
| + if (!read_cb_.is_null()) |
| + return; |
| + |
| + DoReset(); |
| +} |
| + |
| +void FakeVideoDecoder::Stop(const base::Closure& closure) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + stop_cb_.SetCallback(BindToCurrentLoop(closure)); |
| + |
| + // Defer the reset if a read and/or a reset is pending. |
| + if (!read_cb_.is_null() || !reset_cb_.is_null()) |
| + return; |
| + |
| + DoStop(); |
| +} |
| + |
| +void FakeVideoDecoder::HoldNextRead() { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + read_cb_.HoldCallback(); |
| +} |
| + |
| +void FakeVideoDecoder::HoldNextReset() { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + reset_cb_.HoldCallback(); |
| +} |
| + |
| +void FakeVideoDecoder::HoldNextStop() { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + stop_cb_.HoldCallback(); |
| +} |
| + |
| +void FakeVideoDecoder::SatisfyRead() { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + read_cb_.Release(); |
| + |
| + if (!reset_cb_.is_null()) |
| + DoReset(); |
| + |
| + if (reset_cb_.is_null() && !stop_cb_.is_null()) |
| + DoStop(); |
| +} |
| + |
| +void FakeVideoDecoder::SatisfyReset() { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK(read_cb_.is_null()); |
| + reset_cb_.Release(); |
| + |
| + if (!stop_cb_.is_null()) |
| + DoStop(); |
| +} |
| + |
| +void FakeVideoDecoder::SatisfyStop() { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK(read_cb_.is_null()); |
| + DCHECK(reset_cb_.is_null()); |
| + stop_cb_.Release(); |
| +} |
| + |
| +void FakeVideoDecoder::ReadFromDemuxerStream() { |
| + DCHECK_EQ(state_, NORMAL); |
| + DCHECK(!read_cb_.is_null()); |
| + demuxer_stream_->Read(base::Bind(&FakeVideoDecoder::BufferReady, weak_this_)); |
| +} |
| + |
| +void FakeVideoDecoder::BufferReady(DemuxerStream::Status status, |
| + const scoped_refptr<DecoderBuffer>& buffer) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK_EQ(state_, NORMAL); |
| + DCHECK(!read_cb_.is_null()); |
| + DCHECK_EQ(status != DemuxerStream::kOk, !buffer) << status; |
| + |
| + if (!stop_cb_.is_null()) { |
| + read_cb_.Run(kOk, scoped_refptr<VideoFrame>()); |
| + if (!reset_cb_.is_null()) { |
| + DoReset(); |
| + } |
| + DoStop(); |
| + return; |
| + } |
| + |
| + if (status == DemuxerStream::kConfigChanged) { |
| + DCHECK(demuxer_stream_->video_decoder_config().IsValidConfig()); |
| + current_config_ = demuxer_stream_->video_decoder_config(); |
| + |
| + if (reset_cb_.is_null()) { |
| + ReadFromDemuxerStream(); |
| + return; |
| + } |
| + } |
| + |
| + if (!reset_cb_.is_null()) { |
| + read_cb_.Run(kOk, scoped_refptr<VideoFrame>()); |
| + DoReset(); |
| + return; |
| + } |
| + |
| + if (status == DemuxerStream::kAborted) { |
| + read_cb_.Run(kOk, scoped_refptr<VideoFrame>()); |
| + return; |
| + } |
| + |
| + DCHECK_EQ(status, DemuxerStream::kOk); |
| + |
| + if (buffer->IsEndOfStream() && decoded_frames_.empty()) { |
| + read_cb_.Run(kOk, VideoFrame::CreateEmptyFrame()); |
| + return; |
| + } |
| + |
| + if (!buffer->IsEndOfStream()) { |
| + scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame( |
| + current_config_.coded_size(), 0, 0, 0, buffer->GetTimestamp()); |
| + decoded_frames_.push_back(video_frame); |
| + |
| + if (decoded_frames_.size() <= static_cast<size_t>(decoding_delay_)) { |
| + ReadFromDemuxerStream(); |
| + return; |
| + } |
| + } |
| + |
| + scoped_refptr<VideoFrame> frame = decoded_frames_.front(); |
| + decoded_frames_.pop_front(); |
| + read_cb_.Run(kOk, frame); |
| +} |
| + |
| +void FakeVideoDecoder::DoReset() { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK(read_cb_.is_null()); |
| + DCHECK(!reset_cb_.is_null()); |
| + |
| + decoded_frames_.clear(); |
| + reset_cb_.Run(); |
| +} |
| + |
| +void FakeVideoDecoder::DoStop() { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK(read_cb_.is_null()); |
| + DCHECK(reset_cb_.is_null()); |
| + DCHECK(!stop_cb_.is_null()); |
| + |
| + state_ = UNINITIALIZED; |
| + demuxer_stream_ = NULL; |
| + decoded_frames_.clear(); |
| + stop_cb_.Run(); |
| +} |
| + |
| +} // namespace media |