Skip to content

Commit 54b3f50

Browse files
authored
Merge pull request #1395 from CastagnaIT/cleanups
Various code cleaning
2 parents 95c4dfa + 3b6f2ec commit 54b3f50

27 files changed

+316
-269
lines changed

lib/jni/jni/jutils/jutils-details.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,31 @@ struct jcast_helper<std::vector<char>, jbyteArray>
6565
}
6666
};
6767

68+
template<>
69+
struct jcast_helper<std::vector<uint8_t>, jbyteArray>
70+
{
71+
static std::vector<uint8_t> cast(jbyteArray const& v)
72+
{
73+
JNIEnv* env = xbmc_jnienv();
74+
jsize size = 0;
75+
std::vector<uint8_t> vec;
76+
if (v)
77+
{
78+
size = env->GetArrayLength(v);
79+
80+
vec.reserve(size);
81+
82+
jbyte* elements = env->GetByteArrayElements(v, NULL);
83+
for (int i = 0; i < size; i++)
84+
{
85+
vec.emplace_back(static_cast<uint8_t>(elements[i]));
86+
}
87+
env->ReleaseByteArrayElements(v, elements, JNI_ABORT);
88+
}
89+
return vec;
90+
}
91+
};
92+
6893
template <>
6994
struct jcast_helper<std::vector<int>, jintArray>
7095
{
@@ -229,6 +254,15 @@ struct jcast_helper<std::vector<char>, jhbyteArray>
229254
}
230255
};
231256

257+
template<>
258+
struct jcast_helper<std::vector<uint8_t>, jhbyteArray>
259+
{
260+
static std::vector<uint8_t> cast(jhbyteArray const& v)
261+
{
262+
return jcast_helper<std::vector<uint8_t>, jbyteArray>::cast(v.get());
263+
}
264+
};
265+
232266
template <>
233267
struct jcast_helper<std::vector<int>, jhintArray>
234268
{

lib/jni/jni/src/MediaDrm.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ std::string CJNIMediaDrm::getPropertyString(const std::string &propertyName) con
6161
jcast<jhstring>(propertyName)));
6262
}
6363

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

71-
std::vector<char> result;
71+
std::vector<uint8_t> result;
7272

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

89-
void CJNIMediaDrm::setPropertyByteArray(const std::string &propertyName, const std::vector<char> &value) const
89+
void CJNIMediaDrm::setPropertyByteArray(const std::string &propertyName, const std::vector<uint8_t> &value) const
9090
{
91-
JNIEnv *env = xbmc_jnienv();
92-
call_method<void>(m_object,
93-
"setPropertyByteArray", "(Ljava/lang/String;[B)V",
94-
jcast<jhstring>(propertyName), jcast<jhbyteArray, std::vector<char> >(value));
91+
JNIEnv* env = xbmc_jnienv();
92+
call_method<void>(m_object, "setPropertyByteArray", "(Ljava/lang/String;[B)V",
93+
jcast<jhstring>(propertyName), jcast<jhbyteArray, std::vector<uint8_t>>(value));
9594
}
9695

9796
CJNIMediaDrmKeyRequest CJNIMediaDrm::getKeyRequest(

lib/jni/jni/src/MediaDrm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class CJNIMediaDrm : public CJNIBase
4242
void closeSession(const std::vector<char> & sessionId) const;
4343

4444
std::string getPropertyString(const std::string &propertyName) const;
45-
std::vector<char> getPropertyByteArray(const std::string &propertyName) const;
45+
std::vector<uint8_t> getPropertyByteArray(const std::string& propertyName) const;
4646
void setPropertyString(const std::string &propertyName, const std::string &value) const;
47-
void setPropertyByteArray(const std::string &propertyName, const std::vector<char> &value) const;
47+
void setPropertyByteArray(const std::string &propertyName, const std::vector<uint8_t> &value) const;
4848

4949
CJNIMediaDrmKeyRequest getKeyRequest(const std::vector<char> &scope,
5050
const std::vector<uint8_t> &init, const std::string &mimeType, int keyType,

src/Iaes_decrypter.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,4 @@ class IAESDecrypter
3030
virtual void ivFromSequence(uint8_t* buffer, uint64_t sid) = 0;
3131
virtual const std::string& getLicenseKey() const = 0;
3232
virtual bool RenewLicense(const std::string& pluginUrl) = 0;
33-
34-
private:
35-
std::string m_licenseKey;
3633
};

src/Session.cpp

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ CSession::CSession(const PROPERTIES::KodiProperties& kodiProps,
6666

6767
if (!kodiProps.m_serverCertificate.empty())
6868
{
69-
std::string decCert{BASE64::DecodeToStr(kodiProps.m_serverCertificate)};
70-
m_serverCertificate.SetData(reinterpret_cast<const AP4_Byte*>(decCert.data()),
71-
static_cast<AP4_Size>(decCert.size()));
69+
m_serverCertificate = BASE64::Decode(kodiProps.m_serverCertificate);
7270
}
7371
}
7472

@@ -106,7 +104,7 @@ void CSession::SetSupportedDecrypterURN(std::string& key_system)
106104
return;
107105
}
108106

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

