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 
     43   virtual ~PpapiDecryptor();
     44 
     45   // media::MediaKeys implementation.
     46   virtual void CreateSession(
     47       const std::string& init_data_type,
     48       const uint8* init_data,
     49       int init_data_length,
     50       SessionType session_type,
     51       scoped_ptr<media::NewSessionCdmPromise> promise) OVERRIDE;
     52   virtual void LoadSession(
     53       const std::string& web_session_id,
     54       scoped_ptr<media::NewSessionCdmPromise> promise) OVERRIDE;
     55   virtual void UpdateSession(
     56       const std::string& web_session_id,
     57       const uint8* response,
     58       int response_length,
     59       scoped_ptr<media::SimpleCdmPromise> promise) OVERRIDE;
     60   virtual void ReleaseSession(
     61       const std::string& web_session_id,
     62       scoped_ptr<media::SimpleCdmPromise> promise) OVERRIDE;
     63   virtual Decryptor* GetDecryptor() OVERRIDE;
     64 
     65   // media::Decryptor implementation.
     66   virtual void RegisterNewKeyCB(StreamType stream_type,
     67                                 const NewKeyCB& key_added_cb) OVERRIDE;
     68   virtual void Decrypt(StreamType stream_type,
     69                        const scoped_refptr<media::DecoderBuffer>& encrypted,
     70                        const DecryptCB& decrypt_cb) OVERRIDE;
     71   virtual void CancelDecrypt(StreamType stream_type) OVERRIDE;
     72   virtual void InitializeAudioDecoder(const media::AudioDecoderConfig& config,
     73                                       const DecoderInitCB& init_cb) OVERRIDE;
     74   virtual void InitializeVideoDecoder(const media::VideoDecoderConfig& config,
     75                                       const DecoderInitCB& init_cb) OVERRIDE;
     76   virtual void DecryptAndDecodeAudio(
     77       const scoped_refptr<media::DecoderBuffer>& encrypted,
     78       const AudioDecodeCB& audio_decode_cb) OVERRIDE;
     79   virtual void DecryptAndDecodeVideo(
     80       const scoped_refptr<media::DecoderBuffer>& encrypted,
     81       const VideoDecodeCB& video_decode_cb) OVERRIDE;
     82   virtual void ResetDecoder(StreamType stream_type) OVERRIDE;
     83   virtual void DeinitializeDecoder(StreamType stream_type) OVERRIDE;
     84 
     85  private:
     86   PpapiDecryptor(const std::string& key_system,
     87                  scoped_ptr<PepperCdmWrapper> pepper_cdm_wrapper,
     88                  const media::SessionMessageCB& session_message_cb,
     89                  const media::SessionReadyCB& session_ready_cb,
     90                  const media::SessionClosedCB& session_closed_cb,
     91                  const media::SessionErrorCB& session_error_cb);
     92 
     93   void OnDecoderInitialized(StreamType stream_type, bool success);
     94 
     95   // Callbacks for |plugin_cdm_delegate_| to fire session events.
     96   void OnSessionMessage(const std::string& web_session_id,
     97                         const std::vector<uint8>& message,
     98                         const GURL& destination_url);
     99   void OnSessionReady(const std::string& web_session_id);
    100   void OnSessionClosed(const std::string& web_session_id);
    101   void OnSessionError(const std::string& web_session_id,
    102                       MediaKeys::Exception exception_code,
    103                       uint32 system_code,
    104                       const std::string& error_description);
    105 
    106   // On a successful Update() or SessionReady event, trigger playback to resume.
    107   void ResumePlayback();
    108 
    109   // Callback to notify that a fatal error happened in |plugin_cdm_delegate_|.
    110   // The error is terminal and |plugin_cdm_delegate_| should not be used after
    111   // this call.
    112   void OnFatalPluginError();
    113 
    114   ContentDecryptorDelegate* CdmDelegate();
    115 
    116   // Hold a reference of the Pepper CDM wrapper to make sure the plugin lives
    117   // as long as needed.
    118   scoped_ptr<PepperCdmWrapper> pepper_cdm_wrapper_;
    119 
    120   // Callbacks for firing session events.
    121   media::SessionMessageCB session_message_cb_;
    122   media::SessionReadyCB session_ready_cb_;
    123   media::SessionClosedCB session_closed_cb_;
    124   media::SessionErrorCB session_error_cb_;
    125 
    126   scoped_refptr<base::MessageLoopProxy> render_loop_proxy_;
    127 
    128   DecoderInitCB audio_decoder_init_cb_;
    129   DecoderInitCB video_decoder_init_cb_;
    130   NewKeyCB new_audio_key_cb_;
    131   NewKeyCB new_video_key_cb_;
    132 
    133   // NOTE: Weak pointers must be invalidated before all other member variables.
    134   base::WeakPtrFactory<PpapiDecryptor> weak_ptr_factory_;
    135 
    136   DISALLOW_COPY_AND_ASSIGN(PpapiDecryptor);
    137 };
    138 
    139 }  // namespace content
    140 
    141 #endif  // CONTENT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_
    142