mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-11-04 13:29:59 -05:00
add an RTP sender module
git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@712 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
parent
86ad60185a
commit
9522b44842
12 changed files with 12527 additions and 0 deletions
13
src/modules/rtp/Makefile
Normal file
13
src/modules/rtp/Makefile
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
# This is a dirty trick just to ease compilation with emacs
|
||||||
|
#
|
||||||
|
# This file is not intended to be distributed or anything
|
||||||
|
#
|
||||||
|
# So: don't touch it, even better ignore it!
|
||||||
|
|
||||||
|
all:
|
||||||
|
$(MAKE) -C ../..
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(MAKE) -C ../.. clean
|
||||||
|
|
||||||
|
.PHONY: all clean
|
||||||
340
src/modules/rtp/module-rtp-monitor.c
Normal file
340
src/modules/rtp/module-rtp-monitor.c
Normal file
|
|
@ -0,0 +1,340 @@
|
||||||
|
|
||||||
|
/***
|
||||||
|
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/source.h>
|
||||||
|
#include <polypcore/source-output.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-monitor-symdef.h"
|
||||||
|
|
||||||
|
#include "rtp.h"
|
||||||
|
#include "sdp.h"
|
||||||
|
#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_VERSION(PACKAGE_VERSION)
|
||||||
|
PA_MODULE_USAGE(
|
||||||
|
"source=<name for the source> "
|
||||||
|
"format=<sample format> "
|
||||||
|
"channels=<number of channels> "
|
||||||
|
"rate=<sample rate> "
|
||||||
|
"destinaton=<destination IP address> "
|
||||||
|
"port=<port number> "
|
||||||
|
"mtu=<maximum transfer unit> "
|
||||||
|
)
|
||||||
|
|
||||||
|
#define DEFAULT_PORT 5666
|
||||||
|
#define SAP_PORT 9875
|
||||||
|
#define DEFAULT_DESTINATION "224.0.0.252"
|
||||||
|
#define MEMBLOCKQ_MAXLENGTH (1024*170)
|
||||||
|
#define DEFAULT_MTU 1024
|
||||||
|
#define SAP_INTERVAL 5000000
|
||||||
|
|
||||||
|
static const char* const valid_modargs[] = {
|
||||||
|
"source",
|
||||||
|
"format",
|
||||||
|
"channels",
|
||||||
|
"rate",
|
||||||
|
"destination",
|
||||||
|
"port",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
struct userdata {
|
||||||
|
pa_module *module;
|
||||||
|
pa_core *core;
|
||||||
|
|
||||||
|
pa_source_output *source_output;
|
||||||
|
pa_memblockq *memblockq;
|
||||||
|
|
||||||
|
pa_rtp_context rtp_context;
|
||||||
|
pa_sap_context sap_context;
|
||||||
|
size_t mtu;
|
||||||
|
|
||||||
|
pa_time_event *sap_event;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
|
||||||
|
struct userdata *u;
|
||||||
|
assert(o);
|
||||||
|
u = o->userdata;
|
||||||
|
|
||||||
|
if (pa_memblockq_push(u->memblockq, chunk) < 0) {
|
||||||
|
pa_log(__FILE__": Failed to push chunk into memblockq.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pa_rtp_send(&u->rtp_context, u->mtu, u->memblockq);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void source_output_kill(pa_source_output* o) {
|
||||||
|
struct userdata *u;
|
||||||
|
assert(o);
|
||||||
|
u = o->userdata;
|
||||||
|
|
||||||
|
pa_module_unload_request(u->module);
|
||||||
|
|
||||||
|
pa_source_output_disconnect(u->source_output);
|
||||||
|
pa_source_output_unref(u->source_output);
|
||||||
|
u->source_output = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static pa_usec_t source_output_get_latency (pa_source_output *o) {
|
||||||
|
struct userdata *u;
|
||||||
|
assert(o);
|
||||||
|
u = o->userdata;
|
||||||
|
|
||||||
|
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) {
|
||||||
|
struct userdata *u = userdata;
|
||||||
|
struct timeval next;
|
||||||
|
|
||||||
|
assert(m);
|
||||||
|
assert(t);
|
||||||
|
assert(tv);
|
||||||
|
assert(u);
|
||||||
|
|
||||||
|
pa_sap_send(&u->sap_context, 0);
|
||||||
|
|
||||||
|
pa_log("SAP update");
|
||||||
|
pa_gettimeofday(&next);
|
||||||
|
pa_timeval_add(&next, SAP_INTERVAL);
|
||||||
|
m->time_restart(t, &next);
|
||||||
|
}
|
||||||
|
|
||||||
|
int pa__init(pa_core *c, pa_module*m) {
|
||||||
|
struct userdata *u;
|
||||||
|
pa_modargs *ma = NULL;
|
||||||
|
const char *dest;
|
||||||
|
uint32_t port = DEFAULT_PORT, mtu;
|
||||||
|
int af, fd = -1, sap_fd = -1;
|
||||||
|
pa_source *s;
|
||||||
|
pa_sample_spec ss;
|
||||||
|
pa_channel_map cm;
|
||||||
|
struct sockaddr_in sa4, sap_sa4;
|
||||||
|
struct sockaddr_in6 sa6, sap_sa6;
|
||||||
|
struct sockaddr_storage sa_dst;
|
||||||
|
pa_source_output *o = NULL;
|
||||||
|
uint8_t payload;
|
||||||
|
char *p;
|
||||||
|
int r;
|
||||||
|
socklen_t k;
|
||||||
|
struct timeval tv;
|
||||||
|
|
||||||
|
assert(c);
|
||||||
|
assert(m);
|
||||||
|
|
||||||
|
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
|
||||||
|
pa_log(__FILE__": failed to parse module arguments");
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(s = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source", NULL), PA_NAMEREG_SOURCE, 1))) {
|
||||||
|
pa_log(__FILE__": source does not exist.");
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
ss = s->sample_spec;
|
||||||
|
pa_rtp_sample_spec_fixup(&ss);
|
||||||
|
cm = s->channel_map;
|
||||||
|
if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
|
||||||
|
pa_log(__FILE__": failed to parse sample specification");
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pa_rtp_sample_spec_valid(&ss)) {
|
||||||
|
pa_log(__FILE__": specified sample type not compatible with RTP");
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ss.channels != cm.channels)
|
||||||
|
pa_channel_map_init_auto(&cm, ss.channels);
|
||||||
|
|
||||||
|
payload = pa_rtp_payload_type(&ss);
|
||||||
|
|
||||||
|
mtu = (DEFAULT_MTU/pa_frame_size(&ss))*pa_frame_size(&ss);
|
||||||
|
|
||||||
|
if (pa_modargs_get_value_u32(ma, "mtu", &mtu) < 0 || mtu < 1 || mtu % pa_frame_size(&ss) != 0) {
|
||||||
|
pa_log(__FILE__": invalid mtu.");
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
|
||||||
|
pa_log(__FILE__": port= expects a numerical argument between 1 and 65535.");
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((fd = socket(af, SOCK_DGRAM, 0)) < 0) {
|
||||||
|
pa_log(__FILE__": socket() failed: %s", strerror(errno));
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connect(fd, af == AF_INET ? (struct sockaddr*) &sa4 : (struct sockaddr*) &sa6, af == AF_INET ? sizeof(sa4) : sizeof(sa6)) < 0) {
|
||||||
|
pa_log(__FILE__": connect() failed: %s", strerror(errno));
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((sap_fd = socket(af, SOCK_DGRAM, 0)) < 0) {
|
||||||
|
pa_log(__FILE__": socket() failed: %s", strerror(errno));
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connect(sap_fd, af == AF_INET ? (struct sockaddr*) &sap_sa4 : (struct sockaddr*) &sap_sa6, af == AF_INET ? sizeof(sap_sa4) : sizeof(sap_sa6)) < 0) {
|
||||||
|
pa_log(__FILE__": connect() 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
o->push = source_output_push;
|
||||||
|
o->kill = source_output_kill;
|
||||||
|
o->get_latency = source_output_get_latency;
|
||||||
|
o->owner = m;
|
||||||
|
|
||||||
|
u = pa_xnew(struct userdata, 1);
|
||||||
|
m->userdata = u;
|
||||||
|
o->userdata = u;
|
||||||
|
|
||||||
|
u->module = m;
|
||||||
|
u->core = c;
|
||||||
|
u->source_output = o;
|
||||||
|
|
||||||
|
u->memblockq = pa_memblockq_new(
|
||||||
|
0,
|
||||||
|
MEMBLOCKQ_MAXLENGTH,
|
||||||
|
MEMBLOCKQ_MAXLENGTH,
|
||||||
|
pa_frame_size(&ss),
|
||||||
|
1,
|
||||||
|
0,
|
||||||
|
NULL,
|
||||||
|
c->memblock_stat);
|
||||||
|
|
||||||
|
u->mtu = mtu;
|
||||||
|
|
||||||
|
k = sizeof(sa_dst);
|
||||||
|
r = getsockname(fd, (struct sockaddr*) &sa_dst, &k);
|
||||||
|
assert(r >= 0);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
pa_rtp_context_init_send(&u->rtp_context, fd, 0, payload);
|
||||||
|
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_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);
|
||||||
|
|
||||||
|
pa_modargs_free(ma);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
fail:
|
||||||
|
if (ma)
|
||||||
|
pa_modargs_free(ma);
|
||||||
|
|
||||||
|
if (fd >= 0)
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
if (sap_fd >= 0)
|
||||||
|
close(sap_fd);
|
||||||
|
|
||||||
|
if (o) {
|
||||||
|
pa_source_output_disconnect(o);
|
||||||
|
pa_source_output_unref(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pa__done(pa_core *c, pa_module*m) {
|
||||||
|
struct userdata *u;
|
||||||
|
assert(c);
|
||||||
|
assert(m);
|
||||||
|
|
||||||
|
if (!(u = m->userdata))
|
||||||
|
return;
|
||||||
|
|
||||||
|
c->mainloop->time_free(u->sap_event);
|
||||||
|
|
||||||
|
if (u->source_output) {
|
||||||
|
pa_source_output_disconnect(u->source_output);
|
||||||
|
pa_source_output_unref(u->source_output);
|
||||||
|
}
|
||||||
|
|
||||||
|
pa_rtp_context_destroy(&u->rtp_context);
|
||||||
|
|
||||||
|
pa_sap_send(&u->sap_context, 1);
|
||||||
|
pa_sap_context_destroy(&u->sap_context);
|
||||||
|
|
||||||
|
pa_memblockq_free(u->memblockq);
|
||||||
|
|
||||||
|
pa_xfree(u);
|
||||||
|
}
|
||||||
2355
src/modules/rtp/rfc2327.txt
Normal file
2355
src/modules/rtp/rfc2327.txt
Normal file
File diff suppressed because it is too large
Load diff
1011
src/modules/rtp/rfc2974.txt
Normal file
1011
src/modules/rtp/rfc2974.txt
Normal file
File diff suppressed because it is too large
Load diff
5827
src/modules/rtp/rfc3550.txt
Normal file
5827
src/modules/rtp/rfc3550.txt
Normal file
File diff suppressed because it is too large
Load diff
2467
src/modules/rtp/rfc3551.txt
Normal file
2467
src/modules/rtp/rfc3551.txt
Normal file
File diff suppressed because it is too large
Load diff
193
src/modules/rtp/rtp.c
Normal file
193
src/modules/rtp/rtp.c
Normal file
|
|
@ -0,0 +1,193 @@
|
||||||
|
/* $Id$ */
|
||||||
|
|
||||||
|
/***
|
||||||
|
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 <fcntl.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <polypcore/log.h>
|
||||||
|
|
||||||
|
#include "rtp.h"
|
||||||
|
|
||||||
|
pa_rtp_context* pa_rtp_context_init_send(pa_rtp_context *c, int fd, uint32_t ssrc, uint8_t payload) {
|
||||||
|
assert(c);
|
||||||
|
assert(fd >= 0);
|
||||||
|
|
||||||
|
c->fd = fd;
|
||||||
|
c->sequence = (uint16_t) (rand()*rand());
|
||||||
|
c->timestamp = 0;
|
||||||
|
c->ssrc = ssrc ? ssrc : (uint32_t) (rand()*rand());
|
||||||
|
c->payload = payload & 127;
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define MAX_IOVECS 16
|
||||||
|
|
||||||
|
int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q) {
|
||||||
|
struct iovec iov[MAX_IOVECS];
|
||||||
|
pa_memblock* mb[MAX_IOVECS];
|
||||||
|
int iov_idx = 1;
|
||||||
|
size_t n = 0, skip = 0;
|
||||||
|
|
||||||
|
assert(c);
|
||||||
|
assert(size > 0);
|
||||||
|
assert(q);
|
||||||
|
|
||||||
|
if (pa_memblockq_get_length(q) < size)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
int r;
|
||||||
|
pa_memchunk chunk;
|
||||||
|
|
||||||
|
if ((r = pa_memblockq_peek(q, &chunk)) >= 0) {
|
||||||
|
|
||||||
|
size_t k = n + chunk.length > size ? size - n : chunk.length;
|
||||||
|
|
||||||
|
if (chunk.memblock) {
|
||||||
|
iov[iov_idx].iov_base = (uint8_t*) chunk.memblock->data + chunk.index;
|
||||||
|
iov[iov_idx].iov_len = k;
|
||||||
|
mb[iov_idx] = chunk.memblock;
|
||||||
|
iov_idx ++;
|
||||||
|
|
||||||
|
n += k;
|
||||||
|
}
|
||||||
|
|
||||||
|
skip += k;
|
||||||
|
pa_memblockq_drop(q, &chunk, k);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r < 0 || !chunk.memblock || n >= size || iov_idx >= MAX_IOVECS) {
|
||||||
|
uint32_t header[3];
|
||||||
|
struct msghdr m;
|
||||||
|
int k, i;
|
||||||
|
|
||||||
|
if (n > 0) {
|
||||||
|
header[0] = htonl(((uint32_t) 2 << 30) | ((uint32_t) c->payload << 16) | ((uint32_t) c->sequence));
|
||||||
|
header[1] = htonl(c->timestamp);
|
||||||
|
header[2] = htonl(c->ssrc);
|
||||||
|
|
||||||
|
iov[0].iov_base = header;
|
||||||
|
iov[0].iov_len = sizeof(header);
|
||||||
|
|
||||||
|
m.msg_name = NULL;
|
||||||
|
m.msg_namelen = 0;
|
||||||
|
m.msg_iov = iov;
|
||||||
|
m.msg_iovlen = iov_idx;
|
||||||
|
m.msg_control = NULL;
|
||||||
|
m.msg_controllen = 0;
|
||||||
|
m.msg_flags = 0;
|
||||||
|
|
||||||
|
k = sendmsg(c->fd, &m, MSG_DONTWAIT);
|
||||||
|
|
||||||
|
for (i = 1; i < iov_idx; i++)
|
||||||
|
pa_memblock_unref(mb[i]);
|
||||||
|
|
||||||
|
c->sequence++;
|
||||||
|
} else
|
||||||
|
k = 0;
|
||||||
|
|
||||||
|
c->timestamp += skip;
|
||||||
|
|
||||||
|
if (k < 0) {
|
||||||
|
if (errno != EAGAIN) /* If the queue is full, just ignore it */
|
||||||
|
pa_log(__FILE__": sendmsg() failed: %s", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r < 0 || pa_memblockq_get_length(q) < size)
|
||||||
|
break;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
skip = 0;
|
||||||
|
iov_idx = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd) {
|
||||||
|
assert(c);
|
||||||
|
|
||||||
|
c->fd = fd;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk) {
|
||||||
|
assert(c);
|
||||||
|
assert(chunk);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t pa_rtp_payload_type(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;
|
||||||
|
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 11;
|
||||||
|
|
||||||
|
return 127;
|
||||||
|
}
|
||||||
|
|
||||||
|
pa_sample_spec *pa_rtp_sample_spec_fixup(pa_sample_spec * ss) {
|
||||||
|
assert(ss);
|
||||||
|
|
||||||
|
if (!pa_rtp_sample_spec_valid(ss))
|
||||||
|
ss->format = PA_SAMPLE_S16BE;
|
||||||
|
|
||||||
|
assert(pa_rtp_sample_spec_valid(ss));
|
||||||
|
return ss;
|
||||||
|
}
|
||||||
|
|
||||||
|
int pa_rtp_sample_spec_valid(const pa_sample_spec *ss) {
|
||||||
|
assert(ss);
|
||||||
|
|
||||||
|
if (!pa_sample_spec_valid(ss))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return
|
||||||
|
ss->format == PA_SAMPLE_U8 ||
|
||||||
|
ss->format == PA_SAMPLE_ALAW ||
|
||||||
|
ss->format == PA_SAMPLE_ULAW ||
|
||||||
|
ss->format == PA_SAMPLE_S16BE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pa_rtp_context_destroy(pa_rtp_context *c) {
|
||||||
|
assert(c);
|
||||||
|
|
||||||
|
close(c->fd);
|
||||||
|
}
|
||||||
51
src/modules/rtp/rtp.h
Normal file
51
src/modules/rtp/rtp.h
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
#ifndef foortphfoo
|
||||||
|
#define foortphfoo
|
||||||
|
|
||||||
|
/* $Id$ */
|
||||||
|
|
||||||
|
/***
|
||||||
|
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.
|
||||||
|
***/
|
||||||
|
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <polypcore/memblockq.h>
|
||||||
|
#include <polypcore/memchunk.h>
|
||||||
|
|
||||||
|
typedef struct pa_rtp_context {
|
||||||
|
int fd;
|
||||||
|
uint16_t sequence;
|
||||||
|
uint32_t timestamp;
|
||||||
|
uint32_t ssrc;
|
||||||
|
uint8_t payload;
|
||||||
|
} pa_rtp_context;
|
||||||
|
|
||||||
|
pa_rtp_context* pa_rtp_context_init_send(pa_rtp_context *c, int fd, uint32_t ssrc, uint8_t payload);
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
#endif
|
||||||
107
src/modules/rtp/sap.c
Normal file
107
src/modules/rtp/sap.c
Normal file
|
|
@ -0,0 +1,107 @@
|
||||||
|
/* $Id$ */
|
||||||
|
|
||||||
|
/***
|
||||||
|
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 <time.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <polypcore/util.h>
|
||||||
|
#include <polypcore/log.h>
|
||||||
|
#include <polypcore/xmalloc.h>
|
||||||
|
|
||||||
|
#include "sap.h"
|
||||||
|
|
||||||
|
pa_sap_context* pa_sap_context_init_send(pa_sap_context *c, int fd, char *sdp_data) {
|
||||||
|
assert(c);
|
||||||
|
assert(fd >= 0);
|
||||||
|
assert(sdp_data);
|
||||||
|
|
||||||
|
c->fd = fd;
|
||||||
|
c->sdp_data = sdp_data;
|
||||||
|
c->msg_id_hash = (uint16_t) (rand()*rand());
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pa_sap_context_destroy(pa_sap_context *c) {
|
||||||
|
assert(c);
|
||||||
|
|
||||||
|
close(c->fd);
|
||||||
|
pa_xfree(c->sdp_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
struct iovec iov[4];
|
||||||
|
struct msghdr m;
|
||||||
|
int k;
|
||||||
|
|
||||||
|
if (getsockname(c->fd, sa, &salen) < 0) {
|
||||||
|
pa_log("getsockname() failed: %s\n", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
|
||||||
|
|
||||||
|
header = htonl(((uint32_t) 1 << 29) |
|
||||||
|
(sa->sa_family == AF_INET6 ? (uint32_t) 1 << 28 : 0) |
|
||||||
|
(goodbye ? (uint32_t) 1 << 26 : 0) |
|
||||||
|
(c->msg_id_hash));
|
||||||
|
|
||||||
|
iov[0].iov_base = &header;
|
||||||
|
iov[0].iov_len = sizeof(header);
|
||||||
|
|
||||||
|
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[3].iov_base = c->sdp_data;
|
||||||
|
iov[3].iov_len = strlen(c->sdp_data);
|
||||||
|
|
||||||
|
m.msg_name = NULL;
|
||||||
|
m.msg_namelen = 0;
|
||||||
|
m.msg_iov = iov;
|
||||||
|
m.msg_iovlen = 4;
|
||||||
|
m.msg_control = NULL;
|
||||||
|
m.msg_controllen = 0;
|
||||||
|
m.msg_flags = 0;
|
||||||
|
|
||||||
|
if ((k = sendmsg(c->fd, &m, MSG_DONTWAIT)) < 0)
|
||||||
|
pa_log("sendmsg() failed: %s\n", strerror(errno));
|
||||||
|
|
||||||
|
return k;
|
||||||
|
}
|
||||||
43
src/modules/rtp/sap.h
Normal file
43
src/modules/rtp/sap.h
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
#ifndef foosaphfoo
|
||||||
|
#define foosaphfoo
|
||||||
|
|
||||||
|
/* $Id$ */
|
||||||
|
|
||||||
|
/***
|
||||||
|
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.
|
||||||
|
***/
|
||||||
|
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <polypcore/memblockq.h>
|
||||||
|
#include <polypcore/memchunk.h>
|
||||||
|
|
||||||
|
typedef struct pa_sap_context {
|
||||||
|
int fd;
|
||||||
|
char *sdp_data;
|
||||||
|
|
||||||
|
uint16_t msg_id_hash;
|
||||||
|
} pa_sap_context;
|
||||||
|
|
||||||
|
pa_sap_context* pa_sap_context_init_send(pa_sap_context *c, int fd, char *sdp_data);
|
||||||
|
void pa_sap_context_destroy(pa_sap_context *c);
|
||||||
|
|
||||||
|
int pa_sap_send(pa_sap_context *c, int goodbye);
|
||||||
|
|
||||||
|
#endif
|
||||||
87
src/modules/rtp/sdp.c
Normal file
87
src/modules/rtp/sdp.c
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
/* $Id$ */
|
||||||
|
|
||||||
|
/***
|
||||||
|
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 <time.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#include <polypcore/util.h>
|
||||||
|
|
||||||
|
#include "sdp.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;
|
||||||
|
char buf_src[64], buf_dst[64];
|
||||||
|
const char *u, *f, *a;
|
||||||
|
|
||||||
|
assert(src);
|
||||||
|
assert(dst);
|
||||||
|
assert(af == AF_INET || af == AF_INET6);
|
||||||
|
|
||||||
|
f = map_format(ss->format);
|
||||||
|
assert(f);
|
||||||
|
|
||||||
|
if (!(u = getenv("USER")))
|
||||||
|
if (!(u = getenv("USERNAME")))
|
||||||
|
u = "-";
|
||||||
|
|
||||||
|
ntp = time(NULL) + 2208988800;
|
||||||
|
|
||||||
|
a = inet_ntop(af, src, buf_src, sizeof(buf_src));
|
||||||
|
assert(a);
|
||||||
|
a = inet_ntop(af, dst, buf_dst, sizeof(buf_dst));
|
||||||
|
assert(a);
|
||||||
|
|
||||||
|
return pa_sprintf_malloc(
|
||||||
|
"v=0\n"
|
||||||
|
"o=%s %lu 0 IN %s %s\n"
|
||||||
|
"s=%s\n"
|
||||||
|
"c=IN %s %s\n"
|
||||||
|
"t=%lu 0\n"
|
||||||
|
"a=recvonly\n"
|
||||||
|
"m=audio %u RTP/AVP %i\n"
|
||||||
|
"a=rtpmap:%i %s/%u/%u\n"
|
||||||
|
"a=type:broadcast\n",
|
||||||
|
u, (unsigned long) ntp, af == AF_INET ? "IP4" : "IP6", buf_src,
|
||||||
|
name,
|
||||||
|
af == AF_INET ? "IP4" : "IP6", buf_dst,
|
||||||
|
(unsigned long) ntp,
|
||||||
|
port, payload,
|
||||||
|
payload, f, ss->rate, ss->channels);
|
||||||
|
}
|
||||||
33
src/modules/rtp/sdp.h
Normal file
33
src/modules/rtp/sdp.h
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
#ifndef foosdphfoo
|
||||||
|
#define foosdphfoo
|
||||||
|
|
||||||
|
/* $Id$ */
|
||||||
|
|
||||||
|
/***
|
||||||
|
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.
|
||||||
|
***/
|
||||||
|
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include <polyp/sample.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Add table
Add a link
Reference in a new issue