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_
12 #include "base/basictypes.h"
13 #include "base/callback_helpers.h"
14 #include "base/compiler_specific.h"
15 #include "base/containers/scoped_ptr_hash_map.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/channel_layout.h"
20 #include "media/base/decryptor.h"
21 #include "media/base/media_keys.h"
22 #include "media/base/sample_format.h"
23 #include "ppapi/c/private/pp_content_decryptor.h"
24 #include "ppapi/c/private/ppp_content_decryptor_private.h"
25 #include "ui/gfx/size.h"
28 class AudioDecoderConfig
;
30 class VideoDecoderConfig
;
35 class PPB_Buffer_Impl
;
37 class ContentDecryptorDelegate
{
39 // ContentDecryptorDelegate does not take ownership of
40 // |plugin_decryption_interface|. Therefore |plugin_decryption_interface|
41 // must outlive this object.
42 ContentDecryptorDelegate(
43 PP_Instance pp_instance
,
44 const PPP_ContentDecryptor_Private
* plugin_decryption_interface
);
45 ~ContentDecryptorDelegate();
47 // This object should not be accessed after |fatal_plugin_error_cb| is called.
48 void Initialize(const std::string
& key_system
,
49 const media::SessionMessageCB
& session_message_cb
,
50 const media::SessionReadyCB
& session_ready_cb
,
51 const media::SessionClosedCB
& session_closed_cb
,
52 const media::SessionErrorCB
& session_error_cb
,
53 const base::Closure
& fatal_plugin_error_cb
);
55 void InstanceCrashed();
57 // Provides access to PPP_ContentDecryptor_Private.
58 void CreateSession(const std::string
& init_data_type
,
59 const uint8
* init_data
,
61 media::MediaKeys::SessionType session_type
,
62 scoped_ptr
<media::NewSessionCdmPromise
> promise
);
63 void LoadSession(const std::string
& web_session_id
,
64 scoped_ptr
<media::NewSessionCdmPromise
> promise
);
65 void UpdateSession(const std::string
& web_session_id
,
66 const uint8
* response
,
68 scoped_ptr
<media::SimpleCdmPromise
> promise
);
69 void ReleaseSession(const std::string
& web_session_id
,
70 scoped_ptr
<media::SimpleCdmPromise
> promise
);
71 bool Decrypt(media::Decryptor::StreamType stream_type
,
72 const scoped_refptr
<media::DecoderBuffer
>& encrypted_buffer
,
73 const media::Decryptor::DecryptCB
& decrypt_cb
);
74 bool CancelDecrypt(media::Decryptor::StreamType stream_type
);
75 bool InitializeAudioDecoder(
76 const media::AudioDecoderConfig
& decoder_config
,
77 const media::Decryptor::DecoderInitCB
& decoder_init_cb
);
78 bool InitializeVideoDecoder(
79 const media::VideoDecoderConfig
& decoder_config
,
80 const media::Decryptor::DecoderInitCB
& decoder_init_cb
);
81 // TODO(tomfinegan): Add callback args for DeinitializeDecoder() and
83 bool DeinitializeDecoder(media::Decryptor::StreamType stream_type
);
84 bool ResetDecoder(media::Decryptor::StreamType stream_type
);
85 // Note: These methods can be used with unencrypted data.
86 bool DecryptAndDecodeAudio(
87 const scoped_refptr
<media::DecoderBuffer
>& encrypted_buffer
,
88 const media::Decryptor::AudioDecodeCB
& audio_decode_cb
);
89 bool DecryptAndDecodeVideo(
90 const scoped_refptr
<media::DecoderBuffer
>& encrypted_buffer
,
91 const media::Decryptor::VideoDecodeCB
& video_decode_cb
);
93 // PPB_ContentDecryptor_Private dispatching methods.
94 void OnPromiseResolved(uint32 promise_id
);
95 void OnPromiseResolvedWithSession(uint32 promise_id
, PP_Var web_session_id
);
96 void OnPromiseRejected(uint32 promise_id
,
97 PP_CdmExceptionCode exception_code
,
99 PP_Var error_description
);
100 void OnSessionMessage(PP_Var web_session_id
,
102 PP_Var destination_url
);
103 void OnSessionReady(PP_Var web_session_id
);
104 void OnSessionClosed(PP_Var web_session_id
);
105 void OnSessionError(PP_Var web_session_id
,
106 PP_CdmExceptionCode exception_code
,
108 PP_Var error_description
);
109 void DeliverBlock(PP_Resource decrypted_block
,
110 const PP_DecryptedBlockInfo
* block_info
);
111 void DecoderInitializeDone(PP_DecryptorStreamType decoder_type
,
114 void DecoderDeinitializeDone(PP_DecryptorStreamType decoder_type
,
115 uint32_t request_id
);
116 void DecoderResetDone(PP_DecryptorStreamType decoder_type
,
117 uint32_t request_id
);
118 void DeliverFrame(PP_Resource decrypted_frame
,
119 const PP_DecryptedFrameInfo
* frame_info
);
120 void DeliverSamples(PP_Resource audio_frames
,
121 const PP_DecryptedSampleInfo
* sample_info
);
124 // The following types keep track of Promises. The index is the promise_id,
125 // so that returning results can be matched to the corresponding promise.
126 typedef base::ScopedPtrHashMap
<uint32_t, media::CdmPromise
> PromiseMap
;
128 template <typename Callback
>
129 class TrackableCallback
{
131 TrackableCallback() : id_(0u) {}
132 ~TrackableCallback() {
133 // Callbacks must be satisfied.
138 bool Matches(uint32_t id
) const { return id
== id_
; }
140 bool is_null() const { return cb_
.is_null(); }
142 void Set(uint32_t id
, const Callback
& cb
) {
144 DCHECK(cb_
.is_null());
149 Callback
ResetAndReturn() {
151 return base::ResetAndReturn(&cb_
);
159 // Cancels the pending decrypt-and-decode callback for |stream_type|.
160 void CancelDecode(media::Decryptor::StreamType stream_type
);
162 // Fills |resource| with a PPB_Buffer_Impl and copies the data from
163 // |encrypted_buffer| into the buffer resource. This method reuses
164 // |audio_input_resource_| and |video_input_resource_| to reduce the latency
165 // in requesting new PPB_Buffer_Impl resources. The caller must make sure that
166 // |audio_input_resource_| or |video_input_resource_| is available before
167 // calling this method.
169 // An end of stream |encrypted_buffer| is represented as a null |resource|.
171 // Returns true upon success and false if any error happened.
172 bool MakeMediaBufferResource(
173 media::Decryptor::StreamType stream_type
,
174 const scoped_refptr
<media::DecoderBuffer
>& encrypted_buffer
,
175 scoped_refptr
<PPB_Buffer_Impl
>* resource
);
177 void FreeBuffer(uint32_t buffer_id
);
179 void SetBufferToFreeInTrackingInfo(PP_DecryptTrackingInfo
* tracking_info
);
181 // Deserializes audio data stored in |audio_frames| into individual audio
182 // buffers in |frames|. Returns true upon success.
183 bool DeserializeAudioFrames(PP_Resource audio_frames
,
185 media::SampleFormat sample_format
,
186 media::Decryptor::AudioBuffers
* frames
);
188 void SatisfyAllPendingCallbacksOnError();
190 // Takes ownership of |promise| and returns an identifier to be passed via
192 uint32_t SavePromise(scoped_ptr
<media::CdmPromise
> promise
);
194 // Find the promise for a specified |promise_id|. Caller is responsible to
195 // delete the CdmPromise<> once done with it.
196 scoped_ptr
<media::CdmPromise
> TakePromise(uint32_t promise_id
);
198 const PP_Instance pp_instance_
;
199 const PPP_ContentDecryptor_Private
* const plugin_decryption_interface_
;
201 // TODO(ddorwin): Remove after updating the Pepper API to not use key system.
202 std::string key_system_
;
204 // Callbacks for firing session events.
205 media::SessionMessageCB session_message_cb_
;
206 media::SessionReadyCB session_ready_cb_
;
207 media::SessionClosedCB session_closed_cb_
;
208 media::SessionErrorCB session_error_cb_
;
210 // Callback to notify that unexpected error happened and |this| should not
212 base::Closure fatal_plugin_error_cb_
;
214 gfx::Size natural_size_
;
216 // Request ID for tracking pending content decryption callbacks.
217 // Note that zero indicates an invalid request ID.
218 // TODO(xhwang): Add completion callbacks for Reset/Stop and remove the use
220 uint32_t next_decryption_request_id_
;
222 TrackableCallback
<media::Decryptor::DecryptCB
> audio_decrypt_cb_
;
223 TrackableCallback
<media::Decryptor::DecryptCB
> video_decrypt_cb_
;
224 TrackableCallback
<media::Decryptor::DecoderInitCB
> audio_decoder_init_cb_
;
225 TrackableCallback
<media::Decryptor::DecoderInitCB
> video_decoder_init_cb_
;
226 TrackableCallback
<media::Decryptor::AudioDecodeCB
> audio_decode_cb_
;
227 TrackableCallback
<media::Decryptor::VideoDecodeCB
> video_decode_cb_
;
229 // Cached audio and video input buffers. See MakeMediaBufferResource.
230 scoped_refptr
<PPB_Buffer_Impl
> audio_input_resource_
;
231 scoped_refptr
<PPB_Buffer_Impl
> video_input_resource_
;
233 std::queue
<uint32_t> free_buffers_
;
235 // Keep track of audio parameters.
236 int audio_samples_per_second_
;
237 int audio_channel_count_
;
238 media::ChannelLayout audio_channel_layout_
;
240 // Keep track of outstanding promises. Maps have ownership of the promises.
241 uint32_t next_promise_id_
;
242 PromiseMap promises_
;
244 base::WeakPtr
<ContentDecryptorDelegate
> weak_this_
;
245 base::WeakPtrFactory
<ContentDecryptorDelegate
> weak_ptr_factory_
;
247 DISALLOW_COPY_AND_ASSIGN(ContentDecryptorDelegate
);
250 } // namespace content
252 #endif // CONTENT_RENDERER_PEPPER_CONTENT_DECRYPTOR_DELEGATE_H_