* [PATCH v1] audio/virtio-snd: fix latency calc
@ 2025-11-14 9:34 Yanfeng Liu
2025-11-15 10:08 ` Manos Pitsidianakis
0 siblings, 1 reply; 3+ messages in thread
From: Yanfeng Liu @ 2025-11-14 9:34 UTC (permalink / raw)
To: kraxel, manos.pitsidianakis, mst; +Cc: qemu-devel, Yanfeng Liu, Yanfeng Liu
Media players needs meaningful latency_bytes update but it is
zero now most of the time. This adds stream-wise latency_bytes
calculation so that to improve the situation.
Signed-off-by: Yanfeng Liu <p-liuyanfeng9@xiaomi.com>
---
hw/audio/virtio-snd.c | 12 +++++++++++-
include/hw/audio/virtio-snd.h | 1 +
2 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c
index 9101560f38..ed0422b45a 100644
--- a/hw/audio/virtio-snd.c
+++ b/hw/audio/virtio-snd.c
@@ -431,6 +431,7 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)
stream->id = stream_id;
stream->pcm = s->pcm;
stream->s = s;
+ stream->latency_bytes = 0;
qemu_mutex_init(&stream->queue_mutex);
QSIMPLEQ_INIT(&stream->queue);
@@ -899,6 +900,7 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq)
buffer->vq = vq;
buffer->size = size;
buffer->offset = 0;
+ stream->latency_bytes += size;
QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry);
}
@@ -1112,12 +1114,19 @@ error_cleanup:
virtio_snd_unrealize(dev);
}
+static inline void update_latency(VirtIOSoundPCMStream *s, size_t used)
+{
+ s->latency_bytes = s->latency_bytes > used ?
+ s->latency_bytes - used : 0;
+}
+
static inline void return_tx_buffer(VirtIOSoundPCMStream *stream,
VirtIOSoundPCMBuffer *buffer)
{
virtio_snd_pcm_status resp = { 0 };
resp.status = cpu_to_le32(VIRTIO_SND_S_OK);
- resp.latency_bytes = cpu_to_le32((uint32_t)buffer->size);
+ update_latency(stream, buffer->size);
+ resp.latency_bytes = cpu_to_le32(stream->latency_bytes);
iov_from_buf(buffer->elem->in_sg,
buffer->elem->in_num,
0,
@@ -1178,6 +1187,7 @@ static void virtio_snd_pcm_out_cb(void *data, int available)
buffer->size -= size;
buffer->offset += size;
available -= size;
+ update_latency(stream, size);
if (buffer->size < 1) {
return_tx_buffer(stream, buffer);
break;
diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h
index c176066584..9560bac8b1 100644
--- a/include/hw/audio/virtio-snd.h
+++ b/include/hw/audio/virtio-snd.h
@@ -150,6 +150,7 @@ struct VirtIOSoundPCMStream {
} voice;
QemuMutex queue_mutex;
bool active;
+ uint32_t latency_bytes;
QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) queue;
};
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v1] audio/virtio-snd: fix latency calc
2025-11-14 9:34 [PATCH v1] audio/virtio-snd: fix latency calc Yanfeng Liu
@ 2025-11-15 10:08 ` Manos Pitsidianakis
2025-11-17 2:16 ` yf
0 siblings, 1 reply; 3+ messages in thread
From: Manos Pitsidianakis @ 2025-11-15 10:08 UTC (permalink / raw)
To: Yanfeng Liu; +Cc: kraxel, mst, qemu-devel, Yanfeng Liu
On Fri, Nov 14, 2025 at 11:35 AM Yanfeng Liu <yfliu2008@qq.com> wrote:
>
> Media players needs meaningful latency_bytes update but it is
> zero now most of the time. This adds stream-wise latency_bytes
> calculation so that to improve the situation.
>
> Signed-off-by: Yanfeng Liu <p-liuyanfeng9@xiaomi.com>
> ---
Thanks for this patch. The virtio spec doesn't adequately explain what
latency_bytes means, could you explain what it does and why it's
needed? And also what behavior in media players this patch fixes?
Thanks again!
> hw/audio/virtio-snd.c | 12 +++++++++++-
> include/hw/audio/virtio-snd.h | 1 +
> 2 files changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c
> index 9101560f38..ed0422b45a 100644
> --- a/hw/audio/virtio-snd.c
> +++ b/hw/audio/virtio-snd.c
> @@ -431,6 +431,7 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)
> stream->id = stream_id;
> stream->pcm = s->pcm;
> stream->s = s;
> + stream->latency_bytes = 0;
> qemu_mutex_init(&stream->queue_mutex);
> QSIMPLEQ_INIT(&stream->queue);
>
> @@ -899,6 +900,7 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq)
> buffer->vq = vq;
> buffer->size = size;
> buffer->offset = 0;
> + stream->latency_bytes += size;
>
> QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry);
> }
> @@ -1112,12 +1114,19 @@ error_cleanup:
> virtio_snd_unrealize(dev);
> }
>
> +static inline void update_latency(VirtIOSoundPCMStream *s, size_t used)
> +{
> + s->latency_bytes = s->latency_bytes > used ?
> + s->latency_bytes - used : 0;
> +}
> +
> static inline void return_tx_buffer(VirtIOSoundPCMStream *stream,
> VirtIOSoundPCMBuffer *buffer)
> {
> virtio_snd_pcm_status resp = { 0 };
> resp.status = cpu_to_le32(VIRTIO_SND_S_OK);
> - resp.latency_bytes = cpu_to_le32((uint32_t)buffer->size);
> + update_latency(stream, buffer->size);
> + resp.latency_bytes = cpu_to_le32(stream->latency_bytes);
> iov_from_buf(buffer->elem->in_sg,
> buffer->elem->in_num,
> 0,
> @@ -1178,6 +1187,7 @@ static void virtio_snd_pcm_out_cb(void *data, int available)
> buffer->size -= size;
> buffer->offset += size;
> available -= size;
> + update_latency(stream, size);
> if (buffer->size < 1) {
> return_tx_buffer(stream, buffer);
> break;
> diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h
> index c176066584..9560bac8b1 100644
> --- a/include/hw/audio/virtio-snd.h
> +++ b/include/hw/audio/virtio-snd.h
> @@ -150,6 +150,7 @@ struct VirtIOSoundPCMStream {
> } voice;
> QemuMutex queue_mutex;
> bool active;
> + uint32_t latency_bytes;
> QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) queue;
> };
>
> --
> 2.34.1
>
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v1] audio/virtio-snd: fix latency calc
2025-11-15 10:08 ` Manos Pitsidianakis
@ 2025-11-17 2:16 ` yf
0 siblings, 0 replies; 3+ messages in thread
From: yf @ 2025-11-17 2:16 UTC (permalink / raw)
To: Manos Pitsidianakis; +Cc: kraxel, mst, qemu-devel, Yanfeng Liu
On Sat, 2025-11-15 at 12:08 +0200, Manos Pitsidianakis wrote:
> On Fri, Nov 14, 2025 at 11:35 AM Yanfeng Liu <yfliu2008@qq.com> wrote:
> >
> > Media players needs meaningful latency_bytes update but it is
> > zero now most of the time. This adds stream-wise latency_bytes
> > calculation so that to improve the situation.
> >
> > Signed-off-by: Yanfeng Liu <p-liuyanfeng9@xiaomi.com>
> > ---
>
> Thanks for this patch. The virtio spec doesn't adequately explain what
> latency_bytes means, could you explain what it does and why it's
Thanks for your attention!
Yes the spec is not clear. I saw issue 198 in github.com/oasis-tcs/virtio-spec
after seeing your question. I think that discussion may clarify this on spec
later.
> needed? And also what behavior in media players this patch fixes?
The field helps guest players to adjust their data feeding operations less
blindly and improve system efficiency, especially when multiple streams are
used. Here we noticed it when came from the goldfish emulator, so we are
wondering if sharing this can be of help.
>
> Thanks again!
>
> > hw/audio/virtio-snd.c | 12 +++++++++++-
> > include/hw/audio/virtio-snd.h | 1 +
> > 2 files changed, 12 insertions(+), 1 deletion(-)
> >
> > diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c
> > index 9101560f38..ed0422b45a 100644
> > --- a/hw/audio/virtio-snd.c
> > +++ b/hw/audio/virtio-snd.c
> > @@ -431,6 +431,7 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s,
> > uint32_t stream_id)
> > stream->id = stream_id;
> > stream->pcm = s->pcm;
> > stream->s = s;
> > + stream->latency_bytes = 0;
> > qemu_mutex_init(&stream->queue_mutex);
> > QSIMPLEQ_INIT(&stream->queue);
> >
> > @@ -899,6 +900,7 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice
> > *vdev, VirtQueue *vq)
> > buffer->vq = vq;
> > buffer->size = size;
> > buffer->offset = 0;
> > + stream->latency_bytes += size;
> >
> > QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry);
> > }
> > @@ -1112,12 +1114,19 @@ error_cleanup:
> > virtio_snd_unrealize(dev);
> > }
> >
> > +static inline void update_latency(VirtIOSoundPCMStream *s, size_t used)
> > +{
> > + s->latency_bytes = s->latency_bytes > used ?
> > + s->latency_bytes - used : 0;
> > +}
> > +
> > static inline void return_tx_buffer(VirtIOSoundPCMStream *stream,
> > VirtIOSoundPCMBuffer *buffer)
> > {
> > virtio_snd_pcm_status resp = { 0 };
> > resp.status = cpu_to_le32(VIRTIO_SND_S_OK);
> > - resp.latency_bytes = cpu_to_le32((uint32_t)buffer->size);
> > + update_latency(stream, buffer->size);
> > + resp.latency_bytes = cpu_to_le32(stream->latency_bytes);
> > iov_from_buf(buffer->elem->in_sg,
> > buffer->elem->in_num,
> > 0,
> > @@ -1178,6 +1187,7 @@ static void virtio_snd_pcm_out_cb(void *data, int
> > available)
> > buffer->size -= size;
> > buffer->offset += size;
> > available -= size;
> > + update_latency(stream, size);
> > if (buffer->size < 1) {
> > return_tx_buffer(stream, buffer);
> > break;
> > diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h
> > index c176066584..9560bac8b1 100644
> > --- a/include/hw/audio/virtio-snd.h
> > +++ b/include/hw/audio/virtio-snd.h
> > @@ -150,6 +150,7 @@ struct VirtIOSoundPCMStream {
> > } voice;
> > QemuMutex queue_mutex;
> > bool active;
> > + uint32_t latency_bytes;
> > QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) queue;
> > };
> >
> > --
> > 2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-11-17 2:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-14 9:34 [PATCH v1] audio/virtio-snd: fix latency calc Yanfeng Liu
2025-11-15 10:08 ` Manos Pitsidianakis
2025-11-17 2:16 ` yf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).