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>
33 #include <pulsecore/sample-util.h>
34 #include <pulsecore/core-subscribe.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/namereg.h>
37 #include <pulsecore/core-util.h>
39 #include "source-output.h"
41 #define MEMBLOCKQ_MAXLENGTH (32*1024*1024)
43 static PA_DEFINE_CHECK_TYPE(pa_source_output
, pa_msgobject
);
45 static void source_output_free(pa_object
* mo
);
47 pa_source_output_new_data
* pa_source_output_new_data_init(pa_source_output_new_data
*data
) {
50 memset(data
, 0, sizeof(*data
));
51 data
->resample_method
= PA_RESAMPLER_INVALID
;
52 data
->proplist
= pa_proplist_new();
57 void pa_source_output_new_data_set_sample_spec(pa_source_output_new_data
*data
, const pa_sample_spec
*spec
) {
60 if ((data
->sample_spec_is_set
= !!spec
))
61 data
->sample_spec
= *spec
;
64 void pa_source_output_new_data_set_channel_map(pa_source_output_new_data
*data
, const pa_channel_map
*map
) {
67 if ((data
->channel_map_is_set
= !!map
))
68 data
->channel_map
= *map
;
71 void pa_source_output_new_data_done(pa_source_output_new_data
*data
) {
74 pa_proplist_free(data
->proplist
);
77 /* Called from main context */
78 static void reset_callbacks(pa_source_output
*o
) {
82 o
->process_rewind
= NULL
;
83 o
->update_max_rewind
= NULL
;
84 o
->update_source_requested_latency
= NULL
;
85 o
->update_source_latency_range
= NULL
;
91 o
->get_latency
= NULL
;
92 o
->state_change
= NULL
;
93 o
->may_move_to
= NULL
;
96 /* Called from main context */
97 pa_source_output
* pa_source_output_new(
99 pa_source_output_new_data
*data
,
100 pa_source_output_flags_t flags
) {
103 pa_resampler
*resampler
= NULL
;
104 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
109 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_NEW
], data
) < 0)
112 pa_return_null_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
));
115 data
->source
= pa_namereg_get(core
, NULL
, PA_NAMEREG_SOURCE
);
117 pa_return_null_if_fail(data
->source
);
118 pa_return_null_if_fail(pa_source_get_state(data
->source
) != PA_SOURCE_UNLINKED
);
120 pa_return_null_if_fail(!data
->direct_on_input
|| data
->direct_on_input
->sink
== data
->source
->monitor_of
);
122 if (!data
->sample_spec_is_set
)
123 data
->sample_spec
= data
->source
->sample_spec
;
125 pa_return_null_if_fail(pa_sample_spec_valid(&data
->sample_spec
));
127 if (!data
->channel_map_is_set
) {
128 if (pa_channel_map_compatible(&data
->source
->channel_map
, &data
->sample_spec
))
129 data
->channel_map
= data
->source
->channel_map
;
131 pa_channel_map_init_extend(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
);
134 pa_return_null_if_fail(pa_channel_map_valid(&data
->channel_map
));
135 pa_return_null_if_fail(pa_channel_map_compatible(&data
->channel_map
, &data
->sample_spec
));
137 if (flags
& PA_SOURCE_OUTPUT_FIX_FORMAT
)
138 data
->sample_spec
.format
= data
->source
->sample_spec
.format
;
140 if (flags
& PA_SOURCE_OUTPUT_FIX_RATE
)
141 data
->sample_spec
.rate
= data
->source
->sample_spec
.rate
;
143 if (flags
& PA_SOURCE_OUTPUT_FIX_CHANNELS
) {
144 data
->sample_spec
.channels
= data
->source
->sample_spec
.channels
;
145 data
->channel_map
= data
->source
->channel_map
;
148 pa_assert(pa_sample_spec_valid(&data
->sample_spec
));
149 pa_assert(pa_channel_map_valid(&data
->channel_map
));
151 if (data
->resample_method
== PA_RESAMPLER_INVALID
)
152 data
->resample_method
= core
->resample_method
;
154 pa_return_null_if_fail(data
->resample_method
< PA_RESAMPLER_MAX
);
157 pa_proplist_update(data
->proplist
, PA_UPDATE_MERGE
, data
->client
->proplist
);
159 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE
], data
) < 0)
162 if (pa_idxset_size(data
->source
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
163 pa_log("Failed to create source output: too many outputs per source.");
167 if ((flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
168 !pa_sample_spec_equal(&data
->sample_spec
, &data
->source
->sample_spec
) ||
169 !pa_channel_map_equal(&data
->channel_map
, &data
->source
->channel_map
)) {
171 if (!(resampler
= pa_resampler_new(
173 &data
->source
->sample_spec
, &data
->source
->channel_map
,
174 &data
->sample_spec
, &data
->channel_map
,
175 data
->resample_method
,
176 ((flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
177 ((flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
178 (core
->disable_remixing
|| (flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0) |
179 (core
->disable_lfe_remixing
? PA_RESAMPLER_NO_LFE
: 0)))) {
180 pa_log_warn("Unsupported resampling operation.");
184 data
->resample_method
= pa_resampler_get_method(resampler
);
187 o
= pa_msgobject_new(pa_source_output
);
188 o
->parent
.parent
.free
= source_output_free
;
189 o
->parent
.process_msg
= pa_source_output_process_msg
;
192 o
->state
= PA_SOURCE_OUTPUT_INIT
;
194 o
->proplist
= pa_proplist_copy(data
->proplist
);
195 o
->driver
= pa_xstrdup(data
->driver
);
196 o
->module
= data
->module
;
197 o
->source
= data
->source
;
198 o
->client
= data
->client
;
200 o
->resample_method
= data
->resample_method
;
201 o
->sample_spec
= data
->sample_spec
;
202 o
->channel_map
= data
->channel_map
;
204 o
->direct_on_input
= data
->direct_on_input
;
209 o
->thread_info
.state
= o
->state
;
210 o
->thread_info
.attached
= FALSE
;
211 o
->thread_info
.sample_spec
= o
->sample_spec
;
212 o
->thread_info
.resampler
= resampler
;
213 o
->thread_info
.requested_source_latency
= (pa_usec_t
) -1;
214 o
->thread_info
.direct_on_input
= o
->direct_on_input
;
216 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
220 pa_frame_size(&o
->source
->sample_spec
),
224 &o
->source
->silence
);
226 pa_assert_se(pa_idxset_put(core
->source_outputs
, o
, &o
->index
) == 0);
227 pa_assert_se(pa_idxset_put(o
->source
->outputs
, pa_source_output_ref(o
), NULL
) == 0);
230 pa_assert_se(pa_idxset_put(o
->client
->source_outputs
, o
, NULL
) >= 0);
232 if (o
->direct_on_input
)
233 pa_assert_se(pa_idxset_put(o
->direct_on_input
->direct_outputs
, o
, NULL
) == 0);
235 pa_log_info("Created output %u \"%s\" on %s with sample spec %s and channel map %s",
237 pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)),
239 pa_sample_spec_snprint(st
, sizeof(st
), &o
->sample_spec
),
240 pa_channel_map_snprint(cm
, sizeof(cm
), &o
->channel_map
));
242 /* Don't forget to call pa_source_output_put! */
247 /* Called from main context */
248 static void update_n_corked(pa_source_output
*o
, pa_source_output_state_t state
) {
251 if (o
->state
== PA_SOURCE_OUTPUT_CORKED
&& state
!= PA_SOURCE_OUTPUT_CORKED
)
252 pa_assert_se(o
->source
->n_corked
-- >= 1);
253 else if (o
->state
!= PA_SOURCE_OUTPUT_CORKED
&& state
== PA_SOURCE_OUTPUT_CORKED
)
254 o
->source
->n_corked
++;
258 /* Called from main context */
259 static int source_output_set_state(pa_source_output
*o
, pa_source_output_state_t state
) {
262 if (o
->state
== state
)
265 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);
267 update_n_corked(o
, state
);
270 if (state
!= PA_SOURCE_OUTPUT_UNLINKED
)
271 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED
], o
);
273 pa_source_update_status(o
->source
);
278 /* Called from main context */
279 void pa_source_output_unlink(pa_source_output
*o
) {
283 /* See pa_sink_unlink() for a couple of comments how this function
286 pa_source_output_ref(o
);
288 linked
= PA_SOURCE_OUTPUT_IS_LINKED(o
->state
);
291 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK
], o
);
293 if (o
->direct_on_input
)
294 pa_idxset_remove_by_data(o
->direct_on_input
->direct_outputs
, o
, NULL
);
295 pa_idxset_remove_by_data(o
->source
->core
->source_outputs
, o
, NULL
);
296 if (pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
))
297 pa_source_output_unref(o
);
300 pa_idxset_remove_by_data(o
->client
->source_outputs
, o
, NULL
);
302 update_n_corked(o
, PA_SOURCE_OUTPUT_UNLINKED
);
303 o
->state
= PA_SOURCE_OUTPUT_UNLINKED
;
306 if (o
->source
->asyncmsgq
)
307 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
312 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, o
->index
);
313 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST
], o
);
316 pa_source_update_status(o
->source
);
319 pa_source_output_unref(o
);
322 /* Called from main context */
323 static void source_output_free(pa_object
* mo
) {
324 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
327 pa_assert(pa_source_output_refcnt(o
) == 0);
329 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
330 pa_source_output_unlink(o
);
332 pa_log_info("Freeing output %u \"%s\"", o
->index
, pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)));
334 pa_assert(!o
->thread_info
.attached
);
336 if (o
->thread_info
.delay_memblockq
)
337 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
339 if (o
->thread_info
.resampler
)
340 pa_resampler_free(o
->thread_info
.resampler
);
343 pa_proplist_free(o
->proplist
);
349 /* Called from main context */
350 void pa_source_output_put(pa_source_output
*o
) {
351 pa_source_output_state_t state
;
352 pa_source_output_assert_ref(o
);
354 pa_assert(o
->state
== PA_SOURCE_OUTPUT_INIT
);
356 /* The following fields must be initialized properly */
360 state
= o
->flags
& PA_SOURCE_OUTPUT_START_CORKED
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
;
362 update_n_corked(o
, state
);
365 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
367 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
, o
->index
);
368 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PUT
], o
);
370 pa_source_update_status(o
->source
);
373 /* Called from main context */
374 void pa_source_output_kill(pa_source_output
*o
) {
375 pa_source_output_assert_ref(o
);
376 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
381 /* Called from main context */
382 pa_usec_t
pa_source_output_get_latency(pa_source_output
*o
, pa_usec_t
*source_latency
) {
383 pa_usec_t r
[2] = { 0, 0 };
385 pa_source_output_assert_ref(o
);
386 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
388 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
, r
, 0, NULL
) == 0);
391 r
[0] += o
->get_latency(o
);
394 *source_latency
= r
[1];
399 /* Called from thread context */
400 void pa_source_output_push(pa_source_output
*o
, const pa_memchunk
*chunk
) {
402 size_t limit
, mbs
= 0;
404 pa_source_output_assert_ref(o
);
405 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
407 pa_assert(pa_frame_aligned(chunk
->length
, &o
->source
->sample_spec
));
409 if (!o
->push
|| o
->thread_info
.state
== PA_SOURCE_OUTPUT_CORKED
)
412 pa_assert(o
->thread_info
.state
== PA_SOURCE_OUTPUT_RUNNING
);
414 if (pa_memblockq_push(o
->thread_info
.delay_memblockq
, chunk
) < 0) {
415 pa_log_debug("Delay queue overflow!");
416 pa_memblockq_seek(o
->thread_info
.delay_memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
);
419 limit
= o
->process_rewind
? 0 : o
->source
->thread_info
.max_rewind
;
421 /* Implement the delay queue */
422 while ((length
= pa_memblockq_get_length(o
->thread_info
.delay_memblockq
)) > limit
) {
427 pa_assert_se(pa_memblockq_peek(o
->thread_info
.delay_memblockq
, &qchunk
) >= 0);
429 if (qchunk
.length
> length
)
430 qchunk
.length
= length
;
432 pa_assert(qchunk
.length
> 0);
434 if (!o
->thread_info
.resampler
)
440 mbs
= pa_resampler_max_block_size(o
->thread_info
.resampler
);
442 if (qchunk
.length
> mbs
)
445 pa_resampler_run(o
->thread_info
.resampler
, &qchunk
, &rchunk
);
447 if (rchunk
.length
> 0)
451 pa_memblock_unref(rchunk
.memblock
);
454 pa_memblock_unref(qchunk
.memblock
);
455 pa_memblockq_drop(o
->thread_info
.delay_memblockq
, qchunk
.length
);
459 /* Called from thread context */
460 void pa_source_output_process_rewind(pa_source_output
*o
, size_t nbytes
/* in source sample spec */) {
461 pa_source_output_assert_ref(o
);
463 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
464 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
469 if (o
->process_rewind
) {
470 pa_assert(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
) == 0);
472 if (o
->thread_info
.resampler
)
473 nbytes
= pa_resampler_result(o
->thread_info
.resampler
, nbytes
);
475 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) nbytes
);
478 o
->process_rewind(o
, nbytes
);
480 if (o
->thread_info
.resampler
)
481 pa_resampler_reset(o
->thread_info
.resampler
);
484 pa_memblockq_rewind(o
->thread_info
.delay_memblockq
, nbytes
);
487 /* Called from thread context */
488 void pa_source_output_update_max_rewind(pa_source_output
*o
, size_t nbytes
/* in the source's sample spec */) {
489 pa_source_output_assert_ref(o
);
490 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
491 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
493 if (o
->update_max_rewind
)
494 o
->update_max_rewind(o
, o
->thread_info
.resampler
? pa_resampler_result(o
->thread_info
.resampler
, nbytes
) : nbytes
);
497 /* Called from thread context */
498 static pa_usec_t
fixup_latency(pa_source
*s
, pa_usec_t usec
) {
499 pa_source_assert_ref(s
);
501 if (usec
== (pa_usec_t
) -1)
504 if (s
->thread_info
.max_latency
> 0 && usec
> s
->thread_info
.max_latency
)
505 usec
= s
->thread_info
.max_latency
;
507 if (s
->thread_info
.min_latency
> 0 && usec
< s
->thread_info
.min_latency
)
508 usec
= s
->thread_info
.min_latency
;
513 /* Called from thread context */
514 pa_usec_t
pa_source_output_set_requested_latency_within_thread(pa_source_output
*o
, pa_usec_t usec
) {
515 pa_source_output_assert_ref(o
);
517 usec
= fixup_latency(o
->source
, usec
);
518 o
->thread_info
.requested_source_latency
= usec
;
519 pa_source_invalidate_requested_latency(o
->source
);
524 /* Called from main context */
525 pa_usec_t
pa_source_output_set_requested_latency(pa_source_output
*o
, pa_usec_t usec
) {
526 pa_source_output_assert_ref(o
);
528 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
529 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
531 /* If this source output is not realized yet, we have to touch
532 * the thread info data directly */
534 o
->thread_info
.requested_source_latency
= usec
;
539 /* Called from main context */
540 pa_usec_t
pa_source_output_get_requested_latency(pa_source_output
*o
) {
543 pa_source_output_assert_ref(o
);
545 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
546 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
548 /* If this source output is not realized yet, we have to touch
549 * the thread info data directly */
550 usec
= o
->thread_info
.requested_source_latency
;
555 /* Called from main context */
556 void pa_source_output_cork(pa_source_output
*o
, pa_bool_t b
) {
557 pa_source_output_assert_ref(o
);
558 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
560 source_output_set_state(o
, b
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
);
563 /* Called from main context */
564 int pa_source_output_set_rate(pa_source_output
*o
, uint32_t rate
) {
565 pa_source_output_assert_ref(o
);
566 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
567 pa_return_val_if_fail(o
->thread_info
.resampler
, -1);
569 if (o
->sample_spec
.rate
== rate
)
572 o
->sample_spec
.rate
= rate
;
574 pa_asyncmsgq_post(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
, PA_UINT_TO_PTR(rate
), 0, NULL
, NULL
);
576 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
580 /* Called from main context */
581 void pa_source_output_set_name(pa_source_output
*o
, const char *name
) {
583 pa_source_output_assert_ref(o
);
585 if (!name
&& !pa_proplist_contains(o
->proplist
, PA_PROP_MEDIA_NAME
))
588 old
= pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
);
590 if (old
&& name
&& !strcmp(old
, name
))
594 pa_proplist_sets(o
->proplist
, PA_PROP_MEDIA_NAME
, name
);
596 pa_proplist_unset(o
->proplist
, PA_PROP_MEDIA_NAME
);
598 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
599 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
600 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
604 /* Called from main thread */
605 pa_bool_t
pa_source_output_update_proplist(pa_source_output
*o
, pa_update_mode_t mode
, pa_proplist
*p
) {
607 pa_source_output_assert_ref(o
);
609 pa_proplist_update(o
->proplist
, mode
, p
);
611 if (PA_SINK_IS_LINKED(o
->state
)) {
612 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
613 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
619 /* Called from main context */
620 pa_resample_method_t
pa_source_output_get_resample_method(pa_source_output
*o
) {
621 pa_source_output_assert_ref(o
);
623 return o
->resample_method
;
626 pa_bool_t
pa_source_output_may_move_to(pa_source_output
*o
, pa_source
*dest
) {
627 pa_source_output_assert_ref(o
);
628 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
629 pa_source_assert_ref(dest
);
631 if (dest
== o
->source
)
634 if (o
->flags
& PA_SOURCE_OUTPUT_DONT_MOVE
)
637 if (o
->direct_on_input
)
640 if (pa_idxset_size(dest
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
641 pa_log_warn("Failed to move source output: too many outputs per source.");
646 if (!o
->may_move_to(o
, dest
))
652 /* Called from main context */
653 int pa_source_output_move_to(pa_source_output
*o
, pa_source
*dest
) {
655 pa_resampler
*new_resampler
;
656 pa_source_output_move_hook_data hook_data
;
658 pa_source_output_assert_ref(o
);
659 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
660 pa_source_assert_ref(dest
);
667 if (!pa_source_output_may_move_to(o
, dest
))
670 if (o
->thread_info
.resampler
&&
671 pa_sample_spec_equal(&origin
->sample_spec
, &dest
->sample_spec
) &&
672 pa_channel_map_equal(&origin
->channel_map
, &dest
->channel_map
))
674 /* Try to reuse the old resampler if possible */
675 new_resampler
= o
->thread_info
.resampler
;
677 else if ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
678 !pa_sample_spec_equal(&o
->sample_spec
, &dest
->sample_spec
) ||
679 !pa_channel_map_equal(&o
->channel_map
, &dest
->channel_map
)) {
681 /* Okey, we need a new resampler for the new source */
683 if (!(new_resampler
= pa_resampler_new(
685 &dest
->sample_spec
, &dest
->channel_map
,
686 &o
->sample_spec
, &o
->channel_map
,
688 ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
689 ((o
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
690 (o
->core
->disable_remixing
|| (o
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0)))) {
691 pa_log_warn("Unsupported resampling operation.");
695 new_resampler
= NULL
;
697 hook_data
.source_output
= o
;
698 hook_data
.destination
= dest
;
699 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE
], &hook_data
);
701 /* Okey, let's move it */
702 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
704 pa_idxset_remove_by_data(origin
->outputs
, o
, NULL
);
705 pa_idxset_put(dest
->outputs
, o
, NULL
);
708 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
) {
709 pa_assert_se(origin
->n_corked
-- >= 1);
713 /* Replace resampler */
714 if (new_resampler
!= o
->thread_info
.resampler
) {
715 if (o
->thread_info
.resampler
)
716 pa_resampler_free(o
->thread_info
.resampler
);
717 o
->thread_info
.resampler
= new_resampler
;
719 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
721 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
725 pa_frame_size(&o
->source
->sample_spec
),
729 &o
->source
->silence
);
732 pa_source_update_status(origin
);
733 pa_source_update_status(dest
);
735 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
740 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_POST
], o
);
742 pa_log_debug("Successfully moved source output %i from %s to %s.", o
->index
, origin
->name
, dest
->name
);
744 /* Notify everyone */
745 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
750 /* Called from IO thread context */
751 void pa_source_output_set_state_within_thread(pa_source_output
*o
, pa_source_output_state_t state
) {
752 pa_source_output_assert_ref(o
);
754 if (state
== o
->thread_info
.state
)
758 o
->state_change(o
, state
);
760 o
->thread_info
.state
= state
;
763 /* Called from IO thread context, except when it is not */
764 int pa_source_output_process_msg(pa_msgobject
*mo
, int code
, void *userdata
, int64_t offset
, pa_memchunk
* chunk
) {
765 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
766 pa_source_output_assert_ref(o
);
770 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
: {
771 pa_usec_t
*r
= userdata
;
772 pa_usec_t source_usec
= 0;
774 r
[0] += pa_bytes_to_usec(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
), &o
->source
->sample_spec
);
776 if (o
->source
->parent
.process_msg(PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_GET_LATENCY
, &source_usec
, 0, NULL
) >= 0)
782 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
:
784 o
->thread_info
.sample_spec
.rate
= PA_PTR_TO_UINT(userdata
);
785 pa_resampler_set_output_rate(o
->thread_info
.resampler
, PA_PTR_TO_UINT(userdata
));
788 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE
:
790 pa_source_output_set_state_within_thread(o
, PA_PTR_TO_UINT(userdata
));
793 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
: {
794 pa_usec_t
*usec
= userdata
;
796 *usec
= pa_source_output_set_requested_latency_within_thread(o
, *usec
);
801 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
: {
802 pa_usec_t
*r
= userdata
;
804 *r
= o
->thread_info
.requested_source_latency
;