Files
shairport-sync/ptp-utilities.c
T
Mike Brady b405c0b896 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.
2026-02-13 15:17:40 +00:00

265 lines
9.2 KiB
C

/*
* This file is part of Shairport Sync.
* Copyright (c) Mike Brady 2020--2025
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "definitions.h"
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#ifdef COMPILE_FOR_FREEBSD
#include <netinet/in.h>
#endif
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#define __STDC_FORMAT_MACROS
#include "common.h"
#include "ptp-utilities.h"
#include <inttypes.h>
#include <unistd.h>
int shm_fd;
void *mapped_addr = NULL;
// returns a copy of the shared memory data from the nqptp
// shared memory interface, so long as it's open.
int get_nqptp_data(struct shm_structure *nqptp_data) {
// uint64_t tn = get_absolute_time_in_ns(); // if interested in timing the function...
struct shm_structure local_nqptp_data;
int response = -1; // presume the worst. Fix it on success
// We need to ensure that when we read the record, we are not reading it while it is partly
// updated and therefore inconsistent. To achieve this, we do the following:
// We ensure that the secondary record is written by NQPTP _strictly after_
// all writes to the main record are complete.
// Here we read two copies of the entire record, the second
// _strictly after_ all reads from the first are complete.
// (Strict write and read ordering is ensured using the __sync_synchronize() construct.)
// We then compare the main record in the first read to the
// secondary record in the second read.
// If they are equal, we can be sure we have not read a record that has been
// made inconsistent by an interrupted update.
if ((mapped_addr != MAP_FAILED) && (mapped_addr != NULL)) {
int loop_count = 1;
do {
__sync_synchronize();
memcpy(nqptp_data, (char *)mapped_addr, sizeof(struct shm_structure));
__sync_synchronize();
// read again strictly after a full read -- this is to read the secondary strictly after the
// primary
memcpy(&local_nqptp_data, (char *)mapped_addr, sizeof(struct shm_structure));
// check that the main and secondary data sets match
if (memcmp(&nqptp_data->main, &local_nqptp_data.secondary, sizeof(shm_structure_set)) != 0) {
usleep(2); // microseconds
loop_count++;
}
} while (
(memcmp(&nqptp_data->main, &local_nqptp_data.secondary, sizeof(shm_structure_set)) != 0) &&
(loop_count < 10));
if (loop_count == 10) {
debug(1, "get_nqptp_data -- main and secondary records don't match after %d attempts!",
loop_count);
response = -1;
} else {
response = 0;
}
} else {
if (mapped_addr == NULL)
debug(1, "get_nqptp_data failed because the mapped_addr is NULL");
else if (mapped_addr == MAP_FAILED)
debug(1, "get_nqptp_data failed because the mapped_addr is MAP_FAILED");
else
debug(1, "get_nqptp_data failed");
}
// int64_t et = get_absolute_time_in_ns() - tn;
// debug(1, "get_nqptp_data time: %.3f microseconds.", 0.001 * et);
return response;
}
int ptp_get_clock_version() {
int response = 0; // no version number information available
struct shm_structure nqptp_data;
if (get_nqptp_data(&nqptp_data) == 0) {
response = nqptp_data.version;
}
return response;
}
int ptp_get_clock_info(uint64_t *actual_clock_id, uint64_t *time_of_sample, uint64_t *raw_offset,
uint64_t *mastership_start_time) {
int response = clock_ok;
if (actual_clock_id != NULL)
*actual_clock_id = 0;
if (raw_offset != NULL)
*raw_offset = 0;
if (time_of_sample != NULL)
*time_of_sample = 0;
if (mastership_start_time != NULL)
*mastership_start_time = 0;
// if (ptp_shm_interface_open() == 0) {
struct shm_structure nqptp_data;
if (get_nqptp_data(&nqptp_data) == 0) {
if (nqptp_data.version == NQPTP_SHM_STRUCTURES_VERSION) {
// assuming a clock id can not be zero
if (nqptp_data.main.master_clock_id != 0) {
if (actual_clock_id != NULL)
*actual_clock_id = nqptp_data.main.master_clock_id;
if (time_of_sample != NULL)
*time_of_sample = nqptp_data.main.local_time;
if (raw_offset != NULL)
*raw_offset = nqptp_data.main.local_to_master_time_offset;
if (mastership_start_time != NULL)
*mastership_start_time = nqptp_data.main.master_clock_start_time;
} else {
response = clock_no_master;
}
} else {
// the version can not be zero. If zero is returned here, it means that the shared memory is
// not yet initialised, so not availalbe
if (nqptp_data.version == 0)
response = clock_service_unavailable;
else
response = clock_version_mismatch;
}
} else {
response = clock_data_unavailable;
}
return response;
}
int ptp_shm_interface_open() {
int response = 0;
debug(3, "ptp_shm_interface_open with mapped_addr = %" PRIuPTR "", (uintptr_t)mapped_addr);
if ((mapped_addr == NULL) || (mapped_addr == MAP_FAILED)) {
response = -1;
if (mapped_addr == NULL)
debug(3, "ptp_shm_interface_open is NULL");
if (mapped_addr == MAP_FAILED)
debug(3, "ptp_shm_interface_open is MAP_FAILED");
if (strcmp(config.nqptp_shared_memory_interface_name, "") != 0) {
response = 0;
int shared_memory_file_descriptor =
shm_open(config.nqptp_shared_memory_interface_name, O_RDONLY, 0);
if (shared_memory_file_descriptor >= 0) {
mapped_addr =
// needs to be PROT_READ | PROT_WRITE to allow the mapped memory to be writable for the
// mutex to lock and unlock
mmap(NULL, sizeof(struct shm_structure), PROT_READ, MAP_SHARED,
shared_memory_file_descriptor, 0);
if (mapped_addr == MAP_FAILED) {
response = -1;
}
if (close(shared_memory_file_descriptor) == -1) {
response = -1;
}
} else {
response = -1;
}
} else {
debug(1, "No config.nqptp_shared_memory_interface_name");
}
if (response == 0)
debug(3, "ptp_shm_interface_open -- success!");
else
debug(3, "ptp_shm_interface_open -- fail!");
} else {
debug(3, "ptp_shm_interface_open -- already open!");
}
return response;
}
int ptp_shm_interface_close() {
int response = -1;
if ((mapped_addr != MAP_FAILED) && (mapped_addr != NULL)) {
debug(2, "ptp_shm_interface_close");
response = munmap(mapped_addr, sizeof(struct shm_structure));
if (response != 0)
debug(1, "error unmapping shared memory.");
}
mapped_addr = NULL;
return response;
}
void ptp_send_control_message_string(const char *msg) {
size_t full_message_size =
strlen(config.nqptp_shared_memory_interface_name) + strlen(" ") + strlen(msg) + 1;
char *full_message = malloc(full_message_size);
if (full_message != NULL) {
*full_message = '\0';
snprintf(full_message, full_message_size, "%s %s", config.nqptp_shared_memory_interface_name,
msg);
debug(2, "Send control message to NQPTP: \"%s\"", full_message);
int s;
int ret;
struct addrinfo hints, *info;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
/* nqptp is only controllable via localhost */
char portstr[20];
snprintf(portstr, 20, "%d", NQPTP_CONTROL_PORT);
ret = getaddrinfo("localhost", portstr, &hints, &info);
if (ret) {
die("getaddrinfo: %s", gai_strerror(ret));
}
/* Create a datagram socket in the internet domain and use the
* default protocol (UDP).
*/
if ((s = socket(info->ai_family, info->ai_socktype, 0)) < 0) {
die("Can't open a socket to NQPTP");
}
/* Send the message in buf to the server */
if (sendto(s, full_message, full_message_size, 0, info->ai_addr, info->ai_addrlen) < 0) {
die("error sending timing_peer_list to NQPTP");
}
/* Deallocate the socket */
close(s);
freeaddrinfo(info);
/* deallocate the message string */
free(full_message);
} else {
debug(1, "Couldn't allocate memory to prepare a qualified ptp control message string.");
}
}