Giter VIP home page Giter VIP logo

mpegts's Introduction

MPEG-TS

A simple C++ implementation of a MPEG-TS Muxer and Demuxer.

The multiplexer and demultiplexer is FAR from supporting ITU-T H.222. Only the basics are supported meaning a video stream and a audio stream without using any 'trick mode' flags or extensions. Enough for multiplexing some elementary streams to watch using FFPLAY for example. This code should not be used to do anything advanced as it's not suited for that.

This is a cloned projet. This clone adds support for using the multiplexer / demultiplexer as a library into your projects among other features as can be seen in the examples and unit tests.

The upstream master project also contains a lot of serious bugs fixed in this clone. There might still be bugs in the code please let me know if you find any.

Build status ->

Ubuntu 18.04

Windows

MacOS

Unit tests status ->

Unit tests

Build

Requires cmake version >= 3.10 and C++14

Release:

mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --config Release

Debug:

mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Debug ..
cmake --build . --config Debug

Outputs the mpegts static library and the example executables

Use in your CMake project

It's simple to add the MPEG-TS multiplexer/demultiplexer into your existing CMake project. Just follow the simple steps below.

Add the mpegts library as a external project ->

include(ExternalProject)
ExternalProject_Add(project_mpegts
        GIT_REPOSITORY https://github.com/Unit-X/mpegts.git
        GIT_SUBMODULES ""
        UPDATE_COMMAND git pull
        SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mpegts
        BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mpegts
        GIT_PROGRESS 1
        CONFIGURE_COMMAND cmake -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} ${CMAKE_CURRENT_SOURCE_DIR}/mpegts
        BUILD_COMMAND cmake --build ${CMAKE_CURRENT_SOURCE_DIR}/mpegts --config ${CMAKE_BUILD_TYPE} --target mpegts
        STEP_TARGETS build
        EXCLUDE_FROM_ALL TRUE
        INSTALL_COMMAND ""
        )
add_library(mpegts STATIC IMPORTED)
set_property(TARGET mpegts PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/mpegts/libmpegts.a)

Add header search paths ->

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

Link against the library ->

add_executable((your_executable) (source_files))
add_dependencies((your_executable) project_mpegts)
target_link_libraries((your_executable) mpegts)

That will trigger building and linking the mpegts library (For windows the path and name of the library needs to be changed)

Usage

Demuxer ->

#include "mpegts_demuxer.h"

//Callback where the demuxed data ends up
void dmxOutput(EsFrame *pEs) {
//The EsFrame contains all information about the Elementary data
}


//Create a input buffer
SimpleBuffer lIn;
//Create a demuxer
MpegTsDemuxer lDemuxer;

//Provide a callback for the ES data
lDemuxer.esOutCallback = std::bind(&dmxOutput, std::placeholders::_1);

//Append data to the input buffer 
lIn.append(packet, 188);

//Demux the data
demuxer.decode(&lIn);

//Example usage of the demuxer can be seen here ->
//https://github.com/Unit-X/ts2efp/blob/master/main.cpp

Muxer ->

#include "mpegts_muxer.h"

//AAC audio
#define TYPE_AUDIO 0x0f
//H.264 video
#define TYPE_VIDEO 0x1b

//Audio PID
#define AUDIO_PID 257
//Video PID
#define VIDEO_PID 256
//PMT PID
#define PMT_PID 100

//A callback where all the TS-packets are sent from the multiplexer
void muxOutput(SimpleBuffer &rTsOutBuffer){

}

//Create the map defining what datatype to map to what PID
std::map<uint8_t, int> streamPidMap;
streamPidMap[TYPE_AUDIO] = AUDIO_PID;
streamPidMap[TYPE_VIDEO] = VIDEO_PID;

//Create the multiplexer
//param1 = PID map
//param2 = PMT PID 
//param3 = PCR PID
MpegTsMuxer lMuxer(streamPidMap, PMT_PID, VIDEO_PID);

//Provide the callback where TS packets are fed to
lMuxer.tsOutCallback = std::bind(&muxOutput, std::placeholders::_1);

//Build a frame of data (ES)
EsFrame esFrame;
esFrame.mData = std::make_shared<SimpleBuffer>();
//Append your ES-Data
esFrame.mData->append((const char *) rPacket->pFrameData, rPacket->mFrameSize);
esFrame.mPts = rPacket->mPts;
esFrame.mDts = rPacket->mPts;
esFrame.mPcr = 0;
esFrame.mStreamType = TYPE_AUDIO;
esFrame.mStreamId = 192;
esFrame.mPid = AUDIO_PID;
esFrame.mExpectedPesPacketLength = 0;
esFrame.mCompleted = true;

//Multiplex your data
lMuxer.encode(&esFrame);

Runing the included demux/mux example

(Currently only MacOS and Linux)

The tests has to be run in the order as described below

  • Generate 'bars.ts' file by running the script ./generate_bars.sh . This generates the file to be used by the tests. The build instructions has to be followed also since the tests expect the file to ba available from the location ../bars.ts meaning the executables needs to be a directory level lower IE.. {source_root}/build/{test_file_location}

  • Run the demuxer ./mpeg_ts_dmx_tests . The demuxer is demuxing all ES frames and generates one file per audio/video frame in the directory bars_dmx

  • Run the muxer ./mpeg_ts_mx_tests. The multiplexer is assembling the frames from the demuxed files and generates a TS over UDP output on local host port 8100. In order to view the output you can for example use ffplay udp://127.0.0.1:8100

Example usage of the mux library can alse be seen here -> Example multiplexer/demultiplexer

mpegts's People

Contributors

akanchi avatar andersc avatar ecameron avatar permobergedge avatar sirenoremac avatar tomasoh-edgeware avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mpegts's Issues

demuxer is sensitive to buffer size

i.e. I guess usually one will read chunks from a file in some arbtrary size (4k page size for example) and feed that to the demuxer. but the demuxer rarely checks wether the buffer is exhausted and thus depends on either some magic block size or the whole file being fed at once. this can be easily verified by replacing UnitTest1::muxOutput with

void UnitTest1::muxOutput(SimpleBuffer &rTsOutBuffer) {
    //Double to fail at non integer data
    double packets = (double) rTsOutBuffer.size() / 188.0;
    if (packets != (int) packets) {
        std::cout << "Payload not X * 188 " << std::endl;
        mUnitTestStatus = false;
    }

    uint8_t* lpData = rTsOutBuffer.data();

    for (int lI = 0 ; lI < packets ; lI++) {
        auto p = lpData+(lI*188);
        SimpleBuffer lIn;
        lIn.append(p, 100);
        mDemuxer.decode(lIn);
        lIn.clear();
        lIn.append(p + 100, 88);
        mDemuxer.decode(lIn);
    }
}

which feeds the demuxer the same data, but in smaller chunks, which makes the test fail miserably.

Windows build

While Windows may not be a target, during my attempt to build I've run into two issues:

[ERROR] OUTPUT>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\any(429): error C2338: any_cast<T>(any&&) requires T to be constructible from remove_cv_t<remove_reference_t<T>>
[INFO] OUTPUT>c++/TSHandler.cpp(384): note: see reference to function template instantiation '_Ty &std::any_cast<std::shared_ptr<MpegTsDemuxer>&>(std::any &&)' being compiled
[INFO] OUTPUT>        with
[INFO] OUTPUT>        [
[INFO] OUTPUT>            _Ty=std::shared_ptr<MpegTsDemuxer> &
[INFO] OUTPUT>        ]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.