Implement MoveFileLocal (with creating a snapshot).
[chromium-blink-merge.git] / media / cdm / ppapi / cdm_wrapper.h
blob56bb82e2d937e932fbb0131ebcddc92661caddbd
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_WRAPPER_H_
6 #define MEDIA_CDM_PPAPI_CDM_WRAPPER_H_
8 #include <map>
9 #include <queue>
10 #include <string>
12 #include "base/basictypes.h"
13 #include "media/cdm/ppapi/api/content_decryption_module.h"
14 #include "media/cdm/ppapi/cdm_helpers.h"
15 #include "media/cdm/ppapi/supported_cdm_versions.h"
16 #include "ppapi/cpp/logging.h"
18 namespace media {
20 // CdmWrapper wraps different versions of ContentDecryptionModule interfaces and
21 // exposes a common interface to the caller.
23 // The caller should call CdmWrapper::Create() to create a CDM instance.
24 // CdmWrapper will first try to create a CDM instance that supports the latest
25 // CDM interface (ContentDecryptionModule). If such an instance cannot be
26 // created (e.g. an older CDM was loaded), CdmWrapper will try to create a CDM
27 // that supports an older version of CDM interface (e.g.
28 // ContentDecryptionModule_*). Internally CdmWrapper converts the CdmWrapper
29 // calls to corresponding ContentDecryptionModule calls.
31 // Note that CdmWrapper interface always reflects the latest state of content
32 // decryption related PPAPI APIs (e.g. pp::ContentDecryptor_Private).
34 // Since this file is highly templated and default implementations are short
35 // (just a shim layer in most cases), everything is done in this header file.
36 class CdmWrapper {
37 public:
38 static CdmWrapper* Create(const char* key_system,
39 uint32_t key_system_size,
40 GetCdmHostFunc get_cdm_host_func,
41 void* user_data);
43 virtual ~CdmWrapper() {};
45 virtual void SetServerCertificate(uint32_t promise_id,
46 const uint8_t* server_certificate_data,
47 uint32_t server_certificate_data_size) = 0;
48 virtual void CreateSessionAndGenerateRequest(uint32_t promise_id,
49 cdm::SessionType session_type,
50 const char* init_data_type,
51 uint32_t init_data_type_size,
52 const uint8_t* init_data,
53 uint32_t init_data_size) = 0;
54 virtual void LoadSession(uint32_t promise_id,
55 cdm::SessionType session_type,
56 const char* session_id,
57 uint32_t session_id_size) = 0;
58 virtual void UpdateSession(uint32_t promise_id,
59 const char* session_id,
60 uint32_t session_id_size,
61 const uint8_t* response,
62 uint32_t response_size) = 0;
63 virtual void CloseSession(uint32_t promise_id,
64 const char* session_id,
65 uint32_t session_id_size) = 0;
66 virtual void RemoveSession(uint32_t promise_id,
67 const char* session_id,
68 uint32_t session_id_size) = 0;
69 virtual void TimerExpired(void* context) = 0;
70 virtual cdm::Status Decrypt(const cdm::InputBuffer& encrypted_buffer,
71 cdm::DecryptedBlock* decrypted_buffer) = 0;
72 virtual cdm::Status InitializeAudioDecoder(
73 const cdm::AudioDecoderConfig& audio_decoder_config) = 0;
74 virtual cdm::Status InitializeVideoDecoder(
75 const cdm::VideoDecoderConfig& video_decoder_config) = 0;
76 virtual void DeinitializeDecoder(cdm::StreamType decoder_type) = 0;
77 virtual void ResetDecoder(cdm::StreamType decoder_type) = 0;
78 virtual cdm::Status DecryptAndDecodeFrame(
79 const cdm::InputBuffer& encrypted_buffer,
80 cdm::VideoFrame* video_frame) = 0;
81 virtual cdm::Status DecryptAndDecodeSamples(
82 const cdm::InputBuffer& encrypted_buffer,
83 cdm::AudioFrames* audio_frames) = 0;
84 virtual void OnPlatformChallengeResponse(
85 const cdm::PlatformChallengeResponse& response) = 0;
86 virtual void OnQueryOutputProtectionStatus(
87 cdm::QueryResult result,
88 uint32_t link_mask,
89 uint32_t output_protection_mask) = 0;
91 protected:
92 CdmWrapper() {}
94 private:
95 DISALLOW_COPY_AND_ASSIGN(CdmWrapper);
98 // Template class that does the CdmWrapper -> CdmInterface conversion. Default
99 // implementations are provided. Any methods that need special treatment should
100 // be specialized.
101 template <class CdmInterface>
102 class CdmWrapperImpl : public CdmWrapper {
103 public:
104 static CdmWrapper* Create(const char* key_system,
105 uint32_t key_system_size,
106 GetCdmHostFunc get_cdm_host_func,
107 void* user_data) {
108 void* cdm_instance = ::CreateCdmInstance(
109 CdmInterface::kVersion, key_system, key_system_size, get_cdm_host_func,
110 user_data);
111 if (!cdm_instance)
112 return NULL;
114 return new CdmWrapperImpl<CdmInterface>(
115 static_cast<CdmInterface*>(cdm_instance));
118 virtual ~CdmWrapperImpl() {
119 cdm_->Destroy();
122 virtual void SetServerCertificate(
123 uint32_t promise_id,
124 const uint8_t* server_certificate_data,
125 uint32_t server_certificate_data_size) override {
126 cdm_->SetServerCertificate(
127 promise_id, server_certificate_data, server_certificate_data_size);
130 virtual void CreateSessionAndGenerateRequest(
131 uint32_t promise_id,
132 cdm::SessionType session_type,
133 const char* init_data_type,
134 uint32_t init_data_type_size,
135 const uint8_t* init_data,
136 uint32_t init_data_size) override {
137 cdm_->CreateSessionAndGenerateRequest(promise_id, session_type,
138 init_data_type, init_data_type_size,
139 init_data, init_data_size);
142 virtual void LoadSession(uint32_t promise_id,
143 cdm::SessionType session_type,
144 const char* session_id,
145 uint32_t session_id_size) override {
146 cdm_->LoadSession(promise_id, session_type, session_id, session_id_size);
149 virtual void UpdateSession(uint32_t promise_id,
150 const char* session_id,
151 uint32_t session_id_size,
152 const uint8_t* response,
153 uint32_t response_size) override {
154 cdm_->UpdateSession(promise_id, session_id, session_id_size, response,
155 response_size);
158 virtual void CloseSession(uint32_t promise_id,
159 const char* session_id,
160 uint32_t session_id_size) override {
161 cdm_->CloseSession(promise_id, session_id, session_id_size);
164 virtual void RemoveSession(uint32_t promise_id,
165 const char* session_id,
166 uint32_t session_id_size) override {
167 cdm_->RemoveSession(promise_id, session_id, session_id_size);
170 virtual void TimerExpired(void* context) override {
171 cdm_->TimerExpired(context);
174 virtual cdm::Status Decrypt(const cdm::InputBuffer& encrypted_buffer,
175 cdm::DecryptedBlock* decrypted_buffer) override {
176 return cdm_->Decrypt(encrypted_buffer, decrypted_buffer);
179 virtual cdm::Status InitializeAudioDecoder(
180 const cdm::AudioDecoderConfig& audio_decoder_config) override {
181 return cdm_->InitializeAudioDecoder(audio_decoder_config);
184 virtual cdm::Status InitializeVideoDecoder(
185 const cdm::VideoDecoderConfig& video_decoder_config) override {
186 return cdm_->InitializeVideoDecoder(video_decoder_config);
189 virtual void DeinitializeDecoder(cdm::StreamType decoder_type) override {
190 cdm_->DeinitializeDecoder(decoder_type);
193 virtual void ResetDecoder(cdm::StreamType decoder_type) override {
194 cdm_->ResetDecoder(decoder_type);
197 virtual cdm::Status DecryptAndDecodeFrame(
198 const cdm::InputBuffer& encrypted_buffer,
199 cdm::VideoFrame* video_frame) override {
200 return cdm_->DecryptAndDecodeFrame(encrypted_buffer, video_frame);
203 virtual cdm::Status DecryptAndDecodeSamples(
204 const cdm::InputBuffer& encrypted_buffer,
205 cdm::AudioFrames* audio_frames) override {
206 return cdm_->DecryptAndDecodeSamples(encrypted_buffer, audio_frames);
209 virtual void OnPlatformChallengeResponse(
210 const cdm::PlatformChallengeResponse& response) override {
211 cdm_->OnPlatformChallengeResponse(response);
214 virtual void OnQueryOutputProtectionStatus(
215 cdm::QueryResult result,
216 uint32_t link_mask,
217 uint32_t output_protection_mask) override {
218 cdm_->OnQueryOutputProtectionStatus(result, link_mask,
219 output_protection_mask);
222 private:
223 CdmWrapperImpl(CdmInterface* cdm) : cdm_(cdm) {
224 PP_DCHECK(cdm_);
227 CdmInterface* cdm_;
229 DISALLOW_COPY_AND_ASSIGN(CdmWrapperImpl);
232 // Overrides for the cdm::Host_6 methods.
233 // TODO(jrummell): Remove these once Host_6 interface is removed.
235 template <>
236 void CdmWrapperImpl<cdm::ContentDecryptionModule_6>::
237 CreateSessionAndGenerateRequest(uint32_t promise_id,
238 cdm::SessionType session_type,
239 const char* init_data_type,
240 uint32_t init_data_type_size,
241 const uint8_t* init_data,
242 uint32_t init_data_size) {
243 cdm_->CreateSession(promise_id, init_data_type, init_data_type_size,
244 init_data, init_data_size, session_type);
247 template <>
248 void CdmWrapperImpl<cdm::ContentDecryptionModule_6>::LoadSession(
249 uint32_t promise_id,
250 cdm::SessionType session_type,
251 const char* session_id,
252 uint32_t session_id_size) {
253 cdm_->LoadSession(promise_id, session_id, session_id_size);
256 template <>
257 void CdmWrapperImpl<cdm::ContentDecryptionModule_6>::
258 OnQueryOutputProtectionStatus(cdm::QueryResult result,
259 uint32_t link_mask,
260 uint32_t output_protection_mask) {
261 if (result == cdm::kQuerySucceeded) {
262 cdm_->OnQueryOutputProtectionStatus(link_mask, output_protection_mask);
263 return;
266 // Invalid results, so return 0, 0 to indicate failure.
267 cdm_->OnQueryOutputProtectionStatus(0, 0);
270 CdmWrapper* CdmWrapper::Create(const char* key_system,
271 uint32_t key_system_size,
272 GetCdmHostFunc get_cdm_host_func,
273 void* user_data) {
274 static_assert(cdm::ContentDecryptionModule::kVersion ==
275 cdm::ContentDecryptionModule_7::kVersion,
276 "update the code below");
278 // Ensure IsSupportedCdmInterfaceVersion() matches this implementation.
279 // Always update this DCHECK when updating this function.
280 // If this check fails, update this function and DCHECK or update
281 // IsSupportedCdmInterfaceVersion().
282 PP_DCHECK(!IsSupportedCdmInterfaceVersion(
283 cdm::ContentDecryptionModule_7::kVersion + 1) &&
284 IsSupportedCdmInterfaceVersion(
285 cdm::ContentDecryptionModule_7::kVersion) &&
286 IsSupportedCdmInterfaceVersion(
287 cdm::ContentDecryptionModule_6::kVersion) &&
288 !IsSupportedCdmInterfaceVersion(
289 cdm::ContentDecryptionModule_6::kVersion - 1));
291 // Try to create the CDM using the latest CDM interface version.
292 CdmWrapper* cdm_wrapper =
293 CdmWrapperImpl<cdm::ContentDecryptionModule>::Create(
294 key_system, key_system_size, get_cdm_host_func, user_data);
296 // If |cdm_wrapper| is NULL, try to create the CDM using older supported
297 // versions of the CDM interface here.
298 if (!cdm_wrapper) {
299 cdm_wrapper = CdmWrapperImpl<cdm::ContentDecryptionModule_6>::Create(
300 key_system, key_system_size, get_cdm_host_func, user_data);
303 return cdm_wrapper;
306 // When updating the CdmAdapter, ensure you've updated the CdmWrapper to contain
307 // stub implementations for new or modified methods that the older CDM interface
308 // does not have.
309 // Also update supported_cdm_versions.h.
310 static_assert(cdm::ContentDecryptionModule::kVersion ==
311 cdm::ContentDecryptionModule_7::kVersion,
312 "ensure cdm wrapper templates have old version support");
314 } // namespace media
316 #endif // MEDIA_CDM_PPAPI_CDM_WRAPPER_H_