This file is part of polypaudio.
polypaudio is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 2 of the License,
- or (at your option) any later version.
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
polypaudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ Lesser General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with polypaudio; if not, write to the Free Software
+ You should have received a copy of the GNU Lesser General Public
+ License along with polypaudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#include <config.h>
#endif
+#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
+#include <unistd.h>
#include <netinet/in.h>
#include "pstream.h"
#include "queue.h"
+#include "xmalloc.h"
+#include "log.h"
enum pa_pstream_descriptor_index {
PA_PSTREAM_DESCRIPTOR_LENGTH,
typedef uint32_t pa_pstream_descriptor[PA_PSTREAM_DESCRIPTOR_MAX];
#define PA_PSTREAM_DESCRIPTOR_SIZE (PA_PSTREAM_DESCRIPTOR_MAX*sizeof(uint32_t))
-#define FRAME_SIZE_MAX (1024*64)
+#define FRAME_SIZE_MAX (1024*500) /* half a megabyte */
struct item_info {
enum { PA_PSTREAM_ITEM_PACKET, PA_PSTREAM_ITEM_MEMBLOCK } type;
/* memblock info */
struct pa_memchunk chunk;
uint32_t channel;
- int32_t delta;
+ uint32_t delta;
/* packet info */
struct pa_packet *packet;
};
struct pa_pstream {
+ int ref;
+
struct pa_mainloop_api *mainloop;
- struct mainloop_source *mainloop_source;
+ struct pa_defer_event *defer_event;
struct pa_iochannel *io;
struct pa_queue *send_queue;
- int in_use, shall_free;
-
int dead;
void (*die_callback) (struct pa_pstream *p, void *userdata);
void *die_callback_userdata;
void (*recieve_packet_callback) (struct pa_pstream *p, struct pa_packet *packet, void *userdata);
void *recieve_packet_callback_userdata;
- void (*recieve_memblock_callback) (struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata);
+ void (*recieve_memblock_callback) (struct pa_pstream *p, uint32_t channel, uint32_t delta, const struct pa_memchunk *chunk, void *userdata);
void *recieve_memblock_callback_userdata;
void (*drain_callback)(struct pa_pstream *p, void *userdata);
void *drain_userdata;
+
+ struct pa_memblock_stat *memblock_stat;
};
static void do_write(struct pa_pstream *p);
static void do_read(struct pa_pstream *p);
static void do_something(struct pa_pstream *p) {
- assert(p && !p->shall_free);
- p->mainloop->enable_fixed(p->mainloop, p->mainloop_source, 0);
+ assert(p);
if (p->dead)
return;
+
+ p->mainloop->defer_enable(p->defer_event, 0);
- if (pa_iochannel_is_hungup(p->io)) {
+ pa_pstream_ref(p);
+
+ if (!p->dead && pa_iochannel_is_hungup(p->io)) {
p->dead = 1;
if (p->die_callback)
p->die_callback(p, p->die_callback_userdata);
-
- return;
}
- if (pa_iochannel_is_writable(p->io)) {
- p->in_use = 1;
+ if (!p->dead && pa_iochannel_is_writable(p->io))
do_write(p);
- p->in_use = 0;
-
- if (p->shall_free) {
- pa_pstream_free(p);
- return;
- }
- }
- if (pa_iochannel_is_readable(p->io)) {
- p->in_use = 1;
+ if (!p->dead && pa_iochannel_is_readable(p->io))
do_read(p);
- p->in_use = 0;
- if (p->shall_free) {
- pa_pstream_free(p);
- return;
- }
- }
+
+ pa_pstream_unref(p);
}
static void io_callback(struct pa_iochannel*io, void *userdata) {
do_something(p);
}
-static void fixed_callback(struct pa_mainloop_api *m, void *id, void*userdata) {
+static void defer_callback(struct pa_mainloop_api *m, struct pa_defer_event *e, void*userdata) {
struct pa_pstream *p = userdata;
- assert(p && p->mainloop_source == id && p->mainloop == m);
+ assert(p && p->defer_event == e && p->mainloop == m);
do_something(p);
}
-struct pa_pstream *pa_pstream_new(struct pa_mainloop_api *m, struct pa_iochannel *io) {
+struct pa_pstream *pa_pstream_new(struct pa_mainloop_api *m, struct pa_iochannel *io, struct pa_memblock_stat *s) {
struct pa_pstream *p;
assert(io);
- p = malloc(sizeof(struct pa_pstream));
- assert(p);
-
+ p = pa_xmalloc(sizeof(struct pa_pstream));
+ p->ref = 1;
p->io = io;
pa_iochannel_set_callback(io, io_callback, p);
p->die_callback_userdata = NULL;
p->mainloop = m;
- p->mainloop_source = m->source_fixed(m, fixed_callback, p);
- m->enable_fixed(m, p->mainloop_source, 0);
+ p->defer_event = m->defer_new(m, defer_callback, p);
+ m->defer_enable(p->defer_event, 0);
p->send_queue = pa_queue_new();
assert(p->send_queue);
p->drain_callback = NULL;
p->drain_userdata = NULL;
- p->in_use = p->shall_free = 0;
+ p->memblock_stat = s;
+
+ pa_iochannel_socket_set_rcvbuf(io, 1024*8);
+ pa_iochannel_socket_set_sndbuf(io, 1024*8);
return p;
}
pa_packet_unref(i->packet);
}
- free(i);
+ pa_xfree(i);
}
-void pa_pstream_free(struct pa_pstream *p) {
+static void pstream_free(struct pa_pstream *p) {
assert(p);
- if (p->in_use) {
- /* If this pstream object is used by someone else on the call stack, we have to postpone the freeing */
- p->dead = p->shall_free = 1;
- return;
- }
-
- pa_iochannel_free(p->io);
+ pa_pstream_close(p);
+
pa_queue_free(p->send_queue, item_free, NULL);
if (p->write.current)
if (p->read.packet)
pa_packet_unref(p->read.packet);
- p->mainloop->cancel_fixed(p->mainloop, p->mainloop_source);
- free(p);
+ pa_xfree(p);
}
void pa_pstream_send_packet(struct pa_pstream*p, struct pa_packet *packet) {
struct item_info *i;
- assert(p && packet);
+ assert(p && packet && p->ref >= 1);
- i = malloc(sizeof(struct item_info));
- assert(i);
+ if (p->dead)
+ return;
+
+/* pa_log(__FILE__": push-packet %p\n", packet); */
+
+ i = pa_xmalloc(sizeof(struct item_info));
i->type = PA_PSTREAM_ITEM_PACKET;
i->packet = pa_packet_ref(packet);
pa_queue_push(p->send_queue, i);
- p->mainloop->enable_fixed(p->mainloop, p->mainloop_source, 1);
+ p->mainloop->defer_enable(p->defer_event, 1);
}
-void pa_pstream_send_memblock(struct pa_pstream*p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk) {
+void pa_pstream_send_memblock(struct pa_pstream*p, uint32_t channel, uint32_t delta, const struct pa_memchunk *chunk) {
struct item_info *i;
- assert(p && channel != (uint32_t) -1 && chunk);
+ assert(p && channel != (uint32_t) -1 && chunk && p->ref >= 1);
+
+ if (p->dead)
+ return;
- i = malloc(sizeof(struct item_info));
- assert(i);
+/* pa_log(__FILE__": push-memblock %p\n", chunk); */
+
+ i = pa_xmalloc(sizeof(struct item_info));
i->type = PA_PSTREAM_ITEM_MEMBLOCK;
i->chunk = *chunk;
i->channel = channel;
pa_memblock_ref(i->chunk.memblock);
pa_queue_push(p->send_queue, i);
- p->mainloop->enable_fixed(p->mainloop, p->mainloop_source, 1);
+ p->mainloop->defer_enable(p->defer_event, 1);
}
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) {
p->recieve_packet_callback_userdata = userdata;
}
-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) {
+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) {
assert(p && callback);
p->recieve_memblock_callback = callback;
p->write.index = 0;
if (p->write.current->type == PA_PSTREAM_ITEM_PACKET) {
+ /*pa_log(__FILE__": pop-packet %p\n", p->write.current->packet);*/
+
assert(p->write.current->packet);
p->write.data = p->write.current->packet->data;
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH] = htonl(p->write.current->packet->length);
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_DELTA] = 0;
} else {
assert(p->write.current->type == PA_PSTREAM_ITEM_MEMBLOCK && p->write.current->chunk.memblock);
- p->write.data = p->write.current->chunk.memblock->data + p->write.current->chunk.index;
+ p->write.data = (uint8_t*) p->write.current->chunk.memblock->data + p->write.current->chunk.index;
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH] = htonl(p->write.current->chunk.length);
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL] = htonl(p->write.current->channel);
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_DELTA] = htonl(p->write.current->delta);
assert(p->write.data);
if (p->write.index < PA_PSTREAM_DESCRIPTOR_SIZE) {
- d = (void*) p->write.descriptor + p->write.index;
+ d = (uint8_t*) p->write.descriptor + p->write.index;
l = PA_PSTREAM_DESCRIPTOR_SIZE - p->write.index;
} else {
- d = (void*) p->write.data + p->write.index - PA_PSTREAM_DESCRIPTOR_SIZE;
+ d = (uint8_t*) p->write.data + p->write.index - PA_PSTREAM_DESCRIPTOR_SIZE;
l = ntohl(p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) - (p->write.index - PA_PSTREAM_DESCRIPTOR_SIZE);
}
assert(p);
if (p->read.index < PA_PSTREAM_DESCRIPTOR_SIZE) {
- d = (void*) p->read.descriptor + p->read.index;
+ d = (uint8_t*) p->read.descriptor + p->read.index;
l = PA_PSTREAM_DESCRIPTOR_SIZE - p->read.index;
} else {
assert(p->read.data);
- d = (void*) p->read.data + p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE;
+ d = (uint8_t*) p->read.data + p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE;
l = ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) - (p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE);
}
/* Reading of frame descriptor complete */
/* Frame size too large */
- if (ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) > FRAME_SIZE_MAX)
+ if (ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) > FRAME_SIZE_MAX) {
+ pa_log(__FILE__": Frame size too large\n");
goto die;
+ }
assert(!p->read.packet && !p->read.memblock);
p->read.data = p->read.packet->data;
} else {
/* Frame is a memblock frame */
- p->read.memblock = pa_memblock_new(ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]));
+ p->read.memblock = pa_memblock_new(ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]), p->memblock_stat);
assert(p->read.memblock);
p->read.data = p->read.memblock->data;
}
p->recieve_memblock_callback(
p,
ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL]),
- (int32_t) ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_DELTA]),
+ ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_DELTA]),
&chunk,
p->recieve_memblock_callback_userdata);
}
p->drain_userdata = userdata;
}
+void pa_pstream_unref(struct pa_pstream*p) {
+ assert(p && p->ref >= 1);
+
+ if (!(--(p->ref)))
+ pstream_free(p);
+}
+
+struct pa_pstream* pa_pstream_ref(struct pa_pstream*p) {
+ assert(p && p->ref >= 1);
+ p->ref++;
+ return p;
+}
+
+void pa_pstream_close(struct pa_pstream *p) {
+ assert(p);
+
+ p->dead = 1;
+
+ if (p->io) {
+ pa_iochannel_free(p->io);
+ p->io = NULL;
+ }
+
+ if (p->defer_event) {
+ p->mainloop->defer_free(p->defer_event);
+ p->defer_event = NULL;
+ }
+
+ p->die_callback = NULL;
+ p->drain_callback = NULL;
+ p->recieve_packet_callback = NULL;
+ p->recieve_memblock_callback = NULL;
+}