Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / media / cdm / ppapi / cdm_wrapper.h
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.
4
5 #ifndef MEDIA_CDM_PPAPI_CDM_WRAPPER_H_
6 #define MEDIA_CDM_PPAPI_CDM_WRAPPER_H_
7
8 #include <map>
9 #include <queue>
10 #include <string>
11
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"
17
18 namespace media {
19
20 // CdmWrapper wraps different versions of ContentDecryptionModule interfaces and
21 // exposes a common interface to the caller.
22 //
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.
30 //
31 // Note that CdmWrapper interface always reflects the latest state of content
32 // decryption related PPAPI APIs (e.g. pp::ContentDecryptor_Private).
33 //
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);
42
43   virtual ~CdmWrapper() {};
44
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(
89       uint32_t link_mask,
90       uint32_t output_protection_mask) = 0;
91
92  protected:
93   CdmWrapper() {}
94
95  private:
96   DISALLOW_COPY_AND_ASSIGN(CdmWrapper);
97 };
98
99 // Template class that does the CdmWrapper -> CdmInterface conversion. Default
100 // implementations are provided. Any methods that need special treatment should
101 // be specialized.
102 template <class CdmInterface>
103 class CdmWrapperImpl : public CdmWrapper {
104  public:
105   static CdmWrapper* Create(const char* key_system,
106                             uint32_t key_system_size,
107                             GetCdmHostFunc get_cdm_host_func,
108                             void* user_data) {
109     void* cdm_instance = ::CreateCdmInstance(
110         CdmInterface::kVersion, key_system, key_system_size, get_cdm_host_func,
111         user_data);
112     if (!cdm_instance)
113       return NULL;
114
115     return new CdmWrapperImpl<CdmInterface>(
116         static_cast<CdmInterface*>(cdm_instance));
117   }
118
119   virtual ~CdmWrapperImpl() {
120     cdm_->Destroy();
121   }
122
123   virtual void SetServerCertificate(
124       uint32_t promise_id,
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);
129   }
130
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,
138                         init_data_type,
139                         init_data_type_size,
140                         init_data,
141                         init_data_size,
142                         session_type);
143   }
144
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);
149   }
150
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,
157                         web_session_id,
158                         web_session_id_size,
159                         response,
160                         response_size);
161   }
162
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);
167   }
168
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);
173   }
174
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);
179   }
180
181   virtual void TimerExpired(void* context) override {
182     cdm_->TimerExpired(context);
183   }
184
185   virtual cdm::Status Decrypt(const cdm::InputBuffer& encrypted_buffer,
186                               cdm::DecryptedBlock* decrypted_buffer) override {
187     return cdm_->Decrypt(encrypted_buffer, decrypted_buffer);
188   }
189
190   virtual cdm::Status InitializeAudioDecoder(
191       const cdm::AudioDecoderConfig& audio_decoder_config) override {
192     return cdm_->InitializeAudioDecoder(audio_decoder_config);
193   }
194
195   virtual cdm::Status InitializeVideoDecoder(
196       const cdm::VideoDecoderConfig& video_decoder_config) override {
197     return cdm_->InitializeVideoDecoder(video_decoder_config);
198   }
199
200   virtual void DeinitializeDecoder(cdm::StreamType decoder_type) override {
201     cdm_->DeinitializeDecoder(decoder_type);
202   }
203
204   virtual void ResetDecoder(cdm::StreamType decoder_type) override {
205     cdm_->ResetDecoder(decoder_type);
206   }
207
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);
212   }
213
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);
218   }
219
220   virtual void OnPlatformChallengeResponse(
221       const cdm::PlatformChallengeResponse& response) override {
222     cdm_->OnPlatformChallengeResponse(response);
223   }
224
225   virtual void OnQueryOutputProtectionStatus(
226       uint32_t link_mask,
227       uint32_t output_protection_mask) override {
228     cdm_->OnQueryOutputProtectionStatus(link_mask, output_protection_mask);
229   }
230
231  private:
232   CdmWrapperImpl(CdmInterface* cdm) : cdm_(cdm) {
233     PP_DCHECK(cdm_);
234   }
235
236   CdmInterface* cdm_;
237
238   DISALLOW_COPY_AND_ASSIGN(CdmWrapperImpl);
239 };
240
241 CdmWrapper* CdmWrapper::Create(const char* key_system,
242                                uint32_t key_system_size,
243                                GetCdmHostFunc get_cdm_host_func,
244                                void* user_data) {
245   COMPILE_ASSERT(cdm::ContentDecryptionModule::kVersion ==
246                      cdm::ContentDecryptionModule_6::kVersion,
247                  update_code_below);
248
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().
253   PP_DCHECK(
254       !IsSupportedCdmInterfaceVersion(cdm::ContentDecryptionModule::kVersion +
255                                       1) &&
256       IsSupportedCdmInterfaceVersion(cdm::ContentDecryptionModule::kVersion) &&
257       !IsSupportedCdmInterfaceVersion(cdm::ContentDecryptionModule::kVersion -
258                                       1));
259
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);
264
265   // If |cdm_wrapper| is NULL, try to create the CDM using older supported
266   // versions of the CDM interface here.
267
268   return cdm_wrapper;
269 }
270
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
273 // does not have.
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);
278
279 }  // namespace media
280
281 #endif  // MEDIA_CDM_PPAPI_CDM_WRAPPER_H_