diff options
author | Kővágó, Zoltán <dirty.ice.hu@gmail.com> | 2019-09-19 23:24:21 +0200 |
---|---|---|
committer | Gerd Hoffmann <kraxel@redhat.com> | 2019-09-23 12:28:47 +0200 |
commit | 857271a29c2c0e5deb05deb540a2580d1d408b34 (patch) | |
tree | 4fc6973c058119db43306e2fbedecc007396aa12 /audio/audio.c | |
parent | dc88e38fa7f7480becf34712eae41380b62aacc6 (diff) | |
download | qemu-857271a29c2c0e5deb05deb540a2580d1d408b34.zip qemu-857271a29c2c0e5deb05deb540a2580d1d408b34.tar.gz qemu-857271a29c2c0e5deb05deb540a2580d1d408b34.tar.bz2 |
audio: common rate control code for timer based outputs
This commit removes the ad-hoc rate-limiting code from noaudio and
wavaudio, and replaces them with a (slightly modified) code from
spiceaudio. This way multiple write calls (for example when the
circular buffer wraps around) do not cause problems.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: fd0fe5b95b13fa26d09ae77a72f99d0ea411de14.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Diffstat (limited to 'audio/audio.c')
-rw-r--r-- | audio/audio.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/audio/audio.c b/audio/audio.c index ba07fb7..fab1e35 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -2051,3 +2051,33 @@ const char *audio_get_id(QEMUSoundCard *card) return ""; } } + +void audio_rate_start(RateCtl *rate) +{ + memset(rate, 0, sizeof(RateCtl)); + 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 bytes_avail) +{ + int64_t now; + int64_t ticks; + int64_t bytes; + int64_t samples; + size_t ret; + + now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + ticks = now - rate->start_ticks; + bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND); + samples = (bytes - rate->bytes_sent) >> info->shift; + if (samples < 0 || samples > 65536) { + AUD_log(NULL, "Resetting rate control (%" PRId64 " samples)\n", samples); + audio_rate_start(rate); + samples = 0; + } + + ret = MIN(samples << info->shift, bytes_avail); + rate->bytes_sent += ret; + return ret; +} |