output/pulse: move more code into the struct

This commit is contained in:
Max Kellermann 2015-01-04 19:20:36 +01:00
parent f016eef48a
commit f3a160038d
1 changed files with 155 additions and 82 deletions

View File

@ -62,6 +62,37 @@ struct PulseOutput {
mixer(nullptr), mixer(nullptr),
mainloop(nullptr), stream(nullptr) {} mainloop(nullptr), stream(nullptr) {}
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);
}
gcc_const gcc_const
static bool TestDefaultDevice(); static bool TestDefaultDevice();
@ -146,64 +177,66 @@ private:
void void
pulse_output_lock(PulseOutput &po) pulse_output_lock(PulseOutput &po)
{ {
pa_threaded_mainloop_lock(po.mainloop); po.Lock();
} }
void void
pulse_output_unlock(PulseOutput &po) pulse_output_unlock(PulseOutput &po)
{ {
pa_threaded_mainloop_unlock(po.mainloop); po.Unlock();
}
inline void
PulseOutput::SetMixer(PulseMixer &_mixer)
{
assert(mixer == nullptr);
mixer = &_mixer;
if (mainloop == nullptr)
return;
pa_threaded_mainloop_lock(mainloop);
if (context != nullptr &&
pa_context_get_state(context) == PA_CONTEXT_READY) {
pulse_mixer_on_connect(_mixer, context);
if (stream != nullptr &&
pa_stream_get_state(stream) == PA_STREAM_READY)
pulse_mixer_on_change(_mixer, context, stream);
}
pa_threaded_mainloop_unlock(mainloop);
} }
void void
pulse_output_set_mixer(PulseOutput &po, PulseMixer &pm) pulse_output_set_mixer(PulseOutput &po, PulseMixer &pm)
{ {
assert(po.mixer == nullptr); po.SetMixer(pm);
po.mixer = ±
if (po.mainloop == nullptr)
return;
pa_threaded_mainloop_lock(po.mainloop);
if (po.context != nullptr &&
pa_context_get_state(po.context) == PA_CONTEXT_READY) {
pulse_mixer_on_connect(pm, po.context);
if (po.stream != nullptr &&
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 void
pulse_output_clear_mixer(PulseOutput &po, gcc_unused PulseMixer &pm) pulse_output_clear_mixer(PulseOutput &po, PulseMixer &pm)
{ {
assert(po.mixer == &pm); po.ClearMixer(pm);
po.mixer = nullptr;
} }
bool inline bool
pulse_output_set_volume(PulseOutput &po, const pa_cvolume *volume, PulseOutput::SetVolume(const pa_cvolume &volume, Error &error)
Error &error)
{ {
pa_operation *o; if (context == nullptr || stream == nullptr ||
pa_stream_get_state(stream) != PA_STREAM_READY) {
if (po.context == nullptr || po.stream == nullptr ||
pa_stream_get_state(po.stream) != PA_STREAM_READY) {
error.Set(pulse_domain, "disconnected"); error.Set(pulse_domain, "disconnected");
return false; return false;
} }
o = pa_context_set_sink_input_volume(po.context, pa_operation *o =
pa_stream_get_index(po.stream), pa_context_set_sink_input_volume(context,
volume, nullptr, nullptr); pa_stream_get_index(stream),
&volume, nullptr, nullptr);
if (o == nullptr) { if (o == nullptr) {
SetPulseError(error, po.context, SetPulseError(error, context,
"failed to set PulseAudio volume"); "failed to set PulseAudio volume");
return false; return false;
} }
@ -212,6 +245,13 @@ pulse_output_set_volume(PulseOutput &po, const pa_cvolume *volume,
return true; return true;
} }
bool
pulse_output_set_volume(PulseOutput &po, const pa_cvolume *volume,
Error &error)
{
return po.SetVolume(*volume, error);
}
/** /**
* \brief waits for a pulseaudio operation to finish, frees it and * \brief waits for a pulseaudio operation to finish, frees it and
* unlocks the mainloop * unlocks the mainloop
@ -244,32 +284,30 @@ static void
pulse_output_stream_success_cb(gcc_unused pa_stream *s, pulse_output_stream_success_cb(gcc_unused pa_stream *s,
gcc_unused int success, void *userdata) gcc_unused int success, void *userdata)
{ {
PulseOutput *po = (PulseOutput *)userdata; PulseOutput &po = *(PulseOutput *)userdata;
pa_threaded_mainloop_signal(po->mainloop, 0); po.OnStreamSuccess();
} }
static void inline void
pulse_output_context_state_cb(struct pa_context *context, void *userdata) PulseOutput::OnContextStateChanged(pa_context_state_t new_state)
{ {
PulseOutput *po = (PulseOutput *)userdata; switch (new_state) {
switch (pa_context_get_state(context)) {
case PA_CONTEXT_READY: case PA_CONTEXT_READY:
if (po->mixer != nullptr) if (mixer != nullptr)
pulse_mixer_on_connect(*po->mixer, context); pulse_mixer_on_connect(*mixer, context);
pa_threaded_mainloop_signal(po->mainloop, 0); pa_threaded_mainloop_signal(mainloop, 0);
break; break;
case PA_CONTEXT_TERMINATED: case PA_CONTEXT_TERMINATED:
case PA_CONTEXT_FAILED: case PA_CONTEXT_FAILED:
if (po->mixer != nullptr) if (mixer != nullptr)
pulse_mixer_on_disconnect(*po->mixer); pulse_mixer_on_disconnect(*mixer);
/* the caller thread might be waiting for these /* the caller thread might be waiting for these
states */ states */
pa_threaded_mainloop_signal(po->mainloop, 0); pa_threaded_mainloop_signal(mainloop, 0);
break; break;
case PA_CONTEXT_UNCONNECTED: case PA_CONTEXT_UNCONNECTED:
@ -281,24 +319,40 @@ pulse_output_context_state_cb(struct pa_context *context, void *userdata)
} }
static void static void
pulse_output_subscribe_cb(pa_context *context, pulse_output_context_state_cb(struct pa_context *context, void *userdata)
pa_subscription_event_type_t t, {
uint32_t idx, 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)
{ {
PulseOutput *po = (PulseOutput *)userdata;
pa_subscription_event_type_t facility = pa_subscription_event_type_t facility =
pa_subscription_event_type_t(t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK); pa_subscription_event_type_t(t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK);
pa_subscription_event_type_t type = pa_subscription_event_type_t type =
pa_subscription_event_type_t(t & PA_SUBSCRIPTION_EVENT_TYPE_MASK); pa_subscription_event_type_t(t & PA_SUBSCRIPTION_EVENT_TYPE_MASK);
if (po->mixer != nullptr && if (mixer != nullptr &&
facility == PA_SUBSCRIPTION_EVENT_SINK_INPUT && facility == PA_SUBSCRIPTION_EVENT_SINK_INPUT &&
po->stream != nullptr && stream != nullptr &&
pa_stream_get_state(po->stream) == PA_STREAM_READY && pa_stream_get_state(stream) == PA_STREAM_READY &&
idx == pa_stream_get_index(po->stream) && idx == pa_stream_get_index(stream) &&
(type == PA_SUBSCRIPTION_EVENT_NEW || (type == PA_SUBSCRIPTION_EVENT_NEW ||
type == PA_SUBSCRIPTION_EVENT_CHANGE)) type == PA_SUBSCRIPTION_EVENT_CHANGE))
pulse_mixer_on_change(*po->mixer, context, po->stream); 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);
} }
inline bool inline bool
@ -485,42 +539,47 @@ PulseOutput::WaitConnection(Error &error)
} }
} }
static void inline void
pulse_output_stream_suspended_cb(gcc_unused pa_stream *stream, void *userdata) PulseOutput::OnStreamSuspended(gcc_unused pa_stream *_stream)
{ {
PulseOutput *po = (PulseOutput *)userdata; assert(_stream == stream || stream == nullptr);
assert(mainloop != nullptr);
assert(stream == po->stream || po->stream == nullptr);
assert(po->mainloop != nullptr);
/* wake up the main loop to break out of the loop in /* wake up the main loop to break out of the loop in
pulse_output_play() */ pulse_output_play() */
pa_threaded_mainloop_signal(po->mainloop, 0); pa_threaded_mainloop_signal(mainloop, 0);
} }
static void static void
pulse_output_stream_state_cb(pa_stream *stream, void *userdata) pulse_output_stream_suspended_cb(pa_stream *stream, void *userdata)
{ {
PulseOutput *po = (PulseOutput *)userdata; PulseOutput &po = *(PulseOutput *)userdata;
assert(stream == po->stream || po->stream == nullptr); po.OnStreamSuspended(stream);
assert(po->mainloop != nullptr); }
assert(po->context != nullptr);
switch (pa_stream_get_state(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);
switch (new_state) {
case PA_STREAM_READY: case PA_STREAM_READY:
if (po->mixer != nullptr) if (mixer != nullptr)
pulse_mixer_on_change(*po->mixer, po->context, stream); pulse_mixer_on_change(*mixer, context, _stream);
pa_threaded_mainloop_signal(po->mainloop, 0); pa_threaded_mainloop_signal(mainloop, 0);
break; break;
case PA_STREAM_FAILED: case PA_STREAM_FAILED:
case PA_STREAM_TERMINATED: case PA_STREAM_TERMINATED:
if (po->mixer != nullptr) if (mixer != nullptr)
pulse_mixer_on_disconnect(*po->mixer); pulse_mixer_on_disconnect(*mixer);
pa_threaded_mainloop_signal(po->mainloop, 0); pa_threaded_mainloop_signal(mainloop, 0);
break; break;
case PA_STREAM_UNCONNECTED: case PA_STREAM_UNCONNECTED:
@ -529,16 +588,30 @@ pulse_output_stream_state_cb(pa_stream *stream, void *userdata)
} }
} }
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);
}
static void static void
pulse_output_stream_write_cb(gcc_unused pa_stream *stream, size_t nbytes, pulse_output_stream_write_cb(gcc_unused pa_stream *stream, size_t nbytes,
void *userdata) void *userdata)
{ {
PulseOutput *po = (PulseOutput *)userdata; PulseOutput &po = *(PulseOutput *)userdata;
assert(po->mainloop != nullptr); return po.OnStreamWrite(nbytes);
po->writable = nbytes;
pa_threaded_mainloop_signal(po->mainloop, 0);
} }
inline bool inline bool