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_SYS_SOCKET_H
32 #include <sys/socket.h>
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
43 #include <pulse/xmalloc.h>
45 #include <pulsecore/queue.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/core-scache.h>
48 #include <pulsecore/creds.h>
53 PA_PSTREAM_DESCRIPTOR_LENGTH
,
54 PA_PSTREAM_DESCRIPTOR_CHANNEL
,
55 PA_PSTREAM_DESCRIPTOR_OFFSET_HI
,
56 PA_PSTREAM_DESCRIPTOR_OFFSET_LO
,
57 PA_PSTREAM_DESCRIPTOR_SEEK
,
58 PA_PSTREAM_DESCRIPTOR_MAX
61 typedef uint32_t pa_pstream_descriptor
[PA_PSTREAM_DESCRIPTOR_MAX
];
63 #define PA_PSTREAM_DESCRIPTOR_SIZE (PA_PSTREAM_DESCRIPTOR_MAX*sizeof(uint32_t))
64 #define FRAME_SIZE_MAX PA_SCACHE_ENTRY_SIZE_MAX /* allow uploading a single sample in one frame at max */
67 enum { PA_PSTREAM_ITEM_PACKET
, PA_PSTREAM_ITEM_MEMBLOCK
} type
;
73 pa_seek_mode_t seek_mode
;
86 pa_mainloop_api
*mainloop
;
87 pa_defer_event
*defer_event
;
94 struct item_info
* current
;
95 pa_pstream_descriptor descriptor
;
101 pa_memblock
*memblock
;
103 pa_pstream_descriptor descriptor
;
108 pa_pstream_packet_cb_t recieve_packet_callback
;
109 void *recieve_packet_callback_userdata
;
111 pa_pstream_memblock_cb_t recieve_memblock_callback
;
112 void *recieve_memblock_callback_userdata
;
114 pa_pstream_notify_cb_t drain_callback
;
115 void *drain_callback_userdata
;
117 pa_pstream_notify_cb_t die_callback
;
118 void *die_callback_userdata
;
120 pa_memblock_stat
*memblock_stat
;
123 pa_creds read_creds
, write_creds
;
124 int read_creds_valid
, send_creds_now
;
128 static int do_write(pa_pstream
*p
);
129 static int do_read(pa_pstream
*p
);
131 static void do_something(pa_pstream
*p
) {
134 p
->mainloop
->defer_enable(p
->defer_event
, 0);
138 if (!p
->dead
&& pa_iochannel_is_readable(p
->io
)) {
141 } else if (!p
->dead
&& pa_iochannel_is_hungup(p
->io
))
144 if (!p
->dead
&& pa_iochannel_is_writable(p
->io
)) {
157 p
->die_callback(p
, p
->die_callback_userdata
);
162 static void io_callback(pa_iochannel
*io
, void *userdata
) {
163 pa_pstream
*p
= userdata
;
171 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void*userdata
) {
172 pa_pstream
*p
= userdata
;
175 assert(p
->defer_event
== e
);
176 assert(p
->mainloop
== m
);
181 pa_pstream
*pa_pstream_new(pa_mainloop_api
*m
, pa_iochannel
*io
, pa_memblock_stat
*s
) {
185 p
= pa_xnew(pa_pstream
, 1);
189 pa_iochannel_set_callback(io
, io_callback
, p
);
194 p
->defer_event
= m
->defer_new(m
, defer_callback
, p
);
195 m
->defer_enable(p
->defer_event
, 0);
197 p
->send_queue
= pa_queue_new();
198 assert(p
->send_queue
);
200 p
->write
.current
= NULL
;
203 p
->read
.memblock
= NULL
;
204 p
->read
.packet
= NULL
;
207 p
->recieve_packet_callback
= NULL
;
208 p
->recieve_packet_callback_userdata
= NULL
;
210 p
->recieve_memblock_callback
= NULL
;
211 p
->recieve_memblock_callback_userdata
= NULL
;
213 p
->drain_callback
= NULL
;
214 p
->drain_callback_userdata
= NULL
;
216 p
->die_callback
= NULL
;
217 p
->die_callback_userdata
= NULL
;
219 p
->memblock_stat
= s
;
221 pa_iochannel_socket_set_rcvbuf(io
, 1024*8);
222 pa_iochannel_socket_set_sndbuf(io
, 1024*8);
225 p
->send_creds_now
= 0;
226 p
->read_creds_valid
= 0;
231 static void item_free(void *item
, PA_GCC_UNUSED
void *p
) {
232 struct item_info
*i
= item
;
235 if (i
->type
== PA_PSTREAM_ITEM_MEMBLOCK
) {
236 assert(i
->chunk
.memblock
);
237 pa_memblock_unref(i
->chunk
.memblock
);
239 assert(i
->type
== PA_PSTREAM_ITEM_PACKET
);
241 pa_packet_unref(i
->packet
);
247 static void pstream_free(pa_pstream
*p
) {
252 pa_queue_free(p
->send_queue
, item_free
, NULL
);
254 if (p
->write
.current
)
255 item_free(p
->write
.current
, NULL
);
257 if (p
->read
.memblock
)
258 pa_memblock_unref(p
->read
.memblock
);
261 pa_packet_unref(p
->read
.packet
);
266 void pa_pstream_send_packet(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
) {
268 assert(p
&& packet
&& p
->ref
>= 1);
273 /* pa_log(__FILE__": push-packet %p", packet); */
275 i
= pa_xnew(struct item_info
, 1);
276 i
->type
= PA_PSTREAM_ITEM_PACKET
;
277 i
->packet
= pa_packet_ref(packet
);
279 if ((i
->with_creds
= !!creds
))
283 pa_queue_push(p
->send_queue
, i
);
284 p
->mainloop
->defer_enable(p
->defer_event
, 1);
287 void pa_pstream_send_memblock(pa_pstream
*p
, uint32_t channel
, int64_t offset
, pa_seek_mode_t seek_mode
, const pa_memchunk
*chunk
) {
289 assert(p
&& channel
!= (uint32_t) -1 && chunk
&& p
->ref
>= 1);
294 /* pa_log(__FILE__": push-memblock %p", chunk); */
296 i
= pa_xnew(struct item_info
, 1);
297 i
->type
= PA_PSTREAM_ITEM_MEMBLOCK
;
299 i
->channel
= channel
;
301 i
->seek_mode
= seek_mode
;
306 pa_memblock_ref(i
->chunk
.memblock
);
308 pa_queue_push(p
->send_queue
, i
);
309 p
->mainloop
->defer_enable(p
->defer_event
, 1);
312 static void prepare_next_write_item(pa_pstream
*p
) {
315 if (!(p
->write
.current
= pa_queue_pop(p
->send_queue
)))
320 if (p
->write
.current
->type
== PA_PSTREAM_ITEM_PACKET
) {
321 /*pa_log(__FILE__": pop-packet %p", p->write.current->packet);*/
323 assert(p
->write
.current
->packet
);
324 p
->write
.data
= p
->write
.current
->packet
->data
;
325 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
] = htonl(p
->write
.current
->packet
->length
);
326 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
] = htonl((uint32_t) -1);
327 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_HI
] = 0;
328 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_LO
] = 0;
329 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
] = 0;
333 assert(p
->write
.current
->type
== PA_PSTREAM_ITEM_MEMBLOCK
&& p
->write
.current
->chunk
.memblock
);
334 p
->write
.data
= (uint8_t*) p
->write
.current
->chunk
.memblock
->data
+ p
->write
.current
->chunk
.index
;
335 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
] = htonl(p
->write
.current
->chunk
.length
);
336 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
] = htonl(p
->write
.current
->channel
);
337 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_HI
] = htonl((uint32_t) (((uint64_t) p
->write
.current
->offset
) >> 32));
338 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_LO
] = htonl((uint32_t) ((uint64_t) p
->write
.current
->offset
));
339 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
] = htonl(p
->write
.current
->seek_mode
);
343 if ((p
->send_creds_now
= p
->write
.current
->with_creds
))
344 p
->write_creds
= p
->write
.current
->creds
;
350 static int do_write(pa_pstream
*p
) {
356 if (!p
->write
.current
)
357 prepare_next_write_item(p
);
359 if (!p
->write
.current
)
362 assert(p
->write
.data
);
364 if (p
->write
.index
< PA_PSTREAM_DESCRIPTOR_SIZE
) {
365 d
= (uint8_t*) p
->write
.descriptor
+ p
->write
.index
;
366 l
= PA_PSTREAM_DESCRIPTOR_SIZE
- p
->write
.index
;
368 d
= (uint8_t*) p
->write
.data
+ p
->write
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
;
369 l
= ntohl(p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) - (p
->write
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
);
373 if (p
->send_creds_now
) {
375 if ((r
= pa_iochannel_write_with_creds(p
->io
, d
, l
, &p
->write_creds
)) < 0)
378 p
->send_creds_now
= 0;
382 if ((r
= pa_iochannel_write(p
->io
, d
, l
)) < 0)
387 if (p
->write
.index
>= PA_PSTREAM_DESCRIPTOR_SIZE
+ntohl(p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
])) {
388 assert(p
->write
.current
);
389 item_free(p
->write
.current
, (void *) 1);
390 p
->write
.current
= NULL
;
392 if (p
->drain_callback
&& !pa_pstream_is_pending(p
))
393 p
->drain_callback(p
, p
->drain_callback_userdata
);
399 static int do_read(pa_pstream
*p
) {
405 if (p
->read
.index
< PA_PSTREAM_DESCRIPTOR_SIZE
) {
406 d
= (uint8_t*) p
->read
.descriptor
+ p
->read
.index
;
407 l
= PA_PSTREAM_DESCRIPTOR_SIZE
- p
->read
.index
;
409 assert(p
->read
.data
);
410 d
= (uint8_t*) p
->read
.data
+ p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
;
411 l
= ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) - (p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
);
418 if ((r
= pa_iochannel_read_with_creds(p
->io
, d
, l
, &p
->read_creds
, &b
)) <= 0)
421 p
->read_creds_valid
= p
->read_creds_valid
|| b
;
424 if ((r
= pa_iochannel_read(p
->io
, d
, l
)) <= 0)
430 if (p
->read
.index
== PA_PSTREAM_DESCRIPTOR_SIZE
) {
431 /* Reading of frame descriptor complete */
433 /* Frame size too large */
434 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) > FRAME_SIZE_MAX
) {
435 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
);
439 assert(!p
->read
.packet
&& !p
->read
.memblock
);
441 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
]) == (uint32_t) -1) {
442 /* Frame is a packet frame */
443 p
->read
.packet
= pa_packet_new(ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]));
444 p
->read
.data
= p
->read
.packet
->data
;
446 /* Frame is a memblock frame */
447 p
->read
.memblock
= pa_memblock_new(ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]), p
->memblock_stat
);
448 p
->read
.data
= p
->read
.memblock
->data
;
450 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
]) > PA_SEEK_RELATIVE_END
) {
451 pa_log_warn(__FILE__
": Invalid seek mode");
456 } else if (p
->read
.index
> PA_PSTREAM_DESCRIPTOR_SIZE
) {
457 /* Frame payload available */
459 if (p
->read
.memblock
&& p
->recieve_memblock_callback
) { /* Is this memblock data? Than pass it to the user */
460 l
= (p
->read
.index
- r
) < PA_PSTREAM_DESCRIPTOR_SIZE
? p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
: (size_t) r
;
465 chunk
.memblock
= p
->read
.memblock
;
466 chunk
.index
= p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
- l
;
469 if (p
->recieve_memblock_callback
) {
473 (((uint64_t) ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_HI
])) << 32) |
474 (((uint64_t) ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_LO
]))));
476 p
->recieve_memblock_callback(
478 ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
]),
480 ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
]),
482 p
->recieve_memblock_callback_userdata
);
485 /* Drop seek info for following callbacks */
486 p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
] =
487 p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_HI
] =
488 p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_LO
] = 0;
493 if (p
->read
.index
>= ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) + PA_PSTREAM_DESCRIPTOR_SIZE
) {
494 if (p
->read
.memblock
) {
495 assert(!p
->read
.packet
);
497 pa_memblock_unref(p
->read
.memblock
);
498 p
->read
.memblock
= NULL
;
500 assert(p
->read
.packet
);
502 if (p
->recieve_packet_callback
)
504 p
->recieve_packet_callback(p
, p
->read
.packet
, p
->read_creds_valid
? &p
->read_creds
: NULL
, p
->recieve_packet_callback_userdata
);
506 p
->recieve_packet_callback(p
, p
->read
.packet
, NULL
, p
->recieve_packet_callback_userdata
);
509 pa_packet_unref(p
->read
.packet
);
510 p
->read
.packet
= NULL
;
515 p
->read_creds_valid
= 0;
523 void pa_pstream_set_die_callback(pa_pstream
*p
, pa_pstream_notify_cb_t cb
, void *userdata
) {
527 p
->die_callback
= cb
;
528 p
->die_callback_userdata
= userdata
;
532 void pa_pstream_set_drain_callback(pa_pstream
*p
, pa_pstream_notify_cb_t cb
, void *userdata
) {
536 p
->drain_callback
= cb
;
537 p
->drain_callback_userdata
= userdata
;
540 void pa_pstream_set_recieve_packet_callback(pa_pstream
*p
, pa_pstream_packet_cb_t cb
, void *userdata
) {
544 p
->recieve_packet_callback
= cb
;
545 p
->recieve_packet_callback_userdata
= userdata
;
548 void pa_pstream_set_recieve_memblock_callback(pa_pstream
*p
, pa_pstream_memblock_cb_t cb
, void *userdata
) {
552 p
->recieve_memblock_callback
= cb
;
553 p
->recieve_memblock_callback_userdata
= userdata
;
556 int pa_pstream_is_pending(pa_pstream
*p
) {
562 return p
->write
.current
|| !pa_queue_is_empty(p
->send_queue
);
565 void pa_pstream_unref(pa_pstream
*p
) {
573 pa_pstream
* pa_pstream_ref(pa_pstream
*p
) {
581 void pa_pstream_close(pa_pstream
*p
) {
587 pa_iochannel_free(p
->io
);
591 if (p
->defer_event
) {
592 p
->mainloop
->defer_free(p
->defer_event
);
593 p
->defer_event
= NULL
;
596 p
->die_callback
= NULL
;
597 p
->drain_callback
= NULL
;
598 p
->recieve_packet_callback
= NULL
;
599 p
->recieve_memblock_callback
= NULL
;