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
++;
250 pa_source_update_status(o
->source
);
253 /* Called from main context */
254 static int source_output_set_state(pa_source_output
*o
, pa_source_output_state_t state
) {
257 if (o
->state
== state
)
260 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);
262 update_n_corked(o
, state
);
265 if (state
!= PA_SOURCE_OUTPUT_UNLINKED
)
266 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED
], o
);
271 /* Called from main context */
272 void pa_source_output_unlink(pa_source_output
*o
) {
276 /* See pa_sink_unlink() for a couple of comments how this function
279 pa_source_output_ref(o
);
281 linked
= PA_SOURCE_OUTPUT_IS_LINKED(o
->state
);
284 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK
], o
);
286 if (o
->direct_on_input
)
287 pa_idxset_remove_by_data(o
->direct_on_input
->direct_outputs
, o
, NULL
);
288 pa_idxset_remove_by_data(o
->source
->core
->source_outputs
, o
, NULL
);
289 if (pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
))
290 pa_source_output_unref(o
);
292 update_n_corked(o
, PA_SOURCE_OUTPUT_UNLINKED
);
293 o
->state
= PA_SOURCE_OUTPUT_UNLINKED
;
296 if (o
->source
->asyncmsgq
)
297 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
302 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, o
->index
);
303 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST
], o
);
307 pa_source_output_unref(o
);
310 /* Called from main context */
311 static void source_output_free(pa_object
* mo
) {
312 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
315 pa_assert(pa_source_output_refcnt(o
) == 0);
317 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
318 pa_source_output_unlink(o
);
320 pa_log_info("Freeing output %u \"%s\"", o
->index
, pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)));
322 pa_assert(!o
->thread_info
.attached
);
324 if (o
->thread_info
.delay_memblockq
)
325 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
327 if (o
->thread_info
.resampler
)
328 pa_resampler_free(o
->thread_info
.resampler
);
331 pa_proplist_free(o
->proplist
);
337 /* Called from main context */
338 void pa_source_output_put(pa_source_output
*o
) {
339 pa_source_output_state_t state
;
340 pa_source_output_assert_ref(o
);
342 pa_assert(o
->state
== PA_SOURCE_OUTPUT_INIT
);
344 /* The following fields must be initialized properly */
348 state
= o
->flags
& PA_SOURCE_OUTPUT_START_CORKED
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
;
350 update_n_corked(o
, state
);
353 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
355 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
, o
->index
);
356 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PUT
], o
);
359 /* Called from main context */
360 void pa_source_output_kill(pa_source_output
*o
) {
361 pa_source_output_assert_ref(o
);
362 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
367 /* Called from main context */
368 pa_usec_t
pa_source_output_get_latency(pa_source_output
*o
, pa_usec_t
*source_latency
) {
369 pa_usec_t r
[2] = { 0, 0 };
371 pa_source_output_assert_ref(o
);
372 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
374 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
, r
, 0, NULL
) == 0);
377 r
[0] += o
->get_latency(o
);
380 *source_latency
= r
[1];
385 /* Called from thread context */
386 void pa_source_output_push(pa_source_output
*o
, const pa_memchunk
*chunk
) {
388 size_t limit
, mbs
= 0;
390 pa_source_output_assert_ref(o
);
391 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
393 pa_assert(pa_frame_aligned(chunk
->length
, &o
->source
->sample_spec
));
395 if (!o
->push
|| o
->thread_info
.state
== PA_SOURCE_OUTPUT_CORKED
)
398 pa_assert(o
->thread_info
.state
== PA_SOURCE_OUTPUT_RUNNING
);
400 if (pa_memblockq_push(o
->thread_info
.delay_memblockq
, chunk
) < 0) {
401 pa_log_debug("Delay queue overflow!");
402 pa_memblockq_seek(o
->thread_info
.delay_memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
);
405 limit
= o
->process_rewind
? 0 : o
->source
->thread_info
.max_rewind
;
407 /* Implement the delay queue */
408 while ((length
= pa_memblockq_get_length(o
->thread_info
.delay_memblockq
)) > limit
) {
413 pa_assert_se(pa_memblockq_peek(o
->thread_info
.delay_memblockq
, &qchunk
) >= 0);
415 if (qchunk
.length
> length
)
416 qchunk
.length
= length
;
418 pa_assert(qchunk
.length
> 0);
420 if (!o
->thread_info
.resampler
)
426 mbs
= pa_resampler_max_block_size(o
->thread_info
.resampler
);
428 if (qchunk
.length
> mbs
)
431 pa_resampler_run(o
->thread_info
.resampler
, &qchunk
, &rchunk
);
433 if (rchunk
.length
> 0)
437 pa_memblock_unref(rchunk
.memblock
);
440 pa_memblock_unref(qchunk
.memblock
);
441 pa_memblockq_drop(o
->thread_info
.delay_memblockq
, qchunk
.length
);
445 /* Called from thread context */
446 void pa_source_output_process_rewind(pa_source_output
*o
, size_t nbytes
/* in source sample spec */) {
447 pa_source_output_assert_ref(o
);
449 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
450 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
455 if (o
->process_rewind
) {
456 pa_assert(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
) == 0);
458 if (o
->thread_info
.resampler
)
459 nbytes
= pa_resampler_result(o
->thread_info
.resampler
, nbytes
);
461 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) nbytes
);
464 o
->process_rewind(o
, nbytes
);
466 if (o
->thread_info
.resampler
)
467 pa_resampler_reset(o
->thread_info
.resampler
);
470 pa_memblockq_rewind(o
->thread_info
.delay_memblockq
, nbytes
);
473 /* Called from thread context */
474 void pa_source_output_update_max_rewind(pa_source_output
*o
, size_t nbytes
/* in the source's sample spec */) {
475 pa_source_output_assert_ref(o
);
476 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
477 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
479 if (o
->update_max_rewind
)
480 o
->update_max_rewind(o
, o
->thread_info
.resampler
? pa_resampler_result(o
->thread_info
.resampler
, nbytes
) : nbytes
);
483 /* Called from thread context */
484 static pa_usec_t
fixup_latency(pa_source
*s
, pa_usec_t usec
) {
485 pa_source_assert_ref(s
);
487 if (usec
== (pa_usec_t
) -1)
490 if (s
->thread_info
.max_latency
> 0 && usec
> s
->thread_info
.max_latency
)
491 usec
= s
->thread_info
.max_latency
;
493 if (s
->thread_info
.min_latency
> 0 && usec
< s
->thread_info
.min_latency
)
494 usec
= s
->thread_info
.min_latency
;
499 /* Called from thread context */
500 pa_usec_t
pa_source_output_set_requested_latency_within_thread(pa_source_output
*o
, pa_usec_t usec
) {
501 pa_source_output_assert_ref(o
);
503 usec
= fixup_latency(o
->source
, usec
);
504 o
->thread_info
.requested_source_latency
= usec
;
505 pa_source_invalidate_requested_latency(o
->source
);
510 /* Called from main context */
511 pa_usec_t
pa_source_output_set_requested_latency(pa_source_output
*o
, pa_usec_t usec
) {
512 pa_source_output_assert_ref(o
);
514 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
515 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
517 /* If this source output is not realized yet, we have to touch
518 * the thread info data directly */
520 o
->thread_info
.requested_source_latency
= usec
;
525 /* Called from main context */
526 pa_usec_t
pa_source_output_get_requested_latency(pa_source_output
*o
) {
529 pa_source_output_assert_ref(o
);
531 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
532 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
534 /* If this source output is not realized yet, we have to touch
535 * the thread info data directly */
536 usec
= o
->thread_info
.requested_source_latency
;
541 /* Called from main context */
542 void pa_source_output_cork(pa_source_output
*o
, pa_bool_t b
) {
543 pa_source_output_assert_ref(o
);
544 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
546 source_output_set_state(o
, b
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
);
549 /* Called from main context */
550 int pa_source_output_set_rate(pa_source_output
*o
, uint32_t rate
) {
551 pa_source_output_assert_ref(o
);
552 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
553 pa_return_val_if_fail(o
->thread_info
.resampler
, -1);
555 if (o
->sample_spec
.rate
== rate
)
558 o
->sample_spec
.rate
= rate
;
560 pa_asyncmsgq_post(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
, PA_UINT_TO_PTR(rate
), 0, NULL
, NULL
);
562 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
566 /* Called from main context */
567 void pa_source_output_set_name(pa_source_output
*o
, const char *name
) {
569 pa_source_output_assert_ref(o
);
571 if (!name
&& !pa_proplist_contains(o
->proplist
, PA_PROP_MEDIA_NAME
))
574 old
= pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
);
576 if (old
&& name
&& !strcmp(old
, name
))
580 pa_proplist_sets(o
->proplist
, PA_PROP_MEDIA_NAME
, name
);
582 pa_proplist_unset(o
->proplist
, PA_PROP_MEDIA_NAME
);
584 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
585 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
586 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
590 /* Called from main context */
591 pa_resample_method_t
pa_source_output_get_resample_method(pa_source_output
*o
) {
592 pa_source_output_assert_ref(o
);
594 return o
->resample_method
;
597 pa_bool_t
pa_source_output_may_move_to(pa_source_output
*o
, pa_source
*dest
) {
598 pa_source_output_assert_ref(o
);
599 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
600 pa_source_assert_ref(dest
);
602 if (dest
== o
->source
)
605 if (o
->flags
& PA_SOURCE_OUTPUT_DONT_MOVE
)
608 if (o
->direct_on_input
)
611 if (pa_idxset_size(dest
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
612 pa_log_warn("Failed to move source output: too many outputs per source.");
617 if (!o
->may_move_to(o
, dest
))
623 /* Called from main context */
624 int pa_source_output_move_to(pa_source_output
*o
, pa_source
*dest
) {
626 pa_resampler
*new_resampler
;
627 pa_source_output_move_hook_data hook_data
;
629 pa_source_output_assert_ref(o
);
630 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
631 pa_source_assert_ref(dest
);
638 if (!pa_source_output_may_move_to(o
, dest
))
641 if (o
->thread_info
.resampler
&&
642 pa_sample_spec_equal(&origin
->sample_spec
, &dest
->sample_spec
) &&
643 pa_channel_map_equal(&origin
->channel_map
, &dest
->channel_map
))
645 /* Try to reuse the old resampler if possible */
646 new_resampler
= o
->thread_info
.resampler
;
648 else if ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
649 !pa_sample_spec_equal(&o
->sample_spec
, &dest
->sample_spec
) ||
650 !pa_channel_map_equal(&o
->channel_map
, &dest
->channel_map
)) {
652 /* Okey, we need a new resampler for the new source */
654 if (!(new_resampler
= pa_resampler_new(
656 &dest
->sample_spec
, &dest
->channel_map
,
657 &o
->sample_spec
, &o
->channel_map
,
659 ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
660 ((o
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
661 (o
->core
->disable_remixing
|| (o
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0)))) {
662 pa_log_warn("Unsupported resampling operation.");
666 new_resampler
= NULL
;
668 hook_data
.source_output
= o
;
669 hook_data
.destination
= dest
;
670 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE
], &hook_data
);
672 /* Okey, let's move it */
673 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
675 pa_idxset_remove_by_data(origin
->outputs
, o
, NULL
);
676 pa_idxset_put(dest
->outputs
, o
, NULL
);
679 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
) {
680 pa_assert_se(origin
->n_corked
-- >= 1);
684 /* Replace resampler */
685 if (new_resampler
!= o
->thread_info
.resampler
) {
686 if (o
->thread_info
.resampler
)
687 pa_resampler_free(o
->thread_info
.resampler
);
688 o
->thread_info
.resampler
= new_resampler
;
690 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
692 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
696 pa_frame_size(&o
->source
->sample_spec
),
700 &o
->source
->silence
);
703 pa_source_update_status(origin
);
704 pa_source_update_status(dest
);
706 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
711 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_POST
], o
);
713 pa_log_debug("Successfully moved source output %i from %s to %s.", o
->index
, origin
->name
, dest
->name
);
715 /* Notify everyone */
716 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
721 /* Called from IO thread context */
722 void pa_source_output_set_state_within_thread(pa_source_output
*o
, pa_source_output_state_t state
) {
723 pa_source_output_assert_ref(o
);
725 if (state
== o
->thread_info
.state
)
729 o
->state_change(o
, state
);
731 o
->thread_info
.state
= state
;
734 /* Called from IO thread context, except when it is not */
735 int pa_source_output_process_msg(pa_msgobject
*mo
, int code
, void *userdata
, int64_t offset
, pa_memchunk
* chunk
) {
736 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
737 pa_source_output_assert_ref(o
);
741 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
: {
742 pa_usec_t
*r
= userdata
;
743 pa_usec_t source_usec
= 0;
745 r
[0] += pa_bytes_to_usec(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
), &o
->source
->sample_spec
);
747 if (o
->source
->parent
.process_msg(PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_GET_LATENCY
, &source_usec
, 0, NULL
) >= 0)
753 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
:
755 o
->thread_info
.sample_spec
.rate
= PA_PTR_TO_UINT(userdata
);
756 pa_resampler_set_output_rate(o
->thread_info
.resampler
, PA_PTR_TO_UINT(userdata
));
759 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE
:
761 pa_source_output_set_state_within_thread(o
, PA_PTR_TO_UINT(userdata
));
764 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
: {
765 pa_usec_t
*usec
= userdata
;
767 *usec
= pa_source_output_set_requested_latency_within_thread(o
, *usec
);
772 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
: {
773 pa_usec_t
*r
= userdata
;
775 *r
= o
->thread_info
.requested_source_latency
;