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"
33 // GetCdmHostFunc implementation.
34 void* GetCdmHost(int host_interface_version, void* user_data);
36 // An adapter class for abstracting away PPAPI interaction and threading for a
37 // Content Decryption Module (CDM).
38 class CdmAdapter : public pp::Instance,
39 public pp::ContentDecryptor_Private,
42 CdmAdapter(PP_Instance instance, pp::Module* module);
43 virtual ~CdmAdapter();
45 // pp::Instance implementation.
46 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
50 // PPP_ContentDecryptor_Private implementation.
51 // Note: Results of calls to these methods must be reported through the
52 // PPB_ContentDecryptor_Private interface.
53 virtual void Initialize(const std::string& key_system) override;
54 virtual void SetServerCertificate(
56 pp::VarArrayBuffer server_certificate) override;
57 virtual void CreateSession(uint32_t promise_id,
58 const std::string& init_data_type,
59 pp::VarArrayBuffer init_data,
60 PP_SessionType session_type) override;
61 virtual void LoadSession(uint32_t promise_id,
62 const std::string& web_session_id) override;
63 virtual void UpdateSession(uint32_t promise_id,
64 const std::string& web_session_id,
65 pp::VarArrayBuffer response) override;
66 virtual void CloseSession(uint32_t promise_id,
67 const std::string& web_session_id);
68 virtual void RemoveSession(uint32_t promise_id,
69 const std::string& web_session_id) override;
70 virtual void GetUsableKeyIds(uint32_t promise_id,
71 const std::string& web_session_id) override;
73 pp::Buffer_Dev encrypted_buffer,
74 const PP_EncryptedBlockInfo& encrypted_block_info) override;
75 virtual void InitializeAudioDecoder(
76 const PP_AudioDecoderConfig& decoder_config,
77 pp::Buffer_Dev extra_data_buffer) override;
78 virtual void InitializeVideoDecoder(
79 const PP_VideoDecoderConfig& decoder_config,
80 pp::Buffer_Dev extra_data_buffer) override;
81 virtual void DeinitializeDecoder(PP_DecryptorStreamType decoder_type,
82 uint32_t request_id) override;
83 virtual void ResetDecoder(PP_DecryptorStreamType decoder_type,
84 uint32_t request_id) override;
85 virtual void DecryptAndDecode(
86 PP_DecryptorStreamType decoder_type,
87 pp::Buffer_Dev encrypted_buffer,
88 const PP_EncryptedBlockInfo& encrypted_block_info) override;
90 // cdm::Host_6 implementation.
91 virtual cdm::Buffer* Allocate(uint32_t capacity) override;
92 virtual void SetTimer(int64_t delay_ms, void* context) override;
93 virtual cdm::Time GetCurrentWallTime() override;
94 virtual void OnResolveNewSessionPromise(
96 const char* web_session_id,
97 uint32_t web_session_id_length) override;
98 virtual void OnResolvePromise(uint32_t promise_id) override;
99 virtual void OnResolveKeyIdsPromise(uint32_t promise_id,
100 const cdm::BinaryData* usable_key_ids,
101 uint32_t usable_key_ids_length) override;
102 virtual void OnRejectPromise(uint32_t promise_id,
104 uint32_t system_code,
105 const char* error_message,
106 uint32_t error_message_length) override;
107 virtual void OnSessionMessage(const char* web_session_id,
108 uint32_t web_session_id_length,
110 uint32_t message_length,
111 const char* destination_url,
112 uint32_t destination_url_length) override;
113 virtual void OnSessionUsableKeysChange(
114 const char* web_session_id,
115 uint32_t web_session_id_length,
116 bool has_additional_usable_key) override;
117 virtual void OnExpirationChange(const char* web_session_id,
118 uint32_t web_session_id_length,
119 cdm::Time new_expiry_time) override;
120 virtual void OnSessionClosed(const char* web_session_id,
121 uint32_t web_session_id_length) override;
122 virtual void OnSessionError(const char* web_session_id,
123 uint32_t web_session_id_length,
125 uint32_t system_code,
126 const char* error_message,
127 uint32_t error_message_length) override;
128 virtual void SendPlatformChallenge(const char* service_id,
129 uint32_t service_id_length,
130 const char* challenge,
131 uint32_t challenge_length) override;
132 virtual void EnableOutputProtection(
133 uint32_t desired_protection_mask) override;
134 virtual void QueryOutputProtectionStatus() override;
135 virtual void OnDeferredInitializationDone(
136 cdm::StreamType stream_type,
137 cdm::Status decoder_status) override;
138 virtual cdm::FileIO* CreateFileIO(cdm::FileIOClient* client) override;
141 // These are reported to UMA server. Do not change the existing values!
142 enum OutputProtectionStatus {
143 OUTPUT_PROTECTION_QUERIED = 0,
144 OUTPUT_PROTECTION_NO_EXTERNAL_LINK = 1,
145 OUTPUT_PROTECTION_ALL_EXTERNAL_LINKS_PROTECTED = 2,
146 OUTPUT_PROTECTION_MAX = 3
149 typedef linked_ptr<DecryptedBlockImpl> LinkedDecryptedBlock;
150 typedef linked_ptr<VideoFrameImpl> LinkedVideoFrame;
151 typedef linked_ptr<AudioFramesImpl> LinkedAudioFrames;
153 struct SessionError {
154 SessionError(cdm::Error error,
155 uint32_t system_code,
156 std::string error_description);
158 uint32_t system_code;
159 std::string error_description;
162 bool CreateCdmInstance(const std::string& key_system);
164 // <code>PPB_ContentDecryptor_Private</code> dispatchers. These are passed to
165 // <code>callback_factory_</code> to ensure that calls into
166 // <code>PPP_ContentDecryptor_Private</code> are asynchronous.
167 void SendPromiseResolvedInternal(int32_t result, uint32_t promise_id);
168 void SendPromiseResolvedWithSessionInternal(
171 const std::string& web_session_id);
172 void SendPromiseResolvedWithUsableKeyIdsInternal(
175 std::vector<std::vector<uint8> > key_ids);
176 void SendPromiseRejectedInternal(int32_t result,
178 const SessionError& error);
179 void SendSessionMessageInternal(int32_t result,
180 const std::string& web_session_id,
181 const std::vector<uint8>& message,
182 const std::string& destination_url);
183 void SendSessionReadyInternal(int32_t result,
184 const std::string& web_session_id);
185 void SendSessionClosedInternal(int32_t result,
186 const std::string& web_session_id);
187 void SendSessionErrorInternal(int32_t result,
188 const std::string& web_session_id,
189 const SessionError& error);
190 void SendSessionUsableKeysChangeInternal(int32_t result,
191 const std::string& web_session_id,
192 bool has_additional_usable_key);
193 void SendExpirationChangeInternal(int32_t result,
194 const std::string& web_session_id,
195 cdm::Time new_expiry_time);
196 void RejectPromise(uint32_t promise_id,
198 uint32_t system_code,
199 const std::string& error_message);
201 void DeliverBlock(int32_t result,
202 const cdm::Status& status,
203 const LinkedDecryptedBlock& decrypted_block,
204 const PP_DecryptTrackingInfo& tracking_info);
205 void DecoderInitializeDone(int32_t result,
206 PP_DecryptorStreamType decoder_type,
209 void DecoderDeinitializeDone(int32_t result,
210 PP_DecryptorStreamType decoder_type,
211 uint32_t request_id);
212 void DecoderResetDone(int32_t result,
213 PP_DecryptorStreamType decoder_type,
214 uint32_t request_id);
215 void DeliverFrame(int32_t result,
216 const cdm::Status& status,
217 const LinkedVideoFrame& video_frame,
218 const PP_DecryptTrackingInfo& tracking_info);
219 void DeliverSamples(int32_t result,
220 const cdm::Status& status,
221 const LinkedAudioFrames& audio_frames,
222 const PP_DecryptTrackingInfo& tracking_info);
224 // Helper for SetTimer().
225 void TimerExpired(int32_t result, void* context);
227 bool IsValidVideoFrame(const LinkedVideoFrame& video_frame);
229 // Callback to report |file_size_bytes| of the first file read by FileIO.
230 void OnFirstFileRead(int32_t file_size_bytes);
233 // Logs the given message to the JavaScript console associated with the
234 // CDM adapter instance. The name of the CDM adapter issuing the log message
235 // will be automatically prepended to the message.
236 void LogToConsole(const pp::Var& value);
237 #endif // !defined(NDEBUG)
239 #if defined(OS_CHROMEOS)
240 void ReportOutputProtectionUMA(OutputProtectionStatus status);
241 void ReportOutputProtectionQuery();
242 void ReportOutputProtectionQueryResult();
244 struct PepperPlatformChallengeResponse {
246 pp::Var signed_data_signature;
247 pp::Var platform_key_certificate;
250 void SendPlatformChallengeDone(
252 const linked_ptr<PepperPlatformChallengeResponse>& response);
253 void EnableProtectionDone(int32_t result);
254 void QueryOutputProtectionStatusDone(int32_t result);
256 pp::OutputProtection_Private output_protection_;
257 pp::PlatformVerification platform_verification_;
259 // Same as above, these are only read by QueryOutputProtectionStatusDone().
260 uint32_t output_link_mask_;
261 uint32_t output_protection_mask_;
262 bool query_output_protection_in_progress_;
264 // Tracks whether an output protection query and a positive query result (no
265 // unprotected external link) have been reported to UMA.
266 bool uma_for_output_protection_query_reported_;
267 bool uma_for_output_protection_positive_result_reported_;
270 PpbBufferAllocator allocator_;
271 pp::CompletionCallbackFactory<CdmAdapter> callback_factory_;
272 linked_ptr<CdmWrapper> cdm_;
273 std::string key_system_;
275 // If the CDM returned kDeferredInitialization during InitializeAudioDecoder()
276 // or InitializeVideoDecoder(), the (Audio|Video)DecoderConfig.request_id is
277 // saved for the future call to OnDeferredInitializationDone().
278 bool deferred_initialize_audio_decoder_;
279 uint32_t deferred_audio_decoder_config_id_;
280 bool deferred_initialize_video_decoder_;
281 uint32_t deferred_video_decoder_config_id_;
283 uint32_t last_read_file_size_kb_;
284 bool file_size_uma_reported_;
286 DISALLOW_COPY_AND_ASSIGN(CdmAdapter);
291 #endif // MEDIA_CDM_PPAPI_CDM_ADAPTER_H_