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(const std::string
& key_system
,
55 bool allow_distinctive_identifier
,
56 bool allow_persistent_state
) override
;
57 void SetServerCertificate(uint32_t promise_id
,
58 pp::VarArrayBuffer server_certificate
) override
;
59 void CreateSessionAndGenerateRequest(uint32_t promise_id
,
60 PP_SessionType session_type
,
61 PP_InitDataType init_data_type
,
62 pp::VarArrayBuffer init_data
) override
;
63 void LoadSession(uint32_t promise_id
,
64 PP_SessionType session_type
,
65 const std::string
& session_id
) override
;
66 void UpdateSession(uint32_t promise_id
,
67 const std::string
& session_id
,
68 pp::VarArrayBuffer response
) override
;
69 void CloseSession(uint32_t promise_id
, const std::string
& session_id
);
70 void RemoveSession(uint32_t promise_id
,
71 const std::string
& session_id
) override
;
72 void Decrypt(pp::Buffer_Dev encrypted_buffer
,
73 const PP_EncryptedBlockInfo
& encrypted_block_info
) override
;
74 void InitializeAudioDecoder(const PP_AudioDecoderConfig
& decoder_config
,
75 pp::Buffer_Dev extra_data_buffer
) override
;
76 void InitializeVideoDecoder(const PP_VideoDecoderConfig
& decoder_config
,
77 pp::Buffer_Dev extra_data_buffer
) override
;
78 void DeinitializeDecoder(PP_DecryptorStreamType decoder_type
,
79 uint32_t request_id
) override
;
80 void ResetDecoder(PP_DecryptorStreamType decoder_type
,
81 uint32_t request_id
) override
;
82 void DecryptAndDecode(
83 PP_DecryptorStreamType decoder_type
,
84 pp::Buffer_Dev encrypted_buffer
,
85 const PP_EncryptedBlockInfo
& encrypted_block_info
) override
;
87 // cdm::Host_7 and cdm::Host_8 implementation.
88 cdm::Buffer
* Allocate(uint32_t capacity
) override
;
89 void SetTimer(int64_t delay_ms
, void* context
) override
;
90 cdm::Time
GetCurrentWallTime() override
;
91 void OnResolveNewSessionPromise(uint32_t promise_id
,
92 const char* session_id
,
93 uint32_t session_id_size
) override
;
94 void OnResolvePromise(uint32_t promise_id
) override
;
95 void OnRejectPromise(uint32_t promise_id
,
98 const char* error_message
,
99 uint32_t error_message_size
) override
;
100 void OnSessionMessage(const char* session_id
,
101 uint32_t session_id_size
,
102 cdm::MessageType message_type
,
104 uint32_t message_size
,
105 const char* legacy_destination_url
,
106 uint32_t legacy_destination_url_size
) override
;
107 void OnSessionKeysChange(const char* session_id
,
108 uint32_t session_id_size
,
109 bool has_additional_usable_key
,
110 const cdm::KeyInformation
* keys_info
,
111 uint32_t keys_info_count
) override
;
112 void OnExpirationChange(const char* session_id
,
113 uint32_t session_id_size
,
114 cdm::Time new_expiry_time
) override
;
115 void OnSessionClosed(const char* session_id
,
116 uint32_t session_id_size
) override
;
117 void OnLegacySessionError(const char* session_id
,
118 uint32_t session_id_size
,
120 uint32_t system_code
,
121 const char* error_message
,
122 uint32_t error_message_size
) override
;
123 void SendPlatformChallenge(const char* service_id
,
124 uint32_t service_id_size
,
125 const char* challenge
,
126 uint32_t challenge_size
) override
;
127 void EnableOutputProtection(uint32_t desired_protection_mask
) override
;
128 void QueryOutputProtectionStatus() override
;
129 void OnDeferredInitializationDone(cdm::StreamType stream_type
,
130 cdm::Status decoder_status
) override
;
131 cdm::FileIO
* CreateFileIO(cdm::FileIOClient
* client
) override
;
134 // These are reported to UMA server. Do not change the existing values!
135 enum OutputProtectionStatus
{
136 OUTPUT_PROTECTION_QUERIED
= 0,
137 OUTPUT_PROTECTION_NO_EXTERNAL_LINK
= 1,
138 OUTPUT_PROTECTION_ALL_EXTERNAL_LINKS_PROTECTED
= 2,
139 OUTPUT_PROTECTION_MAX
= 3
142 typedef linked_ptr
<DecryptedBlockImpl
> LinkedDecryptedBlock
;
143 typedef linked_ptr
<VideoFrameImpl
> LinkedVideoFrame
;
144 typedef linked_ptr
<AudioFramesImpl
> LinkedAudioFrames
;
146 struct SessionError
{
147 SessionError(cdm::Error error
,
148 uint32_t system_code
,
149 const std::string
& error_description
);
151 uint32_t system_code
;
152 std::string error_description
;
155 struct SessionMessage
{
156 SessionMessage(const std::string
& session_id
,
157 cdm::MessageType message_type
,
159 uint32_t message_size
,
160 const std::string
& legacy_destination_url
);
161 std::string session_id
;
162 cdm::MessageType message_type
;
163 std::vector
<uint8_t> message
;
164 std::string legacy_destination_url
;
167 bool CreateCdmInstance(const std::string
& key_system
);
169 // <code>PPB_ContentDecryptor_Private</code> dispatchers. These are passed to
170 // <code>callback_factory_</code> to ensure that calls into
171 // <code>PPP_ContentDecryptor_Private</code> are asynchronous.
172 void SendPromiseResolvedInternal(int32_t result
, uint32_t promise_id
);
173 void SendPromiseResolvedWithSessionInternal(int32_t result
,
175 const std::string
& session_id
);
176 void SendPromiseRejectedInternal(int32_t result
,
178 const SessionError
& error
);
179 void SendSessionMessageInternal(int32_t result
,
180 const SessionMessage
& message
);
181 void SendSessionClosedInternal(int32_t result
, const std::string
& session_id
);
182 void SendSessionErrorInternal(int32_t result
,
183 const std::string
& session_id
,
184 const SessionError
& error
);
185 void SendSessionKeysChangeInternal(
187 const std::string
& session_id
,
188 bool has_additional_usable_key
,
189 const std::vector
<PP_KeyInformation
>& key_info
);
190 void SendExpirationChangeInternal(int32_t result
,
191 const std::string
& session_id
,
192 cdm::Time new_expiry_time
);
193 void RejectPromise(uint32_t promise_id
,
195 uint32_t system_code
,
196 const std::string
& error_message
);
198 void DeliverBlock(int32_t result
,
199 const cdm::Status
& status
,
200 const LinkedDecryptedBlock
& decrypted_block
,
201 const PP_DecryptTrackingInfo
& tracking_info
);
202 void DecoderInitializeDone(int32_t result
,
203 PP_DecryptorStreamType decoder_type
,
206 void DecoderDeinitializeDone(int32_t result
,
207 PP_DecryptorStreamType decoder_type
,
208 uint32_t request_id
);
209 void DecoderResetDone(int32_t result
,
210 PP_DecryptorStreamType decoder_type
,
211 uint32_t request_id
);
212 void DeliverFrame(int32_t result
,
213 const cdm::Status
& status
,
214 const LinkedVideoFrame
& video_frame
,
215 const PP_DecryptTrackingInfo
& tracking_info
);
216 void DeliverSamples(int32_t result
,
217 const cdm::Status
& status
,
218 const LinkedAudioFrames
& audio_frames
,
219 const PP_DecryptTrackingInfo
& tracking_info
);
221 // Helper for SetTimer().
222 void TimerExpired(int32_t result
, void* context
);
224 bool IsValidVideoFrame(const LinkedVideoFrame
& video_frame
);
226 // Callback to report |file_size_bytes| of the first file read by FileIO.
227 void OnFirstFileRead(int32_t file_size_bytes
);
230 // Logs the given message to the JavaScript console associated with the
231 // CDM adapter instance. The name of the CDM adapter issuing the log message
232 // will be automatically prepended to the message.
233 void LogToConsole(const pp::Var
& value
);
234 #endif // !defined(NDEBUG)
236 #if defined(OS_CHROMEOS)
237 void ReportOutputProtectionUMA(OutputProtectionStatus status
);
238 void ReportOutputProtectionQuery();
239 void ReportOutputProtectionQueryResult();
241 struct PepperPlatformChallengeResponse
{
243 pp::Var signed_data_signature
;
244 pp::Var platform_key_certificate
;
247 void SendPlatformChallengeDone(
249 const linked_ptr
<PepperPlatformChallengeResponse
>& response
);
250 void EnableProtectionDone(int32_t result
);
251 void QueryOutputProtectionStatusDone(int32_t result
);
253 pp::OutputProtection_Private output_protection_
;
254 pp::PlatformVerification platform_verification_
;
256 // Same as above, these are only read by QueryOutputProtectionStatusDone().
257 uint32_t output_link_mask_
;
258 uint32_t output_protection_mask_
;
259 bool query_output_protection_in_progress_
;
261 // Tracks whether an output protection query and a positive query result (no
262 // unprotected external link) have been reported to UMA.
263 bool uma_for_output_protection_query_reported_
;
264 bool uma_for_output_protection_positive_result_reported_
;
267 PpbBufferAllocator allocator_
;
268 pp::CompletionCallbackFactory
<CdmAdapter
> callback_factory_
;
269 linked_ptr
<CdmWrapper
> cdm_
;
270 std::string key_system_
;
271 bool allow_distinctive_identifier_
;
272 bool allow_persistent_state_
;
274 // If the CDM returned kDeferredInitialization during InitializeAudioDecoder()
275 // or InitializeVideoDecoder(), the (Audio|Video)DecoderConfig.request_id is
276 // saved for the future call to OnDeferredInitializationDone().
277 bool deferred_initialize_audio_decoder_
;
278 uint32_t deferred_audio_decoder_config_id_
;
279 bool deferred_initialize_video_decoder_
;
280 uint32_t deferred_video_decoder_config_id_
;
282 uint32_t last_read_file_size_kb_
;
283 bool file_size_uma_reported_
;
285 DISALLOW_COPY_AND_ASSIGN(CdmAdapter
);
290 #endif // MEDIA_CDM_PPAPI_CDM_ADAPTER_H_