basic cli interface
[profile/ivi/pulseaudio.git] / src / source.c
1 #include <stdio.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "source.h"
7 #include "sourceoutput.h"
8 #include "strbuf.h"
9
10 struct source* source_new(struct core *core, const char *name, const struct sample_spec *spec) {
11     struct source *s;
12     int r;
13     assert(core && spec);
14
15     s = malloc(sizeof(struct source));
16     assert(s);
17
18     s->name = name ? strdup(name) : NULL;
19     s->core = core;
20     s->sample_spec = *spec;
21     s->outputs = idxset_new(NULL, NULL);
22
23     s->notify = NULL;
24     s->userdata = NULL;
25
26     r = idxset_put(core->sources, s, &s->index);
27     assert(s->index != IDXSET_INVALID && r >= 0);
28
29     fprintf(stderr, "source: created %u \"%s\"\n", s->index, s->name);
30     
31     return s;
32 }
33
34 void source_free(struct source *s) {
35     struct source_output *o, *j = NULL;
36     assert(s);
37
38     while ((o = idxset_first(s->outputs, NULL))) {
39         assert(o != j);
40         source_output_kill(o);
41         j = o;
42     }
43     idxset_free(s->outputs, NULL, NULL);
44     
45     idxset_remove_by_data(s->core->sources, s, NULL);
46
47     fprintf(stderr, "source: freed %u \"%s\"\n", s->index, s->name);
48
49     free(s->name);
50     free(s);
51 }
52
53 void source_notify(struct source*s) {
54     assert(s);
55
56     if (s->notify)
57         s->notify(s);
58 }
59
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);
64
65     o->push(o, chunk);
66     return 0;
67 }
68
69 void source_post(struct source*s, struct memchunk *chunk) {
70     assert(s && chunk);
71
72     idxset_foreach(s->outputs, do_post, chunk);
73 }
74
75 struct source* source_get_default(struct core *c) {
76     struct source *source;
77     assert(c);
78
79     if ((source = idxset_get_by_index(c->sources, c->default_source_index)))
80         return source;
81
82     if (!(source = idxset_first(c->sources, &c->default_source_index)))
83         return NULL;
84
85     fprintf(stderr, "core: default source vanished, setting to %u.\n", source->index);
86     return source;
87 }
88
89 char *source_list_to_string(struct core *c) {
90     struct strbuf *s;
91     struct source *source, *default_source;
92     uint32_t index = IDXSET_INVALID;
93     assert(c);
94
95     s = strbuf_new();
96     assert(s);
97
98     strbuf_printf(s, "%u source(s) available.\n", idxset_ncontents(c->sources));
99
100     default_source = source_get_default(c);
101     
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);
104     
105     return strbuf_tostring_free(s);
106 }
107