Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(449)

Unified Diff: media/crypto/decrypting_video_decoder.cc

Issue 10969028: Add video decoding methods in Decryptor and add DecryptingVideoDecoder. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/crypto/decrypting_video_decoder.cc
diff --git a/media/crypto/decrypting_video_decoder.cc b/media/crypto/decrypting_video_decoder.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7abb4987b5deb5cebc00ce30f98556f5e3f23c79
--- /dev/null
+++ b/media/crypto/decrypting_video_decoder.cc
@@ -0,0 +1,256 @@
+// Copyright (c) 2012 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/crypto/decrypting_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/decoder_buffer.h"
+#include "media/base/decryptor.h"
+#include "media/base/demuxer_stream.h"
+#include "media/base/pipeline.h"
+#include "media/base/video_decoder_config.h"
+#include "media/base/video_frame.h"
+
+namespace media {
+
+DecryptingVideoDecoder::DecryptingVideoDecoder(
+ const MessageLoopFactoryCB& message_loop_factory_cb,
+ Decryptor* decryptor)
+ : message_loop_factory_cb_(message_loop_factory_cb),
+ message_loop_(NULL),
+ state_(kUninitialized),
+ decryptor_(decryptor) {
+}
+
+void DecryptingVideoDecoder::Initialize(
+ const scoped_refptr<DemuxerStream>& stream,
+ const PipelineStatusCB& status_cb,
+ const StatisticsCB& statistics_cb) {
+ if (!message_loop_) {
+ message_loop_ = base::ResetAndReturn(&message_loop_factory_cb_).Run();
+ message_loop_->PostTask(FROM_HERE, base::Bind(
+ &DecryptingVideoDecoder::Initialize, this,
+ stream, status_cb, statistics_cb));
+ return;
+ }
+
+ DCHECK(!demuxer_stream_);
+ DCHECK(stream);
+ demuxer_stream_ = stream;
+
+ const VideoDecoderConfig& config = demuxer_stream_->video_decoder_config();
+
+ if (!config.IsValidConfig()) {
+ DLOG(ERROR) << "Invalid video stream - " << config.AsHumanReadableString();
+ status_cb.Run(PIPELINE_ERROR_DECODE);
+ return;
+ }
+
+ // DecryptingVideoDecoder only accepts potentially encrypted stream.
+ if (!config.is_encrypted()) {
+ status_cb.Run(DECODER_ERROR_NOT_SUPPORTED);
+ return;
+ }
+
+ status_cb_ = status_cb;
+ statistics_cb_ = statistics_cb;
+
+ decryptor_->InitializeVideoDecoder(
+ config,
+ base::Bind(&DecryptingVideoDecoder::OnDecoderInitDone, this));
+}
+
+void DecryptingVideoDecoder::Read(const ReadCB& read_cb) {
+ // Complete operation asynchronously on different stack of execution as per
+ // the API contract of VideoDecoder::Read()
+ message_loop_->PostTask(FROM_HERE, base::Bind(
+ &DecryptingVideoDecoder::DoRead, this, read_cb));
+}
+
+void DecryptingVideoDecoder::Reset(const base::Closure& closure) {
+ if (!message_loop_->BelongsToCurrentThread()) {
+ message_loop_->PostTask(FROM_HERE, base::Bind(
+ &DecryptingVideoDecoder::Reset, this, closure));
+ return;
+ }
+
+ decryptor_->ResetVideoDecoder();
+ reset_cb_ = closure;
ddorwin 2012/09/21 00:36:08 What happens if Reset() is called twice before the
xhwang 2012/09/25 23:52:32 Good call. I think VideoDecoder interface should d
+
+ // Defer the reset if a read is pending.
+ if (!read_cb_.is_null())
+ return;
+
+ DoReset();
+}
+
+void DecryptingVideoDecoder::Stop(const base::Closure& closure) {
+ if (!message_loop_->BelongsToCurrentThread()) {
+ message_loop_->PostTask(FROM_HERE, base::Bind(
+ &DecryptingVideoDecoder::Stop, this, closure));
+ return;
+ }
+
+ decryptor_->StopVideoDecoder();
+ stop_cb_ = closure;
+
+ // Defer stopping if a read is pending.
+ if (!read_cb_.is_null())
+ return;
+
+ DoStop();
+}
+
+DecryptingVideoDecoder::~DecryptingVideoDecoder() {
+}
+
+void DecryptingVideoDecoder::OnDecoderInitDone(bool success) {
+ if (!message_loop_->BelongsToCurrentThread()) {
+ message_loop_->PostTask(FROM_HERE, base::Bind(
+ &DecryptingVideoDecoder::OnDecoderInitDone, this, success));
+ return;
+ }
+
+ DCHECK(!status_cb_.is_null());
+ if (!success)
+ base::ResetAndReturn(&status_cb_).Run(PIPELINE_ERROR_DECODE);
+
+ // Success!
+ state_ = kNormal;
+ base::ResetAndReturn(&status_cb_).Run(PIPELINE_OK);
+}
+
+void DecryptingVideoDecoder::DoRead(const ReadCB& read_cb) {
+ DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(!read_cb.is_null());
+ CHECK_NE(state_, kUninitialized);
+ CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported.";
+
+ // Return empty frames if decoding has finished.
+ if (state_ == kDecodeFinished) {
+ read_cb.Run(kOk, VideoFrame::CreateEmptyFrame());
+ return;
+ }
+
+ read_cb_ = read_cb;
+ ReadFromDemuxerStream();
+}
+
+void DecryptingVideoDecoder::ReadFromDemuxerStream() {
+ DCHECK_NE(state_, kUninitialized);
+ DCHECK_NE(state_, kDecodeFinished);
+ DCHECK(!read_cb_.is_null());
+
+ demuxer_stream_->Read(
+ base::Bind(&DecryptingVideoDecoder::DecryptAndDecodeBuffer, this));
+}
+
+void DecryptingVideoDecoder::DecryptAndDecodeBuffer(
+ DemuxerStream::Status status,
+ const scoped_refptr<DecoderBuffer>& buffer) {
+ DCHECK_EQ(status != DemuxerStream::kOk, !buffer) << status;
+ // TODO(scherkus): fix FFmpegDemuxerStream::Read() to not execute our read
+ // callback on the same execution stack so we can get rid of forced task post.
+ message_loop_->PostTask(FROM_HERE, base::Bind(
+ &DecryptingVideoDecoder::DoDecryptAndDecodeBuffer, this, status, buffer));
+}
+
+void DecryptingVideoDecoder::DoDecryptAndDecodeBuffer(
+ DemuxerStream::Status status,
+ const scoped_refptr<DecoderBuffer>& buffer) {
+ DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK_NE(state_, kUninitialized);
+ DCHECK_NE(state_, kDecodeFinished);
+ DCHECK(!read_cb_.is_null());
+
+ if (!stop_cb_.is_null()) {
+ base::ResetAndReturn(&read_cb_).Run(kOk, NULL);
+ DoStop();
+ return;
+ }
+
+ if (!reset_cb_.is_null()) {
+ base::ResetAndReturn(&read_cb_).Run(kOk, NULL);
+ DoReset();
+ return;
+ }
+
+ if (status == DemuxerStream::kAborted) {
+ base::ResetAndReturn(&read_cb_).Run(kOk, NULL);
+ return;
+ }
+
+ if (status == DemuxerStream::kConfigChanged) {
+ // TODO(xhwang): Add config change support.
+ base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL);
+ return;
+ }
+
+ DCHECK_EQ(DemuxerStream::kOk, status);
+ DCHECK(buffer);
+
+ decryptor_->DecryptAndDecodeVideo(
+ buffer, base::Bind(&DecryptingVideoDecoder::DeliverFrame, this,
+ buffer->GetDataSize()));
+}
+
+void DecryptingVideoDecoder::DeliverFrame(
+ int buffer_size,
+ Decryptor::Status status,
+ const scoped_refptr<VideoFrame>& frame) {
+ if (!message_loop_->BelongsToCurrentThread()) {
+ message_loop_->PostTask(FROM_HERE, base::Bind(
+ &DecryptingVideoDecoder::DeliverFrame, this,
+ buffer_size, status, frame));
+ return;
+ }
+
+ DCHECK_NE(state_, kUninitialized);
+ DCHECK_NE(state_, kDecodeFinished);
+ DCHECK(!read_cb_.is_null());
+
+ if (status == Decryptor::kNoKey || status == Decryptor::kError) {
+ DCHECK(!frame);
+ state_ = kDecodeFinished;
+ base::ResetAndReturn(&read_cb_).Run(kDecryptError, NULL);
+ return;
+ }
+
+ // The buffer has been accepted by the decoder, let's report statistics.
+ if (buffer_size) {
+ PipelineStatistics statistics;
+ statistics.video_bytes_decoded = buffer_size;
+ statistics_cb_.Run(statistics);
+ }
+
+ if (status == Decryptor::kNeedMoreData) {
+ DCHECK(!frame);
+ ReadFromDemuxerStream();
+ return;
+ }
+
+ DCHECK_EQ(Decryptor::kSuccess, status);
+ DCHECK(frame);
+ if (frame->IsEndOfStream())
+ state_ = kDecodeFinished;
+
+ base::ResetAndReturn(&read_cb_).Run(kOk, frame);
+}
+
+void DecryptingVideoDecoder::DoReset() {
+ DCHECK(read_cb_.is_null());
+ state_ = kNormal;
+ base::ResetAndReturn(&reset_cb_).Run();
+}
+
+void DecryptingVideoDecoder::DoStop() {
+ DCHECK(read_cb_.is_null());
+ state_ = kUninitialized;
+ base::ResetAndReturn(&stop_cb_).Run();
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698