[PATCH 32/37] audio: split AudioBackend

marcandre.lureau@redhat.com posted 37 patches 2 days, 2 hours ago
Maintainers: Gerd Hoffmann <kraxel@redhat.com>, Christian Schoenebeck <qemu_oss@crudebyte.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>, Thomas Huth <huth@tuxfamily.org>, Alexandre Ratchov <alex@caoua.org>, Laurent Vivier <laurent@vivier.eu>, Manos Pitsidianakis <manos.pitsidianakis@linaro.org>, "Michael S. Tsirkin" <mst@redhat.com>, Alistair Francis <alistair@alistair23.me>, "Edgar E. Iglesias" <edgar.iglesias@gmail.com>, Peter Maydell <peter.maydell@linaro.org>, Paolo Bonzini <pbonzini@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, "Daniel P. Berrangé" <berrange@redhat.com>, Eduardo Habkost <eduardo@habkost.net>, John Snow <jsnow@redhat.com>, Cleber Rosa <crosa@redhat.com>
[PATCH 32/37] audio: split AudioBackend
Posted by marcandre.lureau@redhat.com 2 days, 2 hours ago
From: Marc-André Lureau <marcandre.lureau@redhat.com>

Code clean-up, to allow building bare abstract class separately.

The original file is MIT-licensed.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 include/qemu/audio.h |   2 +
 audio/audio-be.c     | 210 ++++++++++++++++++++++++++++++++++++++++++
 audio/audio.c        | 211 +------------------------------------------
 audio/meson.build    |   1 +
 4 files changed, 215 insertions(+), 209 deletions(-)
 create mode 100644 audio/audio-be.c

