Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Widevine] Use default KID as fallback #1567

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/decrypters/widevine/WVCencSingleSampleDecrypter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void CWVCencSingleSampleDecrypter::SetSession(const char* session,

CWVCencSingleSampleDecrypter::CWVCencSingleSampleDecrypter(CWVCdmAdapter& drm,
std::vector<uint8_t>& pssh,
std::string_view defaultKeyId,
std::vector<uint8_t>& defaultKeyId,
bool skipSessionMessage,
CryptoMode cryptoMode,
CWVDecrypter* host)
Expand Down Expand Up @@ -436,7 +436,7 @@ bool CWVCencSingleSampleDecrypter::SendSessionMessage()

if (blocks[2][kidPos - 1] == 'H')
{
std::string keyIdUUID{StringUtils::ToHexadecimal(m_defaultKeyId)};
std::string keyIdUUID{STRING::ToHexadecimal(m_defaultKeyId)};
blocks[2].replace(kidPos - 1, 6, keyIdUUID.c_str(), 32);
}
else
Expand Down Expand Up @@ -664,7 +664,18 @@ AP4_Result CWVCencSingleSampleDecrypter::SetFragmentInfo(AP4_UI32 poolId,
if (poolId >= m_fragmentPool.size())
return AP4_ERROR_OUT_OF_RANGE;

m_fragmentPool[poolId].m_key = keyId;
// KID as 00000000000000000000000000000000
const std::vector<uint8_t> emptyKeyId = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

if (keyId == emptyKeyId)
{
m_fragmentPool[poolId].m_key = m_defaultKeyId;
LOG::Log(LOGDEBUG, "SET DEFAULT KID");
}
else
m_fragmentPool[poolId].m_key = keyId;

m_fragmentPool[poolId].m_nalLengthSize = nalLengthSize;
m_fragmentPool[poolId].m_annexbSpsPps.SetData(annexbSpsPps.GetData(), annexbSpsPps.GetDataSize());
m_fragmentPool[poolId].m_decrypterFlags = flags;
Expand Down Expand Up @@ -1164,7 +1175,7 @@ void CWVCencSingleSampleDecrypter::ResetVideo()

void CWVCencSingleSampleDecrypter::SetDefaultKeyId(std::string_view keyId)
{
m_defaultKeyId = keyId;
m_defaultKeyId.assign(keyId.begin(), keyId.end());
}

void CWVCencSingleSampleDecrypter::AddKeyId(std::string_view keyId)
Expand Down
4 changes: 2 additions & 2 deletions src/decrypters/widevine/WVCencSingleSampleDecrypter.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ATTR_DLL_LOCAL CWVCencSingleSampleDecrypter : public Adaptive_CencSingleSa
// methods
CWVCencSingleSampleDecrypter(CWVCdmAdapter& drm,
std::vector<uint8_t>& pssh,
std::string_view defaultKeyId,
std::vector<uint8_t>& defaultKeyId,
bool skipSessionMessage,
CryptoMode cryptoMode,
CWVDecrypter* host);
Expand Down Expand Up @@ -93,7 +93,7 @@ class ATTR_DLL_LOCAL CWVCencSingleSampleDecrypter : public Adaptive_CencSingleSa
std::string m_strSession;
std::vector<uint8_t> m_pssh;
AP4_DataBuffer m_challenge;
std::string m_defaultKeyId;
std::vector<uint8_t> m_defaultKeyId;
struct WVSKEY
{
bool operator==(WVSKEY const& other) const { return m_keyId == other.m_keyId; };
Expand Down
4 changes: 3 additions & 1 deletion src/decrypters/widevine/WVDecrypter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ Adaptive_CencSingleSampleDecrypter* CWVDecrypter::CreateSingleSampleDecrypter(
bool skipSessionMessage,
CryptoMode cryptoMode)
{
std::vector<uint8_t> kid(defaultKeyId.begin(), defaultKeyId.end());

CWVCencSingleSampleDecrypter* decrypter = new CWVCencSingleSampleDecrypter(
*m_WVCdmAdapter, pssh, defaultKeyId, skipSessionMessage, cryptoMode, this);
*m_WVCdmAdapter, pssh, kid, skipSessionMessage, cryptoMode, this);
if (!decrypter->GetSessionId())
{
delete decrypter;
Expand Down
4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ bool CInputStreamAdaptive::OpenStream(int streamid)
}

uint16_t psshSetPos = stream->m_adStream.getRepresentation()->m_psshSetPos;

std::string kid = stream->m_adStream.getPeriod()->GetPSSHSets()[psshSetPos].defaultKID_;


reader->SetDecrypter(m_session->GetSingleSampleDecryptor(psshSetPos),
m_session->GetDecrypterCaps(psshSetPos));

Expand Down
2 changes: 2 additions & 0 deletions src/samplereader/FragmentedSampleReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ CFragmentedSampleReader::CFragmentedSampleReader(AP4_ByteStream* input,
m_track{track},
m_streamId{streamId}
{
m_defaultKey = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
}

CFragmentedSampleReader::~CFragmentedSampleReader()
Expand Down
11 changes: 11 additions & 0 deletions src/utils/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,17 @@ std::string UTILS::STRING::ToHexadecimal(std::string_view str)
return ss.str();
}

std::string UTILS::STRING::ToHexadecimal(const std::vector<uint8_t>& str)
{
std::ostringstream ss;
ss << std::hex;
for (const uint8_t ch : str)
{
ss << std::setw(2) << std::setfill('0') << static_cast<unsigned long>(ch);
}
return ss.str();
}

std::string UTILS::STRING::Trim(std::string value)
{
StringUtils::Trim(value);
Expand Down
1 change: 1 addition & 0 deletions src/utils/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ std::vector<uint8_t> ToVecUint8(std::string_view str);
* \return The string on its hexadecimal representation
*/
std::string ToHexadecimal(std::string_view str);
std::string ToHexadecimal(const std::vector<uint8_t>& str);

/*!
* \brief Trim a string with remove of not wanted spaces at begin and end of string.
Expand Down
14 changes: 14 additions & 0 deletions src/utils/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,20 @@ std::string UTILS::ConvertKIDtoUUID(std::string_view kid)
return uuid;
}

std::string UTILS::ConvertKIDtoUUID(const std::vector<uint8_t>& kid)
{
static char hexDigits[] = "0123456789abcdef";
std::string uuid;
for (size_t i{0}; i < 16; ++i)
{
if (i == 4 || i == 6 || i == 8 || i == 10)
uuid += '-';
uuid += hexDigits[kid[i] >> 4];
uuid += hexDigits[kid[i] & 15];
}
return uuid;
}

bool UTILS::CreateISMlicense(std::string_view key,
std::string_view licenseData,
std::vector<uint8_t>& initData)
Expand Down
1 change: 1 addition & 0 deletions src/utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ std::vector<uint8_t> AnnexbToAvc(const char* b16Data);
std::vector<uint8_t> AvcToAnnexb(const std::vector<uint8_t>& avc);
std::string ConvertKIDtoWVKID(std::string_view kid);
std::string ConvertKIDtoUUID(std::string_view kid);
std::string ConvertKIDtoUUID(const std::vector<uint8_t>& kid);
bool CreateISMlicense(std::string_view key,
std::string_view licenseData,
std::vector<uint8_t>& initData);
Expand Down