Skip to content

Commit

Permalink
Merge pull request #1718 from CastagnaIT/cleanup_streamid
Browse files Browse the repository at this point in the history
[SampleReader][cleanups] Separate Get/Set stream id methods
  • Loading branch information
CastagnaIT authored Nov 6, 2024
2 parents 4c24b00 + 26eea40 commit 9d7d90a
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 68 deletions.
4 changes: 3 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,16 @@ bool CInputStreamAdaptive::OpenStream(int streamid)

ContainerType reprContainerType = rep->GetContainerType();
uint32_t mask = (1U << stream->m_info.GetStreamType()) | m_session->GetIncludedStreamMask();
auto reader = ADP::CreateStreamReader(reprContainerType, stream, static_cast<uint32_t>(streamid), mask);
auto reader = ADP::CreateStreamReader(reprContainerType, stream, mask);

if (!reader)
{
m_session->EnableStream(stream, false);
return false;
}

reader->SetStreamId(stream->m_info.GetStreamType(), streamid);

uint16_t psshSetPos = stream->m_adStream.getRepresentation()->m_psshSetPos;
reader->SetDecrypter(m_session->GetSingleSampleDecryptor(psshSetPos),
m_session->GetDecrypterCaps(psshSetPos));
Expand Down
2 changes: 1 addition & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ATTR_DLL_LOCAL CInputStreamAdaptive : public kodi::addon::CInstanceInputSt

private:
std::shared_ptr<SESSION::CSession> m_session;
std::map<INPUTSTREAM_TYPE, unsigned int> m_IncludedStreams;
std::map<INPUTSTREAM_TYPE, int> m_IncludedStreams; // stream type - stream id
int m_failedSeekTime = ~0;
std::string m_chapterName;
// The last PTS of the segment package fed to kodi.
Expand Down
6 changes: 2 additions & 4 deletions src/samplereader/ADTSSampleReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@

#include "AdaptiveByteStream.h"

CADTSSampleReader::CADTSSampleReader(AP4_ByteStream* input, AP4_UI32 streamId)
: ADTSReader{input},
m_streamId{streamId},
m_adByteStream{dynamic_cast<CAdaptiveByteStream*>(input)}
CADTSSampleReader::CADTSSampleReader(AP4_ByteStream* input)
: ADTSReader{input}, m_adByteStream{dynamic_cast<CAdaptiveByteStream*>(input)}
{
}

