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