4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <arpa/inet.h>
35 #include <sys/ioctl.h>
37 #ifdef HAVE_SYS_FILIO_H
38 #include <sys/filio.h>
41 #include <pulsecore/core-error.h>
42 #include <pulsecore/log.h>
46 pa_rtp_context* pa_rtp_context_init_send(pa_rtp_context *c, int fd, uint32_t ssrc, uint8_t payload, size_t frame_size) {
51 c->sequence = (uint16_t) (rand()*rand());
53 c->ssrc = ssrc ? ssrc : (uint32_t) (rand()*rand());
54 c->payload = payload & 127;
55 c->frame_size = frame_size;
62 int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q) {
63 struct iovec iov[MAX_IOVECS];
64 pa_memblock* mb[MAX_IOVECS];
66 size_t n = 0, skip = 0;
72 if (pa_memblockq_get_length(q) < size)
79 if ((r = pa_memblockq_peek(q, &chunk)) >= 0) {
81 size_t k = n + chunk.length > size ? size - n : chunk.length;
84 iov[iov_idx].iov_base = (void*)((uint8_t*) chunk.memblock->data + chunk.index);
85 iov[iov_idx].iov_len = k;
86 mb[iov_idx] = chunk.memblock;
93 pa_memblockq_drop(q, &chunk, k);
96 if (r < 0 || !chunk.memblock || n >= size || iov_idx >= MAX_IOVECS) {
102 header[0] = htonl(((uint32_t) 2 << 30) | ((uint32_t) c->payload << 16) | ((uint32_t) c->sequence));
103 header[1] = htonl(c->timestamp);
104 header[2] = htonl(c->ssrc);
106 iov[0].iov_base = (void*)header;
107 iov[0].iov_len = sizeof(header);
112 m.msg_iovlen = iov_idx;
113 m.msg_control = NULL;
114 m.msg_controllen = 0;
117 k = sendmsg(c->fd, &m, MSG_DONTWAIT);
119 for (i = 1; i < iov_idx; i++)
120 pa_memblock_unref(mb[i]);
126 c->timestamp += skip/c->frame_size;
129 if (errno != EAGAIN) /* If the queue is full, just ignore it */
130 pa_log("sendmsg() failed: %s", pa_cstrerror(errno));
134 if (r < 0 || pa_memblockq_get_length(q) < size)
146 pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd, size_t frame_size) {
150 c->frame_size = frame_size;
154 int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool) {
165 chunk->memblock = NULL;
167 if (ioctl(c->fd, FIONREAD, &size) < 0) {
168 pa_log("FIONREAD failed: %s", pa_cstrerror(errno));
175 chunk->memblock = pa_memblock_new(pool, size);
177 iov.iov_base = chunk->memblock->data;
184 m.msg_control = NULL;
185 m.msg_controllen = 0;
188 if ((r = recvmsg(c->fd, &m, 0)) != size) {
189 pa_log("recvmsg() failed: %s", r < 0 ? pa_cstrerror(errno) : "size mismatch");
194 pa_log("RTP packet too short.");
198 memcpy(&header, chunk->memblock->data, sizeof(uint32_t));
199 memcpy(&c->timestamp, (uint8_t*) chunk->memblock->data + 4, sizeof(uint32_t));
200 memcpy(&c->ssrc, (uint8_t*) chunk->memblock->data + 8, sizeof(uint32_t));
202 header = ntohl(header);
203 c->timestamp = ntohl(c->timestamp);
204 c->ssrc = ntohl(c->ssrc);
206 if ((header >> 30) != 2) {
207 pa_log("Unsupported RTP version.");
211 if ((header >> 29) & 1) {
212 pa_log("RTP padding not supported.");
216 if ((header >> 28) & 1) {
217 pa_log("RTP header extensions not supported.");
221 cc = (header >> 24) & 0xF;
222 c->payload = (header >> 16) & 127;
223 c->sequence = header & 0xFFFF;
225 if (12 + cc*4 > size) {
226 pa_log("RTP packet too short. (CSRC)");
230 chunk->index = 12 + cc*4;
231 chunk->length = size - chunk->index;
233 if (chunk->length % c->frame_size != 0) {
234 pa_log("Vad RTP packet size.");
242 pa_memblock_unref(chunk->memblock);
247 uint8_t pa_rtp_payload_from_sample_spec(const pa_sample_spec *ss) {
250 if (ss->format == PA_SAMPLE_ULAW && ss->rate == 8000 && ss->channels == 1)
252 if (ss->format == PA_SAMPLE_ALAW && ss->rate == 8000 && ss->channels == 1)
254 if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 2)
256 if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 1)
262 pa_sample_spec *pa_rtp_sample_spec_from_payload(uint8_t payload, pa_sample_spec *ss) {
268 ss->format = PA_SAMPLE_ULAW;
274 ss->format = PA_SAMPLE_ALAW;
280 ss->format = PA_SAMPLE_S16BE;
286 ss->format = PA_SAMPLE_S16BE;
297 pa_sample_spec *pa_rtp_sample_spec_fixup(pa_sample_spec * ss) {
300 if (!pa_rtp_sample_spec_valid(ss))
301 ss->format = PA_SAMPLE_S16BE;
303 assert(pa_rtp_sample_spec_valid(ss));
307 int pa_rtp_sample_spec_valid(const pa_sample_spec *ss) {
310 if (!pa_sample_spec_valid(ss))
314 ss->format == PA_SAMPLE_U8 ||
315 ss->format == PA_SAMPLE_ALAW ||
316 ss->format == PA_SAMPLE_ULAW ||
317 ss->format == PA_SAMPLE_S16BE;
320 void pa_rtp_context_destroy(pa_rtp_context *c) {
326 const char* pa_rtp_format_to_string(pa_sample_format_t f) {
328 case PA_SAMPLE_S16BE:
341 pa_sample_format_t pa_rtp_string_to_format(const char *s) {
344 if (!(strcmp(s, "L16")))
345 return PA_SAMPLE_S16BE;
346 else if (!strcmp(s, "L8"))
348 else if (!strcmp(s, "PCMA"))
349 return PA_SAMPLE_ALAW;
350 else if (!strcmp(s, "PCMU"))
351 return PA_SAMPLE_ULAW;
353 return PA_SAMPLE_INVALID;