Skip to content

Commit

Permalink
Merge pull request #1318 from CastagnaIT/cleanup_data
Browse files Browse the repository at this point in the history
[Cleanup] Use vector uint8_t to store data
  • Loading branch information
glennguy authored Jul 10, 2023
2 parents 2bef579 + 7d1c746 commit 70dd055
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/Iaes_decrypter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <cstdint>
#include <string>
#include <vector>

class IAESDecrypter
{
Expand All @@ -21,7 +22,7 @@ class IAESDecrypter
virtual void decrypt(const AP4_UI08* aes_key,
const AP4_UI08* aes_iv,
const AP4_UI08* src,
std::string& dst,
std::vector<uint8_t>& dst,
size_t dstOffset,
size_t& dataSize,
bool lastChunk) = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/aes_decrypter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
void AESDecrypter::decrypt(const AP4_UI08* aes_key,
const AP4_UI08* aes_iv,
const AP4_UI08* src,
std::string& dst,
std::vector<uint8_t>& dst,
size_t dstOffset,
size_t& dataSize,
bool lastChunk)
Expand Down
3 changes: 2 additions & 1 deletion src/aes_decrypter.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <bento4/Ap4Types.h>

#include <string>
#include <vector>

#ifdef INPUTSTREAM_TEST_BUILD
#include "test/KodiStubs.h"
Expand All @@ -29,7 +30,7 @@ class ATTR_DLL_LOCAL AESDecrypter : public IAESDecrypter
void decrypt(const AP4_UI08* aes_key,
const AP4_UI08* aes_iv,
const AP4_UI08* src,
std::string& dst,
std::vector<uint8_t>& dst,
size_t dstOffset,
size_t& dataSize,
bool lastChunk);
Expand Down
16 changes: 9 additions & 7 deletions src/common/AdaptiveStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ void adaptive::AdaptiveStream::DeallocateSegmentBuffers()
}
}

bool adaptive::AdaptiveStream::Download(const DownloadInfo& downloadInfo, std::string& data)
bool adaptive::AdaptiveStream::Download(const DownloadInfo& downloadInfo,
std::vector<uint8_t>& data)
{
return DownloadImpl(downloadInfo, &data);
}
Expand All @@ -121,7 +122,7 @@ bool adaptive::AdaptiveStream::DownloadSegment(const DownloadInfo& downloadInfo)
}

