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 CreateSession(uint32_t session_id
,
55 const std::string
& content_type
,
56 pp::VarArrayBuffer init_data
) OVERRIDE
;
57 virtual void LoadSession(uint32_t session_id
,
58 const std::string
& web_session_id
) OVERRIDE
;
59 virtual void UpdateSession(uint32_t session_id
,
60 pp::VarArrayBuffer response
) OVERRIDE
;
61 virtual void ReleaseSession(uint32_t session_id
) OVERRIDE
;
63 pp::Buffer_Dev encrypted_buffer
,
64 const PP_EncryptedBlockInfo
& encrypted_block_info
) OVERRIDE
;
65 virtual void InitializeAudioDecoder(
66 const PP_AudioDecoderConfig
& decoder_config
,
67 pp::Buffer_Dev extra_data_buffer
) OVERRIDE
;
68 virtual void InitializeVideoDecoder(
69 const PP_VideoDecoderConfig
& decoder_config
,
70 pp::Buffer_Dev extra_data_buffer
) OVERRIDE
;
71 virtual void DeinitializeDecoder(PP_DecryptorStreamType decoder_type
,
72 uint32_t request_id
) OVERRIDE
;
73 virtual void ResetDecoder(PP_DecryptorStreamType decoder_type
,
74 uint32_t request_id
) OVERRIDE
;
75 virtual void DecryptAndDecode(
76 PP_DecryptorStreamType decoder_type
,
77 pp::Buffer_Dev encrypted_buffer
,
78 const PP_EncryptedBlockInfo
& encrypted_block_info
) OVERRIDE
;
80 // cdm::Host implementation.
81 virtual cdm::Buffer
* Allocate(uint32_t capacity
) OVERRIDE
;
82 virtual void SetTimer(int64_t delay_ms
, void* context
) OVERRIDE
;
83 virtual double GetCurrentWallTimeInSeconds() OVERRIDE
;
84 virtual void OnSessionCreated(uint32_t session_id
,
85 const char* web_session_id
,
86 uint32_t web_session_id_length
) OVERRIDE
;
87 virtual void OnSessionMessage(uint32_t session_id
,
89 uint32_t message_length
,
90 const char* destination_url
,
91 uint32_t destination_url_length
) OVERRIDE
;
92 virtual void OnSessionReady(uint32_t session_id
) OVERRIDE
;
93 virtual void OnSessionClosed(uint32_t session_id
) OVERRIDE
;
94 virtual void OnSessionError(uint32_t session_id
,
95 cdm::MediaKeyError error_code
,
96 uint32_t system_code
) OVERRIDE
;
97 virtual void SendPlatformChallenge(
98 const char* service_id
, uint32_t service_id_length
,
99 const char* challenge
, uint32_t challenge_length
) OVERRIDE
;
100 virtual void EnableOutputProtection(
101 uint32_t desired_protection_mask
) OVERRIDE
;
102 virtual void QueryOutputProtectionStatus() OVERRIDE
;
103 virtual void OnDeferredInitializationDone(
104 cdm::StreamType stream_type
,
105 cdm::Status decoder_status
) OVERRIDE
;
106 virtual cdm::FileIO
* CreateFileIO(cdm::FileIOClient
* client
) OVERRIDE
;
109 // These are reported to UMA server. Do not change the existing values!
110 enum OutputProtectionStatus
{
111 OUTPUT_PROTECTION_QUERIED
= 0,
112 OUTPUT_PROTECTION_NO_EXTERNAL_LINK
= 1,
113 OUTPUT_PROTECTION_ALL_EXTERNAL_LINKS_PROTECTED
= 2,
114 OUTPUT_PROTECTION_MAX
= 3
117 typedef linked_ptr
<DecryptedBlockImpl
> LinkedDecryptedBlock
;
118 typedef linked_ptr
<VideoFrameImpl
> LinkedVideoFrame
;
119 typedef linked_ptr
<AudioFramesImpl
> LinkedAudioFrames
;
121 bool CreateCdmInstance(const std::string
& key_system
);
123 // <code>PPB_ContentDecryptor_Private</code> dispatchers. These are passed to
124 // <code>callback_factory_</code> to ensure that calls into
125 // <code>PPP_ContentDecryptor_Private</code> are asynchronous.
126 void SendSessionCreatedInternal(int32_t result
,
128 const std::string
& web_session_id
);
129 void SendSessionMessageInternal(int32_t result
,
131 const std::vector
<uint8
>& message
,
132 const std::string
& default_url
);
133 void SendSessionReadyInternal(int32_t result
, uint32_t session_id
);
134 void SendSessionClosedInternal(int32_t result
, uint32_t session_id
);
135 void SendSessionErrorInternal(int32_t result
,
137 cdm::MediaKeyError error_code
,
138 uint32_t system_code
);
140 void DeliverBlock(int32_t result
,
141 const cdm::Status
& status
,
142 const LinkedDecryptedBlock
& decrypted_block
,
143 const PP_DecryptTrackingInfo
& tracking_info
);
144 void DecoderInitializeDone(int32_t result
,
145 PP_DecryptorStreamType decoder_type
,
148 void DecoderDeinitializeDone(int32_t result
,
149 PP_DecryptorStreamType decoder_type
,
150 uint32_t request_id
);
151 void DecoderResetDone(int32_t result
,
152 PP_DecryptorStreamType decoder_type
,
153 uint32_t request_id
);
154 void DeliverFrame(int32_t result
,
155 const cdm::Status
& status
,
156 const LinkedVideoFrame
& video_frame
,
157 const PP_DecryptTrackingInfo
& tracking_info
);
158 void DeliverSamples(int32_t result
,
159 const cdm::Status
& status
,
160 const LinkedAudioFrames
& audio_frames
,
161 const PP_DecryptTrackingInfo
& tracking_info
);
163 // Helper for SetTimer().
164 void TimerExpired(int32_t result
, void* context
);
166 bool IsValidVideoFrame(const LinkedVideoFrame
& video_frame
);
169 // Logs the given message to the JavaScript console associated with the
170 // CDM adapter instance. The name of the CDM adapter issuing the log message
171 // will be automatically prepended to the message.
172 void LogToConsole(const pp::Var
& value
);
173 #endif // !defined(NDEBUG)
175 #if defined(OS_CHROMEOS)
176 void ReportOutputProtectionUMA(OutputProtectionStatus status
);
177 void ReportOutputProtectionQuery();
178 void ReportOutputProtectionQueryResult();
180 void SendPlatformChallengeDone(int32_t result
);
181 void EnableProtectionDone(int32_t result
);
182 void QueryOutputProtectionStatusDone(int32_t result
);
184 pp::OutputProtection_Private output_protection_
;
185 pp::PlatformVerification platform_verification_
;
187 // Since PPAPI doesn't provide handlers for CompletionCallbacks with more than
188 // one output we need to manage our own. These values are only read by
189 // SendPlatformChallengeDone().
190 pp::Var signed_data_output_
;
191 pp::Var signed_data_signature_output_
;
192 pp::Var platform_key_certificate_output_
;
193 bool challenge_in_progress_
;
195 // Same as above, these are only read by QueryOutputProtectionStatusDone().
196 uint32_t output_link_mask_
;
197 uint32_t output_protection_mask_
;
198 bool query_output_protection_in_progress_
;
200 // Tracks whether an output protection query and a positive query result (no
201 // unprotected external link) have been reported to UMA.
202 bool uma_for_output_protection_query_reported_
;
203 bool uma_for_output_protection_positive_result_reported_
;
206 PpbBufferAllocator allocator_
;
207 pp::CompletionCallbackFactory
<CdmAdapter
> callback_factory_
;
208 linked_ptr
<CdmWrapper
> cdm_
;
209 std::string key_system_
;
211 // If the CDM returned kDeferredInitialization during InitializeAudioDecoder()
212 // or InitializeVideoDecoder(), the (Audio|Video)DecoderConfig.request_id is
213 // saved for the future call to OnDeferredInitializationDone().
214 bool deferred_initialize_audio_decoder_
;
215 uint32_t deferred_audio_decoder_config_id_
;
216 bool deferred_initialize_video_decoder_
;
217 uint32_t deferred_video_decoder_config_id_
;
219 DISALLOW_COPY_AND_ASSIGN(CdmAdapter
);
224 #endif // MEDIA_CDM_PPAPI_CDM_ADAPTER_H_