2004-07-16 19:56:36 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2004-07-16 19:56:36 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
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.
|
2004-07-16 19:56:36 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2004-11-14 14:58:54 +00:00
|
|
|
Lesser General Public License for more details.
|
2004-07-16 19:56:36 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2006-06-19 21:53:48 +00:00
|
|
|
License along with PulseAudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-08-03 19:26:56 +00:00
|
|
|
#include <stdio.h>
|
2004-06-08 23:54:24 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <assert.h>
|
2004-09-23 15:57:15 +00:00
|
|
|
#include <unistd.h>
|
2006-07-20 00:13:12 +00:00
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2006-07-19 17:44:19 +00:00
|
|
|
#include <sys/socket.h>
|
2006-07-20 00:13:12 +00:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
2006-07-19 17:44:19 +00:00
|
|
|
#include <sys/un.h>
|
2006-07-20 00:13:12 +00:00
|
|
|
#endif
|
2006-01-10 17:51:06 +00:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2004-06-20 01:12:13 +00:00
|
|
|
#include <netinet/in.h>
|
2006-01-10 17:51:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "winsock.h"
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/queue.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
|
|
|
|
#include <pulsecore/core-scache.h>
|
2006-07-19 21:48:35 +00:00
|
|
|
#include <pulsecore/creds.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
#include "pstream.h"
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
enum {
|
2004-07-03 23:35:12 +00:00
|
|
|
PA_PSTREAM_DESCRIPTOR_LENGTH,
|
|
|
|
|
PA_PSTREAM_DESCRIPTOR_CHANNEL,
|
2006-02-20 04:05:16 +00:00
|
|
|
PA_PSTREAM_DESCRIPTOR_OFFSET_HI,
|
|
|
|
|
PA_PSTREAM_DESCRIPTOR_OFFSET_LO,
|
|
|
|
|
PA_PSTREAM_DESCRIPTOR_SEEK,
|
2004-07-03 23:35:12 +00:00
|
|
|
PA_PSTREAM_DESCRIPTOR_MAX
|
2006-02-20 04:05:16 +00:00
|
|
|
};
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
typedef uint32_t pa_pstream_descriptor[PA_PSTREAM_DESCRIPTOR_MAX];
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
#define PA_PSTREAM_DESCRIPTOR_SIZE (PA_PSTREAM_DESCRIPTOR_MAX*sizeof(uint32_t))
|
2006-04-23 20:59:09 +00:00
|
|
|
#define FRAME_SIZE_MAX PA_SCACHE_ENTRY_SIZE_MAX /* allow uploading a single sample in one frame at max */
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
struct item_info {
|
2004-07-03 23:35:12 +00:00
|
|
|
enum { PA_PSTREAM_ITEM_PACKET, PA_PSTREAM_ITEM_MEMBLOCK } type;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
/* memblock info */
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memchunk chunk;
|
2004-06-08 23:54:24 +00:00
|
|
|
uint32_t channel;
|
2006-02-20 04:05:16 +00:00
|
|
|
int64_t offset;
|
|
|
|
|
pa_seek_mode_t seek_mode;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
/* packet info */
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_packet *packet;
|
2006-07-19 21:48:35 +00:00
|
|
|
#ifdef HAVE_CREDS
|
2006-02-24 15:12:42 +00:00
|
|
|
int with_creds;
|
2006-07-19 21:48:35 +00:00
|
|
|
pa_creds creds;
|
2006-02-24 15:12:42 +00:00
|
|
|
#endif
|
2004-06-08 23:54:24 +00:00
|
|
|
};
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
struct pa_pstream {
|
2004-08-15 00:02:26 +00:00
|
|
|
int ref;
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_mainloop_api *mainloop;
|
|
|
|
|
pa_defer_event *defer_event;
|
|
|
|
|
pa_iochannel *io;
|
|
|
|
|
pa_queue *send_queue;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
int dead;
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
struct item_info* current;
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_pstream_descriptor descriptor;
|
2004-06-08 23:54:24 +00:00
|
|
|
void *data;
|
|
|
|
|
size_t index;
|
|
|
|
|
} write;
|
|
|
|
|
|
|
|
|
|
struct {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memblock *memblock;
|
|
|
|
|
pa_packet *packet;
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_pstream_descriptor descriptor;
|
2004-06-08 23:54:24 +00:00
|
|
|
void *data;
|
|
|
|
|
size_t index;
|
|
|
|
|
} read;
|
|
|
|
|
|
2006-02-24 15:12:42 +00:00
|
|
|
pa_pstream_packet_cb_t recieve_packet_callback;
|
2004-06-08 23:54:24 +00:00
|
|
|
void *recieve_packet_callback_userdata;
|
|
|
|
|
|
2006-02-24 15:12:42 +00:00
|
|
|
pa_pstream_memblock_cb_t recieve_memblock_callback;
|
2004-06-08 23:54:24 +00:00
|
|
|
void *recieve_memblock_callback_userdata;
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2006-02-24 15:12:42 +00:00
|
|
|
pa_pstream_notify_cb_t drain_callback;
|
|
|
|
|
void *drain_callback_userdata;
|
|
|
|
|
|
|
|
|
|
pa_pstream_notify_cb_t die_callback;
|
|
|
|
|
void *die_callback_userdata;
|
2004-08-17 19:37:29 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memblock_stat *memblock_stat;
|
2006-02-24 15:12:42 +00:00
|
|
|
|
2006-07-19 21:48:35 +00:00
|
|
|
#ifdef HAVE_CREDS
|
|
|
|
|
pa_creds read_creds, write_creds;
|
2006-07-19 17:44:19 +00:00
|
|
|
int read_creds_valid, send_creds_now;
|
2006-02-24 15:12:42 +00:00
|
|
|
#endif
|
2004-06-08 23:54:24 +00:00
|
|
|
};
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
static int do_write(pa_pstream *p);
|
|
|
|
|
static int do_read(pa_pstream *p);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void do_something(pa_pstream *p) {
|
2004-08-15 00:02:26 +00:00
|
|
|
assert(p);
|
2004-09-16 22:07:41 +00:00
|
|
|
|
2004-08-05 19:53:57 +00:00
|
|
|
p->mainloop->defer_enable(p->defer_event, 0);
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2004-08-15 00:02:26 +00:00
|
|
|
pa_pstream_ref(p);
|
2004-07-10 16:50:22 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if (!p->dead && pa_iochannel_is_readable(p->io)) {
|
|
|
|
|
if (do_read(p) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
} else if (!p->dead && pa_iochannel_is_hungup(p->io))
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
if (!p->dead && pa_iochannel_is_writable(p->io)) {
|
|
|
|
|
if (do_write(p) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_pstream_unref(p);
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
fail:
|
2004-07-10 16:50:22 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
p->dead = 1;
|
2004-11-18 20:50:44 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if (p->die_callback)
|
|
|
|
|
p->die_callback(p, p->die_callback_userdata);
|
2004-11-18 20:50:44 +00:00
|
|
|
|
2004-08-15 00:02:26 +00:00
|
|
|
pa_pstream_unref(p);
|
2004-07-07 00:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void io_callback(pa_iochannel*io, void *userdata) {
|
|
|
|
|
pa_pstream *p = userdata;
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
assert(p);
|
|
|
|
|
assert(p->io == io);
|
|
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
do_something(p);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void defer_callback(pa_mainloop_api *m, pa_defer_event *e, void*userdata) {
|
|
|
|
|
pa_pstream *p = userdata;
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
assert(p);
|
|
|
|
|
assert(p->defer_event == e);
|
|
|
|
|
assert(p->mainloop == m);
|
|
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
do_something(p);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_pstream *pa_pstream_new(pa_mainloop_api *m, pa_iochannel *io, pa_memblock_stat *s) {
|
|
|
|
|
pa_pstream *p;
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(io);
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
p = pa_xnew(pa_pstream, 1);
|
|
|
|
|
|
2004-08-15 00:02:26 +00:00
|
|
|
p->ref = 1;
|
2004-06-08 23:54:24 +00:00
|
|
|
p->io = io;
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_iochannel_set_callback(io, io_callback, p);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
p->dead = 0;
|
|
|
|
|
|
|
|
|
|
p->mainloop = m;
|
2004-08-05 19:53:57 +00:00
|
|
|
p->defer_event = m->defer_new(m, defer_callback, p);
|
|
|
|
|
m->defer_enable(p->defer_event, 0);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
p->send_queue = pa_queue_new();
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(p->send_queue);
|
|
|
|
|
|
|
|
|
|
p->write.current = NULL;
|
|
|
|
|
p->write.index = 0;
|
|
|
|
|
|
|
|
|
|
p->read.memblock = NULL;
|
|
|
|
|
p->read.packet = NULL;
|
|
|
|
|
p->read.index = 0;
|
|
|
|
|
|
|
|
|
|
p->recieve_packet_callback = NULL;
|
|
|
|
|
p->recieve_packet_callback_userdata = NULL;
|
|
|
|
|
|
|
|
|
|
p->recieve_memblock_callback = NULL;
|
|
|
|
|
p->recieve_memblock_callback_userdata = NULL;
|
|
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
p->drain_callback = NULL;
|
2006-02-24 15:12:42 +00:00
|
|
|
p->drain_callback_userdata = NULL;
|
|
|
|
|
|
|
|
|
|
p->die_callback = NULL;
|
|
|
|
|
p->die_callback_userdata = NULL;
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2004-08-17 19:37:29 +00:00
|
|
|
p->memblock_stat = s;
|
|
|
|
|
|
2004-09-01 00:23:51 +00:00
|
|
|
pa_iochannel_socket_set_rcvbuf(io, 1024*8);
|
2006-02-24 15:12:42 +00:00
|
|
|
pa_iochannel_socket_set_sndbuf(io, 1024*8);
|
2004-09-01 00:23:51 +00:00
|
|
|
|
2006-07-19 21:48:35 +00:00
|
|
|
#ifdef HAVE_CREDS
|
2006-02-24 15:12:42 +00:00
|
|
|
p->send_creds_now = 0;
|
2006-07-19 17:44:19 +00:00
|
|
|
p->read_creds_valid = 0;
|
2006-02-24 15:12:42 +00:00
|
|
|
#endif
|
2004-06-08 23:54:24 +00:00
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void item_free(void *item, PA_GCC_UNUSED void *p) {
|
2004-06-08 23:54:24 +00:00
|
|
|
struct item_info *i = item;
|
|
|
|
|
assert(i);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (i->type == PA_PSTREAM_ITEM_MEMBLOCK) {
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(i->chunk.memblock);
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_memblock_unref(i->chunk.memblock);
|
2004-06-08 23:54:24 +00:00
|
|
|
} else {
|
2004-07-03 23:35:12 +00:00
|
|
|
assert(i->type == PA_PSTREAM_ITEM_PACKET);
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(i->packet);
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_packet_unref(i->packet);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(i);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void pstream_free(pa_pstream *p) {
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(p);
|
|
|
|
|
|
2004-08-15 00:02:26 +00:00
|
|
|
pa_pstream_close(p);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_queue_free(p->send_queue, item_free, NULL);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
if (p->write.current)
|
|
|
|
|
item_free(p->write.current, NULL);
|
|
|
|
|
|
|
|
|
|
if (p->read.memblock)
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_memblock_unref(p->read.memblock);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
if (p->read.packet)
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_packet_unref(p->read.packet);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(p);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-07-19 21:48:35 +00:00
|
|
|
void pa_pstream_send_packet(pa_pstream*p, pa_packet *packet, const pa_creds *creds) {
|
2004-06-08 23:54:24 +00:00
|
|
|
struct item_info *i;
|
2004-09-16 22:07:41 +00:00
|
|
|
assert(p && packet && p->ref >= 1);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-09-16 22:07:41 +00:00
|
|
|
if (p->dead)
|
|
|
|
|
return;
|
|
|
|
|
|
2006-02-23 02:27:19 +00:00
|
|
|
/* pa_log(__FILE__": push-packet %p", packet); */
|
2004-08-27 01:29:49 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
i = pa_xnew(struct item_info, 1);
|
2004-07-03 23:35:12 +00:00
|
|
|
i->type = PA_PSTREAM_ITEM_PACKET;
|
|
|
|
|
i->packet = pa_packet_ref(packet);
|
2006-07-19 21:48:35 +00:00
|
|
|
#ifdef HAVE_CREDS
|
2006-07-19 17:44:19 +00:00
|
|
|
if ((i->with_creds = !!creds))
|
|
|
|
|
i->creds = *creds;
|
2006-02-24 15:12:42 +00:00
|
|
|
#endif
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_queue_push(p->send_queue, i);
|
2004-08-05 19:53:57 +00:00
|
|
|
p->mainloop->defer_enable(p->defer_event, 1);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
void pa_pstream_send_memblock(pa_pstream*p, uint32_t channel, int64_t offset, pa_seek_mode_t seek_mode, const pa_memchunk *chunk) {
|
2004-06-08 23:54:24 +00:00
|
|
|
struct item_info *i;
|
2004-09-16 22:07:41 +00:00
|
|
|
assert(p && channel != (uint32_t) -1 && chunk && p->ref >= 1);
|
2004-09-10 22:35:12 +00:00
|
|
|
|
2004-09-16 22:07:41 +00:00
|
|
|
if (p->dead)
|
|
|
|
|
return;
|
|
|
|
|
|
2006-02-23 02:27:19 +00:00
|
|
|
/* pa_log(__FILE__": push-memblock %p", chunk); */
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
i = pa_xnew(struct item_info, 1);
|
2004-07-03 23:35:12 +00:00
|
|
|
i->type = PA_PSTREAM_ITEM_MEMBLOCK;
|
2004-06-08 23:54:24 +00:00
|
|
|
i->chunk = *chunk;
|
|
|
|
|
i->channel = channel;
|
2006-02-20 04:05:16 +00:00
|
|
|
i->offset = offset;
|
|
|
|
|
i->seek_mode = seek_mode;
|
2006-07-19 21:48:35 +00:00
|
|
|
#ifdef HAVE_CREDS
|
2006-07-14 23:00:57 +00:00
|
|
|
i->with_creds = 0;
|
2006-07-17 11:42:25 +00:00
|
|
|
#endif
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_memblock_ref(i->chunk.memblock);
|
2004-06-20 01:12:13 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_queue_push(p->send_queue, i);
|
2004-08-05 19:53:57 +00:00
|
|
|
p->mainloop->defer_enable(p->defer_event, 1);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void prepare_next_write_item(pa_pstream *p) {
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(p);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (!(p->write.current = pa_queue_pop(p->send_queue)))
|
2004-06-08 23:54:24 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
p->write.index = 0;
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (p->write.current->type == PA_PSTREAM_ITEM_PACKET) {
|
2006-02-23 02:27:19 +00:00
|
|
|
/*pa_log(__FILE__": pop-packet %p", p->write.current->packet);*/
|
2004-08-27 01:29:49 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(p->write.current->packet);
|
|
|
|
|
p->write.data = p->write.current->packet->data;
|
2004-07-03 23:35:12 +00:00
|
|
|
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH] = htonl(p->write.current->packet->length);
|
|
|
|
|
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL] = htonl((uint32_t) -1);
|
2006-02-20 04:05:16 +00:00
|
|
|
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_HI] = 0;
|
|
|
|
|
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_LO] = 0;
|
|
|
|
|
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_SEEK] = 0;
|
2006-02-24 15:12:42 +00:00
|
|
|
|
|
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
} else {
|
2004-07-03 23:35:12 +00:00
|
|
|
assert(p->write.current->type == PA_PSTREAM_ITEM_MEMBLOCK && p->write.current->chunk.memblock);
|
2004-09-01 00:46:56 +00:00
|
|
|
p->write.data = (uint8_t*) p->write.current->chunk.memblock->data + p->write.current->chunk.index;
|
2004-07-03 23:35:12 +00:00
|
|
|
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);
|
2006-02-20 04:05:16 +00:00
|
|
|
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_HI] = htonl((uint32_t) (((uint64_t) p->write.current->offset) >> 32));
|
|
|
|
|
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_LO] = htonl((uint32_t) ((uint64_t) p->write.current->offset));
|
|
|
|
|
p->write.descriptor[PA_PSTREAM_DESCRIPTOR_SEEK] = htonl(p->write.current->seek_mode);
|
2006-07-14 23:00:57 +00:00
|
|
|
}
|
2006-02-24 15:12:42 +00:00
|
|
|
|
2006-07-19 21:48:35 +00:00
|
|
|
#ifdef HAVE_CREDS
|
2006-07-19 17:44:19 +00:00
|
|
|
if ((p->send_creds_now = p->write.current->with_creds))
|
|
|
|
|
p->write_creds = p->write.current->creds;
|
|
|
|
|
|
2006-02-24 15:12:42 +00:00
|
|
|
#endif
|
2006-07-14 23:00:57 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
static int do_write(pa_pstream *p) {
|
2004-06-08 23:54:24 +00:00
|
|
|
void *d;
|
|
|
|
|
size_t l;
|
|
|
|
|
ssize_t r;
|
|
|
|
|
assert(p);
|
|
|
|
|
|
|
|
|
|
if (!p->write.current)
|
|
|
|
|
prepare_next_write_item(p);
|
|
|
|
|
|
|
|
|
|
if (!p->write.current)
|
2006-02-20 04:05:16 +00:00
|
|
|
return 0;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
assert(p->write.data);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (p->write.index < PA_PSTREAM_DESCRIPTOR_SIZE) {
|
2004-09-01 00:46:56 +00:00
|
|
|
d = (uint8_t*) p->write.descriptor + p->write.index;
|
2004-07-03 23:35:12 +00:00
|
|
|
l = PA_PSTREAM_DESCRIPTOR_SIZE - p->write.index;
|
2004-06-08 23:54:24 +00:00
|
|
|
} else {
|
2004-09-01 00:46:56 +00:00
|
|
|
d = (uint8_t*) p->write.data + p->write.index - PA_PSTREAM_DESCRIPTOR_SIZE;
|
2004-07-03 23:35:12 +00:00
|
|
|
l = ntohl(p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) - (p->write.index - PA_PSTREAM_DESCRIPTOR_SIZE);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-07-19 21:48:35 +00:00
|
|
|
#ifdef HAVE_CREDS
|
2006-02-24 15:12:42 +00:00
|
|
|
if (p->send_creds_now) {
|
|
|
|
|
|
2006-07-19 17:44:19 +00:00
|
|
|
if ((r = pa_iochannel_write_with_creds(p->io, d, l, &p->write_creds)) < 0)
|
2006-02-24 15:12:42 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
p->send_creds_now = 0;
|
|
|
|
|
} else
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
if ((r = pa_iochannel_write(p->io, d, l)) < 0)
|
2006-02-20 04:05:16 +00:00
|
|
|
return -1;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
p->write.index += r;
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (p->write.index >= PA_PSTREAM_DESCRIPTOR_SIZE+ntohl(p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH])) {
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(p->write.current);
|
|
|
|
|
item_free(p->write.current, (void *) 1);
|
|
|
|
|
p->write.current = NULL;
|
|
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
if (p->drain_callback && !pa_pstream_is_pending(p))
|
2006-02-24 15:12:42 +00:00
|
|
|
p->drain_callback(p, p->drain_callback_userdata);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
2004-06-20 01:12:13 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
return 0;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
static int do_read(pa_pstream *p) {
|
2004-06-08 23:54:24 +00:00
|
|
|
void *d;
|
2006-01-11 01:17:39 +00:00
|
|
|
size_t l;
|
2004-06-08 23:54:24 +00:00
|
|
|
ssize_t r;
|
|
|
|
|
assert(p);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (p->read.index < PA_PSTREAM_DESCRIPTOR_SIZE) {
|
2004-09-01 00:46:56 +00:00
|
|
|
d = (uint8_t*) p->read.descriptor + p->read.index;
|
2004-07-03 23:35:12 +00:00
|
|
|
l = PA_PSTREAM_DESCRIPTOR_SIZE - p->read.index;
|
2004-06-08 23:54:24 +00:00
|
|
|
} else {
|
|
|
|
|
assert(p->read.data);
|
2004-09-01 00:46:56 +00:00
|
|
|
d = (uint8_t*) p->read.data + p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE;
|
2004-07-03 23:35:12 +00:00
|
|
|
l = ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) - (p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-07-19 21:48:35 +00:00
|
|
|
#ifdef HAVE_CREDS
|
2006-02-24 15:12:42 +00:00
|
|
|
{
|
2006-07-19 17:44:19 +00:00
|
|
|
int b = 0;
|
2006-02-24 15:12:42 +00:00
|
|
|
|
2006-07-19 17:44:19 +00:00
|
|
|
if ((r = pa_iochannel_read_with_creds(p->io, d, l, &p->read_creds, &b)) <= 0)
|
2006-02-24 15:12:42 +00:00
|
|
|
return -1;
|
|
|
|
|
|
2006-07-19 17:44:19 +00:00
|
|
|
p->read_creds_valid = p->read_creds_valid || b;
|
2006-02-24 15:12:42 +00:00
|
|
|
}
|
|
|
|
|
#else
|
2004-07-03 23:35:12 +00:00
|
|
|
if ((r = pa_iochannel_read(p->io, d, l)) <= 0)
|
2006-02-20 04:05:16 +00:00
|
|
|
return -1;
|
2006-02-24 15:12:42 +00:00
|
|
|
#endif
|
2004-06-20 01:12:13 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
p->read.index += r;
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (p->read.index == PA_PSTREAM_DESCRIPTOR_SIZE) {
|
2004-06-08 23:54:24 +00:00
|
|
|
/* Reading of frame descriptor complete */
|
|
|
|
|
|
|
|
|
|
/* Frame size too large */
|
2004-08-03 19:26:56 +00:00
|
|
|
if (ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) > FRAME_SIZE_MAX) {
|
2006-04-23 20:59:09 +00:00
|
|
|
pa_log_warn(__FILE__": Frame size too large: %lu > %lu", (unsigned long) ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]), (unsigned long) FRAME_SIZE_MAX);
|
2006-02-20 04:05:16 +00:00
|
|
|
return -1;
|
2004-08-03 19:26:56 +00:00
|
|
|
}
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
assert(!p->read.packet && !p->read.memblock);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL]) == (uint32_t) -1) {
|
2004-06-08 23:54:24 +00:00
|
|
|
/* Frame is a packet frame */
|
2004-07-03 23:35:12 +00:00
|
|
|
p->read.packet = pa_packet_new(ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]));
|
2004-06-08 23:54:24 +00:00
|
|
|
p->read.data = p->read.packet->data;
|
|
|
|
|
} else {
|
|
|
|
|
/* Frame is a memblock frame */
|
2004-08-17 19:37:29 +00:00
|
|
|
p->read.memblock = pa_memblock_new(ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]), p->memblock_stat);
|
2004-06-08 23:54:24 +00:00
|
|
|
p->read.data = p->read.memblock->data;
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
if (ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_SEEK]) > PA_SEEK_RELATIVE_END) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log_warn(__FILE__": Invalid seek mode");
|
2006-02-20 04:05:16 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
} else if (p->read.index > PA_PSTREAM_DESCRIPTOR_SIZE) {
|
2004-06-08 23:54:24 +00:00
|
|
|
/* Frame payload available */
|
|
|
|
|
|
2004-06-20 01:12:13 +00:00
|
|
|
if (p->read.memblock && p->recieve_memblock_callback) { /* Is this memblock data? Than pass it to the user */
|
2004-07-03 23:35:12 +00:00
|
|
|
l = (p->read.index - r) < PA_PSTREAM_DESCRIPTOR_SIZE ? p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE : (size_t) r;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
if (l > 0) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memchunk chunk;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
chunk.memblock = p->read.memblock;
|
2004-07-03 23:35:12 +00:00
|
|
|
chunk.index = p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE - l;
|
2004-06-08 23:54:24 +00:00
|
|
|
chunk.length = l;
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if (p->recieve_memblock_callback) {
|
|
|
|
|
int64_t offset;
|
|
|
|
|
|
|
|
|
|
offset = (int64_t) (
|
|
|
|
|
(((uint64_t) ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_HI])) << 32) |
|
|
|
|
|
(((uint64_t) ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_LO]))));
|
|
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
p->recieve_memblock_callback(
|
|
|
|
|
p,
|
|
|
|
|
ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL]),
|
2006-02-20 04:05:16 +00:00
|
|
|
offset,
|
|
|
|
|
ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_SEEK]),
|
2004-07-07 00:22:46 +00:00
|
|
|
&chunk,
|
|
|
|
|
p->recieve_memblock_callback_userdata);
|
2006-02-20 04:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Drop seek info for following callbacks */
|
|
|
|
|
p->read.descriptor[PA_PSTREAM_DESCRIPTOR_SEEK] =
|
|
|
|
|
p->read.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_HI] =
|
|
|
|
|
p->read.descriptor[PA_PSTREAM_DESCRIPTOR_OFFSET_LO] = 0;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Frame complete */
|
2004-07-03 23:35:12 +00:00
|
|
|
if (p->read.index >= ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) + PA_PSTREAM_DESCRIPTOR_SIZE) {
|
2004-06-08 23:54:24 +00:00
|
|
|
if (p->read.memblock) {
|
|
|
|
|
assert(!p->read.packet);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_memblock_unref(p->read.memblock);
|
2004-06-08 23:54:24 +00:00
|
|
|
p->read.memblock = NULL;
|
|
|
|
|
} else {
|
|
|
|
|
assert(p->read.packet);
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
if (p->recieve_packet_callback)
|
2006-07-19 21:48:35 +00:00
|
|
|
#ifdef HAVE_CREDS
|
2006-07-19 17:44:19 +00:00
|
|
|
p->recieve_packet_callback(p, p->read.packet, p->read_creds_valid ? &p->read_creds : NULL, p->recieve_packet_callback_userdata);
|
2006-02-24 15:12:42 +00:00
|
|
|
#else
|
|
|
|
|
p->recieve_packet_callback(p, p->read.packet, NULL, p->recieve_packet_callback_userdata);
|
|
|
|
|
#endif
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_packet_unref(p->read.packet);
|
2004-06-08 23:54:24 +00:00
|
|
|
p->read.packet = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p->read.index = 0;
|
2006-07-19 21:48:35 +00:00
|
|
|
#ifdef HAVE_CREDS
|
2006-07-19 17:44:19 +00:00
|
|
|
p->read_creds_valid = 0;
|
2006-02-24 15:12:42 +00:00
|
|
|
#endif
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-06-20 01:12:13 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
return 0;
|
2004-06-20 01:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
2006-02-24 15:12:42 +00:00
|
|
|
void pa_pstream_set_die_callback(pa_pstream *p, pa_pstream_notify_cb_t cb, void *userdata) {
|
|
|
|
|
assert(p);
|
|
|
|
|
assert(p->ref >= 1);
|
|
|
|
|
|
|
|
|
|
p->die_callback = cb;
|
2004-06-20 01:12:13 +00:00
|
|
|
p->die_callback_userdata = userdata;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2006-02-24 15:12:42 +00:00
|
|
|
|
|
|
|
|
void pa_pstream_set_drain_callback(pa_pstream *p, pa_pstream_notify_cb_t cb, void *userdata) {
|
2004-07-07 00:22:46 +00:00
|
|
|
assert(p);
|
2006-02-24 15:12:42 +00:00
|
|
|
assert(p->ref >= 1);
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2006-02-24 15:12:42 +00:00
|
|
|
p->drain_callback = cb;
|
|
|
|
|
p->drain_callback_userdata = userdata;
|
|
|
|
|
}
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2006-02-24 15:12:42 +00:00
|
|
|
void pa_pstream_set_recieve_packet_callback(pa_pstream *p, pa_pstream_packet_cb_t cb, void *userdata) {
|
|
|
|
|
assert(p);
|
|
|
|
|
assert(p->ref >= 1);
|
|
|
|
|
|
|
|
|
|
p->recieve_packet_callback = cb;
|
|
|
|
|
p->recieve_packet_callback_userdata = userdata;
|
2004-07-07 00:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
2006-02-24 15:12:42 +00:00
|
|
|
void pa_pstream_set_recieve_memblock_callback(pa_pstream *p, pa_pstream_memblock_cb_t cb, void *userdata) {
|
2004-07-07 00:22:46 +00:00
|
|
|
assert(p);
|
2006-02-20 04:05:16 +00:00
|
|
|
assert(p->ref >= 1);
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2006-02-24 15:12:42 +00:00
|
|
|
p->recieve_memblock_callback = cb;
|
|
|
|
|
p->recieve_memblock_callback_userdata = userdata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa_pstream_is_pending(pa_pstream *p) {
|
|
|
|
|
assert(p);
|
|
|
|
|
|
|
|
|
|
if (p->dead)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return p->write.current || !pa_queue_is_empty(p->send_queue);
|
2004-07-07 00:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_pstream_unref(pa_pstream*p) {
|
2006-02-20 04:05:16 +00:00
|
|
|
assert(p);
|
|
|
|
|
assert(p->ref >= 1);
|
2004-08-15 00:02:26 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if (--p->ref == 0)
|
2004-08-15 00:02:26 +00:00
|
|
|
pstream_free(p);
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_pstream* pa_pstream_ref(pa_pstream*p) {
|
2006-02-20 04:05:16 +00:00
|
|
|
assert(p);
|
|
|
|
|
assert(p->ref >= 1);
|
|
|
|
|
|
2004-08-15 00:02:26 +00:00
|
|
|
p->ref++;
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_pstream_close(pa_pstream *p) {
|
2004-08-15 00:02:26 +00:00
|
|
|
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;
|
|
|
|
|
}
|