]> code.delx.au - pulseaudio/blob - polyp/pstream.c
e7441b24bcaebefbd7a03b3576fdb62a5cffe0b0
[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 int32_t delta;
54
55 /* packet info */
56 struct pa_packet *packet;
57 };
58
59 struct pa_pstream {
60 struct pa_mainloop_api *mainloop;
61 struct mainloop_source *mainloop_source;
62 struct pa_iochannel *io;
63 struct pa_queue *send_queue;
64
65 int in_use, shall_free;
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, int32_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
96 static void do_write(struct pa_pstream *p);
97 static void do_read(struct pa_pstream *p);
98
99 static void do_something(struct pa_pstream *p) {
100 assert(p && !p->shall_free);
101 p->mainloop->enable_fixed(p->mainloop, p->mainloop_source, 0);
102
103 if (p->dead)
104 return;
105
106 if (pa_iochannel_is_hungup(p->io)) {
107 p->dead = 1;
108 if (p->die_callback)
109 p->die_callback(p, p->die_callback_userdata);
110
111 return;
112 }
113
114 if (pa_iochannel_is_writable(p->io)) {
115 p->in_use = 1;
116 do_write(p);
117 p->in_use = 0;
118
119 if (p->shall_free) {
120 pa_pstream_free(p);
121 return;
122 }
123 }
124
125 if (pa_iochannel_is_readable(p->io)) {
126 p->in_use = 1;
127 do_read(p);
128 p->in_use = 0;
129 if (p->shall_free) {
130 pa_pstream_free(p);
131 return;
132 }
133 }
134 }
135
136 static void io_callback(struct pa_iochannel*io, void *userdata) {
137 struct pa_pstream *p = userdata;
138 assert(p && p->io == io);
139 do_something(p);
140 }
141
142 static void fixed_callback(struct pa_mainloop_api *m, void *id, void*userdata) {
143 struct pa_pstream *p = userdata;
144 assert(p && p->mainloop_source == id && p->mainloop == m);
145 do_something(p);
146 }
147
148 struct pa_pstream *pa_pstream_new(struct pa_mainloop_api *m, struct pa_iochannel *io) {
149 struct pa_pstream *p;
150 assert(io);
151
152 p = pa_xmalloc(sizeof(struct pa_pstream));
153
154 p->io = io;
155 pa_iochannel_set_callback(io, io_callback, p);
156
157 p->dead = 0;
158 p->die_callback = NULL;
159 p->die_callback_userdata = NULL;
160
161 p->mainloop = m;
162 p->mainloop_source = m->source_fixed(m, fixed_callback, p);
163 m->enable_fixed(m, p->mainloop_source, 0);
164
165 p->send_queue = pa_queue_new();
166 assert(p->send_queue);
167
168 p->write.current = NULL;
169 p->write.index = 0;
170
171 p->read.memblock = NULL;
172 p->read.packet = NULL;
173 p->read.index = 0;
174
175 p->recieve_packet_callback = NULL;
176 p->recieve_packet_callback_userdata = NULL;
177
178 p->recieve_memblock_callback = NULL;
179 p->recieve_memblock_callback_userdata = NULL;
180
181 p->drain_callback = NULL;
182 p->drain_userdata = NULL;
183
184 p->in_use = p->shall_free = 0;
185
186 return p;
187 }
188
189 static void item_free(void *item, void *p) {
190 struct item_info *i = item;
191 assert(i);
192
193 if (i->type == PA_PSTREAM_ITEM_MEMBLOCK) {
194 assert(i->chunk.memblock);
195 pa_memblock_unref(i->chunk.memblock);
196 } else {
197 assert(i->type == PA_PSTREAM_ITEM_PACKET);
198 assert(i->packet);
199 pa_packet_unref(i->packet);
200 }
201
202 pa_xfree(i);
203 }
204
205 void pa_pstream_free(struct pa_pstream *p) {
206 assert(p);
207
208 if (p->in_use) {
209 /* If this pstream object is used by someone else on the call stack, we have to postpone the freeing */
210 p->dead = p->shall_free = 1;
211 return;
212 }
213
214 pa_iochannel_free(p->io);
215 pa_queue_free(p->send_queue, item_free, NULL);
216
217 if (p->write.current)
218 item_free(p->write.current, NULL);
219
220 if (p->read.memblock)
221 pa_memblock_unref(p->read.memblock);
222
223 if (p->read.packet)
224 pa_packet_unref(p->read.packet);
225
226 p->mainloop->cancel_fixed(p->mainloop, p->mainloop_source);
227 pa_xfree(p);
228 }
229
230 void pa_pstream_send_packet(struct pa_pstream*p, struct pa_packet *packet) {
231 struct item_info *i;
232 assert(p && packet);
233
234 i = pa_xmalloc(sizeof(struct item_info));
235 i->type = PA_PSTREAM_ITEM_PACKET;
236 i->packet = pa_packet_ref(packet);
237
238 pa_queue_push(p->send_queue, i);
239 p->mainloop->enable_fixed(p->mainloop, p->mainloop_source, 1);
240 }
241
242 void pa_pstream_send_memblock(struct pa_pstream*p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk) {
243 struct item_info *i;
244 assert(p && channel != (uint32_t) -1 && chunk);
245
246 i = pa_xmalloc(sizeof(struct item_info));
247 i->type = PA_PSTREAM_ITEM_MEMBLOCK;
248 i->chunk = *chunk;
249 i->channel = channel;
250 i->delta = delta;
251
252 pa_memblock_ref(i->chunk.memblock);
253
254 pa_queue_push(p->send_queue, i);
255 p->mainloop->enable_fixed(p->mainloop, p->mainloop_source, 1);
256 }
257
258 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) {
259 assert(p && callback);
260
261 p->recieve_packet_callback = callback;
262 p->recieve_packet_callback_userdata = userdata;
263 }
264
265 void pa_pstream_set_recieve_memblock_callback(struct pa_pstream *p, void (*callback) (struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata), void *userdata) {
266 assert(p && callback);
267
268 p->recieve_memblock_callback = callback;
269 p->recieve_memblock_callback_userdata = userdata;
270 }
271
272 static void prepare_next_write_item(struct pa_pstream *p) {
273 assert(p);
274
275 if (!(p->write.current = pa_queue_pop(p->send_queue)))
276 return;
277
278 p->write.index = 0;
279
280 if (p->write.current->type == PA_PSTREAM_ITEM_PACKET) {
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 = 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 = (void*) p->write.descriptor + p->write.index;
311 l = PA_PSTREAM_DESCRIPTOR_SIZE - p->write.index;
312 } else {
313 d = (void*) 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 = (void*) p->read.descriptor + p->read.index;
347 l = PA_PSTREAM_DESCRIPTOR_SIZE - p->read.index;
348 } else {
349 assert(p->read.data);
350 d = (void*) 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 fprintf(stderr, "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]));
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 (int32_t) 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