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
, &data
->channel_map
);
247 pa_idxset_put(tmp
, f
, NULL
);
248 pa_source_output_new_data_set_formats(data
, tmp
);
251 if ((r
= pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_NEW
], data
)) < 0)
254 pa_return_val_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
), -PA_ERR_INVALID
);
257 pa_source
*source
= pa_namereg_get(core
, NULL
, PA_NAMEREG_SOURCE
);
258 pa_return_val_if_fail(source
, -PA_ERR_NOENTITY
);
259 pa_source_output_new_data_set_source(data
, source
, FALSE
);
262 /* Routing's done, we have a source. Now let's fix the format and set up the
265 /* If something didn't pick a format for us, pick the top-most format since
266 * we assume this is sorted in priority order */
267 if (!data
->format
&& data
->nego_formats
&& !pa_idxset_isempty(data
->nego_formats
))
268 data
->format
= pa_format_info_copy(pa_idxset_first(data
->nego_formats
, NULL
));
270 pa_return_val_if_fail(data
->format
, -PA_ERR_NOTSUPPORTED
);
272 /* Now populate the sample spec and format according to the final
273 * format that we've negotiated */
274 if (PA_LIKELY(data
->format
->encoding
== PA_ENCODING_PCM
)) {
275 pa_return_val_if_fail(pa_format_info_to_sample_spec(data
->format
, &ss
, &map
), -PA_ERR_INVALID
);
276 pa_source_output_new_data_set_sample_spec(data
, &ss
);
277 if (pa_channel_map_valid(&map
))
278 pa_source_output_new_data_set_channel_map(data
, &map
);
280 pa_return_val_if_fail(pa_format_info_to_sample_spec_fake(data
->format
, &ss
), -PA_ERR_INVALID
);
281 pa_source_output_new_data_set_sample_spec(data
, &ss
);
284 pa_return_val_if_fail(PA_SOURCE_IS_LINKED(pa_source_get_state(data
->source
)), -PA_ERR_BADSTATE
);
285 pa_return_val_if_fail(!data
->direct_on_input
|| data
->direct_on_input
->sink
== data
->source
->monitor_of
, -PA_ERR_INVALID
);
287 if (!data
->sample_spec_is_set
)
288 data
->sample_spec
= data
->source
->sample_spec
;
290 pa_return_val_if_fail(pa_sample_spec_valid(&data
->sample_spec
), -PA_ERR_INVALID
);
292 if (!data
->channel_map_is_set
) {
293 if (pa_channel_map_compatible(&data
->source
->channel_map
, &data
->sample_spec
))
294 data
->channel_map
= data
->source
->channel_map
;
296 pa_channel_map_init_extend(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
);
299 pa_return_val_if_fail(pa_channel_map_compatible(&data
->channel_map
, &data
->sample_spec
), -PA_ERR_INVALID
);
301 /* Don't restore (or save) stream volume for passthrough streams and
302 * prevent attenuation/gain */
303 if (pa_source_output_new_data_is_passthrough(data
)) {
304 data
->volume_is_set
= TRUE
;
305 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
306 data
->volume_is_absolute
= TRUE
;
307 data
->save_volume
= FALSE
;
310 if (!data
->volume_is_set
) {
311 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
312 data
->volume_is_absolute
= FALSE
;
313 data
->save_volume
= FALSE
;
316 pa_return_val_if_fail(pa_cvolume_compatible(&data
->volume
, &data
->sample_spec
), -PA_ERR_INVALID
);
318 if (!data
->volume_factor_is_set
)
319 pa_cvolume_reset(&data
->volume_factor
, data
->sample_spec
.channels
);
321 pa_return_val_if_fail(pa_cvolume_compatible(&data
->volume_factor
, &data
->sample_spec
), -PA_ERR_INVALID
);
323 if (!data
->volume_factor_source_is_set
)
324 pa_cvolume_reset(&data
->volume_factor_source
, data
->source
->sample_spec
.channels
);
326 pa_return_val_if_fail(pa_cvolume_compatible(&data
->volume_factor_source
, &data
->source
->sample_spec
), -PA_ERR_INVALID
);
328 if (!data
->muted_is_set
)
331 if (data
->flags
& PA_SOURCE_OUTPUT_FIX_FORMAT
)
332 data
->sample_spec
.format
= data
->source
->sample_spec
.format
;
334 if (data
->flags
& PA_SOURCE_OUTPUT_FIX_RATE
)
335 data
->sample_spec
.rate
= data
->source
->sample_spec
.rate
;
337 original_cm
= data
->channel_map
;
339 if (data
->flags
& PA_SOURCE_OUTPUT_FIX_CHANNELS
) {
340 data
->sample_spec
.channels
= data
->source
->sample_spec
.channels
;
341 data
->channel_map
= data
->source
->channel_map
;
344 pa_assert(pa_sample_spec_valid(&data
->sample_spec
));
345 pa_assert(pa_channel_map_valid(&data
->channel_map
));
347 /* Due to the fixing of the sample spec the volume might not match anymore */
348 pa_cvolume_remap(&data
->volume
, &original_cm
, &data
->channel_map
);
350 if (data
->resample_method
== PA_RESAMPLER_INVALID
)
351 data
->resample_method
= core
->resample_method
;
353 pa_return_val_if_fail(data
->resample_method
< PA_RESAMPLER_MAX
, -PA_ERR_INVALID
);
355 if ((r
= pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE
], data
)) < 0)
358 if ((data
->flags
& PA_SOURCE_OUTPUT_NO_CREATE_ON_SUSPEND
) &&
359 pa_source_get_state(data
->source
) == PA_SOURCE_SUSPENDED
) {
360 pa_log("Failed to create source output: source is suspended.");
361 return -PA_ERR_BADSTATE
;
364 if (pa_idxset_size(data
->source
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
365 pa_log("Failed to create source output: too many outputs per source.");
366 return -PA_ERR_TOOLARGE
;
369 if ((data
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
370 !pa_sample_spec_equal(&data
->sample_spec
, &data
->source
->sample_spec
) ||
371 !pa_channel_map_equal(&data
->channel_map
, &data
->source
->channel_map
)) {
373 if (!pa_source_output_new_data_is_passthrough(data
)) /* no resampler for passthrough content */
374 if (!(resampler
= pa_resampler_new(
376 &data
->source
->sample_spec
, &data
->source
->channel_map
,
377 &data
->sample_spec
, &data
->channel_map
,
378 data
->resample_method
,
379 ((data
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
380 ((data
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
381 (core
->disable_remixing
|| (data
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0) |
382 (core
->disable_lfe_remixing
? PA_RESAMPLER_NO_LFE
: 0)))) {
383 pa_log_warn("Unsupported resampling operation.");
384 return -PA_ERR_NOTSUPPORTED
;
388 o
= pa_msgobject_new(pa_source_output
);
389 o
->parent
.parent
.free
= source_output_free
;
390 o
->parent
.process_msg
= pa_source_output_process_msg
;
393 o
->state
= PA_SOURCE_OUTPUT_INIT
;
394 o
->flags
= data
->flags
;
395 o
->proplist
= pa_proplist_copy(data
->proplist
);
396 o
->driver
= pa_xstrdup(pa_path_get_filename(data
->driver
));
397 o
->module
= data
->module
;
398 o
->source
= data
->source
;
399 o
->destination_source
= data
->destination_source
;
400 o
->client
= data
->client
;
402 o
->requested_resample_method
= data
->resample_method
;
403 o
->actual_resample_method
= resampler
? pa_resampler_get_method(resampler
) : PA_RESAMPLER_INVALID
;
404 o
->sample_spec
= data
->sample_spec
;
405 o
->channel_map
= data
->channel_map
;
406 o
->format
= pa_format_info_copy(data
->format
);
408 if (!data
->volume_is_absolute
&& pa_source_flat_volume_enabled(o
->source
)) {
411 /* When the 'absolute' bool is not set then we'll treat the volume
412 * as relative to the source volume even in flat volume mode */
413 remapped
= data
->source
->reference_volume
;
414 pa_cvolume_remap(&remapped
, &data
->source
->channel_map
, &data
->channel_map
);
415 pa_sw_cvolume_multiply(&o
->volume
, &data
->volume
, &remapped
);
417 o
->volume
= data
->volume
;
419 o
->volume_factor
= data
->volume_factor
;
420 o
->volume_factor_source
= data
->volume_factor_source
;
421 o
->real_ratio
= o
->reference_ratio
= data
->volume
;
422 pa_cvolume_reset(&o
->soft_volume
, o
->sample_spec
.channels
);
423 pa_cvolume_reset(&o
->real_ratio
, o
->sample_spec
.channels
);
424 o
->volume_writable
= data
->volume_writable
;
425 o
->save_volume
= data
->save_volume
;
426 o
->save_source
= data
->save_source
;
427 o
->save_muted
= data
->save_muted
;
429 o
->muted
= data
->muted
;
431 o
->direct_on_input
= data
->direct_on_input
;
436 o
->thread_info
.state
= o
->state
;
437 o
->thread_info
.attached
= FALSE
;
438 o
->thread_info
.sample_spec
= o
->sample_spec
;
439 o
->thread_info
.resampler
= resampler
;
440 o
->thread_info
.soft_volume
= o
->soft_volume
;
441 o
->thread_info
.muted
= o
->muted
;
442 o
->thread_info
.requested_source_latency
= (pa_usec_t
) -1;
443 o
->thread_info
.direct_on_input
= o
->direct_on_input
;
445 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
446 "source output delay_memblockq",
450 &o
->source
->sample_spec
,
454 &o
->source
->silence
);
456 pa_assert_se(pa_idxset_put(core
->source_outputs
, o
, &o
->index
) == 0);
457 pa_assert_se(pa_idxset_put(o
->source
->outputs
, pa_source_output_ref(o
), NULL
) == 0);
460 pa_assert_se(pa_idxset_put(o
->client
->source_outputs
, o
, NULL
) >= 0);
462 if (o
->direct_on_input
)
463 pa_assert_se(pa_idxset_put(o
->direct_on_input
->direct_outputs
, o
, NULL
) == 0);
465 pt
= pa_proplist_to_string_sep(o
->proplist
, "\n ");
466 pa_log_info("Created output %u \"%s\" on %s with sample spec %s and channel map %s\n %s",
468 pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)),
470 pa_sample_spec_snprint(st
, sizeof(st
), &o
->sample_spec
),
471 pa_channel_map_snprint(cm
, sizeof(cm
), &o
->channel_map
),
475 /* Don't forget to call pa_source_output_put! */
481 /* Called from main context */
482 static void update_n_corked(pa_source_output
*o
, pa_source_output_state_t state
) {
484 pa_assert_ctl_context();
489 if (o
->state
== PA_SOURCE_OUTPUT_CORKED
&& state
!= PA_SOURCE_OUTPUT_CORKED
)
490 pa_assert_se(o
->source
->n_corked
-- >= 1);
491 else if (o
->state
!= PA_SOURCE_OUTPUT_CORKED
&& state
== PA_SOURCE_OUTPUT_CORKED
)
492 o
->source
->n_corked
++;
495 /* Called from main context */
496 static void source_output_set_state(pa_source_output
*o
, pa_source_output_state_t state
) {
499 pa_assert_ctl_context();
501 if (o
->state
== state
)
504 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);
506 update_n_corked(o
, state
);
509 if (state
!= PA_SOURCE_OUTPUT_UNLINKED
) {
510 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED
], o
);
512 if (PA_SOURCE_OUTPUT_IS_LINKED(state
))
513 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
516 pa_source_update_status(o
->source
);
519 /* Called from main context */
520 void pa_source_output_unlink(pa_source_output
*o
) {
523 pa_assert_ctl_context();
525 /* See pa_sink_unlink() for a couple of comments how this function
528 pa_source_output_ref(o
);
530 linked
= PA_SOURCE_OUTPUT_IS_LINKED(o
->state
);
533 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK
], o
);
535 if (o
->direct_on_input
)
536 pa_idxset_remove_by_data(o
->direct_on_input
->direct_outputs
, o
, NULL
);
538 pa_idxset_remove_by_data(o
->core
->source_outputs
, o
, NULL
);
541 if (pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
))
542 pa_source_output_unref(o
);
545 pa_idxset_remove_by_data(o
->client
->source_outputs
, o
, NULL
);
547 update_n_corked(o
, PA_SOURCE_OUTPUT_UNLINKED
);
548 o
->state
= PA_SOURCE_OUTPUT_UNLINKED
;
550 if (linked
&& o
->source
) {
551 if (pa_source_output_is_passthrough(o
))
552 pa_source_leave_passthrough(o
->source
);
554 /* We might need to update the source's volume if we are in flat volume mode. */
555 if (pa_source_flat_volume_enabled(o
->source
))
556 pa_source_set_volume(o
->source
, NULL
, FALSE
, FALSE
);
558 if (o
->source
->asyncmsgq
)
559 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
565 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, o
->index
);
566 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST
], o
);
570 pa_source_update_status(o
->source
);
574 pa_core_maybe_vacuum(o
->core
);
576 pa_source_output_unref(o
);
579 /* Called from main context */
580 static void source_output_free(pa_object
* mo
) {
581 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
584 pa_assert_ctl_context();
585 pa_assert(pa_source_output_refcnt(o
) == 0);
587 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
588 pa_source_output_unlink(o
);
590 pa_log_info("Freeing output %u \"%s\"", o
->index
, pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)));
592 if (o
->thread_info
.delay_memblockq
)
593 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
595 if (o
->thread_info
.resampler
)
596 pa_resampler_free(o
->thread_info
.resampler
);
599 pa_format_info_free(o
->format
);
602 pa_proplist_free(o
->proplist
);
608 /* Called from main context */
609 void pa_source_output_put(pa_source_output
*o
) {
610 pa_source_output_state_t state
;
612 pa_source_output_assert_ref(o
);
613 pa_assert_ctl_context();
615 pa_assert(o
->state
== PA_SOURCE_OUTPUT_INIT
);
617 /* The following fields must be initialized properly */
621 state
= o
->flags
& PA_SOURCE_OUTPUT_START_CORKED
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
;
623 update_n_corked(o
, state
);
626 /* We might need to update the source's volume if we are in flat volume mode. */
627 if (pa_source_flat_volume_enabled(o
->source
))
628 pa_source_set_volume(o
->source
, NULL
, FALSE
, o
->save_volume
);
630 if (o
->destination_source
&& (o
->destination_source
->flags
& PA_SOURCE_SHARE_VOLUME_WITH_MASTER
)) {
631 pa_assert(pa_cvolume_is_norm(&o
->volume
));
632 pa_assert(pa_cvolume_is_norm(&o
->reference_ratio
));
635 set_real_ratio(o
, &o
->volume
);
638 if (pa_source_output_is_passthrough(o
))
639 pa_source_enter_passthrough(o
->source
);
641 o
->thread_info
.soft_volume
= o
->soft_volume
;
642 o
->thread_info
.muted
= o
->muted
;
644 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
646 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
, o
->index
);
647 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PUT
], o
);
649 pa_source_update_status(o
->source
);
652 /* Called from main context */
653 void pa_source_output_kill(pa_source_output
*o
) {
654 pa_source_output_assert_ref(o
);
655 pa_assert_ctl_context();
656 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
661 /* Called from main context */
662 pa_usec_t
pa_source_output_get_latency(pa_source_output
*o
, pa_usec_t
*source_latency
) {
663 pa_usec_t r
[2] = { 0, 0 };
665 pa_source_output_assert_ref(o
);
666 pa_assert_ctl_context();
667 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
669 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
, r
, 0, NULL
) == 0);
672 r
[0] += o
->get_latency(o
);
675 *source_latency
= r
[1];
680 /* Called from thread context */
681 void pa_source_output_push(pa_source_output
*o
, const pa_memchunk
*chunk
) {
682 pa_bool_t need_volume_factor_source
;
683 pa_bool_t volume_is_norm
;
685 size_t limit
, mbs
= 0;
687 pa_source_output_assert_ref(o
);
688 pa_source_output_assert_io_context(o
);
689 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
691 pa_assert(pa_frame_aligned(chunk
->length
, &o
->source
->sample_spec
));
693 if (!o
->push
|| o
->thread_info
.state
== PA_SOURCE_OUTPUT_CORKED
)
696 pa_assert(o
->thread_info
.state
== PA_SOURCE_OUTPUT_RUNNING
);
698 if (pa_memblockq_push(o
->thread_info
.delay_memblockq
, chunk
) < 0) {
699 pa_log_debug("Delay queue overflow!");
700 pa_memblockq_seek(o
->thread_info
.delay_memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
, TRUE
);
703 limit
= o
->process_rewind
? 0 : o
->source
->thread_info
.max_rewind
;
705 volume_is_norm
= pa_cvolume_is_norm(&o
->thread_info
.soft_volume
) && !o
->thread_info
.muted
;
706 need_volume_factor_source
= !pa_cvolume_is_norm(&o
->volume_factor_source
);
708 if (limit
> 0 && o
->source
->monitor_of
) {
712 /* Hmm, check the latency for knowing how much of the buffered
713 * data is actually still unplayed and might hence still
714 * change. This is suboptimal. Ideally we'd have a call like
715 * pa_sink_get_changeable_size() or so that tells us how much
716 * of the queued data is actually still changeable. Hence
719 latency
= pa_sink_get_latency_within_thread(o
->source
->monitor_of
);
721 n
= pa_usec_to_bytes(latency
, &o
->source
->sample_spec
);
727 /* Implement the delay queue */
728 while ((length
= pa_memblockq_get_length(o
->thread_info
.delay_memblockq
)) > limit
) {
730 pa_bool_t nvfs
= need_volume_factor_source
;
734 pa_assert_se(pa_memblockq_peek(o
->thread_info
.delay_memblockq
, &qchunk
) >= 0);
736 if (qchunk
.length
> length
)
737 qchunk
.length
= length
;
739 pa_assert(qchunk
.length
> 0);
741 /* It might be necessary to adjust the volume here */
742 if (!volume_is_norm
) {
743 pa_memchunk_make_writable(&qchunk
, 0);
745 if (o
->thread_info
.muted
) {
746 pa_silence_memchunk(&qchunk
, &o
->source
->sample_spec
);
749 } else if (!o
->thread_info
.resampler
&& nvfs
) {
752 /* If we don't need a resampler we can merge the
753 * post and the pre volume adjustment into one */
755 pa_sw_cvolume_multiply(&v
, &o
->thread_info
.soft_volume
, &o
->volume_factor_source
);
756 pa_volume_memchunk(&qchunk
, &o
->source
->sample_spec
, &v
);
760 pa_volume_memchunk(&qchunk
, &o
->source
->sample_spec
, &o
->thread_info
.soft_volume
);
763 if (!o
->thread_info
.resampler
) {
765 pa_memchunk_make_writable(&qchunk
, 0);
766 pa_volume_memchunk(&qchunk
, &o
->thread_info
.sample_spec
, &o
->volume_factor_source
);
774 mbs
= pa_resampler_max_block_size(o
->thread_info
.resampler
);
776 if (qchunk
.length
> mbs
)
779 pa_resampler_run(o
->thread_info
.resampler
, &qchunk
, &rchunk
);
781 if (rchunk
.length
> 0) {
783 pa_memchunk_make_writable(&rchunk
, 0);
784 pa_volume_memchunk(&rchunk
, &o
->thread_info
.sample_spec
, &o
->volume_factor_source
);
791 pa_memblock_unref(rchunk
.memblock
);
794 pa_memblock_unref(qchunk
.memblock
);
795 pa_memblockq_drop(o
->thread_info
.delay_memblockq
, qchunk
.length
);
799 /* Called from thread context */
800 void pa_source_output_process_rewind(pa_source_output
*o
, size_t nbytes
/* in source sample spec */) {
802 pa_source_output_assert_ref(o
);
803 pa_source_output_assert_io_context(o
);
804 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
805 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
810 if (o
->process_rewind
) {
811 pa_assert(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
) == 0);
813 if (o
->thread_info
.resampler
)
814 nbytes
= pa_resampler_result(o
->thread_info
.resampler
, nbytes
);
816 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) nbytes
);
819 o
->process_rewind(o
, nbytes
);
821 if (o
->thread_info
.resampler
)
822 pa_resampler_reset(o
->thread_info
.resampler
);
825 pa_memblockq_rewind(o
->thread_info
.delay_memblockq
, nbytes
);
828 /* Called from thread context */
829 size_t pa_source_output_get_max_rewind(pa_source_output
*o
) {
830 pa_source_output_assert_ref(o
);
831 pa_source_output_assert_io_context(o
);
833 return o
->thread_info
.resampler
? pa_resampler_request(o
->thread_info
.resampler
, o
->source
->thread_info
.max_rewind
) : o
->source
->thread_info
.max_rewind
;
836 /* Called from thread context */
837 void pa_source_output_update_max_rewind(pa_source_output
*o
, size_t nbytes
/* in the source's sample spec */) {
838 pa_source_output_assert_ref(o
);
839 pa_source_output_assert_io_context(o
);
840 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
841 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
843 if (o
->update_max_rewind
)
844 o
->update_max_rewind(o
, o
->thread_info
.resampler
? pa_resampler_result(o
->thread_info
.resampler
, nbytes
) : nbytes
);
847 /* Called from thread context */
848 pa_usec_t
pa_source_output_set_requested_latency_within_thread(pa_source_output
*o
, pa_usec_t usec
) {
849 pa_source_output_assert_ref(o
);
850 pa_source_output_assert_io_context(o
);
852 if (!(o
->source
->flags
& PA_SOURCE_DYNAMIC_LATENCY
))
853 usec
= o
->source
->thread_info
.fixed_latency
;
855 if (usec
!= (pa_usec_t
) -1)
856 usec
= PA_CLAMP(usec
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
858 o
->thread_info
.requested_source_latency
= usec
;
859 pa_source_invalidate_requested_latency(o
->source
, TRUE
);
864 /* Called from main context */
865 pa_usec_t
pa_source_output_set_requested_latency(pa_source_output
*o
, pa_usec_t usec
) {
866 pa_source_output_assert_ref(o
);
867 pa_assert_ctl_context();
869 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
) && o
->source
) {
870 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
874 /* If this source output is not realized yet or is being moved, we
875 * have to touch the thread info data directly */
878 if (!(o
->source
->flags
& PA_SOURCE_DYNAMIC_LATENCY
))
879 usec
= pa_source_get_fixed_latency(o
->source
);
881 if (usec
!= (pa_usec_t
) -1) {
882 pa_usec_t min_latency
, max_latency
;
883 pa_source_get_latency_range(o
->source
, &min_latency
, &max_latency
);
884 usec
= PA_CLAMP(usec
, min_latency
, max_latency
);
888 o
->thread_info
.requested_source_latency
= usec
;
893 /* Called from main context */
894 pa_usec_t
pa_source_output_get_requested_latency(pa_source_output
*o
) {
895 pa_source_output_assert_ref(o
);
896 pa_assert_ctl_context();
898 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
) && o
->source
) {
900 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
904 /* If this source output is not realized yet or is being moved, we
905 * have to touch the thread info data directly */
907 return o
->thread_info
.requested_source_latency
;
910 /* Called from main context */
911 void pa_source_output_set_volume(pa_source_output
*o
, const pa_cvolume
*volume
, pa_bool_t save
, pa_bool_t absolute
) {
914 pa_source_output_assert_ref(o
);
915 pa_assert_ctl_context();
916 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
918 pa_assert(pa_cvolume_valid(volume
));
919 pa_assert(volume
->channels
== 1 || pa_cvolume_compatible(volume
, &o
->sample_spec
));
920 pa_assert(o
->volume_writable
);
922 if (!absolute
&& pa_source_flat_volume_enabled(o
->source
)) {
923 v
= o
->source
->reference_volume
;
924 pa_cvolume_remap(&v
, &o
->source
->channel_map
, &o
->channel_map
);
926 if (pa_cvolume_compatible(volume
, &o
->sample_spec
))
927 volume
= pa_sw_cvolume_multiply(&v
, &v
, volume
);
929 volume
= pa_sw_cvolume_multiply_scalar(&v
, &v
, pa_cvolume_max(volume
));
931 if (!pa_cvolume_compatible(volume
, &o
->sample_spec
)) {
933 volume
= pa_cvolume_scale(&v
, pa_cvolume_max(volume
));
937 if (pa_cvolume_equal(volume
, &o
->volume
)) {
938 o
->save_volume
= o
->save_volume
|| save
;
943 o
->save_volume
= save
;
945 if (pa_source_flat_volume_enabled(o
->source
)) {
946 /* We are in flat volume mode, so let's update all source input
947 * volumes and update the flat volume of the source */
949 pa_source_set_volume(o
->source
, NULL
, TRUE
, save
);
952 /* OK, we are in normal volume mode. The volume only affects
954 set_real_ratio(o
, volume
);
956 /* Copy the new soft_volume to the thread_info struct */
957 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_VOLUME
, NULL
, 0, NULL
) == 0);
960 /* The volume changed, let's tell people so */
961 if (o
->volume_changed
)
962 o
->volume_changed(o
);
964 /* The virtual volume changed, let's tell people so */
965 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
968 /* Called from main context */
969 static void set_real_ratio(pa_source_output
*o
, const pa_cvolume
*v
) {
970 pa_source_output_assert_ref(o
);
971 pa_assert_ctl_context();
972 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
973 pa_assert(!v
|| pa_cvolume_compatible(v
, &o
->sample_spec
));
975 /* This basically calculates:
978 * o->soft_volume := o->real_ratio * o->volume_factor */
983 pa_cvolume_reset(&o
->real_ratio
, o
->sample_spec
.channels
);
985 pa_sw_cvolume_multiply(&o
->soft_volume
, &o
->real_ratio
, &o
->volume_factor
);
986 /* We don't copy the data to the thread_info data. That's left for someone else to do */
989 /* Called from main or I/O context */
990 pa_bool_t
pa_source_output_is_passthrough(pa_source_output
*o
) {
991 pa_source_output_assert_ref(o
);
993 if (PA_UNLIKELY(!pa_format_info_is_pcm(o
->format
)))
996 if (PA_UNLIKELY(o
->flags
& PA_SOURCE_OUTPUT_PASSTHROUGH
))
1002 /* Called from main context */
1003 pa_bool_t
pa_source_output_is_volume_readable(pa_source_output
*o
) {
1004 pa_source_output_assert_ref(o
);
1005 pa_assert_ctl_context();
1007 return !pa_source_output_is_passthrough(o
);
1010 /* Called from main context */
1011 pa_cvolume
*pa_source_output_get_volume(pa_source_output
*o
, pa_cvolume
*volume
, pa_bool_t absolute
) {
1012 pa_source_output_assert_ref(o
);
1013 pa_assert_ctl_context();
1014 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1015 pa_assert(pa_source_output_is_volume_readable(o
));
1017 if (absolute
|| !pa_source_flat_volume_enabled(o
->source
))
1018 *volume
= o
->volume
;
1020 *volume
= o
->reference_ratio
;
1025 /* Called from main context */
1026 void pa_source_output_set_mute(pa_source_output
*o
, pa_bool_t mute
, pa_bool_t save
) {
1027 pa_source_output_assert_ref(o
);
1028 pa_assert_ctl_context();
1029 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1031 if (!o
->muted
== !mute
) {
1032 o
->save_muted
= o
->save_muted
|| mute
;
1037 o
->save_muted
= save
;
1039 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE
, NULL
, 0, NULL
) == 0);
1041 /* The mute status changed, let's tell people so */
1042 if (o
->mute_changed
)
1045 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1048 /* Called from main context */
1049 pa_bool_t
pa_source_output_get_mute(pa_source_output
*o
) {
1050 pa_source_output_assert_ref(o
);
1051 pa_assert_ctl_context();
1052 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1057 /* Called from main thread */
1058 void pa_source_output_update_proplist(pa_source_output
*o
, pa_update_mode_t mode
, pa_proplist
*p
) {
1059 pa_source_output_assert_ref(o
);
1060 pa_assert_ctl_context();
1063 pa_proplist_update(o
->proplist
, mode
, p
);
1065 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
1066 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
1067 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1071 /* Called from main context */
1072 void pa_source_output_cork(pa_source_output
*o
, pa_bool_t b
) {
1073 pa_source_output_assert_ref(o
);
1074 pa_assert_ctl_context();
1075 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1077 source_output_set_state(o
, b
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
);
1080 /* Called from main context */
1081 int pa_source_output_set_rate(pa_source_output
*o
, uint32_t rate
) {
1082 pa_source_output_assert_ref(o
);
1083 pa_assert_ctl_context();
1084 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1085 pa_return_val_if_fail(o
->thread_info
.resampler
, -PA_ERR_BADSTATE
);
1087 if (o
->sample_spec
.rate
== rate
)
1090 o
->sample_spec
.rate
= rate
;
1092 pa_asyncmsgq_post(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
, PA_UINT_TO_PTR(rate
), 0, NULL
, NULL
);
1094 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1098 /* Called from main context */
1099 void pa_source_output_set_name(pa_source_output
*o
, const char *name
) {
1101 pa_assert_ctl_context();
1102 pa_source_output_assert_ref(o
);
1104 if (!name
&& !pa_proplist_contains(o
->proplist
, PA_PROP_MEDIA_NAME
))
1107 old
= pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
);
1109 if (old
&& name
&& !strcmp(old
, name
))
1113 pa_proplist_sets(o
->proplist
, PA_PROP_MEDIA_NAME
, name
);
1115 pa_proplist_unset(o
->proplist
, PA_PROP_MEDIA_NAME
);
1117 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
1118 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
1119 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1123 /* Called from main context */
1124 pa_resample_method_t
pa_source_output_get_resample_method(pa_source_output
*o
) {
1125 pa_source_output_assert_ref(o
);
1126 pa_assert_ctl_context();
1128 return o
->actual_resample_method
;
1131 /* Called from main context */
1132 pa_bool_t
pa_source_output_may_move(pa_source_output
*o
) {
1133 pa_source_output_assert_ref(o
);
1134 pa_assert_ctl_context();
1135 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1137 if (o
->flags
& PA_SOURCE_OUTPUT_DONT_MOVE
)
1140 if (o
->direct_on_input
)
1146 /* Called from main context */
1147 pa_bool_t
pa_source_output_may_move_to(pa_source_output
*o
, pa_source
*dest
) {
1148 pa_source_output_assert_ref(o
);
1149 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1150 pa_source_assert_ref(dest
);
1152 if (dest
== o
->source
)
1155 if (!pa_source_output_may_move(o
))
1158 if (pa_idxset_size(dest
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
1159 pa_log_warn("Failed to move source output: too many outputs per source.");
1164 if (!o
->may_move_to(o
, dest
))
1170 /* Called from main context */
1171 int pa_source_output_start_move(pa_source_output
*o
) {
1175 pa_source_output_assert_ref(o
);
1176 pa_assert_ctl_context();
1177 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1178 pa_assert(o
->source
);
1180 if (!pa_source_output_may_move(o
))
1181 return -PA_ERR_NOTSUPPORTED
;
1183 if ((r
= pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START
], o
)) < 0)
1188 pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
);
1190 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
)
1191 pa_assert_se(origin
->n_corked
-- >= 1);
1193 if (pa_source_output_is_passthrough(o
))
1194 pa_source_leave_passthrough(o
->source
);
1196 if (pa_source_flat_volume_enabled(o
->source
))
1197 /* We might need to update the source's volume if we are in flat
1199 pa_source_set_volume(o
->source
, NULL
, FALSE
, FALSE
);
1201 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
1203 pa_source_update_status(o
->source
);
1206 pa_source_output_unref(o
);
1211 /* Called from main context. If it has an origin source that uses volume sharing,
1212 * then also the origin source and all streams connected to it need to update
1213 * their volume - this function does all that by using recursion. */
1214 static void update_volume_due_to_moving(pa_source_output
*o
, pa_source
*dest
) {
1215 pa_cvolume old_volume
;
1219 pa_assert(o
->source
); /* The destination source should already be set. */
1221 if (o
->destination_source
&& (o
->destination_source
->flags
& PA_SOURCE_SHARE_VOLUME_WITH_MASTER
)) {
1222 pa_source
*root_source
;
1223 pa_source_output
*destination_source_output
;
1226 root_source
= pa_source_get_master(o
->source
);
1228 if (PA_UNLIKELY(!root_source
))
1231 if (pa_source_flat_volume_enabled(o
->source
)) {
1232 /* Ok, so the origin source uses volume sharing, and flat volume is
1233 * enabled. The volume will have to be updated as follows:
1235 * o->volume := o->source->real_volume
1236 * (handled later by pa_source_set_volume)
1237 * o->reference_ratio := o->volume / o->source->reference_volume
1238 * (handled later by pa_source_set_volume)
1239 * o->real_ratio stays unchanged
1240 * (streams whose origin source uses volume sharing should
1241 * always have real_ratio of 0 dB)
1242 * o->soft_volume stays unchanged
1243 * (streams whose origin source uses volume sharing should
1244 * always have volume_factor as soft_volume, so no change
1245 * should be needed) */
1247 pa_assert(pa_cvolume_is_norm(&o
->real_ratio
));
1248 pa_assert(pa_cvolume_equal(&o
->soft_volume
, &o
->volume_factor
));
1250 /* Notifications will be sent by pa_source_set_volume(). */
1253 /* Ok, so the origin source uses volume sharing, and flat volume is
1254 * disabled. The volume will have to be updated as follows:
1257 * o->reference_ratio := 0 dB
1258 * o->real_ratio stays unchanged
1259 * (streams whose origin source uses volume sharing should
1260 * always have real_ratio of 0 dB)
1261 * o->soft_volume stays unchanged
1262 * (streams whose origin source uses volume sharing should
1263 * always have volume_factor as soft_volume, so no change
1264 * should be needed) */
1266 old_volume
= o
->volume
;
1267 pa_cvolume_reset(&o
->volume
, o
->volume
.channels
);
1268 pa_cvolume_reset(&o
->reference_ratio
, o
->reference_ratio
.channels
);
1269 pa_assert(pa_cvolume_is_norm(&o
->real_ratio
));
1270 pa_assert(pa_cvolume_equal(&o
->soft_volume
, &o
->volume_factor
));
1272 /* Notify others about the changed source output volume. */
1273 if (!pa_cvolume_equal(&o
->volume
, &old_volume
)) {
1274 if (o
->volume_changed
)
1275 o
->volume_changed(o
);
1277 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1281 /* Additionally, the origin source volume needs updating:
1283 * o->destination_source->reference_volume := root_source->reference_volume
1284 * o->destination_source->real_volume := root_source->real_volume
1285 * o->destination_source->soft_volume stays unchanged
1286 * (sources that use volume sharing should always have
1287 * soft_volume of 0 dB) */
1289 old_volume
= o
->destination_source
->reference_volume
;
1291 o
->destination_source
->reference_volume
= root_source
->reference_volume
;
1292 pa_cvolume_remap(&o
->destination_source
->reference_volume
, &root_source
->channel_map
, &o
->destination_source
->channel_map
);
1294 o
->destination_source
->real_volume
= root_source
->real_volume
;
1295 pa_cvolume_remap(&o
->destination_source
->real_volume
, &root_source
->channel_map
, &o
->destination_source
->channel_map
);
1297 pa_assert(pa_cvolume_is_norm(&o
->destination_source
->soft_volume
));
1299 /* Notify others about the changed source volume. If you wonder whether
1300 * o->destination_source->set_volume() should be called somewhere, that's not
1301 * the case, because sources that use volume sharing shouldn't have any
1302 * internal volume that set_volume() would update. If you wonder
1303 * whether the thread_info variables should be synced, yes, they
1304 * should, and it's done by the PA_SOURCE_MESSAGE_FINISH_MOVE message
1306 if (!pa_cvolume_equal(&o
->destination_source
->reference_volume
, &old_volume
))
1307 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->destination_source
->index
);
1309 /* Recursively update origin source outputs. */
1310 PA_IDXSET_FOREACH(destination_source_output
, o
->destination_source
->outputs
, idx
)
1311 update_volume_due_to_moving(destination_source_output
, dest
);
1314 old_volume
= o
->volume
;
1316 if (pa_source_flat_volume_enabled(o
->source
)) {
1317 /* Ok, so this is a regular stream, and flat volume is enabled. The
1318 * volume will have to be updated as follows:
1320 * o->volume := o->reference_ratio * o->source->reference_volume
1321 * o->reference_ratio stays unchanged
1322 * o->real_ratio := o->volume / o->source->real_volume
1323 * (handled later by pa_source_set_volume)
1324 * o->soft_volume := o->real_ratio * o->volume_factor
1325 * (handled later by pa_source_set_volume) */
1327 o
->volume
= o
->source
->reference_volume
;
1328 pa_cvolume_remap(&o
->volume
, &o
->source
->channel_map
, &o
->channel_map
);
1329 pa_sw_cvolume_multiply(&o
->volume
, &o
->volume
, &o
->reference_ratio
);
1332 /* Ok, so this is a regular stream, and flat volume is disabled.
1333 * The volume will have to be updated as follows:
1335 * o->volume := o->reference_ratio
1336 * o->reference_ratio stays unchanged
1337 * o->real_ratio := o->reference_ratio
1338 * o->soft_volume := o->real_ratio * o->volume_factor */
1340 o
->volume
= o
->reference_ratio
;
1341 o
->real_ratio
= o
->reference_ratio
;
1342 pa_sw_cvolume_multiply(&o
->soft_volume
, &o
->real_ratio
, &o
->volume_factor
);
1345 /* Notify others about the changed source output volume. */
1346 if (!pa_cvolume_equal(&o
->volume
, &old_volume
)) {
1347 /* XXX: In case o->source has flat volume enabled, then real_ratio
1348 * and soft_volume are not updated yet. Let's hope that the
1349 * callback implementation doesn't care about those variables... */
1350 if (o
->volume_changed
)
1351 o
->volume_changed(o
);
1353 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1357 /* If o->source == dest, then recursion has finished, and we can finally call
1358 * pa_source_set_volume(), which will do the rest of the updates. */
1359 if ((o
->source
== dest
) && pa_source_flat_volume_enabled(o
->source
))
1360 pa_source_set_volume(o
->source
, NULL
, FALSE
, o
->save_volume
);
1363 /* Called from main context */
1364 int pa_source_output_finish_move(pa_source_output
*o
, pa_source
*dest
, pa_bool_t save
) {
1365 pa_resampler
*new_resampler
;
1367 pa_source_output_assert_ref(o
);
1368 pa_assert_ctl_context();
1369 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1370 pa_assert(!o
->source
);
1371 pa_source_assert_ref(dest
);
1373 if (!pa_source_output_may_move_to(o
, dest
))
1374 return -PA_ERR_NOTSUPPORTED
;
1376 if (pa_source_output_is_passthrough(o
) && !pa_source_check_format(dest
, o
->format
)) {
1377 pa_proplist
*p
= pa_proplist_new();
1378 pa_log_debug("New source doesn't support stream format, sending format-changed and killing");
1379 /* Tell the client what device we want to be on if it is going to
1381 pa_proplist_sets(p
, "device", dest
->name
);
1382 pa_source_output_send_event(o
, PA_STREAM_EVENT_FORMAT_LOST
, p
);
1383 pa_proplist_free(p
);
1384 return -PA_ERR_NOTSUPPORTED
;
1387 if (o
->thread_info
.resampler
&&
1388 pa_sample_spec_equal(pa_resampler_input_sample_spec(o
->thread_info
.resampler
), &dest
->sample_spec
) &&
1389 pa_channel_map_equal(pa_resampler_input_channel_map(o
->thread_info
.resampler
), &dest
->channel_map
))
1391 /* Try to reuse the old resampler if possible */
1392 new_resampler
= o
->thread_info
.resampler
;
1394 else if (!pa_source_output_is_passthrough(o
) &&
1395 ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
1396 !pa_sample_spec_equal(&o
->sample_spec
, &dest
->sample_spec
) ||
1397 !pa_channel_map_equal(&o
->channel_map
, &dest
->channel_map
))) {
1399 /* Okay, we need a new resampler for the new source */
1401 if (!(new_resampler
= pa_resampler_new(
1403 &dest
->sample_spec
, &dest
->channel_map
,
1404 &o
->sample_spec
, &o
->channel_map
,
1405 o
->requested_resample_method
,
1406 ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
1407 ((o
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
1408 (o
->core
->disable_remixing
|| (o
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0)))) {
1409 pa_log_warn("Unsupported resampling operation.");
1410 return -PA_ERR_NOTSUPPORTED
;
1413 new_resampler
= NULL
;
1419 o
->save_source
= save
;
1420 pa_idxset_put(o
->source
->outputs
, pa_source_output_ref(o
), NULL
);
1422 pa_cvolume_remap(&o
->volume_factor_source
, &o
->channel_map
, &o
->source
->channel_map
);
1424 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
)
1425 o
->source
->n_corked
++;
1427 /* Replace resampler */
1428 if (new_resampler
!= o
->thread_info
.resampler
) {
1430 if (o
->thread_info
.resampler
)
1431 pa_resampler_free(o
->thread_info
.resampler
);
1432 o
->thread_info
.resampler
= new_resampler
;
1434 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
1436 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
1437 "source output delay_memblockq",
1439 MEMBLOCKQ_MAXLENGTH
,
1441 &o
->source
->sample_spec
,
1445 &o
->source
->silence
);
1446 o
->actual_resample_method
= new_resampler
? pa_resampler_get_method(new_resampler
) : PA_RESAMPLER_INVALID
;
1449 pa_source_update_status(dest
);
1451 update_volume_due_to_moving(o
, dest
);
1453 if (pa_source_output_is_passthrough(o
))
1454 pa_source_enter_passthrough(o
->source
);
1456 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
1458 pa_log_debug("Successfully moved source output %i to %s.", o
->index
, dest
->name
);
1460 /* Notify everyone */
1461 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH
], o
);
1462 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1467 /* Called from main context */
1468 void pa_source_output_fail_move(pa_source_output
*o
) {
1470 pa_source_output_assert_ref(o
);
1471 pa_assert_ctl_context();
1472 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1473 pa_assert(!o
->source
);
1475 /* Check if someone wants this source output? */
1476 if (pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL
], o
) == PA_HOOK_STOP
)
1482 pa_source_output_kill(o
);
1485 /* Called from main context */
1486 int pa_source_output_move_to(pa_source_output
*o
, pa_source
*dest
, pa_bool_t save
) {
1489 pa_source_output_assert_ref(o
);
1490 pa_assert_ctl_context();
1491 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1492 pa_assert(o
->source
);
1493 pa_source_assert_ref(dest
);
1495 if (dest
== o
->source
)
1498 if (!pa_source_output_may_move_to(o
, dest
))
1499 return -PA_ERR_NOTSUPPORTED
;
1501 pa_source_output_ref(o
);
1503 if ((r
= pa_source_output_start_move(o
)) < 0) {
1504 pa_source_output_unref(o
);
1508 if ((r
= pa_source_output_finish_move(o
, dest
, save
)) < 0) {
1509 pa_source_output_fail_move(o
);
1510 pa_source_output_unref(o
);
1514 pa_source_output_unref(o
);
1519 /* Called from IO thread context */
1520 void pa_source_output_set_state_within_thread(pa_source_output
*o
, pa_source_output_state_t state
) {
1521 pa_source_output_assert_ref(o
);
1522 pa_source_output_assert_io_context(o
);
1524 if (state
== o
->thread_info
.state
)
1527 if (o
->state_change
)
1528 o
->state_change(o
, state
);
1530 o
->thread_info
.state
= state
;
1533 /* Called from IO thread context, except when it is not */
1534 int pa_source_output_process_msg(pa_msgobject
*mo
, int code
, void *userdata
, int64_t offset
, pa_memchunk
* chunk
) {
1535 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
1536 pa_source_output_assert_ref(o
);
1540 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
: {
1541 pa_usec_t
*r
= userdata
;
1543 r
[0] += pa_bytes_to_usec(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
), &o
->source
->sample_spec
);
1544 r
[1] += pa_source_get_latency_within_thread(o
->source
);
1549 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
:
1551 o
->thread_info
.sample_spec
.rate
= PA_PTR_TO_UINT(userdata
);
1552 pa_resampler_set_output_rate(o
->thread_info
.resampler
, PA_PTR_TO_UINT(userdata
));
1555 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE
:
1557 pa_source_output_set_state_within_thread(o
, PA_PTR_TO_UINT(userdata
));
1561 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
: {
1562 pa_usec_t
*usec
= userdata
;
1564 *usec
= pa_source_output_set_requested_latency_within_thread(o
, *usec
);
1569 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
: {
1570 pa_usec_t
*r
= userdata
;
1572 *r
= o
->thread_info
.requested_source_latency
;
1576 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_VOLUME
:
1577 if (!pa_cvolume_equal(&o
->thread_info
.soft_volume
, &o
->soft_volume
)) {
1578 o
->thread_info
.soft_volume
= o
->soft_volume
;
1582 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE
:
1583 if (o
->thread_info
.muted
!= o
->muted
) {
1584 o
->thread_info
.muted
= o
->muted
;
1589 return -PA_ERR_NOTIMPLEMENTED
;
1592 /* Called from main context */
1593 void pa_source_output_send_event(pa_source_output
*o
, const char *event
, pa_proplist
*data
) {
1594 pa_proplist
*pl
= NULL
;
1595 pa_source_output_send_event_hook_data hook_data
;
1597 pa_source_output_assert_ref(o
);
1598 pa_assert_ctl_context();
1605 data
= pl
= pa_proplist_new();
1607 hook_data
.source_output
= o
;
1608 hook_data
.data
= data
;
1609 hook_data
.event
= event
;
1611 if (pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_SEND_EVENT
], &hook_data
) < 0)
1614 o
->send_event(o
, event
, data
);
1618 pa_proplist_free(pl
);