]> code.delx.au - pulseaudio/blob - src/polypcore/source.c
unhide padsp
[pulseaudio] / src / polypcore / source.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <polyp/utf8.h>
32 #include <polyp/xmalloc.h>
33
34 #include <polypcore/source-output.h>
35 #include <polypcore/namereg.h>
36 #include <polypcore/core-subscribe.h>
37 #include <polypcore/log.h>
38 #include <polypcore/sample-util.h>
39
40 #include "source.h"
41
42 #define CHECK_VALIDITY_RETURN_NULL(condition) \
43 do {\
44 if (!(condition)) \
45 return NULL; \
46 } while (0)
47
48 pa_source* pa_source_new(
49 pa_core *core,
50 const char *driver,
51 const char *name,
52 int fail,
53 const pa_sample_spec *spec,
54 const pa_channel_map *map) {
55
56 pa_source *s;
57 char st[256];
58 int r;
59 pa_channel_map tmap;
60
61 assert(core);
62 assert(name);
63 assert(spec);
64
65 CHECK_VALIDITY_RETURN_NULL(pa_sample_spec_valid(spec));
66
67 if (!map)
68 map = pa_channel_map_init_auto(&tmap, spec->channels, PA_CHANNEL_MAP_DEFAULT);
69
70 CHECK_VALIDITY_RETURN_NULL(map && pa_channel_map_valid(map));
71 CHECK_VALIDITY_RETURN_NULL(map->channels == spec->channels);
72 CHECK_VALIDITY_RETURN_NULL(!driver || pa_utf8_valid(driver));
73 CHECK_VALIDITY_RETURN_NULL(pa_utf8_valid(name) && *name);
74
75 s = pa_xnew(pa_source, 1);
76
77 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SOURCE, s, fail))) {
78 pa_xfree(s);
79 return NULL;
80 }
81
82 s->ref = 1;
83 s->core = core;
84 s->state = PA_SOURCE_RUNNING;
85 s->name = pa_xstrdup(name);
86 s->description = NULL;
87 s->driver = pa_xstrdup(driver);
88 s->owner = NULL;
89
90 s->sample_spec = *spec;
91 s->channel_map = *map;
92
93 s->outputs = pa_idxset_new(NULL, NULL);
94 s->monitor_of = NULL;
95
96 pa_cvolume_reset(&s->sw_volume, spec->channels);
97 pa_cvolume_reset(&s->hw_volume, spec->channels);
98 s->sw_muted = 0;
99 s->hw_muted = 0;
100
101 s->get_latency = NULL;
102 s->notify = NULL;
103 s->set_hw_volume = NULL;
104 s->get_hw_volume = NULL;
105 s->set_hw_mute = NULL;
106 s->get_hw_mute = NULL;
107 s->userdata = NULL;
108
109 r = pa_idxset_put(core->sources, s, &s->index);
110 assert(s->index != PA_IDXSET_INVALID && r >= 0);
111
112 pa_sample_spec_snprint(st, sizeof(st), spec);
113 pa_log_info(__FILE__": created %u \"%s\" with sample spec \"%s\"", s->index, s->name, st);
114
115 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
116
117 return s;
118 }
119
120 void pa_source_disconnect(pa_source *s) {
121 pa_source_output *o, *j = NULL;
122
123 assert(s);
124 assert(s->state == PA_SOURCE_RUNNING);
125
126 pa_namereg_unregister(s->core, s->name);
127
128 while ((o = pa_idxset_first(s->outputs, NULL))) {
129 assert(o != j);
130 pa_source_output_kill(o);
131 j = o;
132 }
133
134 pa_idxset_remove_by_data(s->core->sources, s, NULL);
135
136 s->get_latency = NULL;
137 s->notify = NULL;
138 s->get_hw_volume = NULL;
139 s->set_hw_volume = NULL;
140 s->set_hw_mute = NULL;
141 s->get_hw_mute = NULL;
142
143 s->state = PA_SOURCE_DISCONNECTED;
144 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
145 }
146
147 static void source_free(pa_source *s) {
148 assert(s);
149 assert(!s->ref);
150
151 if (s->state != PA_SOURCE_DISCONNECTED)
152 pa_source_disconnect(s);
153
154 pa_log_info(__FILE__": freed %u \"%s\"", s->index, s->name);
155
156 pa_idxset_free(s->outputs, NULL, NULL);
157
158 pa_xfree(s->name);
159 pa_xfree(s->description);
160 pa_xfree(s->driver);
161 pa_xfree(s);
162 }
163
164 void pa_source_unref(pa_source *s) {
165 assert(s);
166 assert(s->ref >= 1);
167
168 if (!(--s->ref))
169 source_free(s);
170 }
171
172 pa_source* pa_source_ref(pa_source *s) {
173 assert(s);
174 assert(s->ref >= 1);
175
176 s->ref++;
177 return s;
178 }
179
180 void pa_source_notify(pa_source*s) {
181 assert(s);
182 assert(s->ref >= 1);
183
184 if (s->notify)
185 s->notify(s);
186 }
187
188 static int do_post(void *p, PA_GCC_UNUSED uint32_t idx, PA_GCC_UNUSED int *del, void*userdata) {
189 pa_source_output *o = p;
190 const pa_memchunk *chunk = userdata;
191
192 assert(o);
193 assert(chunk);
194
195 pa_source_output_push(o, chunk);
196 return 0;
197 }
198
199 void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
200 assert(s);
201 assert(s->ref >= 1);
202 assert(chunk);
203
204 pa_source_ref(s);
205
206 if (s->sw_muted || !pa_cvolume_is_norm(&s->sw_volume)) {
207 pa_memchunk vchunk = *chunk;
208
209 pa_memblock_ref(vchunk.memblock);
210 pa_memchunk_make_writable(&vchunk, s->core->memblock_stat, 0);
211 if (s->sw_muted)
212 pa_silence_memchunk(&vchunk, &s->sample_spec);
213 else
214 pa_volume_memchunk(&vchunk, &s->sample_spec, &s->sw_volume);
215 pa_idxset_foreach(s->outputs, do_post, &vchunk);
216 pa_memblock_unref(vchunk.memblock);
217 } else
218 pa_idxset_foreach(s->outputs, do_post, (void*) chunk);
219
220 pa_source_unref(s);
221 }
222
223 void pa_source_set_owner(pa_source *s, pa_module *m) {
224 assert(s);
225 assert(s->ref >= 1);
226
227 s->owner = m;
228 }
229
230 pa_usec_t pa_source_get_latency(pa_source *s) {
231 assert(s);
232 assert(s->ref >= 1);
233
234 if (!s->get_latency)
235 return 0;
236
237 return s->get_latency(s);
238 }
239
240 void pa_source_set_volume(pa_source *s, pa_mixer_t m, const pa_cvolume *volume) {
241 pa_cvolume *v;
242
243 assert(s);
244 assert(s->ref >= 1);
245 assert(volume);
246
247 if (m == PA_MIXER_HARDWARE && s->set_hw_volume)
248 v = &s->hw_volume;
249 else
250 v = &s->sw_volume;
251
252 if (pa_cvolume_equal(v, volume))
253 return;
254
255 *v = *volume;
256
257 if (v == &s->hw_volume)
258 if (s->set_hw_volume(s) < 0)
259 s->sw_volume = *volume;
260
261 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
262 }
263
264 const pa_cvolume *pa_source_get_volume(pa_source *s, pa_mixer_t m) {
265 assert(s);
266 assert(s->ref >= 1);
267
268 if (m == PA_MIXER_HARDWARE && s->set_hw_volume) {
269
270 if (s->get_hw_volume)
271 s->get_hw_volume(s);
272
273 return &s->hw_volume;
274 } else
275 return &s->sw_volume;
276 }
277
278 void pa_source_set_mute(pa_source *s, pa_mixer_t m, int mute) {
279 int *t;
280
281 assert(s);
282 assert(s->ref >= 1);
283
284 if (m == PA_MIXER_HARDWARE && s->set_hw_mute)
285 t = &s->hw_muted;
286 else
287 t = &s->sw_muted;
288
289 if (!!*t == !!mute)
290 return;
291
292 *t = !!mute;
293
294 if (t == &s->hw_muted)
295 if (s->set_hw_mute(s) < 0)
296 s->sw_muted = !!mute;
297
298 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
299 }
300
301 int pa_source_get_mute(pa_source *s, pa_mixer_t m) {
302 assert(s);
303 assert(s->ref >= 1);
304
305 if (m == PA_MIXER_HARDWARE && s->set_hw_mute) {
306
307 if (s->get_hw_mute)
308 s->get_hw_mute(s);
309
310 return s->hw_muted;
311 } else
312 return s->sw_muted;
313 }