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
33 #include <pulse/utf8.h>
34 #include <pulse/xmalloc.h>
36 #include <pulsecore/sample-util.h>
37 #include <pulsecore/core-subscribe.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/play-memblockq.h>
40 #include <pulsecore/namereg.h>
42 #include "sink-input.h"
44 #define CONVERT_BUFFER_LENGTH (PA_PAGE_SIZE)
45 #define SILENCE_BUFFER_LENGTH (PA_PAGE_SIZE*12)
46 #define MOVE_BUFFER_LENGTH (PA_PAGE_SIZE*256)
48 static PA_DEFINE_CHECK_TYPE(pa_sink_input
, pa_msgobject
);
50 static void sink_input_free(pa_object
*o
);
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
;
61 void pa_sink_input_new_data_set_channel_map(pa_sink_input_new_data
*data
, const pa_channel_map
*map
) {
64 if ((data
->channel_map_is_set
= !!map
))
65 data
->channel_map
= *map
;
68 void pa_sink_input_new_data_set_volume(pa_sink_input_new_data
*data
, const pa_cvolume
*volume
) {
71 if ((data
->volume_is_set
= !!volume
))
72 data
->volume
= *volume
;
75 void pa_sink_input_new_data_set_sample_spec(pa_sink_input_new_data
*data
, const pa_sample_spec
*spec
) {
78 if ((data
->sample_spec_is_set
= !!spec
))
79 data
->sample_spec
= *spec
;
82 void pa_sink_input_new_data_set_muted(pa_sink_input_new_data
*data
, pa_bool_t mute
) {
85 data
->muted_is_set
= TRUE
;
89 pa_sink_input
* pa_sink_input_new(
91 pa_sink_input_new_data
*data
,
92 pa_sink_input_flags_t flags
) {
95 pa_resampler
*resampler
= NULL
;
96 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
101 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_INPUT_NEW
], data
) < 0)
104 pa_return_null_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
));
105 pa_return_null_if_fail(!data
->name
|| pa_utf8_valid(data
->name
));
108 data
->sink
= pa_namereg_get(core
, NULL
, PA_NAMEREG_SINK
, 1);
110 pa_return_null_if_fail(data
->sink
);
111 pa_return_null_if_fail(pa_sink_get_state(data
->sink
) != PA_SINK_UNLINKED
);
112 pa_return_null_if_fail(!data
->sync_base
|| (data
->sync_base
->sink
== data
->sink
&& pa_sink_input_get_state(data
->sync_base
) == PA_SINK_INPUT_CORKED
));
114 if (!data
->sample_spec_is_set
)
115 data
->sample_spec
= data
->sink
->sample_spec
;
117 pa_return_null_if_fail(pa_sample_spec_valid(&data
->sample_spec
));
119 if (!data
->channel_map_is_set
) {
120 if (data
->sink
->channel_map
.channels
== data
->sample_spec
.channels
)
121 data
->channel_map
= data
->sink
->channel_map
;
123 pa_channel_map_init_auto(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
);
126 pa_return_null_if_fail(pa_channel_map_valid(&data
->channel_map
));
127 pa_return_null_if_fail(data
->channel_map
.channels
== data
->sample_spec
.channels
);
129 if (!data
->volume_is_set
)
130 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
132 pa_return_null_if_fail(pa_cvolume_valid(&data
->volume
));
133 pa_return_null_if_fail(data
->volume
.channels
== data
->sample_spec
.channels
);
135 if (!data
->muted_is_set
)
138 if (data
->resample_method
== PA_RESAMPLER_INVALID
)
139 data
->resample_method
= core
->resample_method
;
141 pa_return_null_if_fail(data
->resample_method
< PA_RESAMPLER_MAX
);
143 if (pa_idxset_size(data
->sink
->inputs
) >= PA_MAX_INPUTS_PER_SINK
) {
144 pa_log_warn("Failed to create sink input: too many inputs per sink.");
148 if ((flags
& PA_SINK_INPUT_VARIABLE_RATE
) ||
149 !pa_sample_spec_equal(&data
->sample_spec
, &data
->sink
->sample_spec
) ||
150 !pa_channel_map_equal(&data
->channel_map
, &data
->sink
->channel_map
)) {
152 if (!(resampler
= pa_resampler_new(
154 &data
->sample_spec
, &data
->channel_map
,
155 &data
->sink
->sample_spec
, &data
->sink
->channel_map
,
156 data
->resample_method
,
157 !!(flags
& PA_SINK_INPUT_VARIABLE_RATE
)))) {
158 pa_log_warn("Unsupported resampling operation.");
162 data
->resample_method
= pa_resampler_get_method(resampler
);
165 i
= pa_msgobject_new(pa_sink_input
);
166 i
->parent
.parent
.free
= sink_input_free
;
167 i
->parent
.process_msg
= pa_sink_input_process_msg
;
170 i
->state
= PA_SINK_INPUT_INIT
;
172 i
->name
= pa_xstrdup(data
->name
);
173 i
->driver
= pa_xstrdup(data
->driver
);
174 i
->module
= data
->module
;
175 i
->sink
= data
->sink
;
176 i
->client
= data
->client
;
178 i
->resample_method
= data
->resample_method
;
179 i
->sample_spec
= data
->sample_spec
;
180 i
->channel_map
= data
->channel_map
;
182 i
->volume
= data
->volume
;
183 i
->muted
= data
->muted
;
185 if (data
->sync_base
) {
186 i
->sync_next
= data
->sync_base
->sync_next
;
187 i
->sync_prev
= data
->sync_base
;
189 if (data
->sync_base
->sync_next
)
190 data
->sync_base
->sync_next
->sync_prev
= i
;
191 data
->sync_base
->sync_next
= i
;
193 i
->sync_next
= i
->sync_prev
= NULL
;
198 i
->get_latency
= NULL
;
204 i
->thread_info
.state
= i
->state
;
205 pa_atomic_store(&i
->thread_info
.drained
, 1);
206 i
->thread_info
.sample_spec
= i
->sample_spec
;
207 i
->thread_info
.silence_memblock
= NULL
;
208 i
->thread_info
.move_silence
= 0;
209 pa_memchunk_reset(&i
->thread_info
.resampled_chunk
);
210 i
->thread_info
.resampler
= resampler
;
211 i
->thread_info
.volume
= i
->volume
;
212 i
->thread_info
.muted
= i
->muted
;
213 i
->thread_info
.attached
= FALSE
;
215 pa_assert_se(pa_idxset_put(core
->sink_inputs
, pa_sink_input_ref(i
), &i
->index
) == 0);
216 pa_assert_se(pa_idxset_put(i
->sink
->inputs
, i
, NULL
) == 0);
218 pa_log_info("Created input %u \"%s\" on %s with sample spec %s",
222 pa_sample_spec_snprint(st
, sizeof(st
), &i
->sample_spec
));
224 /* Don't forget to call pa_sink_input_put! */
229 static void update_n_corked(pa_sink_input
*i
, pa_sink_input_state_t state
) {
232 if (i
->state
== PA_SINK_INPUT_CORKED
&& state
!= PA_SINK_INPUT_CORKED
)
233 pa_assert_se(i
->sink
->n_corked
-- >= 1);
234 else if (i
->state
!= PA_SINK_INPUT_CORKED
&& state
== PA_SINK_INPUT_CORKED
)
238 static int sink_input_set_state(pa_sink_input
*i
, pa_sink_input_state_t state
) {
239 pa_sink_input
*ssync
;
242 if (state
== PA_SINK_INPUT_DRAINED
)
243 state
= PA_SINK_INPUT_RUNNING
;
245 if (i
->state
== state
)
248 if (pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
) < 0)
251 update_n_corked(i
, state
);
254 for (ssync
= i
->sync_prev
; ssync
; ssync
= ssync
->sync_prev
) {
255 update_n_corked(ssync
, state
);
256 ssync
->state
= state
;
258 for (ssync
= i
->sync_next
; ssync
; ssync
= ssync
->sync_next
) {
259 update_n_corked(ssync
, state
);
260 ssync
->state
= state
;
266 void pa_sink_input_unlink(pa_sink_input
*i
) {
270 /* See pa_sink_unlink() for a couple of comments how this function
273 pa_sink_input_ref(i
);
275 linked
= PA_SINK_INPUT_LINKED(i
->state
);
278 pa_hook_fire(&i
->sink
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_UNLINK
], i
);
281 i
->sync_prev
->sync_next
= i
->sync_next
;
283 i
->sync_next
->sync_prev
= i
->sync_prev
;
285 i
->sync_prev
= i
->sync_next
= NULL
;
287 pa_idxset_remove_by_data(i
->sink
->core
->sink_inputs
, i
, NULL
);
288 if (pa_idxset_remove_by_data(i
->sink
->inputs
, i
, NULL
))
289 pa_sink_input_unref(i
);
292 pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
->sink
), PA_SINK_MESSAGE_REMOVE_INPUT
, i
, 0, NULL
);
293 sink_input_set_state(i
, PA_SINK_INPUT_UNLINKED
);
294 pa_sink_update_status(i
->sink
);
296 i
->state
= PA_SINK_INPUT_UNLINKED
;
301 i
->get_latency
= NULL
;
307 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, i
->index
);
308 pa_hook_fire(&i
->sink
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_UNLINK_POST
], i
);
312 pa_sink_input_unref(i
);
315 static void sink_input_free(pa_object
*o
) {
316 pa_sink_input
* i
= PA_SINK_INPUT(o
);
319 pa_assert(pa_sink_input_refcnt(i
) == 0);
321 if (PA_SINK_INPUT_LINKED(i
->state
))
322 pa_sink_input_unlink(i
);
324 pa_log_info("Freeing output %u \"%s\"", i
->index
, i
->name
);
326 pa_assert(!i
->thread_info
.attached
);
328 if (i
->thread_info
.resampled_chunk
.memblock
)
329 pa_memblock_unref(i
->thread_info
.resampled_chunk
.memblock
);
331 if (i
->thread_info
.resampler
)
332 pa_resampler_free(i
->thread_info
.resampler
);
334 if (i
->thread_info
.silence_memblock
)
335 pa_memblock_unref(i
->thread_info
.silence_memblock
);
342 void pa_sink_input_put(pa_sink_input
*i
) {
343 pa_sink_input_assert_ref(i
);
345 pa_assert(i
->state
== PA_SINK_INPUT_INIT
);
349 i
->thread_info
.state
= i
->state
= i
->flags
& PA_SINK_INPUT_START_CORKED
? PA_SINK_INPUT_CORKED
: PA_SINK_INPUT_RUNNING
;
350 i
->thread_info
.volume
= i
->volume
;
351 i
->thread_info
.muted
= i
->muted
;
353 pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
->sink
), PA_SINK_MESSAGE_ADD_INPUT
, i
, 0, NULL
);
354 pa_sink_update_status(i
->sink
);
356 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
, i
->index
);
357 pa_hook_fire(&i
->sink
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_PUT
], i
);
359 /* Please note that if you change something here, you have to
360 change something in pa_sink_input_move() with the ghost stream
364 void pa_sink_input_kill(pa_sink_input
*i
) {
365 pa_sink_input_assert_ref(i
);
366 pa_assert(PA_SINK_INPUT_LINKED(i
->state
));
372 pa_usec_t
pa_sink_input_get_latency(pa_sink_input
*i
) {
375 pa_sink_input_assert_ref(i
);
376 pa_assert(PA_SINK_INPUT_LINKED(i
->state
));
378 if (pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_GET_LATENCY
, &r
, 0, NULL
) < 0)
382 r
+= i
->get_latency(i
);
387 /* Called from thread context */
388 int pa_sink_input_peek(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
, pa_cvolume
*volume
) {
390 int do_volume_adj_here
;
392 size_t block_size_max
;
394 pa_sink_input_assert_ref(i
);
395 pa_assert(PA_SINK_INPUT_LINKED(i
->thread_info
.state
));
396 pa_assert(pa_frame_aligned(length
, &i
->sink
->sample_spec
));
400 if (!i
->peek
|| !i
->drop
|| i
->thread_info
.state
== PA_SINK_INPUT_CORKED
)
403 pa_assert(i
->thread_info
.state
== PA_SINK_INPUT_RUNNING
|| i
->thread_info
.state
== PA_SINK_INPUT_DRAINED
);
405 /* Default buffer size */
407 length
= pa_frame_align(CONVERT_BUFFER_LENGTH
, &i
->sink
->sample_spec
);
409 /* Make sure the buffer fits in the mempool tile */
410 block_size_max
= pa_mempool_block_size_max(i
->sink
->core
->mempool
);
411 if (length
> block_size_max
)
412 length
= pa_frame_align(block_size_max
, &i
->sink
->sample_spec
);
414 if (i
->thread_info
.move_silence
> 0) {
417 /* We have just been moved and shall play some silence for a
418 * while until the old sink has drained its playback buffer */
420 if (!i
->thread_info
.silence_memblock
)
421 i
->thread_info
.silence_memblock
= pa_silence_memblock_new(
422 i
->sink
->core
->mempool
,
423 &i
->sink
->sample_spec
,
424 pa_frame_align(SILENCE_BUFFER_LENGTH
, &i
->sink
->sample_spec
));
426 chunk
->memblock
= pa_memblock_ref(i
->thread_info
.silence_memblock
);
428 l
= pa_memblock_get_length(chunk
->memblock
);
429 chunk
->length
= i
->thread_info
.move_silence
< l
? i
->thread_info
.move_silence
: l
;
432 do_volume_adj_here
= 1;
436 if (!i
->thread_info
.resampler
) {
437 do_volume_adj_here
= 0; /* FIXME??? */
438 ret
= i
->peek(i
, length
, chunk
);
442 do_volume_adj_here
= !pa_channel_map_equal(&i
->channel_map
, &i
->sink
->channel_map
);
443 volume_is_norm
= pa_cvolume_is_norm(&i
->thread_info
.volume
) && !i
->thread_info
.muted
;
445 while (!i
->thread_info
.resampled_chunk
.memblock
) {
449 l
= pa_resampler_request(i
->thread_info
.resampler
, length
);
452 l
= pa_frame_align(CONVERT_BUFFER_LENGTH
, &i
->sample_spec
);
454 rmbs
= pa_resampler_max_block_size(i
->thread_info
.resampler
);
458 if ((ret
= i
->peek(i
, l
, &tchunk
)) < 0)
461 pa_assert(tchunk
.length
> 0);
463 if (tchunk
.length
> l
)
466 i
->drop(i
, tchunk
.length
);
468 /* It might be necessary to adjust the volume here */
469 if (do_volume_adj_here
&& !volume_is_norm
) {
470 pa_memchunk_make_writable(&tchunk
, 0);
472 if (i
->thread_info
.muted
)
473 pa_silence_memchunk(&tchunk
, &i
->thread_info
.sample_spec
);
475 pa_volume_memchunk(&tchunk
, &i
->thread_info
.sample_spec
, &i
->thread_info
.volume
);
478 pa_resampler_run(i
->thread_info
.resampler
, &tchunk
, &i
->thread_info
.resampled_chunk
);
479 pa_memblock_unref(tchunk
.memblock
);
482 pa_assert(i
->thread_info
.resampled_chunk
.memblock
);
483 pa_assert(i
->thread_info
.resampled_chunk
.length
> 0);
485 *chunk
= i
->thread_info
.resampled_chunk
;
486 pa_memblock_ref(i
->thread_info
.resampled_chunk
.memblock
);
493 pa_atomic_store(&i
->thread_info
.drained
, 0);
495 pa_atomic_store(&i
->thread_info
.drained
, 1);
498 /* Let's see if we had to apply the volume adjustment
499 * ourselves, or if this can be done by the sink for us */
501 if (do_volume_adj_here
)
502 /* We had different channel maps, so we already did the adjustment */
503 pa_cvolume_reset(volume
, i
->sink
->sample_spec
.channels
);
504 else if (i
->thread_info
.muted
)
505 /* We've both the same channel map, so let's have the sink do the adjustment for us*/
506 pa_cvolume_mute(volume
, i
->sink
->sample_spec
.channels
);
508 *volume
= i
->thread_info
.volume
;
514 /* Called from thread context */
515 void pa_sink_input_drop(pa_sink_input
*i
, size_t length
) {
516 pa_sink_input_assert_ref(i
);
517 pa_assert(PA_SINK_INPUT_LINKED(i
->thread_info
.state
));
518 pa_assert(pa_frame_aligned(length
, &i
->sink
->sample_spec
));
519 pa_assert(length
> 0);
521 if (!i
->peek
|| !i
->drop
|| i
->thread_info
.state
== PA_SINK_INPUT_CORKED
)
524 if (i
->thread_info
.move_silence
> 0) {
526 if (i
->thread_info
.move_silence
>= length
) {
527 i
->thread_info
.move_silence
-= length
;
530 length
-= i
->thread_info
.move_silence
;
531 i
->thread_info
.move_silence
= 0;
534 if (i
->thread_info
.move_silence
<= 0) {
535 pa_assert(i
->thread_info
.silence_memblock
);
536 pa_memblock_unref(i
->thread_info
.silence_memblock
);
537 i
->thread_info
.silence_memblock
= NULL
;
544 if (i
->thread_info
.resampled_chunk
.memblock
) {
547 if (l
> i
->thread_info
.resampled_chunk
.length
)
548 l
= i
->thread_info
.resampled_chunk
.length
;
550 i
->thread_info
.resampled_chunk
.index
+= l
;
551 i
->thread_info
.resampled_chunk
.length
-= l
;
553 if (i
->thread_info
.resampled_chunk
.length
<= 0) {
554 pa_memblock_unref(i
->thread_info
.resampled_chunk
.memblock
);
555 pa_memchunk_reset(&i
->thread_info
.resampled_chunk
);
563 if (i
->thread_info
.resampler
) {
564 /* So, we have a resampler. To avoid discontinuities we
565 * have to actually read all data that could be read and
566 * pass it through the resampler. */
572 if (pa_sink_input_peek(i
, length
, &chunk
, &volume
) >= 0) {
575 pa_memblock_unref(chunk
.memblock
);
581 pa_sink_input_drop(i
, l
);
587 l
= pa_resampler_request(i
->thread_info
.resampler
, length
);
589 /* Hmmm, peeking failed, so let's at least drop
590 * the right amount of data */
602 /* We have no resampler, hence let's just drop the data */
610 void pa_sink_input_set_volume(pa_sink_input
*i
, const pa_cvolume
*volume
) {
611 pa_sink_input_assert_ref(i
);
612 pa_assert(PA_SINK_INPUT_LINKED(i
->state
));
614 if (pa_cvolume_equal(&i
->volume
, volume
))
619 pa_asyncmsgq_post(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_SET_VOLUME
, pa_xnewdup(struct pa_cvolume
, volume
, 1), 0, NULL
, pa_xfree
);
620 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
623 const pa_cvolume
*pa_sink_input_get_volume(pa_sink_input
*i
) {
624 pa_sink_input_assert_ref(i
);
625 pa_assert(PA_SINK_INPUT_LINKED(i
->state
));
630 void pa_sink_input_set_mute(pa_sink_input
*i
, pa_bool_t mute
) {
632 pa_sink_input_assert_ref(i
);
633 pa_assert(PA_SINK_INPUT_LINKED(i
->state
));
635 if (!i
->muted
== !mute
)
640 pa_asyncmsgq_post(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
641 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
644 int pa_sink_input_get_mute(pa_sink_input
*i
) {
645 pa_sink_input_assert_ref(i
);
646 pa_assert(PA_SINK_INPUT_LINKED(i
->state
));
651 void pa_sink_input_cork(pa_sink_input
*i
, pa_bool_t b
) {
652 pa_sink_input_assert_ref(i
);
653 pa_assert(PA_SINK_INPUT_LINKED(i
->state
));
655 sink_input_set_state(i
, b
? PA_SINK_INPUT_CORKED
: PA_SINK_INPUT_RUNNING
);
658 int pa_sink_input_set_rate(pa_sink_input
*i
, uint32_t rate
) {
659 pa_sink_input_assert_ref(i
);
660 pa_assert(PA_SINK_INPUT_LINKED(i
->state
));
661 pa_return_val_if_fail(i
->thread_info
.resampler
, -1);
663 if (i
->sample_spec
.rate
== rate
)
666 i
->sample_spec
.rate
= rate
;
668 pa_asyncmsgq_post(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_SET_RATE
, PA_UINT_TO_PTR(rate
), 0, NULL
, NULL
);
670 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
674 void pa_sink_input_set_name(pa_sink_input
*i
, const char *name
) {
675 pa_sink_input_assert_ref(i
);
677 if (!i
->name
&& !name
)
680 if (i
->name
&& name
&& !strcmp(i
->name
, name
))
684 i
->name
= pa_xstrdup(name
);
686 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
689 pa_resample_method_t
pa_sink_input_get_resample_method(pa_sink_input
*i
) {
690 pa_sink_input_assert_ref(i
);
692 return i
->resample_method
;
695 int pa_sink_input_move_to(pa_sink_input
*i
, pa_sink
*dest
, int immediately
) {
696 pa_resampler
*new_resampler
;
698 pa_usec_t silence_usec
= 0;
699 pa_sink_input_move_info info
;
701 pa_sink_input_assert_ref(i
);
702 pa_assert(PA_SINK_INPUT_LINKED(i
->state
));
703 pa_sink_assert_ref(dest
);
710 if (i
->flags
& PA_SINK_INPUT_DONT_MOVE
)
713 if (i
->sync_next
|| i
->sync_prev
) {
714 pa_log_warn("Moving synchronised streams not supported.");
718 if (pa_idxset_size(dest
->inputs
) >= PA_MAX_INPUTS_PER_SINK
) {
719 pa_log_warn("Failed to move sink input: too many inputs per sink.");
723 if (i
->thread_info
.resampler
&&
724 pa_sample_spec_equal(&origin
->sample_spec
, &dest
->sample_spec
) &&
725 pa_channel_map_equal(&origin
->channel_map
, &dest
->channel_map
))
727 /* Try to reuse the old resampler if possible */
728 new_resampler
= i
->thread_info
.resampler
;
730 else if ((i
->flags
& PA_SINK_INPUT_VARIABLE_RATE
) ||
731 !pa_sample_spec_equal(&i
->sample_spec
, &dest
->sample_spec
) ||
732 !pa_channel_map_equal(&i
->channel_map
, &dest
->channel_map
)) {
734 /* Okey, we need a new resampler for the new sink */
736 if (!(new_resampler
= pa_resampler_new(
738 &i
->sample_spec
, &i
->channel_map
,
739 &dest
->sample_spec
, &dest
->channel_map
,
741 !!(i
->flags
& PA_SINK_INPUT_VARIABLE_RATE
)))) {
742 pa_log_warn("Unsupported resampling operation.");
746 new_resampler
= NULL
;
748 pa_hook_fire(&i
->sink
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_MOVE
], i
);
750 memset(&info
, 0, sizeof(info
));
754 pa_usec_t old_latency
, new_latency
;
756 /* Let's do a little bit of Voodoo for compensating latency
757 * differences. We assume that the accuracy for our
758 * estimations is still good enough, even though we do these
759 * operations non-atomic. */
761 old_latency
= pa_sink_get_latency(origin
);
762 new_latency
= pa_sink_get_latency(dest
);
764 /* The already resampled data should go to the old sink */
766 if (old_latency
>= new_latency
) {
768 /* The latency of the old sink is larger than the latency
769 * of the new sink. Therefore to compensate for the
770 * difference we to play silence on the new one for a
773 silence_usec
= old_latency
- new_latency
;
777 /* The latency of new sink is larger than the latency of
778 * the old sink. Therefore we have to precompute a little
779 * and make sure that this is still played on the old
780 * sink, until we can play the first sample on the new
783 info
.buffer_bytes
= pa_usec_to_bytes(new_latency
- old_latency
, &origin
->sample_spec
);
786 /* Okey, let's move it */
788 if (info
.buffer_bytes
> 0) {
790 info
.ghost_sink_input
= pa_memblockq_sink_input_new(
793 &origin
->sample_spec
,
794 &origin
->channel_map
,
798 info
.ghost_sink_input
->thread_info
.state
= info
.ghost_sink_input
->state
= PA_SINK_INPUT_RUNNING
;
799 info
.ghost_sink_input
->thread_info
.volume
= info
.ghost_sink_input
->volume
;
800 info
.ghost_sink_input
->thread_info
.muted
= info
.ghost_sink_input
->muted
;
802 info
.buffer
= pa_memblockq_new(0, MOVE_BUFFER_LENGTH
, 0, pa_frame_size(&origin
->sample_spec
), 0, 0, NULL
);
806 pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
->sink
), PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER
, &info
, 0, NULL
);
808 if (info
.ghost_sink_input
) {
809 /* Basically, do what pa_sink_input_put() does ...*/
811 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
, info
.ghost_sink_input
->index
);
812 pa_hook_fire(&i
->sink
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_PUT
], info
.ghost_sink_input
);
813 pa_sink_input_unref(info
.ghost_sink_input
);
816 pa_idxset_remove_by_data(origin
->inputs
, i
, NULL
);
817 pa_idxset_put(dest
->inputs
, i
, NULL
);
820 /* Replace resampler */
821 if (new_resampler
!= i
->thread_info
.resampler
) {
822 if (i
->thread_info
.resampler
)
823 pa_resampler_free(i
->thread_info
.resampler
);
824 i
->thread_info
.resampler
= new_resampler
;
826 /* if the resampler changed, the silence memblock is
827 * probably invalid now, too */
828 if (i
->thread_info
.silence_memblock
) {
829 pa_memblock_unref(i
->thread_info
.silence_memblock
);
830 i
->thread_info
.silence_memblock
= NULL
;
834 /* Dump already resampled data */
835 if (i
->thread_info
.resampled_chunk
.memblock
) {
836 /* Hmm, this data has already been added to the ghost queue, presumably, hence let's sleep a little bit longer */
837 silence_usec
+= pa_bytes_to_usec(i
->thread_info
.resampled_chunk
.length
, &origin
->sample_spec
);
838 pa_memblock_unref(i
->thread_info
.resampled_chunk
.memblock
);
839 pa_memchunk_reset(&i
->thread_info
.resampled_chunk
);
842 /* Calculate the new sleeping time */
844 i
->thread_info
.move_silence
= 0;
846 i
->thread_info
.move_silence
= pa_usec_to_bytes(
847 pa_bytes_to_usec(i
->thread_info
.move_silence
, &origin
->sample_spec
) +
851 pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
->sink
), PA_SINK_MESSAGE_ADD_INPUT
, i
, 0, NULL
);
853 pa_sink_update_status(origin
);
854 pa_sink_update_status(dest
);
856 pa_hook_fire(&i
->sink
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_MOVE_POST
], i
);
858 pa_log_debug("Successfully moved sink input %i from %s to %s.", i
->index
, origin
->name
, dest
->name
);
860 /* Notify everyone */
861 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
866 /* Called from thread context */
867 int pa_sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
868 pa_sink_input
*i
= PA_SINK_INPUT(o
);
870 pa_sink_input_assert_ref(i
);
871 pa_assert(PA_SINK_INPUT_LINKED(i
->thread_info
.state
));
874 case PA_SINK_INPUT_MESSAGE_SET_VOLUME
:
875 i
->thread_info
.volume
= *((pa_cvolume
*) userdata
);
878 case PA_SINK_INPUT_MESSAGE_SET_MUTE
:
879 i
->thread_info
.muted
= PA_PTR_TO_UINT(userdata
);
882 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
883 pa_usec_t
*r
= userdata
;
885 if (i
->thread_info
.resampled_chunk
.memblock
)
886 *r
+= pa_bytes_to_usec(i
->thread_info
.resampled_chunk
.length
, &i
->sink
->sample_spec
);
888 if (i
->thread_info
.move_silence
)
889 *r
+= pa_bytes_to_usec(i
->thread_info
.move_silence
, &i
->sink
->sample_spec
);
894 case PA_SINK_INPUT_MESSAGE_SET_RATE
:
896 i
->thread_info
.sample_spec
.rate
= PA_PTR_TO_UINT(userdata
);
897 pa_resampler_set_input_rate(i
->thread_info
.resampler
, PA_PTR_TO_UINT(userdata
));
901 case PA_SINK_INPUT_MESSAGE_SET_STATE
: {
902 pa_sink_input
*ssync
;
904 if ((PA_PTR_TO_UINT(userdata
) == PA_SINK_INPUT_DRAINED
|| PA_PTR_TO_UINT(userdata
) == PA_SINK_INPUT_RUNNING
) &&
905 (i
->thread_info
.state
!= PA_SINK_INPUT_DRAINED
) && (i
->thread_info
.state
!= PA_SINK_INPUT_RUNNING
))
906 pa_atomic_store(&i
->thread_info
.drained
, 1);
908 i
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
910 for (ssync
= i
->thread_info
.sync_prev
; ssync
; ssync
= ssync
->thread_info
.sync_prev
) {
911 if ((PA_PTR_TO_UINT(userdata
) == PA_SINK_INPUT_DRAINED
|| PA_PTR_TO_UINT(userdata
) == PA_SINK_INPUT_RUNNING
) &&
912 (ssync
->thread_info
.state
!= PA_SINK_INPUT_DRAINED
) && (ssync
->thread_info
.state
!= PA_SINK_INPUT_RUNNING
))
913 pa_atomic_store(&ssync
->thread_info
.drained
, 1);
914 ssync
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
917 for (ssync
= i
->thread_info
.sync_next
; ssync
; ssync
= ssync
->thread_info
.sync_next
) {
918 if ((PA_PTR_TO_UINT(userdata
) == PA_SINK_INPUT_DRAINED
|| PA_PTR_TO_UINT(userdata
) == PA_SINK_INPUT_RUNNING
) &&
919 (ssync
->thread_info
.state
!= PA_SINK_INPUT_DRAINED
) && (ssync
->thread_info
.state
!= PA_SINK_INPUT_RUNNING
))
920 pa_atomic_store(&ssync
->thread_info
.drained
, 1);
921 ssync
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
931 pa_sink_input_state_t
pa_sink_input_get_state(pa_sink_input
*i
) {
932 pa_sink_input_assert_ref(i
);
934 if (i
->state
== PA_SINK_INPUT_RUNNING
|| i
->state
== PA_SINK_INPUT_DRAINED
)
935 return pa_atomic_load(&i
->thread_info
.drained
) ? PA_SINK_INPUT_DRAINED
: PA_SINK_INPUT_RUNNING
;