2022-10-04 09:16:17 +02:00
|
|
|
/* PipeWire
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2022 Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* 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 (including the next
|
|
|
|
|
* paragraph) 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-10-04 13:19:14 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
|
2022-10-04 09:16:17 +02:00
|
|
|
#include <limits.h>
|
|
|
|
|
#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/param/audio/format-utils.h>
|
|
|
|
|
#include <spa/utils/hook.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>
|
|
|
|
|
#include <spa/debug/mem.h>
|
2022-10-04 09:16:17 +02:00
|
|
|
|
|
|
|
|
#include <pipewire/pipewire.h>
|
|
|
|
|
#include <pipewire/private.h>
|
2022-10-04 13:19:14 +02:00
|
|
|
|
|
|
|
|
#include <module-rtp/sap.h>
|
|
|
|
|
#include <module-rtp/rtp.h>
|
|
|
|
|
|
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
|
|
|
|
|
* RTP packets.
|
|
|
|
|
*
|
|
|
|
|
* ## Module Options
|
|
|
|
|
*
|
|
|
|
|
* Options specific to the behavior of this module
|
|
|
|
|
*
|
|
|
|
|
* - `source.props = {}`: properties to be passed to the source stream
|
|
|
|
|
* - `source.name = <str>`: node.name of the source
|
|
|
|
|
* - `local.ip = <str>`: local sender ip
|
|
|
|
|
* - `sess.latency.msec = <str>`: target network latency in milliseconds
|
|
|
|
|
*
|
|
|
|
|
* ## General options
|
|
|
|
|
*
|
|
|
|
|
* Options with well-known behavior:
|
|
|
|
|
*
|
|
|
|
|
* - \ref PW_KEY_NODE_NAME
|
|
|
|
|
* - \ref PW_KEY_NODE_DESCRIPTION
|
|
|
|
|
* - \ref PW_KEY_MEDIA_NAME
|
|
|
|
|
*
|
|
|
|
|
* ## Example configuration
|
|
|
|
|
*\code{.unparsed}
|
|
|
|
|
* context.modules = [
|
|
|
|
|
* { name = libpipewire-module-rtp-source
|
|
|
|
|
* args = {
|
2022-10-05 13:04:08 +02:00
|
|
|
* #sap.address = 224.0.0.56
|
|
|
|
|
* #sap.port = 9875
|
|
|
|
|
* #local.ifname = eth0
|
2022-10-04 13:19:14 +02:00
|
|
|
* sess.latency.msec = 200
|
2022-10-05 13:04:08 +02:00
|
|
|
* stream.props = {
|
2022-10-04 09:16:17 +02:00
|
|
|
* node.name = "rtp-source"
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
*]
|
|
|
|
|
*\endcode
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define NAME "rtp-source"
|
|
|
|
|
|
2022-10-05 13:04:08 +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, "sap.address=<SAP address to listen on> "
|
|
|
|
|
"sap.port=<SAP port to listen on> "
|
|
|
|
|
"local.ifname=<local interface name to use> "
|
|
|
|
|
"sess.latency.msec=<target network latency in milliseconds> "
|
|
|
|
|
"source.props= { key=value ... }" },
|
|
|
|
|
{ PW_KEY_MODULE_VERSION, PACKAGE_VERSION },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2022-10-04 09:16:17 +02:00
|
|
|
PW_LOG_TOPIC_STATIC(mod_topic, "mod." NAME);
|
|
|
|
|
#define PW_LOG_TOPIC_DEFAULT mod_topic
|
|
|
|
|
|
2022-10-04 13:19:14 +02:00
|
|
|
#define SAP_DEFAULT_IP "224.0.0.56"
|
2022-10-04 09:16:17 +02:00
|
|
|
#define SAP_DEFAULT_PORT 9875
|
|
|
|
|
#define DEFAULT_SESS_LATENCY 200
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
#define ERROR_MSEC 2
|
|
|
|
|
#define MAX_SESSIONS 16
|
|
|
|
|
#define MTU 1500
|
2022-10-05 13:47:28 +02:00
|
|
|
#define CLEANUP_INTERVAL_SEC 20
|
2022-10-05 13:04:08 +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;
|
|
|
|
|
struct pw_context *module_context;
|
|
|
|
|
|
|
|
|
|
struct pw_loop *loop;
|
|
|
|
|
|
|
|
|
|
struct pw_core *core;
|
|
|
|
|
struct spa_hook core_listener;
|
|
|
|
|
struct spa_hook core_proxy_listener;
|
|
|
|
|
|
2022-10-05 13:47:28 +02:00
|
|
|
struct spa_source *timer;
|
2022-10-04 09:16:17 +02:00
|
|
|
struct spa_source *sap_source;
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
struct pw_properties *stream_props;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
|
|
|
|
unsigned int do_disconnect:1;
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
char *ifname;
|
|
|
|
|
char *sap_address;
|
|
|
|
|
int sap_port;
|
2022-10-04 09:16:17 +02:00
|
|
|
int sess_latency_msec;
|
2022-10-04 20:44:07 +02:00
|
|
|
|
|
|
|
|
struct spa_list sessions;
|
2022-10-05 13:04:08 +02:00
|
|
|
uint32_t n_sessions;
|
2022-10-04 09:16:17 +02:00
|
|
|
};
|
|
|
|
|
|
2022-10-04 20:44:07 +02:00
|
|
|
struct sdp_info {
|
2022-10-04 22:48:26 +02:00
|
|
|
uint16_t hash;
|
|
|
|
|
|
2022-10-04 20:44:07 +02:00
|
|
|
char origin[128];
|
|
|
|
|
char session[256];
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-04 20:44:07 +02:00
|
|
|
struct sockaddr_storage sa;
|
|
|
|
|
socklen_t salen;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-04 20:44:07 +02:00
|
|
|
uint16_t port;
|
|
|
|
|
uint8_t payload;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-04 20:44:07 +02:00
|
|
|
struct spa_audio_info_raw info;
|
|
|
|
|
uint32_t stride;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define BUFFER_SIZE (1u<<16)
|
|
|
|
|
#define BUFFER_MASK (BUFFER_SIZE-1)
|
|
|
|
|
|
|
|
|
|
struct session {
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
struct spa_list link;
|
|
|
|
|
|
2022-10-05 13:47:28 +02:00
|
|
|
uint64_t timestamp;
|
|
|
|
|
|
2022-10-04 20:44:07 +02:00
|
|
|
struct sdp_info info;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-04 20:44:07 +02:00
|
|
|
struct spa_source *source;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
struct pw_stream *stream;
|
|
|
|
|
struct spa_hook stream_listener;
|
|
|
|
|
|
|
|
|
|
uint32_t expected_ssrc;
|
|
|
|
|
uint16_t expected_seq;
|
|
|
|
|
unsigned have_ssrc:1;
|
|
|
|
|
unsigned have_seq:1;
|
|
|
|
|
unsigned have_sync:1;
|
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
|
|
|
|
|
|
|
|
struct spa_dll dll;
|
|
|
|
|
uint32_t target_buffer;
|
|
|
|
|
float max_error;
|
|
|
|
|
unsigned buffering:1;
|
2022-10-04 20:44:07 +02:00
|
|
|
};
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-04 20:44:07 +02:00
|
|
|
static void stream_destroy(void *d)
|
|
|
|
|
{
|
|
|
|
|
struct session *sess = d;
|
2022-10-05 13:04:08 +02:00
|
|
|
spa_hook_remove(&sess->stream_listener);
|
|
|
|
|
sess->stream = NULL;
|
2022-10-04 09:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
static void stream_process(void *data)
|
2022-10-04 09:16:17 +02:00
|
|
|
{
|
2022-10-04 20:44:07 +02:00
|
|
|
struct session *sess = data;
|
|
|
|
|
struct pw_buffer *buf;
|
|
|
|
|
struct spa_data *d;
|
|
|
|
|
uint32_t index;
|
|
|
|
|
int32_t avail, wanted;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
if ((buf = pw_stream_dequeue_buffer(sess->stream)) == NULL) {
|
|
|
|
|
pw_log_debug("Out of stream buffers: %m");
|
2022-10-04 20:44:07 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
d = buf->buffer->datas;
|
|
|
|
|
|
|
|
|
|
wanted = buf->requested ?
|
|
|
|
|
SPA_MIN(buf->requested * sess->info.stride, d[0].maxsize)
|
|
|
|
|
: d[0].maxsize;
|
|
|
|
|
|
|
|
|
|
avail = spa_ringbuffer_get_read_index(&sess->ring, &index);
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
if (avail < wanted || sess->buffering) {
|
2022-10-04 20:44:07 +02:00
|
|
|
memset(d[0].data, 0, wanted);
|
2022-10-05 13:04:08 +02:00
|
|
|
if (!sess->buffering)
|
2022-10-05 14:03:18 +02:00
|
|
|
pw_log_warn("underrun %u/%u < %u", avail, sess->target_buffer, wanted);
|
2022-10-04 20:44:07 +02:00
|
|
|
} else {
|
2022-10-05 13:04:08 +02:00
|
|
|
float error, corr;
|
|
|
|
|
if (avail > (int32_t)BUFFER_SIZE) {
|
|
|
|
|
index += avail - sess->target_buffer;
|
|
|
|
|
avail = sess->target_buffer;
|
|
|
|
|
pw_log_warn("overrun %u > %u", avail, BUFFER_SIZE);
|
|
|
|
|
} else {
|
|
|
|
|
error = (float)sess->target_buffer - (float)avail;
|
|
|
|
|
error = SPA_CLAMP(error, -sess->max_error, sess->max_error);
|
|
|
|
|
|
|
|
|
|
corr = spa_dll_update(&sess->dll, error);
|
|
|
|
|
|
|
|
|
|
pw_log_debug("avail:%u target:%u error:%f corr:%f", avail,
|
|
|
|
|
sess->target_buffer, error, corr);
|
|
|
|
|
|
|
|
|
|
pw_stream_set_control(sess->stream,
|
|
|
|
|
SPA_PROP_rate, 1, &corr, NULL);
|
|
|
|
|
}
|
|
|
|
|
spa_ringbuffer_read_data(&sess->ring,
|
|
|
|
|
sess->buffer,
|
2022-10-04 20:44:07 +02:00
|
|
|
BUFFER_SIZE,
|
|
|
|
|
index & BUFFER_MASK,
|
|
|
|
|
d[0].data, wanted);
|
2022-10-05 13:04:08 +02:00
|
|
|
|
2022-10-04 20:44:07 +02:00
|
|
|
index += wanted;
|
|
|
|
|
spa_ringbuffer_read_update(&sess->ring, index);
|
|
|
|
|
}
|
|
|
|
|
d[0].chunk->size = wanted;
|
|
|
|
|
d[0].chunk->stride = sess->info.stride;
|
|
|
|
|
d[0].chunk->offset = 0;
|
|
|
|
|
buf->size = wanted / sess->info.stride;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
pw_stream_queue_buffer(sess->stream, buf);
|
2022-10-04 09:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_stream_state_changed(void *d, enum pw_stream_state old,
|
|
|
|
|
enum pw_stream_state state, const char *error)
|
|
|
|
|
{
|
2022-10-04 20:44:07 +02:00
|
|
|
struct session *sess = d;
|
|
|
|
|
struct impl *impl = sess->impl;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
|
case PW_STREAM_STATE_UNCONNECTED:
|
|
|
|
|
pw_log_info("stream disconnected, unloading");
|
2022-10-04 20:44:07 +02:00
|
|
|
pw_impl_module_schedule_destroy(impl->module);
|
2022-10-04 09:16:17 +02:00
|
|
|
break;
|
|
|
|
|
case PW_STREAM_STATE_ERROR:
|
|
|
|
|
pw_log_error("stream error: %s", error);
|
|
|
|
|
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,
|
2022-10-05 13:04:08 +02:00
|
|
|
.process = stream_process
|
2022-10-04 09:16:17 +02:00
|
|
|
};
|
|
|
|
|
|
2022-10-04 22:48:26 +02:00
|
|
|
static void
|
|
|
|
|
on_rtp_io(void *data, int fd, uint32_t mask)
|
|
|
|
|
{
|
|
|
|
|
struct session *sess = data;
|
2022-10-05 13:04:08 +02:00
|
|
|
struct rtp_header *hdr;
|
|
|
|
|
ssize_t len, hlen;
|
|
|
|
|
uint8_t buffer[2048], *payload;
|
2022-10-04 22:48:26 +02:00
|
|
|
|
|
|
|
|
if (mask & SPA_IO_IN) {
|
2022-10-05 13:04:08 +02:00
|
|
|
uint32_t index, expected_index, timestamp;
|
|
|
|
|
uint16_t seq;
|
2022-10-04 22:48:26 +02:00
|
|
|
int32_t filled;
|
|
|
|
|
|
2022-10-04 23:27:44 +02:00
|
|
|
pw_log_debug("got rtp");
|
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
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
if (sess->have_ssrc && sess->expected_ssrc != hdr->ssrc)
|
|
|
|
|
goto unexpected_ssrc;
|
|
|
|
|
sess->expected_ssrc = hdr->ssrc;
|
|
|
|
|
sess->have_ssrc = true;
|
|
|
|
|
|
|
|
|
|
seq = ntohs(hdr->sequence_number);
|
|
|
|
|
if (sess->have_seq && sess->expected_seq != seq) {
|
|
|
|
|
pw_log_warn("unexpected seq (%d != %d)", seq, sess->expected_seq);
|
|
|
|
|
}
|
|
|
|
|
sess->expected_seq = seq + 1;
|
|
|
|
|
sess->have_seq = true;
|
|
|
|
|
|
|
|
|
|
len = SPA_ROUND_DOWN(len - hlen, sess->info.stride);
|
2022-10-04 22:48:26 +02:00
|
|
|
payload = &buffer[hlen];
|
|
|
|
|
|
|
|
|
|
filled = spa_ringbuffer_get_write_index(&sess->ring, &index);
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
timestamp = ntohl(hdr->timestamp);
|
|
|
|
|
expected_index = timestamp * sess->info.stride;
|
|
|
|
|
|
|
|
|
|
if (!sess->have_sync) {
|
|
|
|
|
sess->ring.readindex = sess->ring.writeindex =
|
|
|
|
|
index = expected_index;
|
2022-10-05 13:47:28 +02:00
|
|
|
filled = 0;
|
2022-10-05 13:04:08 +02:00
|
|
|
sess->have_sync = true;
|
|
|
|
|
sess->buffering = true;
|
|
|
|
|
pw_log_info("sync to timestamp %u", index);
|
|
|
|
|
} else if (expected_index != index) {
|
|
|
|
|
pw_log_warn("unexpected timestamp (%u != %u)",
|
|
|
|
|
index / sess->info.stride,
|
|
|
|
|
expected_index / sess->info.stride);
|
|
|
|
|
index = expected_index;
|
2022-10-05 13:47:28 +02:00
|
|
|
filled = 0;
|
2022-10-05 13:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-04 22:48:26 +02:00
|
|
|
if (filled + len > BUFFER_SIZE) {
|
2022-10-05 13:04:08 +02:00
|
|
|
pw_log_warn("capture overrun %u %zd", filled, len);
|
2022-10-05 13:47:28 +02:00
|
|
|
sess->have_sync = false;
|
2022-10-04 22:48:26 +02:00
|
|
|
} else {
|
|
|
|
|
spa_ringbuffer_write_data(&sess->ring,
|
|
|
|
|
sess->buffer,
|
|
|
|
|
BUFFER_SIZE,
|
|
|
|
|
index & BUFFER_MASK,
|
|
|
|
|
payload, len);
|
|
|
|
|
index += len;
|
2022-10-05 13:04:08 +02:00
|
|
|
filled += len;
|
2022-10-04 22:48:26 +02:00
|
|
|
spa_ringbuffer_write_update(&sess->ring, index);
|
2022-10-05 13:04:08 +02:00
|
|
|
|
|
|
|
|
if ((uint32_t)filled > sess->target_buffer)
|
|
|
|
|
sess->buffering = false;
|
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");
|
|
|
|
|
return;
|
|
|
|
|
invalid_len:
|
|
|
|
|
pw_log_warn("invalid RTP length");
|
|
|
|
|
return;
|
|
|
|
|
unexpected_ssrc:
|
|
|
|
|
pw_log_warn("unexpected SSRC (expected %u != %u)",
|
|
|
|
|
sess->expected_ssrc, hdr->ssrc);
|
|
|
|
|
return;
|
2022-10-04 22:48:26 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
static int make_multicast_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;
|
|
|
|
|
const struct sockaddr_in *sa4 = (struct sockaddr_in*)sa;
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
} else if (af == AF_INET6) {
|
|
|
|
|
const struct sockaddr_in6 *sa6 = (struct sockaddr_in6*)sa;
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
} 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;
|
|
|
|
|
pw_log_warn("bind() failed: %m");
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
return fd;
|
|
|
|
|
error:
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
static uint32_t msec_to_bytes(struct sdp_info *info, uint32_t msec)
|
|
|
|
|
{
|
|
|
|
|
return msec * info->stride * info->info.rate / 1000;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-05 13:47:28 +02:00
|
|
|
static void session_touch(struct session *sess)
|
|
|
|
|
{
|
|
|
|
|
struct timespec ts;
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
|
sess->timestamp = SPA_TIMESPEC_TO_NSEC(&ts);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
static int session_new(struct impl *impl, struct sdp_info *info)
|
2022-10-04 09:16:17 +02:00
|
|
|
{
|
2022-10-04 20:44:07 +02:00
|
|
|
struct session *session;
|
2022-10-04 09:16:17 +02:00
|
|
|
const struct spa_pod *params[1];
|
|
|
|
|
struct spa_pod_builder b;
|
|
|
|
|
uint32_t n_params;
|
|
|
|
|
uint8_t buffer[1024];
|
2022-10-04 20:44:07 +02:00
|
|
|
struct pw_properties *props;
|
2022-10-04 22:48:26 +02:00
|
|
|
int res, fd;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
if (impl->n_sessions >= MAX_SESSIONS) {
|
|
|
|
|
pw_log_warn("too many sessions (%u >= %u)", impl->n_sessions, MAX_SESSIONS);
|
|
|
|
|
return -EMFILE;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 20:44:07 +02:00
|
|
|
session = calloc(1, sizeof(struct session));
|
|
|
|
|
if (session == NULL)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
|
|
session->impl = impl;
|
2022-10-05 13:04:08 +02:00
|
|
|
session->info = *info;
|
2022-10-04 20:44:07 +02:00
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
session->target_buffer = msec_to_bytes(info, impl->sess_latency_msec);
|
|
|
|
|
session->max_error = msec_to_bytes(info, ERROR_MSEC);
|
|
|
|
|
|
|
|
|
|
spa_dll_init(&session->dll);
|
|
|
|
|
spa_dll_set_bw(&session->dll, SPA_DLL_BW_MIN, 128, session->info.info.rate);
|
|
|
|
|
|
|
|
|
|
props = pw_properties_copy(impl->stream_props);
|
2022-10-04 20:44:07 +02:00
|
|
|
if (props == NULL)
|
|
|
|
|
return -errno;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
pw_properties_setf(props, PW_KEY_NODE_RATE, "1/%d", info->info.rate);
|
|
|
|
|
pw_properties_setf(props, PW_KEY_NODE_LATENCY, "%d/%d",
|
|
|
|
|
session->target_buffer / info->stride, info->info.rate);
|
|
|
|
|
pw_properties_set(props, "rtp.origin", info->origin);
|
|
|
|
|
pw_properties_setf(props, "rtp.payload", "%u", info->payload);
|
|
|
|
|
if (info->session[0]) {
|
|
|
|
|
pw_properties_set(props, "rtp.session", info->session);
|
|
|
|
|
pw_properties_setf(props, PW_KEY_MEDIA_NAME, "RTP Stream (%s)",
|
|
|
|
|
info->session);
|
|
|
|
|
} else {
|
|
|
|
|
pw_properties_set(props, PW_KEY_MEDIA_NAME, "RTP Stream");
|
|
|
|
|
}
|
2022-10-04 23:27:44 +02:00
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
pw_log_info("new session %s %s", info->origin, info->session);
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
session->stream = pw_stream_new(impl->core,
|
2022-10-04 20:44:07 +02:00
|
|
|
"rtp-source playback", props);
|
2022-10-05 13:04:08 +02:00
|
|
|
if (session->stream == NULL)
|
2022-10-04 09:16:17 +02:00
|
|
|
return -errno;
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
pw_stream_add_listener(session->stream,
|
|
|
|
|
&session->stream_listener,
|
2022-10-04 20:44:07 +02:00
|
|
|
&out_stream_events, session);
|
2022-10-04 09:16:17 +02:00
|
|
|
|
|
|
|
|
n_params = 0;
|
|
|
|
|
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
|
|
|
|
params[n_params++] = spa_format_audio_raw_build(&b, SPA_PARAM_EnumFormat,
|
2022-10-05 13:04:08 +02:00
|
|
|
&info->info);
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
|
|
|
|
|
if ((res = pw_stream_connect(session->stream,
|
2022-10-04 09:16:17 +02:00
|
|
|
PW_DIRECTION_OUTPUT,
|
|
|
|
|
PW_ID_ANY,
|
|
|
|
|
PW_STREAM_FLAG_MAP_BUFFERS |
|
|
|
|
|
PW_STREAM_FLAG_AUTOCONNECT |
|
|
|
|
|
PW_STREAM_FLAG_RT_PROCESS,
|
|
|
|
|
params, n_params)) < 0)
|
|
|
|
|
return res;
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
if ((fd = make_multicast_socket((const struct sockaddr *)&info->sa,
|
|
|
|
|
info->salen, impl->ifname)) < 0)
|
2022-10-04 22:48:26 +02:00
|
|
|
return fd;
|
|
|
|
|
|
2022-10-04 23:27:44 +02:00
|
|
|
pw_log_info("starting RTP listener");
|
2022-10-04 22:48:26 +02:00
|
|
|
session->source = pw_loop_add_io(impl->loop, fd,
|
|
|
|
|
SPA_IO_IN, true, on_rtp_io, session);
|
|
|
|
|
if (session->source == NULL)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
2022-10-05 13:47:28 +02:00
|
|
|
session_touch(session);
|
2022-10-04 22:48:26 +02:00
|
|
|
spa_list_append(&impl->sessions, &session->link);
|
2022-10-05 13:04:08 +02:00
|
|
|
impl->n_sessions++;
|
2022-10-04 22:48:26 +02:00
|
|
|
|
2022-10-04 09:16:17 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 20:44:07 +02:00
|
|
|
static void session_free(struct session *sess)
|
|
|
|
|
{
|
2022-10-05 13:47:28 +02:00
|
|
|
pw_log_info("free session %s %s", sess->info.origin, sess->info.session);
|
2022-10-05 13:04:08 +02:00
|
|
|
sess->impl->n_sessions--;
|
2022-10-04 20:44:07 +02:00
|
|
|
spa_list_remove(&sess->link);
|
2022-10-05 13:04:08 +02:00
|
|
|
if (sess->stream)
|
|
|
|
|
pw_stream_destroy(sess->stream);
|
2022-10-04 22:48:26 +02:00
|
|
|
if (sess->source)
|
|
|
|
|
pw_loop_destroy_source(sess->impl->loop, sess->source);
|
2022-10-04 20:44:07 +02:00
|
|
|
free(sess);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 22:48:26 +02:00
|
|
|
static struct session *session_find(struct impl *impl, struct sdp_info *info)
|
|
|
|
|
{
|
|
|
|
|
struct session *sess;
|
|
|
|
|
spa_list_for_each(sess, &impl->sessions, link) {
|
|
|
|
|
if (info->hash == sess->info.hash &&
|
|
|
|
|
spa_streq(info->origin, sess->info.origin))
|
|
|
|
|
return sess;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 18:12:48 +02:00
|
|
|
static int parse_sdp_c(struct impl *impl, char *c, struct sdp_info *info)
|
|
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
c[strcspn(c, "/")] = 0;
|
|
|
|
|
if (spa_strstartswith(c, "c=IN IP4 ")) {
|
|
|
|
|
struct sockaddr_in *sa = (struct sockaddr_in*) &info->sa;
|
|
|
|
|
|
2022-10-04 22:48:26 +02:00
|
|
|
c += strlen("c=IN IP4 ");
|
2022-10-04 18:12:48 +02:00
|
|
|
if (inet_pton(AF_INET, c, &sa->sin_addr) <= 0) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
pw_log_warn("inet_pton(%s) failed: %m", c);
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
sa->sin_family = AF_INET;
|
|
|
|
|
info->salen = sizeof(struct sockaddr_in);
|
|
|
|
|
}
|
|
|
|
|
else if (spa_strstartswith(c, "c=IN IP6 ")) {
|
|
|
|
|
struct sockaddr_in6 *sa = (struct sockaddr_in6*) &info->sa;
|
|
|
|
|
|
2022-10-04 22:48:26 +02:00
|
|
|
c += strlen("c=IN IP6 ");
|
2022-10-04 18:12:48 +02:00
|
|
|
if (inet_pton(AF_INET6, c, &sa->sin6_addr) <= 0) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
pw_log_warn("inet_pton(%s) failed: %m", c);
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sa->sin6_family = AF_INET6;
|
|
|
|
|
info->salen = sizeof(struct sockaddr_in6);
|
|
|
|
|
} else
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res= 0;
|
|
|
|
|
error:
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int parse_sdp_m(struct impl *impl, char *c, struct sdp_info *info)
|
2022-10-04 13:19:14 +02:00
|
|
|
{
|
2022-10-04 18:12:48 +02:00
|
|
|
int port, payload;
|
|
|
|
|
|
|
|
|
|
if (!spa_strstartswith(c, "m=audio "))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
2022-10-04 22:48:26 +02:00
|
|
|
c += strlen("m=audio ");
|
2022-10-04 18:12:48 +02:00
|
|
|
if (sscanf(c, "%i RTP/AVP %i", &port, &payload) != 2)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
if (port <= 0 || port > 0xFFFF)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
if (payload < 0 || payload > 127)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
info->port = (uint16_t) port;
|
|
|
|
|
info->payload = (uint8_t) payload;
|
|
|
|
|
|
2022-10-04 13:19:14 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 18:12:48 +02:00
|
|
|
static int parse_sdp_a(struct impl *impl, char *c, struct sdp_info *info)
|
|
|
|
|
{
|
|
|
|
|
int payload, len, rate, channels;
|
|
|
|
|
|
|
|
|
|
if (!spa_strstartswith(c, "a=rtpmap:"))
|
|
|
|
|
return 0;
|
|
|
|
|
|
2022-10-04 22:48:26 +02:00
|
|
|
c += strlen("a=rtpmap:");
|
2022-10-04 18:12:48 +02:00
|
|
|
|
|
|
|
|
if (sscanf(c, "%i %n", &payload, &len) != 1)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
if (payload < 0 || payload > 127)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
if (payload != info->payload)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
c += len;
|
|
|
|
|
if (spa_strstartswith(c, "L16/")) {
|
|
|
|
|
info->info.format = SPA_AUDIO_FORMAT_S16_BE;
|
2022-10-04 20:44:07 +02:00
|
|
|
info->stride = 2;
|
2022-10-04 18:12:48 +02:00
|
|
|
c += 4;
|
|
|
|
|
} else
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
if (sscanf(c, "%u/%u", &rate, &channels) == 2) {
|
|
|
|
|
info->info.rate = rate;
|
|
|
|
|
info->info.channels = channels;
|
|
|
|
|
if (channels == 2) {
|
|
|
|
|
info->info.position[0] = SPA_AUDIO_CHANNEL_FL;
|
|
|
|
|
info->info.position[1] = SPA_AUDIO_CHANNEL_FR;
|
|
|
|
|
}
|
|
|
|
|
} else if (sscanf(c, "%u", &rate) == 1) {
|
|
|
|
|
info->info.rate = rate;
|
|
|
|
|
info->info.channels = 1;
|
|
|
|
|
} else
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
2022-10-04 20:44:07 +02:00
|
|
|
info->stride *= info->info.channels;
|
|
|
|
|
|
2022-10-04 18:12:48 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int parse_sdp(struct impl *impl, char *sdp, struct sdp_info *info)
|
|
|
|
|
{
|
|
|
|
|
char *s = sdp;
|
|
|
|
|
int count = 0, res = 0;
|
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
|
|
while (*s) {
|
|
|
|
|
if ((l = strcspn(s, "\r\n")) < 2)
|
|
|
|
|
goto too_short;
|
|
|
|
|
|
|
|
|
|
s[l] = 0;
|
2022-10-04 23:27:44 +02:00
|
|
|
pw_log_debug("%d: %s", count, s);
|
2022-10-04 18:12:48 +02:00
|
|
|
|
|
|
|
|
if (count++ == 0 && strcmp(s, "v=0") != 0)
|
|
|
|
|
goto invalid_version;
|
|
|
|
|
|
|
|
|
|
if (spa_strstartswith(s, "o="))
|
2022-10-04 20:44:07 +02:00
|
|
|
snprintf(info->origin, sizeof(info->origin), "%s", &s[2]);
|
2022-10-04 18:12:48 +02:00
|
|
|
else if (spa_strstartswith(s, "s="))
|
2022-10-04 20:44:07 +02:00
|
|
|
snprintf(info->session, sizeof(info->session), "%s", &s[2]);
|
2022-10-04 18:12:48 +02:00
|
|
|
else if (spa_strstartswith(s, "c="))
|
|
|
|
|
res = parse_sdp_c(impl, s, info);
|
|
|
|
|
else if (spa_strstartswith(s, "m="))
|
|
|
|
|
res = parse_sdp_m(impl, s, info);
|
|
|
|
|
else if (spa_strstartswith(s, "a="))
|
|
|
|
|
res = parse_sdp_a(impl, s, info);
|
|
|
|
|
|
|
|
|
|
if (res < 0)
|
|
|
|
|
goto error;
|
|
|
|
|
s += l + 1;
|
|
|
|
|
while (isspace(*s))
|
|
|
|
|
s++;
|
|
|
|
|
}
|
2022-10-04 22:48:26 +02:00
|
|
|
if (((struct sockaddr*) &info->sa)->sa_family == AF_INET)
|
|
|
|
|
((struct sockaddr_in*) &info->sa)->sin_port = htons(info->port);
|
|
|
|
|
else
|
|
|
|
|
((struct sockaddr_in6*) &info->sa)->sin6_port = htons(info->port);
|
|
|
|
|
|
2022-10-04 18:12:48 +02:00
|
|
|
return 0;
|
|
|
|
|
too_short:
|
|
|
|
|
pw_log_warn("SDP: line starting with `%.6s...' too short", s);
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
invalid_version:
|
|
|
|
|
pw_log_warn("SDP: invalid first version line `%*s'", (int)l, s);
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
error:
|
2022-10-04 22:48:26 +02:00
|
|
|
pw_log_warn("SDP: error: %s", spa_strerror(res));
|
2022-10-04 18:12:48 +02:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int parse_sap(struct impl *impl, void *data, size_t len)
|
2022-10-04 13:19:14 +02:00
|
|
|
{
|
|
|
|
|
struct sap_header *header;
|
2022-10-04 18:12:48 +02:00
|
|
|
char *mime, *sdp;
|
|
|
|
|
struct sdp_info info;
|
2022-10-04 22:48:26 +02:00
|
|
|
struct session *sess;
|
2022-10-04 18:12:48 +02:00
|
|
|
int res;
|
2022-10-05 13:47:28 +02:00
|
|
|
size_t offs;
|
|
|
|
|
bool bye;
|
2022-10-04 13:19:14 +02:00
|
|
|
|
|
|
|
|
if (len < 8)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
header = (struct sap_header*) data;
|
|
|
|
|
if (header->v != 1)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
2022-10-05 13:47:28 +02:00
|
|
|
if (header->e)
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
if (header->c)
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
|
|
|
|
|
offs = header->a ? 12 : 8;
|
|
|
|
|
offs += header->auth_len * 4;
|
|
|
|
|
if (len <= offs)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
mime = SPA_PTROFF(data, offs, char);
|
2022-10-04 22:48:26 +02:00
|
|
|
if (spa_strstartswith(mime, "v=0")) {
|
2022-10-04 13:19:14 +02:00
|
|
|
sdp = mime;
|
2022-10-04 22:48:26 +02:00
|
|
|
mime = "application/sdp";
|
|
|
|
|
} else if (spa_streq(mime, "application/sdp"))
|
2022-10-04 13:19:14 +02:00
|
|
|
sdp = SPA_PTROFF(mime, strlen(mime)+1, char);
|
|
|
|
|
else
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
2022-10-04 23:27:44 +02:00
|
|
|
pw_log_debug("got sap: %s %s", mime, sdp);
|
2022-10-04 22:48:26 +02:00
|
|
|
|
2022-10-04 18:12:48 +02:00
|
|
|
spa_zero(info);
|
|
|
|
|
if ((res = parse_sdp(impl, sdp, &info)) < 0)
|
|
|
|
|
return res;
|
|
|
|
|
|
2022-10-05 13:47:28 +02:00
|
|
|
bye = header->t;
|
|
|
|
|
|
2022-10-04 22:48:26 +02:00
|
|
|
sess = session_find(impl, &info);
|
|
|
|
|
if (sess == NULL) {
|
2022-10-05 13:47:28 +02:00
|
|
|
if (!bye)
|
|
|
|
|
session_new(impl, &info);
|
2022-10-04 22:48:26 +02:00
|
|
|
} else {
|
2022-10-05 13:47:28 +02:00
|
|
|
if (bye)
|
|
|
|
|
session_free(sess);
|
|
|
|
|
else
|
|
|
|
|
session_touch(sess);
|
2022-10-04 22:48:26 +02:00
|
|
|
}
|
2022-10-04 18:12:48 +02:00
|
|
|
return res;
|
2022-10-04 13:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
on_sap_io(void *data, int fd, uint32_t mask)
|
2022-10-04 09:16:17 +02:00
|
|
|
{
|
2022-10-04 13:19:14 +02:00
|
|
|
struct impl *impl = data;
|
|
|
|
|
|
|
|
|
|
if (mask & SPA_IO_IN) {
|
|
|
|
|
uint8_t buffer[2048];
|
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
|
|
if ((len = recv(fd, buffer, sizeof(buffer), 0)) < 0) {
|
|
|
|
|
pw_log_warn("recv error: %m");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ((size_t)len >= sizeof(buffer))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
buffer[len] = 0;
|
|
|
|
|
parse_sap(impl, buffer, len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int start_sap_listener(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
struct sockaddr_in sa4;
|
|
|
|
|
struct sockaddr_in6 sa6;
|
|
|
|
|
struct sockaddr *sa;
|
|
|
|
|
socklen_t salen;
|
2022-10-04 22:48:26 +02:00
|
|
|
int fd, res;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
if (inet_pton(AF_INET, impl->sap_address, &sa4.sin_addr) > 0) {
|
2022-10-04 22:48:26 +02:00
|
|
|
sa4.sin_family = AF_INET;
|
2022-10-05 13:04:08 +02:00
|
|
|
sa4.sin_port = htons(impl->sap_port);
|
2022-10-04 13:19:14 +02:00
|
|
|
sa = (struct sockaddr*) &sa4;
|
|
|
|
|
salen = sizeof(sa4);
|
2022-10-05 13:04:08 +02:00
|
|
|
} else if (inet_pton(AF_INET6, impl->sap_address, &sa6.sin6_addr) > 0) {
|
2022-10-04 22:48:26 +02:00
|
|
|
sa6.sin6_family = AF_INET6;
|
2022-10-05 13:04:08 +02:00
|
|
|
sa6.sin6_port = htons(impl->sap_port);
|
2022-10-04 13:19:14 +02:00
|
|
|
sa = (struct sockaddr*) &sa6;
|
|
|
|
|
salen = sizeof(sa6);
|
|
|
|
|
} else
|
|
|
|
|
return -EINVAL;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
if ((fd = make_multicast_socket(sa, salen, impl->ifname)) < 0)
|
2022-10-04 22:48:26 +02:00
|
|
|
return fd;
|
2022-10-04 13:19:14 +02:00
|
|
|
|
|
|
|
|
pw_log_info("starting SAP listener");
|
|
|
|
|
impl->sap_source = pw_loop_add_io(impl->loop, fd,
|
|
|
|
|
SPA_IO_IN, true, on_sap_io, impl);
|
|
|
|
|
if (impl->sap_source == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2022-10-04 09:16:17 +02:00
|
|
|
error:
|
|
|
|
|
close(fd);
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-05 13:47:28 +02:00
|
|
|
static void on_timer_event(void *data, uint64_t expirations)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
struct timespec now;
|
|
|
|
|
struct session *sess, *tmp;
|
|
|
|
|
uint64_t timestamp, interval;
|
|
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
|
timestamp = SPA_TIMESPEC_TO_NSEC(&now);
|
|
|
|
|
interval = CLEANUP_INTERVAL_SEC * SPA_NSEC_PER_SEC;
|
|
|
|
|
|
|
|
|
|
spa_list_for_each_safe(sess, tmp, &impl->sessions, link) {
|
|
|
|
|
if (sess->timestamp + interval < timestamp)
|
|
|
|
|
session_free(sess);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
struct session *sess;
|
|
|
|
|
spa_list_consume(sess, &impl->sessions, link)
|
|
|
|
|
session_free(sess);
|
|
|
|
|
|
|
|
|
|
if (impl->core && impl->do_disconnect)
|
|
|
|
|
pw_core_disconnect(impl->core);
|
|
|
|
|
|
2022-10-05 13:47:28 +02:00
|
|
|
if (impl->sap_source)
|
|
|
|
|
pw_loop_destroy_source(impl->loop, impl->sap_source);
|
|
|
|
|
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);
|
|
|
|
|
free(impl->sap_address);
|
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,
|
|
|
|
|
};
|
|
|
|
|
|
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);
|
|
|
|
|
struct impl *impl;
|
2022-10-05 13:04:08 +02:00
|
|
|
struct pw_properties *props = NULL, *stream_props = NULL;
|
2022-10-04 09:16:17 +02:00
|
|
|
const char *str;
|
2022-10-05 13:47:28 +02:00
|
|
|
struct timespec value, interval;
|
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 = "";
|
|
|
|
|
|
|
|
|
|
props = pw_properties_new_string(args);
|
|
|
|
|
if (props == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
pw_log_error( "can't create properties: %m");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2022-10-04 20:44:07 +02:00
|
|
|
spa_list_init(&impl->sessions);
|
2022-10-04 09:16:17 +02:00
|
|
|
impl->props = props;
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
stream_props = pw_properties_new(NULL, NULL);
|
|
|
|
|
if (stream_props == NULL) {
|
2022-10-04 09:16:17 +02:00
|
|
|
res = -errno;
|
|
|
|
|
pw_log_error( "can't create properties: %m");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2022-10-05 13:04:08 +02:00
|
|
|
impl->stream_props = stream_props;
|
2022-10-04 09:16:17 +02:00
|
|
|
|
|
|
|
|
impl->module = module;
|
|
|
|
|
impl->module_context = context;
|
|
|
|
|
impl->loop = pw_context_get_main_loop(context);
|
|
|
|
|
|
2022-10-05 13:04:08 +02:00
|
|
|
if ((str = pw_properties_get(props, "stream.props")) != NULL)
|
|
|
|
|
pw_properties_update_string(stream_props, str, strlen(str));
|
|
|
|
|
|
|
|
|
|
if (pw_properties_get(stream_props, PW_KEY_NODE_NAME) == NULL)
|
|
|
|
|
pw_properties_set(stream_props, PW_KEY_NODE_NAME, "rtp-source");
|
|
|
|
|
if (pw_properties_get(stream_props, PW_KEY_NODE_DESCRIPTION) == NULL)
|
|
|
|
|
pw_properties_set(stream_props, PW_KEY_NODE_DESCRIPTION, "RTP Source");
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
str = pw_properties_get(props, "local.ifname");
|
|
|
|
|
impl->ifname = str ? strdup(str) : NULL;
|
|
|
|
|
|
|
|
|
|
str = pw_properties_get(props, "sap.address");
|
|
|
|
|
impl->sap_address = strdup(str ? str : SAP_DEFAULT_IP);
|
|
|
|
|
impl->sap_port = pw_properties_get_uint32(props,
|
|
|
|
|
"sap.port", SAP_DEFAULT_PORT);
|
|
|
|
|
impl->sess_latency_msec = pw_properties_get_uint32(props,
|
|
|
|
|
"sess.latency.msec", DEFAULT_SESS_LATENCY);
|
2022-10-04 09:16:17 +02:00
|
|
|
|
|
|
|
|
impl->core = pw_context_get_object(impl->module_context, PW_TYPE_INTERFACE_Core);
|
|
|
|
|
if (impl->core == NULL) {
|
|
|
|
|
str = pw_properties_get(props, PW_KEY_REMOTE_NAME);
|
|
|
|
|
impl->core = pw_context_connect(impl->module_context,
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
value.tv_sec = 0;
|
|
|
|
|
value.tv_nsec = 1;
|
|
|
|
|
interval.tv_sec = CLEANUP_INTERVAL_SEC;
|
|
|
|
|
interval.tv_nsec = 0;
|
|
|
|
|
pw_loop_update_timer(impl->loop, impl->timer, &value, &interval, false);
|
|
|
|
|
|
2022-10-04 09:16:17 +02:00
|
|
|
if ((res = start_sap_listener(impl)) < 0)
|
|
|
|
|
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;
|
|
|
|
|
}
|