]> code.delx.au - pulseaudio/blob - polyp/pstream.c
add support for SCHED_FIFO
[pulseaudio] / polyp / pstream.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 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 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 <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <netinet/in.h>
30
31 #include "pstream.h"
32 #include "queue.h"
33 #include "xmalloc.h"
34
35 enum pa_pstream_descriptor_index {
36 PA_PSTREAM_DESCRIPTOR_LENGTH,
37 PA_PSTREAM_DESCRIPTOR_CHANNEL,
38 PA_PSTREAM_DESCRIPTOR_DELTA,
39 PA_PSTREAM_DESCRIPTOR_MAX
40 };
41
42 typedef uint32_t pa_pstream_descriptor[PA_PSTREAM_DESCRIPTOR_MAX];
43
44 #define PA_PSTREAM_DESCRIPTOR_SIZE (PA_PSTREAM_DESCRIPTOR_MAX*sizeof(uint32_t))
45 #define FRAME_SIZE_MAX (1024*500) /* half a megabyte */
46
47 struct item_info {
48 enum { PA_PSTREAM_ITEM_PACKET, PA_PSTREAM_ITEM_MEMBLOCK } type;
49
50 /* memblock info */
51 struct pa_memchunk chunk;
52 uint32_t channel;
53 uint32_t delta;
54
55 /* packet info */
56 struct pa_packet *packet;
57 };
58
59 struct pa_pstream {
60 int ref;
61
62 struct pa_mainloop_api *mainloop;
63 struct pa_defer_event *defer_event;
64 struct pa_iochannel *io;
65 struct pa_queue *send_queue;
66
67 int dead;
68 void (*die_callback) (struct pa_pstream *p, void *userdata);
69 void *die_callback_userdata;
70
71 struct {
72 struct item_info* current;
73 pa_pstream_descriptor descriptor;
74 void *data;
75 size_t index;
76 } write;
77
78 struct {
79 struct pa_memblock *memblock;
80 struct pa_packet *packet;
81 pa_pstream_descriptor descriptor;
82 void *data;
83 size_t index;
84 } read;
85
86 void (*recieve_packet_callback) (struct pa_pstream *p, struct pa_packet *packet, void *userdata);
87 void *recieve_packet_callback_userdata;
88
89 void (*recieve_memblock_callback) (struct pa_pstream *p, uint32_t channel, uint32_t delta, const struct pa_memchunk *chunk, void *userdata);
90 void *recieve_memblock_callback_userdata;
91
92 void (*drain_callback)(struct pa_pstream *p, void *userdata);
93 void *drain_userdata;
94
95 struct pa_memblock_stat *memblock_stat;
96 };
97
98 static void do_write(struct pa_pstream *p);
99 static void do_read(struct pa_pstream *p);
100
101 static void do_something(struct pa_pstream *p) {
102 assert(p);
103 p->mainloop->defer_enable(p->defer_event, 0);
104
105 pa_pstream_ref(p);
106
107 if (!p->dead && pa_iochannel_is_hungup(p->io)) {
108 p->dead = 1;
109 if (p->die_callback)
110 p->die_callback(p, p->die_callback_userdata);
111 }
112
113 if (!p->dead && pa_iochannel_is_writable(p->io))
114 do_write(p);
115
116 if (!p->dead && pa_iochannel_is_readable(p->io))
117 do_read(p);
118
119 pa_pstream_unref(p);
120 }
121
122 static void io_callback(struct pa_iochannel*io, void *userdata) {
123 struct pa_pstream *p = userdata;
124 assert(p && p->io == io);
125 do_something(p);
126 }
127
128 static void defer_callback(struct pa_mainloop_api *m, struct pa_defer_event *e, void*userdata) {
129 struct pa_pstream *p = userdata;
130 assert(p && p->defer_event == e && p->mainloop == m);
131 do_something(p);
132 }
133
134 struct pa_pstream *pa_pstream_new(struct pa_mainloop_api *m, struct pa_iochannel *io, struct pa_memblock_stat *s) {
135 struct pa_pstream *p;
136 assert(io);
137
138 p = pa_xmalloc(sizeof(struct pa_pstream));
139 p->ref = 1;
140 p->io = io;
141 pa_iochannel_set_callback(io, io_callback, p);
142
143 p->dead = 0;
144 p->die_callback = NULL;
145 p->die_callback_userdata = NULL;
146
147 p->mainloop = m;
148 p->defer_event = m->defer_new(m, defer_callback, p);
149 m->defer_enable(p->defer_event, 0);
150
151 p->send_queue = pa_queue_new();
152 assert(p->send_queue);
153
154 p->write.current = NULL;
155 p->write.index = 0;
156
157 p->read.memblock = NULL;
158 p->read.packet = NULL;
159 p->read.index = 0;
160
161 p->recieve_packet_callback = NULL;
162 p->recieve_packet_callback_userdata = NULL;
163
164 p->recieve_memblock_callback = NULL;
165 p->recieve_memblock_callback_userdata = NULL;
166
167 p->drain_callback = NULL;
168 p->drain_userdata = NULL;
169
170 p->memblock_stat = s;
171
172 pa_iochannel_socket_set_rcvbuf(io, 1024*8);
173 pa_iochannel_socket_set_sndbuf(io, 1024*8);
174
175 return p;
176 }
177
178 static void item_free(void *item, void *p) {
179 struct item_info *i = item;
180 assert(i);
181
182 if (i->type == PA_PSTREAM_ITEM_MEMBLOCK) {
183 assert(i->chunk.memblock);
184 pa_memblock_unref(i->chunk.memblock);
185 } else {
186 assert(i->type == PA_PSTREAM_ITEM_PACKET);
187 assert(i->packet);
188 pa_packet_unref(i->packet);
189 }
190
191 pa_xfree(i);
192 }
193
194 static void pstream_free(struct pa_pstream *p) {
195 assert(p);
196
197 pa_pstream_close(p);
198
199 pa_queue_free(p->send_queue, item_free, NULL);
200
201 if (p->write.current)
202 item_free(p->write.current, NULL);
203
204 if (p->read.memblock)
205 pa_memblock_unref(p->read.memblock);
206
207 if (p->read.packet)
208 pa_packet_unref(p->read.packet);
209
210 pa_xfree(p);
211 }
212
213 void pa_pstream_send_packet(struct pa_pstream*p, struct pa_packet *packet) {
214 struct item_info *i;
215 assert(p && packet);
216
217 /*fprintf(stderr, "push-packet %p\n", packet);*/
218
219 i = pa_xmalloc(sizeof(struct item_info));
220 i->type = PA_PSTREAM_ITEM_PACKET;
221 i->packet = pa_packet_ref(packet);
222
223 pa_queue_push(p->send_queue, i);
224 p->mainloop->defer_enable(p->defer_event, 1);
225 }
226
227 void pa_pstream_send_memblock(struct pa_pstream*p, uint32_t channel, uint32_t delta, const struct pa_memchunk *chunk) {
228 struct item_info *i;
229 assert(p && channel != (uint32_t) -1 && chunk);
230
231 i = pa_xmalloc(sizeof(struct item_info));
232 i->type = PA_PSTREAM_ITEM_MEMBLOCK;
233 i->chunk = *chunk;
234 i->channel = channel;
235 i->delta = delta;
236
237 pa_memblock_ref(i->chunk.memblock);
238
239 pa_queue_push(p->send_queue, i);
240 p->mainloop->defer_enable(p->defer_event, 1);
241 }
242
243 void pa_pstream_set_recieve_packet_callback(struct pa_pstream *p, void (*callback) (struct pa_pstream *p, struct pa_packet *packet, void *userdata), void *userdata) {
244 assert(p && callback);
245
246 p->recieve_packet_callback = callback;
247 p->recieve_packet_callback_userdata = userdata;
248 }
249
250 void pa_pstream_set_recieve_memblock_callback(struct pa_pstream *p, void (*callback) (struct pa_pstream *p, uint32_t channel, uint32_t delta, const struct pa_memchunk *chunk, void *userdata), void *userdata) {
251 assert(p && callback);
252
253 p->recieve_memblock_callback = callback;
254 p->recieve_memblock_callback_userdata = userdata;
255 }
256
257 static void prepare_next_write_item(struct pa_pstream *p) {
258 assert(p);
259
260 if (!(p->write.current = pa_queue_pop(p->send_queue)))
261 return;
262
263 p->write.index = 0;
264
265 if (p->write.current->type == PA_PSTREAM_ITEM_PACKET) {
266 /*fprintf(stderr, "pop-packet %p\n", p->write.current->packet);*/
267
268 assert(p->write.current->packet);
269 p->write.data = p->write.current->packet->data;
270 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH] = htonl(p->write.current->packet->length);
271 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL] = htonl((uint32_t) -1);
272 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_DELTA] = 0;
273 } else {
274 assert(p->write.current->type == PA_PSTREAM_ITEM_MEMBLOCK && p->write.current->chunk.memblock);
275 p->write.data = p->write.current->chunk.memblock->data + p->write.current->chunk.index;
276 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH] = htonl(p->write.current->chunk.length);
277 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL] = htonl(p->write.current->channel);
278 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_DELTA] = htonl(p->write.current->delta);
279 }
280 }
281
282 static void do_write(struct pa_pstream *p) {
283 void *d;
284 size_t l;
285 ssize_t r;
286 assert(p);
287
288 if (!p->write.current)
289 prepare_next_write_item(p);
290
291 if (!p->write.current)
292 return;
293
294 assert(p->write.data);
295
296 if (p->write.index < PA_PSTREAM_DESCRIPTOR_SIZE) {
297 d = (void*) p->write.descriptor + p->write.index;
298 l = PA_PSTREAM_DESCRIPTOR_SIZE - p->write.index;
299 } else {
300 d = (void*) p->write.data + p->write.index - PA_PSTREAM_DESCRIPTOR_SIZE;
301 l = ntohl(p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) - (p->write.index - PA_PSTREAM_DESCRIPTOR_SIZE);
302 }
303
304 if ((r = pa_iochannel_write(p->io, d, l)) < 0)
305 goto die;
306
307 p->write.index += r;
308
309 if (p->write.index >= PA_PSTREAM_DESCRIPTOR_SIZE+ntohl(p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH])) {
310 assert(p->write.current);
311 item_free(p->write.current, (void *) 1);
312 p->write.current = NULL;
313
314 if (p->drain_callback && !pa_pstream_is_pending(p))
315 p->drain_callback(p, p->drain_userdata);
316 }
317
318 return;
319
320 die:
321 p->dead = 1;
322 if (p->die_callback)
323 p->die_callback(p, p->die_callback_userdata);
324 }
325
326 static void do_read(struct pa_pstream *p) {
327 void *d;
328 size_t l;
329 ssize_t r;
330 assert(p);
331
332 if (p->read.index < PA_PSTREAM_DESCRIPTOR_SIZE) {
333 d = (void*) p->read.descriptor + p->read.index;
334 l = PA_PSTREAM_DESCRIPTOR_SIZE - p->read.index;
335 } else {
336 assert(p->read.data);
337 d = (void*) p->read.data + p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE;
338 l = ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) - (p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE);
339 }
340
341 if ((r = pa_iochannel_read(p->io, d, l)) <= 0)
342 goto die;
343
344 p->read.index += r;
345
346 if (p->read.index == PA_PSTREAM_DESCRIPTOR_SIZE) {
347 /* Reading of frame descriptor complete */
348
349 /* Frame size too large */
350 if (ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) > FRAME_SIZE_MAX) {
351 fprintf(stderr, "frame size too large\n");
352 goto die;
353 }
354
355 assert(!p->read.packet && !p->read.memblock);
356
357 if (ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL]) == (uint32_t) -1) {
358 /* Frame is a packet frame */
359 p->read.packet = pa_packet_new(ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]));
360 assert(p->read.packet);
361 p->read.data = p->read.packet->data;
362 } else {
363 /* Frame is a memblock frame */
364 p->read.memblock = pa_memblock_new(ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]), p->memblock_stat);
365 assert(p->read.memblock);
366 p->read.data = p->read.memblock->data;
367 }
368
369 } else if (p->read.index > PA_PSTREAM_DESCRIPTOR_SIZE) {
370 /* Frame payload available */
371
372 if (p->read.memblock && p->recieve_memblock_callback) { /* Is this memblock data? Than pass it to the user */
373 size_t l;
374
375 l = (p->read.index - r) < PA_PSTREAM_DESCRIPTOR_SIZE ? p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE : (size_t) r;
376
377 if (l > 0) {
378 struct pa_memchunk chunk;
379
380 chunk.memblock = p->read.memblock;
381 chunk.index = p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE - l;
382 chunk.length = l;
383
384 if (p->recieve_memblock_callback)
385 p->recieve_memblock_callback(
386 p,
387 ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL]),
388 ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_DELTA]),
389 &chunk,
390 p->recieve_memblock_callback_userdata);
391 }
392 }
393
394 /* Frame complete */
395 if (p->read.index >= ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) + PA_PSTREAM_DESCRIPTOR_SIZE) {
396 if (p->read.memblock) {
397 assert(!p->read.packet);
398
399 pa_memblock_unref(p->read.memblock);
400 p->read.memblock = NULL;
401 } else {
402 assert(p->read.packet);
403
404 if (p->recieve_packet_callback)
405 p->recieve_packet_callback(p, p->read.packet, p->recieve_packet_callback_userdata);
406
407 pa_packet_unref(p->read.packet);
408 p->read.packet = NULL;
409 }
410
411 p->read.index = 0;
412 }
413 }
414
415 return;
416
417 die:
418 p->dead = 1;
419 if (p->die_callback)
420 p->die_callback(p, p->die_callback_userdata);
421
422 }
423
424 void pa_pstream_set_die_callback(struct pa_pstream *p, void (*callback)(struct pa_pstream *p, void *userdata), void *userdata) {
425 assert(p && callback);
426 p->die_callback = callback;
427 p->die_callback_userdata = userdata;
428 }
429
430 int pa_pstream_is_pending(struct pa_pstream *p) {
431 assert(p);
432
433 if (p->dead)
434 return 0;
435
436 return p->write.current || !pa_queue_is_empty(p->send_queue);
437 }
438
439 void pa_pstream_set_drain_callback(struct pa_pstream *p, void (*cb)(struct pa_pstream *p, void *userdata), void *userdata) {
440 assert(p);
441
442 p->drain_callback = cb;
443 p->drain_userdata = userdata;
444 }
445
446 void pa_pstream_unref(struct pa_pstream*p) {
447 assert(p && p->ref >= 1);
448
449 if (!(--(p->ref)))
450 pstream_free(p);
451 }
452
453 struct pa_pstream* pa_pstream_ref(struct pa_pstream*p) {
454 assert(p && p->ref >= 1);
455 p->ref++;
456 return p;
457 }
458
459 void pa_pstream_close(struct pa_pstream *p) {
460 assert(p);
461
462 p->dead = 1;
463
464 if (p->io) {
465 pa_iochannel_free(p->io);
466 p->io = NULL;
467 }
468
469 if (p->defer_event) {
470 p->mainloop->defer_free(p->defer_event);
471 p->defer_event = NULL;
472 }
473
474 p->die_callback = NULL;
475 p->drain_callback = NULL;
476 p->recieve_packet_callback = NULL;
477 p->recieve_memblock_callback = NULL;
478 }