From 2a6ee7731f6654b2efbc5dbf861f82c2823d1a54 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 16 Jul 2004 18:40:20 +0000 Subject: rename a bunch of files git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@79 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/esound-spec.h | 192 --------- src/esound.h | 192 +++++++++ src/native-common.h | 47 +++ src/polyp-error.c | 28 -- src/polyp-error.h | 10 - src/polyp.c | 948 --------------------------------------------- src/polyp.h | 73 ---- src/polypdef.h | 20 - src/polyplib-def.h | 20 + src/polyplib-error.c | 28 ++ src/polyplib-error.h | 10 + src/polyplib-simple.c | 233 +++++++++++ src/polyplib-simple.h | 28 ++ src/polyplib.c | 948 +++++++++++++++++++++++++++++++++++++++++++++ src/polyplib.h | 73 ++++ src/protocol-native-spec.h | 47 --- src/simple.c | 233 ----------- src/simple.h | 28 -- src/sink-input.c | 138 +++++++ src/sink-input.h | 46 +++ src/sinkinput.c | 138 ------- src/sinkinput.h | 46 --- src/source-output.c | 76 ++++ src/source-output.h | 37 ++ src/sourceoutput.c | 76 ---- src/sourceoutput.h | 37 -- 26 files changed, 1876 insertions(+), 1876 deletions(-) delete mode 100644 src/esound-spec.h create mode 100644 src/esound.h create mode 100644 src/native-common.h delete mode 100644 src/polyp-error.c delete mode 100644 src/polyp-error.h delete mode 100644 src/polyp.c delete mode 100644 src/polyp.h delete mode 100644 src/polypdef.h create mode 100644 src/polyplib-def.h create mode 100644 src/polyplib-error.c create mode 100644 src/polyplib-error.h create mode 100644 src/polyplib-simple.c create mode 100644 src/polyplib-simple.h create mode 100644 src/polyplib.c create mode 100644 src/polyplib.h delete mode 100644 src/protocol-native-spec.h delete mode 100644 src/simple.c delete mode 100644 src/simple.h create mode 100644 src/sink-input.c create mode 100644 src/sink-input.h delete mode 100644 src/sinkinput.c delete mode 100644 src/sinkinput.h create mode 100644 src/source-output.c create mode 100644 src/source-output.h delete mode 100644 src/sourceoutput.c delete mode 100644 src/sourceoutput.h diff --git a/src/esound-spec.h b/src/esound-spec.h deleted file mode 100644 index f7a4d3db..00000000 --- a/src/esound-spec.h +++ /dev/null @@ -1,192 +0,0 @@ -#ifndef fooesoundhfoo -#define fooesoundhfoo - -/* Most of the following is blatantly stolen from esound. */ - - -/* path and name of the default EsounD domain socket */ -#define ESD_UNIX_SOCKET_DIR "/tmp/.esd" -#define ESD_UNIX_SOCKET_NAME "/tmp/.esd/socket" - -/* length of the audio buffer size */ -#define ESD_BUF_SIZE (4 * 1024) -/* maximum size we can write(). Otherwise we might overflow */ -#define ESD_MAX_WRITE_SIZE (21 * 4096) - -/* length of the authorization key, octets */ -#define ESD_KEY_LEN (16) - -/* default port for the EsounD server */ -#define ESD_DEFAULT_PORT (16001) - -/* default sample rate for the EsounD server */ -#define ESD_DEFAULT_RATE (44100) - -/* maximum length of a stream/sample name */ -#define ESD_NAME_MAX (128) - -/* a magic number to identify the relative endianness of a client */ -#define ESD_ENDIAN_KEY ((uint32_t) (('E' << 24) + ('N' << 16) + ('D' << 8) + ('N'))) - -#define ESD_VOLUME_BASE (256) - - -/*************************************/ -/* what can we do to/with the EsounD */ -enum esd_proto { - ESD_PROTO_CONNECT, /* implied on inital client connection */ - - /* pseudo "security" functionality */ - ESD_PROTO_LOCK, /* disable "foreign" client connections */ - ESD_PROTO_UNLOCK, /* enable "foreign" client connections */ - - /* stream functionality: play, record, monitor */ - ESD_PROTO_STREAM_PLAY, /* play all following data as a stream */ - ESD_PROTO_STREAM_REC, /* record data from card as a stream */ - ESD_PROTO_STREAM_MON, /* send mixed buffer output as a stream */ - - /* sample functionality: cache, free, play, loop, EOL, kill */ - ESD_PROTO_SAMPLE_CACHE, /* cache a sample in the server */ - ESD_PROTO_SAMPLE_FREE, /* release a sample in the server */ - ESD_PROTO_SAMPLE_PLAY, /* play a cached sample */ - ESD_PROTO_SAMPLE_LOOP, /* loop a cached sample, til eoloop */ - ESD_PROTO_SAMPLE_STOP, /* stop a looping sample when done */ - ESD_PROTO_SAMPLE_KILL, /* stop the looping sample immed. */ - - /* free and reclaim /dev/dsp functionality */ - ESD_PROTO_STANDBY, /* release /dev/dsp and ignore all data */ - ESD_PROTO_RESUME, /* reclaim /dev/dsp and play sounds again */ - - /* TODO: move these to a more logical place. NOTE: will break the protocol */ - ESD_PROTO_SAMPLE_GETID, /* get the ID for an already-cached sample */ - ESD_PROTO_STREAM_FILT, /* filter mixed buffer output as a stream */ - - /* esd remote management */ - ESD_PROTO_SERVER_INFO, /* get server info (ver, sample rate, format) */ - ESD_PROTO_ALL_INFO, /* get all info (server info, players, samples) */ - ESD_PROTO_SUBSCRIBE, /* track new and removed players and samples */ - ESD_PROTO_UNSUBSCRIBE, /* stop tracking updates */ - - /* esd remote control */ - ESD_PROTO_STREAM_PAN, /* set stream panning */ - ESD_PROTO_SAMPLE_PAN, /* set default sample panning */ - - /* esd status */ - ESD_PROTO_STANDBY_MODE, /* see if server is in standby, autostandby, etc */ - - /* esd latency */ - ESD_PROTO_LATENCY, /* retrieve latency between write()'s and output */ - - ESD_PROTO_MAX /* for bounds checking */ -}; - -/******************/ -/* The EsounD api */ - -/* the properties of a sound buffer are logically or'd */ - -/* bits of stream/sample data */ -#define ESD_MASK_BITS ( 0x000F ) -#define ESD_BITS8 ( 0x0000 ) -#define ESD_BITS16 ( 0x0001 ) - -/* how many interleaved channels of data */ -#define ESD_MASK_CHAN ( 0x00F0 ) -#define ESD_MONO ( 0x0010 ) -#define ESD_STEREO ( 0x0020 ) - -/* whether it's a stream or a sample */ -#define ESD_MASK_MODE ( 0x0F00 ) -#define ESD_STREAM ( 0x0000 ) -#define ESD_SAMPLE ( 0x0100 ) -#define ESD_ADPCM ( 0x0200 ) /* TODO: anyone up for this? =P */ - -/* the function of the stream/sample, and common functions */ -#define ESD_MASK_FUNC ( 0xF000 ) -#define ESD_PLAY ( 0x1000 ) -/* functions for streams only */ -#define ESD_MONITOR ( 0x0000 ) -#define ESD_RECORD ( 0x2000 ) -/* functions for samples only */ -#define ESD_STOP ( 0x0000 ) -#define ESD_LOOP ( 0x2000 ) - -typedef int esd_format_t; -typedef int esd_proto_t; - -/*******************************************************************/ -/* esdmgr.c - functions to implement a "sound manager" for esd */ - -/* structures to retrieve information about streams/samples from the server */ -typedef struct esd_server_info { - - int version; /* server version encoded as an int */ - esd_format_t format; /* magic int with the format info */ - int rate; /* sample rate */ - -} esd_server_info_t; - -typedef struct esd_player_info { - - struct esd_player_info *next; /* point to next entry in list */ - esd_server_info_t *server; /* the server that contains this stream */ - - int source_id; /* either a stream fd or sample id */ - char name[ ESD_NAME_MAX ]; /* name of stream for remote control */ - int rate; /* sample rate */ - int left_vol_scale; /* volume scaling */ - int right_vol_scale; - - esd_format_t format; /* magic int with the format info */ - -} esd_player_info_t; - -typedef struct esd_sample_info { - - struct esd_sample_info *next; /* point to next entry in list */ - esd_server_info_t *server; /* the server that contains this sample */ - - int sample_id; /* either a stream fd or sample id */ - char name[ ESD_NAME_MAX ]; /* name of stream for remote control */ - int rate; /* sample rate */ - int left_vol_scale; /* volume scaling */ - int right_vol_scale; - - esd_format_t format; /* magic int with the format info */ - int length; /* total buffer length */ - -} esd_sample_info_t; - -typedef struct esd_info { - - esd_server_info_t *server; - esd_player_info_t *player_list; - esd_sample_info_t *sample_list; - -} esd_info_t; - -enum esd_standby_mode { - ESM_ERROR, ESM_ON_STANDBY, ESM_ON_AUTOSTANDBY, ESM_RUNNING -}; -typedef int esd_standby_mode_t; - -enum esd_client_state { - ESD_STREAMING_DATA, /* data from here on is streamed data */ - ESD_CACHING_SAMPLE, /* midway through caching a sample */ - ESD_NEEDS_REQDATA, /* more data needed to complere request */ - ESD_NEXT_REQUEST, /* proceed to next request */ - ESD_CLIENT_STATE_MAX /* place holder */ -}; -typedef int esd_client_state_t; - -/* switch endian order for cross platform playing */ -#define swap_endian_32(x) ((x >> 24) | ((x >> 8) & 0xFF00) | (((x & 0xFF00) << 8)) | (x << 24)) -#define maybe_swap_endian_32(c,x) ((c) ? swap_endian_32(x) : x) - -/* the endian key is transferred in binary, if it's read into int, */ -/* and matches ESD_ENDIAN_KEY (ENDN), then the endianness of the */ -/* server and the client match; if it's SWAP_ENDIAN_KEY, swap data */ -#define ESD_SWAP_ENDIAN_KEY ((uint32_t) swap_endian_32(ESD_ENDIAN_KEY)) - - -#endif diff --git a/src/esound.h b/src/esound.h new file mode 100644 index 00000000..f7a4d3db --- /dev/null +++ b/src/esound.h @@ -0,0 +1,192 @@ +#ifndef fooesoundhfoo +#define fooesoundhfoo + +/* Most of the following is blatantly stolen from esound. */ + + +/* path and name of the default EsounD domain socket */ +#define ESD_UNIX_SOCKET_DIR "/tmp/.esd" +#define ESD_UNIX_SOCKET_NAME "/tmp/.esd/socket" + +/* length of the audio buffer size */ +#define ESD_BUF_SIZE (4 * 1024) +/* maximum size we can write(). Otherwise we might overflow */ +#define ESD_MAX_WRITE_SIZE (21 * 4096) + +/* length of the authorization key, octets */ +#define ESD_KEY_LEN (16) + +/* default port for the EsounD server */ +#define ESD_DEFAULT_PORT (16001) + +/* default sample rate for the EsounD server */ +#define ESD_DEFAULT_RATE (44100) + +/* maximum length of a stream/sample name */ +#define ESD_NAME_MAX (128) + +/* a magic number to identify the relative endianness of a client */ +#define ESD_ENDIAN_KEY ((uint32_t) (('E' << 24) + ('N' << 16) + ('D' << 8) + ('N'))) + +#define ESD_VOLUME_BASE (256) + + +/*************************************/ +/* what can we do to/with the EsounD */ +enum esd_proto { + ESD_PROTO_CONNECT, /* implied on inital client connection */ + + /* pseudo "security" functionality */ + ESD_PROTO_LOCK, /* disable "foreign" client connections */ + ESD_PROTO_UNLOCK, /* enable "foreign" client connections */ + + /* stream functionality: play, record, monitor */ + ESD_PROTO_STREAM_PLAY, /* play all following data as a stream */ + ESD_PROTO_STREAM_REC, /* record data from card as a stream */ + ESD_PROTO_STREAM_MON, /* send mixed buffer output as a stream */ + + /* sample functionality: cache, free, play, loop, EOL, kill */ + ESD_PROTO_SAMPLE_CACHE, /* cache a sample in the server */ + ESD_PROTO_SAMPLE_FREE, /* release a sample in the server */ + ESD_PROTO_SAMPLE_PLAY, /* play a cached sample */ + ESD_PROTO_SAMPLE_LOOP, /* loop a cached sample, til eoloop */ + ESD_PROTO_SAMPLE_STOP, /* stop a looping sample when done */ + ESD_PROTO_SAMPLE_KILL, /* stop the looping sample immed. */ + + /* free and reclaim /dev/dsp functionality */ + ESD_PROTO_STANDBY, /* release /dev/dsp and ignore all data */ + ESD_PROTO_RESUME, /* reclaim /dev/dsp and play sounds again */ + + /* TODO: move these to a more logical place. NOTE: will break the protocol */ + ESD_PROTO_SAMPLE_GETID, /* get the ID for an already-cached sample */ + ESD_PROTO_STREAM_FILT, /* filter mixed buffer output as a stream */ + + /* esd remote management */ + ESD_PROTO_SERVER_INFO, /* get server info (ver, sample rate, format) */ + ESD_PROTO_ALL_INFO, /* get all info (server info, players, samples) */ + ESD_PROTO_SUBSCRIBE, /* track new and removed players and samples */ + ESD_PROTO_UNSUBSCRIBE, /* stop tracking updates */ + + /* esd remote control */ + ESD_PROTO_STREAM_PAN, /* set stream panning */ + ESD_PROTO_SAMPLE_PAN, /* set default sample panning */ + + /* esd status */ + ESD_PROTO_STANDBY_MODE, /* see if server is in standby, autostandby, etc */ + + /* esd latency */ + ESD_PROTO_LATENCY, /* retrieve latency between write()'s and output */ + + ESD_PROTO_MAX /* for bounds checking */ +}; + +/******************/ +/* The EsounD api */ + +/* the properties of a sound buffer are logically or'd */ + +/* bits of stream/sample data */ +#define ESD_MASK_BITS ( 0x000F ) +#define ESD_BITS8 ( 0x0000 ) +#define ESD_BITS16 ( 0x0001 ) + +/* how many interleaved channels of data */ +#define ESD_MASK_CHAN ( 0x00F0 ) +#define ESD_MONO ( 0x0010 ) +#define ESD_STEREO ( 0x0020 ) + +/* whether it's a stream or a sample */ +#define ESD_MASK_MODE ( 0x0F00 ) +#define ESD_STREAM ( 0x0000 ) +#define ESD_SAMPLE ( 0x0100 ) +#define ESD_ADPCM ( 0x0200 ) /* TODO: anyone up for this? =P */ + +/* the function of the stream/sample, and common functions */ +#define ESD_MASK_FUNC ( 0xF000 ) +#define ESD_PLAY ( 0x1000 ) +/* functions for streams only */ +#define ESD_MONITOR ( 0x0000 ) +#define ESD_RECORD ( 0x2000 ) +/* functions for samples only */ +#define ESD_STOP ( 0x0000 ) +#define ESD_LOOP ( 0x2000 ) + +typedef int esd_format_t; +typedef int esd_proto_t; + +/*******************************************************************/ +/* esdmgr.c - functions to implement a "sound manager" for esd */ + +/* structures to retrieve information about streams/samples from the server */ +typedef struct esd_server_info { + + int version; /* server version encoded as an int */ + esd_format_t format; /* magic int with the format info */ + int rate; /* sample rate */ + +} esd_server_info_t; + +typedef struct esd_player_info { + + struct esd_player_info *next; /* point to next entry in list */ + esd_server_info_t *server; /* the server that contains this stream */ + + int source_id; /* either a stream fd or sample id */ + char name[ ESD_NAME_MAX ]; /* name of stream for remote control */ + int rate; /* sample rate */ + int left_vol_scale; /* volume scaling */ + int right_vol_scale; + + esd_format_t format; /* magic int with the format info */ + +} esd_player_info_t; + +typedef struct esd_sample_info { + + struct esd_sample_info *next; /* point to next entry in list */ + esd_server_info_t *server; /* the server that contains this sample */ + + int sample_id; /* either a stream fd or sample id */ + char name[ ESD_NAME_MAX ]; /* name of stream for remote control */ + int rate; /* sample rate */ + int left_vol_scale; /* volume scaling */ + int right_vol_scale; + + esd_format_t format; /* magic int with the format info */ + int length; /* total buffer length */ + +} esd_sample_info_t; + +typedef struct esd_info { + + esd_server_info_t *server; + esd_player_info_t *player_list; + esd_sample_info_t *sample_list; + +} esd_info_t; + +enum esd_standby_mode { + ESM_ERROR, ESM_ON_STANDBY, ESM_ON_AUTOSTANDBY, ESM_RUNNING +}; +typedef int esd_standby_mode_t; + +enum esd_client_state { + ESD_STREAMING_DATA, /* data from here on is streamed data */ + ESD_CACHING_SAMPLE, /* midway through caching a sample */ + ESD_NEEDS_REQDATA, /* more data needed to complere request */ + ESD_NEXT_REQUEST, /* proceed to next request */ + ESD_CLIENT_STATE_MAX /* place holder */ +}; +typedef int esd_client_state_t; + +/* switch endian order for cross platform playing */ +#define swap_endian_32(x) ((x >> 24) | ((x >> 8) & 0xFF00) | (((x & 0xFF00) << 8)) | (x << 24)) +#define maybe_swap_endian_32(c,x) ((c) ? swap_endian_32(x) : x) + +/* the endian key is transferred in binary, if it's read into int, */ +/* and matches ESD_ENDIAN_KEY (ENDN), then the endianness of the */ +/* server and the client match; if it's SWAP_ENDIAN_KEY, swap data */ +#define ESD_SWAP_ENDIAN_KEY ((uint32_t) swap_endian_32(ESD_ENDIAN_KEY)) + + +#endif diff --git a/src/native-common.h b/src/native-common.h new file mode 100644 index 00000000..e1db6f64 --- /dev/null +++ b/src/native-common.h @@ -0,0 +1,47 @@ +#ifndef fooprotocolnativespech +#define fooprotocolnativespech + +enum { + PA_COMMAND_ERROR, + PA_COMMAND_TIMEOUT, /* pseudo command */ + PA_COMMAND_REPLY, + PA_COMMAND_CREATE_PLAYBACK_STREAM, + PA_COMMAND_DELETE_PLAYBACK_STREAM, + PA_COMMAND_CREATE_RECORD_STREAM, + PA_COMMAND_DELETE_RECORD_STREAM, + PA_COMMAND_EXIT, + PA_COMMAND_REQUEST, + PA_COMMAND_AUTH, + PA_COMMAND_SET_NAME, + PA_COMMAND_LOOKUP_SINK, + PA_COMMAND_LOOKUP_SOURCE, + PA_COMMAND_DRAIN_PLAYBACK_STREAM, + PA_COMMAND_PLAYBACK_STREAM_KILLED, + PA_COMMAND_RECORD_STREAM_KILLED, + PA_COMMAND_STAT, + PA_COMMAND_GET_PLAYBACK_LATENCY, + PA_COMMAND_MAX +}; + +enum { + PA_ERROR_OK, + PA_ERROR_ACCESS, + PA_ERROR_COMMAND, + PA_ERROR_INVALID, + PA_ERROR_EXIST, + PA_ERROR_NOENTITY, + PA_ERROR_CONNECTIONREFUSED, + PA_ERROR_PROTOCOL, + PA_ERROR_TIMEOUT, + PA_ERROR_AUTHKEY, + PA_ERROR_INTERNAL, + PA_ERROR_CONNECTIONTERMINATED, + PA_ERROR_KILLED, + PA_ERROR_INVALIDSERVER, + PA_ERROR_MAX +}; + +#define PA_NATIVE_COOKIE_LENGTH 256 +#define PA_NATIVE_COOKIE_FILE ".polypaudio-cookie" + +#endif diff --git a/src/polyp-error.c b/src/polyp-error.c deleted file mode 100644 index 86166b0c..00000000 --- a/src/polyp-error.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include - -#include "polyp-error.h" -#include "protocol-native-spec.h" - -static const char* const errortab[PA_ERROR_MAX] = { - [PA_ERROR_OK] = "OK", - [PA_ERROR_ACCESS] = "Access denied", - [PA_ERROR_COMMAND] = "Unknown command", - [PA_ERROR_INVALID] = "Invalid argument", - [PA_ERROR_EXIST] = "Entity exists", - [PA_ERROR_NOENTITY] = "No such entity", - [PA_ERROR_CONNECTIONREFUSED] = "Connection refused", - [PA_ERROR_PROTOCOL] = "Protocol corrupt", - [PA_ERROR_TIMEOUT] = "Timeout", - [PA_ERROR_AUTHKEY] = "Not authorization key", - [PA_ERROR_INTERNAL] = "Internal error", - [PA_ERROR_CONNECTIONTERMINATED] = "Connection terminated", - [PA_ERROR_KILLED] = "Entity killed", -}; - -const char*pa_strerror(uint32_t error) { - if (error >= PA_ERROR_MAX) - return NULL; - - return errortab[error]; -} diff --git a/src/polyp-error.h b/src/polyp-error.h deleted file mode 100644 index 7407f34c..00000000 --- a/src/polyp-error.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef foopolyperrhfoo -#define foopolyperrhfoo - -#include - -#include "protocol-native-spec.h" - -const char* pa_strerror(uint32_t error); - -#endif diff --git a/src/polyp.c b/src/polyp.c deleted file mode 100644 index d6d0f908..00000000 --- a/src/polyp.c +++ /dev/null @@ -1,948 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "polyp.h" -#include "protocol-native-spec.h" -#include "pdispatch.h" -#include "pstream.h" -#include "dynarray.h" -#include "socket-client.h" -#include "pstream-util.h" -#include "authkey.h" -#include "util.h" - -#define DEFAULT_MAXLENGTH 204800 -#define DEFAULT_TLENGTH 10240 -#define DEFAULT_PREBUF 4096 -#define DEFAULT_MINREQ 1024 -#define DEFAULT_FRAGSIZE 1024 - -#define DEFAULT_TIMEOUT (5*60) -#define DEFAULT_SERVER "/tmp/polypaudio/native" -#define DEFAULT_PORT "4713" - -struct pa_context { - char *name; - struct pa_mainloop_api* mainloop; - struct pa_socket_client *client; - struct pa_pstream *pstream; - struct pa_pdispatch *pdispatch; - struct pa_dynarray *record_streams, *playback_streams; - struct pa_stream *first_stream; - uint32_t ctag; - uint32_t error; - enum { - CONTEXT_UNCONNECTED, - CONTEXT_CONNECTING, - CONTEXT_AUTHORIZING, - CONTEXT_SETTING_NAME, - CONTEXT_READY, - CONTEXT_DEAD - } state; - - void (*connect_complete_callback)(struct pa_context*c, int success, void *userdata); - void *connect_complete_userdata; - - void (*drain_complete_callback)(struct pa_context*c, void *userdata); - void *drain_complete_userdata; - - void (*die_callback)(struct pa_context*c, void *userdata); - void *die_userdata; - - void (*stat_callback)(struct pa_context*c, uint32_t count, uint32_t total, void *userdata); - void *stat_userdata; - - uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH]; -}; - -struct pa_stream { - struct pa_context *context; - struct pa_stream *next, *previous; - - char *name; - struct pa_buffer_attr buffer_attr; - struct pa_sample_spec sample_spec; - uint32_t device_index; - uint32_t channel; - int channel_valid; - enum pa_stream_direction direction; - - enum { STREAM_LOOKING_UP, STREAM_CREATING, STREAM_READY, STREAM_DEAD} state; - uint32_t requested_bytes; - - void (*read_callback)(struct pa_stream *p, const void*data, size_t length, void *userdata); - void *read_userdata; - - void (*write_callback)(struct pa_stream *p, size_t length, void *userdata); - void *write_userdata; - - void (*create_complete_callback)(struct pa_stream *s, int success, void *userdata); - void *create_complete_userdata; - - void (*drain_complete_callback)(struct pa_stream *s, void *userdata); - void *drain_complete_userdata; - - void (*die_callback)(struct pa_stream*c, void *userdata); - void *die_userdata; - - void (*get_latency_callback)(struct pa_stream*c, uint32_t latency, void *userdata); - void *get_latency_userdata; -}; - -static void command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata); -static void command_playback_stream_killed(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata); - -static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = { - [PA_COMMAND_ERROR] = { NULL }, - [PA_COMMAND_REPLY] = { NULL }, - [PA_COMMAND_CREATE_PLAYBACK_STREAM] = { NULL }, - [PA_COMMAND_DELETE_PLAYBACK_STREAM] = { NULL }, - [PA_COMMAND_CREATE_RECORD_STREAM] = { NULL }, - [PA_COMMAND_DELETE_RECORD_STREAM] = { NULL }, - [PA_COMMAND_EXIT] = { NULL }, - [PA_COMMAND_REQUEST] = { command_request }, - [PA_COMMAND_PLAYBACK_STREAM_KILLED] = { command_playback_stream_killed }, - [PA_COMMAND_RECORD_STREAM_KILLED] = { command_playback_stream_killed }, -}; - -struct pa_context *pa_context_new(struct pa_mainloop_api *mainloop, const char *name) { - struct pa_context *c; - assert(mainloop && name); - - c = malloc(sizeof(struct pa_context)); - assert(c); - c->name = strdup(name); - c->mainloop = mainloop; - c->client = NULL; - c->pstream = NULL; - c->pdispatch = NULL; - c->playback_streams = pa_dynarray_new(); - assert(c->playback_streams); - c->record_streams = pa_dynarray_new(); - assert(c->record_streams); - c->first_stream = NULL; - c->error = PA_ERROR_OK; - c->state = CONTEXT_UNCONNECTED; - c->ctag = 0; - - c->connect_complete_callback = NULL; - c->connect_complete_userdata = NULL; - - c->drain_complete_callback = NULL; - c->drain_complete_userdata = NULL; - - c->die_callback = NULL; - c->die_userdata = NULL; - - c->stat_callback = NULL; - c->stat_userdata = NULL; - - pa_check_for_sigpipe(); - return c; -} - -void pa_context_free(struct pa_context *c) { - assert(c); - - while (c->first_stream) - pa_stream_free(c->first_stream); - - if (c->client) - pa_socket_client_free(c->client); - if (c->pdispatch) - pa_pdispatch_free(c->pdispatch); - if (c->pstream) - pa_pstream_free(c->pstream); - if (c->record_streams) - pa_dynarray_free(c->record_streams, NULL, NULL); - if (c->playback_streams) - pa_dynarray_free(c->playback_streams, NULL, NULL); - - free(c->name); - free(c); -} - -static void stream_dead(struct pa_stream *s) { - assert(s); - - if (s->state == STREAM_DEAD) - return; - - if (s->state == STREAM_READY) { - s->state = STREAM_DEAD; - if (s->die_callback) - s->die_callback(s, s->die_userdata); - } else - s->state = STREAM_DEAD; -} - -static void context_dead(struct pa_context *c) { - struct pa_stream *s; - assert(c); - - if (c->state == CONTEXT_DEAD) - return; - - if (c->pdispatch) - pa_pdispatch_free(c->pdispatch); - c->pdispatch = NULL; - - if (c->pstream) - pa_pstream_free(c->pstream); - c->pstream = NULL; - - if (c->client) - pa_socket_client_free(c->client); - c->client = NULL; - - for (s = c->first_stream; s; s = s->next) - stream_dead(s); - - if (c->state == CONTEXT_READY) { - c->state = CONTEXT_DEAD; - if (c->die_callback) - c->die_callback(c, c->die_userdata); - } else - c->state = CONTEXT_DEAD; -} - -static void pstream_die_callback(struct pa_pstream *p, void *userdata) { - struct pa_context *c = userdata; - assert(p && c); - c->error = PA_ERROR_CONNECTIONTERMINATED; - context_dead(c); -} - -static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) { - struct pa_context *c = userdata; - assert(p && packet && c); - - if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) { - fprintf(stderr, "polyp.c: invalid packet.\n"); - c->error = PA_ERROR_PROTOCOL; - context_dead(c); - } -} - -static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata) { - struct pa_context *c = userdata; - struct pa_stream *s; - assert(p && chunk && c && chunk->memblock && chunk->memblock->data); - - if (!(s = pa_dynarray_get(c->record_streams, channel))) - return; - - if (s->read_callback) - s->read_callback(s, chunk->memblock->data + chunk->index, chunk->length, s->read_userdata); -} - -static int handle_error(struct pa_context *c, uint32_t command, struct pa_tagstruct *t) { - assert(c && t); - - if (command == PA_COMMAND_ERROR) { - if (pa_tagstruct_getu32(t, &c->error) < 0) { - c->error = PA_ERROR_PROTOCOL; - return -1; - } - - return 0; - } - - c->error = (command == PA_COMMAND_TIMEOUT) ? PA_ERROR_TIMEOUT : PA_ERROR_INTERNAL; - return -1; -} - -static void setup_complete_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { - struct pa_context *c = userdata; - assert(pd && c && (c->state == CONTEXT_AUTHORIZING || c->state == CONTEXT_SETTING_NAME)); - - if (command != PA_COMMAND_REPLY) { - handle_error(c, command, t); - context_dead(c); - - if (c->connect_complete_callback) - c->connect_complete_callback(c, 0, c->connect_complete_userdata); - - return; - } - - if (c->state == CONTEXT_AUTHORIZING) { - struct pa_tagstruct *t; - c->state = CONTEXT_SETTING_NAME; - t = pa_tagstruct_new(NULL, 0); - assert(t); - pa_tagstruct_putu32(t, PA_COMMAND_SET_NAME); - pa_tagstruct_putu32(t, tag = c->ctag++); - pa_tagstruct_puts(t, c->name); - pa_pstream_send_tagstruct(c->pstream, t); - pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c); - } else { - assert(c->state == CONTEXT_SETTING_NAME); - - c->state = CONTEXT_READY; - - if (c->connect_complete_callback) - c->connect_complete_callback(c, 1, c->connect_complete_userdata); - } - - return; -} - -static void on_connection(struct pa_socket_client *client, struct pa_iochannel*io, void *userdata) { - struct pa_context *c = userdata; - struct pa_tagstruct *t; - uint32_t tag; - assert(client && c && c->state == CONTEXT_CONNECTING); - - pa_socket_client_free(client); - c->client = NULL; - - if (!io) { - c->error = PA_ERROR_CONNECTIONREFUSED; - context_dead(c); - - if (c->connect_complete_callback) - c->connect_complete_callback(c, 0, c->connect_complete_userdata); - - return; - } - - c->pstream = pa_pstream_new(c->mainloop, io); - assert(c->pstream); - pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c); - pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c); - pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c); - - c->pdispatch = pa_pdispatch_new(c->mainloop, command_table, PA_COMMAND_MAX); - assert(c->pdispatch); - - t = pa_tagstruct_new(NULL, 0); - assert(t); - pa_tagstruct_putu32(t, PA_COMMAND_AUTH); - pa_tagstruct_putu32(t, tag = c->ctag++); - pa_tagstruct_put_arbitrary(t, c->auth_cookie, sizeof(c->auth_cookie)); - pa_pstream_send_tagstruct(c->pstream, t); - pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c); - c->state = CONTEXT_AUTHORIZING; -} - -static struct sockaddr *resolve_server(const char *server, size_t *len) { - struct sockaddr *sa; - struct addrinfo hints, *result = NULL; - char *port; - assert(server && len); - - if ((port = strrchr(server, ':'))) - port++; - if (!port) - port = DEFAULT_PORT; - - memset(&hints, 0, sizeof(hints)); - hints.ai_family = PF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = 0; - - if (getaddrinfo(server, port, &hints, &result) != 0) - return NULL; - assert(result); - - sa = malloc(*len = result->ai_addrlen); - assert(sa); - memcpy(sa, result->ai_addr, *len); - - freeaddrinfo(result); - - return sa; - -} - -int pa_context_connect(struct pa_context *c, const char *server, void (*complete) (struct pa_context*c, int success, void *userdata), void *userdata) { - assert(c && c->state == CONTEXT_UNCONNECTED); - - if (pa_authkey_load_from_home(PA_NATIVE_COOKIE_FILE, c->auth_cookie, sizeof(c->auth_cookie)) < 0) { - c->error = PA_ERROR_AUTHKEY; - return -1; - } - - if (!server) - if (!(server = getenv("POLYP_SERVER"))) - server = DEFAULT_SERVER; - - assert(!c->client); - - if (*server == '/') { - if (!(c->client = pa_socket_client_new_unix(c->mainloop, server))) { - c->error = PA_ERROR_CONNECTIONREFUSED; - return -1; - } - } else { - struct sockaddr* sa; - size_t sa_len; - - if (!(sa = resolve_server(server, &sa_len))) { - c->error = PA_ERROR_INVALIDSERVER; - return -1; - } - - c->client = pa_socket_client_new_sockaddr(c->mainloop, sa, sa_len); - free(sa); - - if (!c->client) { - c->error = PA_ERROR_CONNECTIONREFUSED; - return -1; - } - } - - c->connect_complete_callback = complete; - c->connect_complete_userdata = userdata; - - pa_socket_client_set_callback(c->client, on_connection, c); - c->state = CONTEXT_CONNECTING; - - return 0; -} - -int pa_context_is_dead(struct pa_context *c) { - assert(c); - return c->state == CONTEXT_DEAD; -} - -int pa_context_is_ready(struct pa_context *c) { - assert(c); - return c->state == CONTEXT_READY; -} - -int pa_context_errno(struct pa_context *c) { - assert(c); - return c->error; -} - -void pa_context_set_die_callback(struct pa_context *c, void (*cb)(struct pa_context *c, void *userdata), void *userdata) { - assert(c); - c->die_callback = cb; - c->die_userdata = userdata; -} - -static void command_playback_stream_killed(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { - struct pa_context *c = userdata; - struct pa_stream *s; - uint32_t channel; - assert(pd && (command == PA_COMMAND_PLAYBACK_STREAM_KILLED || command == PA_COMMAND_RECORD_STREAM_KILLED) && t && c); - - if (pa_tagstruct_getu32(t, &channel) < 0 || - !pa_tagstruct_eof(t)) { - c->error = PA_ERROR_PROTOCOL; - context_dead(c); - return; - } - - if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, channel))) - return; - - c->error = PA_ERROR_KILLED; - stream_dead(s); -} - -static void command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { - struct pa_stream *s; - struct pa_context *c = userdata; - uint32_t bytes, channel; - assert(pd && command == PA_COMMAND_REQUEST && t && c); - - if (pa_tagstruct_getu32(t, &channel) < 0 || - pa_tagstruct_getu32(t, &bytes) < 0 || - !pa_tagstruct_eof(t)) { - c->error = PA_ERROR_PROTOCOL; - context_dead(c); - return; - } - - if (!(s = pa_dynarray_get(c->playback_streams, channel))) - return; - - if (s->state != STREAM_READY) - return; - - s->requested_bytes += bytes; - - if (s->requested_bytes && s->write_callback) - s->write_callback(s, s->requested_bytes, s->write_userdata); -} - -static void create_stream_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { - struct pa_stream *s = userdata; - assert(pd && s && s->state == STREAM_CREATING); - - if (command != PA_COMMAND_REPLY) { - if (handle_error(s->context, command, t) < 0) { - context_dead(s->context); - return; - } - - stream_dead(s); - if (s->create_complete_callback) - s->create_complete_callback(s, 0, s->create_complete_userdata); - - return; - } - - if (pa_tagstruct_getu32(t, &s->channel) < 0 || - pa_tagstruct_getu32(t, &s->device_index) < 0 || - !pa_tagstruct_eof(t)) { - s->context->error = PA_ERROR_PROTOCOL; - context_dead(s->context); - return; - } - - s->channel_valid = 1; - pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, s); - - s->state = STREAM_READY; - if (s->create_complete_callback) - s->create_complete_callback(s, 1, s->create_complete_userdata); -} - -static void create_stream(struct pa_stream *s, uint32_t tdev_index) { - struct pa_tagstruct *t; - uint32_t tag; - assert(s); - - s->state = STREAM_CREATING; - - t = pa_tagstruct_new(NULL, 0); - assert(t); - - pa_tagstruct_putu32(t, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM); - pa_tagstruct_putu32(t, tag = s->context->ctag++); - pa_tagstruct_puts(t, s->name); - pa_tagstruct_put_sample_spec(t, &s->sample_spec); - pa_tagstruct_putu32(t, tdev_index); - pa_tagstruct_putu32(t, s->buffer_attr.maxlength); - if (s->direction == PA_STREAM_PLAYBACK) { - pa_tagstruct_putu32(t, s->buffer_attr.tlength); - pa_tagstruct_putu32(t, s->buffer_attr.prebuf); - pa_tagstruct_putu32(t, s->buffer_attr.minreq); - } else - pa_tagstruct_putu32(t, s->buffer_attr.fragsize); - - pa_pstream_send_tagstruct(s->context->pstream, t); - pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, s); -} - -static void lookup_device_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { - struct pa_stream *s = userdata; - uint32_t tdev; - assert(pd && s && s->state == STREAM_LOOKING_UP); - - if (command != PA_COMMAND_REPLY) { - if (handle_error(s->context, command, t) < 0) { - context_dead(s->context); - return; - } - - stream_dead(s); - if (s->create_complete_callback) - s->create_complete_callback(s, 0, s->create_complete_userdata); - return; - } - - if (pa_tagstruct_getu32(t, &tdev) < 0 || - !pa_tagstruct_eof(t)) { - s->context->error = PA_ERROR_PROTOCOL; - context_dead(s->context); - return; - } - - create_stream(s, tdev); -} - -static void lookup_device(struct pa_stream *s, const char *tdev) { - struct pa_tagstruct *t; - uint32_t tag; - assert(s); - - s->state = STREAM_LOOKING_UP; - - t = pa_tagstruct_new(NULL, 0); - assert(t); - - pa_tagstruct_putu32(t, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_LOOKUP_SINK : PA_COMMAND_LOOKUP_SOURCE); - pa_tagstruct_putu32(t, tag = s->context->ctag++); - pa_tagstruct_puts(t, tdev); - - pa_pstream_send_tagstruct(s->context->pstream, t); - pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, lookup_device_callback, s); -} - -struct pa_stream* pa_stream_new( - struct pa_context *c, - enum pa_stream_direction dir, - const char *dev, - const char *name, - const struct pa_sample_spec *ss, - const struct pa_buffer_attr *attr, - void (*complete) (struct pa_stream*s, int success, void *userdata), - void *userdata) { - - struct pa_stream *s; - - assert(c && name && ss && c->state == CONTEXT_READY); - - s = malloc(sizeof(struct pa_stream)); - assert(s); - s->context = c; - - s->read_callback = NULL; - s->read_userdata = NULL; - s->write_callback = NULL; - s->write_userdata = NULL; - s->die_callback = NULL; - s->die_userdata = NULL; - s->create_complete_callback = complete; - s->create_complete_userdata = NULL; - s->get_latency_callback = NULL; - s->get_latency_userdata = NULL; - - s->name = strdup(name); - s->state = STREAM_CREATING; - s->requested_bytes = 0; - s->channel = 0; - s->channel_valid = 0; - s->device_index = (uint32_t) -1; - s->direction = dir; - s->sample_spec = *ss; - if (attr) - s->buffer_attr = *attr; - else { - s->buffer_attr.maxlength = DEFAULT_MAXLENGTH; - s->buffer_attr.tlength = DEFAULT_TLENGTH; - s->buffer_attr.prebuf = DEFAULT_PREBUF; - s->buffer_attr.minreq = DEFAULT_MINREQ; - s->buffer_attr.fragsize = DEFAULT_FRAGSIZE; - } - - s->next = c->first_stream; - if (s->next) - s->next->previous = s; - s->previous = NULL; - c->first_stream = s; - - if (dev) - lookup_device(s, dev); - else - create_stream(s, (uint32_t) -1); - - return s; -} - -void pa_stream_free(struct pa_stream *s) { - assert(s && s->context); - - if (s->context->pdispatch) - pa_pdispatch_unregister_reply(s->context->pdispatch, s); - - free(s->name); - - if (s->channel_valid && s->context->state == CONTEXT_READY) { - struct pa_tagstruct *t = pa_tagstruct_new(NULL, 0); - assert(t); - - pa_tagstruct_putu32(t, PA_COMMAND_DELETE_PLAYBACK_STREAM); - pa_tagstruct_putu32(t, s->context->ctag++); - pa_tagstruct_putu32(t, s->channel); - pa_pstream_send_tagstruct(s->context->pstream, t); - } - - if (s->channel_valid) - pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, NULL); - - if (s->next) - s->next->previous = s->previous; - if (s->previous) - s->previous->next = s->next; - else - s->context->first_stream = s->next; - - free(s); -} - -void pa_stream_set_write_callback(struct pa_stream *s, void (*cb)(struct pa_stream *p, size_t length, void *userdata), void *userdata) { - assert(s && cb); - s->write_callback = cb; - s->write_userdata = userdata; -} - -void pa_stream_write(struct pa_stream *s, const void *data, size_t length) { - struct pa_memchunk chunk; - assert(s && s->context && data && length && s->state == STREAM_READY); - - chunk.memblock = pa_memblock_new(length); - assert(chunk.memblock && chunk.memblock->data); - memcpy(chunk.memblock->data, data, length); - chunk.index = 0; - chunk.length = length; - - pa_pstream_send_memblock(s->context->pstream, s->channel, 0, &chunk); - pa_memblock_unref(chunk.memblock); - - /*fprintf(stderr, "Sent %u bytes\n", length);*/ - - if (length < s->requested_bytes) - s->requested_bytes -= length; - else - s->requested_bytes = 0; -} - -size_t pa_stream_writable_size(struct pa_stream *s) { - assert(s && s->state == STREAM_READY); - return s->requested_bytes; -} - -void pa_stream_set_read_callback(struct pa_stream *s, void (*cb)(struct pa_stream *p, const void*data, size_t length, void *userdata), void *userdata) { - assert(s && cb); - s->read_callback = cb; - s->read_userdata = userdata; -} - -int pa_stream_is_dead(struct pa_stream *s) { - return s->state == STREAM_DEAD; -} - -int pa_stream_is_ready(struct pa_stream*s) { - return s->state == STREAM_READY; -} - -void pa_stream_set_die_callback(struct pa_stream *s, void (*cb)(struct pa_stream *s, void *userdata), void *userdata) { - assert(s); - s->die_callback = cb; - s->die_userdata = userdata; -} - -int pa_context_is_pending(struct pa_context *c) { - assert(c); - - if (c->state != CONTEXT_READY) - return 0; - - return pa_pstream_is_pending(c->pstream) || pa_pdispatch_is_pending(c->pdispatch); -} - -struct pa_context* pa_stream_get_context(struct pa_stream *p) { - assert(p); - return p->context; -} - -static void set_dispatch_callbacks(struct pa_context *c); - -static void pdispatch_drain_callback(struct pa_pdispatch*pd, void *userdata) { - set_dispatch_callbacks(userdata); -} - -static void pstream_drain_callback(struct pa_pstream *s, void *userdata) { - set_dispatch_callbacks(userdata); -} - -static void set_dispatch_callbacks(struct pa_context *c) { - assert(c && c->state == CONTEXT_READY); - - pa_pstream_set_drain_callback(c->pstream, NULL, NULL); - pa_pdispatch_set_drain_callback(c->pdispatch, NULL, NULL); - - if (pa_pdispatch_is_pending(c->pdispatch)) { - pa_pdispatch_set_drain_callback(c->pdispatch, pdispatch_drain_callback, c); - return; - } - - if (pa_pstream_is_pending(c->pstream)) { - pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c); - return; - } - - assert(c->drain_complete_callback); - c->drain_complete_callback(c, c->drain_complete_userdata); -} - -int pa_context_drain( - struct pa_context *c, - void (*complete) (struct pa_context*c, void *userdata), - void *userdata) { - - assert(c && c->state == CONTEXT_READY); - - if (complete == NULL) { - c->drain_complete_callback = NULL; - pa_pstream_set_drain_callback(c->pstream, NULL, NULL); - pa_pdispatch_set_drain_callback(c->pdispatch, NULL, NULL); - return 0; - } - - if (!pa_context_is_pending(c)) - return -1; - - c->drain_complete_callback = complete; - c->drain_complete_userdata = userdata; - - set_dispatch_callbacks(c); - - return 0; -} - -static void stream_drain_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { - struct pa_stream *s = userdata; - assert(pd && s); - - if (command != PA_COMMAND_REPLY) { - if (handle_error(s->context, command, t) < 0) { - context_dead(s->context); - return; - } - - stream_dead(s); - return; - } - - if (s->state != STREAM_READY) - return; - - if (!pa_tagstruct_eof(t)) { - s->context->error = PA_ERROR_PROTOCOL; - context_dead(s->context); - return; - } - - if (s->drain_complete_callback) { - void (*temp) (struct pa_stream*s, void *userdata) = s->drain_complete_callback; - s->drain_complete_callback = NULL; - temp(s, s->drain_complete_userdata); - } -} - - -void pa_stream_drain(struct pa_stream *s, void (*complete) (struct pa_stream*s, void *userdata), void *userdata) { - struct pa_tagstruct *t; - uint32_t tag; - assert(s && s->state == STREAM_READY); - - if (!complete) { - s->drain_complete_callback = NULL; - return; - } - - s->drain_complete_callback = complete; - s->drain_complete_userdata = userdata; - - t = pa_tagstruct_new(NULL, 0); - assert(t); - - pa_tagstruct_putu32(t, PA_COMMAND_DRAIN_PLAYBACK_STREAM); - pa_tagstruct_putu32(t, tag = s->context->ctag++); - pa_tagstruct_putu32(t, s->channel); - pa_pstream_send_tagstruct(s->context->pstream, t); - pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_drain_callback, s); -} - -void pa_context_exit(struct pa_context *c) { - struct pa_tagstruct *t; - t = pa_tagstruct_new(NULL, 0); - assert(t); - pa_tagstruct_putu32(t, PA_COMMAND_EXIT); - pa_tagstruct_putu32(t, c->ctag++); - pa_pstream_send_tagstruct(c->pstream, t); -} - -static void context_stat_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { - struct pa_context *c = userdata; - uint32_t total, count; - assert(pd && c); - - if (command != PA_COMMAND_REPLY) { - if (handle_error(c, command, t) < 0) { - context_dead(c); - return; - } - - if (c->stat_callback) - c->stat_callback(c, (uint32_t) -1, (uint32_t) -1, c->stat_userdata); - return; - } - - if (pa_tagstruct_getu32(t, &count) < 0 || - pa_tagstruct_getu32(t, &total) < 0 || - !pa_tagstruct_eof(t)) { - c->error = PA_ERROR_PROTOCOL; - context_dead(c); - return; - } - - if (c->stat_callback) - c->stat_callback(c, count, total, c->stat_userdata); -} - -void pa_context_stat(struct pa_context *c, void (*cb)(struct pa_context *c, uint32_t count, uint32_t total, void *userdata), void *userdata) { - uint32_t tag; - struct pa_tagstruct *t; - - c->stat_callback = cb; - c->stat_userdata = userdata; - - if (cb == NULL) - return; - - t = pa_tagstruct_new(NULL, 0); - assert(t); - pa_tagstruct_putu32(t, PA_COMMAND_STAT); - pa_tagstruct_putu32(t, tag = c->ctag++); - pa_pstream_send_tagstruct(c->pstream, t); - pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, context_stat_callback, c); -} - -static void stream_get_latency_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { - struct pa_stream *s = userdata; - uint32_t latency; - assert(pd && s); - - if (command != PA_COMMAND_REPLY) { - if (handle_error(s->context, command, t) < 0) { - context_dead(s->context); - return; - } - - if (s->get_latency_callback) - s->get_latency_callback(s, (uint32_t) -1, s->get_latency_userdata); - return; - } - - if (pa_tagstruct_getu32(t, &latency) < 0 || - !pa_tagstruct_eof(t)) { - s->context->error = PA_ERROR_PROTOCOL; - context_dead(s->context); - return; - } - - if (s->get_latency_callback) - s->get_latency_callback(s, latency, s->get_latency_userdata); -} - -void pa_stream_get_latency(struct pa_stream *p, void (*cb)(struct pa_stream *p, uint32_t latency, void *userdata), void *userdata) { - uint32_t tag; - struct pa_tagstruct *t; - - p->get_latency_callback = cb; - p->get_latency_userdata = userdata; - - if (cb == NULL) - return; - - t = pa_tagstruct_new(NULL, 0); - assert(t); - pa_tagstruct_putu32(t, PA_COMMAND_GET_PLAYBACK_LATENCY); - pa_tagstruct_putu32(t, tag = p->context->ctag++); - pa_tagstruct_putu32(t, p->channel); - pa_pstream_send_tagstruct(p->context->pstream, t); - pa_pdispatch_register_reply(p->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, p); -} diff --git a/src/polyp.h b/src/polyp.h deleted file mode 100644 index 00849445..00000000 --- a/src/polyp.h +++ /dev/null @@ -1,73 +0,0 @@ -#ifndef foopolyphfoo -#define foopolyphfoo - -#include - -#include "sample.h" -#include "polypdef.h" -#include "mainloop-api.h" - -struct pa_context; - -struct pa_context *pa_context_new(struct pa_mainloop_api *mainloop, const char *name); - -int pa_context_connect( - struct pa_context *c, - const char *server, - void (*complete) (struct pa_context*c, int success, void *userdata), - void *userdata); - -int pa_context_drain( - struct pa_context *c, - void (*complete) (struct pa_context*c, void *userdata), - void *userdata); - -void pa_context_free(struct pa_context *c); - -void pa_context_set_die_callback(struct pa_context *c, void (*cb)(struct pa_context *c, void *userdata), void *userdata); - -int pa_context_is_dead(struct pa_context *c); -int pa_context_is_ready(struct pa_context *c); -int pa_context_errno(struct pa_context *c); - -int pa_context_is_pending(struct pa_context *c); - -void pa_context_exit(struct pa_context *c); -void pa_context_stat(struct pa_context *c, void (*cb)(struct pa_context *c, uint32_t count, uint32_t total, void *userdata), void *userdata); - -struct pa_stream; - -struct pa_stream* pa_stream_new( - struct pa_context *c, - enum pa_stream_direction dir, - const char *dev, - const char *name, - const struct pa_sample_spec *ss, - const struct pa_buffer_attr *attr, - void (*complete) (struct pa_stream*s, int success, void *userdata), - void *userdata); - -void pa_stream_free(struct pa_stream *p); - -void pa_stream_drain( - struct pa_stream *s, - void (*complete) (struct pa_stream*s, void *userdata), - void *userdata); - - -void pa_stream_set_die_callback(struct pa_stream *s, void (*cb)(struct pa_stream *s, void *userdata), void *userdata); - -void pa_stream_set_write_callback(struct pa_stream *p, void (*cb)(struct pa_stream *p, size_t length, void *userdata), void *userdata); -void pa_stream_write(struct pa_stream *p, const void *data, size_t length); -size_t pa_stream_writable_size(struct pa_stream *p); - -void pa_stream_set_read_callback(struct pa_stream *p, void (*cb)(struct pa_stream *p, const void*data, size_t length, void *userdata), void *userdata); - -int pa_stream_is_dead(struct pa_stream *p); -int pa_stream_is_ready(struct pa_stream*p); - -void pa_stream_get_latency(struct pa_stream *p, void (*cb)(struct pa_stream *p, uint32_t latency, void *userdata), void *userdata); - -struct pa_context* pa_stream_get_context(struct pa_stream *p); - -#endif diff --git a/src/polypdef.h b/src/polypdef.h deleted file mode 100644 index 6cfafc97..00000000 --- a/src/polypdef.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef foopolypdefhfoo -#define foopolypdefhfoo - -#include - -enum pa_stream_direction { - PA_STREAM_PLAYBACK, - PA_STREAM_RECORD -}; - -struct pa_buffer_attr { - uint32_t maxlength; - uint32_t tlength; - uint32_t prebuf; - uint32_t minreq; - uint32_t fragsize; -}; - - -#endif diff --git a/src/polyplib-def.h b/src/polyplib-def.h new file mode 100644 index 00000000..6cfafc97 --- /dev/null +++ b/src/polyplib-def.h @@ -0,0 +1,20 @@ +#ifndef foopolypdefhfoo +#define foopolypdefhfoo + +#include + +enum pa_stream_direction { + PA_STREAM_PLAYBACK, + PA_STREAM_RECORD +}; + +struct pa_buffer_attr { + uint32_t maxlength; + uint32_t tlength; + uint32_t prebuf; + uint32_t minreq; + uint32_t fragsize; +}; + + +#endif diff --git a/src/polyplib-error.c b/src/polyplib-error.c new file mode 100644 index 00000000..86166b0c --- /dev/null +++ b/src/polyplib-error.c @@ -0,0 +1,28 @@ +#include +#include + +#include "polyp-error.h" +#include "protocol-native-spec.h" + +static const char* const errortab[PA_ERROR_MAX] = { + [PA_ERROR_OK] = "OK", + [PA_ERROR_ACCESS] = "Access denied", + [PA_ERROR_COMMAND] = "Unknown command", + [PA_ERROR_INVALID] = "Invalid argument", + [PA_ERROR_EXIST] = "Entity exists", + [PA_ERROR_NOENTITY] = "No such entity", + [PA_ERROR_CONNECTIONREFUSED] = "Connection refused", + [PA_ERROR_PROTOCOL] = "Protocol corrupt", + [PA_ERROR_TIMEOUT] = "Timeout", + [PA_ERROR_AUTHKEY] = "Not authorization key", + [PA_ERROR_INTERNAL] = "Internal error", + [PA_ERROR_CONNECTIONTERMINATED] = "Connection terminated", + [PA_ERROR_KILLED] = "Entity killed", +}; + +const char*pa_strerror(uint32_t error) { + if (error >= PA_ERROR_MAX) + return NULL; + + return errortab[error]; +} diff --git a/src/polyplib-error.h b/src/polyplib-error.h new file mode 100644 index 00000000..7407f34c --- /dev/null +++ b/src/polyplib-error.h @@ -0,0 +1,10 @@ +#ifndef foopolyperrhfoo +#define foopolyperrhfoo + +#include + +#include "protocol-native-spec.h" + +const char* pa_strerror(uint32_t error); + +#endif diff --git a/src/polyplib-simple.c b/src/polyplib-simple.c new file mode 100644 index 00000000..5f86c5bf --- /dev/null +++ b/src/polyplib-simple.c @@ -0,0 +1,233 @@ +#include +#include +#include +#include + +#include "simple.h" +#include "polyp.h" +#include "mainloop.h" +#include "polyp-error.h" + +struct pa_simple { + struct pa_mainloop *mainloop; + struct pa_context *context; + struct pa_stream *stream; + enum pa_stream_direction direction; + + int dead, drained; + + void *read_data; + size_t read_index, read_length; +}; + +static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata); + +static int check_error(struct pa_simple *p, int *perror) { + assert(p); + + if (pa_context_is_dead(p->context) || (p->stream && pa_stream_is_dead(p->stream))) { + if (perror) + *perror = pa_context_errno(p->context); + return -1; + } + + return 0; +} + +static int iterate(struct pa_simple *p, int block, int *perror) { + assert(p && p->context && p->mainloop); + + if (check_error(p, perror) < 0) + return -1; + + if (!block && !pa_context_is_pending(p->context)) + return 0; + + do { + if (pa_mainloop_iterate(p->mainloop, 1, NULL) < 0) { + if (perror) + *perror = PA_ERROR_INTERNAL; + return -1; + } + + if (check_error(p, perror) < 0) + return -1; + + } while (pa_context_is_pending(p->context)); + + return 0; +} + +struct pa_simple* pa_simple_new( + const char *server, + const char *name, + enum pa_stream_direction dir, + const char *dev, + const char *stream_name, + const struct pa_sample_spec *ss, + const struct pa_buffer_attr *attr, + int *perror) { + + struct pa_simple *p; + int error = PA_ERROR_INTERNAL; + assert(ss); + + p = malloc(sizeof(struct pa_simple)); + assert(p); + p->context = NULL; + p->stream = NULL; + p->mainloop = pa_mainloop_new(); + assert(p->mainloop); + p->dead = 0; + p->direction = dir; + p->read_data = NULL; + p->read_index = p->read_length = 0; + + if (!(p->context = pa_context_new(pa_mainloop_get_api(p->mainloop), name))) + goto fail; + + if (pa_context_connect(p->context, server, NULL, NULL) < 0) { + error = pa_context_errno(p->context); + goto fail; + } + + /* Wait until the context is ready */ + while (!pa_context_is_ready(p->context)) { + if (iterate(p, 1, &error) < 0) + goto fail; + } + + if (!(p->stream = pa_stream_new(p->context, dir, dev, stream_name, ss, attr, NULL, NULL))) + goto fail; + + /* Wait until the stream is ready */ + while (!pa_stream_is_ready(p->stream)) { + if (iterate(p, 1, &error) < 0) + goto fail; + } + + pa_stream_set_read_callback(p->stream, read_callback, p); + + return p; + +fail: + if (perror) + *perror = error; + pa_simple_free(p); + return NULL; +} + +void pa_simple_free(struct pa_simple *s) { + assert(s); + + free(s->read_data); + + if (s->stream) + pa_stream_free(s->stream); + + if (s->context) + pa_context_free(s->context); + + if (s->mainloop) + pa_mainloop_free(s->mainloop); + + free(s); +} + +int pa_simple_write(struct pa_simple *p, const void*data, size_t length, int *perror) { + assert(p && data && p->direction == PA_STREAM_PLAYBACK); + + while (length > 0) { + size_t l; + + while (!(l = pa_stream_writable_size(p->stream))) + if (iterate(p, 1, perror) < 0) + return -1; + + if (l > length) + l = length; + + pa_stream_write(p->stream, data, l); + data += l; + length -= l; + } + + /* Make sure that no data is pending for write */ + if (iterate(p, 0, perror) < 0) + return -1; + + return 0; +} + +static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) { + struct pa_simple *p = userdata; + assert(s && data && length && p); + + if (p->read_data) { + fprintf(stderr, __FILE__": Buffer overflow, dropping incoming memory blocks.\n"); + free(p->read_data); + } + + p->read_data = malloc(p->read_length = length); + assert(p->read_data); + memcpy(p->read_data, data, length); + p->read_index = 0; +} + +int pa_simple_read(struct pa_simple *p, void*data, size_t length, int *perror) { + assert(p && data && p->direction == PA_STREAM_RECORD); + + while (length > 0) { + if (p->read_data) { + size_t l = length; + + if (p->read_length <= l) + l = p->read_length; + + memcpy(data, p->read_data+p->read_index, l); + + data += l; + length -= l; + + p->read_index += l; + p->read_length -= l; + + if (!p->read_length) { + free(p->read_data); + p->read_data = NULL; + p->read_index = 0; + } + + if (!length) + return 0; + + assert(!p->read_data); + } + + if (iterate(p, 1, perror) < 0) + return -1; + } + + return 0; +} + +static void drain_complete(struct pa_stream *s, void *userdata) { + struct pa_simple *p = userdata; + assert(s && p); + p->drained = 1; +} + +int pa_simple_drain(struct pa_simple *p, int *perror) { + assert(p && p->direction == PA_STREAM_PLAYBACK); + p->drained = 0; + pa_stream_drain(p->stream, drain_complete, p); + + while (!p->drained) { + if (iterate(p, 1, perror) < 0) { + pa_stream_drain(p->stream, NULL, NULL); + return -1; + } + } + + return 0; +} diff --git a/src/polyplib-simple.h b/src/polyplib-simple.h new file mode 100644 index 00000000..f5f872ee --- /dev/null +++ b/src/polyplib-simple.h @@ -0,0 +1,28 @@ +#ifndef foosimplehfoo +#define foosimplehfoo + +#include + +#include "sample.h" +#include "polypdef.h" + +struct pa_simple; + +struct pa_simple* pa_simple_new( + const char *server, + const char *name, + enum pa_stream_direction dir, + const char *dev, + const char *stream_name, + const struct pa_sample_spec *ss, + const struct pa_buffer_attr *attr, + int *error); + +void pa_simple_free(struct pa_simple *s); + +int pa_simple_write(struct pa_simple *s, const void*data, size_t length, int *error); +int pa_simple_drain(struct pa_simple *s, int *error); + +int pa_simple_read(struct pa_simple *s, void*data, size_t length, int *error); + +#endif diff --git a/src/polyplib.c b/src/polyplib.c new file mode 100644 index 00000000..d6d0f908 --- /dev/null +++ b/src/polyplib.c @@ -0,0 +1,948 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "polyp.h" +#include "protocol-native-spec.h" +#include "pdispatch.h" +#include "pstream.h" +#include "dynarray.h" +#include "socket-client.h" +#include "pstream-util.h" +#include "authkey.h" +#include "util.h" + +#define DEFAULT_MAXLENGTH 204800 +#define DEFAULT_TLENGTH 10240 +#define DEFAULT_PREBUF 4096 +#define DEFAULT_MINREQ 1024 +#define DEFAULT_FRAGSIZE 1024 + +#define DEFAULT_TIMEOUT (5*60) +#define DEFAULT_SERVER "/tmp/polypaudio/native" +#define DEFAULT_PORT "4713" + +struct pa_context { + char *name; + struct pa_mainloop_api* mainloop; + struct pa_socket_client *client; + struct pa_pstream *pstream; + struct pa_pdispatch *pdispatch; + struct pa_dynarray *record_streams, *playback_streams; + struct pa_stream *first_stream; + uint32_t ctag; + uint32_t error; + enum { + CONTEXT_UNCONNECTED, + CONTEXT_CONNECTING, + CONTEXT_AUTHORIZING, + CONTEXT_SETTING_NAME, + CONTEXT_READY, + CONTEXT_DEAD + } state; + + void (*connect_complete_callback)(struct pa_context*c, int success, void *userdata); + void *connect_complete_userdata; + + void (*drain_complete_callback)(struct pa_context*c, void *userdata); + void *drain_complete_userdata; + + void (*die_callback)(struct pa_context*c, void *userdata); + void *die_userdata; + + void (*stat_callback)(struct pa_context*c, uint32_t count, uint32_t total, void *userdata); + void *stat_userdata; + + uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH]; +}; + +struct pa_stream { + struct pa_context *context; + struct pa_stream *next, *previous; + + char *name; + struct pa_buffer_attr buffer_attr; + struct pa_sample_spec sample_spec; + uint32_t device_index; + uint32_t channel; + int channel_valid; + enum pa_stream_direction direction; + + enum { STREAM_LOOKING_UP, STREAM_CREATING, STREAM_READY, STREAM_DEAD} state; + uint32_t requested_bytes; + + void (*read_callback)(struct pa_stream *p, const void*data, size_t length, void *userdata); + void *read_userdata; + + void (*write_callback)(struct pa_stream *p, size_t length, void *userdata); + void *write_userdata; + + void (*create_complete_callback)(struct pa_stream *s, int success, void *userdata); + void *create_complete_userdata; + + void (*drain_complete_callback)(struct pa_stream *s, void *userdata); + void *drain_complete_userdata; + + void (*die_callback)(struct pa_stream*c, void *userdata); + void *die_userdata; + + void (*get_latency_callback)(struct pa_stream*c, uint32_t latency, void *userdata); + void *get_latency_userdata; +}; + +static void command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata); +static void command_playback_stream_killed(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata); + +static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = { + [PA_COMMAND_ERROR] = { NULL }, + [PA_COMMAND_REPLY] = { NULL }, + [PA_COMMAND_CREATE_PLAYBACK_STREAM] = { NULL }, + [PA_COMMAND_DELETE_PLAYBACK_STREAM] = { NULL }, + [PA_COMMAND_CREATE_RECORD_STREAM] = { NULL }, + [PA_COMMAND_DELETE_RECORD_STREAM] = { NULL }, + [PA_COMMAND_EXIT] = { NULL }, + [PA_COMMAND_REQUEST] = { command_request }, + [PA_COMMAND_PLAYBACK_STREAM_KILLED] = { command_playback_stream_killed }, + [PA_COMMAND_RECORD_STREAM_KILLED] = { command_playback_stream_killed }, +}; + +struct pa_context *pa_context_new(struct pa_mainloop_api *mainloop, const char *name) { + struct pa_context *c; + assert(mainloop && name); + + c = malloc(sizeof(struct pa_context)); + assert(c); + c->name = strdup(name); + c->mainloop = mainloop; + c->client = NULL; + c->pstream = NULL; + c->pdispatch = NULL; + c->playback_streams = pa_dynarray_new(); + assert(c->playback_streams); + c->record_streams = pa_dynarray_new(); + assert(c->record_streams); + c->first_stream = NULL; + c->error = PA_ERROR_OK; + c->state = CONTEXT_UNCONNECTED; + c->ctag = 0; + + c->connect_complete_callback = NULL; + c->connect_complete_userdata = NULL; + + c->drain_complete_callback = NULL; + c->drain_complete_userdata = NULL; + + c->die_callback = NULL; + c->die_userdata = NULL; + + c->stat_callback = NULL; + c->stat_userdata = NULL; + + pa_check_for_sigpipe(); + return c; +} + +void pa_context_free(struct pa_context *c) { + assert(c); + + while (c->first_stream) + pa_stream_free(c->first_stream); + + if (c->client) + pa_socket_client_free(c->client); + if (c->pdispatch) + pa_pdispatch_free(c->pdispatch); + if (c->pstream) + pa_pstream_free(c->pstream); + if (c->record_streams) + pa_dynarray_free(c->record_streams, NULL, NULL); + if (c->playback_streams) + pa_dynarray_free(c->playback_streams, NULL, NULL); + + free(c->name); + free(c); +} + +static void stream_dead(struct pa_stream *s) { + assert(s); + + if (s->state == STREAM_DEAD) + return; + + if (s->state == STREAM_READY) { + s->state = STREAM_DEAD; + if (s->die_callback) + s->die_callback(s, s->die_userdata); + } else + s->state = STREAM_DEAD; +} + +static void context_dead(struct pa_context *c) { + struct pa_stream *s; + assert(c); + + if (c->state == CONTEXT_DEAD) + return; + + if (c->pdispatch) + pa_pdispatch_free(c->pdispatch); + c->pdispatch = NULL; + + if (c->pstream) + pa_pstream_free(c->pstream); + c->pstream = NULL; + + if (c->client) + pa_socket_client_free(c->client); + c->client = NULL; + + for (s = c->first_stream; s; s = s->next) + stream_dead(s); + + if (c->state == CONTEXT_READY) { + c->state = CONTEXT_DEAD; + if (c->die_callback) + c->die_callback(c, c->die_userdata); + } else + c->state = CONTEXT_DEAD; +} + +static void pstream_die_callback(struct pa_pstream *p, void *userdata) { + struct pa_context *c = userdata; + assert(p && c); + c->error = PA_ERROR_CONNECTIONTERMINATED; + context_dead(c); +} + +static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) { + struct pa_context *c = userdata; + assert(p && packet && c); + + if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) { + fprintf(stderr, "polyp.c: invalid packet.\n"); + c->error = PA_ERROR_PROTOCOL; + context_dead(c); + } +} + +static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata) { + struct pa_context *c = userdata; + struct pa_stream *s; + assert(p && chunk && c && chunk->memblock && chunk->memblock->data); + + if (!(s = pa_dynarray_get(c->record_streams, channel))) + return; + + if (s->read_callback) + s->read_callback(s, chunk->memblock->data + chunk->index, chunk->length, s->read_userdata); +} + +static int handle_error(struct pa_context *c, uint32_t command, struct pa_tagstruct *t) { + assert(c && t); + + if (command == PA_COMMAND_ERROR) { + if (pa_tagstruct_getu32(t, &c->error) < 0) { + c->error = PA_ERROR_PROTOCOL; + return -1; + } + + return 0; + } + + c->error = (command == PA_COMMAND_TIMEOUT) ? PA_ERROR_TIMEOUT : PA_ERROR_INTERNAL; + return -1; +} + +static void setup_complete_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { + struct pa_context *c = userdata; + assert(pd && c && (c->state == CONTEXT_AUTHORIZING || c->state == CONTEXT_SETTING_NAME)); + + if (command != PA_COMMAND_REPLY) { + handle_error(c, command, t); + context_dead(c); + + if (c->connect_complete_callback) + c->connect_complete_callback(c, 0, c->connect_complete_userdata); + + return; + } + + if (c->state == CONTEXT_AUTHORIZING) { + struct pa_tagstruct *t; + c->state = CONTEXT_SETTING_NAME; + t = pa_tagstruct_new(NULL, 0); + assert(t); + pa_tagstruct_putu32(t, PA_COMMAND_SET_NAME); + pa_tagstruct_putu32(t, tag = c->ctag++); + pa_tagstruct_puts(t, c->name); + pa_pstream_send_tagstruct(c->pstream, t); + pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c); + } else { + assert(c->state == CONTEXT_SETTING_NAME); + + c->state = CONTEXT_READY; + + if (c->connect_complete_callback) + c->connect_complete_callback(c, 1, c->connect_complete_userdata); + } + + return; +} + +static void on_connection(struct pa_socket_client *client, struct pa_iochannel*io, void *userdata) { + struct pa_context *c = userdata; + struct pa_tagstruct *t; + uint32_t tag; + assert(client && c && c->state == CONTEXT_CONNECTING); + + pa_socket_client_free(client); + c->client = NULL; + + if (!io) { + c->error = PA_ERROR_CONNECTIONREFUSED; + context_dead(c); + + if (c->connect_complete_callback) + c->connect_complete_callback(c, 0, c->connect_complete_userdata); + + return; + } + + c->pstream = pa_pstream_new(c->mainloop, io); + assert(c->pstream); + pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c); + pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c); + pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c); + + c->pdispatch = pa_pdispatch_new(c->mainloop, command_table, PA_COMMAND_MAX); + assert(c->pdispatch); + + t = pa_tagstruct_new(NULL, 0); + assert(t); + pa_tagstruct_putu32(t, PA_COMMAND_AUTH); + pa_tagstruct_putu32(t, tag = c->ctag++); + pa_tagstruct_put_arbitrary(t, c->auth_cookie, sizeof(c->auth_cookie)); + pa_pstream_send_tagstruct(c->pstream, t); + pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c); + c->state = CONTEXT_AUTHORIZING; +} + +static struct sockaddr *resolve_server(const char *server, size_t *len) { + struct sockaddr *sa; + struct addrinfo hints, *result = NULL; + char *port; + assert(server && len); + + if ((port = strrchr(server, ':'))) + port++; + if (!port) + port = DEFAULT_PORT; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = 0; + + if (getaddrinfo(server, port, &hints, &result) != 0) + return NULL; + assert(result); + + sa = malloc(*len = result->ai_addrlen); + assert(sa); + memcpy(sa, result->ai_addr, *len); + + freeaddrinfo(result); + + return sa; + +} + +int pa_context_connect(struct pa_context *c, const char *server, void (*complete) (struct pa_context*c, int success, void *userdata), void *userdata) { + assert(c && c->state == CONTEXT_UNCONNECTED); + + if (pa_authkey_load_from_home(PA_NATIVE_COOKIE_FILE, c->auth_cookie, sizeof(c->auth_cookie)) < 0) { + c->error = PA_ERROR_AUTHKEY; + return -1; + } + + if (!server) + if (!(server = getenv("POLYP_SERVER"))) + server = DEFAULT_SERVER; + + assert(!c->client); + + if (*server == '/') { + if (!(c->client = pa_socket_client_new_unix(c->mainloop, server))) { + c->error = PA_ERROR_CONNECTIONREFUSED; + return -1; + } + } else { + struct sockaddr* sa; + size_t sa_len; + + if (!(sa = resolve_server(server, &sa_len))) { + c->error = PA_ERROR_INVALIDSERVER; + return -1; + } + + c->client = pa_socket_client_new_sockaddr(c->mainloop, sa, sa_len); + free(sa); + + if (!c->client) { + c->error = PA_ERROR_CONNECTIONREFUSED; + return -1; + } + } + + c->connect_complete_callback = complete; + c->connect_complete_userdata = userdata; + + pa_socket_client_set_callback(c->client, on_connection, c); + c->state = CONTEXT_CONNECTING; + + return 0; +} + +int pa_context_is_dead(struct pa_context *c) { + assert(c); + return c->state == CONTEXT_DEAD; +} + +int pa_context_is_ready(struct pa_context *c) { + assert(c); + return c->state == CONTEXT_READY; +} + +int pa_context_errno(struct pa_context *c) { + assert(c); + return c->error; +} + +void pa_context_set_die_callback(struct pa_context *c, void (*cb)(struct pa_context *c, void *userdata), void *userdata) { + assert(c); + c->die_callback = cb; + c->die_userdata = userdata; +} + +static void command_playback_stream_killed(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { + struct pa_context *c = userdata; + struct pa_stream *s; + uint32_t channel; + assert(pd && (command == PA_COMMAND_PLAYBACK_STREAM_KILLED || command == PA_COMMAND_RECORD_STREAM_KILLED) && t && c); + + if (pa_tagstruct_getu32(t, &channel) < 0 || + !pa_tagstruct_eof(t)) { + c->error = PA_ERROR_PROTOCOL; + context_dead(c); + return; + } + + if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, channel))) + return; + + c->error = PA_ERROR_KILLED; + stream_dead(s); +} + +static void command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { + struct pa_stream *s; + struct pa_context *c = userdata; + uint32_t bytes, channel; + assert(pd && command == PA_COMMAND_REQUEST && t && c); + + if (pa_tagstruct_getu32(t, &channel) < 0 || + pa_tagstruct_getu32(t, &bytes) < 0 || + !pa_tagstruct_eof(t)) { + c->error = PA_ERROR_PROTOCOL; + context_dead(c); + return; + } + + if (!(s = pa_dynarray_get(c->playback_streams, channel))) + return; + + if (s->state != STREAM_READY) + return; + + s->requested_bytes += bytes; + + if (s->requested_bytes && s->write_callback) + s->write_callback(s, s->requested_bytes, s->write_userdata); +} + +static void create_stream_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { + struct pa_stream *s = userdata; + assert(pd && s && s->state == STREAM_CREATING); + + if (command != PA_COMMAND_REPLY) { + if (handle_error(s->context, command, t) < 0) { + context_dead(s->context); + return; + } + + stream_dead(s); + if (s->create_complete_callback) + s->create_complete_callback(s, 0, s->create_complete_userdata); + + return; + } + + if (pa_tagstruct_getu32(t, &s->channel) < 0 || + pa_tagstruct_getu32(t, &s->device_index) < 0 || + !pa_tagstruct_eof(t)) { + s->context->error = PA_ERROR_PROTOCOL; + context_dead(s->context); + return; + } + + s->channel_valid = 1; + pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, s); + + s->state = STREAM_READY; + if (s->create_complete_callback) + s->create_complete_callback(s, 1, s->create_complete_userdata); +} + +static void create_stream(struct pa_stream *s, uint32_t tdev_index) { + struct pa_tagstruct *t; + uint32_t tag; + assert(s); + + s->state = STREAM_CREATING; + + t = pa_tagstruct_new(NULL, 0); + assert(t); + + pa_tagstruct_putu32(t, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM); + pa_tagstruct_putu32(t, tag = s->context->ctag++); + pa_tagstruct_puts(t, s->name); + pa_tagstruct_put_sample_spec(t, &s->sample_spec); + pa_tagstruct_putu32(t, tdev_index); + pa_tagstruct_putu32(t, s->buffer_attr.maxlength); + if (s->direction == PA_STREAM_PLAYBACK) { + pa_tagstruct_putu32(t, s->buffer_attr.tlength); + pa_tagstruct_putu32(t, s->buffer_attr.prebuf); + pa_tagstruct_putu32(t, s->buffer_attr.minreq); + } else + pa_tagstruct_putu32(t, s->buffer_attr.fragsize); + + pa_pstream_send_tagstruct(s->context->pstream, t); + pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, s); +} + +static void lookup_device_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { + struct pa_stream *s = userdata; + uint32_t tdev; + assert(pd && s && s->state == STREAM_LOOKING_UP); + + if (command != PA_COMMAND_REPLY) { + if (handle_error(s->context, command, t) < 0) { + context_dead(s->context); + return; + } + + stream_dead(s); + if (s->create_complete_callback) + s->create_complete_callback(s, 0, s->create_complete_userdata); + return; + } + + if (pa_tagstruct_getu32(t, &tdev) < 0 || + !pa_tagstruct_eof(t)) { + s->context->error = PA_ERROR_PROTOCOL; + context_dead(s->context); + return; + } + + create_stream(s, tdev); +} + +static void lookup_device(struct pa_stream *s, const char *tdev) { + struct pa_tagstruct *t; + uint32_t tag; + assert(s); + + s->state = STREAM_LOOKING_UP; + + t = pa_tagstruct_new(NULL, 0); + assert(t); + + pa_tagstruct_putu32(t, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_LOOKUP_SINK : PA_COMMAND_LOOKUP_SOURCE); + pa_tagstruct_putu32(t, tag = s->context->ctag++); + pa_tagstruct_puts(t, tdev); + + pa_pstream_send_tagstruct(s->context->pstream, t); + pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, lookup_device_callback, s); +} + +struct pa_stream* pa_stream_new( + struct pa_context *c, + enum pa_stream_direction dir, + const char *dev, + const char *name, + const struct pa_sample_spec *ss, + const struct pa_buffer_attr *attr, + void (*complete) (struct pa_stream*s, int success, void *userdata), + void *userdata) { + + struct pa_stream *s; + + assert(c && name && ss && c->state == CONTEXT_READY); + + s = malloc(sizeof(struct pa_stream)); + assert(s); + s->context = c; + + s->read_callback = NULL; + s->read_userdata = NULL; + s->write_callback = NULL; + s->write_userdata = NULL; + s->die_callback = NULL; + s->die_userdata = NULL; + s->create_complete_callback = complete; + s->create_complete_userdata = NULL; + s->get_latency_callback = NULL; + s->get_latency_userdata = NULL; + + s->name = strdup(name); + s->state = STREAM_CREATING; + s->requested_bytes = 0; + s->channel = 0; + s->channel_valid = 0; + s->device_index = (uint32_t) -1; + s->direction = dir; + s->sample_spec = *ss; + if (attr) + s->buffer_attr = *attr; + else { + s->buffer_attr.maxlength = DEFAULT_MAXLENGTH; + s->buffer_attr.tlength = DEFAULT_TLENGTH; + s->buffer_attr.prebuf = DEFAULT_PREBUF; + s->buffer_attr.minreq = DEFAULT_MINREQ; + s->buffer_attr.fragsize = DEFAULT_FRAGSIZE; + } + + s->next = c->first_stream; + if (s->next) + s->next->previous = s; + s->previous = NULL; + c->first_stream = s; + + if (dev) + lookup_device(s, dev); + else + create_stream(s, (uint32_t) -1); + + return s; +} + +void pa_stream_free(struct pa_stream *s) { + assert(s && s->context); + + if (s->context->pdispatch) + pa_pdispatch_unregister_reply(s->context->pdispatch, s); + + free(s->name); + + if (s->channel_valid && s->context->state == CONTEXT_READY) { + struct pa_tagstruct *t = pa_tagstruct_new(NULL, 0); + assert(t); + + pa_tagstruct_putu32(t, PA_COMMAND_DELETE_PLAYBACK_STREAM); + pa_tagstruct_putu32(t, s->context->ctag++); + pa_tagstruct_putu32(t, s->channel); + pa_pstream_send_tagstruct(s->context->pstream, t); + } + + if (s->channel_valid) + pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, NULL); + + if (s->next) + s->next->previous = s->previous; + if (s->previous) + s->previous->next = s->next; + else + s->context->first_stream = s->next; + + free(s); +} + +void pa_stream_set_write_callback(struct pa_stream *s, void (*cb)(struct pa_stream *p, size_t length, void *userdata), void *userdata) { + assert(s && cb); + s->write_callback = cb; + s->write_userdata = userdata; +} + +void pa_stream_write(struct pa_stream *s, const void *data, size_t length) { + struct pa_memchunk chunk; + assert(s && s->context && data && length && s->state == STREAM_READY); + + chunk.memblock = pa_memblock_new(length); + assert(chunk.memblock && chunk.memblock->data); + memcpy(chunk.memblock->data, data, length); + chunk.index = 0; + chunk.length = length; + + pa_pstream_send_memblock(s->context->pstream, s->channel, 0, &chunk); + pa_memblock_unref(chunk.memblock); + + /*fprintf(stderr, "Sent %u bytes\n", length);*/ + + if (length < s->requested_bytes) + s->requested_bytes -= length; + else + s->requested_bytes = 0; +} + +size_t pa_stream_writable_size(struct pa_stream *s) { + assert(s && s->state == STREAM_READY); + return s->requested_bytes; +} + +void pa_stream_set_read_callback(struct pa_stream *s, void (*cb)(struct pa_stream *p, const void*data, size_t length, void *userdata), void *userdata) { + assert(s && cb); + s->read_callback = cb; + s->read_userdata = userdata; +} + +int pa_stream_is_dead(struct pa_stream *s) { + return s->state == STREAM_DEAD; +} + +int pa_stream_is_ready(struct pa_stream*s) { + return s->state == STREAM_READY; +} + +void pa_stream_set_die_callback(struct pa_stream *s, void (*cb)(struct pa_stream *s, void *userdata), void *userdata) { + assert(s); + s->die_callback = cb; + s->die_userdata = userdata; +} + +int pa_context_is_pending(struct pa_context *c) { + assert(c); + + if (c->state != CONTEXT_READY) + return 0; + + return pa_pstream_is_pending(c->pstream) || pa_pdispatch_is_pending(c->pdispatch); +} + +struct pa_context* pa_stream_get_context(struct pa_stream *p) { + assert(p); + return p->context; +} + +static void set_dispatch_callbacks(struct pa_context *c); + +static void pdispatch_drain_callback(struct pa_pdispatch*pd, void *userdata) { + set_dispatch_callbacks(userdata); +} + +static void pstream_drain_callback(struct pa_pstream *s, void *userdata) { + set_dispatch_callbacks(userdata); +} + +static void set_dispatch_callbacks(struct pa_context *c) { + assert(c && c->state == CONTEXT_READY); + + pa_pstream_set_drain_callback(c->pstream, NULL, NULL); + pa_pdispatch_set_drain_callback(c->pdispatch, NULL, NULL); + + if (pa_pdispatch_is_pending(c->pdispatch)) { + pa_pdispatch_set_drain_callback(c->pdispatch, pdispatch_drain_callback, c); + return; + } + + if (pa_pstream_is_pending(c->pstream)) { + pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c); + return; + } + + assert(c->drain_complete_callback); + c->drain_complete_callback(c, c->drain_complete_userdata); +} + +int pa_context_drain( + struct pa_context *c, + void (*complete) (struct pa_context*c, void *userdata), + void *userdata) { + + assert(c && c->state == CONTEXT_READY); + + if (complete == NULL) { + c->drain_complete_callback = NULL; + pa_pstream_set_drain_callback(c->pstream, NULL, NULL); + pa_pdispatch_set_drain_callback(c->pdispatch, NULL, NULL); + return 0; + } + + if (!pa_context_is_pending(c)) + return -1; + + c->drain_complete_callback = complete; + c->drain_complete_userdata = userdata; + + set_dispatch_callbacks(c); + + return 0; +} + +static void stream_drain_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { + struct pa_stream *s = userdata; + assert(pd && s); + + if (command != PA_COMMAND_REPLY) { + if (handle_error(s->context, command, t) < 0) { + context_dead(s->context); + return; + } + + stream_dead(s); + return; + } + + if (s->state != STREAM_READY) + return; + + if (!pa_tagstruct_eof(t)) { + s->context->error = PA_ERROR_PROTOCOL; + context_dead(s->context); + return; + } + + if (s->drain_complete_callback) { + void (*temp) (struct pa_stream*s, void *userdata) = s->drain_complete_callback; + s->drain_complete_callback = NULL; + temp(s, s->drain_complete_userdata); + } +} + + +void pa_stream_drain(struct pa_stream *s, void (*complete) (struct pa_stream*s, void *userdata), void *userdata) { + struct pa_tagstruct *t; + uint32_t tag; + assert(s && s->state == STREAM_READY); + + if (!complete) { + s->drain_complete_callback = NULL; + return; + } + + s->drain_complete_callback = complete; + s->drain_complete_userdata = userdata; + + t = pa_tagstruct_new(NULL, 0); + assert(t); + + pa_tagstruct_putu32(t, PA_COMMAND_DRAIN_PLAYBACK_STREAM); + pa_tagstruct_putu32(t, tag = s->context->ctag++); + pa_tagstruct_putu32(t, s->channel); + pa_pstream_send_tagstruct(s->context->pstream, t); + pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_drain_callback, s); +} + +void pa_context_exit(struct pa_context *c) { + struct pa_tagstruct *t; + t = pa_tagstruct_new(NULL, 0); + assert(t); + pa_tagstruct_putu32(t, PA_COMMAND_EXIT); + pa_tagstruct_putu32(t, c->ctag++); + pa_pstream_send_tagstruct(c->pstream, t); +} + +static void context_stat_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { + struct pa_context *c = userdata; + uint32_t total, count; + assert(pd && c); + + if (command != PA_COMMAND_REPLY) { + if (handle_error(c, command, t) < 0) { + context_dead(c); + return; + } + + if (c->stat_callback) + c->stat_callback(c, (uint32_t) -1, (uint32_t) -1, c->stat_userdata); + return; + } + + if (pa_tagstruct_getu32(t, &count) < 0 || + pa_tagstruct_getu32(t, &total) < 0 || + !pa_tagstruct_eof(t)) { + c->error = PA_ERROR_PROTOCOL; + context_dead(c); + return; + } + + if (c->stat_callback) + c->stat_callback(c, count, total, c->stat_userdata); +} + +void pa_context_stat(struct pa_context *c, void (*cb)(struct pa_context *c, uint32_t count, uint32_t total, void *userdata), void *userdata) { + uint32_t tag; + struct pa_tagstruct *t; + + c->stat_callback = cb; + c->stat_userdata = userdata; + + if (cb == NULL) + return; + + t = pa_tagstruct_new(NULL, 0); + assert(t); + pa_tagstruct_putu32(t, PA_COMMAND_STAT); + pa_tagstruct_putu32(t, tag = c->ctag++); + pa_pstream_send_tagstruct(c->pstream, t); + pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, context_stat_callback, c); +} + +static void stream_get_latency_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) { + struct pa_stream *s = userdata; + uint32_t latency; + assert(pd && s); + + if (command != PA_COMMAND_REPLY) { + if (handle_error(s->context, command, t) < 0) { + context_dead(s->context); + return; + } + + if (s->get_latency_callback) + s->get_latency_callback(s, (uint32_t) -1, s->get_latency_userdata); + return; + } + + if (pa_tagstruct_getu32(t, &latency) < 0 || + !pa_tagstruct_eof(t)) { + s->context->error = PA_ERROR_PROTOCOL; + context_dead(s->context); + return; + } + + if (s->get_latency_callback) + s->get_latency_callback(s, latency, s->get_latency_userdata); +} + +void pa_stream_get_latency(struct pa_stream *p, void (*cb)(struct pa_stream *p, uint32_t latency, void *userdata), void *userdata) { + uint32_t tag; + struct pa_tagstruct *t; + + p->get_latency_callback = cb; + p->get_latency_userdata = userdata; + + if (cb == NULL) + return; + + t = pa_tagstruct_new(NULL, 0); + assert(t); + pa_tagstruct_putu32(t, PA_COMMAND_GET_PLAYBACK_LATENCY); + pa_tagstruct_putu32(t, tag = p->context->ctag++); + pa_tagstruct_putu32(t, p->channel); + pa_pstream_send_tagstruct(p->context->pstream, t); + pa_pdispatch_register_reply(p->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, p); +} diff --git a/src/polyplib.h b/src/polyplib.h new file mode 100644 index 00000000..00849445 --- /dev/null +++ b/src/polyplib.h @@ -0,0 +1,73 @@ +#ifndef foopolyphfoo +#define foopolyphfoo + +#include + +#include "sample.h" +#include "polypdef.h" +#include "mainloop-api.h" + +struct pa_context; + +struct pa_context *pa_context_new(struct pa_mainloop_api *mainloop, const char *name); + +int pa_context_connect( + struct pa_context *c, + const char *server, + void (*complete) (struct pa_context*c, int success, void *userdata), + void *userdata); + +int pa_context_drain( + struct pa_context *c, + void (*complete) (struct pa_context*c, void *userdata), + void *userdata); + +void pa_context_free(struct pa_context *c); + +void pa_context_set_die_callback(struct pa_context *c, void (*cb)(struct pa_context *c, void *userdata), void *userdata); + +int pa_context_is_dead(struct pa_context *c); +int pa_context_is_ready(struct pa_context *c); +int pa_context_errno(struct pa_context *c); + +int pa_context_is_pending(struct pa_context *c); + +void pa_context_exit(struct pa_context *c); +void pa_context_stat(struct pa_context *c, void (*cb)(struct pa_context *c, uint32_t count, uint32_t total, void *userdata), void *userdata); + +struct pa_stream; + +struct pa_stream* pa_stream_new( + struct pa_context *c, + enum pa_stream_direction dir, + const char *dev, + const char *name, + const struct pa_sample_spec *ss, + const struct pa_buffer_attr *attr, + void (*complete) (struct pa_stream*s, int success, void *userdata), + void *userdata); + +void pa_stream_free(struct pa_stream *p); + +void pa_stream_drain( + struct pa_stream *s, + void (*complete) (struct pa_stream*s, void *userdata), + void *userdata); + + +void pa_stream_set_die_callback(struct pa_stream *s, void (*cb)(struct pa_stream *s, void *userdata), void *userdata); + +void pa_stream_set_write_callback(struct pa_stream *p, void (*cb)(struct pa_stream *p, size_t length, void *userdata), void *userdata); +void pa_stream_write(struct pa_stream *p, const void *data, size_t length); +size_t pa_stream_writable_size(struct pa_stream *p); + +void pa_stream_set_read_callback(struct pa_stream *p, void (*cb)(struct pa_stream *p, const void*data, size_t length, void *userdata), void *userdata); + +int pa_stream_is_dead(struct pa_stream *p); +int pa_stream_is_ready(struct pa_stream*p); + +void pa_stream_get_latency(struct pa_stream *p, void (*cb)(struct pa_stream *p, uint32_t latency, void *userdata), void *userdata); + +struct pa_context* pa_stream_get_context(struct pa_stream *p); + +#endif diff --git a/src/protocol-native-spec.h b/src/protocol-native-spec.h deleted file mode 100644 index e1db6f64..00000000 --- a/src/protocol-native-spec.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef fooprotocolnativespech -#define fooprotocolnativespech - -enum { - PA_COMMAND_ERROR, - PA_COMMAND_TIMEOUT, /* pseudo command */ - PA_COMMAND_REPLY, - PA_COMMAND_CREATE_PLAYBACK_STREAM, - PA_COMMAND_DELETE_PLAYBACK_STREAM, - PA_COMMAND_CREATE_RECORD_STREAM, - PA_COMMAND_DELETE_RECORD_STREAM, - PA_COMMAND_EXIT, - PA_COMMAND_REQUEST, - PA_COMMAND_AUTH, - PA_COMMAND_SET_NAME, - PA_COMMAND_LOOKUP_SINK, - PA_COMMAND_LOOKUP_SOURCE, - PA_COMMAND_DRAIN_PLAYBACK_STREAM, - PA_COMMAND_PLAYBACK_STREAM_KILLED, - PA_COMMAND_RECORD_STREAM_KILLED, - PA_COMMAND_STAT, - PA_COMMAND_GET_PLAYBACK_LATENCY, - PA_COMMAND_MAX -}; - -enum { - PA_ERROR_OK, - PA_ERROR_ACCESS, - PA_ERROR_COMMAND, - PA_ERROR_INVALID, - PA_ERROR_EXIST, - PA_ERROR_NOENTITY, - PA_ERROR_CONNECTIONREFUSED, - PA_ERROR_PROTOCOL, - PA_ERROR_TIMEOUT, - PA_ERROR_AUTHKEY, - PA_ERROR_INTERNAL, - PA_ERROR_CONNECTIONTERMINATED, - PA_ERROR_KILLED, - PA_ERROR_INVALIDSERVER, - PA_ERROR_MAX -}; - -#define PA_NATIVE_COOKIE_LENGTH 256 -#define PA_NATIVE_COOKIE_FILE ".polypaudio-cookie" - -#endif diff --git a/src/simple.c b/src/simple.c deleted file mode 100644 index 5f86c5bf..00000000 --- a/src/simple.c +++ /dev/null @@ -1,233 +0,0 @@ -#include -#include -#include -#include - -#include "simple.h" -#include "polyp.h" -#include "mainloop.h" -#include "polyp-error.h" - -struct pa_simple { - struct pa_mainloop *mainloop; - struct pa_context *context; - struct pa_stream *stream; - enum pa_stream_direction direction; - - int dead, drained; - - void *read_data; - size_t read_index, read_length; -}; - -static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata); - -static int check_error(struct pa_simple *p, int *perror) { - assert(p); - - if (pa_context_is_dead(p->context) || (p->stream && pa_stream_is_dead(p->stream))) { - if (perror) - *perror = pa_context_errno(p->context); - return -1; - } - - return 0; -} - -static int iterate(struct pa_simple *p, int block, int *perror) { - assert(p && p->context && p->mainloop); - - if (check_error(p, perror) < 0) - return -1; - - if (!block && !pa_context_is_pending(p->context)) - return 0; - - do { - if (pa_mainloop_iterate(p->mainloop, 1, NULL) < 0) { - if (perror) - *perror = PA_ERROR_INTERNAL; - return -1; - } - - if (check_error(p, perror) < 0) - return -1; - - } while (pa_context_is_pending(p->context)); - - return 0; -} - -struct pa_simple* pa_simple_new( - const char *server, - const char *name, - enum pa_stream_direction dir, - const char *dev, - const char *stream_name, - const struct pa_sample_spec *ss, - const struct pa_buffer_attr *attr, - int *perror) { - - struct pa_simple *p; - int error = PA_ERROR_INTERNAL; - assert(ss); - - p = malloc(sizeof(struct pa_simple)); - assert(p); - p->context = NULL; - p->stream = NULL; - p->mainloop = pa_mainloop_new(); - assert(p->mainloop); - p->dead = 0; - p->direction = dir; - p->read_data = NULL; - p->read_index = p->read_length = 0; - - if (!(p->context = pa_context_new(pa_mainloop_get_api(p->mainloop), name))) - goto fail; - - if (pa_context_connect(p->context, server, NULL, NULL) < 0) { - error = pa_context_errno(p->context); - goto fail; - } - - /* Wait until the context is ready */ - while (!pa_context_is_ready(p->context)) { - if (iterate(p, 1, &error) < 0) - goto fail; - } - - if (!(p->stream = pa_stream_new(p->context, dir, dev, stream_name, ss, attr, NULL, NULL))) - goto fail; - - /* Wait until the stream is ready */ - while (!pa_stream_is_ready(p->stream)) { - if (iterate(p, 1, &error) < 0) - goto fail; - } - - pa_stream_set_read_callback(p->stream, read_callback, p); - - return p; - -fail: - if (perror) - *perror = error; - pa_simple_free(p); - return NULL; -} - -void pa_simple_free(struct pa_simple *s) { - assert(s); - - free(s->read_data); - - if (s->stream) - pa_stream_free(s->stream); - - if (s->context) - pa_context_free(s->context); - - if (s->mainloop) - pa_mainloop_free(s->mainloop); - - free(s); -} - -int pa_simple_write(struct pa_simple *p, const void*data, size_t length, int *perror) { - assert(p && data && p->direction == PA_STREAM_PLAYBACK); - - while (length > 0) { - size_t l; - - while (!(l = pa_stream_writable_size(p->stream))) - if (iterate(p, 1, perror) < 0) - return -1; - - if (l > length) - l = length; - - pa_stream_write(p->stream, data, l); - data += l; - length -= l; - } - - /* Make sure that no data is pending for write */ - if (iterate(p, 0, perror) < 0) - return -1; - - return 0; -} - -static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) { - struct pa_simple *p = userdata; - assert(s && data && length && p); - - if (p->read_data) { - fprintf(stderr, __FILE__": Buffer overflow, dropping incoming memory blocks.\n"); - free(p->read_data); - } - - p->read_data = malloc(p->read_length = length); - assert(p->read_data); - memcpy(p->read_data, data, length); - p->read_index = 0; -} - -int pa_simple_read(struct pa_simple *p, void*data, size_t length, int *perror) { - assert(p && data && p->direction == PA_STREAM_RECORD); - - while (length > 0) { - if (p->read_data) { - size_t l = length; - - if (p->read_length <= l) - l = p->read_length; - - memcpy(data, p->read_data+p->read_index, l); - - data += l; - length -= l; - - p->read_index += l; - p->read_length -= l; - - if (!p->read_length) { - free(p->read_data); - p->read_data = NULL; - p->read_index = 0; - } - - if (!length) - return 0; - - assert(!p->read_data); - } - - if (iterate(p, 1, perror) < 0) - return -1; - } - - return 0; -} - -static void drain_complete(struct pa_stream *s, void *userdata) { - struct pa_simple *p = userdata; - assert(s && p); - p->drained = 1; -} - -int pa_simple_drain(struct pa_simple *p, int *perror) { - assert(p && p->direction == PA_STREAM_PLAYBACK); - p->drained = 0; - pa_stream_drain(p->stream, drain_complete, p); - - while (!p->drained) { - if (iterate(p, 1, perror) < 0) { - pa_stream_drain(p->stream, NULL, NULL); - return -1; - } - } - - return 0; -} diff --git a/src/simple.h b/src/simple.h deleted file mode 100644 index f5f872ee..00000000 --- a/src/simple.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef foosimplehfoo -#define foosimplehfoo - -#include - -#include "sample.h" -#include "polypdef.h" - -struct pa_simple; - -struct pa_simple* pa_simple_new( - const char *server, - const char *name, - enum pa_stream_direction dir, - const char *dev, - const char *stream_name, - const struct pa_sample_spec *ss, - const struct pa_buffer_attr *attr, - int *error); - -void pa_simple_free(struct pa_simple *s); - -int pa_simple_write(struct pa_simple *s, const void*data, size_t length, int *error); -int pa_simple_drain(struct pa_simple *s, int *error); - -int pa_simple_read(struct pa_simple *s, void*data, size_t length, int *error); - -#endif diff --git a/src/sink-input.c b/src/sink-input.c new file mode 100644 index 00000000..54778a81 --- /dev/null +++ b/src/sink-input.c @@ -0,0 +1,138 @@ +#include +#include +#include +#include + +#include "sinkinput.h" +#include "sample-util.h" + +#define CONVERT_BUFFER_LENGTH 4096 + +struct pa_sink_input* pa_sink_input_new(struct pa_sink *s, const char *name, const struct pa_sample_spec *spec) { + struct pa_sink_input *i; + struct pa_resampler *resampler = NULL; + int r; + char st[256]; + assert(s && spec); + + if (!pa_sample_spec_equal(spec, &s->sample_spec)) + if (!(resampler = pa_resampler_new(spec, &s->sample_spec))) + return NULL; + + i = malloc(sizeof(struct pa_sink_input)); + assert(i); + i->name = name ? strdup(name) : NULL; + i->client = NULL; + i->owner = NULL; + i->sink = s; + i->sample_spec = *spec; + + i->peek = NULL; + i->drop = NULL; + i->kill = NULL; + i->get_latency = NULL; + i->userdata = NULL; + + i->volume = PA_VOLUME_NORM; + + i->resampled_chunk.memblock = NULL; + i->resampled_chunk.index = i->resampled_chunk.length = 0; + i->resampler = resampler; + + assert(s->core); + r = pa_idxset_put(s->core->sink_inputs, i, &i->index); + assert(r == 0 && i->index != PA_IDXSET_INVALID); + r = pa_idxset_put(s->inputs, i, NULL); + assert(r == 0); + + pa_sample_snprint(st, sizeof(st), spec); + fprintf(stderr, "sink-input: created %u \"%s\" on %u with sample spec \"%s\"\n", i->index, i->name, s->index, st); + + return i; +} + +void pa_sink_input_free(struct pa_sink_input* i) { + assert(i); + + assert(i->sink && i->sink->core); + pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL); + pa_idxset_remove_by_data(i->sink->inputs, i, NULL); + + if (i->resampled_chunk.memblock) + pa_memblock_unref(i->resampled_chunk.memblock); + if (i->resampler) + pa_resampler_free(i->resampler); + + free(i->name); + free(i); +} + +void pa_sink_input_kill(struct pa_sink_input*i) { + assert(i); + + if (i->kill) + i->kill(i); +} + +uint32_t pa_sink_input_get_latency(struct pa_sink_input *i) { + uint32_t l = 0; + + assert(i); + if (i->get_latency) + l += i->get_latency(i); + + assert(i->sink); + l += pa_sink_get_latency(i->sink); + + return l; +} + +int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) { + assert(i && chunk && i->peek && i->drop); + + if (!i->resampler) + return i->peek(i, chunk); + + if (!i->resampled_chunk.memblock) { + struct pa_memchunk tchunk; + size_t l; + int ret; + + if ((ret = i->peek(i, &tchunk)) < 0) + return ret; + + l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH); + if (tchunk.length > l) + tchunk.length = l; + + i->drop(i, tchunk.length); + + pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk); + pa_memblock_unref(tchunk.memblock); + } + + assert(i->resampled_chunk.memblock && i->resampled_chunk.length); + *chunk = i->resampled_chunk; + pa_memblock_ref(i->resampled_chunk.memblock); + return 0; +} + +void pa_sink_input_drop(struct pa_sink_input *i, size_t length) { + assert(i && length); + + if (!i->resampler) { + i->drop(i, length); + return; + } + + assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length); + + i->resampled_chunk.index += length; + i->resampled_chunk.length -= length; + + if (!i->resampled_chunk.length) { + pa_memblock_unref(i->resampled_chunk.memblock); + i->resampled_chunk.memblock = NULL; + i->resampled_chunk.index = i->resampled_chunk.length = 0; + } +} diff --git a/src/sink-input.h b/src/sink-input.h new file mode 100644 index 00000000..02a2a117 --- /dev/null +++ b/src/sink-input.h @@ -0,0 +1,46 @@ +#ifndef foosinkinputhfoo +#define foosinkinputhfoo + +#include + +#include "sink.h" +#include "sample.h" +#include "memblockq.h" +#include "resampler.h" +#include "module.h" +#include "client.h" + +struct pa_sink_input { + uint32_t index; + + char *name; + struct pa_module *owner; + struct pa_client *client; + struct pa_sink *sink; + struct pa_sample_spec sample_spec; + uint32_t volume; + + int (*peek) (struct pa_sink_input *i, struct pa_memchunk *chunk); + void (*drop) (struct pa_sink_input *i, size_t length); + void (*kill) (struct pa_sink_input *i); + uint32_t (*get_latency) (struct pa_sink_input *i); + + void *userdata; + + struct pa_memchunk resampled_chunk; + struct pa_resampler *resampler; +}; + +struct pa_sink_input* pa_sink_input_new(struct pa_sink *s, const char *name, const struct pa_sample_spec *spec); +void pa_sink_input_free(struct pa_sink_input* i); + +/* Code that didn't create the input stream should call this function to + * request destruction of it */ +void pa_sink_input_kill(struct pa_sink_input *i); + +uint32_t pa_sink_input_get_latency(struct pa_sink_input *i); + +int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk); +void pa_sink_input_drop(struct pa_sink_input *i, size_t length); + +#endif diff --git a/src/sinkinput.c b/src/sinkinput.c deleted file mode 100644 index 54778a81..00000000 --- a/src/sinkinput.c +++ /dev/null @@ -1,138 +0,0 @@ -#include -#include -#include -#include - -#include "sinkinput.h" -#include "sample-util.h" - -#define CONVERT_BUFFER_LENGTH 4096 - -struct pa_sink_input* pa_sink_input_new(struct pa_sink *s, const char *name, const struct pa_sample_spec *spec) { - struct pa_sink_input *i; - struct pa_resampler *resampler = NULL; - int r; - char st[256]; - assert(s && spec); - - if (!pa_sample_spec_equal(spec, &s->sample_spec)) - if (!(resampler = pa_resampler_new(spec, &s->sample_spec))) - return NULL; - - i = malloc(sizeof(struct pa_sink_input)); - assert(i); - i->name = name ? strdup(name) : NULL; - i->client = NULL; - i->owner = NULL; - i->sink = s; - i->sample_spec = *spec; - - i->peek = NULL; - i->drop = NULL; - i->kill = NULL; - i->get_latency = NULL; - i->userdata = NULL; - - i->volume = PA_VOLUME_NORM; - - i->resampled_chunk.memblock = NULL; - i->resampled_chunk.index = i->resampled_chunk.length = 0; - i->resampler = resampler; - - assert(s->core); - r = pa_idxset_put(s->core->sink_inputs, i, &i->index); - assert(r == 0 && i->index != PA_IDXSET_INVALID); - r = pa_idxset_put(s->inputs, i, NULL); - assert(r == 0); - - pa_sample_snprint(st, sizeof(st), spec); - fprintf(stderr, "sink-input: created %u \"%s\" on %u with sample spec \"%s\"\n", i->index, i->name, s->index, st); - - return i; -} - -void pa_sink_input_free(struct pa_sink_input* i) { - assert(i); - - assert(i->sink && i->sink->core); - pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL); - pa_idxset_remove_by_data(i->sink->inputs, i, NULL); - - if (i->resampled_chunk.memblock) - pa_memblock_unref(i->resampled_chunk.memblock); - if (i->resampler) - pa_resampler_free(i->resampler); - - free(i->name); - free(i); -} - -void pa_sink_input_kill(struct pa_sink_input*i) { - assert(i); - - if (i->kill) - i->kill(i); -} - -uint32_t pa_sink_input_get_latency(struct pa_sink_input *i) { - uint32_t l = 0; - - assert(i); - if (i->get_latency) - l += i->get_latency(i); - - assert(i->sink); - l += pa_sink_get_latency(i->sink); - - return l; -} - -int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) { - assert(i && chunk && i->peek && i->drop); - - if (!i->resampler) - return i->peek(i, chunk); - - if (!i->resampled_chunk.memblock) { - struct pa_memchunk tchunk; - size_t l; - int ret; - - if ((ret = i->peek(i, &tchunk)) < 0) - return ret; - - l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH); - if (tchunk.length > l) - tchunk.length = l; - - i->drop(i, tchunk.length); - - pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk); - pa_memblock_unref(tchunk.memblock); - } - - assert(i->resampled_chunk.memblock && i->resampled_chunk.length); - *chunk = i->resampled_chunk; - pa_memblock_ref(i->resampled_chunk.memblock); - return 0; -} - -void pa_sink_input_drop(struct pa_sink_input *i, size_t length) { - assert(i && length); - - if (!i->resampler) { - i->drop(i, length); - return; - } - - assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length); - - i->resampled_chunk.index += length; - i->resampled_chunk.length -= length; - - if (!i->resampled_chunk.length) { - pa_memblock_unref(i->resampled_chunk.memblock); - i->resampled_chunk.memblock = NULL; - i->resampled_chunk.index = i->resampled_chunk.length = 0; - } -} diff --git a/src/sinkinput.h b/src/sinkinput.h deleted file mode 100644 index 02a2a117..00000000 --- a/src/sinkinput.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef foosinkinputhfoo -#define foosinkinputhfoo - -#include - -#include "sink.h" -#include "sample.h" -#include "memblockq.h" -#include "resampler.h" -#include "module.h" -#include "client.h" - -struct pa_sink_input { - uint32_t index; - - char *name; - struct pa_module *owner; - struct pa_client *client; - struct pa_sink *sink; - struct pa_sample_spec sample_spec; - uint32_t volume; - - int (*peek) (struct pa_sink_input *i, struct pa_memchunk *chunk); - void (*drop) (struct pa_sink_input *i, size_t length); - void (*kill) (struct pa_sink_input *i); - uint32_t (*get_latency) (struct pa_sink_input *i); - - void *userdata; - - struct pa_memchunk resampled_chunk; - struct pa_resampler *resampler; -}; - -struct pa_sink_input* pa_sink_input_new(struct pa_sink *s, const char *name, const struct pa_sample_spec *spec); -void pa_sink_input_free(struct pa_sink_input* i); - -/* Code that didn't create the input stream should call this function to - * request destruction of it */ -void pa_sink_input_kill(struct pa_sink_input *i); - -uint32_t pa_sink_input_get_latency(struct pa_sink_input *i); - -int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk); -void pa_sink_input_drop(struct pa_sink_input *i, size_t length); - -#endif diff --git a/src/source-output.c b/src/source-output.c new file mode 100644 index 00000000..25c661a9 --- /dev/null +++ b/src/source-output.c @@ -0,0 +1,76 @@ +#include +#include +#include + +#include "sourceoutput.h" + +struct pa_source_output* pa_source_output_new(struct pa_source *s, const char *name, const struct pa_sample_spec *spec) { + struct pa_source_output *o; + struct pa_resampler *resampler = NULL; + int r; + assert(s && spec); + + if (!pa_sample_spec_equal(&s->sample_spec, spec)) + if (!(resampler = pa_resampler_new(&s->sample_spec, spec))) + return NULL; + + o = malloc(sizeof(struct pa_source_output)); + assert(o); + o->name = name ? strdup(name) : NULL; + o->client = NULL; + o->owner = NULL; + o->source = s; + o->sample_spec = *spec; + + o->push = NULL; + o->kill = NULL; + o->userdata = NULL; + o->resampler = resampler; + + assert(s->core); + r = pa_idxset_put(s->core->source_outputs, o, &o->index); + assert(r == 0 && o->index != PA_IDXSET_INVALID); + r = pa_idxset_put(s->outputs, o, NULL); + assert(r == 0); + + return o; +} + +void pa_source_output_free(struct pa_source_output* o) { + assert(o); + + assert(o->source && o->source->core); + pa_idxset_remove_by_data(o->source->core->source_outputs, o, NULL); + pa_idxset_remove_by_data(o->source->outputs, o, NULL); + + if (o->resampler) + pa_resampler_free(o->resampler); + + free(o->name); + free(o); +} + +void pa_source_output_kill(struct pa_source_output*i) { + assert(i); + + if (i->kill) + i->kill(i); +} + +void pa_source_output_push(struct pa_source_output *o, const struct pa_memchunk *chunk) { + struct pa_memchunk rchunk; + assert(o && chunk && chunk->length && o->push); + + if (!o->resampler) { + o->push(o, chunk); + return; + } + + pa_resampler_run(o->resampler, chunk, &rchunk); + if (!rchunk.length) + return; + + assert(rchunk.memblock); + o->push(o, &rchunk); + pa_memblock_unref(rchunk.memblock); +} diff --git a/src/source-output.h b/src/source-output.h new file mode 100644 index 00000000..a8ea5a9f --- /dev/null +++ b/src/source-output.h @@ -0,0 +1,37 @@ +#ifndef foosourceoutputhfoo +#define foosourceoutputhfoo + +#include + +#include "source.h" +#include "sample.h" +#include "memblockq.h" +#include "resampler.h" +#include "module.h" +#include "client.h" + +struct pa_source_output { + uint32_t index; + + char *name; + struct pa_module *owner; + struct pa_client *client; + struct pa_source *source; + struct pa_sample_spec sample_spec; + + void (*push)(struct pa_source_output *o, const struct pa_memchunk *chunk); + void (*kill)(struct pa_source_output* o); + + struct pa_resampler* resampler; + + void *userdata; +}; + +struct pa_source_output* pa_source_output_new(struct pa_source *s, const char *name, const struct pa_sample_spec *spec); +void pa_source_output_free(struct pa_source_output* o); + +void pa_source_output_kill(struct pa_source_output*o); + +void pa_source_output_push(struct pa_source_output *o, const struct pa_memchunk *chunk); + +#endif diff --git a/src/sourceoutput.c b/src/sourceoutput.c deleted file mode 100644 index 25c661a9..00000000 --- a/src/sourceoutput.c +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include -#include - -#include "sourceoutput.h" - -struct pa_source_output* pa_source_output_new(struct pa_source *s, const char *name, const struct pa_sample_spec *spec) { - struct pa_source_output *o; - struct pa_resampler *resampler = NULL; - int r; - assert(s && spec); - - if (!pa_sample_spec_equal(&s->sample_spec, spec)) - if (!(resampler = pa_resampler_new(&s->sample_spec, spec))) - return NULL; - - o = malloc(sizeof(struct pa_source_output)); - assert(o); - o->name = name ? strdup(name) : NULL; - o->client = NULL; - o->owner = NULL; - o->source = s; - o->sample_spec = *spec; - - o->push = NULL; - o->kill = NULL; - o->userdata = NULL; - o->resampler = resampler; - - assert(s->core); - r = pa_idxset_put(s->core->source_outputs, o, &o->index); - assert(r == 0 && o->index != PA_IDXSET_INVALID); - r = pa_idxset_put(s->outputs, o, NULL); - assert(r == 0); - - return o; -} - -void pa_source_output_free(struct pa_source_output* o) { - assert(o); - - assert(o->source && o->source->core); - pa_idxset_remove_by_data(o->source->core->source_outputs, o, NULL); - pa_idxset_remove_by_data(o->source->outputs, o, NULL); - - if (o->resampler) - pa_resampler_free(o->resampler); - - free(o->name); - free(o); -} - -void pa_source_output_kill(struct pa_source_output*i) { - assert(i); - - if (i->kill) - i->kill(i); -} - -void pa_source_output_push(struct pa_source_output *o, const struct pa_memchunk *chunk) { - struct pa_memchunk rchunk; - assert(o && chunk && chunk->length && o->push); - - if (!o->resampler) { - o->push(o, chunk); - return; - } - - pa_resampler_run(o->resampler, chunk, &rchunk); - if (!rchunk.length) - return; - - assert(rchunk.memblock); - o->push(o, &rchunk); - pa_memblock_unref(rchunk.memblock); -} diff --git a/src/sourceoutput.h b/src/sourceoutput.h deleted file mode 100644 index a8ea5a9f..00000000 --- a/src/sourceoutput.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef foosourceoutputhfoo -#define foosourceoutputhfoo - -#include - -#include "source.h" -#include "sample.h" -#include "memblockq.h" -#include "resampler.h" -#include "module.h" -#include "client.h" - -struct pa_source_output { - uint32_t index; - - char *name; - struct pa_module *owner; - struct pa_client *client; - struct pa_source *source; - struct pa_sample_spec sample_spec; - - void (*push)(struct pa_source_output *o, const struct pa_memchunk *chunk); - void (*kill)(struct pa_source_output* o); - - struct pa_resampler* resampler; - - void *userdata; -}; - -struct pa_source_output* pa_source_output_new(struct pa_source *s, const char *name, const struct pa_sample_spec *spec); -void pa_source_output_free(struct pa_source_output* o); - -void pa_source_output_kill(struct pa_source_output*o); - -void pa_source_output_push(struct pa_source_output *o, const struct pa_memchunk *chunk); - -#endif -- cgit