Skip to content

Commit

Permalink
Merge pull request #1395 from CastagnaIT/cleanups
Browse files Browse the repository at this point in the history
Various code cleaning
  • Loading branch information
CastagnaIT authored Oct 15, 2023
2 parents 95c4dfa + 3b6f2ec commit 54b3f50
Show file tree
Hide file tree
Showing 27 changed files with 316 additions and 269 deletions.
34 changes: 34 additions & 0 deletions lib/jni/jni/jutils/jutils-details.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,31 @@ struct jcast_helper<std::vector<char>, jbyteArray>
}
};

template<>
struct jcast_helper<std::vector<uint8_t>, jbyteArray>
{
static std::vector<uint8_t> cast(jbyteArray const& v)
{
JNIEnv* env = xbmc_jnienv();
jsize size = 0;
std::vector<uint8_t> vec;
if (v)
{
size = env->GetArrayLength(v);

vec.reserve(size);

jbyte* elements = env->GetByteArrayElements(v, NULL);
for (int i = 0; i < size; i++)
{
vec.emplace_back(static_cast<uint8_t>(elements[i]));
}
env->ReleaseByteArrayElements(v, elements, JNI_ABORT);
}
return vec;
}
};

template <>
struct jcast_helper<std::vector<int>, jintArray>
{
Expand Down Expand Up @@ -229,6 +254,15 @@ struct jcast_helper<std::vector<char>, jhbyteArray>
}
};

template<>
struct jcast_helper<std::vector<uint8_t>, jhbyteArray>
{
static std::vector<uint8_t> cast(jhbyteArray const& v)
{
return jcast_helper<std::vector<uint8_t>, jbyteArray>::cast(v.get());
}
};

template <>
struct jcast_helper<std::vector<int>, jhintArray>
{
Expand Down
13 changes: 6 additions & 7 deletions lib/jni/jni/src/MediaDrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ std::string CJNIMediaDrm::getPropertyString(const std::string &propertyName) con
jcast<jhstring>(propertyName)));
}

