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
31 #include <pulse/utf8.h>
32 #include <pulse/xmalloc.h>
33 #include <pulse/util.h>
35 #include <pulsecore/sample-util.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 MEMBLOCKQ_MAXLENGTH (32*1024*1024)
45 #define CONVERT_BUFFER_LENGTH (PA_PAGE_SIZE)
47 PA_DEFINE_PUBLIC_CLASS(pa_sink_input, pa_msgobject);
49 static void sink_input_free(pa_object *o);
50 static void set_real_ratio(pa_sink_input *i, const pa_cvolume *v);
52 static int check_passthrough_connection(pa_sink_input_flags_t flags, pa_sink *dest) {
54 if (dest->flags & PA_SINK_PASSTHROUGH) {
56 if (pa_idxset_size(dest->inputs) > 0) {
61 alt_i = pa_idxset_first(dest->inputs, &idx);
63 /* only need to check the first input is not PASSTHROUGH */
64 if (alt_i->flags & PA_SINK_INPUT_PASSTHROUGH) {
65 pa_log_warn("Sink is already connected to PASSTHROUGH input");
69 /* Current inputs are PCM, check new input is not PASSTHROUGH */
70 if (flags & PA_SINK_INPUT_PASSTHROUGH) {
71 pa_log_warn("Sink is already connected, cannot accept new PASSTHROUGH INPUT");
77 if (flags & PA_SINK_INPUT_PASSTHROUGH) {
78 pa_log_warn("Cannot connect PASSTHROUGH sink input to sink without PASSTHROUGH capabilities");
79 return -PA_ERR_INVALID;
85 pa_sink_input_new_data* pa_sink_input_new_data_init(pa_sink_input_new_data *data) {
89 data->resample_method = PA_RESAMPLER_INVALID;
90 data->proplist = pa_proplist_new();
91 data->volume_writable = TRUE;
96 void pa_sink_input_new_data_set_sample_spec(pa_sink_input_new_data *data, const pa_sample_spec *spec) {
99 if ((data->sample_spec_is_set = !!spec))
100 data->sample_spec = *spec;
103 void pa_sink_input_new_data_set_channel_map(pa_sink_input_new_data *data, const pa_channel_map *map) {
106 if ((data->channel_map_is_set = !!map))
107 data->channel_map = *map;
110 void pa_sink_input_new_data_set_volume(pa_sink_input_new_data *data, const pa_cvolume *volume) {
112 pa_assert(data->volume_writable);
114 if ((data->volume_is_set = !!volume))
115 data->volume = *volume;
118 void pa_sink_input_new_data_apply_volume_factor(pa_sink_input_new_data *data, const pa_cvolume *volume_factor) {
120 pa_assert(volume_factor);
122 if (data->volume_factor_is_set)
123 pa_sw_cvolume_multiply(&data->volume_factor, &data->volume_factor, volume_factor);
125 data->volume_factor_is_set = TRUE;
126 data->volume_factor = *volume_factor;
130 void pa_sink_input_new_data_apply_volume_factor_sink(pa_sink_input_new_data *data, const pa_cvolume *volume_factor) {
132 pa_assert(volume_factor);
134 if (data->volume_factor_sink_is_set)
135 pa_sw_cvolume_multiply(&data->volume_factor_sink, &data->volume_factor_sink, volume_factor);
137 data->volume_factor_sink_is_set = TRUE;
138 data->volume_factor_sink = *volume_factor;
142 void pa_sink_input_new_data_set_muted(pa_sink_input_new_data *data, pa_bool_t mute) {
145 data->muted_is_set = TRUE;
146 data->muted = !!mute;
149 void pa_sink_input_new_data_done(pa_sink_input_new_data *data) {
152 pa_proplist_free(data->proplist);
155 /* Called from main context */
156 static void reset_callbacks(pa_sink_input *i) {
160 i->process_rewind = NULL;
161 i->update_max_rewind = NULL;
162 i->update_max_request = NULL;
163 i->update_sink_requested_latency = NULL;
164 i->update_sink_latency_range = NULL;
165 i->update_sink_fixed_latency = NULL;
169 i->suspend_within_thread = NULL;
172 i->get_latency = NULL;
173 i->state_change = NULL;
174 i->may_move_to = NULL;
175 i->send_event = NULL;
176 i->volume_changed = NULL;
177 i->mute_changed = NULL;
180 /* Called from main context */
181 int pa_sink_input_new(
184 pa_sink_input_new_data *data) {
187 pa_resampler *resampler = NULL;
188 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
189 pa_channel_map original_cm;
196 pa_assert_ctl_context();
199 pa_proplist_update(data->proplist, PA_UPDATE_MERGE, data->client->proplist);
201 if (data->origin_sink && (data->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER))
202 data->volume_writable = FALSE;
204 if ((r = pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], data)) < 0)
207 pa_return_val_if_fail(!data->driver || pa_utf8_valid(data->driver), -PA_ERR_INVALID);
210 data->sink = pa_namereg_get(core, NULL, PA_NAMEREG_SINK);
211 data->save_sink = FALSE;
214 pa_return_val_if_fail(data->sink, -PA_ERR_NOENTITY);
215 pa_return_val_if_fail(PA_SINK_IS_LINKED(pa_sink_get_state(data->sink)), -PA_ERR_BADSTATE);
216 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);
218 r = check_passthrough_connection(data->flags, data->sink);
219 pa_return_val_if_fail(r == PA_OK, r);
221 if (!data->sample_spec_is_set)
222 data->sample_spec = data->sink->sample_spec;
224 pa_return_val_if_fail(pa_sample_spec_valid(&data->sample_spec), -PA_ERR_INVALID);
226 if (!data->channel_map_is_set) {
227 if (pa_channel_map_compatible(&data->sink->channel_map, &data->sample_spec))
228 data->channel_map = data->sink->channel_map;
230 pa_channel_map_init_extend(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
233 pa_return_val_if_fail(pa_channel_map_compatible(&data->channel_map, &data->sample_spec), -PA_ERR_INVALID);
235 if (!data->volume_is_set) {
236 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
237 data->volume_is_absolute = FALSE;
238 data->save_volume = FALSE;
241 pa_return_val_if_fail(pa_cvolume_compatible(&data->volume, &data->sample_spec), -PA_ERR_INVALID);
243 if (!data->volume_factor_is_set)
244 pa_cvolume_reset(&data->volume_factor, data->sample_spec.channels);
246 pa_return_val_if_fail(pa_cvolume_compatible(&data->volume_factor, &data->sample_spec), -PA_ERR_INVALID);
248 if (!data->volume_factor_sink_is_set)
249 pa_cvolume_reset(&data->volume_factor_sink, data->sink->sample_spec.channels);
251 pa_return_val_if_fail(pa_cvolume_compatible(&data->volume_factor_sink, &data->sink->sample_spec), -PA_ERR_INVALID);
253 if (!data->muted_is_set)
256 if (data->flags & PA_SINK_INPUT_FIX_FORMAT)
257 data->sample_spec.format = data->sink->sample_spec.format;
259 if (data->flags & PA_SINK_INPUT_FIX_RATE)
260 data->sample_spec.rate = data->sink->sample_spec.rate;
262 original_cm = data->channel_map;
264 if (data->flags & PA_SINK_INPUT_FIX_CHANNELS) {
265 data->sample_spec.channels = data->sink->sample_spec.channels;
266 data->channel_map = data->sink->channel_map;
269 pa_assert(pa_sample_spec_valid(&data->sample_spec));
270 pa_assert(pa_channel_map_valid(&data->channel_map));
272 /* Due to the fixing of the sample spec the volume might not match anymore */
273 pa_cvolume_remap(&data->volume, &original_cm, &data->channel_map);
275 if (data->resample_method == PA_RESAMPLER_INVALID)
276 data->resample_method = core->resample_method;
278 pa_return_val_if_fail(data->resample_method < PA_RESAMPLER_MAX, -PA_ERR_INVALID);
280 if ((r = pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], data)) < 0)
283 if ((data->flags & PA_SINK_INPUT_NO_CREATE_ON_SUSPEND) &&
284 pa_sink_get_state(data->sink) == PA_SINK_SUSPENDED) {
285 pa_log_warn("Failed to create sink input: sink is suspended.");
286 return -PA_ERR_BADSTATE;
289 if (pa_idxset_size(data->sink->inputs) >= PA_MAX_INPUTS_PER_SINK) {
290 pa_log_warn("Failed to create sink input: too many inputs per sink.");
291 return -PA_ERR_TOOLARGE;
294 if ((data->flags & PA_SINK_INPUT_VARIABLE_RATE) ||
295 !pa_sample_spec_equal(&data->sample_spec, &data->sink->sample_spec) ||
296 !pa_channel_map_equal(&data->channel_map, &data->sink->channel_map)) {
298 /* Note: for passthrough content we need to adjust the output rate to that of the current sink-input */
299 if (!(data->flags & PA_SINK_INPUT_PASSTHROUGH)) /* no resampler for passthrough content */
300 if (!(resampler = pa_resampler_new(
302 &data->sample_spec, &data->channel_map,
303 &data->sink->sample_spec, &data->sink->channel_map,
304 data->resample_method,
305 ((data->flags & PA_SINK_INPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
306 ((data->flags & PA_SINK_INPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
307 (core->disable_remixing || (data->flags & PA_SINK_INPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0) |
308 (core->disable_lfe_remixing ? PA_RESAMPLER_NO_LFE : 0)))) {
309 pa_log_warn("Unsupported resampling operation.");
310 return -PA_ERR_NOTSUPPORTED;
314 i = pa_msgobject_new(pa_sink_input);
315 i->parent.parent.free = sink_input_free;
316 i->parent.process_msg = pa_sink_input_process_msg;
319 i->state = PA_SINK_INPUT_INIT;
320 i->flags = data->flags;
321 i->proplist = pa_proplist_copy(data->proplist);
322 i->driver = pa_xstrdup(pa_path_get_filename(data->driver));
323 i->module = data->module;
324 i->sink = data->sink;
325 i->origin_sink = data->origin_sink;
326 i->client = data->client;
328 i->requested_resample_method = data->resample_method;
329 i->actual_resample_method = resampler ? pa_resampler_get_method(resampler) : PA_RESAMPLER_INVALID;
330 i->sample_spec = data->sample_spec;
331 i->channel_map = data->channel_map;
333 if (!data->volume_is_absolute && pa_sink_flat_volume_enabled(i->sink)) {
336 /* When the 'absolute' bool is not set then we'll treat the volume
337 * as relative to the sink volume even in flat volume mode */
338 remapped = data->sink->reference_volume;
339 pa_cvolume_remap(&remapped, &data->sink->channel_map, &data->channel_map);
340 pa_sw_cvolume_multiply(&i->volume, &data->volume, &remapped);
342 i->volume = data->volume;
344 i->volume_factor = data->volume_factor;
345 i->volume_factor_sink = data->volume_factor_sink;
346 i->real_ratio = i->reference_ratio = data->volume;
347 pa_cvolume_reset(&i->soft_volume, i->sample_spec.channels);
348 pa_cvolume_reset(&i->real_ratio, i->sample_spec.channels);
349 i->volume_writable = data->volume_writable;
350 i->save_volume = data->save_volume;
351 i->save_sink = data->save_sink;
352 i->save_muted = data->save_muted;
354 i->muted = data->muted;
356 if (data->sync_base) {
357 i->sync_next = data->sync_base->sync_next;
358 i->sync_prev = data->sync_base;
360 if (data->sync_base->sync_next)
361 data->sync_base->sync_next->sync_prev = i;
362 data->sync_base->sync_next = i;
364 i->sync_next = i->sync_prev = NULL;
366 i->direct_outputs = pa_idxset_new(NULL, NULL);
371 i->thread_info.state = i->state;
372 i->thread_info.attached = FALSE;
373 pa_atomic_store(&i->thread_info.drained, 1);
374 i->thread_info.sample_spec = i->sample_spec;
375 i->thread_info.resampler = resampler;
376 i->thread_info.soft_volume = i->soft_volume;
377 i->thread_info.muted = i->muted;
378 i->thread_info.requested_sink_latency = (pa_usec_t) -1;
379 i->thread_info.rewrite_nbytes = 0;
380 i->thread_info.rewrite_flush = FALSE;
381 i->thread_info.dont_rewind_render = FALSE;
382 i->thread_info.underrun_for = (uint64_t) -1;
383 i->thread_info.playing_for = 0;
384 i->thread_info.direct_outputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
386 i->thread_info.render_memblockq = pa_memblockq_new(
390 pa_frame_size(&i->sink->sample_spec),
396 pa_assert_se(pa_idxset_put(core->sink_inputs, i, &i->index) == 0);
397 pa_assert_se(pa_idxset_put(i->sink->inputs, pa_sink_input_ref(i), NULL) == 0);
400 pa_assert_se(pa_idxset_put(i->client->sink_inputs, i, NULL) >= 0);
402 pt = pa_proplist_to_string_sep(i->proplist, "\n ");
403 pa_log_info("Created input %u \"%s\" on %s with sample spec %s and channel map %s\n %s",
405 pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME)),
407 pa_sample_spec_snprint(st, sizeof(st), &i->sample_spec),
408 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
412 /* Don't forget to call pa_sink_input_put! */
418 /* Called from main context */
419 static void update_n_corked(pa_sink_input *i, pa_sink_input_state_t state) {
421 pa_assert_ctl_context();
426 if (i->state == PA_SINK_INPUT_CORKED && state != PA_SINK_INPUT_CORKED)
427 pa_assert_se(i->sink->n_corked -- >= 1);
428 else if (i->state != PA_SINK_INPUT_CORKED && state == PA_SINK_INPUT_CORKED)
432 /* Called from main context */
433 static void sink_input_set_state(pa_sink_input *i, pa_sink_input_state_t state) {
434 pa_sink_input *ssync;
436 pa_assert_ctl_context();
438 if (state == PA_SINK_INPUT_DRAINED)
439 state = PA_SINK_INPUT_RUNNING;
441 if (i->state == state)
444 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);
446 update_n_corked(i, state);
449 for (ssync = i->sync_prev; ssync; ssync = ssync->sync_prev) {
450 update_n_corked(ssync, state);
451 ssync->state = state;
453 for (ssync = i->sync_next; ssync; ssync = ssync->sync_next) {
454 update_n_corked(ssync, state);
455 ssync->state = state;
458 if (state != PA_SINK_INPUT_UNLINKED) {
459 pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], i);
461 for (ssync = i->sync_prev; ssync; ssync = ssync->sync_prev)
462 pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], ssync);
464 for (ssync = i->sync_next; ssync; ssync = ssync->sync_next)
465 pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], ssync);
467 if (PA_SINK_INPUT_IS_LINKED(state))
468 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
471 pa_sink_update_status(i->sink);
474 /* Called from main context */
475 void pa_sink_input_unlink(pa_sink_input *i) {
477 pa_source_output *o, *p = NULL;
480 pa_assert_ctl_context();
482 /* See pa_sink_unlink() for a couple of comments how this function
485 pa_sink_input_ref(i);
487 linked = PA_SINK_INPUT_IS_LINKED(i->state);
490 pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK], i);
493 i->sync_prev->sync_next = i->sync_next;
495 i->sync_next->sync_prev = i->sync_prev;
497 i->sync_prev = i->sync_next = NULL;
499 pa_idxset_remove_by_data(i->core->sink_inputs, i, NULL);
502 if (pa_idxset_remove_by_data(i->sink->inputs, i, NULL))
503 pa_sink_input_unref(i);
506 pa_idxset_remove_by_data(i->client->sink_inputs, i, NULL);
508 while ((o = pa_idxset_first(i->direct_outputs, NULL))) {
510 pa_source_output_kill(o);
514 update_n_corked(i, PA_SINK_INPUT_UNLINKED);
515 i->state = PA_SINK_INPUT_UNLINKED;
517 if (linked && i->sink) {
518 /* We might need to update the sink's volume if we are in flat volume mode. */
519 if (pa_sink_flat_volume_enabled(i->sink))
520 pa_sink_set_volume(i->sink, NULL, FALSE, FALSE);
522 if (i->sink->asyncmsgq)
523 pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_REMOVE_INPUT, i, 0, NULL) == 0);
529 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
530 pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK_POST], i);
534 pa_sink_update_status(i->sink);
538 pa_core_maybe_vacuum(i->core);
540 pa_sink_input_unref(i);
543 /* Called from main context */
544 static void sink_input_free(pa_object *o) {
545 pa_sink_input* i = PA_SINK_INPUT(o);
548 pa_assert_ctl_context();
549 pa_assert(pa_sink_input_refcnt(i) == 0);
551 if (PA_SINK_INPUT_IS_LINKED(i->state))
552 pa_sink_input_unlink(i);
554 pa_log_info("Freeing input %u \"%s\"", i->index, pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME)));
556 /* Side note: this function must be able to destruct properly any
557 * kind of sink input in any state, even those which are
558 * "half-moved" or are connected to sinks that have no asyncmsgq
559 * and are hence half-destructed themselves! */
561 if (i->thread_info.render_memblockq)
562 pa_memblockq_free(i->thread_info.render_memblockq);
564 if (i->thread_info.resampler)
565 pa_resampler_free(i->thread_info.resampler);
568 pa_proplist_free(i->proplist);
570 if (i->direct_outputs)
571 pa_idxset_free(i->direct_outputs, NULL, NULL);
573 if (i->thread_info.direct_outputs)
574 pa_hashmap_free(i->thread_info.direct_outputs, NULL, NULL);
580 /* Called from main context */
581 void pa_sink_input_put(pa_sink_input *i) {
582 pa_sink_input_state_t state;
584 pa_sink_input_assert_ref(i);
585 pa_assert_ctl_context();
587 pa_assert(i->state == PA_SINK_INPUT_INIT);
589 /* The following fields must be initialized properly */
591 pa_assert(i->process_rewind);
594 state = i->flags & PA_SINK_INPUT_START_CORKED ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING;
596 update_n_corked(i, state);
599 /* We might need to update the sink's volume if we are in flat volume mode. */
600 if (pa_sink_flat_volume_enabled(i->sink))
601 pa_sink_set_volume(i->sink, NULL, FALSE, i->save_volume);
603 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
604 pa_assert(pa_cvolume_is_norm(&i->volume));
605 pa_assert(pa_cvolume_is_norm(&i->reference_ratio));
608 set_real_ratio(i, &i->volume);
611 i->thread_info.soft_volume = i->soft_volume;
612 i->thread_info.muted = i->muted;
614 pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_ADD_INPUT, i, 0, NULL) == 0);
616 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
617 pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], i);
619 pa_sink_update_status(i->sink);
622 /* Called from main context */
623 void pa_sink_input_kill(pa_sink_input*i) {
624 pa_sink_input_assert_ref(i);
625 pa_assert_ctl_context();
626 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
631 /* Called from main context */
632 pa_usec_t pa_sink_input_get_latency(pa_sink_input *i, pa_usec_t *sink_latency) {
633 pa_usec_t r[2] = { 0, 0 };
635 pa_sink_input_assert_ref(i);
636 pa_assert_ctl_context();
637 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
639 pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_LATENCY, r, 0, NULL) == 0);
642 r[0] += i->get_latency(i);
645 *sink_latency = r[1];
650 /* Called from thread context */
651 void pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink frames */, pa_memchunk *chunk, pa_cvolume *volume) {
652 pa_bool_t do_volume_adj_here, need_volume_factor_sink;
653 pa_bool_t volume_is_norm;
654 size_t block_size_max_sink, block_size_max_sink_input;
657 pa_sink_input_assert_ref(i);
658 pa_sink_input_assert_io_context(i);
659 pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
660 pa_assert(pa_frame_aligned(slength, &i->sink->sample_spec));
664 /* pa_log_debug("peek"); */
666 pa_assert(i->thread_info.state == PA_SINK_INPUT_RUNNING ||
667 i->thread_info.state == PA_SINK_INPUT_CORKED ||
668 i->thread_info.state == PA_SINK_INPUT_DRAINED);
670 block_size_max_sink_input = i->thread_info.resampler ?
671 pa_resampler_max_block_size(i->thread_info.resampler) :
672 pa_frame_align(pa_mempool_block_size_max(i->core->mempool), &i->sample_spec);
674 block_size_max_sink = pa_frame_align(pa_mempool_block_size_max(i->core->mempool), &i->sink->sample_spec);
676 /* Default buffer size */
678 slength = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sink->sample_spec);
680 if (slength > block_size_max_sink)
681 slength = block_size_max_sink;
683 if (i->thread_info.resampler) {
684 ilength = pa_resampler_request(i->thread_info.resampler, slength);
687 ilength = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sample_spec);
691 if (ilength > block_size_max_sink_input)
692 ilength = block_size_max_sink_input;
694 /* If the channel maps of the sink and this stream differ, we need
695 * to adjust the volume *before* we resample. Otherwise we can do
696 * it after and leave it for the sink code */
698 do_volume_adj_here = !pa_channel_map_equal(&i->channel_map, &i->sink->channel_map);
699 volume_is_norm = pa_cvolume_is_norm(&i->thread_info.soft_volume) && !i->thread_info.muted;
700 need_volume_factor_sink = !pa_cvolume_is_norm(&i->volume_factor_sink);
702 while (!pa_memblockq_is_readable(i->thread_info.render_memblockq)) {
705 /* There's nothing in our render queue. We need to fill it up
706 * with data from the implementor. */
708 if (i->thread_info.state == PA_SINK_INPUT_CORKED ||
709 i->pop(i, ilength, &tchunk) < 0) {
711 /* OK, we're corked or the implementor didn't give us any
712 * data, so let's just hand out silence */
713 pa_atomic_store(&i->thread_info.drained, 1);
715 pa_memblockq_seek(i->thread_info.render_memblockq, (int64_t) slength, PA_SEEK_RELATIVE, TRUE);
716 i->thread_info.playing_for = 0;
717 if (i->thread_info.underrun_for != (uint64_t) -1)
718 i->thread_info.underrun_for += ilength;
722 pa_atomic_store(&i->thread_info.drained, 0);
724 pa_assert(tchunk.length > 0);
725 pa_assert(tchunk.memblock);
727 i->thread_info.underrun_for = 0;
728 i->thread_info.playing_for += tchunk.length;
730 while (tchunk.length > 0) {
732 pa_bool_t nvfs = need_volume_factor_sink;
735 pa_memblock_ref(wchunk.memblock);
737 if (wchunk.length > block_size_max_sink_input)
738 wchunk.length = block_size_max_sink_input;
740 /* It might be necessary to adjust the volume here */
741 if (do_volume_adj_here && !volume_is_norm) {
742 pa_memchunk_make_writable(&wchunk, 0);
744 if (i->thread_info.muted) {
745 pa_silence_memchunk(&wchunk, &i->thread_info.sample_spec);
748 } else if (!i->thread_info.resampler && nvfs) {
751 /* If we don't need a resampler we can merge the
752 * post and the pre volume adjustment into one */
754 pa_sw_cvolume_multiply(&v, &i->thread_info.soft_volume, &i->volume_factor_sink);
755 pa_volume_memchunk(&wchunk, &i->thread_info.sample_spec, &v);
759 pa_volume_memchunk(&wchunk, &i->thread_info.sample_spec, &i->thread_info.soft_volume);
762 if (!i->thread_info.resampler) {
765 pa_memchunk_make_writable(&wchunk, 0);
766 pa_volume_memchunk(&wchunk, &i->sink->sample_spec, &i->volume_factor_sink);
769 pa_memblockq_push_align(i->thread_info.render_memblockq, &wchunk);
772 pa_resampler_run(i->thread_info.resampler, &wchunk, &rchunk);
774 /* pa_log_debug("pushing %lu", (unsigned long) rchunk.length); */
776 if (rchunk.memblock) {
779 pa_memchunk_make_writable(&rchunk, 0);
780 pa_volume_memchunk(&rchunk, &i->sink->sample_spec, &i->volume_factor_sink);
783 pa_memblockq_push_align(i->thread_info.render_memblockq, &rchunk);
784 pa_memblock_unref(rchunk.memblock);
788 pa_memblock_unref(wchunk.memblock);
790 tchunk.index += wchunk.length;
791 tchunk.length -= wchunk.length;
794 pa_memblock_unref(tchunk.memblock);
797 pa_assert_se(pa_memblockq_peek(i->thread_info.render_memblockq, chunk) >= 0);
799 pa_assert(chunk->length > 0);
800 pa_assert(chunk->memblock);
802 /* pa_log_debug("peeking %lu", (unsigned long) chunk->length); */
804 if (chunk->length > block_size_max_sink)
805 chunk->length = block_size_max_sink;
807 /* Let's see if we had to apply the volume adjustment ourselves,
808 * or if this can be done by the sink for us */
810 if (do_volume_adj_here)
811 /* We had different channel maps, so we already did the adjustment */
812 pa_cvolume_reset(volume, i->sink->sample_spec.channels);
813 else if (i->thread_info.muted)
814 /* We've both the same channel map, so let's have the sink do the adjustment for us*/
815 pa_cvolume_mute(volume, i->sink->sample_spec.channels);
817 *volume = i->thread_info.soft_volume;
820 /* Called from thread context */
821 void pa_sink_input_drop(pa_sink_input *i, size_t nbytes /* in sink sample spec */) {
823 pa_sink_input_assert_ref(i);
824 pa_sink_input_assert_io_context(i);
825 pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
826 pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
827 pa_assert(nbytes > 0);
829 /* pa_log_debug("dropping %lu", (unsigned long) nbytes); */
831 pa_memblockq_drop(i->thread_info.render_memblockq, nbytes);
834 /* Called from thread context */
835 void pa_sink_input_process_rewind(pa_sink_input *i, size_t nbytes /* in sink sample spec */) {
837 pa_bool_t called = FALSE;
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(nbytes, &i->sink->sample_spec));
844 /* pa_log_debug("rewind(%lu, %lu)", (unsigned long) nbytes, (unsigned long) i->thread_info.rewrite_nbytes); */
846 lbq = pa_memblockq_get_length(i->thread_info.render_memblockq);
848 if (nbytes > 0 && !i->thread_info.dont_rewind_render) {
849 pa_log_debug("Have to rewind %lu bytes on render memblockq.", (unsigned long) nbytes);
850 pa_memblockq_rewind(i->thread_info.render_memblockq, nbytes);
853 if (i->thread_info.rewrite_nbytes == (size_t) -1) {
855 /* We were asked to drop all buffered data, and rerequest new
856 * data from implementor the next time push() is called */
858 pa_memblockq_flush_write(i->thread_info.render_memblockq, TRUE);
860 } else if (i->thread_info.rewrite_nbytes > 0) {
861 size_t max_rewrite, amount;
863 /* Calculate how much make sense to rewrite at most */
864 max_rewrite = nbytes + lbq;
866 /* Transform into local domain */
867 if (i->thread_info.resampler)
868 max_rewrite = pa_resampler_request(i->thread_info.resampler, max_rewrite);
870 /* Calculate how much of the rewinded data should actually be rewritten */
871 amount = PA_MIN(i->thread_info.rewrite_nbytes, max_rewrite);
874 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) amount);
876 /* Tell the implementor */
877 if (i->process_rewind)
878 i->process_rewind(i, amount);
881 /* Convert back to to sink domain */
882 if (i->thread_info.resampler)
883 amount = pa_resampler_result(i->thread_info.resampler, amount);
886 /* Ok, now update the write pointer */
887 pa_memblockq_seek(i->thread_info.render_memblockq, - ((int64_t) amount), PA_SEEK_RELATIVE, TRUE);
889 if (i->thread_info.rewrite_flush)
890 pa_memblockq_silence(i->thread_info.render_memblockq);
892 /* And reset the resampler */
893 if (i->thread_info.resampler)
894 pa_resampler_reset(i->thread_info.resampler);
899 if (i->process_rewind)
900 i->process_rewind(i, 0);
902 i->thread_info.rewrite_nbytes = 0;
903 i->thread_info.rewrite_flush = FALSE;
904 i->thread_info.dont_rewind_render = FALSE;
907 /* Called from thread context */
908 size_t pa_sink_input_get_max_rewind(pa_sink_input *i) {
909 pa_sink_input_assert_ref(i);
910 pa_sink_input_assert_io_context(i);
912 return i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, i->sink->thread_info.max_rewind) : i->sink->thread_info.max_rewind;
915 /* Called from thread context */
916 size_t pa_sink_input_get_max_request(pa_sink_input *i) {
917 pa_sink_input_assert_ref(i);
918 pa_sink_input_assert_io_context(i);
920 /* We're not verifying the status here, to allow this to be called
921 * in the state change handler between _INIT and _RUNNING */
923 return i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, i->sink->thread_info.max_request) : i->sink->thread_info.max_request;
926 /* Called from thread context */
927 void pa_sink_input_update_max_rewind(pa_sink_input *i, size_t nbytes /* in the sink's sample spec */) {
928 pa_sink_input_assert_ref(i);
929 pa_sink_input_assert_io_context(i);
930 pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
931 pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
933 pa_memblockq_set_maxrewind(i->thread_info.render_memblockq, nbytes);
935 if (i->update_max_rewind)
936 i->update_max_rewind(i, i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, nbytes) : nbytes);
939 /* Called from thread context */
940 void pa_sink_input_update_max_request(pa_sink_input *i, size_t nbytes /* in the sink's sample spec */) {
941 pa_sink_input_assert_ref(i);
942 pa_sink_input_assert_io_context(i);
943 pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
944 pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
946 if (i->update_max_request)
947 i->update_max_request(i, i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, nbytes) : nbytes);
950 /* Called from thread context */
951 pa_usec_t pa_sink_input_set_requested_latency_within_thread(pa_sink_input *i, pa_usec_t usec) {
952 pa_sink_input_assert_ref(i);
953 pa_sink_input_assert_io_context(i);
955 if (!(i->sink->flags & PA_SINK_DYNAMIC_LATENCY))
956 usec = i->sink->thread_info.fixed_latency;
958 if (usec != (pa_usec_t) -1)
959 usec = PA_CLAMP(usec, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
961 i->thread_info.requested_sink_latency = usec;
962 pa_sink_invalidate_requested_latency(i->sink, TRUE);
967 /* Called from main context */
968 pa_usec_t pa_sink_input_set_requested_latency(pa_sink_input *i, pa_usec_t usec) {
969 pa_sink_input_assert_ref(i);
970 pa_assert_ctl_context();
972 if (PA_SINK_INPUT_IS_LINKED(i->state) && i->sink) {
973 pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
977 /* If this sink input is not realized yet or we are being moved,
978 * we have to touch the thread info data directly */
981 if (!(i->sink->flags & PA_SINK_DYNAMIC_LATENCY))
982 usec = pa_sink_get_fixed_latency(i->sink);
984 if (usec != (pa_usec_t) -1) {
985 pa_usec_t min_latency, max_latency;
986 pa_sink_get_latency_range(i->sink, &min_latency, &max_latency);
987 usec = PA_CLAMP(usec, min_latency, max_latency);
991 i->thread_info.requested_sink_latency = usec;
996 /* Called from main context */
997 pa_usec_t pa_sink_input_get_requested_latency(pa_sink_input *i) {
998 pa_sink_input_assert_ref(i);
999 pa_assert_ctl_context();
1001 if (PA_SINK_INPUT_IS_LINKED(i->state) && i->sink) {
1003 pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
1007 /* If this sink input is not realized yet or we are being moved,
1008 * we have to touch the thread info data directly */
1010 return i->thread_info.requested_sink_latency;
1013 /* Called from main context */
1014 void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume, pa_bool_t save, pa_bool_t absolute) {
1017 pa_sink_input_assert_ref(i);
1018 pa_assert_ctl_context();
1019 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1021 pa_assert(pa_cvolume_valid(volume));
1022 pa_assert(volume->channels == 1 || pa_cvolume_compatible(volume, &i->sample_spec));
1023 pa_assert(i->volume_writable);
1025 if (!absolute && pa_sink_flat_volume_enabled(i->sink)) {
1026 v = i->sink->reference_volume;
1027 pa_cvolume_remap(&v, &i->sink->channel_map, &i->channel_map);
1029 if (pa_cvolume_compatible(volume, &i->sample_spec))
1030 volume = pa_sw_cvolume_multiply(&v, &v, volume);
1032 volume = pa_sw_cvolume_multiply_scalar(&v, &v, pa_cvolume_max(volume));
1034 if (!pa_cvolume_compatible(volume, &i->sample_spec)) {
1036 volume = pa_cvolume_scale(&v, pa_cvolume_max(volume));
1040 if (pa_cvolume_equal(volume, &i->volume)) {
1041 i->save_volume = i->save_volume || save;
1045 i->volume = *volume;
1046 i->save_volume = save;
1048 if (pa_sink_flat_volume_enabled(i->sink)) {
1049 /* We are in flat volume mode, so let's update all sink input
1050 * volumes and update the flat volume of the sink */
1052 pa_sink_set_volume(i->sink, NULL, TRUE, save);
1055 /* OK, we are in normal volume mode. The volume only affects
1057 set_real_ratio(i, volume);
1059 /* Copy the new soft_volume to the thread_info struct */
1060 pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_SOFT_VOLUME, NULL, 0, NULL) == 0);
1063 /* The volume changed, let's tell people so */
1064 if (i->volume_changed)
1065 i->volume_changed(i);
1067 /* The virtual volume changed, let's tell people so */
1068 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1071 /* Called from main context */
1072 static void set_real_ratio(pa_sink_input *i, const pa_cvolume *v) {
1073 pa_sink_input_assert_ref(i);
1074 pa_assert_ctl_context();
1075 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1076 pa_assert(!v || pa_cvolume_compatible(v, &i->sample_spec));
1078 /* This basically calculates:
1080 * i->real_ratio := v
1081 * i->soft_volume := i->real_ratio * i->volume_factor */
1086 pa_cvolume_reset(&i->real_ratio, i->sample_spec.channels);
1088 pa_sw_cvolume_multiply(&i->soft_volume, &i->real_ratio, &i->volume_factor);
1089 /* We don't copy the data to the thread_info data. That's left for someone else to do */
1092 /* Called from main context */
1093 pa_bool_t pa_sink_input_is_volume_readable(pa_sink_input *i) {
1094 pa_sink_input_assert_ref(i);
1095 pa_assert_ctl_context();
1097 return !(i->flags & PA_SINK_INPUT_PASSTHROUGH);
1100 /* Called from main context */
1101 pa_cvolume *pa_sink_input_get_volume(pa_sink_input *i, pa_cvolume *volume, pa_bool_t absolute) {
1102 pa_sink_input_assert_ref(i);
1103 pa_assert_ctl_context();
1104 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1105 pa_assert(pa_sink_input_is_volume_readable(i));
1107 if (absolute || !pa_sink_flat_volume_enabled(i->sink))
1108 *volume = i->volume;
1110 *volume = i->reference_ratio;
1115 /* Called from main context */
1116 void pa_sink_input_set_mute(pa_sink_input *i, pa_bool_t mute, pa_bool_t save) {
1117 pa_sink_input_assert_ref(i);
1118 pa_assert_ctl_context();
1119 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1121 if (!i->muted == !mute) {
1122 i->save_muted = i->save_muted || mute;
1127 i->save_muted = save;
1129 pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_SOFT_MUTE, NULL, 0, NULL) == 0);
1131 /* The mute status changed, let's tell people so */
1132 if (i->mute_changed)
1135 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1138 /* Called from main context */
1139 pa_bool_t pa_sink_input_get_mute(pa_sink_input *i) {
1140 pa_sink_input_assert_ref(i);
1141 pa_assert_ctl_context();
1142 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1147 /* Called from main thread */
1148 void pa_sink_input_update_proplist(pa_sink_input *i, pa_update_mode_t mode, pa_proplist *p) {
1149 pa_sink_input_assert_ref(i);
1150 pa_assert_ctl_context();
1153 pa_proplist_update(i->proplist, mode, p);
1155 if (PA_SINK_INPUT_IS_LINKED(i->state)) {
1156 pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_PROPLIST_CHANGED], i);
1157 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1161 /* Called from main context */
1162 void pa_sink_input_cork(pa_sink_input *i, pa_bool_t b) {
1163 pa_sink_input_assert_ref(i);
1164 pa_assert_ctl_context();
1165 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1167 sink_input_set_state(i, b ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING);
1170 /* Called from main context */
1171 int pa_sink_input_set_rate(pa_sink_input *i, uint32_t rate) {
1172 pa_sink_input_assert_ref(i);
1173 pa_assert_ctl_context();
1174 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1175 pa_return_val_if_fail(i->thread_info.resampler, -PA_ERR_BADSTATE);
1177 if (i->sample_spec.rate == rate)
1180 i->sample_spec.rate = rate;
1182 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_RATE, PA_UINT_TO_PTR(rate), 0, NULL, NULL);
1184 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1188 /* Called from main context */
1189 void pa_sink_input_set_name(pa_sink_input *i, const char *name) {
1191 pa_sink_input_assert_ref(i);
1192 pa_assert_ctl_context();
1194 if (!name && !pa_proplist_contains(i->proplist, PA_PROP_MEDIA_NAME))
1197 old = pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME);
1199 if (old && name && pa_streq(old, name))
1203 pa_proplist_sets(i->proplist, PA_PROP_MEDIA_NAME, name);
1205 pa_proplist_unset(i->proplist, PA_PROP_MEDIA_NAME);
1207 if (PA_SINK_INPUT_IS_LINKED(i->state)) {
1208 pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_PROPLIST_CHANGED], i);
1209 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1213 /* Called from main context */
1214 pa_resample_method_t pa_sink_input_get_resample_method(pa_sink_input *i) {
1215 pa_sink_input_assert_ref(i);
1216 pa_assert_ctl_context();
1218 return i->actual_resample_method;
1221 /* Called from main context */
1222 pa_bool_t pa_sink_input_may_move(pa_sink_input *i) {
1223 pa_sink_input_assert_ref(i);
1224 pa_assert_ctl_context();
1225 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1227 if (i->flags & PA_SINK_INPUT_DONT_MOVE)
1230 if (i->sync_next || i->sync_prev) {
1231 pa_log_warn("Moving synchronized streams not supported.");
1238 /* Called from main context */
1239 pa_bool_t pa_sink_input_may_move_to(pa_sink_input *i, pa_sink *dest) {
1240 pa_sink_input_assert_ref(i);
1241 pa_assert_ctl_context();
1242 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1243 pa_sink_assert_ref(dest);
1245 if (dest == i->sink)
1248 if (!pa_sink_input_may_move(i))
1251 if (pa_idxset_size(dest->inputs) >= PA_MAX_INPUTS_PER_SINK) {
1252 pa_log_warn("Failed to move sink input: too many inputs per sink.");
1256 if (check_passthrough_connection(i->flags, dest) < 0)
1260 if (!i->may_move_to(i, dest))
1266 /* Called from main context */
1267 int pa_sink_input_start_move(pa_sink_input *i) {
1268 pa_source_output *o, *p = NULL;
1271 pa_sink_input_assert_ref(i);
1272 pa_assert_ctl_context();
1273 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1276 if (!pa_sink_input_may_move(i))
1277 return -PA_ERR_NOTSUPPORTED;
1279 if ((r = pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_START], i)) < 0)
1282 /* Kill directly connected outputs */
1283 while ((o = pa_idxset_first(i->direct_outputs, NULL))) {
1285 pa_source_output_kill(o);
1288 pa_assert(pa_idxset_isempty(i->direct_outputs));
1290 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
1292 if (pa_sink_input_get_state(i) == PA_SINK_INPUT_CORKED)
1293 pa_assert_se(i->sink->n_corked-- >= 1);
1295 if (pa_sink_flat_volume_enabled(i->sink))
1296 /* We might need to update the sink's volume if we are in flat
1298 pa_sink_set_volume(i->sink, NULL, FALSE, FALSE);
1300 pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_START_MOVE, i, 0, NULL) == 0);
1302 pa_sink_update_status(i->sink);
1303 pa_cvolume_remap(&i->volume_factor_sink, &i->sink->channel_map, &i->channel_map);
1306 pa_sink_input_unref(i);
1311 /* Called from main context. If i has an origin sink that uses volume sharing,
1312 * then also the origin sink and all streams connected to it need to update
1313 * their volume - this function does all that by using recursion. */
1314 static void update_volume_due_to_moving(pa_sink_input *i, pa_sink *dest) {
1315 pa_cvolume old_volume;
1319 pa_assert(i->sink); /* The destination sink should already be set. */
1321 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
1322 pa_sink *root_sink = i->sink;
1323 pa_sink_input *origin_sink_input;
1326 while (root_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)
1327 root_sink = root_sink->input_to_master->sink;
1329 if (pa_sink_flat_volume_enabled(i->sink)) {
1330 /* Ok, so the origin sink uses volume sharing, and flat volume is
1331 * enabled. The volume will have to be updated as follows:
1333 * i->volume := i->sink->real_volume
1334 * (handled later by pa_sink_set_volume)
1335 * i->reference_ratio := i->volume / i->sink->reference_volume
1336 * (handled later by pa_sink_set_volume)
1337 * i->real_ratio stays unchanged
1338 * (streams whose origin sink uses volume sharing should
1339 * always have real_ratio of 0 dB)
1340 * i->soft_volume stays unchanged
1341 * (streams whose origin sink uses volume sharing should
1342 * always have volume_factor as soft_volume, so no change
1343 * should be needed) */
1345 pa_assert(pa_cvolume_is_norm(&i->real_ratio));
1346 pa_assert(pa_cvolume_equal(&i->soft_volume, &i->volume_factor));
1348 /* Notifications will be sent by pa_sink_set_volume(). */
1351 /* Ok, so the origin sink uses volume sharing, and flat volume is
1352 * disabled. The volume will have to be updated as follows:
1355 * i->reference_ratio := 0 dB
1356 * i->real_ratio stays unchanged
1357 * (streams whose origin sink uses volume sharing should
1358 * always have real_ratio of 0 dB)
1359 * i->soft_volume stays unchanged
1360 * (streams whose origin sink uses volume sharing should
1361 * always have volume_factor as soft_volume, so no change
1362 * should be needed) */
1364 old_volume = i->volume;
1365 pa_cvolume_reset(&i->volume, i->volume.channels);
1366 pa_cvolume_reset(&i->reference_ratio, i->reference_ratio.channels);
1367 pa_assert(pa_cvolume_is_norm(&i->real_ratio));
1368 pa_assert(pa_cvolume_equal(&i->soft_volume, &i->volume_factor));
1370 /* Notify others about the changed sink input volume. */
1371 if (!pa_cvolume_equal(&i->volume, &old_volume)) {
1372 if (i->volume_changed)
1373 i->volume_changed(i);
1375 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1379 /* Additionally, the origin sink volume needs updating:
1381 * i->origin_sink->reference_volume := root_sink->reference_volume
1382 * i->origin_sink->real_volume := root_sink->real_volume
1383 * i->origin_sink->soft_volume stays unchanged
1384 * (sinks that use volume sharing should always have
1385 * soft_volume of 0 dB) */
1387 old_volume = i->origin_sink->reference_volume;
1389 i->origin_sink->reference_volume = root_sink->reference_volume;
1390 pa_cvolume_remap(&i->origin_sink->reference_volume, &root_sink->channel_map, &i->origin_sink->channel_map);
1392 i->origin_sink->real_volume = root_sink->real_volume;
1393 pa_cvolume_remap(&i->origin_sink->real_volume, &root_sink->channel_map, &i->origin_sink->channel_map);
1395 pa_assert(pa_cvolume_is_norm(&i->origin_sink->soft_volume));
1397 /* Notify others about the changed sink volume. If you wonder whether
1398 * i->origin_sink->set_volume() should be called somewhere, that's not
1399 * the case, because sinks that use volume sharing shouldn't have any
1400 * internal volume that set_volume() would update. If you wonder
1401 * whether the thread_info variables should be synced, yes, they
1402 * should, and it's done by the PA_SINK_MESSAGE_FINISH_MOVE message
1404 if (!pa_cvolume_equal(&i->origin_sink->reference_volume, &old_volume))
1405 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, i->origin_sink->index);
1407 /* Recursively update origin sink inputs. */
1408 PA_IDXSET_FOREACH(origin_sink_input, i->origin_sink->inputs, idx)
1409 update_volume_due_to_moving(origin_sink_input, dest);
1412 old_volume = i->volume;
1414 if (pa_sink_flat_volume_enabled(i->sink)) {
1415 /* Ok, so this is a regular stream, and flat volume is enabled. The
1416 * volume will have to be updated as follows:
1418 * i->volume := i->reference_ratio * i->sink->reference_volume
1419 * i->reference_ratio stays unchanged
1420 * i->real_ratio := i->volume / i->sink->real_volume
1421 * (handled later by pa_sink_set_volume)
1422 * i->soft_volume := i->real_ratio * i->volume_factor
1423 * (handled later by pa_sink_set_volume) */
1425 i->volume = i->sink->reference_volume;
1426 pa_cvolume_remap(&i->volume, &i->sink->channel_map, &i->channel_map);
1427 pa_sw_cvolume_multiply(&i->volume, &i->volume, &i->reference_ratio);
1430 /* Ok, so this is a regular stream, and flat volume is disabled.
1431 * The volume will have to be updated as follows:
1433 * i->volume := i->reference_ratio
1434 * i->reference_ratio stays unchanged
1435 * i->real_ratio := i->reference_ratio
1436 * i->soft_volume := i->real_ratio * i->volume_factor */
1438 i->volume = i->reference_ratio;
1439 i->real_ratio = i->reference_ratio;
1440 pa_sw_cvolume_multiply(&i->soft_volume, &i->real_ratio, &i->volume_factor);
1443 /* Notify others about the changed sink input volume. */
1444 if (!pa_cvolume_equal(&i->volume, &old_volume)) {
1445 /* XXX: In case i->sink has flat volume enabled, then real_ratio
1446 * and soft_volume are not updated yet. Let's hope that the
1447 * callback implementation doesn't care about those variables... */
1448 if (i->volume_changed)
1449 i->volume_changed(i);
1451 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1455 /* If i->sink == dest, then recursion has finished, and we can finally call
1456 * pa_sink_set_volume(), which will do the rest of the updates. */
1457 if ((i->sink == dest) && pa_sink_flat_volume_enabled(i->sink))
1458 pa_sink_set_volume(i->sink, NULL, FALSE, i->save_volume);
1461 /* Called from main context */
1462 int pa_sink_input_finish_move(pa_sink_input *i, pa_sink *dest, pa_bool_t save) {
1463 pa_resampler *new_resampler;
1465 pa_sink_input_assert_ref(i);
1466 pa_assert_ctl_context();
1467 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1468 pa_assert(!i->sink);
1469 pa_sink_assert_ref(dest);
1471 if (!pa_sink_input_may_move_to(i, dest))
1472 return -PA_ERR_NOTSUPPORTED;
1474 if (i->thread_info.resampler &&
1475 pa_sample_spec_equal(pa_resampler_output_sample_spec(i->thread_info.resampler), &dest->sample_spec) &&
1476 pa_channel_map_equal(pa_resampler_output_channel_map(i->thread_info.resampler), &dest->channel_map))
1478 /* Try to reuse the old resampler if possible */
1479 new_resampler = i->thread_info.resampler;
1481 else if ((i->flags & PA_SINK_INPUT_VARIABLE_RATE) ||
1482 !pa_sample_spec_equal(&i->sample_spec, &dest->sample_spec) ||
1483 !pa_channel_map_equal(&i->channel_map, &dest->channel_map)) {
1485 /* Okey, we need a new resampler for the new sink */
1487 if (!(new_resampler = pa_resampler_new(
1489 &i->sample_spec, &i->channel_map,
1490 &dest->sample_spec, &dest->channel_map,
1491 i->requested_resample_method,
1492 ((i->flags & PA_SINK_INPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
1493 ((i->flags & PA_SINK_INPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
1494 (i->core->disable_remixing || (i->flags & PA_SINK_INPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0)))) {
1495 pa_log_warn("Unsupported resampling operation.");
1496 return -PA_ERR_NOTSUPPORTED;
1499 new_resampler = NULL;
1505 i->save_sink = save;
1506 pa_idxset_put(dest->inputs, pa_sink_input_ref(i), NULL);
1508 pa_cvolume_remap(&i->volume_factor_sink, &i->channel_map, &i->sink->channel_map);
1510 if (pa_sink_input_get_state(i) == PA_SINK_INPUT_CORKED)
1511 i->sink->n_corked++;
1513 /* Replace resampler and render queue */
1514 if (new_resampler != i->thread_info.resampler) {
1516 if (i->thread_info.resampler)
1517 pa_resampler_free(i->thread_info.resampler);
1518 i->thread_info.resampler = new_resampler;
1520 pa_memblockq_free(i->thread_info.render_memblockq);
1522 i->thread_info.render_memblockq = pa_memblockq_new(
1524 MEMBLOCKQ_MAXLENGTH,
1526 pa_frame_size(&i->sink->sample_spec),
1532 pa_sink_update_status(dest);
1534 update_volume_due_to_moving(i, dest);
1536 pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_FINISH_MOVE, i, 0, NULL) == 0);
1538 pa_log_debug("Successfully moved sink input %i to %s.", i->index, dest->name);
1540 /* Notify everyone */
1541 pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], i);
1543 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1548 /* Called from main context */
1549 void pa_sink_input_fail_move(pa_sink_input *i) {
1551 pa_sink_input_assert_ref(i);
1552 pa_assert_ctl_context();
1553 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1554 pa_assert(!i->sink);
1556 /* Check if someone wants this sink input? */
1557 if (pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FAIL], i) == PA_HOOK_STOP)
1563 pa_sink_input_kill(i);
1566 /* Called from main context */
1567 int pa_sink_input_move_to(pa_sink_input *i, pa_sink *dest, pa_bool_t save) {
1570 pa_sink_input_assert_ref(i);
1571 pa_assert_ctl_context();
1572 pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1574 pa_sink_assert_ref(dest);
1576 if (dest == i->sink)
1579 if (!pa_sink_input_may_move_to(i, dest))
1580 return -PA_ERR_NOTSUPPORTED;
1582 pa_sink_input_ref(i);
1584 if ((r = pa_sink_input_start_move(i)) < 0) {
1585 pa_sink_input_unref(i);
1589 if ((r = pa_sink_input_finish_move(i, dest, save)) < 0) {
1590 pa_sink_input_fail_move(i);
1591 pa_sink_input_unref(i);
1595 pa_sink_input_unref(i);
1600 /* Called from IO thread context */
1601 void pa_sink_input_set_state_within_thread(pa_sink_input *i, pa_sink_input_state_t state) {
1602 pa_bool_t corking, uncorking;
1604 pa_sink_input_assert_ref(i);
1605 pa_sink_input_assert_io_context(i);
1607 if (state == i->thread_info.state)
1610 if ((state == PA_SINK_INPUT_DRAINED || state == PA_SINK_INPUT_RUNNING) &&
1611 !(i->thread_info.state == PA_SINK_INPUT_DRAINED || i->thread_info.state != PA_SINK_INPUT_RUNNING))
1612 pa_atomic_store(&i->thread_info.drained, 1);
1614 corking = state == PA_SINK_INPUT_CORKED && i->thread_info.state == PA_SINK_INPUT_RUNNING;
1615 uncorking = i->thread_info.state == PA_SINK_INPUT_CORKED && state == PA_SINK_INPUT_RUNNING;
1617 if (i->state_change)
1618 i->state_change(i, state);
1620 i->thread_info.state = state;
1624 pa_log_debug("Requesting rewind due to corking");
1626 /* This will tell the implementing sink input driver to rewind
1627 * so that the unplayed already mixed data is not lost */
1628 pa_sink_input_request_rewind(i, 0, TRUE, TRUE, FALSE);
1630 } else if (uncorking) {
1632 i->thread_info.underrun_for = (uint64_t) -1;
1633 i->thread_info.playing_for = 0;
1635 pa_log_debug("Requesting rewind due to uncorking");
1637 /* OK, we're being uncorked. Make sure we're not rewound when
1638 * the hw buffer is remixed and request a remix. */
1639 pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
1643 /* Called from thread context, except when it is not. */
1644 int pa_sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
1645 pa_sink_input *i = PA_SINK_INPUT(o);
1646 pa_sink_input_assert_ref(i);
1650 case PA_SINK_INPUT_MESSAGE_SET_SOFT_VOLUME:
1651 if (!pa_cvolume_equal(&i->thread_info.soft_volume, &i->soft_volume)) {
1652 i->thread_info.soft_volume = i->soft_volume;
1653 pa_sink_input_request_rewind(i, 0, TRUE, FALSE, FALSE);
1657 case PA_SINK_INPUT_MESSAGE_SET_SOFT_MUTE:
1658 if (i->thread_info.muted != i->muted) {
1659 i->thread_info.muted = i->muted;
1660 pa_sink_input_request_rewind(i, 0, TRUE, FALSE, FALSE);
1664 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
1665 pa_usec_t *r = userdata;
1667 r[0] += pa_bytes_to_usec(pa_memblockq_get_length(i->thread_info.render_memblockq), &i->sink->sample_spec);
1668 r[1] += pa_sink_get_latency_within_thread(i->sink);
1673 case PA_SINK_INPUT_MESSAGE_SET_RATE:
1675 i->thread_info.sample_spec.rate = PA_PTR_TO_UINT(userdata);
1676 pa_resampler_set_input_rate(i->thread_info.resampler, PA_PTR_TO_UINT(userdata));
1680 case PA_SINK_INPUT_MESSAGE_SET_STATE: {
1681 pa_sink_input *ssync;
1683 pa_sink_input_set_state_within_thread(i, PA_PTR_TO_UINT(userdata));
1685 for (ssync = i->thread_info.sync_prev; ssync; ssync = ssync->thread_info.sync_prev)
1686 pa_sink_input_set_state_within_thread(ssync, PA_PTR_TO_UINT(userdata));
1688 for (ssync = i->thread_info.sync_next; ssync; ssync = ssync->thread_info.sync_next)
1689 pa_sink_input_set_state_within_thread(ssync, PA_PTR_TO_UINT(userdata));
1694 case PA_SINK_INPUT_MESSAGE_SET_REQUESTED_LATENCY: {
1695 pa_usec_t *usec = userdata;
1697 *usec = pa_sink_input_set_requested_latency_within_thread(i, *usec);
1701 case PA_SINK_INPUT_MESSAGE_GET_REQUESTED_LATENCY: {
1702 pa_usec_t *r = userdata;
1704 *r = i->thread_info.requested_sink_latency;
1709 return -PA_ERR_NOTIMPLEMENTED;
1712 /* Called from main thread */
1713 pa_sink_input_state_t pa_sink_input_get_state(pa_sink_input *i) {
1714 pa_sink_input_assert_ref(i);
1715 pa_assert_ctl_context();
1717 if (i->state == PA_SINK_INPUT_RUNNING || i->state == PA_SINK_INPUT_DRAINED)
1718 return pa_atomic_load(&i->thread_info.drained) ? PA_SINK_INPUT_DRAINED : PA_SINK_INPUT_RUNNING;
1723 /* Called from IO context */
1724 pa_bool_t pa_sink_input_safe_to_remove(pa_sink_input *i) {
1725 pa_sink_input_assert_ref(i);
1726 pa_sink_input_assert_io_context(i);
1728 if (PA_SINK_INPUT_IS_LINKED(i->thread_info.state))
1729 return pa_memblockq_is_empty(i->thread_info.render_memblockq);
1734 /* Called from IO context */
1735 void pa_sink_input_request_rewind(
1737 size_t nbytes /* in our sample spec */,
1740 pa_bool_t dont_rewind_render) {
1744 /* If 'rewrite' is TRUE the sink is rewound as far as requested
1745 * and possible and the exact value of this is passed back the
1746 * implementor via process_rewind(). If 'flush' is also TRUE all
1747 * already rendered data is also dropped.
1749 * If 'rewrite' is FALSE the sink is rewound as far as requested
1750 * and possible and the already rendered data is dropped so that
1751 * in the next iteration we read new data from the
1752 * implementor. This implies 'flush' is TRUE. If
1753 * dont_rewind_render is TRUE then the render memblockq is not
1756 /* nbytes = 0 means maximum rewind request */
1758 pa_sink_input_assert_ref(i);
1759 pa_sink_input_assert_io_context(i);
1760 pa_assert(rewrite || flush);
1761 pa_assert(!dont_rewind_render || !rewrite);
1763 /* We don't take rewind requests while we are corked */
1764 if (i->thread_info.state == PA_SINK_INPUT_CORKED)
1767 nbytes = PA_MAX(i->thread_info.rewrite_nbytes, nbytes);
1769 /* pa_log_debug("request rewrite %zu", nbytes); */
1771 /* Calculate how much we can rewind locally without having to
1774 lbq = pa_memblockq_get_length(i->thread_info.render_memblockq);
1778 /* Check if rewinding for the maximum is requested, and if so, fix up */
1781 /* Calculate maximum number of bytes that could be rewound in theory */
1782 nbytes = i->sink->thread_info.max_rewind + lbq;
1784 /* Transform from sink domain */
1785 if (i->thread_info.resampler)
1786 nbytes = pa_resampler_request(i->thread_info.resampler, nbytes);
1789 /* Remember how much we actually want to rewrite */
1790 if (i->thread_info.rewrite_nbytes != (size_t) -1) {
1792 /* Make sure to not overwrite over underruns */
1793 if (nbytes > i->thread_info.playing_for)
1794 nbytes = (size_t) i->thread_info.playing_for;
1796 i->thread_info.rewrite_nbytes = nbytes;
1798 i->thread_info.rewrite_nbytes = (size_t) -1;
1801 i->thread_info.rewrite_flush =
1802 i->thread_info.rewrite_flush ||
1803 (flush && i->thread_info.rewrite_nbytes != 0);
1805 i->thread_info.dont_rewind_render =
1806 i->thread_info.dont_rewind_render ||
1809 if (nbytes != (size_t) -1) {
1811 /* Transform to sink domain */
1812 if (i->thread_info.resampler)
1813 nbytes = pa_resampler_result(i->thread_info.resampler, nbytes);
1816 pa_sink_request_rewind(i->sink, nbytes - lbq);
1818 /* This call will make sure process_rewind() is called later */
1819 pa_sink_request_rewind(i->sink, 0);
1823 /* Called from main context */
1824 pa_memchunk* pa_sink_input_get_silence(pa_sink_input *i, pa_memchunk *ret) {
1825 pa_sink_input_assert_ref(i);
1826 pa_assert_ctl_context();
1829 /* FIXME: Shouldn't access resampler object from main context! */
1831 pa_silence_memchunk_get(
1832 &i->core->silence_cache,
1836 i->thread_info.resampler ? pa_resampler_max_block_size(i->thread_info.resampler) : 0);
1841 /* Called from main context */
1842 void pa_sink_input_send_event(pa_sink_input *i, const char *event, pa_proplist *data) {
1843 pa_proplist *pl = NULL;
1844 pa_sink_input_send_event_hook_data hook_data;
1846 pa_sink_input_assert_ref(i);
1847 pa_assert_ctl_context();
1854 data = pl = pa_proplist_new();
1856 hook_data.sink_input = i;
1857 hook_data.data = data;
1858 hook_data.event = event;
1860 if (pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_SEND_EVENT], &hook_data) < 0)
1863 i->send_event(i, event, data);
1867 pa_proplist_free(pl);