1 // Copyright (c) 2012 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_PEPPER_CONTENT_DECRYPTOR_DELEGATE_H_
6 #define CONTENT_RENDERER_PEPPER_CONTENT_DECRYPTOR_DELEGATE_H_
13 #include "base/basictypes.h"
14 #include "base/callback_helpers.h"
15 #include "base/compiler_specific.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/weak_ptr.h"
18 #include "media/base/cdm_promise.h"
19 #include "media/base/cdm_promise_adapter.h"
20 #include "media/base/channel_layout.h"
21 #include "media/base/decryptor.h"
22 #include "media/base/media_keys.h"
23 #include "media/base/sample_format.h"
24 #include "ppapi/c/pp_time.h"
25 #include "ppapi/c/private/pp_content_decryptor.h"
26 #include "ppapi/c/private/ppp_content_decryptor_private.h"
27 #include "ui/gfx/geometry/size.h"
30 class AudioDecoderConfig
;
32 class VideoDecoderConfig
;
37 class PPB_Buffer_Impl
;
39 class ContentDecryptorDelegate
{
41 // ContentDecryptorDelegate does not take ownership of
42 // |plugin_decryption_interface|. Therefore |plugin_decryption_interface|
43 // must outlive this object.
44 ContentDecryptorDelegate(
45 PP_Instance pp_instance
,
46 const PPP_ContentDecryptor_Private
* plugin_decryption_interface
);
47 ~ContentDecryptorDelegate();
49 // This object should not be accessed after |fatal_plugin_error_cb| is called.
51 const std::string
& key_system
,
52 bool allow_distinctive_identifier
,
53 bool allow_persistent_state
,
54 const media::SessionMessageCB
& session_message_cb
,
55 const media::SessionClosedCB
& session_closed_cb
,
56 const media::LegacySessionErrorCB
& legacy_session_error_cb
,
57 const media::SessionKeysChangeCB
& session_keys_change_cb
,
58 const media::SessionExpirationUpdateCB
& session_expiration_update_cb
,
59 const base::Closure
& fatal_plugin_error_cb
,
60 scoped_ptr
<media::SimpleCdmPromise
> promise
);
62 void InstanceCrashed();
64 // Provides access to PPP_ContentDecryptor_Private.
65 void SetServerCertificate(const std::vector
<uint8_t>& certificate
,
66 scoped_ptr
<media::SimpleCdmPromise
> promise
);
67 void CreateSessionAndGenerateRequest(
68 media::MediaKeys::SessionType session_type
,
69 media::EmeInitDataType init_data_type
,
70 const std::vector
<uint8_t>& init_data
,
71 scoped_ptr
<media::NewSessionCdmPromise
> promise
);
72 void LoadSession(media::MediaKeys::SessionType session_type
,
73 const std::string
& session_id
,
74 scoped_ptr
<media::NewSessionCdmPromise
> promise
);
75 void UpdateSession(const std::string
& session_id
,
76 const std::vector
<uint8_t>& response
,
77 scoped_ptr
<media::SimpleCdmPromise
> promise
);
78 void CloseSession(const std::string
& session_id
,
79 scoped_ptr
<media::SimpleCdmPromise
> promise
);
80 void RemoveSession(const std::string
& session_id
,
81 scoped_ptr
<media::SimpleCdmPromise
> promise
);
82 bool Decrypt(media::Decryptor::StreamType stream_type
,
83 const scoped_refptr
<media::DecoderBuffer
>& encrypted_buffer
,
84 const media::Decryptor::DecryptCB
& decrypt_cb
);
85 bool CancelDecrypt(media::Decryptor::StreamType stream_type
);
86 bool InitializeAudioDecoder(
87 const media::AudioDecoderConfig
& decoder_config
,
88 const media::Decryptor::DecoderInitCB
& decoder_init_cb
);
89 bool InitializeVideoDecoder(
90 const media::VideoDecoderConfig
& decoder_config
,
91 const media::Decryptor::DecoderInitCB
& decoder_init_cb
);
92 // TODO(tomfinegan): Add callback args for DeinitializeDecoder() and
94 bool DeinitializeDecoder(media::Decryptor::StreamType stream_type
);
95 bool ResetDecoder(media::Decryptor::StreamType stream_type
);
96 // Note: These methods can be used with unencrypted data.
97 bool DecryptAndDecodeAudio(
98 const scoped_refptr
<media::DecoderBuffer
>& encrypted_buffer
,
99 const media::Decryptor::AudioDecodeCB
& audio_decode_cb
);
100 bool DecryptAndDecodeVideo(
101 const scoped_refptr
<media::DecoderBuffer
>& encrypted_buffer
,
102 const media::Decryptor::VideoDecodeCB
& video_decode_cb
);
104 // PPB_ContentDecryptor_Private dispatching methods.
105 void OnPromiseResolved(uint32_t promise_id
);
106 void OnPromiseResolvedWithSession(uint32_t promise_id
, PP_Var session_id
);
107 void OnPromiseRejected(uint32_t promise_id
,
108 PP_CdmExceptionCode exception_code
,
109 uint32_t system_code
,
110 PP_Var error_description
);
111 void OnSessionMessage(PP_Var session_id
,
112 PP_CdmMessageType message_type
,
114 PP_Var legacy_destination_url
);
115 void OnSessionKeysChange(PP_Var session_id
,
116 PP_Bool has_additional_usable_key
,
118 const struct PP_KeyInformation key_information
[]);
119 void OnSessionExpirationChange(PP_Var session_id
, PP_Time new_expiry_time
);
120 void OnSessionClosed(PP_Var session_id
);
121 void OnLegacySessionError(PP_Var session_id
,
122 PP_CdmExceptionCode exception_code
,
123 uint32_t system_code
,
124 PP_Var error_description
);
125 void DeliverBlock(PP_Resource decrypted_block
,
126 const PP_DecryptedBlockInfo
* block_info
);
127 void DecoderInitializeDone(PP_DecryptorStreamType decoder_type
,
130 void DecoderDeinitializeDone(PP_DecryptorStreamType decoder_type
,
131 uint32_t request_id
);
132 void DecoderResetDone(PP_DecryptorStreamType decoder_type
,
133 uint32_t request_id
);
134 void DeliverFrame(PP_Resource decrypted_frame
,
135 const PP_DecryptedFrameInfo
* frame_info
);
136 void DeliverSamples(PP_Resource audio_frames
,
137 const PP_DecryptedSampleInfo
* sample_info
);
140 template <typename Callback
>
141 class TrackableCallback
{
143 TrackableCallback() : id_(0u) {}
144 ~TrackableCallback() {
145 // Callbacks must be satisfied.
150 bool Matches(uint32_t id
) const { return id
== id_
; }
152 bool is_null() const { return cb_
.is_null(); }
154 void Set(uint32_t id
, const Callback
& cb
) {
156 DCHECK(cb_
.is_null());
161 Callback
ResetAndReturn() {
163 return base::ResetAndReturn(&cb_
);
171 // Cancels the pending decrypt-and-decode callback for |stream_type|.
172 void CancelDecode(media::Decryptor::StreamType stream_type
);
174 // Fills |resource| with a PPB_Buffer_Impl and copies the data from
175 // |encrypted_buffer| into the buffer resource. This method reuses
176 // |audio_input_resource_| and |video_input_resource_| to reduce the latency
177 // in requesting new PPB_Buffer_Impl resources. The caller must make sure that
178 // |audio_input_resource_| or |video_input_resource_| is available before
179 // calling this method.
181 // An end of stream |encrypted_buffer| is represented as a null |resource|.
183 // Returns true upon success and false if any error happened.
184 bool MakeMediaBufferResource(
185 media::Decryptor::StreamType stream_type
,
186 const scoped_refptr
<media::DecoderBuffer
>& encrypted_buffer
,
187 scoped_refptr
<PPB_Buffer_Impl
>* resource
);
189 void FreeBuffer(uint32_t buffer_id
);
191 void SetBufferToFreeInTrackingInfo(PP_DecryptTrackingInfo
* tracking_info
);
193 // Deserializes audio data stored in |audio_frames| into individual audio
194 // buffers in |frames|. Returns true upon success.
195 bool DeserializeAudioFrames(PP_Resource audio_frames
,
197 media::SampleFormat sample_format
,
198 media::Decryptor::AudioFrames
* frames
);
200 void SatisfyAllPendingCallbacksOnError();
202 const PP_Instance pp_instance_
;
203 const PPP_ContentDecryptor_Private
* const plugin_decryption_interface_
;
205 // TODO(ddorwin): Remove after updating the Pepper API to not use key system.
206 std::string key_system_
;
208 // Callbacks for firing session events.
209 media::SessionMessageCB session_message_cb_
;
210 media::SessionClosedCB session_closed_cb_
;
211 media::LegacySessionErrorCB legacy_session_error_cb_
;
212 media::SessionKeysChangeCB session_keys_change_cb_
;
213 media::SessionExpirationUpdateCB session_expiration_update_cb_
;
215 // Callback to notify that unexpected error happened and |this| should not
217 base::Closure fatal_plugin_error_cb_
;
219 gfx::Size natural_size_
;
221 // Request ID for tracking pending content decryption callbacks.
222 // Note that zero indicates an invalid request ID.
223 // TODO(xhwang): Add completion callbacks for Reset/Stop and remove the use
225 uint32_t next_decryption_request_id_
;
227 TrackableCallback
<media::Decryptor::DecryptCB
> audio_decrypt_cb_
;
228 TrackableCallback
<media::Decryptor::DecryptCB
> video_decrypt_cb_
;
229 TrackableCallback
<media::Decryptor::DecoderInitCB
> audio_decoder_init_cb_
;
230 TrackableCallback
<media::Decryptor::DecoderInitCB
> video_decoder_init_cb_
;
231 TrackableCallback
<media::Decryptor::AudioDecodeCB
> audio_decode_cb_
;
232 TrackableCallback
<media::Decryptor::VideoDecodeCB
> video_decode_cb_
;
234 // Cached audio and video input buffers. See MakeMediaBufferResource.
235 scoped_refptr
<PPB_Buffer_Impl
> audio_input_resource_
;
236 scoped_refptr
<PPB_Buffer_Impl
> video_input_resource_
;
238 std::queue
<uint32_t> free_buffers_
;
240 // Keep track of audio parameters.
241 int audio_samples_per_second_
;
242 int audio_channel_count_
;
243 media::ChannelLayout audio_channel_layout_
;
245 media::CdmPromiseAdapter cdm_promise_adapter_
;
247 base::WeakPtr
<ContentDecryptorDelegate
> weak_this_
;
248 base::WeakPtrFactory
<ContentDecryptorDelegate
> weak_ptr_factory_
;
250 DISALLOW_COPY_AND_ASSIGN(ContentDecryptorDelegate
);
253 } // namespace content
255 #endif // CONTENT_RENDERER_PEPPER_CONTENT_DECRYPTOR_DELEGATE_H_