Skip to content

Commit

Permalink
Refactor some Bento4 added methods as subclassed code
Browse files Browse the repository at this point in the history
  • Loading branch information
glennguy committed Apr 9, 2023
1 parent 999fe6d commit a54870f
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(ADP_SOURCES
src/codechandler/WebVTTCodecHandler.cpp
src/codechandler/ttml/TTML.cpp
src/common/AdaptationSet.cpp
src/common/AdaptiveCencSampleDecrypter.cpp
src/common/AdaptiveStream.cpp
src/common/AdaptiveTree.cpp
src/common/AdaptiveUtils.cpp
Expand Down Expand Up @@ -80,6 +81,7 @@ set(ADP_HEADERS
src/codechandler/WebVTTCodecHandler.h
src/codechandler/ttml/TTML.h
src/common/AdaptationSet.h
src/common/AdaptiveCencSampleDecrypter.h
src/common/AdaptiveDecrypter.h
src/common/AdaptiveStream.h
src/common/AdaptiveTree.h
Expand Down
57 changes: 57 additions & 0 deletions src/common/AdaptiveCencSampleDecrypter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2023 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#include "AdaptiveCencSampleDecrypter.h"
#include "AdaptiveDecrypter.h"

#include "../utils/log.h"

AP4_Result CAdaptiveCencSampleDecrypter::DecryptSampleData(AP4_UI32 poolid,
AP4_DataBuffer& data_in,
AP4_DataBuffer& data_out,
const AP4_UI08* iv)
{
// increment the sample cursor
unsigned int sample_cursor = m_SampleCursor++;

// setup the IV
unsigned char iv_block[16];
if (!iv)
{
iv = m_SampleInfoTable->GetIv(sample_cursor);
}
if (!iv)
return AP4_ERROR_INVALID_FORMAT;
unsigned int iv_size = m_SampleInfoTable->GetIvSize();
AP4_CopyMemory(iv_block, iv, iv_size);
if (iv_size != 16)
AP4_SetMemory(&iv_block[iv_size], 0, 16 - iv_size);

// get the subsample info for this sample if needed
unsigned int subsample_count = 0;
const AP4_UI16* bytes_of_cleartext_data = nullptr;
const AP4_UI32* bytes_of_encrypted_data = nullptr;
if (m_SampleInfoTable)
{
AP4_Result result = m_SampleInfoTable->GetSampleInfo(
sample_cursor, subsample_count, bytes_of_cleartext_data, bytes_of_encrypted_data);
if (AP4_FAILED(result))
return result;
}

// decrypt the sample
Adaptive_CencSingleSampleDecrypter* decrypter =
dynamic_cast<Adaptive_CencSingleSampleDecrypter*>(m_SingleSampleDecrypter);
if (!decrypter)
{
LOG::LogF(LOGERROR, "Failed to cast AP4 decrypter to Adaptive");
return AP4_ERROR_INVALID_PARAMETERS;
}
return decrypter->DecryptSampleData(poolid, data_in, data_out, iv_block, subsample_count,
bytes_of_cleartext_data, bytes_of_encrypted_data);
}
24 changes: 24 additions & 0 deletions src/common/AdaptiveCencSampleDecrypter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2023 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#include <bento4/Ap4.h>

class CAdaptiveCencSampleDecrypter : public AP4_CencSampleDecrypter
{
public:
CAdaptiveCencSampleDecrypter(AP4_CencSingleSampleDecrypter* single_sample_decrypter,
AP4_CencSampleInfoTable* sample_info_table)
: AP4_CencSampleDecrypter(single_sample_decrypter, sample_info_table)
{
}

virtual AP4_Result DecryptSampleData(AP4_UI32 poolid,
AP4_DataBuffer& data_in,
AP4_DataBuffer& data_out,
const AP4_UI08* iv);
};
21 changes: 20 additions & 1 deletion src/common/AdaptiveDecrypter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#pragma once

#include "../CryptoMode.h"
#include "../utils/CryptoUtils.h"

#include <stdexcept>
#include <string_view>
Expand All @@ -35,4 +35,23 @@ class Adaptive_CencSingleSampleDecrypter : public AP4_CencSingleSampleDecrypter
{
throw std::logic_error("SetDefaultKeyId method not implemented.");
};

virtual AP4_Result SetFragmentInfo(AP4_UI32 pool_id,
const AP4_UI08* key,
const AP4_UI08 nal_length_size,
AP4_DataBuffer& annexb_sps_pps,
AP4_UI32 flags,
CryptoInfo cryptoInfo) = 0;

virtual AP4_Result DecryptSampleData(AP4_UI32 poolid,
AP4_DataBuffer& data_in,
AP4_DataBuffer& data_out,
const AP4_UI08* iv,
unsigned int subsample_count,
const AP4_UI16* bytes_of_cleartext_data,
const AP4_UI32* bytes_of_encrypted_data) = 0;

virtual AP4_UI32 AddPool() { return 0; }
virtual void RemovePool(AP4_UI32 poolid) {}
virtual const char* GetSessionId() { return nullptr; }
};
4 changes: 3 additions & 1 deletion src/samplereader/FragmentedSampleReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,14 @@ AP4_Result CFragmentedSampleReader::ProcessMoof(AP4_ContainerAtom* moof,
// we assume unencrypted fragment here
goto SUCCESS;

AP4_CencSampleDecrypter* decrypter = nullptr;
if (AP4_FAILED(result = AP4_CencSampleDecrypter::Create(sample_table, algorithm_id, 0, 0, 0,
reset_iv, m_singleSampleDecryptor,
m_decrypter)))
decrypter)))
{
return result;
}
m_decrypter = new CAdaptiveCencSampleDecrypter(m_singleSampleDecryptor, sample_table);

// Inform decrypter of pattern decryption (CBCS)
AP4_UI32 schemeType = m_protectedDesc->GetSchemeType();
Expand Down
3 changes: 2 additions & 1 deletion src/samplereader/FragmentedSampleReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "../SSD_dll.h"
#include "../codechandler/CodecHandler.h"
#include "../common/AdaptiveDecrypter.h"
#include "../common/AdaptiveCencSampleDecrypter.h"
#include "../utils/log.h"
#include "SampleReader.h"

Expand Down Expand Up @@ -82,7 +83,7 @@ class ATTR_DLL_LOCAL CFragmentedSampleReader : public ISampleReader, public AP4_
const AP4_UI08* m_defaultKey{nullptr};
AP4_ProtectedSampleDescription* m_protectedDesc{nullptr};
Adaptive_CencSingleSampleDecrypter* m_singleSampleDecryptor;
AP4_CencSampleDecrypter* m_decrypter{nullptr};
CAdaptiveCencSampleDecrypter* m_decrypter{nullptr};
uint64_t m_nextDuration{0};
uint64_t m_nextTimestamp{0};
CryptoInfo m_readerCryptoInfo{};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/CryptoUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum class CryptoMode

struct CryptoInfo
{
CryptoMode m_mode{CryptoMode::NONE};
uint8_t m_cryptBlocks{0};
uint8_t m_skipBlocks{0};
CryptoMode m_mode{CryptoMode::NONE};
};

0 comments on commit a54870f

Please sign in to comment.