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.
5 #ifndef CONTENT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_
6 #define CONTENT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_
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/cdm_context.h"
16 #include "media/base/cdm_factory.h"
17 #include "media/base/decryptor.h"
18 #include "media/base/media_keys.h"
19 #include "media/base/video_decoder_config.h"
24 class SingleThreadTaskRunner
;
28 class ContentDecryptorDelegate
;
29 class PepperPluginInstanceImpl
;
31 // PpapiDecryptor implements media::MediaKeys and media::Decryptor and forwards
32 // all calls to the PluginInstance.
33 // This class should always be created & destroyed on the main renderer thread.
34 class PpapiDecryptor
: public media::MediaKeys
,
35 public media::CdmContext
,
36 public media::Decryptor
{
39 const std::string
& key_system
,
40 const GURL
& security_origin
,
41 bool allow_distinctive_identifier
,
42 bool allow_persistent_state
,
43 const CreatePepperCdmCB
& create_pepper_cdm_cb
,
44 const media::SessionMessageCB
& session_message_cb
,
45 const media::SessionClosedCB
& session_closed_cb
,
46 const media::LegacySessionErrorCB
& legacy_session_error_cb
,
47 const media::SessionKeysChangeCB
& session_keys_change_cb
,
48 const media::SessionExpirationUpdateCB
& session_expiration_update_cb
,
49 const media::CdmCreatedCB
& cdm_created_cb
);
51 ~PpapiDecryptor() override
;
53 // media::MediaKeys implementation.
54 void SetServerCertificate(
55 const std::vector
<uint8_t>& certificate
,
56 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
57 void CreateSessionAndGenerateRequest(
58 SessionType session_type
,
59 media::EmeInitDataType init_data_type
,
60 const std::vector
<uint8_t>& init_data
,
61 scoped_ptr
<media::NewSessionCdmPromise
> promise
) override
;
62 void LoadSession(SessionType session_type
,
63 const std::string
& session_id
,
64 scoped_ptr
<media::NewSessionCdmPromise
> promise
) override
;
65 void UpdateSession(const std::string
& session_id
,
66 const std::vector
<uint8_t>& response
,
67 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
68 void CloseSession(const std::string
& session_id
,
69 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
70 void RemoveSession(const std::string
& session_id
,
71 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
72 CdmContext
* GetCdmContext() override
;
74 // media::CdmContext implementation.
75 Decryptor
* GetDecryptor() override
;
76 int GetCdmId() const override
;
78 // media::Decryptor implementation.
79 void RegisterNewKeyCB(StreamType stream_type
,
80 const NewKeyCB
& key_added_cb
) override
;
81 void Decrypt(StreamType stream_type
,
82 const scoped_refptr
<media::DecoderBuffer
>& encrypted
,
83 const DecryptCB
& decrypt_cb
) override
;
84 void CancelDecrypt(StreamType stream_type
) override
;
85 void InitializeAudioDecoder(const media::AudioDecoderConfig
& config
,
86 const DecoderInitCB
& init_cb
) override
;
87 void InitializeVideoDecoder(const media::VideoDecoderConfig
& config
,
88 const DecoderInitCB
& init_cb
) override
;
89 void DecryptAndDecodeAudio(
90 const scoped_refptr
<media::DecoderBuffer
>& encrypted
,
91 const AudioDecodeCB
& audio_decode_cb
) override
;
92 void DecryptAndDecodeVideo(
93 const scoped_refptr
<media::DecoderBuffer
>& encrypted
,
94 const VideoDecodeCB
& video_decode_cb
) override
;
95 void ResetDecoder(StreamType stream_type
) override
;
96 void DeinitializeDecoder(StreamType stream_type
) override
;
100 scoped_ptr
<PepperCdmWrapper
> pepper_cdm_wrapper
,
101 const media::SessionMessageCB
& session_message_cb
,
102 const media::SessionClosedCB
& session_closed_cb
,
103 const media::LegacySessionErrorCB
& legacy_session_error_cb
,
104 const media::SessionKeysChangeCB
& session_keys_change_cb
,
105 const media::SessionExpirationUpdateCB
& session_expiration_update_cb
);
107 void InitializeCdm(const std::string
& key_system
,
108 bool allow_distinctive_identifier
,
109 bool allow_persistent_state
,
110 scoped_ptr
<media::SimpleCdmPromise
> promise
);
112 void OnDecoderInitialized(StreamType stream_type
, bool success
);
114 // Callbacks for |plugin_cdm_delegate_| to fire session events.
115 void OnSessionMessage(const std::string
& session_id
,
116 MediaKeys::MessageType message_type
,
117 const std::vector
<uint8_t>& message
,
118 const GURL
& legacy_destination_url
);
119 void OnSessionKeysChange(const std::string
& session_id
,
120 bool has_additional_usable_key
,
121 media::CdmKeysInfo keys_info
);
122 void OnSessionExpirationUpdate(const std::string
& session_id
,
123 const base::Time
& new_expiry_time
);
124 void OnSessionClosed(const std::string
& session_id
);
125 void OnLegacySessionError(const std::string
& session_id
,
126 MediaKeys::Exception exception_code
,
127 uint32_t system_code
,
128 const std::string
& error_description
);
130 void AttemptToResumePlayback();
132 // Callback to notify that a fatal error happened in |plugin_cdm_delegate_|.
133 // The error is terminal and |plugin_cdm_delegate_| should not be used after
135 void OnFatalPluginError();
137 ContentDecryptorDelegate
* CdmDelegate();
139 // Hold a reference of the Pepper CDM wrapper to make sure the plugin lives
140 // as long as needed.
141 scoped_ptr
<PepperCdmWrapper
> pepper_cdm_wrapper_
;
143 // Callbacks for firing session events.
144 media::SessionMessageCB session_message_cb_
;
145 media::SessionClosedCB session_closed_cb_
;
146 media::LegacySessionErrorCB legacy_session_error_cb_
;
147 media::SessionKeysChangeCB session_keys_change_cb_
;
148 media::SessionExpirationUpdateCB session_expiration_update_cb_
;
150 scoped_refptr
<base::SingleThreadTaskRunner
> render_task_runner_
;
152 DecoderInitCB audio_decoder_init_cb_
;
153 DecoderInitCB video_decoder_init_cb_
;
154 NewKeyCB new_audio_key_cb_
;
155 NewKeyCB new_video_key_cb_
;
157 // NOTE: Weak pointers must be invalidated before all other member variables.
158 base::WeakPtrFactory
<PpapiDecryptor
> weak_ptr_factory_
;
160 DISALLOW_COPY_AND_ASSIGN(PpapiDecryptor
);
163 } // namespace content
165 #endif // CONTENT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_