]> code.delx.au - pulseaudio/blob - src/polypcore/pstream.c
when loading sound files, initialize channel map data properly
[pulseaudio] / src / polypcore / 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 Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, 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 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License 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 <unistd.h>
30
31 #ifdef HAVE_NETINET_IN_H
32 #include <netinet/in.h>
33 #endif
34
35 #include "winsock.h"
36
37 #include <polypcore/queue.h>
38 #include <polypcore/xmalloc.h>
39 #include <polypcore/log.h>
40
41 #include "pstream.h"
42
43 enum {
44 PA_PSTREAM_DESCRIPTOR_LENGTH,
45 PA_PSTREAM_DESCRIPTOR_CHANNEL,
46 PA_PSTREAM_DESCRIPTOR_OFFSET_HI,
47 PA_PSTREAM_DESCRIPTOR_OFFSET_LO,
48 PA_PSTREAM_DESCRIPTOR_SEEK,
49 PA_PSTREAM_DESCRIPTOR_MAX
50 };
51
52 typedef uint32_t pa_pstream_descriptor[PA_PSTREAM_DESCRIPTOR_MAX];
53
54 #define PA_PSTREAM_DESCRIPTOR_SIZE (PA_PSTREAM_DESCRIPTOR_MAX*sizeof(uint32_t))
55 #define FRAME_SIZE_MAX (1024*500) /* half a megabyte */
56
57 struct item_info {
58 enum { PA_PSTREAM_ITEM_PACKET, PA_PSTREAM_ITEM_MEMBLOCK } type;
59
60 /* memblock info */
61 pa_memchunk chunk;
62 uint32_t channel;
63 int64_t offset;
64 pa_seek_mode_t seek_mode;
65
66 /* packet info */
67 pa_packet *packet;
68 #ifdef SCM_CREDENTIALS
69 int with_creds;
70 #endif
71 };
72
73 struct pa_pstream {
74 int ref;
75
76 pa_mainloop_api *mainloop;
77 pa_defer_event *defer_event;
78 pa_iochannel *io;
79 pa_queue *send_queue;
80
81 int dead;
82
83 struct {
84 struct item_info* current;
85 pa_pstream_descriptor descriptor;
86 void *data;
87 size_t index;
88 } write;
89
90 struct {
91 pa_memblock *memblock;
92 pa_packet *packet;
93 pa_pstream_descriptor descriptor;
94 void *data;
95 size_t index;
96 } read;
97
98 pa_pstream_packet_cb_t recieve_packet_callback;
99 void *recieve_packet_callback_userdata;
100
101 pa_pstream_memblock_cb_t recieve_memblock_callback;
102 void *recieve_memblock_callback_userdata;
103
104 pa_pstream_notify_cb_t drain_callback;
105 void *drain_callback_userdata;
106
107 pa_pstream_notify_cb_t die_callback;
108 void *die_callback_userdata;
109
110 pa_memblock_stat *memblock_stat;
111
112 #ifdef SCM_CREDENTIALS
113 int send_creds_now;
114 struct ucred ucred;
115 int creds_valid;
116 #endif
117 };
118
119 static int do_write(pa_pstream *p);
120 static int do_read(pa_pstream *p);
121
122 static void do_something(pa_pstream *p) {
123 assert(p);
124
125 p->mainloop->defer_enable(p->defer_event, 0);
126
127 pa_pstream_ref(p);
128
129 if (!p->dead && pa_iochannel_is_readable(p->io)) {
130 if (do_read(p) < 0)
131 goto fail;
132 } else if (!p->dead && pa_iochannel_is_hungup(p->io))
133 goto fail;
134
135 if (!p->dead && pa_iochannel_is_writable(p->io)) {
136 if (do_write(p) < 0)
137 goto fail;
138 }
139
140 pa_pstream_unref(p);
141 return;
142
143 fail:
144
145 p->dead = 1;
146
147 if (p->die_callback)
148 p->die_callback(p, p->die_callback_userdata);
149
150 pa_pstream_unref(p);
151 }
152
153 static void io_callback(pa_iochannel*io, void *userdata) {
154 pa_pstream *p = userdata;
155
156 assert(p);
157 assert(p->io == io);
158
159 do_something(p);
160 }
161
162 static void defer_callback(pa_mainloop_api *m, pa_defer_event *e, void*userdata) {
163 pa_pstream *p = userdata;
164
165 assert(p);
166 assert(p->defer_event == e);
167 assert(p->mainloop == m);
168
169 do_something(p);
170 }
171
172 pa_pstream *pa_pstream_new(pa_mainloop_api *m, pa_iochannel *io, pa_memblock_stat *s) {
173 pa_pstream *p;
174 assert(io);
175
176 p = pa_xnew(pa_pstream, 1);
177
178 p->ref = 1;
179 p->io = io;
180 pa_iochannel_set_callback(io, io_callback, p);
181
182 p->dead = 0;
183
184 p->mainloop = m;
185 p->defer_event = m->defer_new(m, defer_callback, p);
186 m->defer_enable(p->defer_event, 0);
187
188 p->send_queue = pa_queue_new();
189 assert(p->send_queue);
190
191 p->write.current = NULL;
192 p->write.index = 0;
193
194 p->read.memblock = NULL;
195 p->read.packet = NULL;
196 p->read.index = 0;
197
198 p->recieve_packet_callback = NULL;
199 p->recieve_packet_callback_userdata = NULL;
200
201 p->recieve_memblock_callback = NULL;
202 p->recieve_memblock_callback_userdata = NULL;
203
204 p->drain_callback = NULL;
205 p->drain_callback_userdata = NULL;
206
207 p->die_callback = NULL;
208 p->die_callback_userdata = NULL;
209
210 p->memblock_stat = s;
211
212 pa_iochannel_socket_set_rcvbuf(io, 1024*8);
213 pa_iochannel_socket_set_sndbuf(io, 1024*8);
214
215 #ifdef SCM_CREDENTIALS
216 p->send_creds_now = 0;
217 p->creds_valid = 0;
218 #endif
219 return p;
220 }
221
222 static void item_free(void *item, PA_GCC_UNUSED void *p) {
223 struct item_info *i = item;
224 assert(i);
225
226 if (i->type == PA_PSTREAM_ITEM_MEMBLOCK) {
227 assert(i->chunk.memblock);
228 pa_memblock_unref(i->chunk.memblock);
229 } else {
230 assert(i->type == PA_PSTREAM_ITEM_PACKET);
231 assert(i->packet);
232 pa_packet_unref(i->packet);
233 }
234
235 pa_xfree(i);
236 }
237
238 static void pstream_free(pa_pstream *p) {
239 assert(p);
240
241 pa_pstream_close(p);
242
243 pa_queue_free(p->send_queue, item_free, NULL);
244
245 if (p->write.current)
246 item_free(p->write.current, NULL);
247
248 if (p->read.memblock)
249 pa_memblock_unref(p->read.memblock);
250
251 if (p->read.packet)
252 pa_packet_unref(p->read.packet);
253
254 pa_xfree(p);
255 }
256
257 void pa_pstream_send_packet(pa_pstream*p, pa_packet *packet, int with_creds) {
258 struct item_info *i;
259 assert(p && packet && p->ref >= 1);
260
261 if (p->dead)
262 return;
263
264 /* pa_log(__FILE__": push-packet %p", packet); */
265
266 i = pa_xnew(struct item_info, 1);
267 i->type = PA_PSTREAM_ITEM_PACKET;
268 i->packet = pa_packet_ref(packet);
269 #ifdef SCM_CREDENTIALS
270 i->with_creds = with_creds;
271 #endif
272
273 pa_queue_push(p->send_queue, i);
274 p->mainloop->defer_enable(p->defer_event, 1);
275 }
276
277 void pa_pstream_send_memblock(pa_pstream*p, uint32_t channel, int64_t offset, pa_seek_mode_t seek_mode, const pa_memchunk *chunk) {
278 struct item_info *i;
279 assert(p && channel != (uint32_t) -1 && chunk && p->ref >= 1);
280
281 if (p->dead)
282 return;
283
284 /* pa_log(__FILE__": push-memblock %p", chunk); */
285
286 i = pa_xnew(struct item_info, 1);
287 i->type = PA_PSTREAM_ITEM_MEMBLOCK;
288 i->chunk = *chunk;
289 i->channel = channel;
290 i->offset = offset;
291 i->seek_mode = seek_mode;
292
293 pa_memblock_ref(i->chunk.memblock);
294
295 pa_queue_push(p->send_queue, i);
296 p->mainloop->defer_enable(p->defer_event, 1);
297 }
298
299 static void prepare_next_write_item(pa_pstream *p) {
300 assert(p);
301
302 if (!(p->write.current = pa_queue_pop(p->send_queue)))
303 return;
304
305 p->write.index = 0;
306
307 if (p->write.current->type == PA_PSTREAM_ITEM_PACKET) {
308 /*pa_log(__FILE__": pop-packet %p", p->write.current->packet);*/
309
310 assert(p->write.current->packet);
311 p->write.data = p->write.current->packet->data;
312 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH] = htonl(p->write.current->packet->length);
313 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL] = htonl((uint32_t) -1);
314 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_HI] = 0;
315 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_LO] = 0;
316 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_SEEK] = 0;
317
318 #ifdef SCM_CREDENTIALS
319 p->send_creds_now = 1;
320 #endif
321
322 } else {
323 assert(p->write.current->type == PA_PSTREAM_ITEM_MEMBLOCK && p->write.current->chunk.memblock);
324 p->write.data = (uint8_t*) p->write.current->chunk.memblock->data + p->write.current->chunk.index;
325 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH] = htonl(p->write.current->chunk.length);
326 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL] = htonl(p->write.current->channel);
327 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_HI] = htonl((uint32_t) (((uint64_t) p->write.current->offset) >> 32));
328 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_LO] = htonl((uint32_t) ((uint64_t) p->write.current->offset));
329 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_SEEK] = htonl(p->write.current->seek_mode);
330
331 #ifdef SCM_CREDENTIALS
332 p->send_creds_now = 1;
333 #endif
334 }
335 }
336
337 static int do_write(pa_pstream *p) {
338 void *d;
339 size_t l;
340 ssize_t r;
341 assert(p);
342
343 if (!p->write.current)
344 prepare_next_write_item(p);
345
346 if (!p->write.current)
347 return 0;
348
349 assert(p->write.data);
350
351 if (p->write.index < PA_PSTREAM_DESCRIPTOR_SIZE) {
352 d = (uint8_t*) p->write.descriptor + p->write.index;
353 l = PA_PSTREAM_DESCRIPTOR_SIZE - p->write.index;
354 } else {
355 d = (uint8_t*) p->write.data + p->write.index - PA_PSTREAM_DESCRIPTOR_SIZE;
356 l = ntohl(p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) - (p->write.index - PA_PSTREAM_DESCRIPTOR_SIZE);
357 }
358
359 #ifdef SCM_CREDENTIALS
360 if (p->send_creds_now) {
361
362 if ((r = pa_iochannel_write_with_creds(p->io, d, l)) < 0)
363 return -1;
364
365 p->send_creds_now = 0;
366 } else
367 #endif
368
369 if ((r = pa_iochannel_write(p->io, d, l)) < 0)
370 return -1;
371
372 p->write.index += r;
373
374 if (p->write.index >= PA_PSTREAM_DESCRIPTOR_SIZE+ntohl(p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH])) {
375 assert(p->write.current);
376 item_free(p->write.current, (void *) 1);
377 p->write.current = NULL;
378
379 if (p->drain_callback && !pa_pstream_is_pending(p))
380 p->drain_callback(p, p->drain_callback_userdata);
381 }
382
383 return 0;
384 }
385
386 static int do_read(pa_pstream *p) {
387 void *d;
388 size_t l;
389 ssize_t r;
390 assert(p);
391
392 if (p->read.index < PA_PSTREAM_DESCRIPTOR_SIZE) {
393 d = (uint8_t*) p->read.descriptor + p->read.index;
394 l = PA_PSTREAM_DESCRIPTOR_SIZE - p->read.index;
395 } else {
396 assert(p->read.data);
397 d = (uint8_t*) p->read.data + p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE;
398 l = ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) - (p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE);
399 }
400
401 #ifdef SCM_CREDENTIALS
402 {
403 int b;
404
405 if ((r = pa_iochannel_read_with_creds(p->io, d, l, &p->ucred, &b)) <= 0)
406 return -1;
407
408 p->creds_valid = p->creds_valid || b;
409 }
410 #else
411 if ((r = pa_iochannel_read(p->io, d, l)) <= 0)
412 return -1;
413 #endif
414
415 p->read.index += r;
416
417 if (p->read.index == PA_PSTREAM_DESCRIPTOR_SIZE) {
418 /* Reading of frame descriptor complete */
419
420 /* Frame size too large */
421 if (ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) > FRAME_SIZE_MAX) {
422 pa_log_warn(__FILE__": Frame size too large");
423 return -1;
424 }
425
426 assert(!p->read.packet && !p->read.memblock);
427
428 if (ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL]) == (uint32_t) -1) {
429 /* Frame is a packet frame */
430 p->read.packet = pa_packet_new(ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]));
431 p->read.data = p->read.packet->data;
432 } else {
433 /* Frame is a memblock frame */
434 p->read.memblock = pa_memblock_new(ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]), p->memblock_stat);
435 p->read.data = p->read.memblock->data;
436
437 if (ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_SEEK]) > PA_SEEK_RELATIVE_END) {
438 pa_log_warn(__FILE__": Invalid seek mode");
439 return -1;
440 }
441 }
442
443 } else if (p->read.index > PA_PSTREAM_DESCRIPTOR_SIZE) {
444 /* Frame payload available */
445
446 if (p->read.memblock && p->recieve_memblock_callback) { /* Is this memblock data? Than pass it to the user */
447 l = (p->read.index - r) < PA_PSTREAM_DESCRIPTOR_SIZE ? p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE : (size_t) r;
448
449 if (l > 0) {
450 pa_memchunk chunk;
451
452 chunk.memblock = p->read.memblock;
453 chunk.index = p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE - l;
454 chunk.length = l;
455
456 if (p->recieve_memblock_callback) {
457 int64_t offset;
458
459 offset = (int64_t) (
460 (((uint64_t) ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_HI])) << 32) |
461 (((uint64_t) ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_LO]))));
462
463 p->recieve_memblock_callback(
464 p,
465 ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL]),
466 offset,
467 ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_SEEK]),
468 &chunk,
469 p->recieve_memblock_callback_userdata);
470 }
471
472 /* Drop seek info for following callbacks */
473 p->read.descriptor[PA_PSTREAM_DESCRIPTOR_SEEK] =
474 p->read.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_HI] =
475 p->read.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_LO] = 0;
476 }
477 }
478
479 /* Frame complete */
480 if (p->read.index >= ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) + PA_PSTREAM_DESCRIPTOR_SIZE) {
481 if (p->read.memblock) {
482 assert(!p->read.packet);
483
484 pa_memblock_unref(p->read.memblock);
485 p->read.memblock = NULL;
486 } else {
487 assert(p->read.packet);
488
489 if (p->recieve_packet_callback)
490 #ifdef SCM_CREDENTIALS
491 p->recieve_packet_callback(p, p->read.packet, p->creds_valid ? &p->ucred : NULL, p->recieve_packet_callback_userdata);
492 #else
493 p->recieve_packet_callback(p, p->read.packet, NULL, p->recieve_packet_callback_userdata);
494 #endif
495
496 pa_packet_unref(p->read.packet);
497 p->read.packet = NULL;
498 }
499
500 p->read.index = 0;
501 #ifdef SCM_CREDENTIALS
502 p->creds_valid = 0;
503 #endif
504 }
505 }
506
507 return 0;
508 }
509
510 void pa_pstream_set_die_callback(pa_pstream *p, pa_pstream_notify_cb_t cb, void *userdata) {
511 assert(p);
512 assert(p->ref >= 1);
513
514 p->die_callback = cb;
515 p->die_callback_userdata = userdata;
516 }
517
518
519 void pa_pstream_set_drain_callback(pa_pstream *p, pa_pstream_notify_cb_t cb, void *userdata) {
520 assert(p);
521 assert(p->ref >= 1);
522
523 p->drain_callback = cb;
524 p->drain_callback_userdata = userdata;
525 }
526
527 void pa_pstream_set_recieve_packet_callback(pa_pstream *p, pa_pstream_packet_cb_t cb, void *userdata) {
528 assert(p);
529 assert(p->ref >= 1);
530
531 p->recieve_packet_callback = cb;
532 p->recieve_packet_callback_userdata = userdata;
533 }
534
535 void pa_pstream_set_recieve_memblock_callback(pa_pstream *p, pa_pstream_memblock_cb_t cb, void *userdata) {
536 assert(p);
537 assert(p->ref >= 1);
538
539 p->recieve_memblock_callback = cb;
540 p->recieve_memblock_callback_userdata = userdata;
541 }
542
543 int pa_pstream_is_pending(pa_pstream *p) {
544 assert(p);
545
546 if (p->dead)
547 return 0;
548
549 return p->write.current || !pa_queue_is_empty(p->send_queue);
550 }
551
552 void pa_pstream_unref(pa_pstream*p) {
553 assert(p);
554 assert(p->ref >= 1);
555
556 if (--p->ref == 0)
557 pstream_free(p);
558 }
559
560 pa_pstream* pa_pstream_ref(pa_pstream*p) {
561 assert(p);
562 assert(p->ref >= 1);
563
564 p->ref++;
565 return p;
566 }
567
568 void pa_pstream_close(pa_pstream *p) {
569 assert(p);
570
571 p->dead = 1;
572
573 if (p->io) {
574 pa_iochannel_free(p->io);
575 p->io = NULL;
576 }
577
578 if (p->defer_event) {
579 p->mainloop->defer_free(p->defer_event);
580 p->defer_event = NULL;
581 }
582
583 p->die_callback = NULL;
584 p->drain_callback = NULL;
585 p->recieve_packet_callback = NULL;
586 p->recieve_memblock_callback = NULL;
587 }