bool adaptive::AdaptiveStream::DownloadImpl(const DownloadInfo& downloadInfo,
std::string* downloadData)
std::vector<uint8_t>* downloadData)
{
if (downloadInfo.m_url.empty())
return false;
Expand Down Expand Up @@ -153,7 +154,7 @@ bool adaptive::AdaptiveStream::DownloadImpl(const DownloadInfo& downloadInfo,

while (downloadStatus == CURL::ReadStatus::CHUNK_READ)
{
std::vector<char> bufferData(CURL::BUFFER_SIZE_32);
std::vector<uint8_t> bufferData(CURL::BUFFER_SIZE_32);
size_t bytesRead{0};

downloadStatus = curl.ReadChunk(bufferData.data(), CURL::BUFFER_SIZE_32, bytesRead);
Expand All @@ -162,7 +163,7 @@ bool adaptive::AdaptiveStream::DownloadImpl(const DownloadInfo& downloadInfo,
{
if (downloadData) // Write the data in to the string
{
downloadData->append(bufferData.data(), bytesRead);
downloadData->insert(downloadData->end(), bufferData.begin(), bufferData.end());
}
else // Write the data to the segment buffer
{
Expand All @@ -177,7 +178,7 @@ bool adaptive::AdaptiveStream::DownloadImpl(const DownloadInfo& downloadInfo,
if (state_ == STOPPED)
break;

std::string& segmentBuffer = downloadInfo.m_segmentBuffer->buffer;
std::vector<uint8_t>& segmentBuffer = downloadInfo.m_segmentBuffer->buffer;

tree_.OnDataArrived(downloadInfo.m_segmentBuffer->segment_number,
downloadInfo.m_segmentBuffer->segment.pssh_set_, m_decrypterIv,
Expand Down Expand Up @@ -436,7 +437,8 @@ int AdaptiveStream::SecondsSinceUpdate() const
.count());
}

bool AdaptiveStream::parseIndexRange(PLAYLIST::CRepresentation* rep, const std::string& buffer)
bool AdaptiveStream::parseIndexRange(PLAYLIST::CRepresentation* rep,
const std::vector<uint8_t>& buffer)
{
#ifndef INPUTSTREAM_TEST_BUILD
LOG::Log(LOGDEBUG, "[AS-%u] Build segments from SIDX atom...", clsId);
Expand Down Expand Up @@ -1226,7 +1228,7 @@ bool AdaptiveStream::ResolveSegmentBase(PLAYLIST::CRepresentation* rep)
else
return false;

std::string sidxBuffer;
std::vector<uint8_t> sidxBuffer;
DownloadInfo downloadInfo;
// We assume mutex_dl is locked so we can safely call prepare_download
if (PrepareDownload(rep, seg, downloadInfo) && Download(downloadInfo, sidxBuffer) &&
Expand Down
9 changes: 5 additions & 4 deletions src/common/AdaptiveStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ class AdaptiveStream;
bool StreamChanged() { return stream_changed_; }

protected:
virtual bool parseIndexRange(PLAYLIST::CRepresentation* rep, const std::string& buffer);
virtual bool parseIndexRange(PLAYLIST::CRepresentation* rep,
const std::vector<uint8_t>& buffer);

virtual void SetLastUpdated(const std::chrono::system_clock::time_point tm) {}
std::chrono::time_point<std::chrono::system_clock> lastUpdated_;
Expand All @@ -105,7 +106,7 @@ class AdaptiveStream;

struct SEGMENTBUFFER
{
std::string buffer;
std::vector<uint8_t> buffer;
PLAYLIST::CSegment segment;
uint64_t segment_number{0};
PLAYLIST::CRepresentation* rep{nullptr};
Expand All @@ -132,7 +133,7 @@ class AdaptiveStream;
* \param data[OUT] The downloaded data
* \return Return true if success, otherwise false
*/
virtual bool Download(const DownloadInfo& downloadInfo, std::string& data);
virtual bool Download(const DownloadInfo& downloadInfo, std::vector<uint8_t>& data);

/*!
* \brief Download a segment file in chunks, the data could be also decrypted by the manifest parser,
Expand All @@ -151,7 +152,7 @@ class AdaptiveStream;
* will be stored to the segment buffer and could be decrypted by the manifest parser.
* \return Return true if success, otherwise false
*/
bool DownloadImpl(const DownloadInfo& downloadInfo, std::string* data);
bool DownloadImpl(const DownloadInfo& downloadInfo, std::vector<uint8_t>* data);

bool PrepareNextDownload(DownloadInfo& downloadInfo);
bool PrepareDownload(const PLAYLIST::CRepresentation* rep,
Expand Down
6 changes: 3 additions & 3 deletions src/common/AdaptiveTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ namespace adaptive
void AdaptiveTree::OnDataArrived(uint64_t segNum,
uint16_t psshSet,
uint8_t iv[16],
const char* srcData,
const uint8_t* srcData,
size_t srcDataSize,
std::string& segBuffer,
std::vector<uint8_t>& segBuffer,
size_t segBufferSize,
bool isLastChunk)
{
segBuffer.append(srcData, srcDataSize);
segBuffer.insert(segBuffer.end(), srcData, srcData + srcDataSize);
}

uint16_t AdaptiveTree::InsertPsshSet(PLAYLIST::StreamType streamType,
Expand Down
4 changes: 2 additions & 2 deletions src/common/AdaptiveTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ class ATTR_DLL_LOCAL AdaptiveTree
virtual void OnDataArrived(uint64_t segNum,
uint16_t psshSet,
uint8_t iv[16],
const char* srcData,
const uint8_t* srcData,
size_t srcDataSize,
std::string& segBuffer,
std::vector<uint8_t>& segBuffer,
size_t segBufferSize,
bool isLastChunk);

Expand Down
8 changes: 4 additions & 4 deletions src/parser/HLSTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,9 @@ PLAYLIST::PrepareRepStatus adaptive::CHLSTree::prepareRepresentation(PLAYLIST::C
void adaptive::CHLSTree::OnDataArrived(uint64_t segNum,
uint16_t psshSet,
uint8_t iv[16],
const char* srcData,
const uint8_t* srcData,
size_t srcDataSize,
std::string& segBuffer,
std::vector<uint8_t>& segBuffer,
size_t segBufferSize,
bool isLastChunk)
{
Expand Down Expand Up @@ -684,7 +684,7 @@ void adaptive::CHLSTree::OnDataArrived(uint64_t segNum,
}
if (pssh.defaultKID_ == "0")
{
segBuffer.insert(segBufferSize, srcDataSize, 0);
segBuffer.resize(segBufferSize + srcDataSize, 0);
return;
}
else if (!segBufferSize)
Expand All @@ -698,7 +698,7 @@ void adaptive::CHLSTree::OnDataArrived(uint64_t segNum,
}
}

// Decrypter needs preallocated string data
// Decrypter needs preallocated data
segBuffer.resize(segBufferSize + srcDataSize);

m_decrypter->decrypt(reinterpret_cast<const uint8_t*>(pssh.defaultKID_.data()), iv,
Expand Down
4 changes: 2 additions & 2 deletions src/parser/HLSTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class ATTR_DLL_LOCAL CHLSTree : public AdaptiveTree
virtual void OnDataArrived(uint64_t segNum,
uint16_t psshSet,
uint8_t iv[16],
const char* srcData,
const uint8_t* srcData,
size_t srcDataSize,
std::string& segBuffer,
std::vector<uint8_t>& segBuffer,
size_t segBufferSize,
bool isLastChunk) override;

Expand Down
13 changes: 7 additions & 6 deletions src/test/TestHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ bool TestAdaptiveStream::DownloadSegment(const DownloadInfo& downloadInfo)
if (downloadInfo.m_url.empty())
return false;

std::string& segmentBuffer = downloadInfo.m_segmentBuffer->buffer;
std::vector<uint8_t>& segmentBuffer = downloadInfo.m_segmentBuffer->buffer;
std::stringstream sampleData("Sixteen bytes!!!");

const size_t bufferSize = 8;
char bufferData[bufferSize];
uint8_t bufferData[bufferSize];
size_t totalByteRead = 0;

sampleData.clear();
Expand All @@ -108,7 +108,7 @@ bool TestAdaptiveStream::DownloadSegment(const DownloadInfo& downloadInfo)
if (state_ == STOPPED)
break;

sampleData.read(bufferData, bufferSize);
sampleData.read(reinterpret_cast<char*>(bufferData), bufferSize);
size_t bytesRead = sampleData.gcount();

if (bytesRead == 0) // EOF
Expand All @@ -134,16 +134,17 @@ bool TestAdaptiveStream::DownloadSegment(const DownloadInfo& downloadInfo)
return true;
}

bool TestAdaptiveStream::Download(const DownloadInfo& downloadInfo, std::string& data)
bool TestAdaptiveStream::Download(const DownloadInfo& downloadInfo, std::vector<uint8_t>& data)
{
data = "Sixteen bytes!!!";
const char* dataStr = "Sixteen bytes!!!";
data.insert(data.end(), dataStr, dataStr + 16);
return true;
}

void AESDecrypter::decrypt(const AP4_UI08* aes_key,
const AP4_UI08* aes_iv,
const AP4_UI08* src,
std::string& dst,
std::vector<uint8_t>& dst,
size_t dstOffset,
size_t& dataSize,
bool lastChunk)
Expand Down
4 changes: 2 additions & 2 deletions src/test/TestHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TestAdaptiveStream : public adaptive::AdaptiveStream
virtual bool DownloadSegment(const DownloadInfo& downloadInfo) override;

protected:
virtual bool Download(const DownloadInfo& downloadInfo, std::string& data) override;
virtual bool Download(const DownloadInfo& downloadInfo, std::vector<uint8_t>& data) override;
};

class AESDecrypter : public IAESDecrypter
Expand All @@ -87,7 +87,7 @@ class AESDecrypter : public IAESDecrypter
void decrypt(const AP4_UI08* aes_key,
const AP4_UI08* aes_iv,
const AP4_UI08* src,
std::string& dst,
std::vector<uint8_t>& dst,
size_t dstOffset,
size_t& dataSize,
bool lastChunk);
Expand Down

0 comments on commit 70dd055

Please sign in to comment.