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
;
62 struct pa_mainloop_api
*mainloop
;
63 struct pa_defer_event
*defer_event
;
64 struct pa_iochannel
*io
;
65 struct pa_queue
*send_queue
;
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
, uint32_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
);
95 struct pa_memblock_stat
*memblock_stat
;
98 static void do_write(struct pa_pstream
*p
);
99 static void do_read(struct pa_pstream
*p
);
101 static void do_something(struct pa_pstream
*p
) {
103 p
->mainloop
->defer_enable(p
->defer_event
, 0);
107 if (!p
->dead
&& pa_iochannel_is_hungup(p
->io
)) {
110 p
->die_callback(p
, p
->die_callback_userdata
);
113 if (!p
->dead
&& pa_iochannel_is_writable(p
->io
))
116 if (!p
->dead
&& pa_iochannel_is_readable(p
->io
))
122 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
123 struct pa_pstream
*p
= userdata
;
124 assert(p
&& p
->io
== io
);
128 static void defer_callback(struct pa_mainloop_api
*m
, struct pa_defer_event
*e
, void*userdata
) {
129 struct pa_pstream
*p
= userdata
;
130 assert(p
&& p
->defer_event
== e
&& p
->mainloop
== m
);
134 struct pa_pstream
*pa_pstream_new(struct pa_mainloop_api
*m
, struct pa_iochannel
*io
, struct pa_memblock_stat
*s
) {
135 struct pa_pstream
*p
;
138 p
= pa_xmalloc(sizeof(struct pa_pstream
));
141 pa_iochannel_set_callback(io
, io_callback
, p
);
144 p
->die_callback
= NULL
;
145 p
->die_callback_userdata
= NULL
;
148 p
->defer_event
= m
->defer_new(m
, defer_callback
, p
);
149 m
->defer_enable(p
->defer_event
, 0);
151 p
->send_queue
= pa_queue_new();
152 assert(p
->send_queue
);
154 p
->write
.current
= NULL
;
157 p
->read
.memblock
= NULL
;
158 p
->read
.packet
= NULL
;
161 p
->recieve_packet_callback
= NULL
;
162 p
->recieve_packet_callback_userdata
= NULL
;
164 p
->recieve_memblock_callback
= NULL
;
165 p
->recieve_memblock_callback_userdata
= NULL
;
167 p
->drain_callback
= NULL
;
168 p
->drain_userdata
= NULL
;
170 p
->memblock_stat
= s
;
175 static void item_free(void *item
, void *p
) {
176 struct item_info
*i
= item
;
179 if (i
->type
== PA_PSTREAM_ITEM_MEMBLOCK
) {
180 assert(i
->chunk
.memblock
);
181 pa_memblock_unref(i
->chunk
.memblock
);
183 assert(i
->type
== PA_PSTREAM_ITEM_PACKET
);
185 pa_packet_unref(i
->packet
);
191 static void pstream_free(struct pa_pstream
*p
) {
196 pa_queue_free(p
->send_queue
, item_free
, NULL
);
198 if (p
->write
.current
)
199 item_free(p
->write
.current
, NULL
);
201 if (p
->read
.memblock
)
202 pa_memblock_unref(p
->read
.memblock
);
205 pa_packet_unref(p
->read
.packet
);
210 void pa_pstream_send_packet(struct pa_pstream
*p
, struct pa_packet
*packet
) {
214 /*fprintf(stderr, "push-packet %p\n", packet);*/
216 i
= pa_xmalloc(sizeof(struct item_info
));
217 i
->type
= PA_PSTREAM_ITEM_PACKET
;
218 i
->packet
= pa_packet_ref(packet
);
220 pa_queue_push(p
->send_queue
, i
);
221 p
->mainloop
->defer_enable(p
->defer_event
, 1);
224 void pa_pstream_send_memblock(struct pa_pstream
*p
, uint32_t channel
, uint32_t delta
, const struct pa_memchunk
*chunk
) {
226 assert(p
&& channel
!= (uint32_t) -1 && chunk
);
228 i
= pa_xmalloc(sizeof(struct item_info
));
229 i
->type
= PA_PSTREAM_ITEM_MEMBLOCK
;
231 i
->channel
= channel
;
234 pa_memblock_ref(i
->chunk
.memblock
);
236 pa_queue_push(p
->send_queue
, i
);
237 p
->mainloop
->defer_enable(p
->defer_event
, 1);
240 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
) {
241 assert(p
&& callback
);
243 p
->recieve_packet_callback
= callback
;
244 p
->recieve_packet_callback_userdata
= userdata
;
247 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
) {
248 assert(p
&& callback
);
250 p
->recieve_memblock_callback
= callback
;
251 p
->recieve_memblock_callback_userdata
= userdata
;
254 static void prepare_next_write_item(struct pa_pstream
*p
) {
257 if (!(p
->write
.current
= pa_queue_pop(p
->send_queue
)))
262 if (p
->write
.current
->type
== PA_PSTREAM_ITEM_PACKET
) {
263 /*fprintf(stderr, "pop-packet %p\n", p->write.current->packet);*/
265 assert(p
->write
.current
->packet
);
266 p
->write
.data
= p
->write
.current
->packet
->data
;
267 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
] = htonl(p
->write
.current
->packet
->length
);
268 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
] = htonl((uint32_t) -1);
269 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_DELTA
] = 0;
271 assert(p
->write
.current
->type
== PA_PSTREAM_ITEM_MEMBLOCK
&& p
->write
.current
->chunk
.memblock
);
272 p
->write
.data
= p
->write
.current
->chunk
.memblock
->data
+ p
->write
.current
->chunk
.index
;
273 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
] = htonl(p
->write
.current
->chunk
.length
);
274 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
] = htonl(p
->write
.current
->channel
);
275 p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_DELTA
] = htonl(p
->write
.current
->delta
);
279 static void do_write(struct pa_pstream
*p
) {
285 if (!p
->write
.current
)
286 prepare_next_write_item(p
);
288 if (!p
->write
.current
)
291 assert(p
->write
.data
);
293 if (p
->write
.index
< PA_PSTREAM_DESCRIPTOR_SIZE
) {
294 d
= (void*) p
->write
.descriptor
+ p
->write
.index
;
295 l
= PA_PSTREAM_DESCRIPTOR_SIZE
- p
->write
.index
;
297 d
= (void*) p
->write
.data
+ p
->write
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
;
298 l
= ntohl(p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) - (p
->write
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
);
301 if ((r
= pa_iochannel_write(p
->io
, d
, l
)) < 0)
306 if (p
->write
.index
>= PA_PSTREAM_DESCRIPTOR_SIZE
+ntohl(p
->write
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
])) {
307 assert(p
->write
.current
);
308 item_free(p
->write
.current
, (void *) 1);
309 p
->write
.current
= NULL
;
311 if (p
->drain_callback
&& !pa_pstream_is_pending(p
))
312 p
->drain_callback(p
, p
->drain_userdata
);
320 p
->die_callback(p
, p
->die_callback_userdata
);
323 static void do_read(struct pa_pstream
*p
) {
329 if (p
->read
.index
< PA_PSTREAM_DESCRIPTOR_SIZE
) {
330 d
= (void*) p
->read
.descriptor
+ p
->read
.index
;
331 l
= PA_PSTREAM_DESCRIPTOR_SIZE
- p
->read
.index
;
333 assert(p
->read
.data
);
334 d
= (void*) p
->read
.data
+ p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
;
335 l
= ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) - (p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
);
338 if ((r
= pa_iochannel_read(p
->io
, d
, l
)) <= 0)
343 if (p
->read
.index
== PA_PSTREAM_DESCRIPTOR_SIZE
) {
344 /* Reading of frame descriptor complete */
346 /* Frame size too large */
347 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) > FRAME_SIZE_MAX
) {
348 fprintf(stderr
, "frame size too large\n");
352 assert(!p
->read
.packet
&& !p
->read
.memblock
);
354 if (ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
]) == (uint32_t) -1) {
355 /* Frame is a packet frame */
356 p
->read
.packet
= pa_packet_new(ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]));
357 assert(p
->read
.packet
);
358 p
->read
.data
= p
->read
.packet
->data
;
360 /* Frame is a memblock frame */
361 p
->read
.memblock
= pa_memblock_new(ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]), p
->memblock_stat
);
362 assert(p
->read
.memblock
);
363 p
->read
.data
= p
->read
.memblock
->data
;
366 } else if (p
->read
.index
> PA_PSTREAM_DESCRIPTOR_SIZE
) {
367 /* Frame payload available */
369 if (p
->read
.memblock
&& p
->recieve_memblock_callback
) { /* Is this memblock data? Than pass it to the user */
372 l
= (p
->read
.index
- r
) < PA_PSTREAM_DESCRIPTOR_SIZE
? p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
: (size_t) r
;
375 struct pa_memchunk chunk
;
377 chunk
.memblock
= p
->read
.memblock
;
378 chunk
.index
= p
->read
.index
- PA_PSTREAM_DESCRIPTOR_SIZE
- l
;
381 if (p
->recieve_memblock_callback
)
382 p
->recieve_memblock_callback(
384 ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_CHANNEL
]),
385 ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_DELTA
]),
387 p
->recieve_memblock_callback_userdata
);
392 if (p
->read
.index
>= ntohl(p
->read
.descriptor
[PA_PSTREAM_DESCRIPTOR_LENGTH
]) + PA_PSTREAM_DESCRIPTOR_SIZE
) {
393 if (p
->read
.memblock
) {
394 assert(!p
->read
.packet
);
396 pa_memblock_unref(p
->read
.memblock
);
397 p
->read
.memblock
= NULL
;
399 assert(p
->read
.packet
);
401 if (p
->recieve_packet_callback
)
402 p
->recieve_packet_callback(p
, p
->read
.packet
, p
->recieve_packet_callback_userdata
);
404 pa_packet_unref(p
->read
.packet
);
405 p
->read
.packet
= NULL
;
417 p
->die_callback(p
, p
->die_callback_userdata
);
421 void pa_pstream_set_die_callback(struct pa_pstream
*p
, void (*callback
)(struct pa_pstream
*p
, void *userdata
), void *userdata
) {
422 assert(p
&& callback
);
423 p
->die_callback
= callback
;
424 p
->die_callback_userdata
= userdata
;
427 int pa_pstream_is_pending(struct pa_pstream
*p
) {
433 return p
->write
.current
|| !pa_queue_is_empty(p
->send_queue
);
436 void pa_pstream_set_drain_callback(struct pa_pstream
*p
, void (*cb
)(struct pa_pstream
*p
, void *userdata
), void *userdata
) {
439 p
->drain_callback
= cb
;
440 p
->drain_userdata
= userdata
;
443 void pa_pstream_unref(struct pa_pstream
*p
) {
444 assert(p
&& p
->ref
>= 1);
450 struct pa_pstream
* pa_pstream_ref(struct pa_pstream
*p
) {
451 assert(p
&& p
->ref
>= 1);
456 void pa_pstream_close(struct pa_pstream
*p
) {
462 pa_iochannel_free(p
->io
);
466 if (p
->defer_event
) {
467 p
->mainloop
->defer_free(p
->defer_event
);
468 p
->defer_event
= NULL
;
471 p
->die_callback
= NULL
;
472 p
->drain_callback
= NULL
;
473 p
->recieve_packet_callback
= NULL
;
474 p
->recieve_memblock_callback
= NULL
;