Hello everyone!
I am learning sound programming on Linux. In my app I am using ffmpeg to decode the audio, and then ALSA to pass it on to speakers. So far everything is fine - the packets are retrieved, stored in a queue. After that, the packets are read from the queue, decoded and the result passed to ALSA. Now my problem is this - the program plays the audio as fast as it can, and not at the speed it supposed to. I have read somewhere that I need some buffer or something, but I have not found any information about it. I would really appreciate if someone could point me int the right direction. Thanks! Here is the playback loop -
while (1)
{
packet_queue_get(&audioq, &packet, 0);
long lAudioBytesRemaining= packet.size;
uint8_t * pAudioRawData=packet.data;
int bytesDecoded = -1;
int iNumAudioBytes = AVCODEC_MAX_AUDIO_FRAME_SIZE;
long lSize = lAudioBytesRemaining + FF_INPUT_BUFFER_PADDING_SIZE;
uint8_t * pIn = new uint8_t[lSize];
memset(pIn, 0, lSize);
memcpy(pIn, pAudioRawData, lAudioBytesRemaining);
//This function decodes the audio, stores the result in buffer
bytesDecoded = avcodec_decode_audio2(aCodecCtx, buffer,
&iNumAudioBytes,
pIn, lAudioBytesRemaining);
delete pIn;
if(bytesDecoded <= 0)
{
cerr << "* Decoding Error!" << endl;
break;
}
else
{
//This function takes the buffer and passes its content to sound card
alsaPlay((void*)buffer, bytesDecoded);
}
}