2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/introspect.h>
32 #include <pulse/utf8.h>
33 #include <pulse/xmalloc.h>
34 #include <pulse/timeval.h>
35 #include <pulse/util.h>
36 #include <pulse/i18n.h>
38 #include <pulsecore/sink-input.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/sample-util.h>
42 #include <pulsecore/core-subscribe.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/macro.h>
45 #include <pulsecore/play-memblockq.h>
49 #define MAX_MIX_CHANNELS 32
50 #define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
51 #define ABSOLUTE_MIN_LATENCY (500)
52 #define ABSOLUTE_MAX_LATENCY (10*PA_USEC_PER_SEC)
53 #define DEFAULT_FIXED_LATENCY (250*PA_USEC_PER_MSEC)
55 PA_DEFINE_PUBLIC_CLASS(pa_sink
, pa_msgobject
);
57 static void sink_free(pa_object
*s
);
59 pa_sink_new_data
* pa_sink_new_data_init(pa_sink_new_data
*data
) {
63 data
->proplist
= pa_proplist_new();
68 void pa_sink_new_data_set_name(pa_sink_new_data
*data
, const char *name
) {
72 data
->name
= pa_xstrdup(name
);
75 void pa_sink_new_data_set_sample_spec(pa_sink_new_data
*data
, const pa_sample_spec
*spec
) {
78 if ((data
->sample_spec_is_set
= !!spec
))
79 data
->sample_spec
= *spec
;
82 void pa_sink_new_data_set_channel_map(pa_sink_new_data
*data
, const pa_channel_map
*map
) {
85 if ((data
->channel_map_is_set
= !!map
))
86 data
->channel_map
= *map
;
89 void pa_sink_new_data_set_volume(pa_sink_new_data
*data
, const pa_cvolume
*volume
) {
92 if ((data
->volume_is_set
= !!volume
))
93 data
->volume
= *volume
;
96 void pa_sink_new_data_set_muted(pa_sink_new_data
*data
, pa_bool_t mute
) {
99 data
->muted_is_set
= TRUE
;
100 data
->muted
= !!mute
;
103 void pa_sink_new_data_set_port(pa_sink_new_data
*data
, const char *port
) {
106 pa_xfree(data
->active_port
);
107 data
->active_port
= pa_xstrdup(port
);
110 void pa_sink_new_data_done(pa_sink_new_data
*data
) {
113 pa_proplist_free(data
->proplist
);
118 while ((p
= pa_hashmap_steal_first(data
->ports
)))
119 pa_device_port_free(p
);
121 pa_hashmap_free(data
->ports
, NULL
, NULL
);
124 pa_xfree(data
->name
);
125 pa_xfree(data
->active_port
);
128 pa_device_port
*pa_device_port_new(const char *name
, const char *description
, size_t extra
) {
133 p
= pa_xmalloc(PA_ALIGN(sizeof(pa_device_port
)) + extra
);
134 p
->name
= pa_xstrdup(name
);
135 p
->description
= pa_xstrdup(description
);
142 void pa_device_port_free(pa_device_port
*p
) {
146 pa_xfree(p
->description
);
150 /* Called from main context */
151 static void reset_callbacks(pa_sink
*s
) {
155 s
->get_volume
= NULL
;
156 s
->set_volume
= NULL
;
159 s
->request_rewind
= NULL
;
160 s
->update_requested_latency
= NULL
;
164 /* Called from main context */
165 pa_sink
* pa_sink_new(
167 pa_sink_new_data
*data
,
168 pa_sink_flags_t flags
) {
172 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
173 pa_source_new_data source_data
;
179 pa_assert(data
->name
);
180 pa_assert_ctl_context();
182 s
= pa_msgobject_new(pa_sink
);
184 if (!(name
= pa_namereg_register(core
, data
->name
, PA_NAMEREG_SINK
, s
, data
->namereg_fail
))) {
185 pa_log_debug("Failed to register name %s.", data
->name
);
190 pa_sink_new_data_set_name(data
, name
);
192 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_NEW
], data
) < 0) {
194 pa_namereg_unregister(core
, name
);
198 /* FIXME, need to free s here on failure */
200 pa_return_null_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
));
201 pa_return_null_if_fail(data
->name
&& pa_utf8_valid(data
->name
) && data
->name
[0]);
203 pa_return_null_if_fail(data
->sample_spec_is_set
&& pa_sample_spec_valid(&data
->sample_spec
));
205 if (!data
->channel_map_is_set
)
206 pa_return_null_if_fail(pa_channel_map_init_auto(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
));
208 pa_return_null_if_fail(pa_channel_map_valid(&data
->channel_map
));
209 pa_return_null_if_fail(data
->channel_map
.channels
== data
->sample_spec
.channels
);
211 if (!data
->volume_is_set
)
212 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
214 pa_return_null_if_fail(pa_cvolume_valid(&data
->volume
));
215 pa_return_null_if_fail(pa_cvolume_compatible(&data
->volume
, &data
->sample_spec
));
217 if (!data
->muted_is_set
)
221 pa_proplist_update(data
->proplist
, PA_UPDATE_MERGE
, data
->card
->proplist
);
223 pa_device_init_description(data
->proplist
);
224 pa_device_init_icon(data
->proplist
, TRUE
);
225 pa_device_init_intended_roles(data
->proplist
);
227 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_FIXATE
], data
) < 0) {
229 pa_namereg_unregister(core
, name
);
233 s
->parent
.parent
.free
= sink_free
;
234 s
->parent
.process_msg
= pa_sink_process_msg
;
237 s
->state
= PA_SINK_INIT
;
240 s
->suspend_cause
= 0;
241 s
->name
= pa_xstrdup(name
);
242 s
->proplist
= pa_proplist_copy(data
->proplist
);
243 s
->driver
= pa_xstrdup(pa_path_get_filename(data
->driver
));
244 s
->module
= data
->module
;
245 s
->card
= data
->card
;
247 s
->priority
= pa_device_init_priority(s
->proplist
);
249 s
->sample_spec
= data
->sample_spec
;
250 s
->channel_map
= data
->channel_map
;
252 s
->inputs
= pa_idxset_new(NULL
, NULL
);
255 s
->reference_volume
= s
->real_volume
= data
->volume
;
256 pa_cvolume_reset(&s
->soft_volume
, s
->sample_spec
.channels
);
257 s
->base_volume
= PA_VOLUME_NORM
;
258 s
->n_volume_steps
= PA_VOLUME_NORM
+1;
259 s
->muted
= data
->muted
;
260 s
->refresh_volume
= s
->refresh_muted
= FALSE
;
267 /* As a minor optimization we just steal the list instead of
269 s
->ports
= data
->ports
;
272 s
->active_port
= NULL
;
273 s
->save_port
= FALSE
;
275 if (data
->active_port
&& s
->ports
)
276 if ((s
->active_port
= pa_hashmap_get(s
->ports
, data
->active_port
)))
277 s
->save_port
= data
->save_port
;
279 if (!s
->active_port
&& s
->ports
) {
283 PA_HASHMAP_FOREACH(p
, s
->ports
, state
)
284 if (!s
->active_port
|| p
->priority
> s
->active_port
->priority
)
288 s
->save_volume
= data
->save_volume
;
289 s
->save_muted
= data
->save_muted
;
291 pa_silence_memchunk_get(
292 &core
->silence_cache
,
298 s
->thread_info
.rtpoll
= NULL
;
299 s
->thread_info
.inputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
300 s
->thread_info
.soft_volume
= s
->soft_volume
;
301 s
->thread_info
.soft_muted
= s
->muted
;
302 s
->thread_info
.state
= s
->state
;
303 s
->thread_info
.rewind_nbytes
= 0;
304 s
->thread_info
.rewind_requested
= FALSE
;
305 s
->thread_info
.max_rewind
= 0;
306 s
->thread_info
.max_request
= 0;
307 s
->thread_info
.requested_latency_valid
= FALSE
;
308 s
->thread_info
.requested_latency
= 0;
309 s
->thread_info
.min_latency
= ABSOLUTE_MIN_LATENCY
;
310 s
->thread_info
.max_latency
= ABSOLUTE_MAX_LATENCY
;
311 s
->thread_info
.fixed_latency
= flags
& PA_SINK_DYNAMIC_LATENCY
? 0 : DEFAULT_FIXED_LATENCY
;
313 /* FIXME: This should probably be moved to pa_sink_put() */
314 pa_assert_se(pa_idxset_put(core
->sinks
, s
, &s
->index
) >= 0);
317 pa_assert_se(pa_idxset_put(s
->card
->sinks
, s
, NULL
) >= 0);
319 pt
= pa_proplist_to_string_sep(s
->proplist
, "\n ");
320 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s\n %s",
323 pa_sample_spec_snprint(st
, sizeof(st
), &s
->sample_spec
),
324 pa_channel_map_snprint(cm
, sizeof(cm
), &s
->channel_map
),
328 pa_source_new_data_init(&source_data
);
329 pa_source_new_data_set_sample_spec(&source_data
, &s
->sample_spec
);
330 pa_source_new_data_set_channel_map(&source_data
, &s
->channel_map
);
331 source_data
.name
= pa_sprintf_malloc("%s.monitor", name
);
332 source_data
.driver
= data
->driver
;
333 source_data
.module
= data
->module
;
334 source_data
.card
= data
->card
;
336 dn
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
337 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Monitor of %s", dn
? dn
: s
->name
);
338 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "monitor");
340 s
->monitor_source
= pa_source_new(core
, &source_data
,
341 ((flags
& PA_SINK_LATENCY
) ? PA_SOURCE_LATENCY
: 0) |
342 ((flags
& PA_SINK_DYNAMIC_LATENCY
) ? PA_SOURCE_DYNAMIC_LATENCY
: 0));
344 pa_source_new_data_done(&source_data
);
346 if (!s
->monitor_source
) {
352 s
->monitor_source
->monitor_of
= s
;
354 pa_source_set_latency_range(s
->monitor_source
, s
->thread_info
.min_latency
, s
->thread_info
.max_latency
);
355 pa_source_set_fixed_latency(s
->monitor_source
, s
->thread_info
.fixed_latency
);
356 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
361 /* Called from main context */
362 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
364 pa_bool_t suspend_change
;
365 pa_sink_state_t original_state
;
368 pa_assert_ctl_context();
370 if (s
->state
== state
)
373 original_state
= s
->state
;
376 (original_state
== PA_SINK_SUSPENDED
&& PA_SINK_IS_OPENED(state
)) ||
377 (PA_SINK_IS_OPENED(original_state
) && state
== PA_SINK_SUSPENDED
);
380 if ((ret
= s
->set_state(s
, state
)) < 0)
384 if ((ret
= pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
)) < 0) {
387 s
->set_state(s
, original_state
);
394 if (state
!= PA_SINK_UNLINKED
) { /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
395 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], s
);
396 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
399 if (suspend_change
) {
403 /* We're suspending or resuming, tell everyone about it */
405 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
)
406 if (s
->state
== PA_SINK_SUSPENDED
&&
407 (i
->flags
& PA_SINK_INPUT_KILL_ON_SUSPEND
))
408 pa_sink_input_kill(i
);
410 i
->suspend(i
, state
== PA_SINK_SUSPENDED
);
412 if (s
->monitor_source
)
413 pa_source_sync_suspend(s
->monitor_source
);
419 /* Called from main context */
420 void pa_sink_put(pa_sink
* s
) {
421 pa_sink_assert_ref(s
);
422 pa_assert_ctl_context();
424 pa_assert(s
->state
== PA_SINK_INIT
);
426 /* The following fields must be initialized properly when calling _put() */
427 pa_assert(s
->asyncmsgq
);
428 pa_assert(s
->thread_info
.min_latency
<= s
->thread_info
.max_latency
);
430 /* Generally, flags should be initialized via pa_sink_new(). As a
431 * special exception we allow volume related flags to be set
432 * between _new() and _put(). */
434 if (!(s
->flags
& PA_SINK_HW_VOLUME_CTRL
))
435 s
->flags
|= PA_SINK_DECIBEL_VOLUME
;
437 if ((s
->flags
& PA_SINK_DECIBEL_VOLUME
) && s
->core
->flat_volumes
)
438 s
->flags
|= PA_SINK_FLAT_VOLUME
;
440 /* We assume that if the sink implementor changed the default
441 * volume he did so in real_volume, because that is the usual
442 * place where he is supposed to place his changes. */
443 s
->reference_volume
= s
->real_volume
;
445 s
->thread_info
.soft_volume
= s
->soft_volume
;
446 s
->thread_info
.soft_muted
= s
->muted
;
448 pa_assert((s
->flags
& PA_SINK_HW_VOLUME_CTRL
) || (s
->base_volume
== PA_VOLUME_NORM
&& s
->flags
& PA_SINK_DECIBEL_VOLUME
));
449 pa_assert(!(s
->flags
& PA_SINK_DECIBEL_VOLUME
) || s
->n_volume_steps
== PA_VOLUME_NORM
+1);
450 pa_assert(!(s
->flags
& PA_SINK_DYNAMIC_LATENCY
) == (s
->thread_info
.fixed_latency
!= 0));
451 pa_assert(!(s
->flags
& PA_SINK_LATENCY
) == !(s
->monitor_source
->flags
& PA_SOURCE_LATENCY
));
452 pa_assert(!(s
->flags
& PA_SINK_DYNAMIC_LATENCY
) == !(s
->monitor_source
->flags
& PA_SOURCE_DYNAMIC_LATENCY
));
454 pa_assert(s
->monitor_source
->thread_info
.fixed_latency
== s
->thread_info
.fixed_latency
);
455 pa_assert(s
->monitor_source
->thread_info
.min_latency
== s
->thread_info
.min_latency
);
456 pa_assert(s
->monitor_source
->thread_info
.max_latency
== s
->thread_info
.max_latency
);
458 pa_assert_se(sink_set_state(s
, PA_SINK_IDLE
) == 0);
460 pa_source_put(s
->monitor_source
);
462 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
463 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PUT
], s
);
466 /* Called from main context */
467 void pa_sink_unlink(pa_sink
* s
) {
469 pa_sink_input
*i
, *j
= NULL
;
472 pa_assert_ctl_context();
474 /* Please note that pa_sink_unlink() does more than simply
475 * reversing pa_sink_put(). It also undoes the registrations
476 * already done in pa_sink_new()! */
478 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
479 * may be called multiple times on the same sink without bad
482 linked
= PA_SINK_IS_LINKED(s
->state
);
485 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
487 if (s
->state
!= PA_SINK_UNLINKED
)
488 pa_namereg_unregister(s
->core
, s
->name
);
489 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
492 pa_idxset_remove_by_data(s
->card
->sinks
, s
, NULL
);
494 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
496 pa_sink_input_kill(i
);
501 sink_set_state(s
, PA_SINK_UNLINKED
);
503 s
->state
= PA_SINK_UNLINKED
;
507 if (s
->monitor_source
)
508 pa_source_unlink(s
->monitor_source
);
511 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
512 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
516 /* Called from main context */
517 static void sink_free(pa_object
*o
) {
518 pa_sink
*s
= PA_SINK(o
);
522 pa_assert_ctl_context();
523 pa_assert(pa_sink_refcnt(s
) == 0);
525 if (PA_SINK_IS_LINKED(s
->state
))
528 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
530 if (s
->monitor_source
) {
531 pa_source_unref(s
->monitor_source
);
532 s
->monitor_source
= NULL
;
535 pa_idxset_free(s
->inputs
, NULL
, NULL
);
537 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
538 pa_sink_input_unref(i
);
540 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
542 if (s
->silence
.memblock
)
543 pa_memblock_unref(s
->silence
.memblock
);
549 pa_proplist_free(s
->proplist
);
554 while ((p
= pa_hashmap_steal_first(s
->ports
)))
555 pa_device_port_free(p
);
557 pa_hashmap_free(s
->ports
, NULL
, NULL
);
563 /* Called from main context, and not while the IO thread is active, please */
564 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
565 pa_sink_assert_ref(s
);
566 pa_assert_ctl_context();
570 if (s
->monitor_source
)
571 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
574 /* Called from main context, and not while the IO thread is active, please */
575 void pa_sink_update_flags(pa_sink
*s
, pa_sink_flags_t mask
, pa_sink_flags_t value
) {
576 pa_sink_assert_ref(s
);
577 pa_assert_ctl_context();
582 /* For now, allow only a minimal set of flags to be changed. */
583 pa_assert((mask
& ~(PA_SINK_DYNAMIC_LATENCY
|PA_SINK_LATENCY
)) == 0);
585 s
->flags
= (s
->flags
& ~mask
) | (value
& mask
);
587 pa_source_update_flags(s
->monitor_source
,
588 ((mask
& PA_SINK_LATENCY
) ? PA_SOURCE_LATENCY
: 0) |
589 ((mask
& PA_SINK_DYNAMIC_LATENCY
) ? PA_SOURCE_DYNAMIC_LATENCY
: 0),
590 ((value
& PA_SINK_LATENCY
) ? PA_SOURCE_LATENCY
: 0) |
591 ((value
& PA_SINK_DYNAMIC_LATENCY
) ? PA_SINK_DYNAMIC_LATENCY
: 0));
594 /* Called from IO context, or before _put() from main context */
595 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
596 pa_sink_assert_ref(s
);
597 pa_sink_assert_io_context(s
);
599 s
->thread_info
.rtpoll
= p
;
601 if (s
->monitor_source
)
602 pa_source_set_rtpoll(s
->monitor_source
, p
);
605 /* Called from main context */
606 int pa_sink_update_status(pa_sink
*s
) {
607 pa_sink_assert_ref(s
);
608 pa_assert_ctl_context();
609 pa_assert(PA_SINK_IS_LINKED(s
->state
));
611 if (s
->state
== PA_SINK_SUSPENDED
)
614 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
617 /* Called from main context */
618 int pa_sink_suspend(pa_sink
*s
, pa_bool_t suspend
, pa_suspend_cause_t cause
) {
619 pa_sink_assert_ref(s
);
620 pa_assert_ctl_context();
621 pa_assert(PA_SINK_IS_LINKED(s
->state
));
622 pa_assert(cause
!= 0);
625 s
->suspend_cause
|= cause
;
626 s
->monitor_source
->suspend_cause
|= cause
;
628 s
->suspend_cause
&= ~cause
;
629 s
->monitor_source
->suspend_cause
&= ~cause
;
632 if ((pa_sink_get_state(s
) == PA_SINK_SUSPENDED
) == !!s
->suspend_cause
)
635 pa_log_debug("Suspend cause of sink %s is 0x%04x, %s", s
->name
, s
->suspend_cause
, s
->suspend_cause
? "suspending" : "resuming");
637 if (s
->suspend_cause
)
638 return sink_set_state(s
, PA_SINK_SUSPENDED
);
640 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
643 /* Called from main context */
644 pa_queue
*pa_sink_move_all_start(pa_sink
*s
, pa_queue
*q
) {
645 pa_sink_input
*i
, *n
;
648 pa_sink_assert_ref(s
);
649 pa_assert_ctl_context();
650 pa_assert(PA_SINK_IS_LINKED(s
->state
));
655 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= n
) {
656 n
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
));
658 pa_sink_input_ref(i
);
660 if (pa_sink_input_start_move(i
) >= 0)
663 pa_sink_input_unref(i
);
669 /* Called from main context */
670 void pa_sink_move_all_finish(pa_sink
*s
, pa_queue
*q
, pa_bool_t save
) {
673 pa_sink_assert_ref(s
);
674 pa_assert_ctl_context();
675 pa_assert(PA_SINK_IS_LINKED(s
->state
));
678 while ((i
= PA_SINK_INPUT(pa_queue_pop(q
)))) {
679 if (pa_sink_input_finish_move(i
, s
, save
) < 0)
680 pa_sink_input_fail_move(i
);
682 pa_sink_input_unref(i
);
685 pa_queue_free(q
, NULL
, NULL
);
688 /* Called from main context */
689 void pa_sink_move_all_fail(pa_queue
*q
) {
692 pa_assert_ctl_context();
695 while ((i
= PA_SINK_INPUT(pa_queue_pop(q
)))) {
696 pa_sink_input_fail_move(i
);
697 pa_sink_input_unref(i
);
700 pa_queue_free(q
, NULL
, NULL
);
703 /* Called from IO thread context */
704 void pa_sink_process_rewind(pa_sink
*s
, size_t nbytes
) {
708 pa_sink_assert_ref(s
);
709 pa_sink_assert_io_context(s
);
710 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
712 /* If nobody requested this and this is actually no real rewind
713 * then we can short cut this. Please note that this means that
714 * not all rewind requests triggered upstream will always be
715 * translated in actual requests! */
716 if (!s
->thread_info
.rewind_requested
&& nbytes
<= 0)
719 s
->thread_info
.rewind_nbytes
= 0;
720 s
->thread_info
.rewind_requested
= FALSE
;
722 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
)
726 pa_log_debug("Processing rewind...");
728 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
) {
729 pa_sink_input_assert_ref(i
);
730 pa_sink_input_process_rewind(i
, nbytes
);
734 if (s
->monitor_source
&& PA_SOURCE_IS_LINKED(s
->monitor_source
->thread_info
.state
))
735 pa_source_process_rewind(s
->monitor_source
, nbytes
);
738 /* Called from IO thread context */
739 static unsigned fill_mix_info(pa_sink
*s
, size_t *length
, pa_mix_info
*info
, unsigned maxinfo
) {
743 size_t mixlength
= *length
;
745 pa_sink_assert_ref(s
);
746 pa_sink_assert_io_context(s
);
749 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
750 pa_sink_input_assert_ref(i
);
752 pa_sink_input_peek(i
, *length
, &info
->chunk
, &info
->volume
);
754 if (mixlength
== 0 || info
->chunk
.length
< mixlength
)
755 mixlength
= info
->chunk
.length
;
757 if (pa_memblock_is_silence(info
->chunk
.memblock
)) {
758 pa_memblock_unref(info
->chunk
.memblock
);
762 info
->userdata
= pa_sink_input_ref(i
);
764 pa_assert(info
->chunk
.memblock
);
765 pa_assert(info
->chunk
.length
> 0);
778 /* Called from IO thread context */
779 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, pa_memchunk
*result
) {
783 unsigned n_unreffed
= 0;
785 pa_sink_assert_ref(s
);
786 pa_sink_assert_io_context(s
);
788 pa_assert(result
->memblock
);
789 pa_assert(result
->length
> 0);
791 /* We optimize for the case where the order of the inputs has not changed */
793 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
795 pa_mix_info
* m
= NULL
;
797 pa_sink_input_assert_ref(i
);
799 /* Let's try to find the matching entry info the pa_mix_info array */
800 for (j
= 0; j
< n
; j
++) {
802 if (info
[p
].userdata
== i
) {
813 pa_sink_input_drop(i
, result
->length
);
815 if (s
->monitor_source
&& PA_SOURCE_IS_LINKED(s
->monitor_source
->thread_info
.state
)) {
817 if (pa_hashmap_size(i
->thread_info
.direct_outputs
) > 0) {
822 if (m
&& m
->chunk
.memblock
) {
824 pa_memblock_ref(c
.memblock
);
825 pa_assert(result
->length
<= c
.length
);
826 c
.length
= result
->length
;
828 pa_memchunk_make_writable(&c
, 0);
829 pa_volume_memchunk(&c
, &s
->sample_spec
, &m
->volume
);
832 pa_memblock_ref(c
.memblock
);
833 pa_assert(result
->length
<= c
.length
);
834 c
.length
= result
->length
;
837 while ((o
= pa_hashmap_iterate(i
->thread_info
.direct_outputs
, &ostate
, NULL
))) {
838 pa_source_output_assert_ref(o
);
839 pa_assert(o
->direct_on_input
== i
);
840 pa_source_post_direct(s
->monitor_source
, o
, &c
);
843 pa_memblock_unref(c
.memblock
);
848 if (m
->chunk
.memblock
)
849 pa_memblock_unref(m
->chunk
.memblock
);
850 pa_memchunk_reset(&m
->chunk
);
852 pa_sink_input_unref(m
->userdata
);
859 /* Now drop references to entries that are included in the
860 * pa_mix_info array but don't exist anymore */
862 if (n_unreffed
< n
) {
863 for (; n
> 0; info
++, n
--) {
865 pa_sink_input_unref(info
->userdata
);
866 if (info
->chunk
.memblock
)
867 pa_memblock_unref(info
->chunk
.memblock
);
871 if (s
->monitor_source
&& PA_SOURCE_IS_LINKED(s
->monitor_source
->thread_info
.state
))
872 pa_source_post(s
->monitor_source
, result
);
875 /* Called from IO thread context */
876 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
877 pa_mix_info info
[MAX_MIX_CHANNELS
];
879 size_t block_size_max
;
881 pa_sink_assert_ref(s
);
882 pa_sink_assert_io_context(s
);
883 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
884 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
889 pa_assert(!s
->thread_info
.rewind_requested
);
890 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
892 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
) {
893 result
->memblock
= pa_memblock_ref(s
->silence
.memblock
);
894 result
->index
= s
->silence
.index
;
895 result
->length
= PA_MIN(s
->silence
.length
, length
);
900 length
= pa_frame_align(MIX_BUFFER_LENGTH
, &s
->sample_spec
);
902 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
903 if (length
> block_size_max
)
904 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
906 pa_assert(length
> 0);
908 n
= fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
);
912 *result
= s
->silence
;
913 pa_memblock_ref(result
->memblock
);
915 if (result
->length
> length
)
916 result
->length
= length
;
921 *result
= info
[0].chunk
;
922 pa_memblock_ref(result
->memblock
);
924 if (result
->length
> length
)
925 result
->length
= length
;
927 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
929 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&volume
)) {
930 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
)) {
931 pa_memblock_unref(result
->memblock
);
932 pa_silence_memchunk_get(&s
->core
->silence_cache
,
938 pa_memchunk_make_writable(result
, 0);
939 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
944 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
946 ptr
= pa_memblock_acquire(result
->memblock
);
947 result
->length
= pa_mix(info
, n
,
950 &s
->thread_info
.soft_volume
,
951 s
->thread_info
.soft_muted
);
952 pa_memblock_release(result
->memblock
);
957 inputs_drop(s
, info
, n
, result
);
962 /* Called from IO thread context */
963 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
964 pa_mix_info info
[MAX_MIX_CHANNELS
];
966 size_t length
, block_size_max
;
968 pa_sink_assert_ref(s
);
969 pa_sink_assert_io_context(s
);
970 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
972 pa_assert(target
->memblock
);
973 pa_assert(target
->length
> 0);
974 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
978 pa_assert(!s
->thread_info
.rewind_requested
);
979 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
981 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
) {
982 pa_silence_memchunk(target
, &s
->sample_spec
);
986 length
= target
->length
;
987 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
988 if (length
> block_size_max
)
989 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
991 pa_assert(length
> 0);
993 n
= fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
);
996 if (target
->length
> length
)
997 target
->length
= length
;
999 pa_silence_memchunk(target
, &s
->sample_spec
);
1000 } else if (n
== 1) {
1003 if (target
->length
> length
)
1004 target
->length
= length
;
1006 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
1008 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
1009 pa_silence_memchunk(target
, &s
->sample_spec
);
1013 vchunk
= info
[0].chunk
;
1014 pa_memblock_ref(vchunk
.memblock
);
1016 if (vchunk
.length
> length
)
1017 vchunk
.length
= length
;
1019 if (!pa_cvolume_is_norm(&volume
)) {
1020 pa_memchunk_make_writable(&vchunk
, 0);
1021 pa_volume_memchunk(&vchunk
, &s
->sample_spec
, &volume
);
1024 pa_memchunk_memcpy(target
, &vchunk
);
1025 pa_memblock_unref(vchunk
.memblock
);
1031 ptr
= pa_memblock_acquire(target
->memblock
);
1033 target
->length
= pa_mix(info
, n
,
1034 (uint8_t*) ptr
+ target
->index
, length
,
1036 &s
->thread_info
.soft_volume
,
1037 s
->thread_info
.soft_muted
);
1039 pa_memblock_release(target
->memblock
);
1042 inputs_drop(s
, info
, n
, target
);
1047 /* Called from IO thread context */
1048 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
1052 pa_sink_assert_ref(s
);
1053 pa_sink_assert_io_context(s
);
1054 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1056 pa_assert(target
->memblock
);
1057 pa_assert(target
->length
> 0);
1058 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
1062 pa_assert(!s
->thread_info
.rewind_requested
);
1063 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
1072 pa_sink_render_into(s
, &chunk
);
1081 /* Called from IO thread context */
1082 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
1083 pa_mix_info info
[MAX_MIX_CHANNELS
];
1084 size_t length1st
= length
;
1087 pa_sink_assert_ref(s
);
1088 pa_sink_assert_io_context(s
);
1089 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1090 pa_assert(length
> 0);
1091 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
1096 pa_assert(!s
->thread_info
.rewind_requested
);
1097 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
1099 pa_assert(length
> 0);
1101 n
= fill_mix_info(s
, &length1st
, info
, MAX_MIX_CHANNELS
);
1104 pa_silence_memchunk_get(&s
->core
->silence_cache
,
1109 } else if (n
== 1) {
1112 *result
= info
[0].chunk
;
1113 pa_memblock_ref(result
->memblock
);
1115 if (result
->length
> length
)
1116 result
->length
= length
;
1118 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
1120 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&volume
)) {
1121 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
)) {
1122 pa_memblock_unref(result
->memblock
);
1123 pa_silence_memchunk_get(&s
->core
->silence_cache
,
1129 pa_memchunk_make_writable(result
, length
);
1130 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
1137 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
1139 ptr
= pa_memblock_acquire(result
->memblock
);
1141 result
->length
= pa_mix(info
, n
,
1142 (uint8_t*) ptr
+ result
->index
, length1st
,
1144 &s
->thread_info
.soft_volume
,
1145 s
->thread_info
.soft_muted
);
1147 pa_memblock_release(result
->memblock
);
1150 inputs_drop(s
, info
, n
, result
);
1152 if (result
->length
< length
) {
1155 pa_memchunk_make_writable(result
, length
);
1157 l
= length
- result
->length
;
1158 d
= result
->index
+ result
->length
;
1164 pa_sink_render_into(s
, &chunk
);
1169 result
->length
= length
;
1175 /* Called from main thread */
1176 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
1179 pa_sink_assert_ref(s
);
1180 pa_assert_ctl_context();
1181 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1183 /* The returned value is supposed to be in the time domain of the sound card! */
1185 if (s
->state
== PA_SINK_SUSPENDED
)
1188 if (!(s
->flags
& PA_SINK_LATENCY
))
1191 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) == 0);
1196 /* Called from IO thread */
1197 pa_usec_t
pa_sink_get_latency_within_thread(pa_sink
*s
) {
1201 pa_sink_assert_ref(s
);
1202 pa_sink_assert_io_context(s
);
1203 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1205 /* The returned value is supposed to be in the time domain of the sound card! */
1207 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
)
1210 if (!(s
->flags
& PA_SINK_LATENCY
))
1213 o
= PA_MSGOBJECT(s
);
1215 /* FIXME: We probably should make this a proper vtable callback instead of going through process_msg() */
1217 if (o
->process_msg(o
, PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
1223 /* Called from main context */
1224 static void compute_reference_ratios(pa_sink
*s
) {
1228 pa_sink_assert_ref(s
);
1229 pa_assert_ctl_context();
1230 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1231 pa_assert(s
->flags
& PA_SINK_FLAT_VOLUME
);
1233 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1235 pa_cvolume remapped
;
1238 * Calculates the reference volume from the sink's reference
1239 * volume. This basically calculates:
1241 * i->reference_ratio = i->volume / s->reference_volume
1244 remapped
= s
->reference_volume
;
1245 pa_cvolume_remap(&remapped
, &s
->channel_map
, &i
->channel_map
);
1247 i
->reference_ratio
.channels
= i
->sample_spec
.channels
;
1249 for (c
= 0; c
< i
->sample_spec
.channels
; c
++) {
1251 /* We don't update when the sink volume is 0 anyway */
1252 if (remapped
.values
[c
] <= PA_VOLUME_MUTED
)
1255 /* Don't update the reference ratio unless necessary */
1256 if (pa_sw_volume_multiply(
1257 i
->reference_ratio
.values
[c
],
1258 remapped
.values
[c
]) == i
->volume
.values
[c
])
1261 i
->reference_ratio
.values
[c
] = pa_sw_volume_divide(
1262 i
->volume
.values
[c
],
1263 remapped
.values
[c
]);
1268 /* Called from main context */
1269 static void compute_real_ratios(pa_sink
*s
) {
1273 pa_sink_assert_ref(s
);
1274 pa_assert_ctl_context();
1275 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1276 pa_assert(s
->flags
& PA_SINK_FLAT_VOLUME
);
1278 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1280 pa_cvolume remapped
;
1283 * This basically calculates:
1285 * i->real_ratio := i->volume / s->real_volume
1286 * i->soft_volume := i->real_ratio * i->volume_factor
1289 remapped
= s
->real_volume
;
1290 pa_cvolume_remap(&remapped
, &s
->channel_map
, &i
->channel_map
);
1292 i
->real_ratio
.channels
= i
->sample_spec
.channels
;
1293 i
->soft_volume
.channels
= i
->sample_spec
.channels
;
1295 for (c
= 0; c
< i
->sample_spec
.channels
; c
++) {
1297 if (remapped
.values
[c
] <= PA_VOLUME_MUTED
) {
1298 /* We leave i->real_ratio untouched */
1299 i
->soft_volume
.values
[c
] = PA_VOLUME_MUTED
;
1303 /* Don't lose accuracy unless necessary */
1304 if (pa_sw_volume_multiply(
1305 i
->real_ratio
.values
[c
],
1306 remapped
.values
[c
]) != i
->volume
.values
[c
])
1308 i
->real_ratio
.values
[c
] = pa_sw_volume_divide(
1309 i
->volume
.values
[c
],
1310 remapped
.values
[c
]);
1312 i
->soft_volume
.values
[c
] = pa_sw_volume_multiply(
1313 i
->real_ratio
.values
[c
],
1314 i
->volume_factor
.values
[c
]);
1317 /* We don't copy the soft_volume to the thread_info data
1318 * here. That must be done by the caller */
1322 /* Called from main thread */
1323 static void compute_real_volume(pa_sink
*s
) {
1327 pa_sink_assert_ref(s
);
1328 pa_assert_ctl_context();
1329 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1330 pa_assert(s
->flags
& PA_SINK_FLAT_VOLUME
);
1332 /* This determines the maximum volume of all streams and sets
1333 * s->real_volume accordingly. */
1335 if (pa_idxset_isempty(s
->inputs
)) {
1336 /* In the special case that we have no sink input we leave the
1337 * volume unmodified. */
1338 s
->real_volume
= s
->reference_volume
;
1342 pa_cvolume_mute(&s
->real_volume
, s
->channel_map
.channels
);
1344 /* First let's determine the new maximum volume of all inputs
1345 * connected to this sink */
1346 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1347 pa_cvolume remapped
;
1349 remapped
= i
->volume
;
1350 pa_cvolume_remap(&remapped
, &i
->channel_map
, &s
->channel_map
);
1351 pa_cvolume_merge(&s
->real_volume
, &s
->real_volume
, &remapped
);
1354 /* Then, let's update the real ratios/soft volumes of all inputs
1355 * connected to this sink */
1356 compute_real_ratios(s
);
1359 /* Called from main thread */
1360 static void propagate_reference_volume(pa_sink
*s
) {
1364 pa_sink_assert_ref(s
);
1365 pa_assert_ctl_context();
1366 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1367 pa_assert(s
->flags
& PA_SINK_FLAT_VOLUME
);
1369 /* This is called whenever the sink volume changes that is not
1370 * caused by a sink input volume change. We need to fix up the
1371 * sink input volumes accordingly */
1373 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1374 pa_cvolume old_volume
, remapped
;
1376 old_volume
= i
->volume
;
1378 /* This basically calculates:
1380 * i->volume := s->reference_volume * i->reference_ratio */
1382 remapped
= s
->reference_volume
;
1383 pa_cvolume_remap(&remapped
, &s
->channel_map
, &i
->channel_map
);
1384 pa_sw_cvolume_multiply(&i
->volume
, &remapped
, &i
->reference_ratio
);
1386 /* The volume changed, let's tell people so */
1387 if (!pa_cvolume_equal(&old_volume
, &i
->volume
)) {
1389 if (i
->volume_changed
)
1390 i
->volume_changed(i
);
1392 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1397 /* Called from main thread */
1398 void pa_sink_set_volume(
1400 const pa_cvolume
*volume
,
1404 pa_cvolume old_reference_volume
;
1405 pa_bool_t reference_changed
;
1407 pa_sink_assert_ref(s
);
1408 pa_assert_ctl_context();
1409 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1410 pa_assert(!volume
|| pa_cvolume_valid(volume
));
1411 pa_assert(!volume
|| pa_cvolume_compatible(volume
, &s
->sample_spec
));
1412 pa_assert(volume
|| (s
->flags
& PA_SINK_FLAT_VOLUME
));
1414 /* If volume is NULL we synchronize the sink's real and reference
1415 * volumes with the stream volumes. If it is not NULL we update
1416 * the reference_volume with it. */
1418 old_reference_volume
= s
->reference_volume
;
1422 s
->reference_volume
= *volume
;
1424 if (s
->flags
& PA_SINK_FLAT_VOLUME
) {
1425 /* OK, propagate this volume change back to the inputs */
1426 propagate_reference_volume(s
);
1428 /* And now recalculate the real volume */
1429 compute_real_volume(s
);
1431 s
->real_volume
= s
->reference_volume
;
1434 pa_assert(s
->flags
& PA_SINK_FLAT_VOLUME
);
1436 /* Ok, let's determine the new real volume */
1437 compute_real_volume(s
);
1439 /* Let's 'push' the reference volume if necessary */
1440 pa_cvolume_merge(&s
->reference_volume
, &s
->reference_volume
, &s
->real_volume
);
1442 /* We need to fix the reference ratios of all streams now that
1443 * we changed the reference volume */
1444 compute_reference_ratios(s
);
1447 reference_changed
= !pa_cvolume_equal(&old_reference_volume
, &s
->reference_volume
);
1448 s
->save_volume
= (!reference_changed
&& s
->save_volume
) || save
;
1450 if (s
->set_volume
) {
1451 /* If we have a function set_volume(), then we do not apply a
1452 * soft volume by default. However, set_volume() is free to
1453 * apply one to s->soft_volume */
1455 pa_cvolume_reset(&s
->soft_volume
, s
->sample_spec
.channels
);
1459 /* If we have no function set_volume(), then the soft volume
1460 * becomes the virtual volume */
1461 s
->soft_volume
= s
->real_volume
;
1463 /* This tells the sink that soft and/or virtual volume changed */
1465 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, NULL
, 0, NULL
) == 0);
1467 if (reference_changed
)
1468 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1471 /* Called from main thread. Only to be called by sink implementor */
1472 void pa_sink_set_soft_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
1473 pa_sink_assert_ref(s
);
1474 pa_assert_ctl_context();
1477 pa_cvolume_reset(&s
->soft_volume
, s
->sample_spec
.channels
);
1479 s
->soft_volume
= *volume
;
1481 if (PA_SINK_IS_LINKED(s
->state
))
1482 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, NULL
, 0, NULL
) == 0);
1484 s
->thread_info
.soft_volume
= s
->soft_volume
;
1487 static void propagate_real_volume(pa_sink
*s
, const pa_cvolume
*old_real_volume
) {
1490 pa_cvolume old_reference_volume
;
1492 pa_sink_assert_ref(s
);
1493 pa_assert_ctl_context();
1494 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1496 /* This is called when the hardware's real volume changes due to
1497 * some external event. We copy the real volume into our
1498 * reference volume and then rebuild the stream volumes based on
1499 * i->real_ratio which should stay fixed. */
1501 if (pa_cvolume_equal(old_real_volume
, &s
->real_volume
))
1504 old_reference_volume
= s
->reference_volume
;
1506 /* 1. Make the real volume the reference volume */
1507 s
->reference_volume
= s
->real_volume
;
1509 if (s
->flags
& PA_SINK_FLAT_VOLUME
) {
1511 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1512 pa_cvolume old_volume
, remapped
;
1514 old_volume
= i
->volume
;
1516 /* 2. Since the sink's reference and real volumes are equal
1517 * now our ratios should be too. */
1518 i
->reference_ratio
= i
->real_ratio
;
1520 /* 3. Recalculate the new stream reference volume based on the
1521 * reference ratio and the sink's reference volume.
1523 * This basically calculates:
1525 * i->volume = s->reference_volume * i->reference_ratio
1527 * This is identical to propagate_reference_volume() */
1528 remapped
= s
->reference_volume
;
1529 pa_cvolume_remap(&remapped
, &s
->channel_map
, &i
->channel_map
);
1530 pa_sw_cvolume_multiply(&i
->volume
, &remapped
, &i
->reference_ratio
);
1532 /* Notify if something changed */
1533 if (!pa_cvolume_equal(&old_volume
, &i
->volume
)) {
1535 if (i
->volume_changed
)
1536 i
->volume_changed(i
);
1538 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1543 /* Something got changed in the hardware. It probably makes sense
1544 * to save changed hw settings given that hw volume changes not
1545 * triggered by PA are almost certainly done by the user. */
1546 s
->save_volume
= TRUE
;
1548 if (!pa_cvolume_equal(&old_reference_volume
, &s
->reference_volume
))
1549 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1552 /* Called from main thread */
1553 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
, pa_bool_t force_refresh
) {
1554 pa_sink_assert_ref(s
);
1555 pa_assert_ctl_context();
1556 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1558 if (s
->refresh_volume
|| force_refresh
) {
1559 struct pa_cvolume old_real_volume
;
1561 old_real_volume
= s
->real_volume
;
1566 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, NULL
, 0, NULL
) == 0);
1568 propagate_real_volume(s
, &old_real_volume
);
1571 return &s
->reference_volume
;
1574 /* Called from main thread */
1575 void pa_sink_volume_changed(pa_sink
*s
, const pa_cvolume
*new_real_volume
) {
1576 pa_cvolume old_real_volume
;
1578 pa_sink_assert_ref(s
);
1579 pa_assert_ctl_context();
1580 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1582 /* The sink implementor may call this if the volume changed to make sure everyone is notified */
1584 old_real_volume
= s
->real_volume
;
1585 s
->real_volume
= *new_real_volume
;
1587 propagate_real_volume(s
, &old_real_volume
);
1590 /* Called from main thread */
1591 void pa_sink_set_mute(pa_sink
*s
, pa_bool_t mute
, pa_bool_t save
) {
1592 pa_bool_t old_muted
;
1594 pa_sink_assert_ref(s
);
1595 pa_assert_ctl_context();
1596 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1598 old_muted
= s
->muted
;
1600 s
->save_muted
= (old_muted
== s
->muted
&& s
->save_muted
) || save
;
1605 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, NULL
, 0, NULL
) == 0);
1607 if (old_muted
!= s
->muted
)
1608 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1611 /* Called from main thread */
1612 pa_bool_t
pa_sink_get_mute(pa_sink
*s
, pa_bool_t force_refresh
) {
1614 pa_sink_assert_ref(s
);
1615 pa_assert_ctl_context();
1616 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1618 if (s
->refresh_muted
|| force_refresh
) {
1619 pa_bool_t old_muted
= s
->muted
;
1624 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, NULL
, 0, NULL
) == 0);
1626 if (old_muted
!= s
->muted
) {
1627 s
->save_muted
= TRUE
;
1629 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1631 /* Make sure the soft mute status stays in sync */
1632 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, NULL
, 0, NULL
) == 0);
1639 /* Called from main thread */
1640 void pa_sink_mute_changed(pa_sink
*s
, pa_bool_t new_muted
) {
1641 pa_sink_assert_ref(s
);
1642 pa_assert_ctl_context();
1643 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1645 /* The sink implementor may call this if the volume changed to make sure everyone is notified */
1647 if (s
->muted
== new_muted
)
1650 s
->muted
= new_muted
;
1651 s
->save_muted
= TRUE
;
1653 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1656 /* Called from main thread */
1657 pa_bool_t
pa_sink_update_proplist(pa_sink
*s
, pa_update_mode_t mode
, pa_proplist
*p
) {
1658 pa_sink_assert_ref(s
);
1659 pa_assert_ctl_context();
1662 pa_proplist_update(s
->proplist
, mode
, p
);
1664 if (PA_SINK_IS_LINKED(s
->state
)) {
1665 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
1666 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1672 /* Called from main thread */
1673 /* FIXME -- this should be dropped and be merged into pa_sink_update_proplist() */
1674 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
1676 pa_sink_assert_ref(s
);
1677 pa_assert_ctl_context();
1679 if (!description
&& !pa_proplist_contains(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
))
1682 old
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1684 if (old
&& description
&& pa_streq(old
, description
))
1688 pa_proplist_sets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, description
);
1690 pa_proplist_unset(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1692 if (s
->monitor_source
) {
1695 n
= pa_sprintf_malloc("Monitor Source of %s", description
? description
: s
->name
);
1696 pa_source_set_description(s
->monitor_source
, n
);
1700 if (PA_SINK_IS_LINKED(s
->state
)) {
1701 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1702 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
1706 /* Called from main thread */
1707 unsigned pa_sink_linked_by(pa_sink
*s
) {
1710 pa_sink_assert_ref(s
);
1711 pa_assert_ctl_context();
1712 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1714 ret
= pa_idxset_size(s
->inputs
);
1716 /* We add in the number of streams connected to us here. Please
1717 * note the asymmmetry to pa_sink_used_by()! */
1719 if (s
->monitor_source
)
1720 ret
+= pa_source_linked_by(s
->monitor_source
);
1725 /* Called from main thread */
1726 unsigned pa_sink_used_by(pa_sink
*s
) {
1729 pa_sink_assert_ref(s
);
1730 pa_assert_ctl_context();
1731 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1733 ret
= pa_idxset_size(s
->inputs
);
1734 pa_assert(ret
>= s
->n_corked
);
1736 /* Streams connected to our monitor source do not matter for
1737 * pa_sink_used_by()!.*/
1739 return ret
- s
->n_corked
;
1742 /* Called from main thread */
1743 unsigned pa_sink_check_suspend(pa_sink
*s
) {
1748 pa_sink_assert_ref(s
);
1749 pa_assert_ctl_context();
1751 if (!PA_SINK_IS_LINKED(s
->state
))
1756 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1757 pa_sink_input_state_t st
;
1759 st
= pa_sink_input_get_state(i
);
1760 pa_assert(PA_SINK_INPUT_IS_LINKED(st
));
1762 if (st
== PA_SINK_INPUT_CORKED
)
1765 if (i
->flags
& PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND
)
1771 if (s
->monitor_source
)
1772 ret
+= pa_source_check_suspend(s
->monitor_source
);
1777 /* Called from the IO thread */
1778 static void sync_input_volumes_within_thread(pa_sink
*s
) {
1782 pa_sink_assert_ref(s
);
1783 pa_sink_assert_io_context(s
);
1785 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
) {
1786 if (pa_cvolume_equal(&i
->thread_info
.soft_volume
, &i
->soft_volume
))
1789 i
->thread_info
.soft_volume
= i
->soft_volume
;
1790 pa_sink_input_request_rewind(i
, 0, TRUE
, FALSE
, FALSE
);
1794 /* Called from IO thread, except when it is not */
1795 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1796 pa_sink
*s
= PA_SINK(o
);
1797 pa_sink_assert_ref(s
);
1799 switch ((pa_sink_message_t
) code
) {
1801 case PA_SINK_MESSAGE_ADD_INPUT
: {
1802 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1804 /* If you change anything here, make sure to change the
1805 * sink input handling a few lines down at
1806 * PA_SINK_MESSAGE_FINISH_MOVE, too. */
1808 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
1810 /* Since the caller sleeps in pa_sink_input_put(), we can
1811 * safely access data outside of thread_info even though
1814 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
1815 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
1816 pa_assert(i
->sync_prev
->sync_next
== i
);
1817 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
1820 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
1821 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
1822 pa_assert(i
->sync_next
->sync_prev
== i
);
1823 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
1826 pa_assert(!i
->thread_info
.attached
);
1827 i
->thread_info
.attached
= TRUE
;
1832 pa_sink_input_set_state_within_thread(i
, i
->state
);
1834 /* The requested latency of the sink input needs to be
1835 * fixed up and then configured on the sink */
1837 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
1838 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
1840 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1841 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1843 /* We don't rewind here automatically. This is left to the
1844 * sink input implementor because some sink inputs need a
1845 * slow start, i.e. need some time to buffer client
1846 * samples before beginning streaming. */
1848 /* In flat volume mode we need to update the volume as
1850 return o
->process_msg(o
, PA_SINK_MESSAGE_SET_VOLUME
, NULL
, 0, NULL
);
1853 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
1854 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1856 /* If you change anything here, make sure to change the
1857 * sink input handling a few lines down at
1858 * PA_SINK_MESSAGE_PREPAPRE_MOVE, too. */
1863 pa_sink_input_set_state_within_thread(i
, i
->state
);
1865 pa_assert(i
->thread_info
.attached
);
1866 i
->thread_info
.attached
= FALSE
;
1868 /* Since the caller sleeps in pa_sink_input_unlink(),
1869 * we can safely access data outside of thread_info even
1870 * though it is mutable */
1872 pa_assert(!i
->sync_prev
);
1873 pa_assert(!i
->sync_next
);
1875 if (i
->thread_info
.sync_prev
) {
1876 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
1877 i
->thread_info
.sync_prev
= NULL
;
1880 if (i
->thread_info
.sync_next
) {
1881 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
1882 i
->thread_info
.sync_next
= NULL
;
1885 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
1886 pa_sink_input_unref(i
);
1888 pa_sink_invalidate_requested_latency(s
, TRUE
);
1889 pa_sink_request_rewind(s
, (size_t) -1);
1891 /* In flat volume mode we need to update the volume as
1893 return o
->process_msg(o
, PA_SINK_MESSAGE_SET_VOLUME
, NULL
, 0, NULL
);
1896 case PA_SINK_MESSAGE_START_MOVE
: {
1897 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1899 /* We don't support moving synchronized streams. */
1900 pa_assert(!i
->sync_prev
);
1901 pa_assert(!i
->sync_next
);
1902 pa_assert(!i
->thread_info
.sync_next
);
1903 pa_assert(!i
->thread_info
.sync_prev
);
1905 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
1907 size_t sink_nbytes
, total_nbytes
;
1909 /* Get the latency of the sink */
1910 usec
= pa_sink_get_latency_within_thread(s
);
1911 sink_nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
1912 total_nbytes
= sink_nbytes
+ pa_memblockq_get_length(i
->thread_info
.render_memblockq
);
1914 if (total_nbytes
> 0) {
1915 i
->thread_info
.rewrite_nbytes
= i
->thread_info
.resampler
? pa_resampler_request(i
->thread_info
.resampler
, total_nbytes
) : total_nbytes
;
1916 i
->thread_info
.rewrite_flush
= TRUE
;
1917 pa_sink_input_process_rewind(i
, sink_nbytes
);
1924 pa_assert(i
->thread_info
.attached
);
1925 i
->thread_info
.attached
= FALSE
;
1927 /* Let's remove the sink input ...*/
1928 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
1929 pa_sink_input_unref(i
);
1931 pa_sink_invalidate_requested_latency(s
, TRUE
);
1933 pa_log_debug("Requesting rewind due to started move");
1934 pa_sink_request_rewind(s
, (size_t) -1);
1936 /* In flat volume mode we need to update the volume as
1938 return o
->process_msg(o
, PA_SINK_MESSAGE_SET_VOLUME
, NULL
, 0, NULL
);
1941 case PA_SINK_MESSAGE_FINISH_MOVE
: {
1942 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1944 /* We don't support moving synchronized streams. */
1945 pa_assert(!i
->sync_prev
);
1946 pa_assert(!i
->sync_next
);
1947 pa_assert(!i
->thread_info
.sync_next
);
1948 pa_assert(!i
->thread_info
.sync_prev
);
1950 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
1952 pa_assert(!i
->thread_info
.attached
);
1953 i
->thread_info
.attached
= TRUE
;
1958 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
1959 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
1961 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1962 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1964 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
1968 /* Get the latency of the sink */
1969 usec
= pa_sink_get_latency_within_thread(s
);
1970 nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
1973 pa_sink_input_drop(i
, nbytes
);
1975 pa_log_debug("Requesting rewind due to finished move");
1976 pa_sink_request_rewind(s
, nbytes
);
1979 /* In flat volume mode we need to update the volume as
1981 return o
->process_msg(o
, PA_SINK_MESSAGE_SET_VOLUME
, NULL
, 0, NULL
);
1984 case PA_SINK_MESSAGE_SET_VOLUME
:
1986 if (!pa_cvolume_equal(&s
->thread_info
.soft_volume
, &s
->soft_volume
)) {
1987 s
->thread_info
.soft_volume
= s
->soft_volume
;
1988 pa_sink_request_rewind(s
, (size_t) -1);
1991 if (!(s
->flags
& PA_SINK_FLAT_VOLUME
))
1994 /* Fall through ... */
1996 case PA_SINK_MESSAGE_SYNC_VOLUMES
:
1997 sync_input_volumes_within_thread(s
);
2000 case PA_SINK_MESSAGE_GET_VOLUME
:
2003 case PA_SINK_MESSAGE_SET_MUTE
:
2005 if (s
->thread_info
.soft_muted
!= s
->muted
) {
2006 s
->thread_info
.soft_muted
= s
->muted
;
2007 pa_sink_request_rewind(s
, (size_t) -1);
2012 case PA_SINK_MESSAGE_GET_MUTE
:
2015 case PA_SINK_MESSAGE_SET_STATE
: {
2017 pa_bool_t suspend_change
=
2018 (s
->thread_info
.state
== PA_SINK_SUSPENDED
&& PA_SINK_IS_OPENED(PA_PTR_TO_UINT(userdata
))) ||
2019 (PA_SINK_IS_OPENED(s
->thread_info
.state
) && PA_PTR_TO_UINT(userdata
) == PA_SINK_SUSPENDED
);
2021 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
2023 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
) {
2024 s
->thread_info
.rewind_nbytes
= 0;
2025 s
->thread_info
.rewind_requested
= FALSE
;
2028 if (suspend_change
) {
2032 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
2033 if (i
->suspend_within_thread
)
2034 i
->suspend_within_thread(i
, s
->thread_info
.state
== PA_SINK_SUSPENDED
);
2040 case PA_SINK_MESSAGE_DETACH
:
2042 /* Detach all streams */
2043 pa_sink_detach_within_thread(s
);
2046 case PA_SINK_MESSAGE_ATTACH
:
2048 /* Reattach all streams */
2049 pa_sink_attach_within_thread(s
);
2052 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
: {
2054 pa_usec_t
*usec
= userdata
;
2055 *usec
= pa_sink_get_requested_latency_within_thread(s
);
2057 /* Yes, that's right, the IO thread will see -1 when no
2058 * explicit requested latency is configured, the main
2059 * thread will see max_latency */
2060 if (*usec
== (pa_usec_t
) -1)
2061 *usec
= s
->thread_info
.max_latency
;
2066 case PA_SINK_MESSAGE_SET_LATENCY_RANGE
: {
2067 pa_usec_t
*r
= userdata
;
2069 pa_sink_set_latency_range_within_thread(s
, r
[0], r
[1]);
2074 case PA_SINK_MESSAGE_GET_LATENCY_RANGE
: {
2075 pa_usec_t
*r
= userdata
;
2077 r
[0] = s
->thread_info
.min_latency
;
2078 r
[1] = s
->thread_info
.max_latency
;
2083 case PA_SINK_MESSAGE_GET_FIXED_LATENCY
:
2085 *((pa_usec_t
*) userdata
) = s
->thread_info
.fixed_latency
;
2088 case PA_SINK_MESSAGE_SET_FIXED_LATENCY
:
2090 pa_sink_set_fixed_latency_within_thread(s
, (pa_usec_t
) offset
);
2093 case PA_SINK_MESSAGE_GET_MAX_REWIND
:
2095 *((size_t*) userdata
) = s
->thread_info
.max_rewind
;
2098 case PA_SINK_MESSAGE_GET_MAX_REQUEST
:
2100 *((size_t*) userdata
) = s
->thread_info
.max_request
;
2103 case PA_SINK_MESSAGE_SET_MAX_REWIND
:
2105 pa_sink_set_max_rewind_within_thread(s
, (size_t) offset
);
2108 case PA_SINK_MESSAGE_SET_MAX_REQUEST
:
2110 pa_sink_set_max_request_within_thread(s
, (size_t) offset
);
2113 case PA_SINK_MESSAGE_GET_LATENCY
:
2114 case PA_SINK_MESSAGE_MAX
:
2121 /* Called from main thread */
2122 int pa_sink_suspend_all(pa_core
*c
, pa_bool_t suspend
, pa_suspend_cause_t cause
) {
2127 pa_core_assert_ref(c
);
2128 pa_assert_ctl_context();
2129 pa_assert(cause
!= 0);
2131 PA_IDXSET_FOREACH(sink
, c
->sinks
, idx
) {
2134 if ((r
= pa_sink_suspend(sink
, suspend
, cause
)) < 0)
2141 /* Called from main thread */
2142 void pa_sink_detach(pa_sink
*s
) {
2143 pa_sink_assert_ref(s
);
2144 pa_assert_ctl_context();
2145 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2147 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_DETACH
, NULL
, 0, NULL
) == 0);
2150 /* Called from main thread */
2151 void pa_sink_attach(pa_sink
*s
) {
2152 pa_sink_assert_ref(s
);
2153 pa_assert_ctl_context();
2154 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2156 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_ATTACH
, NULL
, 0, NULL
) == 0);
2159 /* Called from IO thread */
2160 void pa_sink_detach_within_thread(pa_sink
*s
) {
2164 pa_sink_assert_ref(s
);
2165 pa_sink_assert_io_context(s
);
2166 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
2168 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2172 if (s
->monitor_source
)
2173 pa_source_detach_within_thread(s
->monitor_source
);
2176 /* Called from IO thread */
2177 void pa_sink_attach_within_thread(pa_sink
*s
) {
2181 pa_sink_assert_ref(s
);
2182 pa_sink_assert_io_context(s
);
2183 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
2185 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2189 if (s
->monitor_source
)
2190 pa_source_attach_within_thread(s
->monitor_source
);
2193 /* Called from IO thread */
2194 void pa_sink_request_rewind(pa_sink
*s
, size_t nbytes
) {
2195 pa_sink_assert_ref(s
);
2196 pa_sink_assert_io_context(s
);
2197 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
2199 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
)
2202 if (nbytes
== (size_t) -1)
2203 nbytes
= s
->thread_info
.max_rewind
;
2205 nbytes
= PA_MIN(nbytes
, s
->thread_info
.max_rewind
);
2207 if (s
->thread_info
.rewind_requested
&&
2208 nbytes
<= s
->thread_info
.rewind_nbytes
)
2211 s
->thread_info
.rewind_nbytes
= nbytes
;
2212 s
->thread_info
.rewind_requested
= TRUE
;
2214 if (s
->request_rewind
)
2215 s
->request_rewind(s
);
2218 /* Called from IO thread */
2219 pa_usec_t
pa_sink_get_requested_latency_within_thread(pa_sink
*s
) {
2220 pa_usec_t result
= (pa_usec_t
) -1;
2223 pa_usec_t monitor_latency
;
2225 pa_sink_assert_ref(s
);
2226 pa_sink_assert_io_context(s
);
2228 if (!(s
->flags
& PA_SINK_DYNAMIC_LATENCY
))
2229 return PA_CLAMP(s
->thread_info
.fixed_latency
, s
->thread_info
.min_latency
, s
->thread_info
.max_latency
);
2231 if (s
->thread_info
.requested_latency_valid
)
2232 return s
->thread_info
.requested_latency
;
2234 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2235 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1 &&
2236 (result
== (pa_usec_t
) -1 || result
> i
->thread_info
.requested_sink_latency
))
2237 result
= i
->thread_info
.requested_sink_latency
;
2239 monitor_latency
= pa_source_get_requested_latency_within_thread(s
->monitor_source
);
2241 if (monitor_latency
!= (pa_usec_t
) -1 &&
2242 (result
== (pa_usec_t
) -1 || result
> monitor_latency
))
2243 result
= monitor_latency
;
2245 if (result
!= (pa_usec_t
) -1)
2246 result
= PA_CLAMP(result
, s
->thread_info
.min_latency
, s
->thread_info
.max_latency
);
2248 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
2249 /* Only cache if properly initialized */
2250 s
->thread_info
.requested_latency
= result
;
2251 s
->thread_info
.requested_latency_valid
= TRUE
;
2257 /* Called from main thread */
2258 pa_usec_t
pa_sink_get_requested_latency(pa_sink
*s
) {
2261 pa_sink_assert_ref(s
);
2262 pa_assert_ctl_context();
2263 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2265 if (s
->state
== PA_SINK_SUSPENDED
)
2268 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
2272 /* Called from IO as well as the main thread -- the latter only before the IO thread started up */
2273 void pa_sink_set_max_rewind_within_thread(pa_sink
*s
, size_t max_rewind
) {
2277 pa_sink_assert_ref(s
);
2278 pa_sink_assert_io_context(s
);
2280 if (max_rewind
== s
->thread_info
.max_rewind
)
2283 s
->thread_info
.max_rewind
= max_rewind
;
2285 if (PA_SINK_IS_LINKED(s
->thread_info
.state
))
2286 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2287 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
2289 if (s
->monitor_source
)
2290 pa_source_set_max_rewind_within_thread(s
->monitor_source
, s
->thread_info
.max_rewind
);
2293 /* Called from main thread */
2294 void pa_sink_set_max_rewind(pa_sink
*s
, size_t max_rewind
) {
2295 pa_sink_assert_ref(s
);
2296 pa_assert_ctl_context();
2298 if (PA_SINK_IS_LINKED(s
->state
))
2299 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MAX_REWIND
, NULL
, max_rewind
, NULL
) == 0);
2301 pa_sink_set_max_rewind_within_thread(s
, max_rewind
);
2304 /* Called from IO as well as the main thread -- the latter only before the IO thread started up */
2305 void pa_sink_set_max_request_within_thread(pa_sink
*s
, size_t max_request
) {
2308 pa_sink_assert_ref(s
);
2309 pa_sink_assert_io_context(s
);
2311 if (max_request
== s
->thread_info
.max_request
)
2314 s
->thread_info
.max_request
= max_request
;
2316 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
2319 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2320 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
2324 /* Called from main thread */
2325 void pa_sink_set_max_request(pa_sink
*s
, size_t max_request
) {
2326 pa_sink_assert_ref(s
);
2327 pa_assert_ctl_context();
2329 if (PA_SINK_IS_LINKED(s
->state
))
2330 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MAX_REQUEST
, NULL
, max_request
, NULL
) == 0);
2332 pa_sink_set_max_request_within_thread(s
, max_request
);
2335 /* Called from IO thread */
2336 void pa_sink_invalidate_requested_latency(pa_sink
*s
, pa_bool_t dynamic
) {
2340 pa_sink_assert_ref(s
);
2341 pa_sink_assert_io_context(s
);
2343 if ((s
->flags
& PA_SINK_DYNAMIC_LATENCY
))
2344 s
->thread_info
.requested_latency_valid
= FALSE
;
2348 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
2350 if (s
->update_requested_latency
)
2351 s
->update_requested_latency(s
);
2353 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2354 if (i
->update_sink_requested_latency
)
2355 i
->update_sink_requested_latency(i
);
2359 /* Called from main thread */
2360 void pa_sink_set_latency_range(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
2361 pa_sink_assert_ref(s
);
2362 pa_assert_ctl_context();
2364 /* min_latency == 0: no limit
2365 * min_latency anything else: specified limit
2367 * Similar for max_latency */
2369 if (min_latency
< ABSOLUTE_MIN_LATENCY
)
2370 min_latency
= ABSOLUTE_MIN_LATENCY
;
2372 if (max_latency
<= 0 ||
2373 max_latency
> ABSOLUTE_MAX_LATENCY
)
2374 max_latency
= ABSOLUTE_MAX_LATENCY
;
2376 pa_assert(min_latency
<= max_latency
);
2378 /* Hmm, let's see if someone forgot to set PA_SINK_DYNAMIC_LATENCY here... */
2379 pa_assert((min_latency
== ABSOLUTE_MIN_LATENCY
&&
2380 max_latency
== ABSOLUTE_MAX_LATENCY
) ||
2381 (s
->flags
& PA_SINK_DYNAMIC_LATENCY
));
2383 if (PA_SINK_IS_LINKED(s
->state
)) {
2389 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
2391 pa_sink_set_latency_range_within_thread(s
, min_latency
, max_latency
);
2394 /* Called from main thread */
2395 void pa_sink_get_latency_range(pa_sink
*s
, pa_usec_t
*min_latency
, pa_usec_t
*max_latency
) {
2396 pa_sink_assert_ref(s
);
2397 pa_assert_ctl_context();
2398 pa_assert(min_latency
);
2399 pa_assert(max_latency
);
2401 if (PA_SINK_IS_LINKED(s
->state
)) {
2402 pa_usec_t r
[2] = { 0, 0 };
2404 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
2406 *min_latency
= r
[0];
2407 *max_latency
= r
[1];
2409 *min_latency
= s
->thread_info
.min_latency
;
2410 *max_latency
= s
->thread_info
.max_latency
;
2414 /* Called from IO thread */
2415 void pa_sink_set_latency_range_within_thread(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
2416 pa_sink_assert_ref(s
);
2417 pa_sink_assert_io_context(s
);
2419 pa_assert(min_latency
>= ABSOLUTE_MIN_LATENCY
);
2420 pa_assert(max_latency
<= ABSOLUTE_MAX_LATENCY
);
2421 pa_assert(min_latency
<= max_latency
);
2423 /* Hmm, let's see if someone forgot to set PA_SINK_DYNAMIC_LATENCY here... */
2424 pa_assert((min_latency
== ABSOLUTE_MIN_LATENCY
&&
2425 max_latency
== ABSOLUTE_MAX_LATENCY
) ||
2426 (s
->flags
& PA_SINK_DYNAMIC_LATENCY
));
2428 if (s
->thread_info
.min_latency
== min_latency
&&
2429 s
->thread_info
.max_latency
== max_latency
)
2432 s
->thread_info
.min_latency
= min_latency
;
2433 s
->thread_info
.max_latency
= max_latency
;
2435 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
2439 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2440 if (i
->update_sink_latency_range
)
2441 i
->update_sink_latency_range(i
);
2444 pa_sink_invalidate_requested_latency(s
, FALSE
);
2446 pa_source_set_latency_range_within_thread(s
->monitor_source
, min_latency
, max_latency
);
2449 /* Called from main thread */
2450 void pa_sink_set_fixed_latency(pa_sink
*s
, pa_usec_t latency
) {
2451 pa_sink_assert_ref(s
);
2452 pa_assert_ctl_context();
2454 if (s
->flags
& PA_SINK_DYNAMIC_LATENCY
) {
2455 pa_assert(latency
== 0);
2459 if (latency
< ABSOLUTE_MIN_LATENCY
)
2460 latency
= ABSOLUTE_MIN_LATENCY
;
2462 if (latency
> ABSOLUTE_MAX_LATENCY
)
2463 latency
= ABSOLUTE_MAX_LATENCY
;
2465 if (PA_SINK_IS_LINKED(s
->state
))
2466 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_FIXED_LATENCY
, NULL
, (int64_t) latency
, NULL
) == 0);
2468 s
->thread_info
.fixed_latency
= latency
;
2470 pa_source_set_fixed_latency(s
->monitor_source
, latency
);
2473 /* Called from main thread */
2474 pa_usec_t
pa_sink_get_fixed_latency(pa_sink
*s
) {
2477 pa_sink_assert_ref(s
);
2478 pa_assert_ctl_context();
2480 if (s
->flags
& PA_SINK_DYNAMIC_LATENCY
)
2483 if (PA_SINK_IS_LINKED(s
->state
))
2484 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_FIXED_LATENCY
, &latency
, 0, NULL
) == 0);
2486 latency
= s
->thread_info
.fixed_latency
;
2491 /* Called from IO thread */
2492 void pa_sink_set_fixed_latency_within_thread(pa_sink
*s
, pa_usec_t latency
) {
2493 pa_sink_assert_ref(s
);
2494 pa_sink_assert_io_context(s
);
2496 if (s
->flags
& PA_SINK_DYNAMIC_LATENCY
) {
2497 pa_assert(latency
== 0);
2501 pa_assert(latency
>= ABSOLUTE_MIN_LATENCY
);
2502 pa_assert(latency
<= ABSOLUTE_MAX_LATENCY
);
2504 if (s
->thread_info
.fixed_latency
== latency
)
2507 s
->thread_info
.fixed_latency
= latency
;
2509 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
2513 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2514 if (i
->update_sink_fixed_latency
)
2515 i
->update_sink_fixed_latency(i
);
2518 pa_sink_invalidate_requested_latency(s
, FALSE
);
2520 pa_source_set_fixed_latency_within_thread(s
->monitor_source
, latency
);
2523 /* Called from main context */
2524 size_t pa_sink_get_max_rewind(pa_sink
*s
) {
2526 pa_sink_assert_ref(s
);
2527 pa_assert_ctl_context();
2529 if (!PA_SINK_IS_LINKED(s
->state
))
2530 return s
->thread_info
.max_rewind
;
2532 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REWIND
, &r
, 0, NULL
) == 0);
2537 /* Called from main context */
2538 size_t pa_sink_get_max_request(pa_sink
*s
) {
2540 pa_sink_assert_ref(s
);
2541 pa_assert_ctl_context();
2543 if (!PA_SINK_IS_LINKED(s
->state
))
2544 return s
->thread_info
.max_request
;
2546 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REQUEST
, &r
, 0, NULL
) == 0);
2551 /* Called from main context */
2552 int pa_sink_set_port(pa_sink
*s
, const char *name
, pa_bool_t save
) {
2553 pa_device_port
*port
;
2555 pa_sink_assert_ref(s
);
2556 pa_assert_ctl_context();
2559 pa_log_debug("set_port() operation not implemented for sink %u \"%s\"", s
->index
, s
->name
);
2560 return -PA_ERR_NOTIMPLEMENTED
;
2564 return -PA_ERR_NOENTITY
;
2566 if (!(port
= pa_hashmap_get(s
->ports
, name
)))
2567 return -PA_ERR_NOENTITY
;
2569 if (s
->active_port
== port
) {
2570 s
->save_port
= s
->save_port
|| save
;
2574 if ((s
->set_port(s
, port
)) < 0)
2575 return -PA_ERR_NOENTITY
;
2577 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
2579 pa_log_info("Changed port of sink %u \"%s\" to %s", s
->index
, s
->name
, port
->name
);
2581 s
->active_port
= port
;
2582 s
->save_port
= save
;
2587 pa_bool_t
pa_device_init_icon(pa_proplist
*p
, pa_bool_t is_sink
) {
2588 const char *ff
, *c
, *t
= NULL
, *s
= "", *profile
, *bus
;
2592 if (pa_proplist_contains(p
, PA_PROP_DEVICE_ICON_NAME
))
2595 if ((ff
= pa_proplist_gets(p
, PA_PROP_DEVICE_FORM_FACTOR
))) {
2597 if (pa_streq(ff
, "microphone"))
2598 t
= "audio-input-microphone";
2599 else if (pa_streq(ff
, "webcam"))
2601 else if (pa_streq(ff
, "computer"))
2603 else if (pa_streq(ff
, "handset"))
2605 else if (pa_streq(ff
, "portable"))
2606 t
= "multimedia-player";
2607 else if (pa_streq(ff
, "tv"))
2608 t
= "video-display";
2611 * The following icons are not part of the icon naming spec,
2612 * because Rodney Dawes sucks as the maintainer of that spec.
2614 * http://lists.freedesktop.org/archives/xdg/2009-May/010397.html
2616 else if (pa_streq(ff
, "headset"))
2617 t
= "audio-headset";
2618 else if (pa_streq(ff
, "headphone"))
2619 t
= "audio-headphones";
2620 else if (pa_streq(ff
, "speaker"))
2621 t
= "audio-speakers";
2622 else if (pa_streq(ff
, "hands-free"))
2623 t
= "audio-handsfree";
2627 if ((c
= pa_proplist_gets(p
, PA_PROP_DEVICE_CLASS
)))
2628 if (pa_streq(c
, "modem"))
2635 t
= "audio-input-microphone";
2638 if ((profile
= pa_proplist_gets(p
, PA_PROP_DEVICE_PROFILE_NAME
))) {
2639 if (strstr(profile
, "analog"))
2641 else if (strstr(profile
, "iec958"))
2643 else if (strstr(profile
, "hdmi"))
2647 bus
= pa_proplist_gets(p
, PA_PROP_DEVICE_BUS
);
2649 pa_proplist_setf(p
, PA_PROP_DEVICE_ICON_NAME
, "%s%s%s%s", t
, pa_strempty(s
), bus
? "-" : "", pa_strempty(bus
));
2654 pa_bool_t
pa_device_init_description(pa_proplist
*p
) {
2655 const char *s
, *d
= NULL
, *k
;
2658 if (pa_proplist_contains(p
, PA_PROP_DEVICE_DESCRIPTION
))
2661 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_FORM_FACTOR
)))
2662 if (pa_streq(s
, "internal"))
2663 d
= _("Internal Audio");
2666 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_CLASS
)))
2667 if (pa_streq(s
, "modem"))
2671 d
= pa_proplist_gets(p
, PA_PROP_DEVICE_PRODUCT_NAME
);
2676 k
= pa_proplist_gets(p
, PA_PROP_DEVICE_PROFILE_DESCRIPTION
);
2679 pa_proplist_setf(p
, PA_PROP_DEVICE_DESCRIPTION
, _("%s %s"), d
, k
);
2681 pa_proplist_sets(p
, PA_PROP_DEVICE_DESCRIPTION
, d
);
2686 pa_bool_t
pa_device_init_intended_roles(pa_proplist
*p
) {
2690 if (pa_proplist_contains(p
, PA_PROP_DEVICE_INTENDED_ROLES
))
2693 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_FORM_FACTOR
)))
2694 if (pa_streq(s
, "handset") || pa_streq(s
, "hands-free")) {
2695 pa_proplist_sets(p
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
2702 unsigned pa_device_init_priority(pa_proplist
*p
) {
2704 unsigned priority
= 0;
2708 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_CLASS
))) {
2710 if (pa_streq(s
, "sound"))
2712 else if (!pa_streq(s
, "modem"))
2716 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_FORM_FACTOR
))) {
2718 if (pa_streq(s
, "internal"))
2720 else if (pa_streq(s
, "speaker"))
2722 else if (pa_streq(s
, "headphone"))
2726 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_BUS
))) {
2728 if (pa_streq(s
, "pci"))
2730 else if (pa_streq(s
, "usb"))
2732 else if (pa_streq(s
, "bluetooth"))
2736 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_PROFILE_NAME
))) {
2738 if (pa_startswith(s
, "analog-"))
2740 else if (pa_startswith(s
, "iec958-"))