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 MEDIA_CDM_PPAPI_CDM_ADAPTER_H_
6 #define MEDIA_CDM_PPAPI_CDM_ADAPTER_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "build/build_config.h"
14 #include "media/cdm/ppapi/api/content_decryption_module.h"
15 #include "media/cdm/ppapi/cdm_helpers.h"
16 #include "media/cdm/ppapi/cdm_wrapper.h"
17 #include "media/cdm/ppapi/linked_ptr.h"
18 #include "ppapi/c/pp_stdint.h"
19 #include "ppapi/c/private/pp_content_decryptor.h"
20 #include "ppapi/cpp/completion_callback.h"
21 #include "ppapi/cpp/private/content_decryptor_private.h"
22 #include "ppapi/cpp/var.h"
23 #include "ppapi/cpp/var_array_buffer.h"
24 #include "ppapi/utility/completion_callback_factory.h"
26 #if defined(OS_CHROMEOS)
27 #include "ppapi/cpp/private/output_protection_private.h"
28 #include "ppapi/cpp/private/platform_verification.h"
31 #if defined(GetCurrentTime)
32 // winbase.h defines this which messes up calls to Host_5::GetCurrentTime.
38 // GetCdmHostFunc implementation.
39 void* GetCdmHost(int host_interface_version, void* user_data);
41 // An adapter class for abstracting away PPAPI interaction and threading for a
42 // Content Decryption Module (CDM).
43 class CdmAdapter : public pp::Instance,
44 public pp::ContentDecryptor_Private,
49 CdmAdapter(PP_Instance instance, pp::Module* module);
50 virtual ~CdmAdapter();
52 // pp::Instance implementation.
53 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
57 // PPP_ContentDecryptor_Private implementation.
58 // Note: Results of calls to these methods must be reported through the
59 // PPB_ContentDecryptor_Private interface.
60 virtual void Initialize(const std::string& key_system) OVERRIDE;
61 virtual void CreateSession(uint32_t promise_id,
62 const std::string& init_data_type,
63 pp::VarArrayBuffer init_data,
64 PP_SessionType session_type) OVERRIDE;
65 virtual void LoadSession(uint32_t promise_id,
66 const std::string& web_session_id) OVERRIDE;
67 virtual void UpdateSession(uint32_t promise_id,
68 const std::string& web_session_id,
69 pp::VarArrayBuffer response) OVERRIDE;
70 // TODO(jrummell): Pass this function through Pepper and add OVERRIDE.
71 virtual void CloseSession(uint32_t promise_id,
72 const std::string& web_session_id);
73 // TODO(jrummell): Rename to RemoveSession().
74 virtual void ReleaseSession(uint32_t promise_id,
75 const std::string& web_session_id) OVERRIDE;
76 // TODO(jrummell): Pass this function through Pepper and add OVERRIDE.
77 virtual void GetUsableKeyIds(uint32_t promise_id,
78 const std::string& web_session_id);
80 pp::Buffer_Dev encrypted_buffer,
81 const PP_EncryptedBlockInfo& encrypted_block_info) OVERRIDE;
82 virtual void InitializeAudioDecoder(
83 const PP_AudioDecoderConfig& decoder_config,
84 pp::Buffer_Dev extra_data_buffer) OVERRIDE;
85 virtual void InitializeVideoDecoder(
86 const PP_VideoDecoderConfig& decoder_config,
87 pp::Buffer_Dev extra_data_buffer) OVERRIDE;
88 virtual void DeinitializeDecoder(PP_DecryptorStreamType decoder_type,
89 uint32_t request_id) OVERRIDE;
90 virtual void ResetDecoder(PP_DecryptorStreamType decoder_type,
91 uint32_t request_id) OVERRIDE;
92 virtual void DecryptAndDecode(
93 PP_DecryptorStreamType decoder_type,
94 pp::Buffer_Dev encrypted_buffer,
95 const PP_EncryptedBlockInfo& encrypted_block_info) OVERRIDE;
97 // cdm::Host_4, cdm::Host_5 and cdm::Host_6 implementation.
98 virtual cdm::Buffer* Allocate(uint32_t capacity) OVERRIDE;
99 virtual void SetTimer(int64_t delay_ms, void* context) OVERRIDE;
101 // cdm::Host_4 implementation.
102 virtual double GetCurrentWallTimeInSeconds() OVERRIDE;
103 virtual void OnSessionCreated(uint32_t session_id,
104 const char* web_session_id,
105 uint32_t web_session_id_length) OVERRIDE;
106 virtual void OnSessionMessage(uint32_t session_id,
108 uint32_t message_length,
109 const char* destination_url,
110 uint32_t destination_url_length) OVERRIDE;
111 virtual void OnSessionReady(uint32_t session_id) OVERRIDE;
112 virtual void OnSessionClosed(uint32_t session_id) OVERRIDE;
113 virtual void OnSessionError(uint32_t session_id,
114 cdm::MediaKeyError error_code,
115 uint32_t system_code) OVERRIDE;
117 // cdm::Host_5 implementation.
118 virtual cdm::Time GetCurrentTime() OVERRIDE;
119 virtual void OnResolveNewSessionPromise(
121 const char* web_session_id,
122 uint32_t web_session_id_length) OVERRIDE;
123 virtual void OnResolvePromise(uint32_t promise_id) OVERRIDE;
124 virtual void OnRejectPromise(uint32_t promise_id,
126 uint32_t system_code,
127 const char* error_message,
128 uint32_t error_message_length) OVERRIDE;
129 virtual void OnSessionMessage(const char* web_session_id,
130 uint32_t web_session_id_length,
132 uint32_t message_length,
133 const char* destination_url,
134 uint32_t destination_url_length) OVERRIDE;
135 virtual void OnSessionKeysChange(const char* web_session_id,
136 uint32_t web_session_id_length,
137 bool has_additional_usable_key) OVERRIDE;
138 virtual void OnExpirationChange(const char* web_session_id,
139 uint32_t web_session_id_length,
140 cdm::Time new_expiry_time) OVERRIDE;
141 virtual void OnSessionReady(const char* web_session_id,
142 uint32_t web_session_id_length) OVERRIDE;
143 virtual void OnSessionClosed(const char* web_session_id,
144 uint32_t web_session_id_length) OVERRIDE;
145 virtual void OnSessionError(const char* web_session_id,
146 uint32_t web_session_id_length,
148 uint32_t system_code,
149 const char* error_message,
150 uint32_t error_message_length) OVERRIDE;
152 // cdm::Host_6 implementation.
153 virtual cdm::Time GetCurrentWallTime() OVERRIDE;
154 virtual void OnResolveKeyIdsPromise(uint32_t promise_id,
155 const cdm::BinaryData* usable_key_ids,
156 uint32_t usable_key_ids_length) OVERRIDE;
157 virtual void OnSessionUsableKeysChange(
158 const char* web_session_id,
159 uint32_t web_session_id_length,
160 bool has_additional_usable_key) OVERRIDE;
162 // cdm::Host_4, cdm::Host_5 and cdm::Host_6 implementation.
163 virtual void SendPlatformChallenge(const char* service_id,
164 uint32_t service_id_length,
165 const char* challenge,
166 uint32_t challenge_length) OVERRIDE;
167 virtual void EnableOutputProtection(
168 uint32_t desired_protection_mask) OVERRIDE;
169 virtual void QueryOutputProtectionStatus() OVERRIDE;
170 virtual void OnDeferredInitializationDone(
171 cdm::StreamType stream_type,
172 cdm::Status decoder_status) OVERRIDE;
173 virtual cdm::FileIO* CreateFileIO(cdm::FileIOClient* client) OVERRIDE;
176 // These are reported to UMA server. Do not change the existing values!
177 enum OutputProtectionStatus {
178 OUTPUT_PROTECTION_QUERIED = 0,
179 OUTPUT_PROTECTION_NO_EXTERNAL_LINK = 1,
180 OUTPUT_PROTECTION_ALL_EXTERNAL_LINKS_PROTECTED = 2,
181 OUTPUT_PROTECTION_MAX = 3
184 typedef linked_ptr<DecryptedBlockImpl> LinkedDecryptedBlock;
185 typedef linked_ptr<VideoFrameImpl> LinkedVideoFrame;
186 typedef linked_ptr<AudioFramesImpl> LinkedAudioFrames;
188 struct SessionError {
189 SessionError(cdm::Error error,
190 uint32_t system_code,
191 std::string error_description);
193 uint32_t system_code;
194 std::string error_description;
197 bool CreateCdmInstance(const std::string& key_system);
199 // <code>PPB_ContentDecryptor_Private</code> dispatchers. These are passed to
200 // <code>callback_factory_</code> to ensure that calls into
201 // <code>PPP_ContentDecryptor_Private</code> are asynchronous.
202 void SendPromiseResolvedInternal(int32_t result, uint32_t promise_id);
203 void SendPromiseResolvedWithSessionInternal(
206 const std::string& web_session_id);
207 void SendPromiseResolvedWithUsableKeyIdsInternal(
210 std::vector<std::vector<uint8> > key_ids);
211 void SendPromiseRejectedInternal(int32_t result,
213 const SessionError& error);
214 void SendSessionMessageInternal(int32_t result,
215 const std::string& web_session_id,
216 const std::vector<uint8>& message,
217 const std::string& destination_url);
218 void SendSessionReadyInternal(int32_t result,
219 const std::string& web_session_id);
220 void SendSessionClosedInternal(int32_t result,
221 const std::string& web_session_id);
222 void SendSessionErrorInternal(int32_t result,
223 const std::string& web_session_id,
224 const SessionError& error);
225 void SendSessionUsableKeysChangeInternal(int32_t result,
226 const std::string& web_session_id,
227 bool has_additional_usable_key);
228 void SendExpirationChangeInternal(int32_t result,
229 const std::string& web_session_id,
230 cdm::Time new_expiry_time);
231 void RejectPromise(uint32_t promise_id,
233 uint32_t system_code,
234 const std::string& error_message);
236 void DeliverBlock(int32_t result,
237 const cdm::Status& status,
238 const LinkedDecryptedBlock& decrypted_block,
239 const PP_DecryptTrackingInfo& tracking_info);
240 void DecoderInitializeDone(int32_t result,
241 PP_DecryptorStreamType decoder_type,
244 void DecoderDeinitializeDone(int32_t result,
245 PP_DecryptorStreamType decoder_type,
246 uint32_t request_id);
247 void DecoderResetDone(int32_t result,
248 PP_DecryptorStreamType decoder_type,
249 uint32_t request_id);
250 void DeliverFrame(int32_t result,
251 const cdm::Status& status,
252 const LinkedVideoFrame& video_frame,
253 const PP_DecryptTrackingInfo& tracking_info);
254 void DeliverSamples(int32_t result,
255 const cdm::Status& status,
256 const LinkedAudioFrames& audio_frames,
257 const PP_DecryptTrackingInfo& tracking_info);
259 // Helper for SetTimer().
260 void TimerExpired(int32_t result, void* context);
262 bool IsValidVideoFrame(const LinkedVideoFrame& video_frame);
264 // Callback to report |file_size_bytes| of the first file read by FileIO.
265 void OnFirstFileRead(int32_t file_size_bytes);
268 // Logs the given message to the JavaScript console associated with the
269 // CDM adapter instance. The name of the CDM adapter issuing the log message
270 // will be automatically prepended to the message.
271 void LogToConsole(const pp::Var& value);
272 #endif // !defined(NDEBUG)
274 #if defined(OS_CHROMEOS)
275 void ReportOutputProtectionUMA(OutputProtectionStatus status);
276 void ReportOutputProtectionQuery();
277 void ReportOutputProtectionQueryResult();
279 struct PepperPlatformChallengeResponse {
281 pp::Var signed_data_signature;
282 pp::Var platform_key_certificate;
285 void SendPlatformChallengeDone(
287 const linked_ptr<PepperPlatformChallengeResponse>& response);
288 void EnableProtectionDone(int32_t result);
289 void QueryOutputProtectionStatusDone(int32_t result);
291 pp::OutputProtection_Private output_protection_;
292 pp::PlatformVerification platform_verification_;
294 // Same as above, these are only read by QueryOutputProtectionStatusDone().
295 uint32_t output_link_mask_;
296 uint32_t output_protection_mask_;
297 bool query_output_protection_in_progress_;
299 // Tracks whether an output protection query and a positive query result (no
300 // unprotected external link) have been reported to UMA.
301 bool uma_for_output_protection_query_reported_;
302 bool uma_for_output_protection_positive_result_reported_;
305 PpbBufferAllocator allocator_;
306 pp::CompletionCallbackFactory<CdmAdapter> callback_factory_;
307 linked_ptr<CdmWrapper> cdm_;
308 std::string key_system_;
310 // If the CDM returned kDeferredInitialization during InitializeAudioDecoder()
311 // or InitializeVideoDecoder(), the (Audio|Video)DecoderConfig.request_id is
312 // saved for the future call to OnDeferredInitializationDone().
313 bool deferred_initialize_audio_decoder_;
314 uint32_t deferred_audio_decoder_config_id_;
315 bool deferred_initialize_video_decoder_;
316 uint32_t deferred_video_decoder_config_id_;
318 uint32_t last_read_file_size_kb_;
319 bool file_size_uma_reported_;
321 DISALLOW_COPY_AND_ASSIGN(CdmAdapter);
326 #endif // MEDIA_CDM_PPAPI_CDM_ADAPTER_H_