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