In case of sound devices which are created by default (some of them even with -nodefaults) it would not be nice to users if qemu required explicit: -audiodev driver=none,id=audio0 -machine default-audiodev=audio0 when they just want to run a VM and not care about any audio output. Instead this little helper makes it easy to create a dummy audiodev (driver=none) in case there is no default-audiodev specified for the machine. To make sure users are not surprised by no sound output a helping message is also printed out. Signed-off-by: Martin Kletzander <mkletzan@xxxxxxxxxx> --- audio/audio.c | 34 ++++++++++++++++++++++++++++++++++ audio/audio.h | 2 ++ 2 files changed, 36 insertions(+) diff --git a/audio/audio.c b/audio/audio.c index 9e91a5a4f2b8..671845c65d18 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -37,6 +37,7 @@ #include "sysemu/runstate.h" #include "ui/qemu-spice.h" #include "trace.h" +#include "hw/boards.h" #define AUDIO_CAP "audio" #include "audio_int.h" @@ -2122,6 +2123,39 @@ void audio_init_audiodevs(void) } } +static void audio_init_dummy(const char *id) +{ + Audiodev *dev = g_new0(Audiodev, 1); + AudiodevListEntry *e = g_new0(AudiodevListEntry, 1); + + dev->driver = AUDIODEV_DRIVER_NONE; + dev->id = g_strdup(id); + + audio_validate_opts(dev, &error_abort); + audio_init(dev, NULL); + + e->dev = dev; + QSIMPLEQ_INSERT_TAIL(&audiodevs, e, next); +} + +const char *audio_maybe_init_dummy(const char *default_id) +{ + MachineState *ms = MACHINE(qdev_get_machine()); + + if (ms->default_audiodev) { + return ms->default_audiodev; + } + + dolog("warning: No audiodev specified for implicit machine devices, " + "no audio output will be available for these. " + "For setting a backend audiodev for such devices try using " + "the default-audiodev machine option.\n"); + + audio_init_dummy(default_id); + + return default_id; +} + audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo) { return (audsettings) { diff --git a/audio/audio.h b/audio/audio.h index 3d5ecdecd5c1..335704a4ddb1 100644 --- a/audio/audio.h +++ b/audio/audio.h @@ -175,6 +175,8 @@ void audio_legacy_help(void); AudioState *audio_state_by_name(const char *name); const char *audio_get_id(QEMUSoundCard *card); +const char *audio_maybe_init_dummy(const char *default_id); + #define DEFINE_AUDIO_PROPERTIES(_s, _f) \ DEFINE_PROP_AUDIODEV("audiodev", _s, _f) -- 2.35.1