2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-04-16 20:51:21 +02:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2006-07-13 21:20:34 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2006-07-13 21:20:34 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-04-16 20:51:21 +02:00
|
|
|
#include "PulseOutputPlugin.hxx"
|
2013-07-30 08:34:10 +02:00
|
|
|
#include "OutputAPI.hxx"
|
2013-02-22 20:51:23 +01:00
|
|
|
#include "MixerList.hxx"
|
2013-04-16 20:51:21 +02:00
|
|
|
#include "mixer/PulseMixerPlugin.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2006-07-19 18:48:24 +02:00
|
|
|
|
2008-10-29 20:37:36 +01:00
|
|
|
#include <glib.h>
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
#include <pulse/thread-mainloop.h>
|
|
|
|
#include <pulse/context.h>
|
|
|
|
#include <pulse/stream.h>
|
2009-10-23 10:33:26 +02:00
|
|
|
#include <pulse/introspect.h>
|
|
|
|
#include <pulse/subscribe.h>
|
2009-10-21 10:30:42 +02:00
|
|
|
#include <pulse/error.h>
|
2011-09-17 19:50:36 +02:00
|
|
|
#include <pulse/version.h>
|
2006-07-19 18:48:24 +02:00
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
#include <assert.h>
|
2011-09-17 19:50:36 +02:00
|
|
|
#include <stddef.h>
|
2008-09-24 07:20:55 +02:00
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
#define MPD_PULSE_NAME "Music Player Daemon"
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
struct PulseOutput {
|
2011-09-16 23:31:48 +02:00
|
|
|
struct audio_output base;
|
|
|
|
|
2011-09-17 19:50:36 +02:00
|
|
|
const char *name;
|
|
|
|
const char *server;
|
|
|
|
const char *sink;
|
|
|
|
|
2013-04-16 20:47:55 +02:00
|
|
|
PulseMixer *mixer;
|
2011-09-17 19:50:36 +02:00
|
|
|
|
|
|
|
struct pa_threaded_mainloop *mainloop;
|
|
|
|
struct pa_context *context;
|
|
|
|
struct pa_stream *stream;
|
|
|
|
|
|
|
|
size_t writable;
|
|
|
|
};
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain pulse_output_domain("pulse_output");
|
|
|
|
|
|
|
|
static void
|
|
|
|
SetError(Error &error, pa_context *context, const char *msg)
|
2009-02-26 22:04:59 +01:00
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
const int e = pa_context_errno(context);
|
|
|
|
error.Format(pulse_output_domain, e, "%s: %s", msg, pa_strerror(e));
|
2009-02-26 22:04:59 +01:00
|
|
|
}
|
|
|
|
|
2011-09-17 19:50:36 +02:00
|
|
|
void
|
2013-04-16 20:51:21 +02:00
|
|
|
pulse_output_lock(PulseOutput *po)
|
2011-09-17 19:50:36 +02:00
|
|
|
{
|
|
|
|
pa_threaded_mainloop_lock(po->mainloop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-16 20:51:21 +02:00
|
|
|
pulse_output_unlock(PulseOutput *po)
|
2011-09-17 19:50:36 +02:00
|
|
|
{
|
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
|
|
|
}
|
|
|
|
|
2009-10-23 10:33:26 +02:00
|
|
|
void
|
2013-04-16 20:47:55 +02:00
|
|
|
pulse_output_set_mixer(PulseOutput *po, PulseMixer *pm)
|
2009-10-23 10:33:26 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po != nullptr);
|
|
|
|
assert(po->mixer == nullptr);
|
|
|
|
assert(pm != nullptr);
|
2009-10-23 10:33:26 +02:00
|
|
|
|
|
|
|
po->mixer = pm;
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po->mainloop == nullptr)
|
2009-10-23 10:56:16 +02:00
|
|
|
return;
|
|
|
|
|
2009-10-23 10:33:26 +02:00
|
|
|
pa_threaded_mainloop_lock(po->mainloop);
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po->context != nullptr &&
|
2009-10-23 10:33:26 +02:00
|
|
|
pa_context_get_state(po->context) == PA_CONTEXT_READY) {
|
|
|
|
pulse_mixer_on_connect(pm, po->context);
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po->stream != nullptr &&
|
2009-10-23 10:33:26 +02:00
|
|
|
pa_stream_get_state(po->stream) == PA_STREAM_READY)
|
|
|
|
pulse_mixer_on_change(pm, po->context, po->stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-16 20:47:55 +02:00
|
|
|
pulse_output_clear_mixer(PulseOutput *po, gcc_unused PulseMixer *pm)
|
2009-10-23 10:33:26 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po != nullptr);
|
|
|
|
assert(pm != nullptr);
|
2009-10-23 10:33:26 +02:00
|
|
|
assert(po->mixer == pm);
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
po->mixer = nullptr;
|
2009-10-23 10:33:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-08-10 18:02:44 +02:00
|
|
|
pulse_output_set_volume(PulseOutput *po, const pa_cvolume *volume,
|
|
|
|
Error &error)
|
2009-10-23 10:33:26 +02:00
|
|
|
{
|
|
|
|
pa_operation *o;
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po->context == nullptr || po->stream == nullptr ||
|
2009-10-23 10:33:26 +02:00
|
|
|
pa_stream_get_state(po->stream) != PA_STREAM_READY) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(pulse_output_domain, "disconnected");
|
2009-10-23 10:33:26 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
o = pa_context_set_sink_input_volume(po->context,
|
|
|
|
pa_stream_get_index(po->stream),
|
2013-04-16 20:51:21 +02:00
|
|
|
volume, nullptr, nullptr);
|
|
|
|
if (o == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
SetError(error, po->context,
|
|
|
|
"failed to set PulseAudio volume");
|
2009-10-23 10:33:26 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_operation_unref(o);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
/**
|
|
|
|
* \brief waits for a pulseaudio operation to finish, frees it and
|
|
|
|
* unlocks the mainloop
|
|
|
|
* \param operation the operation to wait for
|
|
|
|
* \return true if operation has finished normally (DONE state),
|
|
|
|
* false otherwise
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
pulse_wait_for_operation(struct pa_threaded_mainloop *mainloop,
|
|
|
|
struct pa_operation *operation)
|
|
|
|
{
|
|
|
|
pa_operation_state_t state;
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(mainloop != nullptr);
|
|
|
|
assert(operation != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
state = pa_operation_get_state(operation);
|
|
|
|
while (state == PA_OPERATION_RUNNING) {
|
|
|
|
pa_threaded_mainloop_wait(mainloop);
|
|
|
|
state = pa_operation_get_state(operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_operation_unref(operation);
|
|
|
|
|
|
|
|
return state == PA_OPERATION_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function for stream operation. It just sends a signal to
|
|
|
|
* the caller thread, to wake pulse_wait_for_operation() up.
|
|
|
|
*/
|
|
|
|
static void
|
2013-08-04 23:48:01 +02:00
|
|
|
pulse_output_stream_success_cb(gcc_unused pa_stream *s,
|
|
|
|
gcc_unused int success, void *userdata)
|
2009-10-21 10:30:42 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)userdata;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
pa_threaded_mainloop_signal(po->mainloop, 0);
|
|
|
|
}
|
|
|
|
|
2009-10-23 10:33:26 +02:00
|
|
|
static void
|
2009-10-21 10:30:42 +02:00
|
|
|
pulse_output_context_state_cb(struct pa_context *context, void *userdata)
|
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)userdata;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
switch (pa_context_get_state(context)) {
|
|
|
|
case PA_CONTEXT_READY:
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po->mixer != nullptr)
|
2009-10-23 10:33:26 +02:00
|
|
|
pulse_mixer_on_connect(po->mixer, context);
|
|
|
|
|
|
|
|
pa_threaded_mainloop_signal(po->mainloop, 0);
|
|
|
|
break;
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
case PA_CONTEXT_TERMINATED:
|
|
|
|
case PA_CONTEXT_FAILED:
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po->mixer != nullptr)
|
2009-10-23 10:33:26 +02:00
|
|
|
pulse_mixer_on_disconnect(po->mixer);
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
/* the caller thread might be waiting for these
|
|
|
|
states */
|
|
|
|
pa_threaded_mainloop_signal(po->mainloop, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_CONTEXT_UNCONNECTED:
|
|
|
|
case PA_CONTEXT_CONNECTING:
|
|
|
|
case PA_CONTEXT_AUTHORIZING:
|
|
|
|
case PA_CONTEXT_SETTING_NAME:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-23 10:33:26 +02:00
|
|
|
static void
|
|
|
|
pulse_output_subscribe_cb(pa_context *context,
|
|
|
|
pa_subscription_event_type_t t,
|
|
|
|
uint32_t idx, void *userdata)
|
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)userdata;
|
|
|
|
pa_subscription_event_type_t facility =
|
|
|
|
pa_subscription_event_type_t(t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK);
|
|
|
|
pa_subscription_event_type_t type =
|
|
|
|
pa_subscription_event_type_t(t & PA_SUBSCRIPTION_EVENT_TYPE_MASK);
|
2009-10-23 10:33:26 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po->mixer != nullptr &&
|
2009-10-23 10:33:26 +02:00
|
|
|
facility == PA_SUBSCRIPTION_EVENT_SINK_INPUT &&
|
2013-04-16 20:51:21 +02:00
|
|
|
po->stream != nullptr &&
|
2009-10-23 10:33:26 +02:00
|
|
|
pa_stream_get_state(po->stream) == PA_STREAM_READY &&
|
|
|
|
idx == pa_stream_get_index(po->stream) &&
|
|
|
|
(type == PA_SUBSCRIPTION_EVENT_NEW ||
|
|
|
|
type == PA_SUBSCRIPTION_EVENT_CHANGE))
|
|
|
|
pulse_mixer_on_change(po->mixer, context, po->stream);
|
|
|
|
}
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
/**
|
|
|
|
* Attempt to connect asynchronously to the PulseAudio server.
|
|
|
|
*
|
|
|
|
* @return true on success, false on error
|
|
|
|
*/
|
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
pulse_output_connect(PulseOutput *po, Error &error)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po != nullptr);
|
|
|
|
assert(po->context != nullptr);
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (pa_context_connect(po->context, po->server,
|
|
|
|
(pa_context_flags_t)0, nullptr) < 0) {
|
|
|
|
SetError(error, po->context,
|
|
|
|
"pa_context_connect() has failed");
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
2006-07-19 18:48:24 +02:00
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
2006-07-19 18:48:24 +02:00
|
|
|
|
2011-08-31 21:00:55 +02:00
|
|
|
/**
|
|
|
|
* Frees and clears the stream.
|
|
|
|
*/
|
|
|
|
static void
|
2013-04-16 20:51:21 +02:00
|
|
|
pulse_output_delete_stream(PulseOutput *po)
|
2011-08-31 21:00:55 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po != nullptr);
|
|
|
|
assert(po->stream != nullptr);
|
2011-08-31 21:00:55 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
pa_stream_set_suspended_callback(po->stream, nullptr, nullptr);
|
2011-08-31 20:55:49 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
pa_stream_set_state_callback(po->stream, nullptr, nullptr);
|
|
|
|
pa_stream_set_write_callback(po->stream, nullptr, nullptr);
|
2011-08-31 20:55:49 +02:00
|
|
|
|
2011-08-31 21:00:55 +02:00
|
|
|
pa_stream_disconnect(po->stream);
|
|
|
|
pa_stream_unref(po->stream);
|
2013-04-16 20:51:21 +02:00
|
|
|
po->stream = nullptr;
|
2011-08-31 21:00:55 +02:00
|
|
|
}
|
|
|
|
|
2011-08-31 20:58:36 +02:00
|
|
|
/**
|
|
|
|
* Frees and clears the context.
|
2011-09-16 22:16:14 +02:00
|
|
|
*
|
|
|
|
* Caller must lock the main loop.
|
2011-08-31 20:58:36 +02:00
|
|
|
*/
|
|
|
|
static void
|
2013-04-16 20:51:21 +02:00
|
|
|
pulse_output_delete_context(PulseOutput *po)
|
2011-08-31 20:58:36 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po != nullptr);
|
|
|
|
assert(po->context != nullptr);
|
2011-08-31 20:58:36 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
pa_context_set_state_callback(po->context, nullptr, nullptr);
|
|
|
|
pa_context_set_subscribe_callback(po->context, nullptr, nullptr);
|
2011-08-31 20:55:49 +02:00
|
|
|
|
2011-08-31 20:58:36 +02:00
|
|
|
pa_context_disconnect(po->context);
|
|
|
|
pa_context_unref(po->context);
|
2013-04-16 20:51:21 +02:00
|
|
|
po->context = nullptr;
|
2011-08-31 20:58:36 +02:00
|
|
|
}
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
/**
|
|
|
|
* Create, set up and connect a context.
|
|
|
|
*
|
2011-09-16 22:16:14 +02:00
|
|
|
* Caller must lock the main loop.
|
|
|
|
*
|
2009-10-21 10:30:42 +02:00
|
|
|
* @return true on success, false on error
|
|
|
|
*/
|
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
pulse_output_setup_context(PulseOutput *po, Error &error)
|
2009-10-21 10:30:42 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po != nullptr);
|
|
|
|
assert(po->mainloop != nullptr);
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
po->context = pa_context_new(pa_threaded_mainloop_get_api(po->mainloop),
|
|
|
|
MPD_PULSE_NAME);
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po->context == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(pulse_output_domain, "pa_context_new() has failed");
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_context_set_state_callback(po->context,
|
|
|
|
pulse_output_context_state_cb, po);
|
2009-10-23 10:33:26 +02:00
|
|
|
pa_context_set_subscribe_callback(po->context,
|
|
|
|
pulse_output_subscribe_cb, po);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!pulse_output_connect(po, error)) {
|
2011-08-31 20:58:36 +02:00
|
|
|
pulse_output_delete_context(po);
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
static struct audio_output *
|
2013-08-10 18:02:44 +02:00
|
|
|
pulse_output_init(const config_param ¶m, Error &error)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po;
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2009-10-20 21:02:10 +02:00
|
|
|
g_setenv("PULSE_PROP_media.role", "music", true);
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
po = new PulseOutput();
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!ao_base_init(&po->base, &pulse_output_plugin, param, error)) {
|
2013-04-16 20:51:21 +02:00
|
|
|
delete po;
|
|
|
|
return nullptr;
|
2011-09-16 23:31:48 +02:00
|
|
|
}
|
|
|
|
|
2013-08-04 12:25:08 +02:00
|
|
|
po->name = param.GetBlockValue("name", "mpd_pulse");
|
|
|
|
po->server = param.GetBlockValue("server");
|
|
|
|
po->sink = param.GetBlockValue("sink");
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
po->mixer = nullptr;
|
|
|
|
po->mainloop = nullptr;
|
|
|
|
po->context = nullptr;
|
|
|
|
po->stream = nullptr;
|
2009-10-23 10:56:16 +02:00
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
return &po->base;
|
2009-10-23 10:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
pulse_output_finish(struct audio_output *ao)
|
2009-10-23 10:56:16 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)ao;
|
2009-10-23 10:56:16 +02:00
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
ao_base_finish(&po->base);
|
2013-04-16 20:51:21 +02:00
|
|
|
delete po;
|
2009-10-23 10:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
pulse_output_enable(struct audio_output *ao, Error &error)
|
2009-10-23 10:56:16 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)ao;
|
2009-10-23 10:56:16 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po->mainloop == nullptr);
|
|
|
|
assert(po->context == nullptr);
|
2009-10-23 10:56:16 +02:00
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
/* create the libpulse mainloop and start the thread */
|
|
|
|
|
|
|
|
po->mainloop = pa_threaded_mainloop_new();
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po->mainloop == nullptr) {
|
2009-10-21 10:30:42 +02:00
|
|
|
g_free(po);
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(pulse_output_domain,
|
|
|
|
"pa_threaded_mainloop_new() has failed");
|
2009-10-23 10:56:16 +02:00
|
|
|
return false;
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(po->mainloop);
|
|
|
|
|
|
|
|
if (pa_threaded_mainloop_start(po->mainloop) < 0) {
|
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
|
|
|
pa_threaded_mainloop_free(po->mainloop);
|
2013-04-16 20:51:21 +02:00
|
|
|
po->mainloop = nullptr;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(pulse_output_domain,
|
|
|
|
"pa_threaded_mainloop_start() has failed");
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create the libpulse context and connect it */
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!pulse_output_setup_context(po, error)) {
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
|
|
|
pa_threaded_mainloop_stop(po->mainloop);
|
|
|
|
pa_threaded_mainloop_free(po->mainloop);
|
2013-04-16 20:51:21 +02:00
|
|
|
po->mainloop = nullptr;
|
2009-10-23 10:56:16 +02:00
|
|
|
return false;
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
|
|
|
|
2009-10-23 10:56:16 +02:00
|
|
|
return true;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
pulse_output_disable(struct audio_output *ao)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)ao;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po->mainloop != nullptr);
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_threaded_mainloop_stop(po->mainloop);
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po->context != nullptr)
|
2009-10-21 10:30:42 +02:00
|
|
|
pulse_output_delete_context(po);
|
|
|
|
pa_threaded_mainloop_free(po->mainloop);
|
2013-04-16 20:51:21 +02:00
|
|
|
po->mainloop = nullptr;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
/**
|
|
|
|
* Check if the context is (already) connected, and waits if not. If
|
|
|
|
* the context has been disconnected, retry to connect.
|
|
|
|
*
|
2011-09-16 22:16:14 +02:00
|
|
|
* Caller must lock the main loop.
|
|
|
|
*
|
2009-10-21 10:30:42 +02:00
|
|
|
* @return true on success, false on error
|
|
|
|
*/
|
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
pulse_output_wait_connection(PulseOutput *po, Error &error)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po->mainloop != nullptr);
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_context_state_t state;
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (po->context == nullptr && !pulse_output_setup_context(po, error))
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
state = pa_context_get_state(po->context);
|
|
|
|
switch (state) {
|
|
|
|
case PA_CONTEXT_READY:
|
|
|
|
/* nothing to do */
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case PA_CONTEXT_UNCONNECTED:
|
|
|
|
case PA_CONTEXT_TERMINATED:
|
|
|
|
case PA_CONTEXT_FAILED:
|
|
|
|
/* failure */
|
2013-08-10 18:02:44 +02:00
|
|
|
SetError(error, po->context, "failed to connect");
|
2009-10-21 10:30:42 +02:00
|
|
|
pulse_output_delete_context(po);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case PA_CONTEXT_CONNECTING:
|
|
|
|
case PA_CONTEXT_AUTHORIZING:
|
|
|
|
case PA_CONTEXT_SETTING_NAME:
|
|
|
|
/* wait some more */
|
|
|
|
pa_threaded_mainloop_wait(po->mainloop);
|
|
|
|
break;
|
|
|
|
}
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2011-08-23 22:43:08 +02:00
|
|
|
static void
|
2013-08-04 23:48:01 +02:00
|
|
|
pulse_output_stream_suspended_cb(gcc_unused pa_stream *stream, void *userdata)
|
2011-08-23 22:43:08 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)userdata;
|
2011-08-23 22:43:08 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(stream == po->stream || po->stream == nullptr);
|
|
|
|
assert(po->mainloop != nullptr);
|
2011-08-23 22:43:08 +02:00
|
|
|
|
|
|
|
/* wake up the main loop to break out of the loop in
|
|
|
|
pulse_output_play() */
|
|
|
|
pa_threaded_mainloop_signal(po->mainloop, 0);
|
|
|
|
}
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
static void
|
|
|
|
pulse_output_stream_state_cb(pa_stream *stream, void *userdata)
|
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)userdata;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(stream == po->stream || po->stream == nullptr);
|
|
|
|
assert(po->mainloop != nullptr);
|
|
|
|
assert(po->context != nullptr);
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
switch (pa_stream_get_state(stream)) {
|
|
|
|
case PA_STREAM_READY:
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po->mixer != nullptr)
|
2009-10-23 10:33:26 +02:00
|
|
|
pulse_mixer_on_change(po->mixer, po->context, stream);
|
|
|
|
|
|
|
|
pa_threaded_mainloop_signal(po->mainloop, 0);
|
|
|
|
break;
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
case PA_STREAM_FAILED:
|
|
|
|
case PA_STREAM_TERMINATED:
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po->mixer != nullptr)
|
2009-10-23 10:33:26 +02:00
|
|
|
pulse_mixer_on_disconnect(po->mixer);
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_threaded_mainloop_signal(po->mainloop, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_STREAM_UNCONNECTED:
|
|
|
|
case PA_STREAM_CREATING:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
static void
|
2013-08-04 23:48:01 +02:00
|
|
|
pulse_output_stream_write_cb(gcc_unused pa_stream *stream, size_t nbytes,
|
2009-10-21 10:30:42 +02:00
|
|
|
void *userdata)
|
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)userdata;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po->mainloop != nullptr);
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
po->writable = nbytes;
|
|
|
|
pa_threaded_mainloop_signal(po->mainloop, 0);
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2011-09-16 22:58:19 +02:00
|
|
|
/**
|
|
|
|
* Create, set up and connect a context.
|
|
|
|
*
|
|
|
|
* Caller must lock the main loop.
|
|
|
|
*
|
|
|
|
* @return true on success, false on error
|
|
|
|
*/
|
|
|
|
static bool
|
2013-04-16 20:51:21 +02:00
|
|
|
pulse_output_setup_stream(PulseOutput *po, const pa_sample_spec *ss,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2011-09-16 22:58:19 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po != nullptr);
|
|
|
|
assert(po->context != nullptr);
|
2011-09-16 22:58:19 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
po->stream = pa_stream_new(po->context, po->name, ss, nullptr);
|
|
|
|
if (po->stream == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
SetError(error, po->context, "pa_stream_new() has failed");
|
2011-09-16 22:58:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_stream_set_suspended_callback(po->stream,
|
|
|
|
pulse_output_stream_suspended_cb, po);
|
|
|
|
|
|
|
|
pa_stream_set_state_callback(po->stream,
|
|
|
|
pulse_output_stream_state_cb, po);
|
|
|
|
pa_stream_set_write_callback(po->stream,
|
|
|
|
pulse_output_stream_write_cb, po);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
static bool
|
2013-08-03 21:00:50 +02:00
|
|
|
pulse_output_open(struct audio_output *ao, AudioFormat &audio_format,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)ao;
|
2006-07-13 21:03:49 +02:00
|
|
|
pa_sample_spec ss;
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po->mainloop != nullptr);
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2011-09-16 22:16:14 +02:00
|
|
|
pa_threaded_mainloop_lock(po->mainloop);
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po->context != nullptr) {
|
2009-10-21 10:30:42 +02:00
|
|
|
switch (pa_context_get_state(po->context)) {
|
|
|
|
case PA_CONTEXT_UNCONNECTED:
|
|
|
|
case PA_CONTEXT_TERMINATED:
|
|
|
|
case PA_CONTEXT_FAILED:
|
|
|
|
/* the connection was closed meanwhile; delete
|
|
|
|
it, and pulse_output_wait_connection() will
|
|
|
|
reopen it */
|
|
|
|
pulse_output_delete_context(po);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_CONTEXT_READY:
|
|
|
|
case PA_CONTEXT_CONNECTING:
|
|
|
|
case PA_CONTEXT_AUTHORIZING:
|
|
|
|
case PA_CONTEXT_SETTING_NAME:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!pulse_output_wait_connection(po, error)) {
|
2011-09-16 22:16:14 +02:00
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
2011-09-16 22:16:14 +02:00
|
|
|
}
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2008-10-25 20:41:28 +02:00
|
|
|
/* MPD doesn't support the other pulseaudio sample formats, so
|
|
|
|
we just force MPD to send us everything as 16 bit */
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.format = SampleFormat::S16;
|
2006-07-13 21:03:49 +02:00
|
|
|
|
|
|
|
ss.format = PA_SAMPLE_S16NE;
|
2013-08-03 21:00:50 +02:00
|
|
|
ss.rate = audio_format.sample_rate;
|
|
|
|
ss.channels = audio_format.channels;
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
/* create a stream .. */
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!pulse_output_setup_stream(po, &ss, error)) {
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* .. and connect it (asynchronously) */
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (pa_stream_connect_playback(po->stream, po->sink,
|
|
|
|
nullptr, pa_stream_flags_t(0),
|
|
|
|
nullptr, nullptr) < 0) {
|
2011-08-31 21:00:55 +02:00
|
|
|
pulse_output_delete_stream(po);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
SetError(error, po->context,
|
|
|
|
"pa_stream_connect_playback() has failed");
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
pulse_output_close(struct audio_output *ao)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)ao;
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_operation *o;
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po->mainloop != nullptr);
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_threaded_mainloop_lock(po->mainloop);
|
|
|
|
|
|
|
|
if (pa_stream_get_state(po->stream) == PA_STREAM_READY) {
|
|
|
|
o = pa_stream_drain(po->stream,
|
|
|
|
pulse_output_stream_success_cb, po);
|
2013-04-16 20:51:21 +02:00
|
|
|
if (o == nullptr) {
|
2009-10-21 10:30:42 +02:00
|
|
|
g_warning("pa_stream_drain() has failed: %s",
|
|
|
|
pa_strerror(pa_context_errno(po->context)));
|
|
|
|
} else
|
|
|
|
pulse_wait_for_operation(po->mainloop, o);
|
|
|
|
}
|
|
|
|
|
2011-08-31 21:00:55 +02:00
|
|
|
pulse_output_delete_stream(po);
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po->context != nullptr &&
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_context_get_state(po->context) != PA_CONTEXT_READY)
|
|
|
|
pulse_output_delete_context(po);
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the stream is (already) connected, and waits if not. The
|
|
|
|
* mainloop must be locked before calling this function.
|
|
|
|
*
|
|
|
|
* @return true on success, false on error
|
|
|
|
*/
|
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
pulse_output_wait_stream(PulseOutput *po, Error &error)
|
2009-10-21 10:30:42 +02:00
|
|
|
{
|
2012-08-14 22:22:55 +02:00
|
|
|
while (true) {
|
|
|
|
switch (pa_stream_get_state(po->stream)) {
|
|
|
|
case PA_STREAM_READY:
|
|
|
|
return true;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2012-08-14 22:22:55 +02:00
|
|
|
case PA_STREAM_FAILED:
|
|
|
|
case PA_STREAM_TERMINATED:
|
|
|
|
case PA_STREAM_UNCONNECTED:
|
2013-08-10 18:02:44 +02:00
|
|
|
SetError(error, po->context,
|
|
|
|
"failed to connect the stream");
|
2012-08-14 22:22:55 +02:00
|
|
|
return false;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2012-08-14 22:22:55 +02:00
|
|
|
case PA_STREAM_CREATING:
|
|
|
|
pa_threaded_mainloop_wait(po->mainloop);
|
|
|
|
break;
|
|
|
|
}
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
/**
|
|
|
|
* Sets cork mode on the stream.
|
|
|
|
*/
|
|
|
|
static bool
|
2013-04-16 20:51:21 +02:00
|
|
|
pulse_output_stream_pause(PulseOutput *po, bool pause,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2009-10-21 10:30:42 +02:00
|
|
|
{
|
|
|
|
pa_operation *o;
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po->mainloop != nullptr);
|
|
|
|
assert(po->context != nullptr);
|
|
|
|
assert(po->stream != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
o = pa_stream_cork(po->stream, pause,
|
|
|
|
pulse_output_stream_success_cb, po);
|
2013-04-16 20:51:21 +02:00
|
|
|
if (o == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
SetError(error, po->context, "pa_stream_cork() has failed");
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pulse_wait_for_operation(po->mainloop, o)) {
|
2013-08-10 18:02:44 +02:00
|
|
|
SetError(error, po->context, "pa_stream_cork() has failed");
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2012-08-14 22:30:46 +02:00
|
|
|
static unsigned
|
|
|
|
pulse_output_delay(struct audio_output *ao)
|
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)ao;
|
2012-08-14 22:30:46 +02:00
|
|
|
unsigned result = 0;
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(po->mainloop);
|
|
|
|
|
2013-09-04 16:44:57 +02:00
|
|
|
if (po->base.pause && pa_stream_is_corked(po->stream) &&
|
2012-08-14 22:30:46 +02:00
|
|
|
pa_stream_get_state(po->stream) == PA_STREAM_READY)
|
|
|
|
/* idle while paused */
|
|
|
|
result = 1000;
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
static size_t
|
2011-09-16 23:31:48 +02:00
|
|
|
pulse_output_play(struct audio_output *ao, const void *chunk, size_t size,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)ao;
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po->mainloop != nullptr);
|
|
|
|
assert(po->stream != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(po->mainloop);
|
|
|
|
|
|
|
|
/* check if the stream is (already) connected */
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!pulse_output_wait_stream(po, error)) {
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po->context != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
/* unpause if previously paused */
|
|
|
|
|
2013-09-04 16:44:57 +02:00
|
|
|
if (pa_stream_is_corked(po->stream) &&
|
2013-08-10 18:02:44 +02:00
|
|
|
!pulse_output_stream_pause(po, false, error)) {
|
2011-08-23 22:44:43 +02:00
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
return 0;
|
2011-08-23 22:44:43 +02:00
|
|
|
}
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
/* wait until the server allows us to write */
|
|
|
|
|
|
|
|
while (po->writable == 0) {
|
2011-08-23 22:43:08 +02:00
|
|
|
if (pa_stream_is_suspended(po->stream)) {
|
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(pulse_output_domain, "suspended");
|
2011-08-23 22:43:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_threaded_mainloop_wait(po->mainloop);
|
|
|
|
|
|
|
|
if (pa_stream_get_state(po->stream) != PA_STREAM_READY) {
|
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(pulse_output_domain, "disconnected");
|
2011-08-23 22:48:15 +02:00
|
|
|
return 0;
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now write */
|
|
|
|
|
|
|
|
if (size > po->writable)
|
|
|
|
/* don't send more than possible */
|
|
|
|
size = po->writable;
|
|
|
|
|
|
|
|
po->writable -= size;
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
int result = pa_stream_write(po->stream, chunk, size, nullptr,
|
|
|
|
0, PA_SEEK_RELATIVE);
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
2013-08-10 18:02:44 +02:00
|
|
|
if (result < 0) {
|
|
|
|
SetError(error, po->context, "pa_stream_write() failed");
|
2009-02-23 09:29:56 +01:00
|
|
|
return 0;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
return size;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
pulse_output_cancel(struct audio_output *ao)
|
2009-10-21 10:30:42 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)ao;
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_operation *o;
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po->mainloop != nullptr);
|
|
|
|
assert(po->stream != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(po->mainloop);
|
|
|
|
|
|
|
|
if (pa_stream_get_state(po->stream) != PA_STREAM_READY) {
|
|
|
|
/* no need to flush when the stream isn't connected
|
|
|
|
yet */
|
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po->context != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
o = pa_stream_flush(po->stream, pulse_output_stream_success_cb, po);
|
2013-04-16 20:51:21 +02:00
|
|
|
if (o == nullptr) {
|
2009-10-21 10:30:42 +02:00
|
|
|
g_warning("pa_stream_flush() has failed: %s",
|
|
|
|
pa_strerror(pa_context_errno(po->context)));
|
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pulse_wait_for_operation(po->mainloop, o);
|
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2011-09-16 23:31:48 +02:00
|
|
|
pulse_output_pause(struct audio_output *ao)
|
2009-10-21 10:30:42 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)ao;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po->mainloop != nullptr);
|
|
|
|
assert(po->stream != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(po->mainloop);
|
|
|
|
|
|
|
|
/* check if the stream is (already/still) connected */
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
if (!pulse_output_wait_stream(po, error)) {
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
2013-08-10 18:02:44 +02:00
|
|
|
g_warning("%s", error.GetMessage());
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(po->context != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
/* cork the stream */
|
|
|
|
|
2013-09-04 16:44:57 +02:00
|
|
|
if (!pa_stream_is_corked(po->stream) &&
|
2013-08-10 18:02:44 +02:00
|
|
|
!pulse_output_stream_pause(po, true, error)) {
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
2013-08-10 18:02:44 +02:00
|
|
|
g_warning("%s", error.GetMessage());
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(po->mainloop);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
pulse_output_test_default_device(void)
|
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
|
2013-08-04 12:25:08 +02:00
|
|
|
const config_param empty;
|
2013-08-10 18:02:44 +02:00
|
|
|
PulseOutput *po = (PulseOutput *)
|
|
|
|
pulse_output_init(empty, IgnoreError());
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po == nullptr)
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
success = pulse_output_wait_connection(po, IgnoreError());
|
2011-09-16 23:31:48 +02:00
|
|
|
pulse_output_finish(&po->base);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2009-10-20 21:05:11 +02:00
|
|
|
const struct audio_output_plugin pulse_output_plugin = {
|
2013-04-16 20:51:21 +02:00
|
|
|
"pulse",
|
|
|
|
pulse_output_test_default_device,
|
|
|
|
pulse_output_init,
|
|
|
|
pulse_output_finish,
|
|
|
|
pulse_output_enable,
|
|
|
|
pulse_output_disable,
|
|
|
|
pulse_output_open,
|
|
|
|
pulse_output_close,
|
|
|
|
pulse_output_delay,
|
|
|
|
nullptr,
|
|
|
|
pulse_output_play,
|
|
|
|
nullptr,
|
|
|
|
pulse_output_cancel,
|
|
|
|
pulse_output_pause,
|
|
|
|
|
|
|
|
&pulse_mixer_plugin,
|
2006-07-13 21:03:49 +02:00
|
|
|
};
|