Skip to content

Commit 87fd790

Browse files
committed
temp
1 parent 6fad13e commit 87fd790

17 files changed

+288
-312
lines changed

src/common/AdaptiveDecrypter.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "decrypters/DrmEngineDefines.h"
1212
#include "utils/CryptoUtils.h"
13+
#include "utils/ResultType.h"
1314

1415
#include <cstdint>
1516
#include <stdexcept>
@@ -59,6 +60,17 @@ class Adaptive_CencSingleSampleDecrypter : public AP4_CencSingleSampleDecrypter
5960
virtual AP4_UI32 AddPool() { return 0; }
6061
virtual void RemovePool(AP4_UI32 poolId) {}
6162

63+
/*
64+
* \brief Create a DRM session.
65+
* \param pssh[OPT] A PSSH to make the license request, its use depends on DRM.
66+
* \param licenseUrl[OPT] A license URL or URI to make the license request, its use depends on DRM.
67+
* \param skipSessionMessage[OPT] Skip the session message to avoid make a license request, by default should be set false.
68+
* \return SResult::Ok on success, otherwise a SResult failed status.
69+
*/
70+
virtual SResult CreateSession(const std::vector<uint8_t>& pssh,
71+
std::string_view licenseUrl,
72+
bool skipSessionMessage) = 0;
73+
6274
/*!
6375
* \brief The session ID, is mandatory to distinguish sessions.
6476
*/

