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/utf8.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/sample-util.h>
38 #include <pulsecore/core-subscribe.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/play-memblockq.h>
41 #include <pulsecore/namereg.h>
43 #include "sink-input.h"
45 #define CONVERT_BUFFER_LENGTH 4096
46 #define MOVE_BUFFER_LENGTH (1024*1024)
47 #define SILENCE_BUFFER_LENGTH (64*1024)
49 #define CHECK_VALIDITY_RETURN_NULL(condition) \
55 pa_sink_input_new_data* pa_sink_input_new_data_init(pa_sink_input_new_data *data) {
58 memset(data, 0, sizeof(*data));
59 data->resample_method = PA_RESAMPLER_INVALID;
63 void pa_sink_input_new_data_set_channel_map(pa_sink_input_new_data *data, const pa_channel_map *map) {
66 if ((data->channel_map_is_set = !!map))
67 data->channel_map = *map;
70 void pa_sink_input_new_data_set_volume(pa_sink_input_new_data *data, const pa_cvolume *volume) {
73 if ((data->volume_is_set = !!volume))
74 data->volume = *volume;
77 void pa_sink_input_new_data_set_sample_spec(pa_sink_input_new_data *data, const pa_sample_spec *spec) {
80 if ((data->sample_spec_is_set = !!spec))
81 data->sample_spec = *spec;
84 pa_sink_input* pa_sink_input_new(
86 pa_sink_input_new_data *data,
87 pa_sink_input_flags_t flags) {
90 pa_resampler *resampler = NULL;
92 char st[PA_SAMPLE_SPEC_SNPRINT_MAX];
97 if (!(flags & PA_SINK_INPUT_NO_HOOKS))
98 if (pa_hook_fire(&core->hook_sink_input_new, data) < 0)
101 CHECK_VALIDITY_RETURN_NULL(!data->driver || pa_utf8_valid(data->driver));
102 CHECK_VALIDITY_RETURN_NULL(!data->name || pa_utf8_valid(data->name));
105 data->sink = pa_namereg_get(core, NULL, PA_NAMEREG_SINK, 1);
107 CHECK_VALIDITY_RETURN_NULL(data->sink);
108 CHECK_VALIDITY_RETURN_NULL(data->sink->state == PA_SINK_RUNNING);
110 if (!data->sample_spec_is_set)
111 data->sample_spec = data->sink->sample_spec;
113 CHECK_VALIDITY_RETURN_NULL(pa_sample_spec_valid(&data->sample_spec));
115 if (!data->channel_map_is_set)
116 pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
118 CHECK_VALIDITY_RETURN_NULL(pa_channel_map_valid(&data->channel_map));
119 CHECK_VALIDITY_RETURN_NULL(data->channel_map.channels == data->sample_spec.channels);
121 if (!data->volume_is_set)
122 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
124 CHECK_VALIDITY_RETURN_NULL(pa_cvolume_valid(&data->volume));
125 CHECK_VALIDITY_RETURN_NULL(data->volume.channels == data->sample_spec.channels);
127 if (data->resample_method == PA_RESAMPLER_INVALID)
128 data->resample_method = core->resample_method;
130 CHECK_VALIDITY_RETURN_NULL(data->resample_method < PA_RESAMPLER_MAX);
132 if (pa_idxset_size(data->sink->inputs) >= PA_MAX_INPUTS_PER_SINK) {
133 pa_log_warn("Failed to create sink input: too many inputs per sink.");
137 if ((flags & PA_SINK_INPUT_VARIABLE_RATE) ||
138 !pa_sample_spec_equal(&data->sample_spec, &data->sink->sample_spec) ||
139 !pa_channel_map_equal(&data->channel_map, &data->sink->channel_map))
141 if (!(resampler = pa_resampler_new(
143 &data->sample_spec, &data->channel_map,
144 &data->sink->sample_spec, &data->sink->channel_map,
145 data->resample_method))) {
146 pa_log_warn("Unsupported resampling operation.");
150 i = pa_xnew(pa_sink_input, 1);
152 i->state = PA_SINK_INPUT_DRAINED;
154 i->name = pa_xstrdup(data->name);
155 i->driver = pa_xstrdup(data->driver);
156 i->module = data->module;
157 i->sink = data->sink;
158 i->client = data->client;
160 i->sample_spec = data->sample_spec;
161 i->channel_map = data->channel_map;
162 i->volume = data->volume;
167 i->get_latency = NULL;
173 pa_memchunk_reset(&i->resampled_chunk);
174 i->resampler = resampler;
175 i->resample_method = data->resample_method;
176 i->silence_memblock = NULL;
178 r = pa_idxset_put(core->sink_inputs, i, &i->index);
180 r = pa_idxset_put(i->sink->inputs, i, NULL);
183 pa_log_info("created %u \"%s\" on %s with sample spec %s",
187 pa_sample_spec_snprint(st, sizeof(st), &i->sample_spec));
189 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
191 /* We do not call pa_sink_notify() here, because the virtual
192 * functions have not yet been initialized */
197 void pa_sink_input_disconnect(pa_sink_input *i) {
199 assert(i->state != PA_SINK_INPUT_DISCONNECTED);
201 assert(i->sink->core);
203 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
204 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
206 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
212 i->get_latency = NULL;
215 i->state = PA_SINK_INPUT_DISCONNECTED;
218 static void sink_input_free(pa_sink_input* i) {
221 if (i->state != PA_SINK_INPUT_DISCONNECTED)
222 pa_sink_input_disconnect(i);
224 pa_log_info("freed %u \"%s\"", i->index, i->name);
226 if (i->resampled_chunk.memblock)
227 pa_memblock_unref(i->resampled_chunk.memblock);
230 pa_resampler_free(i->resampler);
232 if (i->silence_memblock)
233 pa_memblock_unref(i->silence_memblock);
240 void pa_sink_input_unref(pa_sink_input *i) {
248 pa_sink_input* pa_sink_input_ref(pa_sink_input *i) {
256 void pa_sink_input_kill(pa_sink_input*i) {
264 pa_usec_t pa_sink_input_get_latency(pa_sink_input *i) {
271 r += i->get_latency(i);
273 if (i->resampled_chunk.memblock)
274 r += pa_bytes_to_usec(i->resampled_chunk.length, &i->sink->sample_spec);
277 r += pa_bytes_to_usec(i->move_silence, &i->sink->sample_spec);
282 int pa_sink_input_peek(pa_sink_input *i, pa_memchunk *chunk, pa_cvolume *volume) {
284 int do_volume_adj_here;
292 pa_sink_input_ref(i);
294 if (!i->peek || !i->drop || i->state == PA_SINK_INPUT_CORKED)
297 assert(i->state == PA_SINK_INPUT_RUNNING || i->state == PA_SINK_INPUT_DRAINED);
299 if (i->move_silence > 0) {
301 /* We have just been moved and shall play some silence for a
302 * while until the old sink has drained its playback buffer */
304 if (!i->silence_memblock)
305 i->silence_memblock = pa_silence_memblock_new(i->sink->core->mempool, &i->sink->sample_spec, SILENCE_BUFFER_LENGTH);
307 chunk->memblock = pa_memblock_ref(i->silence_memblock);
309 chunk->length = i->move_silence < chunk->memblock->length ? i->move_silence : chunk->memblock->length;
312 do_volume_adj_here = 1;
317 do_volume_adj_here = 0;
318 ret = i->peek(i, chunk);
322 do_volume_adj_here = !pa_channel_map_equal(&i->channel_map, &i->sink->channel_map);
323 volume_is_norm = pa_cvolume_is_norm(&i->volume);
325 while (!i->resampled_chunk.memblock) {
329 if ((ret = i->peek(i, &tchunk)) < 0)
332 assert(tchunk.length);
334 l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
336 if (l > tchunk.length)
339 i->drop(i, &tchunk, l);
342 /* It might be necessary to adjust the volume here */
343 if (do_volume_adj_here && !volume_is_norm) {
344 pa_memchunk_make_writable(&tchunk, 0);
345 pa_volume_memchunk(&tchunk, &i->sample_spec, &i->volume);
348 pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
349 pa_memblock_unref(tchunk.memblock);
352 assert(i->resampled_chunk.memblock);
353 assert(i->resampled_chunk.length);
355 *chunk = i->resampled_chunk;
356 pa_memblock_ref(i->resampled_chunk.memblock);
362 if (ret < 0 && i->state == PA_SINK_INPUT_RUNNING && i->underrun)
366 i->state = PA_SINK_INPUT_RUNNING;
367 else if (ret < 0 && i->state == PA_SINK_INPUT_RUNNING)
368 i->state = PA_SINK_INPUT_DRAINED;
371 /* Let's see if we had to apply the volume adjustment
372 * ourselves, or if this can be done by the sink for us */
374 if (do_volume_adj_here)
375 /* We had different channel maps, so we already did the adjustment */
376 pa_cvolume_reset(volume, i->sink->sample_spec.channels);
378 /* We've both the same channel map, so let's have the sink do the adjustment for us*/
382 pa_sink_input_unref(i);
387 void pa_sink_input_drop(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
392 if (i->move_silence > 0) {
396 if (chunk->memblock != i->silence_memblock ||
398 (chunk->memblock && (chunk->length != (i->silence_memblock->length < i->move_silence ? i->silence_memblock->length : i->move_silence))))
403 assert(i->move_silence >= length);
405 i->move_silence -= length;
407 if (i->move_silence <= 0) {
408 assert(i->silence_memblock);
409 pa_memblock_unref(i->silence_memblock);
410 i->silence_memblock = NULL;
418 i->drop(i, chunk, length);
422 assert(i->resampled_chunk.memblock);
423 assert(i->resampled_chunk.length >= length);
425 i->resampled_chunk.index += length;
426 i->resampled_chunk.length -= length;
428 if (i->resampled_chunk.length <= 0) {
429 pa_memblock_unref(i->resampled_chunk.memblock);
430 i->resampled_chunk.memblock = NULL;
431 i->resampled_chunk.index = i->resampled_chunk.length = 0;
435 void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume) {
439 assert(i->sink->core);
441 if (pa_cvolume_equal(&i->volume, volume))
445 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
448 const pa_cvolume * pa_sink_input_get_volume(pa_sink_input *i) {
455 void pa_sink_input_cork(pa_sink_input *i, int b) {
461 assert(i->state != PA_SINK_INPUT_DISCONNECTED);
463 n = i->state == PA_SINK_INPUT_CORKED && !b;
466 i->state = PA_SINK_INPUT_CORKED;
467 else if (i->state == PA_SINK_INPUT_CORKED)
468 i->state = PA_SINK_INPUT_DRAINED;
471 pa_sink_notify(i->sink);
474 void pa_sink_input_set_rate(pa_sink_input *i, uint32_t rate) {
476 assert(i->resampler);
479 if (i->sample_spec.rate == rate)
482 i->sample_spec.rate = rate;
483 pa_resampler_set_input_rate(i->resampler, rate);
485 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
488 void pa_sink_input_set_name(pa_sink_input *i, const char *name) {
492 if (!i->name && !name)
495 if (i->name && name && !strcmp(i->name, name))
499 i->name = pa_xstrdup(name);
501 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
504 pa_resample_method_t pa_sink_input_get_resample_method(pa_sink_input *i) {
509 return i->resample_method;
511 return pa_resampler_get_method(i->resampler);
514 int pa_sink_input_move_to(pa_sink_input *i, pa_sink *dest, int immediately) {
515 pa_resampler *new_resampler = NULL;
516 pa_memblockq *buffer = NULL;
527 if (pa_idxset_size(dest->inputs) >= PA_MAX_INPUTS_PER_SINK) {
528 pa_log_warn("Failed to move sink input: too many inputs per sink.");
533 pa_sample_spec_equal(&origin->sample_spec, &dest->sample_spec) &&
534 pa_channel_map_equal(&origin->channel_map, &dest->channel_map))
536 /* Try to reuse the old resampler if possible */
537 new_resampler = i->resampler;
539 else if ((i->flags & PA_SINK_INPUT_VARIABLE_RATE) ||
540 !pa_sample_spec_equal(&i->sample_spec, &dest->sample_spec) ||
541 !pa_channel_map_equal(&i->channel_map, &dest->channel_map)) {
543 /* Okey, we need a new resampler for the new sink */
545 if (!(new_resampler = pa_resampler_new(
547 &i->sample_spec, &i->channel_map,
548 &dest->sample_spec, &dest->channel_map,
549 i->resample_method))) {
550 pa_log_warn("Unsupported resampling operation.");
556 pa_usec_t old_latency, new_latency;
557 pa_usec_t silence_usec = 0;
559 buffer = pa_memblockq_new(0, MOVE_BUFFER_LENGTH, 0, pa_frame_size(&origin->sample_spec), 0, 0, NULL);
561 /* Let's do a little bit of Voodoo for compensating latency
564 old_latency = pa_sink_get_latency(origin);
565 new_latency = pa_sink_get_latency(dest);
567 /* The already resampled data should go to the old sink */
569 if (old_latency >= new_latency) {
571 /* The latency of the old sink is larger than the latency
572 * of the new sink. Therefore to compensate for the
573 * difference we to play silence on the new one for a
576 silence_usec = old_latency - new_latency;
582 /* The latency of new sink is larger than the latency of
583 * the old sink. Therefore we have to precompute a little
584 * and make sure that this is still played on the old
585 * sink, until we can play the first sample on the new
588 l = pa_usec_to_bytes(new_latency - old_latency, &origin->sample_spec);
590 volume_is_norm = pa_cvolume_is_norm(&i->volume);
597 if (pa_sink_input_peek(i, &chunk, &volume) < 0)
600 n = chunk.length > l ? l : chunk.length;
601 pa_sink_input_drop(i, &chunk, n);
604 if (!volume_is_norm) {
605 pa_memchunk_make_writable(&chunk, 0);
606 pa_volume_memchunk(&chunk, &origin->sample_spec, &volume);
609 if (pa_memblockq_push(buffer, &chunk) < 0) {
610 pa_memblock_unref(chunk.memblock);
614 pa_memblock_unref(chunk.memblock);
619 if (i->resampled_chunk.memblock) {
621 /* There is still some data left in the already resampled
622 * memory block. Hence, let's output it on the old sink
623 * and sleep so long on the new sink */
625 pa_memblockq_push(buffer, &i->resampled_chunk);
626 silence_usec += pa_bytes_to_usec(i->resampled_chunk.length, &origin->sample_spec);
629 /* Calculate the new sleeping time */
630 i->move_silence = pa_usec_to_bytes(
631 pa_bytes_to_usec(i->move_silence, &i->sample_spec) +
636 /* Okey, let's move it */
637 pa_idxset_remove_by_data(origin->inputs, i, NULL);
638 pa_idxset_put(dest->inputs, i, NULL);
641 /* Replace resampler */
642 if (new_resampler != i->resampler) {
644 pa_resampler_free(i->resampler);
645 i->resampler = new_resampler;
647 /* if the resampler changed, the silence memblock is
648 * probably invalid now, too */
649 if (i->silence_memblock) {
650 pa_memblock_unref(i->silence_memblock);
651 i->silence_memblock = NULL;
655 /* Dump already resampled data */
656 if (i->resampled_chunk.memblock) {
657 pa_memblock_unref(i->resampled_chunk.memblock);
658 i->resampled_chunk.memblock = NULL;
659 i->resampled_chunk.index = i->resampled_chunk.length = 0;
662 /* Notify everyone */
663 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
664 pa_sink_notify(i->sink);
666 /* Ok, no let's feed the precomputed buffer to the old sink */
668 pa_play_memblockq(origin, "Ghost Stream", &origin->sample_spec, &origin->channel_map, buffer, NULL);