From 4914f8e3f58d2f4605c79edb5aab86ee4f1b8994 Mon Sep 17 00:00:00 2001 From: Tuomas Virtanen Date: Thu, 15 Nov 2018 01:36:20 +0200 Subject: Make sure audio is handled properly too :/ --- src/internal/audio/kitaudio.c | 30 +++++++++++++++++++----------- src/internal/kitdecoder.c | 2 +- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/internal/audio/kitaudio.c b/src/internal/audio/kitaudio.c index 4bc284d..33742d0 100644 --- a/src/internal/audio/kitaudio.c +++ b/src/internal/audio/kitaudio.c @@ -182,12 +182,8 @@ static int dec_decode_audio_cb(Kit_Decoder *dec, AVPacket *in_packet) { return 0; } #else -static int dec_decode_audio_cb(Kit_Decoder *dec, AVPacket *in_packet) { - assert(dec != NULL); - assert(in_packet != NULL); - +static void dec_read_audio(Kit_Decoder *dec) { Kit_AudioDecoder *audio_dec = dec->userdata; - int ret; int len; int dst_linesize; int dst_nb_samples; @@ -195,12 +191,7 @@ static int dec_decode_audio_cb(Kit_Decoder *dec, AVPacket *in_packet) { double pts; unsigned char **dst_data; Kit_AudioPacket *out_packet = NULL; - - // Write packet to the decoder for handling. - ret = avcodec_send_packet(dec->codec_ctx, in_packet); - if(ret < 0) { - return 1; - } + int ret = 0; // Pull decoded frames out when ready and if we have room in decoder output buffer while(!ret && Kit_CanWriteDecoderOutput(dec)) { @@ -247,6 +238,23 @@ static int dec_decode_audio_cb(Kit_Decoder *dec, AVPacket *in_packet) { av_freep(&dst_data); } } +} + +static int dec_decode_audio_cb(Kit_Decoder *dec, AVPacket *in_packet) { + assert(dec != NULL); + assert(in_packet != NULL); + + // Try to clear the buffer first. We might have too much content in the ffmpeg buffer, + /// so we want to clear it of outgoing data if we can. + dec_read_audio(dec); + + // Write packet to the decoder for handling. + if(avcodec_send_packet(dec->codec_ctx, in_packet) < 0) { + return 1; + } + + // Some input data was put in succesfully, so try again to get frames. + dec_read_audio(dec); return 0; } #endif diff --git a/src/internal/kitdecoder.c b/src/internal/kitdecoder.c index b1753c1..9b925c7 100644 --- a/src/internal/kitdecoder.c +++ b/src/internal/kitdecoder.c @@ -6,7 +6,7 @@ #include "kitchensink/internal/kitdecoder.h" #include "kitchensink/kiterror.h" -#define BUFFER_IN_SIZE 384 +#define BUFFER_IN_SIZE 256 static void free_in_video_packet_cb(void *packet) { av_packet_free((AVPacket**)&packet); -- cgit v1.2.3