]> code.delx.au - pulseaudio/blob - src/modules/rtp/rtp.c
NetBSD needs to include sys/uio.h for some socket functions
[pulseaudio] / src / modules / rtp / rtp.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5
6 PulseAudio 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.
10
11 PulseAudio 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.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <arpa/inet.h>
31 #include <unistd.h>
32 #include <sys/ioctl.h>
33
34 #ifdef HAVE_SYS_FILIO_H
35 #include <sys/filio.h>
36 #endif
37
38 #ifdef HAVE_SYS_UIO_H
39 #include <sys/uio.h>
40 #endif
41
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/macro.h>
45 #include <pulsecore/core-util.h>
46
47 #include "rtp.h"
48
49 pa_rtp_context* pa_rtp_context_init_send(pa_rtp_context *c, int fd, uint32_t ssrc, uint8_t payload, size_t frame_size) {
50 pa_assert(c);
51 pa_assert(fd >= 0);
52
53 c->fd = fd;
54 c->sequence = (uint16_t) (rand()*rand());
55 c->timestamp = 0;
56 c->ssrc = ssrc ? ssrc : (uint32_t) (rand()*rand());
57 c->payload = (uint8_t) (payload & 127U);
58 c->frame_size = frame_size;
59
60 pa_memchunk_reset(&c->memchunk);
61
62 return c;
63 }
64
65 #define MAX_IOVECS 16
66
67 int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q) {
68 struct iovec iov[MAX_IOVECS];
69 pa_memblock* mb[MAX_IOVECS];
70 int iov_idx = 1;
71 size_t n = 0;
72
73 pa_assert(c);
74 pa_assert(size > 0);
75 pa_assert(q);
76
77 if (pa_memblockq_get_length(q) < size)
78 return 0;
79
80 for (;;) {
81 int r;
82 pa_memchunk chunk;
83
84 pa_memchunk_reset(&chunk);
85
86 if ((r = pa_memblockq_peek(q, &chunk)) >= 0) {
87
88 size_t k = n + chunk.length > size ? size - n : chunk.length;
89
90 pa_assert(chunk.memblock);
91
92 iov[iov_idx].iov_base = ((uint8_t*) pa_memblock_acquire(chunk.memblock) + chunk.index);
93 iov[iov_idx].iov_len = k;
94 mb[iov_idx] = chunk.memblock;
95 iov_idx ++;
96
97 n += k;
98 pa_memblockq_drop(q, k);
99 }
100
101 pa_assert(n % c->frame_size == 0);
102
103 if (r < 0 || n >= size || iov_idx >= MAX_IOVECS) {
104 uint32_t header[3];
105 struct msghdr m;
106 ssize_t k;
107 int i;
108
109 if (n > 0) {
110 header[0] = htonl(((uint32_t) 2 << 30) | ((uint32_t) c->payload << 16) | ((uint32_t) c->sequence));
111 header[1] = htonl(c->timestamp);
112 header[2] = htonl(c->ssrc);
113
114 iov[0].iov_base = (void*)header;
115 iov[0].iov_len = sizeof(header);
116
117 m.msg_name = NULL;
118 m.msg_namelen = 0;
119 m.msg_iov = iov;
120 m.msg_iovlen = (size_t) iov_idx;
121 m.msg_control = NULL;
122 m.msg_controllen = 0;
123 m.msg_flags = 0;
124
125 k = sendmsg(c->fd, &m, MSG_DONTWAIT);
126
127 for (i = 1; i < iov_idx; i++) {
128 pa_memblock_release(mb[i]);
129 pa_memblock_unref(mb[i]);
130 }
131
132 c->sequence++;
133 } else
134 k = 0;
135
136 c->timestamp += (unsigned) (n/c->frame_size);
137
138 if (k < 0) {
139 if (errno != EAGAIN && errno != EINTR) /* If the queue is full, just ignore it */
140 pa_log("sendmsg() failed: %s", pa_cstrerror(errno));
141 return -1;
142 }
143
144 if (r < 0 || pa_memblockq_get_length(q) < size)
145 break;
146
147 n = 0;
148 iov_idx = 1;
149 }
150 }
151
152 return 0;
153 }
154
155 pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd, size_t frame_size) {
156 pa_assert(c);
157
158 c->fd = fd;
159 c->frame_size = frame_size;
160
161 pa_memchunk_reset(&c->memchunk);
162 return c;
163 }
164
165 int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool) {
166 int size;
167 struct msghdr m;
168 struct iovec iov;
169 uint32_t header;
170 unsigned cc;
171 ssize_t r;
172
173 pa_assert(c);
174 pa_assert(chunk);
175
176 pa_memchunk_reset(chunk);
177
178 if (ioctl(c->fd, FIONREAD, &size) < 0) {
179 pa_log_warn("FIONREAD failed: %s", pa_cstrerror(errno));
180 goto fail;
181 }
182
183 if (size <= 0)
184 return 0;
185
186 if (c->memchunk.length < (unsigned) size) {
187 size_t l;
188
189 if (c->memchunk.memblock)
190 pa_memblock_unref(c->memchunk.memblock);
191
192 l = PA_MAX((size_t) size, pa_mempool_block_size_max(pool));
193
194 c->memchunk.memblock = pa_memblock_new(pool, l);
195 c->memchunk.index = 0;
196 c->memchunk.length = pa_memblock_get_length(c->memchunk.memblock);
197 }
198
199 pa_assert(c->memchunk.length >= (size_t) size);
200
201 chunk->memblock = pa_memblock_ref(c->memchunk.memblock);
202 chunk->index = c->memchunk.index;
203
204 iov.iov_base = (uint8_t*) pa_memblock_acquire(chunk->memblock) + chunk->index;
205 iov.iov_len = (size_t) size;
206
207 m.msg_name = NULL;
208 m.msg_namelen = 0;
209 m.msg_iov = &iov;
210 m.msg_iovlen = 1;
211 m.msg_control = NULL;
212 m.msg_controllen = 0;
213 m.msg_flags = 0;
214
215 r = recvmsg(c->fd, &m, 0);
216 pa_memblock_release(chunk->memblock);
217
218 if (r != size) {
219 if (r < 0 && errno != EAGAIN && errno != EINTR)
220 pa_log_warn("recvmsg() failed: %s", r < 0 ? pa_cstrerror(errno) : "size mismatch");
221
222 goto fail;
223 }
224
225 if (size < 12) {
226 pa_log_warn("RTP packet too short.");
227 goto fail;
228 }
229
230 memcpy(&header, iov.iov_base, sizeof(uint32_t));
231 memcpy(&c->timestamp, (uint8_t*) iov.iov_base + 4, sizeof(uint32_t));
232 memcpy(&c->ssrc, (uint8_t*) iov.iov_base + 8, sizeof(uint32_t));
233
234 header = ntohl(header);
235 c->timestamp = ntohl(c->timestamp);
236 c->ssrc = ntohl(c->ssrc);
237
238 if ((header >> 30) != 2) {
239 pa_log_warn("Unsupported RTP version.");
240 goto fail;
241 }
242
243 if ((header >> 29) & 1) {
244 pa_log_warn("RTP padding not supported.");
245 goto fail;
246 }
247
248 if ((header >> 28) & 1) {
249 pa_log_warn("RTP header extensions not supported.");
250 goto fail;
251 }
252
253 cc = (header >> 24) & 0xF;
254 c->payload = (uint8_t) ((header >> 16) & 127U);
255 c->sequence = (uint16_t) (header & 0xFFFFU);
256
257 if (12 + cc*4 > (unsigned) size) {
258 pa_log_warn("RTP packet too short. (CSRC)");
259 goto fail;
260 }
261
262 chunk->index += 12 + cc*4;
263 chunk->length = (size_t) size - 12 + cc*4;
264
265 if (chunk->length % c->frame_size != 0) {
266 pa_log_warn("Bad RTP packet size.");
267 goto fail;
268 }
269
270 c->memchunk.index = chunk->index + chunk->length;
271 c->memchunk.length = pa_memblock_get_length(c->memchunk.memblock) - c->memchunk.index;
272
273 if (c->memchunk.length <= 0) {
274 pa_memblock_unref(c->memchunk.memblock);
275 pa_memchunk_reset(&c->memchunk);
276 }
277
278 return 0;
279
280 fail:
281 if (chunk->memblock)
282 pa_memblock_unref(chunk->memblock);
283
284 return -1;
285 }
286
287 uint8_t pa_rtp_payload_from_sample_spec(const pa_sample_spec *ss) {
288 pa_assert(ss);
289
290 if (ss->format == PA_SAMPLE_ULAW && ss->rate == 8000 && ss->channels == 1)
291 return 0;
292 if (ss->format == PA_SAMPLE_ALAW && ss->rate == 8000 && ss->channels == 1)
293 return 8;
294 if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 2)
295 return 10;
296 if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 1)
297 return 11;
298
299 return 127;
300 }
301
302 pa_sample_spec *pa_rtp_sample_spec_from_payload(uint8_t payload, pa_sample_spec *ss) {
303 pa_assert(ss);
304
305 switch (payload) {
306 case 0:
307 ss->channels = 1;
308 ss->format = PA_SAMPLE_ULAW;
309 ss->rate = 8000;
310 break;
311
312 case 8:
313 ss->channels = 1;
314 ss->format = PA_SAMPLE_ALAW;
315 ss->rate = 8000;
316 break;
317
318 case 10:
319 ss->channels = 2;
320 ss->format = PA_SAMPLE_S16BE;
321 ss->rate = 44100;
322 break;
323
324 case 11:
325 ss->channels = 1;
326 ss->format = PA_SAMPLE_S16BE;
327 ss->rate = 44100;
328 break;
329
330 default:
331 return NULL;
332 }
333
334 return ss;
335 }
336
337 pa_sample_spec *pa_rtp_sample_spec_fixup(pa_sample_spec * ss) {
338 pa_assert(ss);
339
340 if (!pa_rtp_sample_spec_valid(ss))
341 ss->format = PA_SAMPLE_S16BE;
342
343 pa_assert(pa_rtp_sample_spec_valid(ss));
344 return ss;
345 }
346
347 int pa_rtp_sample_spec_valid(const pa_sample_spec *ss) {
348 pa_assert(ss);
349
350 if (!pa_sample_spec_valid(ss))
351 return 0;
352
353 return
354 ss->format == PA_SAMPLE_U8 ||
355 ss->format == PA_SAMPLE_ALAW ||
356 ss->format == PA_SAMPLE_ULAW ||
357 ss->format == PA_SAMPLE_S16BE;
358 }
359
360 void pa_rtp_context_destroy(pa_rtp_context *c) {
361 pa_assert(c);
362
363 pa_assert_se(pa_close(c->fd) == 0);
364
365 if (c->memchunk.memblock)
366 pa_memblock_unref(c->memchunk.memblock);
367 }
368
369 const char* pa_rtp_format_to_string(pa_sample_format_t f) {
370 switch (f) {
371 case PA_SAMPLE_S16BE:
372 return "L16";
373 case PA_SAMPLE_U8:
374 return "L8";
375 case PA_SAMPLE_ALAW:
376 return "PCMA";
377 case PA_SAMPLE_ULAW:
378 return "PCMU";
379 default:
380 return NULL;
381 }
382 }
383
384 pa_sample_format_t pa_rtp_string_to_format(const char *s) {
385 pa_assert(s);
386
387 if (!(strcmp(s, "L16")))
388 return PA_SAMPLE_S16BE;
389 else if (!strcmp(s, "L8"))
390 return PA_SAMPLE_U8;
391 else if (!strcmp(s, "PCMA"))
392 return PA_SAMPLE_ALAW;
393 else if (!strcmp(s, "PCMU"))
394 return PA_SAMPLE_ULAW;
395 else
396 return PA_SAMPLE_INVALID;
397 }