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 // TODO(jrummell): |can_challenge_platform| should be removed.
55 virtual void Initialize(const std::string
& key_system
,
56 bool can_challenge_platform
) OVERRIDE
;
57 virtual void GenerateKeyRequest(const std::string
& type
,
58 pp::VarArrayBuffer init_data
) OVERRIDE
;
59 virtual void AddKey(const std::string
& session_id
,
60 pp::VarArrayBuffer key
,
61 pp::VarArrayBuffer init_data
) OVERRIDE
;
62 virtual void CancelKeyRequest(const std::string
& session_id
) OVERRIDE
;
64 pp::Buffer_Dev encrypted_buffer
,
65 const PP_EncryptedBlockInfo
& encrypted_block_info
) OVERRIDE
;
66 virtual void InitializeAudioDecoder(
67 const PP_AudioDecoderConfig
& decoder_config
,
68 pp::Buffer_Dev extra_data_buffer
) OVERRIDE
;
69 virtual void InitializeVideoDecoder(
70 const PP_VideoDecoderConfig
& decoder_config
,
71 pp::Buffer_Dev extra_data_buffer
) OVERRIDE
;
72 virtual void DeinitializeDecoder(PP_DecryptorStreamType decoder_type
,
73 uint32_t request_id
) OVERRIDE
;
74 virtual void ResetDecoder(PP_DecryptorStreamType decoder_type
,
75 uint32_t request_id
) OVERRIDE
;
76 virtual void DecryptAndDecode(
77 PP_DecryptorStreamType decoder_type
,
78 pp::Buffer_Dev encrypted_buffer
,
79 const PP_EncryptedBlockInfo
& encrypted_block_info
) OVERRIDE
;
81 // cdm::Host implementation.
82 virtual cdm::Buffer
* Allocate(uint32_t capacity
) OVERRIDE
;
83 virtual void SetTimer(int64_t delay_ms
, void* context
) OVERRIDE
;
84 virtual double GetCurrentWallTimeInSeconds() OVERRIDE
;
85 virtual void SendKeyMessage(
86 const char* session_id
, uint32_t session_id_length
,
87 const char* message
, uint32_t message_length
,
88 const char* default_url
, uint32_t default_url_length
) OVERRIDE
;
89 virtual void SendKeyError(const char* session_id
,
90 uint32_t session_id_length
,
91 cdm::MediaKeyError error_code
,
92 uint32_t system_code
) OVERRIDE
;
93 virtual void GetPrivateData(int32_t* instance
,
94 GetPrivateInterface
* get_interface
) OVERRIDE
;
96 // cdm::Host_2 implementation.
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
;
109 SessionInfo(const std::string
& key_system_in
,
110 const std::string
& session_id_in
)
111 : key_system(key_system_in
),
112 session_id(session_id_in
) {}
113 const std::string key_system
;
114 const std::string session_id
;
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 void SendUnknownKeyError(const std::string
& key_system
,
124 const std::string
& session_id
);
126 void SendKeyAdded(const std::string
& key_system
,
127 const std::string
& session_id
);
129 // TODO(jrummell): Drop the |key_system| parameter.
130 void SendKeyErrorInternal(const std::string
& key_system
,
131 const std::string
& session_id
,
132 cdm::MediaKeyError error_code
,
133 uint32_t system_code
);
135 // <code>PPB_ContentDecryptor_Private</code> dispatchers. These are passed to
136 // <code>callback_factory_</code> to ensure that calls into
137 // <code>PPP_ContentDecryptor_Private</code> are asynchronous.
138 void KeyAdded(int32_t result
, const SessionInfo
& session_info
);
139 void KeyMessage(int32_t result
,
140 const SessionInfo
& session_info
,
141 const std::vector
<uint8
>& message
,
142 const std::string
& default_url
);
143 void KeyError(int32_t result
,
144 const SessionInfo
& session_info
,
145 cdm::MediaKeyError error_code
,
146 uint32_t system_code
);
147 void DeliverBlock(int32_t result
,
148 const cdm::Status
& status
,
149 const LinkedDecryptedBlock
& decrypted_block
,
150 const PP_DecryptTrackingInfo
& tracking_info
);
151 void DecoderInitializeDone(int32_t result
,
152 PP_DecryptorStreamType decoder_type
,
155 void DecoderDeinitializeDone(int32_t result
,
156 PP_DecryptorStreamType decoder_type
,
157 uint32_t request_id
);
158 void DecoderResetDone(int32_t result
,
159 PP_DecryptorStreamType decoder_type
,
160 uint32_t request_id
);
161 void DeliverFrame(int32_t result
,
162 const cdm::Status
& status
,
163 const LinkedVideoFrame
& video_frame
,
164 const PP_DecryptTrackingInfo
& tracking_info
);
165 void DeliverSamples(int32_t result
,
166 const cdm::Status
& status
,
167 const LinkedAudioFrames
& audio_frames
,
168 const PP_DecryptTrackingInfo
& tracking_info
);
170 // Helper for SetTimer().
171 void TimerExpired(int32_t result
, void* context
);
173 bool IsValidVideoFrame(const LinkedVideoFrame
& video_frame
);
176 #if defined(OS_CHROMEOS)
177 void SendPlatformChallengeDone(int32_t result
);
178 void EnableProtectionDone(int32_t result
);
179 void QueryOutputProtectionStatusDone(int32_t result
);
181 pp::OutputProtection_Private output_protection_
;
182 pp::PlatformVerification platform_verification_
;
184 // Since PPAPI doesn't provide handlers for CompletionCallbacks with more than
185 // one output we need to manage our own. These values are only read by
186 // SendPlatformChallengeDone().
187 pp::Var signed_data_output_
;
188 pp::Var signed_data_signature_output_
;
189 pp::Var platform_key_certificate_output_
;
190 bool challenge_in_progress_
;
192 // Same as above, these are only read by QueryOutputProtectionStatusDone().
193 uint32_t output_link_mask_
;
194 uint32_t output_protection_mask_
;
195 bool query_output_protection_in_progress_
;
198 PpbBufferAllocator allocator_
;
199 pp::CompletionCallbackFactory
<CdmAdapter
> callback_factory_
;
200 linked_ptr
<CdmWrapper
> cdm_
;
201 std::string key_system_
;
203 // If the CDM returned kDeferredInitialization during InitializeAudioDecoder()
204 // or InitializeVideoDecoder(), the (Audio|Video)DecoderConfig.request_id is
205 // saved for the future call to OnDeferredInitializationDone().
206 bool deferred_initialize_audio_decoder_
;
207 uint32_t deferred_audio_decoder_config_id_
;
208 bool deferred_initialize_video_decoder_
;
209 uint32_t deferred_video_decoder_config_id_
;
211 DISALLOW_COPY_AND_ASSIGN(CdmAdapter
);
216 #endif // MEDIA_CDM_PPAPI_CDM_ADAPTER_H_