2004-06-15 15:18:33 +00:00
|
|
|
#include <stdio.h>
|
2004-06-08 23:54:24 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "source.h"
|
2004-06-15 17:05:03 +00:00
|
|
|
#include "sourceoutput.h"
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
struct source* source_new(struct core *core, const char *name, const struct sample_spec *spec) {
|
|
|
|
|
struct source *s;
|
|
|
|
|
int r;
|
|
|
|
|
assert(core && spec);
|
|
|
|
|
|
|
|
|
|
s = malloc(sizeof(struct source));
|
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
|
|
s->name = name ? strdup(name) : NULL;
|
|
|
|
|
s->core = core;
|
|
|
|
|
s->sample_spec = *spec;
|
2004-06-15 17:05:03 +00:00
|
|
|
s->outputs = idxset_new(NULL, NULL);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
s->notify = NULL;
|
2004-06-08 23:54:24 +00:00
|
|
|
s->userdata = NULL;
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
r = idxset_put(core->sources, s, &s->index);
|
|
|
|
|
assert(s->index != IDXSET_INVALID && r >= 0);
|
|
|
|
|
|
2004-06-15 15:18:33 +00:00
|
|
|
fprintf(stderr, "source: created %u \"%s\"\n", s->index, s->name);
|
|
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void source_free(struct source *s) {
|
2004-06-15 17:05:03 +00:00
|
|
|
struct source_output *o, *j = NULL;
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(s);
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
while ((o = idxset_first(s->outputs, NULL))) {
|
2004-06-14 20:30:50 +00:00
|
|
|
assert(o != j);
|
2004-06-15 17:05:03 +00:00
|
|
|
source_output_kill(o);
|
2004-06-14 20:30:50 +00:00
|
|
|
j = o;
|
|
|
|
|
}
|
2004-06-15 17:05:03 +00:00
|
|
|
idxset_free(s->outputs, NULL, NULL);
|
2004-06-11 21:30:16 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
idxset_remove_by_data(s->core->sources, s, NULL);
|
2004-06-11 21:30:16 +00:00
|
|
|
|
2004-06-15 15:18:33 +00:00
|
|
|
fprintf(stderr, "source: freed %u \"%s\"\n", s->index, s->name);
|
|
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
free(s->name);
|
|
|
|
|
free(s);
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
void source_notify(struct source*s) {
|
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
|
|
if (s->notify)
|
|
|
|
|
s->notify(s);
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
static int do_post(void *p, uint32_t index, int *del, void*userdata) {
|
|
|
|
|
struct memchunk *chunk = userdata;
|
2004-06-15 17:05:03 +00:00
|
|
|
struct source_output *o = p;
|
|
|
|
|
assert(o && o->push && index && del && chunk);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
o->push(o, chunk);
|
2004-06-08 23:54:24 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void source_post(struct source*s, struct memchunk *chunk) {
|
|
|
|
|
assert(s && chunk);
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
idxset_foreach(s->outputs, do_post, chunk);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|