1// Copyright (c) 2012 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. 4 5#ifndef MEDIA_FILTERS_DECRYPTING_VIDEO_DECODER_H_ 6#define MEDIA_FILTERS_DECRYPTING_VIDEO_DECODER_H_ 7 8#include "base/callback.h" 9#include "base/memory/weak_ptr.h" 10#include "media/base/decryptor.h" 11#include "media/base/video_decoder.h" 12#include "media/base/video_decoder_config.h" 13 14namespace base { 15class SingleThreadTaskRunner; 16} 17 18namespace media { 19 20class DecoderBuffer; 21class Decryptor; 22 23// Decryptor-based VideoDecoder implementation that can decrypt and decode 24// encrypted video buffers and return decrypted and decompressed video frames. 25// All public APIs and callbacks are trampolined to the |task_runner_| so 26// that no locks are required for thread safety. 27class MEDIA_EXPORT DecryptingVideoDecoder : public VideoDecoder { 28 public: 29 DecryptingVideoDecoder( 30 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 31 const SetDecryptorReadyCB& set_decryptor_ready_cb); 32 virtual ~DecryptingVideoDecoder(); 33 34 // VideoDecoder implementation. 35 virtual std::string GetDisplayName() const OVERRIDE; 36 virtual void Initialize(const VideoDecoderConfig& config, 37 bool low_delay, 38 const PipelineStatusCB& status_cb, 39 const OutputCB& output_cb) OVERRIDE; 40 virtual void Decode(const scoped_refptr<DecoderBuffer>& buffer, 41 const DecodeCB& decode_cb) OVERRIDE; 42 virtual void Reset(const base::Closure& closure) OVERRIDE; 43 44 private: 45 // For a detailed state diagram please see this link: http://goo.gl/8jAok 46 // TODO(xhwang): Add a ASCII state diagram in this file after this class 47 // stabilizes. 48 enum State { 49 kUninitialized = 0, 50 kDecryptorRequested, 51 kPendingDecoderInit, 52 kIdle, 53 kPendingDecode, 54 kWaitingForKey, 55 kDecodeFinished, 56 kError 57 }; 58 59 // Callback for DecryptorHost::RequestDecryptor(). |decryptor_attached_cb| is 60 // called when the decryptor has been completely attached to the pipeline. 61 void SetDecryptor(Decryptor* decryptor, 62 const DecryptorAttachedCB& decryptor_attached_cb); 63 64 // Callback for Decryptor::InitializeVideoDecoder() during initialization. 65 void FinishInitialization(bool success); 66 67 void DecodePendingBuffer(); 68 69 // Callback for Decryptor::DecryptAndDecodeVideo(). 70 void DeliverFrame(int buffer_size, 71 Decryptor::Status status, 72 const scoped_refptr<VideoFrame>& frame); 73 74 // Callback for the |decryptor_| to notify this object that a new key has been 75 // added. 76 void OnKeyAdded(); 77 78 // Reset decoder and call |reset_cb_|. 79 void DoReset(); 80 81 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 82 83 State state_; 84 85 PipelineStatusCB init_cb_; 86 OutputCB output_cb_; 87 DecodeCB decode_cb_; 88 base::Closure reset_cb_; 89 90 VideoDecoderConfig config_; 91 92 // Callback to request/cancel decryptor creation notification. 93 SetDecryptorReadyCB set_decryptor_ready_cb_; 94 95 Decryptor* decryptor_; 96 97 // The buffer that needs decrypting/decoding. 98 scoped_refptr<media::DecoderBuffer> pending_buffer_to_decode_; 99 100 // Indicates the situation where new key is added during pending decode 101 // (in other words, this variable can only be set in state kPendingDecode). 102 // If this variable is true and kNoKey is returned then we need to try 103 // decrypting/decoding again in case the newly added key is the correct 104 // decryption key. 105 bool key_added_while_decode_pending_; 106 107 // A unique ID to trace Decryptor::DecryptAndDecodeVideo() call and the 108 // matching DecryptCB call (in DoDeliverFrame()). 109 uint32 trace_id_; 110 111 // NOTE: Weak pointers must be invalidated before all other member variables. 112 base::WeakPtrFactory<DecryptingVideoDecoder> weak_factory_; 113 base::WeakPtr<DecryptingVideoDecoder> weak_this_; 114 115 DISALLOW_COPY_AND_ASSIGN(DecryptingVideoDecoder); 116}; 117 118} // namespace media 119 120#endif // MEDIA_FILTERS_DECRYPTING_VIDEO_DECODER_H_ 121