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
34 #include <pulse/introspect.h>
35 #include <pulse/utf8.h>
36 #include <pulse/xmalloc.h>
38 #include <pulsecore/sink-input.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/sample-util.h>
42 #include <pulsecore/core-subscribe.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/macro.h>
48 #define MAX_MIX_CHANNELS 32
50 static void sink_free(pa_object *s);
57 const pa_sample_spec *spec,
58 const pa_channel_map *map) {
70 pa_return_null_if_fail(pa_sample_spec_valid(spec));
73 map = pa_channel_map_init_auto(&tmap, spec->channels, PA_CHANNEL_MAP_DEFAULT);
75 pa_return_null_if_fail(map && pa_channel_map_valid(map));
76 pa_return_null_if_fail(map->channels == spec->channels);
77 pa_return_null_if_fail(!driver || pa_utf8_valid(driver));
78 pa_return_null_if_fail(name && pa_utf8_valid(name) && *name);
80 s = pa_msgobject_new(pa_sink);
82 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SINK, s, fail))) {
87 s->parent.parent.free = sink_free;
88 s->parent.process_msg = pa_sink_process_msg;
91 pa_atomic_store(&s->state, PA_SINK_IDLE);
92 s->name = pa_xstrdup(name);
93 s->description = NULL;
94 s->driver = pa_xstrdup(driver);
97 s->sample_spec = *spec;
98 s->channel_map = *map;
100 s->inputs = pa_idxset_new(NULL, NULL);
102 pa_cvolume_reset(&s->volume, spec->channels);
104 s->refresh_volume = s->refresh_mute = 0;
108 s->get_latency = NULL;
109 s->set_volume = NULL;
110 s->get_volume = NULL;
117 pa_assert_se(s->asyncmsgq = pa_asyncmsgq_new(0));
119 r = pa_idxset_put(core->sinks, s, &s->index);
120 pa_assert(s->index != PA_IDXSET_INVALID && r >= 0);
122 pa_sample_spec_snprint(st, sizeof(st), spec);
123 pa_log_info("Created sink %u \"%s\" with sample spec \"%s\"", s->index, s->name, st);
125 n = pa_sprintf_malloc("%s.monitor", name);
127 if (!(s->monitor_source = pa_source_new(core, driver, n, 0, spec, map)))
128 pa_log_warn("failed to create monitor source.");
131 s->monitor_source->monitor_of = s;
132 d = pa_sprintf_malloc("Monitor Source of %s", s->name);
133 pa_source_set_description(s->monitor_source, d);
139 s->thread_info.inputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
140 s->thread_info.soft_volume = s->volume;
141 s->thread_info.soft_muted = s->muted;
143 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
148 static void sink_start(pa_sink *s) {
149 pa_sink_state_t state;
152 state = pa_sink_get_state(s);
153 pa_return_if_fail(state == PA_SINK_IDLE || state == PA_SINK_SUSPENDED);
155 pa_atomic_store(&s->state, PA_SINK_RUNNING);
160 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_START, NULL, NULL, NULL);
163 static void sink_stop(pa_sink *s) {
164 pa_sink_state_t state;
168 state = pa_sink_get_state(s);
169 pa_return_if_fail(state == PA_SINK_RUNNING || state == PA_SINK_SUSPENDED);
171 stop = state == PA_SINK_RUNNING;
172 pa_atomic_store(&s->state, PA_SINK_IDLE);
178 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_STOP, NULL, NULL, NULL);
182 void pa_sink_disconnect(pa_sink* s) {
183 pa_sink_input *i, *j = NULL;
186 pa_return_if_fail(pa_sink_get_state(s) != PA_SINK_DISCONNECTED);
190 pa_atomic_store(&s->state, PA_SINK_DISCONNECTED);
191 pa_namereg_unregister(s->core, s->name);
193 pa_hook_fire(&s->core->hook_sink_disconnect, s);
195 while ((i = pa_idxset_first(s->inputs, NULL))) {
197 pa_sink_input_kill(i);
201 if (s->monitor_source)
202 pa_source_disconnect(s->monitor_source);
204 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
206 s->get_latency = NULL;
207 s->get_volume = NULL;
208 s->set_volume = NULL;
214 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
217 static void sink_free(pa_object *o) {
218 pa_sink *s = PA_SINK(o);
221 pa_assert(pa_sink_refcnt(s) == 0);
223 pa_sink_disconnect(s);
225 pa_log_info("Freeing sink %u \"%s\"", s->index, s->name);
227 if (s->monitor_source) {
228 pa_source_unref(s->monitor_source);
229 s->monitor_source = NULL;
232 pa_idxset_free(s->inputs, NULL, NULL);
234 pa_hashmap_free(s->thread_info.inputs, (pa_free2_cb_t) pa_sink_input_unref, NULL);
236 pa_asyncmsgq_free(s->asyncmsgq);
239 pa_xfree(s->description);
244 void pa_sink_update_status(pa_sink*s) {
245 pa_sink_assert_ref(s);
247 if (pa_sink_get_state(s) == PA_SINK_SUSPENDED)
250 if (pa_sink_used_by(s) > 0)
256 void pa_sink_suspend(pa_sink *s, int suspend) {
257 pa_sink_state_t state;
259 pa_sink_assert_ref(s);
261 state = pa_sink_get_state(s);
262 pa_return_if_fail(suspend && (state == PA_SINK_RUNNING || state == PA_SINK_IDLE));
263 pa_return_if_fail(!suspend && (state == PA_SINK_SUSPENDED));
267 pa_atomic_store(&s->state, PA_SINK_SUSPENDED);
272 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_STOP, NULL, NULL, NULL);
275 pa_atomic_store(&s->state, PA_SINK_RUNNING);
280 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_START, NULL, NULL, NULL);
284 static unsigned fill_mix_info(pa_sink *s, pa_mix_info *info, unsigned maxinfo) {
289 pa_sink_assert_ref(s);
292 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
293 /* Increase ref counter, to make sure that this input doesn't
294 * vanish while we still need it */
295 pa_sink_input_ref(i);
297 if (pa_sink_input_peek(i, &info->chunk, &info->volume) < 0) {
298 pa_sink_input_unref(i);
304 pa_assert(info->chunk.memblock);
305 pa_assert(info->chunk.length);
315 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned maxinfo, size_t length) {
316 pa_sink_assert_ref(s);
319 for (; maxinfo > 0; maxinfo--, info++) {
320 pa_sink_input *i = info->userdata;
323 pa_assert(info->chunk.memblock);
326 pa_sink_input_drop(i, &info->chunk, length);
327 pa_memblock_unref(info->chunk.memblock);
329 /* Decrease ref counter */
330 pa_sink_input_unref(i);
331 info->userdata = NULL;
335 int pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
336 pa_mix_info info[MAX_MIX_CHANNELS];
340 pa_sink_assert_ref(s);
346 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
354 *result = info[0].chunk;
355 pa_memblock_ref(result->memblock);
357 if (result->length > length)
358 result->length = length;
360 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
362 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&volume)) {
363 pa_memchunk_make_writable(result, 0);
364 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
365 pa_silence_memchunk(result, &s->sample_spec);
367 pa_volume_memchunk(result, &s->sample_spec, &volume);
371 result->memblock = pa_memblock_new(s->core->mempool, length);
373 ptr = pa_memblock_acquire(result->memblock);
374 result->length = pa_mix(info, n, ptr, length, &s->sample_spec, &s->thread_info.soft_volume, s->thread_info.soft_muted);
375 pa_memblock_release(result->memblock);
380 inputs_drop(s, info, n, result->length);
382 if (s->monitor_source)
383 pa_source_post(s->monitor_source, result);
393 int pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
394 pa_mix_info info[MAX_MIX_CHANNELS];
398 pa_sink_assert_ref(s);
400 pa_assert(target->memblock);
401 pa_assert(target->length);
405 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
412 if (target->length > info[0].chunk.length)
413 target->length = info[0].chunk.length;
415 if (s->thread_info.soft_muted)
416 pa_silence_memchunk(target, &s->sample_spec);
421 ptr = pa_memblock_acquire(target->memblock);
422 src = pa_memblock_acquire(info[0].chunk.memblock);
424 memcpy((uint8_t*) ptr + target->index,
425 (uint8_t*) src + info[0].chunk.index,
428 pa_memblock_release(target->memblock);
429 pa_memblock_release(info[0].chunk.memblock);
431 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
433 if (!pa_cvolume_is_norm(&volume))
434 pa_volume_memchunk(target, &s->sample_spec, &volume);
440 ptr = pa_memblock_acquire(target->memblock);
442 target->length = pa_mix(info, n,
443 (uint8_t*) ptr + target->index,
446 &s->thread_info.soft_volume,
447 s->thread_info.soft_muted);
449 pa_memblock_release(target->memblock);
452 inputs_drop(s, info, n, target->length);
454 if (s->monitor_source)
455 pa_source_post(s->monitor_source, target);
465 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
469 pa_sink_assert_ref(s);
471 pa_assert(target->memblock);
472 pa_assert(target->length);
483 if (pa_sink_render_into(s, &chunk) < 0)
494 pa_silence_memchunk(&chunk, &s->sample_spec);
500 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
501 pa_sink_assert_ref(s);
505 /*** This needs optimization ***/
507 result->memblock = pa_memblock_new(s->core->mempool, result->length = length);
510 pa_sink_render_into_full(s, result);
513 pa_usec_t pa_sink_get_latency(pa_sink *s) {
516 pa_sink_assert_ref(s);
519 return s->get_latency(s);
521 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, NULL) < 0)
527 void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume) {
530 pa_sink_assert_ref(s);
533 changed = !pa_cvolume_equal(volume, &s->volume);
536 if (s->set_volume && s->set_volume(s) < 0)
537 s->set_volume = NULL;
540 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), NULL, pa_xfree);
543 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
546 const pa_cvolume *pa_sink_get_volume(pa_sink *s) {
547 struct pa_cvolume old_volume;
549 pa_sink_assert_ref(s);
551 old_volume = s->volume;
553 if (s->get_volume && s->get_volume(s) < 0)
554 s->get_volume = NULL;
556 if (!s->get_volume && s->refresh_volume)
557 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, &s->volume, NULL);
559 if (!pa_cvolume_equal(&old_volume, &s->volume))
560 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
565 void pa_sink_set_mute(pa_sink *s, int mute) {
568 pa_sink_assert_ref(s);
570 changed = s->muted != mute;
572 if (s->set_mute && s->set_mute(s) < 0)
576 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), NULL, NULL);
579 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
582 int pa_sink_get_mute(pa_sink *s) {
585 pa_sink_assert_ref(s);
587 old_muted = s->muted;
589 if (s->get_mute && s->get_mute(s) < 0)
592 if (!s->get_mute && s->refresh_mute)
593 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MUTE, &s->muted, NULL);
595 if (old_muted != s->muted)
596 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
601 void pa_sink_set_module(pa_sink *s, pa_module *m) {
602 pa_sink_assert_ref(s);
609 if (s->monitor_source)
610 pa_source_set_module(s->monitor_source, m);
612 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
615 void pa_sink_set_description(pa_sink *s, const char *description) {
616 pa_sink_assert_ref(s);
618 if (!description && !s->description)
621 if (description && s->description && !strcmp(description, s->description))
624 pa_xfree(s->description);
625 s->description = pa_xstrdup(description);
627 if (s->monitor_source) {
630 n = pa_sprintf_malloc("Monitor Source of %s", s->description? s->description : s->name);
631 pa_source_set_description(s->monitor_source, n);
635 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
638 unsigned pa_sink_used_by(pa_sink *s) {
641 pa_sink_assert_ref(s);
643 ret = pa_idxset_size(s->inputs);
645 if (s->monitor_source)
646 ret += pa_source_used_by(s->monitor_source);
651 int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, pa_memchunk *chunk) {
652 pa_sink *s = PA_SINK(o);
653 pa_sink_assert_ref(s);
656 case PA_SINK_MESSAGE_ADD_INPUT: {
657 pa_sink_input *i = userdata;
658 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
662 case PA_SINK_MESSAGE_REMOVE_INPUT: {
663 pa_sink_input *i = userdata;
664 pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index));
668 case PA_SINK_MESSAGE_SET_VOLUME:
669 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
672 case PA_SINK_MESSAGE_SET_MUTE:
673 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
676 case PA_SINK_MESSAGE_GET_VOLUME:
677 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
680 case PA_SINK_MESSAGE_GET_MUTE:
681 *((int*) userdata) = s->thread_info.soft_muted;