audio/virtio-snd: fix latency calc
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> Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <tencent_66E8C146EA79CD00E966DEDAEF8CACD97D05@qq.com>
Yanfeng Liu committed
Nov 14, 2025 at 17:34 UTC
27b370829294dcc4d1e9052e42bcd2b7cdd5c072
2 files changed
+12
-1
hw/audio/virtio-snd.c
+11
-1
@@ -431,6 +431,7 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)
431
stream->id = stream_id;
432
stream->pcm = s->pcm;
433
stream->s = s;
434
+ stream->latency_bytes = 0;
435
qemu_mutex_init(&stream->queue_mutex);
436
QSIMPLEQ_INIT(&stream->queue);
437
@@ -899,6 +900,7 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq)
900
buffer->vq = vq;
901
buffer->size = size;
902
buffer->offset = 0;
903
+ stream->latency_bytes += size;
904
905
QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry);
906
}
@@ -1112,12 +1114,19 @@ error_cleanup:
1114
virtio_snd_unrealize(dev);
1115
}
1116
1117
+static inline void update_latency(VirtIOSoundPCMStream *s, size_t used)
1118
+{
1119
+ s->latency_bytes = s->latency_bytes > used ?
1120
+ s->latency_bytes - used : 0;
1121
+}
1122
+
1123
static inline void return_tx_buffer(VirtIOSoundPCMStream *stream,
1124
VirtIOSoundPCMBuffer *buffer)
1125
{
1126
virtio_snd_pcm_status resp = { 0 };
1127
resp.status = cpu_to_le32(VIRTIO_SND_S_OK);
1120
- resp.latency_bytes = cpu_to_le32((uint32_t)buffer->size);
1128
+ update_latency(stream, buffer->size);
1129
+ resp.latency_bytes = cpu_to_le32(stream->latency_bytes);
1130
iov_from_buf(buffer->elem->in_sg,
1131
buffer->elem->in_num,
1132
0,
@@ -1178,6 +1187,7 @@ static void virtio_snd_pcm_out_cb(void *data, int available)
1187
buffer->size -= size;
1188
buffer->offset += size;
1189
available -= size;
1190
+ update_latency(stream, size);
1191
if (buffer->size < 1) {
1192
return_tx_buffer(stream, buffer);
1193
break;
include/hw/audio/virtio-snd.h
+1
@@ -150,6 +150,7 @@ struct VirtIOSoundPCMStream {
150
} voice;
151
QemuMutex queue_mutex;
152
bool active;
153
+ uint32_t latency_bytes;
154
QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) queue;
155
};
156