2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2015-01-01 19:48:13 +01:00
|
|
|
* Copyright (C) 2003-2015 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"
|
2014-12-29 22:25:14 +01:00
|
|
|
#include "lib/pulse/Domain.hxx"
|
2014-12-29 22:34:32 +01:00
|
|
|
#include "lib/pulse/Error.hxx"
|
2014-12-29 22:42:52 +01:00
|
|
|
#include "lib/pulse/LogError.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "../OutputAPI.hxx"
|
2014-12-29 18:12:16 +01:00
|
|
|
#include "../Wrapper.hxx"
|
2014-01-24 16:25:21 +01:00
|
|
|
#include "mixer/MixerList.hxx"
|
|
|
|
#include "mixer/plugins/PulseMixerPlugin.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2006-07-19 18:48:24 +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>
|
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>
|
2014-02-24 20:31:29 +01:00
|
|
|
#include <stdlib.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 {
|
2014-01-28 11:34:09 +01:00
|
|
|
AudioOutput base;
|
2011-09-16 23:31:48 +02:00
|
|
|
|
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;
|
2014-01-28 23:39:48 +01:00
|
|
|
|
|
|
|
PulseOutput()
|
2014-12-29 22:57:46 +01:00
|
|
|
:base(pulse_output_plugin),
|
|
|
|
mixer(nullptr),
|
|
|
|
mainloop(nullptr), stream(nullptr) {}
|
2014-12-29 18:12:16 +01:00
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
public:
|
|
|
|
void SetMixer(PulseMixer &_mixer);
|
|
|
|
|
|
|
|
void ClearMixer(gcc_unused PulseMixer &old_mixer) {
|
|
|
|
assert(mixer == &old_mixer);
|
|
|
|
|
|
|
|
mixer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetVolume(const pa_cvolume &volume, Error &error);
|
|
|
|
|
|
|
|
void Lock() {
|
|
|
|
pa_threaded_mainloop_lock(mainloop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unlock() {
|
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnContextStateChanged(pa_context_state_t new_state);
|
|
|
|
void OnServerLayoutChanged(pa_subscription_event_type_t t,
|
|
|
|
uint32_t idx);
|
|
|
|
void OnStreamSuspended(pa_stream *_stream);
|
|
|
|
void OnStreamStateChanged(pa_stream *_stream,
|
|
|
|
pa_stream_state_t new_state);
|
|
|
|
void OnStreamWrite(size_t nbytes);
|
|
|
|
|
|
|
|
void OnStreamSuccess() {
|
|
|
|
pa_threaded_mainloop_signal(mainloop, 0);
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
gcc_const
|
|
|
|
static bool TestDefaultDevice();
|
|
|
|
|
|
|
|
bool Configure(const config_param ¶m, Error &error);
|
2015-01-04 19:44:45 +01:00
|
|
|
static PulseOutput *Create(const config_param ¶m, Error &error);
|
2014-12-29 18:12:16 +01:00
|
|
|
|
|
|
|
bool Enable(Error &error);
|
|
|
|
void Disable();
|
|
|
|
|
|
|
|
bool Open(AudioFormat &audio_format, Error &error);
|
|
|
|
void Close();
|
|
|
|
|
|
|
|
unsigned Delay();
|
|
|
|
size_t Play(const void *chunk, size_t size, Error &error);
|
|
|
|
void Cancel();
|
|
|
|
bool Pause();
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Attempt to connect asynchronously to the PulseAudio server.
|
|
|
|
*
|
|
|
|
* @return true on success, false on error
|
|
|
|
*/
|
|
|
|
bool Connect(Error &error);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create, set up and connect a context.
|
|
|
|
*
|
|
|
|
* Caller must lock the main loop.
|
|
|
|
*
|
|
|
|
* @return true on success, false on error
|
|
|
|
*/
|
|
|
|
bool SetupContext(Error &error);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Frees and clears the context.
|
|
|
|
*
|
|
|
|
* Caller must lock the main loop.
|
|
|
|
*/
|
|
|
|
void DeleteContext();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the context is (already) connected, and waits if
|
|
|
|
* not. If the context has been disconnected, retry to
|
|
|
|
* connect.
|
|
|
|
*
|
|
|
|
* Caller must lock the main loop.
|
|
|
|
*
|
|
|
|
* @return true on success, false on error
|
|
|
|
*/
|
|
|
|
bool WaitConnection(Error &error);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create, set up and connect a context.
|
|
|
|
*
|
|
|
|
* Caller must lock the main loop.
|
|
|
|
*
|
|
|
|
* @return true on success, false on error
|
|
|
|
*/
|
|
|
|
bool SetupStream(const pa_sample_spec &ss, Error &error);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Frees and clears the stream.
|
|
|
|
*/
|
|
|
|
void DeleteStream();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
bool WaitStream(Error &error);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets cork mode on the stream.
|
|
|
|
*/
|
|
|
|
bool StreamPause(bool pause, Error &error);
|
2011-09-17 19:50:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2014-02-06 21:10:12 +01:00
|
|
|
pulse_output_lock(PulseOutput &po)
|
2011-09-17 19:50:36 +02:00
|
|
|
{
|
2015-01-04 19:20:36 +01:00
|
|
|
po.Lock();
|
2011-09-17 19:50:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-06 21:10:12 +01:00
|
|
|
pulse_output_unlock(PulseOutput &po)
|
2011-09-17 19:50:36 +02:00
|
|
|
{
|
2015-01-04 19:20:36 +01:00
|
|
|
po.Unlock();
|
2011-09-17 19:50:36 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
inline void
|
|
|
|
PulseOutput::SetMixer(PulseMixer &_mixer)
|
2009-10-23 10:33:26 +02:00
|
|
|
{
|
2015-01-04 19:20:36 +01:00
|
|
|
assert(mixer == nullptr);
|
2009-10-23 10:33:26 +02:00
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
mixer = &_mixer;
|
2009-10-23 10:33:26 +02:00
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
if (mainloop == nullptr)
|
2009-10-23 10:56:16 +02:00
|
|
|
return;
|
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
pa_threaded_mainloop_lock(mainloop);
|
2009-10-23 10:33:26 +02:00
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
if (context != nullptr &&
|
|
|
|
pa_context_get_state(context) == PA_CONTEXT_READY) {
|
|
|
|
pulse_mixer_on_connect(_mixer, context);
|
2009-10-23 10:33:26 +02:00
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
if (stream != nullptr &&
|
|
|
|
pa_stream_get_state(stream) == PA_STREAM_READY)
|
|
|
|
pulse_mixer_on_change(_mixer, context, stream);
|
2009-10-23 10:33:26 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2009-10-23 10:33:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-04 19:20:36 +01:00
|
|
|
pulse_output_set_mixer(PulseOutput &po, PulseMixer &pm)
|
2009-10-23 10:33:26 +02:00
|
|
|
{
|
2015-01-04 19:20:36 +01:00
|
|
|
po.SetMixer(pm);
|
2009-10-23 10:33:26 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
void
|
|
|
|
pulse_output_clear_mixer(PulseOutput &po, PulseMixer &pm)
|
2009-10-23 10:33:26 +02:00
|
|
|
{
|
2015-01-04 19:20:36 +01:00
|
|
|
po.ClearMixer(pm);
|
|
|
|
}
|
2009-10-23 10:33:26 +02:00
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
inline bool
|
|
|
|
PulseOutput::SetVolume(const pa_cvolume &volume, Error &error)
|
|
|
|
{
|
|
|
|
if (context == nullptr || stream == nullptr ||
|
|
|
|
pa_stream_get_state(stream) != PA_STREAM_READY) {
|
2014-12-29 22:25:14 +01:00
|
|
|
error.Set(pulse_domain, "disconnected");
|
2009-10-23 10:33:26 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
pa_operation *o =
|
|
|
|
pa_context_set_sink_input_volume(context,
|
|
|
|
pa_stream_get_index(stream),
|
|
|
|
&volume, nullptr, nullptr);
|
2013-04-16 20:51:21 +02:00
|
|
|
if (o == nullptr) {
|
2015-01-04 19:20:36 +01:00
|
|
|
SetPulseError(error, context,
|
2014-12-29 22:34:32 +01:00
|
|
|
"failed to set PulseAudio volume");
|
2009-10-23 10:33:26 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_operation_unref(o);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
bool
|
|
|
|
pulse_output_set_volume(PulseOutput &po, const pa_cvolume *volume,
|
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
return po.SetVolume(*volume, error);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
assert(mainloop != nullptr);
|
|
|
|
assert(operation != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-09-05 11:16:09 +02:00
|
|
|
pa_operation_state_t state;
|
|
|
|
while ((state = pa_operation_get_state(operation))
|
|
|
|
== PA_OPERATION_RUNNING)
|
2009-10-21 10:30:42 +02:00
|
|
|
pa_threaded_mainloop_wait(mainloop);
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2015-01-04 19:20:36 +01:00
|
|
|
PulseOutput &po = *(PulseOutput *)userdata;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
po.OnStreamSuccess();
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
inline void
|
|
|
|
PulseOutput::OnContextStateChanged(pa_context_state_t new_state)
|
2009-10-21 10:30:42 +02:00
|
|
|
{
|
2015-01-04 19:20:36 +01:00
|
|
|
switch (new_state) {
|
2009-10-21 10:30:42 +02:00
|
|
|
case PA_CONTEXT_READY:
|
2015-01-04 19:20:36 +01:00
|
|
|
if (mixer != nullptr)
|
|
|
|
pulse_mixer_on_connect(*mixer, context);
|
2009-10-23 10:33:26 +02:00
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
pa_threaded_mainloop_signal(mainloop, 0);
|
2009-10-23 10:33:26 +02:00
|
|
|
break;
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
case PA_CONTEXT_TERMINATED:
|
|
|
|
case PA_CONTEXT_FAILED:
|
2015-01-04 19:20:36 +01:00
|
|
|
if (mixer != nullptr)
|
|
|
|
pulse_mixer_on_disconnect(*mixer);
|
2009-10-23 10:33:26 +02:00
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
/* the caller thread might be waiting for these
|
|
|
|
states */
|
2015-01-04 19:20:36 +01:00
|
|
|
pa_threaded_mainloop_signal(mainloop, 0);
|
2009-10-21 10:30:42 +02:00
|
|
|
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
|
2015-01-04 19:20:36 +01:00
|
|
|
pulse_output_context_state_cb(struct pa_context *context, void *userdata)
|
|
|
|
{
|
|
|
|
PulseOutput &po = *(PulseOutput *)userdata;
|
|
|
|
|
|
|
|
po.OnContextStateChanged(pa_context_get_state(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
PulseOutput::OnServerLayoutChanged(pa_subscription_event_type_t t,
|
|
|
|
uint32_t idx)
|
2009-10-23 10:33:26 +02:00
|
|
|
{
|
2013-04-16 20:51:21 +02:00
|
|
|
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
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
if (mixer != nullptr &&
|
2009-10-23 10:33:26 +02:00
|
|
|
facility == PA_SUBSCRIPTION_EVENT_SINK_INPUT &&
|
2015-01-04 19:20:36 +01:00
|
|
|
stream != nullptr &&
|
|
|
|
pa_stream_get_state(stream) == PA_STREAM_READY &&
|
|
|
|
idx == pa_stream_get_index(stream) &&
|
2009-10-23 10:33:26 +02:00
|
|
|
(type == PA_SUBSCRIPTION_EVENT_NEW ||
|
|
|
|
type == PA_SUBSCRIPTION_EVENT_CHANGE))
|
2015-01-04 19:20:36 +01:00
|
|
|
pulse_mixer_on_change(*mixer, context, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pulse_output_subscribe_cb(gcc_unused pa_context *context,
|
|
|
|
pa_subscription_event_type_t t,
|
|
|
|
uint32_t idx, void *userdata)
|
|
|
|
{
|
|
|
|
PulseOutput &po = *(PulseOutput *)userdata;
|
|
|
|
|
|
|
|
po.OnServerLayoutChanged(t, idx);
|
2009-10-23 10:33:26 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
inline bool
|
|
|
|
PulseOutput::Connect(Error &error)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(context != nullptr);
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (pa_context_connect(context, server,
|
2013-08-10 18:02:44 +02:00
|
|
|
(pa_context_flags_t)0, nullptr) < 0) {
|
2014-12-29 18:12:16 +01:00
|
|
|
SetPulseError(error, context,
|
2014-12-29 22:34:32 +01:00
|
|
|
"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
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
void
|
|
|
|
PulseOutput::DeleteStream()
|
2011-08-31 21:00:55 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(stream != nullptr);
|
2011-08-31 21:00:55 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_stream_set_suspended_callback(stream, nullptr, nullptr);
|
2011-08-31 20:55:49 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_stream_set_state_callback(stream, nullptr, nullptr);
|
|
|
|
pa_stream_set_write_callback(stream, nullptr, nullptr);
|
2011-08-31 20:55:49 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_stream_disconnect(stream);
|
|
|
|
pa_stream_unref(stream);
|
|
|
|
stream = nullptr;
|
2011-08-31 21:00:55 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
void
|
|
|
|
PulseOutput::DeleteContext()
|
2011-08-31 20:58:36 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(context != nullptr);
|
2011-08-31 20:58:36 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_context_set_state_callback(context, nullptr, nullptr);
|
|
|
|
pa_context_set_subscribe_callback(context, nullptr, nullptr);
|
2011-08-31 20:55:49 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_context_disconnect(context);
|
|
|
|
pa_context_unref(context);
|
|
|
|
context = nullptr;
|
2011-08-31 20:58:36 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
bool
|
|
|
|
PulseOutput::SetupContext(Error &error)
|
2009-10-21 10:30:42 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(mainloop != nullptr);
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
context = pa_context_new(pa_threaded_mainloop_get_api(mainloop),
|
|
|
|
MPD_PULSE_NAME);
|
|
|
|
if (context == nullptr) {
|
2014-12-29 22:25:14 +01:00
|
|
|
error.Set(pulse_domain, "pa_context_new() has failed");
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_context_set_state_callback(context,
|
|
|
|
pulse_output_context_state_cb, this);
|
|
|
|
pa_context_set_subscribe_callback(context,
|
|
|
|
pulse_output_subscribe_cb, this);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (!Connect(error)) {
|
|
|
|
DeleteContext();
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
inline bool
|
|
|
|
PulseOutput::Configure(const config_param ¶m, Error &error)
|
|
|
|
{
|
|
|
|
if (!base.Configure(param, error))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
name = param.GetBlockValue("name", "mpd_pulse");
|
|
|
|
server = param.GetBlockValue("server");
|
|
|
|
sink = param.GetBlockValue("sink");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-04 19:44:45 +01:00
|
|
|
PulseOutput *
|
|
|
|
PulseOutput::Create(const config_param ¶m, Error &error)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2014-02-24 20:31:29 +01:00
|
|
|
setenv("PULSE_PROP_media.role", "music", true);
|
2014-03-04 11:36:43 +01:00
|
|
|
setenv("PULSE_PROP_application.icon_name", "mpd", true);
|
2009-10-20 21:02:10 +02:00
|
|
|
|
2015-01-04 19:44:45 +01:00
|
|
|
auto *po = new PulseOutput();
|
2014-12-29 18:12:16 +01:00
|
|
|
if (!po->Configure(param, error)) {
|
2013-04-16 20:51:21 +02:00
|
|
|
delete po;
|
|
|
|
return nullptr;
|
2011-09-16 23:31:48 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:44:45 +01:00
|
|
|
return po;
|
2009-10-23 10:56:16 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
inline bool
|
|
|
|
PulseOutput::Enable(Error &error)
|
2009-10-23 10:56:16 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(mainloop == 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 */
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
mainloop = pa_threaded_mainloop_new();
|
|
|
|
if (mainloop == nullptr) {
|
2014-12-29 22:25:14 +01:00
|
|
|
error.Set(pulse_domain,
|
2013-08-10 18:02:44 +02:00
|
|
|
"pa_threaded_mainloop_new() has failed");
|
2009-10-23 10:56:16 +02:00
|
|
|
return false;
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_lock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (pa_threaded_mainloop_start(mainloop) < 0) {
|
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
|
|
|
pa_threaded_mainloop_free(mainloop);
|
|
|
|
mainloop = nullptr;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 22:25:14 +01:00
|
|
|
error.Set(pulse_domain,
|
2013-08-10 18:02:44 +02:00
|
|
|
"pa_threaded_mainloop_start() has failed");
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create the libpulse context and connect it */
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (!SetupContext(error)) {
|
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
|
|
|
pa_threaded_mainloop_stop(mainloop);
|
|
|
|
pa_threaded_mainloop_free(mainloop);
|
|
|
|
mainloop = nullptr;
|
2009-10-23 10:56:16 +02:00
|
|
|
return false;
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2009-10-23 10:56:16 +02:00
|
|
|
return true;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
inline void
|
|
|
|
PulseOutput::Disable()
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(mainloop != nullptr);
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_stop(mainloop);
|
|
|
|
if (context != nullptr)
|
|
|
|
DeleteContext();
|
|
|
|
pa_threaded_mainloop_free(mainloop);
|
|
|
|
mainloop = nullptr;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
bool
|
|
|
|
PulseOutput::WaitConnection(Error &error)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(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
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (context == nullptr && !SetupContext(error))
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
while (true) {
|
2014-12-29 18:12:16 +01:00
|
|
|
state = pa_context_get_state(context);
|
2009-10-21 10:30:42 +02:00
|
|
|
switch (state) {
|
|
|
|
case PA_CONTEXT_READY:
|
|
|
|
/* nothing to do */
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case PA_CONTEXT_UNCONNECTED:
|
|
|
|
case PA_CONTEXT_TERMINATED:
|
|
|
|
case PA_CONTEXT_FAILED:
|
|
|
|
/* failure */
|
2014-12-29 18:12:16 +01:00
|
|
|
SetPulseError(error, context, "failed to connect");
|
|
|
|
DeleteContext();
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
case PA_CONTEXT_CONNECTING:
|
|
|
|
case PA_CONTEXT_AUTHORIZING:
|
|
|
|
case PA_CONTEXT_SETTING_NAME:
|
|
|
|
/* wait some more */
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_wait(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
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
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
inline void
|
|
|
|
PulseOutput::OnStreamSuspended(gcc_unused pa_stream *_stream)
|
2011-08-23 22:43:08 +02:00
|
|
|
{
|
2015-01-04 19:20:36 +01:00
|
|
|
assert(_stream == stream || stream == nullptr);
|
|
|
|
assert(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() */
|
2015-01-04 19:20:36 +01:00
|
|
|
pa_threaded_mainloop_signal(mainloop, 0);
|
2011-08-23 22:43:08 +02:00
|
|
|
}
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
static void
|
2015-01-04 19:20:36 +01:00
|
|
|
pulse_output_stream_suspended_cb(pa_stream *stream, void *userdata)
|
2009-10-21 10:30:42 +02:00
|
|
|
{
|
2015-01-04 19:20:36 +01:00
|
|
|
PulseOutput &po = *(PulseOutput *)userdata;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
po.OnStreamSuspended(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
PulseOutput::OnStreamStateChanged(pa_stream *_stream,
|
|
|
|
pa_stream_state_t new_state)
|
|
|
|
{
|
|
|
|
assert(_stream == stream || stream == nullptr);
|
|
|
|
assert(mainloop != nullptr);
|
|
|
|
assert(context != nullptr);
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
switch (new_state) {
|
2009-10-21 10:30:42 +02:00
|
|
|
case PA_STREAM_READY:
|
2015-01-04 19:20:36 +01:00
|
|
|
if (mixer != nullptr)
|
|
|
|
pulse_mixer_on_change(*mixer, context, _stream);
|
2009-10-23 10:33:26 +02:00
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
pa_threaded_mainloop_signal(mainloop, 0);
|
2009-10-23 10:33:26 +02:00
|
|
|
break;
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
case PA_STREAM_FAILED:
|
|
|
|
case PA_STREAM_TERMINATED:
|
2015-01-04 19:20:36 +01:00
|
|
|
if (mixer != nullptr)
|
|
|
|
pulse_mixer_on_disconnect(*mixer);
|
2009-10-23 10:33:26 +02:00
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
pa_threaded_mainloop_signal(mainloop, 0);
|
2009-10-21 10:30:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_STREAM_UNCONNECTED:
|
|
|
|
case PA_STREAM_CREATING:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
static void
|
|
|
|
pulse_output_stream_state_cb(pa_stream *stream, void *userdata)
|
|
|
|
{
|
|
|
|
PulseOutput &po = *(PulseOutput *)userdata;
|
|
|
|
|
|
|
|
return po.OnStreamStateChanged(stream, pa_stream_get_state(stream));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
PulseOutput::OnStreamWrite(size_t nbytes)
|
|
|
|
{
|
|
|
|
assert(mainloop != nullptr);
|
|
|
|
|
|
|
|
writable = nbytes;
|
|
|
|
pa_threaded_mainloop_signal(mainloop, 0);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2015-01-04 19:20:36 +01:00
|
|
|
PulseOutput &po = *(PulseOutput *)userdata;
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2015-01-04 19:20:36 +01:00
|
|
|
return po.OnStreamWrite(nbytes);
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
inline bool
|
|
|
|
PulseOutput::SetupStream(const pa_sample_spec &ss, Error &error)
|
2011-09-16 22:58:19 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(context != nullptr);
|
2011-09-16 22:58:19 +02:00
|
|
|
|
2014-10-17 00:06:27 +02:00
|
|
|
/* WAVE-EX is been adopted as the speaker map for most media files */
|
|
|
|
pa_channel_map chan_map;
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_channel_map_init_auto(&chan_map, ss.channels,
|
2014-10-17 00:06:27 +02:00
|
|
|
PA_CHANNEL_MAP_WAVEEX);
|
2014-12-29 18:12:16 +01:00
|
|
|
stream = pa_stream_new(context, name, &ss, &chan_map);
|
|
|
|
if (stream == nullptr) {
|
|
|
|
SetPulseError(error, context,
|
2014-12-29 22:34:32 +01:00
|
|
|
"pa_stream_new() has failed");
|
2011-09-16 22:58:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_stream_set_suspended_callback(stream,
|
|
|
|
pulse_output_stream_suspended_cb,
|
|
|
|
this);
|
2011-09-16 22:58:19 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_stream_set_state_callback(stream,
|
|
|
|
pulse_output_stream_state_cb, this);
|
|
|
|
pa_stream_set_write_callback(stream,
|
|
|
|
pulse_output_stream_write_cb, this);
|
2011-09-16 22:58:19 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
inline bool
|
|
|
|
PulseOutput::Open(AudioFormat &audio_format, Error &error)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(mainloop != nullptr);
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_lock(mainloop);
|
2011-09-16 22:16:14 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (context != nullptr) {
|
|
|
|
switch (pa_context_get_state(context)) {
|
2009-10-21 10:30:42 +02:00
|
|
|
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 */
|
2014-12-29 18:12:16 +01:00
|
|
|
DeleteContext();
|
2009-10-21 10:30:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PA_CONTEXT_READY:
|
|
|
|
case PA_CONTEXT_CONNECTING:
|
|
|
|
case PA_CONTEXT_AUTHORIZING:
|
|
|
|
case PA_CONTEXT_SETTING_NAME:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (!WaitConnection(error)) {
|
|
|
|
pa_threaded_mainloop_unlock(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
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_sample_spec ss;
|
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 .. */
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (!SetupStream(ss, error)) {
|
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* .. and connect it (asynchronously) */
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (pa_stream_connect_playback(stream, sink,
|
2013-08-10 18:02:44 +02:00
|
|
|
nullptr, pa_stream_flags_t(0),
|
|
|
|
nullptr, nullptr) < 0) {
|
2014-12-29 18:12:16 +01:00
|
|
|
DeleteStream();
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
SetPulseError(error, context,
|
2014-12-29 22:34:32 +01:00
|
|
|
"pa_stream_connect_playback() has failed");
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
inline void
|
|
|
|
PulseOutput::Close()
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(mainloop != nullptr);
|
2011-08-23 22:03:24 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_lock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (pa_stream_get_state(stream) == PA_STREAM_READY) {
|
|
|
|
pa_operation *o =
|
|
|
|
pa_stream_drain(stream,
|
|
|
|
pulse_output_stream_success_cb, this);
|
2013-04-16 20:51:21 +02:00
|
|
|
if (o == nullptr) {
|
2014-12-29 18:12:16 +01:00
|
|
|
LogPulseError(context,
|
2014-12-29 22:42:52 +01:00
|
|
|
"pa_stream_drain() has failed");
|
2009-10-21 10:30:42 +02:00
|
|
|
} else
|
2014-12-29 18:12:16 +01:00
|
|
|
pulse_wait_for_operation(mainloop, o);
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
DeleteStream();
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (context != nullptr &&
|
|
|
|
pa_context_get_state(context) != PA_CONTEXT_READY)
|
|
|
|
DeleteContext();
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
bool
|
|
|
|
PulseOutput::WaitStream(Error &error)
|
2009-10-21 10:30:42 +02:00
|
|
|
{
|
2012-08-14 22:22:55 +02:00
|
|
|
while (true) {
|
2014-12-29 18:12:16 +01:00
|
|
|
switch (pa_stream_get_state(stream)) {
|
2012-08-14 22:22:55 +02:00
|
|
|
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:
|
2014-12-29 18:12:16 +01:00
|
|
|
SetPulseError(error, context,
|
2014-12-29 22:34:32 +01:00
|
|
|
"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:
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_wait(mainloop);
|
2012-08-14 22:22:55 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
bool
|
|
|
|
PulseOutput::StreamPause(bool pause, Error &error)
|
2009-10-21 10:30:42 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(mainloop != nullptr);
|
|
|
|
assert(context != nullptr);
|
|
|
|
assert(stream != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_operation *o = pa_stream_cork(stream, pause,
|
|
|
|
pulse_output_stream_success_cb, this);
|
2013-04-16 20:51:21 +02:00
|
|
|
if (o == nullptr) {
|
2014-12-29 18:12:16 +01:00
|
|
|
SetPulseError(error, context,
|
2014-12-29 22:34:32 +01:00
|
|
|
"pa_stream_cork() has failed");
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (!pulse_wait_for_operation(mainloop, o)) {
|
|
|
|
SetPulseError(error, context,
|
2014-12-29 22:34:32 +01:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
inline unsigned
|
|
|
|
PulseOutput::Delay()
|
2012-08-14 22:30:46 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_lock(mainloop);
|
2012-08-14 22:30:46 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
unsigned result = 0;
|
|
|
|
if (base.pause && pa_stream_is_corked(stream) &&
|
|
|
|
pa_stream_get_state(stream) == PA_STREAM_READY)
|
2012-08-14 22:30:46 +02:00
|
|
|
/* idle while paused */
|
|
|
|
result = 1000;
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2012-08-14 22:30:46 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
inline size_t
|
|
|
|
PulseOutput::Play(const void *chunk, size_t size, Error &error)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(mainloop != nullptr);
|
|
|
|
assert(stream != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_lock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
/* check if the stream is (already) connected */
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (!WaitStream(error)) {
|
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(context != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
/* unpause if previously paused */
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (pa_stream_is_corked(stream) && !StreamPause(false, error)) {
|
|
|
|
pa_threaded_mainloop_unlock(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 */
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
while (writable == 0) {
|
|
|
|
if (pa_stream_is_suspended(stream)) {
|
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2014-12-29 22:25:14 +01:00
|
|
|
error.Set(pulse_domain, "suspended");
|
2011-08-23 22:43:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_wait(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (pa_stream_get_state(stream) != PA_STREAM_READY) {
|
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2014-12-29 22:25:14 +01:00
|
|
|
error.Set(pulse_domain, "disconnected");
|
2011-08-23 22:48:15 +02:00
|
|
|
return 0;
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now write */
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (size > writable)
|
2009-10-21 10:30:42 +02:00
|
|
|
/* don't send more than possible */
|
2014-12-29 18:12:16 +01:00
|
|
|
size = writable;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
writable -= size;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
int result = pa_stream_write(stream, chunk, size, nullptr,
|
2013-08-10 18:02:44 +02:00
|
|
|
0, PA_SEEK_RELATIVE);
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2013-08-10 18:02:44 +02:00
|
|
|
if (result < 0) {
|
2014-12-29 18:12:16 +01:00
|
|
|
SetPulseError(error, 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
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
inline void
|
|
|
|
PulseOutput::Cancel()
|
2009-10-21 10:30:42 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(mainloop != nullptr);
|
|
|
|
assert(stream != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_lock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (pa_stream_get_state(stream) != PA_STREAM_READY) {
|
2009-10-21 10:30:42 +02:00
|
|
|
/* no need to flush when the stream isn't connected
|
|
|
|
yet */
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(context != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_operation *o = pa_stream_flush(stream,
|
|
|
|
pulse_output_stream_success_cb,
|
|
|
|
this);
|
2013-04-16 20:51:21 +02:00
|
|
|
if (o == nullptr) {
|
2014-12-29 18:12:16 +01:00
|
|
|
LogPulseError(context, "pa_stream_flush() has failed");
|
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pulse_wait_for_operation(mainloop, o);
|
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
inline bool
|
|
|
|
PulseOutput::Pause()
|
2009-10-21 10:30:42 +02:00
|
|
|
{
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(mainloop != nullptr);
|
|
|
|
assert(stream != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_lock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
/* check if the stream is (already/still) connected */
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2014-12-29 18:12:16 +01:00
|
|
|
if (!WaitStream(error)) {
|
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error);
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(context != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
/* cork the stream */
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
if (!pa_stream_is_corked(stream) && !StreamPause(true, error)) {
|
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error);
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
inline bool
|
|
|
|
PulseOutput::TestDefaultDevice()
|
2009-10-21 10:30:42 +02:00
|
|
|
{
|
2013-08-04 12:25:08 +02:00
|
|
|
const config_param empty;
|
2015-01-04 19:44:45 +01:00
|
|
|
PulseOutput *po = PulseOutput::Create(empty, IgnoreError());
|
2013-04-16 20:51:21 +02:00
|
|
|
if (po == nullptr)
|
2009-10-21 10:30:42 +02:00
|
|
|
return false;
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
bool success = po->WaitConnection(IgnoreError());
|
|
|
|
delete po;
|
2009-10-21 10:30:42 +02:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
static bool
|
|
|
|
pulse_output_test_default_device(void)
|
|
|
|
{
|
|
|
|
return PulseOutput::TestDefaultDevice();
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef AudioOutputWrapper<PulseOutput> Wrapper;
|
|
|
|
|
2014-01-28 11:22:27 +01:00
|
|
|
const struct AudioOutputPlugin pulse_output_plugin = {
|
2013-04-16 20:51:21 +02:00
|
|
|
"pulse",
|
|
|
|
pulse_output_test_default_device,
|
2015-01-04 19:44:45 +01:00
|
|
|
&Wrapper::Init,
|
2014-12-29 18:12:16 +01:00
|
|
|
&Wrapper::Finish,
|
|
|
|
&Wrapper::Enable,
|
|
|
|
&Wrapper::Disable,
|
|
|
|
&Wrapper::Open,
|
|
|
|
&Wrapper::Close,
|
|
|
|
&Wrapper::Delay,
|
2013-04-16 20:51:21 +02:00
|
|
|
nullptr,
|
2014-12-29 18:12:16 +01:00
|
|
|
&Wrapper::Play,
|
2013-04-16 20:51:21 +02:00
|
|
|
nullptr,
|
2014-12-29 18:12:16 +01:00
|
|
|
&Wrapper::Cancel,
|
|
|
|
&Wrapper::Pause,
|
2013-04-16 20:51:21 +02:00
|
|
|
|
|
|
|
&pulse_mixer_plugin,
|
2006-07-13 21:03:49 +02:00
|
|
|
};
|