audio: add more audio rate control functions

The next patch needs two new rate control functions. The first
one returns the bytes needed at call time to maintain the
selected rate. The second one adjusts the bytes actually sent.

Split the audio_rate_get_bytes() function into these two
functions and reintroduce audio_rate_get_bytes().

Signed-off-by: Volker Rümelin <vr_qemu@t-online.de>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20220923183640.8314-5-vr_qemu@t-online.de>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
This commit is contained in:
Volker Rümelin 2022-09-23 20:36:33 +02:00 committed by Gerd Hoffmann
parent 5a9d7ae251
commit 02732641c0
2 changed files with 26 additions and 11 deletions

View File

@ -2251,26 +2251,39 @@ void audio_rate_start(RateCtl *rate)
rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
} }
size_t audio_rate_get_bytes(struct audio_pcm_info *info, RateCtl *rate, size_t audio_rate_peek_bytes(RateCtl *rate, struct audio_pcm_info *info)
size_t bytes_avail)
{ {
int64_t now; int64_t now;
int64_t ticks; int64_t ticks;
int64_t bytes; int64_t bytes;
int64_t samples; int64_t frames;
size_t ret;
now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
ticks = now - rate->start_ticks; ticks = now - rate->start_ticks;
bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND); bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND);
samples = (bytes - rate->bytes_sent) / info->bytes_per_frame; frames = (bytes - rate->bytes_sent) / info->bytes_per_frame;
if (samples < 0 || samples > 65536) { if (frames < 0 || frames > 65536) {
AUD_log(NULL, "Resetting rate control (%" PRId64 " samples)\n", samples); AUD_log(NULL, "Resetting rate control (%" PRId64 " frames)\n", frames);
audio_rate_start(rate); audio_rate_start(rate);
samples = 0; frames = 0;
} }
ret = MIN(samples * info->bytes_per_frame, bytes_avail); return frames * info->bytes_per_frame;
rate->bytes_sent += ret; }
return ret;
void audio_rate_add_bytes(RateCtl *rate, size_t bytes_used)
{
rate->bytes_sent += bytes_used;
}
size_t audio_rate_get_bytes(struct audio_pcm_info *info, RateCtl *rate,
size_t bytes_avail)
{
size_t bytes;
bytes = audio_rate_peek_bytes(rate, info);
bytes = MIN(bytes, bytes_avail);
audio_rate_add_bytes(rate, bytes);
return bytes;
} }

View File

@ -263,6 +263,8 @@ typedef struct RateCtl {
} RateCtl; } RateCtl;
void audio_rate_start(RateCtl *rate); void audio_rate_start(RateCtl *rate);
size_t audio_rate_peek_bytes(RateCtl *rate, struct audio_pcm_info *info);
void audio_rate_add_bytes(RateCtl *rate, size_t bytes_used);
size_t audio_rate_get_bytes(struct audio_pcm_info *info, RateCtl *rate, size_t audio_rate_get_bytes(struct audio_pcm_info *info, RateCtl *rate,
size_t bytes_avail); size_t bytes_avail);