4 This file is part of PulseAudio.
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.
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.
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
31 #include <pulse/utf8.h>
32 #include <pulse/xmalloc.h>
34 #include <pulsecore/sample-util.h>
35 #include <pulsecore/core-subscribe.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/play-memblockq.h>
38 #include <pulsecore/namereg.h>
40 #include "sink-input.h"
42 #define CONVERT_BUFFER_LENGTH 4096
43 #define MOVE_BUFFER_LENGTH (1024*1024)
44 #define SILENCE_BUFFER_LENGTH (64*1024)
46 #define CHECK_VALIDITY_RETURN_NULL(condition) \
52 pa_sink_input_new_data
* pa_sink_input_new_data_init(pa_sink_input_new_data
*data
) {
55 memset(data
, 0, sizeof(*data
));
56 data
->resample_method
= PA_RESAMPLER_INVALID
;
60 void pa_sink_input_new_data_set_channel_map(pa_sink_input_new_data
*data
, const pa_channel_map
*map
) {
63 if ((data
->channel_map_is_set
= !!map
))
64 data
->channel_map
= *map
;
67 void pa_sink_input_new_data_set_volume(pa_sink_input_new_data
*data
, const pa_cvolume
*volume
) {
70 if ((data
->volume_is_set
= !!volume
))
71 data
->volume
= *volume
;
74 void pa_sink_input_new_data_set_sample_spec(pa_sink_input_new_data
*data
, const pa_sample_spec
*spec
) {
77 if ((data
->sample_spec_is_set
= !!spec
))
78 data
->sample_spec
= *spec
;
81 pa_sink_input
* pa_sink_input_new(
83 pa_sink_input_new_data
*data
,
84 pa_sink_input_flags_t flags
) {
87 pa_resampler
*resampler
= NULL
;
89 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
94 if (!(flags
& PA_SINK_INPUT_NO_HOOKS
))
95 if (pa_hook_fire(&core
->hook_sink_input_new
, data
) < 0)
98 CHECK_VALIDITY_RETURN_NULL(!data
->driver
|| pa_utf8_valid(data
->driver
));
99 CHECK_VALIDITY_RETURN_NULL(!data
->name
|| pa_utf8_valid(data
->name
));
102 data
->sink
= pa_namereg_get(core
, NULL
, PA_NAMEREG_SINK
, 1);
104 CHECK_VALIDITY_RETURN_NULL(data
->sink
);
105 CHECK_VALIDITY_RETURN_NULL(data
->sink
->state
== PA_SINK_RUNNING
);
107 if (!data
->sample_spec_is_set
)
108 data
->sample_spec
= data
->sink
->sample_spec
;
110 CHECK_VALIDITY_RETURN_NULL(pa_sample_spec_valid(&data
->sample_spec
));
112 if (!data
->channel_map_is_set
)
113 pa_channel_map_init_auto(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
);
115 CHECK_VALIDITY_RETURN_NULL(pa_channel_map_valid(&data
->channel_map
));
116 CHECK_VALIDITY_RETURN_NULL(data
->channel_map
.channels
== data
->sample_spec
.channels
);
118 if (!data
->volume_is_set
)
119 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
121 CHECK_VALIDITY_RETURN_NULL(pa_cvolume_valid(&data
->volume
));
122 CHECK_VALIDITY_RETURN_NULL(data
->volume
.channels
== data
->sample_spec
.channels
);
124 if (data
->resample_method
== PA_RESAMPLER_INVALID
)
125 data
->resample_method
= core
->resample_method
;
127 CHECK_VALIDITY_RETURN_NULL(data
->resample_method
< PA_RESAMPLER_MAX
);
129 if (pa_idxset_size(data
->sink
->inputs
) >= PA_MAX_INPUTS_PER_SINK
) {
130 pa_log_warn("Failed to create sink input: too many inputs per sink.");
134 if ((flags
& PA_SINK_INPUT_VARIABLE_RATE
) ||
135 !pa_sample_spec_equal(&data
->sample_spec
, &data
->sink
->sample_spec
) ||
136 !pa_channel_map_equal(&data
->channel_map
, &data
->sink
->channel_map
))
138 if (!(resampler
= pa_resampler_new(
140 &data
->sample_spec
, &data
->channel_map
,
141 &data
->sink
->sample_spec
, &data
->sink
->channel_map
,
142 data
->resample_method
))) {
143 pa_log_warn("Unsupported resampling operation.");
147 i
= pa_xnew(pa_sink_input
, 1);
149 i
->state
= PA_SINK_INPUT_DRAINED
;
151 i
->name
= pa_xstrdup(data
->name
);
152 i
->driver
= pa_xstrdup(data
->driver
);
153 i
->module
= data
->module
;
154 i
->sink
= data
->sink
;
155 i
->client
= data
->client
;
157 i
->sample_spec
= data
->sample_spec
;
158 i
->channel_map
= data
->channel_map
;
159 i
->volume
= data
->volume
;
164 i
->get_latency
= NULL
;
170 pa_memchunk_reset(&i
->resampled_chunk
);
171 i
->resampler
= resampler
;
172 i
->resample_method
= data
->resample_method
;
173 i
->silence_memblock
= NULL
;
175 r
= pa_idxset_put(core
->sink_inputs
, i
, &i
->index
);
177 r
= pa_idxset_put(i
->sink
->inputs
, i
, NULL
);
180 pa_log_info("created %u \"%s\" on %s with sample spec %s",
184 pa_sample_spec_snprint(st
, sizeof(st
), &i
->sample_spec
));
186 pa_subscription_post(core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
, i
->index
);
188 /* We do not call pa_sink_notify() here, because the virtual
189 * functions have not yet been initialized */
194 void pa_sink_input_disconnect(pa_sink_input
*i
) {
196 assert(i
->state
!= PA_SINK_INPUT_DISCONNECTED
);
198 assert(i
->sink
->core
);
200 pa_idxset_remove_by_data(i
->sink
->core
->sink_inputs
, i
, NULL
);
201 pa_idxset_remove_by_data(i
->sink
->inputs
, i
, NULL
);
203 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, i
->index
);
209 i
->get_latency
= NULL
;
212 i
->state
= PA_SINK_INPUT_DISCONNECTED
;
215 static void sink_input_free(pa_sink_input
* i
) {
218 if (i
->state
!= PA_SINK_INPUT_DISCONNECTED
)
219 pa_sink_input_disconnect(i
);
221 pa_log_info("freed %u \"%s\"", i
->index
, i
->name
);
223 if (i
->resampled_chunk
.memblock
)
224 pa_memblock_unref(i
->resampled_chunk
.memblock
);
227 pa_resampler_free(i
->resampler
);
229 if (i
->silence_memblock
)
230 pa_memblock_unref(i
->silence_memblock
);
237 void pa_sink_input_unref(pa_sink_input
*i
) {
245 pa_sink_input
* pa_sink_input_ref(pa_sink_input
*i
) {
253 void pa_sink_input_kill(pa_sink_input
*i
) {
261 pa_usec_t
pa_sink_input_get_latency(pa_sink_input
*i
) {
268 r
+= i
->get_latency(i
);
270 if (i
->resampled_chunk
.memblock
)
271 r
+= pa_bytes_to_usec(i
->resampled_chunk
.length
, &i
->sink
->sample_spec
);
274 r
+= pa_bytes_to_usec(i
->move_silence
, &i
->sink
->sample_spec
);
279 int pa_sink_input_peek(pa_sink_input
*i
, pa_memchunk
*chunk
, pa_cvolume
*volume
) {
281 int do_volume_adj_here
;
289 pa_sink_input_ref(i
);
291 if (!i
->peek
|| !i
->drop
|| i
->state
== PA_SINK_INPUT_CORKED
)
294 assert(i
->state
== PA_SINK_INPUT_RUNNING
|| i
->state
== PA_SINK_INPUT_DRAINED
);
296 if (i
->move_silence
> 0) {
298 /* We have just been moved and shall play some silence for a
299 * while until the old sink has drained its playback buffer */
301 if (!i
->silence_memblock
)
302 i
->silence_memblock
= pa_silence_memblock_new(i
->sink
->core
->mempool
, &i
->sink
->sample_spec
, SILENCE_BUFFER_LENGTH
);
304 chunk
->memblock
= pa_memblock_ref(i
->silence_memblock
);
306 chunk
->length
= i
->move_silence
< chunk
->memblock
->length
? i
->move_silence
: chunk
->memblock
->length
;
309 do_volume_adj_here
= 1;
314 do_volume_adj_here
= 0;
315 ret
= i
->peek(i
, chunk
);
319 do_volume_adj_here
= !pa_channel_map_equal(&i
->channel_map
, &i
->sink
->channel_map
);
320 volume_is_norm
= pa_cvolume_is_norm(&i
->volume
);
322 while (!i
->resampled_chunk
.memblock
) {
326 if ((ret
= i
->peek(i
, &tchunk
)) < 0)
329 assert(tchunk
.length
);
331 l
= pa_resampler_request(i
->resampler
, CONVERT_BUFFER_LENGTH
);
333 if (l
> tchunk
.length
)
336 i
->drop(i
, &tchunk
, l
);
339 /* It might be necessary to adjust the volume here */
340 if (do_volume_adj_here
&& !volume_is_norm
) {
341 pa_memchunk_make_writable(&tchunk
, 0);
342 pa_volume_memchunk(&tchunk
, &i
->sample_spec
, &i
->volume
);
345 pa_resampler_run(i
->resampler
, &tchunk
, &i
->resampled_chunk
);
346 pa_memblock_unref(tchunk
.memblock
);
349 assert(i
->resampled_chunk
.memblock
);
350 assert(i
->resampled_chunk
.length
);
352 *chunk
= i
->resampled_chunk
;
353 pa_memblock_ref(i
->resampled_chunk
.memblock
);
359 if (ret
< 0 && i
->state
== PA_SINK_INPUT_RUNNING
&& i
->underrun
)
363 i
->state
= PA_SINK_INPUT_RUNNING
;
364 else if (ret
< 0 && i
->state
== PA_SINK_INPUT_RUNNING
)
365 i
->state
= PA_SINK_INPUT_DRAINED
;
368 /* Let's see if we had to apply the volume adjustment
369 * ourselves, or if this can be done by the sink for us */
371 if (do_volume_adj_here
)
372 /* We had different channel maps, so we already did the adjustment */
373 pa_cvolume_reset(volume
, i
->sink
->sample_spec
.channels
);
375 /* We've both the same channel map, so let's have the sink do the adjustment for us*/
379 pa_sink_input_unref(i
);
384 void pa_sink_input_drop(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
389 if (i
->move_silence
> 0) {
393 if (chunk
->memblock
!= i
->silence_memblock
||
395 (chunk
->memblock
&& (chunk
->length
!= (i
->silence_memblock
->length
< i
->move_silence
? i
->silence_memblock
->length
: i
->move_silence
))))
400 assert(i
->move_silence
>= length
);
402 i
->move_silence
-= length
;
404 if (i
->move_silence
<= 0) {
405 assert(i
->silence_memblock
);
406 pa_memblock_unref(i
->silence_memblock
);
407 i
->silence_memblock
= NULL
;
415 i
->drop(i
, chunk
, length
);
419 assert(i
->resampled_chunk
.memblock
);
420 assert(i
->resampled_chunk
.length
>= length
);
422 i
->resampled_chunk
.index
+= length
;
423 i
->resampled_chunk
.length
-= length
;
425 if (i
->resampled_chunk
.length
<= 0) {
426 pa_memblock_unref(i
->resampled_chunk
.memblock
);
427 i
->resampled_chunk
.memblock
= NULL
;
428 i
->resampled_chunk
.index
= i
->resampled_chunk
.length
= 0;
432 void pa_sink_input_set_volume(pa_sink_input
*i
, const pa_cvolume
*volume
) {
436 assert(i
->sink
->core
);
438 if (pa_cvolume_equal(&i
->volume
, volume
))
442 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
445 const pa_cvolume
* pa_sink_input_get_volume(pa_sink_input
*i
) {
452 void pa_sink_input_cork(pa_sink_input
*i
, int b
) {
458 assert(i
->state
!= PA_SINK_INPUT_DISCONNECTED
);
460 n
= i
->state
== PA_SINK_INPUT_CORKED
&& !b
;
463 i
->state
= PA_SINK_INPUT_CORKED
;
464 else if (i
->state
== PA_SINK_INPUT_CORKED
)
465 i
->state
= PA_SINK_INPUT_DRAINED
;
468 pa_sink_notify(i
->sink
);
471 void pa_sink_input_set_rate(pa_sink_input
*i
, uint32_t rate
) {
473 assert(i
->resampler
);
476 if (i
->sample_spec
.rate
== rate
)
479 i
->sample_spec
.rate
= rate
;
480 pa_resampler_set_input_rate(i
->resampler
, rate
);
482 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
485 void pa_sink_input_set_name(pa_sink_input
*i
, const char *name
) {
489 if (!i
->name
&& !name
)
492 if (i
->name
&& name
&& !strcmp(i
->name
, name
))
496 i
->name
= pa_xstrdup(name
);
498 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
501 pa_resample_method_t
pa_sink_input_get_resample_method(pa_sink_input
*i
) {
506 return i
->resample_method
;
508 return pa_resampler_get_method(i
->resampler
);
511 int pa_sink_input_move_to(pa_sink_input
*i
, pa_sink
*dest
, int immediately
) {
512 pa_resampler
*new_resampler
= NULL
;
513 pa_memblockq
*buffer
= NULL
;
524 if (pa_idxset_size(dest
->inputs
) >= PA_MAX_INPUTS_PER_SINK
) {
525 pa_log_warn("Failed to move sink input: too many inputs per sink.");
530 pa_sample_spec_equal(&origin
->sample_spec
, &dest
->sample_spec
) &&
531 pa_channel_map_equal(&origin
->channel_map
, &dest
->channel_map
))
533 /* Try to reuse the old resampler if possible */
534 new_resampler
= i
->resampler
;
536 else if ((i
->flags
& PA_SINK_INPUT_VARIABLE_RATE
) ||
537 !pa_sample_spec_equal(&i
->sample_spec
, &dest
->sample_spec
) ||
538 !pa_channel_map_equal(&i
->channel_map
, &dest
->channel_map
)) {
540 /* Okey, we need a new resampler for the new sink */
542 if (!(new_resampler
= pa_resampler_new(
544 &i
->sample_spec
, &i
->channel_map
,
545 &dest
->sample_spec
, &dest
->channel_map
,
546 i
->resample_method
))) {
547 pa_log_warn("Unsupported resampling operation.");
553 pa_usec_t old_latency
, new_latency
;
554 pa_usec_t silence_usec
= 0;
556 buffer
= pa_memblockq_new(0, MOVE_BUFFER_LENGTH
, 0, pa_frame_size(&origin
->sample_spec
), 0, 0, NULL
);
558 /* Let's do a little bit of Voodoo for compensating latency
561 old_latency
= pa_sink_get_latency(origin
);
562 new_latency
= pa_sink_get_latency(dest
);
564 /* The already resampled data should go to the old sink */
566 if (old_latency
>= new_latency
) {
568 /* The latency of the old sink is larger than the latency
569 * of the new sink. Therefore to compensate for the
570 * difference we to play silence on the new one for a
573 silence_usec
= old_latency
- new_latency
;
579 /* The latency of new sink is larger than the latency of
580 * the old sink. Therefore we have to precompute a little
581 * and make sure that this is still played on the old
582 * sink, until we can play the first sample on the new
585 l
= pa_usec_to_bytes(new_latency
- old_latency
, &origin
->sample_spec
);
587 volume_is_norm
= pa_cvolume_is_norm(&i
->volume
);
594 if (pa_sink_input_peek(i
, &chunk
, &volume
) < 0)
597 n
= chunk
.length
> l
? l
: chunk
.length
;
598 pa_sink_input_drop(i
, &chunk
, n
);
601 if (!volume_is_norm
) {
602 pa_memchunk_make_writable(&chunk
, 0);
603 pa_volume_memchunk(&chunk
, &origin
->sample_spec
, &volume
);
606 if (pa_memblockq_push(buffer
, &chunk
) < 0) {
607 pa_memblock_unref(chunk
.memblock
);
611 pa_memblock_unref(chunk
.memblock
);
616 if (i
->resampled_chunk
.memblock
) {
618 /* There is still some data left in the already resampled
619 * memory block. Hence, let's output it on the old sink
620 * and sleep so long on the new sink */
622 pa_memblockq_push(buffer
, &i
->resampled_chunk
);
623 silence_usec
+= pa_bytes_to_usec(i
->resampled_chunk
.length
, &origin
->sample_spec
);
626 /* Calculate the new sleeping time */
627 i
->move_silence
= pa_usec_to_bytes(
628 pa_bytes_to_usec(i
->move_silence
, &i
->sample_spec
) +
633 /* Okey, let's move it */
634 pa_idxset_remove_by_data(origin
->inputs
, i
, NULL
);
635 pa_idxset_put(dest
->inputs
, i
, NULL
);
638 /* Replace resampler */
639 if (new_resampler
!= i
->resampler
) {
641 pa_resampler_free(i
->resampler
);
642 i
->resampler
= new_resampler
;
644 /* if the resampler changed, the silence memblock is
645 * probably invalid now, too */
646 if (i
->silence_memblock
) {
647 pa_memblock_unref(i
->silence_memblock
);
648 i
->silence_memblock
= NULL
;
652 /* Dump already resampled data */
653 if (i
->resampled_chunk
.memblock
) {
654 pa_memblock_unref(i
->resampled_chunk
.memblock
);
655 i
->resampled_chunk
.memblock
= NULL
;
656 i
->resampled_chunk
.index
= i
->resampled_chunk
.length
= 0;
659 /* Notify everyone */
660 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
661 pa_sink_notify(i
->sink
);
663 /* Ok, no let's feed the precomputed buffer to the old sink */
665 pa_play_memblockq(origin
, "Ghost Stream", &origin
->sample_spec
, &origin
->channel_map
, buffer
, NULL
);