How to configure output socket for AAC / ADTS encoding in C++#

Here is a quick snippet for configuring the output socket of a Transcoder for Advanced Audio Coding (AAC) / Audio Data Transport Stream (ADTS) encoding using C++.

Diagram#

The Transcoder with an input MediaSocket and an output MediaSocket. The input MediaSocket only has a file. The output MediaSocket has a Pins connector that a MediaPin plugs into; the MediaPin holds an AudioStreamInfo with the StreamType and StreamSubType of the AAC / ADTS stream. The Transcoder with an input MediaSocket and an output MediaSocket. The input MediaSocket only has a file. The output MediaSocket has a Pins connector that a MediaPin plugs into; the MediaPin holds an AudioStreamInfo with the StreamType and StreamSubType of the AAC / ADTS stream.

Code#

This is the code for the output socket:

primo::ref<MediaSocket> createOutputSocket(Options& opt)
{
    // create stream info to describe the output audio stream
    auto asi = primo::make_ref(Library::createAudioStreamInfo());
    asi->setStreamType(StreamType::AAC);
    asi->setStreamSubType(StreamSubType::AAC_ADTS);

    // You can change the sampling rate and the number of the channels
    // asi->setSampleRate(44100);
    // asi->setChannels(1);

    // create a pin using the stream info 
    auto pin = primo::make_ref(Library::createMediaPin());
    pin->setStreamInfo(asi.get());

    // finally create a socket for the output container format 
    // which in this case is AAC packaged as Audio Data Transport Stream (ADTS)
    auto socket = primo::make_ref(Library::createMediaSocket());
    socket->setStreamType(StreamType::AAC);
    socket->setStreamSubType(StreamSubType::AAC_ADTS);

    socket->pins()->add(pin.get());

    // output to a file
    auto output_file = primo::ustring(opt.outputFile);
    socket->setFile(output_file);
    
    return socket;
}

The complete program is available on GitHub:

Comments

comments powered by Disqus