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 General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 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 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <netinet/in.h>
35 enum pa_pstream_descriptor_index
{
36 PA_PSTREAM_DESCRIPTOR_LENGTH
,
37 PA_PSTREAM_DESCRIPTOR_CHANNEL
,
38 PA_PSTREAM_DESCRIPTOR_DELTA
,
39 PA_PSTREAM_DESCRIPTOR_MAX
42 typedef uint32_t pa_pstream_descriptor
[PA_PSTREAM_DESCRIPTOR_MAX
];
44 #define PA_PSTREAM_DESCRIPTOR_SIZE (PA_PSTREAM_DESCRIPTOR_MAX*sizeof(uint32_t))
45 #define FRAME_SIZE_MAX (1024*500) /* half a megabyte */
48 enum { PA_PSTREAM_ITEM_PACKET
, PA_PSTREAM_ITEM_MEMBLOCK
} type
;
51 struct pa_memchunk chunk
;
56 struct pa_packet
*packet
;
60 struct pa_mainloop_api
*mainloop
;
61 struct mainloop_source
*mainloop_source
;
62 struct pa_iochannel
*io
;
63 struct pa_queue
*send_queue
;
65 int in_use
, shall_free
;
68 void (*die_callback
) (struct pa_pstream
*p
, void *userdata
);
69 void *die_callback_userdata
;
72 struct item_info
* current
;
73 pa_pstream_descriptor descriptor
;
79 struct pa_memblock
*memblock
;
80 struct pa_packet
*packet
;
81 pa_pstream_descriptor descriptor
;
86 void (*recieve_packet_callback
) (struct pa_pstream
*p
, struct pa_packet
*packet
, void *userdata
);
87 void *recieve_packet_callback_userdata
;
89 void (*recieve_memblock_callback
) (struct pa_pstream
*p
, uint32_t channel
, int32_t delta
, const struct pa_memchunk
*chunk
, void *userdata
);
90 void *recieve_memblock_callback_userdata
;
92 void (*drain_callback
)(struct pa_pstream
*p
, void *userdata
);
96 static void do_write(struct pa_pstream
*p
);
97 static void do_read(struct pa_pstream
*p
);
99 static void do_something(struct pa_pstream
*p
) {
100 assert(p
&& !p
->shall_free
);
101 p
->mainloop
->enable_fixed(p
->mainloop
, p
->mainloop_source
, 0);
106 if (pa_iochannel_is_hungup(p
->io
)) {
109 p
->die_callback(p
, p
->die_callback_userdata
);
114 if (pa_iochannel_is_writable(p
->io
)) {
125 if (pa_iochannel_is_readable(p
->io
)) {
136 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
137 struct pa_pstream
*p
= userdata
;
138 assert(p
&& p
->io
== io
);
142 static void fixed_callback(struct pa_mainloop_api
*m
, void *id
, void*userdata
) {
143 struct pa_pstream
*p
= userdata
;
144 assert(p
&& p
->mainloop_source
== id
&& p
->mainloop
== m
);
148 struct pa_pstream
*pa_pstream_new(struct pa_mainloop_api
*m
, struct pa_iochannel
*io
) {
149 struct pa_pstream
*p
;
152 p
= pa_xmalloc(sizeof(struct pa_pstream
));
155 pa_iochannel_set_callback(io
, io_callback
, p
);
158 p
->die_callback
= NULL
;
159 p
->die_callback_userdata
= NULL
;
162 p
->mainloop_source
= m
->source_fixed(m
, fixed_callback
, p
);
163 m
->enable_fixed(m
, p
->mainloop_source
, 0);
165 p
->send_queue
= pa_queue_new();
166 assert(p
->send_queue
);
168 p
->write
.current
= NULL
;
171 p
->read
.memblock
= NULL
;
172 p
->read
.packet
= NULL
;
175 p
->recieve_packet_callback
= NULL
;
176 p
->recieve_packet_callback_userdata
= NULL
;
178 p
->recieve_memblock_callback
= NULL
;
179 p
->recieve_memblock_callback_userdata
= NULL
;
181 p
->drain_callback
= NULL
;
182 p
->drain_userdata
= NULL
;
184 p
->in_use
= p
->shall_free
= 0;
189 static void item_free(void *item
, void *p
) {
190 struct item_info
*i
= item
;
193 if (i
->type
== PA_PSTREAM_ITEM_MEMBLOCK
) {
194 assert(i
->chunk
.memblock
);
195 pa_memblock_unref(i
->chunk
.memblock
);
197 assert(i
->type
== PA_PSTREAM_ITEM_PACKET
);
199 pa_packet_unref(i
->packet
);
205 void pa_pstream_free(struct pa_pstream
*p
) {
209 /* If this pstream object is used by someone else on the call stack, we have to postpone the freeing */
210 p
->dead
= p
->shall_free
= 1;
214 pa_iochannel_free(p
->io
);
215 pa_queue_free(p
->send_queue
, item_free
, NULL
);
217 if (p
->write
.current
)
218 item_free(p
->write
.current
, NULL
);
220 if (p
->read
.memblock
)
221 pa_memblock_unref(p
->read
.memblock
);
224 pa_packet_unref(p
->read
.packet
);
226 p
->mainloop
->cancel_fixed(p
->mainloop
, p
->mainloop_source
);
230 void pa_pstream_send_packet(struct pa_pstream
*p
, struct pa_packet
*packet
) {
234 i
= pa_xmalloc(sizeof(struct item_info
));
235 i
->type
= PA_PSTREAM_ITEM_PACKET
;
236 i
->packet
= pa_packet_ref(packet
);
238 pa_queue_push(p
->send_queue
, i
);
239 p
->mainloop
->enable_fixed(p
->mainloop
, p
->mainloop_source
, 1);
242 void pa_pstream_send_memblock(struct pa_pstream
*p
, uint32_t channel
, int32_t delta
, const struct pa_memchunk
*chunk
) {
244 assert(p
&& channel
!= (uint32_t) -1 && chunk
);
246 i
= pa_xmalloc(sizeof(struct item_info
));
247 i
->type
= PA_PSTREAM_ITEM_MEMBLOCK
;
249 i
->channel
= channel
;
252 pa_memblock_ref(i
->chunk
.memblock
);
254 pa_queue_push(p
->send_queue
, i
);
255 p
->mainloop
->enable_fixed(p
->mainloop
, p
->mainloop_source
, 1);
258 void pa_pstream_set_recieve_packet_callback(struct pa_pstream
*p
, void (*callback
) (struct pa_pstream
*p
, struct pa_packet
*packet
, void *userdata
), void *userdata
) {
259 assert(p
&& callback
);
261 p
->recieve_packet_callback
= callback
;
262 p
->recieve_packet_callback_userdata
= userdata
;
265 void pa_pstream_set_recieve_memblock_callback(struct pa_pstream
*p
, void (*callback
) (struct pa_pstream
*p
, uint32_t channel
, int32_t delta
, const struct pa_memchunk
*chunk
, void *userdata
), void *userdata
) {
266 assert(p
&& callback
);
268 p
->recieve_memblock_callback
= callback
;
269 p
->recieve_memblock_callback_userdata
= userdata
;
272 static void prepare_next_write_item(struct pa_pstream
*p
) {
275 if (!(p
->write
.current
= pa_queue_pop(p
->send_queue
)))
280 if (p
->write
.current
->type
== PA_PSTREAM_ITEM_PACKET
) {
281 assert(p
->write
.current
->packet
);
282 p
->write
.data
= p
->write
.current
->packet
->data
;
283 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
] = htonl(p
->write
.current
->packet
->length
);
284 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
] = htonl((uint32_t) -1);
285 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_DELTA
] = 0;
287 assert(p
->write
.current
->type
== PA_PSTREAM_ITEM_MEMBLOCK
&& p
->write
.current
->chunk
.memblock
);
288 p
->write
.data
= p
->write
.current
->chunk
.memblock
->data
+ p
->write
.current
->chunk
.index
;
289 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
] = htonl(p
->write
.current
->chunk
.length
);
290 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
] = htonl(p
->write
.current
->channel
);
291 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_DELTA
] = htonl(p
->write
.current
->delta
);
295 static void do_write(struct pa_pstream
*p
) {
301 if (!p
->write
.current
)
302 prepare_next_write_item(p
);
304 if (!p
->write
.current
)
307 assert(p
->write
.data
);
309 if (p
->write
.index
< PA_PSTREAM_DESCRIPTOR_SIZE
) {
310 d
= (void*) p
->write
.descriptor
+ p
->write
.index
;
311 l
= PA_PSTREAM_DESCRIPTOR_SIZE
- p
->write
.index
;
313 d
= (void*) p
->write
.data
+ p
->write
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
;
314 l
= ntohl(p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) - (p
->write
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
);
317 if ((r
= pa_iochannel_write(p
->io
, d
, l
)) < 0)
322 if (p
->write
.index
>= PA_PSTREAM_DESCRIPTOR_SIZE
+ntohl(p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
])) {
323 assert(p
->write
.current
);
324 item_free(p
->write
.current
, (void *) 1);
325 p
->write
.current
= NULL
;
327 if (p
->drain_callback
&& !pa_pstream_is_pending(p
))
328 p
->drain_callback(p
, p
->drain_userdata
);
336 p
->die_callback(p
, p
->die_callback_userdata
);
339 static void do_read(struct pa_pstream
*p
) {
345 if (p
->read
.index
< PA_PSTREAM_DESCRIPTOR_SIZE
) {
346 d
= (void*) p
->read
.descriptor
+ p
->read
.index
;
347 l
= PA_PSTREAM_DESCRIPTOR_SIZE
- p
->read
.index
;
349 assert(p
->read
.data
);
350 d
= (void*) p
->read
.data
+ p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
;
351 l
= ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) - (p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
);
354 if ((r
= pa_iochannel_read(p
->io
, d
, l
)) <= 0)
359 if (p
->read
.index
== PA_PSTREAM_DESCRIPTOR_SIZE
) {
360 /* Reading of frame descriptor complete */
362 /* Frame size too large */
363 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) > FRAME_SIZE_MAX
) {
364 fprintf(stderr
, "frame size too large\n");
368 assert(!p
->read
.packet
&& !p
->read
.memblock
);
370 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
]) == (uint32_t) -1) {
371 /* Frame is a packet frame */
372 p
->read
.packet
= pa_packet_new(ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]));
373 assert(p
->read
.packet
);
374 p
->read
.data
= p
->read
.packet
->data
;
376 /* Frame is a memblock frame */
377 p
->read
.memblock
= pa_memblock_new(ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]));
378 assert(p
->read
.memblock
);
379 p
->read
.data
= p
->read
.memblock
->data
;
382 } else if (p
->read
.index
> PA_PSTREAM_DESCRIPTOR_SIZE
) {
383 /* Frame payload available */
385 if (p
->read
.memblock
&& p
->recieve_memblock_callback
) { /* Is this memblock data? Than pass it to the user */
388 l
= (p
->read
.index
- r
) < PA_PSTREAM_DESCRIPTOR_SIZE
? p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
: (size_t) r
;
391 struct pa_memchunk chunk
;
393 chunk
.memblock
= p
->read
.memblock
;
394 chunk
.index
= p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
- l
;
397 if (p
->recieve_memblock_callback
)
398 p
->recieve_memblock_callback(
400 ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
]),
401 (int32_t) ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_DELTA
]),
403 p
->recieve_memblock_callback_userdata
);
408 if (p
->read
.index
>= ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) + PA_PSTREAM_DESCRIPTOR_SIZE
) {
409 if (p
->read
.memblock
) {
410 assert(!p
->read
.packet
);
412 pa_memblock_unref(p
->read
.memblock
);
413 p
->read
.memblock
= NULL
;
415 assert(p
->read
.packet
);
417 if (p
->recieve_packet_callback
)
418 p
->recieve_packet_callback(p
, p
->read
.packet
, p
->recieve_packet_callback_userdata
);
420 pa_packet_unref(p
->read
.packet
);
421 p
->read
.packet
= NULL
;
433 p
->die_callback(p
, p
->die_callback_userdata
);
437 void pa_pstream_set_die_callback(struct pa_pstream
*p
, void (*callback
)(struct pa_pstream
*p
, void *userdata
), void *userdata
) {
438 assert(p
&& callback
);
439 p
->die_callback
= callback
;
440 p
->die_callback_userdata
= userdata
;
443 int pa_pstream_is_pending(struct pa_pstream
*p
) {
449 return p
->write
.current
|| !pa_queue_is_empty(p
->send_queue
);
452 void pa_pstream_set_drain_callback(struct pa_pstream
*p
, void (*cb
)(struct pa_pstream
*p
, void *userdata
), void *userdata
) {
455 p
->drain_callback
= cb
;
456 p
->drain_userdata
= userdata
;