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
30 #include <sys/socket.h>
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
39 #include <pulse/xmalloc.h>
41 #include <pulsecore/queue.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/core-scache.h>
44 #include <pulsecore/creds.h>
49 PA_PSTREAM_DESCRIPTOR_LENGTH
,
50 PA_PSTREAM_DESCRIPTOR_CHANNEL
,
51 PA_PSTREAM_DESCRIPTOR_OFFSET_HI
,
52 PA_PSTREAM_DESCRIPTOR_OFFSET_LO
,
53 PA_PSTREAM_DESCRIPTOR_SEEK
,
54 PA_PSTREAM_DESCRIPTOR_MAX
57 typedef uint32_t pa_pstream_descriptor
[PA_PSTREAM_DESCRIPTOR_MAX
];
59 #define PA_PSTREAM_DESCRIPTOR_SIZE (PA_PSTREAM_DESCRIPTOR_MAX*sizeof(uint32_t))
60 #define FRAME_SIZE_MAX PA_SCACHE_ENTRY_SIZE_MAX /* allow uploading a single sample in one frame at max */
63 enum { PA_PSTREAM_ITEM_PACKET
, PA_PSTREAM_ITEM_MEMBLOCK
} type
;
69 pa_seek_mode_t seek_mode
;
82 pa_mainloop_api
*mainloop
;
83 pa_defer_event
*defer_event
;
90 struct item_info
* current
;
91 pa_pstream_descriptor descriptor
;
97 pa_memblock
*memblock
;
99 pa_pstream_descriptor descriptor
;
104 pa_pstream_packet_cb_t recieve_packet_callback
;
105 void *recieve_packet_callback_userdata
;
107 pa_pstream_memblock_cb_t recieve_memblock_callback
;
108 void *recieve_memblock_callback_userdata
;
110 pa_pstream_notify_cb_t drain_callback
;
111 void *drain_callback_userdata
;
113 pa_pstream_notify_cb_t die_callback
;
114 void *die_callback_userdata
;
116 pa_memblock_stat
*memblock_stat
;
119 pa_creds read_creds
, write_creds
;
120 int read_creds_valid
, send_creds_now
;
124 static int do_write(pa_pstream
*p
);
125 static int do_read(pa_pstream
*p
);
127 static void do_something(pa_pstream
*p
) {
130 p
->mainloop
->defer_enable(p
->defer_event
, 0);
134 if (!p
->dead
&& pa_iochannel_is_readable(p
->io
)) {
137 } else if (!p
->dead
&& pa_iochannel_is_hungup(p
->io
))
140 if (!p
->dead
&& pa_iochannel_is_writable(p
->io
)) {
153 p
->die_callback(p
, p
->die_callback_userdata
);
158 static void io_callback(pa_iochannel
*io
, void *userdata
) {
159 pa_pstream
*p
= userdata
;
167 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void*userdata
) {
168 pa_pstream
*p
= userdata
;
171 assert(p
->defer_event
== e
);
172 assert(p
->mainloop
== m
);
177 pa_pstream
*pa_pstream_new(pa_mainloop_api
*m
, pa_iochannel
*io
, pa_memblock_stat
*s
) {
181 p
= pa_xnew(pa_pstream
, 1);
185 pa_iochannel_set_callback(io
, io_callback
, p
);
190 p
->defer_event
= m
->defer_new(m
, defer_callback
, p
);
191 m
->defer_enable(p
->defer_event
, 0);
193 p
->send_queue
= pa_queue_new();
194 assert(p
->send_queue
);
196 p
->write
.current
= NULL
;
199 p
->read
.memblock
= NULL
;
200 p
->read
.packet
= NULL
;
203 p
->recieve_packet_callback
= NULL
;
204 p
->recieve_packet_callback_userdata
= NULL
;
206 p
->recieve_memblock_callback
= NULL
;
207 p
->recieve_memblock_callback_userdata
= NULL
;
209 p
->drain_callback
= NULL
;
210 p
->drain_callback_userdata
= NULL
;
212 p
->die_callback
= NULL
;
213 p
->die_callback_userdata
= NULL
;
215 p
->memblock_stat
= s
;
217 pa_iochannel_socket_set_rcvbuf(io
, 1024*8);
218 pa_iochannel_socket_set_sndbuf(io
, 1024*8);
221 p
->send_creds_now
= 0;
222 p
->read_creds_valid
= 0;
227 static void item_free(void *item
, PA_GCC_UNUSED
void *p
) {
228 struct item_info
*i
= item
;
231 if (i
->type
== PA_PSTREAM_ITEM_MEMBLOCK
) {
232 assert(i
->chunk
.memblock
);
233 pa_memblock_unref(i
->chunk
.memblock
);
235 assert(i
->type
== PA_PSTREAM_ITEM_PACKET
);
237 pa_packet_unref(i
->packet
);
243 static void pstream_free(pa_pstream
*p
) {
248 pa_queue_free(p
->send_queue
, item_free
, NULL
);
250 if (p
->write
.current
)
251 item_free(p
->write
.current
, NULL
);
253 if (p
->read
.memblock
)
254 pa_memblock_unref(p
->read
.memblock
);
257 pa_packet_unref(p
->read
.packet
);
262 void pa_pstream_send_packet(pa_pstream
*p
, pa_packet
*packet
, const pa_creds
*creds
) {
264 assert(p
&& packet
&& p
->ref
>= 1);
269 /* pa_log(__FILE__": push-packet %p", packet); */
271 i
= pa_xnew(struct item_info
, 1);
272 i
->type
= PA_PSTREAM_ITEM_PACKET
;
273 i
->packet
= pa_packet_ref(packet
);
275 if ((i
->with_creds
= !!creds
))
279 pa_queue_push(p
->send_queue
, i
);
280 p
->mainloop
->defer_enable(p
->defer_event
, 1);
283 void pa_pstream_send_memblock(pa_pstream
*p
, uint32_t channel
, int64_t offset
, pa_seek_mode_t seek_mode
, const pa_memchunk
*chunk
) {
285 assert(p
&& channel
!= (uint32_t) -1 && chunk
&& p
->ref
>= 1);
290 /* pa_log(__FILE__": push-memblock %p", chunk); */
292 i
= pa_xnew(struct item_info
, 1);
293 i
->type
= PA_PSTREAM_ITEM_MEMBLOCK
;
295 i
->channel
= channel
;
297 i
->seek_mode
= seek_mode
;
302 pa_memblock_ref(i
->chunk
.memblock
);
304 pa_queue_push(p
->send_queue
, i
);
305 p
->mainloop
->defer_enable(p
->defer_event
, 1);
308 static void prepare_next_write_item(pa_pstream
*p
) {
311 if (!(p
->write
.current
= pa_queue_pop(p
->send_queue
)))
316 if (p
->write
.current
->type
== PA_PSTREAM_ITEM_PACKET
) {
317 /*pa_log(__FILE__": pop-packet %p", p->write.current->packet);*/
319 assert(p
->write
.current
->packet
);
320 p
->write
.data
= p
->write
.current
->packet
->data
;
321 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
] = htonl(p
->write
.current
->packet
->length
);
322 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
] = htonl((uint32_t) -1);
323 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_HI
] = 0;
324 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_LO
] = 0;
325 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
] = 0;
329 assert(p
->write
.current
->type
== PA_PSTREAM_ITEM_MEMBLOCK
&& p
->write
.current
->chunk
.memblock
);
330 p
->write
.data
= (uint8_t*) p
->write
.current
->chunk
.memblock
->data
+ p
->write
.current
->chunk
.index
;
331 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
] = htonl(p
->write
.current
->chunk
.length
);
332 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
] = htonl(p
->write
.current
->channel
);
333 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_HI
] = htonl((uint32_t) (((uint64_t) p
->write
.current
->offset
) >> 32));
334 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_LO
] = htonl((uint32_t) ((uint64_t) p
->write
.current
->offset
));
335 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
] = htonl(p
->write
.current
->seek_mode
);
339 if ((p
->send_creds_now
= p
->write
.current
->with_creds
))
340 p
->write_creds
= p
->write
.current
->creds
;
346 static int do_write(pa_pstream
*p
) {
352 if (!p
->write
.current
)
353 prepare_next_write_item(p
);
355 if (!p
->write
.current
)
358 assert(p
->write
.data
);
360 if (p
->write
.index
< PA_PSTREAM_DESCRIPTOR_SIZE
) {
361 d
= (uint8_t*) p
->write
.descriptor
+ p
->write
.index
;
362 l
= PA_PSTREAM_DESCRIPTOR_SIZE
- p
->write
.index
;
364 d
= (uint8_t*) p
->write
.data
+ p
->write
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
;
365 l
= ntohl(p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) - (p
->write
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
);
369 if (p
->send_creds_now
) {
371 if ((r
= pa_iochannel_write_with_creds(p
->io
, d
, l
, &p
->write_creds
)) < 0)
374 p
->send_creds_now
= 0;
378 if ((r
= pa_iochannel_write(p
->io
, d
, l
)) < 0)
383 if (p
->write
.index
>= PA_PSTREAM_DESCRIPTOR_SIZE
+ntohl(p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
])) {
384 assert(p
->write
.current
);
385 item_free(p
->write
.current
, (void *) 1);
386 p
->write
.current
= NULL
;
388 if (p
->drain_callback
&& !pa_pstream_is_pending(p
))
389 p
->drain_callback(p
, p
->drain_callback_userdata
);
395 static int do_read(pa_pstream
*p
) {
401 if (p
->read
.index
< PA_PSTREAM_DESCRIPTOR_SIZE
) {
402 d
= (uint8_t*) p
->read
.descriptor
+ p
->read
.index
;
403 l
= PA_PSTREAM_DESCRIPTOR_SIZE
- p
->read
.index
;
405 assert(p
->read
.data
);
406 d
= (uint8_t*) p
->read
.data
+ p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
;
407 l
= ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) - (p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
);
414 if ((r
= pa_iochannel_read_with_creds(p
->io
, d
, l
, &p
->read_creds
, &b
)) <= 0)
417 p
->read_creds_valid
= p
->read_creds_valid
|| b
;
420 if ((r
= pa_iochannel_read(p
->io
, d
, l
)) <= 0)
426 if (p
->read
.index
== PA_PSTREAM_DESCRIPTOR_SIZE
) {
427 /* Reading of frame descriptor complete */
429 /* Frame size too large */
430 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) > FRAME_SIZE_MAX
) {
431 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
);
435 assert(!p
->read
.packet
&& !p
->read
.memblock
);
437 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
]) == (uint32_t) -1) {
438 /* Frame is a packet frame */
439 p
->read
.packet
= pa_packet_new(ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]));
440 p
->read
.data
= p
->read
.packet
->data
;
442 /* Frame is a memblock frame */
443 p
->read
.memblock
= pa_memblock_new(ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]), p
->memblock_stat
);
444 p
->read
.data
= p
->read
.memblock
->data
;
446 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
]) > PA_SEEK_RELATIVE_END
) {
447 pa_log_warn(__FILE__
": Invalid seek mode");
452 } else if (p
->read
.index
> PA_PSTREAM_DESCRIPTOR_SIZE
) {
453 /* Frame payload available */
455 if (p
->read
.memblock
&& p
->recieve_memblock_callback
) { /* Is this memblock data? Than pass it to the user */
456 l
= (p
->read
.index
- r
) < PA_PSTREAM_DESCRIPTOR_SIZE
? p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
: (size_t) r
;
461 chunk
.memblock
= p
->read
.memblock
;
462 chunk
.index
= p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
- l
;
465 if (p
->recieve_memblock_callback
) {
469 (((uint64_t) ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_HI
])) << 32) |
470 (((uint64_t) ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_LO
]))));
472 p
->recieve_memblock_callback(
474 ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
]),
476 ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
]),
478 p
->recieve_memblock_callback_userdata
);
481 /* Drop seek info for following callbacks */
482 p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_SEEK
] =
483 p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_HI
] =
484 p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_OFFSET_LO
] = 0;
489 if (p
->read
.index
>= ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) + PA_PSTREAM_DESCRIPTOR_SIZE
) {
490 if (p
->read
.memblock
) {
491 assert(!p
->read
.packet
);
493 pa_memblock_unref(p
->read
.memblock
);
494 p
->read
.memblock
= NULL
;
496 assert(p
->read
.packet
);
498 if (p
->recieve_packet_callback
)
500 p
->recieve_packet_callback(p
, p
->read
.packet
, p
->read_creds_valid
? &p
->read_creds
: NULL
, p
->recieve_packet_callback_userdata
);
502 p
->recieve_packet_callback(p
, p
->read
.packet
, NULL
, p
->recieve_packet_callback_userdata
);
505 pa_packet_unref(p
->read
.packet
);
506 p
->read
.packet
= NULL
;
511 p
->read_creds_valid
= 0;
519 void pa_pstream_set_die_callback(pa_pstream
*p
, pa_pstream_notify_cb_t cb
, void *userdata
) {
523 p
->die_callback
= cb
;
524 p
->die_callback_userdata
= userdata
;
528 void pa_pstream_set_drain_callback(pa_pstream
*p
, pa_pstream_notify_cb_t cb
, void *userdata
) {
532 p
->drain_callback
= cb
;
533 p
->drain_callback_userdata
= userdata
;
536 void pa_pstream_set_recieve_packet_callback(pa_pstream
*p
, pa_pstream_packet_cb_t cb
, void *userdata
) {
540 p
->recieve_packet_callback
= cb
;
541 p
->recieve_packet_callback_userdata
= userdata
;
544 void pa_pstream_set_recieve_memblock_callback(pa_pstream
*p
, pa_pstream_memblock_cb_t cb
, void *userdata
) {
548 p
->recieve_memblock_callback
= cb
;
549 p
->recieve_memblock_callback_userdata
= userdata
;
552 int pa_pstream_is_pending(pa_pstream
*p
) {
558 return p
->write
.current
|| !pa_queue_is_empty(p
->send_queue
);
561 void pa_pstream_unref(pa_pstream
*p
) {
569 pa_pstream
* pa_pstream_ref(pa_pstream
*p
) {
577 void pa_pstream_close(pa_pstream
*p
) {
583 pa_iochannel_free(p
->io
);
587 if (p
->defer_event
) {
588 p
->mainloop
->defer_free(p
->defer_event
);
589 p
->defer_event
= NULL
;
592 p
->die_callback
= NULL
;
593 p
->drain_callback
= NULL
;
594 p
->recieve_packet_callback
= NULL
;
595 p
->recieve_memblock_callback
= NULL
;