2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/utf8.h>
31 #include <pulse/xmalloc.h>
32 #include <pulse/util.h>
33 #include <pulse/internal.h>
35 #include <pulsecore/mix.h>
36 #include <pulsecore/core-subscribe.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/play-memblockq.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/core-util.h>
42 #include "sink-input.h"
44 /* #define SINK_INPUT_DEBUG */
46 #define MEMBLOCKQ_MAXLENGTH (32*1024*1024)
47 #define CONVERT_BUFFER_LENGTH (PA_PAGE_SIZE)
49 PA_DEFINE_PUBLIC_CLASS(pa_sink_input
, pa_msgobject
);
51 struct volume_factor_entry
{
56 static struct volume_factor_entry
*volume_factor_entry_new(const char *key
, const pa_cvolume
*volume
) {
57 struct volume_factor_entry
*entry
;
62 entry
= pa_xnew(struct volume_factor_entry
, 1);
63 entry
->key
= pa_xstrdup(key
);
65 entry
->volume
= *volume
;
70 static void volume_factor_entry_free(struct volume_factor_entry
*volume_entry
) {
71 pa_assert(volume_entry
);
73 pa_xfree(volume_entry
->key
);
74 pa_xfree(volume_entry
);
77 static void volume_factor_from_hashmap(pa_cvolume
*v
, pa_hashmap
*items
, uint8_t channels
) {
78 struct volume_factor_entry
*entry
;
81 pa_cvolume_reset(v
, channels
);
82 PA_HASHMAP_FOREACH(entry
, items
, state
)
83 pa_sw_cvolume_multiply(v
, v
, &entry
->volume
);
86 static void sink_input_free(pa_object
*o
);
87 static void set_real_ratio(pa_sink_input
*i
, const pa_cvolume
*v
);
89 static int check_passthrough_connection(pa_bool_t passthrough
, pa_sink
*dest
) {
90 if (pa_sink_is_passthrough(dest
)) {
91 pa_log_warn("Sink is already connected to PASSTHROUGH input");
95 /* If current input(s) exist, check new input is not PASSTHROUGH */
96 if (pa_idxset_size(dest
->inputs
) > 0 && passthrough
) {
97 pa_log_warn("Sink is already connected, cannot accept new PASSTHROUGH INPUT");
104 pa_sink_input_new_data
* pa_sink_input_new_data_init(pa_sink_input_new_data
*data
) {
108 data
->resample_method
= PA_RESAMPLER_INVALID
;
109 data
->proplist
= pa_proplist_new();
110 data
->volume_writable
= TRUE
;
112 data
->volume_factor_items
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
113 data
->volume_factor_sink_items
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
118 void pa_sink_input_new_data_set_sample_spec(pa_sink_input_new_data
*data
, const pa_sample_spec
*spec
) {
121 if ((data
->sample_spec_is_set
= !!spec
))
122 data
->sample_spec
= *spec
;
125 void pa_sink_input_new_data_set_channel_map(pa_sink_input_new_data
*data
, const pa_channel_map
*map
) {
128 if ((data
->channel_map_is_set
= !!map
))
129 data
->channel_map
= *map
;
132 pa_bool_t
pa_sink_input_new_data_is_passthrough(pa_sink_input_new_data
*data
) {
135 if (PA_LIKELY(data
->format
) && PA_UNLIKELY(!pa_format_info_is_pcm(data
->format
)))
138 if (PA_UNLIKELY(data
->flags
& PA_SINK_INPUT_PASSTHROUGH
))
144 void pa_sink_input_new_data_set_volume(pa_sink_input_new_data
*data
, const pa_cvolume
*volume
) {
146 pa_assert(data
->volume_writable
);
148 if ((data
->volume_is_set
= !!volume
))
149 data
->volume
= *volume
;
152 void pa_sink_input_new_data_add_volume_factor(pa_sink_input_new_data
*data
, const char *key
, const pa_cvolume
*volume_factor
) {
153 struct volume_factor_entry
*v
;
157 pa_assert(volume_factor
);
159 v
= volume_factor_entry_new(key
, volume_factor
);
160 pa_assert_se(pa_hashmap_put(data
->volume_factor_items
, v
->key
, v
) >= 0);
163 void pa_sink_input_new_data_add_volume_factor_sink(pa_sink_input_new_data
*data
, const char *key
, const pa_cvolume
*volume_factor
) {
164 struct volume_factor_entry
*v
;
168 pa_assert(volume_factor
);
170 v
= volume_factor_entry_new(key
, volume_factor
);
171 pa_assert_se(pa_hashmap_put(data
->volume_factor_sink_items
, v
->key
, v
) >= 0);
174 void pa_sink_input_new_data_set_muted(pa_sink_input_new_data
*data
, pa_bool_t mute
) {
177 data
->muted_is_set
= TRUE
;
178 data
->muted
= !!mute
;
181 pa_bool_t
pa_sink_input_new_data_set_sink(pa_sink_input_new_data
*data
, pa_sink
*s
, pa_bool_t save
) {
182 pa_bool_t ret
= TRUE
;
183 pa_idxset
*formats
= NULL
;
188 if (!data
->req_formats
) {
189 /* We're not working with the extended API */
191 data
->save_sink
= save
;
193 /* Extended API: let's see if this sink supports the formats the client can provide */
194 formats
= pa_sink_check_formats(s
, data
->req_formats
);
196 if (formats
&& !pa_idxset_isempty(formats
)) {
197 /* Sink supports at least one of the requested formats */
199 data
->save_sink
= save
;
200 if (data
->nego_formats
)
201 pa_idxset_free(data
->nego_formats
, (pa_free_cb_t
) pa_format_info_free
);
202 data
->nego_formats
= formats
;
204 /* Sink doesn't support any of the formats requested by the client */
206 pa_idxset_free(formats
, (pa_free_cb_t
) pa_format_info_free
);
214 pa_bool_t
pa_sink_input_new_data_set_formats(pa_sink_input_new_data
*data
, pa_idxset
*formats
) {
218 if (data
->req_formats
)
219 pa_idxset_free(formats
, (pa_free_cb_t
) pa_format_info_free
);
221 data
->req_formats
= formats
;
224 /* Trigger format negotiation */
225 return pa_sink_input_new_data_set_sink(data
, data
->sink
, data
->save_sink
);
231 void pa_sink_input_new_data_done(pa_sink_input_new_data
*data
) {
234 if (data
->req_formats
)
235 pa_idxset_free(data
->req_formats
, (pa_free_cb_t
) pa_format_info_free
);
237 if (data
->nego_formats
)
238 pa_idxset_free(data
->nego_formats
, (pa_free_cb_t
) pa_format_info_free
);
241 pa_format_info_free(data
->format
);
243 if (data
->volume_factor_items
)
244 pa_hashmap_free(data
->volume_factor_items
, (pa_free_cb_t
) volume_factor_entry_free
);
246 if (data
->volume_factor_sink_items
)
247 pa_hashmap_free(data
->volume_factor_sink_items
, (pa_free_cb_t
) volume_factor_entry_free
);
249 pa_proplist_free(data
->proplist
);
252 /* Called from main context */
253 static void reset_callbacks(pa_sink_input
*i
) {
257 i
->process_underrun
= NULL
;
258 i
->process_rewind
= NULL
;
259 i
->update_max_rewind
= NULL
;
260 i
->update_max_request
= NULL
;
261 i
->update_sink_requested_latency
= NULL
;
262 i
->update_sink_latency_range
= NULL
;
263 i
->update_sink_fixed_latency
= NULL
;
267 i
->suspend_within_thread
= NULL
;
270 i
->get_latency
= NULL
;
271 i
->state_change
= NULL
;
272 i
->may_move_to
= NULL
;
273 i
->send_event
= NULL
;
274 i
->volume_changed
= NULL
;
275 i
->mute_changed
= NULL
;
278 /* Called from main context */
279 int pa_sink_input_new(
282 pa_sink_input_new_data
*data
) {
285 pa_resampler
*resampler
= NULL
;
286 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
287 pa_channel_map original_cm
;
290 char *memblockq_name
;
297 pa_assert_ctl_context();
300 pa_proplist_update(data
->proplist
, PA_UPDATE_MERGE
, data
->client
->proplist
);
302 if (data
->origin_sink
&& (data
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
303 data
->volume_writable
= FALSE
;
305 if (!data
->req_formats
) {
306 /* From this point on, we want to work only with formats, and get back
307 * to using the sample spec and channel map after all decisions w.r.t.
308 * routing are complete. */
309 pa_idxset
*tmp
= pa_idxset_new(NULL
, NULL
);
310 pa_format_info
*f
= pa_format_info_from_sample_spec(&data
->sample_spec
,
311 data
->channel_map_is_set
? &data
->channel_map
: NULL
);
312 pa_idxset_put(tmp
, f
, NULL
);
313 pa_sink_input_new_data_set_formats(data
, tmp
);
316 if ((r
= pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_INPUT_NEW
], data
)) < 0)
319 pa_return_val_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
), -PA_ERR_INVALID
);
322 pa_sink
*sink
= pa_namereg_get(core
, NULL
, PA_NAMEREG_SINK
);
323 pa_return_val_if_fail(sink
, -PA_ERR_NOENTITY
);
324 pa_sink_input_new_data_set_sink(data
, sink
, FALSE
);
326 /* Routing's done, we have a sink. Now let's fix the format and set up the
329 /* If something didn't pick a format for us, pick the top-most format since
330 * we assume this is sorted in priority order */
331 if (!data
->format
&& data
->nego_formats
&& !pa_idxset_isempty(data
->nego_formats
))
332 data
->format
= pa_format_info_copy(pa_idxset_first(data
->nego_formats
, NULL
));
334 pa_return_val_if_fail(data
->format
, -PA_ERR_NOTSUPPORTED
);
336 /* Now populate the sample spec and format according to the final
337 * format that we've negotiated */
338 pa_return_val_if_fail(pa_format_info_to_sample_spec(data
->format
, &ss
, &map
) == 0, -PA_ERR_INVALID
);
339 pa_sink_input_new_data_set_sample_spec(data
, &ss
);
340 if (pa_format_info_is_pcm(data
->format
) && pa_channel_map_valid(&map
))
341 pa_sink_input_new_data_set_channel_map(data
, &map
);
343 pa_return_val_if_fail(PA_SINK_IS_LINKED(pa_sink_get_state(data
->sink
)), -PA_ERR_BADSTATE
);
344 pa_return_val_if_fail(!data
->sync_base
|| (data
->sync_base
->sink
== data
->sink
&& pa_sink_input_get_state(data
->sync_base
) == PA_SINK_INPUT_CORKED
), -PA_ERR_INVALID
);
346 r
= check_passthrough_connection(pa_sink_input_new_data_is_passthrough(data
), data
->sink
);
350 if (!data
->sample_spec_is_set
)
351 data
->sample_spec
= data
->sink
->sample_spec
;
353 pa_return_val_if_fail(pa_sample_spec_valid(&data
->sample_spec
), -PA_ERR_INVALID
);
355 if (!data
->channel_map_is_set
) {
356 if (pa_channel_map_compatible(&data
->sink
->channel_map
, &data
->sample_spec
))
357 data
->channel_map
= data
->sink
->channel_map
;
359 pa_channel_map_init_extend(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
);
362 pa_return_val_if_fail(pa_channel_map_compatible(&data
->channel_map
, &data
->sample_spec
), -PA_ERR_INVALID
);
364 /* Don't restore (or save) stream volume for passthrough streams and
365 * prevent attenuation/gain */
366 if (pa_sink_input_new_data_is_passthrough(data
)) {
367 data
->volume_is_set
= TRUE
;
368 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
369 data
->volume_is_absolute
= TRUE
;
370 data
->save_volume
= FALSE
;
373 if (!data
->volume_is_set
) {
374 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
375 data
->volume_is_absolute
= FALSE
;
376 data
->save_volume
= FALSE
;
379 pa_return_val_if_fail(pa_cvolume_compatible(&data
->volume
, &data
->sample_spec
), -PA_ERR_INVALID
);
381 if (!data
->muted_is_set
)
384 if (data
->flags
& PA_SINK_INPUT_FIX_FORMAT
)
385 data
->sample_spec
.format
= data
->sink
->sample_spec
.format
;
387 if (data
->flags
& PA_SINK_INPUT_FIX_RATE
)
388 data
->sample_spec
.rate
= data
->sink
->sample_spec
.rate
;
390 original_cm
= data
->channel_map
;
392 if (data
->flags
& PA_SINK_INPUT_FIX_CHANNELS
) {
393 data
->sample_spec
.channels
= data
->sink
->sample_spec
.channels
;
394 data
->channel_map
= data
->sink
->channel_map
;
397 pa_assert(pa_sample_spec_valid(&data
->sample_spec
));
398 pa_assert(pa_channel_map_valid(&data
->channel_map
));
400 if (!(data
->flags
& PA_SINK_INPUT_VARIABLE_RATE
) &&
401 !pa_sample_spec_equal(&data
->sample_spec
, &data
->sink
->sample_spec
)) {
402 /* try to change sink rate. This is done before the FIXATE hook since
403 module-suspend-on-idle can resume a sink */
405 pa_log_info("Trying to change sample rate");
406 if (pa_sink_update_rate(data
->sink
, data
->sample_spec
.rate
, pa_sink_input_new_data_is_passthrough(data
)) == TRUE
)
407 pa_log_info("Rate changed to %u Hz", data
->sink
->sample_spec
.rate
);
410 if (pa_sink_input_new_data_is_passthrough(data
) &&
411 !pa_sample_spec_equal(&data
->sample_spec
, &data
->sink
->sample_spec
)) {
412 /* rate update failed, or other parts of sample spec didn't match */
414 pa_log_debug("Could not update sink sample spec to match passthrough stream");
415 return -PA_ERR_NOTSUPPORTED
;
418 /* Due to the fixing of the sample spec the volume might not match anymore */
419 pa_cvolume_remap(&data
->volume
, &original_cm
, &data
->channel_map
);
421 if (data
->resample_method
== PA_RESAMPLER_INVALID
)
422 data
->resample_method
= core
->resample_method
;
424 pa_return_val_if_fail(data
->resample_method
< PA_RESAMPLER_MAX
, -PA_ERR_INVALID
);
426 if ((r
= pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_INPUT_FIXATE
], data
)) < 0)
429 if ((data
->flags
& PA_SINK_INPUT_NO_CREATE_ON_SUSPEND
) &&
430 pa_sink_get_state(data
->sink
) == PA_SINK_SUSPENDED
) {
431 pa_log_warn("Failed to create sink input: sink is suspended.");
432 return -PA_ERR_BADSTATE
;
435 if (pa_idxset_size(data
->sink
->inputs
) >= PA_MAX_INPUTS_PER_SINK
) {
436 pa_log_warn("Failed to create sink input: too many inputs per sink.");
437 return -PA_ERR_TOOLARGE
;
440 if ((data
->flags
& PA_SINK_INPUT_VARIABLE_RATE
) ||
441 !pa_sample_spec_equal(&data
->sample_spec
, &data
->sink
->sample_spec
) ||
442 !pa_channel_map_equal(&data
->channel_map
, &data
->sink
->channel_map
)) {
444 /* Note: for passthrough content we need to adjust the output rate to that of the current sink-input */
445 if (!pa_sink_input_new_data_is_passthrough(data
)) /* no resampler for passthrough content */
446 if (!(resampler
= pa_resampler_new(
448 &data
->sample_spec
, &data
->channel_map
,
449 &data
->sink
->sample_spec
, &data
->sink
->channel_map
,
450 data
->resample_method
,
451 ((data
->flags
& PA_SINK_INPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
452 ((data
->flags
& PA_SINK_INPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
453 (core
->disable_remixing
|| (data
->flags
& PA_SINK_INPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0) |
454 (core
->disable_lfe_remixing
? PA_RESAMPLER_NO_LFE
: 0)))) {
455 pa_log_warn("Unsupported resampling operation.");
456 return -PA_ERR_NOTSUPPORTED
;
460 i
= pa_msgobject_new(pa_sink_input
);
461 i
->parent
.parent
.free
= sink_input_free
;
462 i
->parent
.process_msg
= pa_sink_input_process_msg
;
465 i
->state
= PA_SINK_INPUT_INIT
;
466 i
->flags
= data
->flags
;
467 i
->proplist
= pa_proplist_copy(data
->proplist
);
468 i
->driver
= pa_xstrdup(pa_path_get_filename(data
->driver
));
469 i
->module
= data
->module
;
470 i
->sink
= data
->sink
;
471 i
->origin_sink
= data
->origin_sink
;
472 i
->client
= data
->client
;
474 i
->requested_resample_method
= data
->resample_method
;
475 i
->actual_resample_method
= resampler
? pa_resampler_get_method(resampler
) : PA_RESAMPLER_INVALID
;
476 i
->sample_spec
= data
->sample_spec
;
477 i
->channel_map
= data
->channel_map
;
478 i
->format
= pa_format_info_copy(data
->format
);
480 if (!data
->volume_is_absolute
&& pa_sink_flat_volume_enabled(i
->sink
)) {
483 /* When the 'absolute' bool is not set then we'll treat the volume
484 * as relative to the sink volume even in flat volume mode */
485 remapped
= data
->sink
->reference_volume
;
486 pa_cvolume_remap(&remapped
, &data
->sink
->channel_map
, &data
->channel_map
);
487 pa_sw_cvolume_multiply(&i
->volume
, &data
->volume
, &remapped
);
489 i
->volume
= data
->volume
;
491 i
->volume_factor_items
= data
->volume_factor_items
;
492 data
->volume_factor_items
= NULL
;
493 volume_factor_from_hashmap(&i
->volume_factor
, i
->volume_factor_items
, i
->sample_spec
.channels
);
495 i
->volume_factor_sink_items
= data
->volume_factor_sink_items
;
496 data
->volume_factor_sink_items
= NULL
;
497 volume_factor_from_hashmap(&i
->volume_factor_sink
, i
->volume_factor_sink_items
, i
->sample_spec
.channels
);
499 i
->real_ratio
= i
->reference_ratio
= data
->volume
;
500 pa_cvolume_reset(&i
->soft_volume
, i
->sample_spec
.channels
);
501 pa_cvolume_reset(&i
->real_ratio
, i
->sample_spec
.channels
);
502 i
->volume_writable
= data
->volume_writable
;
503 i
->save_volume
= data
->save_volume
;
504 i
->save_sink
= data
->save_sink
;
505 i
->save_muted
= data
->save_muted
;
507 i
->muted
= data
->muted
;
509 if (data
->sync_base
) {
510 i
->sync_next
= data
->sync_base
->sync_next
;
511 i
->sync_prev
= data
->sync_base
;
513 if (data
->sync_base
->sync_next
)
514 data
->sync_base
->sync_next
->sync_prev
= i
;
515 data
->sync_base
->sync_next
= i
;
517 i
->sync_next
= i
->sync_prev
= NULL
;
519 i
->direct_outputs
= pa_idxset_new(NULL
, NULL
);
524 i
->thread_info
.state
= i
->state
;
525 i
->thread_info
.attached
= FALSE
;
526 pa_atomic_store(&i
->thread_info
.drained
, 1);
527 i
->thread_info
.sample_spec
= i
->sample_spec
;
528 i
->thread_info
.resampler
= resampler
;
529 i
->thread_info
.soft_volume
= i
->soft_volume
;
530 i
->thread_info
.muted
= i
->muted
;
531 i
->thread_info
.requested_sink_latency
= (pa_usec_t
) -1;
532 i
->thread_info
.rewrite_nbytes
= 0;
533 i
->thread_info
.rewrite_flush
= FALSE
;
534 i
->thread_info
.dont_rewind_render
= FALSE
;
535 i
->thread_info
.underrun_for
= (uint64_t) -1;
536 i
->thread_info
.underrun_for_sink
= 0;
537 i
->thread_info
.playing_for
= 0;
538 i
->thread_info
.direct_outputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
540 pa_assert_se(pa_idxset_put(core
->sink_inputs
, i
, &i
->index
) == 0);
541 pa_assert_se(pa_idxset_put(i
->sink
->inputs
, pa_sink_input_ref(i
), NULL
) == 0);
544 pa_assert_se(pa_idxset_put(i
->client
->sink_inputs
, i
, NULL
) >= 0);
546 memblockq_name
= pa_sprintf_malloc("sink input render_memblockq [%u]", i
->index
);
547 i
->thread_info
.render_memblockq
= pa_memblockq_new(
552 &i
->sink
->sample_spec
,
557 pa_xfree(memblockq_name
);
559 pt
= pa_proplist_to_string_sep(i
->proplist
, "\n ");
560 pa_log_info("Created input %u \"%s\" on %s with sample spec %s and channel map %s\n %s",
562 pa_strnull(pa_proplist_gets(i
->proplist
, PA_PROP_MEDIA_NAME
)),
564 pa_sample_spec_snprint(st
, sizeof(st
), &i
->sample_spec
),
565 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
569 /* Don't forget to call pa_sink_input_put! */
575 /* Called from main context */
576 static void update_n_corked(pa_sink_input
*i
, pa_sink_input_state_t state
) {
578 pa_assert_ctl_context();
583 if (i
->state
== PA_SINK_INPUT_CORKED
&& state
!= PA_SINK_INPUT_CORKED
)
584 pa_assert_se(i
->sink
->n_corked
-- >= 1);
585 else if (i
->state
!= PA_SINK_INPUT_CORKED
&& state
== PA_SINK_INPUT_CORKED
)
589 /* Called from main context */
590 static void sink_input_set_state(pa_sink_input
*i
, pa_sink_input_state_t state
) {
591 pa_sink_input
*ssync
;
593 pa_assert_ctl_context();
595 if (state
== PA_SINK_INPUT_DRAINED
)
596 state
= PA_SINK_INPUT_RUNNING
;
598 if (i
->state
== state
)
601 if (i
->state
== PA_SINK_INPUT_CORKED
&& state
== PA_SINK_INPUT_RUNNING
&& pa_sink_used_by(i
->sink
) == 0 &&
602 !pa_sample_spec_equal(&i
->sample_spec
, &i
->sink
->sample_spec
)) {
603 /* We were uncorked and the sink was not playing anything -- let's try
604 * to update the sample rate to avoid resampling */
605 pa_sink_update_rate(i
->sink
, i
->sample_spec
.rate
, pa_sink_input_is_passthrough(i
));
608 pa_assert_se(pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
) == 0);
610 update_n_corked(i
, state
);
613 for (ssync
= i
->sync_prev
; ssync
; ssync
= ssync
->sync_prev
) {
614 update_n_corked(ssync
, state
);
615 ssync
->state
= state
;
617 for (ssync
= i
->sync_next
; ssync
; ssync
= ssync
->sync_next
) {
618 update_n_corked(ssync
, state
);
619 ssync
->state
= state
;
622 if (state
!= PA_SINK_INPUT_UNLINKED
) {
623 pa_hook_fire(&i
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED
], i
);
625 for (ssync
= i
->sync_prev
; ssync
; ssync
= ssync
->sync_prev
)
626 pa_hook_fire(&i
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED
], ssync
);
628 for (ssync
= i
->sync_next
; ssync
; ssync
= ssync
->sync_next
)
629 pa_hook_fire(&i
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED
], ssync
);
631 if (PA_SINK_INPUT_IS_LINKED(state
))
632 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
635 pa_sink_update_status(i
->sink
);
638 /* Called from main context */
639 void pa_sink_input_unlink(pa_sink_input
*i
) {
641 pa_source_output
*o
, *p
= NULL
;
644 pa_assert_ctl_context();
646 /* See pa_sink_unlink() for a couple of comments how this function
649 pa_sink_input_ref(i
);
651 linked
= PA_SINK_INPUT_IS_LINKED(i
->state
);
654 pa_hook_fire(&i
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_UNLINK
], i
);
657 i
->sync_prev
->sync_next
= i
->sync_next
;
659 i
->sync_next
->sync_prev
= i
->sync_prev
;
661 i
->sync_prev
= i
->sync_next
= NULL
;
663 pa_idxset_remove_by_data(i
->core
->sink_inputs
, i
, NULL
);
666 if (pa_idxset_remove_by_data(i
->sink
->inputs
, i
, NULL
))
667 pa_sink_input_unref(i
);
670 pa_idxset_remove_by_data(i
->client
->sink_inputs
, i
, NULL
);
672 while ((o
= pa_idxset_first(i
->direct_outputs
, NULL
))) {
674 pa_source_output_kill(o
);
678 update_n_corked(i
, PA_SINK_INPUT_UNLINKED
);
679 i
->state
= PA_SINK_INPUT_UNLINKED
;
681 if (linked
&& i
->sink
) {
682 if (pa_sink_input_is_passthrough(i
))
683 pa_sink_leave_passthrough(i
->sink
);
685 /* We might need to update the sink's volume if we are in flat volume mode. */
686 if (pa_sink_flat_volume_enabled(i
->sink
))
687 pa_sink_set_volume(i
->sink
, NULL
, FALSE
, FALSE
);
689 if (i
->sink
->asyncmsgq
)
690 pa_assert_se(pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
->sink
), PA_SINK_MESSAGE_REMOVE_INPUT
, i
, 0, NULL
) == 0);
696 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, i
->index
);
697 pa_hook_fire(&i
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_UNLINK_POST
], i
);
701 if (PA_SINK_IS_LINKED(pa_sink_get_state(i
->sink
)))
702 pa_sink_update_status(i
->sink
);
707 pa_core_maybe_vacuum(i
->core
);
709 pa_sink_input_unref(i
);
712 /* Called from main context */
713 static void sink_input_free(pa_object
*o
) {
714 pa_sink_input
* i
= PA_SINK_INPUT(o
);
717 pa_assert_ctl_context();
718 pa_assert(pa_sink_input_refcnt(i
) == 0);
720 if (PA_SINK_INPUT_IS_LINKED(i
->state
))
721 pa_sink_input_unlink(i
);
723 pa_log_info("Freeing input %u \"%s\"", i
->index
, pa_strnull(pa_proplist_gets(i
->proplist
, PA_PROP_MEDIA_NAME
)));
725 /* Side note: this function must be able to destruct properly any
726 * kind of sink input in any state, even those which are
727 * "half-moved" or are connected to sinks that have no asyncmsgq
728 * and are hence half-destructed themselves! */
730 if (i
->thread_info
.render_memblockq
)
731 pa_memblockq_free(i
->thread_info
.render_memblockq
);
733 if (i
->thread_info
.resampler
)
734 pa_resampler_free(i
->thread_info
.resampler
);
737 pa_format_info_free(i
->format
);
740 pa_proplist_free(i
->proplist
);
742 if (i
->direct_outputs
)
743 pa_idxset_free(i
->direct_outputs
, NULL
);
745 if (i
->thread_info
.direct_outputs
)
746 pa_hashmap_free(i
->thread_info
.direct_outputs
, NULL
);
748 if (i
->volume_factor_items
)
749 pa_hashmap_free(i
->volume_factor_items
, (pa_free_cb_t
) volume_factor_entry_free
);
751 if (i
->volume_factor_sink_items
)
752 pa_hashmap_free(i
->volume_factor_sink_items
, (pa_free_cb_t
) volume_factor_entry_free
);
758 /* Called from main context */
759 void pa_sink_input_put(pa_sink_input
*i
) {
760 pa_sink_input_state_t state
;
762 pa_sink_input_assert_ref(i
);
763 pa_assert_ctl_context();
765 pa_assert(i
->state
== PA_SINK_INPUT_INIT
);
767 /* The following fields must be initialized properly */
769 pa_assert(i
->process_rewind
);
772 state
= i
->flags
& PA_SINK_INPUT_START_CORKED
? PA_SINK_INPUT_CORKED
: PA_SINK_INPUT_RUNNING
;
774 update_n_corked(i
, state
);
777 /* We might need to update the sink's volume if we are in flat volume mode. */
778 if (pa_sink_flat_volume_enabled(i
->sink
))
779 pa_sink_set_volume(i
->sink
, NULL
, FALSE
, i
->save_volume
);
781 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)) {
782 pa_assert(pa_cvolume_is_norm(&i
->volume
));
783 pa_assert(pa_cvolume_is_norm(&i
->reference_ratio
));
786 set_real_ratio(i
, &i
->volume
);
789 if (pa_sink_input_is_passthrough(i
))
790 pa_sink_enter_passthrough(i
->sink
);
792 i
->thread_info
.soft_volume
= i
->soft_volume
;
793 i
->thread_info
.muted
= i
->muted
;
795 pa_assert_se(pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
->sink
), PA_SINK_MESSAGE_ADD_INPUT
, i
, 0, NULL
) == 0);
797 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
, i
->index
);
798 pa_hook_fire(&i
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_PUT
], i
);
800 pa_sink_update_status(i
->sink
);
803 /* Called from main context */
804 void pa_sink_input_kill(pa_sink_input
*i
) {
805 pa_sink_input_assert_ref(i
);
806 pa_assert_ctl_context();
807 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
812 /* Called from main context */
813 pa_usec_t
pa_sink_input_get_latency(pa_sink_input
*i
, pa_usec_t
*sink_latency
) {
814 pa_usec_t r
[2] = { 0, 0 };
816 pa_sink_input_assert_ref(i
);
817 pa_assert_ctl_context();
818 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
820 pa_assert_se(pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_GET_LATENCY
, r
, 0, NULL
) == 0);
823 r
[0] += i
->get_latency(i
);
826 *sink_latency
= r
[1];
831 /* Called from thread context */
832 void pa_sink_input_peek(pa_sink_input
*i
, size_t slength
/* in sink bytes */, pa_memchunk
*chunk
, pa_cvolume
*volume
) {
833 pa_bool_t do_volume_adj_here
, need_volume_factor_sink
;
834 pa_bool_t volume_is_norm
;
835 size_t block_size_max_sink
, block_size_max_sink_input
;
839 pa_sink_input_assert_ref(i
);
840 pa_sink_input_assert_io_context(i
);
841 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->thread_info
.state
));
842 pa_assert(pa_frame_aligned(slength
, &i
->sink
->sample_spec
));
846 #ifdef SINK_INPUT_DEBUG
847 pa_log_debug("peek");
850 block_size_max_sink_input
= i
->thread_info
.resampler
?
851 pa_resampler_max_block_size(i
->thread_info
.resampler
) :
852 pa_frame_align(pa_mempool_block_size_max(i
->core
->mempool
), &i
->sample_spec
);
854 block_size_max_sink
= pa_frame_align(pa_mempool_block_size_max(i
->core
->mempool
), &i
->sink
->sample_spec
);
856 /* Default buffer size */
858 slength
= pa_frame_align(CONVERT_BUFFER_LENGTH
, &i
->sink
->sample_spec
);
860 if (slength
> block_size_max_sink
)
861 slength
= block_size_max_sink
;
863 if (i
->thread_info
.resampler
) {
864 ilength
= pa_resampler_request(i
->thread_info
.resampler
, slength
);
867 ilength
= pa_frame_align(CONVERT_BUFFER_LENGTH
, &i
->sample_spec
);
871 /* Length corresponding to slength (without limiting to
872 * block_size_max_sink_input). */
873 ilength_full
= ilength
;
875 if (ilength
> block_size_max_sink_input
)
876 ilength
= block_size_max_sink_input
;
878 /* If the channel maps of the sink and this stream differ, we need
879 * to adjust the volume *before* we resample. Otherwise we can do
880 * it after and leave it for the sink code */
882 do_volume_adj_here
= !pa_channel_map_equal(&i
->channel_map
, &i
->sink
->channel_map
);
883 volume_is_norm
= pa_cvolume_is_norm(&i
->thread_info
.soft_volume
) && !i
->thread_info
.muted
;
884 need_volume_factor_sink
= !pa_cvolume_is_norm(&i
->volume_factor_sink
);
886 while (!pa_memblockq_is_readable(i
->thread_info
.render_memblockq
)) {
889 /* There's nothing in our render queue. We need to fill it up
890 * with data from the implementor. */
892 if (i
->thread_info
.state
== PA_SINK_INPUT_CORKED
||
893 i
->pop(i
, ilength
, &tchunk
) < 0) {
895 /* OK, we're corked or the implementor didn't give us any
896 * data, so let's just hand out silence */
897 pa_atomic_store(&i
->thread_info
.drained
, 1);
899 pa_memblockq_seek(i
->thread_info
.render_memblockq
, (int64_t) slength
, PA_SEEK_RELATIVE
, TRUE
);
900 i
->thread_info
.playing_for
= 0;
901 if (i
->thread_info
.underrun_for
!= (uint64_t) -1) {
902 i
->thread_info
.underrun_for
+= ilength_full
;
903 i
->thread_info
.underrun_for_sink
+= slength
;
908 pa_atomic_store(&i
->thread_info
.drained
, 0);
910 pa_assert(tchunk
.length
> 0);
911 pa_assert(tchunk
.memblock
);
913 i
->thread_info
.underrun_for
= 0;
914 i
->thread_info
.underrun_for_sink
= 0;
915 i
->thread_info
.playing_for
+= tchunk
.length
;
917 while (tchunk
.length
> 0) {
919 pa_bool_t nvfs
= need_volume_factor_sink
;
922 pa_memblock_ref(wchunk
.memblock
);
924 if (wchunk
.length
> block_size_max_sink_input
)
925 wchunk
.length
= block_size_max_sink_input
;
927 /* It might be necessary to adjust the volume here */
928 if (do_volume_adj_here
&& !volume_is_norm
) {
929 pa_memchunk_make_writable(&wchunk
, 0);
931 if (i
->thread_info
.muted
) {
932 pa_silence_memchunk(&wchunk
, &i
->thread_info
.sample_spec
);
935 } else if (!i
->thread_info
.resampler
&& nvfs
) {
938 /* If we don't need a resampler we can merge the
939 * post and the pre volume adjustment into one */
941 pa_sw_cvolume_multiply(&v
, &i
->thread_info
.soft_volume
, &i
->volume_factor_sink
);
942 pa_volume_memchunk(&wchunk
, &i
->thread_info
.sample_spec
, &v
);
946 pa_volume_memchunk(&wchunk
, &i
->thread_info
.sample_spec
, &i
->thread_info
.soft_volume
);
949 if (!i
->thread_info
.resampler
) {
952 pa_memchunk_make_writable(&wchunk
, 0);
953 pa_volume_memchunk(&wchunk
, &i
->sink
->sample_spec
, &i
->volume_factor_sink
);
956 pa_memblockq_push_align(i
->thread_info
.render_memblockq
, &wchunk
);
959 pa_resampler_run(i
->thread_info
.resampler
, &wchunk
, &rchunk
);
961 #ifdef SINK_INPUT_DEBUG
962 pa_log_debug("pushing %lu", (unsigned long) rchunk
.length
);
965 if (rchunk
.memblock
) {
968 pa_memchunk_make_writable(&rchunk
, 0);
969 pa_volume_memchunk(&rchunk
, &i
->sink
->sample_spec
, &i
->volume_factor_sink
);
972 pa_memblockq_push_align(i
->thread_info
.render_memblockq
, &rchunk
);
973 pa_memblock_unref(rchunk
.memblock
);
977 pa_memblock_unref(wchunk
.memblock
);
979 tchunk
.index
+= wchunk
.length
;
980 tchunk
.length
-= wchunk
.length
;
983 pa_memblock_unref(tchunk
.memblock
);
986 pa_assert_se(pa_memblockq_peek(i
->thread_info
.render_memblockq
, chunk
) >= 0);
988 pa_assert(chunk
->length
> 0);
989 pa_assert(chunk
->memblock
);
991 #ifdef SINK_INPUT_DEBUG
992 pa_log_debug("peeking %lu", (unsigned long) chunk
->length
);
995 if (chunk
->length
> block_size_max_sink
)
996 chunk
->length
= block_size_max_sink
;
998 /* Let's see if we had to apply the volume adjustment ourselves,
999 * or if this can be done by the sink for us */
1001 if (do_volume_adj_here
)
1002 /* We had different channel maps, so we already did the adjustment */
1003 pa_cvolume_reset(volume
, i
->sink
->sample_spec
.channels
);
1004 else if (i
->thread_info
.muted
)
1005 /* We've both the same channel map, so let's have the sink do the adjustment for us*/
1006 pa_cvolume_mute(volume
, i
->sink
->sample_spec
.channels
);
1008 *volume
= i
->thread_info
.soft_volume
;
1011 /* Called from thread context */
1012 void pa_sink_input_drop(pa_sink_input
*i
, size_t nbytes
/* in sink sample spec */) {
1014 pa_sink_input_assert_ref(i
);
1015 pa_sink_input_assert_io_context(i
);
1016 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->thread_info
.state
));
1017 pa_assert(pa_frame_aligned(nbytes
, &i
->sink
->sample_spec
));
1018 pa_assert(nbytes
> 0);
1020 #ifdef SINK_INPUT_DEBUG
1021 pa_log_debug("dropping %lu", (unsigned long) nbytes
);
1024 pa_memblockq_drop(i
->thread_info
.render_memblockq
, nbytes
);
1027 /* Called from thread context */
1028 bool pa_sink_input_process_underrun(pa_sink_input
*i
) {
1029 pa_sink_input_assert_ref(i
);
1030 pa_sink_input_assert_io_context(i
);
1032 if (pa_memblockq_is_readable(i
->thread_info
.render_memblockq
))
1035 if (i
->process_underrun
&& i
->process_underrun(i
)) {
1036 /* All valid data has been played back, so we can empty this queue. */
1037 pa_memblockq_silence(i
->thread_info
.render_memblockq
);
1044 /* Called from thread context */
1045 void pa_sink_input_process_rewind(pa_sink_input
*i
, size_t nbytes
/* in sink sample spec */) {
1047 pa_bool_t called
= FALSE
;
1049 pa_sink_input_assert_ref(i
);
1050 pa_sink_input_assert_io_context(i
);
1051 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->thread_info
.state
));
1052 pa_assert(pa_frame_aligned(nbytes
, &i
->sink
->sample_spec
));
1054 #ifdef SINK_INPUT_DEBUG
1055 pa_log_debug("rewind(%lu, %lu)", (unsigned long) nbytes
, (unsigned long) i
->thread_info
.rewrite_nbytes
);
1058 lbq
= pa_memblockq_get_length(i
->thread_info
.render_memblockq
);
1060 if (nbytes
> 0 && !i
->thread_info
.dont_rewind_render
) {
1061 pa_log_debug("Have to rewind %lu bytes on render memblockq.", (unsigned long) nbytes
);
1062 pa_memblockq_rewind(i
->thread_info
.render_memblockq
, nbytes
);
1065 if (i
->thread_info
.rewrite_nbytes
== (size_t) -1) {
1067 /* We were asked to drop all buffered data, and rerequest new
1068 * data from implementor the next time peek() is called */
1070 pa_memblockq_flush_write(i
->thread_info
.render_memblockq
, TRUE
);
1072 } else if (i
->thread_info
.rewrite_nbytes
> 0) {
1073 size_t max_rewrite
, amount
;
1075 /* Calculate how much make sense to rewrite at most */
1076 max_rewrite
= nbytes
+ lbq
;
1078 /* Transform into local domain */
1079 if (i
->thread_info
.resampler
)
1080 max_rewrite
= pa_resampler_request(i
->thread_info
.resampler
, max_rewrite
);
1082 /* Calculate how much of the rewinded data should actually be rewritten */
1083 amount
= PA_MIN(i
->thread_info
.rewrite_nbytes
, max_rewrite
);
1086 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) amount
);
1088 /* Tell the implementor */
1089 if (i
->process_rewind
)
1090 i
->process_rewind(i
, amount
);
1093 /* Convert back to to sink domain */
1094 if (i
->thread_info
.resampler
)
1095 amount
= pa_resampler_result(i
->thread_info
.resampler
, amount
);
1098 /* Ok, now update the write pointer */
1099 pa_memblockq_seek(i
->thread_info
.render_memblockq
, - ((int64_t) amount
), PA_SEEK_RELATIVE
, TRUE
);
1101 if (i
->thread_info
.rewrite_flush
)
1102 pa_memblockq_silence(i
->thread_info
.render_memblockq
);
1104 /* And reset the resampler */
1105 if (i
->thread_info
.resampler
)
1106 pa_resampler_reset(i
->thread_info
.resampler
);
1111 if (i
->process_rewind
)
1112 i
->process_rewind(i
, 0);
1114 i
->thread_info
.rewrite_nbytes
= 0;
1115 i
->thread_info
.rewrite_flush
= FALSE
;
1116 i
->thread_info
.dont_rewind_render
= FALSE
;
1119 /* Called from thread context */
1120 size_t pa_sink_input_get_max_rewind(pa_sink_input
*i
) {
1121 pa_sink_input_assert_ref(i
);
1122 pa_sink_input_assert_io_context(i
);
1124 return i
->thread_info
.resampler
? pa_resampler_request(i
->thread_info
.resampler
, i
->sink
->thread_info
.max_rewind
) : i
->sink
->thread_info
.max_rewind
;
1127 /* Called from thread context */
1128 size_t pa_sink_input_get_max_request(pa_sink_input
*i
) {
1129 pa_sink_input_assert_ref(i
);
1130 pa_sink_input_assert_io_context(i
);
1132 /* We're not verifying the status here, to allow this to be called
1133 * in the state change handler between _INIT and _RUNNING */
1135 return i
->thread_info
.resampler
? pa_resampler_request(i
->thread_info
.resampler
, i
->sink
->thread_info
.max_request
) : i
->sink
->thread_info
.max_request
;
1138 /* Called from thread context */
1139 void pa_sink_input_update_max_rewind(pa_sink_input
*i
, size_t nbytes
/* in the sink's sample spec */) {
1140 pa_sink_input_assert_ref(i
);
1141 pa_sink_input_assert_io_context(i
);
1142 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->thread_info
.state
));
1143 pa_assert(pa_frame_aligned(nbytes
, &i
->sink
->sample_spec
));
1145 pa_memblockq_set_maxrewind(i
->thread_info
.render_memblockq
, nbytes
);
1147 if (i
->update_max_rewind
)
1148 i
->update_max_rewind(i
, i
->thread_info
.resampler
? pa_resampler_request(i
->thread_info
.resampler
, nbytes
) : nbytes
);
1151 /* Called from thread context */
1152 void pa_sink_input_update_max_request(pa_sink_input
*i
, size_t nbytes
/* in the sink's sample spec */) {
1153 pa_sink_input_assert_ref(i
);
1154 pa_sink_input_assert_io_context(i
);
1155 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->thread_info
.state
));
1156 pa_assert(pa_frame_aligned(nbytes
, &i
->sink
->sample_spec
));
1158 if (i
->update_max_request
)
1159 i
->update_max_request(i
, i
->thread_info
.resampler
? pa_resampler_request(i
->thread_info
.resampler
, nbytes
) : nbytes
);
1162 /* Called from thread context */
1163 pa_usec_t
pa_sink_input_set_requested_latency_within_thread(pa_sink_input
*i
, pa_usec_t usec
) {
1164 pa_sink_input_assert_ref(i
);
1165 pa_sink_input_assert_io_context(i
);
1167 if (!(i
->sink
->flags
& PA_SINK_DYNAMIC_LATENCY
))
1168 usec
= i
->sink
->thread_info
.fixed_latency
;
1170 if (usec
!= (pa_usec_t
) -1)
1171 usec
= PA_CLAMP(usec
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1173 i
->thread_info
.requested_sink_latency
= usec
;
1174 pa_sink_invalidate_requested_latency(i
->sink
, TRUE
);
1179 /* Called from main context */
1180 pa_usec_t
pa_sink_input_set_requested_latency(pa_sink_input
*i
, pa_usec_t usec
) {
1181 pa_sink_input_assert_ref(i
);
1182 pa_assert_ctl_context();
1184 if (PA_SINK_INPUT_IS_LINKED(i
->state
) && i
->sink
) {
1185 pa_assert_se(pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_SET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
1189 /* If this sink input is not realized yet or we are being moved,
1190 * we have to touch the thread info data directly */
1193 if (!(i
->sink
->flags
& PA_SINK_DYNAMIC_LATENCY
))
1194 usec
= pa_sink_get_fixed_latency(i
->sink
);
1196 if (usec
!= (pa_usec_t
) -1) {
1197 pa_usec_t min_latency
, max_latency
;
1198 pa_sink_get_latency_range(i
->sink
, &min_latency
, &max_latency
);
1199 usec
= PA_CLAMP(usec
, min_latency
, max_latency
);
1203 i
->thread_info
.requested_sink_latency
= usec
;
1208 /* Called from main context */
1209 pa_usec_t
pa_sink_input_get_requested_latency(pa_sink_input
*i
) {
1210 pa_sink_input_assert_ref(i
);
1211 pa_assert_ctl_context();
1213 if (PA_SINK_INPUT_IS_LINKED(i
->state
) && i
->sink
) {
1215 pa_assert_se(pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
1219 /* If this sink input is not realized yet or we are being moved,
1220 * we have to touch the thread info data directly */
1222 return i
->thread_info
.requested_sink_latency
;
1225 /* Called from main context */
1226 void pa_sink_input_set_volume(pa_sink_input
*i
, const pa_cvolume
*volume
, pa_bool_t save
, pa_bool_t absolute
) {
1229 pa_sink_input_assert_ref(i
);
1230 pa_assert_ctl_context();
1231 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1233 pa_assert(pa_cvolume_valid(volume
));
1234 pa_assert(volume
->channels
== 1 || pa_cvolume_compatible(volume
, &i
->sample_spec
));
1235 pa_assert(i
->volume_writable
);
1237 if (!absolute
&& pa_sink_flat_volume_enabled(i
->sink
)) {
1238 v
= i
->sink
->reference_volume
;
1239 pa_cvolume_remap(&v
, &i
->sink
->channel_map
, &i
->channel_map
);
1241 if (pa_cvolume_compatible(volume
, &i
->sample_spec
))
1242 volume
= pa_sw_cvolume_multiply(&v
, &v
, volume
);
1244 volume
= pa_sw_cvolume_multiply_scalar(&v
, &v
, pa_cvolume_max(volume
));
1246 if (!pa_cvolume_compatible(volume
, &i
->sample_spec
)) {
1248 volume
= pa_cvolume_scale(&v
, pa_cvolume_max(volume
));
1252 if (pa_cvolume_equal(volume
, &i
->volume
)) {
1253 i
->save_volume
= i
->save_volume
|| save
;
1257 i
->volume
= *volume
;
1258 i
->save_volume
= save
;
1260 if (pa_sink_flat_volume_enabled(i
->sink
)) {
1261 /* We are in flat volume mode, so let's update all sink input
1262 * volumes and update the flat volume of the sink */
1264 pa_sink_set_volume(i
->sink
, NULL
, TRUE
, save
);
1267 /* OK, we are in normal volume mode. The volume only affects
1269 set_real_ratio(i
, volume
);
1271 /* Copy the new soft_volume to the thread_info struct */
1272 pa_assert_se(pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_SET_SOFT_VOLUME
, NULL
, 0, NULL
) == 0);
1275 /* The volume changed, let's tell people so */
1276 if (i
->volume_changed
)
1277 i
->volume_changed(i
);
1279 /* The virtual volume changed, let's tell people so */
1280 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1283 void pa_sink_input_add_volume_factor(pa_sink_input
*i
, const char *key
, const pa_cvolume
*volume_factor
) {
1284 struct volume_factor_entry
*v
;
1286 pa_sink_input_assert_ref(i
);
1287 pa_assert_ctl_context();
1288 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1289 pa_assert(volume_factor
);
1291 pa_assert(pa_cvolume_valid(volume_factor
));
1292 pa_assert(volume_factor
->channels
== 1 || pa_cvolume_compatible(volume_factor
, &i
->sample_spec
));
1294 v
= volume_factor_entry_new(key
, volume_factor
);
1295 if (!pa_cvolume_compatible(volume_factor
, &i
->sample_spec
))
1296 pa_cvolume_set(&v
->volume
, i
->sample_spec
.channels
, volume_factor
->values
[0]);
1298 pa_assert_se(pa_hashmap_put(i
->volume_factor_items
, v
->key
, v
) >= 0);
1299 if (pa_hashmap_size(i
->volume_factor_items
) == 1)
1300 i
->volume_factor
= v
->volume
;
1302 pa_sw_cvolume_multiply(&i
->volume_factor
, &i
->volume_factor
, &v
->volume
);
1304 pa_sw_cvolume_multiply(&i
->soft_volume
, &i
->real_ratio
, &i
->volume_factor
);
1306 /* Copy the new soft_volume to the thread_info struct */
1307 pa_assert_se(pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_SET_SOFT_VOLUME
, NULL
, 0, NULL
) == 0);
1310 void pa_sink_input_remove_volume_factor(pa_sink_input
*i
, const char *key
) {
1311 struct volume_factor_entry
*v
;
1313 pa_sink_input_assert_ref(i
);
1315 pa_assert_ctl_context();
1316 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1318 pa_assert_se(v
= pa_hashmap_remove(i
->volume_factor_items
, key
));
1319 volume_factor_entry_free(v
);
1321 switch (pa_hashmap_size(i
->volume_factor_items
)) {
1323 pa_cvolume_reset(&i
->volume_factor
, i
->sample_spec
.channels
);
1326 v
= pa_hashmap_first(i
->volume_factor_items
);
1327 i
->volume_factor
= v
->volume
;
1330 volume_factor_from_hashmap(&i
->volume_factor
, i
->volume_factor_items
, i
->volume_factor
.channels
);
1333 pa_sw_cvolume_multiply(&i
->soft_volume
, &i
->real_ratio
, &i
->volume_factor
);
1335 /* Copy the new soft_volume to the thread_info struct */
1336 pa_assert_se(pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_SET_SOFT_VOLUME
, NULL
, 0, NULL
) == 0);
1339 /* Called from main context */
1340 static void set_real_ratio(pa_sink_input
*i
, const pa_cvolume
*v
) {
1341 pa_sink_input_assert_ref(i
);
1342 pa_assert_ctl_context();
1343 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1344 pa_assert(!v
|| pa_cvolume_compatible(v
, &i
->sample_spec
));
1346 /* This basically calculates:
1348 * i->real_ratio := v
1349 * i->soft_volume := i->real_ratio * i->volume_factor */
1354 pa_cvolume_reset(&i
->real_ratio
, i
->sample_spec
.channels
);
1356 pa_sw_cvolume_multiply(&i
->soft_volume
, &i
->real_ratio
, &i
->volume_factor
);
1357 /* We don't copy the data to the thread_info data. That's left for someone else to do */
1360 /* Called from main or I/O context */
1361 pa_bool_t
pa_sink_input_is_passthrough(pa_sink_input
*i
) {
1362 pa_sink_input_assert_ref(i
);
1364 if (PA_UNLIKELY(!pa_format_info_is_pcm(i
->format
)))
1367 if (PA_UNLIKELY(i
->flags
& PA_SINK_INPUT_PASSTHROUGH
))
1373 /* Called from main context */
1374 pa_bool_t
pa_sink_input_is_volume_readable(pa_sink_input
*i
) {
1375 pa_sink_input_assert_ref(i
);
1376 pa_assert_ctl_context();
1378 return !pa_sink_input_is_passthrough(i
);
1381 /* Called from main context */
1382 pa_cvolume
*pa_sink_input_get_volume(pa_sink_input
*i
, pa_cvolume
*volume
, pa_bool_t absolute
) {
1383 pa_sink_input_assert_ref(i
);
1384 pa_assert_ctl_context();
1385 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1386 pa_assert(pa_sink_input_is_volume_readable(i
));
1388 if (absolute
|| !pa_sink_flat_volume_enabled(i
->sink
))
1389 *volume
= i
->volume
;
1391 *volume
= i
->reference_ratio
;
1396 /* Called from main context */
1397 void pa_sink_input_set_mute(pa_sink_input
*i
, pa_bool_t mute
, pa_bool_t save
) {
1398 pa_sink_input_assert_ref(i
);
1399 pa_assert_ctl_context();
1400 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1402 if (!i
->muted
== !mute
) {
1403 i
->save_muted
= i
->save_muted
|| mute
;
1408 i
->save_muted
= save
;
1410 pa_assert_se(pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_SET_SOFT_MUTE
, NULL
, 0, NULL
) == 0);
1412 /* The mute status changed, let's tell people so */
1413 if (i
->mute_changed
)
1416 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1419 /* Called from main context */
1420 pa_bool_t
pa_sink_input_get_mute(pa_sink_input
*i
) {
1421 pa_sink_input_assert_ref(i
);
1422 pa_assert_ctl_context();
1423 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1428 /* Called from main thread */
1429 void pa_sink_input_update_proplist(pa_sink_input
*i
, pa_update_mode_t mode
, pa_proplist
*p
) {
1430 pa_sink_input_assert_ref(i
);
1431 pa_assert_ctl_context();
1434 pa_proplist_update(i
->proplist
, mode
, p
);
1436 if (PA_SINK_INPUT_IS_LINKED(i
->state
)) {
1437 pa_hook_fire(&i
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_PROPLIST_CHANGED
], i
);
1438 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1442 /* Called from main context */
1443 void pa_sink_input_cork(pa_sink_input
*i
, pa_bool_t b
) {
1444 pa_sink_input_assert_ref(i
);
1445 pa_assert_ctl_context();
1446 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1448 sink_input_set_state(i
, b
? PA_SINK_INPUT_CORKED
: PA_SINK_INPUT_RUNNING
);
1451 /* Called from main context */
1452 int pa_sink_input_set_rate(pa_sink_input
*i
, uint32_t rate
) {
1453 pa_sink_input_assert_ref(i
);
1454 pa_assert_ctl_context();
1455 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1456 pa_return_val_if_fail(i
->thread_info
.resampler
, -PA_ERR_BADSTATE
);
1458 if (i
->sample_spec
.rate
== rate
)
1461 i
->sample_spec
.rate
= rate
;
1463 pa_asyncmsgq_post(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_SET_RATE
, PA_UINT_TO_PTR(rate
), 0, NULL
, NULL
);
1465 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1469 /* Called from main context */
1470 void pa_sink_input_set_name(pa_sink_input
*i
, const char *name
) {
1472 pa_sink_input_assert_ref(i
);
1473 pa_assert_ctl_context();
1475 if (!name
&& !pa_proplist_contains(i
->proplist
, PA_PROP_MEDIA_NAME
))
1478 old
= pa_proplist_gets(i
->proplist
, PA_PROP_MEDIA_NAME
);
1480 if (old
&& name
&& pa_streq(old
, name
))
1484 pa_proplist_sets(i
->proplist
, PA_PROP_MEDIA_NAME
, name
);
1486 pa_proplist_unset(i
->proplist
, PA_PROP_MEDIA_NAME
);
1488 if (PA_SINK_INPUT_IS_LINKED(i
->state
)) {
1489 pa_hook_fire(&i
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_PROPLIST_CHANGED
], i
);
1490 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1494 /* Called from main context */
1495 pa_resample_method_t
pa_sink_input_get_resample_method(pa_sink_input
*i
) {
1496 pa_sink_input_assert_ref(i
);
1497 pa_assert_ctl_context();
1499 return i
->actual_resample_method
;
1502 /* Called from main context */
1503 pa_bool_t
pa_sink_input_may_move(pa_sink_input
*i
) {
1504 pa_sink_input_assert_ref(i
);
1505 pa_assert_ctl_context();
1506 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1508 if (i
->flags
& PA_SINK_INPUT_DONT_MOVE
)
1511 if (i
->sync_next
|| i
->sync_prev
) {
1512 pa_log_warn("Moving synchronized streams not supported.");
1519 static pa_bool_t
find_filter_sink_input(pa_sink_input
*target
, pa_sink
*s
) {
1521 while (s
&& s
->input_to_master
) {
1522 if (s
->input_to_master
== target
)
1524 s
= s
->input_to_master
->sink
;
1525 pa_assert(i
++ < 100);
1530 /* Called from main context */
1531 pa_bool_t
pa_sink_input_may_move_to(pa_sink_input
*i
, pa_sink
*dest
) {
1532 pa_sink_input_assert_ref(i
);
1533 pa_assert_ctl_context();
1534 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1535 pa_sink_assert_ref(dest
);
1537 if (dest
== i
->sink
)
1540 if (!pa_sink_input_may_move(i
))
1543 /* Make sure we're not creating a filter sink cycle */
1544 if (find_filter_sink_input(i
, dest
)) {
1545 pa_log_debug("Can't connect input to %s, as that would create a cycle.", dest
->name
);
1549 if (pa_idxset_size(dest
->inputs
) >= PA_MAX_INPUTS_PER_SINK
) {
1550 pa_log_warn("Failed to move sink input: too many inputs per sink.");
1554 if (check_passthrough_connection(pa_sink_input_is_passthrough(i
), dest
) < 0)
1558 if (!i
->may_move_to(i
, dest
))
1564 /* Called from main context */
1565 int pa_sink_input_start_move(pa_sink_input
*i
) {
1566 pa_source_output
*o
, *p
= NULL
;
1567 struct volume_factor_entry
*v
;
1571 pa_sink_input_assert_ref(i
);
1572 pa_assert_ctl_context();
1573 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1576 if (!pa_sink_input_may_move(i
))
1577 return -PA_ERR_NOTSUPPORTED
;
1579 if ((r
= pa_hook_fire(&i
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_MOVE_START
], i
)) < 0)
1582 /* Kill directly connected outputs */
1583 while ((o
= pa_idxset_first(i
->direct_outputs
, NULL
))) {
1585 pa_source_output_kill(o
);
1588 pa_assert(pa_idxset_isempty(i
->direct_outputs
));
1590 pa_idxset_remove_by_data(i
->sink
->inputs
, i
, NULL
);
1592 if (pa_sink_input_get_state(i
) == PA_SINK_INPUT_CORKED
)
1593 pa_assert_se(i
->sink
->n_corked
-- >= 1);
1595 if (pa_sink_input_is_passthrough(i
))
1596 pa_sink_leave_passthrough(i
->sink
);
1598 if (pa_sink_flat_volume_enabled(i
->sink
))
1599 /* We might need to update the sink's volume if we are in flat
1601 pa_sink_set_volume(i
->sink
, NULL
, FALSE
, FALSE
);
1603 pa_assert_se(pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
->sink
), PA_SINK_MESSAGE_START_MOVE
, i
, 0, NULL
) == 0);
1605 pa_sink_update_status(i
->sink
);
1607 PA_HASHMAP_FOREACH(v
, i
->volume_factor_sink_items
, state
)
1608 pa_cvolume_remap(&v
->volume
, &i
->sink
->channel_map
, &i
->channel_map
);
1610 pa_cvolume_remap(&i
->volume_factor_sink
, &i
->sink
->channel_map
, &i
->channel_map
);
1614 pa_sink_input_unref(i
);
1619 /* Called from main context. If i has an origin sink that uses volume sharing,
1620 * then also the origin sink and all streams connected to it need to update
1621 * their volume - this function does all that by using recursion. */
1622 static void update_volume_due_to_moving(pa_sink_input
*i
, pa_sink
*dest
) {
1623 pa_cvolume old_volume
;
1627 pa_assert(i
->sink
); /* The destination sink should already be set. */
1629 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)) {
1630 pa_sink
*root_sink
= pa_sink_get_master(i
->sink
);
1631 pa_sink_input
*origin_sink_input
;
1634 if (PA_UNLIKELY(!root_sink
))
1637 if (pa_sink_flat_volume_enabled(i
->sink
)) {
1638 /* Ok, so the origin sink uses volume sharing, and flat volume is
1639 * enabled. The volume will have to be updated as follows:
1641 * i->volume := i->sink->real_volume
1642 * (handled later by pa_sink_set_volume)
1643 * i->reference_ratio := i->volume / i->sink->reference_volume
1644 * (handled later by pa_sink_set_volume)
1645 * i->real_ratio stays unchanged
1646 * (streams whose origin sink uses volume sharing should
1647 * always have real_ratio of 0 dB)
1648 * i->soft_volume stays unchanged
1649 * (streams whose origin sink uses volume sharing should
1650 * always have volume_factor as soft_volume, so no change
1651 * should be needed) */
1653 pa_assert(pa_cvolume_is_norm(&i
->real_ratio
));
1654 pa_assert(pa_cvolume_equal(&i
->soft_volume
, &i
->volume_factor
));
1656 /* Notifications will be sent by pa_sink_set_volume(). */
1659 /* Ok, so the origin sink uses volume sharing, and flat volume is
1660 * disabled. The volume will have to be updated as follows:
1663 * i->reference_ratio := 0 dB
1664 * i->real_ratio stays unchanged
1665 * (streams whose origin sink uses volume sharing should
1666 * always have real_ratio of 0 dB)
1667 * i->soft_volume stays unchanged
1668 * (streams whose origin sink uses volume sharing should
1669 * always have volume_factor as soft_volume, so no change
1670 * should be needed) */
1672 old_volume
= i
->volume
;
1673 pa_cvolume_reset(&i
->volume
, i
->volume
.channels
);
1674 pa_cvolume_reset(&i
->reference_ratio
, i
->reference_ratio
.channels
);
1675 pa_assert(pa_cvolume_is_norm(&i
->real_ratio
));
1676 pa_assert(pa_cvolume_equal(&i
->soft_volume
, &i
->volume_factor
));
1678 /* Notify others about the changed sink input volume. */
1679 if (!pa_cvolume_equal(&i
->volume
, &old_volume
)) {
1680 if (i
->volume_changed
)
1681 i
->volume_changed(i
);
1683 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1687 /* Additionally, the origin sink volume needs updating:
1689 * i->origin_sink->reference_volume := root_sink->reference_volume
1690 * i->origin_sink->real_volume := root_sink->real_volume
1691 * i->origin_sink->soft_volume stays unchanged
1692 * (sinks that use volume sharing should always have
1693 * soft_volume of 0 dB) */
1695 old_volume
= i
->origin_sink
->reference_volume
;
1697 i
->origin_sink
->reference_volume
= root_sink
->reference_volume
;
1698 pa_cvolume_remap(&i
->origin_sink
->reference_volume
, &root_sink
->channel_map
, &i
->origin_sink
->channel_map
);
1700 i
->origin_sink
->real_volume
= root_sink
->real_volume
;
1701 pa_cvolume_remap(&i
->origin_sink
->real_volume
, &root_sink
->channel_map
, &i
->origin_sink
->channel_map
);
1703 pa_assert(pa_cvolume_is_norm(&i
->origin_sink
->soft_volume
));
1705 /* Notify others about the changed sink volume. If you wonder whether
1706 * i->origin_sink->set_volume() should be called somewhere, that's not
1707 * the case, because sinks that use volume sharing shouldn't have any
1708 * internal volume that set_volume() would update. If you wonder
1709 * whether the thread_info variables should be synced, yes, they
1710 * should, and it's done by the PA_SINK_MESSAGE_FINISH_MOVE message
1712 if (!pa_cvolume_equal(&i
->origin_sink
->reference_volume
, &old_volume
))
1713 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->origin_sink
->index
);
1715 /* Recursively update origin sink inputs. */
1716 PA_IDXSET_FOREACH(origin_sink_input
, i
->origin_sink
->inputs
, idx
)
1717 update_volume_due_to_moving(origin_sink_input
, dest
);
1720 old_volume
= i
->volume
;
1722 if (pa_sink_flat_volume_enabled(i
->sink
)) {
1723 /* Ok, so this is a regular stream, and flat volume is enabled. The
1724 * volume will have to be updated as follows:
1726 * i->volume := i->reference_ratio * i->sink->reference_volume
1727 * i->reference_ratio stays unchanged
1728 * i->real_ratio := i->volume / i->sink->real_volume
1729 * (handled later by pa_sink_set_volume)
1730 * i->soft_volume := i->real_ratio * i->volume_factor
1731 * (handled later by pa_sink_set_volume) */
1733 i
->volume
= i
->sink
->reference_volume
;
1734 pa_cvolume_remap(&i
->volume
, &i
->sink
->channel_map
, &i
->channel_map
);
1735 pa_sw_cvolume_multiply(&i
->volume
, &i
->volume
, &i
->reference_ratio
);
1738 /* Ok, so this is a regular stream, and flat volume is disabled.
1739 * The volume will have to be updated as follows:
1741 * i->volume := i->reference_ratio
1742 * i->reference_ratio stays unchanged
1743 * i->real_ratio := i->reference_ratio
1744 * i->soft_volume := i->real_ratio * i->volume_factor */
1746 i
->volume
= i
->reference_ratio
;
1747 i
->real_ratio
= i
->reference_ratio
;
1748 pa_sw_cvolume_multiply(&i
->soft_volume
, &i
->real_ratio
, &i
->volume_factor
);
1751 /* Notify others about the changed sink input volume. */
1752 if (!pa_cvolume_equal(&i
->volume
, &old_volume
)) {
1753 /* XXX: In case i->sink has flat volume enabled, then real_ratio
1754 * and soft_volume are not updated yet. Let's hope that the
1755 * callback implementation doesn't care about those variables... */
1756 if (i
->volume_changed
)
1757 i
->volume_changed(i
);
1759 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1763 /* If i->sink == dest, then recursion has finished, and we can finally call
1764 * pa_sink_set_volume(), which will do the rest of the updates. */
1765 if ((i
->sink
== dest
) && pa_sink_flat_volume_enabled(i
->sink
))
1766 pa_sink_set_volume(i
->sink
, NULL
, FALSE
, i
->save_volume
);
1769 /* Called from main context */
1770 int pa_sink_input_finish_move(pa_sink_input
*i
, pa_sink
*dest
, pa_bool_t save
) {
1771 struct volume_factor_entry
*v
;
1774 pa_sink_input_assert_ref(i
);
1775 pa_assert_ctl_context();
1776 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1777 pa_assert(!i
->sink
);
1778 pa_sink_assert_ref(dest
);
1780 if (!pa_sink_input_may_move_to(i
, dest
))
1781 return -PA_ERR_NOTSUPPORTED
;
1783 if (pa_sink_input_is_passthrough(i
) && !pa_sink_check_format(dest
, i
->format
)) {
1784 pa_proplist
*p
= pa_proplist_new();
1785 pa_log_debug("New sink doesn't support stream format, sending format-changed and killing");
1786 /* Tell the client what device we want to be on if it is going to
1788 pa_proplist_sets(p
, "device", dest
->name
);
1789 pa_sink_input_send_event(i
, PA_STREAM_EVENT_FORMAT_LOST
, p
);
1790 pa_proplist_free(p
);
1791 return -PA_ERR_NOTSUPPORTED
;
1794 if (!(i
->flags
& PA_SINK_INPUT_VARIABLE_RATE
) &&
1795 !pa_sample_spec_equal(&i
->sample_spec
, &dest
->sample_spec
)) {
1796 /* try to change dest sink rate if possible without glitches.
1797 module-suspend-on-idle resumes destination sink with
1798 SINK_INPUT_MOVE_FINISH hook */
1800 pa_log_info("Trying to change sample rate");
1801 if (pa_sink_update_rate(dest
, i
->sample_spec
.rate
, pa_sink_input_is_passthrough(i
)) == TRUE
)
1802 pa_log_info("Rate changed to %u Hz", dest
->sample_spec
.rate
);
1809 i
->save_sink
= save
;
1810 pa_idxset_put(dest
->inputs
, pa_sink_input_ref(i
), NULL
);
1812 PA_HASHMAP_FOREACH(v
, i
->volume_factor_sink_items
, state
)
1813 pa_cvolume_remap(&v
->volume
, &i
->channel_map
, &i
->sink
->channel_map
);
1815 pa_cvolume_remap(&i
->volume_factor_sink
, &i
->channel_map
, &i
->sink
->channel_map
);
1817 if (pa_sink_input_get_state(i
) == PA_SINK_INPUT_CORKED
)
1818 i
->sink
->n_corked
++;
1820 pa_sink_input_update_rate(i
);
1822 pa_sink_update_status(dest
);
1824 update_volume_due_to_moving(i
, dest
);
1826 if (pa_sink_input_is_passthrough(i
))
1827 pa_sink_enter_passthrough(i
->sink
);
1829 pa_assert_se(pa_asyncmsgq_send(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
->sink
), PA_SINK_MESSAGE_FINISH_MOVE
, i
, 0, NULL
) == 0);
1831 pa_log_debug("Successfully moved sink input %i to %s.", i
->index
, dest
->name
);
1833 /* Notify everyone */
1834 pa_hook_fire(&i
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH
], i
);
1835 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1840 /* Called from main context */
1841 void pa_sink_input_fail_move(pa_sink_input
*i
) {
1843 pa_sink_input_assert_ref(i
);
1844 pa_assert_ctl_context();
1845 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1846 pa_assert(!i
->sink
);
1848 /* Check if someone wants this sink input? */
1849 if (pa_hook_fire(&i
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_MOVE_FAIL
], i
) == PA_HOOK_STOP
)
1855 pa_sink_input_kill(i
);
1858 /* Called from main context */
1859 int pa_sink_input_move_to(pa_sink_input
*i
, pa_sink
*dest
, pa_bool_t save
) {
1862 pa_sink_input_assert_ref(i
);
1863 pa_assert_ctl_context();
1864 pa_assert(PA_SINK_INPUT_IS_LINKED(i
->state
));
1866 pa_sink_assert_ref(dest
);
1868 if (dest
== i
->sink
)
1871 if (!pa_sink_input_may_move_to(i
, dest
))
1872 return -PA_ERR_NOTSUPPORTED
;
1874 pa_sink_input_ref(i
);
1876 if ((r
= pa_sink_input_start_move(i
)) < 0) {
1877 pa_sink_input_unref(i
);
1881 if ((r
= pa_sink_input_finish_move(i
, dest
, save
)) < 0) {
1882 pa_sink_input_fail_move(i
);
1883 pa_sink_input_unref(i
);
1887 pa_sink_input_unref(i
);
1892 /* Called from IO thread context */
1893 void pa_sink_input_set_state_within_thread(pa_sink_input
*i
, pa_sink_input_state_t state
) {
1894 pa_bool_t corking
, uncorking
;
1896 pa_sink_input_assert_ref(i
);
1897 pa_sink_input_assert_io_context(i
);
1899 if (state
== i
->thread_info
.state
)
1902 if ((state
== PA_SINK_INPUT_DRAINED
|| state
== PA_SINK_INPUT_RUNNING
) &&
1903 !(i
->thread_info
.state
== PA_SINK_INPUT_DRAINED
|| i
->thread_info
.state
!= PA_SINK_INPUT_RUNNING
))
1904 pa_atomic_store(&i
->thread_info
.drained
, 1);
1906 corking
= state
== PA_SINK_INPUT_CORKED
&& i
->thread_info
.state
== PA_SINK_INPUT_RUNNING
;
1907 uncorking
= i
->thread_info
.state
== PA_SINK_INPUT_CORKED
&& state
== PA_SINK_INPUT_RUNNING
;
1909 if (i
->state_change
)
1910 i
->state_change(i
, state
);
1914 pa_log_debug("Requesting rewind due to corking");
1916 /* This will tell the implementing sink input driver to rewind
1917 * so that the unplayed already mixed data is not lost */
1918 pa_sink_input_request_rewind(i
, 0, TRUE
, TRUE
, FALSE
);
1920 /* Set the corked state *after* requesting rewind */
1921 i
->thread_info
.state
= state
;
1923 } else if (uncorking
) {
1925 pa_log_debug("Requesting rewind due to uncorking");
1927 i
->thread_info
.underrun_for
= (uint64_t) -1;
1928 i
->thread_info
.underrun_for_sink
= 0;
1929 i
->thread_info
.playing_for
= 0;
1931 /* Set the uncorked state *before* requesting rewind */
1932 i
->thread_info
.state
= state
;
1934 /* OK, we're being uncorked. Make sure we're not rewound when
1935 * the hw buffer is remixed and request a remix. */
1936 pa_sink_input_request_rewind(i
, 0, FALSE
, TRUE
, TRUE
);
1938 /* We may not be corking or uncorking, but we still need to set the state. */
1939 i
->thread_info
.state
= state
;
1942 /* Called from thread context, except when it is not. */
1943 int pa_sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1944 pa_sink_input
*i
= PA_SINK_INPUT(o
);
1945 pa_sink_input_assert_ref(i
);
1949 case PA_SINK_INPUT_MESSAGE_SET_SOFT_VOLUME
:
1950 if (!pa_cvolume_equal(&i
->thread_info
.soft_volume
, &i
->soft_volume
)) {
1951 i
->thread_info
.soft_volume
= i
->soft_volume
;
1952 pa_sink_input_request_rewind(i
, 0, TRUE
, FALSE
, FALSE
);
1956 case PA_SINK_INPUT_MESSAGE_SET_SOFT_MUTE
:
1957 if (i
->thread_info
.muted
!= i
->muted
) {
1958 i
->thread_info
.muted
= i
->muted
;
1959 pa_sink_input_request_rewind(i
, 0, TRUE
, FALSE
, FALSE
);
1963 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
1964 pa_usec_t
*r
= userdata
;
1966 r
[0] += pa_bytes_to_usec(pa_memblockq_get_length(i
->thread_info
.render_memblockq
), &i
->sink
->sample_spec
);
1967 r
[1] += pa_sink_get_latency_within_thread(i
->sink
);
1972 case PA_SINK_INPUT_MESSAGE_SET_RATE
:
1974 i
->thread_info
.sample_spec
.rate
= PA_PTR_TO_UINT(userdata
);
1975 pa_resampler_set_input_rate(i
->thread_info
.resampler
, PA_PTR_TO_UINT(userdata
));
1979 case PA_SINK_INPUT_MESSAGE_SET_STATE
: {
1980 pa_sink_input
*ssync
;
1982 pa_sink_input_set_state_within_thread(i
, PA_PTR_TO_UINT(userdata
));
1984 for (ssync
= i
->thread_info
.sync_prev
; ssync
; ssync
= ssync
->thread_info
.sync_prev
)
1985 pa_sink_input_set_state_within_thread(ssync
, PA_PTR_TO_UINT(userdata
));
1987 for (ssync
= i
->thread_info
.sync_next
; ssync
; ssync
= ssync
->thread_info
.sync_next
)
1988 pa_sink_input_set_state_within_thread(ssync
, PA_PTR_TO_UINT(userdata
));
1993 case PA_SINK_INPUT_MESSAGE_SET_REQUESTED_LATENCY
: {
1994 pa_usec_t
*usec
= userdata
;
1996 *usec
= pa_sink_input_set_requested_latency_within_thread(i
, *usec
);
2000 case PA_SINK_INPUT_MESSAGE_GET_REQUESTED_LATENCY
: {
2001 pa_usec_t
*r
= userdata
;
2003 *r
= i
->thread_info
.requested_sink_latency
;
2008 return -PA_ERR_NOTIMPLEMENTED
;
2011 /* Called from main thread */
2012 pa_sink_input_state_t
pa_sink_input_get_state(pa_sink_input
*i
) {
2013 pa_sink_input_assert_ref(i
);
2014 pa_assert_ctl_context();
2016 if (i
->state
== PA_SINK_INPUT_RUNNING
|| i
->state
== PA_SINK_INPUT_DRAINED
)
2017 return pa_atomic_load(&i
->thread_info
.drained
) ? PA_SINK_INPUT_DRAINED
: PA_SINK_INPUT_RUNNING
;
2022 /* Called from IO context */
2023 pa_bool_t
pa_sink_input_safe_to_remove(pa_sink_input
*i
) {
2024 pa_sink_input_assert_ref(i
);
2025 pa_sink_input_assert_io_context(i
);
2027 if (PA_SINK_INPUT_IS_LINKED(i
->thread_info
.state
))
2028 return pa_memblockq_is_empty(i
->thread_info
.render_memblockq
);
2033 /* Called from IO context */
2034 void pa_sink_input_request_rewind(
2036 size_t nbytes
/* in our sample spec */,
2039 pa_bool_t dont_rewind_render
) {
2043 /* If 'rewrite' is TRUE the sink is rewound as far as requested
2044 * and possible and the exact value of this is passed back the
2045 * implementor via process_rewind(). If 'flush' is also TRUE all
2046 * already rendered data is also dropped.
2048 * If 'rewrite' is FALSE the sink is rewound as far as requested
2049 * and possible and the already rendered data is dropped so that
2050 * in the next iteration we read new data from the
2051 * implementor. This implies 'flush' is TRUE. If
2052 * dont_rewind_render is TRUE then the render memblockq is not
2055 /* nbytes = 0 means maximum rewind request */
2057 pa_sink_input_assert_ref(i
);
2058 pa_sink_input_assert_io_context(i
);
2059 pa_assert(rewrite
|| flush
);
2060 pa_assert(!dont_rewind_render
|| !rewrite
);
2062 /* We don't take rewind requests while we are corked */
2063 if (i
->thread_info
.state
== PA_SINK_INPUT_CORKED
)
2066 nbytes
= PA_MAX(i
->thread_info
.rewrite_nbytes
, nbytes
);
2068 #ifdef SINK_INPUT_DEBUG
2069 pa_log_debug("request rewrite %zu", nbytes
);
2072 /* Calculate how much we can rewind locally without having to
2075 lbq
= pa_memblockq_get_length(i
->thread_info
.render_memblockq
);
2079 /* Check if rewinding for the maximum is requested, and if so, fix up */
2082 /* Calculate maximum number of bytes that could be rewound in theory */
2083 nbytes
= i
->sink
->thread_info
.max_rewind
+ lbq
;
2085 /* Transform from sink domain */
2086 if (i
->thread_info
.resampler
)
2087 nbytes
= pa_resampler_request(i
->thread_info
.resampler
, nbytes
);
2090 /* Remember how much we actually want to rewrite */
2091 if (i
->thread_info
.rewrite_nbytes
!= (size_t) -1) {
2093 /* Make sure to not overwrite over underruns */
2094 if (nbytes
> i
->thread_info
.playing_for
)
2095 nbytes
= (size_t) i
->thread_info
.playing_for
;
2097 i
->thread_info
.rewrite_nbytes
= nbytes
;
2099 i
->thread_info
.rewrite_nbytes
= (size_t) -1;
2102 i
->thread_info
.rewrite_flush
=
2103 i
->thread_info
.rewrite_flush
|| flush
;
2105 i
->thread_info
.dont_rewind_render
=
2106 i
->thread_info
.dont_rewind_render
||
2109 /* nbytes is -1 if some earlier rewind request had rewrite == false. */
2110 if (nbytes
!= (size_t) -1) {
2112 /* Transform to sink domain */
2113 if (i
->thread_info
.resampler
)
2114 nbytes
= pa_resampler_result(i
->thread_info
.resampler
, nbytes
);
2117 pa_sink_request_rewind(i
->sink
, nbytes
- lbq
);
2119 /* This call will make sure process_rewind() is called later */
2120 pa_sink_request_rewind(i
->sink
, 0);
2124 /* Called from main context */
2125 pa_memchunk
* pa_sink_input_get_silence(pa_sink_input
*i
, pa_memchunk
*ret
) {
2126 pa_sink_input_assert_ref(i
);
2127 pa_assert_ctl_context();
2130 /* FIXME: Shouldn't access resampler object from main context! */
2132 pa_silence_memchunk_get(
2133 &i
->core
->silence_cache
,
2137 i
->thread_info
.resampler
? pa_resampler_max_block_size(i
->thread_info
.resampler
) : 0);
2142 /* Called from main context */
2143 void pa_sink_input_send_event(pa_sink_input
*i
, const char *event
, pa_proplist
*data
) {
2144 pa_proplist
*pl
= NULL
;
2145 pa_sink_input_send_event_hook_data hook_data
;
2147 pa_sink_input_assert_ref(i
);
2148 pa_assert_ctl_context();
2155 data
= pl
= pa_proplist_new();
2157 hook_data
.sink_input
= i
;
2158 hook_data
.data
= data
;
2159 hook_data
.event
= event
;
2161 if (pa_hook_fire(&i
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_SEND_EVENT
], &hook_data
) < 0)
2164 i
->send_event(i
, event
, data
);
2168 pa_proplist_free(pl
);
2171 /* Called from main context */
2172 /* Updates the sink input's resampler with whatever the current sink requires
2173 * -- useful when the underlying sink's rate might have changed */
2174 int pa_sink_input_update_rate(pa_sink_input
*i
) {
2175 pa_resampler
*new_resampler
;
2176 char *memblockq_name
;
2178 pa_sink_input_assert_ref(i
);
2179 pa_assert_ctl_context();
2181 if (i
->thread_info
.resampler
&&
2182 pa_sample_spec_equal(pa_resampler_output_sample_spec(i
->thread_info
.resampler
), &i
->sink
->sample_spec
) &&
2183 pa_channel_map_equal(pa_resampler_output_channel_map(i
->thread_info
.resampler
), &i
->sink
->channel_map
))
2185 new_resampler
= i
->thread_info
.resampler
;
2187 else if (!pa_sink_input_is_passthrough(i
) &&
2188 ((i
->flags
& PA_SINK_INPUT_VARIABLE_RATE
) ||
2189 !pa_sample_spec_equal(&i
->sample_spec
, &i
->sink
->sample_spec
) ||
2190 !pa_channel_map_equal(&i
->channel_map
, &i
->sink
->channel_map
))) {
2192 new_resampler
= pa_resampler_new(i
->core
->mempool
,
2193 &i
->sample_spec
, &i
->channel_map
,
2194 &i
->sink
->sample_spec
, &i
->sink
->channel_map
,
2195 i
->requested_resample_method
,
2196 ((i
->flags
& PA_SINK_INPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
2197 ((i
->flags
& PA_SINK_INPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
2198 (i
->core
->disable_remixing
|| (i
->flags
& PA_SINK_INPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0));
2200 if (!new_resampler
) {
2201 pa_log_warn("Unsupported resampling operation.");
2202 return -PA_ERR_NOTSUPPORTED
;
2205 new_resampler
= NULL
;
2207 if (new_resampler
== i
->thread_info
.resampler
)
2210 if (i
->thread_info
.resampler
)
2211 pa_resampler_free(i
->thread_info
.resampler
);
2213 i
->thread_info
.resampler
= new_resampler
;
2215 pa_memblockq_free(i
->thread_info
.render_memblockq
);
2217 memblockq_name
= pa_sprintf_malloc("sink input render_memblockq [%u]", i
->index
);
2218 i
->thread_info
.render_memblockq
= pa_memblockq_new(
2221 MEMBLOCKQ_MAXLENGTH
,
2223 &i
->sink
->sample_spec
,
2228 pa_xfree(memblockq_name
);
2230 i
->actual_resample_method
= new_resampler
? pa_resampler_get_method(new_resampler
) : PA_RESAMPLER_INVALID
;
2232 pa_log_debug("Updated resampler for sink input %d", i
->index
);