2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 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"
|
2016-09-09 14:36:24 +02:00
|
|
|
#include "lib/pulse/LockGuard.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "../OutputAPI.hxx"
|
2014-01-24 16:25:21 +01:00
|
|
|
#include "mixer/MixerList.hxx"
|
|
|
|
#include "mixer/plugins/PulseMixerPlugin.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
|
|
|
|
2016-09-09 12:52:51 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
|
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
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
class PulseOutput final : AudioOutput {
|
2011-09-17 19:50:36 +02:00
|
|
|
const char *name;
|
|
|
|
const char *server;
|
|
|
|
const char *sink;
|
|
|
|
|
2016-11-02 11:01:07 +01:00
|
|
|
PulseMixer *mixer = nullptr;
|
2011-09-17 19:50:36 +02:00
|
|
|
|
2016-11-02 11:01:07 +01:00
|
|
|
struct pa_threaded_mainloop *mainloop = nullptr;
|
2011-09-17 19:50:36 +02:00
|
|
|
struct pa_context *context;
|
2016-11-02 11:01:07 +01:00
|
|
|
struct pa_stream *stream = nullptr;
|
2011-09-17 19:50:36 +02:00
|
|
|
|
|
|
|
size_t writable;
|
2014-01-28 23:39:48 +01:00
|
|
|
|
2017-05-23 11:17:04 +02:00
|
|
|
bool pause;
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
explicit PulseOutput(const ConfigBlock &block);
|
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;
|
|
|
|
}
|
|
|
|
|
2016-09-09 12:52:51 +02:00
|
|
|
void SetVolume(const pa_cvolume &volume);
|
2015-01-04 19:20:36 +01:00
|
|
|
|
2016-09-09 14:36:24 +02:00
|
|
|
struct pa_threaded_mainloop *GetMainloop() {
|
|
|
|
return mainloop;
|
2015-01-04 19:20:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2015-01-04 19:42:15 +01:00
|
|
|
Signal();
|
2015-01-04 19:20:36 +01:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
static bool TestDefaultDevice();
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
static AudioOutput *Create(EventLoop &,
|
|
|
|
const ConfigBlock &block) {
|
|
|
|
return new PulseOutput(block);
|
|
|
|
}
|
2014-12-29 18:12:16 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void Enable() override;
|
|
|
|
void Disable() noexcept override;
|
2014-12-29 18:12:16 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void Open(AudioFormat &audio_format) override;
|
|
|
|
void Close() noexcept override;
|
2014-12-29 18:12:16 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
std::chrono::steady_clock::duration Delay() const noexcept override;
|
|
|
|
size_t Play(const void *chunk, size_t size) override;
|
|
|
|
void Cancel() noexcept override;
|
2017-09-08 14:45:53 +02:00
|
|
|
bool Pause() override;
|
2014-12-29 18:12:16 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Attempt to connect asynchronously to the PulseAudio server.
|
|
|
|
*
|
2016-11-02 10:43:25 +01:00
|
|
|
* Throws #std::runtime_error on error.
|
2014-12-29 18:12:16 +01:00
|
|
|
*/
|
2016-11-02 10:43:25 +01:00
|
|
|
void Connect();
|
2014-12-29 18:12:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create, set up and connect a context.
|
|
|
|
*
|
|
|
|
* Caller must lock the main loop.
|
|
|
|
*
|
2016-11-02 10:43:25 +01:00
|
|
|
* Throws #std::runtime_error on error.
|
2014-12-29 18:12:16 +01:00
|
|
|
*/
|
2016-11-02 10:43:25 +01:00
|
|
|
void SetupContext();
|
2014-12-29 18:12:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Frees and clears the context.
|
|
|
|
*
|
|
|
|
* Caller must lock the main loop.
|
|
|
|
*/
|
|
|
|
void DeleteContext();
|
|
|
|
|
2015-01-04 19:42:15 +01:00
|
|
|
void Signal() {
|
|
|
|
pa_threaded_mainloop_signal(mainloop, 0);
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2016-11-02 10:43:25 +01:00
|
|
|
* Throws #std::runtime_error on error.
|
2014-12-29 18:12:16 +01:00
|
|
|
*/
|
2016-11-02 10:43:25 +01:00
|
|
|
void WaitConnection();
|
2014-12-29 18:12:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create, set up and connect a context.
|
|
|
|
*
|
|
|
|
* Caller must lock the main loop.
|
|
|
|
*
|
2016-11-02 10:43:25 +01:00
|
|
|
* Throws #std::runtime_error on error.
|
2014-12-29 18:12:16 +01:00
|
|
|
*/
|
2016-11-02 10:43:25 +01:00
|
|
|
void SetupStream(const pa_sample_spec &ss);
|
2014-12-29 18:12:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2016-11-02 10:43:25 +01:00
|
|
|
* Throws #std::runtime_error on error.
|
2014-12-29 18:12:16 +01:00
|
|
|
*/
|
2016-11-02 10:43:25 +01:00
|
|
|
void WaitStream();
|
2014-12-29 18:12:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets cork mode on the stream.
|
2016-11-02 10:43:25 +01:00
|
|
|
*
|
|
|
|
* Throws #std::runtime_error on error.
|
2014-12-29 18:12:16 +01:00
|
|
|
*/
|
2016-11-02 10:43:25 +01:00
|
|
|
void StreamPause(bool pause);
|
2011-09-17 19:50:36 +02:00
|
|
|
};
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
PulseOutput::PulseOutput(const ConfigBlock &block)
|
2017-08-09 16:58:44 +02:00
|
|
|
:AudioOutput(FLAG_ENABLE_DISABLE|FLAG_PAUSE),
|
2016-11-02 10:43:25 +01:00
|
|
|
name(block.GetBlockValue("name", "mpd_pulse")),
|
|
|
|
server(block.GetBlockValue("server")),
|
|
|
|
sink(block.GetBlockValue("sink"))
|
|
|
|
{
|
|
|
|
setenv("PULSE_PROP_media.role", "music", true);
|
|
|
|
setenv("PULSE_PROP_application.icon_name", "mpd", true);
|
|
|
|
}
|
|
|
|
|
2016-09-09 14:36:24 +02:00
|
|
|
struct pa_threaded_mainloop *
|
|
|
|
pulse_output_get_mainloop(PulseOutput &po)
|
2011-09-17 19:50:36 +02:00
|
|
|
{
|
2016-09-09 14:36:24 +02:00
|
|
|
return po.GetMainloop();
|
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;
|
|
|
|
|
2016-09-09 14:36:24 +02:00
|
|
|
Pulse::LockGuard 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-09-09 12:52:51 +02:00
|
|
|
inline void
|
|
|
|
PulseOutput::SetVolume(const pa_cvolume &volume)
|
2015-01-04 19:20:36 +01:00
|
|
|
{
|
|
|
|
if (context == nullptr || stream == nullptr ||
|
2016-09-09 12:52:51 +02:00
|
|
|
pa_stream_get_state(stream) != PA_STREAM_READY)
|
|
|
|
throw std::runtime_error("disconnected");
|
2009-10-23 10:33:26 +02:00
|
|
|
|
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);
|
2016-09-09 12:52:51 +02:00
|
|
|
if (o == nullptr)
|
|
|
|
throw std::runtime_error("failed to set PulseAudio volume");
|
2009-10-23 10:33:26 +02:00
|
|
|
|
|
|
|
pa_operation_unref(o);
|
|
|
|
}
|
|
|
|
|
2016-09-09 12:52:51 +02:00
|
|
|
void
|
|
|
|
pulse_output_set_volume(PulseOutput &po, const pa_cvolume *volume)
|
2015-01-04 19:20:36 +01:00
|
|
|
{
|
2016-09-09 12:52:51 +02:00
|
|
|
return po.SetVolume(*volume);
|
2015-01-04 19:20:36 +01:00
|
|
|
}
|
|
|
|
|
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:42:15 +01:00
|
|
|
Signal();
|
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:42:15 +01:00
|
|
|
Signal();
|
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
|
|
|
}
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
inline void
|
|
|
|
PulseOutput::Connect()
|
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,
|
2016-11-02 10:43:25 +01:00
|
|
|
(pa_context_flags_t)0, nullptr) < 0)
|
|
|
|
throw MakePulseError(context,
|
|
|
|
"pa_context_connect() has failed");
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
void
|
|
|
|
PulseOutput::SetupContext()
|
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);
|
2016-11-02 10:43:25 +01:00
|
|
|
if (context == nullptr)
|
|
|
|
throw std::runtime_error("pa_context_new() has failed");
|
2009-10-21 10:30:42 +02:00
|
|
|
|
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
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
try {
|
|
|
|
Connect();
|
|
|
|
} catch (...) {
|
2014-12-29 18:12:16 +01:00
|
|
|
DeleteContext();
|
2016-11-02 10:43:25 +01:00
|
|
|
throw;
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
2014-12-29 18:12:16 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
2016-11-09 11:56:01 +01:00
|
|
|
PulseOutput::Enable()
|
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();
|
2016-11-02 10:43:25 +01:00
|
|
|
if (mainloop == nullptr)
|
|
|
|
throw std::runtime_error("pa_threaded_mainloop_new() has failed");
|
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
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
throw std::runtime_error("pa_threaded_mainloop_start() has failed");
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create the libpulse context and connect it */
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
try {
|
|
|
|
SetupContext();
|
|
|
|
} catch (...) {
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
|
|
|
pa_threaded_mainloop_stop(mainloop);
|
|
|
|
pa_threaded_mainloop_free(mainloop);
|
|
|
|
mainloop = nullptr;
|
2016-11-02 10:43:25 +01:00
|
|
|
throw;
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
|
|
|
PulseOutput::Disable() noexcept
|
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
|
|
|
}
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
void
|
|
|
|
PulseOutput::WaitConnection()
|
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
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
if (context == nullptr)
|
|
|
|
SetupContext();
|
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 */
|
2016-11-02 10:43:25 +01:00
|
|
|
return;
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
case PA_CONTEXT_UNCONNECTED:
|
|
|
|
case PA_CONTEXT_TERMINATED:
|
|
|
|
case PA_CONTEXT_FAILED:
|
|
|
|
/* failure */
|
2016-11-02 10:43:25 +01:00
|
|
|
{
|
|
|
|
auto e = MakePulseError(context,
|
|
|
|
"failed to connect");
|
|
|
|
DeleteContext();
|
|
|
|
throw e;
|
|
|
|
}
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
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:42:15 +01:00
|
|
|
Signal();
|
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:42:15 +01:00
|
|
|
Signal();
|
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:42:15 +01:00
|
|
|
Signal();
|
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;
|
2015-01-04 19:42:15 +01:00
|
|
|
Signal();
|
2015-01-04 19:20:36 +01: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)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
inline void
|
|
|
|
PulseOutput::SetupStream(const pa_sample_spec &ss)
|
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);
|
2016-11-02 10:43:25 +01:00
|
|
|
if (stream == nullptr)
|
|
|
|
throw MakePulseError(context,
|
|
|
|
"pa_stream_new() has failed");
|
2011-09-16 22:58:19 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
2016-11-09 11:56:01 +01:00
|
|
|
PulseOutput::Open(AudioFormat &audio_format)
|
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
|
|
|
|
2016-09-09 14:36:24 +02:00
|
|
|
Pulse::LockGuard 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
WaitConnection();
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2016-08-14 18:45:32 +02:00
|
|
|
/* Use the sample formats that our version of PulseAudio and MPD
|
|
|
|
have in common, otherwise force MPD to send 16 bit */
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_sample_spec ss;
|
2016-08-14 18:45:32 +02:00
|
|
|
|
|
|
|
switch (audio_format.format) {
|
|
|
|
case SampleFormat::FLOAT:
|
|
|
|
ss.format = PA_SAMPLE_FLOAT32NE;
|
|
|
|
break;
|
|
|
|
case SampleFormat::S32:
|
|
|
|
ss.format = PA_SAMPLE_S32NE;
|
|
|
|
break;
|
|
|
|
case SampleFormat::S24_P32:
|
|
|
|
ss.format = PA_SAMPLE_S24_32NE;
|
|
|
|
break;
|
|
|
|
case SampleFormat::S16:
|
|
|
|
ss.format = PA_SAMPLE_S16NE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
audio_format.format = SampleFormat::S16;
|
|
|
|
ss.format = PA_SAMPLE_S16NE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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 .. */
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
SetupStream(ss);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
/* .. 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
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
throw MakePulseError(context,
|
|
|
|
"pa_stream_connect_playback() has failed");
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
2017-05-23 11:17:04 +02:00
|
|
|
|
|
|
|
pause = false;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
|
|
|
PulseOutput::Close() noexcept
|
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
|
|
|
|
2016-09-09 14:36:24 +02:00
|
|
|
Pulse::LockGuard 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
|
|
|
}
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
void
|
|
|
|
PulseOutput::WaitStream()
|
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:
|
2016-11-02 10:43:25 +01:00
|
|
|
return;
|
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:
|
2016-11-02 10:43:25 +01:00
|
|
|
throw MakePulseError(context,
|
|
|
|
"failed to connect the stream");
|
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
|
|
|
}
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
void
|
2017-05-23 11:17:04 +02:00
|
|
|
PulseOutput::StreamPause(bool _pause)
|
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
|
|
|
|
2017-05-23 11:17:04 +02:00
|
|
|
pa_operation *o = pa_stream_cork(stream, _pause,
|
2014-12-29 18:12:16 +01:00
|
|
|
pulse_output_stream_success_cb, this);
|
2016-11-02 10:43:25 +01:00
|
|
|
if (o == nullptr)
|
|
|
|
throw MakePulseError(context,
|
|
|
|
"pa_stream_cork() has failed");
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
if (!pulse_wait_for_operation(mainloop, o))
|
|
|
|
throw MakePulseError(context,
|
|
|
|
"pa_stream_cork() has failed");
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
std::chrono::steady_clock::duration
|
|
|
|
PulseOutput::Delay() const noexcept
|
2012-08-14 22:30:46 +02:00
|
|
|
{
|
2016-09-09 14:36:24 +02:00
|
|
|
Pulse::LockGuard lock(mainloop);
|
2012-08-14 22:30:46 +02:00
|
|
|
|
2016-12-28 21:44:18 +01:00
|
|
|
auto result = std::chrono::steady_clock::duration::zero();
|
2017-05-23 11:17:04 +02:00
|
|
|
if (pause && pa_stream_is_corked(stream) &&
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_stream_get_state(stream) == PA_STREAM_READY)
|
2012-08-14 22:30:46 +02:00
|
|
|
/* idle while paused */
|
2016-12-28 21:44:18 +01:00
|
|
|
result = std::chrono::seconds(1);
|
2012-08-14 22:30:46 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
size_t
|
2016-11-09 11:56:01 +01:00
|
|
|
PulseOutput::Play(const void *chunk, size_t size)
|
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
|
|
|
|
2016-09-09 14:36:24 +02:00
|
|
|
Pulse::LockGuard lock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2017-05-23 11:17:04 +02:00
|
|
|
pause = false;
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
/* check if the stream is (already) connected */
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
WaitStream();
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(context != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
/* unpause if previously paused */
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
if (pa_stream_is_corked(stream))
|
|
|
|
StreamPause(false);
|
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) {
|
2016-11-02 10:43:25 +01:00
|
|
|
if (pa_stream_is_suspended(stream))
|
|
|
|
throw std::runtime_error("suspended");
|
2011-08-23 22:43:08 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pa_threaded_mainloop_wait(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
if (pa_stream_get_state(stream) != PA_STREAM_READY)
|
|
|
|
throw std::runtime_error("disconnected");
|
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);
|
2016-11-02 10:43:25 +01:00
|
|
|
if (result < 0)
|
|
|
|
throw MakePulseError(context, "pa_stream_write() failed");
|
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
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
|
|
|
PulseOutput::Cancel() noexcept
|
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
|
|
|
|
2016-09-09 14:36:24 +02:00
|
|
|
Pulse::LockGuard 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 */
|
|
|
|
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");
|
2009-10-21 10:30:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
pulse_wait_for_operation(mainloop, o);
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
bool
|
2017-09-08 14:45:53 +02:00
|
|
|
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
|
|
|
|
2016-09-09 14:36:24 +02:00
|
|
|
Pulse::LockGuard lock(mainloop);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2017-05-23 11:17:04 +02:00
|
|
|
pause = true;
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
/* check if the stream is (already/still) connected */
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
WaitStream();
|
2009-10-21 10:30:42 +02:00
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
assert(context != nullptr);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
/* cork the stream */
|
|
|
|
|
2016-11-02 10:43:25 +01:00
|
|
|
if (!pa_stream_is_corked(stream))
|
|
|
|
StreamPause(true);
|
2009-10-21 10:30:42 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
inline bool
|
|
|
|
PulseOutput::TestDefaultDevice()
|
2016-11-02 10:43:25 +01:00
|
|
|
try {
|
2015-01-21 22:13:44 +01:00
|
|
|
const ConfigBlock empty;
|
2016-11-02 10:43:25 +01:00
|
|
|
PulseOutput po(empty);
|
|
|
|
po.WaitConnection();
|
|
|
|
return true;
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
return false;
|
2009-10-21 10:30:42 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:12:16 +01:00
|
|
|
static bool
|
|
|
|
pulse_output_test_default_device(void)
|
|
|
|
{
|
|
|
|
return PulseOutput::TestDefaultDevice();
|
|
|
|
}
|
|
|
|
|
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,
|
2017-08-09 16:58:44 +02:00
|
|
|
PulseOutput::Create,
|
2013-04-16 20:51:21 +02:00
|
|
|
&pulse_mixer_plugin,
|
2006-07-13 21:03:49 +02:00
|
|
|
};
|