7 #include "sourceoutput.h"
10 struct source* source_new(struct core *core, const char *name, const struct sample_spec *spec) {
15 s = malloc(sizeof(struct source));
18 s->name = name ? strdup(name) : NULL;
20 s->sample_spec = *spec;
21 s->outputs = idxset_new(NULL, NULL);
26 r = idxset_put(core->sources, s, &s->index);
27 assert(s->index != IDXSET_INVALID && r >= 0);
29 fprintf(stderr, "source: created %u \"%s\"\n", s->index, s->name);
34 void source_free(struct source *s) {
35 struct source_output *o, *j = NULL;
38 while ((o = idxset_first(s->outputs, NULL))) {
40 source_output_kill(o);
43 idxset_free(s->outputs, NULL, NULL);
45 idxset_remove_by_data(s->core->sources, s, NULL);
47 fprintf(stderr, "source: freed %u \"%s\"\n", s->index, s->name);
53 void source_notify(struct source*s) {
60 static int do_post(void *p, uint32_t index, int *del, void*userdata) {
61 struct memchunk *chunk = userdata;
62 struct source_output *o = p;
63 assert(o && o->push && del && chunk);
69 void source_post(struct source*s, struct memchunk *chunk) {
72 idxset_foreach(s->outputs, do_post, chunk);
75 struct source* source_get_default(struct core *c) {
76 struct source *source;
79 if ((source = idxset_get_by_index(c->sources, c->default_source_index)))
82 if (!(source = idxset_first(c->sources, &c->default_source_index)))
85 fprintf(stderr, "core: default source vanished, setting to %u.\n", source->index);
89 char *source_list_to_string(struct core *c) {
91 struct source *source, *default_source;
92 uint32_t index = IDXSET_INVALID;
98 strbuf_printf(s, "%u source(s) available.\n", idxset_ncontents(c->sources));
100 default_source = source_get_default(c);
102 for (source = idxset_first(c->sources, &index); source; source = idxset_next(c->sources, &index))
103 strbuf_printf(s, " %c index: %u, name: <%s>\n", source == default_source ? '*' : ' ', source->index, source->name);
105 return strbuf_tostring_free(s);