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, see <http://www.gnu.org/licenses/>.
28 #include <pulse/utf8.h>
29 #include <pulse/xmalloc.h>
30 #include <pulse/util.h>
31 #include <pulse/internal.h>
33 #include <pulsecore/core-format.h>
34 #include <pulsecore/mix.h>
35 #include <pulsecore/stream-util.h>
36 #include <pulsecore/core-subscribe.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/namereg.h>
39 #include <pulsecore/core-util.h>
41 #include "source-output.h"
43 #define MEMBLOCKQ_MAXLENGTH (32*1024*1024)
45 PA_DEFINE_PUBLIC_CLASS(pa_source_output, pa_msgobject);
47 static void source_output_free(pa_object* mo);
48 static void set_real_ratio(pa_source_output *o, const pa_cvolume *v);
50 pa_source_output_new_data* pa_source_output_new_data_init(pa_source_output_new_data *data) {
54 data->resample_method = PA_RESAMPLER_INVALID;
55 data->proplist = pa_proplist_new();
56 data->volume_writable = true;
61 void pa_source_output_new_data_set_sample_spec(pa_source_output_new_data *data, const pa_sample_spec *spec) {
64 if ((data->sample_spec_is_set = !!spec))
65 data->sample_spec = *spec;
68 void pa_source_output_new_data_set_channel_map(pa_source_output_new_data *data, const pa_channel_map *map) {
71 if ((data->channel_map_is_set = !!map))
72 data->channel_map = *map;
75 bool pa_source_output_new_data_is_passthrough(pa_source_output_new_data *data) {
78 if (PA_LIKELY(data->format) && PA_UNLIKELY(!pa_format_info_is_pcm(data->format)))
81 if (PA_UNLIKELY(data->flags & PA_SOURCE_OUTPUT_PASSTHROUGH))
87 void pa_source_output_new_data_set_volume(pa_source_output_new_data *data, const pa_cvolume *volume) {
89 pa_assert(data->volume_writable);
91 if ((data->volume_is_set = !!volume))
92 data->volume = *volume;
95 void pa_source_output_new_data_apply_volume_factor(pa_source_output_new_data *data, const pa_cvolume *volume_factor) {
97 pa_assert(volume_factor);
99 if (data->volume_factor_is_set)
100 pa_sw_cvolume_multiply(&data->volume_factor, &data->volume_factor, volume_factor);
102 data->volume_factor_is_set = true;
103 data->volume_factor = *volume_factor;
107 void pa_source_output_new_data_apply_volume_factor_source(pa_source_output_new_data *data, const pa_cvolume *volume_factor) {
109 pa_assert(volume_factor);
111 if (data->volume_factor_source_is_set)
112 pa_sw_cvolume_multiply(&data->volume_factor_source, &data->volume_factor_source, volume_factor);
114 data->volume_factor_source_is_set = true;
115 data->volume_factor_source = *volume_factor;
119 void pa_source_output_new_data_set_muted(pa_source_output_new_data *data, bool mute) {
122 data->muted_is_set = true;
126 bool pa_source_output_new_data_set_source(pa_source_output_new_data *data, pa_source *s, bool save) {
128 pa_idxset *formats = NULL;
133 if (!data->req_formats) {
134 /* We're not working with the extended API */
136 data->save_source = save;
138 /* Extended API: let's see if this source supports the formats the client would like */
139 formats = pa_source_check_formats(s, data->req_formats);
141 if (formats && !pa_idxset_isempty(formats)) {
142 /* Source supports at least one of the requested formats */
144 data->save_source = save;
145 if (data->nego_formats)
146 pa_idxset_free(data->nego_formats, (pa_free_cb_t) pa_format_info_free);
147 data->nego_formats = formats;
149 /* Source doesn't support any of the formats requested by the client */
151 pa_idxset_free(formats, (pa_free_cb_t) pa_format_info_free);
159 bool pa_source_output_new_data_set_formats(pa_source_output_new_data *data, pa_idxset *formats) {
163 if (data->req_formats)
164 pa_idxset_free(data->req_formats, (pa_free_cb_t) pa_format_info_free);
166 data->req_formats = formats;
169 /* Trigger format negotiation */
170 return pa_source_output_new_data_set_source(data, data->source, data->save_source);
176 void pa_source_output_new_data_done(pa_source_output_new_data *data) {
179 if (data->req_formats)
180 pa_idxset_free(data->req_formats, (pa_free_cb_t) pa_format_info_free);
182 if (data->nego_formats)
183 pa_idxset_free(data->nego_formats, (pa_free_cb_t) pa_format_info_free);
186 pa_format_info_free(data->format);
188 pa_proplist_free(data->proplist);
191 /* Called from main context */
192 static void reset_callbacks(pa_source_output *o) {
196 o->process_rewind = NULL;
197 o->update_max_rewind = NULL;
198 o->update_source_requested_latency = NULL;
199 o->update_source_latency_range = NULL;
200 o->update_source_fixed_latency = NULL;
204 o->suspend_within_thread = NULL;
207 o->get_latency = NULL;
208 o->state_change = NULL;
209 o->may_move_to = NULL;
210 o->send_event = NULL;
211 o->volume_changed = NULL;
212 o->mute_changed = NULL;
215 /* Called from main context */
216 int pa_source_output_new(
217 pa_source_output**_o,
219 pa_source_output_new_data *data) {
222 pa_resampler *resampler = NULL;
223 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX], fmt[PA_FORMAT_INFO_SNPRINT_MAX];
224 pa_channel_map volume_map;
231 pa_assert_ctl_context();
234 pa_proplist_update(data->proplist, PA_UPDATE_MERGE, data->client->proplist);
236 if (data->destination_source && (data->destination_source->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER))
237 data->volume_writable = false;
239 if (!data->req_formats) {
240 /* From this point on, we want to work only with formats, and get back
241 * to using the sample spec and channel map after all decisions w.r.t.
242 * routing are complete. */
246 f = pa_format_info_from_sample_spec2(&data->sample_spec, data->channel_map_is_set ? &data->channel_map : NULL,
247 !(data->flags & PA_SOURCE_OUTPUT_FIX_FORMAT),
248 !(data->flags & PA_SOURCE_OUTPUT_FIX_RATE),
249 !(data->flags & PA_SOURCE_OUTPUT_FIX_CHANNELS));
251 return -PA_ERR_INVALID;
253 formats = pa_idxset_new(NULL, NULL);
254 pa_idxset_put(formats, f, NULL);
255 pa_source_output_new_data_set_formats(data, formats);
258 if ((r = pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], data)) < 0)
261 pa_return_val_if_fail(!data->driver || pa_utf8_valid(data->driver), -PA_ERR_INVALID);
266 if (data->direct_on_input) {
267 source = data->direct_on_input->sink->monitor_source;
268 pa_return_val_if_fail(source, -PA_ERR_INVALID);
270 source = pa_namereg_get(core, NULL, PA_NAMEREG_SOURCE);
271 pa_return_val_if_fail(source, -PA_ERR_NOENTITY);
274 pa_source_output_new_data_set_source(data, source, false);
277 /* If something didn't pick a format for us, pick the top-most format since
278 * we assume this is sorted in priority order */
279 if (!data->format && data->nego_formats && !pa_idxset_isempty(data->nego_formats))
280 data->format = pa_format_info_copy(pa_idxset_first(data->nego_formats, NULL));
282 if (PA_LIKELY(data->format)) {
283 pa_log_debug("Negotiated format: %s", pa_format_info_snprint(fmt, sizeof(fmt), data->format));
285 pa_format_info *format;
288 pa_log_info("Source does not support any requested format:");
289 PA_IDXSET_FOREACH(format, data->req_formats, idx)
290 pa_log_info(" -- %s", pa_format_info_snprint(fmt, sizeof(fmt), format));
292 return -PA_ERR_NOTSUPPORTED;
295 pa_return_val_if_fail(PA_SOURCE_IS_LINKED(pa_source_get_state(data->source)), -PA_ERR_BADSTATE);
296 pa_return_val_if_fail(!data->direct_on_input || data->direct_on_input->sink == data->source->monitor_of, -PA_ERR_INVALID);
298 /* Routing is done. We have a source and a format. */
300 if (data->volume_is_set && pa_format_info_is_pcm(data->format)) {
301 /* If volume is set, we need to save the original data->channel_map,
302 * so that we can remap the volume from the original channel map to the
303 * final channel map of the stream in case data->channel_map gets
304 * modified in pa_format_info_to_sample_spec2(). */
305 r = pa_stream_get_volume_channel_map(&data->volume, data->channel_map_is_set ? &data->channel_map : NULL, data->format, &volume_map);
310 /* Now populate the sample spec and channel map according to the final
311 * format that we've negotiated */
312 r = pa_format_info_to_sample_spec2(data->format, &data->sample_spec, &data->channel_map, &data->source->sample_spec,
313 &data->source->channel_map);
317 /* Don't restore (or save) stream volume for passthrough streams and
318 * prevent attenuation/gain */
319 if (pa_source_output_new_data_is_passthrough(data)) {
320 data->volume_is_set = true;
321 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
322 data->volume_is_absolute = true;
323 data->save_volume = false;
326 if (!data->volume_is_set) {
327 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
328 data->volume_is_absolute = false;
329 data->save_volume = false;
332 if (!data->volume_writable)
333 data->save_volume = false;
335 if (data->volume_is_set)
336 /* The original volume channel map may be different than the final
337 * stream channel map, so remapping may be needed. */
338 pa_cvolume_remap(&data->volume, &volume_map, &data->channel_map);
340 if (!data->volume_factor_is_set)
341 pa_cvolume_reset(&data->volume_factor, data->sample_spec.channels);
343 pa_return_val_if_fail(pa_cvolume_compatible(&data->volume_factor, &data->sample_spec), -PA_ERR_INVALID);
345 if (!data->volume_factor_source_is_set)
346 pa_cvolume_reset(&data->volume_factor_source, data->source->sample_spec.channels);
348 pa_return_val_if_fail(pa_cvolume_compatible(&data->volume_factor_source, &data->source->sample_spec), -PA_ERR_INVALID);
350 if (!data->muted_is_set)
353 if (!(data->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) &&
354 !pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec)) {
355 /* try to change source rate. This is done before the FIXATE hook since
356 module-suspend-on-idle can resume a source */
358 pa_log_info("Trying to change sample rate");
359 if (pa_source_update_rate(data->source, data->sample_spec.rate, pa_source_output_new_data_is_passthrough(data)) >= 0)
360 pa_log_info("Rate changed to %u Hz", data->source->sample_spec.rate);
363 if (pa_source_output_new_data_is_passthrough(data) &&
364 !pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec)) {
365 /* rate update failed, or other parts of sample spec didn't match */
367 pa_log_debug("Could not update source sample spec to match passthrough stream");
368 return -PA_ERR_NOTSUPPORTED;
371 if (data->resample_method == PA_RESAMPLER_INVALID)
372 data->resample_method = core->resample_method;
374 pa_return_val_if_fail(data->resample_method < PA_RESAMPLER_MAX, -PA_ERR_INVALID);
376 if ((r = pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE], data)) < 0)
379 if ((data->flags & PA_SOURCE_OUTPUT_NO_CREATE_ON_SUSPEND) &&
380 pa_source_get_state(data->source) == PA_SOURCE_SUSPENDED) {
381 pa_log("Failed to create source output: source is suspended.");
382 return -PA_ERR_BADSTATE;
385 if (pa_idxset_size(data->source->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
386 pa_log("Failed to create source output: too many outputs per source.");
387 return -PA_ERR_TOOLARGE;
390 if ((data->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ||
391 !pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec) ||
392 !pa_channel_map_equal(&data->channel_map, &data->source->channel_map)) {
394 if (!pa_source_output_new_data_is_passthrough(data)) /* no resampler for passthrough content */
395 if (!(resampler = pa_resampler_new(
397 &data->source->sample_spec, &data->source->channel_map,
398 &data->sample_spec, &data->channel_map,
399 core->lfe_crossover_freq,
400 data->resample_method,
401 ((data->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
402 ((data->flags & PA_SOURCE_OUTPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
403 (core->disable_remixing || (data->flags & PA_SOURCE_OUTPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0) |
404 (core->remixing_use_all_sink_channels ? 0 : PA_RESAMPLER_NO_FILL_SINK) |
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)
528 if (o->state == PA_SOURCE_OUTPUT_CORKED && state == PA_SOURCE_OUTPUT_RUNNING && pa_source_used_by(o->source) == 0 &&
529 !pa_sample_spec_equal(&o->sample_spec, &o->source->sample_spec)) {
530 /* We were uncorked and the source was not playing anything -- let's try
531 * to update the sample rate to avoid resampling */
532 pa_source_update_rate(o->source, o->sample_spec.rate, pa_source_output_is_passthrough(o));
535 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);
537 /* If the source is not valid, pa_source_output_set_state_within_thread() must be called directly */
538 pa_source_output_set_state_within_thread(o, state);
540 update_n_corked(o, state);
543 if (state != PA_SOURCE_OUTPUT_UNLINKED) {
544 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED], o);
546 if (PA_SOURCE_OUTPUT_IS_LINKED(state))
547 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
551 pa_source_update_status(o->source);
554 /* Called from main context */
555 void pa_source_output_unlink(pa_source_output*o) {
558 pa_source_output_assert_ref(o);
559 pa_assert_ctl_context();
561 /* See pa_sink_unlink() for a couple of comments how this function
564 pa_source_output_ref(o);
566 linked = PA_SOURCE_OUTPUT_IS_LINKED(o->state);
569 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK], o);
571 if (o->direct_on_input)
572 pa_idxset_remove_by_data(o->direct_on_input->direct_outputs, o, NULL);
574 pa_idxset_remove_by_data(o->core->source_outputs, o, NULL);
577 if (pa_idxset_remove_by_data(o->source->outputs, o, NULL))
578 pa_source_output_unref(o);
581 pa_idxset_remove_by_data(o->client->source_outputs, o, NULL);
583 update_n_corked(o, PA_SOURCE_OUTPUT_UNLINKED);
584 o->state = PA_SOURCE_OUTPUT_UNLINKED;
586 if (linked && o->source) {
587 if (pa_source_output_is_passthrough(o))
588 pa_source_leave_passthrough(o->source);
590 /* We might need to update the source's volume if we are in flat volume mode. */
591 if (pa_source_flat_volume_enabled(o->source))
592 pa_source_set_volume(o->source, NULL, false, false);
594 if (o->source->asyncmsgq)
595 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL) == 0);
601 if (PA_SOURCE_IS_LINKED(pa_source_get_state(o->source)))
602 pa_source_update_status(o->source);
608 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_REMOVE, o->index);
609 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST], o);
612 pa_core_maybe_vacuum(o->core);
614 pa_source_output_unref(o);
617 /* Called from main context */
618 static void source_output_free(pa_object* mo) {
619 pa_source_output *o = PA_SOURCE_OUTPUT(mo);
622 pa_assert_ctl_context();
623 pa_assert(pa_source_output_refcnt(o) == 0);
624 pa_assert(!PA_SOURCE_OUTPUT_IS_LINKED(o->state));
626 pa_log_info("Freeing output %u \"%s\"", o->index,
627 o->proplist ? pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_MEDIA_NAME)) : "");
629 if (o->thread_info.delay_memblockq)
630 pa_memblockq_free(o->thread_info.delay_memblockq);
632 if (o->thread_info.resampler)
633 pa_resampler_free(o->thread_info.resampler);
636 pa_format_info_free(o->format);
639 pa_proplist_free(o->proplist);
645 /* Called from main context */
646 void pa_source_output_put(pa_source_output *o) {
647 pa_source_output_state_t state;
649 pa_source_output_assert_ref(o);
650 pa_assert_ctl_context();
652 pa_assert(o->state == PA_SOURCE_OUTPUT_INIT);
654 /* The following fields must be initialized properly */
658 state = o->flags & PA_SOURCE_OUTPUT_START_CORKED ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING;
660 update_n_corked(o, state);
663 /* We might need to update the source's volume if we are in flat volume mode. */
664 if (pa_source_flat_volume_enabled(o->source))
665 pa_source_set_volume(o->source, NULL, false, o->save_volume);
667 if (o->destination_source && (o->destination_source->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER)) {
668 pa_assert(pa_cvolume_is_norm(&o->volume));
669 pa_assert(pa_cvolume_is_norm(&o->reference_ratio));
672 set_real_ratio(o, &o->volume);
675 if (pa_source_output_is_passthrough(o))
676 pa_source_enter_passthrough(o->source);
678 o->thread_info.soft_volume = o->soft_volume;
679 o->thread_info.muted = o->muted;
681 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_ADD_OUTPUT, o, 0, NULL) == 0);
683 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, o->index);
684 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PUT], o);
686 pa_source_update_status(o->source);
689 /* Called from main context */
690 void pa_source_output_kill(pa_source_output*o) {
691 pa_source_output_assert_ref(o);
692 pa_assert_ctl_context();
693 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
698 /* Called from main context */
699 pa_usec_t pa_source_output_get_latency(pa_source_output *o, pa_usec_t *source_latency) {
700 pa_usec_t r[2] = { 0, 0 };
702 pa_source_output_assert_ref(o);
703 pa_assert_ctl_context();
704 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
706 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY, r, 0, NULL) == 0);
709 r[0] += o->get_latency(o);
712 *source_latency = r[1];
717 /* Called from thread context */
718 void pa_source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
719 bool need_volume_factor_source;
722 size_t limit, mbs = 0;
724 pa_source_output_assert_ref(o);
725 pa_source_output_assert_io_context(o);
726 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
728 pa_assert(pa_frame_aligned(chunk->length, &o->source->sample_spec));
730 if (!o->push || o->thread_info.state == PA_SOURCE_OUTPUT_CORKED)
733 pa_assert(o->thread_info.state == PA_SOURCE_OUTPUT_RUNNING);
735 if (pa_memblockq_push(o->thread_info.delay_memblockq, chunk) < 0) {
736 pa_log_debug("Delay queue overflow!");
737 pa_memblockq_seek(o->thread_info.delay_memblockq, (int64_t) chunk->length, PA_SEEK_RELATIVE, true);
740 limit = o->process_rewind ? 0 : o->source->thread_info.max_rewind;
742 volume_is_norm = pa_cvolume_is_norm(&o->thread_info.soft_volume) && !o->thread_info.muted;
743 need_volume_factor_source = !pa_cvolume_is_norm(&o->volume_factor_source);
745 if (limit > 0 && o->source->monitor_of) {
749 /* Hmm, check the latency for knowing how much of the buffered
750 * data is actually still unplayed and might hence still
751 * change. This is suboptimal. Ideally we'd have a call like
752 * pa_sink_get_changeable_size() or so that tells us how much
753 * of the queued data is actually still changeable. Hence
756 latency = pa_sink_get_latency_within_thread(o->source->monitor_of, false);
758 n = pa_usec_to_bytes(latency, &o->source->sample_spec);
764 /* Implement the delay queue */
765 while ((length = pa_memblockq_get_length(o->thread_info.delay_memblockq)) > limit) {
767 bool nvfs = need_volume_factor_source;
771 pa_assert_se(pa_memblockq_peek(o->thread_info.delay_memblockq, &qchunk) >= 0);
773 if (qchunk.length > length)
774 qchunk.length = length;
776 pa_assert(qchunk.length > 0);
778 /* It might be necessary to adjust the volume here */
779 if (!volume_is_norm) {
780 pa_memchunk_make_writable(&qchunk, 0);
782 if (o->thread_info.muted) {
783 pa_silence_memchunk(&qchunk, &o->source->sample_spec);
786 } else if (!o->thread_info.resampler && nvfs) {
789 /* If we don't need a resampler we can merge the
790 * post and the pre volume adjustment into one */
792 pa_sw_cvolume_multiply(&v, &o->thread_info.soft_volume, &o->volume_factor_source);
793 pa_volume_memchunk(&qchunk, &o->source->sample_spec, &v);
797 pa_volume_memchunk(&qchunk, &o->source->sample_spec, &o->thread_info.soft_volume);
801 pa_memchunk_make_writable(&qchunk, 0);
802 pa_volume_memchunk(&qchunk, &o->source->sample_spec, &o->volume_factor_source);
805 if (!o->thread_info.resampler)
811 mbs = pa_resampler_max_block_size(o->thread_info.resampler);
813 if (qchunk.length > mbs)
816 pa_resampler_run(o->thread_info.resampler, &qchunk, &rchunk);
818 if (rchunk.length > 0)
822 pa_memblock_unref(rchunk.memblock);
825 pa_memblock_unref(qchunk.memblock);
826 pa_memblockq_drop(o->thread_info.delay_memblockq, qchunk.length);
830 /* Called from thread context */
831 void pa_source_output_process_rewind(pa_source_output *o, size_t nbytes /* in source sample spec */) {
833 pa_source_output_assert_ref(o);
834 pa_source_output_assert_io_context(o);
835 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
836 pa_assert(pa_frame_aligned(nbytes, &o->source->sample_spec));
841 if (o->process_rewind) {
842 pa_assert(pa_memblockq_get_length(o->thread_info.delay_memblockq) == 0);
844 if (o->thread_info.resampler)
845 nbytes = pa_resampler_result(o->thread_info.resampler, nbytes);
847 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) nbytes);
850 o->process_rewind(o, nbytes);
852 if (o->thread_info.resampler)
853 pa_resampler_rewind(o->thread_info.resampler, nbytes);
856 pa_memblockq_rewind(o->thread_info.delay_memblockq, nbytes);
859 /* Called from thread context */
860 size_t pa_source_output_get_max_rewind(pa_source_output *o) {
861 pa_source_output_assert_ref(o);
862 pa_source_output_assert_io_context(o);
864 return o->thread_info.resampler ? pa_resampler_request(o->thread_info.resampler, o->source->thread_info.max_rewind) : o->source->thread_info.max_rewind;
867 /* Called from thread context */
868 void pa_source_output_update_max_rewind(pa_source_output *o, size_t nbytes /* in the source's sample spec */) {
869 pa_source_output_assert_ref(o);
870 pa_source_output_assert_io_context(o);
871 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
872 pa_assert(pa_frame_aligned(nbytes, &o->source->sample_spec));
874 if (o->update_max_rewind)
875 o->update_max_rewind(o, o->thread_info.resampler ? pa_resampler_result(o->thread_info.resampler, nbytes) : nbytes);
878 /* Called from thread context */
879 pa_usec_t pa_source_output_set_requested_latency_within_thread(pa_source_output *o, pa_usec_t usec) {
880 pa_source_output_assert_ref(o);
881 pa_source_output_assert_io_context(o);
883 if (!(o->source->flags & PA_SOURCE_DYNAMIC_LATENCY))
884 usec = o->source->thread_info.fixed_latency;
886 if (usec != (pa_usec_t) -1)
887 usec = PA_CLAMP(usec, o->source->thread_info.min_latency, o->source->thread_info.max_latency);
889 o->thread_info.requested_source_latency = usec;
890 pa_source_invalidate_requested_latency(o->source, true);
895 /* Called from main context */
896 pa_usec_t pa_source_output_set_requested_latency(pa_source_output *o, pa_usec_t usec) {
897 pa_source_output_assert_ref(o);
898 pa_assert_ctl_context();
900 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state) && o->source) {
901 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
905 /* If this source output is not realized yet or is being moved, we
906 * have to touch the thread info data directly */
909 if (!(o->source->flags & PA_SOURCE_DYNAMIC_LATENCY))
910 usec = pa_source_get_fixed_latency(o->source);
912 if (usec != (pa_usec_t) -1) {
913 pa_usec_t min_latency, max_latency;
914 pa_source_get_latency_range(o->source, &min_latency, &max_latency);
915 usec = PA_CLAMP(usec, min_latency, max_latency);
919 o->thread_info.requested_source_latency = usec;
924 /* Called from main context */
925 pa_usec_t pa_source_output_get_requested_latency(pa_source_output *o) {
926 pa_source_output_assert_ref(o);
927 pa_assert_ctl_context();
929 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state) && o->source) {
931 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
935 /* If this source output is not realized yet or is being moved, we
936 * have to touch the thread info data directly */
938 return o->thread_info.requested_source_latency;
941 /* Called from main context */
942 void pa_source_output_set_volume(pa_source_output *o, const pa_cvolume *volume, bool save, bool absolute) {
945 pa_source_output_assert_ref(o);
946 pa_assert_ctl_context();
947 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
949 pa_assert(pa_cvolume_valid(volume));
950 pa_assert(volume->channels == 1 || pa_cvolume_compatible(volume, &o->sample_spec));
951 pa_assert(o->volume_writable);
953 if (!absolute && pa_source_flat_volume_enabled(o->source)) {
954 v = o->source->reference_volume;
955 pa_cvolume_remap(&v, &o->source->channel_map, &o->channel_map);
957 if (pa_cvolume_compatible(volume, &o->sample_spec))
958 volume = pa_sw_cvolume_multiply(&v, &v, volume);
960 volume = pa_sw_cvolume_multiply_scalar(&v, &v, pa_cvolume_max(volume));
962 if (!pa_cvolume_compatible(volume, &o->sample_spec)) {
964 volume = pa_cvolume_scale(&v, pa_cvolume_max(volume));
968 if (pa_cvolume_equal(volume, &o->volume)) {
969 o->save_volume = o->save_volume || save;
973 pa_source_output_set_volume_direct(o, volume);
974 o->save_volume = save;
976 if (pa_source_flat_volume_enabled(o->source)) {
977 /* We are in flat volume mode, so let's update all source input
978 * volumes and update the flat volume of the source */
980 pa_source_set_volume(o->source, NULL, true, save);
983 /* OK, we are in normal volume mode. The volume only affects
985 set_real_ratio(o, volume);
987 /* Copy the new soft_volume to the thread_info struct */
988 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_VOLUME, NULL, 0, NULL) == 0);
991 /* The volume changed, let's tell people so */
992 if (o->volume_changed)
993 o->volume_changed(o);
995 /* The virtual volume changed, let's tell people so */
996 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
999 /* Called from main context */
1000 static void set_real_ratio(pa_source_output *o, const pa_cvolume *v) {
1001 pa_source_output_assert_ref(o);
1002 pa_assert_ctl_context();
1003 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1004 pa_assert(!v || pa_cvolume_compatible(v, &o->sample_spec));
1006 /* This basically calculates:
1008 * o->real_ratio := v
1009 * o->soft_volume := o->real_ratio * o->volume_factor */
1014 pa_cvolume_reset(&o->real_ratio, o->sample_spec.channels);
1016 pa_sw_cvolume_multiply(&o->soft_volume, &o->real_ratio, &o->volume_factor);
1017 /* We don't copy the data to the thread_info data. That's left for someone else to do */
1020 /* Called from main or I/O context */
1021 bool pa_source_output_is_passthrough(pa_source_output *o) {
1022 pa_source_output_assert_ref(o);
1024 if (PA_UNLIKELY(!pa_format_info_is_pcm(o->format)))
1027 if (PA_UNLIKELY(o->flags & PA_SOURCE_OUTPUT_PASSTHROUGH))
1033 /* Called from main context */
1034 bool pa_source_output_is_volume_readable(pa_source_output *o) {
1035 pa_source_output_assert_ref(o);
1036 pa_assert_ctl_context();
1038 return !pa_source_output_is_passthrough(o);
1041 /* Called from main context */
1042 pa_cvolume *pa_source_output_get_volume(pa_source_output *o, pa_cvolume *volume, bool absolute) {
1043 pa_source_output_assert_ref(o);
1044 pa_assert_ctl_context();
1045 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1046 pa_assert(pa_source_output_is_volume_readable(o));
1048 if (absolute || !pa_source_flat_volume_enabled(o->source))
1049 *volume = o->volume;
1051 *volume = o->reference_ratio;
1056 /* Called from main context */
1057 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 old_mute = o->muted;
1066 if (mute == old_mute) {
1067 o->save_muted |= save;
1072 pa_log_debug("The mute of source output %u changed from %s to %s.", o->index, pa_yes_no(old_mute), pa_yes_no(mute));
1074 o->save_muted = save;
1076 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE, NULL, 0, NULL) == 0);
1078 /* The mute status changed, let's tell people so */
1079 if (o->mute_changed)
1082 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1083 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MUTE_CHANGED], o);
1086 void pa_source_output_set_property(pa_source_output *o, const char *key, const char *value) {
1087 char *old_value = NULL;
1088 const char *new_value;
1093 if (pa_proplist_contains(o->proplist, key)) {
1094 old_value = pa_xstrdup(pa_proplist_gets(o->proplist, key));
1095 if (value && old_value && pa_streq(value, old_value))
1099 old_value = pa_xstrdup("(data)");
1104 old_value = pa_xstrdup("(unset)");
1108 pa_proplist_sets(o->proplist, key, value);
1111 pa_proplist_unset(o->proplist, key);
1112 new_value = "(unset)";
1115 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state)) {
1116 pa_log_debug("Source output %u: proplist[%s]: %s -> %s", o->index, key, old_value, new_value);
1117 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED], o);
1118 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT | PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1122 pa_xfree(old_value);
1125 void pa_source_output_set_property_arbitrary(pa_source_output *o, const char *key, const uint8_t *value, size_t nbytes) {
1126 const uint8_t *old_value;
1128 const char *old_value_str;
1129 const char *new_value_str;
1134 if (pa_proplist_get(o->proplist, key, (const void **) &old_value, &old_nbytes) >= 0) {
1135 if (value && nbytes == old_nbytes && !memcmp(value, old_value, nbytes))
1138 old_value_str = "(data)";
1144 old_value_str = "(unset)";
1148 pa_proplist_set(o->proplist, key, value, nbytes);
1149 new_value_str = "(data)";
1151 pa_proplist_unset(o->proplist, key);
1152 new_value_str = "(unset)";
1155 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state)) {
1156 pa_log_debug("Source output %u: proplist[%s]: %s -> %s", o->index, key, old_value_str, new_value_str);
1157 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED], o);
1158 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT | PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1162 /* Called from main thread */
1163 void pa_source_output_update_proplist(pa_source_output *o, pa_update_mode_t mode, pa_proplist *p) {
1166 const uint8_t *value;
1169 pa_source_output_assert_ref(o);
1171 pa_assert_ctl_context();
1175 /* Delete everything that is not in p. */
1176 for (state = NULL; (key = pa_proplist_iterate(o->proplist, &state));) {
1177 if (!pa_proplist_contains(p, key))
1178 pa_source_output_set_property(o, key, NULL);
1182 case PA_UPDATE_REPLACE:
1183 for (state = NULL; (key = pa_proplist_iterate(p, &state));) {
1184 pa_proplist_get(p, key, (const void **) &value, &nbytes);
1185 pa_source_output_set_property_arbitrary(o, key, value, nbytes);
1189 case PA_UPDATE_MERGE:
1190 for (state = NULL; (key = pa_proplist_iterate(p, &state));) {
1191 if (pa_proplist_contains(o->proplist, key))
1194 pa_proplist_get(p, key, (const void **) &value, &nbytes);
1195 pa_source_output_set_property_arbitrary(o, key, value, nbytes);
1202 /* Called from main context */
1203 void pa_source_output_cork(pa_source_output *o, bool b) {
1204 pa_source_output_assert_ref(o);
1205 pa_assert_ctl_context();
1206 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1208 source_output_set_state(o, b ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING);
1211 /* Called from main context */
1212 int pa_source_output_set_rate(pa_source_output *o, uint32_t rate) {
1213 pa_source_output_assert_ref(o);
1214 pa_assert_ctl_context();
1215 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1216 pa_return_val_if_fail(o->thread_info.resampler, -PA_ERR_BADSTATE);
1218 if (o->sample_spec.rate == rate)
1221 o->sample_spec.rate = rate;
1223 pa_asyncmsgq_post(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE, PA_UINT_TO_PTR(rate), 0, NULL, NULL);
1225 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1229 /* Called from main context */
1230 pa_resample_method_t pa_source_output_get_resample_method(pa_source_output *o) {
1231 pa_source_output_assert_ref(o);
1232 pa_assert_ctl_context();
1234 return o->actual_resample_method;
1237 /* Called from main context */
1238 bool pa_source_output_may_move(pa_source_output *o) {
1239 pa_source_output_assert_ref(o);
1240 pa_assert_ctl_context();
1241 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1243 if (o->flags & PA_SOURCE_OUTPUT_DONT_MOVE)
1246 if (o->direct_on_input)
1252 static bool find_filter_source_output(pa_source_output *target, pa_source *s) {
1253 unsigned PA_UNUSED i = 0;
1254 while (s && s->output_from_master) {
1255 if (s->output_from_master == target)
1257 s = s->output_from_master->source;
1258 pa_assert(i++ < 100);
1263 static bool is_filter_source_moving(pa_source_output *o) {
1264 pa_source *source = o->source;
1269 while (source->output_from_master) {
1270 source = source->output_from_master->source;
1279 /* Called from main context */
1280 bool pa_source_output_may_move_to(pa_source_output *o, pa_source *dest) {
1281 pa_source_output_assert_ref(o);
1282 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1283 pa_source_assert_ref(dest);
1285 if (dest == o->source)
1288 if (dest->unlink_requested)
1291 if (!pa_source_output_may_move(o))
1294 /* Make sure we're not creating a filter source cycle */
1295 if (find_filter_source_output(o, dest)) {
1296 pa_log_debug("Can't connect output to %s, as that would create a cycle.", dest->name);
1300 /* If this source output is connected to a filter source that itself is
1301 * moving, then don't allow the move. Moving requires sending a message to
1302 * the IO thread of the old source, and if the old source is a filter
1303 * source that is moving, there's no IO thread associated to the old
1305 if (is_filter_source_moving(o)) {
1306 pa_log_debug("Can't move output from filter source %s, because the filter source itself is currently moving.",
1311 if (pa_idxset_size(dest->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
1312 pa_log_warn("Failed to move source output: too many outputs per source.");
1317 if (!o->may_move_to(o, dest))
1323 /* Called from main context */
1324 int pa_source_output_start_move(pa_source_output *o) {
1328 pa_source_output_assert_ref(o);
1329 pa_assert_ctl_context();
1330 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1331 pa_assert(o->source);
1333 if (!pa_source_output_may_move(o))
1334 return -PA_ERR_NOTSUPPORTED;
1336 if ((r = pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START], o)) < 0)
1339 pa_log_debug("Starting to move source output %u from '%s'", (unsigned) o->index, o->source->name);
1343 pa_idxset_remove_by_data(o->source->outputs, o, NULL);
1345 if (pa_source_output_get_state(o) == PA_SOURCE_OUTPUT_CORKED)
1346 pa_assert_se(origin->n_corked-- >= 1);
1348 if (pa_source_output_is_passthrough(o))
1349 pa_source_leave_passthrough(o->source);
1351 if (pa_source_flat_volume_enabled(o->source))
1352 /* We might need to update the source's volume if we are in flat
1354 pa_source_set_volume(o->source, NULL, false, false);
1356 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL) == 0);
1358 pa_source_update_status(o->source);
1360 pa_cvolume_remap(&o->volume_factor_source, &o->source->channel_map, &o->channel_map);
1364 pa_source_output_unref(o);
1369 /* Called from main context. If it has an origin source that uses volume sharing,
1370 * then also the origin source and all streams connected to it need to update
1371 * their volume - this function does all that by using recursion. */
1372 static void update_volume_due_to_moving(pa_source_output *o, pa_source *dest) {
1373 pa_cvolume new_volume;
1377 pa_assert(o->source); /* The destination source should already be set. */
1379 if (o->destination_source && (o->destination_source->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER)) {
1380 pa_source *root_source;
1381 pa_source_output *destination_source_output;
1384 root_source = pa_source_get_master(o->source);
1386 if (PA_UNLIKELY(!root_source))
1389 if (pa_source_flat_volume_enabled(o->source)) {
1390 /* Ok, so the origin source uses volume sharing, and flat volume is
1391 * enabled. The volume will have to be updated as follows:
1393 * o->volume := o->source->real_volume
1394 * (handled later by pa_source_set_volume)
1395 * o->reference_ratio := o->volume / o->source->reference_volume
1396 * (handled later by pa_source_set_volume)
1397 * o->real_ratio stays unchanged
1398 * (streams whose origin source uses volume sharing should
1399 * always have real_ratio of 0 dB)
1400 * o->soft_volume stays unchanged
1401 * (streams whose origin source uses volume sharing should
1402 * always have volume_factor as soft_volume, so no change
1403 * should be needed) */
1405 pa_assert(pa_cvolume_is_norm(&o->real_ratio));
1406 pa_assert(pa_cvolume_equal(&o->soft_volume, &o->volume_factor));
1408 /* Notifications will be sent by pa_source_set_volume(). */
1411 /* Ok, so the origin source uses volume sharing, and flat volume is
1412 * disabled. The volume will have to be updated as follows:
1415 * o->reference_ratio := 0 dB
1416 * o->real_ratio stays unchanged
1417 * (streams whose origin source uses volume sharing should
1418 * always have real_ratio of 0 dB)
1419 * o->soft_volume stays unchanged
1420 * (streams whose origin source uses volume sharing should
1421 * always have volume_factor as soft_volume, so no change
1422 * should be needed) */
1424 pa_cvolume_reset(&new_volume, o->volume.channels);
1425 pa_source_output_set_volume_direct(o, &new_volume);
1426 pa_source_output_set_reference_ratio(o, &new_volume);
1427 pa_assert(pa_cvolume_is_norm(&o->real_ratio));
1428 pa_assert(pa_cvolume_equal(&o->soft_volume, &o->volume_factor));
1431 /* Additionally, the origin source volume needs updating:
1433 * o->destination_source->reference_volume := root_source->reference_volume
1434 * o->destination_source->real_volume := root_source->real_volume
1435 * o->destination_source->soft_volume stays unchanged
1436 * (sources that use volume sharing should always have
1437 * soft_volume of 0 dB) */
1439 new_volume = root_source->reference_volume;
1440 pa_cvolume_remap(&new_volume, &root_source->channel_map, &o->destination_source->channel_map);
1441 pa_source_set_reference_volume_direct(o->destination_source, &new_volume);
1443 o->destination_source->real_volume = root_source->real_volume;
1444 pa_cvolume_remap(&o->destination_source->real_volume, &root_source->channel_map, &o->destination_source->channel_map);
1446 pa_assert(pa_cvolume_is_norm(&o->destination_source->soft_volume));
1448 /* If you wonder whether o->destination_source->set_volume() should be
1449 * called somewhere, that's not the case, because sources that use
1450 * volume sharing shouldn't have any internal volume that set_volume()
1451 * would update. If you wonder whether the thread_info variables should
1452 * be synced, yes, they should, and it's done by the
1453 * PA_SOURCE_MESSAGE_FINISH_MOVE message handler. */
1455 /* Recursively update origin source outputs. */
1456 PA_IDXSET_FOREACH(destination_source_output, o->destination_source->outputs, idx)
1457 update_volume_due_to_moving(destination_source_output, dest);
1460 if (pa_source_flat_volume_enabled(o->source)) {
1461 /* Ok, so this is a regular stream, and flat volume is enabled. The
1462 * volume will have to be updated as follows:
1464 * o->volume := o->reference_ratio * o->source->reference_volume
1465 * o->reference_ratio stays unchanged
1466 * o->real_ratio := o->volume / o->source->real_volume
1467 * (handled later by pa_source_set_volume)
1468 * o->soft_volume := o->real_ratio * o->volume_factor
1469 * (handled later by pa_source_set_volume) */
1471 new_volume = o->source->reference_volume;
1472 pa_cvolume_remap(&new_volume, &o->source->channel_map, &o->channel_map);
1473 pa_sw_cvolume_multiply(&new_volume, &new_volume, &o->reference_ratio);
1474 pa_source_output_set_volume_direct(o, &new_volume);
1477 /* Ok, so this is a regular stream, and flat volume is disabled.
1478 * The volume will have to be updated as follows:
1480 * o->volume := o->reference_ratio
1481 * o->reference_ratio stays unchanged
1482 * o->real_ratio := o->reference_ratio
1483 * o->soft_volume := o->real_ratio * o->volume_factor */
1485 pa_source_output_set_volume_direct(o, &o->reference_ratio);
1486 o->real_ratio = o->reference_ratio;
1487 pa_sw_cvolume_multiply(&o->soft_volume, &o->real_ratio, &o->volume_factor);
1491 /* If o->source == dest, then recursion has finished, and we can finally call
1492 * pa_source_set_volume(), which will do the rest of the updates. */
1493 if ((o->source == dest) && pa_source_flat_volume_enabled(o->source))
1494 pa_source_set_volume(o->source, NULL, false, o->save_volume);
1497 /* Called from main context */
1498 int pa_source_output_finish_move(pa_source_output *o, pa_source *dest, bool save) {
1499 pa_source_output_assert_ref(o);
1500 pa_assert_ctl_context();
1501 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1502 pa_assert(!o->source);
1503 pa_source_assert_ref(dest);
1505 if (!pa_source_output_may_move_to(o, dest))
1506 return -PA_ERR_NOTSUPPORTED;
1508 if (pa_source_output_is_passthrough(o) && !pa_source_check_format(dest, o->format)) {
1509 pa_proplist *p = pa_proplist_new();
1510 pa_log_debug("New source doesn't support stream format, sending format-changed and killing");
1511 /* Tell the client what device we want to be on if it is going to
1513 pa_proplist_sets(p, "device", dest->name);
1514 pa_source_output_send_event(o, PA_STREAM_EVENT_FORMAT_LOST, p);
1515 pa_proplist_free(p);
1516 return -PA_ERR_NOTSUPPORTED;
1519 if (!(o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) &&
1520 !pa_sample_spec_equal(&o->sample_spec, &dest->sample_spec)) {
1521 /* try to change dest source rate if possible without glitches.
1522 module-suspend-on-idle resumes destination source with
1523 SOURCE_OUTPUT_MOVE_FINISH hook */
1525 pa_log_info("Trying to change sample rate");
1526 if (pa_source_update_rate(dest, o->sample_spec.rate, pa_source_output_is_passthrough(o)) >= 0)
1527 pa_log_info("Rate changed to %u Hz", dest->sample_spec.rate);
1534 o->save_source = save;
1535 pa_idxset_put(o->source->outputs, pa_source_output_ref(o), NULL);
1537 pa_cvolume_remap(&o->volume_factor_source, &o->channel_map, &o->source->channel_map);
1539 if (pa_source_output_get_state(o) == PA_SOURCE_OUTPUT_CORKED)
1540 o->source->n_corked++;
1542 pa_source_output_update_rate(o);
1544 pa_source_update_status(dest);
1546 update_volume_due_to_moving(o, dest);
1548 if (pa_source_output_is_passthrough(o))
1549 pa_source_enter_passthrough(o->source);
1551 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_ADD_OUTPUT, o, 0, NULL) == 0);
1553 pa_log_debug("Successfully moved source output %i to %s.", o->index, dest->name);
1555 /* Notify everyone */
1556 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH], o);
1557 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1562 /* Called from main context */
1563 void pa_source_output_fail_move(pa_source_output *o) {
1565 pa_source_output_assert_ref(o);
1566 pa_assert_ctl_context();
1567 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1568 pa_assert(!o->source);
1570 /* Check if someone wants this source output? */
1571 if (pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL], o) == PA_HOOK_STOP)
1577 pa_source_output_kill(o);
1580 /* Called from main context */
1581 int pa_source_output_move_to(pa_source_output *o, pa_source *dest, bool save) {
1584 pa_source_output_assert_ref(o);
1585 pa_assert_ctl_context();
1586 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1587 pa_assert(o->source);
1588 pa_source_assert_ref(dest);
1590 if (dest == o->source)
1593 if (!pa_source_output_may_move_to(o, dest))
1594 return -PA_ERR_NOTSUPPORTED;
1596 pa_source_output_ref(o);
1598 if ((r = pa_source_output_start_move(o)) < 0) {
1599 pa_source_output_unref(o);
1603 if ((r = pa_source_output_finish_move(o, dest, save)) < 0) {
1604 pa_source_output_fail_move(o);
1605 pa_source_output_unref(o);
1609 pa_source_output_unref(o);
1614 /* Called from IO thread context except when cork() is called without a valid source. */
1615 void pa_source_output_set_state_within_thread(pa_source_output *o, pa_source_output_state_t state) {
1616 pa_source_output_assert_ref(o);
1618 if (state == o->thread_info.state)
1621 if (o->state_change)
1622 o->state_change(o, state);
1624 o->thread_info.state = state;
1627 /* Called from IO thread context, except when it is not */
1628 int pa_source_output_process_msg(pa_msgobject *mo, int code, void *userdata, int64_t offset, pa_memchunk* chunk) {
1629 pa_source_output *o = PA_SOURCE_OUTPUT(mo);
1630 pa_source_output_assert_ref(o);
1634 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY: {
1635 pa_usec_t *r = userdata;
1637 r[0] += pa_bytes_to_usec(pa_memblockq_get_length(o->thread_info.delay_memblockq), &o->source->sample_spec);
1638 r[1] += pa_source_get_latency_within_thread(o->source, false);
1643 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE:
1645 o->thread_info.sample_spec.rate = PA_PTR_TO_UINT(userdata);
1646 pa_resampler_set_output_rate(o->thread_info.resampler, PA_PTR_TO_UINT(userdata));
1649 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE:
1651 pa_source_output_set_state_within_thread(o, PA_PTR_TO_UINT(userdata));
1655 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY: {
1656 pa_usec_t *usec = userdata;
1658 *usec = pa_source_output_set_requested_latency_within_thread(o, *usec);
1663 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY: {
1664 pa_usec_t *r = userdata;
1666 *r = o->thread_info.requested_source_latency;
1670 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_VOLUME:
1671 if (!pa_cvolume_equal(&o->thread_info.soft_volume, &o->soft_volume)) {
1672 o->thread_info.soft_volume = o->soft_volume;
1676 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE:
1677 if (o->thread_info.muted != o->muted) {
1678 o->thread_info.muted = o->muted;
1683 return -PA_ERR_NOTIMPLEMENTED;
1686 /* Called from main context */
1687 void pa_source_output_send_event(pa_source_output *o, const char *event, pa_proplist *data) {
1688 pa_proplist *pl = NULL;
1689 pa_source_output_send_event_hook_data hook_data;
1691 pa_source_output_assert_ref(o);
1692 pa_assert_ctl_context();
1699 data = pl = pa_proplist_new();
1701 hook_data.source_output = o;
1702 hook_data.data = data;
1703 hook_data.event = event;
1705 if (pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_SEND_EVENT], &hook_data) < 0)
1708 o->send_event(o, event, data);
1712 pa_proplist_free(pl);
1715 /* Called from main context */
1716 /* Updates the source output's resampler with whatever the current source
1717 * requires -- useful when the underlying source's rate might have changed */
1718 int pa_source_output_update_rate(pa_source_output *o) {
1719 pa_resampler *new_resampler;
1720 char *memblockq_name;
1722 pa_source_output_assert_ref(o);
1723 pa_assert_ctl_context();
1725 if (o->thread_info.resampler &&
1726 pa_sample_spec_equal(pa_resampler_input_sample_spec(o->thread_info.resampler), &o->source->sample_spec) &&
1727 pa_channel_map_equal(pa_resampler_input_channel_map(o->thread_info.resampler), &o->source->channel_map))
1729 new_resampler = o->thread_info.resampler;
1731 else if (!pa_source_output_is_passthrough(o) &&
1732 ((o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ||
1733 !pa_sample_spec_equal(&o->sample_spec, &o->source->sample_spec) ||
1734 !pa_channel_map_equal(&o->channel_map, &o->source->channel_map))) {
1736 new_resampler = pa_resampler_new(o->core->mempool,
1737 &o->source->sample_spec, &o->source->channel_map,
1738 &o->sample_spec, &o->channel_map,
1739 o->core->lfe_crossover_freq,
1740 o->requested_resample_method,
1741 ((o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
1742 ((o->flags & PA_SOURCE_OUTPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
1743 (o->core->disable_remixing || (o->flags & PA_SOURCE_OUTPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0) |
1744 (o->core->remixing_use_all_sink_channels ? 0 : PA_RESAMPLER_NO_FILL_SINK) |
1745 (o->core->disable_lfe_remixing ? PA_RESAMPLER_NO_LFE : 0));
1747 if (!new_resampler) {
1748 pa_log_warn("Unsupported resampling operation.");
1749 return -PA_ERR_NOTSUPPORTED;
1752 new_resampler = NULL;
1754 if (new_resampler == o->thread_info.resampler)
1757 if (o->thread_info.resampler)
1758 pa_resampler_free(o->thread_info.resampler);
1760 o->thread_info.resampler = new_resampler;
1762 pa_memblockq_free(o->thread_info.delay_memblockq);
1764 memblockq_name = pa_sprintf_malloc("source output delay_memblockq [%u]", o->index);
1765 o->thread_info.delay_memblockq = pa_memblockq_new(
1768 MEMBLOCKQ_MAXLENGTH,
1770 &o->source->sample_spec,
1774 &o->source->silence);
1775 pa_xfree(memblockq_name);
1777 o->actual_resample_method = new_resampler ? pa_resampler_get_method(new_resampler) : PA_RESAMPLER_INVALID;
1779 pa_log_debug("Updated resampler for source output %d", o->index);
1784 /* Called from the IO thread. */
1785 void pa_source_output_attach(pa_source_output *o) {
1787 pa_assert(!o->thread_info.attached);
1789 o->thread_info.attached = true;
1795 /* Called from the IO thread. */
1796 void pa_source_output_detach(pa_source_output *o) {
1799 if (!o->thread_info.attached)
1802 o->thread_info.attached = false;
1808 /* Called from the main thread. */
1809 void pa_source_output_set_volume_direct(pa_source_output *o, const pa_cvolume *volume) {
1810 pa_cvolume old_volume;
1811 char old_volume_str[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
1812 char new_volume_str[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
1817 old_volume = o->volume;
1819 if (pa_cvolume_equal(volume, &old_volume))
1822 o->volume = *volume;
1823 pa_log_debug("The volume of source output %u changed from %s to %s.", o->index,
1824 pa_cvolume_snprint_verbose(old_volume_str, sizeof(old_volume_str), &old_volume, &o->channel_map, true),
1825 pa_cvolume_snprint_verbose(new_volume_str, sizeof(new_volume_str), volume, &o->channel_map, true));
1827 if (o->volume_changed)
1828 o->volume_changed(o);
1830 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1831 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_VOLUME_CHANGED], o);
1834 /* Called from the main thread. */
1835 void pa_source_output_set_reference_ratio(pa_source_output *o, const pa_cvolume *ratio) {
1836 pa_cvolume old_ratio;
1837 char old_ratio_str[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
1838 char new_ratio_str[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
1843 old_ratio = o->reference_ratio;
1845 if (pa_cvolume_equal(ratio, &old_ratio))
1848 o->reference_ratio = *ratio;
1850 if (!PA_SOURCE_OUTPUT_IS_LINKED(o->state))
1853 pa_log_debug("Source output %u reference ratio changed from %s to %s.", o->index,
1854 pa_cvolume_snprint_verbose(old_ratio_str, sizeof(old_ratio_str), &old_ratio, &o->channel_map, true),
1855 pa_cvolume_snprint_verbose(new_ratio_str, sizeof(new_ratio_str), ratio, &o->channel_map, true));