#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "media/cdm/library_cdm/clear_key_cdm/clear_key_cdm.h"
#include <algorithm>
#include <cstring>
#include <memory>
#include <sstream>
#include <utility>
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/files/file.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "media/base/cdm_callback_promise.h"
#include "media/base/cdm_key_information.h"
#include "media/base/decoder_buffer.h"
#include "media/base/decrypt_config.h"
#include "media/base/encryption_pattern.h"
#include "media/cdm/api/content_decryption_module_ext.h"
#include "media/cdm/cdm_type_conversion.h"
#include "media/cdm/clear_key_cdm_common.h"
#include "media/cdm/json_web_key.h"
#include "media/cdm/library_cdm/cdm_host_proxy.h"
#include "media/cdm/library_cdm/cdm_host_proxy_impl.h"
#include "media/cdm/library_cdm/clear_key_cdm/cdm_file_io_test.h"
#include "media/cdm/library_cdm/clear_key_cdm/cdm_video_decoder.h"
#include "media/media_buildflags.h"
#if defined(CLEAR_KEY_CDM_USE_FFMPEG_DECODER)
#include "base/at_exit.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "media/base/media.h"
#include "media/cdm/library_cdm/clear_key_cdm/ffmpeg_cdm_audio_decoder.h"
#if !defined COMPONENT_BUILD
static base::AtExitManager g_at_exit_manager;
#endif
#endif
const char kClearKeyCdmVersion[] = …;
const int64_t kMaxTimerDelayMs = …;
const char kUnitTestResultHeader[] = …;
const char kDummyIndividualizationRequest[] = …;
static bool g_is_cdm_module_initialized = …;
static scoped_refptr<media::DecoderBuffer> CopyDecoderBufferFrom(
const cdm::InputBuffer_2& input_buffer) { … }
static std::string GetUnitTestResultMessage(bool success) { … }
void ConvertCdmKeysInfo(const media::CdmKeysInfo& keys_info,
std::vector<cdm::KeyInformation>* keys_vector) { … }
void INITIALIZE_CDM_MODULE() { … }
void DeinitializeCdmModule() { … }
void* CreateCdmInstance(int cdm_interface_version,
const char* key_system,
uint32_t key_system_size,
GetCdmHostFunc get_cdm_host_func,
void* user_data) { … }
const char* GetCdmVersion() { … }
static bool g_verify_host_files_result = …;
bool VerifyCdmHost_0(const cdm::HostFile* host_files, uint32_t num_files) { … }
namespace media {
namespace {
constexpr cdm::ColorSpace kUnspecifiedColorSpace = …;
cdm::VideoDecoderConfig_3 ToVideoDecoderConfig_3(
cdm::VideoDecoderConfig_2 config) { … }
class CdmVideoFrameAdapter : public cdm::VideoFrame_2 { … };
}
template <typename HostInterface>
ClearKeyCdm::ClearKeyCdm(HostInterface* host, const std::string& key_system)
: host_interface_version_(HostInterface::kVersion),
cdm_host_proxy_(std::make_unique<CdmHostProxyImpl<HostInterface>>(host)),
cdm_(base::MakeRefCounted<ClearKeyPersistentSessionCdm>(
cdm_host_proxy_.get(),
base::BindRepeating(&ClearKeyCdm::OnSessionMessage,
base::Unretained(this)),
base::BindRepeating(&ClearKeyCdm::OnSessionClosed,
base::Unretained(this)),
base::BindRepeating(&ClearKeyCdm::OnSessionKeysChange,
base::Unretained(this)),
base::BindRepeating(&ClearKeyCdm::OnSessionExpirationUpdate,
base::Unretained(this)))),
key_system_(key_system) { … }
ClearKeyCdm::~ClearKeyCdm() = default;
void ClearKeyCdm::Initialize(bool allow_distinctive_identifier,
bool allow_persistent_state,
bool ) { … }
void ClearKeyCdm::GetStatusForPolicy(uint32_t promise_id,
const cdm::Policy& policy) { … }
void ClearKeyCdm::CreateSessionAndGenerateRequest(
uint32_t promise_id,
cdm::SessionType session_type,
cdm::InitDataType init_data_type,
const uint8_t* init_data,
uint32_t init_data_size) { … }
void ClearKeyCdm::LoadSession(uint32_t promise_id,
cdm::SessionType session_type,
const char* session_id,
uint32_t session_id_length) { … }
void ClearKeyCdm::UpdateSession(uint32_t promise_id,
const char* session_id,
uint32_t session_id_length,
const uint8_t* response,
uint32_t response_size) { … }
void ClearKeyCdm::OnUpdateSuccess(uint32_t promise_id,
const std::string& session_id) { … }
void ClearKeyCdm::CloseSession(uint32_t promise_id,
const char* session_id,
uint32_t session_id_length) { … }
void ClearKeyCdm::RemoveSession(uint32_t promise_id,
const char* session_id,
uint32_t session_id_length) { … }
void ClearKeyCdm::SetServerCertificate(uint32_t promise_id,
const uint8_t* server_certificate_data,
uint32_t server_certificate_data_size) { … }
void ClearKeyCdm::TimerExpired(void* context) { … }
static void CopyDecryptResults(Decryptor::Status* status_copy,
scoped_refptr<DecoderBuffer>* buffer_copy,
Decryptor::Status status,
scoped_refptr<DecoderBuffer> buffer) { … }
cdm::Status ClearKeyCdm::Decrypt(const cdm::InputBuffer_2& encrypted_buffer,
cdm::DecryptedBlock* decrypted_block) { … }
cdm::Status ClearKeyCdm::InitializeAudioDecoder(
const cdm::AudioDecoderConfig_2& audio_decoder_config) { … }
cdm::Status ClearKeyCdm::InitializeVideoDecoder(
const cdm::VideoDecoderConfig_2& video_decoder_config) { … }
cdm::Status ClearKeyCdm::InitializeVideoDecoder(
const cdm::VideoDecoderConfig_3& video_decoder_config) { … }
void ClearKeyCdm::ResetDecoder(cdm::StreamType decoder_type) { … }
void ClearKeyCdm::DeinitializeDecoder(cdm::StreamType decoder_type) { … }
cdm::Status ClearKeyCdm::DecryptAndDecodeFrame(
const cdm::InputBuffer_2& encrypted_buffer,
cdm::VideoFrame* decoded_frame) { … }
cdm::Status ClearKeyCdm::DecryptAndDecodeFrame(
const cdm::InputBuffer_2& encrypted_buffer,
cdm::VideoFrame_2* decoded_frame) { … }
cdm::Status ClearKeyCdm::DecryptAndDecodeSamples(
const cdm::InputBuffer_2& encrypted_buffer,
cdm::AudioFrames* audio_frames) { … }
void ClearKeyCdm::Destroy() { … }
void ClearKeyCdm::ScheduleNextTimer() { … }
cdm::Status ClearKeyCdm::DecryptToMediaDecoderBuffer(
const cdm::InputBuffer_2& encrypted_buffer,
scoped_refptr<DecoderBuffer>* decrypted_buffer) { … }
void ClearKeyCdm::OnPlatformChallengeResponse(
const cdm::PlatformChallengeResponse& response) { … }
void ClearKeyCdm::OnQueryOutputProtectionStatus(
cdm::QueryResult result,
uint32_t link_mask,
uint32_t output_protection_mask) { … }
void ClearKeyCdm::OnStorageId(uint32_t version,
const uint8_t* storage_id,
uint32_t storage_id_size) { … }
void ClearKeyCdm::OnSessionMessage(const std::string& session_id,
CdmMessageType message_type,
const std::vector<uint8_t>& message) { … }
void ClearKeyCdm::OnSessionKeysChange(const std::string& session_id,
bool has_additional_usable_key,
CdmKeysInfo keys_info) { … }
void ClearKeyCdm::OnSessionClosed(const std::string& session_id,
CdmSessionClosedReason ) { … }
void ClearKeyCdm::OnSessionExpirationUpdate(const std::string& session_id,
base::Time new_expiry_time) { … }
void ClearKeyCdm::OnSessionCreated(uint32_t promise_id,
const std::string& session_id) { … }
void ClearKeyCdm::OnPromiseResolved(uint32_t promise_id) { … }
void ClearKeyCdm::OnPromiseFailed(uint32_t promise_id,
CdmPromise::Exception exception_code,
uint32_t system_code,
const std::string& error_message) { … }
void ClearKeyCdm::OnUnitTestComplete(bool success) { … }
void ClearKeyCdm::StartFileIOTest() { … }
void ClearKeyCdm::OnFileIOTestComplete(bool success) { … }
void ClearKeyCdm::StartOutputProtectionTest() { … }
void ClearKeyCdm::StartPlatformVerificationTest() { … }
void ClearKeyCdm::ReportVerifyCdmHostTestResult() { … }
void ClearKeyCdm::StartStorageIdTest() { … }
}