2023-02-08 18:12:00 +01:00
|
|
|
/* PipeWire */
|
|
|
|
|
/* SPDX-FileCopyrightText: Copyright © 2022 Wim Taymans <wim.taymans@gmail.com> */
|
|
|
|
|
/* SPDX-License-Identifier: MIT */
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-04 13:19:14 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
|
2022-10-04 09:16:17 +02:00
|
|
|
#include <limits.h>
|
2023-01-25 17:40:49 +03:00
|
|
|
#include <string.h>
|
2022-10-04 09:16:17 +02:00
|
|
|
#include <unistd.h>
|
2022-10-05 13:04:08 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
2022-10-04 09:16:17 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <netinet/in.h>
|
2022-10-05 13:04:08 +02:00
|
|
|
#include <net/if.h>
|
2022-10-04 18:12:48 +02:00
|
|
|
#include <ctype.h>
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-04 13:19:14 +02:00
|
|
|
#include <spa/utils/hook.h>
|
2023-01-20 16:08:38 +01:00
|
|
|
#include <spa/utils/result.h>
|
2022-10-04 20:44:07 +02:00
|
|
|
#include <spa/utils/ringbuffer.h>
|
2022-10-05 13:04:08 +02:00
|
|
|
#include <spa/utils/dll.h>
|
2023-02-21 17:18:52 +01:00
|
|
|
#include <spa/utils/json.h>
|
2023-01-20 16:08:38 +01:00
|
|
|
#include <spa/param/audio/format-utils.h>
|
2023-02-16 09:52:04 +01:00
|
|
|
#include <spa/control/control.h>
|
2023-02-21 17:18:52 +01:00
|
|
|
#include <spa/debug/types.h>
|
|
|
|
|
#include <spa/debug/mem.h>
|
2022-10-04 09:16:17 +02:00
|
|
|
|
|
|
|
|
#include <pipewire/pipewire.h>
|
2023-01-20 16:08:38 +01:00
|
|
|
#include <pipewire/impl.h>
|
2022-10-04 13:19:14 +02:00
|
|
|
|
|
|
|
|
#include <module-rtp/rtp.h>
|
|
|
|
|
|
2022-11-14 21:03:22 +03:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
|
#define ifr_ifindex ifr_index
|
|
|
|
|
#endif
|
2022-10-04 09:16:17 +02:00
|
|
|
|
|
|
|
|
/** \page page_module_rtp_source PipeWire Module: RTP source
|
|
|
|
|
*
|
|
|
|
|
* The `rtp-source` module creates a PipeWire source that receives audio
|
2023-02-21 17:18:52 +01:00
|
|
|
* and midi RTP packets.
|
2022-10-04 09:16:17 +02:00
|
|
|
*
|
|
|
|
|
* ## Module Options
|
|
|
|
|
*
|
|
|
|
|
* Options specific to the behavior of this module
|
|
|
|
|
*
|
2022-10-05 18:00:11 +02:00
|
|
|
* - `local.ifname = <str>`: interface name to use
|
2023-02-21 17:18:52 +01:00
|
|
|
* - `node.always-process = <bool>`: true to receive even when not running
|
2022-10-06 11:41:01 +02:00
|
|
|
* - `sess.latency.msec = <str>`: target network latency in milliseconds, default 100
|
2023-02-21 17:18:52 +01:00
|
|
|
* - `rtp.media = <string>`: the media type audio|midi, default audio
|
2022-10-06 11:41:01 +02:00
|
|
|
* - `stream.props = {}`: properties to be passed to the stream
|
2022-10-04 09:16:17 +02:00
|
|
|
*
|
|
|
|
|
* ## General options
|
|
|
|
|
*
|
|
|
|
|
* Options with well-known behavior:
|
|
|
|
|
*
|
2023-02-21 17:18:52 +01:00
|
|
|
* - \ref PW_KEY_REMOTE_NAME
|
|
|
|
|
* - \ref PW_KEY_AUDIO_FORMAT
|
|
|
|
|
* - \ref PW_KEY_AUDIO_RATE
|
|
|
|
|
* - \ref PW_KEY_AUDIO_CHANNELS
|
|
|
|
|
* - \ref SPA_KEY_AUDIO_POSITION
|
2022-10-04 09:16:17 +02:00
|
|
|
* - \ref PW_KEY_MEDIA_NAME
|
2022-10-06 11:41:01 +02:00
|
|
|
* - \ref PW_KEY_MEDIA_CLASS
|
2023-02-21 17:18:52 +01:00
|
|
|
* - \ref PW_KEY_NODE_NAME
|
|
|
|
|
* - \ref PW_KEY_NODE_DESCRIPTION
|
|
|
|
|
* - \ref PW_KEY_NODE_GROUP
|
|
|
|
|
* - \ref PW_KEY_NODE_LATENCY
|
|
|
|
|
* - \ref PW_KEY_NODE_VIRTUAL
|
2022-10-04 09:16:17 +02:00
|
|
|
*
|
|
|
|
|
* ## Example configuration
|
|
|
|
|
*\code{.unparsed}
|
|
|
|
|
* context.modules = [
|
2023-01-25 14:58:15 +01:00
|
|
|
* { name = libpipewire-module-rtp-source
|
|
|
|
|
* args = {
|
|
|
|
|
* #local.ifname = eth0
|
|
|
|
|
* sess.latency.msec = 100
|
2023-02-21 17:18:52 +01:00
|
|
|
* #node.always-process = false
|
|
|
|
|
* #rtp.media = "audio"
|
|
|
|
|
* #audio.format = "S16BE"
|
|
|
|
|
* #audio.rate = 48000
|
|
|
|
|
* #audio.channels = 2
|
|
|
|
|
* #audio.position = [ FL FR ]
|
2023-01-25 14:58:15 +01:00
|
|
|
* stream.props = {
|
|
|
|
|
* #media.class = "Audio/Source"
|
2023-02-21 17:18:52 +01:00
|
|
|
* node.name = "rtp-source"
|
2023-01-25 14:58:15 +01:00
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ]
|
2022-10-04 09:16:17 +02:00
|
|
|
*\endcode
|
|
|
|
|
*
|
2022-10-13 13:16:33 +02:00
|
|
|
* \since 0.3.60
|
2022-10-04 09:16:17 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define NAME "rtp-source"
|
|
|
|
|
|
|
|
|
|
PW_LOG_TOPIC_STATIC(mod_topic, "mod." NAME);
|
|
|
|
|
#define PW_LOG_TOPIC_DEFAULT mod_topic
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
#define DEFAULT_CLEANUP_SEC 60
|
2022-10-31 19:19:15 +03:00
|
|
|
#define ERROR_MSEC 2
|
2022-10-05 13:04:08 +02:00
|
|
|
|
2022-10-31 19:19:15 +03:00
|
|
|
#define DEFAULT_SESS_LATENCY 100
|
2022-10-06 11:41:01 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
#define DEFAULT_SOURCE_IP "224.0.0.56"
|
|
|
|
|
|
|
|
|
|
#define DEFAULT_FORMAT "S16BE"
|
|
|
|
|
#define DEFAULT_RATE 48000
|
|
|
|
|
#define DEFAULT_CHANNELS 2
|
|
|
|
|
#define DEFAULT_POSITION "[ FL FR ]"
|
|
|
|
|
|
2022-10-31 19:19:15 +03:00
|
|
|
#define BUFFER_SIZE (1u<<22)
|
|
|
|
|
#define BUFFER_MASK (BUFFER_SIZE-1)
|
2023-02-16 09:52:04 +01:00
|
|
|
#define BUFFER_SIZE2 (BUFFER_SIZE>>1)
|
|
|
|
|
#define BUFFER_MASK2 (BUFFER_SIZE2-1)
|
2022-10-06 11:41:01 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
#define USAGE "local.ifname=<local interface name to use> " \
|
|
|
|
|
"source.ip=<source IP address, default:"DEFAULT_SOURCE_IP"> " \
|
|
|
|
|
"source.port=<int, source port> " \
|
2022-10-06 11:41:01 +02:00
|
|
|
"sess.latency.msec=<target network latency, default "SPA_STRINGIFY(DEFAULT_SESS_LATENCY)"> " \
|
2023-02-21 17:18:52 +01:00
|
|
|
"rtp.media=<string, the media type audio|midi, default audio> " \
|
|
|
|
|
"audio.format=<format, default:"DEFAULT_FORMAT"> " \
|
|
|
|
|
"audio.rate=<sample rate, default:"SPA_STRINGIFY(DEFAULT_RATE)"> " \
|
|
|
|
|
"audio.channels=<number of channels, default:"SPA_STRINGIFY(DEFAULT_CHANNELS)"> "\
|
|
|
|
|
"audio.position=<channel map, default:"DEFAULT_POSITION"> " \
|
|
|
|
|
"stream.props= { key=value ... } "
|
2022-10-06 11:41:01 +02:00
|
|
|
|
|
|
|
|
static const struct spa_dict_item module_info[] = {
|
|
|
|
|
{ PW_KEY_MODULE_AUTHOR, "Wim Taymans <wim.taymans@gmail.com>" },
|
|
|
|
|
{ PW_KEY_MODULE_DESCRIPTION, "RTP Source" },
|
|
|
|
|
{ PW_KEY_MODULE_USAGE, USAGE },
|
|
|
|
|
{ PW_KEY_MODULE_VERSION, PACKAGE_VERSION },
|
|
|
|
|
};
|
2022-10-05 18:00:11 +02:00
|
|
|
|
2022-10-04 09:16:17 +02:00
|
|
|
struct impl {
|
|
|
|
|
struct pw_impl_module *module;
|
|
|
|
|
struct spa_hook module_listener;
|
|
|
|
|
struct pw_properties *props;
|
2023-02-21 19:16:23 +01:00
|
|
|
struct pw_context *context;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
|
|
|
|
struct pw_loop *loop;
|
2022-10-07 13:04:46 +02:00
|
|
|
struct pw_loop *data_loop;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
|
|
|
|
struct pw_core *core;
|
|
|
|
|
struct spa_hook core_listener;
|
|
|
|
|
struct spa_hook core_proxy_listener;
|
|
|
|
|
unsigned int do_disconnect:1;
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
char *ifname;
|
2023-01-25 19:03:17 +03:00
|
|
|
bool always_process;
|
2022-10-04 09:16:17 +02:00
|
|
|
int sess_latency_msec;
|
2022-10-31 19:19:15 +03:00
|
|
|
uint32_t cleanup_interval;
|
2022-10-04 20:44:07 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
struct spa_source *timer;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2023-02-16 09:52:04 +01:00
|
|
|
struct spa_audio_info info;
|
2023-02-21 17:18:52 +01:00
|
|
|
struct pw_properties *stream_props;
|
2022-10-05 13:04:08 +02:00
|
|
|
struct pw_stream *stream;
|
|
|
|
|
struct spa_hook stream_listener;
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
uint16_t src_port;
|
|
|
|
|
struct sockaddr_storage src_addr;
|
|
|
|
|
socklen_t src_len;
|
|
|
|
|
struct spa_source *source;
|
|
|
|
|
|
|
|
|
|
uint32_t rate;
|
|
|
|
|
uint32_t stride;
|
2022-10-05 13:04:08 +02:00
|
|
|
uint32_t expected_ssrc;
|
|
|
|
|
uint16_t expected_seq;
|
|
|
|
|
unsigned have_ssrc:1;
|
|
|
|
|
unsigned have_seq:1;
|
|
|
|
|
unsigned have_sync:1;
|
2023-02-21 17:18:52 +01:00
|
|
|
uint32_t ts_offset;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-04 20:44:07 +02:00
|
|
|
struct spa_ringbuffer ring;
|
|
|
|
|
uint8_t buffer[BUFFER_SIZE];
|
2022-10-05 13:04:08 +02:00
|
|
|
|
2022-12-08 11:01:24 +01:00
|
|
|
struct spa_io_rate_match *rate_match;
|
2023-02-01 18:31:33 +01:00
|
|
|
struct spa_io_position *position;
|
2022-10-05 13:04:08 +02:00
|
|
|
struct spa_dll dll;
|
2023-02-17 11:22:29 +01:00
|
|
|
double corr;
|
2022-10-05 13:04:08 +02:00
|
|
|
uint32_t target_buffer;
|
|
|
|
|
float max_error;
|
2023-02-21 17:18:52 +01:00
|
|
|
|
2023-01-25 16:23:00 +01:00
|
|
|
unsigned first:1;
|
2023-01-27 11:49:20 +01:00
|
|
|
unsigned receiving:1;
|
2023-02-01 18:31:33 +01:00
|
|
|
unsigned direct_timestamp:1;
|
2023-02-17 11:22:29 +01:00
|
|
|
|
|
|
|
|
float last_timestamp;
|
|
|
|
|
float last_time;
|
2022-10-04 20:44:07 +02:00
|
|
|
};
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
struct format_info {
|
|
|
|
|
uint32_t media_subtype;
|
|
|
|
|
uint32_t format;
|
|
|
|
|
uint32_t size;
|
|
|
|
|
const char *mime;
|
|
|
|
|
const char *media_type;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static uint32_t audio_get_stride(const struct spa_audio_info *info)
|
2022-10-04 20:44:07 +02:00
|
|
|
{
|
2023-02-21 17:18:52 +01:00
|
|
|
uint32_t stride = 0;
|
|
|
|
|
|
|
|
|
|
if (info->media_type != SPA_MEDIA_TYPE_audio ||
|
|
|
|
|
info->media_subtype != SPA_MEDIA_SUBTYPE_raw)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
switch (info->info.raw.format) {
|
|
|
|
|
case SPA_AUDIO_FORMAT_U8:
|
|
|
|
|
case SPA_AUDIO_FORMAT_ALAW:
|
|
|
|
|
case SPA_AUDIO_FORMAT_ULAW:
|
|
|
|
|
stride = 1;
|
|
|
|
|
break;
|
|
|
|
|
case SPA_AUDIO_FORMAT_S16_BE:
|
|
|
|
|
stride = 2;
|
|
|
|
|
break;
|
|
|
|
|
case SPA_AUDIO_FORMAT_S24_BE:
|
|
|
|
|
stride = 3;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return stride * info->info.raw.channels;
|
2022-10-04 09:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
static void process_audio(struct impl *impl)
|
2022-10-04 09:16:17 +02:00
|
|
|
{
|
2022-10-04 20:44:07 +02:00
|
|
|
struct pw_buffer *buf;
|
|
|
|
|
struct spa_data *d;
|
2023-02-02 15:49:58 +01:00
|
|
|
uint32_t wanted, timestamp, target_buffer, stride, maxsize;
|
|
|
|
|
int32_t avail;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
if ((buf = pw_stream_dequeue_buffer(impl->stream)) == NULL) {
|
2022-10-05 13:04:08 +02:00
|
|
|
pw_log_debug("Out of stream buffers: %m");
|
2022-10-04 20:44:07 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
d = buf->buffer->datas;
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
stride = impl->stride;
|
2023-02-02 15:49:58 +01:00
|
|
|
|
|
|
|
|
maxsize = d[0].maxsize / stride;
|
|
|
|
|
wanted = buf->requested ? SPA_MIN(buf->requested, maxsize) : maxsize;
|
2022-10-04 20:44:07 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
if (impl->position && impl->direct_timestamp) {
|
2023-02-02 15:49:58 +01:00
|
|
|
/* in direct mode, read directly from the timestamp index,
|
|
|
|
|
* because sender and receiver are in sync, this would keep
|
2023-02-16 09:52:04 +01:00
|
|
|
* target_buffer of samples available. */
|
2023-02-21 17:18:52 +01:00
|
|
|
spa_ringbuffer_read_update(&impl->ring,
|
|
|
|
|
impl->position->clock.position);
|
2023-02-01 18:31:33 +01:00
|
|
|
}
|
2023-02-21 17:18:52 +01:00
|
|
|
avail = spa_ringbuffer_get_read_index(&impl->ring, ×tamp);
|
2022-10-04 20:44:07 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
target_buffer = impl->target_buffer;
|
2023-01-25 16:23:00 +01:00
|
|
|
|
2023-02-02 15:49:58 +01:00
|
|
|
if (avail < (int32_t)wanted) {
|
|
|
|
|
enum spa_log_level level;
|
|
|
|
|
memset(d[0].data, 0, wanted * stride);
|
2023-02-21 17:18:52 +01:00
|
|
|
if (impl->have_sync) {
|
|
|
|
|
impl->have_sync = false;
|
2023-02-02 15:49:58 +01:00
|
|
|
level = SPA_LOG_LEVEL_WARN;
|
|
|
|
|
} else {
|
|
|
|
|
level = SPA_LOG_LEVEL_DEBUG;
|
2022-10-06 17:29:05 +02:00
|
|
|
}
|
2023-02-02 15:49:58 +01:00
|
|
|
pw_log(level, "underrun %d/%u < %u",
|
|
|
|
|
avail, target_buffer, wanted);
|
2023-01-25 16:23:00 +01:00
|
|
|
} else {
|
2022-10-05 13:04:08 +02:00
|
|
|
float error, corr;
|
2023-02-21 17:18:52 +01:00
|
|
|
if (impl->first) {
|
2023-02-02 15:49:58 +01:00
|
|
|
if ((uint32_t)avail > target_buffer) {
|
|
|
|
|
uint32_t skip = avail - target_buffer;
|
|
|
|
|
pw_log_debug("first: avail:%d skip:%u target:%u",
|
2023-01-25 16:23:00 +01:00
|
|
|
avail, skip, target_buffer);
|
2023-02-02 15:49:58 +01:00
|
|
|
timestamp += skip;
|
|
|
|
|
avail = target_buffer;
|
2023-01-25 16:23:00 +01:00
|
|
|
}
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->first = false;
|
2023-02-16 09:52:04 +01:00
|
|
|
} else if (avail > (int32_t)SPA_MIN(target_buffer * 8, BUFFER_SIZE / stride)) {
|
|
|
|
|
pw_log_warn("overrun %u > %u", avail, target_buffer * 8);
|
|
|
|
|
timestamp += avail - target_buffer;
|
|
|
|
|
avail = target_buffer;
|
2023-02-02 15:49:58 +01:00
|
|
|
}
|
2023-02-21 17:18:52 +01:00
|
|
|
if (!impl->direct_timestamp) {
|
2023-02-02 15:49:58 +01:00
|
|
|
/* when not using direct timestamp and clocks are not
|
|
|
|
|
* in sync, try to adjust our playback rate to keep the
|
|
|
|
|
* requested target_buffer bytes in the ringbuffer */
|
2023-01-25 16:23:00 +01:00
|
|
|
error = (float)target_buffer - (float)avail;
|
2023-02-21 17:18:52 +01:00
|
|
|
error = SPA_CLAMP(error, -impl->max_error, impl->max_error);
|
2022-10-05 13:04:08 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
corr = spa_dll_update(&impl->dll, error);
|
2022-10-05 13:04:08 +02:00
|
|
|
|
|
|
|
|
pw_log_debug("avail:%u target:%u error:%f corr:%f", avail,
|
2023-01-25 16:23:00 +01:00
|
|
|
target_buffer, error, corr);
|
2022-10-05 13:04:08 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
if (impl->rate_match) {
|
|
|
|
|
SPA_FLAG_SET(impl->rate_match->flags,
|
2023-02-02 15:49:58 +01:00
|
|
|
SPA_IO_RATE_MATCH_FLAG_ACTIVE);
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->rate_match->rate = 1.0f / corr;
|
2022-12-08 11:01:24 +01:00
|
|
|
}
|
2022-10-05 13:04:08 +02:00
|
|
|
}
|
2023-02-21 17:18:52 +01:00
|
|
|
spa_ringbuffer_read_data(&impl->ring,
|
|
|
|
|
impl->buffer,
|
2022-10-04 20:44:07 +02:00
|
|
|
BUFFER_SIZE,
|
2023-02-02 15:49:58 +01:00
|
|
|
(timestamp * stride) & BUFFER_MASK,
|
|
|
|
|
d[0].data, wanted * stride);
|
2022-10-05 13:04:08 +02:00
|
|
|
|
2023-02-02 15:49:58 +01:00
|
|
|
timestamp += wanted;
|
2023-02-21 17:18:52 +01:00
|
|
|
spa_ringbuffer_read_update(&impl->ring, timestamp);
|
2023-01-25 16:23:00 +01:00
|
|
|
}
|
2023-02-02 15:49:58 +01:00
|
|
|
d[0].chunk->size = wanted * stride;
|
|
|
|
|
d[0].chunk->stride = stride;
|
2023-01-25 16:23:00 +01:00
|
|
|
d[0].chunk->offset = 0;
|
2023-02-02 15:49:58 +01:00
|
|
|
buf->size = wanted;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
pw_stream_queue_buffer(impl->stream, buf);
|
2022-10-04 09:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
static void receive_audio(struct impl *impl, uint8_t *packet,
|
2023-02-16 09:52:04 +01:00
|
|
|
uint32_t timestamp, uint32_t payload_offset, uint32_t len)
|
|
|
|
|
{
|
|
|
|
|
uint32_t plen = len - payload_offset;
|
|
|
|
|
uint8_t *payload = &packet[payload_offset];
|
2023-02-21 17:18:52 +01:00
|
|
|
uint32_t stride = impl->stride;
|
2023-02-16 09:52:04 +01:00
|
|
|
uint32_t samples = plen / stride;
|
|
|
|
|
uint32_t write, expected_write;
|
|
|
|
|
int32_t filled;
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
filled = spa_ringbuffer_get_write_index(&impl->ring, &expected_write);
|
2023-02-16 09:52:04 +01:00
|
|
|
|
|
|
|
|
/* we always write to timestamp + delay */
|
2023-02-21 17:18:52 +01:00
|
|
|
write = timestamp + impl->target_buffer;
|
2023-02-16 09:52:04 +01:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
if (!impl->have_sync) {
|
2023-02-22 11:54:27 +01:00
|
|
|
pw_log_info("sync to timestamp:%u seq:%u ts_offset:%u SSRC:%u direct:%d",
|
|
|
|
|
write, impl->expected_seq-1, impl->ts_offset, impl->expected_ssrc,
|
|
|
|
|
impl->direct_timestamp);
|
|
|
|
|
|
2023-02-16 09:52:04 +01:00
|
|
|
/* we read from timestamp, keeping target_buffer of data
|
|
|
|
|
* in the ringbuffer. */
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->ring.readindex = timestamp;
|
|
|
|
|
impl->ring.writeindex = write;
|
|
|
|
|
filled = impl->target_buffer;
|
|
|
|
|
|
|
|
|
|
spa_dll_init(&impl->dll);
|
|
|
|
|
spa_dll_set_bw(&impl->dll, SPA_DLL_BW_MIN, 128, impl->rate);
|
|
|
|
|
memset(impl->buffer, 0, BUFFER_SIZE);
|
|
|
|
|
impl->have_sync = true;
|
2023-02-16 09:52:04 +01:00
|
|
|
} else if (expected_write != write) {
|
|
|
|
|
pw_log_debug("unexpected write (%u != %u)",
|
|
|
|
|
write, expected_write);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filled + samples > BUFFER_SIZE / stride) {
|
|
|
|
|
pw_log_debug("capture overrun %u + %u > %u", filled, samples,
|
|
|
|
|
BUFFER_SIZE / stride);
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->have_sync = false;
|
2023-02-16 09:52:04 +01:00
|
|
|
} else {
|
|
|
|
|
pw_log_debug("got samples:%u", samples);
|
2023-02-21 17:18:52 +01:00
|
|
|
spa_ringbuffer_write_data(&impl->ring,
|
|
|
|
|
impl->buffer,
|
2023-02-16 09:52:04 +01:00
|
|
|
BUFFER_SIZE,
|
|
|
|
|
(write * stride) & BUFFER_MASK,
|
|
|
|
|
payload, (samples * stride));
|
|
|
|
|
write += samples;
|
2023-02-21 17:18:52 +01:00
|
|
|
spa_ringbuffer_write_update(&impl->ring, write);
|
2023-02-16 09:52:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
static void process_midi(struct impl *impl)
|
2023-02-16 09:52:04 +01:00
|
|
|
{
|
|
|
|
|
struct pw_buffer *buf;
|
|
|
|
|
struct spa_data *d;
|
|
|
|
|
uint32_t timestamp, duration, maxsize, read;
|
|
|
|
|
struct spa_pod_builder b;
|
|
|
|
|
struct spa_pod_frame f[1];
|
|
|
|
|
void *ptr;
|
|
|
|
|
struct spa_pod *pod;
|
|
|
|
|
struct spa_pod_control *c;
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
if ((buf = pw_stream_dequeue_buffer(impl->stream)) == NULL) {
|
2023-02-16 09:52:04 +01:00
|
|
|
pw_log_debug("Out of stream buffers: %m");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
d = buf->buffer->datas;
|
|
|
|
|
|
|
|
|
|
maxsize = d[0].maxsize;
|
|
|
|
|
|
2023-02-17 11:22:29 +01:00
|
|
|
/* we always use the graph position to select events, the receiver side is
|
|
|
|
|
* responsible for smoothing out the RTP timestamps to graph time */
|
2023-02-21 17:18:52 +01:00
|
|
|
duration = impl->position->clock.duration;
|
|
|
|
|
if (impl->position)
|
|
|
|
|
timestamp = impl->position->clock.position;
|
2023-02-16 09:52:04 +01:00
|
|
|
else
|
|
|
|
|
timestamp = 0;
|
|
|
|
|
|
2023-02-17 11:22:29 +01:00
|
|
|
/* we copy events into the buffer based on the rtp timestamp + delay. */
|
2023-02-16 09:52:04 +01:00
|
|
|
spa_pod_builder_init(&b, d[0].data, maxsize);
|
|
|
|
|
spa_pod_builder_push_sequence(&b, &f[0], 0);
|
|
|
|
|
|
|
|
|
|
while (true) {
|
2023-02-21 17:18:52 +01:00
|
|
|
int32_t avail = spa_ringbuffer_get_read_index(&impl->ring, &read);
|
2023-02-16 09:52:04 +01:00
|
|
|
if (avail <= 0)
|
|
|
|
|
break;
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
ptr = SPA_PTROFF(impl->buffer, read & BUFFER_MASK2, void);
|
2023-02-16 09:52:04 +01:00
|
|
|
|
|
|
|
|
if ((pod = spa_pod_from_data(ptr, avail, 0, avail)) == NULL)
|
|
|
|
|
goto done;
|
|
|
|
|
if (!spa_pod_is_sequence(pod))
|
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
|
|
/* the ringbuffer contains series of sequences, one for each
|
|
|
|
|
* received packet */
|
|
|
|
|
SPA_POD_SEQUENCE_FOREACH((struct spa_pod_sequence*)pod, c) {
|
|
|
|
|
/* try to render with given delay */
|
2023-02-21 17:18:52 +01:00
|
|
|
uint32_t target = c->offset + impl->target_buffer;
|
2023-02-16 09:52:04 +01:00
|
|
|
if (timestamp != 0) {
|
|
|
|
|
/* skip old packets */
|
|
|
|
|
if (target < timestamp)
|
|
|
|
|
continue;
|
|
|
|
|
/* event for next cycle */
|
|
|
|
|
if (target >= timestamp + duration)
|
|
|
|
|
goto complete;
|
|
|
|
|
} else {
|
|
|
|
|
timestamp = target;
|
|
|
|
|
}
|
|
|
|
|
spa_pod_builder_control(&b, target - timestamp, SPA_CONTROL_Midi);
|
|
|
|
|
spa_pod_builder_bytes(&b,
|
|
|
|
|
SPA_POD_BODY(&c->value),
|
|
|
|
|
SPA_POD_BODY_SIZE(&c->value));
|
|
|
|
|
}
|
|
|
|
|
/* we completed a sequence (one RTP packet), advance ringbuffer
|
|
|
|
|
* and go to the next packet */
|
|
|
|
|
read += SPA_PTRDIFF(c, ptr);
|
2023-02-21 17:18:52 +01:00
|
|
|
spa_ringbuffer_read_update(&impl->ring, read);
|
2023-02-16 09:52:04 +01:00
|
|
|
}
|
|
|
|
|
complete:
|
|
|
|
|
spa_pod_builder_pop(&b, &f[0]);
|
|
|
|
|
|
|
|
|
|
if (b.state.offset > maxsize) {
|
|
|
|
|
pw_log_warn("overflow buffer %u %u", b.state.offset, maxsize);
|
|
|
|
|
b.state.offset = 0;
|
|
|
|
|
}
|
|
|
|
|
d[0].chunk->size = b.state.offset;
|
|
|
|
|
d[0].chunk->stride = 1;
|
|
|
|
|
d[0].chunk->offset = 0;
|
|
|
|
|
done:
|
2023-02-21 17:18:52 +01:00
|
|
|
pw_stream_queue_buffer(impl->stream, buf);
|
2023-02-16 09:52:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int parse_varlen(uint8_t *p, uint32_t avail, uint32_t *result)
|
|
|
|
|
{
|
|
|
|
|
uint32_t value = 0, offs = 0;
|
|
|
|
|
while (offs < avail) {
|
|
|
|
|
uint8_t b = p[offs++];
|
|
|
|
|
value = (value << 7) | (b & 0x7f);
|
|
|
|
|
if ((b & 0x80) == 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
*result = value;
|
|
|
|
|
return offs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int get_midi_size(uint8_t *p, uint32_t avail)
|
|
|
|
|
{
|
|
|
|
|
int size;
|
|
|
|
|
uint32_t offs = 0, value;
|
|
|
|
|
|
|
|
|
|
switch (p[offs++]) {
|
|
|
|
|
case 0xc0 ... 0xdf:
|
|
|
|
|
size = 2;
|
|
|
|
|
break;
|
|
|
|
|
case 0x80 ... 0xbf:
|
|
|
|
|
case 0xe0 ... 0xef:
|
|
|
|
|
size = 3;
|
|
|
|
|
break;
|
|
|
|
|
case 0xff:
|
|
|
|
|
case 0xf0:
|
|
|
|
|
case 0xf7:
|
|
|
|
|
size = parse_varlen(&p[offs], avail - offs, &value);
|
|
|
|
|
size += value + 1;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
static double get_time(struct impl *impl)
|
2023-02-17 11:22:29 +01:00
|
|
|
{
|
|
|
|
|
struct timespec ts;
|
|
|
|
|
double t;
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
2023-02-21 17:18:52 +01:00
|
|
|
t = impl->position->clock.position / (double) impl->position->clock.rate.denom;
|
|
|
|
|
t += (SPA_TIMESPEC_TO_NSEC(&ts) - impl->position->clock.nsec) / (double)SPA_NSEC_PER_SEC;
|
2023-02-17 11:22:29 +01:00
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
static void receive_midi(struct impl *impl, uint8_t *packet,
|
2023-02-16 09:52:04 +01:00
|
|
|
uint32_t timestamp, uint32_t payload_offset, uint32_t plen)
|
|
|
|
|
{
|
|
|
|
|
uint32_t write;
|
|
|
|
|
struct rtp_midi_header *hdr;
|
|
|
|
|
int32_t filled;
|
|
|
|
|
struct spa_pod_builder b;
|
|
|
|
|
struct spa_pod_frame f[1];
|
|
|
|
|
void *ptr;
|
|
|
|
|
uint32_t offs = payload_offset, len, end;
|
|
|
|
|
bool first = true;
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
if (impl->direct_timestamp) {
|
2023-02-17 11:22:29 +01:00
|
|
|
/* in direct timestamp we attach the RTP timestamp directly on the
|
|
|
|
|
* midi events and render them in the corresponding cycle */
|
2023-02-21 17:18:52 +01:00
|
|
|
if (!impl->have_sync) {
|
2023-02-22 11:54:27 +01:00
|
|
|
pw_log_info("sync to timestamp:%u seq:%u ts_offset:%u SSRC:%u direct:%d",
|
|
|
|
|
timestamp, impl->expected_seq-1, impl->ts_offset, impl->expected_ssrc,
|
|
|
|
|
impl->direct_timestamp);
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->have_sync = true;
|
2023-02-17 11:22:29 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* in non-direct timestamp mode, we relate the graph clock against
|
|
|
|
|
* the RTP timestamps */
|
2023-02-21 17:18:52 +01:00
|
|
|
double ts = timestamp / (float) impl->rate;
|
|
|
|
|
double t = get_time(impl);
|
2023-02-17 11:22:29 +01:00
|
|
|
double elapsed, estimated, diff;
|
|
|
|
|
|
|
|
|
|
/* the elapsed time between RTP timestamps */
|
2023-02-21 17:18:52 +01:00
|
|
|
elapsed = ts - impl->last_timestamp;
|
2023-02-17 11:22:29 +01:00
|
|
|
/* for that elapsed time, our clock should have advanced
|
|
|
|
|
* by this amount since the last estimation */
|
2023-02-21 17:18:52 +01:00
|
|
|
estimated = impl->last_time + elapsed * impl->corr;
|
2023-02-17 11:22:29 +01:00
|
|
|
/* calculate the diff between estimated and current clock time in
|
|
|
|
|
* samples */
|
2023-02-21 17:18:52 +01:00
|
|
|
diff = (estimated - t) * impl->rate;
|
2023-02-17 11:22:29 +01:00
|
|
|
|
|
|
|
|
/* no sync or we drifted too far, resync */
|
2023-02-21 17:18:52 +01:00
|
|
|
if (!impl->have_sync || fabs(diff) > impl->target_buffer) {
|
|
|
|
|
impl->corr = 1.0;
|
|
|
|
|
spa_dll_set_bw(&impl->dll, SPA_DLL_BW_MIN, 256, impl->rate);
|
2023-02-17 11:22:29 +01:00
|
|
|
|
2023-02-22 11:54:27 +01:00
|
|
|
pw_log_info("sync to timestamp:%u seq:%u ts_offset:%u SSRC:%u direct:%d",
|
|
|
|
|
timestamp, impl->expected_seq-1, impl->ts_offset, impl->expected_ssrc,
|
|
|
|
|
impl->direct_timestamp);
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->have_sync = true;
|
|
|
|
|
impl->ring.readindex = impl->ring.writeindex;
|
2023-02-17 11:22:29 +01:00
|
|
|
} else {
|
|
|
|
|
/* update our new rate correction */
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->corr = spa_dll_update(&impl->dll, diff);
|
2023-02-17 11:22:29 +01:00
|
|
|
/* our current time is now the estimated time */
|
|
|
|
|
t = estimated;
|
|
|
|
|
}
|
2023-02-21 17:18:52 +01:00
|
|
|
pw_log_debug("%f %f %f %f", t, estimated, diff, impl->corr);
|
2023-02-17 11:22:29 +01:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
timestamp = t * impl->rate;
|
2023-02-17 11:22:29 +01:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->last_timestamp = ts;
|
|
|
|
|
impl->last_time = t;
|
2023-02-16 09:52:04 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
filled = spa_ringbuffer_get_write_index(&impl->ring, &write);
|
2023-02-16 09:52:04 +01:00
|
|
|
if (filled > (int32_t)BUFFER_SIZE2)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
hdr = (struct rtp_midi_header *)&packet[offs++];
|
|
|
|
|
len = hdr->len;
|
|
|
|
|
if (hdr->b) {
|
|
|
|
|
len = (len << 8) | hdr->len_b;
|
|
|
|
|
offs++;
|
|
|
|
|
}
|
|
|
|
|
end = len + offs;
|
|
|
|
|
if (end > plen)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
ptr = SPA_PTROFF(impl->buffer, write & BUFFER_MASK2, void);
|
2023-02-16 09:52:04 +01:00
|
|
|
|
|
|
|
|
/* each packet is written as a sequence of events. The offset is
|
|
|
|
|
* the RTP timestamp */
|
|
|
|
|
spa_pod_builder_init(&b, ptr, BUFFER_SIZE2 - filled);
|
|
|
|
|
spa_pod_builder_push_sequence(&b, &f[0], 0);
|
|
|
|
|
|
|
|
|
|
while (offs < end) {
|
|
|
|
|
uint32_t delta;
|
|
|
|
|
int size;
|
|
|
|
|
|
|
|
|
|
if (first && !hdr->z)
|
|
|
|
|
delta = 0;
|
|
|
|
|
else
|
|
|
|
|
offs += parse_varlen(&packet[offs], end - offs, &delta);
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
timestamp += delta * impl->corr;
|
2023-02-16 09:52:04 +01:00
|
|
|
spa_pod_builder_control(&b, timestamp, SPA_CONTROL_Midi);
|
|
|
|
|
|
|
|
|
|
size = get_midi_size(&packet[offs], end - offs);
|
|
|
|
|
|
|
|
|
|
if (size <= 0 || offs + size > end) {
|
|
|
|
|
pw_log_warn("invalid size (%08x) %d (%u %u)",
|
|
|
|
|
packet[offs], size, offs, end);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spa_pod_builder_bytes(&b, &packet[offs], size);
|
|
|
|
|
|
|
|
|
|
offs += size;
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
spa_pod_builder_pop(&b, &f[0]);
|
|
|
|
|
|
|
|
|
|
write += b.state.offset;
|
2023-02-21 17:18:52 +01:00
|
|
|
spa_ringbuffer_write_update(&impl->ring, write);
|
2023-02-16 09:52:04 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-08 11:01:24 +01:00
|
|
|
static void stream_io_changed(void *data, uint32_t id, void *area, uint32_t size)
|
|
|
|
|
{
|
2023-02-21 17:18:52 +01:00
|
|
|
struct impl *impl = data;
|
2022-12-08 11:01:24 +01:00
|
|
|
switch (id) {
|
|
|
|
|
case SPA_IO_RateMatch:
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->rate_match = area;
|
2022-12-08 11:01:24 +01:00
|
|
|
break;
|
2023-02-01 18:31:33 +01:00
|
|
|
case SPA_IO_Position:
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->position = area;
|
2023-02-01 18:31:33 +01:00
|
|
|
break;
|
2022-12-08 11:01:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 09:52:04 +01:00
|
|
|
static void stream_destroy(void *d)
|
2023-01-25 20:07:52 +03:00
|
|
|
{
|
2023-02-21 17:18:52 +01:00
|
|
|
struct impl *impl = d;
|
|
|
|
|
spa_hook_remove(&impl->stream_listener);
|
|
|
|
|
impl->stream = NULL;
|
2023-02-16 09:52:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void stream_process(void *data)
|
|
|
|
|
{
|
2023-02-21 17:18:52 +01:00
|
|
|
struct impl *impl = data;
|
|
|
|
|
switch (impl->info.media_type) {
|
2023-02-16 09:52:04 +01:00
|
|
|
case SPA_MEDIA_TYPE_audio:
|
2023-02-21 17:18:52 +01:00
|
|
|
process_audio(impl);
|
2023-02-16 09:52:04 +01:00
|
|
|
break;
|
|
|
|
|
case SPA_MEDIA_TYPE_application:
|
2023-02-21 17:18:52 +01:00
|
|
|
process_midi(impl);
|
2023-02-16 09:52:04 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2023-01-25 20:07:52 +03:00
|
|
|
}
|
|
|
|
|
|
2022-10-04 22:48:26 +02:00
|
|
|
static void
|
|
|
|
|
on_rtp_io(void *data, int fd, uint32_t mask)
|
|
|
|
|
{
|
2023-02-21 17:18:52 +01:00
|
|
|
struct impl *impl = data;
|
2022-10-05 13:04:08 +02:00
|
|
|
struct rtp_header *hdr;
|
|
|
|
|
ssize_t len, hlen;
|
2023-02-16 09:52:04 +01:00
|
|
|
uint8_t buffer[2048];
|
2022-10-04 22:48:26 +02:00
|
|
|
|
|
|
|
|
if (mask & SPA_IO_IN) {
|
2022-10-05 13:04:08 +02:00
|
|
|
uint16_t seq;
|
2023-02-16 09:52:04 +01:00
|
|
|
uint32_t timestamp;
|
2022-10-04 22:48:26 +02:00
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
if ((len = recv(fd, buffer, sizeof(buffer), 0)) < 0)
|
|
|
|
|
goto receive_error;
|
|
|
|
|
|
2022-10-04 22:48:26 +02:00
|
|
|
if (len < 12)
|
2022-10-05 13:04:08 +02:00
|
|
|
goto short_packet;
|
2022-10-04 22:48:26 +02:00
|
|
|
|
|
|
|
|
hdr = (struct rtp_header*)buffer;
|
|
|
|
|
if (hdr->v != 2)
|
2022-10-05 13:04:08 +02:00
|
|
|
goto invalid_version;
|
2022-10-04 22:48:26 +02:00
|
|
|
|
|
|
|
|
hlen = 12 + hdr->cc * 4;
|
|
|
|
|
if (hlen > len)
|
2022-10-05 13:04:08 +02:00
|
|
|
goto invalid_len;
|
2022-10-04 22:48:26 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
if (impl->have_ssrc && impl->expected_ssrc != hdr->ssrc)
|
2022-10-05 13:04:08 +02:00
|
|
|
goto unexpected_ssrc;
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->expected_ssrc = hdr->ssrc;
|
|
|
|
|
impl->have_ssrc = true;
|
2022-10-05 13:04:08 +02:00
|
|
|
|
|
|
|
|
seq = ntohs(hdr->sequence_number);
|
2023-02-21 17:18:52 +01:00
|
|
|
if (impl->have_seq && impl->expected_seq != seq) {
|
2023-02-22 11:54:27 +01:00
|
|
|
pw_log_info("unexpected seq (%d != %d) SSRC:%u",
|
|
|
|
|
seq, impl->expected_seq, hdr->ssrc);
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->have_sync = false;
|
2022-10-05 13:04:08 +02:00
|
|
|
}
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->expected_seq = seq + 1;
|
|
|
|
|
impl->have_seq = true;
|
2022-10-05 13:04:08 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
timestamp = ntohl(hdr->timestamp) - impl->ts_offset;
|
2022-10-05 13:04:08 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
switch (impl->info.media_type) {
|
2023-02-16 09:52:04 +01:00
|
|
|
case SPA_MEDIA_TYPE_audio:
|
2023-02-21 17:18:52 +01:00
|
|
|
receive_audio(impl, buffer, timestamp, hlen, len);
|
2023-02-16 09:52:04 +01:00
|
|
|
break;
|
|
|
|
|
case SPA_MEDIA_TYPE_application:
|
2023-02-21 17:18:52 +01:00
|
|
|
receive_midi(impl, buffer, timestamp, hlen, len);
|
2022-10-04 22:48:26 +02:00
|
|
|
}
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->receiving = true;
|
2022-10-04 22:48:26 +02:00
|
|
|
}
|
2022-10-05 13:04:08 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
receive_error:
|
|
|
|
|
pw_log_warn("recv error: %m");
|
|
|
|
|
return;
|
|
|
|
|
short_packet:
|
|
|
|
|
pw_log_warn("short packet received");
|
|
|
|
|
return;
|
|
|
|
|
invalid_version:
|
|
|
|
|
pw_log_warn("invalid RTP version");
|
2023-02-21 17:18:52 +01:00
|
|
|
spa_debug_mem(0, buffer, len);
|
2022-10-05 13:04:08 +02:00
|
|
|
return;
|
|
|
|
|
invalid_len:
|
|
|
|
|
pw_log_warn("invalid RTP length");
|
|
|
|
|
return;
|
|
|
|
|
unexpected_ssrc:
|
|
|
|
|
pw_log_warn("unexpected SSRC (expected %u != %u)",
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->expected_ssrc, hdr->ssrc);
|
2022-10-05 13:04:08 +02:00
|
|
|
return;
|
2022-10-04 22:48:26 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
static int parse_address(const char *address, uint16_t port,
|
|
|
|
|
struct sockaddr_storage *addr, socklen_t *len)
|
|
|
|
|
{
|
|
|
|
|
struct sockaddr_in *sa4 = (struct sockaddr_in*)addr;
|
|
|
|
|
struct sockaddr_in6 *sa6 = (struct sockaddr_in6*)addr;
|
|
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET, address, &sa4->sin_addr) > 0) {
|
|
|
|
|
sa4->sin_family = AF_INET;
|
|
|
|
|
sa4->sin_port = htons(port);
|
|
|
|
|
*len = sizeof(*sa4);
|
|
|
|
|
} else if (inet_pton(AF_INET6, address, &sa6->sin6_addr) > 0) {
|
|
|
|
|
sa6->sin6_family = AF_INET6;
|
|
|
|
|
sa6->sin6_port = htons(port);
|
|
|
|
|
*len = sizeof(*sa6);
|
|
|
|
|
} else
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-07 10:33:24 +02:00
|
|
|
static int make_socket(const struct sockaddr* sa, socklen_t salen, char *ifname)
|
2022-10-04 22:48:26 +02:00
|
|
|
{
|
|
|
|
|
int af, fd, val, res;
|
2022-10-05 13:04:08 +02:00
|
|
|
struct ifreq req;
|
2022-10-04 22:48:26 +02:00
|
|
|
|
|
|
|
|
af = sa->sa_family;
|
|
|
|
|
if ((fd = socket(af, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)) < 0) {
|
|
|
|
|
pw_log_error("socket failed: %m");
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
|
|
|
|
#ifdef SO_TIMESTAMP
|
|
|
|
|
val = 1;
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &val, sizeof(val)) < 0) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
pw_log_error("setsockopt failed: %m");
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
val = 1;
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
pw_log_error("setsockopt failed: %m");
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2022-10-05 13:04:08 +02:00
|
|
|
|
|
|
|
|
spa_zero(req);
|
|
|
|
|
if (ifname) {
|
|
|
|
|
snprintf(req.ifr_name, sizeof(req.ifr_name), "%s", ifname);
|
|
|
|
|
res = ioctl(fd, SIOCGIFINDEX, &req);
|
|
|
|
|
if (res < 0)
|
|
|
|
|
pw_log_warn("SIOCGIFINDEX %s failed: %m", ifname);
|
|
|
|
|
}
|
2022-10-04 22:48:26 +02:00
|
|
|
res = 0;
|
|
|
|
|
if (af == AF_INET) {
|
|
|
|
|
static const uint32_t ipv4_mcast_mask = 0xe0000000;
|
2022-10-07 10:33:24 +02:00
|
|
|
struct sockaddr_in *sa4 = (struct sockaddr_in*)sa;
|
2022-10-04 22:48:26 +02:00
|
|
|
if ((ntohl(sa4->sin_addr.s_addr) & ipv4_mcast_mask) == ipv4_mcast_mask) {
|
2022-10-05 13:04:08 +02:00
|
|
|
struct ip_mreqn mr4;
|
2022-10-04 22:48:26 +02:00
|
|
|
memset(&mr4, 0, sizeof(mr4));
|
|
|
|
|
mr4.imr_multiaddr = sa4->sin_addr;
|
2022-10-05 13:04:08 +02:00
|
|
|
mr4.imr_ifindex = req.ifr_ifindex;
|
2022-10-04 22:48:26 +02:00
|
|
|
res = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mr4, sizeof(mr4));
|
2022-10-07 10:33:24 +02:00
|
|
|
} else {
|
|
|
|
|
sa4->sin_addr.s_addr = INADDR_ANY;
|
2022-10-04 22:48:26 +02:00
|
|
|
}
|
|
|
|
|
} else if (af == AF_INET6) {
|
2022-10-07 10:33:24 +02:00
|
|
|
struct sockaddr_in6 *sa6 = (struct sockaddr_in6*)sa;
|
2022-10-04 22:48:26 +02:00
|
|
|
if (sa6->sin6_addr.s6_addr[0] == 0xff) {
|
|
|
|
|
struct ipv6_mreq mr6;
|
|
|
|
|
memset(&mr6, 0, sizeof(mr6));
|
|
|
|
|
mr6.ipv6mr_multiaddr = sa6->sin6_addr;
|
2022-10-05 13:04:08 +02:00
|
|
|
mr6.ipv6mr_interface = req.ifr_ifindex;
|
2022-10-04 22:48:26 +02:00
|
|
|
res = setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mr6, sizeof(mr6));
|
2022-10-07 10:33:24 +02:00
|
|
|
} else {
|
|
|
|
|
sa6->sin6_addr = in6addr_any;
|
2022-10-04 22:48:26 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
res = -EINVAL;
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
pw_log_error("join mcast failed: %m");
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bind(fd, sa, salen) < 0) {
|
|
|
|
|
res = -errno;
|
2022-10-07 10:33:24 +02:00
|
|
|
pw_log_error("bind() failed: %m");
|
2022-10-04 22:48:26 +02:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
return fd;
|
|
|
|
|
error:
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
static uint32_t msec_to_samples(struct impl *impl, uint32_t msec)
|
2022-10-05 13:04:08 +02:00
|
|
|
{
|
2023-02-21 17:18:52 +01:00
|
|
|
return msec * impl->rate / 1000;
|
2022-10-05 13:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
static int stream_start(struct impl *impl)
|
2022-10-07 10:33:24 +02:00
|
|
|
{
|
2023-01-25 19:03:17 +03:00
|
|
|
int fd;
|
2023-02-21 17:18:52 +01:00
|
|
|
|
|
|
|
|
if (impl->source != NULL)
|
|
|
|
|
return 0;
|
2023-01-25 19:03:17 +03:00
|
|
|
|
|
|
|
|
pw_log_info("starting RTP listener");
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
if ((fd = make_socket((const struct sockaddr *)&impl->src_addr,
|
|
|
|
|
impl->src_len, impl->ifname)) < 0) {
|
2023-01-25 19:03:17 +03:00
|
|
|
pw_log_error("failed to create socket: %m");
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->source = pw_loop_add_io(impl->data_loop, fd,
|
|
|
|
|
SPA_IO_IN, true, on_rtp_io, impl);
|
|
|
|
|
if (impl->source == NULL) {
|
2023-01-25 19:03:17 +03:00
|
|
|
pw_log_error("can't create io source: %m");
|
|
|
|
|
close(fd);
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
static void stream_stop(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
if (!impl->source)
|
2023-01-25 19:03:17 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
pw_log_info("stopping RTP listener");
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
pw_loop_destroy_source(impl->data_loop, impl->source);
|
|
|
|
|
impl->source = NULL;
|
2023-01-25 19:03:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_stream_state_changed(void *d, enum pw_stream_state old,
|
|
|
|
|
enum pw_stream_state state, const char *error)
|
|
|
|
|
{
|
2023-02-21 17:18:52 +01:00
|
|
|
struct impl *impl = d;
|
2023-01-25 19:03:17 +03:00
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
|
case PW_STREAM_STATE_UNCONNECTED:
|
|
|
|
|
pw_log_info("stream disconnected, unloading");
|
|
|
|
|
pw_impl_module_schedule_destroy(impl->module);
|
|
|
|
|
break;
|
|
|
|
|
case PW_STREAM_STATE_ERROR:
|
|
|
|
|
pw_log_error("stream error: %s", error);
|
|
|
|
|
break;
|
|
|
|
|
case PW_STREAM_STATE_STREAMING:
|
2023-02-21 17:18:52 +01:00
|
|
|
if ((errno = -stream_start(impl)) < 0)
|
2023-01-25 19:03:17 +03:00
|
|
|
pw_log_error("failed to start RTP stream: %m");
|
|
|
|
|
break;
|
|
|
|
|
case PW_STREAM_STATE_PAUSED:
|
|
|
|
|
if (!impl->always_process)
|
2023-02-21 17:18:52 +01:00
|
|
|
stream_stop(impl);
|
2023-01-25 19:03:17 +03:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_stream_events out_stream_events = {
|
|
|
|
|
PW_VERSION_STREAM_EVENTS,
|
|
|
|
|
.destroy = stream_destroy,
|
|
|
|
|
.state_changed = on_stream_state_changed,
|
|
|
|
|
.io_changed = stream_io_changed,
|
|
|
|
|
.process = stream_process
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
static int setup_stream(struct impl *impl)
|
2022-10-04 09:16:17 +02:00
|
|
|
{
|
|
|
|
|
const struct spa_pod *params[1];
|
|
|
|
|
struct spa_pod_builder b;
|
|
|
|
|
uint32_t n_params;
|
2023-02-21 17:42:01 +01:00
|
|
|
enum pw_stream_flags flags;
|
2022-10-04 09:16:17 +02:00
|
|
|
uint8_t buffer[1024];
|
2022-10-04 20:44:07 +02:00
|
|
|
struct pw_properties *props;
|
2023-02-21 17:18:52 +01:00
|
|
|
int res;
|
2022-10-04 20:44:07 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->first = true;
|
2022-10-04 20:44:07 +02:00
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
props = pw_properties_copy(impl->stream_props);
|
2022-10-07 10:33:24 +02:00
|
|
|
if (props == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
spa_dll_init(&impl->dll);
|
|
|
|
|
spa_dll_set_bw(&impl->dll, SPA_DLL_BW_MIN, 128, impl->rate);
|
|
|
|
|
impl->corr = 1.0;
|
2023-01-25 14:58:15 +01:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->stream = pw_stream_new(impl->core,
|
2022-10-04 20:44:07 +02:00
|
|
|
"rtp-source playback", props);
|
2023-02-21 17:18:52 +01:00
|
|
|
if (impl->stream == NULL) {
|
2022-10-07 10:33:24 +02:00
|
|
|
res = -errno;
|
|
|
|
|
pw_log_error("can't create stream: %m");
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
pw_stream_add_listener(impl->stream,
|
|
|
|
|
&impl->stream_listener,
|
|
|
|
|
&out_stream_events, impl);
|
2022-10-04 09:16:17 +02:00
|
|
|
|
|
|
|
|
n_params = 0;
|
|
|
|
|
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
2023-02-16 09:52:04 +01:00
|
|
|
|
2023-02-21 17:42:01 +01:00
|
|
|
flags = PW_STREAM_FLAG_MAP_BUFFERS | PW_STREAM_FLAG_RT_PROCESS;
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
switch (impl->info.media_type) {
|
2023-02-16 09:52:04 +01:00
|
|
|
case SPA_MEDIA_TYPE_audio:
|
|
|
|
|
params[n_params++] = spa_format_audio_build(&b,
|
2023-02-21 17:18:52 +01:00
|
|
|
SPA_PARAM_EnumFormat, &impl->info);
|
2023-02-21 17:42:01 +01:00
|
|
|
flags |= PW_STREAM_FLAG_AUTOCONNECT;
|
2023-02-16 09:52:04 +01:00
|
|
|
break;
|
|
|
|
|
case SPA_MEDIA_TYPE_application:
|
|
|
|
|
params[n_params++] = spa_pod_builder_add_object(&b,
|
|
|
|
|
SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat,
|
|
|
|
|
SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_application),
|
|
|
|
|
SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_control));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
if ((res = pw_stream_connect(impl->stream,
|
2022-10-04 09:16:17 +02:00
|
|
|
PW_DIRECTION_OUTPUT,
|
|
|
|
|
PW_ID_ANY,
|
2023-02-21 17:42:01 +01:00
|
|
|
flags,
|
2022-10-07 10:33:24 +02:00
|
|
|
params, n_params)) < 0) {
|
|
|
|
|
pw_log_error("can't connect stream: %s", spa_strerror(res));
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2023-01-25 19:03:17 +03:00
|
|
|
if (impl->always_process &&
|
2023-02-21 17:18:52 +01:00
|
|
|
(res = stream_start(impl)) < 0)
|
2022-10-07 10:33:24 +02:00
|
|
|
goto error;
|
2022-10-04 22:48:26 +02:00
|
|
|
|
2022-10-04 09:16:17 +02:00
|
|
|
return 0;
|
2022-10-04 18:12:48 +02:00
|
|
|
error:
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-05 13:47:28 +02:00
|
|
|
static void on_timer_event(void *data, uint64_t expirations)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
2023-02-21 17:18:52 +01:00
|
|
|
|
|
|
|
|
if (!impl->receiving) {
|
2023-02-21 17:42:01 +01:00
|
|
|
pw_log_info("timeout, inactive RTP source");
|
|
|
|
|
//pw_impl_module_schedule_destroy(impl->module);
|
2023-02-21 17:18:52 +01:00
|
|
|
} else {
|
|
|
|
|
pw_log_debug("timeout, keeping active RTP source");
|
2022-10-05 13:47:28 +02:00
|
|
|
}
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->receiving = false;
|
2022-10-05 13:47:28 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-04 20:44:07 +02:00
|
|
|
static void core_destroy(void *d)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = d;
|
|
|
|
|
spa_hook_remove(&impl->core_listener);
|
|
|
|
|
impl->core = NULL;
|
|
|
|
|
pw_impl_module_schedule_destroy(impl->module);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_proxy_events core_proxy_events = {
|
|
|
|
|
.destroy = core_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void impl_destroy(struct impl *impl)
|
|
|
|
|
{
|
2023-02-21 17:18:52 +01:00
|
|
|
if (impl->stream)
|
|
|
|
|
pw_stream_destroy(impl->stream);
|
|
|
|
|
if (impl->source)
|
|
|
|
|
pw_loop_destroy_source(impl->data_loop, impl->source);
|
2022-10-04 20:44:07 +02:00
|
|
|
|
|
|
|
|
if (impl->core && impl->do_disconnect)
|
|
|
|
|
pw_core_disconnect(impl->core);
|
|
|
|
|
|
2022-10-05 13:47:28 +02:00
|
|
|
if (impl->timer)
|
|
|
|
|
pw_loop_destroy_source(impl->loop, impl->timer);
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
pw_properties_free(impl->stream_props);
|
2022-10-04 20:44:07 +02:00
|
|
|
pw_properties_free(impl->props);
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
free(impl->ifname);
|
2022-10-04 20:44:07 +02:00
|
|
|
free(impl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void module_destroy(void *d)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = d;
|
|
|
|
|
spa_hook_remove(&impl->module_listener);
|
|
|
|
|
impl_destroy(impl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_impl_module_events module_events = {
|
|
|
|
|
PW_VERSION_IMPL_MODULE_EVENTS,
|
|
|
|
|
.destroy = module_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void on_core_error(void *d, uint32_t id, int seq, int res, const char *message)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = d;
|
|
|
|
|
|
|
|
|
|
pw_log_error("error id:%u seq:%d res:%d (%s): %s",
|
|
|
|
|
id, seq, res, spa_strerror(res), message);
|
|
|
|
|
|
|
|
|
|
if (id == PW_ID_CORE && res == -EPIPE)
|
|
|
|
|
pw_impl_module_schedule_destroy(impl->module);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_core_events core_events = {
|
|
|
|
|
PW_VERSION_CORE_EVENTS,
|
|
|
|
|
.error = on_core_error,
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
static inline uint32_t format_from_name(const char *name, size_t len)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; spa_type_audio_format[i].name; i++) {
|
|
|
|
|
if (strncmp(name, spa_debug_type_short_name(spa_type_audio_format[i].name), len) == 0)
|
|
|
|
|
return spa_type_audio_format[i].type;
|
|
|
|
|
}
|
|
|
|
|
return SPA_AUDIO_FORMAT_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint32_t channel_from_name(const char *name)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; spa_type_audio_channel[i].name; i++) {
|
|
|
|
|
if (spa_streq(name, spa_debug_type_short_name(spa_type_audio_channel[i].name)))
|
|
|
|
|
return spa_type_audio_channel[i].type;
|
|
|
|
|
}
|
|
|
|
|
return SPA_AUDIO_CHANNEL_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void parse_position(struct spa_audio_info_raw *info, const char *val, size_t len)
|
|
|
|
|
{
|
|
|
|
|
struct spa_json it[2];
|
|
|
|
|
char v[256];
|
|
|
|
|
|
|
|
|
|
spa_json_init(&it[0], val, len);
|
|
|
|
|
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
|
|
|
|
|
spa_json_init(&it[1], val, len);
|
|
|
|
|
|
|
|
|
|
info->channels = 0;
|
|
|
|
|
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 &&
|
|
|
|
|
info->channels < SPA_AUDIO_MAX_CHANNELS) {
|
|
|
|
|
info->position[info->channels++] = channel_from_name(v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void parse_audio_info(const struct pw_properties *props, struct spa_audio_info_raw *info)
|
|
|
|
|
{
|
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
|
|
spa_zero(*info);
|
|
|
|
|
if ((str = pw_properties_get(props, PW_KEY_AUDIO_FORMAT)) == NULL)
|
|
|
|
|
str = DEFAULT_FORMAT;
|
|
|
|
|
info->format = format_from_name(str, strlen(str));
|
|
|
|
|
|
|
|
|
|
info->rate = pw_properties_get_uint32(props, PW_KEY_AUDIO_RATE, info->rate);
|
|
|
|
|
if (info->rate == 0)
|
|
|
|
|
info->rate = DEFAULT_RATE;
|
|
|
|
|
|
|
|
|
|
info->channels = pw_properties_get_uint32(props, PW_KEY_AUDIO_CHANNELS, info->channels);
|
|
|
|
|
info->channels = SPA_MIN(info->channels, SPA_AUDIO_MAX_CHANNELS);
|
|
|
|
|
if ((str = pw_properties_get(props, SPA_KEY_AUDIO_POSITION)) != NULL)
|
|
|
|
|
parse_position(info, str, strlen(str));
|
|
|
|
|
if (info->channels == 0)
|
|
|
|
|
parse_position(info, DEFAULT_POSITION, strlen(DEFAULT_POSITION));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void copy_props(struct impl *impl, struct pw_properties *props, const char *key)
|
|
|
|
|
{
|
|
|
|
|
const char *str;
|
|
|
|
|
if ((str = pw_properties_get(props, key)) != NULL) {
|
|
|
|
|
if (pw_properties_get(impl->stream_props, key) == NULL)
|
|
|
|
|
pw_properties_set(impl->stream_props, key, str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 09:16:17 +02:00
|
|
|
SPA_EXPORT
|
|
|
|
|
int pipewire__module_init(struct pw_impl_module *module, const char *args)
|
|
|
|
|
{
|
|
|
|
|
struct pw_context *context = pw_impl_module_get_context(module);
|
2023-02-21 17:18:52 +01:00
|
|
|
uint32_t id = pw_global_get_id(pw_impl_module_get_global(module));
|
|
|
|
|
uint32_t pid = getpid();
|
2022-10-04 09:16:17 +02:00
|
|
|
struct impl *impl;
|
|
|
|
|
const char *str;
|
2022-10-05 13:47:28 +02:00
|
|
|
struct timespec value, interval;
|
2023-02-21 17:18:52 +01:00
|
|
|
struct pw_properties *props, *stream_props;
|
2022-10-04 09:16:17 +02:00
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
|
|
PW_LOG_TOPIC_INIT(mod_topic);
|
|
|
|
|
|
|
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
|
|
if (args == NULL)
|
|
|
|
|
args = "";
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
props = impl->props = pw_properties_new_string(args);
|
|
|
|
|
stream_props = impl->stream_props = pw_properties_new(NULL, NULL);
|
|
|
|
|
if (props == NULL || stream_props == NULL) {
|
2022-10-04 09:16:17 +02:00
|
|
|
res = -errno;
|
|
|
|
|
pw_log_error( "can't create properties: %m");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl->module = module;
|
2023-02-21 19:16:23 +01:00
|
|
|
impl->context = context;
|
2022-10-04 09:16:17 +02:00
|
|
|
impl->loop = pw_context_get_main_loop(context);
|
2022-10-07 13:04:46 +02:00
|
|
|
impl->data_loop = pw_data_loop_get_loop(pw_context_get_data_loop(context));
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
if (pw_properties_get(stream_props, PW_KEY_NODE_VIRTUAL) == NULL)
|
|
|
|
|
pw_properties_set(stream_props, PW_KEY_NODE_VIRTUAL, "true");
|
|
|
|
|
if (pw_properties_get(stream_props, PW_KEY_NODE_NETWORK) == NULL)
|
|
|
|
|
pw_properties_set(stream_props, PW_KEY_NODE_NETWORK, "true");
|
|
|
|
|
|
|
|
|
|
if (pw_properties_get(props, PW_KEY_NODE_NAME) == NULL)
|
|
|
|
|
pw_properties_setf(props, PW_KEY_NODE_NAME, "rtp-source-%u-%u", pid, id);
|
|
|
|
|
if (pw_properties_get(props, PW_KEY_NODE_DESCRIPTION) == NULL)
|
|
|
|
|
pw_properties_set(props, PW_KEY_NODE_DESCRIPTION,
|
|
|
|
|
pw_properties_get(props, PW_KEY_NODE_NAME));
|
|
|
|
|
if (pw_properties_get(props, PW_KEY_MEDIA_NAME) == NULL)
|
|
|
|
|
pw_properties_set(props, PW_KEY_MEDIA_NAME, "RTP Receiver Stream");
|
|
|
|
|
|
|
|
|
|
if ((str = pw_properties_get(props, "stream.props")) != NULL)
|
|
|
|
|
pw_properties_update_string(stream_props, str, strlen(str));
|
|
|
|
|
|
|
|
|
|
copy_props(impl, props, PW_KEY_AUDIO_FORMAT);
|
|
|
|
|
copy_props(impl, props, PW_KEY_AUDIO_RATE);
|
|
|
|
|
copy_props(impl, props, PW_KEY_AUDIO_CHANNELS);
|
|
|
|
|
copy_props(impl, props, SPA_KEY_AUDIO_POSITION);
|
|
|
|
|
copy_props(impl, props, PW_KEY_NODE_NAME);
|
|
|
|
|
copy_props(impl, props, PW_KEY_NODE_DESCRIPTION);
|
|
|
|
|
copy_props(impl, props, PW_KEY_NODE_GROUP);
|
|
|
|
|
copy_props(impl, props, PW_KEY_NODE_LATENCY);
|
|
|
|
|
copy_props(impl, props, PW_KEY_NODE_VIRTUAL);
|
|
|
|
|
copy_props(impl, props, PW_KEY_NODE_CHANNELNAMES);
|
|
|
|
|
copy_props(impl, props, PW_KEY_MEDIA_NAME);
|
|
|
|
|
copy_props(impl, props, PW_KEY_MEDIA_CLASS);
|
|
|
|
|
|
|
|
|
|
impl->info.media_type = SPA_MEDIA_TYPE_audio;
|
|
|
|
|
impl->info.media_subtype = SPA_MEDIA_SUBTYPE_raw;
|
|
|
|
|
if ((str = pw_properties_get(stream_props, "rtp.media")) != NULL) {
|
|
|
|
|
if (spa_streq(str, "audio")) {
|
|
|
|
|
impl->info.media_type = SPA_MEDIA_TYPE_audio;
|
|
|
|
|
impl->info.media_subtype = SPA_MEDIA_SUBTYPE_raw;
|
|
|
|
|
}
|
|
|
|
|
else if (spa_streq(str, "midi")) {
|
|
|
|
|
impl->info.media_type = SPA_MEDIA_TYPE_application;
|
|
|
|
|
impl->info.media_subtype = SPA_MEDIA_SUBTYPE_control;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
pw_log_error("unsupported media type:%s", str);
|
|
|
|
|
res = -EINVAL;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-05 13:04:08 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
switch (impl->info.media_type) {
|
|
|
|
|
case SPA_MEDIA_TYPE_audio:
|
|
|
|
|
parse_audio_info(stream_props, &impl->info.info.raw);
|
|
|
|
|
impl->stride = audio_get_stride(&impl->info);
|
|
|
|
|
if (impl->stride == 0) {
|
|
|
|
|
pw_log_error("unsupported audio format:%d channels:%d",
|
|
|
|
|
impl->info.info.raw.format, impl->info.info.raw.channels);
|
|
|
|
|
res = -EINVAL;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
impl->rate = impl->info.info.raw.rate;
|
|
|
|
|
break;
|
|
|
|
|
case SPA_MEDIA_TYPE_application:
|
|
|
|
|
pw_properties_set(stream_props, PW_KEY_FORMAT_DSP, "8 bit raw midi");
|
|
|
|
|
impl->stride = 1;
|
|
|
|
|
impl->rate = 48000;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
spa_assert_not_reached();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-10-07 16:12:22 +02:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
str = pw_properties_get(props, "local.ifname");
|
2022-10-05 13:04:08 +02:00
|
|
|
impl->ifname = str ? strdup(str) : NULL;
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->src_port = pw_properties_get_uint32(stream_props, "source.port", 0);
|
|
|
|
|
if (impl->src_port == 0) {
|
|
|
|
|
pw_log_error("invalid source.port");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
if ((str = pw_properties_get(stream_props, "source.ip")) == NULL ||
|
|
|
|
|
(res = parse_address(str, impl->src_port, &impl->src_addr, &impl->src_len)) < 0) {
|
|
|
|
|
pw_log_error("invalid source.ip %s: %s", str, spa_strerror(res));
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl->always_process = pw_properties_get_bool(stream_props,
|
|
|
|
|
PW_KEY_NODE_ALWAYS_PROCESS, true);
|
|
|
|
|
|
|
|
|
|
impl->cleanup_interval = pw_properties_get_uint32(props,
|
|
|
|
|
"cleanup.sec", DEFAULT_CLEANUP_SEC);
|
|
|
|
|
|
|
|
|
|
if ((str = pw_properties_get(props, "sess.name")) != NULL) {
|
|
|
|
|
pw_properties_set(stream_props, "rtp.session", str);
|
|
|
|
|
if (pw_properties_get(stream_props, PW_KEY_MEDIA_NAME) == NULL)
|
|
|
|
|
pw_properties_setf(stream_props, PW_KEY_MEDIA_NAME, "RTP Stream (%s)", str);
|
|
|
|
|
if (pw_properties_get(stream_props, PW_KEY_NODE_NAME) == NULL)
|
|
|
|
|
pw_properties_setf(stream_props, PW_KEY_NODE_NAME, "%s", str);
|
|
|
|
|
} else {
|
|
|
|
|
if (pw_properties_get(stream_props, PW_KEY_MEDIA_NAME) == NULL)
|
|
|
|
|
pw_properties_set(stream_props, PW_KEY_MEDIA_NAME, "RTP Stream");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl->ts_offset = pw_properties_get_int64(stream_props, "sess.ts-offset", 0);
|
|
|
|
|
pw_properties_setf(stream_props, "rtp.ts-offset", "%u", impl->ts_offset);
|
2023-01-25 19:03:17 +03:00
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->direct_timestamp = pw_properties_get_bool(stream_props,
|
|
|
|
|
"sess.ts-direct", false);
|
|
|
|
|
|
|
|
|
|
impl->sess_latency_msec = pw_properties_get_uint32(stream_props,
|
2022-10-05 13:04:08 +02:00
|
|
|
"sess.latency.msec", DEFAULT_SESS_LATENCY);
|
2023-02-21 17:18:52 +01:00
|
|
|
impl->target_buffer = msec_to_samples(impl, impl->sess_latency_msec);
|
|
|
|
|
impl->max_error = msec_to_samples(impl, ERROR_MSEC);
|
|
|
|
|
|
|
|
|
|
pw_properties_setf(stream_props, PW_KEY_NODE_RATE, "1/%d", impl->rate);
|
|
|
|
|
pw_properties_setf(stream_props, PW_KEY_NODE_LATENCY, "%d/%d",
|
|
|
|
|
impl->target_buffer / 2, impl->rate);
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2023-02-21 19:16:23 +01:00
|
|
|
impl->core = pw_context_get_object(impl->context, PW_TYPE_INTERFACE_Core);
|
2022-10-04 09:16:17 +02:00
|
|
|
if (impl->core == NULL) {
|
2023-02-21 17:18:52 +01:00
|
|
|
str = pw_properties_get(props, PW_KEY_REMOTE_NAME);
|
2023-02-21 19:16:23 +01:00
|
|
|
impl->core = pw_context_connect(impl->context,
|
2022-10-04 09:16:17 +02:00
|
|
|
pw_properties_new(
|
|
|
|
|
PW_KEY_REMOTE_NAME, str,
|
|
|
|
|
NULL),
|
|
|
|
|
0);
|
|
|
|
|
impl->do_disconnect = true;
|
|
|
|
|
}
|
|
|
|
|
if (impl->core == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
pw_log_error("can't connect: %m");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pw_proxy_add_listener((struct pw_proxy*)impl->core,
|
|
|
|
|
&impl->core_proxy_listener,
|
|
|
|
|
&core_proxy_events, impl);
|
|
|
|
|
pw_core_add_listener(impl->core,
|
|
|
|
|
&impl->core_listener,
|
|
|
|
|
&core_events, impl);
|
|
|
|
|
|
2022-10-05 13:47:28 +02:00
|
|
|
impl->timer = pw_loop_add_timer(impl->loop, on_timer_event, impl);
|
|
|
|
|
if (impl->timer == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
pw_log_error("can't create timer source: %m");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2023-02-21 17:18:52 +01:00
|
|
|
value.tv_sec = impl->cleanup_interval;
|
|
|
|
|
value.tv_nsec = 0;
|
2022-10-31 19:19:15 +03:00
|
|
|
interval.tv_sec = impl->cleanup_interval;
|
2022-10-05 13:47:28 +02:00
|
|
|
interval.tv_nsec = 0;
|
|
|
|
|
pw_loop_update_timer(impl->loop, impl->timer, &value, &interval, false);
|
|
|
|
|
|
2023-02-21 17:18:52 +01:00
|
|
|
if ((res = setup_stream(impl)) < 0)
|
2022-10-04 09:16:17 +02:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
pw_impl_module_add_listener(module, &impl->module_listener, &module_events, impl);
|
|
|
|
|
|
|
|
|
|
pw_impl_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_info));
|
|
|
|
|
|
|
|
|
|
pw_log_info("Successfully loaded module-rtp-source");
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
out:
|
|
|
|
|
impl_destroy(impl);
|
|
|
|
|
return res;
|
|
|
|
|
}
|