]> code.delx.au - pulseaudio/blob - src/pulsecore/source.c
0d55da448d83a63d61a0d1fbb0d6280ed6ab4982
[pulseaudio] / src / pulsecore / source.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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 <pulse/utf8.h>
32 #include <pulse/xmalloc.h>
33
34 #include <pulsecore/source-output.h>
35 #include <pulsecore/namereg.h>
36 #include <pulsecore/core-subscribe.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/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->is_hardware = 0;
102
103 s->get_latency = NULL;
104 s->notify = NULL;
105 s->set_hw_volume = NULL;
106 s->get_hw_volume = NULL;
107 s->set_hw_mute = NULL;
108 s->get_hw_mute = NULL;
109 s->userdata = NULL;
110
111 r = pa_idxset_put(core->sources, s, &s->index);
112 assert(s->index != PA_IDXSET_INVALID && r >= 0);
113
114 pa_sample_spec_snprint(st, sizeof(st), spec);
115 pa_log_info(__FILE__": created %u \"%s\" with sample spec \"%s\"", s->index, s->name, st);
116
117 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
118
119 return s;
120 }
121
122 void pa_source_disconnect(pa_source *s) {
123 pa_source_output *o, *j = NULL;
124
125 assert(s);
126 assert(s->state == PA_SOURCE_RUNNING);
127
128 s->state = PA_SOURCE_DISCONNECTED;
129 pa_namereg_unregister(s->core, s->name);
130
131 pa_hook_fire(&s->core->hook_source_disconnect, s);
132
133 while ((o = pa_idxset_first(s->outputs, NULL))) {
134 assert(o != j);
135 pa_source_output_kill(o);
136 j = o;
137 }
138
139 pa_idxset_remove_by_data(s->core->sources, s, NULL);
140
141 s->get_latency = NULL;
142 s->notify = NULL;
143 s->get_hw_volume = NULL;
144 s->set_hw_volume = NULL;
145 s->set_hw_mute = NULL;
146 s->get_hw_mute = NULL;
147
148 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
149 }
150
151 static void source_free(pa_source *s) {
152 assert(s);
153 assert(!s->ref);
154
155 if (s->state != PA_SOURCE_DISCONNECTED)
156 pa_source_disconnect(s);
157
158 pa_log_info(__FILE__": freed %u \"%s\"", s->index, s->name);
159
160 pa_idxset_free(s->outputs, NULL, NULL);
161
162 pa_xfree(s->name);
163 pa_xfree(s->description);
164 pa_xfree(s->driver);
165 pa_xfree(s);
166 }
167
168 void pa_source_unref(pa_source *s) {
169 assert(s);
170 assert(s->ref >= 1);
171
172 if (!(--s->ref))
173 source_free(s);
174 }
175
176 pa_source* pa_source_ref(pa_source *s) {
177 assert(s);
178 assert(s->ref >= 1);
179
180 s->ref++;
181 return s;
182 }
183
184 void pa_source_notify(pa_source*s) {
185 assert(s);
186 assert(s->ref >= 1);
187
188 if (s->notify)
189 s->notify(s);
190 }
191
192 static int do_post(void *p, PA_GCC_UNUSED uint32_t idx, PA_GCC_UNUSED int *del, void*userdata) {
193 pa_source_output *o = p;
194 const pa_memchunk *chunk = userdata;
195
196 assert(o);
197 assert(chunk);
198
199 pa_source_output_push(o, chunk);
200 return 0;
201 }
202
203 void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
204 assert(s);
205 assert(s->ref >= 1);
206 assert(chunk);
207
208 pa_source_ref(s);
209
210 if (s->sw_muted || !pa_cvolume_is_norm(&s->sw_volume)) {
211 pa_memchunk vchunk = *chunk;
212
213 pa_memblock_ref(vchunk.memblock);
214 pa_memchunk_make_writable(&vchunk, s->core->memblock_stat, 0);
215 if (s->sw_muted)
216 pa_silence_memchunk(&vchunk, &s->sample_spec);
217 else
218 pa_volume_memchunk(&vchunk, &s->sample_spec, &s->sw_volume);
219 pa_idxset_foreach(s->outputs, do_post, &vchunk);
220 pa_memblock_unref(vchunk.memblock);
221 } else
222 pa_idxset_foreach(s->outputs, do_post, (void*) chunk);
223
224 pa_source_unref(s);
225 }
226
227 void pa_source_set_owner(pa_source *s, pa_module *m) {
228 assert(s);
229 assert(s->ref >= 1);
230
231 if (m == s->owner)
232 return;
233
234 s->owner = m;
235 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
236 }
237
238 pa_usec_t pa_source_get_latency(pa_source *s) {
239 assert(s);
240 assert(s->ref >= 1);
241
242 if (!s->get_latency)
243 return 0;
244
245 return s->get_latency(s);
246 }
247
248 void pa_source_set_volume(pa_source *s, pa_mixer_t m, const pa_cvolume *volume) {
249 pa_cvolume *v;
250
251 assert(s);
252 assert(s->ref >= 1);
253 assert(volume);
254
255 if (m == PA_MIXER_HARDWARE && s->set_hw_volume)
256 v = &s->hw_volume;
257 else
258 v = &s->sw_volume;
259
260 if (pa_cvolume_equal(v, volume))
261 return;
262
263 *v = *volume;
264
265 if (v == &s->hw_volume)
266 if (s->set_hw_volume(s) < 0)
267 s->sw_volume = *volume;
268
269 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
270 }
271
272 const pa_cvolume *pa_source_get_volume(pa_source *s, pa_mixer_t m) {
273 assert(s);
274 assert(s->ref >= 1);
275
276 if (m == PA_MIXER_HARDWARE && s->set_hw_volume) {
277
278 if (s->get_hw_volume)
279 s->get_hw_volume(s);
280
281 return &s->hw_volume;
282 } else
283 return &s->sw_volume;
284 }
285
286 void pa_source_set_mute(pa_source *s, pa_mixer_t m, int mute) {
287 int *t;
288
289 assert(s);
290 assert(s->ref >= 1);
291
292 if (m == PA_MIXER_HARDWARE && s->set_hw_mute)
293 t = &s->hw_muted;
294 else
295 t = &s->sw_muted;
296
297 if (!!*t == !!mute)
298 return;
299
300 *t = !!mute;
301
302 if (t == &s->hw_muted)
303 if (s->set_hw_mute(s) < 0)
304 s->sw_muted = !!mute;
305
306 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
307 }
308
309 int pa_source_get_mute(pa_source *s, pa_mixer_t m) {
310 assert(s);
311 assert(s->ref >= 1);
312
313 if (m == PA_MIXER_HARDWARE && s->set_hw_mute) {
314
315 if (s->get_hw_mute)
316 s->get_hw_mute(s);
317
318 return s->hw_muted;
319 } else
320 return s->sw_muted;
321 }
322
323 void pa_source_set_description(pa_source *s, const char *description) {
324 assert(s);
325 assert(s->ref >= 1);
326
327 if (!description && !s->description)
328 return;
329
330 if (description && s->description && !strcmp(description, s->description))
331 return;
332
333 pa_xfree(s->description);
334 s->description = pa_xstrdup(description);
335
336 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
337 }
338
339 unsigned pa_source_used_by(pa_source *s) {
340 assert(s);
341 assert(s->ref >= 1);
342
343 return pa_idxset_size(s->outputs);
344 }