How to obtain mp3 audio packets for streaming in C/C++

audio, audio-streaming, c++, mp3, streaming

Solution

An MP3 file is just a succession of MP3 frames. Each frame is made of a header and a data block.

Splitting the MP3 file as MP3 frames will involve parsing the MP3 file. You can refer to this documentation for a good description of the format.

Note that in the case of mpeg layer 3 codec, frames are not independant. In the worst case, 9 input frames may be needed before beeing able to decode one single frame.

What I would do instead of this

I guess you could probably ignore most of these details and focus on the streaming problem itself. Here is what I would try to build first:

on the sender side, split a file into packets, and send them one by one using your system. Command example: `send_stream test.mp3`

on the receiver side, receive the packets and rebuild the original file. Command example: `receive_stream test.mp3`

Once you have this working fine, modify the receiver program so that it writes the packets in-order on the standard output. This will allow you to redirect stdout to a file

# sender side did not change
send_stream test.mp3

# receiver side
receive_stream > test.mp3

Then, you can use `madplay` to play the mp3 while it is received simply by redirecting `receive_stream` output to madplay:

# madplay - tells madplay to read its input from standard input.
receive_stream | madplay -

For a good mp3 decoder, take a look at MAD.

Problem

I want to be able to break a song into packets and have access to these individual packets. The reason for that is that I want to send each individual packet over the network using an experimental network protocol called Named Data Network. As the packets arrive at the destination I want to play them. So I want to implement a streaming functionality. The only difference is the network layer that I will use. This network layer is not based on IP. Does anyone know any C/C++ implementation of breaking a song file into pieces and then playing these packets individually? I looked over Gstreamer, but it seems complicated to get individual packets from its pipeline structure. I found this reference which was the closest to what I wanted, however it was not so clear for me: how can I parse audio raw data recorder with gstreamer? Summarizing the points I need: - Break a song into packets - Play the audio content of a single packet (or a small set of packets). Thank you very much for the help!

Original source

Related problems