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
,
43 CdmAdapter(PP_Instance instance
, pp::Module
* module
);
44 virtual ~CdmAdapter();
46 // pp::Instance implementation.
47 virtual bool Init(uint32_t argc
, const char* argn
[], const char* argv
[]) {
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
;
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
,
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
,
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
,
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
;
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
143 typedef linked_ptr
<DecryptedBlockImpl
> LinkedDecryptedBlock
;
144 typedef linked_ptr
<VideoFrameImpl
> LinkedVideoFrame
;
145 typedef linked_ptr
<AudioFramesImpl
> LinkedAudioFrames
;
147 struct SessionError
{
148 SessionError(cdm::Error error
,
149 uint32_t system_code
,
150 const std::string
& error_description
);
152 uint32_t system_code
;
153 std::string error_description
;
156 struct SessionMessage
{
157 SessionMessage(const std::string
& session_id
,
158 cdm::MessageType message_type
,
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
;
168 CdmWrapper
* CreateCdmInstance(const std::string
& key_system
);
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
,
176 const std::string
& session_id
);
177 void SendPromiseRejectedInternal(int32_t result
,
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(
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
,
196 uint32_t system_code
,
197 const std::string
& error_message
);
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
,
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
);
222 // Helper for SetTimer().
223 void TimerExpired(int32_t result
, void* context
);
225 bool IsValidVideoFrame(const LinkedVideoFrame
& video_frame
);
227 // Callback to report |file_size_bytes| of the first file read by FileIO.
228 void OnFirstFileRead(int32_t file_size_bytes
);
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)
237 #if defined(OS_CHROMEOS)
238 void ReportOutputProtectionUMA(OutputProtectionStatus status
);
239 void ReportOutputProtectionQuery();
240 void ReportOutputProtectionQueryResult();
242 struct PepperPlatformChallengeResponse
{
244 pp::Var signed_data_signature
;
245 pp::Var platform_key_certificate
;
248 void SendPlatformChallengeDone(
250 const linked_ptr
<PepperPlatformChallengeResponse
>& response
);
251 void EnableProtectionDone(int32_t result
);
252 void QueryOutputProtectionStatusDone(int32_t result
);
254 pp::OutputProtection_Private output_protection_
;
255 pp::PlatformVerification platform_verification_
;
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_
;
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_
;
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_
;
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_