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
;
95 /* Called from main context */
96 pa_source_output
* pa_source_output_new(
98 pa_source_output_new_data
*data
,
99 pa_source_output_flags_t flags
) {
102 pa_resampler
*resampler
= NULL
;
103 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
108 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_NEW
], data
) < 0)
111 pa_return_null_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
));
114 data
->source
= pa_namereg_get(core
, NULL
, PA_NAMEREG_SOURCE
, TRUE
);
116 pa_return_null_if_fail(data
->source
);
117 pa_return_null_if_fail(pa_source_get_state(data
->source
) != PA_SOURCE_UNLINKED
);
119 pa_return_null_if_fail(!data
->direct_on_input
|| data
->direct_on_input
->sink
== data
->source
->monitor_of
);
121 if (!data
->sample_spec_is_set
)
122 data
->sample_spec
= data
->source
->sample_spec
;
124 pa_return_null_if_fail(pa_sample_spec_valid(&data
->sample_spec
));
126 if (!data
->channel_map_is_set
) {
127 if (data
->source
->channel_map
.channels
== data
->sample_spec
.channels
)
128 data
->channel_map
= data
->source
->channel_map
;
130 pa_return_null_if_fail(pa_channel_map_init_auto(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
));
133 pa_return_null_if_fail(pa_channel_map_valid(&data
->channel_map
));
134 pa_return_null_if_fail(data
->channel_map
.channels
== data
->sample_spec
.channels
);
136 if (flags
& PA_SOURCE_OUTPUT_FIX_FORMAT
)
137 data
->sample_spec
.format
= data
->source
->sample_spec
.format
;
139 if (flags
& PA_SOURCE_OUTPUT_FIX_RATE
)
140 data
->sample_spec
.rate
= data
->source
->sample_spec
.rate
;
142 if (flags
& PA_SOURCE_OUTPUT_FIX_CHANNELS
) {
143 data
->sample_spec
.channels
= data
->source
->sample_spec
.channels
;
144 data
->channel_map
= data
->source
->channel_map
;
147 pa_assert(pa_sample_spec_valid(&data
->sample_spec
));
148 pa_assert(pa_channel_map_valid(&data
->channel_map
));
150 if (data
->resample_method
== PA_RESAMPLER_INVALID
)
151 data
->resample_method
= core
->resample_method
;
153 pa_return_null_if_fail(data
->resample_method
< PA_RESAMPLER_MAX
);
155 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE
], data
) < 0)
158 if (pa_idxset_size(data
->source
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
159 pa_log("Failed to create source output: too many outputs per source.");
163 if ((flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
164 !pa_sample_spec_equal(&data
->sample_spec
, &data
->source
->sample_spec
) ||
165 !pa_channel_map_equal(&data
->channel_map
, &data
->source
->channel_map
)) {
167 if (!(resampler
= pa_resampler_new(
169 &data
->source
->sample_spec
, &data
->source
->channel_map
,
170 &data
->sample_spec
, &data
->channel_map
,
171 data
->resample_method
,
172 ((flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
173 ((flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
174 (core
->disable_remixing
|| (flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0) |
175 (core
->disable_lfe_remixing
? PA_RESAMPLER_NO_LFE
: 0)))) {
176 pa_log_warn("Unsupported resampling operation.");
180 data
->resample_method
= pa_resampler_get_method(resampler
);
183 o
= pa_msgobject_new(pa_source_output
);
184 o
->parent
.parent
.free
= source_output_free
;
185 o
->parent
.process_msg
= pa_source_output_process_msg
;
188 o
->state
= PA_SOURCE_OUTPUT_INIT
;
190 o
->proplist
= pa_proplist_copy(data
->proplist
);
191 o
->driver
= pa_xstrdup(data
->driver
);
192 o
->module
= data
->module
;
193 o
->source
= data
->source
;
194 o
->client
= data
->client
;
196 o
->resample_method
= data
->resample_method
;
197 o
->sample_spec
= data
->sample_spec
;
198 o
->channel_map
= data
->channel_map
;
200 o
->direct_on_input
= data
->direct_on_input
;
205 o
->thread_info
.state
= o
->state
;
206 o
->thread_info
.attached
= FALSE
;
207 o
->thread_info
.sample_spec
= o
->sample_spec
;
208 o
->thread_info
.resampler
= resampler
;
209 o
->thread_info
.requested_source_latency
= (pa_usec_t
) -1;
210 o
->thread_info
.direct_on_input
= o
->direct_on_input
;
212 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
216 pa_frame_size(&o
->source
->sample_spec
),
220 &o
->source
->silence
);
222 pa_assert_se(pa_idxset_put(core
->source_outputs
, o
, &o
->index
) == 0);
223 pa_assert_se(pa_idxset_put(o
->source
->outputs
, pa_source_output_ref(o
), NULL
) == 0);
225 if (o
->direct_on_input
)
226 pa_assert_se(pa_idxset_put(o
->direct_on_input
->direct_outputs
, o
, NULL
) == 0);
228 pa_log_info("Created output %u \"%s\" on %s with sample spec %s and channel map %s",
230 pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)),
232 pa_sample_spec_snprint(st
, sizeof(st
), &o
->sample_spec
),
233 pa_channel_map_snprint(cm
, sizeof(cm
), &o
->channel_map
));
235 /* Don't forget to call pa_source_output_put! */
240 /* Called from main context */
241 static void update_n_corked(pa_source_output
*o
, pa_source_output_state_t state
) {
244 if (o
->state
== PA_SOURCE_OUTPUT_CORKED
&& state
!= PA_SOURCE_OUTPUT_CORKED
)
245 pa_assert_se(o
->source
->n_corked
-- >= 1);
246 else if (o
->state
!= PA_SOURCE_OUTPUT_CORKED
&& state
== PA_SOURCE_OUTPUT_CORKED
)
247 o
->source
->n_corked
++;
249 pa_source_update_status(o
->source
);
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
);
270 /* Called from main context */
271 void pa_source_output_unlink(pa_source_output
*o
) {
275 /* See pa_sink_unlink() for a couple of comments how this function
278 pa_source_output_ref(o
);
280 linked
= PA_SOURCE_OUTPUT_IS_LINKED(o
->state
);
283 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK
], o
);
285 if (o
->direct_on_input
)
286 pa_idxset_remove_by_data(o
->direct_on_input
->direct_outputs
, o
, NULL
);
287 pa_idxset_remove_by_data(o
->source
->core
->source_outputs
, o
, NULL
);
288 if (pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
))
289 pa_source_output_unref(o
);
291 update_n_corked(o
, PA_SOURCE_OUTPUT_UNLINKED
);
292 o
->state
= PA_SOURCE_OUTPUT_UNLINKED
;
295 if (o
->source
->asyncmsgq
)
296 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
301 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, o
->index
);
302 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST
], o
);
306 pa_source_output_unref(o
);
309 /* Called from main context */
310 static void source_output_free(pa_object
* mo
) {
311 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
314 pa_assert(pa_source_output_refcnt(o
) == 0);
316 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
317 pa_source_output_unlink(o
);
319 pa_log_info("Freeing output %u \"%s\"", o
->index
, pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)));
321 pa_assert(!o
->thread_info
.attached
);
323 if (o
->thread_info
.delay_memblockq
)
324 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
326 if (o
->thread_info
.resampler
)
327 pa_resampler_free(o
->thread_info
.resampler
);
330 pa_proplist_free(o
->proplist
);
336 /* Called from main context */
337 void pa_source_output_put(pa_source_output
*o
) {
338 pa_source_output_state_t state
;
339 pa_source_output_assert_ref(o
);
341 pa_assert(o
->state
== PA_SOURCE_OUTPUT_INIT
);
343 /* The following fields must be initialized properly */
347 state
= o
->flags
& PA_SOURCE_OUTPUT_START_CORKED
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
;
349 update_n_corked(o
, state
);
352 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
354 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
, o
->index
);
355 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PUT
], o
);
358 /* Called from main context */
359 void pa_source_output_kill(pa_source_output
*o
) {
360 pa_source_output_assert_ref(o
);
361 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
366 /* Called from main context */
367 pa_usec_t
pa_source_output_get_latency(pa_source_output
*o
, pa_usec_t
*source_latency
) {
368 pa_usec_t r
[2] = { 0, 0 };
370 pa_source_output_assert_ref(o
);
371 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
373 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
, r
, 0, NULL
) == 0);
376 r
[0] += o
->get_latency(o
);
379 *source_latency
= r
[1];
384 /* Called from thread context */
385 void pa_source_output_push(pa_source_output
*o
, const pa_memchunk
*chunk
) {
387 size_t limit
, mbs
= 0;
389 pa_source_output_assert_ref(o
);
390 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
392 pa_assert(pa_frame_aligned(chunk
->length
, &o
->source
->sample_spec
));
394 if (!o
->push
|| o
->thread_info
.state
== PA_SOURCE_OUTPUT_CORKED
)
397 pa_assert(o
->thread_info
.state
== PA_SOURCE_OUTPUT_RUNNING
);
399 if (pa_memblockq_push(o
->thread_info
.delay_memblockq
, chunk
) < 0) {
400 pa_log_debug("Delay queue overflow!");
401 pa_memblockq_seek(o
->thread_info
.delay_memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
);
404 limit
= o
->process_rewind
? 0 : o
->source
->thread_info
.max_rewind
;
406 /* Implement the delay queue */
407 while ((length
= pa_memblockq_get_length(o
->thread_info
.delay_memblockq
)) > limit
) {
412 pa_assert_se(pa_memblockq_peek(o
->thread_info
.delay_memblockq
, &qchunk
) >= 0);
414 if (qchunk
.length
> length
)
415 qchunk
.length
= length
;
417 pa_assert(qchunk
.length
> 0);
419 if (!o
->thread_info
.resampler
)
425 mbs
= pa_resampler_max_block_size(o
->thread_info
.resampler
);
427 if (qchunk
.length
> mbs
)
430 pa_resampler_run(o
->thread_info
.resampler
, &qchunk
, &rchunk
);
432 if (rchunk
.length
> 0)
436 pa_memblock_unref(rchunk
.memblock
);
439 pa_memblock_unref(qchunk
.memblock
);
440 pa_memblockq_drop(o
->thread_info
.delay_memblockq
, qchunk
.length
);
444 /* Called from thread context */
445 void pa_source_output_process_rewind(pa_source_output
*o
, size_t nbytes
/* in source sample spec */) {
446 pa_source_output_assert_ref(o
);
448 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
449 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
454 if (o
->process_rewind
) {
455 pa_assert(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
) == 0);
457 if (o
->thread_info
.resampler
)
458 nbytes
= pa_resampler_result(o
->thread_info
.resampler
, nbytes
);
460 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) nbytes
);
463 o
->process_rewind(o
, nbytes
);
465 if (o
->thread_info
.resampler
)
466 pa_resampler_reset(o
->thread_info
.resampler
);
469 pa_memblockq_rewind(o
->thread_info
.delay_memblockq
, nbytes
);
472 /* Called from thread context */
473 void pa_source_output_update_max_rewind(pa_source_output
*o
, size_t nbytes
/* in the source's sample spec */) {
474 pa_source_output_assert_ref(o
);
475 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
476 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
478 if (o
->update_max_rewind
)
479 o
->update_max_rewind(o
, o
->thread_info
.resampler
? pa_resampler_result(o
->thread_info
.resampler
, nbytes
) : nbytes
);
482 /* Called from thread context */
483 static pa_usec_t
fixup_latency(pa_source
*s
, pa_usec_t usec
) {
484 pa_source_assert_ref(s
);
486 if (usec
== (pa_usec_t
) -1)
489 if (s
->thread_info
.max_latency
> 0 && usec
> s
->thread_info
.max_latency
)
490 usec
= s
->thread_info
.max_latency
;
492 if (s
->thread_info
.min_latency
> 0 && usec
< s
->thread_info
.min_latency
)
493 usec
= s
->thread_info
.min_latency
;
498 /* Called from thread context */
499 pa_usec_t
pa_source_output_set_requested_latency_within_thread(pa_source_output
*o
, pa_usec_t usec
) {
500 pa_source_output_assert_ref(o
);
502 usec
= fixup_latency(o
->source
, usec
);
503 o
->thread_info
.requested_source_latency
= usec
;
504 pa_source_invalidate_requested_latency(o
->source
);
509 /* Called from main context */
510 pa_usec_t
pa_source_output_set_requested_latency(pa_source_output
*o
, pa_usec_t usec
) {
511 pa_source_output_assert_ref(o
);
513 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
514 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
516 /* If this source output is not realized yet, we have to touch
517 * the thread info data directly */
519 o
->thread_info
.requested_source_latency
= usec
;
524 /* Called from main context */
525 pa_usec_t
pa_source_output_get_requested_latency(pa_source_output
*o
) {
528 pa_source_output_assert_ref(o
);
530 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
531 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
533 /* If this source output is not realized yet, we have to touch
534 * the thread info data directly */
535 usec
= o
->thread_info
.requested_source_latency
;
540 /* Called from main context */
541 void pa_source_output_cork(pa_source_output
*o
, pa_bool_t b
) {
542 pa_source_output_assert_ref(o
);
543 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
545 source_output_set_state(o
, b
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
);
548 /* Called from main context */
549 int pa_source_output_set_rate(pa_source_output
*o
, uint32_t rate
) {
550 pa_source_output_assert_ref(o
);
551 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
552 pa_return_val_if_fail(o
->thread_info
.resampler
, -1);
554 if (o
->sample_spec
.rate
== rate
)
557 o
->sample_spec
.rate
= rate
;
559 pa_asyncmsgq_post(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
, PA_UINT_TO_PTR(rate
), 0, NULL
, NULL
);
561 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
565 /* Called from main context */
566 void pa_source_output_set_name(pa_source_output
*o
, const char *name
) {
568 pa_source_output_assert_ref(o
);
570 if (!name
&& !pa_proplist_contains(o
->proplist
, PA_PROP_MEDIA_NAME
))
573 old
= pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
);
575 if (old
&& name
&& !strcmp(old
, name
))
579 pa_proplist_sets(o
->proplist
, PA_PROP_MEDIA_NAME
, name
);
581 pa_proplist_unset(o
->proplist
, PA_PROP_MEDIA_NAME
);
583 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
584 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
585 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
589 /* Called from main context */
590 pa_resample_method_t
pa_source_output_get_resample_method(pa_source_output
*o
) {
591 pa_source_output_assert_ref(o
);
593 return o
->resample_method
;
596 /* Called from main context */
597 int pa_source_output_move_to(pa_source_output
*o
, pa_source
*dest
) {
599 pa_resampler
*new_resampler
;
600 pa_source_output_move_hook_data hook_data
;
602 pa_source_output_assert_ref(o
);
603 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
604 pa_source_assert_ref(dest
);
611 if (o
->flags
& PA_SOURCE_OUTPUT_DONT_MOVE
)
614 if (o
->direct_on_input
)
617 if (pa_idxset_size(dest
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
618 pa_log_warn("Failed to move source output: too many outputs per source.");
622 if (o
->thread_info
.resampler
&&
623 pa_sample_spec_equal(&origin
->sample_spec
, &dest
->sample_spec
) &&
624 pa_channel_map_equal(&origin
->channel_map
, &dest
->channel_map
))
626 /* Try to reuse the old resampler if possible */
627 new_resampler
= o
->thread_info
.resampler
;
629 else if ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
630 !pa_sample_spec_equal(&o
->sample_spec
, &dest
->sample_spec
) ||
631 !pa_channel_map_equal(&o
->channel_map
, &dest
->channel_map
)) {
633 /* Okey, we need a new resampler for the new source */
635 if (!(new_resampler
= pa_resampler_new(
637 &dest
->sample_spec
, &dest
->channel_map
,
638 &o
->sample_spec
, &o
->channel_map
,
640 ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
641 ((o
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
642 (o
->core
->disable_remixing
|| (o
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0)))) {
643 pa_log_warn("Unsupported resampling operation.");
647 new_resampler
= NULL
;
649 hook_data
.source_output
= o
;
650 hook_data
.destination
= dest
;
651 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE
], &hook_data
);
653 /* Okey, let's move it */
654 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
656 pa_idxset_remove_by_data(origin
->outputs
, o
, NULL
);
657 pa_idxset_put(dest
->outputs
, o
, NULL
);
660 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
) {
661 pa_assert_se(origin
->n_corked
-- >= 1);
665 /* Replace resampler */
666 if (new_resampler
!= o
->thread_info
.resampler
) {
667 if (o
->thread_info
.resampler
)
668 pa_resampler_free(o
->thread_info
.resampler
);
669 o
->thread_info
.resampler
= new_resampler
;
671 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
673 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
677 pa_frame_size(&o
->source
->sample_spec
),
681 &o
->source
->silence
);
684 pa_source_update_status(origin
);
685 pa_source_update_status(dest
);
687 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
692 pa_hook_fire(&o
->source
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_POST
], o
);
694 pa_log_debug("Successfully moved source output %i from %s to %s.", o
->index
, origin
->name
, dest
->name
);
696 /* Notify everyone */
697 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
702 /* Called from IO thread context */
703 void pa_source_output_set_state_within_thread(pa_source_output
*o
, pa_source_output_state_t state
) {
704 pa_source_output_assert_ref(o
);
706 if (state
== o
->thread_info
.state
)
710 o
->state_change(o
, state
);
712 o
->thread_info
.state
= state
;
715 /* Called from IO thread context, except when it is not */
716 int pa_source_output_process_msg(pa_msgobject
*mo
, int code
, void *userdata
, int64_t offset
, pa_memchunk
* chunk
) {
717 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
718 pa_source_output_assert_ref(o
);
722 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
: {
723 pa_usec_t
*r
= userdata
;
724 pa_usec_t source_usec
= 0;
726 r
[0] += pa_bytes_to_usec(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
), &o
->source
->sample_spec
);
728 if (o
->source
->parent
.process_msg(PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_GET_LATENCY
, &source_usec
, 0, NULL
) >= 0)
734 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
:
736 o
->thread_info
.sample_spec
.rate
= PA_PTR_TO_UINT(userdata
);
737 pa_resampler_set_output_rate(o
->thread_info
.resampler
, PA_PTR_TO_UINT(userdata
));
740 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE
:
742 pa_source_output_set_state_within_thread(o
, PA_PTR_TO_UINT(userdata
));
745 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
: {
746 pa_usec_t
*usec
= userdata
;
748 *usec
= pa_source_output_set_requested_latency_within_thread(o
, *usec
);
753 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
: {
754 pa_usec_t
*r
= userdata
;
756 *r
= o
->thread_info
.requested_source_latency
;