4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
33 #include <pulse/utf8.h>
34 #include <pulse/xmalloc.h>
36 #include <pulsecore/source-output.h>
37 #include <pulsecore/namereg.h>
38 #include <pulsecore/core-subscribe.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/sample-util.h>
44 static PA_DEFINE_CHECK_TYPE(pa_source, pa_msgobject);
46 static void source_free(pa_object *o);
48 pa_source* pa_source_new(
53 const pa_sample_spec *spec,
54 const pa_channel_map *map) {
64 pa_return_null_if_fail(pa_sample_spec_valid(spec));
67 map = pa_channel_map_init_auto(&tmap, spec->channels, PA_CHANNEL_MAP_DEFAULT);
69 pa_return_null_if_fail(map && pa_channel_map_valid(map));
70 pa_return_null_if_fail(map->channels == spec->channels);
71 pa_return_null_if_fail(!driver || pa_utf8_valid(driver));
72 pa_return_null_if_fail(pa_utf8_valid(name) && *name);
74 s = pa_msgobject_new(pa_source);
76 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SOURCE, s, fail))) {
81 s->parent.parent.free = source_free;
82 s->parent.process_msg = pa_source_process_msg;
85 s->state = PA_SOURCE_INIT;
87 s->name = pa_xstrdup(name);
88 s->description = NULL;
89 s->driver = pa_xstrdup(driver);
92 s->sample_spec = *spec;
93 s->channel_map = *map;
95 s->outputs = pa_idxset_new(NULL, NULL);
99 pa_cvolume_reset(&s->volume, spec->channels);
101 s->refresh_volume = s->refresh_muted = FALSE;
103 s->get_latency = NULL;
104 s->set_volume = NULL;
105 s->get_volume = NULL;
114 pa_assert_se(pa_idxset_put(core->sources, s, &s->index) >= 0);
116 pa_sample_spec_snprint(st, sizeof(st), spec);
117 pa_log_info("Created source %u \"%s\" with sample spec \"%s\"", s->index, s->name, st);
119 s->thread_info.outputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
120 s->thread_info.soft_volume = s->volume;
121 s->thread_info.soft_muted = s->muted;
122 s->thread_info.state = s->state;
127 void pa_source_put(pa_source *s) {
128 pa_source_assert_ref(s);
130 pa_assert(s->state == PA_SINK_INIT);
131 pa_assert(s->rtpoll);
132 pa_assert(s->asyncmsgq);
134 s->thread_info.state = s->state = PA_SOURCE_IDLE;
136 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
137 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_NEW_POST], s);
140 static int source_set_state(pa_source *s, pa_source_state_t state) {
145 if (s->state == state)
148 if ((s->state == PA_SOURCE_SUSPENDED && PA_SOURCE_OPENED(state)) ||
149 (PA_SOURCE_OPENED(s->state) && state == PA_SOURCE_SUSPENDED)) {
153 /* We're suspending or resuming, tell everyone about it */
155 for (o = PA_SOURCE_OUTPUT(pa_idxset_first(s->outputs, &idx)); o; o = PA_SOURCE_OUTPUT(pa_idxset_next(s->outputs, &idx)))
157 o->suspend(o, state == PA_SINK_SUSPENDED);
161 if ((ret = s->set_state(s, state)) < 0)
164 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) < 0)
169 if (state != PA_SOURCE_UNLINKED) /* if we enter UNLINKED state pa_source_unlink() will fire the apropriate events */
170 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], s);
174 void pa_source_unlink(pa_source *s) {
176 pa_source_output *o, *j = NULL;
180 /* See pa_sink_unlink() for a couple of comments how this function
183 linked = PA_SOURCE_LINKED(s->state);
186 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], s);
188 if (s->state != PA_SOURCE_UNLINKED)
189 pa_namereg_unregister(s->core, s->name);
190 pa_idxset_remove_by_data(s->core->sources, s, NULL);
192 while ((o = pa_idxset_first(s->outputs, NULL))) {
194 pa_source_output_kill(o);
199 source_set_state(s, PA_SOURCE_UNLINKED);
201 s->state = PA_SOURCE_UNLINKED;
203 s->get_latency = NULL;
204 s->get_volume = NULL;
205 s->set_volume = NULL;
211 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
212 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK_POST], s);
216 static void source_free(pa_object *o) {
217 pa_source_output *so;
218 pa_source *s = PA_SOURCE(o);
221 pa_assert(pa_source_refcnt(s) == 0);
223 if (PA_SOURCE_LINKED(s->state))
226 pa_log_info("Freeing source %u \"%s\"", s->index, s->name);
228 pa_idxset_free(s->outputs, NULL, NULL);
230 while ((so = pa_hashmap_steal_first(s->thread_info.outputs)))
231 pa_source_output_unref(so);
233 pa_hashmap_free(s->thread_info.outputs, NULL, NULL);
236 pa_xfree(s->description);
241 int pa_source_update_status(pa_source*s) {
242 pa_source_assert_ref(s);
243 pa_assert(PA_SOURCE_LINKED(s->state));
245 if (s->state == PA_SOURCE_SUSPENDED)
248 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
251 int pa_source_suspend(pa_source *s, pa_bool_t suspend) {
252 pa_source_assert_ref(s);
253 pa_assert(PA_SOURCE_LINKED(s->state));
256 return source_set_state(s, PA_SOURCE_SUSPENDED);
258 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
261 void pa_source_ping(pa_source *s) {
262 pa_source_assert_ref(s);
263 pa_assert(PA_SOURCE_LINKED(s->state));
265 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_PING, NULL, 0, NULL, NULL);
268 void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
272 pa_source_assert_ref(s);
273 pa_assert(PA_SOURCE_OPENED(s->thread_info.state));
276 if (s->thread_info.state != PA_SOURCE_RUNNING)
279 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&s->thread_info.soft_volume)) {
280 pa_memchunk vchunk = *chunk;
282 pa_memblock_ref(vchunk.memblock);
283 pa_memchunk_make_writable(&vchunk, 0);
285 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&s->thread_info.soft_volume))
286 pa_silence_memchunk(&vchunk, &s->sample_spec);
288 pa_volume_memchunk(&vchunk, &s->sample_spec, &s->thread_info.soft_volume);
290 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
291 pa_source_output_push(o, &vchunk);
293 pa_memblock_unref(vchunk.memblock);
296 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
297 pa_source_output_push(o, chunk);
301 pa_usec_t pa_source_get_latency(pa_source *s) {
304 pa_source_assert_ref(s);
305 pa_assert(PA_SOURCE_LINKED(s->state));
307 if (!PA_SOURCE_OPENED(s->state))
311 return s->get_latency(s);
313 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
319 void pa_source_set_volume(pa_source *s, const pa_cvolume *volume) {
322 pa_source_assert_ref(s);
323 pa_assert(PA_SOURCE_LINKED(s->state));
326 changed = !pa_cvolume_equal(volume, &s->volume);
329 if (s->set_volume && s->set_volume(s) < 0)
330 s->set_volume = NULL;
333 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), 0, NULL, pa_xfree);
336 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
339 const pa_cvolume *pa_source_get_volume(pa_source *s) {
340 pa_cvolume old_volume;
342 pa_source_assert_ref(s);
343 pa_assert(PA_SOURCE_LINKED(s->state));
345 old_volume = s->volume;
347 if (s->get_volume && s->get_volume(s) < 0)
348 s->get_volume = NULL;
350 if (!s->get_volume && s->refresh_volume)
351 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_VOLUME, &s->volume, 0, NULL);
353 if (!pa_cvolume_equal(&old_volume, &s->volume))
354 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
359 void pa_source_set_mute(pa_source *s, pa_bool_t mute) {
362 pa_source_assert_ref(s);
363 pa_assert(PA_SOURCE_LINKED(s->state));
365 changed = s->muted != mute;
368 if (s->set_mute && s->set_mute(s) < 0)
372 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
375 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
378 pa_bool_t pa_source_get_mute(pa_source *s) {
381 pa_source_assert_ref(s);
382 pa_assert(PA_SOURCE_LINKED(s->state));
384 old_muted = s->muted;
386 if (s->get_mute && s->get_mute(s) < 0)
389 if (!s->get_mute && s->refresh_muted)
390 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_MUTE, &s->muted, 0, NULL);
392 if (old_muted != s->muted)
393 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
398 void pa_source_set_module(pa_source *s, pa_module *m) {
399 pa_source_assert_ref(s);
406 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
409 void pa_source_set_description(pa_source *s, const char *description) {
410 pa_source_assert_ref(s);
412 if (!description && !s->description)
415 if (description && s->description && !strcmp(description, s->description))
418 pa_xfree(s->description);
419 s->description = pa_xstrdup(description);
421 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
424 void pa_source_set_asyncmsgq(pa_source *s, pa_asyncmsgq *q) {
425 pa_source_assert_ref(s);
431 void pa_source_set_rtpoll(pa_source *s, pa_rtpoll *p) {
432 pa_source_assert_ref(s);
438 unsigned pa_source_linked_by(pa_source *s) {
439 pa_source_assert_ref(s);
440 pa_assert(PA_SOURCE_LINKED(s->state));
442 return pa_idxset_size(s->outputs);
445 unsigned pa_source_used_by(pa_source *s) {
448 pa_source_assert_ref(s);
449 pa_assert(PA_SOURCE_LINKED(s->state));
451 ret = pa_idxset_size(s->outputs);
452 pa_assert(ret >= s->n_corked);
454 return ret - s->n_corked;
457 int pa_source_process_msg(pa_msgobject *object, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
458 pa_source *s = PA_SOURCE(object);
459 pa_source_assert_ref(s);
460 pa_assert(PA_SOURCE_LINKED(s->thread_info.state));
462 switch ((pa_source_message_t) code) {
463 case PA_SOURCE_MESSAGE_ADD_OUTPUT: {
464 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
465 pa_hashmap_put(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index), pa_source_output_ref(o));
467 pa_assert(!o->thread_info.attached);
468 o->thread_info.attached = TRUE;
476 case PA_SOURCE_MESSAGE_REMOVE_OUTPUT: {
477 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
482 pa_assert(o->thread_info.attached);
483 o->thread_info.attached = FALSE;
485 if (pa_hashmap_remove(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index)))
486 pa_source_output_unref(o);
491 case PA_SOURCE_MESSAGE_SET_VOLUME:
492 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
495 case PA_SOURCE_MESSAGE_SET_MUTE:
496 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
499 case PA_SOURCE_MESSAGE_GET_VOLUME:
500 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
503 case PA_SOURCE_MESSAGE_GET_MUTE:
504 *((pa_bool_t*) userdata) = s->thread_info.soft_muted;
507 case PA_SOURCE_MESSAGE_PING:
510 case PA_SOURCE_MESSAGE_SET_STATE:
511 s->thread_info.state = PA_PTR_TO_UINT(userdata);
514 case PA_SOURCE_MESSAGE_DETACH:
516 /* We're detaching all our output streams so that the
517 * asyncmsgq and rtpoll fields can be changed without
519 pa_source_detach_within_thread(s);
522 case PA_SOURCE_MESSAGE_ATTACH:
524 /* Reattach all streams */
525 pa_source_attach_within_thread(s);
528 case PA_SOURCE_MESSAGE_GET_LATENCY:
529 case PA_SOURCE_MESSAGE_MAX:
536 int pa_source_suspend_all(pa_core *c, pa_bool_t suspend) {
541 pa_core_assert_ref(c);
543 for (source = PA_SOURCE(pa_idxset_first(c->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(c->sources, &idx)))
544 ret -= pa_source_suspend(source, suspend) < 0;
549 void pa_source_detach(pa_source *s) {
550 pa_source_assert_ref(s);
551 pa_assert(PA_SOURCE_LINKED(s->state));
553 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_DETACH, NULL, 0, NULL);
556 void pa_source_attach(pa_source *s) {
557 pa_source_assert_ref(s);
558 pa_assert(PA_SOURCE_LINKED(s->state));
560 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_ATTACH, NULL, 0, NULL);
563 void pa_source_detach_within_thread(pa_source *s) {
567 pa_source_assert_ref(s);
568 pa_assert(PA_SOURCE_LINKED(s->thread_info.state));
570 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
575 void pa_source_attach_within_thread(pa_source *s) {
579 pa_source_assert_ref(s);
580 pa_assert(PA_SOURCE_LINKED(s->thread_info.state));
582 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))