OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef MEDIA_CDM_PPAPI_CDM_ADAPTER_H_ | |
6 #define MEDIA_CDM_PPAPI_CDM_ADAPTER_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/compiler_specific.h" | |
13 #include "build/build_config.h" | |
14 #include "media/cdm/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" | |
25 | |
26 #if defined(OS_CHROMEOS) | |
27 #include "ppapi/cpp/private/output_protection_private.h" | |
28 #include "ppapi/cpp/private/platform_verification.h" | |
29 #endif | |
30 | |
31 namespace media { | |
32 | |
33 // GetCdmHostFunc implementation. | |
34 void* GetCdmHost(int host_interface_version, void* user_data); | |
35 | |
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, | |
40 public cdm::Host_7, | |
41 public cdm::Host_8 { | |
42 public: | |
43 CdmAdapter(PP_Instance instance, pp::Module* module); | |
44 virtual ~CdmAdapter(); | |
45 | |
46 // pp::Instance implementation. | |
47 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { | |
48 return true; | |
49 } | |
50 | |
51 // PPP_ContentDecryptor_Private implementation. | |
52 // Note: Results of calls to these methods must be reported through the | |
53 // PPB_ContentDecryptor_Private interface. | |
54 void Initialize(uint32_t promise_id, | |
55 const std::string& key_system, | |
56 bool allow_distinctive_identifier, | |
57 bool allow_persistent_state) override; | |
58 void SetServerCertificate(uint32_t promise_id, | |
59 pp::VarArrayBuffer server_certificate) override; | |
60 void CreateSessionAndGenerateRequest(uint32_t promise_id, | |
61 PP_SessionType session_type, | |
62 PP_InitDataType init_data_type, | |
63 pp::VarArrayBuffer init_data) override; | |
64 void LoadSession(uint32_t promise_id, | |
65 PP_SessionType session_type, | |
66 const std::string& session_id) override; | |
67 void UpdateSession(uint32_t promise_id, | |
68 const std::string& session_id, | |
69 pp::VarArrayBuffer response) override; | |
70 void CloseSession(uint32_t promise_id, const std::string& session_id); | |
71 void RemoveSession(uint32_t promise_id, | |
72 const std::string& session_id) override; | |
73 void Decrypt(pp::Buffer_Dev encrypted_buffer, | |
74 const PP_EncryptedBlockInfo& encrypted_block_info) override; | |
75 void InitializeAudioDecoder(const PP_AudioDecoderConfig& decoder_config, | |
76 pp::Buffer_Dev extra_data_buffer) override; | |
77 void InitializeVideoDecoder(const PP_VideoDecoderConfig& decoder_config, | |
78 pp::Buffer_Dev extra_data_buffer) override; | |
79 void DeinitializeDecoder(PP_DecryptorStreamType decoder_type, | |
80 uint32_t request_id) override; | |
81 void ResetDecoder(PP_DecryptorStreamType decoder_type, | |
82 uint32_t request_id) override; | |
83 void DecryptAndDecode( | |
84 PP_DecryptorStreamType decoder_type, | |
85 pp::Buffer_Dev encrypted_buffer, | |
86 const PP_EncryptedBlockInfo& encrypted_block_info) override; | |
87 | |
88 // cdm::Host_7 and cdm::Host_8 implementation. | |
89 cdm::Buffer* Allocate(uint32_t capacity) override; | |
90 void SetTimer(int64_t delay_ms, void* context) override; | |
91 cdm::Time GetCurrentWallTime() override; | |
92 void OnResolveNewSessionPromise(uint32_t promise_id, | |
93 const char* session_id, | |
94 uint32_t session_id_size) override; | |
95 void OnResolvePromise(uint32_t promise_id) override; | |
96 void OnRejectPromise(uint32_t promise_id, | |
97 cdm::Error error, | |
98 uint32_t system_code, | |
99 const char* error_message, | |
100 uint32_t error_message_size) override; | |
101 void OnSessionMessage(const char* session_id, | |
102 uint32_t session_id_size, | |
103 cdm::MessageType message_type, | |
104 const char* message, | |
105 uint32_t message_size, | |
106 const char* legacy_destination_url, | |
107 uint32_t legacy_destination_url_size) override; | |
108 void OnSessionKeysChange(const char* session_id, | |
109 uint32_t session_id_size, | |
110 bool has_additional_usable_key, | |
111 const cdm::KeyInformation* keys_info, | |
112 uint32_t keys_info_count) override; | |
113 void OnExpirationChange(const char* session_id, | |
114 uint32_t session_id_size, | |
115 cdm::Time new_expiry_time) override; | |
116 void OnSessionClosed(const char* session_id, | |
117 uint32_t session_id_size) override; | |
118 void OnLegacySessionError(const char* session_id, | |
119 uint32_t session_id_size, | |
120 cdm::Error error, | |
121 uint32_t system_code, | |
122 const char* error_message, | |
123 uint32_t error_message_size) override; | |
124 void SendPlatformChallenge(const char* service_id, | |
125 uint32_t service_id_size, | |
126 const char* challenge, | |
127 uint32_t challenge_size) override; | |
128 void EnableOutputProtection(uint32_t desired_protection_mask) override; | |
129 void QueryOutputProtectionStatus() override; | |
130 void OnDeferredInitializationDone(cdm::StreamType stream_type, | |
131 cdm::Status decoder_status) override; | |
132 cdm::FileIO* CreateFileIO(cdm::FileIOClient* client) override; | |
133 | |
134 private: | |
135 // These are reported to UMA server. Do not change the existing values! | |
136 enum OutputProtectionStatus { | |
137 OUTPUT_PROTECTION_QUERIED = 0, | |
138 OUTPUT_PROTECTION_NO_EXTERNAL_LINK = 1, | |
139 OUTPUT_PROTECTION_ALL_EXTERNAL_LINKS_PROTECTED = 2, | |
140 OUTPUT_PROTECTION_MAX = 3 | |
141 }; | |
142 | |
143 typedef linked_ptr<DecryptedBlockImpl> LinkedDecryptedBlock; | |
144 typedef linked_ptr<VideoFrameImpl> LinkedVideoFrame; | |
145 typedef linked_ptr<AudioFramesImpl> LinkedAudioFrames; | |
146 | |
147 struct SessionError { | |
148 SessionError(cdm::Error error, | |
149 uint32_t system_code, | |
150 const std::string& error_description); | |
151 cdm::Error error; | |
152 uint32_t system_code; | |
153 std::string error_description; | |
154 }; | |
155 | |
156 struct SessionMessage { | |
157 SessionMessage(const std::string& session_id, | |
158 cdm::MessageType message_type, | |
159 const char* message, | |
160 uint32_t message_size, | |
161 const std::string& legacy_destination_url); | |
162 std::string session_id; | |
163 cdm::MessageType message_type; | |
164 std::vector<uint8_t> message; | |
165 std::string legacy_destination_url; | |
166 }; | |
167 | |
168 CdmWrapper* CreateCdmInstance(const std::string& key_system); | |
169 | |
170 // <code>PPB_ContentDecryptor_Private</code> dispatchers. These are passed to | |
171 // <code>callback_factory_</code> to ensure that calls into | |
172 // <code>PPP_ContentDecryptor_Private</code> are asynchronous. | |
173 void SendPromiseResolvedInternal(int32_t result, uint32_t promise_id); | |
174 void SendPromiseResolvedWithSessionInternal(int32_t result, | |
175 uint32_t promise_id, | |
176 const std::string& session_id); | |
177 void SendPromiseRejectedInternal(int32_t result, | |
178 uint32_t promise_id, | |
179 const SessionError& error); | |
180 void SendSessionMessageInternal(int32_t result, | |
181 const SessionMessage& message); | |
182 void SendSessionClosedInternal(int32_t result, const std::string& session_id); | |
183 void SendSessionErrorInternal(int32_t result, | |
184 const std::string& session_id, | |
185 const SessionError& error); | |
186 void SendSessionKeysChangeInternal( | |
187 int32_t result, | |
188 const std::string& session_id, | |
189 bool has_additional_usable_key, | |
190 const std::vector<PP_KeyInformation>& key_info); | |
191 void SendExpirationChangeInternal(int32_t result, | |
192 const std::string& session_id, | |
193 cdm::Time new_expiry_time); | |
194 void RejectPromise(uint32_t promise_id, | |
195 cdm::Error error, | |
196 uint32_t system_code, | |
197 const std::string& error_message); | |
198 | |
199 void DeliverBlock(int32_t result, | |
200 const cdm::Status& status, | |
201 const LinkedDecryptedBlock& decrypted_block, | |
202 const PP_DecryptTrackingInfo& tracking_info); | |
203 void DecoderInitializeDone(int32_t result, | |
204 PP_DecryptorStreamType decoder_type, | |
205 uint32_t request_id, | |
206 bool success); | |
207 void DecoderDeinitializeDone(int32_t result, | |
208 PP_DecryptorStreamType decoder_type, | |
209 uint32_t request_id); | |
210 void DecoderResetDone(int32_t result, | |
211 PP_DecryptorStreamType decoder_type, | |
212 uint32_t request_id); | |
213 void DeliverFrame(int32_t result, | |
214 const cdm::Status& status, | |
215 const LinkedVideoFrame& video_frame, | |
216 const PP_DecryptTrackingInfo& tracking_info); | |
217 void DeliverSamples(int32_t result, | |
218 const cdm::Status& status, | |
219 const LinkedAudioFrames& audio_frames, | |
220 const PP_DecryptTrackingInfo& tracking_info); | |
221 | |
222 // Helper for SetTimer(). | |
223 void TimerExpired(int32_t result, void* context); | |
224 | |
225 bool IsValidVideoFrame(const LinkedVideoFrame& video_frame); | |
226 | |
227 // Callback to report |file_size_bytes| of the first file read by FileIO. | |
228 void OnFirstFileRead(int32_t file_size_bytes); | |
229 | |
230 #if !defined(NDEBUG) | |
231 // Logs the given message to the JavaScript console associated with the | |
232 // CDM adapter instance. The name of the CDM adapter issuing the log message | |
233 // will be automatically prepended to the message. | |
234 void LogToConsole(const pp::Var& value); | |
235 #endif // !defined(NDEBUG) | |
236 | |
237 #if defined(OS_CHROMEOS) | |
238 void ReportOutputProtectionUMA(OutputProtectionStatus status); | |
239 void ReportOutputProtectionQuery(); | |
240 void ReportOutputProtectionQueryResult(); | |
241 | |
242 struct PepperPlatformChallengeResponse { | |
243 pp::Var signed_data; | |
244 pp::Var signed_data_signature; | |
245 pp::Var platform_key_certificate; | |
246 }; | |
247 | |
248 void SendPlatformChallengeDone( | |
249 int32_t result, | |
250 const linked_ptr<PepperPlatformChallengeResponse>& response); | |
251 void EnableProtectionDone(int32_t result); | |
252 void QueryOutputProtectionStatusDone(int32_t result); | |
253 | |
254 pp::OutputProtection_Private output_protection_; | |
255 pp::PlatformVerification platform_verification_; | |
256 | |
257 // Same as above, these are only read by QueryOutputProtectionStatusDone(). | |
258 uint32_t output_link_mask_; | |
259 uint32_t output_protection_mask_; | |
260 bool query_output_protection_in_progress_; | |
261 | |
262 // Tracks whether an output protection query and a positive query result (no | |
263 // unprotected external link) have been reported to UMA. | |
264 bool uma_for_output_protection_query_reported_; | |
265 bool uma_for_output_protection_positive_result_reported_; | |
266 #endif | |
267 | |
268 PpbBufferAllocator allocator_; | |
269 pp::CompletionCallbackFactory<CdmAdapter> callback_factory_; | |
270 linked_ptr<CdmWrapper> cdm_; | |
271 std::string key_system_; | |
272 bool allow_distinctive_identifier_; | |
273 bool allow_persistent_state_; | |
274 | |
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_; | |
282 | |
283 uint32_t last_read_file_size_kb_; | |
284 bool file_size_uma_reported_; | |
285 | |
286 DISALLOW_COPY_AND_ASSIGN(CdmAdapter); | |
287 }; | |
288 | |
289 } // namespace media | |
290 | |
291 #endif // MEDIA_CDM_PPAPI_CDM_ADAPTER_H_ | |
OLD | NEW |