Home | History | Annotate | Download | only in crypto
      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.
      4 
      5 #ifndef CONTENT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_
      6 #define CONTENT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "media/base/decryptor.h"
     15 #include "media/base/media_keys.h"
     16 #include "media/base/video_decoder_config.h"
     17 
     18 namespace base {
     19 class MessageLoopProxy;
     20 }
     21 
     22 namespace content {
     23 class ContentDecryptorDelegate;
     24 class PepperPluginInstanceImpl;
     25 
     26 // PpapiDecryptor implements media::Decryptor and forwards all calls to the
     27 // PluginInstance.
     28 // This class should always be created & destroyed on the main renderer thread.
     29 class PpapiDecryptor : public media::MediaKeys, public media::Decryptor {
     30  public:
     31   static scoped_ptr<PpapiDecryptor> Create(
     32       // TODO(ddorwin): Remove after updating the delegate.
     33       const std::string& key_system,
     34       const scoped_refptr<PepperPluginInstanceImpl>& plugin_instance,
     35       const media::KeyAddedCB& key_added_cb,
     36       const media::KeyErrorCB& key_error_cb,
     37       const media::KeyMessageCB& key_message_cb,
     38       const base::Closure& destroy_plugin_cb);
     39 
     40   virtual ~PpapiDecryptor();
     41 
     42   // media::MediaKeys implementation.
     43   virtual bool GenerateKeyRequest(const std::string& type,
     44                                   const uint8* init_data,
     45                                   int init_data_length) OVERRIDE;
     46   virtual void AddKey(const uint8* key, int key_length,
     47                       const uint8* init_data, int init_data_length,
     48                       const std::string& session_id) OVERRIDE;
     49   virtual void CancelKeyRequest(const std::string& session_id) OVERRIDE;
     50   virtual Decryptor* GetDecryptor() OVERRIDE;
     51 
     52   // media::Decryptor implementation.
     53   virtual void RegisterNewKeyCB(StreamType stream_type,
     54                                 const NewKeyCB& key_added_cb) OVERRIDE;
     55   virtual void Decrypt(StreamType stream_type,
     56                        const scoped_refptr<media::DecoderBuffer>& encrypted,
     57                        const DecryptCB& decrypt_cb) OVERRIDE;
     58   virtual void CancelDecrypt(StreamType stream_type) OVERRIDE;
     59   virtual void InitializeAudioDecoder(const media::AudioDecoderConfig& config,
     60                                       const DecoderInitCB& init_cb) OVERRIDE;
     61   virtual void InitializeVideoDecoder(const media::VideoDecoderConfig& config,
     62                                       const DecoderInitCB& init_cb) OVERRIDE;
     63   virtual void DecryptAndDecodeAudio(
     64       const scoped_refptr<media::DecoderBuffer>& encrypted,
     65       const AudioDecodeCB& audio_decode_cb) OVERRIDE;
     66   virtual void DecryptAndDecodeVideo(
     67       const scoped_refptr<media::DecoderBuffer>& encrypted,
     68       const VideoDecodeCB& video_decode_cb) OVERRIDE;
     69   virtual void ResetDecoder(StreamType stream_type) OVERRIDE;
     70   virtual void DeinitializeDecoder(StreamType stream_type) OVERRIDE;
     71 
     72  private:
     73   PpapiDecryptor(
     74       const scoped_refptr<PepperPluginInstanceImpl>& plugin_instance,
     75       ContentDecryptorDelegate* plugin_cdm_delegate,
     76       const media::KeyAddedCB& key_added_cb,
     77       const media::KeyErrorCB& key_error_cb,
     78       const media::KeyMessageCB& key_message_cb,
     79       const base::Closure& destroy_plugin_cb);
     80 
     81   void ReportFailureToCallPlugin(const std::string& session_id);
     82 
     83   void OnDecoderInitialized(StreamType stream_type, bool success);
     84 
     85   // Callbacks for |plugin_cdm_delegate_| to fire key events.
     86   void KeyAdded(const std::string& session_id);
     87   void KeyError(const std::string& session_id,
     88                 media::MediaKeys::KeyError error_code,
     89                 int system_code);
     90   void KeyMessage(const std::string& session_id,
     91                   const std::vector<uint8>& message,
     92                   const std::string& default_url);
     93 
     94   // Hold a reference of the plugin instance to make sure the plugin outlives
     95   // the |plugin_cdm_delegate_|. This is needed because |plugin_cdm_delegate_|
     96   // is owned by the |plugin_instance_|.
     97   scoped_refptr<PepperPluginInstanceImpl> plugin_instance_;
     98 
     99   ContentDecryptorDelegate* plugin_cdm_delegate_;
    100 
    101   // Callbacks for firing key events.
    102   media::KeyAddedCB key_added_cb_;
    103   media::KeyErrorCB key_error_cb_;
    104   media::KeyMessageCB key_message_cb_;
    105 
    106   // Called to destroy the helper plugin when this class no longer needs it.
    107   base::Closure destroy_plugin_cb_;
    108 
    109   scoped_refptr<base::MessageLoopProxy> render_loop_proxy_;
    110 
    111   DecoderInitCB audio_decoder_init_cb_;
    112   DecoderInitCB video_decoder_init_cb_;
    113   NewKeyCB new_audio_key_cb_;
    114   NewKeyCB new_video_key_cb_;
    115 
    116   base::WeakPtrFactory<PpapiDecryptor> weak_ptr_factory_;
    117   base::WeakPtr<PpapiDecryptor> weak_this_;
    118 
    119   DISALLOW_COPY_AND_ASSIGN(PpapiDecryptor);
    120 };
    121 
    122 }  // namespace content
    123 
    124 #endif  // CONTENT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_
    125