Skip to content

feat[Elementary stream descriptors] add encoding for Elementary stream descriptors #9

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 12 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ find_package (Threads REQUIRED)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

option(ENABLE_TEST_APPS "Should the tests be Built?" ON)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/mpegts/)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/unit_tests/)

file(GLOB libfiles ${CMAKE_CURRENT_SOURCE_DIR}/mpegts/*.cpp)
add_library(mpegts STATIC ${libfiles})

add_executable(mpeg_ts_dmx_tests ${CMAKE_CURRENT_SOURCE_DIR}/main_dmx.cpp)
target_link_libraries(mpeg_ts_dmx_tests mpegts Threads::Threads)
if (ENABLE_TEST_APPS)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/unit_tests/)
add_executable(mpeg_ts_dmx_tests ${CMAKE_CURRENT_SOURCE_DIR}/main_dmx.cpp)
target_link_libraries(mpeg_ts_dmx_tests mpegts Threads::Threads)

add_executable(mpeg_ts_mx_tests ${CMAKE_CURRENT_SOURCE_DIR}/main_mx.cpp)
target_link_libraries(mpeg_ts_mx_tests mpegts Threads::Threads)
add_executable(mpeg_ts_mx_tests ${CMAKE_CURRENT_SOURCE_DIR}/main_mx.cpp)
target_link_libraries(mpeg_ts_mx_tests mpegts Threads::Threads)

file(GLOB unit_tests_sources ${CMAKE_CURRENT_SOURCE_DIR}/unit_tests/*.cpp)
add_executable(mpeg_ts_unit_tests ${unit_tests_sources} ${CMAKE_CURRENT_SOURCE_DIR}/unit_tests.cpp)
target_link_libraries(mpeg_ts_unit_tests mpegts Threads::Threads)
file(GLOB unit_tests_sources ${CMAKE_CURRENT_SOURCE_DIR}/unit_tests/*.cpp)
add_executable(mpeg_ts_unit_tests ${unit_tests_sources} ${CMAKE_CURRENT_SOURCE_DIR}/unit_tests.cpp)
target_link_libraries(mpeg_ts_unit_tests mpegts Threads::Threads)
endif()
8 changes: 4 additions & 4 deletions main_mx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ void fakeVideoEncoder() {
int main(int argc, char *argv[]) {
std::cout << "TS - muxlib test " << std::endl;

std::map<uint8_t, int> gStreamPidMap;
gStreamPidMap[TYPE_AUDIO] = AUDIO_PID;
gStreamPidMap[TYPE_VIDEO] = VIDEO_PID;
gpMuxer = new MpegTsMuxer(gStreamPidMap, PMT_PID, VIDEO_PID, MpegTsMuxer::MuxType::h222Type);
std::vector<std::shared_ptr<PMTElementInfo>> gEsStreamInfo;
gEsStreamInfo.push_back(std::shared_ptr<PMTElementInfo>(new PMTElementInfo(TYPE_AUDIO, AUDIO_PID)));
gEsStreamInfo.push_back(std::shared_ptr<PMTElementInfo>(new PMTElementInfo(TYPE_VIDEO, VIDEO_PID)));
gpMuxer = new MpegTsMuxer(gEsStreamInfo, PMT_PID, VIDEO_PID, MpegTsMuxer::MuxType::h222Type);

gpMuxer->tsOutCallback = std::bind(&muxOutput, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);

Expand Down
14 changes: 6 additions & 8 deletions mpegts/mpegts_muxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ static const uint16_t MPEGTS_NULL_PACKET_PID = 0x1FFF;
static const uint16_t MPEGTS_PAT_PID = 0x0000;
static const uint32_t MPEGTS_PAT_INTERVAL = 20;

MpegTsMuxer::MpegTsMuxer(std::map<uint8_t, int> lStreamPidMap, uint16_t lPmtPid, uint16_t lPcrPid, MuxType lType) {
MpegTsMuxer::MpegTsMuxer(std::vector<std::shared_ptr<PMTElementInfo>> lEsStreamInfo, uint16_t lPmtPid, uint16_t lPcrPid, MuxType lType) {
mPmtPid = lPmtPid;
mStreamPidMap = lStreamPidMap;
mEsStreamInfo = lEsStreamInfo;
mPcrPid = lPcrPid;
mMuxType = lType;
}
Expand Down Expand Up @@ -66,7 +66,7 @@ void MpegTsMuxer::createPat(SimpleBuffer &rSb, uint16_t lPmtPid, uint8_t lCc) {
rSb.append(lPatSb.data(), lPatSb.size());
}

void MpegTsMuxer::createPmt(SimpleBuffer &rSb, std::map<uint8_t, int> lStreamPidMap, uint16_t lPmtPid, uint8_t lCc) {
void MpegTsMuxer::createPmt(SimpleBuffer &rSb, std::vector<std::shared_ptr<PMTElementInfo>> lEsStreamInfo, uint16_t lPmtPid, uint8_t lCc) {
SimpleBuffer lPmtSb;
TsHeader lTsHeader;
lTsHeader.mSyncByte = 0x47;
Expand Down Expand Up @@ -96,9 +96,7 @@ void MpegTsMuxer::createPmt(SimpleBuffer &rSb, std::map<uint8_t, int> lStreamPid
lPmtHeader.mReserved3 = 0xf;
lPmtHeader.mProgramInfoLength = 0;
lPmtHeader.mPcrPid = mPcrPid;
for (auto lIt = lStreamPidMap.begin(); lIt != lStreamPidMap.end(); lIt++) {
lPmtHeader.mInfos.push_back(std::shared_ptr<PMTElementInfo>(new PMTElementInfo(lIt->first, lIt->second)));
}
lPmtHeader.mInfos = lEsStreamInfo;

uint16_t lSectionLength = lPmtHeader.size() - 3 + 4;
lPmtHeader.mSectionLength = lSectionLength & 0x3ff;
Expand Down Expand Up @@ -326,11 +324,11 @@ void MpegTsMuxer::encode(EsFrame &rFrame, uint8_t lTag, bool lRandomAccess) {
if (mMuxType == MpegTsMuxer::MuxType::segmentType && lRandomAccess) {
uint8_t lPatPmtCc = getCc(0);
createPat(lSb, mPmtPid, lPatPmtCc);
createPmt(lSb, mStreamPidMap, mPmtPid, lPatPmtCc);
createPmt(lSb, mEsStreamInfo, mPmtPid, lPatPmtCc);
} else if (mMuxType == MpegTsMuxer::MuxType::h222Type && shouldCreatePat()) { //FIXME h222Type is NOT implemented correct
uint8_t lPatPmtCc = getCc(0);
createPat(lSb, mPmtPid, lPatPmtCc);
createPmt(lSb, mStreamPidMap, mPmtPid, lPatPmtCc);
createPmt(lSb, mEsStreamInfo, mPmtPid, lPatPmtCc);
}

createPes(rFrame, lSb);
Expand Down
6 changes: 3 additions & 3 deletions mpegts/mpegts_muxer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class MpegTsMuxer {
dvbType //Not implemented at all
};

MpegTsMuxer(std::map<uint8_t, int> lStreamPidMap, uint16_t lPmtPid, uint16_t lPcrPid, MuxType lType);
MpegTsMuxer(std::vector<std::shared_ptr<PMTElementInfo>> lEsStreamInfo, uint16_t lPmtPid, uint16_t lPcrPid, MuxType lType);

virtual ~MpegTsMuxer();

void createPat(SimpleBuffer &rSb, uint16_t lPmtPid, uint8_t lCc);

void createPmt(SimpleBuffer &rSb, std::map<uint8_t, int> lStreamPidMap, uint16_t lPmtPid, uint8_t lCc);
void createPmt(SimpleBuffer &rSb, std::vector<std::shared_ptr<PMTElementInfo>> lEsStreamInfo, uint16_t lPmtPid, uint8_t lCc);

void createPes(EsFrame &rFrame, SimpleBuffer &rSb);

Expand All @@ -51,7 +51,7 @@ class MpegTsMuxer {

uint16_t mPmtPid = 0;

std::map<uint8_t, int> mStreamPidMap;
std::vector<std::shared_ptr<PMTElementInfo>> mEsStreamInfo;

uint16_t mPcrPid;

Expand Down
11 changes: 8 additions & 3 deletions mpegts/simple_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ void SimpleBuffer::write8Bytes(int64_t val)
}
}

void SimpleBuffer::writeString(std::string str){
for (char c : str) {
mData.push_back(static_cast<uint8_t>(c));
}
}

void SimpleBuffer::append(const uint8_t* bytes, int size)
{
if (!bytes || size <= 0) {
Expand Down Expand Up @@ -155,10 +161,9 @@ int64_t SimpleBuffer::read8Bytes()
std::string SimpleBuffer::readString(int len)
{
assert(require(len));

std::string val(*(char*)&mData[0] + mPos, len);
std::vector<uint8_t> subData(mData.begin() + mPos, mData.begin() + mPos + len);
std::string val(subData.begin(), subData.end());
mPos += len;

return val;
}

Expand Down
1 change: 1 addition & 0 deletions mpegts/simple_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class SimpleBuffer
void write3Bytes(int32_t val);
void write4Bytes(int32_t val);
void write8Bytes(int64_t val);
void writeString(std::string str);
void append(const uint8_t* bytes, int size);
void prepend(const uint8_t* bytes, int size);

Expand Down
2 changes: 1 addition & 1 deletion mpegts/ts_packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void PMTElementInfo::encode(SimpleBuffer &rSb) {
rSb.write2Bytes(lB3b4);

if (mEsInfoLength > 0) {
// TODO:
rSb.writeString(mEsInfo);
}
}

Expand Down
6 changes: 3 additions & 3 deletions unit_tests/unit_test_1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ bool UnitTest1::runTest() {
mDemuxer.esOutCallback = std::bind(&UnitTest1::dmxOutput, this, std::placeholders::_1);

uint8_t testVector[TEST_VECTOR_SIZE];
std::map<uint8_t, int> gStreamPidMap;
gStreamPidMap[TYPE_VIDEO] = VIDEO_PID;
MpegTsMuxer lMuxer(gStreamPidMap, PMT_PID, VIDEO_PID, MpegTsMuxer::MuxType::h222Type);
std::vector<std::shared_ptr<PMTElementInfo>> gEsStreamInfo;
gEsStreamInfo.push_back(std::shared_ptr<PMTElementInfo>(new PMTElementInfo(TYPE_VIDEO, VIDEO_PID)));
MpegTsMuxer lMuxer(gEsStreamInfo, PMT_PID, VIDEO_PID, MpegTsMuxer::MuxType::h222Type);
lMuxer.tsOutCallback = std::bind(&UnitTest1::muxOutput, this, std::placeholders::_1);

//Make Vector
Expand Down
6 changes: 3 additions & 3 deletions unit_tests/unit_test_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ bool UnitTest2::runTest() {
mDemuxer.esOutCallback = std::bind(&UnitTest2::dmxOutput, this, std::placeholders::_1);

uint8_t testVector[TEST_VECTOR_SIZE];
std::map<uint8_t, int> gStreamPidMap;
gStreamPidMap[TYPE_VIDEO] = VIDEO_PID;
MpegTsMuxer lMuxer(gStreamPidMap, PMT_PID, VIDEO_PID, MpegTsMuxer::MuxType::h222Type);
std::vector<std::shared_ptr<PMTElementInfo>> gEsStreamInfo;
gEsStreamInfo.push_back(std::shared_ptr<PMTElementInfo>(new PMTElementInfo(TYPE_VIDEO, VIDEO_PID)));
MpegTsMuxer lMuxer(gEsStreamInfo, PMT_PID, VIDEO_PID, MpegTsMuxer::MuxType::h222Type);
lMuxer.tsOutCallback = std::bind(&UnitTest2::muxOutput, this, std::placeholders::_1);

//Make Vector
Expand Down
6 changes: 3 additions & 3 deletions unit_tests/unit_test_3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ bool UnitTest3::runTest() {
mDemuxer.esOutCallback = std::bind(&UnitTest3::dmxOutput, this, std::placeholders::_1);

uint8_t testVector[TEST_VECTOR_SIZE];
std::map<uint8_t, int> gStreamPidMap;
gStreamPidMap[TYPE_VIDEO] = VIDEO_PID;
MpegTsMuxer lMuxer(gStreamPidMap, PMT_PID, PCR_PID, MpegTsMuxer::MuxType::h222Type);
std::vector<std::shared_ptr<PMTElementInfo>> gEsStreamInfo;
gEsStreamInfo.push_back(std::shared_ptr<PMTElementInfo>(new PMTElementInfo(TYPE_VIDEO, VIDEO_PID)));
MpegTsMuxer lMuxer(gEsStreamInfo, PMT_PID, PCR_PID, MpegTsMuxer::MuxType::h222Type);
lMuxer.tsOutCallback = std::bind(&UnitTest3::muxOutput, this, std::placeholders::_1);

//Make Vector
Expand Down
6 changes: 3 additions & 3 deletions unit_tests/unit_test_4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ bool UnitTest4::runTest() {
mDemuxer.esOutCallback = std::bind(&UnitTest4::dmxOutput, this, std::placeholders::_1);

uint8_t testVector[TEST_VECTOR_SIZE];
std::map<uint8_t, int> gStreamPidMap;
gStreamPidMap[TYPE_VIDEO] = VIDEO_PID;
MpegTsMuxer lMuxer(gStreamPidMap, PMT_PID, PCR_PID, MpegTsMuxer::MuxType::h222Type);
std::vector<std::shared_ptr<PMTElementInfo>> gEsStreamInfo;
gEsStreamInfo.push_back(std::shared_ptr<PMTElementInfo>(new PMTElementInfo(TYPE_VIDEO, VIDEO_PID)));
MpegTsMuxer lMuxer(gEsStreamInfo, PMT_PID, PCR_PID, MpegTsMuxer::MuxType::h222Type);
lMuxer.tsOutCallback = std::bind(&UnitTest4::muxOutput, this, std::placeholders::_1);

//Make Vector
Expand Down
9 changes: 5 additions & 4 deletions unit_tests/unit_test_6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ bool UnitTest6::runTest() {
mDemuxer.esOutCallback = std::bind(&UnitTest6::dmxOutputPts, this, std::placeholders::_1);

uint8_t testVector[TEST_VECTOR_SIZE];
std::map<uint8_t, int> gStreamPidMap;
gStreamPidMap[TYPE_VIDEO] = VIDEO_PID;
mpMuxer = new MpegTsMuxer(gStreamPidMap, PMT_PID, PCR_PID, MpegTsMuxer::MuxType::h222Type);
std::vector<std::shared_ptr<PMTElementInfo>> gEsStreamInfo;
gEsStreamInfo.push_back(std::shared_ptr<PMTElementInfo>(new PMTElementInfo(TYPE_VIDEO, VIDEO_PID)));
mpMuxer = new MpegTsMuxer(gEsStreamInfo, PMT_PID, PCR_PID, MpegTsMuxer::MuxType::h222Type);
mpMuxer->tsOutCallback = std::bind(&UnitTest6::muxOutput, this, std::placeholders::_1);

//Make Vector
Expand Down Expand Up @@ -199,9 +199,10 @@ bool UnitTest6::runTest() {
//So we delete the old muxer and create a new one using the video pid as PCR pid
//That means that the PCR will be taken from the Elementary stream data.
delete mpMuxer;
mpMuxer = new MpegTsMuxer(gStreamPidMap, PMT_PID, VIDEO_PID, MpegTsMuxer::MuxType::h222Type);
mpMuxer = new MpegTsMuxer(gEsStreamInfo, PMT_PID, VIDEO_PID, MpegTsMuxer::MuxType::h222Type);
mpMuxer->tsOutCallback = std::bind(&UnitTest6::muxOutput, this, std::placeholders::_1);


//Send frames containing PTS / DTS then also send in band PCR
for (int x = 1; x < NUM_FRAMES_LOOP; x++) {
EsFrame lEsFrame;
Expand Down