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
, TRUE
);
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
);
156 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE
], data
) < 0)
159 if (pa_idxset_size(data
->source
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
160 pa_log("Failed to create source output: too many outputs per source.");
164 if ((flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
165 !pa_sample_spec_equal(&data
->sample_spec
, &data
->source
->sample_spec
) ||
166 !pa_channel_map_equal(&data
->channel_map
, &data
->source
->channel_map
)) {
168 if (!(resampler
= pa_resampler_new(
170 &data
->source
->sample_spec
, &data
->source
->channel_map
,
171 &data
->sample_spec
, &data
->channel_map
,
172 data
->resample_method
,
173 ((flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
174 ((flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
175 (core
->disable_remixing
|| (flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0) |
176 (core
->disable_lfe_remixing
? PA_RESAMPLER_NO_LFE
: 0)))) {
177 pa_log_warn("Unsupported resampling operation.");
181 data
->resample_method
= pa_resampler_get_method(resampler
);
184 o
= pa_msgobject_new(pa_source_output
);
185 o
->parent
.parent
.free
= source_output_free
;
186 o
->parent
.process_msg
= pa_source_output_process_msg
;
189 o
->state
= PA_SOURCE_OUTPUT_INIT
;
191 o
->proplist
= pa_proplist_copy(data
->proplist
);
192 o
->driver
= pa_xstrdup(data
->driver
);
193 o
->module
= data
->module
;
194 o
->source
= data
->source
;
195 o
->client
= data
->client
;
197 o
->resample_method
= data
->resample_method
;
198 o
->sample_spec
= data
->sample_spec
;
199 o
->channel_map
= data
->channel_map
;
201 o
->direct_on_input
= data
->direct_on_input
;
206 o
->thread_info
.state
= o
->state
;
207 o
->thread_info
.attached
= FALSE
;
208 o
->thread_info
.sample_spec
= o
->sample_spec
;
209 o
->thread_info
.resampler
= resampler
;
210 o
->thread_info
.requested_source_latency
= (pa_usec_t
) -1;
211 o
->thread_info
.direct_on_input
= o
->direct_on_input
;
213 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
217 pa_frame_size(&o
->source
->sample_spec
),
221 &o
->source
->silence
);
223 pa_assert_se(pa_idxset_put(core
->source_outputs
, o
, &o
->index
) == 0);
224 pa_assert_se(pa_idxset_put(o
->source
->outputs
, pa_source_output_ref(o
), NULL
) == 0);
226 if (o
->direct_on_input
)
227 pa_assert_se(pa_idxset_put(o
->direct_on_input
->direct_outputs
, o
, NULL
) == 0);
229 pa_log_info("Created output %u \"%s\" on %s with sample spec %s and channel map %s",
231 pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)),
233 pa_sample_spec_snprint(st
, sizeof(st
), &o
->sample_spec
),
234 pa_channel_map_snprint(cm
, sizeof(cm
), &o
->channel_map
));
236 /* Don't forget to call pa_source_output_put! */
241 /* Called from main context */
242 static void update_n_corked(pa_source_output
*o
, pa_source_output_state_t state
) {
245 if (o
->state
== PA_SOURCE_OUTPUT_CORKED
&& state
!= PA_SOURCE_OUTPUT_CORKED
)
246 pa_assert_se(o
->source
->n_corked
-- >= 1);
247 else if (o
->state
!= PA_SOURCE_OUTPUT_CORKED
&& state
== PA_SOURCE_OUTPUT_CORKED
)
248 o
->source
->n_corked
++;
252 /* Called from main context */
253 static int source_output_set_state(pa_source_output
*o
, pa_source_output_state_t state
) {
256 if (o
->state
== state
)
259 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);
261 update_n_corked(o
, state
);
264 if (state
!= PA_SOURCE_OUTPUT_UNLINKED
)
265 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED
], o
);
267 pa_source_update_status(o
->source
);
272 /* Called from main context */
273 void pa_source_output_unlink(pa_source_output
*o
) {
277 /* See pa_sink_unlink() for a couple of comments how this function
280 pa_source_output_ref(o
);
282 linked
= PA_SOURCE_OUTPUT_IS_LINKED(o
->state
);
285 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK
], o
);
287 if (o
->direct_on_input
)
288 pa_idxset_remove_by_data(o
->direct_on_input
->direct_outputs
, o
, NULL
);
289 pa_idxset_remove_by_data(o
->source
->core
->source_outputs
, o
, NULL
);
290 if (pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
))
291 pa_source_output_unref(o
);
293 update_n_corked(o
, PA_SOURCE_OUTPUT_UNLINKED
);
294 o
->state
= PA_SOURCE_OUTPUT_UNLINKED
;
297 if (o
->source
->asyncmsgq
)
298 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
303 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, o
->index
);
304 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST
], o
);
307 pa_source_update_status(o
->source
);
310 pa_source_output_unref(o
);
313 /* Called from main context */
314 static void source_output_free(pa_object
* mo
) {
315 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
318 pa_assert(pa_source_output_refcnt(o
) == 0);
320 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
321 pa_source_output_unlink(o
);
323 pa_log_info("Freeing output %u \"%s\"", o
->index
, pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)));
325 pa_assert(!o
->thread_info
.attached
);
327 if (o
->thread_info
.delay_memblockq
)
328 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
330 if (o
->thread_info
.resampler
)
331 pa_resampler_free(o
->thread_info
.resampler
);
334 pa_proplist_free(o
->proplist
);
340 /* Called from main context */
341 void pa_source_output_put(pa_source_output
*o
) {
342 pa_source_output_state_t state
;
343 pa_source_output_assert_ref(o
);
345 pa_assert(o
->state
== PA_SOURCE_OUTPUT_INIT
);
347 /* The following fields must be initialized properly */
351 state
= o
->flags
& PA_SOURCE_OUTPUT_START_CORKED
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
;
353 update_n_corked(o
, state
);
356 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
358 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
, o
->index
);
359 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PUT
], o
);
361 pa_source_update_status(o
->source
);
364 /* Called from main context */
365 void pa_source_output_kill(pa_source_output
*o
) {
366 pa_source_output_assert_ref(o
);
367 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
372 /* Called from main context */
373 pa_usec_t
pa_source_output_get_latency(pa_source_output
*o
, pa_usec_t
*source_latency
) {
374 pa_usec_t r
[2] = { 0, 0 };
376 pa_source_output_assert_ref(o
);
377 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
379 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
, r
, 0, NULL
) == 0);
382 r
[0] += o
->get_latency(o
);
385 *source_latency
= r
[1];
390 /* Called from thread context */
391 void pa_source_output_push(pa_source_output
*o
, const pa_memchunk
*chunk
) {
393 size_t limit
, mbs
= 0;
395 pa_source_output_assert_ref(o
);
396 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
398 pa_assert(pa_frame_aligned(chunk
->length
, &o
->source
->sample_spec
));
400 if (!o
->push
|| o
->thread_info
.state
== PA_SOURCE_OUTPUT_CORKED
)
403 pa_assert(o
->thread_info
.state
== PA_SOURCE_OUTPUT_RUNNING
);
405 if (pa_memblockq_push(o
->thread_info
.delay_memblockq
, chunk
) < 0) {
406 pa_log_debug("Delay queue overflow!");
407 pa_memblockq_seek(o
->thread_info
.delay_memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
);
410 limit
= o
->process_rewind
? 0 : o
->source
->thread_info
.max_rewind
;
412 /* Implement the delay queue */
413 while ((length
= pa_memblockq_get_length(o
->thread_info
.delay_memblockq
)) > limit
) {
418 pa_assert_se(pa_memblockq_peek(o
->thread_info
.delay_memblockq
, &qchunk
) >= 0);
420 if (qchunk
.length
> length
)
421 qchunk
.length
= length
;
423 pa_assert(qchunk
.length
> 0);
425 if (!o
->thread_info
.resampler
)
431 mbs
= pa_resampler_max_block_size(o
->thread_info
.resampler
);
433 if (qchunk
.length
> mbs
)
436 pa_resampler_run(o
->thread_info
.resampler
, &qchunk
, &rchunk
);
438 if (rchunk
.length
> 0)
442 pa_memblock_unref(rchunk
.memblock
);
445 pa_memblock_unref(qchunk
.memblock
);
446 pa_memblockq_drop(o
->thread_info
.delay_memblockq
, qchunk
.length
);
450 /* Called from thread context */
451 void pa_source_output_process_rewind(pa_source_output
*o
, size_t nbytes
/* in source sample spec */) {
452 pa_source_output_assert_ref(o
);
454 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
455 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
460 if (o
->process_rewind
) {
461 pa_assert(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
) == 0);
463 if (o
->thread_info
.resampler
)
464 nbytes
= pa_resampler_result(o
->thread_info
.resampler
, nbytes
);
466 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) nbytes
);
469 o
->process_rewind(o
, nbytes
);
471 if (o
->thread_info
.resampler
)
472 pa_resampler_reset(o
->thread_info
.resampler
);
475 pa_memblockq_rewind(o
->thread_info
.delay_memblockq
, nbytes
);
478 /* Called from thread context */
479 void pa_source_output_update_max_rewind(pa_source_output
*o
, size_t nbytes
/* in the source's sample spec */) {
480 pa_source_output_assert_ref(o
);
481 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
482 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
484 if (o
->update_max_rewind
)
485 o
->update_max_rewind(o
, o
->thread_info
.resampler
? pa_resampler_result(o
->thread_info
.resampler
, nbytes
) : nbytes
);
488 /* Called from thread context */
489 static pa_usec_t
fixup_latency(pa_source
*s
, pa_usec_t usec
) {
490 pa_source_assert_ref(s
);
492 if (usec
== (pa_usec_t
) -1)
495 if (s
->thread_info
.max_latency
> 0 && usec
> s
->thread_info
.max_latency
)
496 usec
= s
->thread_info
.max_latency
;
498 if (s
->thread_info
.min_latency
> 0 && usec
< s
->thread_info
.min_latency
)
499 usec
= s
->thread_info
.min_latency
;
504 /* Called from thread context */
505 pa_usec_t
pa_source_output_set_requested_latency_within_thread(pa_source_output
*o
, pa_usec_t usec
) {
506 pa_source_output_assert_ref(o
);
508 usec
= fixup_latency(o
->source
, usec
);
509 o
->thread_info
.requested_source_latency
= usec
;
510 pa_source_invalidate_requested_latency(o
->source
);
515 /* Called from main context */
516 pa_usec_t
pa_source_output_set_requested_latency(pa_source_output
*o
, pa_usec_t usec
) {
517 pa_source_output_assert_ref(o
);
519 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
520 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
522 /* If this source output is not realized yet, we have to touch
523 * the thread info data directly */
525 o
->thread_info
.requested_source_latency
= usec
;
530 /* Called from main context */
531 pa_usec_t
pa_source_output_get_requested_latency(pa_source_output
*o
) {
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_GET_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 */
541 usec
= o
->thread_info
.requested_source_latency
;
546 /* Called from main context */
547 void pa_source_output_cork(pa_source_output
*o
, pa_bool_t b
) {
548 pa_source_output_assert_ref(o
);
549 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
551 source_output_set_state(o
, b
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
);
554 /* Called from main context */
555 int pa_source_output_set_rate(pa_source_output
*o
, uint32_t rate
) {
556 pa_source_output_assert_ref(o
);
557 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
558 pa_return_val_if_fail(o
->thread_info
.resampler
, -1);
560 if (o
->sample_spec
.rate
== rate
)
563 o
->sample_spec
.rate
= rate
;
565 pa_asyncmsgq_post(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
, PA_UINT_TO_PTR(rate
), 0, NULL
, NULL
);
567 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
571 /* Called from main context */
572 void pa_source_output_set_name(pa_source_output
*o
, const char *name
) {
574 pa_source_output_assert_ref(o
);
576 if (!name
&& !pa_proplist_contains(o
->proplist
, PA_PROP_MEDIA_NAME
))
579 old
= pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
);
581 if (old
&& name
&& !strcmp(old
, name
))
585 pa_proplist_sets(o
->proplist
, PA_PROP_MEDIA_NAME
, name
);
587 pa_proplist_unset(o
->proplist
, PA_PROP_MEDIA_NAME
);
589 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
590 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
591 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
595 /* Called from main context */
596 pa_resample_method_t
pa_source_output_get_resample_method(pa_source_output
*o
) {
597 pa_source_output_assert_ref(o
);
599 return o
->resample_method
;
602 pa_bool_t
pa_source_output_may_move_to(pa_source_output
*o
, pa_source
*dest
) {
603 pa_source_output_assert_ref(o
);
604 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
605 pa_source_assert_ref(dest
);
607 if (dest
== o
->source
)
610 if (o
->flags
& PA_SOURCE_OUTPUT_DONT_MOVE
)
613 if (o
->direct_on_input
)
616 if (pa_idxset_size(dest
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
617 pa_log_warn("Failed to move source output: too many outputs per source.");
622 if (!o
->may_move_to(o
, dest
))
628 /* Called from main context */
629 int pa_source_output_move_to(pa_source_output
*o
, pa_source
*dest
) {
631 pa_resampler
*new_resampler
;
632 pa_source_output_move_hook_data hook_data
;
634 pa_source_output_assert_ref(o
);
635 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
636 pa_source_assert_ref(dest
);
643 if (!pa_source_output_may_move_to(o
, dest
))
646 if (o
->thread_info
.resampler
&&
647 pa_sample_spec_equal(&origin
->sample_spec
, &dest
->sample_spec
) &&
648 pa_channel_map_equal(&origin
->channel_map
, &dest
->channel_map
))
650 /* Try to reuse the old resampler if possible */
651 new_resampler
= o
->thread_info
.resampler
;
653 else if ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
654 !pa_sample_spec_equal(&o
->sample_spec
, &dest
->sample_spec
) ||
655 !pa_channel_map_equal(&o
->channel_map
, &dest
->channel_map
)) {
657 /* Okey, we need a new resampler for the new source */
659 if (!(new_resampler
= pa_resampler_new(
661 &dest
->sample_spec
, &dest
->channel_map
,
662 &o
->sample_spec
, &o
->channel_map
,
664 ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
665 ((o
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
666 (o
->core
->disable_remixing
|| (o
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0)))) {
667 pa_log_warn("Unsupported resampling operation.");
671 new_resampler
= NULL
;
673 hook_data
.source_output
= o
;
674 hook_data
.destination
= dest
;
675 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE
], &hook_data
);
677 /* Okey, let's move it */
678 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
680 pa_idxset_remove_by_data(origin
->outputs
, o
, NULL
);
681 pa_idxset_put(dest
->outputs
, o
, NULL
);
684 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
) {
685 pa_assert_se(origin
->n_corked
-- >= 1);
689 /* Replace resampler */
690 if (new_resampler
!= o
->thread_info
.resampler
) {
691 if (o
->thread_info
.resampler
)
692 pa_resampler_free(o
->thread_info
.resampler
);
693 o
->thread_info
.resampler
= new_resampler
;
695 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
697 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
701 pa_frame_size(&o
->source
->sample_spec
),
705 &o
->source
->silence
);
708 pa_source_update_status(origin
);
709 pa_source_update_status(dest
);
711 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
716 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_POST
], o
);
718 pa_log_debug("Successfully moved source output %i from %s to %s.", o
->index
, origin
->name
, dest
->name
);
720 /* Notify everyone */
721 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
726 /* Called from IO thread context */
727 void pa_source_output_set_state_within_thread(pa_source_output
*o
, pa_source_output_state_t state
) {
728 pa_source_output_assert_ref(o
);
730 if (state
== o
->thread_info
.state
)
734 o
->state_change(o
, state
);
736 o
->thread_info
.state
= state
;
739 /* Called from IO thread context, except when it is not */
740 int pa_source_output_process_msg(pa_msgobject
*mo
, int code
, void *userdata
, int64_t offset
, pa_memchunk
* chunk
) {
741 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
742 pa_source_output_assert_ref(o
);
746 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
: {
747 pa_usec_t
*r
= userdata
;
748 pa_usec_t source_usec
= 0;
750 r
[0] += pa_bytes_to_usec(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
), &o
->source
->sample_spec
);
752 if (o
->source
->parent
.process_msg(PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_GET_LATENCY
, &source_usec
, 0, NULL
) >= 0)
758 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
:
760 o
->thread_info
.sample_spec
.rate
= PA_PTR_TO_UINT(userdata
);
761 pa_resampler_set_output_rate(o
->thread_info
.resampler
, PA_PTR_TO_UINT(userdata
));
764 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE
:
766 pa_source_output_set_state_within_thread(o
, PA_PTR_TO_UINT(userdata
));
769 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
: {
770 pa_usec_t
*usec
= userdata
;
772 *usec
= pa_source_output_set_requested_latency_within_thread(o
, *usec
);
777 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
: {
778 pa_usec_t
*r
= userdata
;
780 *r
= o
->thread_info
.requested_source_latency
;