OLD | NEW |
(Empty) | |
| 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_ADAPTER_H_ |
| 6 #define MEDIA_CDM_PPAPI_CDM_ADAPTER_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "media/cdm/ppapi/api/content_decryption_module.h" |
| 10 #include "media/cdm/ppapi/cdm_helpers.h" |
| 11 #include "ppapi/cpp/logging.h" |
| 12 |
| 13 namespace media { |
| 14 |
| 15 // CdmAdapter wraps different versions of ContentDecryptionModule interfaces and |
| 16 // exposes a common interface to the caller. |
| 17 // |
| 18 // The caller should call CdmAdapter::Create() to create a CDM instance. |
| 19 // CdmAdapter will first try to create a CDM instance that supports the latest |
| 20 // CDM interface (i.e. ContentDecryptionModule). If such an instance cannot be |
| 21 // created (e.g. an older CDM was loaded), CdmAdapter will try to create a CDM |
| 22 // that supports an older version of CDM interface (e.g. |
| 23 // ContentDecryptionModule_*). Internally CdmAdapter converts the CdmAdapter |
| 24 // calls to corresponding ContentDecryptionModule calls. |
| 25 // |
| 26 // Note that CdmAdapter interface always reflects the latest state of content |
| 27 // decryption related PPAPI APIs (e.g. pp::ContentDecryptor_Private). |
| 28 // |
| 29 // Since this file is highly templated and default implementations are short |
| 30 // (just a shim layer in most cases), everything is done in this header file. |
| 31 class CdmAdapter { |
| 32 public: |
| 33 static CdmAdapter* Create(const char* key_system, |
| 34 int key_system_size, |
| 35 GetCdmHostFunc get_cdm_host_func, |
| 36 void* user_data); |
| 37 |
| 38 virtual ~CdmAdapter() {}; |
| 39 |
| 40 virtual cdm::Status GenerateKeyRequest(const char* type, |
| 41 int type_size, |
| 42 const uint8_t* init_data, |
| 43 int init_data_size) = 0; |
| 44 virtual cdm::Status AddKey(const char* session_id, |
| 45 int session_id_size, |
| 46 const uint8_t* key, |
| 47 int key_size, |
| 48 const uint8_t* key_id, |
| 49 int key_id_size) = 0; |
| 50 virtual cdm::Status CancelKeyRequest(const char* session_id, |
| 51 int session_id_size) = 0; |
| 52 virtual void TimerExpired(void* context) = 0; |
| 53 virtual cdm::Status Decrypt(const cdm::InputBuffer& encrypted_buffer, |
| 54 cdm::DecryptedBlock* decrypted_buffer) = 0; |
| 55 virtual cdm::Status InitializeAudioDecoder( |
| 56 const cdm::AudioDecoderConfig& audio_decoder_config) = 0; |
| 57 virtual cdm::Status InitializeVideoDecoder( |
| 58 const cdm::VideoDecoderConfig& video_decoder_config) = 0; |
| 59 virtual void DeinitializeDecoder(cdm::StreamType decoder_type) = 0; |
| 60 virtual void ResetDecoder(cdm::StreamType decoder_type) = 0; |
| 61 virtual cdm::Status DecryptAndDecodeFrame( |
| 62 const cdm::InputBuffer& encrypted_buffer, |
| 63 cdm::VideoFrame* video_frame) = 0; |
| 64 virtual cdm::Status DecryptAndDecodeSamples( |
| 65 const cdm::InputBuffer& encrypted_buffer, |
| 66 cdm::AudioFrames* audio_frames) = 0; |
| 67 virtual void OnPlatformChallengeResponse( |
| 68 const cdm::PlatformChallengeResponse& response) = 0; |
| 69 virtual void OnQueryOutputProtectionStatus( |
| 70 uint32_t link_mask, |
| 71 uint32_t output_protection_mask) = 0; |
| 72 |
| 73 protected: |
| 74 CdmAdapter() {}; |
| 75 |
| 76 private: |
| 77 DISALLOW_COPY_AND_ASSIGN(CdmAdapter); |
| 78 }; |
| 79 |
| 80 // Template class that does the CdmAdapter -> CdmType conversion. Default |
| 81 // implementations are provided. Any methods that need special treatment should |
| 82 // be specialized. |
| 83 template <class CdmType> |
| 84 class CdmAdapterImpl : public CdmAdapter { |
| 85 public: |
| 86 static CdmAdapter* Create(const char* key_system, |
| 87 int key_system_size, |
| 88 GetCdmHostFunc get_cdm_host_func, |
| 89 void* user_data) { |
| 90 void* cdm_instance = CreateCdmInstance(CdmType::kVersion, |
| 91 key_system, key_system_size, get_cdm_host_func, user_data); |
| 92 if (!cdm_instance) |
| 93 return NULL; |
| 94 |
| 95 return new CdmAdapterImpl<CdmType>(static_cast<CdmType*>(cdm_instance)); |
| 96 } |
| 97 |
| 98 virtual ~CdmAdapterImpl() { |
| 99 if (cdm_) |
| 100 cdm_->Destroy(); |
| 101 } |
| 102 |
| 103 virtual cdm::Status GenerateKeyRequest(const char* type, |
| 104 int type_size, |
| 105 const uint8_t* init_data, |
| 106 int init_data_size) OVERRIDE { |
| 107 return cdm_->GenerateKeyRequest(type, type_size, init_data, init_data_size); |
| 108 } |
| 109 |
| 110 virtual cdm::Status AddKey(const char* session_id, |
| 111 int session_id_size, |
| 112 const uint8_t* key, |
| 113 int key_size, |
| 114 const uint8_t* key_id, |
| 115 int key_id_size) OVERRIDE { |
| 116 return cdm_->AddKey( |
| 117 session_id, session_id_size, key, key_size, key_id, key_id_size); |
| 118 } |
| 119 |
| 120 virtual cdm::Status CancelKeyRequest(const char* session_id, |
| 121 int session_id_size) OVERRIDE { |
| 122 return cdm_->CancelKeyRequest(session_id, session_id_size); |
| 123 } |
| 124 |
| 125 virtual void TimerExpired(void* context) OVERRIDE { |
| 126 cdm_->TimerExpired(context); |
| 127 } |
| 128 |
| 129 virtual cdm::Status Decrypt(const cdm::InputBuffer& encrypted_buffer, |
| 130 cdm::DecryptedBlock* decrypted_buffer) OVERRIDE { |
| 131 return cdm_->Decrypt(encrypted_buffer, decrypted_buffer); |
| 132 } |
| 133 |
| 134 virtual cdm::Status InitializeAudioDecoder( |
| 135 const cdm::AudioDecoderConfig& audio_decoder_config) OVERRIDE { |
| 136 return cdm_->InitializeAudioDecoder(audio_decoder_config); |
| 137 } |
| 138 |
| 139 virtual cdm::Status InitializeVideoDecoder( |
| 140 const cdm::VideoDecoderConfig& video_decoder_config) OVERRIDE { |
| 141 return cdm_->InitializeVideoDecoder(video_decoder_config); |
| 142 } |
| 143 |
| 144 virtual void DeinitializeDecoder(cdm::StreamType decoder_type) OVERRIDE { |
| 145 cdm_->DeinitializeDecoder(decoder_type); |
| 146 } |
| 147 |
| 148 virtual void ResetDecoder(cdm::StreamType decoder_type) OVERRIDE { |
| 149 cdm_->ResetDecoder(decoder_type); |
| 150 } |
| 151 |
| 152 virtual cdm::Status DecryptAndDecodeFrame( |
| 153 const cdm::InputBuffer& encrypted_buffer, |
| 154 cdm::VideoFrame* video_frame) OVERRIDE { |
| 155 return cdm_->DecryptAndDecodeFrame(encrypted_buffer, video_frame); |
| 156 } |
| 157 |
| 158 virtual cdm::Status DecryptAndDecodeSamples( |
| 159 const cdm::InputBuffer& encrypted_buffer, |
| 160 cdm::AudioFrames* audio_frames) OVERRIDE { |
| 161 return cdm_->DecryptAndDecodeSamples(encrypted_buffer, audio_frames); |
| 162 } |
| 163 |
| 164 virtual void OnPlatformChallengeResponse( |
| 165 const cdm::PlatformChallengeResponse& response) OVERRIDE { |
| 166 cdm_->OnPlatformChallengeResponse(response); |
| 167 } |
| 168 |
| 169 virtual void OnQueryOutputProtectionStatus( |
| 170 uint32_t link_mask, |
| 171 uint32_t output_protection_mask) OVERRIDE { |
| 172 cdm_->OnQueryOutputProtectionStatus(link_mask, output_protection_mask); |
| 173 } |
| 174 |
| 175 private: |
| 176 CdmAdapterImpl(CdmType* cdm) : cdm_(cdm) { |
| 177 PP_DCHECK(cdm_); |
| 178 } |
| 179 |
| 180 CdmType* cdm_; |
| 181 |
| 182 DISALLOW_COPY_AND_ASSIGN(CdmAdapterImpl); |
| 183 }; |
| 184 |
| 185 // Specializations for ContentDecryptionModule_1. |
| 186 |
| 187 template <> void CdmAdapterImpl<cdm::ContentDecryptionModule_1>:: |
| 188 OnPlatformChallengeResponse( |
| 189 const cdm::PlatformChallengeResponse& response) { |
| 190 PP_NOTREACHED(); |
| 191 } |
| 192 |
| 193 template <> void CdmAdapterImpl<cdm::ContentDecryptionModule_1>:: |
| 194 OnQueryOutputProtectionStatus(uint32_t link_mask, |
| 195 uint32_t output_protection_mask) { |
| 196 PP_NOTREACHED(); |
| 197 } |
| 198 |
| 199 template <> cdm::Status CdmAdapterImpl<cdm::ContentDecryptionModule_1>:: |
| 200 DecryptAndDecodeSamples(const cdm::InputBuffer& encrypted_buffer, |
| 201 cdm::AudioFrames* audio_frames) { |
| 202 AudioFramesImpl audio_frames_1; |
| 203 cdm::Status status = |
| 204 cdm_->DecryptAndDecodeSamples(encrypted_buffer, &audio_frames_1); |
| 205 if (status != cdm::kSuccess) |
| 206 return status; |
| 207 |
| 208 audio_frames->SetFrameBuffer(audio_frames_1.PassFrameBuffer()); |
| 209 audio_frames->SetFormat(cdm::kAudioFormatS16); |
| 210 return cdm::kSuccess; |
| 211 } |
| 212 |
| 213 CdmAdapter* CdmAdapter::Create(const char* key_system, |
| 214 int key_system_size, |
| 215 GetCdmHostFunc get_cdm_host_func, |
| 216 void* user_data) { |
| 217 // Try to create the CDM using the latest CDM interface version. |
| 218 CdmAdapter* cdm_adapter = |
| 219 CdmAdapterImpl<cdm::ContentDecryptionModule>::Create( |
| 220 key_system, key_system_size, get_cdm_host_func, user_data); |
| 221 if (cdm_adapter) |
| 222 return cdm_adapter; |
| 223 |
| 224 // Try to see if the CDM supports older version(s) of CDM interface(s). |
| 225 cdm_adapter = CdmAdapterImpl<cdm::ContentDecryptionModule_1>::Create( |
| 226 key_system, key_system_size, get_cdm_host_func, user_data); |
| 227 return cdm_adapter; |
| 228 } |
| 229 |
| 230 } // namespace media |
| 231 |
| 232 #endif // MEDIA_CDM_PPAPI_CDM_ADAPTER_H_ |
OLD | NEW |