4 This file is part of PulseAudio.
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.
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.
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
31 #ifdef HAVE_NETINET_IN_H
32 #include <netinet/in.h>
37 #include <pulse/xmalloc.h>
39 #include <pulsecore/queue.h>
40 #include <pulsecore/log.h>
41 #include <pulsecore/core-scache.h>
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
54 typedef uint32_t pa_pstream_descriptor
[PA_PSTREAM_DESCRIPTOR_MAX
];
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 */
60 enum { PA_PSTREAM_ITEM_PACKET
, PA_PSTREAM_ITEM_MEMBLOCK
} type
;
66 pa_seek_mode_t seek_mode
;
70 #ifdef SCM_CREDENTIALS
78 pa_mainloop_api
*mainloop
;
79 pa_defer_event
*defer_event
;
86 struct item_info
* current
;
87 pa_pstream_descriptor descriptor
;
93 pa_memblock
*memblock
;
95 pa_pstream_descriptor descriptor
;
100 pa_pstream_packet_cb_t recieve_packet_callback
;
101 void *recieve_packet_callback_userdata
;
103 pa_pstream_memblock_cb_t recieve_memblock_callback
;
104 void *recieve_memblock_callback_userdata
;
106 pa_pstream_notify_cb_t drain_callback
;
107 void *drain_callback_userdata
;
109 pa_pstream_notify_cb_t die_callback
;
110 void *die_callback_userdata
;
112 pa_memblock_stat
*memblock_stat
;
114 #ifdef SCM_CREDENTIALS
121 static int do_write(pa_pstream
*p
);
122 static int do_read(pa_pstream
*p
);
124 static void do_something(pa_pstream
*p
) {
127 p
->mainloop
->defer_enable(p
->defer_event
, 0);
131 if (!p
->dead
&& pa_iochannel_is_readable(p
->io
)) {
134 } else if (!p
->dead
&& pa_iochannel_is_hungup(p
->io
))
137 if (!p
->dead
&& pa_iochannel_is_writable(p
->io
)) {
150 p
->die_callback(p
, p
->die_callback_userdata
);
155 static void io_callback(pa_iochannel
*io
, void *userdata
) {
156 pa_pstream
*p
= userdata
;
164 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void*userdata
) {
165 pa_pstream
*p
= userdata
;
168 assert(p
->defer_event
== e
);
169 assert(p
->mainloop
== m
);
174 pa_pstream
*pa_pstream_new(pa_mainloop_api
*m
, pa_iochannel
*io
, pa_memblock_stat
*s
) {
178 p
= pa_xnew(pa_pstream
, 1);
182 pa_iochannel_set_callback(io
, io_callback
, p
);
187 p
->defer_event
= m
->defer_new(m
, defer_callback
, p
);
188 m
->defer_enable(p
->defer_event
, 0);
190 p
->send_queue
= pa_queue_new();
191 assert(p
->send_queue
);
193 p
->write
.current
= NULL
;
196 p
->read
.memblock
= NULL
;
197 p
->read
.packet
= NULL
;
200 p
->recieve_packet_callback
= NULL
;
201 p
->recieve_packet_callback_userdata
= NULL
;
203 p
->recieve_memblock_callback
= NULL
;
204 p
->recieve_memblock_callback_userdata
= NULL
;
206 p
->drain_callback
= NULL
;
207 p
->drain_callback_userdata
= NULL
;
209 p
->die_callback
= NULL
;
210 p
->die_callback_userdata
= NULL
;
212 p
->memblock_stat
= s
;
214 pa_iochannel_socket_set_rcvbuf(io
, 1024*8);
215 pa_iochannel_socket_set_sndbuf(io
, 1024*8);
217 #ifdef SCM_CREDENTIALS
218 p
->send_creds_now
= 0;
224 static void item_free(void *item
, PA_GCC_UNUSED
void *p
) {
225 struct item_info
*i
= item
;
228 if (i
->type
== PA_PSTREAM_ITEM_MEMBLOCK
) {
229 assert(i
->chunk
.memblock
);
230 pa_memblock_unref(i
->chunk
.memblock
);
232 assert(i
->type
== PA_PSTREAM_ITEM_PACKET
);
234 pa_packet_unref(i
->packet
);
240 static void pstream_free(pa_pstream
*p
) {
245 pa_queue_free(p
->send_queue
, item_free
, NULL
);
247 if (p
->write
.current
)
248 item_free(p
->write
.current
, NULL
);
250 if (p
->read
.memblock
)
251 pa_memblock_unref(p
->read
.memblock
);
254 pa_packet_unref(p
->read
.packet
);
259 void pa_pstream_send_packet(pa_pstream
*p
, pa_packet
*packet
, int with_creds
) {
261 assert(p
&& packet
&& p
->ref
>= 1);
266 /* pa_log(__FILE__": push-packet %p", packet); */
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
;
275 pa_queue_push(p
->send_queue
, i
);
276 p
->mainloop
->defer_enable(p
->defer_event
, 1);
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
) {
281 assert(p
&& channel
!= (uint32_t) -1 && chunk
&& p
->ref
>= 1);
286 /* pa_log(__FILE__": push-memblock %p", chunk); */
288 i
= pa_xnew(struct item_info
, 1);
289 i
->type
= PA_PSTREAM_ITEM_MEMBLOCK
;
291 i
->channel
= channel
;
293 i
->seek_mode
= seek_mode
;
296 pa_memblock_ref(i
->chunk
.memblock
);
298 pa_queue_push(p
->send_queue
, i
);
299 p
->mainloop
->defer_enable(p
->defer_event
, 1);
302 static void prepare_next_write_item(pa_pstream
*p
) {
305 if (!(p
->write
.current
= pa_queue_pop(p
->send_queue
)))
310 if (p
->write
.current
->type
== PA_PSTREAM_ITEM_PACKET
) {
311 /*pa_log(__FILE__": pop-packet %p", p->write.current->packet);*/
313 assert(p
->write
.current
->packet
);
314 p
->write
.data
= p
->write
.current
->packet
->data
;
315 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
] = htonl(p
->write
.current
->packet
->length
);
316 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
] = htonl((uint32_t) -1);
317 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_HI
] = 0;
318 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_LO
] = 0;
319 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
] = 0;
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
);
332 #ifdef SCM_CREDENTIALS
333 p
->send_creds_now
= p
->write
.current
->with_creds
;
338 static int do_write(pa_pstream
*p
) {
344 if (!p
->write
.current
)
345 prepare_next_write_item(p
);
347 if (!p
->write
.current
)
350 assert(p
->write
.data
);
352 if (p
->write
.index
< PA_PSTREAM_DESCRIPTOR_SIZE
) {
353 d
= (uint8_t*) p
->write
.descriptor
+ p
->write
.index
;
354 l
= PA_PSTREAM_DESCRIPTOR_SIZE
- p
->write
.index
;
356 d
= (uint8_t*) p
->write
.data
+ p
->write
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
;
357 l
= ntohl(p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) - (p
->write
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
);
360 #ifdef SCM_CREDENTIALS
361 if (p
->send_creds_now
) {
363 if ((r
= pa_iochannel_write_with_creds(p
->io
, d
, l
)) < 0)
366 p
->send_creds_now
= 0;
370 if ((r
= pa_iochannel_write(p
->io
, d
, l
)) < 0)
375 if (p
->write
.index
>= PA_PSTREAM_DESCRIPTOR_SIZE
+ntohl(p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
])) {
376 assert(p
->write
.current
);
377 item_free(p
->write
.current
, (void *) 1);
378 p
->write
.current
= NULL
;
380 if (p
->drain_callback
&& !pa_pstream_is_pending(p
))
381 p
->drain_callback(p
, p
->drain_callback_userdata
);
387 static int do_read(pa_pstream
*p
) {
393 if (p
->read
.index
< PA_PSTREAM_DESCRIPTOR_SIZE
) {
394 d
= (uint8_t*) p
->read
.descriptor
+ p
->read
.index
;
395 l
= PA_PSTREAM_DESCRIPTOR_SIZE
- p
->read
.index
;
397 assert(p
->read
.data
);
398 d
= (uint8_t*) p
->read
.data
+ p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
;
399 l
= ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) - (p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
);
402 #ifdef SCM_CREDENTIALS
406 if ((r
= pa_iochannel_read_with_creds(p
->io
, d
, l
, &p
->ucred
, &b
)) <= 0)
409 p
->creds_valid
= p
->creds_valid
|| b
;
412 if ((r
= pa_iochannel_read(p
->io
, d
, l
)) <= 0)
418 if (p
->read
.index
== PA_PSTREAM_DESCRIPTOR_SIZE
) {
419 /* Reading of frame descriptor complete */
421 /* Frame size too large */
422 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) > FRAME_SIZE_MAX
) {
423 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
);
427 assert(!p
->read
.packet
&& !p
->read
.memblock
);
429 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
]) == (uint32_t) -1) {
430 /* Frame is a packet frame */
431 p
->read
.packet
= pa_packet_new(ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]));
432 p
->read
.data
= p
->read
.packet
->data
;
434 /* Frame is a memblock frame */
435 p
->read
.memblock
= pa_memblock_new(ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]), p
->memblock_stat
);
436 p
->read
.data
= p
->read
.memblock
->data
;
438 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
]) > PA_SEEK_RELATIVE_END
) {
439 pa_log_warn(__FILE__
": Invalid seek mode");
444 } else if (p
->read
.index
> PA_PSTREAM_DESCRIPTOR_SIZE
) {
445 /* Frame payload available */
447 if (p
->read
.memblock
&& p
->recieve_memblock_callback
) { /* Is this memblock data? Than pass it to the user */
448 l
= (p
->read
.index
- r
) < PA_PSTREAM_DESCRIPTOR_SIZE
? p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
: (size_t) r
;
453 chunk
.memblock
= p
->read
.memblock
;
454 chunk
.index
= p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
- l
;
457 if (p
->recieve_memblock_callback
) {
461 (((uint64_t) ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_HI
])) << 32) |
462 (((uint64_t) ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_LO
]))));
464 p
->recieve_memblock_callback(
466 ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
]),
468 ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
]),
470 p
->recieve_memblock_callback_userdata
);
473 /* Drop seek info for following callbacks */
474 p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
] =
475 p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_HI
] =
476 p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_LO
] = 0;
481 if (p
->read
.index
>= ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) + PA_PSTREAM_DESCRIPTOR_SIZE
) {
482 if (p
->read
.memblock
) {
483 assert(!p
->read
.packet
);
485 pa_memblock_unref(p
->read
.memblock
);
486 p
->read
.memblock
= NULL
;
488 assert(p
->read
.packet
);
490 if (p
->recieve_packet_callback
)
491 #ifdef SCM_CREDENTIALS
492 p
->recieve_packet_callback(p
, p
->read
.packet
, p
->creds_valid
? &p
->ucred
: NULL
, p
->recieve_packet_callback_userdata
);
494 p
->recieve_packet_callback(p
, p
->read
.packet
, NULL
, p
->recieve_packet_callback_userdata
);
497 pa_packet_unref(p
->read
.packet
);
498 p
->read
.packet
= NULL
;
502 #ifdef SCM_CREDENTIALS
511 void pa_pstream_set_die_callback(pa_pstream
*p
, pa_pstream_notify_cb_t cb
, void *userdata
) {
515 p
->die_callback
= cb
;
516 p
->die_callback_userdata
= userdata
;
520 void pa_pstream_set_drain_callback(pa_pstream
*p
, pa_pstream_notify_cb_t cb
, void *userdata
) {
524 p
->drain_callback
= cb
;
525 p
->drain_callback_userdata
= userdata
;
528 void pa_pstream_set_recieve_packet_callback(pa_pstream
*p
, pa_pstream_packet_cb_t cb
, void *userdata
) {
532 p
->recieve_packet_callback
= cb
;
533 p
->recieve_packet_callback_userdata
= userdata
;
536 void pa_pstream_set_recieve_memblock_callback(pa_pstream
*p
, pa_pstream_memblock_cb_t cb
, void *userdata
) {
540 p
->recieve_memblock_callback
= cb
;
541 p
->recieve_memblock_callback_userdata
= userdata
;
544 int pa_pstream_is_pending(pa_pstream
*p
) {
550 return p
->write
.current
|| !pa_queue_is_empty(p
->send_queue
);
553 void pa_pstream_unref(pa_pstream
*p
) {
561 pa_pstream
* pa_pstream_ref(pa_pstream
*p
) {
569 void pa_pstream_close(pa_pstream
*p
) {
575 pa_iochannel_free(p
->io
);
579 if (p
->defer_event
) {
580 p
->mainloop
->defer_free(p
->defer_event
);
581 p
->defer_event
= NULL
;
584 p
->die_callback
= NULL
;
585 p
->drain_callback
= NULL
;
586 p
->recieve_packet_callback
= NULL
;
587 p
->recieve_memblock_callback
= NULL
;