diff --git a/include/qemu/audio.h b/include/qemu/audio.h
index 4fae48886ba..9f85c6316a8 100644
--- a/include/qemu/audio.h
+++ b/include/qemu/audio.h
@@ -111,6 +111,8 @@ typedef struct AudioBackendClass {
 
 bool AUD_backend_check(AudioBackend **be, Error **errp);
 
+AudioBackend *audio_be_new(Audiodev *dev, Error **errp);
+
 SWVoiceOut *AUD_open_out(
     AudioBackend *be,
     SWVoiceOut *sw,
diff --git a/audio/audio-be.c b/audio/audio-be.c
new file mode 100644
index 00000000000..8154727dbfb
--- /dev/null
+++ b/audio/audio-be.c
@@ -0,0 +1,210 @@
+/* SPDX-License-Identifier: MIT */
+
+#include "qemu/osdep.h"
+#include "qemu/audio.h"
+#include "qemu/audio-capture.h"
+#include "qapi/error.h"
+
+bool AUD_backend_check(AudioBackend **be, Error **errp)
+{
+    assert(be != NULL);
+
+    if (!*be) {
+        *be = audio_get_default_audio_be(errp);
+        if (!*be) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+SWVoiceIn *AUD_open_in(
+    AudioBackend *be,
+    SWVoiceIn *sw,
+    const char *name,
+    void *callback_opaque,
+    audio_callback_fn callback_fn,
+    const struct audsettings *as)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    return klass->open_in(be, sw, name, callback_opaque, callback_fn, as);
+}
+
+SWVoiceOut *AUD_open_out(
+    AudioBackend *be,
+    SWVoiceOut *sw,
+    const char *name,
+    void *callback_opaque,
+    audio_callback_fn callback_fn,
+    const struct audsettings *as)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    return klass->open_out(be, sw, name, callback_opaque, callback_fn, as);
+}
+
+void AUD_close_out(AudioBackend *be, SWVoiceOut *sw)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    return klass->close_out(be, sw);
+}
+
+void AUD_close_in(AudioBackend *be, SWVoiceIn *sw)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    return klass->close_in(be, sw);
+}
+
+bool AUD_is_active_out(AudioBackend *be, SWVoiceOut *sw)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    return klass->is_active_out(be, sw);
+}
+
+bool AUD_is_active_in(AudioBackend *be, SWVoiceIn *sw)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    return klass->is_active_in(be, sw);
+}
+
+size_t AUD_write(AudioBackend *be, SWVoiceOut *sw, void *buf, size_t size)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    return klass->write(be, sw, buf, size);
+}
+
+size_t AUD_read(AudioBackend *be, SWVoiceIn *sw, void *buf, size_t size)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    return klass->read(be, sw, buf, size);
+}
+
+int AUD_get_buffer_size_out(AudioBackend *be, SWVoiceOut *sw)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    return klass->get_buffer_size_out(be, sw);
+}
+
+void AUD_set_active_out(AudioBackend *be, SWVoiceOut *sw, bool on)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    return klass->set_active_out(be, sw, on);
+}
+
+void AUD_set_active_in(AudioBackend *be, SWVoiceIn *sw, bool on)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    return klass->set_active_in(be, sw, on);
+}
+
+void AUD_set_volume_out(AudioBackend *be, SWVoiceOut *sw, Volume *vol)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    klass->set_volume_out(be, sw, vol);
+}
+
+void AUD_set_volume_in(AudioBackend *be, SWVoiceIn *sw, Volume *vol)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    klass->set_volume_in(be, sw, vol);
+}
+
+CaptureVoiceOut *AUD_add_capture(
+    AudioBackend *be,
+    struct audsettings *as,
+    struct audio_capture_ops *ops,
+    void *cb_opaque)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    return klass->add_capture(be, as, ops, cb_opaque);
+}
+
+void AUD_del_capture(AudioBackend *be, CaptureVoiceOut *cap, void *cb_opaque)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    klass->del_capture(be, cap, cb_opaque);
+}
+
+#ifdef CONFIG_GIO
+bool audio_be_can_set_dbus_server(AudioBackend *be)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    return klass->set_dbus_server != NULL;
+}
+
+bool audio_be_set_dbus_server(AudioBackend *be,
+                              GDBusObjectManagerServer *server,
+                              bool p2p,
+                              Error **errp)
+{
+    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
+
+    if (!audio_be_can_set_dbus_server(be)) {
+        error_setg(errp, "Audiodev '%s' is not compatible with DBus",
+                   audio_be_get_id(be));
+        return false;
+    }
+
+    return klass->set_dbus_server(be, server, p2p, errp);
+}
+#endif
+
+const char *audio_be_get_id(AudioBackend *be)
+{
+    if (be) {
+        return AUDIO_BACKEND_GET_CLASS(be)->get_id(be);
+    } else {
+        return "";
+    }
+}
+
+AudioBackend *audio_be_new(Audiodev *dev, Error **errp)
+{
+    const char *drvname = AudiodevDriver_str(dev->driver);
+    g_autofree char *type = g_strconcat("audio-", drvname, NULL);
+    AudioBackend *be = AUDIO_BACKEND(object_new(type));
+
+    if (!be) {
+        error_setg(errp, "Unknown audio driver `%s'", drvname);
+        return NULL;
+    }
+
+    if (!AUDIO_BACKEND_GET_CLASS(be)->realize(be, dev, errp)) {
+        object_unref(OBJECT(be));
+        return NULL;
+    }
+
+    return be;
+}
+
+
+static const TypeInfo audio_be_info = {
+    .name = TYPE_AUDIO_BACKEND,
+    .parent = TYPE_OBJECT,
+    .instance_size = sizeof(AudioBackend),
+    .abstract = true,
+    .class_size = sizeof(AudioBackendClass),
+};
+
+static void register_types(void)
+{
+    type_register_static(&audio_be_info);
+}
+
+type_init(register_types);
diff --git a/audio/audio.c b/audio/audio.c
index 3f09452e300..1d948084e80 100644
--- a/audio/audio.c
+++ b/audio/audio.c
@@ -869,15 +869,8 @@ static size_t audio_mixeng_backend_write(AudioBackend *be, SWVoiceOut *sw,
     }
 }
 
