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
;
975 pa_source_output_set_volume_direct(o
, volume
);
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
) {
1062 pa_source_output_assert_ref(o
);
1063 pa_assert_ctl_context();
1064 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1066 old_mute
= o
->muted
;
1068 if (mute
== old_mute
) {
1069 o
->save_muted
|= save
;
1074 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
));
1076 o
->save_muted
= save
;
1078 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE
, NULL
, 0, NULL
) == 0);
1080 /* The mute status changed, let's tell people so */
1081 if (o
->mute_changed
)
1084 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1087 /* Called from main thread */
1088 void pa_source_output_update_proplist(pa_source_output
*o
, pa_update_mode_t mode
, pa_proplist
*p
) {
1089 pa_source_output_assert_ref(o
);
1090 pa_assert_ctl_context();
1093 pa_proplist_update(o
->proplist
, mode
, p
);
1095 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
1096 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
1097 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1101 /* Called from main context */
1102 void pa_source_output_cork(pa_source_output
*o
, bool b
) {
1103 pa_source_output_assert_ref(o
);
1104 pa_assert_ctl_context();
1105 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1107 source_output_set_state(o
, b
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
);
1110 /* Called from main context */
1111 int pa_source_output_set_rate(pa_source_output
*o
, uint32_t rate
) {
1112 pa_source_output_assert_ref(o
);
1113 pa_assert_ctl_context();
1114 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1115 pa_return_val_if_fail(o
->thread_info
.resampler
, -PA_ERR_BADSTATE
);
1117 if (o
->sample_spec
.rate
== rate
)
1120 o
->sample_spec
.rate
= rate
;
1122 pa_asyncmsgq_post(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
, PA_UINT_TO_PTR(rate
), 0, NULL
, NULL
);
1124 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1128 /* Called from main context */
1129 void pa_source_output_set_name(pa_source_output
*o
, const char *name
) {
1131 pa_assert_ctl_context();
1132 pa_source_output_assert_ref(o
);
1134 if (!name
&& !pa_proplist_contains(o
->proplist
, PA_PROP_MEDIA_NAME
))
1137 old
= pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
);
1139 if (old
&& name
&& pa_streq(old
, name
))
1143 pa_proplist_sets(o
->proplist
, PA_PROP_MEDIA_NAME
, name
);
1145 pa_proplist_unset(o
->proplist
, PA_PROP_MEDIA_NAME
);
1147 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
1148 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
1149 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1153 /* Called from main context */
1154 pa_resample_method_t
pa_source_output_get_resample_method(pa_source_output
*o
) {
1155 pa_source_output_assert_ref(o
);
1156 pa_assert_ctl_context();
1158 return o
->actual_resample_method
;
1161 /* Called from main context */
1162 bool pa_source_output_may_move(pa_source_output
*o
) {
1163 pa_source_output_assert_ref(o
);
1164 pa_assert_ctl_context();
1165 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1167 if (o
->flags
& PA_SOURCE_OUTPUT_DONT_MOVE
)
1170 if (o
->direct_on_input
)
1176 static bool find_filter_source_output(pa_source_output
*target
, pa_source
*s
) {
1178 while (s
&& s
->output_from_master
) {
1179 if (s
->output_from_master
== target
)
1181 s
= s
->output_from_master
->source
;
1182 pa_assert(i
++ < 100);
1187 /* Called from main context */
1188 bool pa_source_output_may_move_to(pa_source_output
*o
, pa_source
*dest
) {
1189 pa_source_output_assert_ref(o
);
1190 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1191 pa_source_assert_ref(dest
);
1193 if (dest
== o
->source
)
1196 if (!pa_source_output_may_move(o
))
1199 /* Make sure we're not creating a filter source cycle */
1200 if (find_filter_source_output(o
, dest
)) {
1201 pa_log_debug("Can't connect output to %s, as that would create a cycle.", dest
->name
);
1205 if (pa_idxset_size(dest
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
1206 pa_log_warn("Failed to move source output: too many outputs per source.");
1211 if (!o
->may_move_to(o
, dest
))
1217 /* Called from main context */
1218 int pa_source_output_start_move(pa_source_output
*o
) {
1222 pa_source_output_assert_ref(o
);
1223 pa_assert_ctl_context();
1224 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1225 pa_assert(o
->source
);
1227 if (!pa_source_output_may_move(o
))
1228 return -PA_ERR_NOTSUPPORTED
;
1230 if ((r
= pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START
], o
)) < 0)
1235 pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
);
1237 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
)
1238 pa_assert_se(origin
->n_corked
-- >= 1);
1240 if (pa_source_output_is_passthrough(o
))
1241 pa_source_leave_passthrough(o
->source
);
1243 if (pa_source_flat_volume_enabled(o
->source
))
1244 /* We might need to update the source's volume if we are in flat
1246 pa_source_set_volume(o
->source
, NULL
, false, false);
1248 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
1250 pa_source_update_status(o
->source
);
1253 pa_source_output_unref(o
);
1258 /* Called from main context. If it has an origin source that uses volume sharing,
1259 * then also the origin source and all streams connected to it need to update
1260 * their volume - this function does all that by using recursion. */
1261 static void update_volume_due_to_moving(pa_source_output
*o
, pa_source
*dest
) {
1262 pa_cvolume new_volume
;
1266 pa_assert(o
->source
); /* The destination source should already be set. */
1268 if (o
->destination_source
&& (o
->destination_source
->flags
& PA_SOURCE_SHARE_VOLUME_WITH_MASTER
)) {
1269 pa_source
*root_source
;
1270 pa_source_output
*destination_source_output
;
1273 root_source
= pa_source_get_master(o
->source
);
1275 if (PA_UNLIKELY(!root_source
))
1278 if (pa_source_flat_volume_enabled(o
->source
)) {
1279 /* Ok, so the origin source uses volume sharing, and flat volume is
1280 * enabled. The volume will have to be updated as follows:
1282 * o->volume := o->source->real_volume
1283 * (handled later by pa_source_set_volume)
1284 * o->reference_ratio := o->volume / o->source->reference_volume
1285 * (handled later by pa_source_set_volume)
1286 * o->real_ratio stays unchanged
1287 * (streams whose origin source uses volume sharing should
1288 * always have real_ratio of 0 dB)
1289 * o->soft_volume stays unchanged
1290 * (streams whose origin source uses volume sharing should
1291 * always have volume_factor as soft_volume, so no change
1292 * should be needed) */
1294 pa_assert(pa_cvolume_is_norm(&o
->real_ratio
));
1295 pa_assert(pa_cvolume_equal(&o
->soft_volume
, &o
->volume_factor
));
1297 /* Notifications will be sent by pa_source_set_volume(). */
1300 /* Ok, so the origin source uses volume sharing, and flat volume is
1301 * disabled. The volume will have to be updated as follows:
1304 * o->reference_ratio := 0 dB
1305 * o->real_ratio stays unchanged
1306 * (streams whose origin source uses volume sharing should
1307 * always have real_ratio of 0 dB)
1308 * o->soft_volume stays unchanged
1309 * (streams whose origin source uses volume sharing should
1310 * always have volume_factor as soft_volume, so no change
1311 * should be needed) */
1313 pa_cvolume_reset(&new_volume
, o
->volume
.channels
);
1314 pa_source_output_set_volume_direct(o
, &new_volume
);
1315 pa_cvolume_reset(&o
->reference_ratio
, o
->reference_ratio
.channels
);
1316 pa_assert(pa_cvolume_is_norm(&o
->real_ratio
));
1317 pa_assert(pa_cvolume_equal(&o
->soft_volume
, &o
->volume_factor
));
1320 /* Additionally, the origin source volume needs updating:
1322 * o->destination_source->reference_volume := root_source->reference_volume
1323 * o->destination_source->real_volume := root_source->real_volume
1324 * o->destination_source->soft_volume stays unchanged
1325 * (sources that use volume sharing should always have
1326 * soft_volume of 0 dB) */
1328 new_volume
= root_source
->reference_volume
;
1329 pa_cvolume_remap(&new_volume
, &root_source
->channel_map
, &o
->destination_source
->channel_map
);
1330 pa_source_set_reference_volume_direct(o
->destination_source
, &new_volume
);
1332 o
->destination_source
->real_volume
= root_source
->real_volume
;
1333 pa_cvolume_remap(&o
->destination_source
->real_volume
, &root_source
->channel_map
, &o
->destination_source
->channel_map
);
1335 pa_assert(pa_cvolume_is_norm(&o
->destination_source
->soft_volume
));
1337 /* If you wonder whether o->destination_source->set_volume() should be
1338 * called somewhere, that's not the case, because sources that use
1339 * volume sharing shouldn't have any internal volume that set_volume()
1340 * would update. If you wonder whether the thread_info variables should
1341 * be synced, yes, they should, and it's done by the
1342 * PA_SOURCE_MESSAGE_FINISH_MOVE message handler. */
1344 /* Recursively update origin source outputs. */
1345 PA_IDXSET_FOREACH(destination_source_output
, o
->destination_source
->outputs
, idx
)
1346 update_volume_due_to_moving(destination_source_output
, dest
);
1349 if (pa_source_flat_volume_enabled(o
->source
)) {
1350 /* Ok, so this is a regular stream, and flat volume is enabled. The
1351 * volume will have to be updated as follows:
1353 * o->volume := o->reference_ratio * o->source->reference_volume
1354 * o->reference_ratio stays unchanged
1355 * o->real_ratio := o->volume / o->source->real_volume
1356 * (handled later by pa_source_set_volume)
1357 * o->soft_volume := o->real_ratio * o->volume_factor
1358 * (handled later by pa_source_set_volume) */
1360 new_volume
= o
->source
->reference_volume
;
1361 pa_cvolume_remap(&new_volume
, &o
->source
->channel_map
, &o
->channel_map
);
1362 pa_sw_cvolume_multiply(&new_volume
, &new_volume
, &o
->reference_ratio
);
1363 pa_source_output_set_volume_direct(o
, &new_volume
);
1366 /* Ok, so this is a regular stream, and flat volume is disabled.
1367 * The volume will have to be updated as follows:
1369 * o->volume := o->reference_ratio
1370 * o->reference_ratio stays unchanged
1371 * o->real_ratio := o->reference_ratio
1372 * o->soft_volume := o->real_ratio * o->volume_factor */
1374 pa_source_output_set_volume_direct(o
, &o
->reference_ratio
);
1375 o
->real_ratio
= o
->reference_ratio
;
1376 pa_sw_cvolume_multiply(&o
->soft_volume
, &o
->real_ratio
, &o
->volume_factor
);
1380 /* If o->source == dest, then recursion has finished, and we can finally call
1381 * pa_source_set_volume(), which will do the rest of the updates. */
1382 if ((o
->source
== dest
) && pa_source_flat_volume_enabled(o
->source
))
1383 pa_source_set_volume(o
->source
, NULL
, false, o
->save_volume
);
1386 /* Called from main context */
1387 int pa_source_output_finish_move(pa_source_output
*o
, pa_source
*dest
, bool save
) {
1388 pa_source_output_assert_ref(o
);
1389 pa_assert_ctl_context();
1390 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1391 pa_assert(!o
->source
);
1392 pa_source_assert_ref(dest
);
1394 if (!pa_source_output_may_move_to(o
, dest
))
1395 return -PA_ERR_NOTSUPPORTED
;
1397 if (pa_source_output_is_passthrough(o
) && !pa_source_check_format(dest
, o
->format
)) {
1398 pa_proplist
*p
= pa_proplist_new();
1399 pa_log_debug("New source doesn't support stream format, sending format-changed and killing");
1400 /* Tell the client what device we want to be on if it is going to
1402 pa_proplist_sets(p
, "device", dest
->name
);
1403 pa_source_output_send_event(o
, PA_STREAM_EVENT_FORMAT_LOST
, p
);
1404 pa_proplist_free(p
);
1405 return -PA_ERR_NOTSUPPORTED
;
1408 if (!(o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) &&
1409 !pa_sample_spec_equal(&o
->sample_spec
, &dest
->sample_spec
)) {
1410 /* try to change dest sink rate if possible without glitches.
1411 module-suspend-on-idle resumes destination source with
1412 SOURCE_OUTPUT_MOVE_FINISH hook */
1414 pa_log_info("Trying to change sample rate");
1415 if (pa_source_update_rate(dest
, o
->sample_spec
.rate
, pa_source_output_is_passthrough(o
)) >= 0)
1416 pa_log_info("Rate changed to %u Hz", dest
->sample_spec
.rate
);
1423 o
->save_source
= save
;
1424 pa_idxset_put(o
->source
->outputs
, pa_source_output_ref(o
), NULL
);
1426 pa_cvolume_remap(&o
->volume_factor_source
, &o
->channel_map
, &o
->source
->channel_map
);
1428 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
)
1429 o
->source
->n_corked
++;
1431 pa_source_output_update_rate(o
);
1433 pa_source_update_status(dest
);
1435 update_volume_due_to_moving(o
, dest
);
1437 if (pa_source_output_is_passthrough(o
))
1438 pa_source_enter_passthrough(o
->source
);
1440 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
1442 pa_log_debug("Successfully moved source output %i to %s.", o
->index
, dest
->name
);
1444 /* Notify everyone */
1445 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH
], o
);
1446 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1451 /* Called from main context */
1452 void pa_source_output_fail_move(pa_source_output
*o
) {
1454 pa_source_output_assert_ref(o
);
1455 pa_assert_ctl_context();
1456 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1457 pa_assert(!o
->source
);
1459 /* Check if someone wants this source output? */
1460 if (pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL
], o
) == PA_HOOK_STOP
)
1466 pa_source_output_kill(o
);
1469 /* Called from main context */
1470 int pa_source_output_move_to(pa_source_output
*o
, pa_source
*dest
, bool save
) {
1473 pa_source_output_assert_ref(o
);
1474 pa_assert_ctl_context();
1475 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1476 pa_assert(o
->source
);
1477 pa_source_assert_ref(dest
);
1479 if (dest
== o
->source
)
1482 if (!pa_source_output_may_move_to(o
, dest
))
1483 return -PA_ERR_NOTSUPPORTED
;
1485 pa_source_output_ref(o
);
1487 if ((r
= pa_source_output_start_move(o
)) < 0) {
1488 pa_source_output_unref(o
);
1492 if ((r
= pa_source_output_finish_move(o
, dest
, save
)) < 0) {
1493 pa_source_output_fail_move(o
);
1494 pa_source_output_unref(o
);
1498 pa_source_output_unref(o
);
1503 /* Called from IO thread context */
1504 void pa_source_output_set_state_within_thread(pa_source_output
*o
, pa_source_output_state_t state
) {
1505 pa_source_output_assert_ref(o
);
1506 pa_source_output_assert_io_context(o
);
1508 if (state
== o
->thread_info
.state
)
1511 if (o
->state_change
)
1512 o
->state_change(o
, state
);
1514 o
->thread_info
.state
= state
;
1517 /* Called from IO thread context, except when it is not */
1518 int pa_source_output_process_msg(pa_msgobject
*mo
, int code
, void *userdata
, int64_t offset
, pa_memchunk
* chunk
) {
1519 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
1520 pa_source_output_assert_ref(o
);
1524 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
: {
1525 pa_usec_t
*r
= userdata
;
1527 r
[0] += pa_bytes_to_usec(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
), &o
->source
->sample_spec
);
1528 r
[1] += pa_source_get_latency_within_thread(o
->source
);
1533 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
:
1535 o
->thread_info
.sample_spec
.rate
= PA_PTR_TO_UINT(userdata
);
1536 pa_resampler_set_output_rate(o
->thread_info
.resampler
, PA_PTR_TO_UINT(userdata
));
1539 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE
:
1541 pa_source_output_set_state_within_thread(o
, PA_PTR_TO_UINT(userdata
));
1545 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
: {
1546 pa_usec_t
*usec
= userdata
;
1548 *usec
= pa_source_output_set_requested_latency_within_thread(o
, *usec
);
1553 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
: {
1554 pa_usec_t
*r
= userdata
;
1556 *r
= o
->thread_info
.requested_source_latency
;
1560 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_VOLUME
:
1561 if (!pa_cvolume_equal(&o
->thread_info
.soft_volume
, &o
->soft_volume
)) {
1562 o
->thread_info
.soft_volume
= o
->soft_volume
;
1566 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE
:
1567 if (o
->thread_info
.muted
!= o
->muted
) {
1568 o
->thread_info
.muted
= o
->muted
;
1573 return -PA_ERR_NOTIMPLEMENTED
;
1576 /* Called from main context */
1577 void pa_source_output_send_event(pa_source_output
*o
, const char *event
, pa_proplist
*data
) {
1578 pa_proplist
*pl
= NULL
;
1579 pa_source_output_send_event_hook_data hook_data
;
1581 pa_source_output_assert_ref(o
);
1582 pa_assert_ctl_context();
1589 data
= pl
= pa_proplist_new();
1591 hook_data
.source_output
= o
;
1592 hook_data
.data
= data
;
1593 hook_data
.event
= event
;
1595 if (pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_SEND_EVENT
], &hook_data
) < 0)
1598 o
->send_event(o
, event
, data
);
1602 pa_proplist_free(pl
);
1605 /* Called from main context */
1606 /* Updates the source output's resampler with whatever the current source
1607 * requires -- useful when the underlying source's rate might have changed */
1608 int pa_source_output_update_rate(pa_source_output
*o
) {
1609 pa_resampler
*new_resampler
;
1610 char *memblockq_name
;
1612 pa_source_output_assert_ref(o
);
1613 pa_assert_ctl_context();
1615 if (o
->thread_info
.resampler
&&
1616 pa_sample_spec_equal(pa_resampler_input_sample_spec(o
->thread_info
.resampler
), &o
->source
->sample_spec
) &&
1617 pa_channel_map_equal(pa_resampler_input_channel_map(o
->thread_info
.resampler
), &o
->source
->channel_map
))
1619 new_resampler
= o
->thread_info
.resampler
;
1621 else if (!pa_source_output_is_passthrough(o
) &&
1622 ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
1623 !pa_sample_spec_equal(&o
->sample_spec
, &o
->source
->sample_spec
) ||
1624 !pa_channel_map_equal(&o
->channel_map
, &o
->source
->channel_map
))) {
1626 new_resampler
= pa_resampler_new(o
->core
->mempool
,
1627 &o
->source
->sample_spec
, &o
->source
->channel_map
,
1628 &o
->sample_spec
, &o
->channel_map
,
1629 o
->requested_resample_method
,
1630 ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
1631 ((o
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
1632 (o
->core
->disable_remixing
|| (o
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0) |
1633 (o
->core
->disable_lfe_remixing
? PA_RESAMPLER_NO_LFE
: 0));
1635 if (!new_resampler
) {
1636 pa_log_warn("Unsupported resampling operation.");
1637 return -PA_ERR_NOTSUPPORTED
;
1640 new_resampler
= NULL
;
1642 if (new_resampler
== o
->thread_info
.resampler
)
1645 if (o
->thread_info
.resampler
)
1646 pa_resampler_free(o
->thread_info
.resampler
);
1648 o
->thread_info
.resampler
= new_resampler
;
1650 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
1652 memblockq_name
= pa_sprintf_malloc("source output delay_memblockq [%u]", o
->index
);
1653 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
1656 MEMBLOCKQ_MAXLENGTH
,
1658 &o
->source
->sample_spec
,
1662 &o
->source
->silence
);
1663 pa_xfree(memblockq_name
);
1665 o
->actual_resample_method
= new_resampler
? pa_resampler_get_method(new_resampler
) : PA_RESAMPLER_INVALID
;
1667 pa_log_debug("Updated resampler for source output %d", o
->index
);
1672 /* Called from the main thread. */
1673 void pa_source_output_set_volume_direct(pa_source_output
*o
, const pa_cvolume
*volume
) {
1674 pa_cvolume old_volume
;
1675 char old_volume_str
[PA_CVOLUME_SNPRINT_VERBOSE_MAX
];
1676 char new_volume_str
[PA_CVOLUME_SNPRINT_VERBOSE_MAX
];
1681 old_volume
= o
->volume
;
1683 if (pa_cvolume_equal(volume
, &old_volume
))
1686 o
->volume
= *volume
;
1687 pa_log_debug("The volume of source output %u changed from %s to %s.", o
->index
,
1688 pa_cvolume_snprint_verbose(old_volume_str
, sizeof(old_volume_str
), &old_volume
, &o
->channel_map
, true),
1689 pa_cvolume_snprint_verbose(new_volume_str
, sizeof(new_volume_str
), volume
, &o
->channel_map
, true));
1691 if (o
->volume_changed
)
1692 o
->volume_changed(o
);
1694 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);