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>
39 #include "sink-input.h"
41 #define CONVERT_BUFFER_LENGTH 4096
42 #define MOVE_BUFFER_LENGTH (1024*1024)
43 #define SILENCE_BUFFER_LENGTH (64*1024)
45 #define CHECK_VALIDITY_RETURN_NULL(condition) \
51 pa_sink_input
* pa_sink_input_new(
55 const pa_sample_spec
*spec
,
56 const pa_channel_map
*map
,
57 const pa_cvolume
*volume
,
59 int resample_method
) {
62 pa_resampler
*resampler
= NULL
;
70 assert(s
->state
== PA_SINK_RUNNING
);
72 CHECK_VALIDITY_RETURN_NULL(pa_sample_spec_valid(spec
));
75 map
= pa_channel_map_init_auto(&tmap
, spec
->channels
, PA_CHANNEL_MAP_DEFAULT
);
77 volume
= pa_cvolume_reset(&tvol
, spec
->channels
);
79 CHECK_VALIDITY_RETURN_NULL(map
&& pa_channel_map_valid(map
));
80 CHECK_VALIDITY_RETURN_NULL(volume
&& pa_cvolume_valid(volume
));
81 CHECK_VALIDITY_RETURN_NULL(map
->channels
== spec
->channels
);
82 CHECK_VALIDITY_RETURN_NULL(volume
->channels
== spec
->channels
);
83 CHECK_VALIDITY_RETURN_NULL(!driver
|| pa_utf8_valid(driver
));
84 CHECK_VALIDITY_RETURN_NULL(pa_utf8_valid(name
));
86 if (pa_idxset_size(s
->inputs
) >= PA_MAX_INPUTS_PER_SINK
) {
87 pa_log_warn(__FILE__
": Failed to create sink input: too many inputs per sink.");
91 if (resample_method
== PA_RESAMPLER_INVALID
)
92 resample_method
= s
->core
->resample_method
;
94 if (variable_rate
|| !pa_sample_spec_equal(spec
, &s
->sample_spec
) || !pa_channel_map_equal(map
, &s
->channel_map
))
95 if (!(resampler
= pa_resampler_new(spec
, map
, &s
->sample_spec
, &s
->channel_map
, s
->core
->memblock_stat
, resample_method
))) {
96 pa_log_warn(__FILE__
": Unsupported resampling operation.");
100 i
= pa_xnew(pa_sink_input
, 1);
102 i
->state
= PA_SINK_INPUT_DRAINED
;
103 i
->name
= pa_xstrdup(name
);
104 i
->driver
= pa_xstrdup(driver
);
109 i
->sample_spec
= *spec
;
110 i
->channel_map
= *map
;
116 i
->get_latency
= NULL
;
121 pa_memchunk_reset(&i
->resampled_chunk
);
122 i
->resampler
= resampler
;
123 i
->resample_method
= resample_method
;
124 i
->variable_rate
= variable_rate
;
126 i
->silence_memblock
= NULL
;
129 r
= pa_idxset_put(s
->core
->sink_inputs
, i
, &i
->index
);
130 assert(r
== 0 && i
->index
!= PA_IDXSET_INVALID
);
131 r
= pa_idxset_put(s
->inputs
, i
, NULL
);
134 pa_sample_spec_snprint(st
, sizeof(st
), spec
);
135 pa_log_info(__FILE__
": created %u \"%s\" on %u with sample spec \"%s\"", i
->index
, i
->name
, s
->index
, st
);
137 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
, i
->index
);
139 /* We do not call pa_sink_notify() here, because the virtual
140 * functions have not yet been initialized */
145 void pa_sink_input_disconnect(pa_sink_input
*i
) {
147 assert(i
->state
!= PA_SINK_INPUT_DISCONNECTED
);
149 assert(i
->sink
->core
);
151 pa_idxset_remove_by_data(i
->sink
->core
->sink_inputs
, i
, NULL
);
152 pa_idxset_remove_by_data(i
->sink
->inputs
, i
, NULL
);
154 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, i
->index
);
160 i
->get_latency
= NULL
;
163 i
->state
= PA_SINK_INPUT_DISCONNECTED
;
166 static void sink_input_free(pa_sink_input
* i
) {
169 if (i
->state
!= PA_SINK_INPUT_DISCONNECTED
)
170 pa_sink_input_disconnect(i
);
172 pa_log_info(__FILE__
": freed %u \"%s\"", i
->index
, i
->name
);
174 if (i
->resampled_chunk
.memblock
)
175 pa_memblock_unref(i
->resampled_chunk
.memblock
);
178 pa_resampler_free(i
->resampler
);
180 if (i
->silence_memblock
)
181 pa_memblock_unref(i
->silence_memblock
);
188 void pa_sink_input_unref(pa_sink_input
*i
) {
196 pa_sink_input
* pa_sink_input_ref(pa_sink_input
*i
) {
204 void pa_sink_input_kill(pa_sink_input
*i
) {
212 pa_usec_t
pa_sink_input_get_latency(pa_sink_input
*i
) {
219 r
+= i
->get_latency(i
);
221 if (i
->resampled_chunk
.memblock
)
222 r
+= pa_bytes_to_usec(i
->resampled_chunk
.length
, &i
->sink
->sample_spec
);
225 r
+= pa_bytes_to_usec(i
->move_silence
, &i
->sink
->sample_spec
);
230 int pa_sink_input_peek(pa_sink_input
*i
, pa_memchunk
*chunk
, pa_cvolume
*volume
) {
232 int do_volume_adj_here
;
240 pa_sink_input_ref(i
);
242 if (!i
->peek
|| !i
->drop
|| i
->state
== PA_SINK_INPUT_CORKED
)
245 assert(i
->state
== PA_SINK_INPUT_RUNNING
|| i
->state
== PA_SINK_INPUT_DRAINED
);
247 if (i
->move_silence
> 0) {
249 /* We have just been moved and shall play some silence for a
250 * while until the old sink has drained its playback buffer */
252 if (!i
->silence_memblock
)
253 i
->silence_memblock
= pa_silence_memblock_new(&i
->sink
->sample_spec
, SILENCE_BUFFER_LENGTH
, i
->sink
->core
->memblock_stat
);
255 chunk
->memblock
= pa_memblock_ref(i
->silence_memblock
);
257 chunk
->length
= i
->move_silence
< chunk
->memblock
->length
? i
->move_silence
: chunk
->memblock
->length
;
260 do_volume_adj_here
= 1;
265 do_volume_adj_here
= 0;
266 ret
= i
->peek(i
, chunk
);
270 do_volume_adj_here
= !pa_channel_map_equal(&i
->channel_map
, &i
->sink
->channel_map
);
271 volume_is_norm
= pa_cvolume_is_norm(&i
->volume
);
273 while (!i
->resampled_chunk
.memblock
) {
277 if ((ret
= i
->peek(i
, &tchunk
)) < 0)
280 assert(tchunk
.length
);
282 l
= pa_resampler_request(i
->resampler
, CONVERT_BUFFER_LENGTH
);
284 if (l
> tchunk
.length
)
287 i
->drop(i
, &tchunk
, l
);
290 /* It might be necessary to adjust the volume here */
291 if (do_volume_adj_here
&& !volume_is_norm
) {
292 pa_memchunk_make_writable(&tchunk
, i
->sink
->core
->memblock_stat
, 0);
293 pa_volume_memchunk(&tchunk
, &i
->sample_spec
, &i
->volume
);
296 pa_resampler_run(i
->resampler
, &tchunk
, &i
->resampled_chunk
);
297 pa_memblock_unref(tchunk
.memblock
);
300 assert(i
->resampled_chunk
.memblock
);
301 assert(i
->resampled_chunk
.length
);
303 *chunk
= i
->resampled_chunk
;
304 pa_memblock_ref(i
->resampled_chunk
.memblock
);
310 if (ret
< 0 && i
->state
== PA_SINK_INPUT_RUNNING
&& i
->underrun
)
314 i
->state
= PA_SINK_INPUT_RUNNING
;
315 else if (ret
< 0 && i
->state
== PA_SINK_INPUT_RUNNING
)
316 i
->state
= PA_SINK_INPUT_DRAINED
;
319 /* Let's see if we had to apply the volume adjustment
320 * ourselves, or if this can be done by the sink for us */
322 if (do_volume_adj_here
)
323 /* We had different channel maps, so we already did the adjustment */
324 pa_cvolume_reset(volume
, i
->sink
->sample_spec
.channels
);
326 /* We've both the same channel map, so let's have the sink do the adjustment for us*/
330 pa_sink_input_unref(i
);
335 void pa_sink_input_drop(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
340 if (i
->move_silence
> 0) {
344 if (chunk
->memblock
!= i
->silence_memblock
||
346 (chunk
->memblock
&& (chunk
->length
!= (i
->silence_memblock
->length
< i
->move_silence
? i
->silence_memblock
->length
: i
->move_silence
))))
351 assert(i
->move_silence
>= length
);
353 i
->move_silence
-= length
;
355 if (i
->move_silence
<= 0) {
356 assert(i
->silence_memblock
);
357 pa_memblock_unref(i
->silence_memblock
);
358 i
->silence_memblock
= NULL
;
366 i
->drop(i
, chunk
, length
);
370 assert(i
->resampled_chunk
.memblock
);
371 assert(i
->resampled_chunk
.length
>= length
);
373 i
->resampled_chunk
.index
+= length
;
374 i
->resampled_chunk
.length
-= length
;
376 if (i
->resampled_chunk
.length
<= 0) {
377 pa_memblock_unref(i
->resampled_chunk
.memblock
);
378 i
->resampled_chunk
.memblock
= NULL
;
379 i
->resampled_chunk
.index
= i
->resampled_chunk
.length
= 0;
383 void pa_sink_input_set_volume(pa_sink_input
*i
, const pa_cvolume
*volume
) {
387 assert(i
->sink
->core
);
389 if (pa_cvolume_equal(&i
->volume
, volume
))
393 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
396 const pa_cvolume
* pa_sink_input_get_volume(pa_sink_input
*i
) {
403 void pa_sink_input_cork(pa_sink_input
*i
, int b
) {
409 assert(i
->state
!= PA_SINK_INPUT_DISCONNECTED
);
411 n
= i
->state
== PA_SINK_INPUT_CORKED
&& !b
;
414 i
->state
= PA_SINK_INPUT_CORKED
;
415 else if (i
->state
== PA_SINK_INPUT_CORKED
)
416 i
->state
= PA_SINK_INPUT_DRAINED
;
419 pa_sink_notify(i
->sink
);
422 void pa_sink_input_set_rate(pa_sink_input
*i
, uint32_t rate
) {
424 assert(i
->resampler
);
427 if (i
->sample_spec
.rate
== rate
)
430 i
->sample_spec
.rate
= rate
;
431 pa_resampler_set_input_rate(i
->resampler
, rate
);
433 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
436 void pa_sink_input_set_name(pa_sink_input
*i
, const char *name
) {
440 if (!i
->name
&& !name
)
443 if (i
->name
&& name
&& !strcmp(i
->name
, name
))
447 i
->name
= pa_xstrdup(name
);
449 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
452 pa_resample_method_t
pa_sink_input_get_resample_method(pa_sink_input
*i
) {
457 return i
->resample_method
;
459 return pa_resampler_get_method(i
->resampler
);
462 int pa_sink_input_move_to(pa_sink_input
*i
, pa_sink
*dest
, int immediately
) {
463 pa_resampler
*new_resampler
= NULL
;
464 pa_memblockq
*buffer
= NULL
;
475 if (pa_idxset_size(dest
->inputs
) >= PA_MAX_INPUTS_PER_SINK
) {
476 pa_log_warn(__FILE__
": Failed to move sink input: too many inputs per sink.");
481 pa_sample_spec_equal(&origin
->sample_spec
, &dest
->sample_spec
) &&
482 pa_channel_map_equal(&origin
->channel_map
, &dest
->channel_map
))
484 /* Try to reuse the old resampler if possible */
485 new_resampler
= i
->resampler
;
487 else if (i
->variable_rate
||
488 !pa_sample_spec_equal(&i
->sample_spec
, &dest
->sample_spec
) ||
489 !pa_channel_map_equal(&i
->channel_map
, &dest
->channel_map
)) {
491 /* Okey, we need a new resampler for the new sink */
493 if (!(new_resampler
= pa_resampler_new(
494 &i
->sample_spec
, &i
->channel_map
,
495 &dest
->sample_spec
, &dest
->channel_map
,
496 dest
->core
->memblock_stat
,
497 i
->resample_method
))) {
498 pa_log_warn(__FILE__
": Unsupported resampling operation.");
504 pa_usec_t old_latency
, new_latency
;
505 pa_usec_t silence_usec
= 0;
507 buffer
= pa_memblockq_new(0, MOVE_BUFFER_LENGTH
, 0, pa_frame_size(&origin
->sample_spec
), 0, 0, NULL
, NULL
);
509 /* Let's do a little bit of Voodoo for compensating latency
512 old_latency
= pa_sink_get_latency(origin
);
513 new_latency
= pa_sink_get_latency(dest
);
515 /* The already resampled data should go to the old sink */
517 if (old_latency
>= new_latency
) {
519 /* The latency of the old sink is larger than the latency
520 * of the new sink. Therefore to compensate for the
521 * difference we to play silence on the new one for a
524 silence_usec
= old_latency
- new_latency
;
530 /* The latency of new sink is larger than the latency of
531 * the old sink. Therefore we have to precompute a little
532 * and make sure that this is still played on the old
533 * sink, until we can play the first sample on the new
536 l
= pa_usec_to_bytes(new_latency
- old_latency
, &origin
->sample_spec
);
538 volume_is_norm
= pa_cvolume_is_norm(&i
->volume
);
545 if (pa_sink_input_peek(i
, &chunk
, &volume
) < 0)
548 n
= chunk
.length
> l
? l
: chunk
.length
;
549 pa_sink_input_drop(i
, &chunk
, n
);
552 if (!volume_is_norm
) {
553 pa_memchunk_make_writable(&chunk
, origin
->core
->memblock_stat
, 0);
554 pa_volume_memchunk(&chunk
, &origin
->sample_spec
, &volume
);
557 if (pa_memblockq_push(buffer
, &chunk
) < 0) {
558 pa_memblock_unref(chunk
.memblock
);
562 pa_memblock_unref(chunk
.memblock
);
567 if (i
->resampled_chunk
.memblock
) {
569 /* There is still some data left in the already resampled
570 * memory block. Hence, let's output it on the old sink
571 * and sleep so long on the new sink */
573 pa_memblockq_push(buffer
, &i
->resampled_chunk
);
574 silence_usec
+= pa_bytes_to_usec(i
->resampled_chunk
.length
, &origin
->sample_spec
);
577 /* Calculate the new sleeping time */
578 i
->move_silence
= pa_usec_to_bytes(
579 pa_bytes_to_usec(i
->move_silence
, &i
->sample_spec
) +
584 /* Okey, let's move it */
585 pa_idxset_remove_by_data(origin
->inputs
, i
, NULL
);
586 pa_idxset_put(dest
->inputs
, i
, NULL
);
589 /* Replace resampler */
590 if (new_resampler
!= i
->resampler
) {
592 pa_resampler_free(i
->resampler
);
593 i
->resampler
= new_resampler
;
595 /* if the resampler changed, the silence memblock is
596 * probably invalid now, too */
597 if (i
->silence_memblock
) {
598 pa_memblock_unref(i
->silence_memblock
);
599 i
->silence_memblock
= NULL
;
603 /* Dump already resampled data */
604 if (i
->resampled_chunk
.memblock
) {
605 pa_memblock_unref(i
->resampled_chunk
.memblock
);
606 i
->resampled_chunk
.memblock
= NULL
;
607 i
->resampled_chunk
.index
= i
->resampled_chunk
.length
= 0;
610 /* Notify everyone */
611 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
612 pa_sink_notify(i
->sink
);
614 /* Ok, no let's feed the precomputed buffer to the old sink */
616 pa_play_memblockq(origin
, "Ghost Stream", &origin
->sample_spec
, &origin
->channel_map
, buffer
, NULL
);