[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / content / renderer / pepper / content_decryptor_delegate.h
blobf00274eaf8160f25964d91c949e9c1af8c211368
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 const media::SessionMessageCB& session_message_cb,
52 const media::SessionClosedCB& session_closed_cb,
53 const media::SessionErrorCB& session_error_cb,
54 const media::SessionKeysChangeCB& session_keys_change_cb,
55 const media::SessionExpirationUpdateCB& session_expiration_update_cb,
56 const base::Closure& fatal_plugin_error_cb);
58 void InstanceCrashed();
60 // Provides access to PPP_ContentDecryptor_Private.
61 void SetServerCertificate(const uint8_t* certificate,
62 uint32_t certificate_length,
63 scoped_ptr<media::SimpleCdmPromise> promise);
64 void CreateSessionAndGenerateRequest(
65 media::MediaKeys::SessionType session_type,
66 const std::string& init_data_type,
67 const uint8* init_data,
68 int init_data_length,
69 scoped_ptr<media::NewSessionCdmPromise> promise);
70 void LoadSession(media::MediaKeys::SessionType session_type,
71 const std::string& session_id,
72 scoped_ptr<media::NewSessionCdmPromise> promise);
73 void UpdateSession(const std::string& session_id,
74 const uint8* response,
75 int response_length,
76 scoped_ptr<media::SimpleCdmPromise> promise);
77 void CloseSession(const std::string& session_id,
78 scoped_ptr<media::SimpleCdmPromise> promise);
79 void RemoveSession(const std::string& session_id,
80 scoped_ptr<media::SimpleCdmPromise> promise);
81 bool Decrypt(media::Decryptor::StreamType stream_type,
82 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer,
83 const media::Decryptor::DecryptCB& decrypt_cb);
84 bool CancelDecrypt(media::Decryptor::StreamType stream_type);
85 bool InitializeAudioDecoder(
86 const media::AudioDecoderConfig& decoder_config,
87 const media::Decryptor::DecoderInitCB& decoder_init_cb);
88 bool InitializeVideoDecoder(
89 const media::VideoDecoderConfig& decoder_config,
90 const media::Decryptor::DecoderInitCB& decoder_init_cb);
91 // TODO(tomfinegan): Add callback args for DeinitializeDecoder() and
92 // ResetDecoder()
93 bool DeinitializeDecoder(media::Decryptor::StreamType stream_type);
94 bool ResetDecoder(media::Decryptor::StreamType stream_type);
95 // Note: These methods can be used with unencrypted data.
96 bool DecryptAndDecodeAudio(
97 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer,
98 const media::Decryptor::AudioDecodeCB& audio_decode_cb);
99 bool DecryptAndDecodeVideo(
100 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer,
101 const media::Decryptor::VideoDecodeCB& video_decode_cb);
103 // PPB_ContentDecryptor_Private dispatching methods.
104 void OnPromiseResolved(uint32 promise_id);
105 void OnPromiseResolvedWithSession(uint32 promise_id, PP_Var session_id);
106 void OnPromiseRejected(uint32 promise_id,
107 PP_CdmExceptionCode exception_code,
108 uint32 system_code,
109 PP_Var error_description);
110 void OnSessionMessage(PP_Var session_id,
111 PP_CdmMessageType message_type,
112 PP_Var message,
113 PP_Var legacy_destination_url);
114 void OnSessionKeysChange(PP_Var session_id,
115 PP_Bool has_additional_usable_key,
116 uint32_t key_count,
117 const struct PP_KeyInformation key_information[]);
118 void OnSessionExpirationChange(PP_Var session_id, PP_Time new_expiry_time);
119 void OnSessionClosed(PP_Var session_id);
120 void OnSessionError(PP_Var session_id,
121 PP_CdmExceptionCode exception_code,
122 uint32 system_code,
123 PP_Var error_description);
124 void DeliverBlock(PP_Resource decrypted_block,
125 const PP_DecryptedBlockInfo* block_info);
126 void DecoderInitializeDone(PP_DecryptorStreamType decoder_type,
127 uint32_t request_id,
128 PP_Bool success);
129 void DecoderDeinitializeDone(PP_DecryptorStreamType decoder_type,
130 uint32_t request_id);
131 void DecoderResetDone(PP_DecryptorStreamType decoder_type,
132 uint32_t request_id);
133 void DeliverFrame(PP_Resource decrypted_frame,
134 const PP_DecryptedFrameInfo* frame_info);
135 void DeliverSamples(PP_Resource audio_frames,
136 const PP_DecryptedSampleInfo* sample_info);
138 private:
139 template <typename Callback>
140 class TrackableCallback {
141 public:
142 TrackableCallback() : id_(0u) {}
143 ~TrackableCallback() {
144 // Callbacks must be satisfied.
145 DCHECK_EQ(id_, 0u);
146 DCHECK(is_null());
149 bool Matches(uint32_t id) const { return id == id_; }
151 bool is_null() const { return cb_.is_null(); }
153 void Set(uint32_t id, const Callback& cb) {
154 DCHECK_EQ(id_, 0u);
155 DCHECK(cb_.is_null());
156 id_ = id;
157 cb_ = cb;
160 Callback ResetAndReturn() {
161 id_ = 0;
162 return base::ResetAndReturn(&cb_);
165 private:
166 uint32_t id_;
167 Callback cb_;
170 // Cancels the pending decrypt-and-decode callback for |stream_type|.
171 void CancelDecode(media::Decryptor::StreamType stream_type);
173 // Fills |resource| with a PPB_Buffer_Impl and copies the data from
174 // |encrypted_buffer| into the buffer resource. This method reuses
175 // |audio_input_resource_| and |video_input_resource_| to reduce the latency
176 // in requesting new PPB_Buffer_Impl resources. The caller must make sure that
177 // |audio_input_resource_| or |video_input_resource_| is available before
178 // calling this method.
180 // An end of stream |encrypted_buffer| is represented as a null |resource|.
182 // Returns true upon success and false if any error happened.
183 bool MakeMediaBufferResource(
184 media::Decryptor::StreamType stream_type,
185 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer,
186 scoped_refptr<PPB_Buffer_Impl>* resource);
188 void FreeBuffer(uint32_t buffer_id);
190 void SetBufferToFreeInTrackingInfo(PP_DecryptTrackingInfo* tracking_info);
192 // Deserializes audio data stored in |audio_frames| into individual audio
193 // buffers in |frames|. Returns true upon success.
194 bool DeserializeAudioFrames(PP_Resource audio_frames,
195 size_t data_size,
196 media::SampleFormat sample_format,
197 media::Decryptor::AudioFrames* frames);
199 void SatisfyAllPendingCallbacksOnError();
201 const PP_Instance pp_instance_;
202 const PPP_ContentDecryptor_Private* const plugin_decryption_interface_;
204 // TODO(ddorwin): Remove after updating the Pepper API to not use key system.
205 std::string key_system_;
207 // Callbacks for firing session events.
208 media::SessionMessageCB session_message_cb_;
209 media::SessionClosedCB session_closed_cb_;
210 media::SessionErrorCB session_error_cb_;
211 media::SessionKeysChangeCB session_keys_change_cb_;
212 media::SessionExpirationUpdateCB session_expiration_update_cb_;
214 // Callback to notify that unexpected error happened and |this| should not
215 // be used anymore.
216 base::Closure fatal_plugin_error_cb_;
218 gfx::Size natural_size_;
220 // Request ID for tracking pending content decryption callbacks.
221 // Note that zero indicates an invalid request ID.
222 // TODO(xhwang): Add completion callbacks for Reset/Stop and remove the use
223 // of request IDs.
224 uint32_t next_decryption_request_id_;
226 TrackableCallback<media::Decryptor::DecryptCB> audio_decrypt_cb_;
227 TrackableCallback<media::Decryptor::DecryptCB> video_decrypt_cb_;
228 TrackableCallback<media::Decryptor::DecoderInitCB> audio_decoder_init_cb_;
229 TrackableCallback<media::Decryptor::DecoderInitCB> video_decoder_init_cb_;
230 TrackableCallback<media::Decryptor::AudioDecodeCB> audio_decode_cb_;
231 TrackableCallback<media::Decryptor::VideoDecodeCB> video_decode_cb_;
233 // Cached audio and video input buffers. See MakeMediaBufferResource.
234 scoped_refptr<PPB_Buffer_Impl> audio_input_resource_;
235 scoped_refptr<PPB_Buffer_Impl> video_input_resource_;
237 std::queue<uint32_t> free_buffers_;
239 // Keep track of audio parameters.
240 int audio_samples_per_second_;
241 int audio_channel_count_;
242 media::ChannelLayout audio_channel_layout_;
244 media::CdmPromiseAdapter cdm_promise_adapter_;
246 base::WeakPtr<ContentDecryptorDelegate> weak_this_;
247 base::WeakPtrFactory<ContentDecryptorDelegate> weak_ptr_factory_;
249 DISALLOW_COPY_AND_ASSIGN(ContentDecryptorDelegate);
252 } // namespace content
254 #endif // CONTENT_RENDERER_PEPPER_CONTENT_DECRYPTOR_DELEGATE_H_