encode audio ffmpeg c++ with different number of samples between input and output

I am trying to make an audio encoder to encode a live stream. I get my audio stream from a webrtc source. The properties for the source audio buffer is(AND I CANNOT CHANGE IT):

number of samples: 480 
sample size: 2 bytes 
sample rate: 44100Hz 
number of channels: 1

I am using MP2 codec to encode the audio. It expects an audio number of samples of 1152(CAN'T CHANGE THAT EITHER), which is different from the source(480)

I generate the audio frame using:

frame->nb_samples = 480;
avcodec_fill_audio_frame(frame, nb_channels(=1), sample_fmt(=AVCodecContext sample_fmt), temp_audio_buffer(=source), 480, 0);

And I am getting a "chopped" sound. From what I know, It is because of the difference between number of samples in each frame.

Is there a way to fill the entire frame(1152 samples) somehow? Will I be able to encode this live stream?

thanks

Upvotes: 0

Views: 969

Answers (1)

szatmary
szatmary

Reputation: 31140

Fill a buffer until you have the necessary number of samples then encoded the buffer. Use the remainder to start the next buffer.

Upvotes: 1

Related Questions