module-esound-sink.la \
module-http-protocol-tcp.la \
module-detect.la \
- module-rtp-monitor.la
+ module-rtp-monitor.la \
+ module-rtp-recv.la
if HAVE_AF_UNIX
modlib_LTLIBRARIES += \
modules/module-solaris-symdef.h \
modules/module-waveout-symdef.h \
modules/module-detect-symdef.h \
- modules/rtp/module-rtp-monitor-symdef.h
+ modules/rtp/module-rtp-monitor-symdef.h \
+ modules/rtp/module-rtp-recv-symdef.h
EXTRA_DIST += $(SYMDEF_FILES)
BUILT_SOURCES += $(SYMDEF_FILES)
module_detect_la_LIBADD = $(AM_LIBADD) libpolypcore.la
module_detect_la_CFLAGS = $(AM_CFLAGS)
-# Hardware autodetection module
+# RTP modules
module_rtp_monitor_la_SOURCES = modules/rtp/module-rtp-monitor.c
module_rtp_monitor_la_LDFLAGS = -module -avoid-version
module_rtp_monitor_la_LIBADD = $(AM_LIBADD) libpolypcore.la
module_rtp_monitor_la_CFLAGS = $(AM_CFLAGS)
+module_rtp_recv_la_SOURCES = modules/rtp/module-rtp-recv.c
+module_rtp_recv_la_LDFLAGS = -module -avoid-version
+module_rtp_recv_la_LIBADD = $(AM_LIBADD) libpolypcore.la
+module_rtp_recv_la_CFLAGS = $(AM_CFLAGS)
+
###################################
# Some minor stuff #
###################################
+/* $Id */
/***
This file is part of polypaudio.
#include "sap.h"
PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Read data from source and send it to the network via RTP")
+PA_MODULE_DESCRIPTION("Read data from source and send it to the network via RTP/SAP/SDP")
PA_MODULE_VERSION(PACKAGE_VERSION)
PA_MODULE_USAGE(
- "source=<name for the source> "
+ "source=<name of the source> "
"format=<sample format> "
"channels=<number of channels> "
"rate=<sample rate> "
"destinaton=<destination IP address> "
"port=<port number> "
"mtu=<maximum transfer unit> "
+ "loop=<loopback to local host?>"
)
#define DEFAULT_PORT 5004
"rate",
"destination",
"port",
+ "loop",
NULL
};
return pa_bytes_to_usec(pa_memblockq_get_length(u->memblockq), &o->sample_spec);
}
-static void sap_event(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) {
+static void sap_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) {
struct userdata *u = userdata;
struct timeval next;
int r;
socklen_t k;
struct timeval tv;
+ char hn[128], *n;
+ int loop = 0;
assert(c);
assert(m);
goto fail;
}
+ if (pa_modargs_get_value_boolean(ma, "loop", &loop) < 0) {
+ pa_log(__FILE__": failed to parse \"loop\" parameter.");
+ goto fail;
+ }
+
ss = s->sample_spec;
pa_rtp_sample_spec_fixup(&ss);
cm = s->channel_map;
if (ss.channels != cm.channels)
pa_channel_map_init_auto(&cm, ss.channels);
- payload = pa_rtp_payload_type(&ss);
+ payload = pa_rtp_payload_from_sample_spec(&ss);
mtu = (DEFAULT_MTU/pa_frame_size(&ss))*pa_frame_size(&ss);
goto fail;
}
- if ((dest = pa_modargs_get_value(ma, "destination", DEFAULT_DESTINATION))) {
- if (inet_pton(AF_INET6, dest, &sa6.sin6_addr) > 0) {
- sa6.sin6_family = af = AF_INET6;
- sa6.sin6_port = htons(port);
- sap_sa6 = sa6;
- sap_sa6.sin6_port = htons(SAP_PORT);
- } else if (inet_pton(AF_INET, dest, &sa4.sin_addr) > 0) {
- sa4.sin_family = af = AF_INET;
- sa4.sin_port = htons(port);
- sap_sa4 = sa4;
- sap_sa4.sin_port = htons(SAP_PORT);
- } else {
- pa_log(__FILE__": invalid destination '%s'", dest);
- goto fail;
- }
+ dest = pa_modargs_get_value(ma, "destination", DEFAULT_DESTINATION);
+
+ if (inet_pton(AF_INET6, dest, &sa6.sin6_addr) > 0) {
+ sa6.sin6_family = af = AF_INET6;
+ sa6.sin6_port = htons(port);
+ sap_sa6 = sa6;
+ sap_sa6.sin6_port = htons(SAP_PORT);
+ } else if (inet_pton(AF_INET, dest, &sa4.sin_addr) > 0) {
+ sa4.sin_family = af = AF_INET;
+ sa4.sin_port = htons(port);
+ sap_sa4 = sa4;
+ sap_sa4.sin_port = htons(SAP_PORT);
+ } else {
+ pa_log(__FILE__": invalid destination '%s'", dest);
+ goto fail;
}
if ((fd = socket(af, SOCK_DGRAM, 0)) < 0) {
goto fail;
}
+ if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0 ||
+ setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0) {
+ pa_log(__FILE__": IP_MULTICAST_LOOP failed: %s", strerror(errno));
+ goto fail;
+ }
+
if (!(o = pa_source_output_new(s, __FILE__, "RTP Monitor Stream", &ss, &cm, PA_RESAMPLER_INVALID))) {
pa_log(__FILE__": failed to create source output.");
goto fail;
k = sizeof(sa_dst);
r = getsockname(fd, (struct sockaddr*) &sa_dst, &k);
assert(r >= 0);
+
+ n = pa_sprintf_malloc("Polypaudio RTP Stream on %s", pa_get_fqdn(hn, sizeof(hn)));
p = pa_sdp_build(af,
af == AF_INET ? (void*) &((struct sockaddr_in*) &sa_dst)->sin_addr : (void*) &((struct sockaddr_in6*) &sa_dst)->sin6_addr,
af == AF_INET ? (void*) &sa4.sin_addr : (void*) &sa6.sin6_addr,
- "Polypaudio RTP Stream", port, payload, &ss);
+ n, port, payload, &ss);
+
+ pa_xfree(n);
- pa_rtp_context_init_send(&u->rtp_context, fd, 0, payload, pa_frame_size(&ss));
+ pa_rtp_context_init_send(&u->rtp_context, fd, c->cookie, payload, pa_frame_size(&ss));
pa_sap_context_init_send(&u->sap_context, sap_fd, p);
- pa_log_info("RTP stream initialized with mtu %u on %s:%u, SSRC=0x%08x, payload=%u, initial sequence #%u", mtu, dest, port, u->rtp_context.ssrc, payload, u->rtp_context.sequence);
- pa_log_info("SDP-Data:\n%s\nEOF", p);
-
+ pa_log_info(__FILE__": RTP stream initialized with mtu %u on %s:%u, SSRC=0x%08x, payload=%u, initial sequence #%u", mtu, dest, port, u->rtp_context.ssrc, payload, u->rtp_context.sequence);
+ pa_log_info(__FILE__": SDP-Data:\n%s\n"__FILE__": EOF", p);
+
pa_sap_send(&u->sap_context, 0);
pa_gettimeofday(&tv);
pa_timeval_add(&tv, SAP_INTERVAL);
- u->sap_event = c->mainloop->time_new(c->mainloop, &tv, sap_event, u);
+ u->sap_event = c->mainloop->time_new(c->mainloop, &tv, sap_event_cb, u);
pa_modargs_free(ma);
--- /dev/null
+
+/***
+ This file is part of polypaudio.
+
+ polypaudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2 of the License,
+ or (at your option) any later version.
+
+ polypaudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with polypaudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <assert.h>
+#include <stdio.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <polypcore/module.h>
+#include <polypcore/llist.h>
+#include <polypcore/sink.h>
+#include <polypcore/sink-input.h>
+#include <polypcore/memblockq.h>
+#include <polypcore/log.h>
+#include <polypcore/util.h>
+#include <polypcore/xmalloc.h>
+#include <polypcore/modargs.h>
+#include <polypcore/namereg.h>
+
+#include "module-rtp-recv-symdef.h"
+
+#include "rtp.h"
+#include "sdp.h"
+#include "sap.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering")
+PA_MODULE_DESCRIPTION("Recieve data from a network via RTP/SAP/SDP")
+PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_USAGE(
+ "sink=<name of the sink> "
+ "sap_address=<multicast address to listen on> "
+)
+
+#define SAP_PORT 9875
+#define DEFAULT_SAP_ADDRESS "224.0.1.2"
+#define MEMBLOCKQ_MAXLENGTH (1024*170)
+#define MAX_SESSIONS 16
+#define DEATH_TIMEOUT 20000000
+
+static const char* const valid_modargs[] = {
+ "sink",
+ "sap_address",
+ NULL
+};
+
+struct session {
+ struct userdata *userdata;
+
+ pa_sink_input *sink_input;
+ pa_memblockq *memblockq;
+
+ pa_time_event *death_event;
+
+ int first_packet;
+ uint32_t ssrc;
+ uint32_t offset;
+
+ struct pa_sdp_info sdp_info;
+
+ pa_rtp_context rtp_context;
+ pa_io_event* rtp_event;
+};
+
+struct userdata {
+ pa_module *module;
+ pa_core *core;
+
+ pa_sap_context sap_context;
+ pa_io_event* sap_event;
+
+ pa_hashmap *by_origin;
+
+ char *sink_name;
+};
+
+static void session_free(struct session *s, int from_hash);
+
+static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
+ struct session *s;
+ assert(i);
+ s = i->userdata;
+
+ return pa_memblockq_peek(s->memblockq, chunk);
+}
+
+static void sink_input_drop(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
+ struct session *s;
+ assert(i);
+ s = i->userdata;
+
+ pa_memblockq_drop(s->memblockq, chunk, length);
+}
+
+static void sink_input_kill(pa_sink_input* i) {
+ struct session *s;
+ assert(i);
+ s = i->userdata;
+
+ session_free(s, 1);
+}
+
+static pa_usec_t sink_input_get_latency(pa_sink_input *i) {
+ struct session *s;
+ assert(i);
+ s = i->userdata;
+
+ return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &i->sample_spec);
+}
+
+static void rtp_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
+ struct session *s = userdata;
+ pa_memchunk chunk;
+ int64_t k, j, delta;
+ struct timeval tv;
+
+ assert(m);
+ assert(e);
+ assert(s);
+ assert(fd == s->rtp_context.fd);
+ assert(flags == PA_IO_EVENT_INPUT);
+
+ if (pa_rtp_recv(&s->rtp_context, &chunk, s->userdata->core->memblock_stat) < 0)
+ return;
+
+ if (s->sdp_info.payload != s->rtp_context.payload) {
+ pa_memblock_unref(chunk.memblock);
+ return;
+ }
+
+ if (!s->first_packet) {
+ s->first_packet = 1;
+
+ s->ssrc = s->rtp_context.ssrc;
+ s->offset = s->rtp_context.timestamp;
+ } else {
+ if (s->ssrc != s->rtp_context.ssrc) {
+ pa_memblock_unref(chunk.memblock);
+ return;
+ }
+ }
+
+ /* Check wheter there was a timestamp overflow */
+ k = (int64_t) s->rtp_context.timestamp - (int64_t) s->offset;
+ j = (int64_t) 0x100000000 - (int64_t) s->offset + (int64_t) s->rtp_context.timestamp;
+
+ if ((k < 0 ? -k : k) < (j < 0 ? -j : j))
+ delta = k;
+ else
+ delta = j;
+
+ pa_memblockq_seek(s->memblockq, delta * s->rtp_context.frame_size, PA_SEEK_RELATIVE);
+ pa_memblockq_push(s->memblockq, &chunk);
+
+ /* The next timestamp we expect */
+ s->offset = s->rtp_context.timestamp + (chunk.length / s->rtp_context.frame_size);
+
+ pa_memblock_unref(chunk.memblock);
+
+ /* Reset death timer */
+ pa_gettimeofday(&tv);
+ pa_timeval_add(&tv, DEATH_TIMEOUT);
+ m->time_restart(s->death_event, &tv);
+}
+
+static void death_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) {
+ struct session *s = userdata;
+
+ assert(m);
+ assert(t);
+ assert(tv);
+ assert(s);
+
+ session_free(s, 1);
+}
+
+static int mcast_socket(const struct sockaddr* sa, socklen_t salen) {
+ int af, fd = -1, r, one;
+
+ af = sa->sa_family;
+ if ((fd = socket(af, SOCK_DGRAM, 0)) < 0) {
+ pa_log(__FILE__": Failed to create socket: %s", strerror(errno));
+ goto fail;
+ }
+
+ one = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
+ pa_log(__FILE__": SO_REUSEADDR failed: %s", strerror(errno));
+ goto fail;
+ }
+
+ if (af == AF_INET) {
+ struct ip_mreq mr4;
+ memset(&mr4, 0, sizeof(mr4));
+ mr4.imr_multiaddr = ((const struct sockaddr_in*) sa)->sin_addr;
+ r = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mr4, sizeof(mr4));
+ } else {
+ struct ipv6_mreq mr6;
+ memset(&mr6, 0, sizeof(mr6));
+ mr6.ipv6mr_multiaddr = ((const struct sockaddr_in6*) sa)->sin6_addr;
+ r = setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mr6, sizeof(mr6));
+ }
+
+ if (r < 0) {
+ pa_log_info(__FILE__": Joining mcast group failed: %s", strerror(errno));
+ goto fail;
+ }
+
+ if (bind(fd, sa, salen) < 0) {
+ pa_log(__FILE__": bind() failed: %s", strerror(errno));
+ goto fail;
+ }
+
+ return fd;
+
+fail:
+ if (fd >= 0)
+ close(fd);
+
+ return -1;
+}
+
+static struct session *session_new(struct userdata *u, const pa_sdp_info *sdp_info) {
+ struct session *s = NULL;
+ struct timeval tv;
+ char *c;
+ pa_sink *sink;
+ int fd = -1;
+
+ if (!(sink = pa_namereg_get(u->core, u->sink_name, PA_NAMEREG_SINK, 1))) {
+ pa_log(__FILE__": sink does not exist.");
+ goto fail;
+ }
+
+ s = pa_xnew0(struct session, 1);
+ s->userdata = u;
+ s->first_packet = 0;
+ s->sdp_info = *sdp_info;
+
+ if ((fd = mcast_socket((const struct sockaddr*) &sdp_info->sa, sdp_info->salen)) < 0)
+ goto fail;
+
+ c = pa_sprintf_malloc("RTP Stream%s%s%s",
+ sdp_info->session_name ? " (" : "",
+ sdp_info->session_name ? sdp_info->session_name : "",
+ sdp_info->session_name ? ")" : "");
+
+ s->sink_input = pa_sink_input_new(sink, __FILE__, c, &sdp_info->sample_spec, NULL, 0, PA_RESAMPLER_INVALID);
+ pa_xfree(c);
+
+ if (!s->sink_input) {
+ pa_log(__FILE__": failed to create sink input.");
+ goto fail;
+ }
+
+ s->sink_input->userdata = s;
+ s->sink_input->owner = u->module;
+
+ s->sink_input->peek = sink_input_peek;
+ s->sink_input->drop = sink_input_drop;
+ s->sink_input->kill = sink_input_kill;
+ s->sink_input->get_latency = sink_input_get_latency;
+
+ s->memblockq = pa_memblockq_new(
+ 0,
+ MEMBLOCKQ_MAXLENGTH,
+ MEMBLOCKQ_MAXLENGTH,
+ pa_frame_size(&s->sink_input->sample_spec),
+ 1,
+ 0,
+ NULL,
+ u->core->memblock_stat);
+
+ s->rtp_event = u->core->mainloop->io_new(u->core->mainloop, fd, PA_IO_EVENT_INPUT, rtp_event_cb, s);
+
+ pa_gettimeofday(&tv);
+ pa_timeval_add(&tv, DEATH_TIMEOUT);
+ s->death_event = u->core->mainloop->time_new(u->core->mainloop, &tv, death_event_cb, s);
+
+ pa_hashmap_put(s->userdata->by_origin, s->sdp_info.origin, s);
+
+ pa_rtp_context_init_recv(&s->rtp_context, fd, pa_frame_size(&s->sdp_info.sample_spec));
+
+ pa_log_info(__FILE__": Found new session '%s'", s->sdp_info.session_name);
+
+ return s;
+
+fail:
+ if (s) {
+ if (fd >= 0)
+ close(fd);
+
+ pa_xfree(s);
+ }
+
+ return NULL;
+}
+
+static void session_free(struct session *s, int from_hash) {
+ assert(s);
+
+ pa_log_info(__FILE__": Freeing session '%s'", s->sdp_info.session_name);
+
+ s->userdata->core->mainloop->time_free(s->death_event);
+ s->userdata->core->mainloop->io_free(s->rtp_event);
+
+ if (from_hash)
+ pa_hashmap_remove(s->userdata->by_origin, s->sdp_info.origin);
+
+ pa_sink_input_disconnect(s->sink_input);
+ pa_sink_input_unref(s->sink_input);
+
+ pa_memblockq_free(s->memblockq);
+ pa_sdp_info_destroy(&s->sdp_info);
+ pa_rtp_context_destroy(&s->rtp_context);
+
+ pa_xfree(s);
+}
+
+static void sap_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
+ struct userdata *u = userdata;
+ int goodbye;
+ pa_sdp_info info;
+ struct session *s;
+
+ assert(m);
+ assert(e);
+ assert(u);
+ assert(fd == u->sap_context.fd);
+ assert(flags == PA_IO_EVENT_INPUT);
+
+ if (pa_sap_recv(&u->sap_context, &goodbye) < 0)
+ return;
+
+ if (!pa_sdp_parse(u->sap_context.sdp_data, &info, goodbye))
+ return;
+
+ if (goodbye) {
+
+ if ((s = pa_hashmap_get(u->by_origin, info.origin)))
+ session_free(s, 1);
+
+ pa_sdp_info_destroy(&info);
+ } else {
+
+ if (!(s = pa_hashmap_get(u->by_origin, info.origin))) {
+ if (!(s = session_new(u, &info)))
+ pa_sdp_info_destroy(&info);
+
+ } else {
+ struct timeval tv;
+
+ pa_gettimeofday(&tv);
+ pa_timeval_add(&tv, DEATH_TIMEOUT);
+ m->time_restart(s->death_event, &tv);
+
+ pa_sdp_info_destroy(&info);
+ }
+ }
+}
+
+int pa__init(pa_core *c, pa_module*m) {
+ struct userdata *u;
+ pa_modargs *ma = NULL;
+ struct sockaddr_in sa4;
+ struct sockaddr_in6 sa6;
+ struct sockaddr *sa;
+ socklen_t salen;
+ const char *sap_address;
+ int fd = -1;
+
+ assert(c);
+ assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log(__FILE__": failed to parse module arguments");
+ goto fail;
+ }
+
+ sap_address = pa_modargs_get_value(ma, "sap_address", DEFAULT_SAP_ADDRESS);
+
+ if (inet_pton(AF_INET6, sap_address, &sa6.sin6_addr) > 0) {
+ sa6.sin6_family = AF_INET6;
+ sa6.sin6_port = htons(SAP_PORT);
+ sa = (struct sockaddr*) &sa6;
+ salen = sizeof(sa6);
+ } else if (inet_pton(AF_INET, sap_address, &sa4.sin_addr) > 0) {
+ sa4.sin_family = AF_INET;
+ sa4.sin_port = htons(SAP_PORT);
+ sa = (struct sockaddr*) &sa4;
+ salen = sizeof(sa4);
+ } else {
+ pa_log(__FILE__": invalid SAP address '%s'", sap_address);
+ goto fail;
+ }
+
+ if ((fd = mcast_socket(sa, salen)) < 0)
+ goto fail;
+
+ u = pa_xnew(struct userdata, 1);
+ m->userdata = u;
+ u->module = m;
+ u->core = c;
+ u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
+
+ u->sap_event = c->mainloop->io_new(c->mainloop, fd, PA_IO_EVENT_INPUT, sap_event_cb, u);
+
+ u->by_origin = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+
+ pa_sap_context_init_recv(&u->sap_context, fd);
+
+ pa_modargs_free(ma);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ if (fd >= 0)
+ close(fd);
+
+ return -1;
+}
+
+static void free_func(void *p, void *userdata) {
+ session_free(p, 0);
+}
+
+void pa__done(pa_core *c, pa_module*m) {
+ struct userdata *u;
+ assert(c);
+ assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ c->mainloop->io_free(u->sap_event);
+ pa_sap_context_destroy(&u->sap_context);
+
+ pa_hashmap_free(u->by_origin, free_func, NULL);
+
+ pa_xfree(u->sink_name);
+ pa_xfree(u);
+}
#include <errno.h>
#include <arpa/inet.h>
#include <unistd.h>
+#include <sys/ioctl.h>
#include <polypcore/log.h>
return 0;
}
-pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd) {
+pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd, size_t frame_size) {
assert(c);
c->fd = fd;
+ c->frame_size = frame_size;
return c;
}
-int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk) {
+int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_memblock_stat *st) {
+ int size;
+ struct msghdr m;
+ struct iovec iov;
+ uint32_t header;
+ int cc;
+ ssize_t r;
+
assert(c);
assert(chunk);
+ chunk->memblock = NULL;
+
+ if (ioctl(c->fd, FIONREAD, &size) < 0) {
+ pa_log(__FILE__": FIONREAD failed: %s", strerror(errno));
+ goto fail;
+ }
+
+ if (!size)
+ return 0;
+
+ chunk->memblock = pa_memblock_new(size, st);
+
+ iov.iov_base = chunk->memblock->data;
+ iov.iov_len = size;
+
+ m.msg_name = NULL;
+ m.msg_namelen = 0;
+ m.msg_iov = &iov;
+ m.msg_iovlen = 1;
+ m.msg_control = NULL;
+ m.msg_controllen = 0;
+ m.msg_flags = 0;
+
+ if ((r = recvmsg(c->fd, &m, 0)) != size) {
+ pa_log(__FILE__": recvmsg() failed: %s", r < 0 ? strerror(errno) : "size mismatch");
+ goto fail;
+ }
+
+ if (size < 12) {
+ pa_log(__FILE__": RTP packet too short.");
+ goto fail;
+ }
+
+ memcpy(&header, chunk->memblock->data, sizeof(uint32_t));
+ memcpy(&c->timestamp, (uint8_t*) chunk->memblock->data + 4, sizeof(uint32_t));
+ memcpy(&c->ssrc, (uint8_t*) chunk->memblock->data + 8, sizeof(uint32_t));
+
+ header = ntohl(header);
+ c->timestamp = ntohl(c->timestamp);
+ c->ssrc = ntohl(c->ssrc);
+
+ if ((header >> 30) != 2) {
+ pa_log(__FILE__": Unsupported RTP version.");
+ goto fail;
+ }
+
+ if ((header >> 29) & 1) {
+ pa_log(__FILE__": RTP padding not supported.");
+ goto fail;
+ }
+
+ if ((header >> 28) & 1) {
+ pa_log(__FILE__": RTP header extensions not supported.");
+ goto fail;
+ }
+
+ cc = (header >> 24) & 0xF;
+ c->payload = (header >> 16) & 127;
+ c->sequence = header & 0xFFFF;
+
+ if (12 + cc*4 > size) {
+ pa_log(__FILE__": RTP packet too short. (CSRC)");
+ goto fail;
+ }
+
+ chunk->index = 12 + cc*4;
+ chunk->length = size - chunk->index;
+
+ if (chunk->length % c->frame_size != 0) {
+ pa_log(__FILE__": Vad RTP packet size.");
+ goto fail;
+ }
+
return 0;
+
+fail:
+ if (chunk->memblock)
+ pa_memblock_unref(chunk->memblock);
+
+ return -1;
}
-uint8_t pa_rtp_payload_type(const pa_sample_spec *ss) {
+uint8_t pa_rtp_payload_from_sample_spec(const pa_sample_spec *ss) {
assert(ss);
if (ss->format == PA_SAMPLE_ULAW && ss->rate == 8000 && ss->channels == 1)
return 0;
if (ss->format == PA_SAMPLE_ALAW && ss->rate == 8000 && ss->channels == 1)
- return 0;
+ return 8;
if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 2)
return 10;
if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 1)
return 127;
}
+pa_sample_spec *pa_rtp_sample_spec_from_payload(uint8_t payload, pa_sample_spec *ss) {
+ assert(ss);
+
+ switch (payload) {
+ case 0:
+ ss->channels = 1;
+ ss->format = PA_SAMPLE_ULAW;
+ ss->rate = 8000;
+ break;
+
+ case 8:
+ ss->channels = 1;
+ ss->format = PA_SAMPLE_ALAW;
+ ss->rate = 8000;
+ break;
+
+ case 10:
+ ss->channels = 2;
+ ss->format = PA_SAMPLE_S16BE;
+ ss->rate = 44100;
+ break;
+
+ case 11:
+ ss->channels = 1;
+ ss->format = PA_SAMPLE_S16BE;
+ ss->rate = 44100;
+ break;
+
+ default:
+ return NULL;
+ }
+
+ return ss;
+}
+
pa_sample_spec *pa_rtp_sample_spec_fixup(pa_sample_spec * ss) {
assert(ss);
close(c->fd);
}
+
+const char* pa_rtp_format_to_string(pa_sample_format_t f) {
+ switch (f) {
+ case PA_SAMPLE_S16BE:
+ return "L16";
+ case PA_SAMPLE_U8:
+ return "L8";
+ case PA_SAMPLE_ALAW:
+ return "PCMA";
+ case PA_SAMPLE_ULAW:
+ return "PCMU";
+ default:
+ return NULL;
+ }
+}
+
+pa_sample_format_t pa_rtp_string_to_format(const char *s) {
+ assert(s);
+
+ if (!(strcmp(s, "L16")))
+ return PA_SAMPLE_S16BE;
+ else if (!strcmp(s, "L8"))
+ return PA_SAMPLE_U8;
+ else if (!strcmp(s, "PCMA"))
+ return PA_SAMPLE_ALAW;
+ else if (!strcmp(s, "PCMU"))
+ return PA_SAMPLE_ULAW;
+ else
+ return PA_SAMPLE_INVALID;
+}
+
pa_rtp_context* pa_rtp_context_init_send(pa_rtp_context *c, int fd, uint32_t ssrc, uint8_t payload, size_t frame_size);
int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q);
-pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd);
-int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk);
+pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd, size_t frame_size);
+int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_memblock_stat *st);
+
+void pa_rtp_context_destroy(pa_rtp_context *c);
-uint8_t pa_rtp_payload_type(const pa_sample_spec *ss);
pa_sample_spec* pa_rtp_sample_spec_fixup(pa_sample_spec *ss);
int pa_rtp_sample_spec_valid(const pa_sample_spec *ss);
-void pa_rtp_context_destroy(pa_rtp_context *c);
+uint8_t pa_rtp_payload_from_sample_spec(const pa_sample_spec *ss);
+pa_sample_spec *pa_rtp_sample_spec_from_payload(uint8_t payload, pa_sample_spec *ss);
+
+const char* pa_rtp_format_to_string(pa_sample_format_t f);
+pa_sample_format_t pa_rtp_string_to_format(const char *s);
#endif
#include <errno.h>
#include <string.h>
#include <unistd.h>
+#include <sys/ioctl.h>
#include <polypcore/util.h>
#include <polypcore/log.h>
#include <polypcore/xmalloc.h>
#include "sap.h"
+#include "sdp.h"
+
+#define MIME_TYPE "application/sdp"
pa_sap_context* pa_sap_context_init_send(pa_sap_context *c, int fd, char *sdp_data) {
assert(c);
int pa_sap_send(pa_sap_context *c, int goodbye) {
uint32_t header;
- const char mime[] = "application/sdp";
struct sockaddr_storage sa_buf;
struct sockaddr *sa = (struct sockaddr*) &sa_buf;
socklen_t salen = sizeof(sa_buf);
iov[1].iov_base = sa->sa_family == AF_INET ? (void*) &((struct sockaddr_in*) sa)->sin_addr : (void*) &((struct sockaddr_in6*) sa)->sin6_addr;
iov[1].iov_len = sa->sa_family == AF_INET ? 4 : 16;
- iov[2].iov_base = (char*) mime;
- iov[2].iov_len = sizeof(mime);
+ iov[2].iov_base = (char*) MIME_TYPE;
+ iov[2].iov_len = sizeof(MIME_TYPE);
iov[3].iov_base = c->sdp_data;
iov[3].iov_len = strlen(c->sdp_data);
return k;
}
+
+pa_sap_context* pa_sap_context_init_recv(pa_sap_context *c, int fd) {
+ assert(c);
+ assert(fd >= 0);
+
+ c->fd = fd;
+ c->sdp_data = NULL;
+ return c;
+}
+
+int pa_sap_recv(pa_sap_context *c, int *goodbye) {
+ struct msghdr m;
+ struct iovec iov;
+ int size, k;
+ char *buf = NULL, *e;
+ uint32_t header;
+ int six, ac;
+ ssize_t r;
+
+ assert(c);
+ assert(goodbye);
+
+ if (ioctl(c->fd, FIONREAD, &size) < 0) {
+ pa_log(__FILE__": FIONREAD failed: %s", strerror(errno));
+ goto fail;
+ }
+
+ if (!size)
+ return 0;
+
+ buf = pa_xnew(char, size+1);
+ buf[size] = 0;
+
+ iov.iov_base = buf;
+ iov.iov_len = size;
+
+ m.msg_name = NULL;
+ m.msg_namelen = 0;
+ m.msg_iov = &iov;
+ m.msg_iovlen = 1;
+ m.msg_control = NULL;
+ m.msg_controllen = 0;
+ m.msg_flags = 0;
+
+ if ((r = recvmsg(c->fd, &m, 0)) != size) {
+ pa_log(__FILE__": recvmsg() failed: %s", r < 0 ? strerror(errno) : "size mismatch");
+ goto fail;
+ }
+
+ if (size < 4) {
+ pa_log(__FILE__": SAP packet too short.");
+ goto fail;
+ }
+
+ memcpy(&header, buf, sizeof(uint32_t));
+ header = ntohl(header);
+
+ if (header >> 29 != 1) {
+ pa_log(__FILE__": Unsupported SAP version.");
+ goto fail;
+ }
+
+ if ((header >> 25) & 1) {
+ pa_log(__FILE__": Encrypted SAP not supported.");
+ goto fail;
+ }
+
+ if ((header >> 24) & 1) {
+ pa_log(__FILE__": Compressed SAP not supported.");
+ goto fail;
+ }
+
+ six = (header >> 28) & 1;
+ ac = (header >> 16) & 0xFF;
+
+ k = 4 + (six ? 16 : 4) + ac*4;
+ if (size < k) {
+ pa_log(__FILE__": SAP packet too short (AD).");
+ goto fail;
+ }
+
+ e = buf + k;
+ size -= k;
+
+ if ((unsigned) size >= sizeof(MIME_TYPE) && !strcmp(e, MIME_TYPE)) {
+ e += sizeof(MIME_TYPE);
+ size -= sizeof(MIME_TYPE);
+ } else if ((unsigned) size < sizeof(PA_SDP_HEADER)-1 || strncmp(e, PA_SDP_HEADER, sizeof(PA_SDP_HEADER)-1)) {
+ pa_log(__FILE__": Invalid SDP header.");
+ goto fail;
+ }
+
+ if (c->sdp_data)
+ pa_xfree(c->sdp_data);
+
+ c->sdp_data = pa_xstrndup(e, size);
+ pa_xfree(buf);
+
+ *goodbye = !!((header >> 26) & 1);
+
+ return 0;
+
+fail:
+ pa_xfree(buf);
+
+ return -1;
+}
int pa_sap_send(pa_sap_context *c, int goodbye);
+pa_sap_context* pa_sap_context_init_recv(pa_sap_context *c, int fd);
+int pa_sap_recv(pa_sap_context *c, int *goodbye);
+
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
+#include <string.h>
#include <polypcore/util.h>
+#include <polypcore/log.h>
+#include <polypcore/xmalloc.h>
#include "sdp.h"
+#include "rtp.h"
-static const char* map_format(pa_sample_format_t f) {
- switch (f) {
- case PA_SAMPLE_S16BE: return "L16";
- case PA_SAMPLE_U8: return "L8";
- case PA_SAMPLE_ALAW: return "PCMA";
- case PA_SAMPLE_ULAW: return "PCMU";
- default:
- return NULL;
- }
-}
char *pa_sdp_build(int af, const void *src, const void *dst, const char *name, uint16_t port, uint8_t payload, const pa_sample_spec *ss) {
uint32_t ntp;
assert(dst);
assert(af == AF_INET || af == AF_INET6);
- f = map_format(ss->format);
+ f = pa_rtp_format_to_string(ss->format);
assert(f);
if (!(u = getenv("USER")))
assert(a);
return pa_sprintf_malloc(
- "v=0\n"
+ PA_SDP_HEADER
"o=%s %lu 0 IN %s %s\n"
"s=%s\n"
"c=IN %s %s\n"
port, payload,
payload, f, ss->rate, ss->channels);
}
+
+static pa_sample_spec *parse_sdp_sample_spec(pa_sample_spec *ss, char *c) {
+ unsigned rate, channels;
+ assert(ss);
+ assert(c);
+
+ if (pa_startswith(c, "L16/")) {
+ ss->format = PA_SAMPLE_S16BE;
+ c += 4;
+ } else if (pa_startswith(c, "L8/")) {
+ ss->format = PA_SAMPLE_U8;
+ c += 3;
+ } else if (pa_startswith(c, "PCMA/")) {
+ ss->format = PA_SAMPLE_ALAW;
+ c += 5;
+ } else if (pa_startswith(c, "PCMU/")) {
+ ss->format = PA_SAMPLE_ULAW;
+ c += 5;
+ } else
+ return NULL;
+
+ if (sscanf(c, "%u/%u", &rate, &channels) == 2) {
+ ss->rate = rate;
+ ss->channels = channels;
+ } else if (sscanf(c, "%u", &rate) == 2) {
+ ss->rate = rate;
+ ss->channels = 1;
+ } else
+ return NULL;
+
+ if (!pa_sample_spec_valid(ss))
+ return NULL;
+
+ return ss;
+}
+
+pa_sdp_info *pa_sdp_parse(const char *t, pa_sdp_info *i, int is_goodbye) {
+ uint16_t port = 0;
+ int ss_valid = 0;
+
+ assert(t);
+ assert(i);
+
+ i->origin = i->session_name = NULL;
+ i->salen = 0;
+ i->payload = 255;
+
+ if (!pa_startswith(t, PA_SDP_HEADER)) {
+ pa_log(__FILE__": Failed to parse SDP data: invalid header.");
+ goto fail;
+ }
+
+ t += sizeof(PA_SDP_HEADER)-1;
+
+ while (*t) {
+ size_t l;
+
+ l = strcspn(t, "\n");
+
+ if (l <= 2) {
+ pa_log(__FILE__": Failed to parse SDP data: line too short: >%s<.", t);
+ goto fail;
+ }
+
+ if (pa_startswith(t, "o="))
+ i->origin = pa_xstrndup(t+2, l-2);
+ else if (pa_startswith(t, "s="))
+ i->session_name = pa_xstrndup(t+2, l-2);
+ else if (pa_startswith(t, "c=IN IP4 ")) {
+ char a[64];
+ size_t k;
+
+ k = l-8 > sizeof(a) ? sizeof(a) : l-8;
+
+ pa_strlcpy(a, t+9, k);
+ a[strcspn(a, "/")] = 0;
+
+ if (inet_pton(AF_INET, a, &((struct sockaddr_in*) &i->sa)->sin_addr) <= 0) {
+ pa_log(__FILE__": Failed to parse SDP data: bad address: >%s<.", a);
+ goto fail;
+ }
+
+ ((struct sockaddr_in*) &i->sa)->sin_family = AF_INET;
+ ((struct sockaddr_in*) &i->sa)->sin_port = 0;
+ i->salen = sizeof(struct sockaddr_in);
+ } else if (pa_startswith(t, "c=IN IP6 ")) {
+ char a[64];
+ size_t k;
+
+ k = l-8 > sizeof(a) ? sizeof(a) : l-8;
+
+ pa_strlcpy(a, t+9, k);
+ a[strcspn(a, "/")] = 0;
+
+ if (inet_pton(AF_INET6, a, &((struct sockaddr_in6*) &i->sa)->sin6_addr) <= 0) {
+ pa_log(__FILE__": Failed to parse SDP data: bad address: >%s<.", a);
+ goto fail;
+ }
+
+ ((struct sockaddr_in6*) &i->sa)->sin6_family = AF_INET6;
+ ((struct sockaddr_in6*) &i->sa)->sin6_port = 0;
+ i->salen = sizeof(struct sockaddr_in6);
+ } else if (pa_startswith(t, "m=audio ")) {
+
+ if (i->payload > 127) {
+ int _port, _payload;
+
+ if (sscanf(t+8, "%i RTP/AVP %i", &_port, &_payload) == 2) {
+
+ if (_port <= 0 || _port > 0xFFFF) {
+ pa_log(__FILE__": Failed to parse SDP data: invalid port %i.", _port);
+ goto fail;
+ }
+
+ if (_payload < 0 || _payload > 127) {
+ pa_log(__FILE__": Failed to parse SDP data: invalid payload %i.", _payload);
+ goto fail;
+ }
+
+ port = (uint16_t) _port;
+ i->payload = (uint8_t) _payload;
+
+ if (pa_rtp_sample_spec_from_payload(i->payload, &i->sample_spec))
+ ss_valid = 1;
+ }
+ }
+ } else if (pa_startswith(t, "a=rtpmap:")) {
+
+ if (i->payload <= 127) {
+ char c[64];
+ int _payload;
+
+ if (sscanf(t+9, "%i %64c", &_payload, c) == 2) {
+
+ if (_payload < 0 || _payload > 127) {
+ pa_log(__FILE__": Failed to parse SDP data: invalid payload %i.", _payload);
+ goto fail;
+ }
+ if (_payload == i->payload) {
+
+ c[strcspn(c, "\n")] = 0;
+
+ if (parse_sdp_sample_spec(&i->sample_spec, c))
+ ss_valid = 1;
+ }
+ }
+ }
+ }
+
+ t += l;
+
+ if (*t == '\n')
+ t++;
+ }
+
+ if (!i->origin || (!is_goodbye && (!i->salen || i->payload > 127 || !ss_valid || port == 0))) {
+ pa_log(__FILE__": Failed to parse SDP data: missing data.");
+ goto fail;
+ }
+
+ if (((struct sockaddr*) &i->sa)->sa_family == AF_INET)
+ ((struct sockaddr_in*) &i->sa)->sin_port = htons(port);
+ else
+ ((struct sockaddr_in6*) &i->sa)->sin6_port = htons(port);
+
+ return i;
+
+fail:
+ pa_xfree(i->origin);
+ pa_xfree(i->session_name);
+
+ return NULL;
+}
+
+void pa_sdp_info_destroy(pa_sdp_info *i) {
+ assert(i);
+
+ pa_xfree(i->origin);
+ pa_xfree(i->session_name);
+}
#include <polyp/sample.h>
+#define PA_SDP_HEADER "v=0\n"
+
+typedef struct pa_sdp_info {
+ char *origin;
+ char *session_name;
+
+ struct sockaddr_storage sa;
+ socklen_t salen;
+
+ pa_sample_spec sample_spec;
+ uint8_t payload;
+} pa_sdp_info;
+
char *pa_sdp_build(int af, const void *src, const void *dst, const char *name, uint16_t port, uint8_t payload, const pa_sample_spec *ss);
+pa_sdp_info *pa_sdp_parse(const char *t, pa_sdp_info *info, int is_goodbye);
+
+void pa_sdp_info_destroy(pa_sdp_info *i);
+
#endif