[PATCH 3/3] virtio-snd: introduce VirtIOPcmParams

Roman Kiryanov posted 3 patches 4 weeks ago
Maintainers: Gerd Hoffmann <kraxel@redhat.com>, Manos Pitsidianakis <manos.pitsidianakis@linaro.org>, "Michael S. Tsirkin" <mst@redhat.com>
[PATCH 3/3] virtio-snd: introduce VirtIOPcmParams
Posted by Roman Kiryanov 4 weeks ago
Introduce the `VirtIOPcmParams` struct to represent
the payload of `virtio_snd_pcm_set_params` without
the VirtIO transport-specific header fields.

By decoupling the internal PCM state from the
transport layout, `VirtIOPcmParams` simplifies the
snapshot and migration handling code and slightly
reduces the allocation footprint per stream.

Signed-off-by: Roman Kiryanov <rkir@google.com>
---
 hw/audio/virtio-snd.c         | 12 ++++++------
 include/hw/audio/virtio-snd.h | 13 ++++++++++++-
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c
index 6d820b56a6..8899eb7d5e 100644
--- a/hw/audio/virtio-snd.c
+++ b/hw/audio/virtio-snd.c
@@ -139,8 +139,8 @@ static VirtIOSoundPCMStream *virtio_snd_pcm_get_stream(VirtIOSound *s,
  * @s: VirtIOSound device
  * @stream_id: stream id
  */
-static virtio_snd_pcm_set_params *virtio_snd_pcm_get_params(VirtIOSound *s,
-                                                            uint32_t stream_id)
+static VirtIOPcmParams *virtio_snd_pcm_get_params(VirtIOSound *s,
+                                                  uint32_t stream_id)
 {
     return stream_id >= s->snd_conf.streams ? NULL
         : &s->pcm.pcm_params[stream_id];
@@ -258,7 +258,7 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s,
                                    uint32_t stream_id,
                                    virtio_snd_pcm_set_params *params)
 {
-    virtio_snd_pcm_set_params *st_params;
+    VirtIOPcmParams *st_params;
 
     if (stream_id >= s->snd_conf.streams || s->pcm.pcm_params == NULL) {
         virtio_error(VIRTIO_DEVICE(s), "Streams have not been initialized.\n");
@@ -384,7 +384,7 @@ static uint32_t virtio_snd_get_qemu_freq(uint32_t rate)
  * params.
  */
 static void virtio_snd_get_qemu_audsettings(audsettings *as,
-                                            virtio_snd_pcm_set_params *params)
+                                            VirtIOPcmParams *params)
 {
     as->nchannels = MIN(AUDIO_MAX_CHANNELS, params->channels);
     as->fmt = virtio_snd_get_qemu_format(params->format);
@@ -421,7 +421,7 @@ static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
 static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)
 {
     audsettings as;
-    virtio_snd_pcm_set_params *params;
+    VirtIOPcmParams *params;
     VirtIOSoundPCMStream *stream;
 
     if (s->pcm.streams == NULL ||
@@ -1063,7 +1063,7 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)
     vsnd->pcm.streams =
         g_new0(VirtIOSoundPCMStream *, vsnd->snd_conf.streams);
     vsnd->pcm.pcm_params =
-        g_new0(virtio_snd_pcm_set_params, vsnd->snd_conf.streams);
+        g_new0(VirtIOPcmParams, vsnd->snd_conf.streams);
 
     virtio_init(vdev, VIRTIO_ID_SOUND, sizeof(virtio_snd_config));
     virtio_add_feature(&vsnd->features, VIRTIO_F_VERSION_1);
diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h
index a767f68301..e9fa9c620d 100644
--- a/include/hw/audio/virtio-snd.h
+++ b/include/hw/audio/virtio-snd.h
@@ -71,6 +71,8 @@ typedef struct virtio_snd_pcm_status virtio_snd_pcm_status;
 
 typedef struct VirtIOSound VirtIOSound;
 
+typedef struct VirtIOPcmParams VirtIOPcmParams;
+
 typedef struct VirtIOSoundPCMStream VirtIOSoundPCMStream;
 
 typedef struct virtio_snd_ctrl_command virtio_snd_ctrl_command;
@@ -121,6 +123,15 @@ struct VirtIOSoundPCMBuffer {
     uint8_t data[];
 };
 
+struct VirtIOPcmParams {
+    uint32_t buffer_bytes;
+    uint32_t period_bytes;
+    uint32_t features;
+    uint8_t channels;
+    uint8_t format;
+    uint8_t rate;
+};
+
 struct VirtIOSoundPCM {
     /*
      * PCM parameters are a separate field instead of a VirtIOSoundPCMStream
@@ -129,7 +140,7 @@ struct VirtIOSoundPCM {
      * means that some times we get parameters without having an allocated
      * stream yet.
      */
-    virtio_snd_pcm_set_params *pcm_params;
+    VirtIOPcmParams *pcm_params;
     VirtIOSoundPCMStream **streams;
 };
 
-- 
2.53.0.473.g4a7958ca14-goog