44d7da01d6cf87a685aa900a899a627926fd20bd
[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 #include "namereg.h"
10
11 struct pa_source* pa_source_new(struct pa_core *core, const char *name, int fail, const struct pa_sample_spec *spec) {
12     struct pa_source *s;
13     char st[256];
14     int r;
15     assert(core && spec);
16
17     s = malloc(sizeof(struct pa_source));
18     assert(s);
19
20     if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SOURCE, s, fail))) {
21         free(s);
22         return NULL;
23     }
24
25     s->name = strdup(name);
26     s->core = core;
27     s->sample_spec = *spec;
28     s->outputs = pa_idxset_new(NULL, NULL);
29     s->monitor_of = NULL;
30
31     s->notify = NULL;
32     s->userdata = NULL;
33
34     r = pa_idxset_put(core->sources, s, &s->index);
35     assert(s->index != PA_IDXSET_INVALID && r >= 0);
36
37     pa_sample_snprint(st, sizeof(st), spec);
38     fprintf(stderr, "source: created %u \"%s\" with sample spec \"%s\"\n", s->index, s->name, st);
39     
40     return s;
41 }
42
43 void pa_source_free(struct pa_source *s) {
44     struct pa_source_output *o, *j = NULL;
45     assert(s);
46
47     pa_namereg_unregister(s->core, s->name);
48     
49     while ((o = pa_idxset_first(s->outputs, NULL))) {
50         assert(o != j);
51         pa_source_output_kill(o);
52         j = o;
53     }
54     pa_idxset_free(s->outputs, NULL, NULL);
55     
56     pa_idxset_remove_by_data(s->core->sources, s, NULL);
57
58     fprintf(stderr, "source: freed %u \"%s\"\n", s->index, s->name);
59
60     free(s->name);
61     free(s);
62 }
63
64 void pa_source_notify(struct pa_source*s) {
65     assert(s);
66
67     if (s->notify)
68         s->notify(s);
69 }
70
71 static int do_post(void *p, uint32_t index, int *del, void*userdata) {
72     struct pa_memchunk *chunk = userdata;
73     struct pa_source_output *o = p;
74     assert(o && o->push && del && chunk);
75
76     pa_source_output_push(o, chunk);
77     return 0;
78 }
79
80 void pa_source_post(struct pa_source*s, struct pa_memchunk *chunk) {
81     assert(s && chunk);
82
83     pa_idxset_foreach(s->outputs, do_post, chunk);
84 }
85
86 struct pa_source* pa_source_get_default(struct pa_core *c) {
87     struct pa_source *source;
88     assert(c);
89
90     if ((source = pa_idxset_get_by_index(c->sources, c->default_source_index)))
91         return source;
92
93     if (!(source = pa_idxset_first(c->sources, &c->default_source_index)))
94         return NULL;
95
96     fprintf(stderr, "core: default source vanished, setting to %u.\n", source->index);
97     return source;
98 }
99
100 char *pa_source_list_to_string(struct pa_core *c) {
101     struct pa_strbuf *s;
102     struct pa_source *source, *default_source;
103     uint32_t index = PA_IDXSET_INVALID;
104     assert(c);
105
106     s = pa_strbuf_new();
107     assert(s);
108
109     pa_strbuf_printf(s, "%u source(s) available.\n", pa_idxset_ncontents(c->sources));
110
111     default_source = pa_source_get_default(c);
112     
113     for (source = pa_idxset_first(c->sources, &index); source; source = pa_idxset_next(c->sources, &index)) {
114         char ss[PA_SAMPLE_SNPRINT_MAX_LENGTH];
115         char mo[256] = "";
116         if (source->monitor_of) 
117             snprintf(mo, sizeof(mo), "\n\tmonitor_of: <%u>", source->monitor_of->index);
118         pa_sample_snprint(ss, sizeof(ss), &source->sample_spec);
119         pa_strbuf_printf(s, "  %c index: %u\n\tname: <%s>\n\tsample_spec: <%s>%s\n", source == default_source ? '*' : ' ', source->index, source->name, ss, mo);
120     }
121     
122     return pa_strbuf_tostring_free(s);
123 }
124