return -1;
}
- pa_module_unload_request(c, m);
+ pa_module_unload_request(m);
return 0;
}
c->default_sample_spec.channels = 2;
c->auto_unload_event = NULL;
+ c->defer_unload_event = NULL;
c->subscription_defer_event = NULL;
c->subscription_event_queue = NULL;
struct pa_sample_spec default_sample_spec;
struct pa_time_event *auto_unload_event;
+ struct pa_defer_event *defer_unload_event;
struct pa_defer_event *subscription_defer_event;
struct pa_queue *subscription_event_queue;
if (!(u = m->userdata))
return;
- if (u->sink)
- pa_sink_free(u->sink);
+ if (u->sink) {
+ pa_sink_disconnect(u->sink);
+ pa_sink_unref(u->sink);
+ }
if (u->io_events)
pa_free_io_events(c->mainloop, u->io_events, u->n_io_events);
if (!(u = m->userdata))
return;
- if (u->source)
- pa_source_free(u->source);
+ if (u->source) {
+ pa_source_disconnect(u->source);
+ pa_source_unref(u->source);
+ }
if (u->io_events)
pa_free_io_events(c->mainloop, u->io_events, u->n_io_events);
struct pa_module *m = userdata;
assert(c && m);
- pa_module_unload_request(m->core, m);
+ pa_module_unload_request(m);
}
int pa__init(struct pa_core *c, struct pa_module*m) {
#include "namereg.h"
PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Makes one playback device out of many")
+PA_MODULE_DESCRIPTION("Combine multiple sinks to one")
PA_MODULE_VERSION(PACKAGE_VERSION)
PA_MODULE_USAGE("sink_name=<name for the sink> master=<master sink> slave=<slave sinks>")
assert(u && u->sink);
for (o = u->outputs; o; o = o->next) {
- o->sink_latency = pa_sink_get_latency(o->sink_input->sink);
+ o->sink_latency = o->sink_input->sink ? pa_sink_get_latency(o->sink_input->sink) : 0;
if (o->sink_latency > max)
max = o->sink_latency;
static void sink_input_kill_cb(struct pa_sink_input *i) {
struct output *o = i->userdata;
assert(i && o && o->sink_input);
+ pa_module_unload_request(o->userdata->module);
clear_up(o->userdata);
}
fail:
if (o) {
- if (o->sink_input)
- pa_sink_input_free(o->sink_input);
+ if (o->sink_input) {
+ pa_sink_input_disconnect(o->sink_input);
+ pa_sink_input_unref(o->sink_input);
+ }
if (o->memblockq)
pa_memblockq_free(o->memblockq);
PA_LLIST_REMOVE(struct output, o->userdata->outputs, o);
o->userdata->n_outputs--;
pa_memblockq_free(o->memblockq);
- pa_sink_input_free(o->sink_input);
+ pa_sink_input_disconnect(o->sink_input);
+ pa_sink_input_unref(o->sink_input);
pa_xfree(o);
}
u->master = NULL;
if (u->sink) {
- pa_sink_free(u->sink);
+ pa_sink_disconnect(u->sink);
+ pa_sink_unref(u->sink);
u->sink = NULL;
}
}
if (u->out_mmap && u->out_mmap != MAP_FAILED)
munmap(u->out_mmap, u->out_mmap_length);
- if (u->sink)
- pa_sink_free(u->sink);
+ if (u->sink) {
+ pa_sink_disconnect(u->sink);
+ pa_sink_unref(u->sink);
+ }
- if (u->source)
- pa_source_free(u->source);
+ if (u->source) {
+ pa_source_disconnect(u->source);
+ pa_source_unref(u->source);
+ }
if (u->io_event)
u->core->mainloop->io_free(u->io_event);
if (u->silence.memblock)
pa_memblock_unref(u->silence.memblock);
- if (u->sink)
- pa_sink_free(u->sink);
- if (u->source)
- pa_source_free(u->source);
+ if (u->sink) {
+ pa_sink_disconnect(u->sink);
+ pa_sink_unref(u->sink);
+ }
+
+ if (u->source) {
+ pa_source_disconnect(u->source);
+ pa_source_unref(u->source);
+ }
pa_iochannel_free(u->io);
pa_xfree(u);
if (u->memchunk.memblock)
pa_memblock_unref(u->memchunk.memblock);
- pa_sink_free(u->sink);
+ pa_sink_disconnect(u->sink);
+ pa_sink_unref(u->sink);
pa_iochannel_free(u->io);
u->core->mainloop->defer_free(u->defer_event);
if (u->chunk.memblock)
pa_memblock_unref(u->chunk.memblock);
- pa_source_free(u->source);
+ pa_source_disconnect(u->source);
+ pa_source_unref(u->source);
pa_iochannel_free(u->io);
assert(u->filename);
struct userdata {
struct pa_core *core;
+ struct pa_module *module;
struct pa_sink_input *sink_input;
struct pa_memblock *memblock;
size_t peek_index;
assert(i && i->userdata);
u = i->userdata;
- pa_sink_input_free(u->sink_input);
+ pa_sink_input_disconnect(u->sink_input);
+ pa_sink_input_unref(u->sink_input);
u->sink_input = NULL;
+
+ pa_module_unload_request(u->module);
}
static void calc_sine(float *f, size_t l, float freq) {
m->userdata = u = pa_xmalloc(sizeof(struct userdata));
u->core = c;
+ u->module = m;
u->sink_input = NULL;
u->memblock = NULL;
if (!u)
return;
- if (u->sink_input)
- pa_sink_input_free(u->sink_input);
+ if (u->sink_input) {
+ pa_sink_input_disconnect(u->sink_input);
+ pa_sink_input_unref(u->sink_input);
+ }
+
if (u->memblock)
pa_memblock_unref(u->memblock);
pa_xfree(u);
m->core = c;
m->n_used = -1;
m->auto_unload = 0;
+ m->unload_requested = 0;
assert(m->init);
if (m->init(c, m) < 0) {
pa_idxset_free(c->modules, free_callback, NULL);
c->modules = NULL;
- if (c->auto_unload_event)
+ if (c->auto_unload_event) {
c->mainloop->time_free(c->auto_unload_event);
- c->auto_unload_event = NULL;
+ c->auto_unload_event = NULL;
+ }
+
+ if (c->defer_unload_event) {
+ c->mainloop->defer_free(c->defer_unload_event);
+ c->defer_unload_event = NULL;
+ }
}
static int unused_callback(void *p, uint32_t index, int *del, void *userdata) {
pa_idxset_foreach(c->modules, unused_callback, &now);
}
-struct once_info {
- struct pa_core *core;
- uint32_t index;
-};
-
-static void module_unload_once_callback(struct pa_mainloop_api *m, void *userdata) {
- struct once_info *i = userdata;
- assert(i);
- pa_module_unload_by_index(i->core, i->index);
- pa_xfree(i);
+static int unload_callback(void *p, uint32_t index, int *del, void *userdata) {
+ struct pa_module *m = p;
+ assert(m);
+
+ if (m->unload_requested) {
+ pa_module_free(m);
+ *del = 1;
+ }
+
+ return 0;
}
-void pa_module_unload_request(struct pa_core *c, struct pa_module *m) {
- struct once_info *i;
- assert(c && m);
+static void defer_cb(struct pa_mainloop_api*api, struct pa_defer_event *e, void *userdata) {
+ struct pa_core *core = userdata;
+ api->defer_enable(e, 0);
+
+ if (!core->modules)
+ return;
+
+ pa_idxset_foreach(core->modules, unload_callback, NULL);
+
+}
+
+void pa_module_unload_request(struct pa_module *m) {
+ assert(m);
+
+ m->unload_requested = 1;
+
+ if (!m->core->defer_unload_event)
+ m->core->defer_unload_event = m->core->mainloop->defer_new(m->core->mainloop, defer_cb, m->core);
- i = pa_xmalloc(sizeof(struct once_info));
- i->core = c;
- i->index = m->index;
- pa_mainloop_api_once(c->mainloop, module_unload_once_callback, i);
+ m->core->mainloop->defer_enable(m->core->defer_unload_event, 1);
}
void pa_module_set_used(struct pa_module*m, int used) {
int n_used;
int auto_unload;
time_t last_used_time;
+
+ int unload_requested;
};
struct pa_module* pa_module_load(struct pa_core *c, const char *name, const char*argument);
-void pa_module_unload(struct pa_core *c, struct pa_module *m);
-void pa_module_unload_by_index(struct pa_core *c, uint32_t index);
+/* void pa_module_unload(struct pa_core *c, struct pa_module *m); */
+/* void pa_module_unload_by_index(struct pa_core *c, uint32_t index); */
void pa_module_unload_all(struct pa_core *c);
void pa_module_unload_unused(struct pa_core *c);
-void pa_module_unload_request(struct pa_core *c, struct pa_module *m);
+void pa_module_unload_request(struct pa_module *m);
void pa_module_set_used(struct pa_module*m, int used);
assert(i && i->userdata);
c = i->userdata;
+ pa_sink_input_disconnect(i);
+ pa_sink_input_unref(i);
+
pa_memblock_unref(c->memblock);
pa_xfree(c);
- pa_sink_input_free(i);
+
}
static int sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
pa_client_free(c->client);
- if (c->sink_input)
- pa_sink_input_free(c->sink_input);
- if (c->source_output)
- pa_source_output_free(c->source_output);
+ if (c->sink_input) {
+ pa_sink_input_disconnect(c->sink_input);
+ pa_sink_input_unref(c->sink_input);
+ }
+
+ if (c->source_output) {
+ pa_source_output_disconnect(c->source_output);
+ pa_source_output_unref(c->source_output);
+ }
+
if (c->input_memblockq)
pa_memblockq_free(c->input_memblockq);
if (c->output_memblockq)
assert(r && r->connection);
pa_idxset_remove_by_data(r->connection->record_streams, r, NULL);
- pa_source_output_free(r->source_output);
+ pa_source_output_disconnect(r->source_output);
+ pa_source_output_unref(r->source_output);
pa_memblockq_free(r->memblockq);
pa_xfree(r);
}
pa_pstream_send_error(p->connection->pstream, p->drain_tag, PA_ERROR_NOENTITY);
pa_idxset_remove_by_data(p->connection->output_streams, p, NULL);
- pa_sink_input_free(p->sink_input);
+ pa_sink_input_disconnect(p->sink_input);
+ pa_sink_input_unref(p->sink_input);
pa_memblockq_free(p->memblockq);
pa_xfree(p);
}
if (c->playback.current_memblock)
pa_memblock_unref(c->playback.current_memblock);
- if (c->sink_input)
- pa_sink_input_free(c->sink_input);
- if (c->source_output)
- pa_source_output_free(c->source_output);
+ if (c->sink_input) {
+ pa_sink_input_disconnect(c->sink_input);
+ pa_sink_input_unref(c->sink_input);
+ }
+ if (c->source_output) {
+ pa_source_output_disconnect(c->source_output);
+ pa_source_output_unref(c->source_output);
+ }
if (c->client)
pa_client_free(c->client);
if (c->io)
struct pa_resampler *resampler = NULL;
int r;
char st[256];
- assert(s && spec);
+ assert(s && spec && s->state == PA_SINK_RUNNING);
if (pa_idxset_ncontents(s->inputs) >= PA_MAX_INPUTS_PER_SINK) {
pa_log(__FILE__": Failed to create sink input: too many inputs per sink.\n");
return NULL;
i = pa_xmalloc(sizeof(struct pa_sink_input));
+ i->ref = 1;
+ i->state = PA_SINK_INPUT_RUNNING;
i->name = pa_xstrdup(name);
i->client = NULL;
i->owner = NULL;
return i;
}
-void pa_sink_input_free(struct pa_sink_input* i) {
- assert(i);
+void pa_sink_input_disconnect(struct pa_sink_input *i) {
+ assert(i && i->state == PA_SINK_INPUT_RUNNING && i->sink && i->sink->core);
- 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);
+ pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
+ i->sink = NULL;
+
+ i->peek = NULL;
+ i->drop = NULL;
+ i->kill = NULL;
+ i->get_latency = NULL;
+
+ i->state = PA_SINK_INPUT_DISCONNECTED;
+}
+
+static void sink_input_free(struct pa_sink_input* i) {
+ assert(i);
+
+ if (i->state != PA_SINK_INPUT_DISCONNECTED)
+ pa_sink_input_disconnect(i);
+
if (i->resampled_chunk.memblock)
pa_memblock_unref(i->resampled_chunk.memblock);
if (i->resampler)
pa_resampler_free(i->resampler);
- pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
-
pa_xfree(i->name);
pa_xfree(i);
}
+void pa_sink_input_unref(struct pa_sink_input *i) {
+ assert(i && i->ref >= 1);
+
+ if (!(--i->ref))
+ sink_input_free(i);
+}
+
+struct pa_sink_input* pa_sink_input_ref(struct pa_sink_input *i) {
+ assert(i && i->ref >= 1);
+ i->ref++;
+ return i;
+}
+
void pa_sink_input_kill(struct pa_sink_input*i) {
- assert(i);
+ assert(i && i->ref >= 1);
if (i->kill)
i->kill(i);
pa_usec_t pa_sink_input_get_latency(struct pa_sink_input *i) {
pa_usec_t r = 0;
- assert(i);
+ assert(i && i->ref >= 1);
if (i->get_latency)
r += i->get_latency(i);
}
int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
- assert(i && chunk && i->peek && i->drop);
+ int ret = 0;
+ assert(i && chunk && i->ref >= 1);
+
+ if (!i->peek || !i->drop)
+ return -1;
if (i->corked)
return -1;
if (!i->resampler)
return i->peek(i, chunk);
+ pa_sink_input_ref(i);
+
while (!i->resampled_chunk.memblock) {
struct pa_memchunk tchunk;
size_t l;
- int ret;
if ((ret = i->peek(i, &tchunk)) < 0)
- return ret;
+ goto finish;
assert(tchunk.length);
assert(i->resampled_chunk.memblock && i->resampled_chunk.length);
*chunk = i->resampled_chunk;
pa_memblock_ref(i->resampled_chunk.memblock);
- return 0;
+
+ ret = 0;
+
+finish:
+
+ pa_sink_input_unref(i);
+
+ return ret;
}
void pa_sink_input_drop(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length) {
- assert(i && length);
+ assert(i && length && i->ref >= 1);
if (!i->resampler) {
- i->drop(i, chunk, length);
+ if (i->drop)
+ i->drop(i, chunk, length);
return;
}
}
void pa_sink_input_set_volume(struct pa_sink_input *i, pa_volume_t volume) {
- assert(i && i->sink && i->sink->core);
+ assert(i && i->sink && i->sink->core && i->ref >= 1);
if (i->volume != volume) {
i->volume = volume;
void pa_sink_input_cork(struct pa_sink_input *i, int b) {
int n;
- assert(i);
+ assert(i && i->ref >= 1);
+
n = i->corked && !b;
i->corked = b;
}
void pa_sink_input_set_rate(struct pa_sink_input *i, uint32_t rate) {
- assert(i && i->resampler);
+ assert(i && i->resampler && i->ref >= 1);
if (i->sample_spec.rate == rate)
return;
i->sample_spec.rate = rate;
pa_resampler_set_input_rate(i->resampler, rate);
}
+
+void pa_sink_input_set_name(struct pa_sink_input *i, const char *name) {
+ assert(i && i->ref >= 1);
+
+ pa_xfree(i->name);
+ i->name = pa_xstrdup(name);
+}
#include "module.h"
#include "client.h"
+enum pa_sink_input_state {
+ PA_SINK_INPUT_RUNNING,
+ PA_SINK_INPUT_DISCONNECTED
+};
+
struct pa_sink_input {
+ int ref;
+ enum pa_sink_input_state state;
+
uint32_t index;
int corked;
};
struct pa_sink_input* pa_sink_input_new(struct pa_sink *s, const char *name, const struct pa_sample_spec *spec, int variable_rate);
-void pa_sink_input_free(struct pa_sink_input* i);
+void pa_sink_input_unref(struct pa_sink_input* i);
+struct pa_sink_input* pa_sink_input_ref(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);
+/* To be called by the implementing module only */
+void pa_sink_input_disconnect(struct pa_sink_input* i);
+
+/* External code may request disconnection with this funcion */
+void pa_sink_input_kill(struct pa_sink_input*i);
pa_usec_t pa_sink_input_get_latency(struct pa_sink_input *i);
void pa_sink_input_set_rate(struct pa_sink_input *i, uint32_t rate);
+void pa_sink_input_set_name(struct pa_sink_input *i, const char *name);
+
#endif
pa_xfree(s);
return NULL;
}
-
+
s->name = pa_xstrdup(name);
s->description = NULL;
+
+ s->ref = 1;
+ s->state = PA_SINK_RUNNING;
s->owner = NULL;
s->core = core;
return s;
}
-void pa_sink_free(struct pa_sink *s) {
+void pa_sink_disconnect(struct pa_sink* s) {
struct pa_sink_input *i, *j = NULL;
- assert(s);
+ assert(s && s->state == PA_SINK_RUNNING);
pa_namereg_unregister(s->core, s->name);
pa_sink_input_kill(i);
j = i;
}
- pa_idxset_free(s->inputs, NULL, NULL);
- pa_source_free(s->monitor_source);
+ pa_source_disconnect(s->monitor_source);
+
pa_idxset_remove_by_data(s->core->sinks, s, NULL);
+ pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
+
+ s->notify = NULL;
+ s->get_latency = NULL;
+
+ s->state = PA_SINK_DISCONNECTED;
+}
+
+static void sink_free(struct pa_sink *s) {
+ assert(s && s->ref == 0);
+
+ if (s->state != PA_SINK_DISCONNECTED)
+ pa_sink_disconnect(s);
pa_log(__FILE__": freed %u \"%s\"\n", s->index, s->name);
- pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
+ pa_source_unref(s->monitor_source);
+ s->monitor_source = NULL;
+ pa_idxset_free(s->inputs, NULL, NULL);
+
pa_xfree(s->name);
pa_xfree(s->description);
pa_xfree(s);
}
+void pa_sink_unref(struct pa_sink*s) {
+ assert(s && s->ref >= 1);
+
+ if (!(--s->ref))
+ sink_free(s);
+}
+
+struct pa_sink* pa_sink_ref(struct pa_sink *s) {
+ assert(s && s->ref >= 1);
+ s->ref++;
+ return s;
+}
+
void pa_sink_notify(struct pa_sink*s) {
- assert(s);
+ assert(s && s->ref >= 1);
if (s->notify)
s->notify(s);
struct pa_sink_input *i;
unsigned n = 0;
- assert(s && info);
+ assert(s && s->ref >= 1 && info);
for (i = pa_idxset_first(s->inputs, &index); maxinfo > 0 && i; i = pa_idxset_next(s->inputs, &index)) {
- if (pa_sink_input_peek(i, &info->chunk) < 0)
+ pa_sink_input_ref(i);
+
+ if (pa_sink_input_peek(i, &info->chunk) < 0) {
+ pa_sink_input_unref(i);
continue;
+ }
info->volume = i->volume;
+ info->userdata = i;
assert(info->chunk.memblock && info->chunk.memblock->data && info->chunk.length);
- info->userdata = i;
info++;
maxinfo--;
}
static void inputs_drop(struct pa_sink *s, struct pa_mix_info *info, unsigned maxinfo, size_t length) {
- assert(s && info);
+ assert(s && s->ref >= 1 && info);
for (; maxinfo > 0; maxinfo--, info++) {
struct pa_sink_input *i = info->userdata;
pa_sink_input_drop(i, &info->chunk, length);
pa_memblock_unref(info->chunk.memblock);
+
+ pa_sink_input_unref(i);
+ info->userdata = NULL;
}
}
struct pa_mix_info info[MAX_MIX_CHANNELS];
unsigned n;
size_t l;
- assert(s && length && result);
+ int r = -1;
+ assert(s && s->ref >= 1 && length && result);
+ pa_sink_ref(s);
+
n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
if (n <= 0)
- return -1;
+ goto finish;
if (n == 1) {
uint32_t volume = PA_VOLUME_NORM;
assert(s->monitor_source);
pa_source_post(s->monitor_source, result);
- return 0;
+ r = 0;
+
+finish:
+ pa_sink_unref(s);
+
+ return r;
}
int pa_sink_render_into(struct pa_sink*s, struct pa_memchunk *target) {
struct pa_mix_info info[MAX_MIX_CHANNELS];
unsigned n;
size_t l;
- assert(s && target && target->length && target->memblock && target->memblock->data);
+ int r = -1;
+ assert(s && s->ref >= 1 && target && target->length && target->memblock && target->memblock->data);
+
+ pa_sink_ref(s);
n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
if (n <= 0)
- return -1;
+ goto finish;
if (n == 1) {
uint32_t volume = PA_VOLUME_NORM;
assert(s->monitor_source);
pa_source_post(s->monitor_source, target);
- return 0;
+ r = 0;
+
+finish:
+ pa_sink_unref(s);
+
+ return r;
}
void pa_sink_render_into_full(struct pa_sink *s, struct pa_memchunk *target) {
struct pa_memchunk chunk;
size_t l, d;
- assert(s && target && target->memblock && target->length && target->memblock->data);
+ assert(s && s->ref >= 1 && target && target->memblock && target->length && target->memblock->data);
+ pa_sink_ref(s);
+
l = target->length;
d = 0;
while (l > 0) {
chunk.length -= d;
pa_silence_memchunk(&chunk, &s->sample_spec);
}
+
+ pa_sink_unref(s);
}
void pa_sink_render_full(struct pa_sink *s, size_t length, struct pa_memchunk *result) {
- assert(s && length && result);
+ assert(s && s->ref >= 1 && length && result);
/*** This needs optimization ***/
}
pa_usec_t pa_sink_get_latency(struct pa_sink *s) {
- assert(s);
+ assert(s && s->ref >= 1);
if (!s->get_latency)
return 0;
return s->get_latency(s);
}
-void pa_sink_set_owner(struct pa_sink *sink, struct pa_module *m) {
- sink->owner = m;
+void pa_sink_set_owner(struct pa_sink *s, struct pa_module *m) {
+ assert(s && s->ref >= 1);
+
+ s->owner = m;
- if (sink->monitor_source)
- pa_source_set_owner(sink->monitor_source, m);
+ if (s->monitor_source)
+ pa_source_set_owner(s->monitor_source, m);
}
-void pa_sink_set_volume(struct pa_sink *sink, pa_volume_t volume) {
- assert(sink);
+void pa_sink_set_volume(struct pa_sink *s, pa_volume_t volume) {
+ assert(s && s->ref >= 1);
- if (sink->volume != volume) {
- sink->volume = volume;
- pa_subscription_post(sink->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, sink->index);
+ if (s->volume != volume) {
+ s->volume = volume;
+ pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
}
}
#define PA_MAX_INPUTS_PER_SINK 6
+enum pa_sink_state {
+ PA_SINK_RUNNING,
+ PA_SINK_DISCONNECTED
+};
+
struct pa_sink {
+ int ref;
+ enum pa_sink_state state;
+
uint32_t index;
char *name, *description;
};
struct pa_sink* pa_sink_new(struct pa_core *core, const char *name, int fail, const struct pa_sample_spec *spec);
-void pa_sink_free(struct pa_sink* s);
+void pa_sink_disconnect(struct pa_sink* s);
+void pa_sink_unref(struct pa_sink*s);
+struct pa_sink* pa_sink_ref(struct pa_sink *s);
+
int pa_sink_render(struct pa_sink*s, size_t length, struct pa_memchunk *result);
void pa_sink_render_full(struct pa_sink *s, size_t length, struct pa_memchunk *result);
static void free_userdata(struct userdata *u) {
assert(u);
- if (u->sink_input)
- pa_sink_input_free(u->sink_input);
+ if (u->sink_input) {
+ pa_sink_input_disconnect(u->sink_input);
+ pa_sink_input_unref(u->sink_input);
+ }
+
if (u->memchunk.memblock)
pa_memblock_unref(u->memchunk.memblock);
if (u->sndfile)
return NULL;
o = pa_xmalloc(sizeof(struct pa_source_output));
+ o->ref = 1;
+ o->state = PA_SOURCE_OUTPUT_RUNNING;
o->name = pa_xstrdup(name);
o->client = NULL;
o->owner = NULL;
return o;
}
-void pa_source_output_free(struct pa_source_output* o) {
- assert(o);
-
- assert(o->source && o->source->core);
+void pa_source_output_disconnect(struct pa_source_output*o) {
+ assert(o && o->state == PA_SOURCE_OUTPUT_RUNNING && 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);
+ pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_REMOVE, o->index);
+ o->source = NULL;
+
+ o->push = NULL;
+ o->kill = NULL;
+
+
+ o->state = PA_SOURCE_OUTPUT_DISCONNECTED;
+}
+
+static void source_output_free(struct pa_source_output* o) {
+ assert(o);
+
+ if (o->state != PA_SOURCE_OUTPUT_DISCONNECTED)
+ pa_source_output_disconnect(o);
+
if (o->resampler)
pa_resampler_free(o->resampler);
- pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_REMOVE, o->index);
-
pa_xfree(o->name);
pa_xfree(o);
}
-void pa_source_output_kill(struct pa_source_output*i) {
- assert(i);
- if (i->kill)
- i->kill(i);
+void pa_source_output_unref(struct pa_source_output* o) {
+ assert(o && o->ref >= 1);
+
+ if (!(--o->ref))
+ source_output_free(o);
+}
+
+struct pa_source_output* pa_source_output_ref(struct pa_source_output *o) {
+ assert(o && o->ref >= 1);
+ o->ref++;
+ return o;
+}
+
+
+void pa_source_output_kill(struct pa_source_output*o) {
+ assert(o && o->ref >= 1);
+
+ if (o->kill)
+ o->kill(o);
}
void pa_source_output_push(struct pa_source_output *o, const struct pa_memchunk *chunk) {
o->push(o, &rchunk);
pa_memblock_unref(rchunk.memblock);
}
+
+void pa_source_output_set_name(struct pa_source_output *o, const char *name) {
+ assert(o && o->ref >= 1);
+ pa_xfree(o->name);
+ o->name = pa_xstrdup(name);
+}
#include "module.h"
#include "client.h"
+enum pa_source_output_state {
+ PA_SOURCE_OUTPUT_RUNNING,
+ PA_SOURCE_OUTPUT_DISCONNECTED
+};
+
struct pa_source_output {
+ int ref;
+ enum pa_source_output_state state;
+
uint32_t index;
char *name;
};
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_unref(struct pa_source_output* o);
+struct pa_source_output* pa_source_output_ref(struct pa_source_output *o);
+/* To be called by the implementing module only */
+void pa_source_output_disconnect(struct pa_source_output*o);
+
+/* External code may request disconnection with this funcion */
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);
+void pa_source_output_set_name(struct pa_source_output *i, const char *name);
+
#endif
return NULL;
}
+ s->ref = 1;
+ s->state = PA_SOURCE_RUNNING;
+
s->name = pa_xstrdup(name);
s->description = NULL;
return s;
}
-void pa_source_free(struct pa_source *s) {
+void pa_source_disconnect(struct pa_source *s) {
struct pa_source_output *o, *j = NULL;
- assert(s);
+ assert(s && s->state == PA_SOURCE_RUNNING);
pa_namereg_unregister(s->core, s->name);
pa_source_output_kill(o);
j = o;
}
- pa_idxset_free(s->outputs, NULL, NULL);
-
+
pa_idxset_remove_by_data(s->core->sources, s, NULL);
+ pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
- pa_log(__FILE__": freed %u \"%s\"\n", s->index, s->name);
+ s->notify = NULL;
+
+ s->state = PA_SOURCE_DISCONNECTED;
+}
- pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
+static void source_free(struct pa_source *s) {
+ assert(s && !s->ref);
+
+ if (s->state != PA_SOURCE_DISCONNECTED)
+ pa_source_disconnect(s);
+ pa_log(__FILE__": freed %u \"%s\"\n", s->index, s->name);
+
+ pa_idxset_free(s->outputs, NULL, NULL);
+
pa_xfree(s->name);
pa_xfree(s->description);
pa_xfree(s);
}
+void pa_source_unref(struct pa_source *s) {
+ assert(s && s->ref >= 1);
+
+ if (!(--s->ref))
+ source_free(s);
+}
+
+struct pa_source* pa_source_ref(struct pa_source *s) {
+ assert(s && s->ref >= 1);
+ s->ref++;
+ return s;
+}
+
void pa_source_notify(struct pa_source*s) {
- assert(s);
+ assert(s && s->ref >= 1);
if (s->notify)
s->notify(s);
}
void pa_source_post(struct pa_source*s, struct pa_memchunk *chunk) {
- assert(s && chunk);
+ assert(s && s->ref >= 1 && chunk);
+ pa_source_ref(s);
pa_idxset_foreach(s->outputs, do_post, chunk);
+ pa_source_unref(s);
}
void pa_source_set_owner(struct pa_source *s, struct pa_module *m) {
#define PA_MAX_OUTPUTS_PER_SOURCE 16
+enum pa_source_state {
+ PA_SOURCE_RUNNING,
+ PA_SOURCE_DISCONNECTED,
+};
+
struct pa_source {
+ int ref;
+ enum pa_source_state state;
+
uint32_t index;
char *name, *description;
};
struct pa_source* pa_source_new(struct pa_core *core, const char *name, int fail, const struct pa_sample_spec *spec);
-void pa_source_free(struct pa_source *s);
+void pa_source_disconnect(struct pa_source *s);
+void pa_source_unref(struct pa_source *s);
+struct pa_source* pa_source_ref(struct pa_source *c);
/* Pass a new memory block to all output streams */
void pa_source_post(struct pa_source*s, struct pa_memchunk *b);