mixer/pipewire: new plugin

This commit is contained in:
Max Kellermann
2021-08-04 17:35:21 +02:00
parent b838bf3109
commit 968624035c
5 changed files with 180 additions and 2 deletions

View File

@@ -21,6 +21,7 @@
#include "lib/pipewire/ThreadLoop.hxx"
#include "../OutputAPI.hxx"
#include "../Error.hxx"
#include "mixer/plugins/PipeWireMixerPlugin.hxx"
#ifdef __GNUC__
#pragma GCC diagnostic push
@@ -32,6 +33,7 @@
#include <pipewire/pipewire.h>
#include <spa/param/audio/format-utils.h>
#include <spa/param/props.h>
#ifdef __GNUC__
#pragma GCC diagnostic pop
@@ -56,7 +58,18 @@ class PipeWireOutput final : AudioOutput {
const uint32_t target_id;
float volume = 1.0;
bool disconnected;
/**
* Shall the previously known volume be restored as soon as
* PW_STREAM_STATE_STREAMING is reached? This needs to be
* done each time after the pw_stream got created, thus this
* flag gets set by Open().
*/
bool restore_volume;
bool interrupted;
bool paused;
bool drained;
@@ -80,6 +93,8 @@ public:
return events;
}
void SetVolume(float volume);
private:
void CheckThrowError() {
if (disconnected)
@@ -147,6 +162,20 @@ PipeWireOutput::PipeWireOutput(const ConfigBlock &block)
{
}
void
PipeWireOutput::SetVolume(float _volume)
{
const PipeWire::ThreadLoopLock lock(thread_loop);
if (stream != nullptr && !restore_volume &&
pw_stream_set_control(stream,
SPA_PROP_volume, 1, &_volume,
0) != 0)
throw std::runtime_error("pw_stream_set_control() failed");
volume = _volume;
}
void
PipeWireOutput::Enable()
{
@@ -282,6 +311,7 @@ void
PipeWireOutput::Open(AudioFormat &audio_format)
{
disconnected = false;
restore_volume = true;
paused = false;
drained = true;
@@ -292,6 +322,8 @@ PipeWireOutput::Open(AudioFormat &audio_format)
PW_KEY_NODE_NAME, "mpd",
nullptr);
const PipeWire::ThreadLoopLock lock(thread_loop);
stream = pw_stream_new_simple(pw_thread_loop_get_loop(thread_loop),
"mpd",
props,
@@ -317,7 +349,6 @@ PipeWireOutput::Open(AudioFormat &audio_format)
params[0] = spa_format_audio_raw_build(&pod_builder,
SPA_PARAM_EnumFormat, &raw);
const PipeWire::ThreadLoopLock lock(thread_loop);
pw_stream_connect(stream,
PW_DIRECTION_OUTPUT,
target_id,
@@ -333,6 +364,7 @@ PipeWireOutput::Close() noexcept
{
const PipeWire::ThreadLoopLock lock(thread_loop);
pw_stream_destroy(stream);
stream = nullptr;
}
delete ring_buffer;
@@ -347,6 +379,15 @@ PipeWireOutput::StateChanged(enum pw_stream_state state,
state == PW_STREAM_STATE_UNCONNECTED;
if (!was_disconnected && disconnected)
pw_thread_loop_signal(thread_loop, false);
if (state == PW_STREAM_STATE_STREAMING && restore_volume) {
/* restore the last known volume after creating a new
pw_stream */
restore_volume = false;
pw_stream_set_control(stream,
SPA_PROP_volume, 1, &volume,
0);
}
}
inline void
@@ -442,5 +483,11 @@ const struct AudioOutputPlugin pipewire_output_plugin = {
"pipewire",
nullptr,
&PipeWireOutput::Create,
nullptr,
&pipewire_mixer_plugin,
};
void
pipewire_output_set_volume(PipeWireOutput &output, float volume)
{
output.SetVolume(volume);
}

View File

@@ -20,6 +20,11 @@
#ifndef MPD_PIPEWIRE_OUTPUT_PLUGIN_HXX
#define MPD_PIPEWIRE_OUTPUT_PLUGIN_HXX
class PipeWireOutput;
extern const struct AudioOutputPlugin pipewire_output_plugin;
void
pipewire_output_set_volume(PipeWireOutput &output, float volume);
#endif