]> code.delx.au - pulseaudio/blob - src/source.c
45ccfb2713cd19e18e150fda68b61570cdae2957
[pulseaudio] / 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 && name);
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->description = NULL;
27
28 s->owner = NULL;
29 s->core = core;
30 s->sample_spec = *spec;
31 s->outputs = pa_idxset_new(NULL, NULL);
32 s->monitor_of = NULL;
33
34 s->notify = NULL;
35 s->userdata = NULL;
36
37 r = pa_idxset_put(core->sources, s, &s->index);
38 assert(s->index != PA_IDXSET_INVALID && r >= 0);
39
40 pa_sample_snprint(st, sizeof(st), spec);
41 fprintf(stderr, "source: created %u \"%s\" with sample spec \"%s\"\n", s->index, s->name, st);
42
43 return s;
44 }
45
46 void pa_source_free(struct pa_source *s) {
47 struct pa_source_output *o, *j = NULL;
48 assert(s);
49
50 pa_namereg_unregister(s->core, s->name);
51
52 while ((o = pa_idxset_first(s->outputs, NULL))) {
53 assert(o != j);
54 pa_source_output_kill(o);
55 j = o;
56 }
57 pa_idxset_free(s->outputs, NULL, NULL);
58
59 pa_idxset_remove_by_data(s->core->sources, s, NULL);
60
61 fprintf(stderr, "source: freed %u \"%s\"\n", s->index, s->name);
62
63 free(s->name);
64 free(s->description);
65 free(s);
66 }
67
68 void pa_source_notify(struct pa_source*s) {
69 assert(s);
70
71 if (s->notify)
72 s->notify(s);
73 }
74
75 static int do_post(void *p, uint32_t index, int *del, void*userdata) {
76 struct pa_memchunk *chunk = userdata;
77 struct pa_source_output *o = p;
78 assert(o && o->push && del && chunk);
79
80 pa_source_output_push(o, chunk);
81 return 0;
82 }
83
84 void pa_source_post(struct pa_source*s, struct pa_memchunk *chunk) {
85 assert(s && chunk);
86
87 pa_idxset_foreach(s->outputs, do_post, chunk);
88 }
89
90 struct pa_source* pa_source_get_default(struct pa_core *c) {
91 struct pa_source *source;
92 assert(c);
93
94 if ((source = pa_idxset_get_by_index(c->sources, c->default_source_index)))
95 return source;
96
97 if (!(source = pa_idxset_first(c->sources, &c->default_source_index)))
98 return NULL;
99
100 fprintf(stderr, "core: default source vanished, setting to %u.\n", source->index);
101 return source;
102 }
103
104 char *pa_source_list_to_string(struct pa_core *c) {
105 struct pa_strbuf *s;
106 struct pa_source *source, *default_source;
107 uint32_t index = PA_IDXSET_INVALID;
108 assert(c);
109
110 s = pa_strbuf_new();
111 assert(s);
112
113 pa_strbuf_printf(s, "%u source(s) available.\n", pa_idxset_ncontents(c->sources));
114
115 default_source = pa_source_get_default(c);
116
117 for (source = pa_idxset_first(c->sources, &index); source; source = pa_idxset_next(c->sources, &index)) {
118 char ss[PA_SAMPLE_SNPRINT_MAX_LENGTH];
119 pa_sample_snprint(ss, sizeof(ss), &source->sample_spec);
120 pa_strbuf_printf(s, " %c index: %u\n\tname: <%s>\n\tsample_spec: <%s>\n", source == default_source ? '*' : ' ', source->index, source->name, ss);
121
122 if (source->monitor_of)
123 pa_strbuf_printf(s, "\tmonitor_of: <%u>\n", source->monitor_of->index);
124 if (source->owner)
125 pa_strbuf_printf(s, "\towner module: <%u>\n", source->owner->index);
126 if (source->description)
127 pa_strbuf_printf(s, "\tdescription: <%s>\n", source->description);
128 }
129
130 return pa_strbuf_tostring_free(s);
131 }
132
133 void pa_source_set_owner(struct pa_source *s, struct pa_module *m) {
134 assert(s);
135 s->owner = m;
136 }