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 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>
34 #include <pulsecore/sample-util.h>
35 #include <pulsecore/core-subscribe.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/namereg.h>
38 #include <pulsecore/core-util.h>
40 #include "source-output.h"
42 #define MEMBLOCKQ_MAXLENGTH (32*1024*1024)
44 static PA_DEFINE_CHECK_TYPE(pa_source_output
, pa_msgobject
);
46 static void source_output_free(pa_object
* mo
);
48 pa_source_output_new_data
* pa_source_output_new_data_init(pa_source_output_new_data
*data
) {
51 memset(data
, 0, sizeof(*data
));
52 data
->resample_method
= PA_RESAMPLER_INVALID
;
53 data
->proplist
= pa_proplist_new();
58 void pa_source_output_new_data_set_sample_spec(pa_source_output_new_data
*data
, const pa_sample_spec
*spec
) {
61 if ((data
->sample_spec_is_set
= !!spec
))
62 data
->sample_spec
= *spec
;
65 void pa_source_output_new_data_set_channel_map(pa_source_output_new_data
*data
, const pa_channel_map
*map
) {
68 if ((data
->channel_map_is_set
= !!map
))
69 data
->channel_map
= *map
;
72 void pa_source_output_new_data_done(pa_source_output_new_data
*data
) {
75 pa_proplist_free(data
->proplist
);
78 /* Called from main context */
79 static void reset_callbacks(pa_source_output
*o
) {
83 o
->process_rewind
= NULL
;
84 o
->update_max_rewind
= NULL
;
85 o
->update_source_requested_latency
= NULL
;
86 o
->update_source_latency_range
= NULL
;
92 o
->get_latency
= NULL
;
93 o
->state_change
= NULL
;
94 o
->may_move_to
= NULL
;
97 /* Called from main context */
98 pa_source_output
* pa_source_output_new(
100 pa_source_output_new_data
*data
,
101 pa_source_output_flags_t flags
) {
104 pa_resampler
*resampler
= NULL
;
105 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
110 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_NEW
], data
) < 0)
113 pa_return_null_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
));
116 data
->source
= pa_namereg_get(core
, NULL
, PA_NAMEREG_SOURCE
);
118 pa_return_null_if_fail(data
->source
);
119 pa_return_null_if_fail(pa_source_get_state(data
->source
) != PA_SOURCE_UNLINKED
);
121 pa_return_null_if_fail(!data
->direct_on_input
|| data
->direct_on_input
->sink
== data
->source
->monitor_of
);
123 if (!data
->sample_spec_is_set
)
124 data
->sample_spec
= data
->source
->sample_spec
;
126 pa_return_null_if_fail(pa_sample_spec_valid(&data
->sample_spec
));
128 if (!data
->channel_map_is_set
) {
129 if (pa_channel_map_compatible(&data
->source
->channel_map
, &data
->sample_spec
))
130 data
->channel_map
= data
->source
->channel_map
;
132 pa_channel_map_init_extend(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
);
135 pa_return_null_if_fail(pa_channel_map_valid(&data
->channel_map
));
136 pa_return_null_if_fail(pa_channel_map_compatible(&data
->channel_map
, &data
->sample_spec
));
138 if (flags
& PA_SOURCE_OUTPUT_FIX_FORMAT
)
139 data
->sample_spec
.format
= data
->source
->sample_spec
.format
;
141 if (flags
& PA_SOURCE_OUTPUT_FIX_RATE
)
142 data
->sample_spec
.rate
= data
->source
->sample_spec
.rate
;
144 if (flags
& PA_SOURCE_OUTPUT_FIX_CHANNELS
) {
145 data
->sample_spec
.channels
= data
->source
->sample_spec
.channels
;
146 data
->channel_map
= data
->source
->channel_map
;
149 pa_assert(pa_sample_spec_valid(&data
->sample_spec
));
150 pa_assert(pa_channel_map_valid(&data
->channel_map
));
152 if (data
->resample_method
== PA_RESAMPLER_INVALID
)
153 data
->resample_method
= core
->resample_method
;
155 pa_return_null_if_fail(data
->resample_method
< PA_RESAMPLER_MAX
);
158 pa_proplist_update(data
->proplist
, PA_UPDATE_MERGE
, data
->client
->proplist
);
160 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE
], data
) < 0)
163 if (pa_idxset_size(data
->source
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
164 pa_log("Failed to create source output: too many outputs per source.");
168 if ((flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
169 !pa_sample_spec_equal(&data
->sample_spec
, &data
->source
->sample_spec
) ||
170 !pa_channel_map_equal(&data
->channel_map
, &data
->source
->channel_map
)) {
172 if (!(resampler
= pa_resampler_new(
174 &data
->source
->sample_spec
, &data
->source
->channel_map
,
175 &data
->sample_spec
, &data
->channel_map
,
176 data
->resample_method
,
177 ((flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
178 ((flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
179 (core
->disable_remixing
|| (flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0) |
180 (core
->disable_lfe_remixing
? PA_RESAMPLER_NO_LFE
: 0)))) {
181 pa_log_warn("Unsupported resampling operation.");
186 o
= pa_msgobject_new(pa_source_output
);
187 o
->parent
.parent
.free
= source_output_free
;
188 o
->parent
.process_msg
= pa_source_output_process_msg
;
191 o
->state
= PA_SOURCE_OUTPUT_INIT
;
193 o
->proplist
= pa_proplist_copy(data
->proplist
);
194 o
->driver
= pa_xstrdup(pa_path_get_filename(data
->driver
));
195 o
->module
= data
->module
;
196 o
->source
= data
->source
;
197 o
->client
= data
->client
;
200 o
->actual_resample_method
= resampler
? pa_resampler_get_method(resampler
) : PA_RESAMPLER_INVALID
;
201 o
->requested_resample_method
= data
->resample_method
;
202 o
->sample_spec
= data
->sample_spec
;
203 o
->channel_map
= data
->channel_map
;
205 o
->direct_on_input
= data
->direct_on_input
;
210 o
->thread_info
.state
= o
->state
;
211 o
->thread_info
.attached
= FALSE
;
212 o
->thread_info
.sample_spec
= o
->sample_spec
;
213 o
->thread_info
.resampler
= resampler
;
214 o
->thread_info
.requested_source_latency
= (pa_usec_t
) -1;
215 o
->thread_info
.direct_on_input
= o
->direct_on_input
;
217 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
221 pa_frame_size(&o
->source
->sample_spec
),
225 &o
->source
->silence
);
227 pa_assert_se(pa_idxset_put(core
->source_outputs
, o
, &o
->index
) == 0);
228 pa_assert_se(pa_idxset_put(o
->source
->outputs
, pa_source_output_ref(o
), NULL
) == 0);
231 pa_assert_se(pa_idxset_put(o
->client
->source_outputs
, o
, NULL
) >= 0);
233 if (o
->direct_on_input
)
234 pa_assert_se(pa_idxset_put(o
->direct_on_input
->direct_outputs
, o
, NULL
) == 0);
236 pa_log_info("Created output %u \"%s\" on %s with sample spec %s and channel map %s",
238 pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)),
240 pa_sample_spec_snprint(st
, sizeof(st
), &o
->sample_spec
),
241 pa_channel_map_snprint(cm
, sizeof(cm
), &o
->channel_map
));
243 /* Don't forget to call pa_source_output_put! */
248 /* Called from main context */
249 static void update_n_corked(pa_source_output
*o
, pa_source_output_state_t state
) {
255 if (o
->state
== PA_SOURCE_OUTPUT_CORKED
&& state
!= PA_SOURCE_OUTPUT_CORKED
)
256 pa_assert_se(o
->source
->n_corked
-- >= 1);
257 else if (o
->state
!= PA_SOURCE_OUTPUT_CORKED
&& state
== PA_SOURCE_OUTPUT_CORKED
)
258 o
->source
->n_corked
++;
261 /* Called from main context */
262 static int source_output_set_state(pa_source_output
*o
, pa_source_output_state_t state
) {
265 if (o
->state
== state
)
268 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);
270 update_n_corked(o
, state
);
273 if (state
!= PA_SOURCE_OUTPUT_UNLINKED
)
274 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED
], o
);
276 pa_source_update_status(o
->source
);
281 /* Called from main context */
282 void pa_source_output_unlink(pa_source_output
*o
) {
286 /* See pa_sink_unlink() for a couple of comments how this function
289 pa_source_output_ref(o
);
291 linked
= PA_SOURCE_OUTPUT_IS_LINKED(o
->state
);
294 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK
], o
);
296 if (o
->direct_on_input
)
297 pa_idxset_remove_by_data(o
->direct_on_input
->direct_outputs
, o
, NULL
);
299 pa_idxset_remove_by_data(o
->core
->source_outputs
, o
, NULL
);
302 if (pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
))
303 pa_source_output_unref(o
);
306 pa_idxset_remove_by_data(o
->client
->source_outputs
, o
, NULL
);
308 update_n_corked(o
, PA_SOURCE_OUTPUT_UNLINKED
);
309 o
->state
= PA_SOURCE_OUTPUT_UNLINKED
;
311 if (linked
&& o
->source
)
312 if (o
->source
->asyncmsgq
)
313 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
318 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, o
->index
);
319 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST
], o
);
323 pa_source_update_status(o
->source
);
327 pa_source_output_unref(o
);
330 /* Called from main context */
331 static void source_output_free(pa_object
* mo
) {
332 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
335 pa_assert(pa_source_output_refcnt(o
) == 0);
337 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
338 pa_source_output_unlink(o
);
340 pa_log_info("Freeing output %u \"%s\"", o
->index
, pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)));
342 pa_assert(!o
->thread_info
.attached
);
344 if (o
->thread_info
.delay_memblockq
)
345 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
347 if (o
->thread_info
.resampler
)
348 pa_resampler_free(o
->thread_info
.resampler
);
351 pa_proplist_free(o
->proplist
);
357 /* Called from main context */
358 void pa_source_output_put(pa_source_output
*o
) {
359 pa_source_output_state_t state
;
360 pa_source_output_assert_ref(o
);
362 pa_assert(o
->state
== PA_SOURCE_OUTPUT_INIT
);
364 /* The following fields must be initialized properly */
368 state
= o
->flags
& PA_SOURCE_OUTPUT_START_CORKED
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
;
370 update_n_corked(o
, state
);
373 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
375 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
, o
->index
);
376 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PUT
], o
);
378 pa_source_update_status(o
->source
);
381 /* Called from main context */
382 void pa_source_output_kill(pa_source_output
*o
) {
383 pa_source_output_assert_ref(o
);
384 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
389 /* Called from main context */
390 pa_usec_t
pa_source_output_get_latency(pa_source_output
*o
, pa_usec_t
*source_latency
) {
391 pa_usec_t r
[2] = { 0, 0 };
393 pa_source_output_assert_ref(o
);
394 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
396 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
, r
, 0, NULL
) == 0);
399 r
[0] += o
->get_latency(o
);
402 *source_latency
= r
[1];
407 /* Called from thread context */
408 void pa_source_output_push(pa_source_output
*o
, const pa_memchunk
*chunk
) {
410 size_t limit
, mbs
= 0;
412 pa_source_output_assert_ref(o
);
413 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
415 pa_assert(pa_frame_aligned(chunk
->length
, &o
->source
->sample_spec
));
417 if (!o
->push
|| o
->thread_info
.state
== PA_SOURCE_OUTPUT_CORKED
)
420 pa_assert(o
->thread_info
.state
== PA_SOURCE_OUTPUT_RUNNING
);
422 if (pa_memblockq_push(o
->thread_info
.delay_memblockq
, chunk
) < 0) {
423 pa_log_debug("Delay queue overflow!");
424 pa_memblockq_seek(o
->thread_info
.delay_memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
);
427 limit
= o
->process_rewind
? 0 : o
->source
->thread_info
.max_rewind
;
429 /* Implement the delay queue */
430 while ((length
= pa_memblockq_get_length(o
->thread_info
.delay_memblockq
)) > limit
) {
435 pa_assert_se(pa_memblockq_peek(o
->thread_info
.delay_memblockq
, &qchunk
) >= 0);
437 if (qchunk
.length
> length
)
438 qchunk
.length
= length
;
440 pa_assert(qchunk
.length
> 0);
442 if (!o
->thread_info
.resampler
)
448 mbs
= pa_resampler_max_block_size(o
->thread_info
.resampler
);
450 if (qchunk
.length
> mbs
)
453 pa_resampler_run(o
->thread_info
.resampler
, &qchunk
, &rchunk
);
455 if (rchunk
.length
> 0)
459 pa_memblock_unref(rchunk
.memblock
);
462 pa_memblock_unref(qchunk
.memblock
);
463 pa_memblockq_drop(o
->thread_info
.delay_memblockq
, qchunk
.length
);
467 /* Called from thread context */
468 void pa_source_output_process_rewind(pa_source_output
*o
, size_t nbytes
/* in source sample spec */) {
469 pa_source_output_assert_ref(o
);
471 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
472 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
477 if (o
->process_rewind
) {
478 pa_assert(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
) == 0);
480 if (o
->thread_info
.resampler
)
481 nbytes
= pa_resampler_result(o
->thread_info
.resampler
, nbytes
);
483 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) nbytes
);
486 o
->process_rewind(o
, nbytes
);
488 if (o
->thread_info
.resampler
)
489 pa_resampler_reset(o
->thread_info
.resampler
);
492 pa_memblockq_rewind(o
->thread_info
.delay_memblockq
, nbytes
);
495 /* Called from thread context */
496 void pa_source_output_update_max_rewind(pa_source_output
*o
, size_t nbytes
/* in the source's sample spec */) {
497 pa_source_output_assert_ref(o
);
498 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
499 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
501 if (o
->update_max_rewind
)
502 o
->update_max_rewind(o
, o
->thread_info
.resampler
? pa_resampler_result(o
->thread_info
.resampler
, nbytes
) : nbytes
);
505 /* Called from thread context */
506 static pa_usec_t
fixup_latency(pa_source
*s
, pa_usec_t usec
) {
507 pa_source_assert_ref(s
);
509 if (usec
== (pa_usec_t
) -1)
512 if (s
->thread_info
.max_latency
> 0 && usec
> s
->thread_info
.max_latency
)
513 usec
= s
->thread_info
.max_latency
;
515 if (s
->thread_info
.min_latency
> 0 && usec
< s
->thread_info
.min_latency
)
516 usec
= s
->thread_info
.min_latency
;
521 /* Called from thread context */
522 pa_usec_t
pa_source_output_set_requested_latency_within_thread(pa_source_output
*o
, pa_usec_t usec
) {
523 pa_source_output_assert_ref(o
);
525 usec
= fixup_latency(o
->source
, usec
);
526 o
->thread_info
.requested_source_latency
= usec
;
527 pa_source_invalidate_requested_latency(o
->source
);
532 /* Called from main context */
533 pa_usec_t
pa_source_output_set_requested_latency(pa_source_output
*o
, pa_usec_t usec
) {
534 pa_source_output_assert_ref(o
);
536 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
537 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
539 /* If this source output is not realized yet, we have to touch
540 * the thread info data directly */
542 o
->thread_info
.requested_source_latency
= usec
;
547 /* Called from main context */
548 pa_usec_t
pa_source_output_get_requested_latency(pa_source_output
*o
) {
551 pa_source_output_assert_ref(o
);
553 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
554 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
556 /* If this source output is not realized yet, we have to touch
557 * the thread info data directly */
558 usec
= o
->thread_info
.requested_source_latency
;
563 /* Called from main context */
564 void pa_source_output_cork(pa_source_output
*o
, pa_bool_t b
) {
565 pa_source_output_assert_ref(o
);
566 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
568 source_output_set_state(o
, b
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
);
571 /* Called from main context */
572 int pa_source_output_set_rate(pa_source_output
*o
, uint32_t rate
) {
573 pa_source_output_assert_ref(o
);
574 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
575 pa_return_val_if_fail(o
->thread_info
.resampler
, -1);
577 if (o
->sample_spec
.rate
== rate
)
580 o
->sample_spec
.rate
= rate
;
582 pa_asyncmsgq_post(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
, PA_UINT_TO_PTR(rate
), 0, NULL
, NULL
);
584 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
588 /* Called from main context */
589 void pa_source_output_set_name(pa_source_output
*o
, const char *name
) {
591 pa_source_output_assert_ref(o
);
593 if (!name
&& !pa_proplist_contains(o
->proplist
, PA_PROP_MEDIA_NAME
))
596 old
= pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
);
598 if (old
&& name
&& !strcmp(old
, name
))
602 pa_proplist_sets(o
->proplist
, PA_PROP_MEDIA_NAME
, name
);
604 pa_proplist_unset(o
->proplist
, PA_PROP_MEDIA_NAME
);
606 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
607 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
608 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
612 /* Called from main thread */
613 pa_bool_t
pa_source_output_update_proplist(pa_source_output
*o
, pa_update_mode_t mode
, pa_proplist
*p
) {
615 pa_source_output_assert_ref(o
);
617 pa_proplist_update(o
->proplist
, mode
, p
);
619 if (PA_SINK_IS_LINKED(o
->state
)) {
620 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
621 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
627 /* Called from main context */
628 pa_resample_method_t
pa_source_output_get_resample_method(pa_source_output
*o
) {
629 pa_source_output_assert_ref(o
);
631 return o
->actual_resample_method
;
634 /* Called from main context */
635 pa_bool_t
pa_source_output_may_move(pa_source_output
*o
) {
636 pa_source_output_assert_ref(o
);
637 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
639 if (o
->flags
& PA_SOURCE_OUTPUT_DONT_MOVE
)
642 if (o
->direct_on_input
)
648 /* Called from main context */
649 pa_bool_t
pa_source_output_may_move_to(pa_source_output
*o
, pa_source
*dest
) {
650 pa_source_output_assert_ref(o
);
651 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
652 pa_source_assert_ref(dest
);
654 if (dest
== o
->source
)
657 if (!pa_source_output_may_move(o
))
660 if (pa_idxset_size(dest
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
661 pa_log_warn("Failed to move source output: too many outputs per source.");
666 if (!o
->may_move_to(o
, dest
))
672 /* Called from main context */
673 int pa_source_output_start_move(pa_source_output
*o
) {
676 pa_source_output_assert_ref(o
);
677 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
678 pa_assert(o
->source
);
680 if (!pa_source_output_may_move(o
))
683 if (pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START
], o
) < 0)
688 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
690 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
)
691 pa_assert_se(origin
->n_corked
-- >= 1);
693 pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
);
696 pa_source_update_status(origin
);
701 /* Called from main context */
702 int pa_source_output_finish_move(pa_source_output
*o
, pa_source
*dest
) {
703 pa_resampler
*new_resampler
;
705 pa_source_output_assert_ref(o
);
706 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
707 pa_assert(!o
->source
);
708 pa_source_assert_ref(dest
);
711 pa_idxset_put(o
->source
->outputs
, o
, NULL
);
713 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
)
714 o
->source
->n_corked
++;
716 if (o
->thread_info
.resampler
&&
717 pa_sample_spec_equal(pa_resampler_input_sample_spec(o
->thread_info
.resampler
), &dest
->sample_spec
) &&
718 pa_channel_map_equal(pa_resampler_input_channel_map(o
->thread_info
.resampler
), &dest
->channel_map
))
720 /* Try to reuse the old resampler if possible */
721 new_resampler
= o
->thread_info
.resampler
;
723 else if ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
724 !pa_sample_spec_equal(&o
->sample_spec
, &dest
->sample_spec
) ||
725 !pa_channel_map_equal(&o
->channel_map
, &dest
->channel_map
)) {
727 /* Okey, we need a new resampler for the new source */
729 if (!(new_resampler
= pa_resampler_new(
731 &dest
->sample_spec
, &dest
->channel_map
,
732 &o
->sample_spec
, &o
->channel_map
,
733 o
->requested_resample_method
,
734 ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
735 ((o
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
736 (o
->core
->disable_remixing
|| (o
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0)))) {
737 pa_log_warn("Unsupported resampling operation.");
741 new_resampler
= NULL
;
743 /* Replace resampler */
744 if (new_resampler
!= o
->thread_info
.resampler
) {
745 if (o
->thread_info
.resampler
)
746 pa_resampler_free(o
->thread_info
.resampler
);
747 o
->thread_info
.resampler
= new_resampler
;
749 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
751 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
755 pa_frame_size(&o
->source
->sample_spec
),
759 &o
->source
->silence
);
762 pa_source_update_status(dest
);
763 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
765 pa_log_debug("Successfully moved source output %i to %s.", o
->index
, dest
->name
);
767 /* Notify everyone */
771 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH
], o
);
772 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
777 /* Called from main context */
778 int pa_source_output_move_to(pa_source_output
*o
, pa_source
*dest
) {
780 pa_source_output_assert_ref(o
);
781 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
782 pa_assert(o
->source
);
783 pa_source_assert_ref(dest
);
785 if (dest
== o
->source
)
788 if (!pa_source_output_may_move_to(o
, dest
))
791 if (pa_source_output_start_move(o
) < 0)
794 if (pa_source_output_finish_move(o
, dest
) < 0)
800 /* Called from IO thread context */
801 void pa_source_output_set_state_within_thread(pa_source_output
*o
, pa_source_output_state_t state
) {
802 pa_source_output_assert_ref(o
);
804 if (state
== o
->thread_info
.state
)
808 o
->state_change(o
, state
);
810 o
->thread_info
.state
= state
;
813 /* Called from IO thread context, except when it is not */
814 int pa_source_output_process_msg(pa_msgobject
*mo
, int code
, void *userdata
, int64_t offset
, pa_memchunk
* chunk
) {
815 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
816 pa_source_output_assert_ref(o
);
820 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
: {
821 pa_usec_t
*r
= userdata
;
822 pa_usec_t source_usec
= 0;
824 r
[0] += pa_bytes_to_usec(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
), &o
->source
->sample_spec
);
826 if (o
->source
->parent
.process_msg(PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_GET_LATENCY
, &source_usec
, 0, NULL
) >= 0)
832 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
:
834 o
->thread_info
.sample_spec
.rate
= PA_PTR_TO_UINT(userdata
);
835 pa_resampler_set_output_rate(o
->thread_info
.resampler
, PA_PTR_TO_UINT(userdata
));
838 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE
:
840 pa_source_output_set_state_within_thread(o
, PA_PTR_TO_UINT(userdata
));
843 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
: {
844 pa_usec_t
*usec
= userdata
;
846 *usec
= pa_source_output_set_requested_latency_within_thread(o
, *usec
);
851 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
: {
852 pa_usec_t
*r
= userdata
;
854 *r
= o
->thread_info
.requested_source_latency
;