2006-04-14 23:47:33 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2006-04-14 23:47:33 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2006-04-14 23:47:33 +00:00
|
|
|
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.
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2006-04-14 23:47:33 +00:00
|
|
|
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
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2006-04-14 23:47:33 +00:00
|
|
|
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>
|
2006-04-16 00:16:53 +00:00
|
|
|
#include <sys/ioctl.h>
|
2006-04-14 23:47:33 +00:00
|
|
|
|
2006-04-18 14:09:56 +00:00
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
|
|
|
|
#include <sys/filio.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/core-error.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
2006-04-14 23:47:33 +00:00
|
|
|
|
|
|
|
|
#include "rtp.h"
|
|
|
|
|
|
2006-04-15 15:26:42 +00:00
|
|
|
pa_rtp_context* pa_rtp_context_init_send(pa_rtp_context *c, int fd, uint32_t ssrc, uint8_t payload, size_t frame_size) {
|
2006-04-14 23:47:33 +00:00
|
|
|
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;
|
2006-04-15 15:26:42 +00:00
|
|
|
c->frame_size = frame_size;
|
|
|
|
|
|
2006-04-14 23:47:33 +00:00
|
|
|
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) {
|
2006-04-18 14:09:56 +00:00
|
|
|
iov[iov_idx].iov_base = (void*)((uint8_t*) chunk.memblock->data + chunk.index);
|
2006-04-14 23:47:33 +00:00
|
|
|
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);
|
|
|
|
|
|
2006-04-18 14:09:56 +00:00
|
|
|
iov[0].iov_base = (void*)header;
|
2006-04-14 23:47:33 +00:00
|
|
|
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;
|
|
|
|
|
|
2006-04-15 15:26:42 +00:00
|
|
|
c->timestamp += skip/c->frame_size;
|
2006-04-14 23:47:33 +00:00
|
|
|
|
|
|
|
|
if (k < 0) {
|
|
|
|
|
if (errno != EAGAIN) /* If the queue is full, just ignore it */
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("sendmsg() failed: %s", pa_cstrerror(errno));
|
2006-04-14 23:47:33 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (r < 0 || pa_memblockq_get_length(q) < size)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
skip = 0;
|
|
|
|
|
iov_idx = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-16 00:16:53 +00:00
|
|
|
pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd, size_t frame_size) {
|
2006-04-14 23:47:33 +00:00
|
|
|
assert(c);
|
|
|
|
|
|
|
|
|
|
c->fd = fd;
|
2006-04-16 00:16:53 +00:00
|
|
|
c->frame_size = frame_size;
|
2006-04-14 23:47:33 +00:00
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-18 19:55:18 +00:00
|
|
|
int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool) {
|
2006-04-16 00:16:53 +00:00
|
|
|
int size;
|
|
|
|
|
struct msghdr m;
|
|
|
|
|
struct iovec iov;
|
|
|
|
|
uint32_t header;
|
|
|
|
|
int cc;
|
|
|
|
|
ssize_t r;
|
|
|
|
|
|
2006-04-14 23:47:33 +00:00
|
|
|
assert(c);
|
|
|
|
|
assert(chunk);
|
|
|
|
|
|
2006-04-16 00:16:53 +00:00
|
|
|
chunk->memblock = NULL;
|
|
|
|
|
|
|
|
|
|
if (ioctl(c->fd, FIONREAD, &size) < 0) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("FIONREAD failed: %s", pa_cstrerror(errno));
|
2006-04-16 00:16:53 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!size)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2006-08-18 19:55:18 +00:00
|
|
|
chunk->memblock = pa_memblock_new(pool, size);
|
2006-04-16 00:16:53 +00:00
|
|
|
|
|
|
|
|
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) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("recvmsg() failed: %s", r < 0 ? pa_cstrerror(errno) : "size mismatch");
|
2006-04-16 00:16:53 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (size < 12) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("RTP packet too short.");
|
2006-04-16 00:16:53 +00:00
|
|
|
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) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("Unsupported RTP version.");
|
2006-04-16 00:16:53 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((header >> 29) & 1) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("RTP padding not supported.");
|
2006-04-16 00:16:53 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((header >> 28) & 1) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("RTP header extensions not supported.");
|
2006-04-16 00:16:53 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cc = (header >> 24) & 0xF;
|
|
|
|
|
c->payload = (header >> 16) & 127;
|
|
|
|
|
c->sequence = header & 0xFFFF;
|
|
|
|
|
|
|
|
|
|
if (12 + cc*4 > size) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("RTP packet too short. (CSRC)");
|
2006-04-16 00:16:53 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chunk->index = 12 + cc*4;
|
|
|
|
|
chunk->length = size - chunk->index;
|
|
|
|
|
|
|
|
|
|
if (chunk->length % c->frame_size != 0) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("Vad RTP packet size.");
|
2006-04-16 00:16:53 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-14 23:47:33 +00:00
|
|
|
return 0;
|
2006-04-16 00:16:53 +00:00
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
if (chunk->memblock)
|
|
|
|
|
pa_memblock_unref(chunk->memblock);
|
|
|
|
|
|
|
|
|
|
return -1;
|
2006-04-14 23:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
2006-04-16 00:16:53 +00:00
|
|
|
uint8_t pa_rtp_payload_from_sample_spec(const pa_sample_spec *ss) {
|
2006-04-14 23:47:33 +00:00
|
|
|
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)
|
2006-04-16 00:16:53 +00:00
|
|
|
return 8;
|
2006-04-14 23:47:33 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-16 00:16:53 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-14 23:47:33 +00:00
|
|
|
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);
|
|
|
|
|
}
|
2006-04-16 00:16:53 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|