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.1 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 PA_DEFINE_PUBLIC_CLASS(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
) {
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
;
87 o
->update_source_fixed_latency
= NULL
;
91 o
->suspend_within_thread
= NULL
;
94 o
->get_latency
= NULL
;
95 o
->state_change
= NULL
;
96 o
->may_move_to
= NULL
;
100 /* Called from main context */
101 int pa_source_output_new(
102 pa_source_output
**_o
,
104 pa_source_output_new_data
*data
) {
107 pa_resampler
*resampler
= NULL
;
108 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
115 pa_assert_ctl_context();
118 pa_proplist_update(data
->proplist
, PA_UPDATE_MERGE
, data
->client
->proplist
);
120 if ((r
= pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_NEW
], data
)) < 0)
123 pa_return_val_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
), -PA_ERR_INVALID
);
126 data
->source
= pa_namereg_get(core
, NULL
, PA_NAMEREG_SOURCE
);
127 data
->save_source
= FALSE
;
130 pa_return_val_if_fail(data
->source
, -PA_ERR_NOENTITY
);
131 pa_return_val_if_fail(PA_SOURCE_IS_LINKED(pa_source_get_state(data
->source
)), -PA_ERR_BADSTATE
);
132 pa_return_val_if_fail(!data
->direct_on_input
|| data
->direct_on_input
->sink
== data
->source
->monitor_of
, -PA_ERR_INVALID
);
134 if (!data
->sample_spec_is_set
)
135 data
->sample_spec
= data
->source
->sample_spec
;
137 pa_return_val_if_fail(pa_sample_spec_valid(&data
->sample_spec
), -PA_ERR_INVALID
);
139 if (!data
->channel_map_is_set
) {
140 if (pa_channel_map_compatible(&data
->source
->channel_map
, &data
->sample_spec
))
141 data
->channel_map
= data
->source
->channel_map
;
143 pa_channel_map_init_extend(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
);
146 pa_return_val_if_fail(pa_channel_map_valid(&data
->channel_map
), -PA_ERR_INVALID
);
147 pa_return_val_if_fail(pa_channel_map_compatible(&data
->channel_map
, &data
->sample_spec
), -PA_ERR_INVALID
);
149 if (data
->flags
& PA_SOURCE_OUTPUT_FIX_FORMAT
)
150 data
->sample_spec
.format
= data
->source
->sample_spec
.format
;
152 if (data
->flags
& PA_SOURCE_OUTPUT_FIX_RATE
)
153 data
->sample_spec
.rate
= data
->source
->sample_spec
.rate
;
155 if (data
->flags
& PA_SOURCE_OUTPUT_FIX_CHANNELS
) {
156 data
->sample_spec
.channels
= data
->source
->sample_spec
.channels
;
157 data
->channel_map
= data
->source
->channel_map
;
160 pa_assert(pa_sample_spec_valid(&data
->sample_spec
));
161 pa_assert(pa_channel_map_valid(&data
->channel_map
));
163 if (data
->resample_method
== PA_RESAMPLER_INVALID
)
164 data
->resample_method
= core
->resample_method
;
166 pa_return_val_if_fail(data
->resample_method
< PA_RESAMPLER_MAX
, -PA_ERR_INVALID
);
168 if ((r
= pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE
], data
)) < 0)
171 if ((data
->flags
& PA_SOURCE_OUTPUT_NO_CREATE_ON_SUSPEND
) &&
172 pa_source_get_state(data
->source
) == PA_SOURCE_SUSPENDED
) {
173 pa_log("Failed to create source output: source is suspended.");
174 return -PA_ERR_BADSTATE
;
177 if (pa_idxset_size(data
->source
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
178 pa_log("Failed to create source output: too many outputs per source.");
179 return -PA_ERR_TOOLARGE
;
182 if ((data
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
183 !pa_sample_spec_equal(&data
->sample_spec
, &data
->source
->sample_spec
) ||
184 !pa_channel_map_equal(&data
->channel_map
, &data
->source
->channel_map
)) {
186 if (!(resampler
= pa_resampler_new(
188 &data
->source
->sample_spec
, &data
->source
->channel_map
,
189 &data
->sample_spec
, &data
->channel_map
,
190 data
->resample_method
,
191 ((data
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
192 ((data
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
193 (core
->disable_remixing
|| (data
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0) |
194 (core
->disable_lfe_remixing
? PA_RESAMPLER_NO_LFE
: 0)))) {
195 pa_log_warn("Unsupported resampling operation.");
196 return -PA_ERR_NOTSUPPORTED
;
200 o
= pa_msgobject_new(pa_source_output
);
201 o
->parent
.parent
.free
= source_output_free
;
202 o
->parent
.process_msg
= pa_source_output_process_msg
;
205 o
->state
= PA_SOURCE_OUTPUT_INIT
;
206 o
->flags
= data
->flags
;
207 o
->proplist
= pa_proplist_copy(data
->proplist
);
208 o
->driver
= pa_xstrdup(pa_path_get_filename(data
->driver
));
209 o
->module
= data
->module
;
210 o
->source
= data
->source
;
211 o
->client
= data
->client
;
213 o
->actual_resample_method
= resampler
? pa_resampler_get_method(resampler
) : PA_RESAMPLER_INVALID
;
214 o
->requested_resample_method
= data
->resample_method
;
215 o
->sample_spec
= data
->sample_spec
;
216 o
->channel_map
= data
->channel_map
;
218 o
->direct_on_input
= data
->direct_on_input
;
220 o
->save_source
= data
->save_source
;
225 o
->thread_info
.state
= o
->state
;
226 o
->thread_info
.attached
= FALSE
;
227 o
->thread_info
.sample_spec
= o
->sample_spec
;
228 o
->thread_info
.resampler
= resampler
;
229 o
->thread_info
.requested_source_latency
= (pa_usec_t
) -1;
230 o
->thread_info
.direct_on_input
= o
->direct_on_input
;
232 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
236 pa_frame_size(&o
->source
->sample_spec
),
240 &o
->source
->silence
);
242 pa_assert_se(pa_idxset_put(core
->source_outputs
, o
, &o
->index
) == 0);
243 pa_assert_se(pa_idxset_put(o
->source
->outputs
, pa_source_output_ref(o
), NULL
) == 0);
246 pa_assert_se(pa_idxset_put(o
->client
->source_outputs
, o
, NULL
) >= 0);
248 if (o
->direct_on_input
)
249 pa_assert_se(pa_idxset_put(o
->direct_on_input
->direct_outputs
, o
, NULL
) == 0);
251 pt
= pa_proplist_to_string_sep(o
->proplist
, "\n ");
252 pa_log_info("Created output %u \"%s\" on %s with sample spec %s and channel map %s\n %s",
254 pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)),
256 pa_sample_spec_snprint(st
, sizeof(st
), &o
->sample_spec
),
257 pa_channel_map_snprint(cm
, sizeof(cm
), &o
->channel_map
),
261 /* Don't forget to call pa_source_output_put! */
267 /* Called from main context */
268 static void update_n_corked(pa_source_output
*o
, pa_source_output_state_t state
) {
270 pa_assert_ctl_context();
275 if (o
->state
== PA_SOURCE_OUTPUT_CORKED
&& state
!= PA_SOURCE_OUTPUT_CORKED
)
276 pa_assert_se(o
->source
->n_corked
-- >= 1);
277 else if (o
->state
!= PA_SOURCE_OUTPUT_CORKED
&& state
== PA_SOURCE_OUTPUT_CORKED
)
278 o
->source
->n_corked
++;
281 /* Called from main context */
282 static void source_output_set_state(pa_source_output
*o
, pa_source_output_state_t state
) {
284 pa_assert_ctl_context();
286 if (o
->state
== state
)
289 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);
291 update_n_corked(o
, state
);
294 if (state
!= PA_SOURCE_OUTPUT_UNLINKED
) {
295 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED
], o
);
297 if (PA_SOURCE_OUTPUT_IS_LINKED(state
))
298 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
301 pa_source_update_status(o
->source
);
304 /* Called from main context */
305 void pa_source_output_unlink(pa_source_output
*o
) {
308 pa_assert_ctl_context();
310 /* See pa_sink_unlink() for a couple of comments how this function
313 pa_source_output_ref(o
);
315 linked
= PA_SOURCE_OUTPUT_IS_LINKED(o
->state
);
318 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK
], o
);
320 if (o
->direct_on_input
)
321 pa_idxset_remove_by_data(o
->direct_on_input
->direct_outputs
, o
, NULL
);
323 pa_idxset_remove_by_data(o
->core
->source_outputs
, o
, NULL
);
326 if (pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
))
327 pa_source_output_unref(o
);
330 pa_idxset_remove_by_data(o
->client
->source_outputs
, o
, NULL
);
332 update_n_corked(o
, PA_SOURCE_OUTPUT_UNLINKED
);
333 o
->state
= PA_SOURCE_OUTPUT_UNLINKED
;
335 if (linked
&& o
->source
)
336 if (o
->source
->asyncmsgq
)
337 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
342 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, o
->index
);
343 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST
], o
);
347 pa_source_update_status(o
->source
);
351 pa_core_maybe_vacuum(o
->core
);
353 pa_source_output_unref(o
);
356 /* Called from main context */
357 static void source_output_free(pa_object
* mo
) {
358 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
361 pa_assert_ctl_context();
362 pa_assert(pa_source_output_refcnt(o
) == 0);
364 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
))
365 pa_source_output_unlink(o
);
367 pa_log_info("Freeing output %u \"%s\"", o
->index
, pa_strnull(pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
)));
369 if (o
->thread_info
.delay_memblockq
)
370 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
372 if (o
->thread_info
.resampler
)
373 pa_resampler_free(o
->thread_info
.resampler
);
376 pa_proplist_free(o
->proplist
);
382 /* Called from main context */
383 void pa_source_output_put(pa_source_output
*o
) {
384 pa_source_output_state_t state
;
386 pa_source_output_assert_ref(o
);
387 pa_assert_ctl_context();
389 pa_assert(o
->state
== PA_SOURCE_OUTPUT_INIT
);
391 /* The following fields must be initialized properly */
395 state
= o
->flags
& PA_SOURCE_OUTPUT_START_CORKED
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
;
397 update_n_corked(o
, state
);
400 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
402 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
, o
->index
);
403 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PUT
], o
);
405 pa_source_update_status(o
->source
);
408 /* Called from main context */
409 void pa_source_output_kill(pa_source_output
*o
) {
410 pa_source_output_assert_ref(o
);
411 pa_assert_ctl_context();
412 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
417 /* Called from main context */
418 pa_usec_t
pa_source_output_get_latency(pa_source_output
*o
, pa_usec_t
*source_latency
) {
419 pa_usec_t r
[2] = { 0, 0 };
421 pa_source_output_assert_ref(o
);
422 pa_assert_ctl_context();
423 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
425 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
, r
, 0, NULL
) == 0);
428 r
[0] += o
->get_latency(o
);
431 *source_latency
= r
[1];
436 /* Called from thread context */
437 void pa_source_output_push(pa_source_output
*o
, const pa_memchunk
*chunk
) {
439 size_t limit
, mbs
= 0;
441 pa_source_output_assert_ref(o
);
442 pa_source_output_assert_io_context(o
);
443 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
445 pa_assert(pa_frame_aligned(chunk
->length
, &o
->source
->sample_spec
));
447 if (!o
->push
|| o
->thread_info
.state
== PA_SOURCE_OUTPUT_CORKED
)
450 pa_assert(o
->thread_info
.state
== PA_SOURCE_OUTPUT_RUNNING
);
452 if (pa_memblockq_push(o
->thread_info
.delay_memblockq
, chunk
) < 0) {
453 pa_log_debug("Delay queue overflow!");
454 pa_memblockq_seek(o
->thread_info
.delay_memblockq
, (int64_t) chunk
->length
, PA_SEEK_RELATIVE
, TRUE
);
457 limit
= o
->process_rewind
? 0 : o
->source
->thread_info
.max_rewind
;
459 if (limit
> 0 && o
->source
->monitor_of
) {
463 /* Hmm, check the latency for knowing how much of the buffered
464 * data is actually still unplayed and might hence still
465 * change. This is suboptimal. Ideally we'd have a call like
466 * pa_sink_get_changeable_size() or so that tells us how much
467 * of the queued data is actually still changeable. Hence
470 latency
= pa_sink_get_latency_within_thread(o
->source
->monitor_of
);
472 n
= pa_usec_to_bytes(latency
, &o
->source
->sample_spec
);
478 /* Implement the delay queue */
479 while ((length
= pa_memblockq_get_length(o
->thread_info
.delay_memblockq
)) > limit
) {
484 pa_assert_se(pa_memblockq_peek(o
->thread_info
.delay_memblockq
, &qchunk
) >= 0);
486 if (qchunk
.length
> length
)
487 qchunk
.length
= length
;
489 pa_assert(qchunk
.length
> 0);
491 if (!o
->thread_info
.resampler
)
497 mbs
= pa_resampler_max_block_size(o
->thread_info
.resampler
);
499 if (qchunk
.length
> mbs
)
502 pa_resampler_run(o
->thread_info
.resampler
, &qchunk
, &rchunk
);
504 if (rchunk
.length
> 0)
508 pa_memblock_unref(rchunk
.memblock
);
511 pa_memblock_unref(qchunk
.memblock
);
512 pa_memblockq_drop(o
->thread_info
.delay_memblockq
, qchunk
.length
);
516 /* Called from thread context */
517 void pa_source_output_process_rewind(pa_source_output
*o
, size_t nbytes
/* in source sample spec */) {
519 pa_source_output_assert_ref(o
);
520 pa_source_output_assert_io_context(o
);
521 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
522 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
527 if (o
->process_rewind
) {
528 pa_assert(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
) == 0);
530 if (o
->thread_info
.resampler
)
531 nbytes
= pa_resampler_result(o
->thread_info
.resampler
, nbytes
);
533 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) nbytes
);
536 o
->process_rewind(o
, nbytes
);
538 if (o
->thread_info
.resampler
)
539 pa_resampler_reset(o
->thread_info
.resampler
);
542 pa_memblockq_rewind(o
->thread_info
.delay_memblockq
, nbytes
);
545 /* Called from thread context */
546 size_t pa_source_output_get_max_rewind(pa_source_output
*o
) {
547 pa_source_output_assert_ref(o
);
548 pa_source_output_assert_io_context(o
);
550 return o
->thread_info
.resampler
? pa_resampler_request(o
->thread_info
.resampler
, o
->source
->thread_info
.max_rewind
) : o
->source
->thread_info
.max_rewind
;
553 /* Called from thread context */
554 void pa_source_output_update_max_rewind(pa_source_output
*o
, size_t nbytes
/* in the source's sample spec */) {
555 pa_source_output_assert_ref(o
);
556 pa_source_output_assert_io_context(o
);
557 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->thread_info
.state
));
558 pa_assert(pa_frame_aligned(nbytes
, &o
->source
->sample_spec
));
560 if (o
->update_max_rewind
)
561 o
->update_max_rewind(o
, o
->thread_info
.resampler
? pa_resampler_result(o
->thread_info
.resampler
, nbytes
) : nbytes
);
564 /* Called from thread context */
565 pa_usec_t
pa_source_output_set_requested_latency_within_thread(pa_source_output
*o
, pa_usec_t usec
) {
566 pa_source_output_assert_ref(o
);
567 pa_source_output_assert_io_context(o
);
569 if (!(o
->source
->flags
& PA_SOURCE_DYNAMIC_LATENCY
))
570 usec
= o
->source
->thread_info
.fixed_latency
;
572 if (usec
!= (pa_usec_t
) -1)
573 usec
= PA_CLAMP(usec
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
575 o
->thread_info
.requested_source_latency
= usec
;
576 pa_source_invalidate_requested_latency(o
->source
, TRUE
);
581 /* Called from main context */
582 pa_usec_t
pa_source_output_set_requested_latency(pa_source_output
*o
, pa_usec_t usec
) {
583 pa_source_output_assert_ref(o
);
584 pa_assert_ctl_context();
586 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
) && o
->source
) {
587 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
591 /* If this source output is not realized yet or is being moved, we
592 * have to touch the thread info data directly */
595 if (!(o
->source
->flags
& PA_SOURCE_DYNAMIC_LATENCY
))
596 usec
= pa_source_get_fixed_latency(o
->source
);
598 if (usec
!= (pa_usec_t
) -1) {
599 pa_usec_t min_latency
, max_latency
;
600 pa_source_get_latency_range(o
->source
, &min_latency
, &max_latency
);
601 usec
= PA_CLAMP(usec
, min_latency
, max_latency
);
605 o
->thread_info
.requested_source_latency
= usec
;
610 /* Called from main context */
611 pa_usec_t
pa_source_output_get_requested_latency(pa_source_output
*o
) {
612 pa_source_output_assert_ref(o
);
613 pa_assert_ctl_context();
615 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
) && o
->source
) {
617 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
621 /* If this source output is not realized yet or is being moved, we
622 * have to touch the thread info data directly */
624 return o
->thread_info
.requested_source_latency
;
627 /* Called from main context */
628 void pa_source_output_cork(pa_source_output
*o
, pa_bool_t b
) {
629 pa_source_output_assert_ref(o
);
630 pa_assert_ctl_context();
631 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
633 source_output_set_state(o
, b
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
);
636 /* Called from main context */
637 int pa_source_output_set_rate(pa_source_output
*o
, uint32_t rate
) {
638 pa_source_output_assert_ref(o
);
639 pa_assert_ctl_context();
640 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
641 pa_return_val_if_fail(o
->thread_info
.resampler
, -PA_ERR_BADSTATE
);
643 if (o
->sample_spec
.rate
== rate
)
646 o
->sample_spec
.rate
= rate
;
648 pa_asyncmsgq_post(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
, PA_UINT_TO_PTR(rate
), 0, NULL
, NULL
);
650 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
654 /* Called from main context */
655 void pa_source_output_set_name(pa_source_output
*o
, const char *name
) {
657 pa_assert_ctl_context();
658 pa_source_output_assert_ref(o
);
660 if (!name
&& !pa_proplist_contains(o
->proplist
, PA_PROP_MEDIA_NAME
))
663 old
= pa_proplist_gets(o
->proplist
, PA_PROP_MEDIA_NAME
);
665 if (old
&& name
&& !strcmp(old
, name
))
669 pa_proplist_sets(o
->proplist
, PA_PROP_MEDIA_NAME
, name
);
671 pa_proplist_unset(o
->proplist
, PA_PROP_MEDIA_NAME
);
673 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
674 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
675 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
679 /* Called from main thread */
680 void pa_source_output_update_proplist(pa_source_output
*o
, pa_update_mode_t mode
, pa_proplist
*p
) {
681 pa_source_output_assert_ref(o
);
682 pa_assert_ctl_context();
685 pa_proplist_update(o
->proplist
, mode
, p
);
687 if (PA_SOURCE_OUTPUT_IS_LINKED(o
->state
)) {
688 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], o
);
689 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
693 /* Called from main context */
694 pa_resample_method_t
pa_source_output_get_resample_method(pa_source_output
*o
) {
695 pa_source_output_assert_ref(o
);
696 pa_assert_ctl_context();
698 return o
->actual_resample_method
;
701 /* Called from main context */
702 pa_bool_t
pa_source_output_may_move(pa_source_output
*o
) {
703 pa_source_output_assert_ref(o
);
704 pa_assert_ctl_context();
705 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
707 if (o
->flags
& PA_SOURCE_OUTPUT_DONT_MOVE
)
710 if (o
->direct_on_input
)
716 /* Called from main context */
717 pa_bool_t
pa_source_output_may_move_to(pa_source_output
*o
, pa_source
*dest
) {
718 pa_source_output_assert_ref(o
);
719 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
720 pa_source_assert_ref(dest
);
722 if (dest
== o
->source
)
725 if (!pa_source_output_may_move(o
))
728 if (pa_idxset_size(dest
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
729 pa_log_warn("Failed to move source output: too many outputs per source.");
734 if (!o
->may_move_to(o
, dest
))
740 /* Called from main context */
741 int pa_source_output_start_move(pa_source_output
*o
) {
745 pa_source_output_assert_ref(o
);
746 pa_assert_ctl_context();
747 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
748 pa_assert(o
->source
);
750 if (!pa_source_output_may_move(o
))
751 return -PA_ERR_NOTSUPPORTED
;
753 if ((r
= pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START
], o
)) < 0)
758 pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
);
760 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
)
761 pa_assert_se(origin
->n_corked
-- >= 1);
763 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
) == 0);
765 pa_source_update_status(o
->source
);
768 pa_source_output_unref(o
);
773 /* Called from main context */
774 int pa_source_output_finish_move(pa_source_output
*o
, pa_source
*dest
, pa_bool_t save
) {
775 pa_resampler
*new_resampler
;
777 pa_source_output_assert_ref(o
);
778 pa_assert_ctl_context();
779 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
780 pa_assert(!o
->source
);
781 pa_source_assert_ref(dest
);
783 if (!pa_source_output_may_move_to(o
, dest
))
786 if (o
->thread_info
.resampler
&&
787 pa_sample_spec_equal(pa_resampler_input_sample_spec(o
->thread_info
.resampler
), &dest
->sample_spec
) &&
788 pa_channel_map_equal(pa_resampler_input_channel_map(o
->thread_info
.resampler
), &dest
->channel_map
))
790 /* Try to reuse the old resampler if possible */
791 new_resampler
= o
->thread_info
.resampler
;
793 else if ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ||
794 !pa_sample_spec_equal(&o
->sample_spec
, &dest
->sample_spec
) ||
795 !pa_channel_map_equal(&o
->channel_map
, &dest
->channel_map
)) {
797 /* Okey, we need a new resampler for the new source */
799 if (!(new_resampler
= pa_resampler_new(
801 &dest
->sample_spec
, &dest
->channel_map
,
802 &o
->sample_spec
, &o
->channel_map
,
803 o
->requested_resample_method
,
804 ((o
->flags
& PA_SOURCE_OUTPUT_VARIABLE_RATE
) ? PA_RESAMPLER_VARIABLE_RATE
: 0) |
805 ((o
->flags
& PA_SOURCE_OUTPUT_NO_REMAP
) ? PA_RESAMPLER_NO_REMAP
: 0) |
806 (o
->core
->disable_remixing
|| (o
->flags
& PA_SOURCE_OUTPUT_NO_REMIX
) ? PA_RESAMPLER_NO_REMIX
: 0)))) {
807 pa_log_warn("Unsupported resampling operation.");
808 return -PA_ERR_NOTSUPPORTED
;
811 new_resampler
= NULL
;
817 o
->save_source
= save
;
818 pa_idxset_put(o
->source
->outputs
, pa_source_output_ref(o
), NULL
);
820 if (pa_source_output_get_state(o
) == PA_SOURCE_OUTPUT_CORKED
)
821 o
->source
->n_corked
++;
823 /* Replace resampler */
824 if (new_resampler
!= o
->thread_info
.resampler
) {
825 if (o
->thread_info
.resampler
)
826 pa_resampler_free(o
->thread_info
.resampler
);
827 o
->thread_info
.resampler
= new_resampler
;
829 pa_memblockq_free(o
->thread_info
.delay_memblockq
);
831 o
->thread_info
.delay_memblockq
= pa_memblockq_new(
835 pa_frame_size(&o
->source
->sample_spec
),
839 &o
->source
->silence
);
842 pa_source_update_status(dest
);
844 pa_assert_se(pa_asyncmsgq_send(o
->source
->asyncmsgq
, PA_MSGOBJECT(o
->source
), PA_SOURCE_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
) == 0);
846 pa_log_debug("Successfully moved source output %i to %s.", o
->index
, dest
->name
);
848 /* Notify everyone */
849 pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH
], o
);
850 pa_subscription_post(o
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
855 /* Called from main context */
856 void pa_source_output_fail_move(pa_source_output
*o
) {
858 pa_source_output_assert_ref(o
);
859 pa_assert_ctl_context();
860 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
861 pa_assert(!o
->source
);
863 /* Check if someone wants this source output? */
864 if (pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL
], o
) == PA_HOOK_STOP
)
870 pa_source_output_kill(o
);
873 /* Called from main context */
874 int pa_source_output_move_to(pa_source_output
*o
, pa_source
*dest
, pa_bool_t save
) {
877 pa_source_output_assert_ref(o
);
878 pa_assert_ctl_context();
879 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o
->state
));
880 pa_assert(o
->source
);
881 pa_source_assert_ref(dest
);
883 if (dest
== o
->source
)
886 if (!pa_source_output_may_move_to(o
, dest
))
887 return -PA_ERR_NOTSUPPORTED
;
889 pa_source_output_ref(o
);
891 if ((r
= pa_source_output_start_move(o
)) < 0) {
892 pa_source_output_unref(o
);
896 if ((r
= pa_source_output_finish_move(o
, dest
, save
)) < 0) {
897 pa_source_output_fail_move(o
);
898 pa_source_output_unref(o
);
902 pa_source_output_unref(o
);
907 /* Called from IO thread context */
908 void pa_source_output_set_state_within_thread(pa_source_output
*o
, pa_source_output_state_t state
) {
909 pa_source_output_assert_ref(o
);
910 pa_source_output_assert_io_context(o
);
912 if (state
== o
->thread_info
.state
)
916 o
->state_change(o
, state
);
918 o
->thread_info
.state
= state
;
921 /* Called from IO thread context, except when it is not */
922 int pa_source_output_process_msg(pa_msgobject
*mo
, int code
, void *userdata
, int64_t offset
, pa_memchunk
* chunk
) {
923 pa_source_output
*o
= PA_SOURCE_OUTPUT(mo
);
924 pa_source_output_assert_ref(o
);
928 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
: {
929 pa_usec_t
*r
= userdata
;
931 r
[0] += pa_bytes_to_usec(pa_memblockq_get_length(o
->thread_info
.delay_memblockq
), &o
->source
->sample_spec
);
932 r
[1] += pa_source_get_latency_within_thread(o
->source
);
937 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE
:
939 o
->thread_info
.sample_spec
.rate
= PA_PTR_TO_UINT(userdata
);
940 pa_resampler_set_output_rate(o
->thread_info
.resampler
, PA_PTR_TO_UINT(userdata
));
943 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE
:
945 pa_source_output_set_state_within_thread(o
, PA_PTR_TO_UINT(userdata
));
948 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY
: {
949 pa_usec_t
*usec
= userdata
;
951 *usec
= pa_source_output_set_requested_latency_within_thread(o
, *usec
);
956 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY
: {
957 pa_usec_t
*r
= userdata
;
959 *r
= o
->thread_info
.requested_source_latency
;
964 return -PA_ERR_NOTIMPLEMENTED
;
967 /* Called from main context */
968 void pa_source_output_send_event(pa_source_output
*o
, const char *event
, pa_proplist
*data
) {
969 pa_proplist
*pl
= NULL
;
970 pa_source_output_send_event_hook_data hook_data
;
972 pa_source_output_assert_ref(o
);
973 pa_assert_ctl_context();
980 data
= pl
= pa_proplist_new();
982 hook_data
.source_output
= o
;
983 hook_data
.data
= data
;
984 hook_data
.event
= event
;
986 if (pa_hook_fire(&o
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_SEND_EVENT
], &hook_data
) < 0)
989 o
->send_event(o
, event
, data
);
993 pa_proplist_free(pl
);