Skip to content

Commit

Permalink
Keep track of backend audio buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
katajakasa committed Nov 11, 2023
1 parent ee085e2 commit ba7ef00
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/example_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ int main(int argc, char *argv[]) {
// Refresh audio
int queued = SDL_GetQueuedAudioSize(audio_dev);
if(queued < AUDIO_BUFFER_SIZE) {
ret = Kit_GetPlayerAudioData(player, (unsigned char*)audio_buf, AUDIO_BUFFER_SIZE - queued);
ret = Kit_GetPlayerAudioData(player, queued, (unsigned char*)audio_buf, AUDIO_BUFFER_SIZE - queued);
if(ret > 0) {
SDL_QueueAudio(audio_dev, audio_buf, ret);
}
Expand Down
1 change: 1 addition & 0 deletions examples/example_complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ int main(int argc, char *argv[]) {
while(need > 0) {
ret = Kit_GetPlayerAudioData(
player,
queued,
(unsigned char*)audio_buf,
AUDIO_BUFFER_SIZE);
need -= ret;
Expand Down
1 change: 1 addition & 0 deletions examples/example_custom.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ int main(int argc, char *argv[]) {
while(need > 0) {
ret = Kit_GetPlayerAudioData(
player,
queued,
(unsigned char*)audio_buf,
AUDIO_BUFFER_SIZE);
need -= ret;
Expand Down
1 change: 1 addition & 0 deletions examples/example_rwops.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ int main(int argc, char *argv[]) {
while(need > 0) {
ret = Kit_GetPlayerAudioData(
player,
queued,
(unsigned char*)audio_buf,
AUDIO_BUFFER_SIZE);
need -= ret;
Expand Down
1 change: 1 addition & 0 deletions examples/example_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ int main(int argc, char *argv[]) {
while(need > 0) {
ret = Kit_GetPlayerAudioData(
player,
queued,
(unsigned char*)audio_buf,
AUDIO_BUFFER_SIZE);
need -= ret;
Expand Down
2 changes: 1 addition & 1 deletion include/kitchensink/internal/audio/kitaudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "kitchensink/kitsource.h"

KIT_LOCAL Kit_Decoder* Kit_CreateAudioDecoder(const Kit_Source *src, Kit_Timer *sync_timer, int stream_index);
KIT_LOCAL int Kit_GetAudioDecoderData(Kit_Decoder *dec, unsigned char *buf, size_t len);
KIT_LOCAL int Kit_GetAudioDecoderData(Kit_Decoder *dec, size_t backend_buffer_size, unsigned char *buf, size_t len);
KIT_LOCAL int Kit_GetAudioDecoderOutputFormat(const Kit_Decoder *dec, Kit_AudioOutputFormat *output);

#endif // KITAUDIO_H
2 changes: 2 additions & 0 deletions include/kitchensink/internal/utils/kithelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
KIT_LOCAL double Kit_GetSystemTime();
KIT_LOCAL bool Kit_StreamIsFontAttachment(const AVStream *stream);

KIT_LOCAL int min2(int a, int b);

#endif // KITHELPERS_H
3 changes: 2 additions & 1 deletion include/kitchensink/kitplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,12 @@ KIT_API int Kit_GetPlayerSubtitleData(Kit_Player *player,
* This function will do nothing if player playback has not been started.
*
* @param player Player instance
* @param backend_buffer_size Amount of data currently queued to the driver/hw device.
* @param buffer Buffer to read into
* @param length Maximum length of the buffer
* @return Amount of data that was read, <0 on error.
*/
KIT_API int Kit_GetPlayerAudioData(Kit_Player *player, unsigned char *buffer, int length);
KIT_API int Kit_GetPlayerAudioData(Kit_Player *player, size_t backend_buffer_size, unsigned char *buffer, size_t length);

/**
* @brief Fetches information about the currently selected streams
Expand Down
22 changes: 18 additions & 4 deletions src/internal/audio/kitaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ static double Kit_GetCurrentPTS(const Kit_Decoder *decoder) {
return audio_decoder->current->best_effort_timestamp * av_q2d(decoder->stream->time_base);
}

int Kit_GetAudioDecoderData(Kit_Decoder *decoder, unsigned char *buf, size_t len) {
int Kit_GetAudioDecoderData(Kit_Decoder *decoder, size_t backend_buffer_size, unsigned char *buf, size_t len) {
assert(decoder != NULL);

Kit_AudioDecoder *audio_decoder = decoder->userdata;
Expand All @@ -240,7 +240,7 @@ int Kit_GetAudioDecoderData(Kit_Decoder *decoder, unsigned char *buf, size_t len
if(len <= 0)
return 0;
if(!Kit_BeginPacketBufferRead(audio_decoder->buffer, audio_decoder->current, 0))
return 0;
goto no_data;

// If packet should not yet be played, stop here and wait.
// If packet should have already been played, skip it and try to find a better packet.
Expand All @@ -253,7 +253,7 @@ int Kit_GetAudioDecoderData(Kit_Decoder *decoder, unsigned char *buf, size_t len
av_frame_unref(audio_decoder->current);
Kit_FinishPacketBufferRead(audio_decoder->buffer);
if(!Kit_BeginPacketBufferRead(audio_decoder->buffer, audio_decoder->current, 0))
return 0;
goto no_data;
decoder->clock_pos = Kit_GetCurrentPTS(decoder);
}

Expand All @@ -271,7 +271,7 @@ int Kit_GetAudioDecoderData(Kit_Decoder *decoder, unsigned char *buf, size_t len
av_frame_unref(audio_decoder->current);
Kit_FinishPacketBufferRead(audio_decoder->buffer);
if(!Kit_BeginPacketBufferRead(audio_decoder->buffer, audio_decoder->current, 0))
return 0;
goto no_data;
decoder->clock_pos = Kit_GetCurrentPTS(decoder);
}
//LOG("[AUDIO] >>> SYNC!: pts = %lf, sync = %lf\n", decoder->clock_pos, sync_ts);
Expand All @@ -293,4 +293,18 @@ int Kit_GetAudioDecoderData(Kit_Decoder *decoder, unsigned char *buf, size_t len
Kit_FinishPacketBufferRead(audio_decoder->buffer);
}
return ret;

no_data:
if(backend_buffer_size < 8192) {
len = min2(floor(len / SAMPLE_BYTES(audio_decoder)), 1024);
av_samples_set_silence(
&buf,
0,
len,
audio_decoder->output.channels,
Kit_FindAVSampleFormat(audio_decoder->output.format)
);
return len * SAMPLE_BYTES(audio_decoder);
}
return 0;
}
7 changes: 5 additions & 2 deletions src/internal/kitdecoderthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


static void Kit_ProcessPacket(Kit_DecoderThread *thread, bool *pts_jumped) {
if(!Kit_BeginPacketBufferRead(thread->input, thread->scratch_packet, 10))
if(!Kit_BeginPacketBufferRead(thread->input, thread->scratch_packet, 100))
return;

// If a valid packet was found, first check if it's a control packet. Value 1 means seek.
Expand Down Expand Up @@ -52,7 +52,10 @@ static int Kit_DecodeMain(void *ptr) {
// since a single data packet might contain multiple frames.
while(SDL_AtomicGet(&thread->run) && Kit_RunDecoder(thread->decoder, &pts)) {
if(pts_jumped) {
Kit_AdjustTimerBase(thread->decoder->sync_timer, pts);
// Note that we change the sync a bit to give decoders some time to decode.
// The 0.1 is essentially a hack that moves the sync time forwards a bit, so that the data getter
// functions wait a little bit before they start feeding again.
Kit_AdjustTimerBase(thread->decoder->sync_timer, pts - 0.1);
pts_jumped = false;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/internal/kitpacketbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,9 @@ void Kit_FinishPacketBufferRead(Kit_PacketBuffer *buffer) {
advance_read(buffer);
SDL_UnlockMutex(buffer->mutex);
SDL_CondSignal(buffer->can_write);
return;
}

void Kit_CancelPacketBufferRead(Kit_PacketBuffer *buffer) {
assert(buffer);
SDL_UnlockMutex(buffer->mutex);
return;
}
4 changes: 4 additions & 0 deletions src/internal/utils/kithelpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ bool Kit_StreamIsFontAttachment(const AVStream *stream) {
}
return false;
}

int min2(int a, int b) {
return a < b ? a : b;
}
2 changes: 1 addition & 1 deletion src/internal/video/kitvideo.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "kitchensink/internal/video/kitvideo.h"

#define KIT_VIDEO_EARLY_FAIL 1.0
#define KIT_VIDEO_EARLY_THRESHOLD 0.01
#define KIT_VIDEO_EARLY_THRESHOLD 0.005
#define KIT_VIDEO_LATE_THRESHOLD 0.05

typedef struct Kit_VideoDecoder {
Expand Down
4 changes: 2 additions & 2 deletions src/kitplayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ int Kit_GetPlayerVideoData(Kit_Player *player, SDL_Texture *texture, SDL_Rect *a
return Kit_GetVideoDecoderData(player->decoders[KIT_VIDEO_INDEX], texture, area);
}

int Kit_GetPlayerAudioData(Kit_Player *player, unsigned char *buffer, int length) {
int Kit_GetPlayerAudioData(Kit_Player *player, size_t backend_buffer_size, unsigned char *buffer, size_t length) {
assert(player != NULL);
assert(buffer != NULL);
if(player->decoders[KIT_AUDIO_INDEX] == NULL)
Expand All @@ -238,7 +238,7 @@ int Kit_GetPlayerAudioData(Kit_Player *player, unsigned char *buffer, int length
return 0;
if(player->state == KIT_PAUSED || player->state == KIT_STOPPED)
return 0;
return Kit_GetAudioDecoderData(player->decoders[KIT_AUDIO_INDEX], buffer, length);
return Kit_GetAudioDecoderData(player->decoders[KIT_AUDIO_INDEX], backend_buffer_size, buffer, length);
}

int Kit_GetPlayerSubtitleData(Kit_Player *player, SDL_Texture *texture, SDL_Rect *sources, SDL_Rect *targets, int limit) {
Expand Down

0 comments on commit ba7ef00

Please sign in to comment.