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_WRAPPER_H_ |
| 6 #define MEDIA_CDM_PPAPI_CDM_WRAPPER_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 // CdmWrapper wraps different versions of ContentDecryptionModule interfaces and |
| 16 // exposes a common interface to the caller. |
| 17 // |
| 18 // The caller should call CdmWrapper::Create() to create a CDM instance. |
| 19 // CdmWrapper will first try to create a CDM instance that supports the latest |
| 20 // CDM interface (ContentDecryptionModule). If such an instance cannot be |
| 21 // created (e.g. an older CDM was loaded), CdmWrapper will try to create a CDM |
| 22 // that supports an older version of CDM interface (e.g. |
| 23 // ContentDecryptionModule_*). Internally CdmWrapper converts the CdmWrapper |
| 24 // calls to corresponding ContentDecryptionModule calls. |
| 25 // |
| 26 // Note that CdmWrapper 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 CdmWrapper { |
| 32 public: |
| 33 static CdmWrapper* Create(const char* key_system, |
| 34 int key_system_size, |
| 35 GetCdmHostFunc get_cdm_host_func, |
| 36 void* user_data); |
| 37 |
| 38 virtual ~CdmWrapper() {}; |
| 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 |
| 68 protected: |
| 69 CdmWrapper() {}; |
| 70 |
| 71 private: |
| 72 DISALLOW_COPY_AND_ASSIGN(CdmWrapper); |
| 73 }; |
| 74 |
| 75 // Template class that does the CdmWrapper -> CdmInterface conversion. Default |
| 76 // implementations are provided. Any methods that need special treatment should |
| 77 // be specialized. |
| 78 // TODO(xhwang): Remove CdmInterfaceVersion template parameter after we roll |
| 79 // CDM.h DEPS. |
| 80 template <class CdmInterface, int CdmInterfaceVersion> |
| 81 class CdmWrapperImpl : public CdmWrapper { |
| 82 public: |
| 83 static CdmWrapper* Create(const char* key_system, |
| 84 int key_system_size, |
| 85 GetCdmHostFunc get_cdm_host_func, |
| 86 void* user_data) { |
| 87 void* cdm_instance = ::CreateCdmInstance(CdmInterfaceVersion, |
| 88 key_system, key_system_size, get_cdm_host_func, user_data); |
| 89 if (!cdm_instance) |
| 90 return NULL; |
| 91 |
| 92 return new CdmWrapperImpl<CdmInterface, CdmInterfaceVersion>( |
| 93 static_cast<CdmInterface*>(cdm_instance)); |
| 94 } |
| 95 |
| 96 virtual ~CdmWrapperImpl() { |
| 97 cdm_->Destroy(); |
| 98 } |
| 99 |
| 100 virtual cdm::Status GenerateKeyRequest(const char* type, |
| 101 int type_size, |
| 102 const uint8_t* init_data, |
| 103 int init_data_size) OVERRIDE { |
| 104 return cdm_->GenerateKeyRequest(type, type_size, init_data, init_data_size); |
| 105 } |
| 106 |
| 107 virtual cdm::Status AddKey(const char* session_id, |
| 108 int session_id_size, |
| 109 const uint8_t* key, |
| 110 int key_size, |
| 111 const uint8_t* key_id, |
| 112 int key_id_size) OVERRIDE { |
| 113 return cdm_->AddKey( |
| 114 session_id, session_id_size, key, key_size, key_id, key_id_size); |
| 115 } |
| 116 |
| 117 virtual cdm::Status CancelKeyRequest(const char* session_id, |
| 118 int session_id_size) OVERRIDE { |
| 119 return cdm_->CancelKeyRequest(session_id, session_id_size); |
| 120 } |
| 121 |
| 122 virtual void TimerExpired(void* context) OVERRIDE { |
| 123 cdm_->TimerExpired(context); |
| 124 } |
| 125 |
| 126 virtual cdm::Status Decrypt(const cdm::InputBuffer& encrypted_buffer, |
| 127 cdm::DecryptedBlock* decrypted_buffer) OVERRIDE { |
| 128 return cdm_->Decrypt(encrypted_buffer, decrypted_buffer); |
| 129 } |
| 130 |
| 131 virtual cdm::Status InitializeAudioDecoder( |
| 132 const cdm::AudioDecoderConfig& audio_decoder_config) OVERRIDE { |
| 133 return cdm_->InitializeAudioDecoder(audio_decoder_config); |
| 134 } |
| 135 |
| 136 virtual cdm::Status InitializeVideoDecoder( |
| 137 const cdm::VideoDecoderConfig& video_decoder_config) OVERRIDE { |
| 138 return cdm_->InitializeVideoDecoder(video_decoder_config); |
| 139 } |
| 140 |
| 141 virtual void DeinitializeDecoder(cdm::StreamType decoder_type) OVERRIDE { |
| 142 cdm_->DeinitializeDecoder(decoder_type); |
| 143 } |
| 144 |
| 145 virtual void ResetDecoder(cdm::StreamType decoder_type) OVERRIDE { |
| 146 cdm_->ResetDecoder(decoder_type); |
| 147 } |
| 148 |
| 149 virtual cdm::Status DecryptAndDecodeFrame( |
| 150 const cdm::InputBuffer& encrypted_buffer, |
| 151 cdm::VideoFrame* video_frame) OVERRIDE { |
| 152 return cdm_->DecryptAndDecodeFrame(encrypted_buffer, video_frame); |
| 153 } |
| 154 |
| 155 virtual cdm::Status DecryptAndDecodeSamples( |
| 156 const cdm::InputBuffer& encrypted_buffer, |
| 157 cdm::AudioFrames* audio_frames) OVERRIDE { |
| 158 return cdm_->DecryptAndDecodeSamples(encrypted_buffer, audio_frames); |
| 159 } |
| 160 |
| 161 private: |
| 162 CdmWrapperImpl(CdmInterface* cdm) : cdm_(cdm) { |
| 163 PP_DCHECK(cdm_); |
| 164 } |
| 165 |
| 166 CdmInterface* cdm_; |
| 167 |
| 168 DISALLOW_COPY_AND_ASSIGN(CdmWrapperImpl); |
| 169 }; |
| 170 |
| 171 // Specializations for old ContentDecryptionModule interfaces. |
| 172 // For example: |
| 173 |
| 174 // template <> cdm::Status CdmAdapterImpl<cdm::ContentDecryptionModule_1>:: |
| 175 // DecryptAndDecodeSamples(const cdm::InputBuffer& encrypted_buffer, |
| 176 // cdm::AudioFrames* audio_frames) { |
| 177 // AudioFramesImpl audio_frames_1; |
| 178 // cdm::Status status = |
| 179 // cdm_->DecryptAndDecodeSamples(encrypted_buffer, &audio_frames_1); |
| 180 // if (status != cdm::kSuccess) |
| 181 // return status; |
| 182 // |
| 183 // audio_frames->SetFrameBuffer(audio_frames_1.PassFrameBuffer()); |
| 184 // audio_frames->SetFormat(cdm::kAudioFormatS16); |
| 185 // return cdm::kSuccess; |
| 186 // } |
| 187 |
| 188 CdmWrapper* CdmWrapper::Create(const char* key_system, |
| 189 int key_system_size, |
| 190 GetCdmHostFunc get_cdm_host_func, |
| 191 void* user_data) { |
| 192 // Try to create the CDM using the latest CDM interface version. |
| 193 CdmWrapper* cdm_wrapper = |
| 194 CdmWrapperImpl<cdm::ContentDecryptionModule, cdm::kCdmInterfaceVersion>:: |
| 195 Create(key_system, key_system_size, get_cdm_host_func, user_data); |
| 196 |
| 197 // Try to see if the CDM supports older version(s) of CDM interface(s). |
| 198 // For example: |
| 199 // |
| 200 // if (cdm_wrapper) |
| 201 // return cdm_wrapper; |
| 202 // |
| 203 // cdm_wrapper = CdmWrapperImpl<cdm::ContentDecryptionModule_1>::Create( |
| 204 // key_system, key_system_size, get_cdm_host_func, user_data); |
| 205 |
| 206 return cdm_wrapper; |
| 207 } |
| 208 |
| 209 } // namespace media |
| 210 |
| 211 #endif // MEDIA_CDM_PPAPI_CDM_WRAPPER_H_ |
OLD | NEW |