]> code.delx.au - pulseaudio/blob - src/pulsecore/source.c
use single array for storing pa_core hook lists, add sink state changed hook, drop...
[pulseaudio] / src / pulsecore / source.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <pulse/utf8.h>
35 #include <pulse/xmalloc.h>
36
37 #include <pulsecore/source-output.h>
38 #include <pulsecore/namereg.h>
39 #include <pulsecore/core-subscribe.h>
40 #include <pulsecore/log.h>
41 #include <pulsecore/sample-util.h>
42
43 #include "source.h"
44
45 static PA_DEFINE_CHECK_TYPE(pa_source, pa_msgobject);
46
47 static void source_free(pa_object *o);
48
49 pa_source* pa_source_new(
50 pa_core *core,
51 const char *driver,
52 const char *name,
53 int fail,
54 const pa_sample_spec *spec,
55 const pa_channel_map *map) {
56
57 pa_source *s;
58 char st[256];
59 int r;
60 pa_channel_map tmap;
61
62 assert(core);
63 assert(name);
64 assert(spec);
65
66 pa_return_null_if_fail(pa_sample_spec_valid(spec));
67
68 if (!map)
69 map = pa_channel_map_init_auto(&tmap, spec->channels, PA_CHANNEL_MAP_DEFAULT);
70
71 pa_return_null_if_fail(map && pa_channel_map_valid(map));
72 pa_return_null_if_fail(map->channels == spec->channels);
73 pa_return_null_if_fail(!driver || pa_utf8_valid(driver));
74 pa_return_null_if_fail(pa_utf8_valid(name) && *name);
75
76 s = pa_msgobject_new(pa_source);
77
78 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SOURCE, s, fail))) {
79 pa_xfree(s);
80 return NULL;
81 }
82
83 s->parent.parent.free = source_free;
84 s->parent.process_msg = pa_source_process_msg;
85
86 s->core = core;
87 s->state = PA_SOURCE_IDLE;
88 s->name = pa_xstrdup(name);
89 s->description = NULL;
90 s->driver = pa_xstrdup(driver);
91 s->module = NULL;
92
93 s->sample_spec = *spec;
94 s->channel_map = *map;
95
96 s->outputs = pa_idxset_new(NULL, NULL);
97 s->monitor_of = NULL;
98
99 pa_cvolume_reset(&s->volume, spec->channels);
100 s->muted = 0;
101 s->refresh_volume = s->refresh_muted = 0;
102
103 s->is_hardware = 0;
104
105 s->get_latency = NULL;
106 s->set_volume = NULL;
107 s->get_volume = NULL;
108 s->set_mute = NULL;
109 s->get_mute = NULL;
110 s->set_state = NULL;
111 s->userdata = NULL;
112
113 s->asyncmsgq = NULL;
114
115 r = pa_idxset_put(core->sources, s, &s->index);
116 assert(s->index != PA_IDXSET_INVALID && r >= 0);
117
118 pa_sample_spec_snprint(st, sizeof(st), spec);
119 pa_log_info("Created source %u \"%s\" with sample spec \"%s\"", s->index, s->name, st);
120
121 s->thread_info.outputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
122 s->thread_info.soft_volume = s->volume;
123 s->thread_info.soft_muted = s->muted;
124 s->thread_info.state = s->state;
125
126 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
127
128 pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_NEW_POST], s);
129
130 return s;
131 }
132
133 static int source_set_state(pa_source *s, pa_source_state_t state) {
134 int ret;
135
136 pa_assert(s);
137
138 if (s->state == state)
139 return 0;
140
141 if (s->set_state)
142 if ((ret = s->set_state(s, state)) < 0)
143 return -1;
144
145 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) < 0)
146 return -1;
147
148 s->state = state;
149 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], s);
150 return 0;
151 }
152
153 void pa_source_disconnect(pa_source *s) {
154 pa_source_output *o, *j = NULL;
155
156 pa_assert(s);
157 pa_return_if_fail(s->state != PA_SOURCE_DISCONNECTED);
158
159 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_DISCONNECT], s);
160
161 pa_namereg_unregister(s->core, s->name);
162 pa_idxset_remove_by_data(s->core->sources, s, NULL);
163
164 while ((o = pa_idxset_first(s->outputs, NULL))) {
165 pa_assert(o != j);
166 pa_source_output_kill(o);
167 j = o;
168 }
169
170 source_set_state(s, PA_SOURCE_DISCONNECTED);
171
172 s->get_latency = NULL;
173 s->get_volume = NULL;
174 s->set_volume = NULL;
175 s->set_mute = NULL;
176 s->get_mute = NULL;
177 s->set_state = NULL;
178
179 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
180
181 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_DISCONNECT_POST], s);
182 }
183
184 static void source_free(pa_object *o) {
185 pa_source_output *so;
186 pa_source *s = PA_SOURCE(o);
187
188 pa_assert(s);
189 pa_assert(pa_source_refcnt(s) == 0);
190
191 if (s->state != PA_SOURCE_DISCONNECTED)
192 pa_source_disconnect(s);
193
194 pa_log_info("Freeing source %u \"%s\"", s->index, s->name);
195
196 pa_idxset_free(s->outputs, NULL, NULL);
197
198 while ((so = pa_hashmap_steal_first(s->thread_info.outputs)))
199 pa_source_output_unref(so);
200
201 pa_hashmap_free(s->thread_info.outputs, NULL, NULL);
202
203 pa_xfree(s->name);
204 pa_xfree(s->description);
205 pa_xfree(s->driver);
206 pa_xfree(s);
207 }
208
209 int pa_source_update_status(pa_source*s) {
210 pa_source_assert_ref(s);
211
212 if (s->state == PA_SOURCE_SUSPENDED)
213 return 0;
214
215 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
216 }
217
218 int pa_source_suspend(pa_source *s, int suspend) {
219 pa_source_assert_ref(s);
220
221 if (suspend)
222 return source_set_state(s, PA_SOURCE_SUSPENDED);
223 else
224 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
225 }
226
227 void pa_source_ping(pa_source *s) {
228 pa_source_assert_ref(s);
229
230 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_PING, NULL, 0, NULL, NULL);
231 }
232
233 void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
234 pa_source_output *o;
235 void *state = NULL;
236
237 pa_source_assert_ref(s);
238 pa_assert(chunk);
239
240 if (s->thread_info.state != PA_SOURCE_RUNNING)
241 return;
242
243 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&s->thread_info.soft_volume)) {
244 pa_memchunk vchunk = *chunk;
245
246 pa_memblock_ref(vchunk.memblock);
247 pa_memchunk_make_writable(&vchunk, 0);
248
249 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&s->thread_info.soft_volume))
250 pa_silence_memchunk(&vchunk, &s->sample_spec);
251 else
252 pa_volume_memchunk(&vchunk, &s->sample_spec, &s->thread_info.soft_volume);
253
254 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
255 pa_source_output_push(o, &vchunk);
256
257 pa_memblock_unref(vchunk.memblock);
258 } else {
259
260 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
261 pa_source_output_push(o, chunk);
262
263 }
264 }
265
266 pa_usec_t pa_source_get_latency(pa_source *s) {
267 pa_usec_t usec;
268
269 pa_source_assert_ref(s);
270
271 if (s->get_latency)
272 return s->get_latency(s);
273
274 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
275 return 0;
276
277 return usec;
278 }
279
280 void pa_source_set_volume(pa_source *s, const pa_cvolume *volume) {
281 int changed;
282
283 pa_source_assert_ref(s);
284 pa_assert(volume);
285
286 changed = !pa_cvolume_equal(volume, &s->volume);
287 s->volume = *volume;
288
289 if (s->set_volume && s->set_volume(s) < 0)
290 s->set_volume = NULL;
291
292 if (!s->set_volume)
293 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), 0, NULL, pa_xfree);
294
295 if (changed)
296 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
297 }
298
299 const pa_cvolume *pa_source_get_volume(pa_source *s) {
300 pa_cvolume old_volume;
301 pa_source_assert_ref(s);
302
303 old_volume = s->volume;
304
305 if (s->get_volume && s->get_volume(s) < 0)
306 s->get_volume = NULL;
307
308 if (!s->get_volume && s->refresh_volume)
309 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_VOLUME, &s->volume, 0, NULL);
310
311 if (!pa_cvolume_equal(&old_volume, &s->volume))
312 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
313
314 return &s->volume;
315 }
316
317 void pa_source_set_mute(pa_source *s, int mute) {
318 int changed;
319
320 pa_source_assert_ref(s);
321
322 changed = s->muted != mute;
323
324 if (s->set_mute && s->set_mute(s) < 0)
325 s->set_mute = NULL;
326
327 if (!s->set_mute)
328 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
329
330 if (changed)
331 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
332 }
333
334 int pa_source_get_mute(pa_source *s) {
335 int old_muted;
336
337 pa_source_assert_ref(s);
338
339 old_muted = s->muted;
340
341 if (s->get_mute && s->get_mute(s) < 0)
342 s->get_mute = NULL;
343
344 if (!s->get_mute && s->refresh_muted)
345 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_MUTE, &s->muted, 0, NULL);
346
347 if (old_muted != s->muted)
348 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
349
350 return s->muted;
351 }
352
353 void pa_source_set_module(pa_source *s, pa_module *m) {
354 pa_source_assert_ref(s);
355
356 if (m == s->module)
357 return;
358
359 s->module = m;
360
361 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
362 }
363
364 void pa_source_set_description(pa_source *s, const char *description) {
365 pa_source_assert_ref(s);
366
367 if (!description && !s->description)
368 return;
369
370 if (description && s->description && !strcmp(description, s->description))
371 return;
372
373 pa_xfree(s->description);
374 s->description = pa_xstrdup(description);
375
376 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
377 }
378
379 void pa_source_set_asyncmsgq(pa_source *s, pa_asyncmsgq *q) {
380 pa_source_assert_ref(s);
381 pa_assert(q);
382
383 s->asyncmsgq = q;
384 }
385
386 unsigned pa_source_used_by(pa_source *s) {
387 pa_source_assert_ref(s);
388
389 return pa_idxset_size(s->outputs);
390 }
391
392 int pa_source_process_msg(pa_msgobject *object, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
393 pa_source *s = PA_SOURCE(object);
394 pa_source_assert_ref(s);
395
396 switch ((pa_source_message_t) code) {
397 case PA_SOURCE_MESSAGE_ADD_OUTPUT: {
398 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
399 pa_hashmap_put(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index), pa_source_output_ref(o));
400 return 0;
401 }
402
403 case PA_SOURCE_MESSAGE_REMOVE_OUTPUT: {
404 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
405 if (pa_hashmap_remove(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index)))
406 pa_source_output_unref(o);
407
408 return 0;
409 }
410
411 case PA_SOURCE_MESSAGE_SET_VOLUME:
412 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
413 return 0;
414
415 case PA_SOURCE_MESSAGE_SET_MUTE:
416 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
417 return 0;
418
419 case PA_SOURCE_MESSAGE_GET_VOLUME:
420 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
421 return 0;
422
423 case PA_SOURCE_MESSAGE_GET_MUTE:
424 *((int*) userdata) = s->thread_info.soft_muted;
425 return 0;
426
427 case PA_SOURCE_MESSAGE_PING:
428 return 0;
429
430 case PA_SOURCE_MESSAGE_SET_STATE:
431 s->thread_info.state = PA_PTR_TO_UINT(userdata);
432 return 0;
433
434 case PA_SOURCE_MESSAGE_GET_LATENCY:
435 case PA_SOURCE_MESSAGE_MAX:
436 ;
437 }
438
439 return -1;
440 }