From patchwork Wed Jun 3 21:03:48 2015 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: 480209 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 50D7A1402A0 for ; Thu, 4 Jun 2015 07:08:19 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=gpUKVMtU; dkim-atps=neutral Received: from localhost ([::1]:38170 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z0FtZ-0006g7-DA for incoming@patchwork.ozlabs.org; Wed, 03 Jun 2015 17:08:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58409) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z0FpM-0007d7-F3 for qemu-devel@nongnu.org; Wed, 03 Jun 2015 17:03:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z0FpK-0005gf-7K for qemu-devel@nongnu.org; Wed, 03 Jun 2015 17:03:56 -0400 Received: from mail-wi0-x22d.google.com ([2a00:1450:400c:c05::22d]:34944) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z0FpJ-0005gH-Tn for qemu-devel@nongnu.org; Wed, 03 Jun 2015 17:03:54 -0400 Received: by wiga1 with SMTP id a1so27776660wig.0 for ; Wed, 03 Jun 2015 14:03:53 -0700 (PDT) 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=o17yM7MNQH3qRIBSSJDkXKNVXNZYaaXBxGz/WJ63d24=; b=gpUKVMtUq7v1XQTT+iTxmP46kevqxID/Cf5K3NebHos2Px8bFHMpF6MRFv/qJLLhuG /CiCe/RlFyyzNYxtpkeg8Bk9uv5C7GEobp0lEognJmKbqNA37DyacjQN+EyGAVHrV/hL QJNV2AvAil3VF/zgC45YRVlMFfKH5PM8CCYtiqlKibCKU5mDIQQ6oJO3c4uBe/7l6wrx dsetaN9sz/G0dpGyZKSfah3MbcVKhn1eWzxwIHP4RNPawPwKlwYjsbUSq8h7DxB7YRg3 NjGZlo4vBqB8ltWhema7i7tsWqfcClWQjfyzOvaTkcb0q6cM5efxGKj5wDAY+ljw9wg8 pHDg== X-Received: by 10.180.19.100 with SMTP id d4mr1077037wie.29.1433365431730; Wed, 03 Jun 2015 14:03:51 -0700 (PDT) Received: from nullptr.home.dirty-ice.org (94-21-43-102.pool.digikabel.hu. [94.21.43.102]) by mx.google.com with ESMTPSA id l6sm2727050wjz.4.2015.06.03.14.03.50 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 03 Jun 2015 14:03:51 -0700 (PDT) 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: peter.maydell@linaro.org, qemu-devel@nongnu.org Date: Wed, 3 Jun 2015 23:03:48 +0200 Message-Id: <2752d84ed00971ed9188f781d53136a07a683d8a.1433365252.git.DirtY.iCE.hu@gmail.com> X-Mailer: git-send-email 2.4.2 In-Reply-To: References: MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c05::22d Cc: Gerd Hoffmann Subject: [Qemu-devel] [PATCH v3 05/12] alsaaudio: do not use global variables 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 Signed-off-by: Kővágó, Zoltán --- audio/alsaaudio.c | 160 +++++++++++++++++++++++++++++------------------------- 1 file changed, 87 insertions(+), 73 deletions(-) diff --git a/audio/alsaaudio.c b/audio/alsaaudio.c index 87e71c6..d7e181b 100644 --- a/audio/alsaaudio.c +++ b/audio/alsaaudio.c @@ -33,30 +33,7 @@ #define AUDIO_CAP "alsa" #include "audio_int.h" -struct pollhlp { - snd_pcm_t *handle; - struct pollfd *pfds; - int count; - int mask; -}; - -typedef struct ALSAVoiceOut { - HWVoiceOut hw; - int wpos; - int pending; - void *pcm_buf; - snd_pcm_t *handle; - struct pollhlp pollhlp; -} ALSAVoiceOut; - -typedef struct ALSAVoiceIn { - HWVoiceIn hw; - snd_pcm_t *handle; - void *pcm_buf; - struct pollhlp pollhlp; -} ALSAVoiceIn; - -static struct { +typedef struct ALSAConf { int size_in_usec_in; int size_in_usec_out; const char *pcm_name_in; @@ -73,13 +50,32 @@ static struct { int buffer_size_out_overridden; int period_size_out_overridden; int verbose; -} conf = { - .buffer_size_out = 4096, - .period_size_out = 1024, - .pcm_name_out = "default", - .pcm_name_in = "default", +} ALSAConf; + +struct pollhlp { + snd_pcm_t *handle; + struct pollfd *pfds; + ALSAConf *conf; + int count; + int mask; }; +typedef struct ALSAVoiceOut { + HWVoiceOut hw; + int wpos; + int pending; + void *pcm_buf; + snd_pcm_t *handle; + struct pollhlp pollhlp; +} ALSAVoiceOut; + +typedef struct ALSAVoiceIn { + HWVoiceIn hw; + snd_pcm_t *handle; + void *pcm_buf; + struct pollhlp pollhlp; +} ALSAVoiceIn; + struct alsa_params_req { int freq; snd_pcm_format_t fmt; @@ -184,6 +180,7 @@ static void alsa_poll_handler (void *opaque) snd_pcm_state_t state; struct pollhlp *hlp = opaque; unsigned short revents; + ALSAConf *conf = hlp->conf; count = poll (hlp->pfds, hlp->count, 0); if (count < 0) { @@ -205,7 +202,7 @@ static void alsa_poll_handler (void *opaque) } if (!(revents & hlp->mask)) { - if (conf.verbose) { + if (conf->verbose) { dolog ("revents = %d\n", revents); } return; @@ -242,6 +239,7 @@ static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask) { int i, count, err; struct pollfd *pfds; + ALSAConf *conf = hlp->conf; count = snd_pcm_poll_descriptors_count (handle); if (count <= 0) { @@ -270,13 +268,13 @@ static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask) NULL, hlp); } if (pfds[i].events & POLLOUT) { - if (conf.verbose) { + if (conf->verbose) { dolog ("POLLOUT %d %d\n", i, pfds[i].fd); } err = qemu_set_fd_handler (pfds[i].fd, NULL, alsa_poll_handler, hlp); } - if (conf.verbose) { + if (conf->verbose) { dolog ("Set handler events=%#x index=%d fd=%d err=%d\n", pfds[i].events, i, pfds[i].fd, err); } @@ -476,14 +474,15 @@ static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold) } static int alsa_open (int in, struct alsa_params_req *req, - struct alsa_params_obt *obt, snd_pcm_t **handlep) + struct alsa_params_obt *obt, snd_pcm_t **handlep, + ALSAConf *conf) { snd_pcm_t *handle; snd_pcm_hw_params_t *hw_params; int err; int size_in_usec; unsigned int freq, nchannels; - const char *pcm_name = in ? conf.pcm_name_in : conf.pcm_name_out; + const char *pcm_name = in ? conf->pcm_name_in : conf->pcm_name_out; snd_pcm_uframes_t obt_buffer_size; const char *typ = in ? "ADC" : "DAC"; snd_pcm_format_t obtfmt; @@ -522,7 +521,7 @@ static int alsa_open (int in, struct alsa_params_req *req, } err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt); - if (err < 0 && conf.verbose) { + if (err < 0 && conf->verbose) { alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt); } @@ -654,7 +653,7 @@ static int alsa_open (int in, struct alsa_params_req *req, goto err; } - if (!in && conf.threshold) { + if (!in && conf->threshold) { snd_pcm_uframes_t threshold; int bytes_per_sec; @@ -676,7 +675,7 @@ static int alsa_open (int in, struct alsa_params_req *req, break; } - threshold = (conf.threshold * bytes_per_sec) / 1000; + threshold = (conf->threshold * bytes_per_sec) / 1000; alsa_set_threshold (handle, threshold); } @@ -686,7 +685,7 @@ static int alsa_open (int in, struct alsa_params_req *req, *handlep = handle; - if (conf.verbose && + if (conf->verbose && (obtfmt != req->fmt || obt->nchannels != req->nchannels || obt->freq != req->freq)) { @@ -729,6 +728,7 @@ static snd_pcm_sframes_t alsa_get_avail (snd_pcm_t *handle) static void alsa_write_pending (ALSAVoiceOut *alsa) { HWVoiceOut *hw = &alsa->hw; + ALSAConf *conf = alsa->pollhlp.conf; while (alsa->pending) { int left_till_end_samples = hw->samples - alsa->wpos; @@ -743,7 +743,7 @@ static void alsa_write_pending (ALSAVoiceOut *alsa) if (written <= 0) { switch (written) { case 0: - if (conf.verbose) { + if (conf->verbose) { dolog ("Failed to write %d frames (wrote zero)\n", len); } return; @@ -754,7 +754,7 @@ static void alsa_write_pending (ALSAVoiceOut *alsa) len); return; } - if (conf.verbose) { + if (conf->verbose) { dolog ("Recovering from playback xrun\n"); } continue; @@ -767,7 +767,7 @@ static void alsa_write_pending (ALSAVoiceOut *alsa) len); return; } - if (conf.verbose) { + if (conf->verbose) { dolog ("Resuming suspended output stream\n"); } continue; @@ -827,18 +827,19 @@ static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as, struct alsa_params_obt obt; snd_pcm_t *handle; struct audsettings obt_as; + ALSAConf *conf = drv_opaque; req.fmt = aud_to_alsafmt (as->fmt, as->endianness); req.freq = as->freq; req.nchannels = as->nchannels; - req.period_size = conf.period_size_out; - req.buffer_size = conf.buffer_size_out; - req.size_in_usec = conf.size_in_usec_out; + req.period_size = conf->period_size_out; + req.buffer_size = conf->buffer_size_out; + req.size_in_usec = conf->size_in_usec_out; req.override_mask = - (conf.period_size_out_overridden ? 1 : 0) | - (conf.buffer_size_out_overridden ? 2 : 0); + (conf->period_size_out_overridden ? 1 : 0) | + (conf->buffer_size_out_overridden ? 2 : 0); - if (alsa_open (0, &req, &obt, &handle)) { + if (alsa_open (0, &req, &obt, &handle, conf)) { return -1; } @@ -859,6 +860,7 @@ static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as, } alsa->handle = handle; + alsa->pollhlp.conf = conf; return 0; } @@ -936,18 +938,19 @@ static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque) struct alsa_params_obt obt; snd_pcm_t *handle; struct audsettings obt_as; + ALSAConf *conf = drv_opaque; req.fmt = aud_to_alsafmt (as->fmt, as->endianness); req.freq = as->freq; req.nchannels = as->nchannels; - req.period_size = conf.period_size_in; - req.buffer_size = conf.buffer_size_in; - req.size_in_usec = conf.size_in_usec_in; + req.period_size = conf->period_size_in; + req.buffer_size = conf->buffer_size_in; + req.size_in_usec = conf->size_in_usec_in; req.override_mask = - (conf.period_size_in_overridden ? 1 : 0) | - (conf.buffer_size_in_overridden ? 2 : 0); + (conf->period_size_in_overridden ? 1 : 0) | + (conf->buffer_size_in_overridden ? 2 : 0); - if (alsa_open (1, &req, &obt, &handle)) { + if (alsa_open (1, &req, &obt, &handle, conf)) { return -1; } @@ -968,6 +971,7 @@ static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque) } alsa->handle = handle; + alsa->pollhlp.conf = conf; return 0; } @@ -998,6 +1002,7 @@ static int alsa_run_in (HWVoiceIn *hw) }; snd_pcm_sframes_t avail; snd_pcm_uframes_t read_samples = 0; + ALSAConf *conf = alsa->pollhlp.conf; if (!dead) { return 0; @@ -1023,12 +1028,12 @@ static int alsa_run_in (HWVoiceIn *hw) dolog ("Failed to resume suspended input stream\n"); return 0; } - if (conf.verbose) { + if (conf->verbose) { dolog ("Resuming suspended input stream\n"); } break; default: - if (conf.verbose) { + if (conf->verbose) { dolog ("No frames available and ALSA state is %d\n", state); } return 0; @@ -1065,7 +1070,7 @@ static int alsa_run_in (HWVoiceIn *hw) if (nread <= 0) { switch (nread) { case 0: - if (conf.verbose) { + if (conf->verbose) { dolog ("Failed to read %ld frames (read zero)\n", len); } goto exit; @@ -1075,7 +1080,7 @@ static int alsa_run_in (HWVoiceIn *hw) alsa_logerr (nread, "Failed to read %ld frames\n", len); goto exit; } - if (conf.verbose) { + if (conf->verbose) { dolog ("Recovering from capture xrun\n"); } continue; @@ -1149,80 +1154,89 @@ static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...) return -1; } +static ALSAConf glob_conf = { + .buffer_size_out = 4096, + .period_size_out = 1024, + .pcm_name_out = "default", + .pcm_name_in = "default", +}; + static void *alsa_audio_init (void) { - return &conf; + ALSAConf *conf = g_malloc(sizeof(ALSAConf)); + *conf = glob_conf; + return conf; } static void alsa_audio_fini (void *opaque) { - (void) opaque; + g_free(opaque); } static struct audio_option alsa_options[] = { { .name = "DAC_SIZE_IN_USEC", .tag = AUD_OPT_BOOL, - .valp = &conf.size_in_usec_out, + .valp = &glob_conf.size_in_usec_out, .descr = "DAC period/buffer size in microseconds (otherwise in frames)" }, { .name = "DAC_PERIOD_SIZE", .tag = AUD_OPT_INT, - .valp = &conf.period_size_out, + .valp = &glob_conf.period_size_out, .descr = "DAC period size (0 to go with system default)", - .overriddenp = &conf.period_size_out_overridden + .overriddenp = &glob_conf.period_size_out_overridden }, { .name = "DAC_BUFFER_SIZE", .tag = AUD_OPT_INT, - .valp = &conf.buffer_size_out, + .valp = &glob_conf.buffer_size_out, .descr = "DAC buffer size (0 to go with system default)", - .overriddenp = &conf.buffer_size_out_overridden + .overriddenp = &glob_conf.buffer_size_out_overridden }, { .name = "ADC_SIZE_IN_USEC", .tag = AUD_OPT_BOOL, - .valp = &conf.size_in_usec_in, + .valp = &glob_conf.size_in_usec_in, .descr = "ADC period/buffer size in microseconds (otherwise in frames)" }, { .name = "ADC_PERIOD_SIZE", .tag = AUD_OPT_INT, - .valp = &conf.period_size_in, + .valp = &glob_conf.period_size_in, .descr = "ADC period size (0 to go with system default)", - .overriddenp = &conf.period_size_in_overridden + .overriddenp = &glob_conf.period_size_in_overridden }, { .name = "ADC_BUFFER_SIZE", .tag = AUD_OPT_INT, - .valp = &conf.buffer_size_in, + .valp = &glob_conf.buffer_size_in, .descr = "ADC buffer size (0 to go with system default)", - .overriddenp = &conf.buffer_size_in_overridden + .overriddenp = &glob_conf.buffer_size_in_overridden }, { .name = "THRESHOLD", .tag = AUD_OPT_INT, - .valp = &conf.threshold, + .valp = &glob_conf.threshold, .descr = "(undocumented)" }, { .name = "DAC_DEV", .tag = AUD_OPT_STR, - .valp = &conf.pcm_name_out, + .valp = &glob_conf.pcm_name_out, .descr = "DAC device name (for instance dmix)" }, { .name = "ADC_DEV", .tag = AUD_OPT_STR, - .valp = &conf.pcm_name_in, + .valp = &glob_conf.pcm_name_in, .descr = "ADC device name" }, { .name = "VERBOSE", .tag = AUD_OPT_BOOL, - .valp = &conf.verbose, + .valp = &glob_conf.verbose, .descr = "Behave in a more verbose way" }, { /* End of list */ }