[PATCH 100/103] qemuBuildSoundCommandLine: Generate codecs via JSON

Peter Krempa pkrempa at redhat.com
Thu Oct 7 15:18:28 UTC 2021


The codec devices have the following properties we control:
  cad=<uint32>           -  (default: 4294967295)
  audiodev=<str>         - ID of an audiodev to use as a backend

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/qemu/qemu_command.c | 64 ++++++++++++++++++-----------------------
 1 file changed, 28 insertions(+), 36 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index cf56ec3a76..d0bf3dbb30 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -4505,30 +4505,36 @@ qemuBuildSoundDevCmd(virCommand *cmd,
 }


-static char *
-qemuBuildSoundCodecStr(const virDomainDef *def,
+static int
+qemuBuildSoundCodecCmd(virCommand *cmd,
+                       const virDomainDef *def,
                        virDomainSoundDef *sound,
                        virDomainSoundCodecDef *codec,
                        virQEMUCaps *qemuCaps)
 {
-    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
-    const char *stype;
-    int type;
-
-    type = codec->type;
-    stype = qemuSoundCodecTypeToString(type);
-
-    virBufferAsprintf(&buf, "%s,id=%s-codec%d,bus=%s.0,cad=%d",
-                      stype, sound->info.alias, codec->cad, sound->info.alias, codec->cad);
+    g_autoptr(virJSONValue) props = NULL;
+    g_autofree char *audioid = NULL;
+    g_autofree char *alias = g_strdup_printf("%s-codec%d", sound->info.alias, codec->cad);
+    g_autofree char *bus = g_strdup_printf("%s.0", sound->info.alias);

     if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_AUDIODEV)) {
-        g_autofree char *audioid = qemuGetAudioIDString(def, sound->audioId);
-        if (!audioid)
-            return NULL;
-        virBufferAsprintf(&buf, ",audiodev=%s", audioid);
+        if (!(audioid = qemuGetAudioIDString(def, sound->audioId)))
+            return -1;
     }

-    return virBufferContentAndReset(&buf);
+    if (virJSONValueObjectCreate(&props,
+                                 "s:driver", qemuSoundCodecTypeToString(codec->type),
+                                 "s:id", alias,
+                                 "s:bus", bus,
+                                 "i:cad", codec->cad,
+                                 "S:audiodev", audioid,
+                                 NULL) < 0)
+        return -1;
+
+    if (qemuBuildDeviceCommandlineFromJSON(cmd, props, qemuCaps) < 0)
+        return -1;
+
+    return 0;
 }


@@ -4556,30 +4562,16 @@ qemuBuildSoundCommandLine(virCommand *cmd,

             if (virDomainSoundModelSupportsCodecs(sound)) {
                 for (j = 0; j < sound->ncodecs; j++) {
-                    g_autofree char *codecstr = NULL;
-                    virCommandAddArg(cmd, "-device");
-                    if (!(codecstr =
-                          qemuBuildSoundCodecStr(def, sound,
-                                                 sound->codecs[j], qemuCaps))) {
+                    if (qemuBuildSoundCodecCmd(cmd, def, sound, sound->codecs[j],
+                                               qemuCaps) < 0)
                         return -1;
-
-                    }
-                    virCommandAddArg(cmd, codecstr);
                 }
+
                 if (j == 0) {
-                    g_autofree char *codecstr = NULL;
-                    virDomainSoundCodecDef codec = {
-                        VIR_DOMAIN_SOUND_CODEC_TYPE_DUPLEX,
-                        0
-                    };
-                    virCommandAddArg(cmd, "-device");
-                    if (!(codecstr =
-                          qemuBuildSoundCodecStr(def, sound,
-                                                 &codec, qemuCaps))) {
-                        return -1;
+                    virDomainSoundCodecDef codec = { VIR_DOMAIN_SOUND_CODEC_TYPE_DUPLEX, 0 };

-                    }
-                    virCommandAddArg(cmd, codecstr);
+                    if (qemuBuildSoundCodecCmd(cmd, def, sound, &codec, qemuCaps) < 0)
+                        return -1;
                 }
             }
         }
-- 
2.31.1




More information about the libvir-list mailing list