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/sample-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 pa_bool_t
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
, pa_bool_t mute
) {
122 data
->muted_is_set
= TRUE
;
123 data
->muted
= !!mute
;
126 pa_bool_t
pa_source_output_new_data_set_source(pa_source_output_new_data
*data
, pa_source
*s
, pa_bool_t save
) {
127 pa_bool_t ret
= TRUE
;
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_free2_cb_t
) pa_format_info_free2
, NULL
);
147 data
->nego_formats
= formats
;
149 /* Source doesn't support any of the formats requested by the client */
151 pa_idxset_free(formats
, (pa_free2_cb_t
) pa_format_info_free2
, NULL
);
159 pa_bool_t
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(formats
, (pa_free2_cb_t
) pa_format_info_free2
, NULL
);
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_free2_cb_t
) pa_format_info_free2
, NULL
);
182 if (data
->nego_formats
)
183 pa_idxset_free(data
->nego_formats
, (pa_free2_cb_t
) pa_format_info_free2
, NULL
);
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
];
224 pa_channel_map original_cm
;
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. */
245 pa_idxset
*tmp
= pa_idxset_new(NULL
, NULL
);
246 pa_format_info
*f
= pa_format_info_from_sample_spec(&data
->sample_spec
,
247 data
->channel_map_is_set
? &data
->channel_map
: NULL
);
248 pa_idxset_put(tmp
, f
, NULL
);
249 pa_source_output_new_data_set_formats(data
, tmp
);
252 if ((r
= pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_NEW
], data
)) < 0)
255 pa_return_val_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
), -PA_ERR_INVALID
);
258 pa_source
*source
= pa_namereg_get(core
, NULL
, PA_NAMEREG_SOURCE
);
259 pa_return_val_if_fail(source
, -PA_ERR_NOENTITY
);
260 pa_source_output_new_data_set_source(data
, source
, FALSE
);
263 /* Routing's done, we have a source. Now let's fix the format and set up the
266 /* If something didn't pick a format for us, pick the top-most format since
267 * we assume this is sorted in priority order */
268 if (!data
->format
&& data
->nego_formats
&& !pa_idxset_isempty(data
->nego_formats
))
269 data
->format
= pa_format_info_copy(pa_idxset_first(data
->nego_formats
, NULL
));
271 pa_return_val_if_fail(data
->format
, -PA_ERR_NOTSUPPORTED
);
273 /* Now populate the sample spec and format according to the final
274 * format that we've negotiated */
275 pa_return_val_if_fail(pa_format_info_to_sample_spec(data
->format
, &ss
, &map
) == 0, -PA_ERR_INVALID
);
276 pa_source_output_new_data_set_sample_spec(data
, &ss
);
277 if (pa_format_info_is_pcm(data
->format
) && pa_channel_map_valid(&map
))
278 pa_source_output_new_data_set_channel_map(data
, &map
);
280 pa_return_val_if_fail(PA_SOURCE_IS_LINKED(pa_source_get_state(data
->source
)), -PA_ERR_BADSTATE
);
281 pa_return_val_if_fail(!data
->direct_on_input
|| data
->direct_on_input
->sink
== data
->source
->monitor_of
, -PA_ERR_INVALID
);
283 if (!data
->sample_spec_is_set
)
284 data
->sample_spec
= data
->source
->sample_spec
;
286 pa_return_val_if_fail(pa_sample_spec_valid(&data
->sample_spec
), -PA_ERR_INVALID
);
288 if (!data
->channel_map_is_set
) {
289 if (pa_channel_map_compatible(&data
->source
->channel_map
, &data
->sample_spec
))
290 data
->channel_map
= data
->source
->channel_map
;
292 pa_channel_map_init_extend(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
);
295 pa_return_val_if_fail(pa_channel_map_compatible(&data
->channel_map
, &data
->sample_spec
), -PA_ERR_INVALID
);
297 /* Don't restore (or save) stream volume for passthrough streams and
298 * prevent attenuation/gain */
299 if (pa_source_output_new_data_is_passthrough(data
)) {
300 data
->volume_is_set
= TRUE
;
301 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
302 data
->volume_is_absolute
= TRUE
;
303 data
->save_volume
= FALSE
;
306 if (!data
->volume_is_set
) {
307 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
308 data
->volume_is_absolute
= FALSE
;
309 data
->save_volume
= FALSE
;
312 pa_return_val_if_fail(pa_cvolume_compatible(&data
->volume
, &data
->sample_spec
), -PA_ERR_INVALID
);
314 if (!data
->volume_factor_is_set
)
315 pa_cvolume_reset(&data
->volume_factor
, data
->sample_spec
.channels
);
317 pa_return_val_if_fail(pa_cvolume_compatible(&data
->volume_factor
, &data
->sample_spec
), -PA_ERR_INVALID
);
319 if (!data
->volume_factor_source_is_set
)
320 pa_cvolume_reset(&data
->volume_factor_source
, data
->source
->sample_spec
.channels
);
322 pa_return_val_if_fail(pa_cvolume_compatible(&data
->volume_factor_source
, &data
->source
->sample_spec
), -PA_ERR_INVALID
);
324 if (!data
->muted_is_set
)
327 if (data
->flags
& PA_SOURCE_OUTPUT_FIX_FORMAT
)
328 data
->sample_spec
.format
= data
->source
->sample_spec
.format
;
330 if (data
->flags
& PA_SOURCE_OUTPUT_FIX_RATE
)
331 data
->sample_spec
.rate
= data
->source
->sample_spec
.rate
;
333 original_cm
= data
->channel_map
;
335 if (data
->flags
& PA_SOURCE_OUTPUT_FIX_CHANNELS
) {
336 data
->sample_spec
.channels
= data
->source
->sample_spec
.channels
;
337 data
->channel_map
= data
->source
->channel_map
;
340 pa_assert(pa_sample_spec_valid(&data
->sample_spec
));
341 pa_assert(pa_channel_map_valid(&data
->channel_map
));
343 /* Due to the fixing of the sample spec the volume might not match anymore */
344 pa_cvolume_remap(&data
->volume
, &original_cm
, &data
->channel_map
);
346 if (!(data
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) &&
347 !pa_sample_spec_equal(&data
->sample_spec
, &data
->source
->sample_spec
)){
348 /* try to change source rate. This is done before the FIXATE hook since
349 module-suspend-on-idle can resume a source */
351 pa_log_info("Trying to change sample rate");
352 if (pa_source_update_rate(data
->source
, data
->sample_spec
.rate
, pa_source_output_new_data_is_passthrough(data
)) == TRUE
)
353 pa_log_info("Rate changed to %u Hz",
354 data
->source
->sample_spec
.rate
);
356 pa_log_info("Resampling enabled to %u Hz", data
->source
->sample_spec
.rate
);
359 if (data
->resample_method
== PA_RESAMPLER_INVALID
)
360 data
->resample_method
= core
->resample_method
;
362 pa_return_val_if_fail(data
->resample_method
< PA_RESAMPLER_MAX
, -PA_ERR_INVALID
);
364 if ((r
= pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE
], data
)) < 0)
367 if ((data
->flags
& PA_SOURCE_OUTPUT_NO_CREATE_ON_SUSPEND
) &&
368 pa_source_get_state(data
->source
) == PA_SOURCE_SUSPENDED
) {
369 pa_log("Failed to create source output: source is suspended.");
370 return -PA_ERR_BADSTATE
;
373 if (pa_idxset_size(data
->source
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
374 pa_log("Failed to create source output: too many outputs per source.");
375 return -PA_ERR_TOOLARGE
;
378 if ((data
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
379 !pa_sample_spec_equal(&data
->sample_spec
, &data
->source
->sample_spec
) ||
380 !pa_channel_map_equal(&data
->channel_map
, &data
->source
->channel_map
)) {
382 if (!pa_source_output_new_data_is_passthrough(data
)) /* no resampler for passthrough content */
383 if (!(resampler
= pa_resampler_new(
385 &data
->source
->sample_spec
, &data
->source
->channel_map
,
386 &data
->sample_spec
, &data
->channel_map
,
387 data
->resample_method
,
388 ((data
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
389 ((data
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
390 (core
->disable_remixing
|| (data
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0) |
391 (core
->disable_lfe_remixing
? PA_RESAMPLER_NO_LFE
: 0)))) {
392 pa_log_warn("Unsupported resampling operation.");
393 return -PA_ERR_NOTSUPPORTED
;
397 o
= pa_msgobject_new(pa_source_output
);
398 o
->parent
.parent
.free
= source_output_free
;
399 o
->parent
.process_msg
= pa_source_output_process_msg
;
402 o
->state
= PA_SOURCE_OUTPUT_INIT
;
403 o
->flags
= data
->flags
;
404 o
->proplist
= pa_proplist_copy(data
->proplist
);
405 o
->driver
= pa_xstrdup(pa_path_get_filename(data
->driver
));
406 o
->module
= data
->module
;
407 o
->source
= data
->source
;
408 o
->destination_source
= data
->destination_source
;
409 o
->client
= data
->client
;
411 o
->requested_resample_method
= data
->resample_method
;
412 o
->actual_resample_method
= resampler
? pa_resampler_get_method(resampler
) : PA_RESAMPLER_INVALID
;
413 o
->sample_spec
= data
->sample_spec
;
414 o
->channel_map
= data
->channel_map
;
415 o
->format
= pa_format_info_copy(data
->format
);
417 if (!data
->volume_is_absolute
&& pa_source_flat_volume_enabled(o
->source
)) {
420 /* When the 'absolute' bool is not set then we'll treat the volume
421 * as relative to the source volume even in flat volume mode */
422 remapped
= data
->source
->reference_volume
;
423 pa_cvolume_remap(&remapped
, &data
->source
->channel_map
, &data
->channel_map
);
424 pa_sw_cvolume_multiply(&o
->volume
, &data
->volume
, &remapped
);
426 o
->volume
= data
->volume
;
428 o
->volume_factor
= data
->volume_factor
;
429 o
->volume_factor_source
= data
->volume_factor_source
;
430 o
->real_ratio
= o
->reference_ratio
= data
->volume
;
431 pa_cvolume_reset(&o
->soft_volume
, o
->sample_spec
.channels
);
432 pa_cvolume_reset(&o
->real_ratio
, o
->sample_spec
.channels
);
433 o
->volume_writable
= data
->volume_writable
;
434 o
->save_volume
= data
->save_volume
;
435 o
->save_source
= data
->save_source
;
436 o
->save_muted
= data
->save_muted
;
438 o
->muted
= data
->muted
;
440 o
->direct_on_input
= data
->direct_on_input
;
445 o
->thread_info
.state
= o
->state
;
446 o
->thread_info
.attached
= FALSE
;
447 o
->thread_info
.sample_spec
= o
->sample_spec
;
448 o
->thread_info
.resampler
= resampler
;
449 o
->thread_info
.soft_volume
= o
->soft_volume
;
450 o
->thread_info
.muted
= o
->muted
;
451 o
->thread_info
.requested_source_latency
= (pa_usec_t
) -1;
452 o
->thread_info
.direct_on_input
= o
->direct_on_input
;
454 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
455 "source output delay_memblockq",
459 &o
->source
->sample_spec
,
463 &o
->source
->silence
);
465 pa_assert_se(pa_idxset_put(core
->source_outputs
, o
, &o
->index
) == 0);
466 pa_assert_se(pa_idxset_put(o
->source
->outputs
, pa_source_output_ref(o
), NULL
) == 0);
469 pa_assert_se(pa_idxset_put(o
->client
->source_outputs
, o
, NULL
) >= 0);
471 if (o
->direct_on_input
)
472 pa_assert_se(pa_idxset_put(o
->direct_on_input
->direct_outputs
, o
, NULL
) == 0);
474 pt
= pa_proplist_to_string_sep(o
->proplist
, "\n ");
475 pa_log_info("Created output %u \"%s\" on %s with sample spec %s and channel map %s\n %s",
477 pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)),
479 pa_sample_spec_snprint(st
, sizeof(st
), &o
->sample_spec
),
480 pa_channel_map_snprint(cm
, sizeof(cm
), &o
->channel_map
),
484 /* Don't forget to call pa_source_output_put! */
490 /* Called from main context */
491 static void update_n_corked(pa_source_output
*o
, pa_source_output_state_t state
) {
493 pa_assert_ctl_context();
498 if (o
->state
== PA_SOURCE_OUTPUT_CORKED
&& state
!= PA_SOURCE_OUTPUT_CORKED
)
499 pa_assert_se(o
->source
->n_corked
-- >= 1);
500 else if (o
->state
!= PA_SOURCE_OUTPUT_CORKED
&& state
== PA_SOURCE_OUTPUT_CORKED
)
501 o
->source
->n_corked
++;
504 /* Called from main context */
505 static void source_output_set_state(pa_source_output
*o
, pa_source_output_state_t state
) {
508 pa_assert_ctl_context();
510 if (o
->state
== state
)
513 if (o
->state
== PA_SOURCE_OUTPUT_CORKED
&& state
== PA_SOURCE_OUTPUT_RUNNING
&& pa_source_used_by(o
->source
) == 0) {
514 /* We were uncorked and the source was not playing anything -- let's try
515 * to update the sample rate to avoid resampling */
516 pa_source_update_rate(o
->source
, o
->sample_spec
.rate
, pa_source_output_is_passthrough(o
));
519 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);
521 update_n_corked(o
, state
);
524 if (state
!= PA_SOURCE_OUTPUT_UNLINKED
) {
525 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED
], o
);
527 if (PA_SOURCE_OUTPUT_IS_LINKED(state
))
528 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
531 pa_source_update_status(o
->source
);
534 /* Called from main context */
535 void pa_source_output_unlink(pa_source_output
*o
) {
538 pa_assert_ctl_context();
540 /* See pa_sink_unlink() for a couple of comments how this function
543 pa_source_output_ref(o
);
545 linked
= PA_SOURCE_OUTPUT_IS_LINKED(o
->state
);
548 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK
], o
);
550 if (o
->direct_on_input
)
551 pa_idxset_remove_by_data(o
->direct_on_input
->direct_outputs
, o
, NULL
);
553 pa_idxset_remove_by_data(o
->core
->source_outputs
, o
, NULL
);
556 if (pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
))
557 pa_source_output_unref(o
);
560 pa_idxset_remove_by_data(o
->client
->source_outputs
, o
, NULL
);
562 update_n_corked(o
, PA_SOURCE_OUTPUT_UNLINKED
);
563 o
->state
= PA_SOURCE_OUTPUT_UNLINKED
;
565 if (linked
&& o
->source
) {
566 if (pa_source_output_is_passthrough(o
))
567 pa_source_leave_passthrough(o
->source
);
569 /* We might need to update the source's volume if we are in flat volume mode. */
570 if (pa_source_flat_volume_enabled(o
->source
))
571 pa_source_set_volume(o
->source
, NULL
, FALSE
, FALSE
);
573 if (o
->source
->asyncmsgq
)
574 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
580 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, o
->index
);
581 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST
], o
);
585 if (PA_SOURCE_IS_LINKED(pa_source_get_state(o
->source
)))
586 pa_source_update_status(o
->source
);
591 pa_core_maybe_vacuum(o
->core
);
593 pa_source_output_unref(o
);
596 /* Called from main context */
597 static void source_output_free(pa_object
* mo
) {
598 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
601 pa_assert_ctl_context();
602 pa_assert(pa_source_output_refcnt(o
) == 0);
604 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
605 pa_source_output_unlink(o
);
607 pa_log_info("Freeing output %u \"%s\"", o
->index
, pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)));
609 if (o
->thread_info
.delay_memblockq
)
610 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
612 if (o
->thread_info
.resampler
)
613 pa_resampler_free(o
->thread_info
.resampler
);
616 pa_format_info_free(o
->format
);
619 pa_proplist_free(o
->proplist
);
625 /* Called from main context */
626 void pa_source_output_put(pa_source_output
*o
) {
627 pa_source_output_state_t state
;
629 pa_source_output_assert_ref(o
);
630 pa_assert_ctl_context();
632 pa_assert(o
->state
== PA_SOURCE_OUTPUT_INIT
);
634 /* The following fields must be initialized properly */
638 state
= o
->flags
& PA_SOURCE_OUTPUT_START_CORKED
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
;
640 update_n_corked(o
, state
);
643 /* We might need to update the source's volume if we are in flat volume mode. */
644 if (pa_source_flat_volume_enabled(o
->source
))
645 pa_source_set_volume(o
->source
, NULL
, FALSE
, o
->save_volume
);
647 if (o
->destination_source
&& (o
->destination_source
->flags
& PA_SOURCE_SHARE_VOLUME_WITH_MASTER
)) {
648 pa_assert(pa_cvolume_is_norm(&o
->volume
));
649 pa_assert(pa_cvolume_is_norm(&o
->reference_ratio
));
652 set_real_ratio(o
, &o
->volume
);
655 if (pa_source_output_is_passthrough(o
))
656 pa_source_enter_passthrough(o
->source
);
658 o
->thread_info
.soft_volume
= o
->soft_volume
;
659 o
->thread_info
.muted
= o
->muted
;
661 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
663 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
, o
->index
);
664 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PUT
], o
);
666 pa_source_update_status(o
->source
);
669 /* Called from main context */
670 void pa_source_output_kill(pa_source_output
*o
) {
671 pa_source_output_assert_ref(o
);
672 pa_assert_ctl_context();
673 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
678 /* Called from main context */
679 pa_usec_t
pa_source_output_get_latency(pa_source_output
*o
, pa_usec_t
*source_latency
) {
680 pa_usec_t r
[2] = { 0, 0 };
682 pa_source_output_assert_ref(o
);
683 pa_assert_ctl_context();
684 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
686 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
, r
, 0, NULL
) == 0);
689 r
[0] += o
->get_latency(o
);
692 *source_latency
= r
[1];
697 /* Called from thread context */
698 void pa_source_output_push(pa_source_output
*o
, const pa_memchunk
*chunk
) {
699 pa_bool_t need_volume_factor_source
;
700 pa_bool_t volume_is_norm
;
702 size_t limit
, mbs
= 0;
704 pa_source_output_assert_ref(o
);
705 pa_source_output_assert_io_context(o
);
706 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
708 pa_assert(pa_frame_aligned(chunk
->length
, &o
->source
->sample_spec
));
710 if (!o
->push
|| o
->thread_info
.state
== PA_SOURCE_OUTPUT_CORKED
)
713 pa_assert(o
->thread_info
.state
== PA_SOURCE_OUTPUT_RUNNING
);
715 if (pa_memblockq_push(o
->thread_info
.delay_memblockq
, chunk
) < 0) {
716 pa_log_debug("Delay queue overflow!");
717 pa_memblockq_seek(o
->thread_info
.delay_memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
, TRUE
);
720 limit
= o
->process_rewind
? 0 : o
->source
->thread_info
.max_rewind
;
722 volume_is_norm
= pa_cvolume_is_norm(&o
->thread_info
.soft_volume
) && !o
->thread_info
.muted
;
723 need_volume_factor_source
= !pa_cvolume_is_norm(&o
->volume_factor_source
);
725 if (limit
> 0 && o
->source
->monitor_of
) {
729 /* Hmm, check the latency for knowing how much of the buffered
730 * data is actually still unplayed and might hence still
731 * change. This is suboptimal. Ideally we'd have a call like
732 * pa_sink_get_changeable_size() or so that tells us how much
733 * of the queued data is actually still changeable. Hence
736 latency
= pa_sink_get_latency_within_thread(o
->source
->monitor_of
);
738 n
= pa_usec_to_bytes(latency
, &o
->source
->sample_spec
);
744 /* Implement the delay queue */
745 while ((length
= pa_memblockq_get_length(o
->thread_info
.delay_memblockq
)) > limit
) {
747 pa_bool_t nvfs
= need_volume_factor_source
;
751 pa_assert_se(pa_memblockq_peek(o
->thread_info
.delay_memblockq
, &qchunk
) >= 0);
753 if (qchunk
.length
> length
)
754 qchunk
.length
= length
;
756 pa_assert(qchunk
.length
> 0);
758 /* It might be necessary to adjust the volume here */
759 if (!volume_is_norm
) {
760 pa_memchunk_make_writable(&qchunk
, 0);
762 if (o
->thread_info
.muted
) {
763 pa_silence_memchunk(&qchunk
, &o
->source
->sample_spec
);
766 } else if (!o
->thread_info
.resampler
&& nvfs
) {
769 /* If we don't need a resampler we can merge the
770 * post and the pre volume adjustment into one */
772 pa_sw_cvolume_multiply(&v
, &o
->thread_info
.soft_volume
, &o
->volume_factor_source
);
773 pa_volume_memchunk(&qchunk
, &o
->source
->sample_spec
, &v
);
777 pa_volume_memchunk(&qchunk
, &o
->source
->sample_spec
, &o
->thread_info
.soft_volume
);
780 if (!o
->thread_info
.resampler
) {
782 pa_memchunk_make_writable(&qchunk
, 0);
783 pa_volume_memchunk(&qchunk
, &o
->thread_info
.sample_spec
, &o
->volume_factor_source
);
791 mbs
= pa_resampler_max_block_size(o
->thread_info
.resampler
);
793 if (qchunk
.length
> mbs
)
796 pa_resampler_run(o
->thread_info
.resampler
, &qchunk
, &rchunk
);
798 if (rchunk
.length
> 0) {
800 pa_memchunk_make_writable(&rchunk
, 0);
801 pa_volume_memchunk(&rchunk
, &o
->thread_info
.sample_spec
, &o
->volume_factor_source
);
808 pa_memblock_unref(rchunk
.memblock
);
811 pa_memblock_unref(qchunk
.memblock
);
812 pa_memblockq_drop(o
->thread_info
.delay_memblockq
, qchunk
.length
);
816 /* Called from thread context */
817 void pa_source_output_process_rewind(pa_source_output
*o
, size_t nbytes
/* in source sample spec */) {
819 pa_source_output_assert_ref(o
);
820 pa_source_output_assert_io_context(o
);
821 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
822 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
827 if (o
->process_rewind
) {
828 pa_assert(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
) == 0);
830 if (o
->thread_info
.resampler
)
831 nbytes
= pa_resampler_result(o
->thread_info
.resampler
, nbytes
);
833 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) nbytes
);
836 o
->process_rewind(o
, nbytes
);
838 if (o
->thread_info
.resampler
)
839 pa_resampler_reset(o
->thread_info
.resampler
);
842 pa_memblockq_rewind(o
->thread_info
.delay_memblockq
, nbytes
);
845 /* Called from thread context */
846 size_t pa_source_output_get_max_rewind(pa_source_output
*o
) {
847 pa_source_output_assert_ref(o
);
848 pa_source_output_assert_io_context(o
);
850 return o
->thread_info
.resampler
? pa_resampler_request(o
->thread_info
.resampler
, o
->source
->thread_info
.max_rewind
) : o
->source
->thread_info
.max_rewind
;
853 /* Called from thread context */
854 void pa_source_output_update_max_rewind(pa_source_output
*o
, size_t nbytes
/* in the source's sample spec */) {
855 pa_source_output_assert_ref(o
);
856 pa_source_output_assert_io_context(o
);
857 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
858 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
860 if (o
->update_max_rewind
)
861 o
->update_max_rewind(o
, o
->thread_info
.resampler
? pa_resampler_result(o
->thread_info
.resampler
, nbytes
) : nbytes
);
864 /* Called from thread context */
865 pa_usec_t
pa_source_output_set_requested_latency_within_thread(pa_source_output
*o
, pa_usec_t usec
) {
866 pa_source_output_assert_ref(o
);
867 pa_source_output_assert_io_context(o
);
869 if (!(o
->source
->flags
& PA_SOURCE_DYNAMIC_LATENCY
))
870 usec
= o
->source
->thread_info
.fixed_latency
;
872 if (usec
!= (pa_usec_t
) -1)
873 usec
= PA_CLAMP(usec
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
875 o
->thread_info
.requested_source_latency
= usec
;
876 pa_source_invalidate_requested_latency(o
->source
, TRUE
);
881 /* Called from main context */
882 pa_usec_t
pa_source_output_set_requested_latency(pa_source_output
*o
, pa_usec_t usec
) {
883 pa_source_output_assert_ref(o
);
884 pa_assert_ctl_context();
886 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
) && o
->source
) {
887 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
891 /* If this source output is not realized yet or is being moved, we
892 * have to touch the thread info data directly */
895 if (!(o
->source
->flags
& PA_SOURCE_DYNAMIC_LATENCY
))
896 usec
= pa_source_get_fixed_latency(o
->source
);
898 if (usec
!= (pa_usec_t
) -1) {
899 pa_usec_t min_latency
, max_latency
;
900 pa_source_get_latency_range(o
->source
, &min_latency
, &max_latency
);
901 usec
= PA_CLAMP(usec
, min_latency
, max_latency
);
905 o
->thread_info
.requested_source_latency
= usec
;
910 /* Called from main context */
911 pa_usec_t
pa_source_output_get_requested_latency(pa_source_output
*o
) {
912 pa_source_output_assert_ref(o
);
913 pa_assert_ctl_context();
915 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
) && o
->source
) {
917 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
921 /* If this source output is not realized yet or is being moved, we
922 * have to touch the thread info data directly */
924 return o
->thread_info
.requested_source_latency
;
927 /* Called from main context */
928 void pa_source_output_set_volume(pa_source_output
*o
, const pa_cvolume
*volume
, pa_bool_t save
, pa_bool_t absolute
) {
931 pa_source_output_assert_ref(o
);
932 pa_assert_ctl_context();
933 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
935 pa_assert(pa_cvolume_valid(volume
));
936 pa_assert(volume
->channels
== 1 || pa_cvolume_compatible(volume
, &o
->sample_spec
));
937 pa_assert(o
->volume_writable
);
939 if (!absolute
&& pa_source_flat_volume_enabled(o
->source
)) {
940 v
= o
->source
->reference_volume
;
941 pa_cvolume_remap(&v
, &o
->source
->channel_map
, &o
->channel_map
);
943 if (pa_cvolume_compatible(volume
, &o
->sample_spec
))
944 volume
= pa_sw_cvolume_multiply(&v
, &v
, volume
);
946 volume
= pa_sw_cvolume_multiply_scalar(&v
, &v
, pa_cvolume_max(volume
));
948 if (!pa_cvolume_compatible(volume
, &o
->sample_spec
)) {
950 volume
= pa_cvolume_scale(&v
, pa_cvolume_max(volume
));
954 if (pa_cvolume_equal(volume
, &o
->volume
)) {
955 o
->save_volume
= o
->save_volume
|| save
;
960 o
->save_volume
= save
;
962 if (pa_source_flat_volume_enabled(o
->source
)) {
963 /* We are in flat volume mode, so let's update all source input
964 * volumes and update the flat volume of the source */
966 pa_source_set_volume(o
->source
, NULL
, TRUE
, save
);
969 /* OK, we are in normal volume mode. The volume only affects
971 set_real_ratio(o
, volume
);
973 /* Copy the new soft_volume to the thread_info struct */
974 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_VOLUME
, NULL
, 0, NULL
) == 0);
977 /* The volume changed, let's tell people so */
978 if (o
->volume_changed
)
979 o
->volume_changed(o
);
981 /* The virtual volume changed, let's tell people so */
982 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
985 /* Called from main context */
986 static void set_real_ratio(pa_source_output
*o
, const pa_cvolume
*v
) {
987 pa_source_output_assert_ref(o
);
988 pa_assert_ctl_context();
989 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
990 pa_assert(!v
|| pa_cvolume_compatible(v
, &o
->sample_spec
));
992 /* This basically calculates:
995 * o->soft_volume := o->real_ratio * o->volume_factor */
1000 pa_cvolume_reset(&o
->real_ratio
, o
->sample_spec
.channels
);
1002 pa_sw_cvolume_multiply(&o
->soft_volume
, &o
->real_ratio
, &o
->volume_factor
);
1003 /* We don't copy the data to the thread_info data. That's left for someone else to do */
1006 /* Called from main or I/O context */
1007 pa_bool_t
pa_source_output_is_passthrough(pa_source_output
*o
) {
1008 pa_source_output_assert_ref(o
);
1010 if (PA_UNLIKELY(!pa_format_info_is_pcm(o
->format
)))
1013 if (PA_UNLIKELY(o
->flags
& PA_SOURCE_OUTPUT_PASSTHROUGH
))
1019 /* Called from main context */
1020 pa_bool_t
pa_source_output_is_volume_readable(pa_source_output
*o
) {
1021 pa_source_output_assert_ref(o
);
1022 pa_assert_ctl_context();
1024 return !pa_source_output_is_passthrough(o
);
1027 /* Called from main context */
1028 pa_cvolume
*pa_source_output_get_volume(pa_source_output
*o
, pa_cvolume
*volume
, pa_bool_t absolute
) {
1029 pa_source_output_assert_ref(o
);
1030 pa_assert_ctl_context();
1031 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1032 pa_assert(pa_source_output_is_volume_readable(o
));
1034 if (absolute
|| !pa_source_flat_volume_enabled(o
->source
))
1035 *volume
= o
->volume
;
1037 *volume
= o
->reference_ratio
;
1042 /* Called from main context */
1043 void pa_source_output_set_mute(pa_source_output
*o
, pa_bool_t mute
, pa_bool_t save
) {
1044 pa_source_output_assert_ref(o
);
1045 pa_assert_ctl_context();
1046 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1048 if (!o
->muted
== !mute
) {
1049 o
->save_muted
= o
->save_muted
|| mute
;
1054 o
->save_muted
= save
;
1056 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE
, NULL
, 0, NULL
) == 0);
1058 /* The mute status changed, let's tell people so */
1059 if (o
->mute_changed
)
1062 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1065 /* Called from main context */
1066 pa_bool_t
pa_source_output_get_mute(pa_source_output
*o
) {
1067 pa_source_output_assert_ref(o
);
1068 pa_assert_ctl_context();
1069 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1074 /* Called from main thread */
1075 void pa_source_output_update_proplist(pa_source_output
*o
, pa_update_mode_t mode
, pa_proplist
*p
) {
1076 pa_source_output_assert_ref(o
);
1077 pa_assert_ctl_context();
1080 pa_proplist_update(o
->proplist
, mode
, p
);
1082 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
1083 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
1084 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1088 /* Called from main context */
1089 void pa_source_output_cork(pa_source_output
*o
, pa_bool_t b
) {
1090 pa_source_output_assert_ref(o
);
1091 pa_assert_ctl_context();
1092 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1094 source_output_set_state(o
, b
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
);
1097 /* Called from main context */
1098 int pa_source_output_set_rate(pa_source_output
*o
, uint32_t rate
) {
1099 pa_source_output_assert_ref(o
);
1100 pa_assert_ctl_context();
1101 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1102 pa_return_val_if_fail(o
->thread_info
.resampler
, -PA_ERR_BADSTATE
);
1104 if (o
->sample_spec
.rate
== rate
)
1107 o
->sample_spec
.rate
= rate
;
1109 pa_asyncmsgq_post(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
, PA_UINT_TO_PTR(rate
), 0, NULL
, NULL
);
1111 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1115 /* Called from main context */
1116 void pa_source_output_set_name(pa_source_output
*o
, const char *name
) {
1118 pa_assert_ctl_context();
1119 pa_source_output_assert_ref(o
);
1121 if (!name
&& !pa_proplist_contains(o
->proplist
, PA_PROP_MEDIA_NAME
))
1124 old
= pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
);
1126 if (old
&& name
&& !strcmp(old
, name
))
1130 pa_proplist_sets(o
->proplist
, PA_PROP_MEDIA_NAME
, name
);
1132 pa_proplist_unset(o
->proplist
, PA_PROP_MEDIA_NAME
);
1134 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
1135 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
1136 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1140 /* Called from main context */
1141 pa_resample_method_t
pa_source_output_get_resample_method(pa_source_output
*o
) {
1142 pa_source_output_assert_ref(o
);
1143 pa_assert_ctl_context();
1145 return o
->actual_resample_method
;
1148 /* Called from main context */
1149 pa_bool_t
pa_source_output_may_move(pa_source_output
*o
) {
1150 pa_source_output_assert_ref(o
);
1151 pa_assert_ctl_context();
1152 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1154 if (o
->flags
& PA_SOURCE_OUTPUT_DONT_MOVE
)
1157 if (o
->direct_on_input
)
1163 /* Called from main context */
1164 pa_bool_t
pa_source_output_may_move_to(pa_source_output
*o
, pa_source
*dest
) {
1165 pa_source_output_assert_ref(o
);
1166 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1167 pa_source_assert_ref(dest
);
1169 if (dest
== o
->source
)
1172 if (!pa_source_output_may_move(o
))
1175 if (pa_idxset_size(dest
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
1176 pa_log_warn("Failed to move source output: too many outputs per source.");
1181 if (!o
->may_move_to(o
, dest
))
1187 /* Called from main context */
1188 int pa_source_output_start_move(pa_source_output
*o
) {
1192 pa_source_output_assert_ref(o
);
1193 pa_assert_ctl_context();
1194 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1195 pa_assert(o
->source
);
1197 if (!pa_source_output_may_move(o
))
1198 return -PA_ERR_NOTSUPPORTED
;
1200 if ((r
= pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START
], o
)) < 0)
1205 pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
);
1207 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
)
1208 pa_assert_se(origin
->n_corked
-- >= 1);
1210 if (pa_source_output_is_passthrough(o
))
1211 pa_source_leave_passthrough(o
->source
);
1213 if (pa_source_flat_volume_enabled(o
->source
))
1214 /* We might need to update the source's volume if we are in flat
1216 pa_source_set_volume(o
->source
, NULL
, FALSE
, FALSE
);
1218 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
1220 pa_source_update_status(o
->source
);
1223 pa_source_output_unref(o
);
1228 /* Called from main context. If it has an origin source that uses volume sharing,
1229 * then also the origin source and all streams connected to it need to update
1230 * their volume - this function does all that by using recursion. */
1231 static void update_volume_due_to_moving(pa_source_output
*o
, pa_source
*dest
) {
1232 pa_cvolume old_volume
;
1236 pa_assert(o
->source
); /* The destination source should already be set. */
1238 if (o
->destination_source
&& (o
->destination_source
->flags
& PA_SOURCE_SHARE_VOLUME_WITH_MASTER
)) {
1239 pa_source
*root_source
;
1240 pa_source_output
*destination_source_output
;
1243 root_source
= pa_source_get_master(o
->source
);
1245 if (PA_UNLIKELY(!root_source
))
1248 if (pa_source_flat_volume_enabled(o
->source
)) {
1249 /* Ok, so the origin source uses volume sharing, and flat volume is
1250 * enabled. The volume will have to be updated as follows:
1252 * o->volume := o->source->real_volume
1253 * (handled later by pa_source_set_volume)
1254 * o->reference_ratio := o->volume / o->source->reference_volume
1255 * (handled later by pa_source_set_volume)
1256 * o->real_ratio stays unchanged
1257 * (streams whose origin source uses volume sharing should
1258 * always have real_ratio of 0 dB)
1259 * o->soft_volume stays unchanged
1260 * (streams whose origin source uses volume sharing should
1261 * always have volume_factor as soft_volume, so no change
1262 * should be needed) */
1264 pa_assert(pa_cvolume_is_norm(&o
->real_ratio
));
1265 pa_assert(pa_cvolume_equal(&o
->soft_volume
, &o
->volume_factor
));
1267 /* Notifications will be sent by pa_source_set_volume(). */
1270 /* Ok, so the origin source uses volume sharing, and flat volume is
1271 * disabled. The volume will have to be updated as follows:
1274 * o->reference_ratio := 0 dB
1275 * o->real_ratio stays unchanged
1276 * (streams whose origin source uses volume sharing should
1277 * always have real_ratio of 0 dB)
1278 * o->soft_volume stays unchanged
1279 * (streams whose origin source uses volume sharing should
1280 * always have volume_factor as soft_volume, so no change
1281 * should be needed) */
1283 old_volume
= o
->volume
;
1284 pa_cvolume_reset(&o
->volume
, o
->volume
.channels
);
1285 pa_cvolume_reset(&o
->reference_ratio
, o
->reference_ratio
.channels
);
1286 pa_assert(pa_cvolume_is_norm(&o
->real_ratio
));
1287 pa_assert(pa_cvolume_equal(&o
->soft_volume
, &o
->volume_factor
));
1289 /* Notify others about the changed source output volume. */
1290 if (!pa_cvolume_equal(&o
->volume
, &old_volume
)) {
1291 if (o
->volume_changed
)
1292 o
->volume_changed(o
);
1294 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1298 /* Additionally, the origin source volume needs updating:
1300 * o->destination_source->reference_volume := root_source->reference_volume
1301 * o->destination_source->real_volume := root_source->real_volume
1302 * o->destination_source->soft_volume stays unchanged
1303 * (sources that use volume sharing should always have
1304 * soft_volume of 0 dB) */
1306 old_volume
= o
->destination_source
->reference_volume
;
1308 o
->destination_source
->reference_volume
= root_source
->reference_volume
;
1309 pa_cvolume_remap(&o
->destination_source
->reference_volume
, &root_source
->channel_map
, &o
->destination_source
->channel_map
);
1311 o
->destination_source
->real_volume
= root_source
->real_volume
;
1312 pa_cvolume_remap(&o
->destination_source
->real_volume
, &root_source
->channel_map
, &o
->destination_source
->channel_map
);
1314 pa_assert(pa_cvolume_is_norm(&o
->destination_source
->soft_volume
));
1316 /* Notify others about the changed source volume. If you wonder whether
1317 * o->destination_source->set_volume() should be called somewhere, that's not
1318 * the case, because sources that use volume sharing shouldn't have any
1319 * internal volume that set_volume() would update. If you wonder
1320 * whether the thread_info variables should be synced, yes, they
1321 * should, and it's done by the PA_SOURCE_MESSAGE_FINISH_MOVE message
1323 if (!pa_cvolume_equal(&o
->destination_source
->reference_volume
, &old_volume
))
1324 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->destination_source
->index
);
1326 /* Recursively update origin source outputs. */
1327 PA_IDXSET_FOREACH(destination_source_output
, o
->destination_source
->outputs
, idx
)
1328 update_volume_due_to_moving(destination_source_output
, dest
);
1331 old_volume
= o
->volume
;
1333 if (pa_source_flat_volume_enabled(o
->source
)) {
1334 /* Ok, so this is a regular stream, and flat volume is enabled. The
1335 * volume will have to be updated as follows:
1337 * o->volume := o->reference_ratio * o->source->reference_volume
1338 * o->reference_ratio stays unchanged
1339 * o->real_ratio := o->volume / o->source->real_volume
1340 * (handled later by pa_source_set_volume)
1341 * o->soft_volume := o->real_ratio * o->volume_factor
1342 * (handled later by pa_source_set_volume) */
1344 o
->volume
= o
->source
->reference_volume
;
1345 pa_cvolume_remap(&o
->volume
, &o
->source
->channel_map
, &o
->channel_map
);
1346 pa_sw_cvolume_multiply(&o
->volume
, &o
->volume
, &o
->reference_ratio
);
1349 /* Ok, so this is a regular stream, and flat volume is disabled.
1350 * The volume will have to be updated as follows:
1352 * o->volume := o->reference_ratio
1353 * o->reference_ratio stays unchanged
1354 * o->real_ratio := o->reference_ratio
1355 * o->soft_volume := o->real_ratio * o->volume_factor */
1357 o
->volume
= o
->reference_ratio
;
1358 o
->real_ratio
= o
->reference_ratio
;
1359 pa_sw_cvolume_multiply(&o
->soft_volume
, &o
->real_ratio
, &o
->volume_factor
);
1362 /* Notify others about the changed source output volume. */
1363 if (!pa_cvolume_equal(&o
->volume
, &old_volume
)) {
1364 /* XXX: In case o->source has flat volume enabled, then real_ratio
1365 * and soft_volume are not updated yet. Let's hope that the
1366 * callback implementation doesn't care about those variables... */
1367 if (o
->volume_changed
)
1368 o
->volume_changed(o
);
1370 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1374 /* If o->source == dest, then recursion has finished, and we can finally call
1375 * pa_source_set_volume(), which will do the rest of the updates. */
1376 if ((o
->source
== dest
) && pa_source_flat_volume_enabled(o
->source
))
1377 pa_source_set_volume(o
->source
, NULL
, FALSE
, o
->save_volume
);
1380 /* Called from main context */
1381 int pa_source_output_finish_move(pa_source_output
*o
, pa_source
*dest
, pa_bool_t save
) {
1382 pa_source_output_assert_ref(o
);
1383 pa_assert_ctl_context();
1384 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1385 pa_assert(!o
->source
);
1386 pa_source_assert_ref(dest
);
1388 if (!pa_source_output_may_move_to(o
, dest
))
1389 return -PA_ERR_NOTSUPPORTED
;
1391 if (pa_source_output_is_passthrough(o
) && !pa_source_check_format(dest
, o
->format
)) {
1392 pa_proplist
*p
= pa_proplist_new();
1393 pa_log_debug("New source doesn't support stream format, sending format-changed and killing");
1394 /* Tell the client what device we want to be on if it is going to
1396 pa_proplist_sets(p
, "device", dest
->name
);
1397 pa_source_output_send_event(o
, PA_STREAM_EVENT_FORMAT_LOST
, p
);
1398 pa_proplist_free(p
);
1399 return -PA_ERR_NOTSUPPORTED
;
1402 if (!(o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) &&
1403 !pa_sample_spec_equal(&o
->sample_spec
, &dest
->sample_spec
)){
1404 /* try to change dest sink rate if possible without glitches.
1405 module-suspend-on-idle resumes destination source with
1406 SOURCE_OUTPUT_MOVE_FINISH hook */
1408 pa_log_info("Trying to change sample rate");
1409 if (pa_source_update_rate(dest
, o
->sample_spec
.rate
, pa_source_output_is_passthrough(o
)) == TRUE
)
1410 pa_log_info("Rate changed to %u Hz",
1411 dest
->sample_spec
.rate
);
1413 pa_log_info("Resampling enabled to %u Hz",
1414 dest
->sample_spec
.rate
);
1421 o
->save_source
= save
;
1422 pa_idxset_put(o
->source
->outputs
, pa_source_output_ref(o
), NULL
);
1424 pa_cvolume_remap(&o
->volume_factor_source
, &o
->channel_map
, &o
->source
->channel_map
);
1426 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
)
1427 o
->source
->n_corked
++;
1429 pa_source_output_update_rate(o
);
1431 pa_source_update_status(dest
);
1433 update_volume_due_to_moving(o
, dest
);
1435 if (pa_source_output_is_passthrough(o
))
1436 pa_source_enter_passthrough(o
->source
);
1438 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
1440 pa_log_debug("Successfully moved source output %i to %s.", o
->index
, dest
->name
);
1442 /* Notify everyone */
1443 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH
], o
);
1444 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1449 /* Called from main context */
1450 void pa_source_output_fail_move(pa_source_output
*o
) {
1452 pa_source_output_assert_ref(o
);
1453 pa_assert_ctl_context();
1454 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1455 pa_assert(!o
->source
);
1457 /* Check if someone wants this source output? */
1458 if (pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL
], o
) == PA_HOOK_STOP
)
1464 pa_source_output_kill(o
);
1467 /* Called from main context */
1468 int pa_source_output_move_to(pa_source_output
*o
, pa_source
*dest
, pa_bool_t save
) {
1471 pa_source_output_assert_ref(o
);
1472 pa_assert_ctl_context();
1473 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1474 pa_assert(o
->source
);
1475 pa_source_assert_ref(dest
);
1477 if (dest
== o
->source
)
1480 if (!pa_source_output_may_move_to(o
, dest
))
1481 return -PA_ERR_NOTSUPPORTED
;
1483 pa_source_output_ref(o
);
1485 if ((r
= pa_source_output_start_move(o
)) < 0) {
1486 pa_source_output_unref(o
);
1490 if ((r
= pa_source_output_finish_move(o
, dest
, save
)) < 0) {
1491 pa_source_output_fail_move(o
);
1492 pa_source_output_unref(o
);
1496 pa_source_output_unref(o
);
1501 /* Called from IO thread context */
1502 void pa_source_output_set_state_within_thread(pa_source_output
*o
, pa_source_output_state_t state
) {
1503 pa_source_output_assert_ref(o
);
1504 pa_source_output_assert_io_context(o
);
1506 if (state
== o
->thread_info
.state
)
1509 if (o
->state_change
)
1510 o
->state_change(o
, state
);
1512 o
->thread_info
.state
= state
;
1515 /* Called from IO thread context, except when it is not */
1516 int pa_source_output_process_msg(pa_msgobject
*mo
, int code
, void *userdata
, int64_t offset
, pa_memchunk
* chunk
) {
1517 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
1518 pa_source_output_assert_ref(o
);
1522 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
: {
1523 pa_usec_t
*r
= userdata
;
1525 r
[0] += pa_bytes_to_usec(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
), &o
->source
->sample_spec
);
1526 r
[1] += pa_source_get_latency_within_thread(o
->source
);
1531 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
:
1533 o
->thread_info
.sample_spec
.rate
= PA_PTR_TO_UINT(userdata
);
1534 pa_resampler_set_output_rate(o
->thread_info
.resampler
, PA_PTR_TO_UINT(userdata
));
1537 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE
:
1539 pa_source_output_set_state_within_thread(o
, PA_PTR_TO_UINT(userdata
));
1543 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
: {
1544 pa_usec_t
*usec
= userdata
;
1546 *usec
= pa_source_output_set_requested_latency_within_thread(o
, *usec
);
1551 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
: {
1552 pa_usec_t
*r
= userdata
;
1554 *r
= o
->thread_info
.requested_source_latency
;
1558 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_VOLUME
:
1559 if (!pa_cvolume_equal(&o
->thread_info
.soft_volume
, &o
->soft_volume
)) {
1560 o
->thread_info
.soft_volume
= o
->soft_volume
;
1564 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE
:
1565 if (o
->thread_info
.muted
!= o
->muted
) {
1566 o
->thread_info
.muted
= o
->muted
;
1571 return -PA_ERR_NOTIMPLEMENTED
;
1574 /* Called from main context */
1575 void pa_source_output_send_event(pa_source_output
*o
, const char *event
, pa_proplist
*data
) {
1576 pa_proplist
*pl
= NULL
;
1577 pa_source_output_send_event_hook_data hook_data
;
1579 pa_source_output_assert_ref(o
);
1580 pa_assert_ctl_context();
1587 data
= pl
= pa_proplist_new();
1589 hook_data
.source_output
= o
;
1590 hook_data
.data
= data
;
1591 hook_data
.event
= event
;
1593 if (pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_SEND_EVENT
], &hook_data
) < 0)
1596 o
->send_event(o
, event
, data
);
1600 pa_proplist_free(pl
);
1603 /* Called from main context */
1604 /* Updates the source output's resampler with whatever the current source
1605 * requires -- useful when the underlying source's rate might have changed */
1606 int pa_source_output_update_rate(pa_source_output
*o
) {
1607 pa_resampler
*new_resampler
;
1608 char *memblockq_name
;
1610 pa_source_output_assert_ref(o
);
1611 pa_assert_ctl_context();
1613 if (o
->thread_info
.resampler
&&
1614 pa_sample_spec_equal(pa_resampler_input_sample_spec(o
->thread_info
.resampler
), &o
->source
->sample_spec
) &&
1615 pa_channel_map_equal(pa_resampler_input_channel_map(o
->thread_info
.resampler
), &o
->source
->channel_map
))
1617 new_resampler
= o
->thread_info
.resampler
;
1619 else if (!pa_source_output_is_passthrough(o
) &&
1620 ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
1621 !pa_sample_spec_equal(&o
->sample_spec
, &o
->source
->sample_spec
) ||
1622 !pa_channel_map_equal(&o
->channel_map
, &o
->source
->channel_map
))) {
1624 new_resampler
= pa_resampler_new(o
->core
->mempool
,
1625 &o
->source
->sample_spec
, &o
->source
->channel_map
,
1626 &o
->sample_spec
, &o
->channel_map
,
1627 o
->requested_resample_method
,
1628 ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
1629 ((o
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
1630 (o
->core
->disable_remixing
|| (o
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0));
1632 if (!new_resampler
) {
1633 pa_log_warn("Unsupported resampling operation.");
1634 return -PA_ERR_NOTSUPPORTED
;
1637 new_resampler
= NULL
;
1639 if (new_resampler
== o
->thread_info
.resampler
)
1642 if (o
->thread_info
.resampler
)
1643 pa_resampler_free(o
->thread_info
.resampler
);
1645 o
->thread_info
.resampler
= new_resampler
;
1647 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
1649 memblockq_name
= pa_sprintf_malloc("source output delay_memblockq [%u]", o
->index
);
1650 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
1653 MEMBLOCKQ_MAXLENGTH
,
1655 &o
->source
->sample_spec
,
1659 &o
->source
->silence
);
1660 pa_xfree(memblockq_name
);
1662 o
->actual_resample_method
= new_resampler
? pa_resampler_get_method(new_resampler
) : PA_RESAMPLER_INVALID
;
1664 pa_log_debug("Updated resampler for source output %d", o
->index
);