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