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) {
299 /* We have just been moved and shall play some silence for a
300 * while until the old sink has drained its playback buffer */
302 if (!i
->silence_memblock
)
303 i
->silence_memblock
= pa_silence_memblock_new(i
->sink
->core
->mempool
, &i
->sink
->sample_spec
, SILENCE_BUFFER_LENGTH
);
305 chunk
->memblock
= pa_memblock_ref(i
->silence_memblock
);
307 l
= pa_memblock_get_length(chunk
->memblock
);
308 chunk
->length
= i
->move_silence
< l
? i
->move_silence
: l
;
311 do_volume_adj_here
= 1;
316 do_volume_adj_here
= 0;
317 ret
= i
->peek(i
, chunk
);
321 do_volume_adj_here
= !pa_channel_map_equal(&i
->channel_map
, &i
->sink
->channel_map
);
322 volume_is_norm
= pa_cvolume_is_norm(&i
->volume
);
324 while (!i
->resampled_chunk
.memblock
) {
328 if ((ret
= i
->peek(i
, &tchunk
)) < 0)
331 assert(tchunk
.length
);
333 l
= pa_resampler_request(i
->resampler
, CONVERT_BUFFER_LENGTH
);
335 if (l
> tchunk
.length
)
338 i
->drop(i
, &tchunk
, l
);
341 /* It might be necessary to adjust the volume here */
342 if (do_volume_adj_here
&& !volume_is_norm
) {
343 pa_memchunk_make_writable(&tchunk
, 0);
344 pa_volume_memchunk(&tchunk
, &i
->sample_spec
, &i
->volume
);
347 pa_resampler_run(i
->resampler
, &tchunk
, &i
->resampled_chunk
);
348 pa_memblock_unref(tchunk
.memblock
);
351 assert(i
->resampled_chunk
.memblock
);
352 assert(i
->resampled_chunk
.length
);
354 *chunk
= i
->resampled_chunk
;
355 pa_memblock_ref(i
->resampled_chunk
.memblock
);
361 if (ret
< 0 && i
->state
== PA_SINK_INPUT_RUNNING
&& i
->underrun
)
365 i
->state
= PA_SINK_INPUT_RUNNING
;
366 else if (ret
< 0 && i
->state
== PA_SINK_INPUT_RUNNING
)
367 i
->state
= PA_SINK_INPUT_DRAINED
;
370 /* Let's see if we had to apply the volume adjustment
371 * ourselves, or if this can be done by the sink for us */
373 if (do_volume_adj_here
)
374 /* We had different channel maps, so we already did the adjustment */
375 pa_cvolume_reset(volume
, i
->sink
->sample_spec
.channels
);
377 /* We've both the same channel map, so let's have the sink do the adjustment for us*/
381 pa_sink_input_unref(i
);
386 void pa_sink_input_drop(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
391 if (i
->move_silence
> 0) {
396 l
= pa_memblock_get_length(i
->silence_memblock
);
398 if (chunk
->memblock
!= i
->silence_memblock
||
400 (chunk
->memblock
&& (chunk
->length
!= (l
< i
->move_silence
? l
: i
->move_silence
))))
405 assert(i
->move_silence
>= length
);
407 i
->move_silence
-= length
;
409 if (i
->move_silence
<= 0) {
410 assert(i
->silence_memblock
);
411 pa_memblock_unref(i
->silence_memblock
);
412 i
->silence_memblock
= NULL
;
420 i
->drop(i
, chunk
, length
);
424 assert(i
->resampled_chunk
.memblock
);
425 assert(i
->resampled_chunk
.length
>= length
);
427 i
->resampled_chunk
.index
+= length
;
428 i
->resampled_chunk
.length
-= length
;
430 if (i
->resampled_chunk
.length
<= 0) {
431 pa_memblock_unref(i
->resampled_chunk
.memblock
);
432 i
->resampled_chunk
.memblock
= NULL
;
433 i
->resampled_chunk
.index
= i
->resampled_chunk
.length
= 0;
437 void pa_sink_input_set_volume(pa_sink_input
*i
, const pa_cvolume
*volume
) {
441 assert(i
->sink
->core
);
443 if (pa_cvolume_equal(&i
->volume
, volume
))
447 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
450 const pa_cvolume
* pa_sink_input_get_volume(pa_sink_input
*i
) {
457 void pa_sink_input_cork(pa_sink_input
*i
, int b
) {
463 assert(i
->state
!= PA_SINK_INPUT_DISCONNECTED
);
465 n
= i
->state
== PA_SINK_INPUT_CORKED
&& !b
;
468 i
->state
= PA_SINK_INPUT_CORKED
;
469 else if (i
->state
== PA_SINK_INPUT_CORKED
)
470 i
->state
= PA_SINK_INPUT_DRAINED
;
473 pa_sink_notify(i
->sink
);
476 void pa_sink_input_set_rate(pa_sink_input
*i
, uint32_t rate
) {
478 assert(i
->resampler
);
481 if (i
->sample_spec
.rate
== rate
)
484 i
->sample_spec
.rate
= rate
;
485 pa_resampler_set_input_rate(i
->resampler
, rate
);
487 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
490 void pa_sink_input_set_name(pa_sink_input
*i
, const char *name
) {
494 if (!i
->name
&& !name
)
497 if (i
->name
&& name
&& !strcmp(i
->name
, name
))
501 i
->name
= pa_xstrdup(name
);
503 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
506 pa_resample_method_t
pa_sink_input_get_resample_method(pa_sink_input
*i
) {
511 return i
->resample_method
;
513 return pa_resampler_get_method(i
->resampler
);
516 int pa_sink_input_move_to(pa_sink_input
*i
, pa_sink
*dest
, int immediately
) {
517 pa_resampler
*new_resampler
= NULL
;
518 pa_memblockq
*buffer
= NULL
;
529 if (pa_idxset_size(dest
->inputs
) >= PA_MAX_INPUTS_PER_SINK
) {
530 pa_log_warn("Failed to move sink input: too many inputs per sink.");
535 pa_sample_spec_equal(&origin
->sample_spec
, &dest
->sample_spec
) &&
536 pa_channel_map_equal(&origin
->channel_map
, &dest
->channel_map
))
538 /* Try to reuse the old resampler if possible */
539 new_resampler
= i
->resampler
;
541 else if ((i
->flags
& PA_SINK_INPUT_VARIABLE_RATE
) ||
542 !pa_sample_spec_equal(&i
->sample_spec
, &dest
->sample_spec
) ||
543 !pa_channel_map_equal(&i
->channel_map
, &dest
->channel_map
)) {
545 /* Okey, we need a new resampler for the new sink */
547 if (!(new_resampler
= pa_resampler_new(
549 &i
->sample_spec
, &i
->channel_map
,
550 &dest
->sample_spec
, &dest
->channel_map
,
551 i
->resample_method
))) {
552 pa_log_warn("Unsupported resampling operation.");
558 pa_usec_t old_latency
, new_latency
;
559 pa_usec_t silence_usec
= 0;
561 buffer
= pa_memblockq_new(0, MOVE_BUFFER_LENGTH
, 0, pa_frame_size(&origin
->sample_spec
), 0, 0, NULL
);
563 /* Let's do a little bit of Voodoo for compensating latency
566 old_latency
= pa_sink_get_latency(origin
);
567 new_latency
= pa_sink_get_latency(dest
);
569 /* The already resampled data should go to the old sink */
571 if (old_latency
>= new_latency
) {
573 /* The latency of the old sink is larger than the latency
574 * of the new sink. Therefore to compensate for the
575 * difference we to play silence on the new one for a
578 silence_usec
= old_latency
- new_latency
;
584 /* The latency of new sink is larger than the latency of
585 * the old sink. Therefore we have to precompute a little
586 * and make sure that this is still played on the old
587 * sink, until we can play the first sample on the new
590 l
= pa_usec_to_bytes(new_latency
- old_latency
, &origin
->sample_spec
);
592 volume_is_norm
= pa_cvolume_is_norm(&i
->volume
);
599 if (pa_sink_input_peek(i
, &chunk
, &volume
) < 0)
602 n
= chunk
.length
> l
? l
: chunk
.length
;
603 pa_sink_input_drop(i
, &chunk
, n
);
606 if (!volume_is_norm
) {
607 pa_memchunk_make_writable(&chunk
, 0);
608 pa_volume_memchunk(&chunk
, &origin
->sample_spec
, &volume
);
611 if (pa_memblockq_push(buffer
, &chunk
) < 0) {
612 pa_memblock_unref(chunk
.memblock
);
616 pa_memblock_unref(chunk
.memblock
);
621 if (i
->resampled_chunk
.memblock
) {
623 /* There is still some data left in the already resampled
624 * memory block. Hence, let's output it on the old sink
625 * and sleep so long on the new sink */
627 pa_memblockq_push(buffer
, &i
->resampled_chunk
);
628 silence_usec
+= pa_bytes_to_usec(i
->resampled_chunk
.length
, &origin
->sample_spec
);
631 /* Calculate the new sleeping time */
632 i
->move_silence
= pa_usec_to_bytes(
633 pa_bytes_to_usec(i
->move_silence
, &i
->sample_spec
) +
638 /* Okey, let's move it */
639 pa_idxset_remove_by_data(origin
->inputs
, i
, NULL
);
640 pa_idxset_put(dest
->inputs
, i
, NULL
);
643 /* Replace resampler */
644 if (new_resampler
!= i
->resampler
) {
646 pa_resampler_free(i
->resampler
);
647 i
->resampler
= new_resampler
;
649 /* if the resampler changed, the silence memblock is
650 * probably invalid now, too */
651 if (i
->silence_memblock
) {
652 pa_memblock_unref(i
->silence_memblock
);
653 i
->silence_memblock
= NULL
;
657 /* Dump already resampled data */
658 if (i
->resampled_chunk
.memblock
) {
659 pa_memblock_unref(i
->resampled_chunk
.memblock
);
660 i
->resampled_chunk
.memblock
= NULL
;
661 i
->resampled_chunk
.index
= i
->resampled_chunk
.length
= 0;
664 /* Notify everyone */
665 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
666 pa_sink_notify(i
->sink
);
668 /* Ok, no let's feed the precomputed buffer to the old sink */
670 pa_play_memblockq(origin
, "Ghost Stream", &origin
->sample_spec
, &origin
->channel_map
, buffer
, NULL
);