Skip to content

Commit

Permalink
cdmadapter minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
peak3d committed May 17, 2019
1 parent 32ab6d7 commit 12738c5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 35 deletions.
6 changes: 6 additions & 0 deletions wvdecrypter/cdm/base/macros.h
Expand Up @@ -13,6 +13,12 @@
#include <stddef.h> // For size_t.
#include <string.h> // For memcpy.

// INITIALIZE_CDM_MODULE is a macro in api/content_decryption_module.h.
// However, we need to pass it as a string to GetFunctionPointer() once it
// is expanded.
#define STRINGIFY(X) #X
#define MAKE_STRING(X) STRINGIFY(X)

// Put this in the declarations for a class to be uncopyable.
#define DISALLOW_COPY(TypeName) \
TypeName(const TypeName&) = delete
Expand Down
51 changes: 25 additions & 26 deletions wvdecrypter/cdm/media/cdm/cdm_adapter.cc
Expand Up @@ -39,7 +39,7 @@ uint64_t gtc()

namespace {

static void* GetCdmHost(int host_interface_version, void* user_data)
void* GetCdmHost(int host_interface_version, void* user_data)
{
if (!user_data)
return nullptr;
Expand All @@ -59,21 +59,6 @@ static void* GetCdmHost(int host_interface_version, void* user_data)
}
}

// Returns a pointer to the requested CDM upon success.
// Returns NULL if an error occurs or the requested |cdm_interface_version| or
// |key_system| is not supported or another error occurs.
// The caller should cast the returned pointer to the type matching
// |cdm_interface_version|.
// Caller retains ownership of arguments and must call Destroy() on the returned
// object.
typedef void* (*CreateCdmFunc)(int cdm_interface_version,
const char* key_system,
uint32_t key_system_size,
GetCdmHostFunc get_cdm_host_func,
void* user_data);

typedef void (*INITIALIZE_CDM_MODULE_FN)();

} // namespace

void timerfunc(std::shared_ptr<CdmAdapter> adp, uint64_t delay, void* context)
Expand All @@ -92,6 +77,7 @@ CdmAdapter::CdmAdapter(
const CdmConfig& cdm_config,
CdmAdapterClient *client)
: library_(0)
, cdm_path_(cdm_path)
, cdm_base_path_(base_path)
, client_(client)
, key_system_(key_system)
Expand All @@ -100,7 +86,7 @@ CdmAdapter::CdmAdapter(
, cdm8_(0), cdm9_(0), cdm10_(0)
{
//DCHECK(!key_system_.empty());
Initialize(cdm_path);
Initialize();
}

CdmAdapter::~CdmAdapter()
Expand All @@ -114,14 +100,14 @@ CdmAdapter::~CdmAdapter()
else
return;

INITIALIZE_CDM_MODULE_FN deinit_cdm_func = reinterpret_cast<INITIALIZE_CDM_MODULE_FN>(base::GetFunctionPointerFromNativeLibrary(library_, "DeinitializeCdmModule"));
DeinitializeCdmModuleFunc deinit_cdm_func = reinterpret_cast<DeinitializeCdmModuleFunc>(base::GetFunctionPointerFromNativeLibrary(library_, "DeinitializeCdmModule"));
if (deinit_cdm_func)
deinit_cdm_func();

base::UnloadNativeLibrary(library_);
}

