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