src/decrypters/DrmEngine.cpp

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,7 @@ STREAM_CRYPTO_KEY_SYSTEM KSToCryptoKeySystem(std::string_view keySystem)
4545
else
4646
return STREAM_CRYPTO_KEY_SYSTEM_NONE;
4747
}
48-
/*
49-
SResult CreateDRM(std::string_view keySystem, std::shared_ptr<DRM::IDecrypter>& drm)
50-
{
51-
std::string decrypterPath = CSrvBroker::GetSettings().GetDecrypterPath();
52-
if (decrypterPath.empty())
53-
{
54-
LOG::LogF(LOGERROR, "No decrypter path set in the add-on settings");
55-
return SResult::Error(GUI::GetLocalizedString(30302));
56-
}
57-
58-
drm = DRM::FACTORY::GetDecrypter(KSToCryptoKeySystem(keySystem));
59-
60-
if (!drm)
61-
{
62-
LOG::LogF(LOGERROR, "Unable to create the DRM decrypter");
63-
return SResult::Error(GUI::GetLocalizedString(30303));
64-
}
65-
66-
drm->SetLibraryPath(decrypterPath);
67-
68-
if (!drm->Initialize())
69-
{
70-
drm = nullptr;
71-
LOG::LogF(LOGERROR, "Unable to initialize the DRM decrypter");
72-
return SResult::Error(GUI::GetLocalizedString(30303));
73-
}
7448

75-
return SResultCode::OK;
76-
}
77-
*/
7849
/*!
7950
* \brief Get a DRMInfo by Key System
8051
* \param drmInfos The manifest DRM info
@@ -128,10 +99,10 @@ bool GetCapabilities(const std::optional<bool> isForceSecureDecoder,
12899
}
129100
} // unnamed namespace
130101

131-
void DRM::CDRMEngine::Initialize()
102+
bool DRM::CDRMEngine::Initialize()
132103
{
133104
if (!m_drms.empty())
134-
return; // assume as already initialized
105+
return true; // assume as already initialized
135106

136107
//! @todo: to test a way to initialize DRM when manifest is downloaded/parsed
137108
//! in the hoping to have a more smoother playback transition from unencrypted->to->encrypted periods
@@ -169,7 +140,8 @@ void DRM::CDRMEngine::Initialize()
169140
{
170141
LOG::LogF(LOGERROR,
171142
"Cannot initialize DrmEngine, no decrypter path set in the add-on settings");
172-
return;
143+
m_status = EngineStatus::DRM_ERROR;
144+
return false;
173145
}
174146

175147
// Initialize DRMs
@@ -178,7 +150,10 @@ void DRM::CDRMEngine::Initialize()
178150
(*it)->SetLibraryPath(decrypterPath);
179151

180152
if (!(*it)->Initialize()) // Failed to initialize DRM, delete it and go on
153+
{
154+
LOG::LogF(LOGERROR, "Unable to initialize %s DRM", (*it)->GetName().c_str());
181155
it = drms.erase(it);
156+
}
182157
else
183158
++it;
184159
}
@@ -193,6 +168,14 @@ void DRM::CDRMEngine::Initialize()
193168
m_drms.emplace_back(ks, drm);
194169
}
195170
}
171+
172+
if (m_drms.empty())
173+
{
174+
LOG::LogF(LOGWARNING, "No DRM available");
175+
return false;
176+
}
177+
178+
return true;
196179
}
197180

198181
bool DRM::CDRMEngine::PreInitializeDRM(DRMSession& session)
@@ -204,7 +187,8 @@ bool DRM::CDRMEngine::PreInitializeDRM(DRMSession& session)
204187
if (!propDrmCfg.priority.has_value() || propDrmCfg.priority != 1 || propDrmCfg.preInitData.empty())
205188
return false;
206189

207-
Initialize();
190+
if (!Initialize())
191+
return false;
208192

209193
// Pre-initialize the DRM is available for Widevine only.
210194
// Since the manifest will be downloaded later its assumed that
@@ -237,30 +221,31 @@ bool DRM::CDRMEngine::PreInitializeDRM(DRMSession& session)
237221
if (!drm)
238222
{
239223
m_status = EngineStatus::DRM_ERROR;
240-
LOG::LogF(LOGERROR, "Cannot get the DRM instance");
224+
LOG::LogF(LOGERROR, "Cannot get the DRM instance for keysystem %s", m_keySystem.c_str());
241225
GUI::ErrorDialog(GUI::GetLocalizedString(30303));
242226
return false;
243227
}
244228

229+
LOG::LogF(LOGDEBUG, "Initializing session with KID: %s", STRING::ToHexadecimal(kidData).c_str());
230+
245231
DRM::Config drmCfg = CreateDRMConfig(m_keySystem, kodiProps.GetDrmConfig(m_keySystem));
246232

247-
SResult ret = drm->OpenDRMSystem(drmCfg);
248-
if (ret.IsFailed())
233+
auto dec = drm->CreateSingleSampleDecrypter(drmCfg, kidData, CryptoMode::AES_CTR);
234+
235+
if (!dec)
249236
{
250-
LOG::LogF(LOGERROR, "Failed to open the DRM");
251-
m_status = EngineStatus::DRM_ERROR;
252-
GUI::ErrorDialog(ret.Message());
237+
LOG::Log(LOGERROR, "Failed to initialize the %s DRM decrypter", drm->GetName().c_str());
238+
m_status = EngineStatus::DECRYPTER_ERROR;
253239
return false;
254240
}
255241

256-
LOG::LogF(LOGDEBUG, "Initializing session with KID: %s", STRING::ToHexadecimal(kidData).c_str());
257-
258-
auto dec = drm->CreateSingleSampleDecrypter(initData, kidData, "", true, CryptoMode::AES_CTR);
242+
const SResult ret = dec->CreateSession(initData, "", true);
259243

260-
if (!dec)
244+
if (ret.IsFailed())
261245
{
262-
LOG::LogF(LOGERROR, "Failed to initialize the decrypter");
263-
m_status = EngineStatus::DECRYPTER_ERROR;
246+
LOG::LogF(LOGERROR, "Failed to create the DRM session");
247+
m_status = EngineStatus::DRM_ERROR;
248+
GUI::ErrorDialog(ret.Message());
264249
return false;
265250
}
266251

@@ -291,7 +276,8 @@ bool DRM::CDRMEngine::InitializeSession(std::vector<DRM::DRMInfo> drmInfos,
291276
if (drmInfos.empty())
292277
return false;
293278

294-
Initialize();
279+
if (!Initialize())
280+
return false;
295281

296282
LOG::Log(LOGDEBUG, "Initialize crypto session");
297283

@@ -428,7 +414,7 @@ bool DRM::CDRMEngine::InitializeSession(std::vector<DRM::DRMInfo> drmInfos,
428414
if (!drm)
429415
{
430416
m_status = EngineStatus::DRM_ERROR;
431-
LOG::LogF(LOGERROR, "Cannot get the DRM instance");
417+
LOG::LogF(LOGERROR, "Cannot get the DRM instance for keysystem %s", m_keySystem.c_str());
432418
GUI::ErrorDialog(GUI::GetLocalizedString(30303));
433419
return false;
434420
}
@@ -438,26 +424,29 @@ bool DRM::CDRMEngine::InitializeSession(std::vector<DRM::DRMInfo> drmInfos,
438424
newSes.mediaType = mediaType;
439425
newSes.kid = drmInfo.defaultKid;
440426

441-
if (!newSes.drm->IsInitialised())
442-
{
443-
DRM::Config drmCfg = DRM::CreateDRMConfig(m_keySystem, drmPropCfg);
444-
const SResult ret = newSes.drm->OpenDRMSystem(drmCfg);
445-
if (ret.IsFailed())
446-
{
447-
LOG::LogF(LOGERROR, "Failed to open the DRM");
448-
m_status = EngineStatus::DRM_ERROR;
449-
GUI::ErrorDialog(ret.Message());
450-
return false;
451-
}
452-
}
427+
DRM::Config drmCfg = DRM::CreateDRMConfig(m_keySystem, drmPropCfg);
453428

454429
newSes.decrypter = newSes.drm->CreateSingleSampleDecrypter(
455-
drmInfo.initData, drmInfoKidBytes, drmInfo.licenseServerUri, false,
430+
drmCfg, drmInfoKidBytes,
456431
drmInfo.cryptoMode == CryptoMode::NONE ? CryptoMode::AES_CTR : drmInfo.cryptoMode);
432+
457433
if (!newSes.decrypter)
458434
{
459-
LOG::Log(LOGERROR, "Failed to initialize the decrypter");
460435
m_status = EngineStatus::DECRYPTER_ERROR;
436+
LOG::Log(LOGERROR, "Failed to initialize the %s DRM decrypter",
437+
newSes.drm->GetName().c_str());
438+
GUI::ErrorDialog(GUI::GetLocalizedString(30303));
439+
return false;
440+
}
441+
442+
const SResult ret =
443+
newSes.decrypter->CreateSession(drmInfo.initData, drmInfo.licenseServerUri, false);
444+
445+
if (ret.IsFailed())
446+
{
447+
LOG::LogF(LOGERROR, "Failed to create the DRM session");
448+
m_status = EngineStatus::DRM_ERROR;
449+
GUI::ErrorDialog(ret.Message());
461450
return false;
462451
}
463452

@@ -660,6 +649,7 @@ bool DRM::CDRMEngine::SelectDRM(std::vector<DRM::DRMInfo>& drmInfos)
660649
if (drmInfo)
661650
{
662651
m_keySystem = drm.keySystem;
652+
LOG::LogF(LOGDEBUG, "Selected DRM key system: %s", m_keySystem.c_str());
663653
break;
664654
}
665655
}

src/decrypters/DrmEngine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class ATTR_DLL_LOCAL CDRMEngine
9797
/*!
9898
* \brief Initialize the DRM engine.
9999
*/
100-
void Initialize();
100+
bool Initialize();
101101