std::vector<char> CJNIMediaDrm::getPropertyByteArray(const std::string &propertyName) const
std::vector<uint8_t> CJNIMediaDrm::getPropertyByteArray(const std::string &propertyName) const
{
JNIEnv *env = xbmc_jnienv();
jhbyteArray array = call_method<jhbyteArray>(m_object,
"getPropertyByteArray", "(Ljava/lang/String;)[B",
jcast<jhstring>(propertyName));

std::vector<char> result;
std::vector<uint8_t> result;

if (!env->ExceptionCheck())
{
Expand All @@ -86,12 +86,11 @@ void CJNIMediaDrm::setPropertyString(const std::string &propertyName, const std:
jcast<jhstring>(propertyName), jcast<jhstring>(value));
}

void CJNIMediaDrm::setPropertyByteArray(const std::string &propertyName, const std::vector<char> &value) const
void CJNIMediaDrm::setPropertyByteArray(const std::string &propertyName, const std::vector<uint8_t> &value) const
{
JNIEnv *env = xbmc_jnienv();
call_method<void>(m_object,
"setPropertyByteArray", "(Ljava/lang/String;[B)V",
jcast<jhstring>(propertyName), jcast<jhbyteArray, std::vector<char> >(value));
JNIEnv* env = xbmc_jnienv();
call_method<void>(m_object, "setPropertyByteArray", "(Ljava/lang/String;[B)V",
jcast<jhstring>(propertyName), jcast<jhbyteArray, std::vector<uint8_t>>(value));
}

CJNIMediaDrmKeyRequest CJNIMediaDrm::getKeyRequest(
Expand Down
4 changes: 2 additions & 2 deletions lib/jni/jni/src/MediaDrm.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class CJNIMediaDrm : public CJNIBase
void closeSession(const std::vector<char> & sessionId) const;

std::string getPropertyString(const std::string &propertyName) const;
std::vector<char> getPropertyByteArray(const std::string &propertyName) const;
std::vector<uint8_t> getPropertyByteArray(const std::string& propertyName) const;
void setPropertyString(const std::string &propertyName, const std::string &value) const;
void setPropertyByteArray(const std::string &propertyName, const std::vector<char> &value) const;
void setPropertyByteArray(const std::string &propertyName, const std::vector<uint8_t> &value) const;

CJNIMediaDrmKeyRequest getKeyRequest(const std::vector<char> &scope,
const std::vector<uint8_t> &init, const std::string &mimeType, int keyType,
Expand Down
3 changes: 0 additions & 3 deletions src/Iaes_decrypter.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,4 @@ class IAESDecrypter
virtual void ivFromSequence(uint8_t* buffer, uint64_t sid) = 0;
virtual const std::string& getLicenseKey() const = 0;
virtual bool RenewLicense(const std::string& pluginUrl) = 0;

private:
std::string m_licenseKey;
};
68 changes: 29 additions & 39 deletions src/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ CSession::CSession(const PROPERTIES::KodiProperties& kodiProps,

if (!kodiProps.m_serverCertificate.empty())
{
std::string decCert{BASE64::DecodeToStr(kodiProps.m_serverCertificate)};
m_serverCertificate.SetData(reinterpret_cast<const AP4_Byte*>(decCert.data()),
static_cast<AP4_Size>(decCert.size()));
m_serverCertificate = BASE64::Decode(kodiProps.m_serverCertificate);
}
}

Expand Down Expand Up @@ -106,7 +104,7 @@ void CSession::SetSupportedDecrypterURN(std::string& key_system)
return;
}

key_system = m_decrypter->SelectKeySytem(m_kodiProps.m_licenseType.c_str());
key_system = m_decrypter->SelectKeySytem(m_kodiProps.m_licenseType);
m_decrypter->SetLibraryPath(kodi::vfs::TranslateSpecialProtocol(specialpath).c_str());
m_decrypter->SetProfilePath(m_profilePath);
m_decrypter->SetDebugSaveLicense(kodi::addon::GetSettingBoolean("debug.save.license"));
Expand Down Expand Up @@ -310,20 +308,18 @@ bool CSession::PreInitializeDRM(std::string& challengeB64,

if (!m_decrypter->IsInitialised())
{
if (!m_decrypter->OpenDRMSystem(m_kodiProps.m_licenseKey.c_str(), m_serverCertificate,
if (!m_decrypter->OpenDRMSystem(m_kodiProps.m_licenseKey, m_serverCertificate,
m_drmConfig))
{
LOG::LogF(LOGERROR, "OpenDRMSystem failed");
return false;
}
}

AP4_DataBuffer init_data;
init_data.SetBufferSize(1024);
std::vector<uint8_t> initData;

// Set the provided PSSH
std::vector<uint8_t> decPsshData = BASE64::Decode(psshData);
init_data.SetData(decPsshData.data(), static_cast<AP4_Size>(decPsshData.size()));
initData = BASE64::Decode(psshData);

// Decode the provided KID
std::string decKid{BASE64::DecodeToStr(kidData)};
Expand All @@ -333,9 +329,9 @@ bool CSession::PreInitializeDRM(std::string& challengeB64,
std::string hexKid{StringUtils::ToHexadecimal(decKid)};
LOG::LogF(LOGDEBUG, "Initializing session with KID: %s", hexKid.c_str());

if (m_decrypter && init_data.GetDataSize() >= 4 &&
if (m_decrypter && initData.size() >= 4 &&
(session.m_cencSingleSampleDecrypter = m_decrypter->CreateSingleSampleDecrypter(
init_data, "", decKid, true, CryptoMode::AES_CTR)) != 0)
initData, "", decKid, true, CryptoMode::AES_CTR)) != 0)
{
session.m_cdmSessionStr = session.m_cencSingleSampleDecrypter->GetSessionId();
sessionId = session.m_cdmSessionStr;
Expand Down Expand Up @@ -387,7 +383,7 @@ bool CSession::InitializeDRM(bool addDefaultKID /* = false */)

if (!m_decrypter->IsInitialised())
{
if (!m_decrypter->OpenDRMSystem(licenseKey.c_str(), m_serverCertificate, m_drmConfig))
if (!m_decrypter->OpenDRMSystem(licenseKey, m_serverCertificate, m_drmConfig))
{
LOG::Log(LOGERROR, "OpenDRMSystem failed");
return false;
Expand All @@ -404,7 +400,7 @@ bool CSession::InitializeDRM(bool addDefaultKID /* = false */)
// cdmSession 0 is reserved for unencrypted streams
for (size_t ses{1}; ses < m_cdmSessions.size(); ++ses)
{
AP4_DataBuffer init_data;
std::vector<uint8_t> initData;
std::string drmOptionalKeyParam;

CPeriod::PSSHSet& sessionPsshset = m_adaptiveTree->m_currentPeriod->GetPSSHSets()[ses];
Expand All @@ -428,9 +424,7 @@ bool CSession::InitializeDRM(bool addDefaultKID /* = false */)
LOG::Log(LOGDEBUG, "License data: Create Widevine PSSH for SmoothStreaming, based on "
"license data property");
}
std::vector<uint8_t> init_data_v;
CreateISMlicense(sessionPsshset.defaultKID_, licenseData, init_data_v);
init_data.SetData(init_data_v.data(), static_cast<AP4_Size>(init_data_v.size()));
CreateISMlicense(sessionPsshset.defaultKID_, licenseData, initData);
}
else if (m_kodiProps.m_licenseType == "com.microsoft.playready")
{
Expand All @@ -453,22 +447,20 @@ bool CSession::InitializeDRM(bool addDefaultKID /* = false */)
// This can allow to initialize a DRM that could be also not specified
// as supported in the manifest (e.g. missing DASH ContentProtection tags)
LOG::Log(LOGDEBUG, "License data: Use PSSH data provided by the license data property");
std::vector<uint8_t> licenseData = BASE64::Decode(m_kodiProps.m_licenseData);
init_data.SetData(licenseData.data(), static_cast<AP4_Size>(licenseData.size()));
initData = BASE64::Decode(m_kodiProps.m_licenseData);
}

if (init_data.GetDataSize() == 0)
if (initData.size() == 0)
{
if (!sessionPsshset.pssh_.empty())
{
// Use the PSSH provided by manifest
init_data.SetData(sessionPsshset.pssh_.data(),
static_cast<AP4_Size>(sessionPsshset.pssh_.size()));
initData = sessionPsshset.pssh_;
}
else
{
// Try extract the PSSH/KID from the stream
if (!ExtractStreamProtectionData(sessionPsshset, init_data, keySystem))
if (!ExtractStreamProtectionData(sessionPsshset, initData, keySystem))
LOG::Log(LOGERROR, "License data: Cannot extract PSSH/KID data from the stream");
}
}
Expand Down Expand Up @@ -518,10 +510,10 @@ bool CSession::InitializeDRM(bool addDefaultKID /* = false */)
}
}

if (m_decrypter && init_data.GetDataSize() >= 4 &&
if (m_decrypter && initData.size() >= 4 &&
(session.m_cencSingleSampleDecrypter ||
(session.m_cencSingleSampleDecrypter = m_decrypter->CreateSingleSampleDecrypter(
init_data, drmOptionalKeyParam, defaultKid, false,
initData, drmOptionalKeyParam, defaultKid, false,
sessionPsshset.m_cryptoMode == CryptoMode::NONE ? CryptoMode::AES_CTR
: sessionPsshset.m_cryptoMode)) !=
0))
Expand Down Expand Up @@ -736,8 +728,8 @@ void CSession::UpdateStream(CStream& stream)

if (!rep->GetCodecPrivateData().empty())
{
std::string annexb;
const std::string* extraData(&annexb);
std::vector<uint8_t> annexb;
const std::vector<uint8_t>* extraData(&annexb);

const DRM::IDecrypter::DecrypterCapabilites& caps{GetDecrypterCaps(rep->m_psshSetPos)};

Expand All @@ -751,8 +743,7 @@ void CSession::UpdateStream(CStream& stream)
{
extraData = &rep->GetCodecPrivateData();
}
stream.m_info.SetExtraData(reinterpret_cast<const uint8_t*>(extraData->c_str()),
extraData->size());
stream.m_info.SetExtraData(extraData->data(), extraData->size());
}

stream.m_info.SetCodecFourCC(0);
Expand Down Expand Up @@ -1452,30 +1443,27 @@ AP4_Movie* CSession::CreateMovieAtom(CStream* stream)
if (repr->GetContainerType() == ContainerType::MP4 && !repr->HasInitSegment())
{
AP4_SampleDescription* sampleDesc;
const std::string& extradata = repr->GetCodecPrivateData();
const std::vector<uint8_t>& extradata = repr->GetCodecPrivateData();

if (stream->m_info.GetCodecName() == CODEC::NAME_H264)
{
AP4_MemoryByteStream ms{reinterpret_cast<const uint8_t*>(extradata.data()),
static_cast<const AP4_Size>(extradata.size())};
AP4_MemoryByteStream ms{extradata.data(), static_cast<const AP4_Size>(extradata.size())};
AP4_AvccAtom* atom =
AP4_AvccAtom::Create(static_cast<AP4_Size>(AP4_ATOM_HEADER_SIZE + extradata.size()), ms);
sampleDesc = new AP4_AvcSampleDescription(AP4_SAMPLE_FORMAT_AVC1, stream->m_info.GetWidth(),
stream->m_info.GetHeight(), 0, nullptr, atom);
}
else if (stream->m_info.GetCodecName() == CODEC::NAME_HEVC)
{
AP4_MemoryByteStream ms{reinterpret_cast<const AP4_UI08*>(extradata.data()),
static_cast<const AP4_Size>(extradata.size())};
AP4_MemoryByteStream ms{extradata.data(), static_cast<const AP4_Size>(extradata.size())};
AP4_HvccAtom* atom =
AP4_HvccAtom::Create(static_cast<AP4_Size>(AP4_ATOM_HEADER_SIZE + extradata.size()), ms);
sampleDesc = new AP4_HevcSampleDescription(AP4_SAMPLE_FORMAT_HEV1, stream->m_info.GetWidth(),
stream->m_info.GetHeight(), 0, nullptr, atom);
}
else if (stream->m_info.GetCodecName() == CODEC::NAME_AV1)
{
AP4_MemoryByteStream ms{reinterpret_cast<const AP4_UI08*>(extradata.data()),
static_cast<AP4_Size>(extradata.size())};
AP4_MemoryByteStream ms{extradata.data(), static_cast<const AP4_Size>(extradata.size())};
AP4_Av1cAtom* atom =
AP4_Av1cAtom::Create(static_cast<AP4_Size>(AP4_ATOM_HEADER_SIZE + extradata.size()), ms);
sampleDesc = new AP4_Av1SampleDescription(AP4_SAMPLE_FORMAT_AV01, stream->m_info.GetWidth(),
Expand Down Expand Up @@ -1525,7 +1513,7 @@ AP4_Movie* CSession::CreateMovieAtom(CStream* stream)
}

bool CSession::ExtractStreamProtectionData(PLAYLIST::CPeriod::PSSHSet& sessionPsshset,
AP4_DataBuffer& init_data,
std::vector<uint8_t>& initData,
std::string keySystem)
{
keySystem = STRING::ToHexadecimal(keySystem);
Expand All @@ -1547,11 +1535,13 @@ bool CSession::ExtractStreamProtectionData(PLAYLIST::CPeriod::PSSHSet& sessionPs
}
AP4_Array<AP4_PsshAtom>& pssh{movie->GetPsshAtoms()};

for (unsigned int i = 0; init_data.GetDataSize() == 0 && i < pssh.ItemCount(); i++)
for (unsigned int i = 0; initData.size() == 0 && i < pssh.ItemCount(); i++)
{
if (std::memcmp(pssh[i].GetSystemId(), keySystem.c_str(), 16) == 0)
{
init_data.AppendData(pssh[i].GetData().GetData(), pssh[i].GetData().GetDataSize());
const AP4_DataBuffer& dataBuf = pssh[i].GetData();
initData.insert(initData.end(), dataBuf.GetData(), dataBuf.GetData() + dataBuf.GetDataSize());

if (sessionPsshset.defaultKID_.empty())
{
if (pssh[i].GetKid(0))
Expand Down Expand Up @@ -1592,5 +1582,5 @@ bool CSession::ExtractStreamProtectionData(PLAYLIST::CPeriod::PSSHSet& sessionPs
}

stream.Disable();
return init_data.GetDataSize() > 0;
return initData.size() > 0;
}
4 changes: 2 additions & 2 deletions src/Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,14 @@ class ATTR_DLL_LOCAL CSession : public adaptive::AdaptiveStreamObserver
void DisposeDecrypter();

bool ExtractStreamProtectionData(PLAYLIST::CPeriod::PSSHSet& sessionPsshset,
AP4_DataBuffer& init_data,
std::vector<uint8_t>& initData,
std::string keySystem);

private:
const UTILS::PROPERTIES::KodiProperties m_kodiProps;
std::string m_manifestUrl;
std::string m_profilePath;
AP4_DataBuffer m_serverCertificate;
std::vector<uint8_t> m_serverCertificate;
std::unique_ptr<kodi::tools::CDllHelper> m_dllHelper;
DRM::IDecrypter* m_decrypter{nullptr};

Expand Down
4 changes: 0 additions & 4 deletions src/common/AdaptiveTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ namespace CHOOSER
{
class IRepresentationChooser;
}
namespace PLAYLIST
{
enum class TreeType;
}

namespace adaptive
{
Expand Down
Loading

0 comments on commit 54b3f50

Please sign in to comment.