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 "content/renderer/media/crypto/pepper_cdm_wrapper.h"
     15 #include "media/base/decryptor.h"
     16 #include "media/base/media_keys.h"
     17 #include "media/base/video_decoder_config.h"
     18 
     19 class GURL;
     20 
     21 namespace base {
     22 class MessageLoopProxy;
     23 }
     24 
     25 namespace content {
     26 class ContentDecryptorDelegate;
     27 class PepperPluginInstanceImpl;
     28 
     29 // PpapiDecryptor implements media::MediaKeys and media::Decryptor and forwards
     30 // all calls to the PluginInstance.
     31 // This class should always be created & destroyed on the main renderer thread.
     32 class PpapiDecryptor : public media::MediaKeys, public media::Decryptor {
     33  public:
     34   static scoped_ptr<PpapiDecryptor> Create(
     35       const std::string& key_system,
     36       const GURL& security_origin,
     37       const CreatePepperCdmCB& create_pepper_cdm_cb,
     38       const media::SessionMessageCB& session_message_cb,
     39       const media::SessionReadyCB& session_ready_cb,
     40       const media::SessionClosedCB& session_closed_cb,
     41       const media::SessionErrorCB& session_error_cb,
     42       const media::SessionKeysChangeCB& session_keys_change_cb,
     43       const media::SessionExpirationUpdateCB& session_expiration_update_cb);
     44 
     45   virtual ~PpapiDecryptor();
     46 
     47   // media::MediaKeys implementation.
     48   virtual void SetServerCertificate(
     49       const uint8* certificate_data,
     50       int certificate_data_length,
     51       scoped_ptr<media::SimpleCdmPromise> promise) OVERRIDE;
     52   virtual void CreateSession(
     53       const std::string& init_data_type,
     54       const uint8* init_data,
     55       int init_data_length,
     56       SessionType session_type,
     57       scoped_ptr<media::NewSessionCdmPromise> promise) OVERRIDE;
     58   virtual void LoadSession(
     59       const std::string& web_session_id,
     60       scoped_ptr<media::NewSessionCdmPromise> promise) OVERRIDE;
     61   virtual void UpdateSession(
     62       const std::string& web_session_id,
     63       const uint8* response,
     64       int response_length,
     65       scoped_ptr<media::SimpleCdmPromise> promise) OVERRIDE;
     66   virtual void CloseSession(
     67       const std::string& web_session_id,
     68       scoped_ptr<media::SimpleCdmPromise> promise) OVERRIDE;
     69   virtual void RemoveSession(
     70       const std::string& web_session_id,
     71       scoped_ptr<media::SimpleCdmPromise> promise) OVERRIDE;
     72   virtual void GetUsableKeyIds(
     73       const std::string& web_session_id,
     74       scoped_ptr<media::KeyIdsPromise> promise) OVERRIDE;
     75   virtual Decryptor* GetDecryptor() OVERRIDE;
     76 
     77   // media::Decryptor implementation.
     78   virtual void RegisterNewKeyCB(StreamType stream_type,
     79                                 const NewKeyCB& key_added_cb) OVERRIDE;
     80   virtual void Decrypt(StreamType stream_type,
     81                        const scoped_refptr<media::DecoderBuffer>& encrypted,
     82                        const DecryptCB& decrypt_cb) OVERRIDE;
     83   virtual void CancelDecrypt(StreamType stream_type) OVERRIDE;
     84   virtual void InitializeAudioDecoder(const media::AudioDecoderConfig& config,
     85                                       const DecoderInitCB& init_cb) OVERRIDE;
     86   virtual void InitializeVideoDecoder(const media::VideoDecoderConfig& config,
     87                                       const DecoderInitCB& init_cb) OVERRIDE;
     88   virtual void DecryptAndDecodeAudio(
     89       const scoped_refptr<media::DecoderBuffer>& encrypted,
     90       const AudioDecodeCB& audio_decode_cb) OVERRIDE;
     91   virtual void DecryptAndDecodeVideo(
     92       const scoped_refptr<media::DecoderBuffer>& encrypted,
     93       const VideoDecodeCB& video_decode_cb) OVERRIDE;
     94   virtual void ResetDecoder(StreamType stream_type) OVERRIDE;
     95   virtual void DeinitializeDecoder(StreamType stream_type) OVERRIDE;
     96 
     97  private:
     98   PpapiDecryptor(
     99       const std::string& key_system,
    100       scoped_ptr<PepperCdmWrapper> pepper_cdm_wrapper,
    101       const media::SessionMessageCB& session_message_cb,
    102       const media::SessionReadyCB& session_ready_cb,
    103       const media::SessionClosedCB& session_closed_cb,
    104       const media::SessionErrorCB& session_error_cb,
    105       const media::SessionKeysChangeCB& session_keys_change_cb,
    106       const media::SessionExpirationUpdateCB& session_expiration_update_cb);
    107 
    108   void OnDecoderInitialized(StreamType stream_type, bool success);
    109 
    110   // Callbacks for |plugin_cdm_delegate_| to fire session events.
    111   void OnSessionMessage(const std::string& web_session_id,
    112                         const std::vector<uint8>& message,
    113                         const GURL& destination_url);
    114   void OnSessionKeysChange(const std::string& web_session_id,
    115                            bool has_additional_usable_key);
    116   void OnSessionExpirationUpdate(const std::string& web_session_id,
    117                                  const base::Time& new_expiry_time);
    118   void OnSessionReady(const std::string& web_session_id);
    119   void OnSessionClosed(const std::string& web_session_id);
    120   void OnSessionError(const std::string& web_session_id,
    121                       MediaKeys::Exception exception_code,
    122                       uint32 system_code,
    123                       const std::string& error_description);
    124 
    125   // On a successful Update() or SessionReady event, trigger playback to resume.
    126   void ResumePlayback();
    127 
    128   // Callback to notify that a fatal error happened in |plugin_cdm_delegate_|.
    129   // The error is terminal and |plugin_cdm_delegate_| should not be used after
    130   // this call.
    131   void OnFatalPluginError();
    132 
    133   ContentDecryptorDelegate* CdmDelegate();
    134 
    135   // Hold a reference of the Pepper CDM wrapper to make sure the plugin lives
    136   // as long as needed.
    137   scoped_ptr<PepperCdmWrapper> pepper_cdm_wrapper_;
    138 
    139   // Callbacks for firing session events.
    140   media::SessionMessageCB session_message_cb_;
    141   media::SessionReadyCB session_ready_cb_;
    142   media::SessionClosedCB session_closed_cb_;
    143   media::SessionErrorCB session_error_cb_;
    144   media::SessionKeysChangeCB session_keys_change_cb_;
    145   media::SessionExpirationUpdateCB session_expiration_update_cb_;
    146 
    147   scoped_refptr<base::MessageLoopProxy> render_loop_proxy_;
    148 
    149   DecoderInitCB audio_decoder_init_cb_;
    150   DecoderInitCB video_decoder_init_cb_;
    151   NewKeyCB new_audio_key_cb_;
    152   NewKeyCB new_video_key_cb_;
    153 
    154   // NOTE: Weak pointers must be invalidated before all other member variables.
    155   base::WeakPtrFactory<PpapiDecryptor> weak_ptr_factory_;
    156 
    157   DISALLOW_COPY_AND_ASSIGN(PpapiDecryptor);
    158 };
    159 
    160 }  // namespace content
    161 
    162 #endif  // CONTENT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_
    163