311309
if (!m_decrypter->IsInitialised())
312310
{
313-
if (!m_decrypter->OpenDRMSystem(m_kodiProps.m_licenseKey.c_str(), m_serverCertificate,
311+
if (!m_decrypter->OpenDRMSystem(m_kodiProps.m_licenseKey, m_serverCertificate,
314312
m_drmConfig))
315313
{
316314
LOG::LogF(LOGERROR, "OpenDRMSystem failed");
317315
return false;
318316
}
319317
}
320318

321-
AP4_DataBuffer init_data;
322-
init_data.SetBufferSize(1024);
319+
std::vector<uint8_t> initData;
323320

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

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

336-
if (m_decrypter && init_data.GetDataSize() >= 4 &&
332+
if (m_decrypter && initData.size() >= 4 &&
337333
(session.m_cencSingleSampleDecrypter = m_decrypter->CreateSingleSampleDecrypter(
338-
init_data, "", decKid, true, CryptoMode::AES_CTR)) != 0)
334+
initData, "", decKid, true, CryptoMode::AES_CTR)) != 0)
339335
{
340336
session.m_cdmSessionStr = session.m_cencSingleSampleDecrypter->GetSessionId();
341337
sessionId = session.m_cdmSessionStr;
@@ -387,7 +383,7 @@ bool CSession::InitializeDRM(bool addDefaultKID /* = false */)
387383

388384
if (!m_decrypter->IsInitialised())
389385
{
390-
if (!m_decrypter->OpenDRMSystem(licenseKey.c_str(), m_serverCertificate, m_drmConfig))
386+
if (!m_decrypter->OpenDRMSystem(licenseKey, m_serverCertificate, m_drmConfig))
391387
{
392388
LOG::Log(LOGERROR, "OpenDRMSystem failed");
393389
return false;
@@ -404,7 +400,7 @@ bool CSession::InitializeDRM(bool addDefaultKID /* = false */)
404400
// cdmSession 0 is reserved for unencrypted streams
405401
for (size_t ses{1}; ses < m_cdmSessions.size(); ++ses)
406402
{
407-
AP4_DataBuffer init_data;
403+
std::vector<uint8_t> initData;
408404
std::string drmOptionalKeyParam;
409405

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

460-
if (init_data.GetDataSize() == 0)
453+
if (initData.size() == 0)
461454
{
462455
if (!sessionPsshset.pssh_.empty())
463456
{
464457
// Use the PSSH provided by manifest
465-
init_data.SetData(sessionPsshset.pssh_.data(),
466-
static_cast<AP4_Size>(sessionPsshset.pssh_.size()));
458+
initData = sessionPsshset.pssh_;
467459
}
468460
else
469461
{
470462
// Try extract the PSSH/KID from the stream
471-
if (!ExtractStreamProtectionData(sessionPsshset, init_data, keySystem))
463+
if (!ExtractStreamProtectionData(sessionPsshset, initData, keySystem))
472464
LOG::Log(LOGERROR, "License data: Cannot extract PSSH/KID data from the stream");
473465
}
474466
}
@@ -518,10 +510,10 @@ bool CSession::InitializeDRM(bool addDefaultKID /* = false */)
518510
}
519511
}
520512

521-
if (m_decrypter && init_data.GetDataSize() >= 4 &&
513+
if (m_decrypter && initData.size() >= 4 &&
522514
(session.m_cencSingleSampleDecrypter ||
523515
(session.m_cencSingleSampleDecrypter = m_decrypter->CreateSingleSampleDecrypter(
524-
init_data, drmOptionalKeyParam, defaultKid, false,
516+
initData, drmOptionalKeyParam, defaultKid, false,
525517
sessionPsshset.m_cryptoMode == CryptoMode::NONE ? CryptoMode::AES_CTR
526518
: sessionPsshset.m_cryptoMode)) !=
527519
0))
@@ -736,8 +728,8 @@ void CSession::UpdateStream(CStream& stream)
736728

737729
if (!rep->GetCodecPrivateData().empty())
738730
{
739-
std::string annexb;
740-
const std::string* extraData(&annexb);
731+
std::vector<uint8_t> annexb;
732+
const std::vector<uint8_t>* extraData(&annexb);
741733

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

@@ -751,8 +743,7 @@ void CSession::UpdateStream(CStream& stream)
751743
{
752744
extraData = &rep->GetCodecPrivateData();
753745
}
754-
stream.m_info.SetExtraData(reinterpret_cast<const uint8_t*>(extraData->c_str()),
755-
extraData->size());
746+
stream.m_info.SetExtraData(extraData->data(), extraData->size());
756747
}
757748

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

14571448
if (stream->m_info.GetCodecName() == CODEC::NAME_H264)
14581449
{
1459-
AP4_MemoryByteStream ms{reinterpret_cast<const uint8_t*>(extradata.data()),
1460-
static_cast<const AP4_Size>(extradata.size())};
1450+
AP4_MemoryByteStream ms{extradata.data(), static_cast<const AP4_Size>(extradata.size())};
14611451
AP4_AvccAtom* atom =
14621452
AP4_AvccAtom::Create(static_cast<AP4_Size>(AP4_ATOM_HEADER_SIZE + extradata.size()), ms);
14631453
sampleDesc = new AP4_AvcSampleDescription(AP4_SAMPLE_FORMAT_AVC1, stream->m_info.GetWidth(),
14641454
stream->m_info.GetHeight(), 0, nullptr, atom);
14651455
}
14661456
else if (stream->m_info.GetCodecName() == CODEC::NAME_HEVC)
14671457
{
1468-
AP4_MemoryByteStream ms{reinterpret_cast<const AP4_UI08*>(extradata.data()),
1469-
static_cast<const AP4_Size>(extradata.size())};
1458+
AP4_MemoryByteStream ms{extradata.data(), static_cast<const AP4_Size>(extradata.size())};
14701459
AP4_HvccAtom* atom =
14711460
AP4_HvccAtom::Create(static_cast<AP4_Size>(AP4_ATOM_HEADER_SIZE + extradata.size()), ms);
14721461
sampleDesc = new AP4_HevcSampleDescription(AP4_SAMPLE_FORMAT_HEV1, stream->m_info.GetWidth(),
14731462
stream->m_info.GetHeight(), 0, nullptr, atom);
14741463
}
14751464
else if (stream->m_info.GetCodecName() == CODEC::NAME_AV1)
14761465
{
1477-
AP4_MemoryByteStream ms{reinterpret_cast<const AP4_UI08*>(extradata.data()),
1478-
static_cast<AP4_Size>(extradata.size())};
1466+
AP4_MemoryByteStream ms{extradata.data(), static_cast<const AP4_Size>(extradata.size())};
14791467
AP4_Av1cAtom* atom =
14801468
AP4_Av1cAtom::Create(static_cast<AP4_Size>(AP4_ATOM_HEADER_SIZE + extradata.size()), ms);
14811469
sampleDesc = new AP4_Av1SampleDescription(AP4_SAMPLE_FORMAT_AV01, stream->m_info.GetWidth(),
@@ -1525,7 +1513,7 @@ AP4_Movie* CSession::CreateMovieAtom(CStream* stream)
15251513
}
15261514

15271515
bool CSession::ExtractStreamProtectionData(PLAYLIST::CPeriod::PSSHSet& sessionPsshset,
1528-
AP4_DataBuffer& init_data,
1516+
std::vector<uint8_t>& initData,
15291517
std::string keySystem)
15301518
{
15311519
keySystem = STRING::ToHexadecimal(keySystem);
@@ -1547,11 +1535,13 @@ bool CSession::ExtractStreamProtectionData(PLAYLIST::CPeriod::PSSHSet& sessionPs
15471535
}
15481536
AP4_Array<AP4_PsshAtom>& pssh{movie->GetPsshAtoms()};
15491537

1550-
for (unsigned int i = 0; init_data.GetDataSize() == 0 && i < pssh.ItemCount(); i++)
1538+
for (unsigned int i = 0; initData.size() == 0 && i < pssh.ItemCount(); i++)
15511539
{
15521540
if (std::memcmp(pssh[i].GetSystemId(), keySystem.c_str(), 16) == 0)
15531541
{
1554-
init_data.AppendData(pssh[i].GetData().GetData(), pssh[i].GetData().GetDataSize());
1542+
const AP4_DataBuffer& dataBuf = pssh[i].GetData();
1543+
initData.insert(initData.end(), dataBuf.GetData(), dataBuf.GetData() + dataBuf.GetDataSize());
1544+
15551545
if (sessionPsshset.defaultKID_.empty())
15561546
{
15571547
if (pssh[i].GetKid(0))
@@ -1592,5 +1582,5 @@ bool CSession::ExtractStreamProtectionData(PLAYLIST::CPeriod::PSSHSet& sessionPs
15921582
}
15931583

15941584
stream.Disable();
1595-
return init_data.GetDataSize() > 0;
1585+
return initData.size() > 0;
15961586
}

src/Session.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,14 @@ class ATTR_DLL_LOCAL CSession : public adaptive::AdaptiveStreamObserver
366366
void DisposeDecrypter();
367367

368368
bool ExtractStreamProtectionData(PLAYLIST::CPeriod::PSSHSet& sessionPsshset,
369-
AP4_DataBuffer& init_data,
369+
std::vector<uint8_t>& initData,
370370
std::string keySystem);
371371

372372
private:
373373
const UTILS::PROPERTIES::KodiProperties m_kodiProps;
374374
std::string m_manifestUrl;
375375
std::string m_profilePath;
376-
AP4_DataBuffer m_serverCertificate;
376+
std::vector<uint8_t> m_serverCertificate;
377377
std::unique_ptr<kodi::tools::CDllHelper> m_dllHelper;
378378
DRM::IDecrypter* m_decrypter{nullptr};
379379

src/common/AdaptiveTree.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ namespace CHOOSER
3737
{
3838
class IRepresentationChooser;
3939
}
40-
namespace PLAYLIST
41-
{
42-
enum class TreeType;
43-
}
4440

4541
namespace adaptive
4642
{

0 commit comments

Comments
 (0)