]> code.delx.au - pulseaudio/blob - src/modules/rtp/rtp.c
* add RTP/SAP/SDP reciever module
[pulseaudio] / src / modules / rtp / rtp.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <fcntl.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <arpa/inet.h>
32 #include <unistd.h>
33 #include <sys/ioctl.h>
34
35 #include <polypcore/log.h>
36
37 #include "rtp.h"
38
39 pa_rtp_context* pa_rtp_context_init_send(pa_rtp_context *c, int fd, uint32_t ssrc, uint8_t payload, size_t frame_size) {
40 assert(c);
41 assert(fd >= 0);
42
43 c->fd = fd;
44 c->sequence = (uint16_t) (rand()*rand());
45 c->timestamp = 0;
46 c->ssrc = ssrc ? ssrc : (uint32_t) (rand()*rand());
47 c->payload = payload & 127;
48 c->frame_size = frame_size;
49
50 return c;
51 }
52
53 #define MAX_IOVECS 16
54
55 int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q) {
56 struct iovec iov[MAX_IOVECS];
57 pa_memblock* mb[MAX_IOVECS];
58 int iov_idx = 1;
59 size_t n = 0, skip = 0;
60
61 assert(c);
62 assert(size > 0);
63 assert(q);
64
65 if (pa_memblockq_get_length(q) < size)
66 return 0;
67
68 for (;;) {
69 int r;
70 pa_memchunk chunk;
71
72 if ((r = pa_memblockq_peek(q, &chunk)) >= 0) {
73
74 size_t k = n + chunk.length > size ? size - n : chunk.length;
75
76 if (chunk.memblock) {
77 iov[iov_idx].iov_base = (uint8_t*) chunk.memblock->data + chunk.index;
78 iov[iov_idx].iov_len = k;
79 mb[iov_idx] = chunk.memblock;
80 iov_idx ++;
81
82 n += k;
83 }
84
85 skip += k;
86 pa_memblockq_drop(q, &chunk, k);
87 }
88
89 if (r < 0 || !chunk.memblock || n >= size || iov_idx >= MAX_IOVECS) {
90 uint32_t header[3];
91 struct msghdr m;
92 int k, i;
93
94 if (n > 0) {
95 header[0] = htonl(((uint32_t) 2 << 30) | ((uint32_t) c->payload << 16) | ((uint32_t) c->sequence));
96 header[1] = htonl(c->timestamp);
97 header[2] = htonl(c->ssrc);
98
99 iov[0].iov_base = header;
100 iov[0].iov_len = sizeof(header);
101
102 m.msg_name = NULL;
103 m.msg_namelen = 0;
104 m.msg_iov = iov;
105 m.msg_iovlen = iov_idx;
106 m.msg_control = NULL;
107 m.msg_controllen = 0;
108 m.msg_flags = 0;
109
110 k = sendmsg(c->fd, &m, MSG_DONTWAIT);
111
112 for (i = 1; i < iov_idx; i++)
113 pa_memblock_unref(mb[i]);
114
115 c->sequence++;
116 } else
117 k = 0;
118
119 c->timestamp += skip/c->frame_size;
120
121 if (k < 0) {
122 if (errno != EAGAIN) /* If the queue is full, just ignore it */
123 pa_log(__FILE__": sendmsg() failed: %s", strerror(errno));
124 return -1;
125 }
126
127 if (r < 0 || pa_memblockq_get_length(q) < size)
128 break;
129
130 n = 0;
131 skip = 0;
132 iov_idx = 1;
133 }
134 }
135
136 return 0;
137 }
138
139 pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd, size_t frame_size) {
140 assert(c);
141
142 c->fd = fd;
143 c->frame_size = frame_size;
144 return c;
145 }
146
147 int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_memblock_stat *st) {
148 int size;
149 struct msghdr m;
150 struct iovec iov;
151 uint32_t header;
152 int cc;
153 ssize_t r;
154
155 assert(c);
156 assert(chunk);
157
158 chunk->memblock = NULL;
159
160 if (ioctl(c->fd, FIONREAD, &size) < 0) {
161 pa_log(__FILE__": FIONREAD failed: %s", strerror(errno));
162 goto fail;
163 }
164
165 if (!size)
166 return 0;
167
168 chunk->memblock = pa_memblock_new(size, st);
169
170 iov.iov_base = chunk->memblock->data;
171 iov.iov_len = size;
172
173 m.msg_name = NULL;
174 m.msg_namelen = 0;
175 m.msg_iov = &iov;
176 m.msg_iovlen = 1;
177 m.msg_control = NULL;
178 m.msg_controllen = 0;
179 m.msg_flags = 0;
180
181 if ((r = recvmsg(c->fd, &m, 0)) != size) {
182 pa_log(__FILE__": recvmsg() failed: %s", r < 0 ? strerror(errno) : "size mismatch");
183 goto fail;
184 }
185
186 if (size < 12) {
187 pa_log(__FILE__": RTP packet too short.");
188 goto fail;
189 }
190
191 memcpy(&header, chunk->memblock->data, sizeof(uint32_t));
192 memcpy(&c->timestamp, (uint8_t*) chunk->memblock->data + 4, sizeof(uint32_t));
193 memcpy(&c->ssrc, (uint8_t*) chunk->memblock->data + 8, sizeof(uint32_t));
194
195 header = ntohl(header);
196 c->timestamp = ntohl(c->timestamp);
197 c->ssrc = ntohl(c->ssrc);
198
199 if ((header >> 30) != 2) {
200 pa_log(__FILE__": Unsupported RTP version.");
201 goto fail;
202 }
203
204 if ((header >> 29) & 1) {
205 pa_log(__FILE__": RTP padding not supported.");
206 goto fail;
207 }
208
209 if ((header >> 28) & 1) {
210 pa_log(__FILE__": RTP header extensions not supported.");
211 goto fail;
212 }
213
214 cc = (header >> 24) & 0xF;
215 c->payload = (header >> 16) & 127;
216 c->sequence = header & 0xFFFF;
217
218 if (12 + cc*4 > size) {
219 pa_log(__FILE__": RTP packet too short. (CSRC)");
220 goto fail;
221 }
222
223 chunk->index = 12 + cc*4;
224 chunk->length = size - chunk->index;
225
226 if (chunk->length % c->frame_size != 0) {
227 pa_log(__FILE__": Vad RTP packet size.");
228 goto fail;
229 }
230
231 return 0;
232
233 fail:
234 if (chunk->memblock)
235 pa_memblock_unref(chunk->memblock);
236
237 return -1;
238 }
239
240 uint8_t pa_rtp_payload_from_sample_spec(const pa_sample_spec *ss) {
241 assert(ss);
242
243 if (ss->format == PA_SAMPLE_ULAW && ss->rate == 8000 && ss->channels == 1)
244 return 0;
245 if (ss->format == PA_SAMPLE_ALAW && ss->rate == 8000 && ss->channels == 1)
246 return 8;
247 if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 2)
248 return 10;
249 if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 1)
250 return 11;
251
252 return 127;
253 }
254
255 pa_sample_spec *pa_rtp_sample_spec_from_payload(uint8_t payload, pa_sample_spec *ss) {
256 assert(ss);
257
258 switch (payload) {
259 case 0:
260 ss->channels = 1;
261 ss->format = PA_SAMPLE_ULAW;
262 ss->rate = 8000;
263 break;
264
265 case 8:
266 ss->channels = 1;
267 ss->format = PA_SAMPLE_ALAW;
268 ss->rate = 8000;
269 break;
270
271 case 10:
272 ss->channels = 2;
273 ss->format = PA_SAMPLE_S16BE;
274 ss->rate = 44100;
275 break;
276
277 case 11:
278 ss->channels = 1;
279 ss->format = PA_SAMPLE_S16BE;
280 ss->rate = 44100;
281 break;
282
283 default:
284 return NULL;
285 }
286
287 return ss;
288 }
289
290 pa_sample_spec *pa_rtp_sample_spec_fixup(pa_sample_spec * ss) {
291 assert(ss);
292
293 if (!pa_rtp_sample_spec_valid(ss))
294 ss->format = PA_SAMPLE_S16BE;
295
296 assert(pa_rtp_sample_spec_valid(ss));
297 return ss;
298 }
299
300 int pa_rtp_sample_spec_valid(const pa_sample_spec *ss) {
301 assert(ss);
302
303 if (!pa_sample_spec_valid(ss))
304 return 0;
305
306 return
307 ss->format == PA_SAMPLE_U8 ||
308 ss->format == PA_SAMPLE_ALAW ||
309 ss->format == PA_SAMPLE_ULAW ||
310 ss->format == PA_SAMPLE_S16BE;
311 }
312
313 void pa_rtp_context_destroy(pa_rtp_context *c) {
314 assert(c);
315
316 close(c->fd);
317 }
318
319 const char* pa_rtp_format_to_string(pa_sample_format_t f) {
320 switch (f) {
321 case PA_SAMPLE_S16BE:
322 return "L16";
323 case PA_SAMPLE_U8:
324 return "L8";
325 case PA_SAMPLE_ALAW:
326 return "PCMA";
327 case PA_SAMPLE_ULAW:
328 return "PCMU";
329 default:
330 return NULL;
331 }
332 }
333
334 pa_sample_format_t pa_rtp_string_to_format(const char *s) {
335 assert(s);
336
337 if (!(strcmp(s, "L16")))
338 return PA_SAMPLE_S16BE;
339 else if (!strcmp(s, "L8"))
340 return PA_SAMPLE_U8;
341 else if (!strcmp(s, "PCMA"))
342 return PA_SAMPLE_ALAW;
343 else if (!strcmp(s, "PCMU"))
344 return PA_SAMPLE_ULAW;
345 else
346 return PA_SAMPLE_INVALID;
347 }
348