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
32 #include <arpa/inet.h>
34 #include <sys/ioctl.h>
36 #ifdef HAVE_SYS_FILIO_H
37 #include <sys/filio.h>
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/macro.h>
43 #include <pulsecore/core-util.h>
47 pa_rtp_context* pa_rtp_context_init_send(pa_rtp_context *c, int fd, uint32_t ssrc, uint8_t payload, size_t frame_size) {
52 c->sequence = (uint16_t) (rand()*rand());
54 c->ssrc = ssrc ? ssrc : (uint32_t) (rand()*rand());
55 c->payload = payload & 127;
56 c->frame_size = frame_size;
58 pa_memchunk_reset(&c->memchunk);
65 int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q) {
66 struct iovec iov[MAX_IOVECS];
67 pa_memblock* mb[MAX_IOVECS];
75 if (pa_memblockq_get_length(q) < size)
82 pa_memchunk_reset(&chunk);
84 if ((r = pa_memblockq_peek(q, &chunk)) >= 0) {
86 size_t k = n + chunk.length > size ? size - n : chunk.length;
88 pa_assert(chunk.memblock);
90 iov[iov_idx].iov_base = ((uint8_t*) pa_memblock_acquire(chunk.memblock) + chunk.index);
91 iov[iov_idx].iov_len = k;
92 mb[iov_idx] = chunk.memblock;
96 pa_memblockq_drop(q, k);
99 pa_assert(n % c->frame_size == 0);
101 if (r < 0 || n >= size || iov_idx >= MAX_IOVECS) {
107 header[0] = htonl(((uint32_t) 2 << 30) | ((uint32_t) c->payload << 16) | ((uint32_t) c->sequence));
108 header[1] = htonl(c->timestamp);
109 header[2] = htonl(c->ssrc);
111 iov[0].iov_base = (void*)header;
112 iov[0].iov_len = sizeof(header);
117 m.msg_iovlen = iov_idx;
118 m.msg_control = NULL;
119 m.msg_controllen = 0;
122 k = sendmsg(c->fd, &m, MSG_DONTWAIT);
124 for (i = 1; i < iov_idx; i++) {
125 pa_memblock_release(mb[i]);
126 pa_memblock_unref(mb[i]);
133 c->timestamp += n/c->frame_size;
136 if (errno != EAGAIN && errno != EINTR) /* If the queue is full, just ignore it */
137 pa_log("sendmsg() failed: %s", pa_cstrerror(errno));
141 if (r < 0 || pa_memblockq_get_length(q) < size)
152 pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd, size_t frame_size) {
156 c->frame_size = frame_size;
158 pa_memchunk_reset(&c->memchunk);
162 int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool) {
173 pa_memchunk_reset(chunk);
175 if (ioctl(c->fd, FIONREAD, &size) < 0) {
176 pa_log_warn("FIONREAD failed: %s", pa_cstrerror(errno));
183 if (c->memchunk.length < (unsigned) size) {
186 if (c->memchunk.memblock)
187 pa_memblock_unref(c->memchunk.memblock);
189 l = PA_MAX((size_t) size, pa_mempool_block_size_max(pool));
191 c->memchunk.memblock = pa_memblock_new(pool, l);
192 c->memchunk.index = 0;
193 c->memchunk.length = pa_memblock_get_length(c->memchunk.memblock);
196 pa_assert(c->memchunk.length >= (size_t) size);
198 chunk->memblock = pa_memblock_ref(c->memchunk.memblock);
199 chunk->index = c->memchunk.index;
201 iov.iov_base = (uint8_t*) pa_memblock_acquire(chunk->memblock) + chunk->index;
208 m.msg_control = NULL;
209 m.msg_controllen = 0;
212 r = recvmsg(c->fd, &m, 0);
213 pa_memblock_release(chunk->memblock);
216 if (r < 0 && errno != EAGAIN && errno != EINTR)
217 pa_log_warn("recvmsg() failed: %s", r < 0 ? pa_cstrerror(errno) : "size mismatch");
223 pa_log_warn("RTP packet too short.");
227 memcpy(&header, iov.iov_base, sizeof(uint32_t));
228 memcpy(&c->timestamp, (uint8_t*) iov.iov_base + 4, sizeof(uint32_t));
229 memcpy(&c->ssrc, (uint8_t*) iov.iov_base + 8, sizeof(uint32_t));
231 header = ntohl(header);
232 c->timestamp = ntohl(c->timestamp);
233 c->ssrc = ntohl(c->ssrc);
235 if ((header >> 30) != 2) {
236 pa_log_warn("Unsupported RTP version.");
240 if ((header >> 29) & 1) {
241 pa_log_warn("RTP padding not supported.");
245 if ((header >> 28) & 1) {
246 pa_log_warn("RTP header extensions not supported.");
250 cc = (header >> 24) & 0xF;
251 c->payload = (header >> 16) & 127;
252 c->sequence = header & 0xFFFF;
254 if (12 + cc*4 > size) {
255 pa_log_warn("RTP packet too short. (CSRC)");
259 chunk->index += 12 + cc*4;
260 chunk->length = size - 12 + cc*4;
262 if (chunk->length % c->frame_size != 0) {
263 pa_log_warn("Bad RTP packet size.");
267 c->memchunk.index = chunk->index + chunk->length;
268 c->memchunk.length = pa_memblock_get_length(c->memchunk.memblock) - c->memchunk.index;
270 if (c->memchunk.length <= 0) {
271 pa_memblock_unref(c->memchunk.memblock);
272 pa_memchunk_reset(&c->memchunk);
279 pa_memblock_unref(chunk->memblock);
284 uint8_t pa_rtp_payload_from_sample_spec(const pa_sample_spec *ss) {
287 if (ss->format == PA_SAMPLE_ULAW && ss->rate == 8000 && ss->channels == 1)
289 if (ss->format == PA_SAMPLE_ALAW && ss->rate == 8000 && ss->channels == 1)
291 if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 2)
293 if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 1)
299 pa_sample_spec *pa_rtp_sample_spec_from_payload(uint8_t payload, pa_sample_spec *ss) {
305 ss->format = PA_SAMPLE_ULAW;
311 ss->format = PA_SAMPLE_ALAW;
317 ss->format = PA_SAMPLE_S16BE;
323 ss->format = PA_SAMPLE_S16BE;
334 pa_sample_spec *pa_rtp_sample_spec_fixup(pa_sample_spec * ss) {
337 if (!pa_rtp_sample_spec_valid(ss))
338 ss->format = PA_SAMPLE_S16BE;
340 pa_assert(pa_rtp_sample_spec_valid(ss));
344 int pa_rtp_sample_spec_valid(const pa_sample_spec *ss) {
347 if (!pa_sample_spec_valid(ss))
351 ss->format == PA_SAMPLE_U8 ||
352 ss->format == PA_SAMPLE_ALAW ||
353 ss->format == PA_SAMPLE_ULAW ||
354 ss->format == PA_SAMPLE_S16BE;
357 void pa_rtp_context_destroy(pa_rtp_context *c) {
360 pa_assert_se(pa_close(c->fd) == 0);
362 if (c->memchunk.memblock)
363 pa_memblock_unref(c->memchunk.memblock);
366 const char* pa_rtp_format_to_string(pa_sample_format_t f) {
368 case PA_SAMPLE_S16BE:
381 pa_sample_format_t pa_rtp_string_to_format(const char *s) {
384 if (!(strcmp(s, "L16")))
385 return PA_SAMPLE_S16BE;
386 else if (!strcmp(s, "L8"))
388 else if (!strcmp(s, "PCMA"))
389 return PA_SAMPLE_ALAW;
390 else if (!strcmp(s, "PCMU"))
391 return PA_SAMPLE_ULAW;
393 return PA_SAMPLE_INVALID;