102102
/*!
103103
* \brief Configure DRM ClearKey, by replacing manifest DRM info when needed.

src/decrypters/DrmEngineDefines.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ struct DRMInfo
125125

126126
struct DRMInstance
127127
{
128-
std::string_view keySystem;
128+
std::string keySystem;
129129
std::shared_ptr<DRM::IDecrypter> drm;
130130

131-
DRMInstance(const std::string_view& ks, std::shared_ptr<DRM::IDecrypter> d)
131+
DRMInstance(std::string_view ks, std::shared_ptr<DRM::IDecrypter> d)
132132
: keySystem(ks), drm(d)
133133
{
134134
}

src/decrypters/IDecrypter.h

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ class IDecrypter : public IDecrypterDecoder
7575

7676
virtual ~IDecrypter() {}
7777

78+
/*
79+
* \brief Return a short name for the decrypter implementation
80+
*/
81+
virtual const std::string GetName() const { return "Unknown"; }
82+
7883
/*
7984
* \brief Initialize the decrypter library
8085
* \return True if has success, otherwise false
@@ -83,13 +88,6 @@ class IDecrypter : public IDecrypterDecoder
8388

8489
virtual bool IsKeySystemSupported(std::string_view keySystem) = 0;
8590

86-
/*
87-
* \brief Initialise the DRM system
88-
* \param config The DRM configuration
89-
* \return true on success
90-
*/
91-
virtual SResult OpenDRMSystem(const DRM::Config& config) = 0;
92-
9391
/*
9492
* \brief Creates a Single Sample Decrypter for decrypting content
9593
* \param initData The data for initialising the decrypter (e.g. PSSH)
@@ -100,10 +98,8 @@ class IDecrypter : public IDecrypterDecoder
10098
* \return The single sample decrypter if successfully created
10199
*/
102100
virtual std::shared_ptr<Adaptive_CencSingleSampleDecrypter> CreateSingleSampleDecrypter(
103-
const std::vector<uint8_t>& initData,
101+
const DRM::Config& config,
104102
const std::vector<uint8_t>& defaultKeyId,
105-
std::string_view licenseUrl,
106-
bool skipSessionMessage,
107103
CryptoMode cryptoMode) = 0;
108104

109105
/*
@@ -131,12 +127,6 @@ class IDecrypter : public IDecrypterDecoder
131127
return std::nullopt;
132128
}
133129

134-
/*
135-
* \brief Check if the decrypter has been initialised (OpenDRMSystem called)
136-
* \return True if decrypter has been initialised otherwise false
137-
*/
138-
virtual bool IsInitialised() = 0;
139-
140130
/*
141131
* \brief Retrieve license challenge data
142132
* \param decrypter The single sample decrypter to use for license challenge

0 commit comments

Comments
 (0)