2021-12-24 07:43:20 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2022-01-05 08:42:46 +00:00
|
|
|
* Copyright © 2017-2022 The Looking Glass Authors
|
2021-12-24 07:43:20 +00:00
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "interface/audiodev.h"
|
|
|
|
|
|
|
|
#include <spa/param/audio/format-utils.h>
|
2021-12-25 09:37:52 +00:00
|
|
|
#include <spa/param/props.h>
|
2021-12-24 07:43:20 +00:00
|
|
|
#include <pipewire/pipewire.h>
|
2021-12-25 09:53:11 +00:00
|
|
|
#include <math.h>
|
2021-12-24 07:43:20 +00:00
|
|
|
|
|
|
|
#include "common/debug.h"
|
|
|
|
#include "common/ringbuffer.h"
|
|
|
|
|
|
|
|
struct PipeWire
|
|
|
|
{
|
|
|
|
struct pw_loop * loop;
|
|
|
|
struct pw_thread_loop * thread;
|
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
struct pw_stream * stream;
|
|
|
|
|
|
|
|
int channels;
|
|
|
|
int sampleRate;
|
|
|
|
int stride;
|
|
|
|
|
|
|
|
RingBuffer buffer;
|
|
|
|
bool active;
|
|
|
|
}
|
|
|
|
playback;
|
2021-12-24 07:43:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct PipeWire pw = {0};
|
|
|
|
|
|
|
|
static void pipewire_on_process(void * userdata)
|
|
|
|
{
|
|
|
|
struct pw_buffer * pbuf;
|
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
if (!ringbuffer_getCount(pw.playback.buffer))
|
2021-12-24 07:43:20 +00:00
|
|
|
return;
|
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
if (!(pbuf = pw_stream_dequeue_buffer(pw.playback.stream)))
|
|
|
|
{
|
2021-12-24 07:43:20 +00:00
|
|
|
DEBUG_WARN("out of buffers");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct spa_buffer * sbuf = pbuf->buffer;
|
|
|
|
uint8_t * dst;
|
|
|
|
|
|
|
|
if (!(dst = sbuf->datas[0].data))
|
|
|
|
return;
|
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
int frames = sbuf->datas[0].maxsize / pw.playback.stride;
|
|
|
|
void * values = ringbuffer_consume(pw.playback.buffer, &frames);
|
|
|
|
memcpy(dst, values, frames * pw.playback.stride);
|
2021-12-24 07:43:20 +00:00
|
|
|
|
|
|
|
sbuf->datas[0].chunk->offset = 0;
|
2022-01-06 12:56:12 +00:00
|
|
|
sbuf->datas[0].chunk->stride = pw.playback.stride;
|
|
|
|
sbuf->datas[0].chunk->size = frames * pw.playback.stride;
|
2021-12-24 07:43:20 +00:00
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
pw_stream_queue_buffer(pw.playback.stream, pbuf);
|
2021-12-24 07:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool pipewire_init(void)
|
|
|
|
{
|
|
|
|
pw_init(NULL, NULL);
|
|
|
|
|
|
|
|
pw.loop = pw_loop_new(NULL);
|
|
|
|
struct pw_context * context = pw_context_new(pw.loop, NULL, 0);
|
|
|
|
if (!context)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to create a context");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is just to test for PipeWire availabillity */
|
|
|
|
struct pw_core * core = pw_context_connect(context, NULL, 0);
|
|
|
|
if (!core)
|
|
|
|
goto err_context;
|
|
|
|
|
|
|
|
pw_context_destroy(context);
|
|
|
|
|
|
|
|
/* PipeWire is available so create the loop thread and start it */
|
2022-01-06 12:56:12 +00:00
|
|
|
pw.thread = pw_thread_loop_new_full(pw.loop, "PipeWire", NULL);
|
2021-12-24 07:43:20 +00:00
|
|
|
if (!pw.thread)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to create the thread loop");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
pw_thread_loop_start(pw.thread);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
err_context:
|
|
|
|
pw_context_destroy(context);
|
|
|
|
|
|
|
|
err:
|
2021-12-25 04:03:50 +00:00
|
|
|
pw_loop_destroy(pw.loop);
|
2021-12-24 07:43:20 +00:00
|
|
|
pw_deinit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-06 13:09:34 +00:00
|
|
|
static void pipewire_playbackStopStream(void)
|
2021-12-24 15:38:11 +00:00
|
|
|
{
|
2022-01-06 12:56:12 +00:00
|
|
|
if (!pw.playback.stream)
|
2021-12-24 15:38:11 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
pw_thread_loop_lock(pw.thread);
|
2022-01-06 12:56:12 +00:00
|
|
|
pw_stream_flush(pw.playback.stream, true);
|
|
|
|
pw_stream_destroy(pw.playback.stream);
|
|
|
|
pw.playback.stream = NULL;
|
2021-12-24 15:38:11 +00:00
|
|
|
pw_thread_loop_unlock(pw.thread);
|
|
|
|
}
|
|
|
|
|
2021-12-25 04:03:50 +00:00
|
|
|
static void pipewire_free(void)
|
|
|
|
{
|
2022-01-06 13:09:34 +00:00
|
|
|
pipewire_playbackStopStream();
|
2021-12-25 04:03:50 +00:00
|
|
|
pw_thread_loop_stop(pw.thread);
|
|
|
|
pw_thread_loop_destroy(pw.thread);
|
|
|
|
pw_loop_destroy(pw.loop);
|
|
|
|
|
|
|
|
pw.loop = NULL;
|
|
|
|
pw.thread = NULL;
|
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
ringbuffer_free(&pw.playback.buffer);
|
2021-12-25 04:03:50 +00:00
|
|
|
pw_deinit();
|
|
|
|
}
|
|
|
|
|
2022-01-06 12:58:02 +00:00
|
|
|
static void pipewire_playbackStart(int channels, int sampleRate)
|
2021-12-24 07:43:20 +00:00
|
|
|
{
|
|
|
|
const struct spa_pod * params[1];
|
|
|
|
uint8_t buffer[1024];
|
|
|
|
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
|
|
|
|
static const struct pw_stream_events events =
|
|
|
|
{
|
|
|
|
.version = PW_VERSION_STREAM_EVENTS,
|
|
|
|
.process = pipewire_on_process
|
|
|
|
};
|
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
if (pw.playback.stream &&
|
|
|
|
pw.playback.channels == channels &&
|
|
|
|
pw.playback.sampleRate == sampleRate)
|
2021-12-25 02:44:40 +00:00
|
|
|
return;
|
|
|
|
|
2022-01-06 13:09:34 +00:00
|
|
|
pipewire_playbackStopStream();
|
2021-12-24 15:38:11 +00:00
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
pw.playback.channels = channels;
|
|
|
|
pw.playback.sampleRate = sampleRate;
|
|
|
|
pw.playback.stride = sizeof(uint16_t) * channels;
|
|
|
|
pw.playback.buffer = ringbuffer_new(sampleRate / 10, channels * sizeof(uint16_t));
|
2021-12-24 07:43:20 +00:00
|
|
|
|
|
|
|
pw_thread_loop_lock(pw.thread);
|
2022-01-06 12:56:12 +00:00
|
|
|
pw.playback.stream = pw_stream_new_simple(
|
2021-12-24 07:43:20 +00:00
|
|
|
pw.loop,
|
2021-12-26 07:49:35 +00:00
|
|
|
"Looking Glass",
|
2021-12-24 07:43:20 +00:00
|
|
|
pw_properties_new(
|
2021-12-24 15:57:38 +00:00
|
|
|
PW_KEY_NODE_NAME , "Looking Glass",
|
2021-12-24 07:43:20 +00:00
|
|
|
PW_KEY_MEDIA_TYPE , "Audio",
|
|
|
|
PW_KEY_MEDIA_CATEGORY, "Playback",
|
|
|
|
PW_KEY_MEDIA_ROLE , "Music",
|
|
|
|
NULL
|
|
|
|
),
|
|
|
|
&events,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
if (!pw.playback.stream)
|
2021-12-24 07:43:20 +00:00
|
|
|
{
|
|
|
|
pw_thread_loop_unlock(pw.thread);
|
|
|
|
DEBUG_ERROR("Failed to create the stream");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
params[0] = spa_format_audio_raw_build(&b, SPA_PARAM_EnumFormat,
|
|
|
|
&SPA_AUDIO_INFO_RAW_INIT(
|
|
|
|
.format = SPA_AUDIO_FORMAT_S16,
|
|
|
|
.channels = channels,
|
|
|
|
.rate = sampleRate
|
|
|
|
));
|
|
|
|
|
|
|
|
pw_stream_connect(
|
2022-01-06 12:56:12 +00:00
|
|
|
pw.playback.stream,
|
2021-12-24 07:43:20 +00:00
|
|
|
PW_DIRECTION_OUTPUT,
|
|
|
|
PW_ID_ANY,
|
|
|
|
PW_STREAM_FLAG_AUTOCONNECT |
|
|
|
|
PW_STREAM_FLAG_MAP_BUFFERS |
|
2021-12-24 22:40:41 +00:00
|
|
|
PW_STREAM_FLAG_RT_PROCESS |
|
|
|
|
PW_STREAM_FLAG_INACTIVE,
|
2021-12-24 07:43:20 +00:00
|
|
|
params, 1);
|
|
|
|
|
|
|
|
pw_thread_loop_unlock(pw.thread);
|
|
|
|
}
|
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
static void pipewire_playbackPlay(uint8_t * data, size_t size)
|
2021-12-24 07:43:20 +00:00
|
|
|
{
|
2022-01-06 12:56:12 +00:00
|
|
|
if (!pw.playback.stream)
|
2021-12-24 07:43:20 +00:00
|
|
|
return;
|
|
|
|
|
2021-12-25 02:36:08 +00:00
|
|
|
// if the buffer fill is higher then the average skip the update to reduce lag
|
|
|
|
static unsigned int ttlSize = 0;
|
|
|
|
static unsigned int count = 0;
|
|
|
|
ttlSize += size;
|
2022-01-06 12:56:12 +00:00
|
|
|
if (++count > 100 && ringbuffer_getCount(pw.playback.buffer) > ttlSize / count)
|
2021-12-25 02:36:08 +00:00
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
ttlSize = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
ringbuffer_append(pw.playback.buffer, data, size / pw.playback.stride);
|
2021-12-24 22:40:41 +00:00
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
if (!pw.playback.active)
|
2021-12-24 22:40:41 +00:00
|
|
|
{
|
|
|
|
pw_thread_loop_lock(pw.thread);
|
2022-01-06 12:56:12 +00:00
|
|
|
pw_stream_set_active(pw.playback.stream, true);
|
|
|
|
pw.playback.active = true;
|
2021-12-24 22:40:41 +00:00
|
|
|
pw_thread_loop_unlock(pw.thread);
|
|
|
|
}
|
2021-12-24 07:43:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
static void pipewire_playbackStop(void)
|
2021-12-24 07:43:20 +00:00
|
|
|
{
|
2022-01-06 12:56:12 +00:00
|
|
|
if (!pw.playback.active)
|
2021-12-24 22:40:41 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
pw_thread_loop_lock(pw.thread);
|
2022-01-06 12:56:12 +00:00
|
|
|
pw_stream_set_active(pw.playback.stream, false);
|
|
|
|
pw.playback.active = false;
|
2021-12-24 22:40:41 +00:00
|
|
|
pw_thread_loop_unlock(pw.thread);
|
2021-12-24 07:43:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
static void pipewire_playbackVolume(int channels, const uint16_t volume[])
|
2021-12-25 09:37:52 +00:00
|
|
|
{
|
2022-01-06 12:56:12 +00:00
|
|
|
if (channels != pw.playback.channels)
|
2021-12-25 09:37:52 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
float param[channels];
|
|
|
|
for(int i = 0; i < channels; ++i)
|
2021-12-25 09:53:11 +00:00
|
|
|
param[i] = 9.3234e-7 * pow(1.000211902, volume[i]) - 0.000172787;
|
2021-12-25 09:37:52 +00:00
|
|
|
|
|
|
|
pw_thread_loop_lock(pw.thread);
|
2022-01-06 12:56:12 +00:00
|
|
|
pw_stream_set_control(pw.playback.stream, SPA_PROP_channelVolumes,
|
|
|
|
channels, param, 0);
|
2021-12-25 09:37:52 +00:00
|
|
|
pw_thread_loop_unlock(pw.thread);
|
|
|
|
}
|
|
|
|
|
2022-01-06 12:56:12 +00:00
|
|
|
static void pipewire_playbackMute(bool mute)
|
2021-12-25 09:37:52 +00:00
|
|
|
{
|
|
|
|
pw_thread_loop_lock(pw.thread);
|
2022-01-06 12:56:12 +00:00
|
|
|
pw_stream_set_control(pw.playback.stream, SPA_PROP_mute, 1, (void *)&mute, 0);
|
2021-12-25 09:37:52 +00:00
|
|
|
pw_thread_loop_unlock(pw.thread);
|
|
|
|
}
|
|
|
|
|
2021-12-24 07:43:20 +00:00
|
|
|
struct LG_AudioDevOps LGAD_PipeWire =
|
|
|
|
{
|
2021-12-25 09:37:52 +00:00
|
|
|
.name = "PipeWire",
|
|
|
|
.init = pipewire_init,
|
|
|
|
.free = pipewire_free,
|
2022-01-06 11:47:22 +00:00
|
|
|
.playback =
|
|
|
|
{
|
2022-01-06 12:58:02 +00:00
|
|
|
.start = pipewire_playbackStart,
|
2022-01-06 12:56:12 +00:00
|
|
|
.play = pipewire_playbackPlay,
|
|
|
|
.stop = pipewire_playbackStop,
|
|
|
|
.volume = pipewire_playbackVolume,
|
|
|
|
.mute = pipewire_playbackMute
|
2022-01-06 11:47:22 +00:00
|
|
|
}
|
2021-12-24 07:43:20 +00:00
|
|
|
};
|