Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / content_decryptor_delegate.h
blob1fbeddba5ead7b680a64d0b3ce469258563470b3
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_
8 #include <map>
9 #include <queue>
10 #include <string>
12 #include "base/basictypes.h"
13 #include "base/callback_helpers.h"
14 #include "base/compiler_specific.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/weak_ptr.h"
17 #include "media/base/cdm_promise.h"
18 #include "media/base/cdm_promise_adapter.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/pp_time.h"
24 #include "ppapi/c/private/pp_content_decryptor.h"
25 #include "ppapi/c/private/ppp_content_decryptor_private.h"
26 #include "ui/gfx/geometry/size.h"
28 namespace media {
29 class AudioDecoderConfig;
30 class DecoderBuffer;
31 class VideoDecoderConfig;
34 namespace content {
36 class PPB_Buffer_Impl;
38 class ContentDecryptorDelegate {
39 public:
40 // ContentDecryptorDelegate does not take ownership of
41 // |plugin_decryption_interface|. Therefore |plugin_decryption_interface|
42 // must outlive this object.
43 ContentDecryptorDelegate(
44 PP_Instance pp_instance,
45 const PPP_ContentDecryptor_Private* plugin_decryption_interface);
46 ~ContentDecryptorDelegate();
48 // This object should not be accessed after |fatal_plugin_error_cb| is called.
49 void Initialize(
50 const std::string& key_system,
51 bool allow_distinctive_identifier,
52 bool allow_persistent_state,
53 const media::SessionMessageCB& session_message_cb,
54 const media::SessionClosedCB& session_closed_cb,
55 const media::LegacySessionErrorCB& legacy_session_error_cb,
56 const media::SessionKeysChangeCB& session_keys_change_cb,
57 const media::SessionExpirationUpdateCB& session_expiration_update_cb,
58 const base::Closure& fatal_plugin_error_cb);
60 void InstanceCrashed();
62 // Provides access to PPP_ContentDecryptor_Private.
63 void SetServerCertificate(const uint8_t* certificate,
64 uint32_t certificate_length,
65 scoped_ptr<media::SimpleCdmPromise> promise);
66 void CreateSessionAndGenerateRequest(
67 media::MediaKeys::SessionType session_type,
68 media::EmeInitDataType init_data_type,
69 const uint8* init_data,
70 int init_data_length,
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 uint8* response,
77 int response_length,
78 scoped_ptr<media::SimpleCdmPromise> promise);
79 void CloseSession(const std::string& session_id,
80 scoped_ptr<media::SimpleCdmPromise> promise);
81 void RemoveSession(const std::string& session_id,
82 scoped_ptr<media::SimpleCdmPromise> promise);
83 bool Decrypt(media::Decryptor::StreamType stream_type,
84 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer,
85 const media::Decryptor::DecryptCB& decrypt_cb);
86 bool CancelDecrypt(media::Decryptor::StreamType stream_type);
87 bool InitializeAudioDecoder(
88 const media::AudioDecoderConfig& decoder_config,
89 const media::Decryptor::DecoderInitCB& decoder_init_cb);
90 bool InitializeVideoDecoder(
91 const media::VideoDecoderConfig& decoder_config,
92 const media::Decryptor::DecoderInitCB& decoder_init_cb);
93 // TODO(tomfinegan): Add callback args for DeinitializeDecoder() and
94 // ResetDecoder()
95 bool DeinitializeDecoder(media::Decryptor::StreamType stream_type);
96 bool ResetDecoder(media::Decryptor::StreamType stream_type);
97 // Note: These methods can be used with unencrypted data.
98 bool DecryptAndDecodeAudio(
99 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer,
100 const media::Decryptor::AudioDecodeCB& audio_decode_cb);
101 bool DecryptAndDecodeVideo(
102 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer,
103 const media::Decryptor::VideoDecodeCB& video_decode_cb);
105 // PPB_ContentDecryptor_Private dispatching methods.
106 void OnPromiseResolved(uint32 promise_id);
107 void OnPromiseResolvedWithSession(uint32 promise_id, PP_Var session_id);
108 void OnPromiseRejected(uint32 promise_id,
109 PP_CdmExceptionCode exception_code,
110 uint32 system_code,
111 PP_Var error_description);
112 void OnSessionMessage(PP_Var session_id,
113 PP_CdmMessageType message_type,
114 PP_Var message,
115 PP_Var legacy_destination_url);
116 void OnSessionKeysChange(PP_Var session_id,
117 PP_Bool has_additional_usable_key,
118 uint32_t key_count,
119 const struct PP_KeyInformation key_information[]);
120 void OnSessionExpirationChange(PP_Var session_id, PP_Time new_expiry_time);
121 void OnSessionClosed(PP_Var session_id);
122 void OnLegacySessionError(PP_Var session_id,
123 PP_CdmExceptionCode exception_code,
124 uint32 system_code,
125 PP_Var error_description);
126 void DeliverBlock(PP_Resource decrypted_block,
127 const PP_DecryptedBlockInfo* block_info);
128 void DecoderInitializeDone(PP_DecryptorStreamType decoder_type,
129 uint32_t request_id,
130 PP_Bool success);
131 void DecoderDeinitializeDone(PP_DecryptorStreamType decoder_type,
132 uint32_t request_id);
133 void DecoderResetDone(PP_DecryptorStreamType decoder_type,
134 uint32_t request_id);
135 void DeliverFrame(PP_Resource decrypted_frame,
136 const PP_DecryptedFrameInfo* frame_info);
137 void DeliverSamples(PP_Resource audio_frames,
138 const PP_DecryptedSampleInfo* sample_info);
140 private:
141 template <typename Callback>
142 class TrackableCallback {
143 public:
144 TrackableCallback() : id_(0u) {}
145 ~TrackableCallback() {
146 // Callbacks must be satisfied.
147 DCHECK_EQ(id_, 0u);
148 DCHECK(is_null());
151 bool Matches(uint32_t id) const { return id == id_; }
153 bool is_null() const { return cb_.is_null(); }
155 void Set(uint32_t id, const Callback& cb) {
156 DCHECK_EQ(id_, 0u);
157 DCHECK(cb_.is_null());
158 id_ = id;
159 cb_ = cb;
162 Callback ResetAndReturn() {
163 id_ = 0;
164 return base::ResetAndReturn(&cb_);
167 private:
168 uint32_t id_;
169 Callback cb_;
172 // Cancels the pending decrypt-and-decode callback for |stream_type|.
173 void CancelDecode(media::Decryptor::StreamType stream_type);
175 // Fills |resource| with a PPB_Buffer_Impl and copies the data from
176 // |encrypted_buffer| into the buffer resource. This method reuses
177 // |audio_input_resource_| and |video_input_resource_| to reduce the latency
178 // in requesting new PPB_Buffer_Impl resources. The caller must make sure that
179 // |audio_input_resource_| or |video_input_resource_| is available before
180 // calling this method.
182 // An end of stream |encrypted_buffer| is represented as a null |resource|.
184 // Returns true upon success and false if any error happened.
185 bool MakeMediaBufferResource(
186 media::Decryptor::StreamType stream_type,
187 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer,
188 scoped_refptr<PPB_Buffer_Impl>* resource);
190 void FreeBuffer(uint32_t buffer_id);
192 void SetBufferToFreeInTrackingInfo(PP_DecryptTrackingInfo* tracking_info);
194 // Deserializes audio data stored in |audio_frames| into individual audio
195 // buffers in |frames|. Returns true upon success.
196 bool DeserializeAudioFrames(PP_Resource audio_frames,
197 size_t data_size,
198 media::SampleFormat sample_format,
199 media::Decryptor::AudioFrames* frames);
201 void SatisfyAllPendingCallbacksOnError();
203 const PP_Instance pp_instance_;
204 const PPP_ContentDecryptor_Private* const plugin_decryption_interface_;
206 // TODO(ddorwin): Remove after updating the Pepper API to not use key system.
207 std::string key_system_;
209 // Callbacks for firing session events.
210 media::SessionMessageCB session_message_cb_;
211 media::SessionClosedCB session_closed_cb_;
212 media::LegacySessionErrorCB legacy_session_error_cb_;
213 media::SessionKeysChangeCB session_keys_change_cb_;
214 media::SessionExpirationUpdateCB session_expiration_update_cb_;
216 // Callback to notify that unexpected error happened and |this| should not
217 // be used anymore.
218 base::Closure fatal_plugin_error_cb_;
220 gfx::Size natural_size_;
222 // Request ID for tracking pending content decryption callbacks.
223 // Note that zero indicates an invalid request ID.
224 // TODO(xhwang): Add completion callbacks for Reset/Stop and remove the use
225 // of request IDs.
226 uint32_t next_decryption_request_id_;
228 TrackableCallback<media::Decryptor::DecryptCB> audio_decrypt_cb_;
229 TrackableCallback<media::Decryptor::DecryptCB> video_decrypt_cb_;
230 TrackableCallback<media::Decryptor::DecoderInitCB> audio_decoder_init_cb_;
231 TrackableCallback<media::Decryptor::DecoderInitCB> video_decoder_init_cb_;
232 TrackableCallback<media::Decryptor::AudioDecodeCB> audio_decode_cb_;
233 TrackableCallback<media::Decryptor::VideoDecodeCB> video_decode_cb_;
235 // Cached audio and video input buffers. See MakeMediaBufferResource.
236 scoped_refptr<PPB_Buffer_Impl> audio_input_resource_;
237 scoped_refptr<PPB_Buffer_Impl> video_input_resource_;
239 std::queue<uint32_t> free_buffers_;
241 // Keep track of audio parameters.
242 int audio_samples_per_second_;
243 int audio_channel_count_;
244 media::ChannelLayout audio_channel_layout_;
246 media::CdmPromiseAdapter cdm_promise_adapter_;
248 base::WeakPtr<ContentDecryptorDelegate> weak_this_;
249 base::WeakPtrFactory<ContentDecryptorDelegate> weak_ptr_factory_;
251 DISALLOW_COPY_AND_ASSIGN(ContentDecryptorDelegate);
254 } // namespace content
256 #endif // CONTENT_RENDERER_PEPPER_CONTENT_DECRYPTOR_DELEGATE_H_