Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
CastagnaIT committed Aug 27, 2024
1 parent e1690f5 commit e753b0d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "common/Chooser.h"
#include "decrypters/DrmFactory.h"
#include "decrypters/Helpers.h"
#include "parser/PRProtectionParser.h"
#include "utils/Base64Utils.h"
#include "utils/CurlUtils.h"
#include "utils/StringUtils.h"
Expand Down Expand Up @@ -451,6 +452,23 @@ bool CSession::InitializeDRM(bool addDefaultKID /* = false */)
initData = BASE64::Decode(licenseDataStr);
}

if (!initData.empty() && defaultKidStr.empty())
{
CPsshParser parser;
if (parser.Parse(initData))
{
if (!parser.GetData().empty())
{
auto data = DRM::GetKIDWidevinePsshData(parser.GetData());
if (!data.empty())
{
LOG::LogF(LOGDEBUG, "KID PARSED FROM PSSH");
defaultKidStr = STRING::ToHexadecimal(data);
}
}
}
}

//! @todo: as is implemented InitializeDRM will initialize all PSSHSet's also when are not used,
//! therefore ExtractStreamProtectionData can perform many (not needed) downloads of mp4 init files
if ((initData.empty() && licenseType != DRM::KS_CLEARKEY) || defaultKidStr.empty())
Expand Down
40 changes: 40 additions & 0 deletions src/decrypters/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ void WriteProtobufVarint(std::vector<uint8_t>& data, int size)
} while (size > 0);
}

int ReadProtobufVarint(const std::vector<uint8_t>& data, size_t& offset)
{
int value = 0;
int shift = 0;
while (true)
{
uint8_t byte = data[offset++];
value |= (byte & 0x7F) << shift;
if (!(byte & 0x80))
break;
shift += 7;
}
return value;
}

/*!
* \brief Replace in a vector, a sequence of vector data with another one.
* \param data The data to be modified
Expand Down Expand Up @@ -229,6 +244,31 @@ bool DRM::IsValidPsshHeader(const std::vector<uint8_t>& pssh)
return pssh.size() >= 8 && std::equal(pssh.begin() + 4, pssh.begin() + 8, PSSHBOX_HEADER_PSSH);
}

std::vector<uint8_t> DRM::GetKIDWidevinePsshData(const std::vector<uint8_t>& wvPsshData)
{
size_t offset = 0;
while (offset < wvPsshData.size())
{
uint8_t tag = wvPsshData[offset++];
int fieldNumber = tag >> 3;
int wireType = tag & 0x07;

if (fieldNumber == 2 && wireType == 2) // "key_id" field, id: 2
{
int length = ReadProtobufVarint(wvPsshData, offset);
std::vector<uint8_t> kid(wvPsshData.begin() + offset, wvPsshData.begin() + offset + length);
return kid;
}
else // Skip field
{
int length = ReadProtobufVarint(wvPsshData, offset);
if (wireType != 0)
offset += length;
}
}
return {}; // Not found
}

bool DRM::MakeWidevinePsshData(const std::vector<uint8_t>& kid,
std::vector<uint8_t> contentIdData,
std::vector<uint8_t>& wvPsshData)
Expand Down
1 change: 1 addition & 0 deletions src/decrypters/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ std::vector<uint8_t> ConvertKidToUUIDVec(const std::vector<uint8_t>& kid);
* \param kid The PlayReady KeyId
* \return The Widevine KeyId, otherwise empty if fails.
*/
std::vector<uint8_t> GetKIDWidevinePsshData(const std::vector<uint8_t>& wvPsshData);
std::vector<uint8_t> ConvertPrKidtoWvKid(std::vector<uint8_t> kid);

bool IsValidPsshHeader(const std::vector<uint8_t>& pssh);
Expand Down
2 changes: 1 addition & 1 deletion src/parser/DASHTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ bool adaptive::CDashTree::GetProtectionData(

// There are no constraints on the Kid format, it is recommended to be as UUID but not mandatory
STRING::ReplaceAll(selectedKid, "-", "");
kid = selectedKid;
//kid = selectedKid;

return isEncrypted;
}
Expand Down

0 comments on commit e753b0d

Please sign in to comment.