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
30 #include <netinet/in.h>
37 enum pa_pstream_descriptor_index
{
38 PA_PSTREAM_DESCRIPTOR_LENGTH
,
39 PA_PSTREAM_DESCRIPTOR_CHANNEL
,
40 PA_PSTREAM_DESCRIPTOR_DELTA
,
41 PA_PSTREAM_DESCRIPTOR_MAX
44 typedef uint32_t pa_pstream_descriptor
[PA_PSTREAM_DESCRIPTOR_MAX
];
46 #define PA_PSTREAM_DESCRIPTOR_SIZE (PA_PSTREAM_DESCRIPTOR_MAX*sizeof(uint32_t))
47 #define FRAME_SIZE_MAX (1024*500) /* half a megabyte */
50 enum { PA_PSTREAM_ITEM_PACKET
, PA_PSTREAM_ITEM_MEMBLOCK
} type
;
53 struct pa_memchunk chunk
;
58 struct pa_packet
*packet
;
64 struct pa_mainloop_api
*mainloop
;
65 struct pa_defer_event
*defer_event
;
66 struct pa_iochannel
*io
;
67 struct pa_queue
*send_queue
;
70 void (*die_callback
) (struct pa_pstream
*p
, void *userdata
);
71 void *die_callback_userdata
;
74 struct item_info
* current
;
75 pa_pstream_descriptor descriptor
;
81 struct pa_memblock
*memblock
;
82 struct pa_packet
*packet
;
83 pa_pstream_descriptor descriptor
;
88 void (*recieve_packet_callback
) (struct pa_pstream
*p
, struct pa_packet
*packet
, void *userdata
);
89 void *recieve_packet_callback_userdata
;
91 void (*recieve_memblock_callback
) (struct pa_pstream
*p
, uint32_t channel
, uint32_t delta
, const struct pa_memchunk
*chunk
, void *userdata
);
92 void *recieve_memblock_callback_userdata
;
94 void (*drain_callback
)(struct pa_pstream
*p
, void *userdata
);
97 struct pa_memblock_stat
*memblock_stat
;
100 static void do_write(struct pa_pstream
*p
);
101 static void do_read(struct pa_pstream
*p
);
103 static void do_something(struct pa_pstream
*p
) {
109 p
->mainloop
->defer_enable(p
->defer_event
, 0);
113 if (!p
->dead
&& pa_iochannel_is_hungup(p
->io
)) {
116 p
->die_callback(p
, p
->die_callback_userdata
);
119 if (!p
->dead
&& pa_iochannel_is_writable(p
->io
))
122 if (!p
->dead
&& pa_iochannel_is_readable(p
->io
))
128 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
129 struct pa_pstream
*p
= userdata
;
130 assert(p
&& p
->io
== io
);
134 static void defer_callback(struct pa_mainloop_api
*m
, struct pa_defer_event
*e
, void*userdata
) {
135 struct pa_pstream
*p
= userdata
;
136 assert(p
&& p
->defer_event
== e
&& p
->mainloop
== m
);
140 struct pa_pstream
*pa_pstream_new(struct pa_mainloop_api
*m
, struct pa_iochannel
*io
, struct pa_memblock_stat
*s
) {
141 struct pa_pstream
*p
;
144 p
= pa_xmalloc(sizeof(struct pa_pstream
));
147 pa_iochannel_set_callback(io
, io_callback
, p
);
150 p
->die_callback
= NULL
;
151 p
->die_callback_userdata
= NULL
;
154 p
->defer_event
= m
->defer_new(m
, defer_callback
, p
);
155 m
->defer_enable(p
->defer_event
, 0);
157 p
->send_queue
= pa_queue_new();
158 assert(p
->send_queue
);
160 p
->write
.current
= NULL
;
163 p
->read
.memblock
= NULL
;
164 p
->read
.packet
= NULL
;
167 p
->recieve_packet_callback
= NULL
;
168 p
->recieve_packet_callback_userdata
= NULL
;
170 p
->recieve_memblock_callback
= NULL
;
171 p
->recieve_memblock_callback_userdata
= NULL
;
173 p
->drain_callback
= NULL
;
174 p
->drain_userdata
= NULL
;
176 p
->memblock_stat
= s
;
178 pa_iochannel_socket_set_rcvbuf(io
, 1024*8);
179 pa_iochannel_socket_set_sndbuf(io
, 1024*8);
184 static void item_free(void *item
, void *p
) {
185 struct item_info
*i
= item
;
188 if (i
->type
== PA_PSTREAM_ITEM_MEMBLOCK
) {
189 assert(i
->chunk
.memblock
);
190 pa_memblock_unref(i
->chunk
.memblock
);
192 assert(i
->type
== PA_PSTREAM_ITEM_PACKET
);
194 pa_packet_unref(i
->packet
);
200 static void pstream_free(struct pa_pstream
*p
) {
205 pa_queue_free(p
->send_queue
, item_free
, NULL
);
207 if (p
->write
.current
)
208 item_free(p
->write
.current
, NULL
);
210 if (p
->read
.memblock
)
211 pa_memblock_unref(p
->read
.memblock
);
214 pa_packet_unref(p
->read
.packet
);
219 void pa_pstream_send_packet(struct pa_pstream
*p
, struct pa_packet
*packet
) {
221 assert(p
&& packet
&& p
->ref
>= 1);
226 /* pa_log(__FILE__": push-packet %p\n", packet); */
228 i
= pa_xmalloc(sizeof(struct item_info
));
229 i
->type
= PA_PSTREAM_ITEM_PACKET
;
230 i
->packet
= pa_packet_ref(packet
);
232 pa_queue_push(p
->send_queue
, i
);
233 p
->mainloop
->defer_enable(p
->defer_event
, 1);
236 void pa_pstream_send_memblock(struct pa_pstream
*p
, uint32_t channel
, uint32_t delta
, const struct pa_memchunk
*chunk
) {
238 assert(p
&& channel
!= (uint32_t) -1 && chunk
&& p
->ref
>= 1);
243 /* pa_log(__FILE__": push-memblock %p\n", chunk); */
245 i
= pa_xmalloc(sizeof(struct item_info
));
246 i
->type
= PA_PSTREAM_ITEM_MEMBLOCK
;
248 i
->channel
= channel
;
251 pa_memblock_ref(i
->chunk
.memblock
);
253 pa_queue_push(p
->send_queue
, i
);
254 p
->mainloop
->defer_enable(p
->defer_event
, 1);
257 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
) {
258 assert(p
&& callback
);
260 p
->recieve_packet_callback
= callback
;
261 p
->recieve_packet_callback_userdata
= userdata
;
264 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
) {
265 assert(p
&& callback
);
267 p
->recieve_memblock_callback
= callback
;
268 p
->recieve_memblock_callback_userdata
= userdata
;
271 static void prepare_next_write_item(struct pa_pstream
*p
) {
274 if (!(p
->write
.current
= pa_queue_pop(p
->send_queue
)))
279 if (p
->write
.current
->type
== PA_PSTREAM_ITEM_PACKET
) {
280 /*pa_log(__FILE__": pop-packet %p\n", p->write.current->packet);*/
282 assert(p
->write
.current
->packet
);
283 p
->write
.data
= p
->write
.current
->packet
->data
;
284 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
] = htonl(p
->write
.current
->packet
->length
);
285 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
] = htonl((uint32_t) -1);
286 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_DELTA
] = 0;
288 assert(p
->write
.current
->type
== PA_PSTREAM_ITEM_MEMBLOCK
&& p
->write
.current
->chunk
.memblock
);
289 p
->write
.data
= (uint8_t*) p
->write
.current
->chunk
.memblock
->data
+ p
->write
.current
->chunk
.index
;
290 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
] = htonl(p
->write
.current
->chunk
.length
);
291 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
] = htonl(p
->write
.current
->channel
);
292 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_DELTA
] = htonl(p
->write
.current
->delta
);
296 static void do_write(struct pa_pstream
*p
) {
302 if (!p
->write
.current
)
303 prepare_next_write_item(p
);
305 if (!p
->write
.current
)
308 assert(p
->write
.data
);
310 if (p
->write
.index
< PA_PSTREAM_DESCRIPTOR_SIZE
) {
311 d
= (uint8_t*) p
->write
.descriptor
+ p
->write
.index
;
312 l
= PA_PSTREAM_DESCRIPTOR_SIZE
- p
->write
.index
;
314 d
= (uint8_t*) p
->write
.data
+ p
->write
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
;
315 l
= ntohl(p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) - (p
->write
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
);
318 if ((r
= pa_iochannel_write(p
->io
, d
, l
)) < 0)
323 if (p
->write
.index
>= PA_PSTREAM_DESCRIPTOR_SIZE
+ntohl(p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
])) {
324 assert(p
->write
.current
);
325 item_free(p
->write
.current
, (void *) 1);
326 p
->write
.current
= NULL
;
328 if (p
->drain_callback
&& !pa_pstream_is_pending(p
))
329 p
->drain_callback(p
, p
->drain_userdata
);
337 p
->die_callback(p
, p
->die_callback_userdata
);
340 static void do_read(struct pa_pstream
*p
) {
346 if (p
->read
.index
< PA_PSTREAM_DESCRIPTOR_SIZE
) {
347 d
= (uint8_t*) p
->read
.descriptor
+ p
->read
.index
;
348 l
= PA_PSTREAM_DESCRIPTOR_SIZE
- p
->read
.index
;
350 assert(p
->read
.data
);
351 d
= (uint8_t*) p
->read
.data
+ p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
;
352 l
= ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) - (p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
);
355 if ((r
= pa_iochannel_read(p
->io
, d
, l
)) <= 0)
360 if (p
->read
.index
== PA_PSTREAM_DESCRIPTOR_SIZE
) {
361 /* Reading of frame descriptor complete */
363 /* Frame size too large */
364 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) > FRAME_SIZE_MAX
) {
365 pa_log(__FILE__
": Frame size too large\n");
369 assert(!p
->read
.packet
&& !p
->read
.memblock
);
371 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
]) == (uint32_t) -1) {
372 /* Frame is a packet frame */
373 p
->read
.packet
= pa_packet_new(ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]));
374 assert(p
->read
.packet
);
375 p
->read
.data
= p
->read
.packet
->data
;
377 /* Frame is a memblock frame */
378 p
->read
.memblock
= pa_memblock_new(ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]), p
->memblock_stat
);
379 assert(p
->read
.memblock
);
380 p
->read
.data
= p
->read
.memblock
->data
;
383 } else if (p
->read
.index
> PA_PSTREAM_DESCRIPTOR_SIZE
) {
384 /* Frame payload available */
386 if (p
->read
.memblock
&& p
->recieve_memblock_callback
) { /* Is this memblock data? Than pass it to the user */
389 l
= (p
->read
.index
- r
) < PA_PSTREAM_DESCRIPTOR_SIZE
? p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
: (size_t) r
;
392 struct pa_memchunk chunk
;
394 chunk
.memblock
= p
->read
.memblock
;
395 chunk
.index
= p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
- l
;
398 if (p
->recieve_memblock_callback
)
399 p
->recieve_memblock_callback(
401 ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
]),
402 ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_DELTA
]),
404 p
->recieve_memblock_callback_userdata
);
409 if (p
->read
.index
>= ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) + PA_PSTREAM_DESCRIPTOR_SIZE
) {
410 if (p
->read
.memblock
) {
411 assert(!p
->read
.packet
);
413 pa_memblock_unref(p
->read
.memblock
);
414 p
->read
.memblock
= NULL
;
416 assert(p
->read
.packet
);
418 if (p
->recieve_packet_callback
)
419 p
->recieve_packet_callback(p
, p
->read
.packet
, p
->recieve_packet_callback_userdata
);
421 pa_packet_unref(p
->read
.packet
);
422 p
->read
.packet
= NULL
;
434 p
->die_callback(p
, p
->die_callback_userdata
);
438 void pa_pstream_set_die_callback(struct pa_pstream
*p
, void (*callback
)(struct pa_pstream
*p
, void *userdata
), void *userdata
) {
439 assert(p
&& callback
);
440 p
->die_callback
= callback
;
441 p
->die_callback_userdata
= userdata
;
444 int pa_pstream_is_pending(struct pa_pstream
*p
) {
450 return p
->write
.current
|| !pa_queue_is_empty(p
->send_queue
);
453 void pa_pstream_set_drain_callback(struct pa_pstream
*p
, void (*cb
)(struct pa_pstream
*p
, void *userdata
), void *userdata
) {
456 p
->drain_callback
= cb
;
457 p
->drain_userdata
= userdata
;
460 void pa_pstream_unref(struct pa_pstream
*p
) {
461 assert(p
&& p
->ref
>= 1);
467 struct pa_pstream
* pa_pstream_ref(struct pa_pstream
*p
) {
468 assert(p
&& p
->ref
>= 1);
473 void pa_pstream_close(struct pa_pstream
*p
) {
479 pa_iochannel_free(p
->io
);
483 if (p
->defer_event
) {
484 p
->mainloop
->defer_free(p
->defer_event
);
485 p
->defer_event
= NULL
;
488 p
->die_callback
= NULL
;
489 p
->drain_callback
= NULL
;
490 p
->recieve_packet_callback
= NULL
;
491 p
->recieve_memblock_callback
= NULL
;