-size_t AUD_write(AudioBackend *be, SWVoiceOut *sw, void *buf, size_t size)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    return klass->write(be, sw, buf, size);
-}
-
-static size_t audio_mixeng_backend_read(AudioBackend *be, SWVoiceIn *sw,
-                                        void *buf, size_t size)
+static size_t audio_mixeng_backend_read(AudioBackend *be,
+                                        SWVoiceIn *sw, void *buf, size_t size)
 {
     HWVoiceIn *hw;
 
@@ -900,13 +893,6 @@ static size_t audio_mixeng_backend_read(AudioBackend *be, SWVoiceIn *sw,
 
 }
 
-size_t AUD_read(AudioBackend *be, SWVoiceIn *sw, void *buf, size_t size)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    return klass->read(be, sw, buf, size);
-}
-
 static int audio_mixeng_backend_get_buffer_size_out(AudioBackend *be, SWVoiceOut *sw)
 {
     if (!sw) {
@@ -920,13 +906,6 @@ static int audio_mixeng_backend_get_buffer_size_out(AudioBackend *be, SWVoiceOut
     return sw->hw->samples * sw->hw->info.bytes_per_frame;
 }
 
-int AUD_get_buffer_size_out(AudioBackend *be, SWVoiceOut *sw)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    return klass->get_buffer_size_out(be, sw);
-}
-
 static void audio_mixeng_backend_set_active_out(AudioBackend *be, SWVoiceOut *sw,
                                                 bool on)
 {
@@ -977,13 +956,6 @@ static void audio_mixeng_backend_set_active_out(AudioBackend *be, SWVoiceOut *sw
 
 }
 
-void AUD_set_active_out(AudioBackend *be, SWVoiceOut *sw, bool on)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    return klass->set_active_out(be, sw, on);
-}
-
 static void audio_mixeng_backend_set_active_in(AudioBackend *be, SWVoiceIn *sw, bool on)
 {
     HWVoiceIn *hw;
@@ -1029,13 +1001,6 @@ static void audio_mixeng_backend_set_active_in(AudioBackend *be, SWVoiceIn *sw,
     }
 }
 
-void AUD_set_active_in(AudioBackend *be, SWVoiceIn *sw, bool on)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    return klass->set_active_in(be, sw, on);
-}
-
 static size_t audio_get_avail(SWVoiceIn *sw)
 {
     size_t live;
@@ -1642,25 +1607,6 @@ static bool audio_mixeng_backend_realize(AudioBackend *abe,
     return true;
 }
 
-static AudioBackend *audio_be_new(Audiodev *dev, Error **errp)
-{
-    const char *drvname = AudiodevDriver_str(dev->driver);
-    g_autofree char *type = g_strconcat("audio-", drvname, NULL);
-    AudioBackend *be = AUDIO_BACKEND(object_new(type));
-
-    if (!be) {
-        error_setg(errp, "Unknown audio driver `%s'", drvname);
-        return NULL;
-    }
-
-    if (!AUDIO_BACKEND_GET_CLASS(be)->realize(be, dev, errp)) {
-        object_unref(OBJECT(be));
-        return NULL;
-    }
-
-    return be;
-}
-
 static void audio_vm_change_state_handler (void *opaque, bool running,
                                            RunState state)
 {
@@ -1684,14 +1630,6 @@ static void audio_vm_change_state_handler (void *opaque, bool running,
 
 static const VMStateDescription vmstate_audio;
 
-static void audio_be_init(Object *obj)
-{
-}
-
-static void audio_be_finalize(Object *obj)
-{
-}
-
 static const char *audio_mixeng_backend_get_id(AudioBackend *be)
 {
     return AUDIO_MIXENG_BACKEND(be)->dev->id;
@@ -1713,60 +1651,6 @@ static void audio_mixeng_backend_set_volume_out(AudioBackend *be, SWVoiceOut *sw
 static void audio_mixeng_backend_set_volume_in(AudioBackend *be, SWVoiceIn *sw,
                                                Volume *vol);
 
-SWVoiceOut *AUD_open_out(
-    AudioBackend *be,
-    SWVoiceOut *sw,
-    const char *name,
-    void *callback_opaque,
-    audio_callback_fn callback_fn,
-    const struct audsettings *as)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    return klass->open_out(be, sw, name, callback_opaque, callback_fn, as);
-}
-
-SWVoiceIn *AUD_open_in(
-    AudioBackend *be,
-    SWVoiceIn *sw,
-    const char *name,
-    void *callback_opaque,
-    audio_callback_fn callback_fn,
-    const struct audsettings *as)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    return klass->open_in(be, sw, name, callback_opaque, callback_fn, as);
-}
-
-void AUD_close_out(AudioBackend *be, SWVoiceOut *sw)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    return klass->close_out(be, sw);
-}
-
-void AUD_close_in(AudioBackend *be, SWVoiceIn *sw)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    return klass->close_in(be, sw);
-}
-
-bool AUD_is_active_out(AudioBackend *be, SWVoiceOut *sw)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    return klass->is_active_out(be, sw);
-}
-
-bool AUD_is_active_in(AudioBackend *be, SWVoiceIn *sw)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    return klass->is_active_in(be, sw);
-}
-
 static void audio_mixeng_backend_class_init(ObjectClass *klass, const void *data)
 {
     AudioBackendClass *be = AUDIO_BACKEND_CLASS(klass);
@@ -1971,20 +1855,6 @@ AudioBackend *audio_get_default_audio_be(Error **errp)
     return default_audio_be;
 }
 
