4 This file is part of polypaudio.
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.
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.
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
31 #ifdef HAVE_NETINET_IN_H
32 #include <netinet/in.h>
37 #include <polypcore/queue.h>
38 #include <polypcore/xmalloc.h>
39 #include <polypcore/log.h>
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
52 typedef uint32_t pa_pstream_descriptor
[PA_PSTREAM_DESCRIPTOR_MAX
];
54 #define PA_PSTREAM_DESCRIPTOR_SIZE (PA_PSTREAM_DESCRIPTOR_MAX*sizeof(uint32_t))
55 #define FRAME_SIZE_MAX (1024*500) /* half a megabyte */
58 enum { PA_PSTREAM_ITEM_PACKET
, PA_PSTREAM_ITEM_MEMBLOCK
} type
;
64 pa_seek_mode_t seek_mode
;
68 #ifdef SCM_CREDENTIALS
76 pa_mainloop_api
*mainloop
;
77 pa_defer_event
*defer_event
;
84 struct item_info
* current
;
85 pa_pstream_descriptor descriptor
;
91 pa_memblock
*memblock
;
93 pa_pstream_descriptor descriptor
;
98 pa_pstream_packet_cb_t recieve_packet_callback
;
99 void *recieve_packet_callback_userdata
;
101 pa_pstream_memblock_cb_t recieve_memblock_callback
;
102 void *recieve_memblock_callback_userdata
;
104 pa_pstream_notify_cb_t drain_callback
;
105 void *drain_callback_userdata
;
107 pa_pstream_notify_cb_t die_callback
;
108 void *die_callback_userdata
;
110 pa_memblock_stat
*memblock_stat
;
112 #ifdef SCM_CREDENTIALS
119 static int do_write(pa_pstream
*p
);
120 static int do_read(pa_pstream
*p
);
122 static void do_something(pa_pstream
*p
) {
125 p
->mainloop
->defer_enable(p
->defer_event
, 0);
129 if (!p
->dead
&& pa_iochannel_is_readable(p
->io
)) {
132 } else if (!p
->dead
&& pa_iochannel_is_hungup(p
->io
))
135 if (!p
->dead
&& pa_iochannel_is_writable(p
->io
)) {
148 p
->die_callback(p
, p
->die_callback_userdata
);
153 static void io_callback(pa_iochannel
*io
, void *userdata
) {
154 pa_pstream
*p
= userdata
;
162 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void*userdata
) {
163 pa_pstream
*p
= userdata
;
166 assert(p
->defer_event
== e
);
167 assert(p
->mainloop
== m
);
172 pa_pstream
*pa_pstream_new(pa_mainloop_api
*m
, pa_iochannel
*io
, pa_memblock_stat
*s
) {
176 p
= pa_xnew(pa_pstream
, 1);
180 pa_iochannel_set_callback(io
, io_callback
, p
);
185 p
->defer_event
= m
->defer_new(m
, defer_callback
, p
);
186 m
->defer_enable(p
->defer_event
, 0);
188 p
->send_queue
= pa_queue_new();
189 assert(p
->send_queue
);
191 p
->write
.current
= NULL
;
194 p
->read
.memblock
= NULL
;
195 p
->read
.packet
= NULL
;
198 p
->recieve_packet_callback
= NULL
;
199 p
->recieve_packet_callback_userdata
= NULL
;
201 p
->recieve_memblock_callback
= NULL
;
202 p
->recieve_memblock_callback_userdata
= NULL
;
204 p
->drain_callback
= NULL
;
205 p
->drain_callback_userdata
= NULL
;
207 p
->die_callback
= NULL
;
208 p
->die_callback_userdata
= NULL
;
210 p
->memblock_stat
= s
;
212 pa_iochannel_socket_set_rcvbuf(io
, 1024*8);
213 pa_iochannel_socket_set_sndbuf(io
, 1024*8);
215 #ifdef SCM_CREDENTIALS
216 p
->send_creds_now
= 0;
222 static void item_free(void *item
, PA_GCC_UNUSED
void *p
) {
223 struct item_info
*i
= item
;
226 if (i
->type
== PA_PSTREAM_ITEM_MEMBLOCK
) {
227 assert(i
->chunk
.memblock
);
228 pa_memblock_unref(i
->chunk
.memblock
);
230 assert(i
->type
== PA_PSTREAM_ITEM_PACKET
);
232 pa_packet_unref(i
->packet
);
238 static void pstream_free(pa_pstream
*p
) {
243 pa_queue_free(p
->send_queue
, item_free
, NULL
);
245 if (p
->write
.current
)
246 item_free(p
->write
.current
, NULL
);
248 if (p
->read
.memblock
)
249 pa_memblock_unref(p
->read
.memblock
);
252 pa_packet_unref(p
->read
.packet
);
257 void pa_pstream_send_packet(pa_pstream
*p
, pa_packet
*packet
, int with_creds
) {
259 assert(p
&& packet
&& p
->ref
>= 1);
264 /* pa_log(__FILE__": push-packet %p", packet); */
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
;
273 pa_queue_push(p
->send_queue
, i
);
274 p
->mainloop
->defer_enable(p
->defer_event
, 1);
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
) {
279 assert(p
&& channel
!= (uint32_t) -1 && chunk
&& p
->ref
>= 1);
284 /* pa_log(__FILE__": push-memblock %p", chunk); */
286 i
= pa_xnew(struct item_info
, 1);
287 i
->type
= PA_PSTREAM_ITEM_MEMBLOCK
;
289 i
->channel
= channel
;
291 i
->seek_mode
= seek_mode
;
293 pa_memblock_ref(i
->chunk
.memblock
);
295 pa_queue_push(p
->send_queue
, i
);
296 p
->mainloop
->defer_enable(p
->defer_event
, 1);
299 static void prepare_next_write_item(pa_pstream
*p
) {
302 if (!(p
->write
.current
= pa_queue_pop(p
->send_queue
)))
307 if (p
->write
.current
->type
== PA_PSTREAM_ITEM_PACKET
) {
308 /*pa_log(__FILE__": pop-packet %p", p->write.current->packet);*/
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;
318 #ifdef SCM_CREDENTIALS
319 p
->send_creds_now
= 1;
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
);
331 #ifdef SCM_CREDENTIALS
332 p
->send_creds_now
= 1;
337 static int do_write(pa_pstream
*p
) {
343 if (!p
->write
.current
)
344 prepare_next_write_item(p
);
346 if (!p
->write
.current
)
349 assert(p
->write
.data
);
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
;
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
);
359 #ifdef SCM_CREDENTIALS
360 if (p
->send_creds_now
) {
362 if ((r
= pa_iochannel_write_with_creds(p
->io
, d
, l
)) < 0)
365 p
->send_creds_now
= 0;
369 if ((r
= pa_iochannel_write(p
->io
, d
, l
)) < 0)
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
;
379 if (p
->drain_callback
&& !pa_pstream_is_pending(p
))
380 p
->drain_callback(p
, p
->drain_callback_userdata
);
386 static int do_read(pa_pstream
*p
) {
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
;
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
);
401 #ifdef SCM_CREDENTIALS
405 if ((r
= pa_iochannel_read_with_creds(p
->io
, d
, l
, &p
->ucred
, &b
)) <= 0)
408 p
->creds_valid
= p
->creds_valid
|| b
;
411 if ((r
= pa_iochannel_read(p
->io
, d
, l
)) <= 0)
417 if (p
->read
.index
== PA_PSTREAM_DESCRIPTOR_SIZE
) {
418 /* Reading of frame descriptor complete */
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");
426 assert(!p
->read
.packet
&& !p
->read
.memblock
);
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
;
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
;
437 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
]) > PA_SEEK_RELATIVE_END
) {
438 pa_log_warn(__FILE__
": Invalid seek mode");
443 } else if (p
->read
.index
> PA_PSTREAM_DESCRIPTOR_SIZE
) {
444 /* Frame payload available */
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
;
452 chunk
.memblock
= p
->read
.memblock
;
453 chunk
.index
= p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
- l
;
456 if (p
->recieve_memblock_callback
) {
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
]))));
463 p
->recieve_memblock_callback(
465 ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
]),
467 ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
]),
469 p
->recieve_memblock_callback_userdata
);
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;
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
);
484 pa_memblock_unref(p
->read
.memblock
);
485 p
->read
.memblock
= NULL
;
487 assert(p
->read
.packet
);
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
);
493 p
->recieve_packet_callback(p
, p
->read
.packet
, NULL
, p
->recieve_packet_callback_userdata
);
496 pa_packet_unref(p
->read
.packet
);
497 p
->read
.packet
= NULL
;
501 #ifdef SCM_CREDENTIALS
510 void pa_pstream_set_die_callback(pa_pstream
*p
, pa_pstream_notify_cb_t cb
, void *userdata
) {
514 p
->die_callback
= cb
;
515 p
->die_callback_userdata
= userdata
;
519 void pa_pstream_set_drain_callback(pa_pstream
*p
, pa_pstream_notify_cb_t cb
, void *userdata
) {
523 p
->drain_callback
= cb
;
524 p
->drain_callback_userdata
= userdata
;
527 void pa_pstream_set_recieve_packet_callback(pa_pstream
*p
, pa_pstream_packet_cb_t cb
, void *userdata
) {
531 p
->recieve_packet_callback
= cb
;
532 p
->recieve_packet_callback_userdata
= userdata
;
535 void pa_pstream_set_recieve_memblock_callback(pa_pstream
*p
, pa_pstream_memblock_cb_t cb
, void *userdata
) {
539 p
->recieve_memblock_callback
= cb
;
540 p
->recieve_memblock_callback_userdata
= userdata
;
543 int pa_pstream_is_pending(pa_pstream
*p
) {
549 return p
->write
.current
|| !pa_queue_is_empty(p
->send_queue
);
552 void pa_pstream_unref(pa_pstream
*p
) {
560 pa_pstream
* pa_pstream_ref(pa_pstream
*p
) {
568 void pa_pstream_close(pa_pstream
*p
) {
574 pa_iochannel_free(p
->io
);
578 if (p
->defer_event
) {
579 p
->mainloop
->defer_free(p
->defer_event
);
580 p
->defer_event
= NULL
;
583 p
->die_callback
= NULL
;
584 p
->drain_callback
= NULL
;
585 p
->recieve_packet_callback
= NULL
;
586 p
->recieve_memblock_callback
= NULL
;