From patchwork Thu Jan 14 13:45:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?UTF-8?B?Wm9sdMOhbiBLxZF2w6Fnw7M=?= X-Patchwork-Id: 567466 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id B94DE1402E2 for ; Fri, 15 Jan 2016 01:04:01 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=ZaoIQJpn; dkim-atps=neutral Received: from localhost ([::1]:42679 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aJiVL-0004Cl-J1 for incoming@patchwork.ozlabs.org; Thu, 14 Jan 2016 09:03:59 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35605) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aJiER-0001PD-6v for qemu-devel@nongnu.org; Thu, 14 Jan 2016 08:46:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aJiEK-0003S1-Eu for qemu-devel@nongnu.org; Thu, 14 Jan 2016 08:46:31 -0500 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:33164) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aJiEK-0003Rn-5m for qemu-devel@nongnu.org; Thu, 14 Jan 2016 08:46:24 -0500 Received: by mail-wm0-x241.google.com with SMTP id u188so43609440wmu.0 for ; Thu, 14 Jan 2016 05:46:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=g+dK/IooGvBYJGJkBC85unZSBUh+Ro8Gdx6jgCcWcaM=; b=ZaoIQJpn5b7Mr+qOMZ9Gz7K4kSWmHaz15cegrsVP2v7pxx8rT34Nh1rnZYESBW24rc z2lik01a1N32o8aE5ZM1/7mH7WLJxpjLV7jtyz5U6RRiZv6Y19C2HtIiodqqgRlzk4pa NGX/MK5Lyy2fSNwKPCwcDbJU6fiVQFrfy/D6T/jh8TqMFZvU33r+wMq9gar6cAdRPPO0 WoIjuXgmpvZaRcgXdKioHB+McRteQzJu4PWzcNLZz44YldkFGwLrCjxe0NAdUuPK7kmX wKgj/InZ8UwjuG1RH0P5ovtpNeRi9mAb7T4PG/bpPUTWjbIupygwAOaSMYXzabWjw+k1 d9CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-type:content-transfer-encoding; bh=g+dK/IooGvBYJGJkBC85unZSBUh+Ro8Gdx6jgCcWcaM=; b=Eg8LFnj+/vhqqEKnCa1yaFtFuqGH7YeKrvyohc0mYZdrEFy4dPHs8wfwZYTl4CElfV q8cjxSI78NFwc6gaLuJW6HbV18SekSq5WvuiXZdjQUYH9kUlpcp2SG5bJiUJKskRuc6t kZyopYFDQCIZmFQlZ3qx8XOUr/6/KZmZ0JJbW3a6Do6ueoERmA04DZSgYHM71pk1iDmj MW2s8CALbEjGEtgxrUWVNVJys9H3z5SWF0W7GKi5MRyAguUjA+oJ1K9akvwyITnZtILp YgB5x0PHx3y8k/7DATcIMO3KybXC6Kzn9jBVooIe504eyiisejC2XRxQliAeE9Znudc+ uL4A== X-Gm-Message-State: ALoCoQml3FxisXTHBHY4trW9xJroAjVhYV3yA/XWifHyANz9kzj7dsMguP8IoIZtnf2wZU8R1aZuwbp2/DDuPNuL6OkOGhm2bw== X-Received: by 10.194.120.226 with SMTP id lf2mr4135040wjb.108.1452779183686; Thu, 14 Jan 2016 05:46:23 -0800 (PST) Received: from nullptr.home.dirty-ice.org (188-143-126-18.pool.digikabel.hu. [188.143.126.18]) by smtp.gmail.com with ESMTPSA id 79sm7398061wmo.7.2016.01.14.05.46.22 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 14 Jan 2016 05:46:23 -0800 (PST) From: "=?UTF-8?q?K=C5=91v=C3=A1g=C3=B3=2C=20Zolt=C3=A1n?=" X-Google-Original-From: =?UTF-8?q?K=C5=91v=C3=A1g=C3=B3=2C=20Zolt=C3=A1n?= To: qemu-devel@nongnu.org Date: Thu, 14 Jan 2016 14:45:31 +0100 Message-Id: <4d880e9b029b08d6fa7a65d4dcdeab00dd1a9f2e.1452776884.git.DirtY.iCE.hu@gmail.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: References: MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::241 Cc: Markus Armbruster , Paolo Bonzini , Gerd Hoffmann , Luiz Capitulino Subject: [Qemu-devel] [PATCH 18/51] audio: basic support for multi backend audio X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Audio functions no longer access glob_audio_state, instead they get an AudioState as a parameter. This is required in order to support multiple backends. glob_audio_state is also gone, and replaced with a tailq so we can store more than one states. Signed-off-by: Kővágó, Zoltán --- audio/audio.c | 98 +++++++++++++++++++++++++++++++++++++------------- audio/audio.h | 12 +++++-- audio/audio_int.h | 2 ++ audio/audio_template.h | 2 +- audio/wavcapture.c | 6 ++-- hmp-commands.hx | 11 +++--- monitor.c | 12 ++++++- qemu-options.hx | 5 +++ ui/vnc.c | 15 +++++++- ui/vnc.h | 2 ++ vl.c | 3 +- 11 files changed, 129 insertions(+), 39 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index 2aa2071..b435a35 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -55,7 +55,8 @@ struct audio_driver *drvtab[] = { NULL }; -static AudioState glob_audio_state; +static QTAILQ_HEAD(AudioStateHead, AudioState) audio_states = + QTAILQ_HEAD_INITIALIZER(audio_states); const struct mixeng_volume nominal_volume = { .mute = 0, @@ -1193,11 +1194,14 @@ static void audio_run_capture (AudioState *s) void audio_run (const char *msg) { - AudioState *s = &glob_audio_state; + AudioState *s; + + QTAILQ_FOREACH(s, &audio_states, list) { + audio_run_out (s); + audio_run_in (s); + audio_run_capture (s); + } - audio_run_out (s); - audio_run_in (s); - audio_run_capture (s); #ifdef DEBUG_POLL { static double prevtime; @@ -1252,9 +1256,8 @@ static void audio_vm_change_state_handler (void *opaque, int running, audio_reset_timer (s); } -static void audio_atexit (void) +static void free_audio_state(AudioState *s) { - AudioState *s = &glob_audio_state; HWVoiceOut *hwo = NULL; HWVoiceIn *hwi = NULL; @@ -1288,6 +1291,16 @@ static void audio_atexit (void) } qapi_free_Audiodev(s->dev); + g_free(s); +} + +static void audio_atexit(void) +{ + while (!QTAILQ_EMPTY(&audio_states)) { + AudioState *s = QTAILQ_FIRST(&audio_states); + QTAILQ_REMOVE(&audio_states, s, list); + free_audio_state(s); + } } static const VMStateDescription vmstate_audio = { @@ -1300,26 +1313,27 @@ static const VMStateDescription vmstate_audio = { }; static Audiodev *parse_option(QemuOpts *opts, Error **errp); -static int audio_init(Audiodev *dev) + +/* if we have dev, this function was called because of an -audiodev argument => + * initialize a new state with it + * if dev == NULL => legacy implicit initialization, return the already created + * state or create a new one */ +static AudioState *audio_init(Audiodev *dev) { + static bool atexit_registered; size_t i; int done = 0; const char *drvname = NULL; VMChangeStateEntry *e; - AudioState *s = &glob_audio_state; + AudioState *s; QemuOptsList *list = NULL; /* silence gcc warning about uninitialized * variable */ - if (s->drv) { - if (dev) { - dolog("Cannot create more than one audio backend, sorry\n"); - qapi_free_Audiodev(dev); - } - return -1; - } - if (dev) { drvname = AudiodevDriver_lookup[dev->driver]; + } else if (!QTAILQ_EMPTY(&audio_states)) { + /* todo: chack for -audiodev we have normal audiodev selection support */ + return QTAILQ_FIRST(&audio_states); } else { audio_handle_legacy_opts(); list = qemu_find_opts("audiodev"); @@ -1328,12 +1342,18 @@ static int audio_init(Audiodev *dev) exit(1); } } + + s = g_malloc0(sizeof(AudioState)); s->dev = dev; QLIST_INIT (&s->hw_head_out); QLIST_INIT (&s->hw_head_in); QLIST_INIT (&s->cap_head); - atexit (audio_atexit); + if (!atexit_registered) { + atexit(audio_atexit); + atexit_registered = true; + } + QTAILQ_INSERT_TAIL(&audio_states, s, list); s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s); if (!s->ts) { @@ -1414,15 +1434,18 @@ static int audio_init(Audiodev *dev) QLIST_INIT (&s->card_head); vmstate_register (NULL, 0, &vmstate_audio, s); - return 0; + return s; } void AUD_register_card (const char *name, QEMUSoundCard *card) { - audio_init(NULL); + if (!card->state) { + card->state = audio_init(NULL); + } + card->name = g_strdup (name); memset (&card->entries, 0, sizeof (card->entries)); - QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries); + QLIST_INSERT_HEAD (&card->state->card_head, card, entries); } void AUD_remove_card (QEMUSoundCard *card) @@ -1432,16 +1455,21 @@ void AUD_remove_card (QEMUSoundCard *card) } -CaptureVoiceOut *AUD_add_capture ( +CaptureVoiceOut *AUD_add_capture( + AudioState *s, struct audsettings *as, struct audio_capture_ops *ops, void *cb_opaque ) { - AudioState *s = &glob_audio_state; CaptureVoiceOut *cap; struct capture_callback *cb; + if (!s) { + /* todo: remove when we have normal audiodev selection support */ + s = QTAILQ_FIRST(&audio_states); + } + if (audio_validate_settings (as)) { dolog ("Invalid settings were passed when trying to add capture\n"); audio_print_settings (as); @@ -1677,7 +1705,7 @@ static int each_option(void *opaque, QemuOpts *opts, Error **errp) if (!dev) { return -1; } - return audio_init(dev); + return audio_init(dev) ? 0 : -1; } void audio_set_options(void) @@ -1743,3 +1771,25 @@ int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo, return audio_buffer_samples(pdo, as, def_usecs) * audioformat_bytes_per_sample(as->fmt); } + +AudioState *audio_state_by_name(const char *name) +{ + AudioState *s; + QTAILQ_FOREACH(s, &audio_states, list) { + assert(s->dev); + if (strcmp(name, s->dev->id) == 0) { + return s; + } + } + return NULL; +} + +const char *audio_get_id(QEMUSoundCard *card) +{ + if (card->state) { + assert(card->state->dev); + return card->state->dev->id; + } else { + return ""; + } +} diff --git a/audio/audio.h b/audio/audio.h index 177a673..0085a07 100644 --- a/audio/audio.h +++ b/audio/audio.h @@ -80,8 +80,10 @@ typedef struct SWVoiceOut SWVoiceOut; typedef struct CaptureVoiceOut CaptureVoiceOut; typedef struct SWVoiceIn SWVoiceIn; +typedef struct AudioState AudioState; typedef struct QEMUSoundCard { char *name; + AudioState *state; QLIST_ENTRY (QEMUSoundCard) entries; } QEMUSoundCard; @@ -96,7 +98,8 @@ void AUD_log (const char *cap, const char *fmt, ...) GCC_FMT_ATTR(2, 3); void AUD_register_card (const char *name, QEMUSoundCard *card); void AUD_remove_card (QEMUSoundCard *card); -CaptureVoiceOut *AUD_add_capture ( +CaptureVoiceOut *AUD_add_capture( + AudioState *s, struct audsettings *as, struct audio_capture_ops *ops, void *opaque @@ -164,11 +167,14 @@ static inline void *advance (void *p, int incr) #define audio_MAX(a, b) ((a)<(b)?(b):(a)) #endif -int wav_start_capture (CaptureState *s, const char *path, int freq, - int bits, int nchannels); +int wav_start_capture(AudioState *state, CaptureState *s, const char *path, + int freq, int bits, int nchannels); void audio_set_options(void); void audio_handle_legacy_opts(void); void audio_legacy_help(void); +AudioState *audio_state_by_name(const char *name); +const char *audio_get_id(QEMUSoundCard *card); + #endif /* audio.h */ diff --git a/audio/audio_int.h b/audio/audio_int.h index 101081b..1d81658 100644 --- a/audio/audio_int.h +++ b/audio/audio_int.h @@ -189,6 +189,8 @@ struct AudioState { int nb_hw_voices_in; int vm_running; int64_t period_ticks; + + QTAILQ_ENTRY(AudioState) list; }; extern struct audio_driver no_audio_driver; diff --git a/audio/audio_template.h b/audio/audio_template.h index 455e50d..5a3dc90 100644 --- a/audio/audio_template.h +++ b/audio/audio_template.h @@ -399,7 +399,7 @@ SW *glue (AUD_open_, TYPE) ( struct audsettings *as ) { - AudioState *s = &glob_audio_state; + AudioState *s = card->state; AudiodevPerDirectionOptions *pdo = s->dev->TYPE; if (audio_bug (AUDIO_FUNC, !card || !name || !callback_fn || !as)) { diff --git a/audio/wavcapture.c b/audio/wavcapture.c index 798acdd..8b8d3d0 100644 --- a/audio/wavcapture.c +++ b/audio/wavcapture.c @@ -104,8 +104,8 @@ static struct capture_ops wav_capture_ops = { .info = wav_capture_info }; -int wav_start_capture (CaptureState *s, const char *path, int freq, - int bits, int nchannels) +int wav_start_capture(AudioState *state, CaptureState *s, const char *path, + int freq, int bits, int nchannels) { Monitor *mon = cur_mon; WAVState *wav; @@ -172,7 +172,7 @@ int wav_start_capture (CaptureState *s, const char *path, int freq, goto error_free; } - cap = AUD_add_capture (&as, &ops, wav); + cap = AUD_add_capture(state, &as, &ops, wav); if (!cap) { monitor_printf (mon, "Failed to add audio capture\n"); goto error_free; diff --git a/hmp-commands.hx b/hmp-commands.hx index bb52e4d..cc75baa 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -757,16 +757,17 @@ ETEXI { .name = "wavcapture", - .args_type = "path:F,freq:i?,bits:i?,nchannels:i?", - .params = "path [frequency [bits [channels]]]", + .args_type = "path:F,freq:i?,bits:i?,nchannels:i?,audiodev:s?", + .params = "path [frequency [bits [channels [audiodev]]]]", .help = "capture audio to a wave file (default frequency=44100 bits=16 channels=2)", .mhandler.cmd = hmp_wavcapture, }, STEXI -@item wavcapture @var{filename} [@var{frequency} [@var{bits} [@var{channels}]]] +@item wavcapture @var{filename} [@var{frequency} [@var{bits} [@var{channels} [@var{audiodev}]]]] @findex wavcapture -Capture audio into @var{filename}. Using sample rate @var{frequency} -bits per sample @var{bits} and number of channels @var{channels}. +Capture audio into @var{filename} from @var{audiodev}. Using sample rate +@var{frequency} bits per sample @var{bits} and number of channels +@var{channels}. Defaults: @itemize @minus diff --git a/monitor.c b/monitor.c index e7e7ae2..a56ef20 100644 --- a/monitor.c +++ b/monitor.c @@ -1559,7 +1559,17 @@ static void hmp_wavcapture(Monitor *mon, const QDict *qdict) int bits = qdict_get_try_int(qdict, "bits", -1); int has_channels = qdict_haskey(qdict, "nchannels"); int nchannels = qdict_get_try_int(qdict, "nchannels", -1); + const char *audiodev = qdict_get_try_str(qdict, "audiodev"); CaptureState *s; + AudioState *as = NULL; + + if (audiodev) { + as = audio_state_by_name(audiodev); + if (!as) { + monitor_printf(mon, "Invalid audiodev specified\n"); + return; + } + } s = g_malloc0 (sizeof (*s)); @@ -1567,7 +1577,7 @@ static void hmp_wavcapture(Monitor *mon, const QDict *qdict) bits = has_bits ? bits : 16; nchannels = has_channels ? nchannels : 2; - if (wav_start_capture (s, path, freq, bits, nchannels)) { + if (wav_start_capture(as, s, path, freq, bits, nchannels)) { monitor_printf(mon, "Failed to add wave capture\n"); g_free (s); return; diff --git a/qemu-options.hx b/qemu-options.hx index 97f021f..184ffd4 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -1621,6 +1621,11 @@ everybody else. 'ignore' completely ignores the shared flag and allows everybody connect unconditionally. Doesn't conform to the rfb spec but is traditional QEMU behavior. +@item audiodev=@var{audiodev} + +Use the specified @var{audiodev} when the VNC client requests audio +transmission. + @end table ETEXI diff --git a/ui/vnc.c b/ui/vnc.c index 2dd269b..dc54e06 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -1154,7 +1154,7 @@ static void audio_add(VncState *vs) ops.destroy = audio_capture_destroy; ops.capture = audio_capture; - vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs); + vs->audio_cap = AUD_add_capture(vs->vd->audio_state, &vs->as, &ops, vs); if (!vs->audio_cap) { error_report("Failed to add audio capture"); } @@ -3262,6 +3262,9 @@ static QemuOptsList qemu_vnc_opts = { },{ .name = "non-adaptive", .type = QEMU_OPT_BOOL, + },{ + .name = "audiodev", + .type = QEMU_OPT_STRING, }, { /* end of list */ } }, @@ -3480,6 +3483,7 @@ void vnc_display_open(const char *id, Error **errp) #endif int acl = 0; int lock_key_sync = 1; + const char *audiodev; if (!vs) { error_setg(errp, "VNC display not active"); @@ -3723,6 +3727,15 @@ void vnc_display_open(const char *id, Error **errp) #endif vs->lock_key_sync = lock_key_sync; + audiodev = qemu_opt_get(opts, "audiodev"); + if (audiodev) { + vs->audio_state = audio_state_by_name(audiodev); + if (!vs->audio_state) { + error_setg(errp, "Audiodev '%s' not found", audiodev); + goto fail; + } + } + device_id = qemu_opt_get(opts, "display"); if (device_id) { DeviceState *dev; diff --git a/ui/vnc.h b/ui/vnc.h index a95cc15..21ecfdb 100644 --- a/ui/vnc.h +++ b/ui/vnc.h @@ -182,6 +182,8 @@ struct VncDisplay #ifdef CONFIG_VNC_SASL VncDisplaySASL sasl; #endif + + AudioState *audio_state; }; typedef struct VncTight { diff --git a/vl.c b/vl.c index de47b6b..a8e8beb 100644 --- a/vl.c +++ b/vl.c @@ -4326,6 +4326,8 @@ int main(int argc, char **argv, char **envp) exit(0); } + audio_set_options(); + machine_opts = qemu_get_machine_opts(); if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine, NULL)) { @@ -4540,7 +4542,6 @@ int main(int argc, char **argv, char **envp) realtime_init(); - audio_set_options(); audio_init(); cpu_synchronize_all_post_init();