From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49135) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z4TUz-0006lP-LZ for qemu-devel@nongnu.org; Mon, 15 Jun 2015 08:28:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z4TUw-00060E-OI for qemu-devel@nongnu.org; Mon, 15 Jun 2015 08:28:21 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33583) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z4TUw-0005zm-Ds for qemu-devel@nongnu.org; Mon, 15 Jun 2015 08:28:18 -0400 From: Gerd Hoffmann Date: Mon, 15 Jun 2015 14:27:57 +0200 Message-Id: <1434371291-6994-7-git-send-email-kraxel@redhat.com> In-Reply-To: <1434371291-6994-1-git-send-email-kraxel@redhat.com> References: <1434371291-6994-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/20] paaudio: do not use global variables List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: 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/paaudio.c | 98 ++++++++++++++++++++++++++++++---------------------= ------ 1 file changed, 52 insertions(+), 46 deletions(-) diff --git a/audio/paaudio.c b/audio/paaudio.c index bdf6cd5..35e8887 100644 --- a/audio/paaudio.c +++ b/audio/paaudio.c @@ -9,6 +9,19 @@ #include "audio_pt_int.h" =20 typedef struct { + int samples; + char *server; + char *sink; + char *source; +} PAConf; + +typedef struct { + PAConf conf; + pa_threaded_mainloop *mainloop; + pa_context *context; +} paaudio; + +typedef struct { HWVoiceOut hw; int done; int live; @@ -17,6 +30,7 @@ typedef struct { pa_stream *stream; void *pcm_buf; struct audio_pt pt; + paaudio *g; } PAVoiceOut; =20 typedef struct { @@ -30,21 +44,9 @@ typedef struct { struct audio_pt pt; const void *read_data; size_t read_index, read_length; + paaudio *g; } PAVoiceIn; =20 -typedef struct { - int samples; - char *server; - char *sink; - char *source; - pa_threaded_mainloop *mainloop; - pa_context *context; -} paaudio; - -static paaudio glob_paaudio =3D { - .samples =3D 4096, -}; - static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err, const char *fmt, ..= .) { va_list ap; @@ -106,7 +108,7 @@ static inline int PA_STREAM_IS_GOOD(pa_stream_state_t= x) =20 static int qpa_simple_read (PAVoiceIn *p, void *data, size_t length, int= *rerror) { - paaudio *g =3D &glob_paaudio; + paaudio *g =3D p->g; =20 pa_threaded_mainloop_lock (g->mainloop); =20 @@ -160,7 +162,7 @@ unlock_and_fail: =20 static int qpa_simple_write (PAVoiceOut *p, const void *data, size_t len= gth, int *rerror) { - paaudio *g =3D &glob_paaudio; + paaudio *g =3D p->g; =20 pa_threaded_mainloop_lock (g->mainloop); =20 @@ -222,7 +224,7 @@ static void *qpa_thread_out (void *arg) } } =20 - decr =3D to_mix =3D audio_MIN (pa->live, glob_paaudio.samples >>= 2); + decr =3D to_mix =3D audio_MIN (pa->live, pa->g->conf.samples >> = 2); rpos =3D pa->rpos; =20 if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) { @@ -314,7 +316,7 @@ static void *qpa_thread_in (void *arg) } } =20 - incr =3D to_grab =3D audio_MIN (pa->dead, glob_paaudio.samples >= > 2); + incr =3D to_grab =3D audio_MIN (pa->dead, pa->g->conf.samples >>= 2); wpos =3D pa->wpos; =20 if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) { @@ -430,7 +432,7 @@ static audfmt_e pa_to_audfmt (pa_sample_format_t fmt,= int *endianness) =20 static void context_state_cb (pa_context *c, void *userdata) { - paaudio *g =3D &glob_paaudio; + paaudio *g =3D userdata; =20 switch (pa_context_get_state(c)) { case PA_CONTEXT_READY: @@ -449,7 +451,7 @@ static void context_state_cb (pa_context *c, void *us= erdata) =20 static void stream_state_cb (pa_stream *s, void * userdata) { - paaudio *g =3D &glob_paaudio; + paaudio *g =3D userdata; =20 switch (pa_stream_get_state (s)) { =20 @@ -467,23 +469,21 @@ static void stream_state_cb (pa_stream *s, void * u= serdata) =20 static void stream_request_cb (pa_stream *s, size_t length, void *userda= ta) { - paaudio *g =3D &glob_paaudio; + paaudio *g =3D userdata; =20 pa_threaded_mainloop_signal (g->mainloop, 0); } =20 static pa_stream *qpa_simple_new ( - const char *server, + paaudio *g, const char *name, pa_stream_direction_t dir, const char *dev, - const char *stream_name, const pa_sample_spec *ss, const pa_channel_map *map, const pa_buffer_attr *attr, int *rerror) { - paaudio *g =3D &glob_paaudio; int r; pa_stream *stream; =20 @@ -538,10 +538,11 @@ static int qpa_init_out(HWVoiceOut *hw, struct auds= ettings *as, void *drv_opaque) { int error; - static pa_sample_spec ss; - static pa_buffer_attr ba; + pa_sample_spec ss; + pa_buffer_attr ba; struct audsettings obt_as =3D *as; PAVoiceOut *pa =3D (PAVoiceOut *) hw; + paaudio *g =3D pa->g =3D drv_opaque; =20 ss.format =3D audfmt_to_pa (as->fmt, as->endianness); ss.channels =3D as->nchannels; @@ -559,11 +560,10 @@ static int qpa_init_out(HWVoiceOut *hw, struct auds= ettings *as, obt_as.fmt =3D pa_to_audfmt (ss.format, &obt_as.endianness); =20 pa->stream =3D qpa_simple_new ( - glob_paaudio.server, + g, "qemu", PA_STREAM_PLAYBACK, - glob_paaudio.sink, - "pcm.playback", + g->conf.sink, &ss, NULL, /* channel map */ &ba, /* buffering attributes */ @@ -575,7 +575,7 @@ static int qpa_init_out(HWVoiceOut *hw, struct audset= tings *as, } =20 audio_pcm_init_info (&hw->info, &obt_as); - hw->samples =3D glob_paaudio.samples; + hw->samples =3D g->conf.samples; pa->pcm_buf =3D audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info= .shift); pa->rpos =3D hw->rpos; if (!pa->pcm_buf) { @@ -605,9 +605,10 @@ static int qpa_init_out(HWVoiceOut *hw, struct audse= ttings *as, static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_= opaque) { int error; - static pa_sample_spec ss; + pa_sample_spec ss; struct audsettings obt_as =3D *as; PAVoiceIn *pa =3D (PAVoiceIn *) hw; + paaudio *g =3D pa->g =3D drv_opaque; =20 ss.format =3D audfmt_to_pa (as->fmt, as->endianness); ss.channels =3D as->nchannels; @@ -616,11 +617,10 @@ static int qpa_init_in(HWVoiceIn *hw, struct audset= tings *as, void *drv_opaque) obt_as.fmt =3D pa_to_audfmt (ss.format, &obt_as.endianness); =20 pa->stream =3D qpa_simple_new ( - glob_paaudio.server, + g, "qemu", PA_STREAM_RECORD, - glob_paaudio.source, - "pcm.capture", + g->conf.source, &ss, NULL, /* channel map */ NULL, /* buffering attributes */ @@ -632,7 +632,7 @@ static int qpa_init_in(HWVoiceIn *hw, struct audsetti= ngs *as, void *drv_opaque) } =20 audio_pcm_init_info (&hw->info, &obt_as); - hw->samples =3D glob_paaudio.samples; + hw->samples =3D g->conf.samples; pa->pcm_buf =3D audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info= .shift); pa->wpos =3D hw->wpos; if (!pa->pcm_buf) { @@ -704,7 +704,7 @@ static int qpa_ctl_out (HWVoiceOut *hw, int cmd, ...) PAVoiceOut *pa =3D (PAVoiceOut *) hw; pa_operation *op; pa_cvolume v; - paaudio *g =3D &glob_paaudio; + paaudio *g =3D pa->g; =20 #ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */ pa_cvolume_init (&v); /* function is present in 0.9.13+ */ @@ -756,7 +756,7 @@ static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...) PAVoiceIn *pa =3D (PAVoiceIn *) hw; pa_operation *op; pa_cvolume v; - paaudio *g =3D &glob_paaudio; + paaudio *g =3D pa->g; =20 #ifdef PA_CHECK_VERSION pa_cvolume_init (&v); @@ -806,23 +806,29 @@ static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...) } =20 /* common */ +static PAConf glob_conf =3D { + .samples =3D 4096, +}; + static void *qpa_audio_init (void) { - paaudio *g =3D &glob_paaudio; + paaudio *g =3D g_malloc(sizeof(paaudio)); + g->conf =3D glob_conf; =20 g->mainloop =3D pa_threaded_mainloop_new (); if (!g->mainloop) { goto fail; } =20 - g->context =3D pa_context_new (pa_threaded_mainloop_get_api (g->main= loop), glob_paaudio.server); + g->context =3D pa_context_new (pa_threaded_mainloop_get_api (g->main= loop), + g->conf.server); if (!g->context) { goto fail; } =20 pa_context_set_state_callback (g->context, context_state_cb, g); =20 - if (pa_context_connect (g->context, glob_paaudio.server, 0, NULL) < = 0) { + if (pa_context_connect (g->context, g->conf.server, 0, NULL) < 0) { qpa_logerr (pa_context_errno (g->context), "pa_context_connect() failed\n"); goto fail; @@ -855,12 +861,13 @@ static void *qpa_audio_init (void) =20 pa_threaded_mainloop_unlock (g->mainloop); =20 - return &glob_paaudio; + return g; =20 unlock_and_fail: pa_threaded_mainloop_unlock (g->mainloop); fail: AUD_log (AUDIO_CAP, "Failed to initialize PA context"); + g_free(g); return NULL; } =20 @@ -875,39 +882,38 @@ static void qpa_audio_fini (void *opaque) if (g->context) { pa_context_disconnect (g->context); pa_context_unref (g->context); - g->context =3D NULL; } =20 if (g->mainloop) { pa_threaded_mainloop_free (g->mainloop); } =20 - g->mainloop =3D NULL; + g_free(g); } =20 struct audio_option qpa_options[] =3D { { .name =3D "SAMPLES", .tag =3D AUD_OPT_INT, - .valp =3D &glob_paaudio.samples, + .valp =3D &glob_conf.samples, .descr =3D "buffer size in samples" }, { .name =3D "SERVER", .tag =3D AUD_OPT_STR, - .valp =3D &glob_paaudio.server, + .valp =3D &glob_conf.server, .descr =3D "server address" }, { .name =3D "SINK", .tag =3D AUD_OPT_STR, - .valp =3D &glob_paaudio.sink, + .valp =3D &glob_conf.sink, .descr =3D "sink device name" }, { .name =3D "SOURCE", .tag =3D AUD_OPT_STR, - .valp =3D &glob_paaudio.source, + .valp =3D &glob_conf.source, .descr =3D "source device name" }, { /* End of list */ } --=20 1.8.3.1