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/decryptor.h"
17 #include "media/base/media_keys.h"
18 #include "media/base/video_decoder_config.h"
23 class MessageLoopProxy
;
27 class ContentDecryptorDelegate
;
28 class PepperPluginInstanceImpl
;
30 // PpapiDecryptor implements media::MediaKeys and media::Decryptor and forwards
31 // all calls to the PluginInstance.
32 // This class should always be created & destroyed on the main renderer thread.
33 class PpapiDecryptor
: public media::MediaKeys
,
34 public media::CdmContext
,
35 public media::Decryptor
{
37 static scoped_ptr
<PpapiDecryptor
> Create(
38 const std::string
& key_system
,
39 const GURL
& security_origin
,
40 const CreatePepperCdmCB
& create_pepper_cdm_cb
,
41 const media::SessionMessageCB
& session_message_cb
,
42 const media::SessionClosedCB
& session_closed_cb
,
43 const media::SessionErrorCB
& session_error_cb
,
44 const media::SessionKeysChangeCB
& session_keys_change_cb
,
45 const media::SessionExpirationUpdateCB
& session_expiration_update_cb
);
47 ~PpapiDecryptor() override
;
49 // media::MediaKeys implementation.
50 void SetServerCertificate(
51 const uint8
* certificate_data
,
52 int certificate_data_length
,
53 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
54 void CreateSessionAndGenerateRequest(
55 SessionType session_type
,
56 const std::string
& init_data_type
,
57 const uint8
* init_data
,
59 scoped_ptr
<media::NewSessionCdmPromise
> promise
) override
;
60 void LoadSession(SessionType session_type
,
61 const std::string
& session_id
,
62 scoped_ptr
<media::NewSessionCdmPromise
> promise
) override
;
63 void UpdateSession(const std::string
& session_id
,
64 const uint8
* response
,
66 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
67 void CloseSession(const std::string
& session_id
,
68 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
69 void RemoveSession(const std::string
& session_id
,
70 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
71 CdmContext
* GetCdmContext() override
;
73 // media::CdmContext implementation.
74 Decryptor
* GetDecryptor() override
;
76 // media::Decryptor implementation.
77 void RegisterNewKeyCB(StreamType stream_type
,
78 const NewKeyCB
& key_added_cb
) override
;
79 void Decrypt(StreamType stream_type
,
80 const scoped_refptr
<media::DecoderBuffer
>& encrypted
,
81 const DecryptCB
& decrypt_cb
) override
;
82 void CancelDecrypt(StreamType stream_type
) override
;
83 void InitializeAudioDecoder(const media::AudioDecoderConfig
& config
,
84 const DecoderInitCB
& init_cb
) override
;
85 void InitializeVideoDecoder(const media::VideoDecoderConfig
& config
,
86 const DecoderInitCB
& init_cb
) override
;
87 void DecryptAndDecodeAudio(
88 const scoped_refptr
<media::DecoderBuffer
>& encrypted
,
89 const AudioDecodeCB
& audio_decode_cb
) override
;
90 void DecryptAndDecodeVideo(
91 const scoped_refptr
<media::DecoderBuffer
>& encrypted
,
92 const VideoDecodeCB
& video_decode_cb
) override
;
93 void ResetDecoder(StreamType stream_type
) override
;
94 void DeinitializeDecoder(StreamType stream_type
) override
;
98 const std::string
& key_system
,
99 scoped_ptr
<PepperCdmWrapper
> pepper_cdm_wrapper
,
100 const media::SessionMessageCB
& session_message_cb
,
101 const media::SessionClosedCB
& session_closed_cb
,
102 const media::SessionErrorCB
& session_error_cb
,
103 const media::SessionKeysChangeCB
& session_keys_change_cb
,
104 const media::SessionExpirationUpdateCB
& session_expiration_update_cb
);
106 void OnDecoderInitialized(StreamType stream_type
, bool success
);
108 // Callbacks for |plugin_cdm_delegate_| to fire session events.
109 void OnSessionMessage(const std::string
& session_id
,
110 MediaKeys::MessageType message_type
,
111 const std::vector
<uint8
>& message
,
112 const GURL
& legacy_destination_url
);
113 void OnSessionKeysChange(const std::string
& session_id
,
114 bool has_additional_usable_key
,
115 media::CdmKeysInfo keys_info
);
116 void OnSessionExpirationUpdate(const std::string
& session_id
,
117 const base::Time
& new_expiry_time
);
118 void OnSessionClosed(const std::string
& session_id
);
119 void OnSessionError(const std::string
& session_id
,
120 MediaKeys::Exception exception_code
,
122 const std::string
& error_description
);
124 void AttemptToResumePlayback();
126 // Callback to notify that a fatal error happened in |plugin_cdm_delegate_|.
127 // The error is terminal and |plugin_cdm_delegate_| should not be used after
129 void OnFatalPluginError();
131 ContentDecryptorDelegate
* CdmDelegate();
133 // Hold a reference of the Pepper CDM wrapper to make sure the plugin lives
134 // as long as needed.
135 scoped_ptr
<PepperCdmWrapper
> pepper_cdm_wrapper_
;
137 // Callbacks for firing session events.
138 media::SessionMessageCB session_message_cb_
;
139 media::SessionClosedCB session_closed_cb_
;
140 media::SessionErrorCB session_error_cb_
;
141 media::SessionKeysChangeCB session_keys_change_cb_
;
142 media::SessionExpirationUpdateCB session_expiration_update_cb_
;
144 scoped_refptr
<base::MessageLoopProxy
> render_loop_proxy_
;
146 DecoderInitCB audio_decoder_init_cb_
;
147 DecoderInitCB video_decoder_init_cb_
;
148 NewKeyCB new_audio_key_cb_
;
149 NewKeyCB new_video_key_cb_
;
151 // NOTE: Weak pointers must be invalidated before all other member variables.
152 base::WeakPtrFactory
<PpapiDecryptor
> weak_ptr_factory_
;
154 DISALLOW_COPY_AND_ASSIGN(PpapiDecryptor
);
157 } // namespace content
159 #endif // CONTENT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_