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 8, 2023
1 parent e66c06c commit aa50b63
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ set(ADP_HEADERS
src/codechandler/WebVTTCodecHandler.h
src/codechandler/ttml/TTML.h
src/common/AdaptationSet.h
src/common/AdaptiveDecrypter.h
src/common/AdaptiveCencSampleDecrypter.h
src/common/AdaptiveDecrypter.h
src/common/AdaptiveStream.h
src/common/AdaptiveTree.h
src/common/AdaptiveUtils.h
Expand Down
68 changes: 68 additions & 0 deletions src/common/AdaptiveCencSampleDecrypter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2022 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.
*/

#pragma once

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

#include <bento4/Ap4.h>

class Adaptive_CencSampleDecrypter : public AP4_CencSampleDecrypter
{
public:
Adaptive_CencSampleDecrypter(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)
{
// increment the sample cursor
unsigned int sample_cursor = m_SampleCursor++;

// setup the IV
unsigned char iv_block[16];
if (iv == NULL)
{
iv = m_SampleInfoTable->GetIv(sample_cursor);
}
if (iv == NULL)
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 = NULL;
const AP4_UI32* bytes_of_encrypted_data = NULL;
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::Log(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);
}
};
28 changes: 28 additions & 0 deletions src/common/AdaptiveDecrypter.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,32 @@ 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,
AP4_UI08 cryptoType,
AP4_UI08 cryptBlocks,
AP4_UI08 skipBlocks)
{
return AP4_ERROR_NOT_SUPPORTED;
};

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)
{
return AP4_CencSingleSampleDecrypter::DecryptSampleData(
data_in, data_out, iv, subsample_count, bytes_of_cleartext_data, bytes_of_encrypted_data);
};

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 Adaptive_CencSampleDecrypter(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};
Adaptive_CencSampleDecrypter* m_decrypter{nullptr};
uint64_t m_nextDuration{0};
uint64_t m_nextTimestamp{0};
ReaderCryptoInfo m_readerCryptoInfo{};
Expand Down

0 comments on commit aa50b63

Please sign in to comment.