-bool AUD_backend_check(AudioBackend **be, Error **errp)
-{
-    assert(be != NULL);
-
-    if (!*be) {
-        *be = audio_get_default_audio_be(errp);
-        if (!*be) {
-            return false;
-        }
-    }
-
-    return true;
-}
-
 static struct audio_pcm_ops capture_pcm_ops;
 
 static CaptureVoiceOut *audio_mixeng_backend_add_capture(
@@ -2061,17 +1931,6 @@ static CaptureVoiceOut *audio_mixeng_backend_add_capture(
     return cap;
 }
 
-CaptureVoiceOut *AUD_add_capture(
-    AudioBackend *be,
-    struct audsettings *as,
-    struct audio_capture_ops *ops,
-    void *cb_opaque)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    return klass->add_capture(be, as, ops, cb_opaque);
-}
-
 static void audio_mixeng_backend_del_capture(
     AudioBackend *be,
     CaptureVoiceOut *cap,
@@ -2114,13 +1973,6 @@ static void audio_mixeng_backend_del_capture(
     }
 }
 
-void AUD_del_capture(AudioBackend *be, CaptureVoiceOut *cap, void *cb_opaque)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    klass->del_capture(be, cap, cb_opaque);
-}
-
 static void audio_mixeng_backend_set_volume_out(AudioBackend *be, SWVoiceOut *sw,
                                                 Volume *vol)
 {
@@ -2138,13 +1990,6 @@ static void audio_mixeng_backend_set_volume_out(AudioBackend *be, SWVoiceOut *sw
     }
 }
 
-void AUD_set_volume_out(AudioBackend *be, SWVoiceOut *sw, Volume *vol)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    klass->set_volume_out(be, sw, vol);
-}
-
 static void audio_mixeng_backend_set_volume_in(AudioBackend *be, SWVoiceIn *sw,
                                                Volume *vol)
 {
@@ -2162,13 +2007,6 @@ static void audio_mixeng_backend_set_volume_in(AudioBackend *be, SWVoiceIn *sw,
     }
 }
 
-void AUD_set_volume_in(AudioBackend *be, SWVoiceIn *sw, Volume *vol)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    klass->set_volume_in(be, sw, vol);
-}
-
 static void audio_create_pdos(Audiodev *dev)
 {
     switch (dev->driver) {
@@ -2422,40 +2260,6 @@ AudioBackend *audio_be_by_name(const char *name, Error **errp)
     }
 }
 
-#ifdef CONFIG_GIO
-bool audio_be_can_set_dbus_server(AudioBackend *be)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    return klass->set_dbus_server != NULL;
-}
-
-bool audio_be_set_dbus_server(AudioBackend *be,
-                              GDBusObjectManagerServer *server,
-                              bool p2p,
-                              Error **errp)
-{
-    AudioBackendClass *klass = AUDIO_BACKEND_GET_CLASS(be);
-
-    if (!audio_be_can_set_dbus_server(be)) {
-        error_setg(errp, "Audiodev '%s' is not compatible with DBus",
-                   audio_be_get_id(be));
-        return false;
-    }
-
-    return klass->set_dbus_server(be, server, p2p, errp);
-}
-#endif
-
-const char *audio_be_get_id(AudioBackend *be)
-{
-    if (be) {
-        return AUDIO_BACKEND_GET_CLASS(be)->get_id(be);
-    } else {
-        return "";
-    }
-}
-
 const char *audio_application_name(void)
 {
     const char *vm_name;
@@ -2519,16 +2323,6 @@ AudiodevList *qmp_query_audiodevs(Error **errp)
     return ret;
 }
 
-static const TypeInfo audio_be_info = {
-    .name = TYPE_AUDIO_BACKEND,
-    .parent = TYPE_OBJECT,
-    .instance_size = sizeof(AudioBackend),
-    .instance_init = audio_be_init,
-    .instance_finalize = audio_be_finalize,
-    .abstract = true,
-    .class_size = sizeof(AudioBackendClass),
-};
-
 static const TypeInfo audio_mixeng_backend_info = {
     .name = TYPE_AUDIO_MIXENG_BACKEND,
     .parent = TYPE_AUDIO_BACKEND,
@@ -2542,7 +2336,6 @@ static const TypeInfo audio_mixeng_backend_info = {
 
 static void register_types(void)
 {
-    type_register_static(&audio_be_info);
     type_register_static(&audio_mixeng_backend_info);
 }
 
diff --git a/audio/meson.build b/audio/meson.build
index a5fec14fb3a..417670bd4c7 100644
--- a/audio/meson.build
+++ b/audio/meson.build
@@ -1,6 +1,7 @@
 audio_ss = ss.source_set()
 audio_ss.add(files(
   'audio.c',
+  'audio-be.c',
   'mixeng.c',
   'noaudio.c',
   'wavaudio.c',
-- 
2.52.0