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
;
98 /* Called from main context */
99 int pa_source_output_new(
100 pa_source_output
**_o
,
102 pa_source_output_new_data
*data
,
103 pa_source_output_flags_t flags
) {
106 pa_resampler
*resampler
= NULL
;
107 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
115 pa_proplist_update(data
->proplist
, PA_UPDATE_MERGE
, data
->client
->proplist
);
117 if ((r
= pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_NEW
], data
)) < 0)
120 pa_return_val_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
), -PA_ERR_INVALID
);
123 data
->source
= pa_namereg_get(core
, NULL
, PA_NAMEREG_SOURCE
);
124 data
->save_source
= FALSE
;
127 pa_return_val_if_fail(data
->source
, -PA_ERR_NOENTITY
);
128 pa_return_val_if_fail(PA_SOURCE_IS_LINKED(pa_source_get_state(data
->source
)), -PA_ERR_BADSTATE
);
129 pa_return_val_if_fail(!data
->direct_on_input
|| data
->direct_on_input
->sink
== data
->source
->monitor_of
, -PA_ERR_INVALID
);
131 if (!data
->sample_spec_is_set
)
132 data
->sample_spec
= data
->source
->sample_spec
;
134 pa_return_val_if_fail(pa_sample_spec_valid(&data
->sample_spec
), -PA_ERR_INVALID
);
136 if (!data
->channel_map_is_set
) {
137 if (pa_channel_map_compatible(&data
->source
->channel_map
, &data
->sample_spec
))
138 data
->channel_map
= data
->source
->channel_map
;
140 pa_channel_map_init_extend(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
);
143 pa_return_val_if_fail(pa_channel_map_valid(&data
->channel_map
), -PA_ERR_INVALID
);
144 pa_return_val_if_fail(pa_channel_map_compatible(&data
->channel_map
, &data
->sample_spec
), -PA_ERR_INVALID
);
146 if (flags
& PA_SOURCE_OUTPUT_FIX_FORMAT
)
147 data
->sample_spec
.format
= data
->source
->sample_spec
.format
;
149 if (flags
& PA_SOURCE_OUTPUT_FIX_RATE
)
150 data
->sample_spec
.rate
= data
->source
->sample_spec
.rate
;
152 if (flags
& PA_SOURCE_OUTPUT_FIX_CHANNELS
) {
153 data
->sample_spec
.channels
= data
->source
->sample_spec
.channels
;
154 data
->channel_map
= data
->source
->channel_map
;
157 pa_assert(pa_sample_spec_valid(&data
->sample_spec
));
158 pa_assert(pa_channel_map_valid(&data
->channel_map
));
160 if (data
->resample_method
== PA_RESAMPLER_INVALID
)
161 data
->resample_method
= core
->resample_method
;
163 pa_return_val_if_fail(data
->resample_method
< PA_RESAMPLER_MAX
, -PA_ERR_INVALID
);
165 if ((r
= pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE
], data
)) < 0)
168 if ((flags
& PA_SOURCE_OUTPUT_FAIL_ON_SUSPEND
) &&
169 pa_source_get_state(data
->source
) == PA_SOURCE_SUSPENDED
) {
170 pa_log("Failed to create source output: source is suspended.");
171 return -PA_ERR_BADSTATE
;
174 if (pa_idxset_size(data
->source
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
175 pa_log("Failed to create source output: too many outputs per source.");
176 return -PA_ERR_TOOLARGE
;
179 if ((flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
180 !pa_sample_spec_equal(&data
->sample_spec
, &data
->source
->sample_spec
) ||
181 !pa_channel_map_equal(&data
->channel_map
, &data
->source
->channel_map
)) {
183 if (!(resampler
= pa_resampler_new(
185 &data
->source
->sample_spec
, &data
->source
->channel_map
,
186 &data
->sample_spec
, &data
->channel_map
,
187 data
->resample_method
,
188 ((flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
189 ((flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
190 (core
->disable_remixing
|| (flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0) |
191 (core
->disable_lfe_remixing
? PA_RESAMPLER_NO_LFE
: 0)))) {
192 pa_log_warn("Unsupported resampling operation.");
193 return -PA_ERR_NOTSUPPORTED
;
197 o
= pa_msgobject_new(pa_source_output
);
198 o
->parent
.parent
.free
= source_output_free
;
199 o
->parent
.process_msg
= pa_source_output_process_msg
;
202 o
->state
= PA_SOURCE_OUTPUT_INIT
;
204 o
->proplist
= pa_proplist_copy(data
->proplist
);
205 o
->driver
= pa_xstrdup(pa_path_get_filename(data
->driver
));
206 o
->module
= data
->module
;
207 o
->source
= data
->source
;
208 o
->client
= data
->client
;
210 o
->actual_resample_method
= resampler
? pa_resampler_get_method(resampler
) : PA_RESAMPLER_INVALID
;
211 o
->requested_resample_method
= data
->resample_method
;
212 o
->sample_spec
= data
->sample_spec
;
213 o
->channel_map
= data
->channel_map
;
215 o
->direct_on_input
= data
->direct_on_input
;
217 o
->save_source
= data
->save_source
;
222 o
->thread_info
.state
= o
->state
;
223 o
->thread_info
.attached
= FALSE
;
224 o
->thread_info
.sample_spec
= o
->sample_spec
;
225 o
->thread_info
.resampler
= resampler
;
226 o
->thread_info
.requested_source_latency
= (pa_usec_t
) -1;
227 o
->thread_info
.direct_on_input
= o
->direct_on_input
;
229 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
233 pa_frame_size(&o
->source
->sample_spec
),
237 &o
->source
->silence
);
239 pa_assert_se(pa_idxset_put(core
->source_outputs
, o
, &o
->index
) == 0);
240 pa_assert_se(pa_idxset_put(o
->source
->outputs
, pa_source_output_ref(o
), NULL
) == 0);
243 pa_assert_se(pa_idxset_put(o
->client
->source_outputs
, o
, NULL
) >= 0);
245 if (o
->direct_on_input
)
246 pa_assert_se(pa_idxset_put(o
->direct_on_input
->direct_outputs
, o
, NULL
) == 0);
248 pa_log_info("Created output %u \"%s\" on %s with sample spec %s and channel map %s",
250 pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)),
252 pa_sample_spec_snprint(st
, sizeof(st
), &o
->sample_spec
),
253 pa_channel_map_snprint(cm
, sizeof(cm
), &o
->channel_map
));
255 /* Don't forget to call pa_source_output_put! */
261 /* Called from main context */
262 static void update_n_corked(pa_source_output
*o
, pa_source_output_state_t state
) {
268 if (o
->state
== PA_SOURCE_OUTPUT_CORKED
&& state
!= PA_SOURCE_OUTPUT_CORKED
)
269 pa_assert_se(o
->source
->n_corked
-- >= 1);
270 else if (o
->state
!= PA_SOURCE_OUTPUT_CORKED
&& state
== PA_SOURCE_OUTPUT_CORKED
)
271 o
->source
->n_corked
++;
274 /* Called from main context */
275 static void source_output_set_state(pa_source_output
*o
, pa_source_output_state_t state
) {
278 if (o
->state
== state
)
281 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);
283 update_n_corked(o
, state
);
286 if (state
!= PA_SOURCE_OUTPUT_UNLINKED
)
287 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED
], o
);
289 pa_source_update_status(o
->source
);
292 /* Called from main context */
293 void pa_source_output_unlink(pa_source_output
*o
) {
297 /* See pa_sink_unlink() for a couple of comments how this function
300 pa_source_output_ref(o
);
302 linked
= PA_SOURCE_OUTPUT_IS_LINKED(o
->state
);
305 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK
], o
);
307 if (o
->direct_on_input
)
308 pa_idxset_remove_by_data(o
->direct_on_input
->direct_outputs
, o
, NULL
);
310 pa_idxset_remove_by_data(o
->core
->source_outputs
, o
, NULL
);
313 if (pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
))
314 pa_source_output_unref(o
);
317 pa_idxset_remove_by_data(o
->client
->source_outputs
, o
, NULL
);
319 update_n_corked(o
, PA_SOURCE_OUTPUT_UNLINKED
);
320 o
->state
= PA_SOURCE_OUTPUT_UNLINKED
;
322 if (linked
&& o
->source
)
323 if (o
->source
->asyncmsgq
)
324 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
329 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, o
->index
);
330 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST
], o
);
334 pa_source_update_status(o
->source
);
338 pa_core_maybe_vacuum(o
->core
);
340 pa_source_output_unref(o
);
343 /* Called from main context */
344 static void source_output_free(pa_object
* mo
) {
345 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
348 pa_assert(pa_source_output_refcnt(o
) == 0);
350 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
351 pa_source_output_unlink(o
);
353 pa_log_info("Freeing output %u \"%s\"", o
->index
, pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)));
355 pa_assert(!o
->thread_info
.attached
);
357 if (o
->thread_info
.delay_memblockq
)
358 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
360 if (o
->thread_info
.resampler
)
361 pa_resampler_free(o
->thread_info
.resampler
);
364 pa_proplist_free(o
->proplist
);
370 /* Called from main context */
371 void pa_source_output_put(pa_source_output
*o
) {
372 pa_source_output_state_t state
;
373 pa_source_output_assert_ref(o
);
375 pa_assert(o
->state
== PA_SOURCE_OUTPUT_INIT
);
377 /* The following fields must be initialized properly */
381 state
= o
->flags
& PA_SOURCE_OUTPUT_START_CORKED
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
;
383 update_n_corked(o
, state
);
386 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
388 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
, o
->index
);
389 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PUT
], o
);
391 pa_source_update_status(o
->source
);
394 /* Called from main context */
395 void pa_source_output_kill(pa_source_output
*o
) {
396 pa_source_output_assert_ref(o
);
397 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
402 /* Called from main context */
403 pa_usec_t
pa_source_output_get_latency(pa_source_output
*o
, pa_usec_t
*source_latency
) {
404 pa_usec_t r
[2] = { 0, 0 };
406 pa_source_output_assert_ref(o
);
407 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
409 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
, r
, 0, NULL
) == 0);
412 r
[0] += o
->get_latency(o
);
415 *source_latency
= r
[1];
420 /* Called from thread context */
421 void pa_source_output_push(pa_source_output
*o
, const pa_memchunk
*chunk
) {
423 size_t limit
, mbs
= 0;
425 pa_source_output_assert_ref(o
);
426 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
428 pa_assert(pa_frame_aligned(chunk
->length
, &o
->source
->sample_spec
));
430 if (!o
->push
|| o
->thread_info
.state
== PA_SOURCE_OUTPUT_CORKED
)
433 pa_assert(o
->thread_info
.state
== PA_SOURCE_OUTPUT_RUNNING
);
435 if (pa_memblockq_push(o
->thread_info
.delay_memblockq
, chunk
) < 0) {
436 pa_log_debug("Delay queue overflow!");
437 pa_memblockq_seek(o
->thread_info
.delay_memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
);
440 limit
= o
->process_rewind
? 0 : o
->source
->thread_info
.max_rewind
;
442 /* Implement the delay queue */
443 while ((length
= pa_memblockq_get_length(o
->thread_info
.delay_memblockq
)) > limit
) {
448 pa_assert_se(pa_memblockq_peek(o
->thread_info
.delay_memblockq
, &qchunk
) >= 0);
450 if (qchunk
.length
> length
)
451 qchunk
.length
= length
;
453 pa_assert(qchunk
.length
> 0);
455 if (!o
->thread_info
.resampler
)
461 mbs
= pa_resampler_max_block_size(o
->thread_info
.resampler
);
463 if (qchunk
.length
> mbs
)
466 pa_resampler_run(o
->thread_info
.resampler
, &qchunk
, &rchunk
);
468 if (rchunk
.length
> 0)
472 pa_memblock_unref(rchunk
.memblock
);
475 pa_memblock_unref(qchunk
.memblock
);
476 pa_memblockq_drop(o
->thread_info
.delay_memblockq
, qchunk
.length
);
480 /* Called from thread context */
481 void pa_source_output_process_rewind(pa_source_output
*o
, size_t nbytes
/* in source sample spec */) {
482 pa_source_output_assert_ref(o
);
484 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
485 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
490 if (o
->process_rewind
) {
491 pa_assert(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
) == 0);
493 if (o
->thread_info
.resampler
)
494 nbytes
= pa_resampler_result(o
->thread_info
.resampler
, nbytes
);
496 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) nbytes
);
499 o
->process_rewind(o
, nbytes
);
501 if (o
->thread_info
.resampler
)
502 pa_resampler_reset(o
->thread_info
.resampler
);
505 pa_memblockq_rewind(o
->thread_info
.delay_memblockq
, nbytes
);
508 /* Called from thread context */
509 void pa_source_output_update_max_rewind(pa_source_output
*o
, size_t nbytes
/* in the source's sample spec */) {
510 pa_source_output_assert_ref(o
);
511 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
512 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
514 if (o
->update_max_rewind
)
515 o
->update_max_rewind(o
, o
->thread_info
.resampler
? pa_resampler_result(o
->thread_info
.resampler
, nbytes
) : nbytes
);
518 /* Called from thread context */
519 static pa_usec_t
fixup_latency(pa_source
*s
, pa_usec_t usec
) {
520 pa_source_assert_ref(s
);
522 if (usec
== (pa_usec_t
) -1)
525 if (s
->thread_info
.max_latency
> 0 && usec
> s
->thread_info
.max_latency
)
526 usec
= s
->thread_info
.max_latency
;
528 if (s
->thread_info
.min_latency
> 0 && usec
< s
->thread_info
.min_latency
)
529 usec
= s
->thread_info
.min_latency
;
534 /* Called from thread context */
535 pa_usec_t
pa_source_output_set_requested_latency_within_thread(pa_source_output
*o
, pa_usec_t usec
) {
536 pa_source_output_assert_ref(o
);
538 usec
= fixup_latency(o
->source
, usec
);
539 o
->thread_info
.requested_source_latency
= usec
;
540 pa_source_invalidate_requested_latency(o
->source
);
545 /* Called from main context */
546 pa_usec_t
pa_source_output_set_requested_latency(pa_source_output
*o
, pa_usec_t usec
) {
547 pa_source_output_assert_ref(o
);
549 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
550 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
552 /* If this source output is not realized yet, we have to touch
553 * the thread info data directly */
555 o
->thread_info
.requested_source_latency
= usec
;
560 /* Called from main context */
561 pa_usec_t
pa_source_output_get_requested_latency(pa_source_output
*o
) {
564 pa_source_output_assert_ref(o
);
566 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
567 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
569 /* If this source output is not realized yet, we have to touch
570 * the thread info data directly */
571 usec
= o
->thread_info
.requested_source_latency
;
576 /* Called from main context */
577 void pa_source_output_cork(pa_source_output
*o
, pa_bool_t b
) {
578 pa_source_output_assert_ref(o
);
579 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
581 source_output_set_state(o
, b
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
);
584 /* Called from main context */
585 int pa_source_output_set_rate(pa_source_output
*o
, uint32_t rate
) {
586 pa_source_output_assert_ref(o
);
587 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
588 pa_return_val_if_fail(o
->thread_info
.resampler
, -PA_ERR_BADSTATE
);
590 if (o
->sample_spec
.rate
== rate
)
593 o
->sample_spec
.rate
= rate
;
595 pa_asyncmsgq_post(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
, PA_UINT_TO_PTR(rate
), 0, NULL
, NULL
);
597 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
601 /* Called from main context */
602 void pa_source_output_set_name(pa_source_output
*o
, const char *name
) {
604 pa_source_output_assert_ref(o
);
606 if (!name
&& !pa_proplist_contains(o
->proplist
, PA_PROP_MEDIA_NAME
))
609 old
= pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
);
611 if (old
&& name
&& !strcmp(old
, name
))
615 pa_proplist_sets(o
->proplist
, PA_PROP_MEDIA_NAME
, name
);
617 pa_proplist_unset(o
->proplist
, PA_PROP_MEDIA_NAME
);
619 if (PA_SOURCE_OUTPUT_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
);
625 /* Called from main thread */
626 void pa_source_output_update_proplist(pa_source_output
*o
, pa_update_mode_t mode
, pa_proplist
*p
) {
627 pa_source_output_assert_ref(o
);
630 pa_proplist_update(o
->proplist
, mode
, p
);
632 if (PA_SINK_IS_LINKED(o
->state
)) {
633 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
634 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
638 /* Called from main context */
639 pa_resample_method_t
pa_source_output_get_resample_method(pa_source_output
*o
) {
640 pa_source_output_assert_ref(o
);
642 return o
->actual_resample_method
;
645 /* Called from main context */
646 pa_bool_t
pa_source_output_may_move(pa_source_output
*o
) {
647 pa_source_output_assert_ref(o
);
648 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
650 if (o
->flags
& PA_SOURCE_OUTPUT_DONT_MOVE
)
653 if (o
->direct_on_input
)
659 /* Called from main context */
660 pa_bool_t
pa_source_output_may_move_to(pa_source_output
*o
, pa_source
*dest
) {
661 pa_source_output_assert_ref(o
);
662 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
663 pa_source_assert_ref(dest
);
665 if (dest
== o
->source
)
668 if (!pa_source_output_may_move(o
))
671 if (pa_idxset_size(dest
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
672 pa_log_warn("Failed to move source output: too many outputs per source.");
677 if (!o
->may_move_to(o
, dest
))
683 /* Called from main context */
684 int pa_source_output_start_move(pa_source_output
*o
) {
688 pa_source_output_assert_ref(o
);
689 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
690 pa_assert(o
->source
);
692 if (!pa_source_output_may_move(o
))
693 return -PA_ERR_NOTSUPPORTED
;
695 if ((r
= pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START
], o
)) < 0)
700 pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
);
702 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
)
703 pa_assert_se(origin
->n_corked
-- >= 1);
705 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
707 pa_source_update_status(o
->source
);
713 /* Called from main context */
714 int pa_source_output_finish_move(pa_source_output
*o
, pa_source
*dest
, pa_bool_t save
) {
715 pa_resampler
*new_resampler
;
717 pa_source_output_assert_ref(o
);
718 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
719 pa_assert(!o
->source
);
720 pa_source_assert_ref(dest
);
722 if (!pa_source_output_may_move_to(o
, dest
))
725 if (o
->thread_info
.resampler
&&
726 pa_sample_spec_equal(pa_resampler_input_sample_spec(o
->thread_info
.resampler
), &dest
->sample_spec
) &&
727 pa_channel_map_equal(pa_resampler_input_channel_map(o
->thread_info
.resampler
), &dest
->channel_map
))
729 /* Try to reuse the old resampler if possible */
730 new_resampler
= o
->thread_info
.resampler
;
732 else if ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
733 !pa_sample_spec_equal(&o
->sample_spec
, &dest
->sample_spec
) ||
734 !pa_channel_map_equal(&o
->channel_map
, &dest
->channel_map
)) {
736 /* Okey, we need a new resampler for the new source */
738 if (!(new_resampler
= pa_resampler_new(
740 &dest
->sample_spec
, &dest
->channel_map
,
741 &o
->sample_spec
, &o
->channel_map
,
742 o
->requested_resample_method
,
743 ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
744 ((o
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
745 (o
->core
->disable_remixing
|| (o
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0)))) {
746 pa_log_warn("Unsupported resampling operation.");
747 return -PA_ERR_NOTSUPPORTED
;
750 new_resampler
= NULL
;
753 o
->save_source
= save
;
754 pa_idxset_put(o
->source
->outputs
, o
, NULL
);
756 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
)
757 o
->source
->n_corked
++;
759 /* Replace resampler */
760 if (new_resampler
!= o
->thread_info
.resampler
) {
761 if (o
->thread_info
.resampler
)
762 pa_resampler_free(o
->thread_info
.resampler
);
763 o
->thread_info
.resampler
= new_resampler
;
765 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
767 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
771 pa_frame_size(&o
->source
->sample_spec
),
775 &o
->source
->silence
);
778 pa_source_update_status(dest
);
779 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
781 pa_log_debug("Successfully moved source output %i to %s.", o
->index
, dest
->name
);
783 /* Notify everyone */
787 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH
], o
);
788 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
793 /* Called from main context */
794 int pa_source_output_move_to(pa_source_output
*o
, pa_source
*dest
, pa_bool_t save
) {
797 pa_source_output_assert_ref(o
);
798 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
799 pa_assert(o
->source
);
800 pa_source_assert_ref(dest
);
802 if (dest
== o
->source
)
805 if (!pa_source_output_may_move_to(o
, dest
))
806 return -PA_ERR_NOTSUPPORTED
;
808 if ((r
= pa_source_output_start_move(o
)) < 0)
811 if ((r
= pa_source_output_finish_move(o
, dest
, save
)) < 0)
817 /* Called from IO thread context */
818 void pa_source_output_set_state_within_thread(pa_source_output
*o
, pa_source_output_state_t state
) {
819 pa_source_output_assert_ref(o
);
821 if (state
== o
->thread_info
.state
)
825 o
->state_change(o
, state
);
827 o
->thread_info
.state
= state
;
830 /* Called from IO thread context, except when it is not */
831 int pa_source_output_process_msg(pa_msgobject
*mo
, int code
, void *userdata
, int64_t offset
, pa_memchunk
* chunk
) {
832 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
833 pa_source_output_assert_ref(o
);
837 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
: {
838 pa_usec_t
*r
= userdata
;
839 pa_usec_t source_usec
= 0;
841 r
[0] += pa_bytes_to_usec(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
), &o
->source
->sample_spec
);
843 if (o
->source
->parent
.process_msg(PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_GET_LATENCY
, &source_usec
, 0, NULL
) >= 0)
849 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
:
851 o
->thread_info
.sample_spec
.rate
= PA_PTR_TO_UINT(userdata
);
852 pa_resampler_set_output_rate(o
->thread_info
.resampler
, PA_PTR_TO_UINT(userdata
));
855 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE
:
857 pa_source_output_set_state_within_thread(o
, PA_PTR_TO_UINT(userdata
));
860 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
: {
861 pa_usec_t
*usec
= userdata
;
863 *usec
= pa_source_output_set_requested_latency_within_thread(o
, *usec
);
868 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
: {
869 pa_usec_t
*r
= userdata
;
871 *r
= o
->thread_info
.requested_source_latency
;
876 return -PA_ERR_NOTIMPLEMENTED
;
879 void pa_source_output_send_event(pa_source_output
*o
, const char *event
, pa_proplist
*data
) {
880 pa_proplist
*pl
= NULL
;
881 pa_source_output_send_event_hook_data hook_data
;
883 pa_source_output_assert_ref(o
);
890 data
= pl
= pa_proplist_new();
892 hook_data
.source_output
= o
;
893 hook_data
.data
= data
;
894 hook_data
.event
= event
;
896 if (pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_SEND_EVENT
], &hook_data
) < 0)
899 o
->send_event(o
, event
, data
);
903 pa_proplist_free(pl
);