| 1 |
/* |
| 2 |
* Copyright (C) 2016 liberty-developer (https://github.com/liberty-developer) |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "WVDecrypter.h" |
| 10 |
|
| 11 |
#include "decrypters/Helpers.h" |
| 12 |
#include "WVCdmAdapter.h" |
| 13 |
#include "WVCencSingleSampleDecrypter.h" |
| 14 |
#include "utils/Base64Utils.h" |
| 15 |
#include "utils/FileUtils.h" |
| 16 |
#include "utils/StringUtils.h" |
| 17 |
#include "utils/log.h" |
| 18 |
|
| 19 |
#include <kodi/Filesystem.h> |
| 20 |
|
| 21 |
#if defined(__linux__) && (defined(__aarch64__) || defined(__arm64__)) |
| 22 |
#include <dlfcn.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
using namespace DRM; |
| 26 |
using namespace UTILS; |
| 27 |
using namespace kodi::tools; |
| 28 |
|
| 29 |
|
| 30 |
CWVDecrypter::~CWVDecrypter() |
| 31 |
{ |
| 32 |
delete m_WVCdmAdapter; |
| 33 |
m_WVCdmAdapter = nullptr; |
| 34 |
#if defined(__linux__) && (defined(__aarch64__) || defined(__arm64__)) |
| 35 |
if (m_hdlLibLoader) |
| 36 |
dlclose(m_hdlLibLoader); |
| 37 |
#endif |
| 38 |
} |
| 39 |
|
| 40 |
bool CWVDecrypter::Initialize() |
| 41 |
{ |
| 42 |
#if defined(__linux__) && (defined(__aarch64__) || defined(__arm64__)) |
| 43 |
// On linux arm64, libwidevinecdm.so depends on two dynamic symbols: |
| 44 |
// __aarch64_ldadd4_acq_rel |
| 45 |
// __aarch64_swp4_acq_rel |
| 46 |
// These are defined from a separate library cdm_aarch64_loader, |
| 47 |
// but to make them available in the main binary's PLT, we need RTLD_GLOBAL. |
| 48 |
// Kodi kodi::tools::CDllHelper LoadDll() cannot be used because use RTLD_LOCAL, |
| 49 |
// and we need the RTLD_GLOBAL flag. |
| 50 |
std::string binaryPath; |
| 51 |
std::vector<kodi::vfs::CDirEntry> items; |
| 52 |
if (kodi::vfs::GetDirectory(FILESYS::GetAddonPath(), "", items)) |
| 53 |
{ |
| 54 |
for (auto item : items) |
| 55 |
{ |
| 56 |
if (!STRING::Contains(item.Label(), "cdm_aarch64_loader")) |
| 57 |
continue; |
| 58 |
|
| 59 |
binaryPath = item.Path(); |
| 60 |
break; |
| 61 |
} |
| 62 |
} |
| 63 |
if (binaryPath.empty()) |
| 64 |
{ |
| 65 |
LOG::Log(LOGERROR, "Cannot find the cdm_aarch64_loader file"); |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
m_hdlLibLoader = dlopen(binaryPath.c_str(), RTLD_GLOBAL | RTLD_LAZY); |
| 70 |
if (!m_hdlLibLoader) |
| 71 |
{ |
| 72 |
LOG::LogF(LOGERROR, "Failed to load CDM aarch64 loader from path \"%s\", error: %s", |
| 73 |
binaryPath.c_str(), dlerror()); |
| 74 |
return false; |
| 75 |
} |
| 76 |
#endif |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
std::vector<std::string_view> CWVDecrypter::SelectKeySystems(std::string_view keySystem) |
| 81 |
{ |
| 82 |
std::vector<std::string_view> keySystems; |
| 83 |
if (keySystem == KS_WIDEVINE) |
| 84 |
keySystems.push_back(URN_WIDEVINE); |
| 85 |
|
| 86 |
return keySystems; |
| 87 |
} |
| 88 |
|
| 89 |
bool CWVDecrypter::OpenDRMSystem(std::string_view licenseURL, |
| 90 |
const std::vector<uint8_t>& serverCertificate, |
| 91 |
const uint8_t config) |
| 92 |
{ |
| 93 |
if (licenseURL.empty()) |
| 94 |
{ |
| 95 |
LOG::LogF(LOGERROR, "License Key property cannot be empty"); |
| 96 |
return false; |
| 97 |
} |
| 98 |
m_WVCdmAdapter = new CWVCdmAdapter(licenseURL, serverCertificate, config, this); |
| 99 |
|
| 100 |
return m_WVCdmAdapter->GetCdmAdapter() != nullptr; |
| 101 |
} |
| 102 |
|
| 103 |
Adaptive_CencSingleSampleDecrypter* CWVDecrypter::CreateSingleSampleDecrypter( |
| 104 |
std::vector<uint8_t>& initData, |
| 105 |
std::string_view optionalKeyParameter, |
| 106 |
const std::vector<uint8_t>& defaultKeyId, |
| 107 |
std::string_view licenseUrl, |
| 108 |
bool skipSessionMessage, |
| 109 |
CryptoMode cryptoMode) |
| 110 |
{ |
| 111 |
CWVCencSingleSampleDecrypter* decrypter = new CWVCencSingleSampleDecrypter( |
| 112 |
*m_WVCdmAdapter, initData, defaultKeyId, skipSessionMessage, cryptoMode, this); |
| 113 |
if (!decrypter->GetSessionId()) |
| 114 |
{ |
| 115 |
delete decrypter; |
| 116 |
decrypter = nullptr; |
| 117 |
} |
| 118 |
return decrypter; |
| 119 |
} |
| 120 |
|
| 121 |
void CWVDecrypter::DestroySingleSampleDecrypter(Adaptive_CencSingleSampleDecrypter* decrypter) |
| 122 |
{ |
| 123 |
if (decrypter) |
| 124 |
{ |
| 125 |
// close session before dispose |
| 126 |
static_cast<CWVCencSingleSampleDecrypter*>(decrypter)->CloseSessionId(); |
| 127 |
delete static_cast<CWVCencSingleSampleDecrypter*>(decrypter); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void CWVDecrypter::GetCapabilities(Adaptive_CencSingleSampleDecrypter* decrypter, |
| 132 |
const std::vector<uint8_t>& keyId, |
| 133 |
uint32_t media, |
| 134 |
DecrypterCapabilites& caps) |
| 135 |
{ |
| 136 |
if (!decrypter) |
| 137 |
{ |
| 138 |
caps = {0, 0, 0}; |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
static_cast<CWVCencSingleSampleDecrypter*>(decrypter)->GetCapabilities(keyId, media, caps); |
| 143 |
} |
| 144 |
|
| 145 |
bool CWVDecrypter::HasLicenseKey(Adaptive_CencSingleSampleDecrypter* decrypter, |
| 146 |
const std::vector<uint8_t>& keyId) |
| 147 |
{ |
| 148 |
if (decrypter) |
| 149 |
return static_cast<CWVCencSingleSampleDecrypter*>(decrypter)->HasKeyId(keyId); |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
std::string CWVDecrypter::GetChallengeB64Data(Adaptive_CencSingleSampleDecrypter* decrypter) |
| 154 |
{ |
| 155 |
if (!decrypter) |
| 156 |
return ""; |
| 157 |
|
| 158 |
AP4_DataBuffer challengeData = |
| 159 |
static_cast<CWVCencSingleSampleDecrypter*>(decrypter)->GetChallengeData(); |
| 160 |
return BASE64::Encode(challengeData.GetData(), challengeData.GetDataSize()); |
| 161 |
} |
| 162 |
|
| 163 |
bool CWVDecrypter::OpenVideoDecoder(Adaptive_CencSingleSampleDecrypter* decrypter, |
| 164 |
const VIDEOCODEC_INITDATA* initData) |
| 165 |
{ |
| 166 |
if (!decrypter || !initData) |
| 167 |
return false; |
| 168 |
|
| 169 |
m_decodingDecrypter = static_cast<CWVCencSingleSampleDecrypter*>(decrypter); |
| 170 |
return m_decodingDecrypter->OpenVideoDecoder(initData); |
| 171 |
} |
| 172 |
|
| 173 |
VIDEOCODEC_RETVAL CWVDecrypter::DecryptAndDecodeVideo( |
| 174 |
kodi::addon::CInstanceVideoCodec* codecInstance, const DEMUX_PACKET* sample) |
| 175 |
{ |
| 176 |
if (!m_decodingDecrypter) |
| 177 |
return VC_ERROR; |
| 178 |
|
| 179 |
return m_decodingDecrypter->DecryptAndDecodeVideo(codecInstance, sample); |
| 180 |
} |
| 181 |
|
| 182 |
VIDEOCODEC_RETVAL CWVDecrypter::VideoFrameDataToPicture( |
| 183 |
kodi::addon::CInstanceVideoCodec* codecInstance, VIDEOCODEC_PICTURE* picture) |
| 184 |
{ |
| 185 |
if (!m_decodingDecrypter) |
| 186 |
return VC_ERROR; |
| 187 |
|
| 188 |
return m_decodingDecrypter->VideoFrameDataToPicture(codecInstance, picture); |
| 189 |
} |
| 190 |
|
| 191 |
void CWVDecrypter::ResetVideo() |
| 192 |
{ |
| 193 |
if (m_decodingDecrypter) |
| 194 |
m_decodingDecrypter->ResetVideo(); |
| 195 |
} |
| 196 |
|
| 197 |
void CWVDecrypter::SetLibraryPath(std::string_view libraryPath) |
| 198 |
{ |
| 199 |
m_libraryPath = libraryPath; |
| 200 |
} |
| 201 |
|
| 202 |
bool CWVDecrypter::GetBuffer(void* instance, VIDEOCODEC_PICTURE& picture) |
| 203 |
{ |
| 204 |
return instance ? static_cast<kodi::addon::CInstanceVideoCodec*>(instance)->GetFrameBuffer( |
| 205 |
*reinterpret_cast<VIDEOCODEC_PICTURE*>(&picture)) |
| 206 |
: false; |
| 207 |
} |
| 208 |
|
| 209 |
void CWVDecrypter::ReleaseBuffer(void* instance, void* buffer) |
| 210 |
{ |
| 211 |
if (instance) |
| 212 |
static_cast<kodi::addon::CInstanceVideoCodec*>(instance)->ReleaseFrameBuffer(buffer); |
| 213 |
} |