From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49736) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z2H4L-0001vY-I4 for qemu-devel@nongnu.org; Tue, 09 Jun 2015 06:47:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z2H4I-0004dj-0G for qemu-devel@nongnu.org; Tue, 09 Jun 2015 06:47:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58501) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z2H4H-0004dQ-MS for qemu-devel@nongnu.org; Tue, 09 Jun 2015 06:47:41 -0400 From: Gerd Hoffmann Date: Tue, 9 Jun 2015 12:47:25 +0200 Message-Id: <1433846851-20552-7-git-send-email-kraxel@redhat.com> In-Reply-To: <1433846851-20552-1-git-send-email-kraxel@redhat.com> References: <1433846851-20552-1-git-send-email-kraxel@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 06/12] alsaaudio: do not use global variables List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: "Vassili Karpov (malc)" , Gerd Hoffmann , =?UTF-8?q?K=C5=91v=C3=A1g=C3=B3=2C=20Zolt=C3=A1n?= From: K=C5=91v=C3=A1g=C3=B3, Zolt=C3=A1n Signed-off-by: K=C5=91v=C3=A1g=C3=B3, Zolt=C3=A1n Signed-off-by: Gerd Hoffmann --- 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" =20 -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 =3D { - .buffer_size_out =3D 4096, - .period_size_out =3D 1024, - .pcm_name_out =3D "default", - .pcm_name_in =3D "default", +} ALSAConf; + +struct pollhlp { + snd_pcm_t *handle; + struct pollfd *pfds; + ALSAConf *conf; + int count; + int mask; }; =20 +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 =3D opaque; unsigned short revents; + ALSAConf *conf =3D hlp->conf; =20 count =3D poll (hlp->pfds, hlp->count, 0); if (count < 0) { @@ -205,7 +202,7 @@ static void alsa_poll_handler (void *opaque) } =20 if (!(revents & hlp->mask)) { - if (conf.verbose) { + if (conf->verbose) { dolog ("revents =3D %d\n", revents); } return; @@ -242,6 +239,7 @@ static int alsa_poll_helper (snd_pcm_t *handle, struc= t pollhlp *hlp, int mask) { int i, count, err; struct pollfd *pfds; + ALSAConf *conf =3D hlp->conf; =20 count =3D snd_pcm_poll_descriptors_count (handle); if (count <=3D 0) { @@ -270,13 +268,13 @@ static int alsa_poll_helper (snd_pcm_t *handle, str= uct 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 =3D qemu_set_fd_handler (pfds[i].fd, NULL, alsa_poll_handler, hlp); } - if (conf.verbose) { + if (conf->verbose) { dolog ("Set handler events=3D%#x index=3D%d fd=3D%d err=3D%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) } =20 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 =3D in ? conf.pcm_name_in : conf.pcm_name_out; + const char *pcm_name =3D in ? conf->pcm_name_in : conf->pcm_name_out= ; snd_pcm_uframes_t obt_buffer_size; const char *typ =3D in ? "ADC" : "DAC"; snd_pcm_format_t obtfmt; @@ -522,7 +521,7 @@ static int alsa_open (int in, struct alsa_params_req = *req, } =20 err =3D 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); } =20 @@ -654,7 +653,7 @@ static int alsa_open (int in, struct alsa_params_req = *req, goto err; } =20 - if (!in && conf.threshold) { + if (!in && conf->threshold) { snd_pcm_uframes_t threshold; int bytes_per_sec; =20 @@ -676,7 +675,7 @@ static int alsa_open (int in, struct alsa_params_req = *req, break; } =20 - threshold =3D (conf.threshold * bytes_per_sec) / 1000; + threshold =3D (conf->threshold * bytes_per_sec) / 1000; alsa_set_threshold (handle, threshold); } =20 @@ -686,7 +685,7 @@ static int alsa_open (int in, struct alsa_params_req = *req, =20 *handlep =3D handle; =20 - if (conf.verbose && + if (conf->verbose && (obtfmt !=3D req->fmt || obt->nchannels !=3D req->nchannels || obt->freq !=3D req->freq)) { @@ -729,6 +728,7 @@ static snd_pcm_sframes_t alsa_get_avail (snd_pcm_t *h= andle) static void alsa_write_pending (ALSAVoiceOut *alsa) { HWVoiceOut *hw =3D &alsa->hw; + ALSAConf *conf =3D alsa->pollhlp.conf; =20 while (alsa->pending) { int left_till_end_samples =3D hw->samples - alsa->wpos; @@ -743,7 +743,7 @@ static void alsa_write_pending (ALSAVoiceOut *alsa) if (written <=3D 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 aud= settings *as, struct alsa_params_obt obt; snd_pcm_t *handle; struct audsettings obt_as; + ALSAConf *conf =3D drv_opaque; =20 req.fmt =3D aud_to_alsafmt (as->fmt, as->endianness); req.freq =3D as->freq; req.nchannels =3D as->nchannels; - req.period_size =3D conf.period_size_out; - req.buffer_size =3D conf.buffer_size_out; - req.size_in_usec =3D conf.size_in_usec_out; + req.period_size =3D conf->period_size_out; + req.buffer_size =3D conf->buffer_size_out; + req.size_in_usec =3D conf->size_in_usec_out; req.override_mask =3D - (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); =20 - if (alsa_open (0, &req, &obt, &handle)) { + if (alsa_open (0, &req, &obt, &handle, conf)) { return -1; } =20 @@ -859,6 +860,7 @@ static int alsa_init_out(HWVoiceOut *hw, struct audse= ttings *as, } =20 alsa->handle =3D handle; + alsa->pollhlp.conf =3D conf; return 0; } =20 @@ -936,18 +938,19 @@ static int alsa_init_in(HWVoiceIn *hw, struct audse= ttings *as, void *drv_opaque) struct alsa_params_obt obt; snd_pcm_t *handle; struct audsettings obt_as; + ALSAConf *conf =3D drv_opaque; =20 req.fmt =3D aud_to_alsafmt (as->fmt, as->endianness); req.freq =3D as->freq; req.nchannels =3D as->nchannels; - req.period_size =3D conf.period_size_in; - req.buffer_size =3D conf.buffer_size_in; - req.size_in_usec =3D conf.size_in_usec_in; + req.period_size =3D conf->period_size_in; + req.buffer_size =3D conf->buffer_size_in; + req.size_in_usec =3D conf->size_in_usec_in; req.override_mask =3D - (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); =20 - if (alsa_open (1, &req, &obt, &handle)) { + if (alsa_open (1, &req, &obt, &handle, conf)) { return -1; } =20 @@ -968,6 +971,7 @@ static int alsa_init_in(HWVoiceIn *hw, struct audsett= ings *as, void *drv_opaque) } =20 alsa->handle =3D handle; + alsa->pollhlp.conf =3D conf; return 0; } =20 @@ -998,6 +1002,7 @@ static int alsa_run_in (HWVoiceIn *hw) }; snd_pcm_sframes_t avail; snd_pcm_uframes_t read_samples =3D 0; + ALSAConf *conf =3D alsa->pollhlp.conf; =20 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", sta= te); } return 0; @@ -1065,7 +1070,7 @@ static int alsa_run_in (HWVoiceIn *hw) if (nread <=3D 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; } =20 +static ALSAConf glob_conf =3D { + .buffer_size_out =3D 4096, + .period_size_out =3D 1024, + .pcm_name_out =3D "default", + .pcm_name_in =3D "default", +}; + static void *alsa_audio_init (void) { - return &conf; + ALSAConf *conf =3D g_malloc(sizeof(ALSAConf)); + *conf =3D glob_conf; + return conf; } =20 static void alsa_audio_fini (void *opaque) { - (void) opaque; + g_free(opaque); } =20 static struct audio_option alsa_options[] =3D { { .name =3D "DAC_SIZE_IN_USEC", .tag =3D AUD_OPT_BOOL, - .valp =3D &conf.size_in_usec_out, + .valp =3D &glob_conf.size_in_usec_out, .descr =3D "DAC period/buffer size in microseconds (otherw= ise in frames)" }, { .name =3D "DAC_PERIOD_SIZE", .tag =3D AUD_OPT_INT, - .valp =3D &conf.period_size_out, + .valp =3D &glob_conf.period_size_out, .descr =3D "DAC period size (0 to go with system default)"= , - .overriddenp =3D &conf.period_size_out_overridden + .overriddenp =3D &glob_conf.period_size_out_overridden }, { .name =3D "DAC_BUFFER_SIZE", .tag =3D AUD_OPT_INT, - .valp =3D &conf.buffer_size_out, + .valp =3D &glob_conf.buffer_size_out, .descr =3D "DAC buffer size (0 to go with system default)"= , - .overriddenp =3D &conf.buffer_size_out_overridden + .overriddenp =3D &glob_conf.buffer_size_out_overridden }, { .name =3D "ADC_SIZE_IN_USEC", .tag =3D AUD_OPT_BOOL, - .valp =3D &conf.size_in_usec_in, + .valp =3D &glob_conf.size_in_usec_in, .descr =3D "ADC period/buffer size in microseconds (otherwise in frames)" }, { .name =3D "ADC_PERIOD_SIZE", .tag =3D AUD_OPT_INT, - .valp =3D &conf.period_size_in, + .valp =3D &glob_conf.period_size_in, .descr =3D "ADC period size (0 to go with system default)"= , - .overriddenp =3D &conf.period_size_in_overridden + .overriddenp =3D &glob_conf.period_size_in_overridden }, { .name =3D "ADC_BUFFER_SIZE", .tag =3D AUD_OPT_INT, - .valp =3D &conf.buffer_size_in, + .valp =3D &glob_conf.buffer_size_in, .descr =3D "ADC buffer size (0 to go with system default)"= , - .overriddenp =3D &conf.buffer_size_in_overridden + .overriddenp =3D &glob_conf.buffer_size_in_overridden }, { .name =3D "THRESHOLD", .tag =3D AUD_OPT_INT, - .valp =3D &conf.threshold, + .valp =3D &glob_conf.threshold, .descr =3D "(undocumented)" }, { .name =3D "DAC_DEV", .tag =3D AUD_OPT_STR, - .valp =3D &conf.pcm_name_out, + .valp =3D &glob_conf.pcm_name_out, .descr =3D "DAC device name (for instance dmix)" }, { .name =3D "ADC_DEV", .tag =3D AUD_OPT_STR, - .valp =3D &conf.pcm_name_in, + .valp =3D &glob_conf.pcm_name_in, .descr =3D "ADC device name" }, { .name =3D "VERBOSE", .tag =3D AUD_OPT_BOOL, - .valp =3D &conf.verbose, + .valp =3D &glob_conf.verbose, .descr =3D "Behave in a more verbose way" }, { /* End of list */ } --=20 1.8.3.1