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 */
302 if (!pa_format_info_is_pcm(data
->format
)) {
303 data
->volume_is_set
= FALSE
;
304 data
->volume_factor_is_set
= FALSE
;
307 if (!data
->volume_is_set
) {
308 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
309 data
->volume_is_absolute
= FALSE
;
310 data
->save_volume
= FALSE
;
313 pa_return_val_if_fail(pa_cvolume_compatible(&data
->volume
, &data
->sample_spec
), -PA_ERR_INVALID
);
315 if (!data
->volume_factor_is_set
)
316 pa_cvolume_reset(&data
->volume_factor
, data
->sample_spec
.channels
);
318 pa_return_val_if_fail(pa_cvolume_compatible(&data
->volume_factor
, &data
->sample_spec
), -PA_ERR_INVALID
);
320 if (!data
->volume_factor_source_is_set
)
321 pa_cvolume_reset(&data
->volume_factor_source
, data
->source
->sample_spec
.channels
);
323 pa_return_val_if_fail(pa_cvolume_compatible(&data
->volume_factor_source
, &data
->source
->sample_spec
), -PA_ERR_INVALID
);
325 if (!data
->muted_is_set
)
328 if (data
->flags
& PA_SOURCE_OUTPUT_FIX_FORMAT
)
329 data
->sample_spec
.format
= data
->source
->sample_spec
.format
;
331 if (data
->flags
& PA_SOURCE_OUTPUT_FIX_RATE
)
332 data
->sample_spec
.rate
= data
->source
->sample_spec
.rate
;
334 original_cm
= data
->channel_map
;
336 if (data
->flags
& PA_SOURCE_OUTPUT_FIX_CHANNELS
) {
337 data
->sample_spec
.channels
= data
->source
->sample_spec
.channels
;
338 data
->channel_map
= data
->source
->channel_map
;
341 pa_assert(pa_sample_spec_valid(&data
->sample_spec
));
342 pa_assert(pa_channel_map_valid(&data
->channel_map
));
344 /* Due to the fixing of the sample spec the volume might not match anymore */
345 pa_cvolume_remap(&data
->volume
, &original_cm
, &data
->channel_map
);
347 if (data
->resample_method
== PA_RESAMPLER_INVALID
)
348 data
->resample_method
= core
->resample_method
;
350 pa_return_val_if_fail(data
->resample_method
< PA_RESAMPLER_MAX
, -PA_ERR_INVALID
);
352 if ((r
= pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE
], data
)) < 0)
355 if ((data
->flags
& PA_SOURCE_OUTPUT_NO_CREATE_ON_SUSPEND
) &&
356 pa_source_get_state(data
->source
) == PA_SOURCE_SUSPENDED
) {
357 pa_log("Failed to create source output: source is suspended.");
358 return -PA_ERR_BADSTATE
;
361 if (pa_idxset_size(data
->source
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
362 pa_log("Failed to create source output: too many outputs per source.");
363 return -PA_ERR_TOOLARGE
;
366 if ((data
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
367 !pa_sample_spec_equal(&data
->sample_spec
, &data
->source
->sample_spec
) ||
368 !pa_channel_map_equal(&data
->channel_map
, &data
->source
->channel_map
)) {
370 if (!pa_source_output_new_data_is_passthrough(data
)) /* no resampler for passthrough content */
371 if (!(resampler
= pa_resampler_new(
373 &data
->source
->sample_spec
, &data
->source
->channel_map
,
374 &data
->sample_spec
, &data
->channel_map
,
375 data
->resample_method
,
376 ((data
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
377 ((data
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
378 (core
->disable_remixing
|| (data
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0) |
379 (core
->disable_lfe_remixing
? PA_RESAMPLER_NO_LFE
: 0)))) {
380 pa_log_warn("Unsupported resampling operation.");
381 return -PA_ERR_NOTSUPPORTED
;
385 o
= pa_msgobject_new(pa_source_output
);
386 o
->parent
.parent
.free
= source_output_free
;
387 o
->parent
.process_msg
= pa_source_output_process_msg
;
390 o
->state
= PA_SOURCE_OUTPUT_INIT
;
391 o
->flags
= data
->flags
;
392 o
->proplist
= pa_proplist_copy(data
->proplist
);
393 o
->driver
= pa_xstrdup(pa_path_get_filename(data
->driver
));
394 o
->module
= data
->module
;
395 o
->source
= data
->source
;
396 o
->destination_source
= data
->destination_source
;
397 o
->client
= data
->client
;
399 o
->requested_resample_method
= data
->resample_method
;
400 o
->actual_resample_method
= resampler
? pa_resampler_get_method(resampler
) : PA_RESAMPLER_INVALID
;
401 o
->sample_spec
= data
->sample_spec
;
402 o
->channel_map
= data
->channel_map
;
403 o
->format
= pa_format_info_copy(data
->format
);
405 if (!data
->volume_is_absolute
&& pa_source_flat_volume_enabled(o
->source
)) {
408 /* When the 'absolute' bool is not set then we'll treat the volume
409 * as relative to the source volume even in flat volume mode */
410 remapped
= data
->source
->reference_volume
;
411 pa_cvolume_remap(&remapped
, &data
->source
->channel_map
, &data
->channel_map
);
412 pa_sw_cvolume_multiply(&o
->volume
, &data
->volume
, &remapped
);
414 o
->volume
= data
->volume
;
416 o
->volume_factor
= data
->volume_factor
;
417 o
->volume_factor_source
= data
->volume_factor_source
;
418 o
->real_ratio
= o
->reference_ratio
= data
->volume
;
419 pa_cvolume_reset(&o
->soft_volume
, o
->sample_spec
.channels
);
420 pa_cvolume_reset(&o
->real_ratio
, o
->sample_spec
.channels
);
421 o
->volume_writable
= data
->volume_writable
;
422 o
->save_volume
= data
->save_volume
;
423 o
->save_source
= data
->save_source
;
424 o
->save_muted
= data
->save_muted
;
426 o
->muted
= data
->muted
;
428 o
->direct_on_input
= data
->direct_on_input
;
433 o
->thread_info
.state
= o
->state
;
434 o
->thread_info
.attached
= FALSE
;
435 o
->thread_info
.sample_spec
= o
->sample_spec
;
436 o
->thread_info
.resampler
= resampler
;
437 o
->thread_info
.soft_volume
= o
->soft_volume
;
438 o
->thread_info
.muted
= o
->muted
;
439 o
->thread_info
.requested_source_latency
= (pa_usec_t
) -1;
440 o
->thread_info
.direct_on_input
= o
->direct_on_input
;
442 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
446 pa_frame_size(&o
->source
->sample_spec
),
450 &o
->source
->silence
);
452 pa_assert_se(pa_idxset_put(core
->source_outputs
, o
, &o
->index
) == 0);
453 pa_assert_se(pa_idxset_put(o
->source
->outputs
, pa_source_output_ref(o
), NULL
) == 0);
456 pa_assert_se(pa_idxset_put(o
->client
->source_outputs
, o
, NULL
) >= 0);
458 if (o
->direct_on_input
)
459 pa_assert_se(pa_idxset_put(o
->direct_on_input
->direct_outputs
, o
, NULL
) == 0);
461 pt
= pa_proplist_to_string_sep(o
->proplist
, "\n ");
462 pa_log_info("Created output %u \"%s\" on %s with sample spec %s and channel map %s\n %s",
464 pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)),
466 pa_sample_spec_snprint(st
, sizeof(st
), &o
->sample_spec
),
467 pa_channel_map_snprint(cm
, sizeof(cm
), &o
->channel_map
),
471 /* Don't forget to call pa_source_output_put! */
477 /* Called from main context */
478 static void update_n_corked(pa_source_output
*o
, pa_source_output_state_t state
) {
480 pa_assert_ctl_context();
485 if (o
->state
== PA_SOURCE_OUTPUT_CORKED
&& state
!= PA_SOURCE_OUTPUT_CORKED
)
486 pa_assert_se(o
->source
->n_corked
-- >= 1);
487 else if (o
->state
!= PA_SOURCE_OUTPUT_CORKED
&& state
== PA_SOURCE_OUTPUT_CORKED
)
488 o
->source
->n_corked
++;
491 /* Called from main context */
492 static void source_output_set_state(pa_source_output
*o
, pa_source_output_state_t state
) {
495 pa_assert_ctl_context();
497 if (o
->state
== state
)
500 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);
502 update_n_corked(o
, state
);
505 if (state
!= PA_SOURCE_OUTPUT_UNLINKED
) {
506 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED
], o
);
508 if (PA_SOURCE_OUTPUT_IS_LINKED(state
))
509 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
512 pa_source_update_status(o
->source
);
515 /* Called from main context */
516 void pa_source_output_unlink(pa_source_output
*o
) {
519 pa_assert_ctl_context();
521 /* See pa_sink_unlink() for a couple of comments how this function
524 pa_source_output_ref(o
);
526 linked
= PA_SOURCE_OUTPUT_IS_LINKED(o
->state
);
529 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK
], o
);
531 if (o
->direct_on_input
)
532 pa_idxset_remove_by_data(o
->direct_on_input
->direct_outputs
, o
, NULL
);
534 pa_idxset_remove_by_data(o
->core
->source_outputs
, o
, NULL
);
537 if (pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
))
538 pa_source_output_unref(o
);
541 pa_idxset_remove_by_data(o
->client
->source_outputs
, o
, NULL
);
543 update_n_corked(o
, PA_SOURCE_OUTPUT_UNLINKED
);
544 o
->state
= PA_SOURCE_OUTPUT_UNLINKED
;
546 if (linked
&& o
->source
) {
547 /* We might need to update the source's volume if we are in flat volume mode. */
548 if (pa_source_flat_volume_enabled(o
->source
))
549 pa_source_set_volume(o
->source
, NULL
, FALSE
, FALSE
);
551 if (o
->source
->asyncmsgq
)
552 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
558 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, o
->index
);
559 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST
], o
);
563 pa_source_update_status(o
->source
);
567 pa_core_maybe_vacuum(o
->core
);
569 pa_source_output_unref(o
);
572 /* Called from main context */
573 static void source_output_free(pa_object
* mo
) {
574 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
577 pa_assert_ctl_context();
578 pa_assert(pa_source_output_refcnt(o
) == 0);
580 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
581 pa_source_output_unlink(o
);
583 pa_log_info("Freeing output %u \"%s\"", o
->index
, pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)));
585 if (o
->thread_info
.delay_memblockq
)
586 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
588 if (o
->thread_info
.resampler
)
589 pa_resampler_free(o
->thread_info
.resampler
);
592 pa_format_info_free(o
->format
);
595 pa_proplist_free(o
->proplist
);
601 /* Called from main context */
602 void pa_source_output_put(pa_source_output
*o
) {
603 pa_source_output_state_t state
;
605 pa_source_output_assert_ref(o
);
606 pa_assert_ctl_context();
608 pa_assert(o
->state
== PA_SOURCE_OUTPUT_INIT
);
610 /* The following fields must be initialized properly */
614 state
= o
->flags
& PA_SOURCE_OUTPUT_START_CORKED
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
;
616 update_n_corked(o
, state
);
619 /* We might need to update the source's volume if we are in flat volume mode. */
620 if (pa_source_flat_volume_enabled(o
->source
))
621 pa_source_set_volume(o
->source
, NULL
, FALSE
, o
->save_volume
);
623 if (o
->destination_source
&& (o
->destination_source
->flags
& PA_SOURCE_SHARE_VOLUME_WITH_MASTER
)) {
624 pa_assert(pa_cvolume_is_norm(&o
->volume
));
625 pa_assert(pa_cvolume_is_norm(&o
->reference_ratio
));
628 set_real_ratio(o
, &o
->volume
);
631 o
->thread_info
.soft_volume
= o
->soft_volume
;
632 o
->thread_info
.muted
= o
->muted
;
634 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
636 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
, o
->index
);
637 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PUT
], o
);
639 pa_source_update_status(o
->source
);
642 /* Called from main context */
643 void pa_source_output_kill(pa_source_output
*o
) {
644 pa_source_output_assert_ref(o
);
645 pa_assert_ctl_context();
646 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
651 /* Called from main context */
652 pa_usec_t
pa_source_output_get_latency(pa_source_output
*o
, pa_usec_t
*source_latency
) {
653 pa_usec_t r
[2] = { 0, 0 };
655 pa_source_output_assert_ref(o
);
656 pa_assert_ctl_context();
657 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
659 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
, r
, 0, NULL
) == 0);
662 r
[0] += o
->get_latency(o
);
665 *source_latency
= r
[1];
670 /* Called from thread context */
671 void pa_source_output_push(pa_source_output
*o
, const pa_memchunk
*chunk
) {
672 pa_bool_t need_volume_factor_source
;
673 pa_bool_t volume_is_norm
;
675 size_t limit
, mbs
= 0;
677 pa_source_output_assert_ref(o
);
678 pa_source_output_assert_io_context(o
);
679 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
681 pa_assert(pa_frame_aligned(chunk
->length
, &o
->source
->sample_spec
));
683 if (!o
->push
|| o
->thread_info
.state
== PA_SOURCE_OUTPUT_CORKED
)
686 pa_assert(o
->thread_info
.state
== PA_SOURCE_OUTPUT_RUNNING
);
688 if (pa_memblockq_push(o
->thread_info
.delay_memblockq
, chunk
) < 0) {
689 pa_log_debug("Delay queue overflow!");
690 pa_memblockq_seek(o
->thread_info
.delay_memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
, TRUE
);
693 limit
= o
->process_rewind
? 0 : o
->source
->thread_info
.max_rewind
;
695 volume_is_norm
= pa_cvolume_is_norm(&o
->thread_info
.soft_volume
) && !o
->thread_info
.muted
;
696 need_volume_factor_source
= !pa_cvolume_is_norm(&o
->volume_factor_source
);
698 if (limit
> 0 && o
->source
->monitor_of
) {
702 /* Hmm, check the latency for knowing how much of the buffered
703 * data is actually still unplayed and might hence still
704 * change. This is suboptimal. Ideally we'd have a call like
705 * pa_sink_get_changeable_size() or so that tells us how much
706 * of the queued data is actually still changeable. Hence
709 latency
= pa_sink_get_latency_within_thread(o
->source
->monitor_of
);
711 n
= pa_usec_to_bytes(latency
, &o
->source
->sample_spec
);
717 /* Implement the delay queue */
718 while ((length
= pa_memblockq_get_length(o
->thread_info
.delay_memblockq
)) > limit
) {
720 pa_bool_t nvfs
= need_volume_factor_source
;
724 pa_assert_se(pa_memblockq_peek(o
->thread_info
.delay_memblockq
, &qchunk
) >= 0);
726 if (qchunk
.length
> length
)
727 qchunk
.length
= length
;
729 pa_assert(qchunk
.length
> 0);
731 /* It might be necessary to adjust the volume here */
732 if (!volume_is_norm
) {
733 pa_memchunk_make_writable(&qchunk
, 0);
735 if (o
->thread_info
.muted
) {
736 pa_silence_memchunk(&qchunk
, &o
->thread_info
.sample_spec
);
739 } else if (!o
->thread_info
.resampler
&& nvfs
) {
742 /* If we don't need a resampler we can merge the
743 * post and the pre volume adjustment into one */
745 pa_sw_cvolume_multiply(&v
, &o
->thread_info
.soft_volume
, &o
->volume_factor_source
);
746 pa_volume_memchunk(&qchunk
, &o
->thread_info
.sample_spec
, &v
);
750 pa_volume_memchunk(&qchunk
, &o
->thread_info
.sample_spec
, &o
->thread_info
.soft_volume
);
753 if (!o
->thread_info
.resampler
) {
755 pa_memchunk_make_writable(&qchunk
, 0);
756 pa_volume_memchunk(&qchunk
, &o
->source
->sample_spec
, &o
->volume_factor_source
);
764 mbs
= pa_resampler_max_block_size(o
->thread_info
.resampler
);
766 if (qchunk
.length
> mbs
)
769 pa_resampler_run(o
->thread_info
.resampler
, &qchunk
, &rchunk
);
771 if (rchunk
.length
> 0) {
773 pa_memchunk_make_writable(&rchunk
, 0);
774 pa_volume_memchunk(&rchunk
, &o
->source
->sample_spec
, &o
->volume_factor_source
);
781 pa_memblock_unref(rchunk
.memblock
);
784 pa_memblock_unref(qchunk
.memblock
);
785 pa_memblockq_drop(o
->thread_info
.delay_memblockq
, qchunk
.length
);
789 /* Called from thread context */
790 void pa_source_output_process_rewind(pa_source_output
*o
, size_t nbytes
/* in source sample spec */) {
792 pa_source_output_assert_ref(o
);
793 pa_source_output_assert_io_context(o
);
794 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
795 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
800 if (o
->process_rewind
) {
801 pa_assert(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
) == 0);
803 if (o
->thread_info
.resampler
)
804 nbytes
= pa_resampler_result(o
->thread_info
.resampler
, nbytes
);
806 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) nbytes
);
809 o
->process_rewind(o
, nbytes
);
811 if (o
->thread_info
.resampler
)
812 pa_resampler_reset(o
->thread_info
.resampler
);
815 pa_memblockq_rewind(o
->thread_info
.delay_memblockq
, nbytes
);
818 /* Called from thread context */
819 size_t pa_source_output_get_max_rewind(pa_source_output
*o
) {
820 pa_source_output_assert_ref(o
);
821 pa_source_output_assert_io_context(o
);
823 return o
->thread_info
.resampler
? pa_resampler_request(o
->thread_info
.resampler
, o
->source
->thread_info
.max_rewind
) : o
->source
->thread_info
.max_rewind
;
826 /* Called from thread context */
827 void pa_source_output_update_max_rewind(pa_source_output
*o
, size_t nbytes
/* in the source's sample spec */) {
828 pa_source_output_assert_ref(o
);
829 pa_source_output_assert_io_context(o
);
830 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
831 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
833 if (o
->update_max_rewind
)
834 o
->update_max_rewind(o
, o
->thread_info
.resampler
? pa_resampler_result(o
->thread_info
.resampler
, nbytes
) : nbytes
);
837 /* Called from thread context */
838 pa_usec_t
pa_source_output_set_requested_latency_within_thread(pa_source_output
*o
, pa_usec_t usec
) {
839 pa_source_output_assert_ref(o
);
840 pa_source_output_assert_io_context(o
);
842 if (!(o
->source
->flags
& PA_SOURCE_DYNAMIC_LATENCY
))
843 usec
= o
->source
->thread_info
.fixed_latency
;
845 if (usec
!= (pa_usec_t
) -1)
846 usec
= PA_CLAMP(usec
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
848 o
->thread_info
.requested_source_latency
= usec
;
849 pa_source_invalidate_requested_latency(o
->source
, TRUE
);
854 /* Called from main context */
855 pa_usec_t
pa_source_output_set_requested_latency(pa_source_output
*o
, pa_usec_t usec
) {
856 pa_source_output_assert_ref(o
);
857 pa_assert_ctl_context();
859 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
) && o
->source
) {
860 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
864 /* If this source output is not realized yet or is being moved, we
865 * have to touch the thread info data directly */
868 if (!(o
->source
->flags
& PA_SOURCE_DYNAMIC_LATENCY
))
869 usec
= pa_source_get_fixed_latency(o
->source
);
871 if (usec
!= (pa_usec_t
) -1) {
872 pa_usec_t min_latency
, max_latency
;
873 pa_source_get_latency_range(o
->source
, &min_latency
, &max_latency
);
874 usec
= PA_CLAMP(usec
, min_latency
, max_latency
);
878 o
->thread_info
.requested_source_latency
= usec
;
883 /* Called from main context */
884 pa_usec_t
pa_source_output_get_requested_latency(pa_source_output
*o
) {
885 pa_source_output_assert_ref(o
);
886 pa_assert_ctl_context();
888 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
) && o
->source
) {
890 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
894 /* If this source output is not realized yet or is being moved, we
895 * have to touch the thread info data directly */
897 return o
->thread_info
.requested_source_latency
;
900 /* Called from main context */
901 void pa_source_output_set_volume(pa_source_output
*o
, const pa_cvolume
*volume
, pa_bool_t save
, pa_bool_t absolute
) {
904 pa_source_output_assert_ref(o
);
905 pa_assert_ctl_context();
906 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
908 pa_assert(pa_cvolume_valid(volume
));
909 pa_assert(volume
->channels
== 1 || pa_cvolume_compatible(volume
, &o
->sample_spec
));
910 pa_assert(o
->volume_writable
);
912 if (!absolute
&& pa_source_flat_volume_enabled(o
->source
)) {
913 v
= o
->source
->reference_volume
;
914 pa_cvolume_remap(&v
, &o
->source
->channel_map
, &o
->channel_map
);
916 if (pa_cvolume_compatible(volume
, &o
->sample_spec
))
917 volume
= pa_sw_cvolume_multiply(&v
, &v
, volume
);
919 volume
= pa_sw_cvolume_multiply_scalar(&v
, &v
, pa_cvolume_max(volume
));
921 if (!pa_cvolume_compatible(volume
, &o
->sample_spec
)) {
923 volume
= pa_cvolume_scale(&v
, pa_cvolume_max(volume
));
927 if (pa_cvolume_equal(volume
, &o
->volume
)) {
928 o
->save_volume
= o
->save_volume
|| save
;
933 o
->save_volume
= save
;
935 if (pa_source_flat_volume_enabled(o
->source
)) {
936 /* We are in flat volume mode, so let's update all source input
937 * volumes and update the flat volume of the source */
939 pa_source_set_volume(o
->source
, NULL
, TRUE
, save
);
942 /* OK, we are in normal volume mode. The volume only affects
944 set_real_ratio(o
, volume
);
946 /* Copy the new soft_volume to the thread_info struct */
947 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_VOLUME
, NULL
, 0, NULL
) == 0);
950 /* The volume changed, let's tell people so */
951 if (o
->volume_changed
)
952 o
->volume_changed(o
);
954 /* The virtual volume changed, let's tell people so */
955 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
958 /* Called from main context */
959 static void set_real_ratio(pa_source_output
*o
, const pa_cvolume
*v
) {
960 pa_source_output_assert_ref(o
);
961 pa_assert_ctl_context();
962 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
963 pa_assert(!v
|| pa_cvolume_compatible(v
, &o
->sample_spec
));
965 /* This basically calculates:
968 * o->soft_volume := o->real_ratio * o->volume_factor */
973 pa_cvolume_reset(&o
->real_ratio
, o
->sample_spec
.channels
);
975 pa_sw_cvolume_multiply(&o
->soft_volume
, &o
->real_ratio
, &o
->volume_factor
);
976 /* We don't copy the data to the thread_info data. That's left for someone else to do */
979 /* Called from main or I/O context */
980 pa_bool_t
pa_source_output_is_passthrough(pa_source_output
*o
) {
981 pa_source_output_assert_ref(o
);
983 if (PA_UNLIKELY(!pa_format_info_is_pcm(o
->format
)))
986 if (PA_UNLIKELY(o
->flags
& PA_SOURCE_OUTPUT_PASSTHROUGH
))
992 /* Called from main context */
993 pa_bool_t
pa_source_output_is_volume_readable(pa_source_output
*o
) {
994 pa_source_output_assert_ref(o
);
995 pa_assert_ctl_context();
997 return !pa_source_output_is_passthrough(o
);
1000 /* Called from main context */
1001 pa_cvolume
*pa_source_output_get_volume(pa_source_output
*o
, pa_cvolume
*volume
, pa_bool_t absolute
) {
1002 pa_source_output_assert_ref(o
);
1003 pa_assert_ctl_context();
1004 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1005 pa_assert(pa_source_output_is_volume_readable(o
));
1007 if (absolute
|| !pa_source_flat_volume_enabled(o
->source
))
1008 *volume
= o
->volume
;
1010 *volume
= o
->reference_ratio
;
1015 /* Called from main context */
1016 void pa_source_output_set_mute(pa_source_output
*o
, pa_bool_t mute
, pa_bool_t save
) {
1017 pa_source_output_assert_ref(o
);
1018 pa_assert_ctl_context();
1019 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1021 if (!o
->muted
== !mute
) {
1022 o
->save_muted
= o
->save_muted
|| mute
;
1027 o
->save_muted
= save
;
1029 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE
, NULL
, 0, NULL
) == 0);
1031 /* The mute status changed, let's tell people so */
1032 if (o
->mute_changed
)
1035 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1038 /* Called from main context */
1039 pa_bool_t
pa_source_output_get_mute(pa_source_output
*o
) {
1040 pa_source_output_assert_ref(o
);
1041 pa_assert_ctl_context();
1042 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1047 /* Called from main thread */
1048 void pa_source_output_update_proplist(pa_source_output
*o
, pa_update_mode_t mode
, pa_proplist
*p
) {
1049 pa_source_output_assert_ref(o
);
1050 pa_assert_ctl_context();
1053 pa_proplist_update(o
->proplist
, mode
, p
);
1055 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
1056 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
1057 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1061 /* Called from main context */
1062 void pa_source_output_cork(pa_source_output
*o
, pa_bool_t b
) {
1063 pa_source_output_assert_ref(o
);
1064 pa_assert_ctl_context();
1065 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1067 source_output_set_state(o
, b
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
);
1070 /* Called from main context */
1071 int pa_source_output_set_rate(pa_source_output
*o
, uint32_t rate
) {
1072 pa_source_output_assert_ref(o
);
1073 pa_assert_ctl_context();
1074 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1075 pa_return_val_if_fail(o
->thread_info
.resampler
, -PA_ERR_BADSTATE
);
1077 if (o
->sample_spec
.rate
== rate
)
1080 o
->sample_spec
.rate
= rate
;
1082 pa_asyncmsgq_post(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
, PA_UINT_TO_PTR(rate
), 0, NULL
, NULL
);
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_set_name(pa_source_output
*o
, const char *name
) {
1091 pa_assert_ctl_context();
1092 pa_source_output_assert_ref(o
);
1094 if (!name
&& !pa_proplist_contains(o
->proplist
, PA_PROP_MEDIA_NAME
))
1097 old
= pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
);
1099 if (old
&& name
&& !strcmp(old
, name
))
1103 pa_proplist_sets(o
->proplist
, PA_PROP_MEDIA_NAME
, name
);
1105 pa_proplist_unset(o
->proplist
, PA_PROP_MEDIA_NAME
);
1107 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
1108 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
1109 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1113 /* Called from main context */
1114 pa_resample_method_t
pa_source_output_get_resample_method(pa_source_output
*o
) {
1115 pa_source_output_assert_ref(o
);
1116 pa_assert_ctl_context();
1118 return o
->actual_resample_method
;
1121 /* Called from main context */
1122 pa_bool_t
pa_source_output_may_move(pa_source_output
*o
) {
1123 pa_source_output_assert_ref(o
);
1124 pa_assert_ctl_context();
1125 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1127 if (o
->flags
& PA_SOURCE_OUTPUT_DONT_MOVE
)
1130 if (o
->direct_on_input
)
1136 /* Called from main context */
1137 pa_bool_t
pa_source_output_may_move_to(pa_source_output
*o
, pa_source
*dest
) {
1138 pa_source_output_assert_ref(o
);
1139 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1140 pa_source_assert_ref(dest
);
1142 if (dest
== o
->source
)
1145 if (!pa_source_output_may_move(o
))
1148 if (pa_idxset_size(dest
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
1149 pa_log_warn("Failed to move source output: too many outputs per source.");
1154 if (!o
->may_move_to(o
, dest
))
1160 /* Called from main context */
1161 int pa_source_output_start_move(pa_source_output
*o
) {
1165 pa_source_output_assert_ref(o
);
1166 pa_assert_ctl_context();
1167 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1168 pa_assert(o
->source
);
1170 if (!pa_source_output_may_move(o
))
1171 return -PA_ERR_NOTSUPPORTED
;
1173 if ((r
= pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START
], o
)) < 0)
1178 pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
);
1180 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
)
1181 pa_assert_se(origin
->n_corked
-- >= 1);
1183 if (pa_source_flat_volume_enabled(o
->source
))
1184 /* We might need to update the source's volume if we are in flat
1186 pa_source_set_volume(o
->source
, NULL
, FALSE
, FALSE
);
1188 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
1190 pa_source_update_status(o
->source
);
1193 pa_source_output_unref(o
);
1198 /* Called from main context. If it has an origin source that uses volume sharing,
1199 * then also the origin source and all streams connected to it need to update
1200 * their volume - this function does all that by using recursion. */
1201 static void update_volume_due_to_moving(pa_source_output
*o
, pa_source
*dest
) {
1202 pa_cvolume old_volume
;
1206 pa_assert(o
->source
); /* The destination source should already be set. */
1208 if (o
->destination_source
&& (o
->destination_source
->flags
& PA_SOURCE_SHARE_VOLUME_WITH_MASTER
)) {
1209 pa_source
*root_source
= o
->source
;
1210 pa_source_output
*destination_source_output
;
1213 while (root_source
->flags
& PA_SOURCE_SHARE_VOLUME_WITH_MASTER
)
1214 root_source
= root_source
->output_from_master
->source
;
1216 if (pa_source_flat_volume_enabled(o
->source
)) {
1217 /* Ok, so the origin source uses volume sharing, and flat volume is
1218 * enabled. The volume will have to be updated as follows:
1220 * o->volume := o->source->real_volume
1221 * (handled later by pa_source_set_volume)
1222 * o->reference_ratio := o->volume / o->source->reference_volume
1223 * (handled later by pa_source_set_volume)
1224 * o->real_ratio stays unchanged
1225 * (streams whose origin source uses volume sharing should
1226 * always have real_ratio of 0 dB)
1227 * o->soft_volume stays unchanged
1228 * (streams whose origin source uses volume sharing should
1229 * always have volume_factor as soft_volume, so no change
1230 * should be needed) */
1232 pa_assert(pa_cvolume_is_norm(&o
->real_ratio
));
1233 pa_assert(pa_cvolume_equal(&o
->soft_volume
, &o
->volume_factor
));
1235 /* Notifications will be sent by pa_source_set_volume(). */
1238 /* Ok, so the origin source uses volume sharing, and flat volume is
1239 * disabled. The volume will have to be updated as follows:
1242 * o->reference_ratio := 0 dB
1243 * o->real_ratio stays unchanged
1244 * (streams whose origin source uses volume sharing should
1245 * always have real_ratio of 0 dB)
1246 * o->soft_volume stays unchanged
1247 * (streams whose origin source uses volume sharing should
1248 * always have volume_factor as soft_volume, so no change
1249 * should be needed) */
1251 old_volume
= o
->volume
;
1252 pa_cvolume_reset(&o
->volume
, o
->volume
.channels
);
1253 pa_cvolume_reset(&o
->reference_ratio
, o
->reference_ratio
.channels
);
1254 pa_assert(pa_cvolume_is_norm(&o
->real_ratio
));
1255 pa_assert(pa_cvolume_equal(&o
->soft_volume
, &o
->volume_factor
));
1257 /* Notify others about the changed source output volume. */
1258 if (!pa_cvolume_equal(&o
->volume
, &old_volume
)) {
1259 if (o
->volume_changed
)
1260 o
->volume_changed(o
);
1262 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1266 /* Additionally, the origin source volume needs updating:
1268 * o->destination_source->reference_volume := root_source->reference_volume
1269 * o->destination_source->real_volume := root_source->real_volume
1270 * o->destination_source->soft_volume stays unchanged
1271 * (sources that use volume sharing should always have
1272 * soft_volume of 0 dB) */
1274 old_volume
= o
->destination_source
->reference_volume
;
1276 o
->destination_source
->reference_volume
= root_source
->reference_volume
;
1277 pa_cvolume_remap(&o
->destination_source
->reference_volume
, &root_source
->channel_map
, &o
->destination_source
->channel_map
);
1279 o
->destination_source
->real_volume
= root_source
->real_volume
;
1280 pa_cvolume_remap(&o
->destination_source
->real_volume
, &root_source
->channel_map
, &o
->destination_source
->channel_map
);
1282 pa_assert(pa_cvolume_is_norm(&o
->destination_source
->soft_volume
));
1284 /* Notify others about the changed source volume. If you wonder whether
1285 * o->destination_source->set_volume() should be called somewhere, that's not
1286 * the case, because sources that use volume sharing shouldn't have any
1287 * internal volume that set_volume() would update. If you wonder
1288 * whether the thread_info variables should be synced, yes, they
1289 * should, and it's done by the PA_SOURCE_MESSAGE_FINISH_MOVE message
1291 if (!pa_cvolume_equal(&o
->destination_source
->reference_volume
, &old_volume
))
1292 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->destination_source
->index
);
1294 /* Recursively update origin source outputs. */
1295 PA_IDXSET_FOREACH(destination_source_output
, o
->destination_source
->outputs
, idx
)
1296 update_volume_due_to_moving(destination_source_output
, dest
);
1299 old_volume
= o
->volume
;
1301 if (pa_source_flat_volume_enabled(o
->source
)) {
1302 /* Ok, so this is a regular stream, and flat volume is enabled. The
1303 * volume will have to be updated as follows:
1305 * o->volume := o->reference_ratio * o->source->reference_volume
1306 * o->reference_ratio stays unchanged
1307 * o->real_ratio := o->volume / o->source->real_volume
1308 * (handled later by pa_source_set_volume)
1309 * o->soft_volume := o->real_ratio * o->volume_factor
1310 * (handled later by pa_source_set_volume) */
1312 o
->volume
= o
->source
->reference_volume
;
1313 pa_cvolume_remap(&o
->volume
, &o
->source
->channel_map
, &o
->channel_map
);
1314 pa_sw_cvolume_multiply(&o
->volume
, &o
->volume
, &o
->reference_ratio
);
1317 /* Ok, so this is a regular stream, and flat volume is disabled.
1318 * The volume will have to be updated as follows:
1320 * o->volume := o->reference_ratio
1321 * o->reference_ratio stays unchanged
1322 * o->real_ratio := o->reference_ratio
1323 * o->soft_volume := o->real_ratio * o->volume_factor */
1325 o
->volume
= o
->reference_ratio
;
1326 o
->real_ratio
= o
->reference_ratio
;
1327 pa_sw_cvolume_multiply(&o
->soft_volume
, &o
->real_ratio
, &o
->volume_factor
);
1330 /* Notify others about the changed source output volume. */
1331 if (!pa_cvolume_equal(&o
->volume
, &old_volume
)) {
1332 /* XXX: In case o->source has flat volume enabled, then real_ratio
1333 * and soft_volume are not updated yet. Let's hope that the
1334 * callback implementation doesn't care about those variables... */
1335 if (o
->volume_changed
)
1336 o
->volume_changed(o
);
1338 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1342 /* If o->source == dest, then recursion has finished, and we can finally call
1343 * pa_source_set_volume(), which will do the rest of the updates. */
1344 if ((o
->source
== dest
) && pa_source_flat_volume_enabled(o
->source
))
1345 pa_source_set_volume(o
->source
, NULL
, FALSE
, o
->save_volume
);
1348 /* Called from main context */
1349 int pa_source_output_finish_move(pa_source_output
*o
, pa_source
*dest
, pa_bool_t save
) {
1350 pa_resampler
*new_resampler
;
1352 pa_source_output_assert_ref(o
);
1353 pa_assert_ctl_context();
1354 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1355 pa_assert(!o
->source
);
1356 pa_source_assert_ref(dest
);
1358 if (!pa_source_output_may_move_to(o
, dest
))
1359 return -PA_ERR_NOTSUPPORTED
;
1361 if (pa_source_output_is_passthrough(o
) && !pa_source_check_format(dest
, o
->format
)) {
1362 pa_proplist
*p
= pa_proplist_new();
1363 pa_log_debug("New source doesn't support stream format, sending format-changed and killing");
1364 /* Tell the client what device we want to be on if it is going to
1366 pa_proplist_sets(p
, "device", dest
->name
);
1367 pa_source_output_send_event(o
, PA_STREAM_EVENT_FORMAT_LOST
, p
);
1368 pa_proplist_free(p
);
1369 return -PA_ERR_NOTSUPPORTED
;
1372 if (o
->thread_info
.resampler
&&
1373 pa_sample_spec_equal(pa_resampler_input_sample_spec(o
->thread_info
.resampler
), &dest
->sample_spec
) &&
1374 pa_channel_map_equal(pa_resampler_input_channel_map(o
->thread_info
.resampler
), &dest
->channel_map
))
1376 /* Try to reuse the old resampler if possible */
1377 new_resampler
= o
->thread_info
.resampler
;
1379 else if ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
1380 !pa_sample_spec_equal(&o
->sample_spec
, &dest
->sample_spec
) ||
1381 !pa_channel_map_equal(&o
->channel_map
, &dest
->channel_map
)) {
1383 /* Okay, we need a new resampler for the new source */
1385 if (!(new_resampler
= pa_resampler_new(
1387 &dest
->sample_spec
, &dest
->channel_map
,
1388 &o
->sample_spec
, &o
->channel_map
,
1389 o
->requested_resample_method
,
1390 ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
1391 ((o
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
1392 (o
->core
->disable_remixing
|| (o
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0)))) {
1393 pa_log_warn("Unsupported resampling operation.");
1394 return -PA_ERR_NOTSUPPORTED
;
1397 new_resampler
= NULL
;
1403 o
->save_source
= save
;
1404 pa_idxset_put(o
->source
->outputs
, pa_source_output_ref(o
), NULL
);
1406 pa_cvolume_remap(&o
->volume_factor_source
, &o
->channel_map
, &o
->source
->channel_map
);
1408 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
)
1409 o
->source
->n_corked
++;
1411 /* Replace resampler */
1412 if (new_resampler
!= o
->thread_info
.resampler
) {
1414 if (o
->thread_info
.resampler
)
1415 pa_resampler_free(o
->thread_info
.resampler
);
1416 o
->thread_info
.resampler
= new_resampler
;
1418 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
1420 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
1422 MEMBLOCKQ_MAXLENGTH
,
1424 pa_frame_size(&o
->source
->sample_spec
),
1428 &o
->source
->silence
);
1429 o
->actual_resample_method
= new_resampler
? pa_resampler_get_method(new_resampler
) : PA_RESAMPLER_INVALID
;
1432 pa_source_update_status(dest
);
1434 update_volume_due_to_moving(o
, dest
);
1436 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
1438 pa_log_debug("Successfully moved source output %i to %s.", o
->index
, dest
->name
);
1440 /* Notify everyone */
1441 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH
], o
);
1442 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
1447 /* Called from main context */
1448 void pa_source_output_fail_move(pa_source_output
*o
) {
1450 pa_source_output_assert_ref(o
);
1451 pa_assert_ctl_context();
1452 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1453 pa_assert(!o
->source
);
1455 /* Check if someone wants this source output? */
1456 if (pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL
], o
) == PA_HOOK_STOP
)
1462 pa_source_output_kill(o
);
1465 /* Called from main context */
1466 int pa_source_output_move_to(pa_source_output
*o
, pa_source
*dest
, pa_bool_t save
) {
1469 pa_source_output_assert_ref(o
);
1470 pa_assert_ctl_context();
1471 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
1472 pa_assert(o
->source
);
1473 pa_source_assert_ref(dest
);
1475 if (dest
== o
->source
)
1478 if (!pa_source_output_may_move_to(o
, dest
))
1479 return -PA_ERR_NOTSUPPORTED
;
1481 pa_source_output_ref(o
);
1483 if ((r
= pa_source_output_start_move(o
)) < 0) {
1484 pa_source_output_unref(o
);
1488 if ((r
= pa_source_output_finish_move(o
, dest
, save
)) < 0) {
1489 pa_source_output_fail_move(o
);
1490 pa_source_output_unref(o
);
1494 pa_source_output_unref(o
);
1499 /* Called from IO thread context */
1500 void pa_source_output_set_state_within_thread(pa_source_output
*o
, pa_source_output_state_t state
) {
1501 pa_source_output_assert_ref(o
);
1502 pa_source_output_assert_io_context(o
);
1504 if (state
== o
->thread_info
.state
)
1507 if (o
->state_change
)
1508 o
->state_change(o
, state
);
1510 o
->thread_info
.state
= state
;
1513 /* Called from IO thread context, except when it is not */
1514 int pa_source_output_process_msg(pa_msgobject
*mo
, int code
, void *userdata
, int64_t offset
, pa_memchunk
* chunk
) {
1515 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
1516 pa_source_output_assert_ref(o
);
1520 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
: {
1521 pa_usec_t
*r
= userdata
;
1523 r
[0] += pa_bytes_to_usec(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
), &o
->source
->sample_spec
);
1524 r
[1] += pa_source_get_latency_within_thread(o
->source
);
1529 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
:
1531 o
->thread_info
.sample_spec
.rate
= PA_PTR_TO_UINT(userdata
);
1532 pa_resampler_set_output_rate(o
->thread_info
.resampler
, PA_PTR_TO_UINT(userdata
));
1535 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE
:
1537 pa_source_output_set_state_within_thread(o
, PA_PTR_TO_UINT(userdata
));
1541 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
: {
1542 pa_usec_t
*usec
= userdata
;
1544 *usec
= pa_source_output_set_requested_latency_within_thread(o
, *usec
);
1549 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
: {
1550 pa_usec_t
*r
= userdata
;
1552 *r
= o
->thread_info
.requested_source_latency
;
1556 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_VOLUME
:
1557 if (!pa_cvolume_equal(&o
->thread_info
.soft_volume
, &o
->soft_volume
)) {
1558 o
->thread_info
.soft_volume
= o
->soft_volume
;
1562 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE
:
1563 if (o
->thread_info
.muted
!= o
->muted
) {
1564 o
->thread_info
.muted
= o
->muted
;
1569 return -PA_ERR_NOTIMPLEMENTED
;
1572 /* Called from main context */
1573 void pa_source_output_send_event(pa_source_output
*o
, const char *event
, pa_proplist
*data
) {
1574 pa_proplist
*pl
= NULL
;
1575 pa_source_output_send_event_hook_data hook_data
;
1577 pa_source_output_assert_ref(o
);
1578 pa_assert_ctl_context();
1585 data
= pl
= pa_proplist_new();
1587 hook_data
.source_output
= o
;
1588 hook_data
.data
= data
;
1589 hook_data
.event
= event
;
1591 if (pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_SEND_EVENT
], &hook_data
) < 0)
1594 o
->send_event(o
, event
, data
);
1598 pa_proplist_free(pl
);