/* public domain */
+#include "qemu/osdep.h"
#include "qemu-common.h"
#include "audio.h"
#include "audio_int.h"
#include "audio_pt_int.h"
+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;
pa_stream *stream;
void *pcm_buf;
struct audio_pt pt;
+ paaudio *g;
} PAVoiceOut;
typedef struct {
struct audio_pt pt;
const void *read_data;
size_t read_index, read_length;
+ paaudio *g;
} PAVoiceIn;
-typedef struct {
- int samples;
- char *server;
- char *sink;
- char *source;
- pa_threaded_mainloop *mainloop;
- pa_context *context;
-} paaudio;
-
-static paaudio glob_paaudio = {
- .samples = 4096,
-};
+static void qpa_audio_fini(void *opaque);
static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err, const char *fmt, ...)
{
} \
goto label; \
} \
- } while (0);
+ } while (0)
#define CHECK_DEAD_GOTO(c, stream, rerror, label) \
do { \
} \
goto label; \
} \
- } while (0);
+ } while (0)
static int qpa_simple_read (PAVoiceIn *p, void *data, size_t length, int *rerror)
{
- paaudio *g = &glob_paaudio;
+ paaudio *g = p->g;
pa_threaded_mainloop_lock (g->mainloop);
static int qpa_simple_write (PAVoiceOut *p, const void *data, size_t length, int *rerror)
{
- paaudio *g = &glob_paaudio;
+ paaudio *g = p->g;
pa_threaded_mainloop_lock (g->mainloop);
PAVoiceOut *pa = arg;
HWVoiceOut *hw = &pa->hw;
- if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
+ if (audio_pt_lock(&pa->pt, __func__)) {
return NULL;
}
break;
}
- if (audio_pt_wait (&pa->pt, AUDIO_FUNC)) {
+ if (audio_pt_wait(&pa->pt, __func__)) {
goto exit;
}
}
- decr = to_mix = audio_MIN (pa->live, glob_paaudio.samples >> 2);
+ decr = to_mix = audio_MIN (pa->live, pa->g->conf.samples >> 2);
rpos = pa->rpos;
- if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
+ if (audio_pt_unlock(&pa->pt, __func__)) {
return NULL;
}
to_mix -= chunk;
}
- if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
+ if (audio_pt_lock(&pa->pt, __func__)) {
return NULL;
}
}
exit:
- audio_pt_unlock (&pa->pt, AUDIO_FUNC);
+ audio_pt_unlock(&pa->pt, __func__);
return NULL;
}
int decr;
PAVoiceOut *pa = (PAVoiceOut *) hw;
- if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
+ if (audio_pt_lock(&pa->pt, __func__)) {
return 0;
}
pa->live = live - decr;
hw->rpos = pa->rpos;
if (pa->live > 0) {
- audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
+ audio_pt_unlock_and_signal(&pa->pt, __func__);
}
else {
- audio_pt_unlock (&pa->pt, AUDIO_FUNC);
+ audio_pt_unlock(&pa->pt, __func__);
}
return decr;
}
PAVoiceIn *pa = arg;
HWVoiceIn *hw = &pa->hw;
- if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
+ if (audio_pt_lock(&pa->pt, __func__)) {
return NULL;
}
break;
}
- if (audio_pt_wait (&pa->pt, AUDIO_FUNC)) {
+ if (audio_pt_wait(&pa->pt, __func__)) {
goto exit;
}
}
- incr = to_grab = audio_MIN (pa->dead, glob_paaudio.samples >> 2);
+ incr = to_grab = audio_MIN (pa->dead, pa->g->conf.samples >> 2);
wpos = pa->wpos;
- if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
+ if (audio_pt_unlock(&pa->pt, __func__)) {
return NULL;
}
to_grab -= chunk;
}
- if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
+ if (audio_pt_lock(&pa->pt, __func__)) {
return NULL;
}
}
exit:
- audio_pt_unlock (&pa->pt, AUDIO_FUNC);
+ audio_pt_unlock(&pa->pt, __func__);
return NULL;
}
int live, incr, dead;
PAVoiceIn *pa = (PAVoiceIn *) hw;
- if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
+ if (audio_pt_lock(&pa->pt, __func__)) {
return 0;
}
pa->dead = dead - incr;
hw->wpos = pa->wpos;
if (pa->dead > 0) {
- audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
+ audio_pt_unlock_and_signal(&pa->pt, __func__);
}
else {
- audio_pt_unlock (&pa->pt, AUDIO_FUNC);
+ audio_pt_unlock(&pa->pt, __func__);
}
return incr;
}
static void context_state_cb (pa_context *c, void *userdata)
{
- paaudio *g = &glob_paaudio;
+ paaudio *g = userdata;
switch (pa_context_get_state(c)) {
case PA_CONTEXT_READY:
static void stream_state_cb (pa_stream *s, void * userdata)
{
- paaudio *g = &glob_paaudio;
+ paaudio *g = userdata;
switch (pa_stream_get_state (s)) {
static void stream_request_cb (pa_stream *s, size_t length, void *userdata)
{
- paaudio *g = &glob_paaudio;
+ paaudio *g = userdata;
pa_threaded_mainloop_signal (g->mainloop, 0);
}
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 = &glob_paaudio;
int r;
pa_stream *stream;
return NULL;
}
-static int qpa_init_out (HWVoiceOut *hw, struct audsettings *as)
+static int qpa_init_out(HWVoiceOut *hw, struct audsettings *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 = *as;
PAVoiceOut *pa = (PAVoiceOut *) hw;
+ paaudio *g = pa->g = drv_opaque;
ss.format = audfmt_to_pa (as->fmt, as->endianness);
ss.channels = as->nchannels;
obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
pa->stream = 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 */
}
audio_pcm_init_info (&hw->info, &obt_as);
- hw->samples = glob_paaudio.samples;
- pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
+ hw->samples = g->conf.samples;
+ pa->pcm_buf = audio_calloc(__func__, hw->samples, 1 << hw->info.shift);
pa->rpos = hw->rpos;
if (!pa->pcm_buf) {
dolog ("Could not allocate buffer (%d bytes)\n",
goto fail2;
}
- if (audio_pt_init (&pa->pt, qpa_thread_out, hw, AUDIO_CAP, AUDIO_FUNC)) {
+ if (audio_pt_init(&pa->pt, qpa_thread_out, hw, AUDIO_CAP, __func__)) {
goto fail3;
}
return -1;
}
-static int qpa_init_in (HWVoiceIn *hw, struct audsettings *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 = *as;
PAVoiceIn *pa = (PAVoiceIn *) hw;
+ paaudio *g = pa->g = drv_opaque;
ss.format = audfmt_to_pa (as->fmt, as->endianness);
ss.channels = as->nchannels;
obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
pa->stream = 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 */
}
audio_pcm_init_info (&hw->info, &obt_as);
- hw->samples = glob_paaudio.samples;
- pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
+ hw->samples = g->conf.samples;
+ pa->pcm_buf = audio_calloc(__func__, hw->samples, 1 << hw->info.shift);
pa->wpos = hw->wpos;
if (!pa->pcm_buf) {
dolog ("Could not allocate buffer (%d bytes)\n",
goto fail2;
}
- if (audio_pt_init (&pa->pt, qpa_thread_in, hw, AUDIO_CAP, AUDIO_FUNC)) {
+ if (audio_pt_init(&pa->pt, qpa_thread_in, hw, AUDIO_CAP, __func__)) {
goto fail3;
}
void *ret;
PAVoiceOut *pa = (PAVoiceOut *) hw;
- audio_pt_lock (&pa->pt, AUDIO_FUNC);
+ audio_pt_lock(&pa->pt, __func__);
pa->done = 1;
- audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
- audio_pt_join (&pa->pt, &ret, AUDIO_FUNC);
+ audio_pt_unlock_and_signal(&pa->pt, __func__);
+ audio_pt_join(&pa->pt, &ret, __func__);
if (pa->stream) {
pa_stream_unref (pa->stream);
pa->stream = NULL;
}
- audio_pt_fini (&pa->pt, AUDIO_FUNC);
+ audio_pt_fini(&pa->pt, __func__);
g_free (pa->pcm_buf);
pa->pcm_buf = NULL;
}
void *ret;
PAVoiceIn *pa = (PAVoiceIn *) hw;
- audio_pt_lock (&pa->pt, AUDIO_FUNC);
+ audio_pt_lock(&pa->pt, __func__);
pa->done = 1;
- audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
- audio_pt_join (&pa->pt, &ret, AUDIO_FUNC);
+ audio_pt_unlock_and_signal(&pa->pt, __func__);
+ audio_pt_join(&pa->pt, &ret, __func__);
if (pa->stream) {
pa_stream_unref (pa->stream);
pa->stream = NULL;
}
- audio_pt_fini (&pa->pt, AUDIO_FUNC);
+ audio_pt_fini(&pa->pt, __func__);
g_free (pa->pcm_buf);
pa->pcm_buf = NULL;
}
PAVoiceOut *pa = (PAVoiceOut *) hw;
pa_operation *op;
pa_cvolume v;
- paaudio *g = &glob_paaudio;
+ paaudio *g = pa->g;
#ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */
pa_cvolume_init (&v); /* function is present in 0.9.13+ */
PAVoiceIn *pa = (PAVoiceIn *) hw;
pa_operation *op;
pa_cvolume v;
- paaudio *g = &glob_paaudio;
+ paaudio *g = pa->g;
#ifdef PA_CHECK_VERSION
pa_cvolume_init (&v);
pa_threaded_mainloop_lock (g->mainloop);
- /* FIXME: use the upcoming "set_source_output_{volume,mute}" */
- op = pa_context_set_source_volume_by_index (g->context,
- pa_stream_get_device_index (pa->stream),
+ op = pa_context_set_source_output_volume (g->context,
+ pa_stream_get_index (pa->stream),
&v, NULL, NULL);
if (!op) {
qpa_logerr (pa_context_errno (g->context),
- "set_source_volume() failed\n");
+ "set_source_output_volume() failed\n");
} else {
pa_operation_unref(op);
}
- op = pa_context_set_source_mute_by_index (g->context,
+ op = pa_context_set_source_output_mute (g->context,
pa_stream_get_index (pa->stream),
sw->vol.mute, NULL, NULL);
if (!op) {
qpa_logerr (pa_context_errno (g->context),
- "set_source_mute() failed\n");
+ "set_source_output_mute() failed\n");
} else {
pa_operation_unref (op);
}
}
/* common */
+static PAConf glob_conf = {
+ .samples = 4096,
+};
+
static void *qpa_audio_init (void)
{
- paaudio *g = &glob_paaudio;
+ paaudio *g = g_malloc(sizeof(paaudio));
+ g->conf = glob_conf;
+ g->mainloop = NULL;
+ g->context = NULL;
g->mainloop = pa_threaded_mainloop_new ();
if (!g->mainloop) {
goto fail;
}
- g->context = pa_context_new (pa_threaded_mainloop_get_api (g->mainloop), glob_paaudio.server);
+ g->context = pa_context_new (pa_threaded_mainloop_get_api (g->mainloop),
+ g->conf.server);
if (!g->context) {
goto fail;
}
pa_context_set_state_callback (g->context, context_state_cb, g);
- 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;
pa_threaded_mainloop_unlock (g->mainloop);
- return &glob_paaudio;
+ return g;
unlock_and_fail:
pa_threaded_mainloop_unlock (g->mainloop);
fail:
AUD_log (AUDIO_CAP, "Failed to initialize PA context");
+ qpa_audio_fini(g);
return NULL;
}
if (g->context) {
pa_context_disconnect (g->context);
pa_context_unref (g->context);
- g->context = NULL;
}
if (g->mainloop) {
pa_threaded_mainloop_free (g->mainloop);
}
- g->mainloop = NULL;
+ g_free(g);
}
struct audio_option qpa_options[] = {
{
.name = "SAMPLES",
.tag = AUD_OPT_INT,
- .valp = &glob_paaudio.samples,
+ .valp = &glob_conf.samples,
.descr = "buffer size in samples"
},
{
.name = "SERVER",
.tag = AUD_OPT_STR,
- .valp = &glob_paaudio.server,
+ .valp = &glob_conf.server,
.descr = "server address"
},
{
.name = "SINK",
.tag = AUD_OPT_STR,
- .valp = &glob_paaudio.sink,
+ .valp = &glob_conf.sink,
.descr = "sink device name"
},
{
.name = "SOURCE",
.tag = AUD_OPT_STR,
- .valp = &glob_paaudio.source,
+ .valp = &glob_conf.source,
.descr = "source device name"
},
{ /* End of list */ }
.ctl_in = qpa_ctl_in
};
-struct audio_driver pa_audio_driver = {
+static struct audio_driver pa_audio_driver = {
.name = "pa",
.descr = "http://www.pulseaudio.org/",
.options = qpa_options,
.voice_size_in = sizeof (PAVoiceIn),
.ctl_caps = VOICE_VOLUME_CAP
};
+
+static void register_audio_pa(void)
+{
+ audio_driver_register(&pa_audio_driver);
+}
+type_init(register_audio_pa);