Expand Down
4 changes: 1 addition & 3 deletions src/samplereader/ADTSSampleReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CAdaptiveByteStream;
class ATTR_DLL_LOCAL CADTSSampleReader : public ISampleReader, public ADTSReader
{
public:
CADTSSampleReader(AP4_ByteStream* input, AP4_UI32 streamId);
CADTSSampleReader(AP4_ByteStream* input);

bool IsStarted() const override { return m_started; }
bool EOS() const override { return m_eos; }
Expand All @@ -32,7 +32,6 @@ class ATTR_DLL_LOCAL CADTSSampleReader : public ISampleReader, public ADTSReader
void SetPTSOffset(uint64_t offset) override { m_ptsOffs = offset; }
int64_t GetPTSDiff() const override { return m_ptsDiff; }
uint32_t GetTimeScale() const override { return 90000; }
AP4_UI32 GetStreamId() const override { return m_streamId; }
AP4_Size GetSampleDataSize() const override { return GetPacketSize(); }
const AP4_Byte* GetSampleData() const override { return GetPacketData(); }
uint64_t GetDuration() const override { return (ADTSReader::GetDuration() * 100) / 9; }
Expand All @@ -41,7 +40,6 @@ class ATTR_DLL_LOCAL CADTSSampleReader : public ISampleReader, public ADTSReader
private:
bool m_eos{false};
bool m_started{false};
AP4_UI32 m_streamId;
uint64_t m_pts{0};
int64_t m_ptsDiff{0};
uint64_t m_ptsOffs{~0ULL};
Expand Down
7 changes: 2 additions & 5 deletions src/samplereader/FragmentedSampleReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ constexpr uint8_t MP4_TFRFBOX_UUID[] = {0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46

CFragmentedSampleReader::CFragmentedSampleReader(AP4_ByteStream* input,
AP4_Movie* movie,
AP4_Track* track,
AP4_UI32 streamId)
: AP4_LinearReader{*movie, input},
m_track{track},
m_streamId{streamId}
AP4_Track* track)
: AP4_LinearReader{*movie, input}, m_track{track}
{
}

Expand Down
7 changes: 1 addition & 6 deletions src/samplereader/FragmentedSampleReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ class CodecHandler;
class ATTR_DLL_LOCAL CFragmentedSampleReader : public ISampleReader, public AP4_LinearReader
{
public:
CFragmentedSampleReader(AP4_ByteStream* input,
AP4_Movie* movie,
AP4_Track* track,
AP4_UI32 streamId);
CFragmentedSampleReader(AP4_ByteStream* input, AP4_Movie* movie, AP4_Track* track);

~CFragmentedSampleReader();

Expand All @@ -36,7 +33,6 @@ class ATTR_DLL_LOCAL CFragmentedSampleReader : public ISampleReader, public AP4_
bool IsStarted() const override { return m_started; }
uint64_t DTS() const override { return m_dts; }
uint64_t PTS() const override { return m_pts; }
AP4_UI32 GetStreamId() const override { return m_streamId; }
AP4_Size GetSampleDataSize() const override { return m_sampleData.GetDataSize(); }
const AP4_Byte* GetSampleData() const override { return m_sampleData.GetData(); }
uint64_t GetDuration() const override;
Expand All @@ -61,7 +57,6 @@ class ATTR_DLL_LOCAL CFragmentedSampleReader : public ISampleReader, public AP4_

AP4_Track* m_track;
AP4_UI32 m_poolId{0};
AP4_UI32 m_streamId;
AP4_UI32 m_sampleDescIndex{1};
DRM::DecrypterCapabilites m_decrypterCaps;
unsigned int m_failCount{0};
Expand Down
10 changes: 7 additions & 3 deletions src/samplereader/SampleReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,16 @@ class ATTR_DLL_LOCAL ISampleReader
virtual bool GetFragmentInfo(uint64_t& duration) { return false; }

virtual uint32_t GetTimeScale() const = 0;
virtual AP4_UI32 GetStreamId() const = 0;

virtual int GetStreamId() const { return m_streamId; }
virtual void SetStreamId(INPUTSTREAM_TYPE type, int streamId) { m_streamId = streamId; }

virtual AP4_Size GetSampleDataSize() const = 0;
virtual const AP4_Byte* GetSampleData() const = 0;
virtual uint64_t GetDuration() const = 0;
virtual bool IsEncrypted() const = 0;
virtual void AddStreamType(INPUTSTREAM_TYPE type, uint32_t sid) {};
virtual void SetStreamType(INPUTSTREAM_TYPE type, uint32_t sid) {};
virtual void AddStreamType(INPUTSTREAM_TYPE type, int streamId) {}
virtual void SetStreamType(INPUTSTREAM_TYPE type, int streamId) {}
virtual bool RemoveStreamType(INPUTSTREAM_TYPE type) { return true; };
virtual bool IsStarted() const = 0;
virtual CryptoInfo GetReaderCryptoInfo() const { return CryptoInfo(); }
Expand Down Expand Up @@ -123,4 +126,5 @@ class ATTR_DLL_LOCAL ISampleReader

private:
std::future<AP4_Result> m_readSampleAsyncState;
int m_streamId{0};
};
15 changes: 6 additions & 9 deletions src/samplereader/SampleReaderFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,25 @@ using namespace PLAYLIST;

std::unique_ptr<ISampleReader> ADP::CreateStreamReader(PLAYLIST::ContainerType& containerType,
SESSION::CStream* stream,
uint32_t streamId,
uint32_t includedStreamMask)
{
std::unique_ptr<ISampleReader> reader;

if (containerType == ContainerType::TEXT)
{
reader = std::make_unique<CSubtitleSampleReader>(streamId);
reader = std::make_unique<CSubtitleSampleReader>();
}
else if (containerType == ContainerType::TS)
{
reader = std::make_unique<CTSSampleReader>(
stream->GetAdByteStream(), stream->m_info.GetStreamType(), streamId, includedStreamMask);
reader = std::make_unique<CTSSampleReader>(stream->GetAdByteStream(), includedStreamMask);
}
else if (containerType == ContainerType::ADTS)
{
reader = std::make_unique<CADTSSampleReader>(stream->GetAdByteStream(), streamId);
reader = std::make_unique<CADTSSampleReader>(stream->GetAdByteStream());
}
else if (containerType == ContainerType::WEBM)
{
reader = std::make_unique<CWebmSampleReader>(stream->GetAdByteStream(), streamId);
reader = std::make_unique<CWebmSampleReader>(stream->GetAdByteStream());
}
else if (containerType == ContainerType::MP4)
{
Expand Down Expand Up @@ -85,8 +83,7 @@ std::unique_ptr<ISampleReader> ADP::CreateStreamReader(PLAYLIST::ContainerType&
return nullptr;
}

reader = std::make_unique<CFragmentedSampleReader>(stream->GetAdByteStream(), movie, track,
streamId);
reader = std::make_unique<CFragmentedSampleReader>(stream->GetAdByteStream(), movie, track);
}
else
{
Expand All @@ -110,7 +107,7 @@ std::unique_ptr<ISampleReader> ADP::CreateStreamReader(PLAYLIST::ContainerType&
stream->m_adStream.getRepresentation()->SetContainerType(containerType);

stream->GetAdByteStream()->Seek(0); // Seek because bytes are consumed from previous reader
reader = std::make_unique<CADTSSampleReader>(stream->GetAdByteStream(), streamId);
reader = std::make_unique<CADTSSampleReader>(stream->GetAdByteStream());
if (!reader->Initialize(stream))
reader.reset();
}
Expand Down
2 changes: 0 additions & 2 deletions src/samplereader/SampleReaderFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ namespace ADP
* \brief Create and initialize a sample stream reader for the specified container
* \param containerType The type of stream container, the value could be changed if the type is wrong
* \param stream The stream object that will use the reader
* \param streamId The stream id
* \param includedStreamMask The flags for included streams
* \return The sample reader if has success, otherwise nullptr
*/
std::unique_ptr<ISampleReader> CreateStreamReader(PLAYLIST::ContainerType& containerType,
SESSION::CStream* stream,
uint32_t streamId,
uint32_t includedStreamMask);

} // namespace ADP
4 changes: 0 additions & 4 deletions src/samplereader/SubtitleSampleReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
using namespace PLAYLIST;
using namespace UTILS;

CSubtitleSampleReader::CSubtitleSampleReader(AP4_UI32 streamId) : m_streamId{streamId}
{
}

bool CSubtitleSampleReader::Initialize(SESSION::CStream* stream)
{

Expand Down
4 changes: 1 addition & 3 deletions src/samplereader/SubtitleSampleReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AdaptiveStream;
class ATTR_DLL_LOCAL CSubtitleSampleReader : public ISampleReader
{
public:
CSubtitleSampleReader(AP4_UI32 streamId);
CSubtitleSampleReader() = default;

virtual bool Initialize(SESSION::CStream* stream) override;
bool IsStarted() const override { return m_started; }
Expand All @@ -44,7 +44,6 @@ class ATTR_DLL_LOCAL CSubtitleSampleReader : public ISampleReader
int64_t GetPTSDiff() const override { return m_ptsDiff; }
void SetPTSDiff(uint64_t pts) override;
uint32_t GetTimeScale() const override { return 1000; }
AP4_UI32 GetStreamId() const override { return m_streamId; }
AP4_Size GetSampleDataSize() const override { return m_sampleData.GetDataSize(); }
const AP4_Byte* GetSampleData() const override { return m_sampleData.GetData(); }
uint64_t GetDuration() const override { return m_sample.GetDuration() * 1000; }
Expand All @@ -55,7 +54,6 @@ class ATTR_DLL_LOCAL CSubtitleSampleReader : public ISampleReader

uint64_t m_pts{0};
int64_t m_ptsDiff{0};
AP4_UI32 m_streamId;
bool m_eos{false};
bool m_started{false};
std::unique_ptr<CodecHandler> m_codecHandler;
Expand Down
22 changes: 11 additions & 11 deletions src/samplereader/TSSampleReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

#include "AdaptiveByteStream.h"

CTSSampleReader::CTSSampleReader(AP4_ByteStream* input,
INPUTSTREAM_TYPE type,
AP4_UI32 streamId,
uint32_t requiredMask)
: TSReader{input, requiredMask},
m_adByteStream{dynamic_cast<CAdaptiveByteStream*>(input)},
m_typeMask{1U << type}
CTSSampleReader::CTSSampleReader(AP4_ByteStream* input, uint32_t requiredMask)
: TSReader{input, requiredMask}, m_adByteStream{dynamic_cast<CAdaptiveByteStream*>(input)}
{
}

void CTSSampleReader::SetStreamId(INPUTSTREAM_TYPE type, int streamId)
{
m_typeMask = 1U << type;
m_typeMap[INPUTSTREAM_TYPE_NONE] = streamId;
m_typeMap[type] = streamId;
}
Expand All @@ -27,17 +27,17 @@ bool CTSSampleReader::Initialize(SESSION::CStream* stream)
return TSReader::Initialize();
}

void CTSSampleReader::AddStreamType(INPUTSTREAM_TYPE type, uint32_t sid)
void CTSSampleReader::AddStreamType(INPUTSTREAM_TYPE type, int streamId)
{
m_typeMap[type] = sid;
m_typeMap[type] = streamId;
m_typeMask |= (1 << type);
if (m_started)
StartStreaming(m_typeMask);
}

void CTSSampleReader::SetStreamType(INPUTSTREAM_TYPE type, uint32_t sid)
void CTSSampleReader::SetStreamType(INPUTSTREAM_TYPE type, int streamId)
{
m_typeMap[type] = sid;
m_typeMap[type] = streamId;
m_typeMask = (1 << type);
}

Expand Down
18 changes: 9 additions & 9 deletions src/samplereader/TSSampleReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ class CAdaptiveByteStream;
class ATTR_DLL_LOCAL CTSSampleReader : public ISampleReader, public TSReader
{
public:
CTSSampleReader(AP4_ByteStream* input,
INPUTSTREAM_TYPE type,
AP4_UI32 streamId,
uint32_t requiredMask);
CTSSampleReader(AP4_ByteStream* input, uint32_t requiredMask);

bool Initialize(SESSION::CStream* stream) override;
void AddStreamType(INPUTSTREAM_TYPE type, uint32_t sid) override;
void SetStreamType(INPUTSTREAM_TYPE type, uint32_t sid) override;
void AddStreamType(INPUTSTREAM_TYPE type, int streamId) override;
void SetStreamType(INPUTSTREAM_TYPE type, int streamId) override;
bool RemoveStreamType(INPUTSTREAM_TYPE type) override;
bool IsStarted() const override { return m_started; }
bool EOS() const override { return m_eos; }
Expand All @@ -39,15 +36,18 @@ class ATTR_DLL_LOCAL CTSSampleReader : public ISampleReader, public TSReader
void SetPTSOffset(uint64_t offset) override { m_ptsOffs = offset; }
int64_t GetPTSDiff() const override { return m_ptsDiff; }
uint32_t GetTimeScale() const override { return 90000; }
AP4_UI32 GetStreamId() const override { return m_typeMap[GetStreamType()]; }

int GetStreamId() const override { return m_typeMap[GetStreamType()]; }
void SetStreamId(INPUTSTREAM_TYPE type, int streamId) override;

AP4_Size GetSampleDataSize() const override { return GetPacketSize(); }
const AP4_Byte* GetSampleData() const override { return GetPacketData(); }
uint64_t GetDuration() const override { return (TSReader::GetDuration() * 100) / 9; }
bool IsEncrypted() const override { return false; }

private:
uint32_t m_typeMask; //Bit representation of INPUTSTREAM_TYPES
uint32_t m_typeMap[16];
uint32_t m_typeMask{0}; //Bit representation of INPUTSTREAM_TYPES
int m_typeMap[16]{};
uint64_t m_pts{0};
uint64_t m_dts{0};
uint64_t m_ptsOffs{~0ULL};
Expand Down
6 changes: 2 additions & 4 deletions src/samplereader/WebmSampleReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@

#include "AdaptiveByteStream.h"

CWebmSampleReader::CWebmSampleReader(AP4_ByteStream* input, AP4_UI32 streamId)
: WebmReader{input},
m_streamId{streamId},
m_adByteStream{dynamic_cast<CAdaptiveByteStream*>(input)} {};
CWebmSampleReader::CWebmSampleReader(AP4_ByteStream* input)
: WebmReader{input}, m_adByteStream{dynamic_cast<CAdaptiveByteStream*>(input)} {};

bool CWebmSampleReader::Initialize(SESSION::CStream* stream)
{
Expand Down
4 changes: 1 addition & 3 deletions src/samplereader/WebmSampleReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CAdaptiveByteStream;
class ATTR_DLL_LOCAL CWebmSampleReader : public ISampleReader, public WebmReader
{
public:
CWebmSampleReader(AP4_ByteStream* input, AP4_UI32 streamId);
CWebmSampleReader(AP4_ByteStream* input);

bool IsStarted() const override { return m_started; }
bool EOS() const override { return m_eos; }
Expand All @@ -33,14 +33,12 @@ class ATTR_DLL_LOCAL CWebmSampleReader : public ISampleReader, public WebmReader
void SetPTSOffset(uint64_t offset) override { m_ptsOffs = offset; }
int64_t GetPTSDiff() const override { return m_ptsDiff; }
uint32_t GetTimeScale() const override { return 1000; }
AP4_UI32 GetStreamId() const override { return m_streamId; }
AP4_Size GetSampleDataSize() const override { return GetPacketSize(); }
const AP4_Byte* GetSampleData() const override { return GetPacketData(); }
uint64_t GetDuration() const override { return WebmReader::GetDuration() * 1000; }
bool IsEncrypted() const override { return false; }

private:
AP4_UI32 m_streamId;
uint64_t m_pts{0};
uint64_t m_dts{0};
uint64_t m_ptsOffs{~0ULL};
Expand Down

0 comments on commit 9d7d90a

Please sign in to comment.