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>
36 enum pa_pstream_descriptor_index
{
37 PA_PSTREAM_DESCRIPTOR_LENGTH
,
38 PA_PSTREAM_DESCRIPTOR_CHANNEL
,
39 PA_PSTREAM_DESCRIPTOR_DELTA
,
40 PA_PSTREAM_DESCRIPTOR_MAX
43 typedef uint32_t pa_pstream_descriptor
[PA_PSTREAM_DESCRIPTOR_MAX
];
45 #define PA_PSTREAM_DESCRIPTOR_SIZE (PA_PSTREAM_DESCRIPTOR_MAX*sizeof(uint32_t))
46 #define FRAME_SIZE_MAX (1024*500) /* half a megabyte */
49 enum { PA_PSTREAM_ITEM_PACKET
, PA_PSTREAM_ITEM_MEMBLOCK
} type
;
52 struct pa_memchunk chunk
;
57 struct pa_packet
*packet
;
63 struct pa_mainloop_api
*mainloop
;
64 struct pa_defer_event
*defer_event
;
65 struct pa_iochannel
*io
;
66 struct pa_queue
*send_queue
;
69 void (*die_callback
) (struct pa_pstream
*p
, void *userdata
);
70 void *die_callback_userdata
;
73 struct item_info
* current
;
74 pa_pstream_descriptor descriptor
;
80 struct pa_memblock
*memblock
;
81 struct pa_packet
*packet
;
82 pa_pstream_descriptor descriptor
;
87 void (*recieve_packet_callback
) (struct pa_pstream
*p
, struct pa_packet
*packet
, void *userdata
);
88 void *recieve_packet_callback_userdata
;
90 void (*recieve_memblock_callback
) (struct pa_pstream
*p
, uint32_t channel
, uint32_t delta
, const struct pa_memchunk
*chunk
, void *userdata
);
91 void *recieve_memblock_callback_userdata
;
93 void (*drain_callback
)(struct pa_pstream
*p
, void *userdata
);
96 struct pa_memblock_stat
*memblock_stat
;
99 static void do_write(struct pa_pstream
*p
);
100 static void do_read(struct pa_pstream
*p
);
102 static void do_something(struct pa_pstream
*p
) {
108 p
->mainloop
->defer_enable(p
->defer_event
, 0);
112 if (!p
->dead
&& pa_iochannel_is_hungup(p
->io
)) {
115 p
->die_callback(p
, p
->die_callback_userdata
);
118 if (!p
->dead
&& pa_iochannel_is_writable(p
->io
))
121 if (!p
->dead
&& pa_iochannel_is_readable(p
->io
))
127 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
128 struct pa_pstream
*p
= userdata
;
129 assert(p
&& p
->io
== io
);
133 static void defer_callback(struct pa_mainloop_api
*m
, struct pa_defer_event
*e
, void*userdata
) {
134 struct pa_pstream
*p
= userdata
;
135 assert(p
&& p
->defer_event
== e
&& p
->mainloop
== m
);
139 struct pa_pstream
*pa_pstream_new(struct pa_mainloop_api
*m
, struct pa_iochannel
*io
, struct pa_memblock_stat
*s
) {
140 struct pa_pstream
*p
;
143 p
= pa_xmalloc(sizeof(struct pa_pstream
));
146 pa_iochannel_set_callback(io
, io_callback
, p
);
149 p
->die_callback
= NULL
;
150 p
->die_callback_userdata
= NULL
;
153 p
->defer_event
= m
->defer_new(m
, defer_callback
, p
);
154 m
->defer_enable(p
->defer_event
, 0);
156 p
->send_queue
= pa_queue_new();
157 assert(p
->send_queue
);
159 p
->write
.current
= NULL
;
162 p
->read
.memblock
= NULL
;
163 p
->read
.packet
= NULL
;
166 p
->recieve_packet_callback
= NULL
;
167 p
->recieve_packet_callback_userdata
= NULL
;
169 p
->recieve_memblock_callback
= NULL
;
170 p
->recieve_memblock_callback_userdata
= NULL
;
172 p
->drain_callback
= NULL
;
173 p
->drain_userdata
= NULL
;
175 p
->memblock_stat
= s
;
177 pa_iochannel_socket_set_rcvbuf(io
, 1024*8);
178 pa_iochannel_socket_set_sndbuf(io
, 1024*8);
183 static void item_free(void *item
, void *p
) {
184 struct item_info
*i
= item
;
187 if (i
->type
== PA_PSTREAM_ITEM_MEMBLOCK
) {
188 assert(i
->chunk
.memblock
);
189 pa_memblock_unref(i
->chunk
.memblock
);
191 assert(i
->type
== PA_PSTREAM_ITEM_PACKET
);
193 pa_packet_unref(i
->packet
);
199 static void pstream_free(struct pa_pstream
*p
) {
204 pa_queue_free(p
->send_queue
, item_free
, NULL
);
206 if (p
->write
.current
)
207 item_free(p
->write
.current
, NULL
);
209 if (p
->read
.memblock
)
210 pa_memblock_unref(p
->read
.memblock
);
213 pa_packet_unref(p
->read
.packet
);
218 void pa_pstream_send_packet(struct pa_pstream
*p
, struct pa_packet
*packet
) {
220 assert(p
&& packet
&& p
->ref
>= 1);
225 /* pa_log(__FILE__": push-packet %p\n", packet); */
227 i
= pa_xmalloc(sizeof(struct item_info
));
228 i
->type
= PA_PSTREAM_ITEM_PACKET
;
229 i
->packet
= pa_packet_ref(packet
);
231 pa_queue_push(p
->send_queue
, i
);
232 p
->mainloop
->defer_enable(p
->defer_event
, 1);
235 void pa_pstream_send_memblock(struct pa_pstream
*p
, uint32_t channel
, uint32_t delta
, const struct pa_memchunk
*chunk
) {
237 assert(p
&& channel
!= (uint32_t) -1 && chunk
&& p
->ref
>= 1);
242 /* pa_log(__FILE__": push-memblock %p\n", chunk); */
244 i
= pa_xmalloc(sizeof(struct item_info
));
245 i
->type
= PA_PSTREAM_ITEM_MEMBLOCK
;
247 i
->channel
= channel
;
250 pa_memblock_ref(i
->chunk
.memblock
);
252 pa_queue_push(p
->send_queue
, i
);
253 p
->mainloop
->defer_enable(p
->defer_event
, 1);
256 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
) {
257 assert(p
&& callback
);
259 p
->recieve_packet_callback
= callback
;
260 p
->recieve_packet_callback_userdata
= userdata
;
263 void pa_pstream_set_recieve_memblock_callback(struct pa_pstream
*p
, void (*callback
) (struct pa_pstream
*p
, uint32_t channel
, uint32_t delta
, const struct pa_memchunk
*chunk
, void *userdata
), void *userdata
) {
264 assert(p
&& callback
);
266 p
->recieve_memblock_callback
= callback
;
267 p
->recieve_memblock_callback_userdata
= userdata
;
270 static void prepare_next_write_item(struct pa_pstream
*p
) {
273 if (!(p
->write
.current
= pa_queue_pop(p
->send_queue
)))
278 if (p
->write
.current
->type
== PA_PSTREAM_ITEM_PACKET
) {
279 /*pa_log(__FILE__": pop-packet %p\n", p->write.current->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
= (uint8_t*) 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
= (uint8_t*) p
->write
.descriptor
+ p
->write
.index
;
311 l
= PA_PSTREAM_DESCRIPTOR_SIZE
- p
->write
.index
;
313 d
= (uint8_t*) 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
= (uint8_t*) p
->read
.descriptor
+ p
->read
.index
;
347 l
= PA_PSTREAM_DESCRIPTOR_SIZE
- p
->read
.index
;
349 assert(p
->read
.data
);
350 d
= (uint8_t*) 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 pa_log(__FILE__
": 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
]), p
->memblock_stat
);
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 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
;
459 void pa_pstream_unref(struct pa_pstream
*p
) {
460 assert(p
&& p
->ref
>= 1);
466 struct pa_pstream
* pa_pstream_ref(struct pa_pstream
*p
) {
467 assert(p
&& p
->ref
>= 1);
472 void pa_pstream_close(struct pa_pstream
*p
) {
478 pa_iochannel_free(p
->io
);
482 if (p
->defer_event
) {
483 p
->mainloop
->defer_free(p
->defer_event
);
484 p
->defer_event
= NULL
;
487 p
->die_callback
= NULL
;
488 p
->drain_callback
= NULL
;
489 p
->recieve_packet_callback
= NULL
;
490 p
->recieve_memblock_callback
= NULL
;