void CdmAdapter::Initialize(const std::string& cdm_path)
void CdmAdapter::Initialize()
{
if (cdm8_ || cdm9_ || cdm10_)
{
Expand All @@ -137,14 +123,14 @@ void CdmAdapter::Initialize(const std::string& cdm_path)

base::NativeLibraryLoadError error;
#if defined(OS_WIN)
library_ = base::LoadNativeLibraryDynamically(cdm_path);
library_ = base::LoadNativeLibraryDynamically(cdm_path_);
#else
library_ = base::LoadNativeLibrary(cdm_path, 0);
#endif
if (!library_)
return;

INITIALIZE_CDM_MODULE_FN init_cdm_func = reinterpret_cast<INITIALIZE_CDM_MODULE_FN>(base::GetFunctionPointerFromNativeLibrary(library_, "InitializeCdmModule"));
InitializeCdmModuleFunc init_cdm_func = reinterpret_cast<InitializeCdmModuleFunc>(base::GetFunctionPointerFromNativeLibrary(library_, MAKE_STRING(INITIALIZE_CDM_MODULE)));
if (init_cdm_func)
init_cdm_func();

Expand All @@ -156,13 +142,23 @@ void CdmAdapter::Initialize(const std::string& cdm_path)
return;
}

GetCdmVersionFunc get_cdm_verion_func = reinterpret_cast<GetCdmVersionFunc>(base::GetFunctionPointerFromNativeLibrary(library_, "GetCdmVersion"));
if (get_cdm_verion_func)
{
std::string version = get_cdm_verion_func();
version = "CDM version: " + version;
client_->CDMLog(version.c_str());
}

cdm10_ = static_cast<cdm::ContentDecryptionModule_10*>(create_cdm_func(10, key_system_.data(), key_system_.size(), GetCdmHost, this));

if (!cdm10_)
{
cdm9_ = static_cast<cdm::ContentDecryptionModule_9*>(create_cdm_func(9, key_system_.data(), key_system_.size(), GetCdmHost, this));

if (!cdm9_)
cdm8_ = reinterpret_cast<cdm::ContentDecryptionModule_8*>(create_cdm_func(8, key_system_.data(), key_system_.size(), GetCdmHost, this));
if (!cdm9_)
cdm8_ = reinterpret_cast<cdm::ContentDecryptionModule_8*>(create_cdm_func(8, key_system_.data(), key_system_.size(), GetCdmHost, this));
}

if (cdm8_ || cdm9_ || cdm10_)
{
Expand Down Expand Up @@ -322,7 +318,10 @@ cdm::Status CdmAdapter::Decrypt(const cdm::InputBuffer& encrypted_buffer,
else if (cdm9_)
ret = cdm9_->Decrypt(encrypted_buffer, decrypted_buffer);
else if (cdm10_)
ret = cdm10_->Decrypt(encrypted_buffer, decrypted_buffer);
{
cdm::InputBuffer_2 tmp(encrypted_buffer);
ret = cdm10_->Decrypt(tmp, decrypted_buffer);
}

active_buffer_ = 0;
return ret;
Expand Down Expand Up @@ -570,9 +569,9 @@ void CdmAdapter::OnSessionMessage(const char* session_id, uint32_t session_id_si
void CdmAdapter::RequestStorageId(uint32_t version)
{
if (cdm9_)
cdm9_->OnStorageId(version, nullptr, 0);
cdm9_->OnStorageId(1, nullptr, 0);
else if (cdm10_)
cdm10_->OnStorageId(version, nullptr, 0);
cdm10_->OnStorageId(1, nullptr, 0);
}

void CdmAdapter::OnInitialized(bool success)
Expand Down
13 changes: 12 additions & 1 deletion wvdecrypter/cdm/media/cdm/cdm_adapter.h
Expand Up @@ -191,12 +191,23 @@ class CdmAdapter : public std::enable_shared_from_this<CdmAdapter>
virtual ~CdmAdapter();
bool valid(){ return library_ != 0; };
private:
virtual void Initialize(const std::string& cdm_path);
using InitializeCdmModuleFunc = void(*)();
using DeinitializeCdmModuleFunc = void(*)();
using GetCdmVersionFunc = char* (*)();
using CreateCdmFunc = void* (*)(int cdm_interface_version,
const char* key_system,
uint32_t key_system_size,
GetCdmHostFunc get_cdm_host_func,
void* user_data);


virtual void Initialize();
void SendClientMessage(const char* session, uint32_t session_size, CdmAdapterClient::CDMADPMSG msg, const uint8_t *data, size_t data_size, uint32_t status);

// Keep a reference to the CDM.
base::NativeLibrary library_;

std::string cdm_path_;
std::string cdm_base_path_;
CdmAdapterClient *client_;
std::mutex client_mutex_, decrypt_mutex_;
Expand Down
13 changes: 5 additions & 8 deletions wvdecrypter/wvdecrypter.cpp
Expand Up @@ -435,6 +435,7 @@ WV_DRM::WV_DRM(const char* licenseURL, const AP4_DataBuffer &serverCert, const u
if (license_url_.find('|') == std::string::npos)
license_url_ += "|Content-Type=application%2Fx-www-form-urlencoded|widevine2Challenge=B{SSM}&includeHdcpTestKeyInLicense=true|JBlicense;hdcpEnforcementResolutionPixels";

//wv_adapter->GetStatusForPolicy();
//wv_adapter->QueryOutputProtectionStatus();
}

Expand Down Expand Up @@ -509,12 +510,7 @@ WV_CencSingleSampleDecrypter::WV_CencSingleSampleDecrypter(WV_DRM &drm, AP4_Data
Log(SSD_HOST::LL_DEBUG, "%s: could not open debug file for writing (init)!", __func__);
#endif

if (memcmp(pssh.GetData() + 4, "pssh", 4) == 0)
{
drm.GetCdmAdapter()->CreateSessionAndGenerateRequest(promise_id_++, cdm::SessionType::kTemporary, cdm::InitDataType::kCenc,
reinterpret_cast<const uint8_t *>(pssh.GetData()), pssh.GetDataSize());
}
else
if (memcmp(pssh.GetData() + 4, "pssh", 4) != 0)
{
unsigned int buf_size = 32 + pssh.GetDataSize();
uint8_t buf[4096 + 32];
Expand All @@ -533,10 +529,11 @@ WV_CencSingleSampleDecrypter::WV_CencSingleSampleDecrypter(WV_DRM &drm, AP4_Data
memcpy(buf, proto, sizeof(proto));
memcpy(&buf[32], pssh.GetData(), pssh.GetDataSize());
pssh_.SetData(buf, buf_size);

drm.GetCdmAdapter()->CreateSessionAndGenerateRequest(promise_id_++, cdm::SessionType::kTemporary, cdm::InitDataType::kCenc, buf, buf_size);
}

drm.GetCdmAdapter()->CreateSessionAndGenerateRequest(promise_id_++, cdm::SessionType::kTemporary, cdm::InitDataType::kCenc,
reinterpret_cast<const uint8_t *>(pssh_.GetData()), pssh_.GetDataSize());

int retrycount=0;
while (session_.empty() && ++retrycount < 100)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
Expand Down

0 comments on commit 12738c5

Please sign in to comment.