Version 5.0 Major Release.
New Features:
Multi-Channel and High-Resolution Audio Support
48,000 frames per second ("48k") operation.
48k lossless stereo support.
5.1 and 7.1 surround sound support.
Multi-channel and multi-rate operation on ALSA, PipeWire, PulseAudio, FreeBSD, stdout and Unix pipe output backends.
Automatic Audio Format Selection
Flexible and controllable output format selection.
Automatic rate, sample format, and channel count selection.
Full FFmpeg Integration
Support for transcoding.
Advanced resampling capabilities.
New audio format support.
Enhanced Resampling
New vernier resampling and interpolation method optimized for low-power CPUs.
Better performance on resource-constrained devices.
Convolution and Loudness Enhancements:
Convolution system is now multithreaded and works on stereo and multichannel audio at 48k and 44.1k.
Multiple impulse response (IR) files can now be provided via convolution_ir_files setting.
New convolution_thread_pool_size setting for multithreaded processing (defaults to 1).
Loudness processing now works with stereo and multichannel audio at 48k and 44.1k.
Updated to the most recent HiFi-LoFi FFT convolver.
MQTT Enhancements:
Added new publish_retain boolean option. When enabled, published MQTT messages have the retain flag set, so the MQTT broker stores the last message per topic and new subscribers receive the most recent value immediately. Thanks to lululombard for PR #2142.
D-Bus Enhancements:
Added new dbus_default_message_bus command-line argument (can be system or session) to set the default message bus for both D-Bus native service and MPRIS service.
Performance Improvements:
Enhanced compatibility with AirPlay 2 AutoMix and Smart Tracklists resulting in less unexplained track skipping.
Better operation on low-power devices down to Raspberry Pi B.
Improved efficiency on embedded systems.
Enhanced timestamp handling for better synchronization.
Improved sync error calculation.
Rebuilt buffered audio processor for cleaner handling of immediate and deferred flush requests.
Docker Enhancements:
Reduced Docker image sizes with slimmed-down FFmpeg library.
Removed dhclient from Docker images for smaller footprint.
Bug Fixes:
Fixed MQTT warning on service startup: "Could not establish a mqtt connection". The startup script now correctly states that the mosquitto service is required. Thanks to Hugo Villeneuve for PR #2137.
Fixed compatibility with mbedtls library version 3.4+ (present on recent Linux versions). Thanks to Christian Beier for finding and fixing the bug.
Fixed PulseAudio backend so that PA_ERR_NODATA returns "No latency data yet". Thanks to Vladimir Shakov for the report and fix.
Ensured old flush requests are deleted when a new play session starts. Thanks to saujanyashah for the report.
Fixed format warnings on 64-bit and 32-bit systems
Removed compilation warnings on 32-bit builds
Improved argument checking for debug(), inform(), warn() and die() functions
Fixed "daemon" typos throughout codebase. Thanks to Chris Boot for PR #1981.
Added warning if a convolution impulse response file cannot be read due to bad path or permissions
Build System Improvements:
Unified service file with variable substitution for Avahi support, making it easier to add future service dependencies. Thanks to Hugo Villeneuve.
Network interface selection now only considers interfaces that are up, running and not loopback interfaces. Thanks to Carl Johnson for the suggestion.
Configuration File Changes and Deprecations
New settings: convolution_ir_files (replaces convolution_ir_file), convolution_enabled (replaces convolution), convolution_max_length_in_seconds (replaces convolution_max_length), loudness_enabled (replaces loudness).
New convolution_thread_pool_size setting (defaults to 1).
Deprecated settings: convolution_ir_file, convolution, convolution_max_length, loudness.
Corresponding D-Bus methods and properties have been updated.
Deprecation Notice:
The Jack Audio and soundio backends are deprecated and will be removed in a future release. Consider using the updated PipeWire backend instead.
Documentation Updates
Updated BUILD.md with latest build instructions.
Updated AIRPLAY2.md with feature information.
Enhanced convolution and loudness documentation.
Maintenance:
Fixed FFmpeg deprecation warnings.
Bumped actions/checkout from 6.0.1 to 6.0.2.
Bumped docker/login-action from 3.6.0 to 3.7.0.
Bumped docker/build-push-action from 6.13.0 to 6.15.0.
Bumped docker/setup-qemu-action from 3.4.0 to 3.6.0.
Bumped docker/setup-buildx-action from 3.9.0 to 3.10.0.
This commit is contained in:
+360
-169
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Asynchronous PipeWire Backend. This file is part of Shairport Sync.
|
||||
* Copyright (c) Mike Brady 2023
|
||||
* Copyright (c) Mike Brady 2024--2025
|
||||
* All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
@@ -39,65 +39,81 @@
|
||||
#include <pipewire/pipewire.h>
|
||||
#include <spa/param/audio/format-utils.h>
|
||||
|
||||
// note -- these are hardwired into this code.
|
||||
#define DEFAULT_FORMAT SPA_AUDIO_FORMAT_S16_LE
|
||||
#define DEFAULT_BYTES_PER_SAMPLE 2
|
||||
static char channel_map_mono[] = "FC";
|
||||
static char channel_map_stereo[] = "FL FR";
|
||||
static char channel_map_2p1[] = "FL FR LFE";
|
||||
static char channel_map_4p0[] = "FL FR FC BC";
|
||||
static char channel_map_5p0[] = "FL FR FC BL BR";
|
||||
static char channel_map_5p1[] = "FL FR FC LFE BL BR";
|
||||
static char channel_map_6p1[] = "FL FR FC LFE BC SL SR";
|
||||
static char channel_map_7p1[] = "FL FR FC LFE BL BR SL SR";
|
||||
|
||||
#define DEFAULT_RATE 44100
|
||||
#define DEFAULT_CHANNELS 2
|
||||
#define DEFAULT_BUFFER_SIZE_IN_SECONDS 4
|
||||
typedef struct {
|
||||
enum spa_audio_format spa_format;
|
||||
sps_format_t sps_format;
|
||||
unsigned int bytes_per_sample;
|
||||
} spa_sps_t;
|
||||
|
||||
// Four seconds buffer -- should be plenty
|
||||
#define buffer_allocation DEFAULT_RATE * DEFAULT_BUFFER_SIZE_IN_SECONDS * DEFAULT_BYTES_PER_SAMPLE * DEFAULT_CHANNELS
|
||||
// these are the only formats that audio_pw will ever allow itself to be configured with
|
||||
static spa_sps_t format_lookup[] = {{SPA_AUDIO_FORMAT_S16_LE, SPS_FORMAT_S16_LE, 2},
|
||||
{SPA_AUDIO_FORMAT_S16_BE, SPS_FORMAT_S16_BE, 2},
|
||||
{SPA_AUDIO_FORMAT_S32_LE, SPS_FORMAT_S32_LE, 4},
|
||||
{SPA_AUDIO_FORMAT_S32_BE, SPS_FORMAT_S32_BE, 4}};
|
||||
|
||||
#define BUFFER_SIZE_IN_SECONDS 1
|
||||
|
||||
static uint8_t buffer[1024];
|
||||
static pthread_mutex_t buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
static int32_t current_encoded_output_format = 0;
|
||||
static char *audio_lmb, *audio_umb, *audio_toq, *audio_eoq;
|
||||
static size_t audio_size = buffer_allocation;
|
||||
static size_t audio_size = 0;
|
||||
static size_t audio_occupancy;
|
||||
static int enable_fill;
|
||||
static int stream_is_active;
|
||||
|
||||
struct timing_data {
|
||||
int pw_time_is_valid; // set when the pw_time has been set
|
||||
struct pw_time time_info; // information about the last time a process callback occurred
|
||||
size_t frames; // the number of frames sent at that time
|
||||
};
|
||||
|
||||
// to avoid using a mutex, write the same data twice and check they are the same
|
||||
// to ensure they are consistent. Make sure the first is written strictly before the second
|
||||
// using __sync_synchronize();
|
||||
struct timing_data timing_data_1, timing_data_2;
|
||||
static int on_process_is_running = 0;
|
||||
|
||||
struct data {
|
||||
struct pw_thread_loop *loop;
|
||||
struct pw_stream *stream;
|
||||
unsigned int rate;
|
||||
unsigned int bytes_per_sample;
|
||||
unsigned int channels;
|
||||
};
|
||||
|
||||
// the pipewire global data structure
|
||||
struct data data = {NULL, NULL};
|
||||
struct data data = {NULL, NULL, 0, 0, 0};
|
||||
|
||||
/*
|
||||
static void on_state_changed(__attribute__((unused)) void *userdata, enum pw_stream_state old,
|
||||
enum pw_stream_state state,
|
||||
__attribute__((unused)) const char *error) {
|
||||
// struct pw_data *pw = userdata;
|
||||
debug(3, "pw: stream state changed %s -> %s", pw_stream_state_as_string(old),
|
||||
pw_stream_state_as_string(state));
|
||||
// use an SPS_FORMAT_... to find an entry in the format_lookup table or return NULL
|
||||
static spa_sps_t *sps_format_lookup(sps_format_t to_find) {
|
||||
spa_sps_t *response = NULL;
|
||||
unsigned int i = 0;
|
||||
while ((response == NULL) && (i < sizeof(format_lookup) / sizeof(spa_sps_t))) {
|
||||
if (format_lookup[i].sps_format == to_find)
|
||||
response = &format_lookup[i];
|
||||
else
|
||||
i++;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
*/
|
||||
|
||||
static void on_process(void *userdata) {
|
||||
|
||||
struct data *data = userdata;
|
||||
struct data *local_data = userdata;
|
||||
int n_frames = 0;
|
||||
|
||||
pthread_mutex_lock(&buffer_mutex);
|
||||
|
||||
// debug(1, "on_process called.");
|
||||
|
||||
if (stream_is_active == 0)
|
||||
debug(1, "on_process called while stream inactive!");
|
||||
|
||||
on_process_is_running = 1;
|
||||
if ((audio_occupancy > 0) || (enable_fill)) {
|
||||
|
||||
// get a buffer to see how big it can be
|
||||
struct pw_buffer *b = pw_stream_dequeue_buffer(data->stream);
|
||||
struct pw_buffer *b = pw_stream_dequeue_buffer(local_data->stream);
|
||||
if (b == NULL) {
|
||||
pw_log_warn("out of buffers: %m");
|
||||
die("PipeWire failure -- out of buffers!");
|
||||
@@ -105,8 +121,8 @@ static void on_process(void *userdata) {
|
||||
struct spa_buffer *buf = b->buffer;
|
||||
uint8_t *dest = buf->datas[0].data;
|
||||
if (dest != NULL) {
|
||||
int stride = DEFAULT_BYTES_PER_SAMPLE * DEFAULT_CHANNELS;
|
||||
|
||||
int stride = local_data->bytes_per_sample * local_data->channels;
|
||||
|
||||
// note: the requested field is the number of frames, not bytes, requested
|
||||
int max_possible_frames = SPA_MIN(b->requested, buf->datas[0].maxsize / stride);
|
||||
|
||||
@@ -145,89 +161,76 @@ static void on_process(void *userdata) {
|
||||
memset(dest, 0, bytes_we_can_transfer);
|
||||
n_frames = max_possible_frames;
|
||||
}
|
||||
|
||||
buf->datas[0].chunk->offset = 0;
|
||||
buf->datas[0].chunk->stride = stride;
|
||||
buf->datas[0].chunk->size = n_frames * stride;
|
||||
pw_stream_queue_buffer(data->stream, b);
|
||||
debug(3, "Queueing %d frames for output.", n_frames);
|
||||
pw_stream_queue_buffer(local_data->stream, b);
|
||||
|
||||
} // (else the first data block does not contain a data pointer)
|
||||
}
|
||||
pthread_mutex_unlock(&buffer_mutex);
|
||||
|
||||
timing_data_1.frames = n_frames;
|
||||
if (pw_stream_get_time_n(data->stream, &timing_data_1.time_info, sizeof(struct timing_data)) == 0)
|
||||
timing_data_1.pw_time_is_valid = 1;
|
||||
else
|
||||
timing_data_1.pw_time_is_valid = 0;
|
||||
__sync_synchronize();
|
||||
memcpy((char *)&timing_data_2, (char *)&timing_data_1, sizeof(struct timing_data));
|
||||
__sync_synchronize();
|
||||
}
|
||||
|
||||
static const struct pw_stream_events stream_events = {PW_VERSION_STREAM_EVENTS,
|
||||
.process = on_process};
|
||||
// PW_VERSION_STREAM_EVENTS, .process = on_process, .state_changed = on_state_changed};
|
||||
|
||||
static void deinit(void) {
|
||||
pw_thread_loop_stop(data.loop);
|
||||
pw_stream_destroy(data.stream);
|
||||
if (data.stream != NULL)
|
||||
pw_stream_destroy(data.stream);
|
||||
pw_thread_loop_destroy(data.loop);
|
||||
pw_deinit();
|
||||
free(audio_lmb); // deallocate that buffer
|
||||
on_process_is_running = 0;
|
||||
if (audio_lmb != NULL)
|
||||
free(audio_lmb); // deallocate that buffer
|
||||
}
|
||||
|
||||
static int init(__attribute__((unused)) int argc, __attribute__((unused)) char **argv) {
|
||||
// set up default values first
|
||||
memset(&timing_data_1, 0, sizeof(struct timing_data));
|
||||
memset(&timing_data_2, 0, sizeof(struct timing_data));
|
||||
config.audio_backend_buffer_desired_length = 0.35;
|
||||
config.audio_backend_buffer_desired_length = 0.5;
|
||||
config.audio_backend_buffer_interpolation_threshold_in_seconds =
|
||||
0.02; // below this, soxr interpolation will not occur -- it'll be basic interpolation
|
||||
// instead.
|
||||
|
||||
config.audio_backend_latency_offset = 0;
|
||||
|
||||
// get settings from settings file
|
||||
// do the "general" audio options. Note, these options are in the "general" stanza!
|
||||
parse_general_audio_options();
|
||||
|
||||
// now any PipeWire-specific options
|
||||
if (config.cfg != NULL) {
|
||||
const char *str;
|
||||
// get settings from settings file, passing in defaults for format_set, rate_set and channel_set
|
||||
// Note, these options may be in the "general" stanza or the named stanza
|
||||
#ifdef CONFIG_FFMPEG
|
||||
parse_audio_options("pipewire", SPS_FORMAT_SET, SPS_RATE_SET, SPS_CHANNEL_SET);
|
||||
#else
|
||||
parse_audio_options("pipewire", SPS_FORMAT_NON_FFMPEG_SET, SPS_RATE_NON_FFMPEG_SET,
|
||||
SPS_CHANNNEL_NON_FFMPEG_SET);
|
||||
#endif
|
||||
|
||||
// Get the optional Application Name, if provided.
|
||||
if (config_lookup_string(config.cfg, "pw.application_name", &str)) {
|
||||
config.pw_application_name = (char *)str;
|
||||
}
|
||||
// now any PipeWire-specific options
|
||||
if (config.cfg != NULL) {
|
||||
const char *str;
|
||||
|
||||
// Get the optional PipeWire node name, if provided.
|
||||
if (config_lookup_string(config.cfg, "pw.node_name", &str)) {
|
||||
config.pw_node_name = (char *)str;
|
||||
}
|
||||
|
||||
// Get the optional PipeWire sink target name, if provided.
|
||||
if (config_lookup_string(config.cfg, "pw.sink_target", &str)) {
|
||||
config.pw_sink_target = (char *)str;
|
||||
}
|
||||
// Get the optional Application Name, if provided.
|
||||
if (config_lookup_non_empty_string(config.cfg, "pipewire.application_name", &str)) {
|
||||
config.pw_application_name = (char *)str;
|
||||
}
|
||||
|
||||
|
||||
// Get the optional PipeWire node name, if provided.
|
||||
if (config_lookup_non_empty_string(config.cfg, "pipewire.node_name", &str)) {
|
||||
config.pw_node_name = (char *)str;
|
||||
}
|
||||
|
||||
// Get the optional PipeWire sink target name, if provided.
|
||||
if (config_lookup_non_empty_string(config.cfg, "pipewire.sink_target", &str)) {
|
||||
config.pw_sink_target = (char *)str;
|
||||
}
|
||||
}
|
||||
|
||||
// finished collecting settings
|
||||
|
||||
// allocate space for the audio buffer
|
||||
audio_lmb = malloc(audio_size);
|
||||
if (audio_lmb == NULL)
|
||||
die("Can't allocate %d bytes for PipeWire buffer.", audio_size);
|
||||
audio_toq = audio_eoq = audio_lmb;
|
||||
audio_umb = audio_lmb + audio_size;
|
||||
audio_occupancy = 0;
|
||||
// debug(1, "init enable_fill");
|
||||
audio_lmb = NULL;
|
||||
audio_size = 0;
|
||||
current_encoded_output_format = 0;
|
||||
enable_fill = 1;
|
||||
|
||||
const struct spa_pod *params[1];
|
||||
uint8_t buffer[1024];
|
||||
struct pw_properties *props;
|
||||
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
|
||||
|
||||
int largc = 0;
|
||||
pw_init(&largc, NULL);
|
||||
|
||||
@@ -236,20 +239,18 @@ static int init(__attribute__((unused)) int argc, __attribute__((unused)) char *
|
||||
|
||||
pw_thread_loop_lock(data.loop);
|
||||
|
||||
pw_thread_loop_start(data.loop);
|
||||
|
||||
char* appname = config.pw_application_name;
|
||||
char *appname = config.pw_application_name;
|
||||
if (appname == NULL)
|
||||
appname = "Shairport Sync";
|
||||
|
||||
char* nodename = config.pw_node_name;
|
||||
|
||||
char *nodename = config.pw_node_name;
|
||||
if (nodename == NULL)
|
||||
nodename = "Shairport Sync";
|
||||
|
||||
props = pw_properties_new(PW_KEY_MEDIA_TYPE, "Audio", PW_KEY_MEDIA_CATEGORY, "Playback",
|
||||
PW_KEY_MEDIA_ROLE, "Music", PW_KEY_APP_NAME, appname,
|
||||
PW_KEY_NODE_NAME, nodename, NULL);
|
||||
|
||||
struct pw_properties *props = pw_properties_new(
|
||||
PW_KEY_MEDIA_TYPE, "Audio", PW_KEY_MEDIA_CATEGORY, "Playback", PW_KEY_MEDIA_ROLE, "Music",
|
||||
PW_KEY_APP_NAME, appname, PW_KEY_NODE_NAME, nodename, NULL);
|
||||
|
||||
if (config.pw_sink_target != NULL) {
|
||||
debug(3, "setting sink target to \"%s\".", config.pw_sink_target);
|
||||
pw_properties_set(props, PW_KEY_TARGET_OBJECT, config.pw_sink_target);
|
||||
@@ -257,38 +258,208 @@ static int init(__attribute__((unused)) int argc, __attribute__((unused)) char *
|
||||
|
||||
data.stream = pw_stream_new_simple(pw_thread_loop_get_loop(data.loop), config.appName, props,
|
||||
&stream_events, &data);
|
||||
pw_thread_loop_start(data.loop);
|
||||
|
||||
// Make one parameter with the supported formats. The SPA_PARAM_EnumFormat
|
||||
// id means that this is a format enumeration (of 1 value).
|
||||
params[0] = spa_format_audio_raw_build(&b, SPA_PARAM_EnumFormat,
|
||||
&SPA_AUDIO_INFO_RAW_INIT(.format = DEFAULT_FORMAT,
|
||||
.channels = DEFAULT_CHANNELS,
|
||||
.rate = DEFAULT_RATE));
|
||||
on_process_is_running = 0;
|
||||
|
||||
// Now connect this stream. We ask that our process function is
|
||||
// called in a realtime thread.
|
||||
pw_stream_connect(data.stream, PW_DIRECTION_OUTPUT, PW_ID_ANY,
|
||||
PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS |
|
||||
PW_STREAM_FLAG_RT_PROCESS | PW_STREAM_FLAG_INACTIVE,
|
||||
params, 1);
|
||||
stream_is_active = 0;
|
||||
pw_thread_loop_unlock(data.loop);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void start(__attribute__((unused)) int sample_rate,
|
||||
__attribute__((unused)) int sample_format) {
|
||||
static int check_settings(sps_format_t sample_format, unsigned int sample_rate,
|
||||
unsigned int channel_count) {
|
||||
// we know the formats with be big- or little-ended.
|
||||
// we will accept only S32_..., S16_...
|
||||
|
||||
int response = EINVAL;
|
||||
|
||||
if (sps_format_lookup(sample_format) != NULL)
|
||||
response = 0;
|
||||
|
||||
debug(3, "pw: configuration: %u/%s/%u %s.", sample_rate,
|
||||
sps_format_description_string(sample_format), channel_count,
|
||||
response == 0 ? "is okay" : "can not be configured");
|
||||
return response;
|
||||
}
|
||||
|
||||
static void prepare_to_play() {
|
||||
// debug(1, "prepare to play");
|
||||
if (stream_is_active == 0) {
|
||||
static int check_configuration(unsigned int channels, unsigned int rate, unsigned int format) {
|
||||
return check_settings(format, rate, channels);
|
||||
}
|
||||
|
||||
static int32_t get_configuration(unsigned int channels, unsigned int rate, unsigned int format) {
|
||||
return search_for_suitable_configuration(channels, rate, format, &check_configuration);
|
||||
}
|
||||
|
||||
static int configure(int32_t requested_encoded_format, char **resulting_channel_map) {
|
||||
// debug(2, "pw: configure %s.", short_format_description(requested_encoded_format));
|
||||
int response = 0;
|
||||
char *channel_map = NULL;
|
||||
// if (1) {
|
||||
if (current_encoded_output_format != requested_encoded_format) {
|
||||
uint64_t start_time = get_absolute_time_in_ns();
|
||||
if (current_encoded_output_format == 0)
|
||||
debug(2, "pw: setting output configuration to %s.",
|
||||
short_format_description(requested_encoded_format));
|
||||
else
|
||||
// note -- can't use short_format_description twice in one call because it returns the same
|
||||
// string buffer each time
|
||||
debug(2, "pw: changing output configuration to %s.",
|
||||
short_format_description(requested_encoded_format));
|
||||
current_encoded_output_format = requested_encoded_format;
|
||||
spa_sps_t *format_info =
|
||||
sps_format_lookup(FORMAT_FROM_ENCODED_FORMAT(current_encoded_output_format));
|
||||
|
||||
if (format_info == NULL)
|
||||
die("Can't find format information!");
|
||||
// enum spa_audio_format spa_format = format_info->spa_format;
|
||||
data.bytes_per_sample = format_info->bytes_per_sample;
|
||||
data.channels = CHANNELS_FROM_ENCODED_FORMAT(current_encoded_output_format);
|
||||
data.rate = RATE_FROM_ENCODED_FORMAT(current_encoded_output_format);
|
||||
|
||||
pw_thread_loop_lock(data.loop);
|
||||
pw_stream_set_active(data.stream, true);
|
||||
enable_fill = 0;
|
||||
|
||||
if (pw_stream_get_state(data.stream, NULL) != PW_STREAM_STATE_UNCONNECTED) {
|
||||
response = pw_stream_disconnect(data.stream);
|
||||
if (response != 0) {
|
||||
debug(1, "error %d disconnecting stream.", response);
|
||||
}
|
||||
}
|
||||
|
||||
if (audio_lmb != NULL) {
|
||||
// debug(3, "deallocating existing audio_pw.c buffer.");
|
||||
free(audio_lmb);
|
||||
}
|
||||
|
||||
audio_size = data.rate * BUFFER_SIZE_IN_SECONDS * data.bytes_per_sample * data.channels;
|
||||
// allocate space for the audio buffer
|
||||
audio_lmb = malloc(audio_size);
|
||||
if (audio_lmb == NULL)
|
||||
die("Can't allocate %zd bytes for PipeWire buffer.", audio_size);
|
||||
audio_toq = audio_eoq = audio_lmb;
|
||||
audio_umb = audio_lmb + audio_size;
|
||||
audio_occupancy = 0;
|
||||
|
||||
// Make one parameter with the supported formats. The SPA_PARAM_EnumFormat
|
||||
// id means that this is a format enumeration (of 1 value).
|
||||
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
|
||||
|
||||
const struct spa_pod *params[1];
|
||||
// create a stream with the default channel layout corresponding to
|
||||
// the number of channels
|
||||
switch (CHANNELS_FROM_ENCODED_FORMAT(current_encoded_output_format)) {
|
||||
case 1:
|
||||
channel_map = channel_map_mono;
|
||||
params[0] = spa_format_audio_raw_build(
|
||||
&b, SPA_PARAM_EnumFormat,
|
||||
// we are giving the position of 8 channels here, even if we need less than that.
|
||||
&SPA_AUDIO_INFO_RAW_INIT(.format = format_info->spa_format, .channels = data.channels,
|
||||
.rate = data.rate, .position = {SPA_AUDIO_CHANNEL_FC}));
|
||||
break;
|
||||
case 2:
|
||||
channel_map = channel_map_stereo;
|
||||
params[0] = spa_format_audio_raw_build(
|
||||
&b, SPA_PARAM_EnumFormat,
|
||||
// we are giving the position of 8 channels here, even if we need less than that.
|
||||
&SPA_AUDIO_INFO_RAW_INIT(.format = format_info->spa_format, .channels = data.channels,
|
||||
.rate = data.rate,
|
||||
.position = {SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR}));
|
||||
break;
|
||||
case 3:
|
||||
channel_map = channel_map_2p1;
|
||||
params[0] = spa_format_audio_raw_build(
|
||||
&b, SPA_PARAM_EnumFormat,
|
||||
// we are giving the position of 8 channels here, even if we need less than that.
|
||||
&SPA_AUDIO_INFO_RAW_INIT(.format = format_info->spa_format, .channels = data.channels,
|
||||
.rate = data.rate,
|
||||
.position = {SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR,
|
||||
SPA_AUDIO_CHANNEL_LFE}));
|
||||
break;
|
||||
case 4:
|
||||
channel_map = channel_map_4p0;
|
||||
params[0] = spa_format_audio_raw_build(
|
||||
&b, SPA_PARAM_EnumFormat,
|
||||
// we are giving the position of 8 channels here, even if we need less than that.
|
||||
&SPA_AUDIO_INFO_RAW_INIT(.format = format_info->spa_format, .channels = data.channels,
|
||||
.rate = data.rate,
|
||||
.position = {SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR,
|
||||
SPA_AUDIO_CHANNEL_FC, SPA_AUDIO_CHANNEL_BC}));
|
||||
break;
|
||||
case 5:
|
||||
channel_map = channel_map_5p0;
|
||||
params[0] = spa_format_audio_raw_build(
|
||||
&b, SPA_PARAM_EnumFormat,
|
||||
// we are giving the position of 8 channels here, even if we need less than that.
|
||||
&SPA_AUDIO_INFO_RAW_INIT(.format = format_info->spa_format, .channels = data.channels,
|
||||
.rate = data.rate,
|
||||
.position = {SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR,
|
||||
SPA_AUDIO_CHANNEL_FC, SPA_AUDIO_CHANNEL_RL,
|
||||
SPA_AUDIO_CHANNEL_RR}));
|
||||
break;
|
||||
case 6:
|
||||
channel_map = channel_map_5p1;
|
||||
params[0] = spa_format_audio_raw_build(
|
||||
&b, SPA_PARAM_EnumFormat,
|
||||
&SPA_AUDIO_INFO_RAW_INIT(.format = format_info->spa_format, .channels = data.channels,
|
||||
.rate = data.rate,
|
||||
.position = {SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR,
|
||||
SPA_AUDIO_CHANNEL_FC, SPA_AUDIO_CHANNEL_LFE,
|
||||
SPA_AUDIO_CHANNEL_RL, SPA_AUDIO_CHANNEL_RR}));
|
||||
break;
|
||||
case 7:
|
||||
channel_map = channel_map_6p1;
|
||||
params[0] = spa_format_audio_raw_build(
|
||||
&b, SPA_PARAM_EnumFormat,
|
||||
&SPA_AUDIO_INFO_RAW_INIT(.format = format_info->spa_format, .channels = data.channels,
|
||||
.rate = data.rate,
|
||||
.position = {SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR,
|
||||
SPA_AUDIO_CHANNEL_FC, SPA_AUDIO_CHANNEL_LFE,
|
||||
SPA_AUDIO_CHANNEL_BC, SPA_AUDIO_CHANNEL_SL,
|
||||
SPA_AUDIO_CHANNEL_SR}));
|
||||
break;
|
||||
case 8:
|
||||
channel_map = channel_map_7p1;
|
||||
params[0] = spa_format_audio_raw_build(
|
||||
&b, SPA_PARAM_EnumFormat,
|
||||
&SPA_AUDIO_INFO_RAW_INIT(.format = format_info->spa_format, .channels = data.channels,
|
||||
.rate = data.rate,
|
||||
.position = {SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR,
|
||||
SPA_AUDIO_CHANNEL_FC, SPA_AUDIO_CHANNEL_LFE,
|
||||
SPA_AUDIO_CHANNEL_RL, SPA_AUDIO_CHANNEL_RR,
|
||||
SPA_AUDIO_CHANNEL_SL, SPA_AUDIO_CHANNEL_SR}));
|
||||
break;
|
||||
default:
|
||||
channel_map = NULL;
|
||||
params[0] = spa_format_audio_raw_build(
|
||||
&b, SPA_PARAM_EnumFormat,
|
||||
// we are giving the position of 8 channels here, even if we need less than that.
|
||||
&SPA_AUDIO_INFO_RAW_INIT(.format = format_info->spa_format, .channels = data.channels,
|
||||
.rate = data.rate,
|
||||
.position = {SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR,
|
||||
SPA_AUDIO_CHANNEL_FC, SPA_AUDIO_CHANNEL_LFE,
|
||||
SPA_AUDIO_CHANNEL_RL, SPA_AUDIO_CHANNEL_RR,
|
||||
SPA_AUDIO_CHANNEL_SL, SPA_AUDIO_CHANNEL_SR}));
|
||||
break;
|
||||
}
|
||||
|
||||
// Now connect this stream. We ask that our process function is
|
||||
// called in a realtime thread.
|
||||
pw_stream_connect(data.stream, PW_DIRECTION_OUTPUT, PW_ID_ANY,
|
||||
PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS |
|
||||
PW_STREAM_FLAG_RT_PROCESS,
|
||||
params, 1);
|
||||
stream_is_active = 0;
|
||||
enable_fill = 1;
|
||||
pw_thread_loop_unlock(data.loop);
|
||||
stream_is_active = 1;
|
||||
debug(3, "prepare to play activating stream");
|
||||
int64_t elapsed_time = get_absolute_time_in_ns() - start_time;
|
||||
debug(3, "pw: configuration took %0.3f mS.", elapsed_time * 0.000001);
|
||||
} else {
|
||||
debug(2, "pw: setting output configuration -- configuration unchanged, so nothing done.");
|
||||
}
|
||||
if ((response == 0) && (resulting_channel_map != NULL)) {
|
||||
*resulting_channel_map = channel_map;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
static int play(__attribute__((unused)) void *buf, int samples,
|
||||
@@ -296,14 +467,16 @@ static int play(__attribute__((unused)) void *buf, int samples,
|
||||
__attribute__((unused)) uint64_t playtime) {
|
||||
if (stream_is_active == 0) {
|
||||
pw_thread_loop_lock(data.loop);
|
||||
on_process_is_running = 0;
|
||||
pw_stream_set_active(data.stream, true);
|
||||
pw_thread_loop_unlock(data.loop);
|
||||
stream_is_active = 1;
|
||||
debug(3, "set stream active");
|
||||
// debug(1, "set stream active");
|
||||
}
|
||||
// copy the samples into the queue
|
||||
debug(3, "play %u samples; %u bytes already in the buffer.", samples, audio_occupancy);
|
||||
size_t bytes_to_transfer = samples * DEFAULT_CHANNELS * DEFAULT_BYTES_PER_SAMPLE;
|
||||
// debug(3, "play %u samples; %u samples already in the buffer.", samples, audio_occupancy /
|
||||
// (data.bytes_per_sample * data.channels));
|
||||
size_t bytes_to_transfer = samples * data.channels * data.bytes_per_sample;
|
||||
pthread_mutex_lock(&buffer_mutex);
|
||||
size_t bytes_available = audio_size - audio_occupancy;
|
||||
if (bytes_available < bytes_to_transfer)
|
||||
@@ -325,58 +498,76 @@ static int play(__attribute__((unused)) void *buf, int samples,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int delay(long *the_delay) {
|
||||
static int delay(long *the_delay) {
|
||||
long result = 0;
|
||||
int reply = 0;
|
||||
// find out what's already in the PipeWire system and when
|
||||
struct timing_data timing_data;
|
||||
int loop_count = 1;
|
||||
do {
|
||||
memcpy(&timing_data, (char *)&timing_data_1, sizeof(struct timing_data));
|
||||
__sync_synchronize();
|
||||
if (memcmp(&timing_data, (char *)&timing_data_2, sizeof(struct timing_data)) != 0) {
|
||||
usleep(2); // microseconds
|
||||
loop_count++;
|
||||
__sync_synchronize();
|
||||
}
|
||||
} while ((memcmp(&timing_data, (char *)&timing_data_2, sizeof(struct timing_data)) != 0) &&
|
||||
(loop_count < 10));
|
||||
long total_delay_now_frames_long = 0;
|
||||
if ((loop_count < 10) && (timing_data.pw_time_is_valid != 0)) {
|
||||
struct timespec time_now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &time_now);
|
||||
int64_t interval_from_process_time_to_now =
|
||||
SPA_TIMESPEC_TO_NSEC(&time_now) - timing_data.time_info.now;
|
||||
int64_t delay_in_ns = timing_data.time_info.delay + timing_data.time_info.buffered;
|
||||
delay_in_ns = delay_in_ns * 1000000000;
|
||||
delay_in_ns = delay_in_ns * timing_data.time_info.rate.num;
|
||||
delay_in_ns = delay_in_ns / timing_data.time_info.rate.denom;
|
||||
int reply = -ENODEV; // ENODATA is not defined in FreeBSD
|
||||
|
||||
int64_t total_delay_now_ns = delay_in_ns - interval_from_process_time_to_now;
|
||||
int64_t total_delay_now_frames = (total_delay_now_ns * DEFAULT_RATE) / 1000000000 + timing_data.frames;
|
||||
total_delay_now_frames_long = total_delay_now_frames;
|
||||
debug(3, "total delay in frames: %ld.", total_delay_now_frames_long);
|
||||
if (on_process_is_running == 0) {
|
||||
debug(3, "pw_processor not running");
|
||||
}
|
||||
|
||||
if (timing_data.time_info.queued != 0) {
|
||||
debug(1, "buffers queued: %d", timing_data.time_info.queued);
|
||||
if ((stream_is_active == 0) && (on_process_is_running != 0)) {
|
||||
debug(3, "stream not active but on_process_is_running is true.");
|
||||
}
|
||||
if (on_process_is_running != 0) {
|
||||
|
||||
struct pw_time stream_time_info_1, stream_time_info_2;
|
||||
ssize_t audio_occupancy_now;
|
||||
|
||||
// get stable pw_time info to ensure we get an audio occupancy figure
|
||||
// that relates to the pw_time we have.
|
||||
// we do this by getting a pw_time before and after getting the occupancy
|
||||
// and accepting the information if they are both the same
|
||||
|
||||
int loop_count = 1;
|
||||
int non_matching;
|
||||
int stream_time_valid_if_zero;
|
||||
do {
|
||||
stream_time_valid_if_zero =
|
||||
pw_stream_get_time_n(data.stream, &stream_time_info_1, sizeof(struct pw_time));
|
||||
audio_occupancy_now = audio_occupancy;
|
||||
pw_stream_get_time_n(data.stream, &stream_time_info_2, sizeof(struct pw_time));
|
||||
|
||||
non_matching = memcmp(&stream_time_info_1, &stream_time_info_2, sizeof(struct pw_time));
|
||||
if (non_matching != 0) {
|
||||
loop_count++;
|
||||
}
|
||||
} while (((non_matching != 0) || (stream_time_valid_if_zero != 0)) && (loop_count < 10));
|
||||
|
||||
if (non_matching != 0) {
|
||||
debug(1, "can't get a stable pw_time!");
|
||||
}
|
||||
if (stream_time_valid_if_zero != 0) {
|
||||
debug(1, "can't get valid stream info");
|
||||
}
|
||||
if (stream_time_info_1.rate.denom == 0) {
|
||||
debug(2, "non valid stream_time_info_1");
|
||||
}
|
||||
|
||||
if ((non_matching == 0) && (stream_time_valid_if_zero == 0) &&
|
||||
(stream_time_info_1.rate.denom != 0)) {
|
||||
int64_t interval_from_pw_time_to_now_ns =
|
||||
pw_stream_get_nsec(data.stream) - stream_time_info_1.now;
|
||||
|
||||
uint64_t frames_possibly_played_since_measurement =
|
||||
((interval_from_pw_time_to_now_ns * data.rate) + 500000000L) / 1000000000L;
|
||||
|
||||
uint64_t net_delay_in_frames = stream_time_info_1.queued + stream_time_info_1.buffered;
|
||||
|
||||
uint64_t fixed_delay_ns =
|
||||
(stream_time_info_1.delay * stream_time_info_1.rate.num * 1000000000L) /
|
||||
stream_time_info_1.rate.denom; // ns;
|
||||
uint64_t fixed_delay_in_frames = ((fixed_delay_ns * data.rate) + 500000000L) / 1000000000L;
|
||||
|
||||
net_delay_in_frames = net_delay_in_frames + fixed_delay_in_frames +
|
||||
audio_occupancy_now / (data.bytes_per_sample * data.channels) -
|
||||
frames_possibly_played_since_measurement;
|
||||
|
||||
result = net_delay_in_frames;
|
||||
reply = 0;
|
||||
}
|
||||
/*
|
||||
debug(3,
|
||||
"interval_from_process_time_to_now: %" PRId64 " ns, "
|
||||
"delay_in_ns: %" PRId64 ", queued: %" PRId64 ", buffered: %" PRId64 ".",
|
||||
// delay_timing_data.time_info.rate.num, delay_timing_data.time_info.rate.denom,
|
||||
interval_from_process_time_to_now, delay_in_ns,
|
||||
timing_data.time_info.queued, timing_data.time_info.buffered);
|
||||
*/
|
||||
|
||||
} else {
|
||||
warn("Shairport Sync's PipeWire backend can not get timing information from the PipeWire "
|
||||
"system. Is PipeWire running?");
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&buffer_mutex);
|
||||
result = total_delay_now_frames_long + audio_occupancy / (DEFAULT_BYTES_PER_SAMPLE * DEFAULT_CHANNELS);
|
||||
pthread_mutex_unlock(&buffer_mutex);
|
||||
*the_delay = result;
|
||||
return reply;
|
||||
}
|
||||
@@ -408,23 +599,23 @@ static void stop(void) {
|
||||
pw_stream_set_active(data.stream, false);
|
||||
pw_thread_loop_unlock(data.loop);
|
||||
stream_is_active = 0;
|
||||
debug(3, "set stream inactive");
|
||||
// debug(1, "set stream inactive");
|
||||
}
|
||||
}
|
||||
|
||||
audio_output audio_pw = {.name = "pw",
|
||||
audio_output audio_pw = {.name = "pipewire",
|
||||
.help = NULL,
|
||||
.init = &init,
|
||||
.deinit = &deinit,
|
||||
.prepare = NULL,
|
||||
.start = &start,
|
||||
.start = NULL,
|
||||
.get_configuration = &get_configuration,
|
||||
.configure = &configure,
|
||||
.stop = &stop,
|
||||
.is_running = NULL,
|
||||
.flush = &flush,
|
||||
.delay = &delay,
|
||||
.stats = NULL,
|
||||
.play = &play,
|
||||
.prepare_to_play = &prepare_to_play,
|
||||
.volume = NULL,
|
||||
.parameters = NULL,
|
||||
.mute = NULL};
|
||||
|
||||
Reference in New Issue
Block a user