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_
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"
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.
38 static CdmWrapper* Create(const char* key_system,
39 uint32_t key_system_size,
40 GetCdmHostFunc get_cdm_host_func,
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 CreateSession(uint32_t promise_id,
49 const char* init_data_type,
50 uint32_t init_data_type_size,
51 const uint8_t* init_data,
52 uint32_t init_data_size,
53 cdm::SessionType session_type) = 0;
54 virtual void LoadSession(uint32_t promise_id,
55 const char* web_session_id,
56 uint32_t web_session_id_size) = 0;
57 virtual void UpdateSession(uint32_t promise_id,
58 const char* web_session_id,
59 uint32_t web_session_id_size,
60 const uint8_t* response,
61 uint32_t response_size) = 0;
62 virtual void CloseSession(uint32_t promise_id,
63 const char* web_session_id,
64 uint32_t web_session_id_size) = 0;
65 virtual void RemoveSession(uint32_t promise_id,
66 const char* web_session_id,
67 uint32_t web_session_id_size) = 0;
68 virtual void GetUsableKeyIds(uint32_t promise_id,
69 const char* web_session_id,
70 uint32_t web_session_id_size) = 0;
71 virtual void TimerExpired(void* context) = 0;
72 virtual cdm::Status Decrypt(const cdm::InputBuffer& encrypted_buffer,
73 cdm::DecryptedBlock* decrypted_buffer) = 0;
74 virtual cdm::Status InitializeAudioDecoder(
75 const cdm::AudioDecoderConfig& audio_decoder_config) = 0;
76 virtual cdm::Status InitializeVideoDecoder(
77 const cdm::VideoDecoderConfig& video_decoder_config) = 0;
78 virtual void DeinitializeDecoder(cdm::StreamType decoder_type) = 0;
79 virtual void ResetDecoder(cdm::StreamType decoder_type) = 0;
80 virtual cdm::Status DecryptAndDecodeFrame(
81 const cdm::InputBuffer& encrypted_buffer,
82 cdm::VideoFrame* video_frame) = 0;
83 virtual cdm::Status DecryptAndDecodeSamples(
84 const cdm::InputBuffer& encrypted_buffer,
85 cdm::AudioFrames* audio_frames) = 0;
86 virtual void OnPlatformChallengeResponse(
87 const cdm::PlatformChallengeResponse& response) = 0;
88 virtual void OnQueryOutputProtectionStatus(
90 uint32_t output_protection_mask) = 0;
96 DISALLOW_COPY_AND_ASSIGN(CdmWrapper);
99 // Template class that does the CdmWrapper -> CdmInterface conversion. Default
100 // implementations are provided. Any methods that need special treatment should
102 template <class CdmInterface>
103 class CdmWrapperImpl : public CdmWrapper {
105 static CdmWrapper* Create(const char* key_system,
106 uint32_t key_system_size,
107 GetCdmHostFunc get_cdm_host_func,
109 void* cdm_instance = ::CreateCdmInstance(
110 CdmInterface::kVersion, key_system, key_system_size, get_cdm_host_func,
115 return new CdmWrapperImpl<CdmInterface>(
116 static_cast<CdmInterface*>(cdm_instance));
119 virtual ~CdmWrapperImpl() {
123 virtual void SetServerCertificate(
125 const uint8_t* server_certificate_data,
126 uint32_t server_certificate_data_size) override {
127 cdm_->SetServerCertificate(
128 promise_id, server_certificate_data, server_certificate_data_size);
131 virtual void CreateSession(uint32_t promise_id,
132 const char* init_data_type,
133 uint32_t init_data_type_size,
134 const uint8_t* init_data,
135 uint32_t init_data_size,
136 cdm::SessionType session_type) override {
137 cdm_->CreateSession(promise_id,
145 virtual void LoadSession(uint32_t promise_id,
146 const char* web_session_id,
147 uint32_t web_session_id_size) override {
148 cdm_->LoadSession(promise_id, web_session_id, web_session_id_size);
151 virtual void UpdateSession(uint32_t promise_id,
152 const char* web_session_id,
153 uint32_t web_session_id_size,
154 const uint8_t* response,
155 uint32_t response_size) override {
156 cdm_->UpdateSession(promise_id,
163 virtual void CloseSession(uint32_t promise_id,
164 const char* web_session_id,
165 uint32_t web_session_id_size) override {
166 cdm_->CloseSession(promise_id, web_session_id, web_session_id_size);
169 virtual void RemoveSession(uint32_t promise_id,
170 const char* web_session_id,
171 uint32_t web_session_id_size) override {
172 cdm_->RemoveSession(promise_id, web_session_id, web_session_id_size);
175 virtual void GetUsableKeyIds(uint32_t promise_id,
176 const char* web_session_id,
177 uint32_t web_session_id_size) override {
178 cdm_->GetUsableKeyIds(promise_id, web_session_id, web_session_id_size);
181 virtual void TimerExpired(void* context) override {
182 cdm_->TimerExpired(context);
185 virtual cdm::Status Decrypt(const cdm::InputBuffer& encrypted_buffer,
186 cdm::DecryptedBlock* decrypted_buffer) override {
187 return cdm_->Decrypt(encrypted_buffer, decrypted_buffer);
190 virtual cdm::Status InitializeAudioDecoder(
191 const cdm::AudioDecoderConfig& audio_decoder_config) override {
192 return cdm_->InitializeAudioDecoder(audio_decoder_config);
195 virtual cdm::Status InitializeVideoDecoder(
196 const cdm::VideoDecoderConfig& video_decoder_config) override {
197 return cdm_->InitializeVideoDecoder(video_decoder_config);
200 virtual void DeinitializeDecoder(cdm::StreamType decoder_type) override {
201 cdm_->DeinitializeDecoder(decoder_type);
204 virtual void ResetDecoder(cdm::StreamType decoder_type) override {
205 cdm_->ResetDecoder(decoder_type);
208 virtual cdm::Status DecryptAndDecodeFrame(
209 const cdm::InputBuffer& encrypted_buffer,
210 cdm::VideoFrame* video_frame) override {
211 return cdm_->DecryptAndDecodeFrame(encrypted_buffer, video_frame);
214 virtual cdm::Status DecryptAndDecodeSamples(
215 const cdm::InputBuffer& encrypted_buffer,
216 cdm::AudioFrames* audio_frames) override {
217 return cdm_->DecryptAndDecodeSamples(encrypted_buffer, audio_frames);
220 virtual void OnPlatformChallengeResponse(
221 const cdm::PlatformChallengeResponse& response) override {
222 cdm_->OnPlatformChallengeResponse(response);
225 virtual void OnQueryOutputProtectionStatus(
227 uint32_t output_protection_mask) override {
228 cdm_->OnQueryOutputProtectionStatus(link_mask, output_protection_mask);
232 CdmWrapperImpl(CdmInterface* cdm) : cdm_(cdm) {
238 DISALLOW_COPY_AND_ASSIGN(CdmWrapperImpl);
241 CdmWrapper* CdmWrapper::Create(const char* key_system,
242 uint32_t key_system_size,
243 GetCdmHostFunc get_cdm_host_func,
245 COMPILE_ASSERT(cdm::ContentDecryptionModule::kVersion ==
246 cdm::ContentDecryptionModule_6::kVersion,
249 // Ensure IsSupportedCdmInterfaceVersion() matches this implementation.
250 // Always update this DCHECK when updating this function.
251 // If this check fails, update this function and DCHECK or update
252 // IsSupportedCdmInterfaceVersion().
254 !IsSupportedCdmInterfaceVersion(cdm::ContentDecryptionModule::kVersion +
256 IsSupportedCdmInterfaceVersion(cdm::ContentDecryptionModule::kVersion) &&
257 !IsSupportedCdmInterfaceVersion(cdm::ContentDecryptionModule::kVersion -
260 // Try to create the CDM using the latest CDM interface version.
261 CdmWrapper* cdm_wrapper =
262 CdmWrapperImpl<cdm::ContentDecryptionModule>::Create(
263 key_system, key_system_size, get_cdm_host_func, user_data);
265 // If |cdm_wrapper| is NULL, try to create the CDM using older supported
266 // versions of the CDM interface here.
271 // When updating the CdmAdapter, ensure you've updated the CdmWrapper to contain
272 // stub implementations for new or modified methods that the older CDM interface
274 // Also update supported_cdm_versions.h.
275 COMPILE_ASSERT(cdm::ContentDecryptionModule::kVersion ==
276 cdm::ContentDecryptionModule_6::kVersion,
277 ensure_cdm_wrapper_templates_have_old_version_support);
281 #endif // MEDIA_CDM_PPAPI_CDM_WRAPPER_H_