2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/utf8.h>
31 #include <pulse/xmalloc.h>
32 #include <pulse/util.h>
33 #include <pulse/internal.h>
35 #include <pulsecore/core-format.h>
36 #include <pulsecore/mix.h>
37 #include <pulsecore/stream-util.h>
38 #include <pulsecore/core-subscribe.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/namereg.h>
41 #include <pulsecore/core-util.h>
43 #include "source-output.h"
45 #define MEMBLOCKQ_MAXLENGTH (32*1024*1024)
47 PA_DEFINE_PUBLIC_CLASS(pa_source_output, pa_msgobject);
49 static void source_output_free(pa_object* mo);
50 static void set_real_ratio(pa_source_output *o, const pa_cvolume *v);
52 pa_source_output_new_data* pa_source_output_new_data_init(pa_source_output_new_data *data) {
56 data->resample_method = PA_RESAMPLER_INVALID;
57 data->proplist = pa_proplist_new();
58 data->volume_writable = true;
63 void pa_source_output_new_data_set_sample_spec(pa_source_output_new_data *data, const pa_sample_spec *spec) {
66 if ((data->sample_spec_is_set = !!spec))
67 data->sample_spec = *spec;
70 void pa_source_output_new_data_set_channel_map(pa_source_output_new_data *data, const pa_channel_map *map) {
73 if ((data->channel_map_is_set = !!map))
74 data->channel_map = *map;
77 bool pa_source_output_new_data_is_passthrough(pa_source_output_new_data *data) {
80 if (PA_LIKELY(data->format) && PA_UNLIKELY(!pa_format_info_is_pcm(data->format)))
83 if (PA_UNLIKELY(data->flags & PA_SOURCE_OUTPUT_PASSTHROUGH))
89 void pa_source_output_new_data_set_volume(pa_source_output_new_data *data, const pa_cvolume *volume) {
91 pa_assert(data->volume_writable);
93 if ((data->volume_is_set = !!volume))
94 data->volume = *volume;
97 void pa_source_output_new_data_apply_volume_factor(pa_source_output_new_data *data, const pa_cvolume *volume_factor) {
99 pa_assert(volume_factor);
101 if (data->volume_factor_is_set)
102 pa_sw_cvolume_multiply(&data->volume_factor, &data->volume_factor, volume_factor);
104 data->volume_factor_is_set = true;
105 data->volume_factor = *volume_factor;
109 void pa_source_output_new_data_apply_volume_factor_source(pa_source_output_new_data *data, const pa_cvolume *volume_factor) {
111 pa_assert(volume_factor);
113 if (data->volume_factor_source_is_set)
114 pa_sw_cvolume_multiply(&data->volume_factor_source, &data->volume_factor_source, volume_factor);
116 data->volume_factor_source_is_set = true;
117 data->volume_factor_source = *volume_factor;
121 void pa_source_output_new_data_set_muted(pa_source_output_new_data *data, bool mute) {
124 data->muted_is_set = true;
125 data->muted = !!mute;
128 bool pa_source_output_new_data_set_source(pa_source_output_new_data *data, pa_source *s, bool save) {
130 pa_idxset *formats = NULL;
135 if (!data->req_formats) {
136 /* We're not working with the extended API */
138 data->save_source = save;
140 /* Extended API: let's see if this source supports the formats the client would like */
141 formats = pa_source_check_formats(s, data->req_formats);
143 if (formats && !pa_idxset_isempty(formats)) {
144 /* Source supports at least one of the requested formats */
146 data->save_source = save;
147 if (data->nego_formats)
148 pa_idxset_free(data->nego_formats, (pa_free_cb_t) pa_format_info_free);
149 data->nego_formats = formats;
151 /* Source doesn't support any of the formats requested by the client */
153 pa_idxset_free(formats, (pa_free_cb_t) pa_format_info_free);
161 bool pa_source_output_new_data_set_formats(pa_source_output_new_data *data, pa_idxset *formats) {
165 if (data->req_formats)
166 pa_idxset_free(data->req_formats, (pa_free_cb_t) pa_format_info_free);
168 data->req_formats = formats;
171 /* Trigger format negotiation */
172 return pa_source_output_new_data_set_source(data, data->source, data->save_source);
178 void pa_source_output_new_data_done(pa_source_output_new_data *data) {
181 if (data->req_formats)
182 pa_idxset_free(data->req_formats, (pa_free_cb_t) pa_format_info_free);
184 if (data->nego_formats)
185 pa_idxset_free(data->nego_formats, (pa_free_cb_t) pa_format_info_free);
188 pa_format_info_free(data->format);
190 pa_proplist_free(data->proplist);
193 /* Called from main context */
194 static void reset_callbacks(pa_source_output *o) {
198 o->process_rewind = NULL;
199 o->update_max_rewind = NULL;
200 o->update_source_requested_latency = NULL;
201 o->update_source_latency_range = NULL;
202 o->update_source_fixed_latency = NULL;
206 o->suspend_within_thread = NULL;
209 o->get_latency = NULL;
210 o->state_change = NULL;
211 o->may_move_to = NULL;
212 o->send_event = NULL;
213 o->volume_changed = NULL;
214 o->mute_changed = NULL;
217 /* Called from main context */
218 int pa_source_output_new(
219 pa_source_output**_o,
221 pa_source_output_new_data *data) {
224 pa_resampler *resampler = NULL;
225 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX], fmt[PA_FORMAT_INFO_SNPRINT_MAX];
226 pa_channel_map volume_map;
233 pa_assert_ctl_context();
236 pa_proplist_update(data->proplist, PA_UPDATE_MERGE, data->client->proplist);
238 if (data->destination_source && (data->destination_source->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER))
239 data->volume_writable = false;
241 if (!data->req_formats) {
242 /* From this point on, we want to work only with formats, and get back
243 * to using the sample spec and channel map after all decisions w.r.t.
244 * routing are complete. */
248 f = pa_format_info_from_sample_spec2(&data->sample_spec, data->channel_map_is_set ? &data->channel_map : NULL,
249 !(data->flags & PA_SOURCE_OUTPUT_FIX_FORMAT),
250 !(data->flags & PA_SOURCE_OUTPUT_FIX_RATE),
251 !(data->flags & PA_SOURCE_OUTPUT_FIX_CHANNELS));
253 return -PA_ERR_INVALID;
255 formats = pa_idxset_new(NULL, NULL);
256 pa_idxset_put(formats, f, NULL);
257 pa_source_output_new_data_set_formats(data, formats);
260 if ((r = pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], data)) < 0)
263 pa_return_val_if_fail(!data->driver || pa_utf8_valid(data->driver), -PA_ERR_INVALID);
268 if (data->direct_on_input) {
269 source = data->direct_on_input->sink->monitor_source;
270 pa_return_val_if_fail(source, -PA_ERR_INVALID);
272 source = pa_namereg_get(core, NULL, PA_NAMEREG_SOURCE);
273 pa_return_val_if_fail(source, -PA_ERR_NOENTITY);
276 pa_source_output_new_data_set_source(data, source, false);
279 /* If something didn't pick a format for us, pick the top-most format since
280 * we assume this is sorted in priority order */
281 if (!data->format && data->nego_formats && !pa_idxset_isempty(data->nego_formats))
282 data->format = pa_format_info_copy(pa_idxset_first(data->nego_formats, NULL));
284 if (PA_LIKELY(data->format)) {
285 pa_log_debug("Negotiated format: %s", pa_format_info_snprint(fmt, sizeof(fmt), data->format));
287 pa_format_info *format;
290 pa_log_info("Source does not support any requested format:");
291 PA_IDXSET_FOREACH(format, data->req_formats, idx)
292 pa_log_info(" -- %s", pa_format_info_snprint(fmt, sizeof(fmt), format));
294 return -PA_ERR_NOTSUPPORTED;
297 pa_return_val_if_fail(PA_SOURCE_IS_LINKED(pa_source_get_state(data->source)), -PA_ERR_BADSTATE);
298 pa_return_val_if_fail(!data->direct_on_input || data->direct_on_input->sink == data->source->monitor_of, -PA_ERR_INVALID);
300 /* Routing is done. We have a source and a format. */
302 if (data->volume_is_set && pa_format_info_is_pcm(data->format)) {
303 /* If volume is set, we need to save the original data->channel_map,
304 * so that we can remap the volume from the original channel map to the
305 * final channel map of the stream in case data->channel_map gets
306 * modified in pa_format_info_to_sample_spec2(). */
307 r = pa_stream_get_volume_channel_map(&data->volume, data->channel_map_is_set ? &data->channel_map : NULL, data->format, &volume_map);
312 /* Now populate the sample spec and channel map according to the final
313 * format that we've negotiated */
314 r = pa_format_info_to_sample_spec2(data->format, &data->sample_spec, &data->channel_map, &data->source->sample_spec,
315 &data->source->channel_map);
319 /* Don't restore (or save) stream volume for passthrough streams and
320 * prevent attenuation/gain */
321 if (pa_source_output_new_data_is_passthrough(data)) {
322 data->volume_is_set = true;
323 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
324 data->volume_is_absolute = true;
325 data->save_volume = false;
328 if (!data->volume_is_set) {
329 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
330 data->volume_is_absolute = false;
331 data->save_volume = false;
334 if (!data->volume_writable)
335 data->save_volume = false;
337 if (data->volume_is_set)
338 /* The original volume channel map may be different than the final
339 * stream channel map, so remapping may be needed. */
340 pa_cvolume_remap(&data->volume, &volume_map, &data->channel_map);
342 if (!data->volume_factor_is_set)
343 pa_cvolume_reset(&data->volume_factor, data->sample_spec.channels);
345 pa_return_val_if_fail(pa_cvolume_compatible(&data->volume_factor, &data->sample_spec), -PA_ERR_INVALID);
347 if (!data->volume_factor_source_is_set)
348 pa_cvolume_reset(&data->volume_factor_source, data->source->sample_spec.channels);
350 pa_return_val_if_fail(pa_cvolume_compatible(&data->volume_factor_source, &data->source->sample_spec), -PA_ERR_INVALID);
352 if (!data->muted_is_set)
355 if (!(data->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) &&
356 !pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec)) {
357 /* try to change source rate. This is done before the FIXATE hook since
358 module-suspend-on-idle can resume a source */
360 pa_log_info("Trying to change sample rate");
361 if (pa_source_update_rate(data->source, data->sample_spec.rate, pa_source_output_new_data_is_passthrough(data)) >= 0)
362 pa_log_info("Rate changed to %u Hz", data->source->sample_spec.rate);
365 if (pa_source_output_new_data_is_passthrough(data) &&
366 !pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec)) {
367 /* rate update failed, or other parts of sample spec didn't match */
369 pa_log_debug("Could not update source sample spec to match passthrough stream");
370 return -PA_ERR_NOTSUPPORTED;
373 if (data->resample_method == PA_RESAMPLER_INVALID)
374 data->resample_method = core->resample_method;
376 pa_return_val_if_fail(data->resample_method < PA_RESAMPLER_MAX, -PA_ERR_INVALID);
378 if ((r = pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE], data)) < 0)
381 if ((data->flags & PA_SOURCE_OUTPUT_NO_CREATE_ON_SUSPEND) &&
382 pa_source_get_state(data->source) == PA_SOURCE_SUSPENDED) {
383 pa_log("Failed to create source output: source is suspended.");
384 return -PA_ERR_BADSTATE;
387 if (pa_idxset_size(data->source->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
388 pa_log("Failed to create source output: too many outputs per source.");
389 return -PA_ERR_TOOLARGE;
392 if ((data->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ||
393 !pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec) ||
394 !pa_channel_map_equal(&data->channel_map, &data->source->channel_map)) {
396 if (!pa_source_output_new_data_is_passthrough(data)) /* no resampler for passthrough content */
397 if (!(resampler = pa_resampler_new(
399 &data->source->sample_spec, &data->source->channel_map,
400 &data->sample_spec, &data->channel_map,
401 data->resample_method,
402 ((data->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
403 ((data->flags & PA_SOURCE_OUTPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
404 (core->disable_remixing || (data->flags & PA_SOURCE_OUTPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0) |
405 (core->disable_lfe_remixing ? PA_RESAMPLER_NO_LFE : 0)))) {
406 pa_log_warn("Unsupported resampling operation.");
407 return -PA_ERR_NOTSUPPORTED;
411 o = pa_msgobject_new(pa_source_output);
412 o->parent.parent.free = source_output_free;
413 o->parent.process_msg = pa_source_output_process_msg;
416 o->state = PA_SOURCE_OUTPUT_INIT;
417 o->flags = data->flags;
418 o->proplist = pa_proplist_copy(data->proplist);
419 o->driver = pa_xstrdup(pa_path_get_filename(data->driver));
420 o->module = data->module;
421 o->source = data->source;
422 o->destination_source = data->destination_source;
423 o->client = data->client;
425 o->requested_resample_method = data->resample_method;
426 o->actual_resample_method = resampler ? pa_resampler_get_method(resampler) : PA_RESAMPLER_INVALID;
427 o->sample_spec = data->sample_spec;
428 o->channel_map = data->channel_map;
429 o->format = pa_format_info_copy(data->format);
431 if (!data->volume_is_absolute && pa_source_flat_volume_enabled(o->source)) {
434 /* When the 'absolute' bool is not set then we'll treat the volume
435 * as relative to the source volume even in flat volume mode */
436 remapped = data->source->reference_volume;
437 pa_cvolume_remap(&remapped, &data->source->channel_map, &data->channel_map);
438 pa_sw_cvolume_multiply(&o->volume, &data->volume, &remapped);
440 o->volume = data->volume;
442 o->volume_factor = data->volume_factor;
443 o->volume_factor_source = data->volume_factor_source;
444 o->real_ratio = o->reference_ratio = data->volume;
445 pa_cvolume_reset(&o->soft_volume, o->sample_spec.channels);
446 pa_cvolume_reset(&o->real_ratio, o->sample_spec.channels);
447 o->volume_writable = data->volume_writable;
448 o->save_volume = data->save_volume;
449 o->save_source = data->save_source;
450 o->save_muted = data->save_muted;
452 o->muted = data->muted;
454 o->direct_on_input = data->direct_on_input;
459 o->thread_info.state = o->state;
460 o->thread_info.attached = false;
461 o->thread_info.sample_spec = o->sample_spec;
462 o->thread_info.resampler = resampler;
463 o->thread_info.soft_volume = o->soft_volume;
464 o->thread_info.muted = o->muted;
465 o->thread_info.requested_source_latency = (pa_usec_t) -1;
466 o->thread_info.direct_on_input = o->direct_on_input;
468 o->thread_info.delay_memblockq = pa_memblockq_new(
469 "source output delay_memblockq",
473 &o->source->sample_spec,
477 &o->source->silence);
479 pa_assert_se(pa_idxset_put(core->source_outputs, o, &o->index) == 0);
480 pa_assert_se(pa_idxset_put(o->source->outputs, pa_source_output_ref(o), NULL) == 0);
483 pa_assert_se(pa_idxset_put(o->client->source_outputs, o, NULL) >= 0);
485 if (o->direct_on_input)
486 pa_assert_se(pa_idxset_put(o->direct_on_input->direct_outputs, o, NULL) == 0);
488 pt = pa_proplist_to_string_sep(o->proplist, "\n ");
489 pa_log_info("Created output %u \"%s\" on %s with sample spec %s and channel map %s\n %s",
491 pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_MEDIA_NAME)),
493 pa_sample_spec_snprint(st, sizeof(st), &o->sample_spec),
494 pa_channel_map_snprint(cm, sizeof(cm), &o->channel_map),
498 /* Don't forget to call pa_source_output_put! */
504 /* Called from main context */
505 static void update_n_corked(pa_source_output *o, pa_source_output_state_t state) {
507 pa_assert_ctl_context();
512 if (o->state == PA_SOURCE_OUTPUT_CORKED && state != PA_SOURCE_OUTPUT_CORKED)
513 pa_assert_se(o->source->n_corked -- >= 1);
514 else if (o->state != PA_SOURCE_OUTPUT_CORKED && state == PA_SOURCE_OUTPUT_CORKED)
515 o->source->n_corked++;
518 /* Called from main context */
519 static void source_output_set_state(pa_source_output *o, pa_source_output_state_t state) {
522 pa_assert_ctl_context();
524 if (o->state == state)
527 if (o->state == PA_SOURCE_OUTPUT_CORKED && state == PA_SOURCE_OUTPUT_RUNNING && pa_source_used_by(o->source) == 0 &&
528 !pa_sample_spec_equal(&o->sample_spec, &o->source->sample_spec)) {
529 /* We were uncorked and the source was not playing anything -- let's try
530 * to update the sample rate to avoid resampling */
531 pa_source_update_rate(o->source, o->sample_spec.rate, pa_source_output_is_passthrough(o));
534 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) == 0);
536 update_n_corked(o, state);
539 if (state != PA_SOURCE_OUTPUT_UNLINKED) {
540 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED], o);
542 if (PA_SOURCE_OUTPUT_IS_LINKED(state))
543 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
546 pa_source_update_status(o->source);
549 /* Called from main context */
550 void pa_source_output_unlink(pa_source_output*o) {
553 pa_assert_ctl_context();
555 /* See pa_sink_unlink() for a couple of comments how this function
558 pa_source_output_ref(o);
560 linked = PA_SOURCE_OUTPUT_IS_LINKED(o->state);
563 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK], o);
565 if (o->direct_on_input)
566 pa_idxset_remove_by_data(o->direct_on_input->direct_outputs, o, NULL);
568 pa_idxset_remove_by_data(o->core->source_outputs, o, NULL);
571 if (pa_idxset_remove_by_data(o->source->outputs, o, NULL))
572 pa_source_output_unref(o);
575 pa_idxset_remove_by_data(o->client->source_outputs, o, NULL);
577 update_n_corked(o, PA_SOURCE_OUTPUT_UNLINKED);
578 o->state = PA_SOURCE_OUTPUT_UNLINKED;
580 if (linked && o->source) {
581 if (pa_source_output_is_passthrough(o))
582 pa_source_leave_passthrough(o->source);
584 /* We might need to update the source's volume if we are in flat volume mode. */
585 if (pa_source_flat_volume_enabled(o->source))
586 pa_source_set_volume(o->source, NULL, false, false);
588 if (o->source->asyncmsgq)
589 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL) == 0);
595 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_REMOVE, o->index);
596 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST], o);
600 if (PA_SOURCE_IS_LINKED(pa_source_get_state(o->source)))
601 pa_source_update_status(o->source);
606 pa_core_maybe_vacuum(o->core);
608 pa_source_output_unref(o);
611 /* Called from main context */
612 static void source_output_free(pa_object* mo) {
613 pa_source_output *o = PA_SOURCE_OUTPUT(mo);
616 pa_assert_ctl_context();
617 pa_assert(pa_source_output_refcnt(o) == 0);
619 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state))
620 pa_source_output_unlink(o);
622 pa_log_info("Freeing output %u \"%s\"", o->index,
623 o->proplist ? pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_MEDIA_NAME)) : "");
625 if (o->thread_info.delay_memblockq)
626 pa_memblockq_free(o->thread_info.delay_memblockq);
628 if (o->thread_info.resampler)
629 pa_resampler_free(o->thread_info.resampler);
632 pa_format_info_free(o->format);
635 pa_proplist_free(o->proplist);
641 /* Called from main context */
642 void pa_source_output_put(pa_source_output *o) {
643 pa_source_output_state_t state;
645 pa_source_output_assert_ref(o);
646 pa_assert_ctl_context();
648 pa_assert(o->state == PA_SOURCE_OUTPUT_INIT);
650 /* The following fields must be initialized properly */
654 state = o->flags & PA_SOURCE_OUTPUT_START_CORKED ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING;
656 update_n_corked(o, state);
659 /* We might need to update the source's volume if we are in flat volume mode. */
660 if (pa_source_flat_volume_enabled(o->source))
661 pa_source_set_volume(o->source, NULL, false, o->save_volume);
663 if (o->destination_source && (o->destination_source->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER)) {
664 pa_assert(pa_cvolume_is_norm(&o->volume));
665 pa_assert(pa_cvolume_is_norm(&o->reference_ratio));
668 set_real_ratio(o, &o->volume);
671 if (pa_source_output_is_passthrough(o))
672 pa_source_enter_passthrough(o->source);
674 o->thread_info.soft_volume = o->soft_volume;
675 o->thread_info.muted = o->muted;
677 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_ADD_OUTPUT, o, 0, NULL) == 0);
679 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, o->index);
680 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PUT], o);
682 pa_source_update_status(o->source);
685 /* Called from main context */
686 void pa_source_output_kill(pa_source_output*o) {
687 pa_source_output_assert_ref(o);
688 pa_assert_ctl_context();
689 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
694 /* Called from main context */
695 pa_usec_t pa_source_output_get_latency(pa_source_output *o, pa_usec_t *source_latency) {
696 pa_usec_t r[2] = { 0, 0 };
698 pa_source_output_assert_ref(o);
699 pa_assert_ctl_context();
700 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
702 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY, r, 0, NULL) == 0);
705 r[0] += o->get_latency(o);
708 *source_latency = r[1];
713 /* Called from thread context */
714 void pa_source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
715 bool need_volume_factor_source;
718 size_t limit, mbs = 0;
720 pa_source_output_assert_ref(o);
721 pa_source_output_assert_io_context(o);
722 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
724 pa_assert(pa_frame_aligned(chunk->length, &o->source->sample_spec));
726 if (!o->push || o->thread_info.state == PA_SOURCE_OUTPUT_CORKED)
729 pa_assert(o->thread_info.state == PA_SOURCE_OUTPUT_RUNNING);
731 if (pa_memblockq_push(o->thread_info.delay_memblockq, chunk) < 0) {
732 pa_log_debug("Delay queue overflow!");
733 pa_memblockq_seek(o->thread_info.delay_memblockq, (int64_t) chunk->length, PA_SEEK_RELATIVE, true);
736 limit = o->process_rewind ? 0 : o->source->thread_info.max_rewind;
738 volume_is_norm = pa_cvolume_is_norm(&o->thread_info.soft_volume) && !o->thread_info.muted;
739 need_volume_factor_source = !pa_cvolume_is_norm(&o->volume_factor_source);
741 if (limit > 0 && o->source->monitor_of) {
745 /* Hmm, check the latency for knowing how much of the buffered
746 * data is actually still unplayed and might hence still
747 * change. This is suboptimal. Ideally we'd have a call like
748 * pa_sink_get_changeable_size() or so that tells us how much
749 * of the queued data is actually still changeable. Hence
752 latency = pa_sink_get_latency_within_thread(o->source->monitor_of);
754 n = pa_usec_to_bytes(latency, &o->source->sample_spec);
760 /* Implement the delay queue */
761 while ((length = pa_memblockq_get_length(o->thread_info.delay_memblockq)) > limit) {
763 bool nvfs = need_volume_factor_source;
767 pa_assert_se(pa_memblockq_peek(o->thread_info.delay_memblockq, &qchunk) >= 0);
769 if (qchunk.length > length)
770 qchunk.length = length;
772 pa_assert(qchunk.length > 0);
774 /* It might be necessary to adjust the volume here */
775 if (!volume_is_norm) {
776 pa_memchunk_make_writable(&qchunk, 0);
778 if (o->thread_info.muted) {
779 pa_silence_memchunk(&qchunk, &o->source->sample_spec);
782 } else if (!o->thread_info.resampler && nvfs) {
785 /* If we don't need a resampler we can merge the
786 * post and the pre volume adjustment into one */
788 pa_sw_cvolume_multiply(&v, &o->thread_info.soft_volume, &o->volume_factor_source);
789 pa_volume_memchunk(&qchunk, &o->source->sample_spec, &v);
793 pa_volume_memchunk(&qchunk, &o->source->sample_spec, &o->thread_info.soft_volume);
796 if (!o->thread_info.resampler) {
798 pa_memchunk_make_writable(&qchunk, 0);
799 pa_volume_memchunk(&qchunk, &o->thread_info.sample_spec, &o->volume_factor_source);
807 mbs = pa_resampler_max_block_size(o->thread_info.resampler);
809 if (qchunk.length > mbs)
812 pa_resampler_run(o->thread_info.resampler, &qchunk, &rchunk);
814 if (rchunk.length > 0) {
816 pa_memchunk_make_writable(&rchunk, 0);
817 pa_volume_memchunk(&rchunk, &o->thread_info.sample_spec, &o->volume_factor_source);
824 pa_memblock_unref(rchunk.memblock);
827 pa_memblock_unref(qchunk.memblock);
828 pa_memblockq_drop(o->thread_info.delay_memblockq, qchunk.length);
832 /* Called from thread context */
833 void pa_source_output_process_rewind(pa_source_output *o, size_t nbytes /* in source sample spec */) {
835 pa_source_output_assert_ref(o);
836 pa_source_output_assert_io_context(o);
837 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
838 pa_assert(pa_frame_aligned(nbytes, &o->source->sample_spec));
843 if (o->process_rewind) {
844 pa_assert(pa_memblockq_get_length(o->thread_info.delay_memblockq) == 0);
846 if (o->thread_info.resampler)
847 nbytes = pa_resampler_result(o->thread_info.resampler, nbytes);
849 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) nbytes);
852 o->process_rewind(o, nbytes);
854 if (o->thread_info.resampler)
855 pa_resampler_reset(o->thread_info.resampler);
858 pa_memblockq_rewind(o->thread_info.delay_memblockq, nbytes);
861 /* Called from thread context */
862 size_t pa_source_output_get_max_rewind(pa_source_output *o) {
863 pa_source_output_assert_ref(o);
864 pa_source_output_assert_io_context(o);
866 return o->thread_info.resampler ? pa_resampler_request(o->thread_info.resampler, o->source->thread_info.max_rewind) : o->source->thread_info.max_rewind;
869 /* Called from thread context */
870 void pa_source_output_update_max_rewind(pa_source_output *o, size_t nbytes /* in the source's sample spec */) {
871 pa_source_output_assert_ref(o);
872 pa_source_output_assert_io_context(o);
873 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
874 pa_assert(pa_frame_aligned(nbytes, &o->source->sample_spec));
876 if (o->update_max_rewind)
877 o->update_max_rewind(o, o->thread_info.resampler ? pa_resampler_result(o->thread_info.resampler, nbytes) : nbytes);
880 /* Called from thread context */
881 pa_usec_t pa_source_output_set_requested_latency_within_thread(pa_source_output *o, pa_usec_t usec) {
882 pa_source_output_assert_ref(o);
883 pa_source_output_assert_io_context(o);
885 if (!(o->source->flags & PA_SOURCE_DYNAMIC_LATENCY))
886 usec = o->source->thread_info.fixed_latency;
888 if (usec != (pa_usec_t) -1)
889 usec = PA_CLAMP(usec, o->source->thread_info.min_latency, o->source->thread_info.max_latency);
891 o->thread_info.requested_source_latency = usec;
892 pa_source_invalidate_requested_latency(o->source, true);
897 /* Called from main context */
898 pa_usec_t pa_source_output_set_requested_latency(pa_source_output *o, pa_usec_t usec) {
899 pa_source_output_assert_ref(o);
900 pa_assert_ctl_context();
902 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state) && o->source) {
903 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
907 /* If this source output is not realized yet or is being moved, we
908 * have to touch the thread info data directly */
911 if (!(o->source->flags & PA_SOURCE_DYNAMIC_LATENCY))
912 usec = pa_source_get_fixed_latency(o->source);
914 if (usec != (pa_usec_t) -1) {
915 pa_usec_t min_latency, max_latency;
916 pa_source_get_latency_range(o->source, &min_latency, &max_latency);
917 usec = PA_CLAMP(usec, min_latency, max_latency);
921 o->thread_info.requested_source_latency = usec;
926 /* Called from main context */
927 pa_usec_t pa_source_output_get_requested_latency(pa_source_output *o) {
928 pa_source_output_assert_ref(o);
929 pa_assert_ctl_context();
931 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state) && o->source) {
933 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
937 /* If this source output is not realized yet or is being moved, we
938 * have to touch the thread info data directly */
940 return o->thread_info.requested_source_latency;
943 /* Called from main context */
944 void pa_source_output_set_volume(pa_source_output *o, const pa_cvolume *volume, bool save, bool absolute) {
947 pa_source_output_assert_ref(o);
948 pa_assert_ctl_context();
949 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
951 pa_assert(pa_cvolume_valid(volume));
952 pa_assert(volume->channels == 1 || pa_cvolume_compatible(volume, &o->sample_spec));
953 pa_assert(o->volume_writable);
955 if (!absolute && pa_source_flat_volume_enabled(o->source)) {
956 v = o->source->reference_volume;
957 pa_cvolume_remap(&v, &o->source->channel_map, &o->channel_map);
959 if (pa_cvolume_compatible(volume, &o->sample_spec))
960 volume = pa_sw_cvolume_multiply(&v, &v, volume);
962 volume = pa_sw_cvolume_multiply_scalar(&v, &v, pa_cvolume_max(volume));
964 if (!pa_cvolume_compatible(volume, &o->sample_spec)) {
966 volume = pa_cvolume_scale(&v, pa_cvolume_max(volume));
970 if (pa_cvolume_equal(volume, &o->volume)) {
971 o->save_volume = o->save_volume || save;
976 o->save_volume = save;
978 if (pa_source_flat_volume_enabled(o->source)) {
979 /* We are in flat volume mode, so let's update all source input
980 * volumes and update the flat volume of the source */
982 pa_source_set_volume(o->source, NULL, true, save);
985 /* OK, we are in normal volume mode. The volume only affects
987 set_real_ratio(o, volume);
989 /* Copy the new soft_volume to the thread_info struct */
990 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_VOLUME, NULL, 0, NULL) == 0);
993 /* The volume changed, let's tell people so */
994 if (o->volume_changed)
995 o->volume_changed(o);
997 /* The virtual volume changed, let's tell people so */
998 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1001 /* Called from main context */
1002 static void set_real_ratio(pa_source_output *o, const pa_cvolume *v) {
1003 pa_source_output_assert_ref(o);
1004 pa_assert_ctl_context();
1005 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1006 pa_assert(!v || pa_cvolume_compatible(v, &o->sample_spec));
1008 /* This basically calculates:
1010 * o->real_ratio := v
1011 * o->soft_volume := o->real_ratio * o->volume_factor */
1016 pa_cvolume_reset(&o->real_ratio, o->sample_spec.channels);
1018 pa_sw_cvolume_multiply(&o->soft_volume, &o->real_ratio, &o->volume_factor);
1019 /* We don't copy the data to the thread_info data. That's left for someone else to do */
1022 /* Called from main or I/O context */
1023 bool pa_source_output_is_passthrough(pa_source_output *o) {
1024 pa_source_output_assert_ref(o);
1026 if (PA_UNLIKELY(!pa_format_info_is_pcm(o->format)))
1029 if (PA_UNLIKELY(o->flags & PA_SOURCE_OUTPUT_PASSTHROUGH))
1035 /* Called from main context */
1036 bool pa_source_output_is_volume_readable(pa_source_output *o) {
1037 pa_source_output_assert_ref(o);
1038 pa_assert_ctl_context();
1040 return !pa_source_output_is_passthrough(o);
1043 /* Called from main context */
1044 pa_cvolume *pa_source_output_get_volume(pa_source_output *o, pa_cvolume *volume, bool absolute) {
1045 pa_source_output_assert_ref(o);
1046 pa_assert_ctl_context();
1047 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1048 pa_assert(pa_source_output_is_volume_readable(o));
1050 if (absolute || !pa_source_flat_volume_enabled(o->source))
1051 *volume = o->volume;
1053 *volume = o->reference_ratio;
1058 /* Called from main context */
1059 void pa_source_output_set_mute(pa_source_output *o, bool mute, bool save) {
1060 pa_source_output_assert_ref(o);
1061 pa_assert_ctl_context();
1062 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1064 if (!o->muted == !mute) {
1065 o->save_muted = o->save_muted || mute;
1070 o->save_muted = save;
1072 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE, NULL, 0, NULL) == 0);
1074 /* The mute status changed, let's tell people so */
1075 if (o->mute_changed)
1078 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1081 /* Called from main context */
1082 bool pa_source_output_get_mute(pa_source_output *o) {
1083 pa_source_output_assert_ref(o);
1084 pa_assert_ctl_context();
1085 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1090 /* Called from main thread */
1091 void pa_source_output_update_proplist(pa_source_output *o, pa_update_mode_t mode, pa_proplist *p) {
1092 pa_source_output_assert_ref(o);
1093 pa_assert_ctl_context();
1096 pa_proplist_update(o->proplist, mode, p);
1098 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state)) {
1099 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED], o);
1100 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1104 /* Called from main context */
1105 void pa_source_output_cork(pa_source_output *o, bool b) {
1106 pa_source_output_assert_ref(o);
1107 pa_assert_ctl_context();
1108 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1110 source_output_set_state(o, b ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING);
1113 /* Called from main context */
1114 int pa_source_output_set_rate(pa_source_output *o, uint32_t rate) {
1115 pa_source_output_assert_ref(o);
1116 pa_assert_ctl_context();
1117 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1118 pa_return_val_if_fail(o->thread_info.resampler, -PA_ERR_BADSTATE);
1120 if (o->sample_spec.rate == rate)
1123 o->sample_spec.rate = rate;
1125 pa_asyncmsgq_post(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE, PA_UINT_TO_PTR(rate), 0, NULL, NULL);
1127 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1131 /* Called from main context */
1132 void pa_source_output_set_name(pa_source_output *o, const char *name) {
1134 pa_assert_ctl_context();
1135 pa_source_output_assert_ref(o);
1137 if (!name && !pa_proplist_contains(o->proplist, PA_PROP_MEDIA_NAME))
1140 old = pa_proplist_gets(o->proplist, PA_PROP_MEDIA_NAME);
1142 if (old && name && pa_streq(old, name))
1146 pa_proplist_sets(o->proplist, PA_PROP_MEDIA_NAME, name);
1148 pa_proplist_unset(o->proplist, PA_PROP_MEDIA_NAME);
1150 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state)) {
1151 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED], o);
1152 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1156 /* Called from main context */
1157 pa_resample_method_t pa_source_output_get_resample_method(pa_source_output *o) {
1158 pa_source_output_assert_ref(o);
1159 pa_assert_ctl_context();
1161 return o->actual_resample_method;
1164 /* Called from main context */
1165 bool pa_source_output_may_move(pa_source_output *o) {
1166 pa_source_output_assert_ref(o);
1167 pa_assert_ctl_context();
1168 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1170 if (o->flags & PA_SOURCE_OUTPUT_DONT_MOVE)
1173 if (o->direct_on_input)
1179 static bool find_filter_source_output(pa_source_output *target, pa_source *s) {
1181 while (s && s->output_from_master) {
1182 if (s->output_from_master == target)
1184 s = s->output_from_master->source;
1185 pa_assert(i++ < 100);
1190 /* Called from main context */
1191 bool pa_source_output_may_move_to(pa_source_output *o, pa_source *dest) {
1192 pa_source_output_assert_ref(o);
1193 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1194 pa_source_assert_ref(dest);
1196 if (dest == o->source)
1199 if (!pa_source_output_may_move(o))
1202 /* Make sure we're not creating a filter source cycle */
1203 if (find_filter_source_output(o, dest)) {
1204 pa_log_debug("Can't connect output to %s, as that would create a cycle.", dest->name);
1208 if (pa_idxset_size(dest->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
1209 pa_log_warn("Failed to move source output: too many outputs per source.");
1214 if (!o->may_move_to(o, dest))
1220 /* Called from main context */
1221 int pa_source_output_start_move(pa_source_output *o) {
1225 pa_source_output_assert_ref(o);
1226 pa_assert_ctl_context();
1227 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1228 pa_assert(o->source);
1230 if (!pa_source_output_may_move(o))
1231 return -PA_ERR_NOTSUPPORTED;
1233 if ((r = pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START], o)) < 0)
1238 pa_idxset_remove_by_data(o->source->outputs, o, NULL);
1240 if (pa_source_output_get_state(o) == PA_SOURCE_OUTPUT_CORKED)
1241 pa_assert_se(origin->n_corked-- >= 1);
1243 if (pa_source_output_is_passthrough(o))
1244 pa_source_leave_passthrough(o->source);
1246 if (pa_source_flat_volume_enabled(o->source))
1247 /* We might need to update the source's volume if we are in flat
1249 pa_source_set_volume(o->source, NULL, false, false);
1251 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL) == 0);
1253 pa_source_update_status(o->source);
1256 pa_source_output_unref(o);
1261 /* Called from main context. If it has an origin source that uses volume sharing,
1262 * then also the origin source and all streams connected to it need to update
1263 * their volume - this function does all that by using recursion. */
1264 static void update_volume_due_to_moving(pa_source_output *o, pa_source *dest) {
1265 pa_cvolume old_volume;
1269 pa_assert(o->source); /* The destination source should already be set. */
1271 if (o->destination_source && (o->destination_source->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER)) {
1272 pa_source *root_source;
1273 pa_source_output *destination_source_output;
1276 root_source = pa_source_get_master(o->source);
1278 if (PA_UNLIKELY(!root_source))
1281 if (pa_source_flat_volume_enabled(o->source)) {
1282 /* Ok, so the origin source uses volume sharing, and flat volume is
1283 * enabled. The volume will have to be updated as follows:
1285 * o->volume := o->source->real_volume
1286 * (handled later by pa_source_set_volume)
1287 * o->reference_ratio := o->volume / o->source->reference_volume
1288 * (handled later by pa_source_set_volume)
1289 * o->real_ratio stays unchanged
1290 * (streams whose origin source uses volume sharing should
1291 * always have real_ratio of 0 dB)
1292 * o->soft_volume stays unchanged
1293 * (streams whose origin source uses volume sharing should
1294 * always have volume_factor as soft_volume, so no change
1295 * should be needed) */
1297 pa_assert(pa_cvolume_is_norm(&o->real_ratio));
1298 pa_assert(pa_cvolume_equal(&o->soft_volume, &o->volume_factor));
1300 /* Notifications will be sent by pa_source_set_volume(). */
1303 /* Ok, so the origin source uses volume sharing, and flat volume is
1304 * disabled. The volume will have to be updated as follows:
1307 * o->reference_ratio := 0 dB
1308 * o->real_ratio stays unchanged
1309 * (streams whose origin source uses volume sharing should
1310 * always have real_ratio of 0 dB)
1311 * o->soft_volume stays unchanged
1312 * (streams whose origin source uses volume sharing should
1313 * always have volume_factor as soft_volume, so no change
1314 * should be needed) */
1316 old_volume = o->volume;
1317 pa_cvolume_reset(&o->volume, o->volume.channels);
1318 pa_cvolume_reset(&o->reference_ratio, o->reference_ratio.channels);
1319 pa_assert(pa_cvolume_is_norm(&o->real_ratio));
1320 pa_assert(pa_cvolume_equal(&o->soft_volume, &o->volume_factor));
1322 /* Notify others about the changed source output volume. */
1323 if (!pa_cvolume_equal(&o->volume, &old_volume)) {
1324 if (o->volume_changed)
1325 o->volume_changed(o);
1327 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1331 /* Additionally, the origin source volume needs updating:
1333 * o->destination_source->reference_volume := root_source->reference_volume
1334 * o->destination_source->real_volume := root_source->real_volume
1335 * o->destination_source->soft_volume stays unchanged
1336 * (sources that use volume sharing should always have
1337 * soft_volume of 0 dB) */
1339 old_volume = o->destination_source->reference_volume;
1341 o->destination_source->reference_volume = root_source->reference_volume;
1342 pa_cvolume_remap(&o->destination_source->reference_volume, &root_source->channel_map, &o->destination_source->channel_map);
1344 o->destination_source->real_volume = root_source->real_volume;
1345 pa_cvolume_remap(&o->destination_source->real_volume, &root_source->channel_map, &o->destination_source->channel_map);
1347 pa_assert(pa_cvolume_is_norm(&o->destination_source->soft_volume));
1349 /* Notify others about the changed source volume. If you wonder whether
1350 * o->destination_source->set_volume() should be called somewhere, that's not
1351 * the case, because sources that use volume sharing shouldn't have any
1352 * internal volume that set_volume() would update. If you wonder
1353 * whether the thread_info variables should be synced, yes, they
1354 * should, and it's done by the PA_SOURCE_MESSAGE_FINISH_MOVE message
1356 if (!pa_cvolume_equal(&o->destination_source->reference_volume, &old_volume))
1357 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, o->destination_source->index);
1359 /* Recursively update origin source outputs. */
1360 PA_IDXSET_FOREACH(destination_source_output, o->destination_source->outputs, idx)
1361 update_volume_due_to_moving(destination_source_output, dest);
1364 old_volume = o->volume;
1366 if (pa_source_flat_volume_enabled(o->source)) {
1367 /* Ok, so this is a regular stream, and flat volume is enabled. The
1368 * volume will have to be updated as follows:
1370 * o->volume := o->reference_ratio * o->source->reference_volume
1371 * o->reference_ratio stays unchanged
1372 * o->real_ratio := o->volume / o->source->real_volume
1373 * (handled later by pa_source_set_volume)
1374 * o->soft_volume := o->real_ratio * o->volume_factor
1375 * (handled later by pa_source_set_volume) */
1377 o->volume = o->source->reference_volume;
1378 pa_cvolume_remap(&o->volume, &o->source->channel_map, &o->channel_map);
1379 pa_sw_cvolume_multiply(&o->volume, &o->volume, &o->reference_ratio);
1382 /* Ok, so this is a regular stream, and flat volume is disabled.
1383 * The volume will have to be updated as follows:
1385 * o->volume := o->reference_ratio
1386 * o->reference_ratio stays unchanged
1387 * o->real_ratio := o->reference_ratio
1388 * o->soft_volume := o->real_ratio * o->volume_factor */
1390 o->volume = o->reference_ratio;
1391 o->real_ratio = o->reference_ratio;
1392 pa_sw_cvolume_multiply(&o->soft_volume, &o->real_ratio, &o->volume_factor);
1395 /* Notify others about the changed source output volume. */
1396 if (!pa_cvolume_equal(&o->volume, &old_volume)) {
1397 /* XXX: In case o->source has flat volume enabled, then real_ratio
1398 * and soft_volume are not updated yet. Let's hope that the
1399 * callback implementation doesn't care about those variables... */
1400 if (o->volume_changed)
1401 o->volume_changed(o);
1403 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1407 /* If o->source == dest, then recursion has finished, and we can finally call
1408 * pa_source_set_volume(), which will do the rest of the updates. */
1409 if ((o->source == dest) && pa_source_flat_volume_enabled(o->source))
1410 pa_source_set_volume(o->source, NULL, false, o->save_volume);
1413 /* Called from main context */
1414 int pa_source_output_finish_move(pa_source_output *o, pa_source *dest, bool save) {
1415 pa_source_output_assert_ref(o);
1416 pa_assert_ctl_context();
1417 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1418 pa_assert(!o->source);
1419 pa_source_assert_ref(dest);
1421 if (!pa_source_output_may_move_to(o, dest))
1422 return -PA_ERR_NOTSUPPORTED;
1424 if (pa_source_output_is_passthrough(o) && !pa_source_check_format(dest, o->format)) {
1425 pa_proplist *p = pa_proplist_new();
1426 pa_log_debug("New source doesn't support stream format, sending format-changed and killing");
1427 /* Tell the client what device we want to be on if it is going to
1429 pa_proplist_sets(p, "device", dest->name);
1430 pa_source_output_send_event(o, PA_STREAM_EVENT_FORMAT_LOST, p);
1431 pa_proplist_free(p);
1432 return -PA_ERR_NOTSUPPORTED;
1435 if (!(o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) &&
1436 !pa_sample_spec_equal(&o->sample_spec, &dest->sample_spec)) {
1437 /* try to change dest sink rate if possible without glitches.
1438 module-suspend-on-idle resumes destination source with
1439 SOURCE_OUTPUT_MOVE_FINISH hook */
1441 pa_log_info("Trying to change sample rate");
1442 if (pa_source_update_rate(dest, o->sample_spec.rate, pa_source_output_is_passthrough(o)) >= 0)
1443 pa_log_info("Rate changed to %u Hz", dest->sample_spec.rate);
1450 o->save_source = save;
1451 pa_idxset_put(o->source->outputs, pa_source_output_ref(o), NULL);
1453 pa_cvolume_remap(&o->volume_factor_source, &o->channel_map, &o->source->channel_map);
1455 if (pa_source_output_get_state(o) == PA_SOURCE_OUTPUT_CORKED)
1456 o->source->n_corked++;
1458 pa_source_output_update_rate(o);
1460 pa_source_update_status(dest);
1462 update_volume_due_to_moving(o, dest);
1464 if (pa_source_output_is_passthrough(o))
1465 pa_source_enter_passthrough(o->source);
1467 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_ADD_OUTPUT, o, 0, NULL) == 0);
1469 pa_log_debug("Successfully moved source output %i to %s.", o->index, dest->name);
1471 /* Notify everyone */
1472 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH], o);
1473 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1478 /* Called from main context */
1479 void pa_source_output_fail_move(pa_source_output *o) {
1481 pa_source_output_assert_ref(o);
1482 pa_assert_ctl_context();
1483 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1484 pa_assert(!o->source);
1486 /* Check if someone wants this source output? */
1487 if (pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL], o) == PA_HOOK_STOP)
1493 pa_source_output_kill(o);
1496 /* Called from main context */
1497 int pa_source_output_move_to(pa_source_output *o, pa_source *dest, bool save) {
1500 pa_source_output_assert_ref(o);
1501 pa_assert_ctl_context();
1502 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1503 pa_assert(o->source);
1504 pa_source_assert_ref(dest);
1506 if (dest == o->source)
1509 if (!pa_source_output_may_move_to(o, dest))
1510 return -PA_ERR_NOTSUPPORTED;
1512 pa_source_output_ref(o);
1514 if ((r = pa_source_output_start_move(o)) < 0) {
1515 pa_source_output_unref(o);
1519 if ((r = pa_source_output_finish_move(o, dest, save)) < 0) {
1520 pa_source_output_fail_move(o);
1521 pa_source_output_unref(o);
1525 pa_source_output_unref(o);
1530 /* Called from IO thread context */
1531 void pa_source_output_set_state_within_thread(pa_source_output *o, pa_source_output_state_t state) {
1532 pa_source_output_assert_ref(o);
1533 pa_source_output_assert_io_context(o);
1535 if (state == o->thread_info.state)
1538 if (o->state_change)
1539 o->state_change(o, state);
1541 o->thread_info.state = state;
1544 /* Called from IO thread context, except when it is not */
1545 int pa_source_output_process_msg(pa_msgobject *mo, int code, void *userdata, int64_t offset, pa_memchunk* chunk) {
1546 pa_source_output *o = PA_SOURCE_OUTPUT(mo);
1547 pa_source_output_assert_ref(o);
1551 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY: {
1552 pa_usec_t *r = userdata;
1554 r[0] += pa_bytes_to_usec(pa_memblockq_get_length(o->thread_info.delay_memblockq), &o->source->sample_spec);
1555 r[1] += pa_source_get_latency_within_thread(o->source);
1560 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE:
1562 o->thread_info.sample_spec.rate = PA_PTR_TO_UINT(userdata);
1563 pa_resampler_set_output_rate(o->thread_info.resampler, PA_PTR_TO_UINT(userdata));
1566 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE:
1568 pa_source_output_set_state_within_thread(o, PA_PTR_TO_UINT(userdata));
1572 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY: {
1573 pa_usec_t *usec = userdata;
1575 *usec = pa_source_output_set_requested_latency_within_thread(o, *usec);
1580 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY: {
1581 pa_usec_t *r = userdata;
1583 *r = o->thread_info.requested_source_latency;
1587 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_VOLUME:
1588 if (!pa_cvolume_equal(&o->thread_info.soft_volume, &o->soft_volume)) {
1589 o->thread_info.soft_volume = o->soft_volume;
1593 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE:
1594 if (o->thread_info.muted != o->muted) {
1595 o->thread_info.muted = o->muted;
1600 return -PA_ERR_NOTIMPLEMENTED;
1603 /* Called from main context */
1604 void pa_source_output_send_event(pa_source_output *o, const char *event, pa_proplist *data) {
1605 pa_proplist *pl = NULL;
1606 pa_source_output_send_event_hook_data hook_data;
1608 pa_source_output_assert_ref(o);
1609 pa_assert_ctl_context();
1616 data = pl = pa_proplist_new();
1618 hook_data.source_output = o;
1619 hook_data.data = data;
1620 hook_data.event = event;
1622 if (pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_SEND_EVENT], &hook_data) < 0)
1625 o->send_event(o, event, data);
1629 pa_proplist_free(pl);
1632 /* Called from main context */
1633 /* Updates the source output's resampler with whatever the current source
1634 * requires -- useful when the underlying source's rate might have changed */
1635 int pa_source_output_update_rate(pa_source_output *o) {
1636 pa_resampler *new_resampler;
1637 char *memblockq_name;
1639 pa_source_output_assert_ref(o);
1640 pa_assert_ctl_context();
1642 if (o->thread_info.resampler &&
1643 pa_sample_spec_equal(pa_resampler_input_sample_spec(o->thread_info.resampler), &o->source->sample_spec) &&
1644 pa_channel_map_equal(pa_resampler_input_channel_map(o->thread_info.resampler), &o->source->channel_map))
1646 new_resampler = o->thread_info.resampler;
1648 else if (!pa_source_output_is_passthrough(o) &&
1649 ((o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ||
1650 !pa_sample_spec_equal(&o->sample_spec, &o->source->sample_spec) ||
1651 !pa_channel_map_equal(&o->channel_map, &o->source->channel_map))) {
1653 new_resampler = pa_resampler_new(o->core->mempool,
1654 &o->source->sample_spec, &o->source->channel_map,
1655 &o->sample_spec, &o->channel_map,
1656 o->requested_resample_method,
1657 ((o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
1658 ((o->flags & PA_SOURCE_OUTPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
1659 (o->core->disable_remixing || (o->flags & PA_SOURCE_OUTPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0) |
1660 (o->core->disable_lfe_remixing ? PA_RESAMPLER_NO_LFE : 0));
1662 if (!new_resampler) {
1663 pa_log_warn("Unsupported resampling operation.");
1664 return -PA_ERR_NOTSUPPORTED;
1667 new_resampler = NULL;
1669 if (new_resampler == o->thread_info.resampler)
1672 if (o->thread_info.resampler)
1673 pa_resampler_free(o->thread_info.resampler);
1675 o->thread_info.resampler = new_resampler;
1677 pa_memblockq_free(o->thread_info.delay_memblockq);
1679 memblockq_name = pa_sprintf_malloc("source output delay_memblockq [%u]", o->index);
1680 o->thread_info.delay_memblockq = pa_memblockq_new(
1683 MEMBLOCKQ_MAXLENGTH,
1685 &o->source->sample_spec,
1689 &o->source->silence);
1690 pa_xfree(memblockq_name);
1692 o->actual_resample_method = new_resampler ? pa_resampler_get_method(new_resampler) : PA_RESAMPLER_INVALID;
1694 pa_log_debug("Updated resampler for source output %d", o->index);