4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <arpa/inet.h>
34 #include <polypcore/log.h>
38 pa_rtp_context* pa_rtp_context_init_send(pa_rtp_context *c, int fd, uint32_t ssrc, uint8_t payload) {
43 c->sequence = (uint16_t) (rand()*rand());
45 c->ssrc = ssrc ? ssrc : (uint32_t) (rand()*rand());
46 c->payload = payload & 127;
53 int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q) {
54 struct iovec iov[MAX_IOVECS];
55 pa_memblock* mb[MAX_IOVECS];
57 size_t n = 0, skip = 0;
63 if (pa_memblockq_get_length(q) < size)
70 if ((r = pa_memblockq_peek(q, &chunk)) >= 0) {
72 size_t k = n + chunk.length > size ? size - n : chunk.length;
75 iov[iov_idx].iov_base = (uint8_t*) chunk.memblock->data + chunk.index;
76 iov[iov_idx].iov_len = k;
77 mb[iov_idx] = chunk.memblock;
84 pa_memblockq_drop(q, &chunk, k);
87 if (r < 0 || !chunk.memblock || n >= size || iov_idx >= MAX_IOVECS) {
93 header[0] = htonl(((uint32_t) 2 << 30) | ((uint32_t) c->payload << 16) | ((uint32_t) c->sequence));
94 header[1] = htonl(c->timestamp);
95 header[2] = htonl(c->ssrc);
97 iov[0].iov_base = header;
98 iov[0].iov_len = sizeof(header);
103 m.msg_iovlen = iov_idx;
104 m.msg_control = NULL;
105 m.msg_controllen = 0;
108 k = sendmsg(c->fd, &m, MSG_DONTWAIT);
110 for (i = 1; i < iov_idx; i++)
111 pa_memblock_unref(mb[i]);
117 c->timestamp += skip;
120 if (errno != EAGAIN) /* If the queue is full, just ignore it */
121 pa_log(__FILE__": sendmsg() failed: %s", strerror(errno));
125 if (r < 0 || pa_memblockq_get_length(q) < size)
137 pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd) {
144 int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk) {
151 uint8_t pa_rtp_payload_type(const pa_sample_spec *ss) {
154 if (ss->format == PA_SAMPLE_ULAW && ss->rate == 8000 && ss->channels == 1)
156 if (ss->format == PA_SAMPLE_ALAW && ss->rate == 8000 && ss->channels == 1)
158 if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 2)
160 if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 1)
166 pa_sample_spec *pa_rtp_sample_spec_fixup(pa_sample_spec * ss) {
169 if (!pa_rtp_sample_spec_valid(ss))
170 ss->format = PA_SAMPLE_S16BE;
172 assert(pa_rtp_sample_spec_valid(ss));
176 int pa_rtp_sample_spec_valid(const pa_sample_spec *ss) {
179 if (!pa_sample_spec_valid(ss))
183 ss->format == PA_SAMPLE_U8 ||
184 ss->format == PA_SAMPLE_ALAW ||
185 ss->format == PA_SAMPLE_ULAW ||
186 ss->format == PA_SAMPLE_S16BE;
189 void pa_rtp_context_destroy(pa_rtp_context *c) {