]> code.delx.au - pulseaudio/blob - src/pulsecore/sink.c
sink,source: Allow sample rate switching with corked streams
[pulseaudio] / src / pulsecore / sink.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
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.
11
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.
16
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
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <pulse/introspect.h>
32 #include <pulse/format.h>
33 #include <pulse/utf8.h>
34 #include <pulse/xmalloc.h>
35 #include <pulse/timeval.h>
36 #include <pulse/util.h>
37 #include <pulse/rtclock.h>
38 #include <pulse/internal.h>
39
40 #include <pulsecore/i18n.h>
41 #include <pulsecore/sink-input.h>
42 #include <pulsecore/namereg.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/sample-util.h>
45 #include <pulsecore/core-subscribe.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/macro.h>
48 #include <pulsecore/play-memblockq.h>
49 #include <pulsecore/flist.h>
50
51 #include "sink.h"
52
53 #define MAX_MIX_CHANNELS 32
54 #define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
55 #define ABSOLUTE_MIN_LATENCY (500)
56 #define ABSOLUTE_MAX_LATENCY (10*PA_USEC_PER_SEC)
57 #define DEFAULT_FIXED_LATENCY (250*PA_USEC_PER_MSEC)
58
59 PA_DEFINE_PUBLIC_CLASS(pa_sink, pa_msgobject);
60
61 struct pa_sink_volume_change {
62 pa_usec_t at;
63 pa_cvolume hw_volume;
64
65 PA_LLIST_FIELDS(pa_sink_volume_change);
66 };
67
68 struct sink_message_set_port {
69 pa_device_port *port;
70 int ret;
71 };
72
73 static void sink_free(pa_object *s);
74
75 static void pa_sink_volume_change_push(pa_sink *s);
76 static void pa_sink_volume_change_flush(pa_sink *s);
77 static void pa_sink_volume_change_rewind(pa_sink *s, size_t nbytes);
78
79 pa_sink_new_data* pa_sink_new_data_init(pa_sink_new_data *data) {
80 pa_assert(data);
81
82 pa_zero(*data);
83 data->proplist = pa_proplist_new();
84
85 return data;
86 }
87
88 void pa_sink_new_data_set_name(pa_sink_new_data *data, const char *name) {
89 pa_assert(data);
90
91 pa_xfree(data->name);
92 data->name = pa_xstrdup(name);
93 }
94
95 void pa_sink_new_data_set_sample_spec(pa_sink_new_data *data, const pa_sample_spec *spec) {
96 pa_assert(data);
97
98 if ((data->sample_spec_is_set = !!spec))
99 data->sample_spec = *spec;
100 }
101
102 void pa_sink_new_data_set_channel_map(pa_sink_new_data *data, const pa_channel_map *map) {
103 pa_assert(data);
104
105 if ((data->channel_map_is_set = !!map))
106 data->channel_map = *map;
107 }
108
109 void pa_sink_new_data_set_alternate_sample_rate(pa_sink_new_data *data, const uint32_t alternate_sample_rate) {
110 pa_assert(data);
111
112 data->alternate_sample_rate_is_set = TRUE;
113 data->alternate_sample_rate = alternate_sample_rate;
114 }
115
116 void pa_sink_new_data_set_volume(pa_sink_new_data *data, const pa_cvolume *volume) {
117 pa_assert(data);
118
119 if ((data->volume_is_set = !!volume))
120 data->volume = *volume;
121 }
122
123 void pa_sink_new_data_set_muted(pa_sink_new_data *data, pa_bool_t mute) {
124 pa_assert(data);
125
126 data->muted_is_set = TRUE;
127 data->muted = !!mute;
128 }
129
130 void pa_sink_new_data_set_port(pa_sink_new_data *data, const char *port) {
131 pa_assert(data);
132
133 pa_xfree(data->active_port);
134 data->active_port = pa_xstrdup(port);
135 }
136
137 void pa_sink_new_data_done(pa_sink_new_data *data) {
138 pa_assert(data);
139
140 pa_proplist_free(data->proplist);
141
142 if (data->ports) {
143 pa_device_port *p;
144
145 while ((p = pa_hashmap_steal_first(data->ports)))
146 pa_device_port_free(p);
147
148 pa_hashmap_free(data->ports, NULL, NULL);
149 }
150
151 pa_xfree(data->name);
152 pa_xfree(data->active_port);
153 }
154
155 pa_device_port *pa_device_port_new(const char *name, const char *description, size_t extra) {
156 pa_device_port *p;
157
158 pa_assert(name);
159
160 p = pa_xmalloc(PA_ALIGN(sizeof(pa_device_port)) + extra);
161 p->name = pa_xstrdup(name);
162 p->description = pa_xstrdup(description);
163
164 p->priority = 0;
165
166 return p;
167 }
168
169 void pa_device_port_free(pa_device_port *p) {
170 pa_assert(p);
171
172 pa_xfree(p->name);
173 pa_xfree(p->description);
174 pa_xfree(p);
175 }
176
177 /* Called from main context */
178 static void reset_callbacks(pa_sink *s) {
179 pa_assert(s);
180
181 s->set_state = NULL;
182 s->get_volume = NULL;
183 s->set_volume = NULL;
184 s->write_volume = NULL;
185 s->get_mute = NULL;
186 s->set_mute = NULL;
187 s->request_rewind = NULL;
188 s->update_requested_latency = NULL;
189 s->set_port = NULL;
190 s->get_formats = NULL;
191 s->set_formats = NULL;
192 s->update_rate = NULL;
193 }
194
195 /* Called from main context */
196 pa_sink* pa_sink_new(
197 pa_core *core,
198 pa_sink_new_data *data,
199 pa_sink_flags_t flags) {
200
201 pa_sink *s;
202 const char *name;
203 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
204 pa_source_new_data source_data;
205 const char *dn;
206 char *pt;
207
208 pa_assert(core);
209 pa_assert(data);
210 pa_assert(data->name);
211 pa_assert_ctl_context();
212
213 s = pa_msgobject_new(pa_sink);
214
215 if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_SINK, s, data->namereg_fail))) {
216 pa_log_debug("Failed to register name %s.", data->name);
217 pa_xfree(s);
218 return NULL;
219 }
220
221 pa_sink_new_data_set_name(data, name);
222
223 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_NEW], data) < 0) {
224 pa_xfree(s);
225 pa_namereg_unregister(core, name);
226 return NULL;
227 }
228
229 /* FIXME, need to free s here on failure */
230
231 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
232 pa_return_null_if_fail(data->name && pa_utf8_valid(data->name) && data->name[0]);
233
234 pa_return_null_if_fail(data->sample_spec_is_set && pa_sample_spec_valid(&data->sample_spec));
235
236 if (!data->channel_map_is_set)
237 pa_return_null_if_fail(pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT));
238
239 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
240 pa_return_null_if_fail(data->channel_map.channels == data->sample_spec.channels);
241
242 /* FIXME: There should probably be a general function for checking whether
243 * the sink volume is allowed to be set, like there is for sink inputs. */
244 pa_assert(!data->volume_is_set || !(flags & PA_SINK_SHARE_VOLUME_WITH_MASTER));
245
246 if (!data->volume_is_set) {
247 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
248 data->save_volume = FALSE;
249 }
250
251 pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
252 pa_return_null_if_fail(pa_cvolume_compatible(&data->volume, &data->sample_spec));
253
254 if (!data->muted_is_set)
255 data->muted = FALSE;
256
257 if (data->card)
258 pa_proplist_update(data->proplist, PA_UPDATE_MERGE, data->card->proplist);
259
260 pa_device_init_description(data->proplist);
261 pa_device_init_icon(data->proplist, TRUE);
262 pa_device_init_intended_roles(data->proplist);
263
264 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_FIXATE], data) < 0) {
265 pa_xfree(s);
266 pa_namereg_unregister(core, name);
267 return NULL;
268 }
269
270 s->parent.parent.free = sink_free;
271 s->parent.process_msg = pa_sink_process_msg;
272
273 s->core = core;
274 s->state = PA_SINK_INIT;
275 s->flags = flags;
276 s->priority = 0;
277 s->suspend_cause = 0;
278 s->name = pa_xstrdup(name);
279 s->proplist = pa_proplist_copy(data->proplist);
280 s->driver = pa_xstrdup(pa_path_get_filename(data->driver));
281 s->module = data->module;
282 s->card = data->card;
283
284 s->priority = pa_device_init_priority(s->proplist);
285
286 s->sample_spec = data->sample_spec;
287 s->channel_map = data->channel_map;
288 s->default_sample_rate = s->sample_spec.rate;
289
290 if (data->alternate_sample_rate_is_set)
291 s->alternate_sample_rate = data->alternate_sample_rate;
292 else
293 s->alternate_sample_rate = s->core->alternate_sample_rate;
294
295 if (s->sample_spec.rate == s->alternate_sample_rate) {
296 pa_log_warn("Default and alternate sample rates are the same.");
297 s->alternate_sample_rate = 0;
298 }
299
300 s->inputs = pa_idxset_new(NULL, NULL);
301 s->n_corked = 0;
302 s->input_to_master = NULL;
303
304 s->reference_volume = s->real_volume = data->volume;
305 pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
306 s->base_volume = PA_VOLUME_NORM;
307 s->n_volume_steps = PA_VOLUME_NORM+1;
308 s->muted = data->muted;
309 s->refresh_volume = s->refresh_muted = FALSE;
310
311 reset_callbacks(s);
312 s->userdata = NULL;
313
314 s->asyncmsgq = NULL;
315
316 /* As a minor optimization we just steal the list instead of
317 * copying it here */
318 s->ports = data->ports;
319 data->ports = NULL;
320
321 s->active_port = NULL;
322 s->save_port = FALSE;
323
324 if (data->active_port && s->ports)
325 if ((s->active_port = pa_hashmap_get(s->ports, data->active_port)))
326 s->save_port = data->save_port;
327
328 if (!s->active_port && s->ports) {
329 void *state;
330 pa_device_port *p;
331
332 PA_HASHMAP_FOREACH(p, s->ports, state)
333 if (!s->active_port || p->priority > s->active_port->priority)
334 s->active_port = p;
335 }
336
337 s->save_volume = data->save_volume;
338 s->save_muted = data->save_muted;
339
340 pa_silence_memchunk_get(
341 &core->silence_cache,
342 core->mempool,
343 &s->silence,
344 &s->sample_spec,
345 0);
346
347 s->thread_info.rtpoll = NULL;
348 s->thread_info.inputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
349 s->thread_info.soft_volume = s->soft_volume;
350 s->thread_info.soft_muted = s->muted;
351 s->thread_info.state = s->state;
352 s->thread_info.rewind_nbytes = 0;
353 s->thread_info.rewind_requested = FALSE;
354 s->thread_info.max_rewind = 0;
355 s->thread_info.max_request = 0;
356 s->thread_info.requested_latency_valid = FALSE;
357 s->thread_info.requested_latency = 0;
358 s->thread_info.min_latency = ABSOLUTE_MIN_LATENCY;
359 s->thread_info.max_latency = ABSOLUTE_MAX_LATENCY;
360 s->thread_info.fixed_latency = flags & PA_SINK_DYNAMIC_LATENCY ? 0 : DEFAULT_FIXED_LATENCY;
361
362 PA_LLIST_HEAD_INIT(pa_sink_volume_change, s->thread_info.volume_changes);
363 s->thread_info.volume_changes_tail = NULL;
364 pa_sw_cvolume_multiply(&s->thread_info.current_hw_volume, &s->soft_volume, &s->real_volume);
365 s->thread_info.volume_change_safety_margin = core->deferred_volume_safety_margin_usec;
366 s->thread_info.volume_change_extra_delay = core->deferred_volume_extra_delay_usec;
367
368 /* FIXME: This should probably be moved to pa_sink_put() */
369 pa_assert_se(pa_idxset_put(core->sinks, s, &s->index) >= 0);
370
371 if (s->card)
372 pa_assert_se(pa_idxset_put(s->card->sinks, s, NULL) >= 0);
373
374 pt = pa_proplist_to_string_sep(s->proplist, "\n ");
375 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s\n %s",
376 s->index,
377 s->name,
378 pa_sample_spec_snprint(st, sizeof(st), &s->sample_spec),
379 pa_channel_map_snprint(cm, sizeof(cm), &s->channel_map),
380 pt);
381 pa_xfree(pt);
382
383 pa_source_new_data_init(&source_data);
384 pa_source_new_data_set_sample_spec(&source_data, &s->sample_spec);
385 pa_source_new_data_set_channel_map(&source_data, &s->channel_map);
386 pa_source_new_data_set_alternate_sample_rate(&source_data, s->alternate_sample_rate);
387 source_data.name = pa_sprintf_malloc("%s.monitor", name);
388 source_data.driver = data->driver;
389 source_data.module = data->module;
390 source_data.card = data->card;
391
392 dn = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
393 pa_proplist_setf(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Monitor of %s", dn ? dn : s->name);
394 pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_CLASS, "monitor");
395
396 s->monitor_source = pa_source_new(core, &source_data,
397 ((flags & PA_SINK_LATENCY) ? PA_SOURCE_LATENCY : 0) |
398 ((flags & PA_SINK_DYNAMIC_LATENCY) ? PA_SOURCE_DYNAMIC_LATENCY : 0));
399
400 pa_source_new_data_done(&source_data);
401
402 if (!s->monitor_source) {
403 pa_sink_unlink(s);
404 pa_sink_unref(s);
405 return NULL;
406 }
407
408 s->monitor_source->monitor_of = s;
409
410 pa_source_set_latency_range(s->monitor_source, s->thread_info.min_latency, s->thread_info.max_latency);
411 pa_source_set_fixed_latency(s->monitor_source, s->thread_info.fixed_latency);
412 pa_source_set_max_rewind(s->monitor_source, s->thread_info.max_rewind);
413
414 return s;
415 }
416
417 /* Called from main context */
418 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
419 int ret;
420 pa_bool_t suspend_change;
421 pa_sink_state_t original_state;
422
423 pa_assert(s);
424 pa_assert_ctl_context();
425
426 if (s->state == state)
427 return 0;
428
429 original_state = s->state;
430
431 suspend_change =
432 (original_state == PA_SINK_SUSPENDED && PA_SINK_IS_OPENED(state)) ||
433 (PA_SINK_IS_OPENED(original_state) && state == PA_SINK_SUSPENDED);
434
435 if (s->set_state)
436 if ((ret = s->set_state(s, state)) < 0)
437 return ret;
438
439 if (s->asyncmsgq)
440 if ((ret = pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL)) < 0) {
441
442 if (s->set_state)
443 s->set_state(s, original_state);
444
445 return ret;
446 }
447
448 s->state = state;
449
450 if (state != PA_SINK_UNLINKED) { /* if we enter UNLINKED state pa_sink_unlink() will fire the appropriate events */
451 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], s);
452 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
453 }
454
455 if (suspend_change) {
456 pa_sink_input *i;
457 uint32_t idx;
458
459 /* We're suspending or resuming, tell everyone about it */
460
461 PA_IDXSET_FOREACH(i, s->inputs, idx)
462 if (s->state == PA_SINK_SUSPENDED &&
463 (i->flags & PA_SINK_INPUT_KILL_ON_SUSPEND))
464 pa_sink_input_kill(i);
465 else if (i->suspend)
466 i->suspend(i, state == PA_SINK_SUSPENDED);
467
468 if (s->monitor_source)
469 pa_source_sync_suspend(s->monitor_source);
470 }
471
472 return 0;
473 }
474
475 void pa_sink_set_get_volume_callback(pa_sink *s, pa_sink_cb_t cb) {
476 pa_assert(s);
477
478 s->get_volume = cb;
479 }
480
481 void pa_sink_set_set_volume_callback(pa_sink *s, pa_sink_cb_t cb) {
482 pa_sink_flags_t flags;
483
484 pa_assert(s);
485 pa_assert(!s->write_volume || cb);
486
487 s->set_volume = cb;
488
489 /* Save the current flags so we can tell if they've changed */
490 flags = s->flags;
491
492 if (cb) {
493 /* The sink implementor is responsible for setting decibel volume support */
494 s->flags |= PA_SINK_HW_VOLUME_CTRL;
495 } else {
496 s->flags &= ~PA_SINK_HW_VOLUME_CTRL;
497 /* See note below in pa_sink_put() about volume sharing and decibel volumes */
498 pa_sink_enable_decibel_volume(s, !(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER));
499 }
500
501 /* If the flags have changed after init, let any clients know via a change event */
502 if (s->state != PA_SINK_INIT && flags != s->flags)
503 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
504 }
505
506 void pa_sink_set_write_volume_callback(pa_sink *s, pa_sink_cb_t cb) {
507 pa_sink_flags_t flags;
508
509 pa_assert(s);
510 pa_assert(!cb || s->set_volume);
511
512 s->write_volume = cb;
513
514 /* Save the current flags so we can tell if they've changed */
515 flags = s->flags;
516
517 if (cb)
518 s->flags |= PA_SINK_DEFERRED_VOLUME;
519 else
520 s->flags &= ~PA_SINK_DEFERRED_VOLUME;
521
522 /* If the flags have changed after init, let any clients know via a change event */
523 if (s->state != PA_SINK_INIT && flags != s->flags)
524 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
525 }
526
527 void pa_sink_set_get_mute_callback(pa_sink *s, pa_sink_cb_t cb) {
528 pa_assert(s);
529
530 s->get_mute = cb;
531 }
532
533 void pa_sink_set_set_mute_callback(pa_sink *s, pa_sink_cb_t cb) {
534 pa_sink_flags_t flags;
535
536 pa_assert(s);
537
538 s->set_mute = cb;
539
540 /* Save the current flags so we can tell if they've changed */
541 flags = s->flags;
542
543 if (cb)
544 s->flags |= PA_SINK_HW_MUTE_CTRL;
545 else
546 s->flags &= ~PA_SINK_HW_MUTE_CTRL;
547
548 /* If the flags have changed after init, let any clients know via a change event */
549 if (s->state != PA_SINK_INIT && flags != s->flags)
550 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
551 }
552
553 static void enable_flat_volume(pa_sink *s, pa_bool_t enable) {
554 pa_sink_flags_t flags;
555
556 pa_assert(s);
557
558 /* Always follow the overall user preference here */
559 enable = enable && s->core->flat_volumes;
560
561 /* Save the current flags so we can tell if they've changed */
562 flags = s->flags;
563
564 if (enable)
565 s->flags |= PA_SINK_FLAT_VOLUME;
566 else
567 s->flags &= ~PA_SINK_FLAT_VOLUME;
568
569 /* If the flags have changed after init, let any clients know via a change event */
570 if (s->state != PA_SINK_INIT && flags != s->flags)
571 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
572 }
573
574 void pa_sink_enable_decibel_volume(pa_sink *s, pa_bool_t enable) {
575 pa_sink_flags_t flags;
576
577 pa_assert(s);
578
579 /* Save the current flags so we can tell if they've changed */
580 flags = s->flags;
581
582 if (enable) {
583 s->flags |= PA_SINK_DECIBEL_VOLUME;
584 enable_flat_volume(s, TRUE);
585 } else {
586 s->flags &= ~PA_SINK_DECIBEL_VOLUME;
587 enable_flat_volume(s, FALSE);
588 }
589
590 /* If the flags have changed after init, let any clients know via a change event */
591 if (s->state != PA_SINK_INIT && flags != s->flags)
592 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
593 }
594
595 /* Called from main context */
596 void pa_sink_put(pa_sink* s) {
597 pa_sink_assert_ref(s);
598 pa_assert_ctl_context();
599
600 pa_assert(s->state == PA_SINK_INIT);
601 pa_assert(!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER) || s->input_to_master);
602
603 /* The following fields must be initialized properly when calling _put() */
604 pa_assert(s->asyncmsgq);
605 pa_assert(s->thread_info.min_latency <= s->thread_info.max_latency);
606
607 /* Generally, flags should be initialized via pa_sink_new(). As a
608 * special exception we allow some volume related flags to be set
609 * between _new() and _put() by the callback setter functions above.
610 *
611 * Thus we implement a couple safeguards here which ensure the above
612 * setters were used (or at least the implementor made manual changes
613 * in a compatible way).
614 *
615 * Note: All of these flags set here can change over the life time
616 * of the sink. */
617 pa_assert(!(s->flags & PA_SINK_HW_VOLUME_CTRL) || s->set_volume);
618 pa_assert(!(s->flags & PA_SINK_DEFERRED_VOLUME) || s->write_volume);
619 pa_assert(!(s->flags & PA_SINK_HW_MUTE_CTRL) || s->set_mute);
620
621 /* XXX: Currently decibel volume is disabled for all sinks that use volume
622 * sharing. When the master sink supports decibel volume, it would be good
623 * to have the flag also in the filter sink, but currently we don't do that
624 * so that the flags of the filter sink never change when it's moved from
625 * a master sink to another. One solution for this problem would be to
626 * remove user-visible volume altogether from filter sinks when volume
627 * sharing is used, but the current approach was easier to implement... */
628 /* We always support decibel volumes in software, otherwise we leave it to
629 * the sink implementor to set this flag as needed.
630 *
631 * Note: This flag can also change over the life time of the sink. */
632 if (!(s->flags & PA_SINK_HW_VOLUME_CTRL) && !(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER))
633 pa_sink_enable_decibel_volume(s, TRUE);
634
635 /* If the sink implementor support DB volumes by itself, we should always
636 * try and enable flat volumes too */
637 if ((s->flags & PA_SINK_DECIBEL_VOLUME))
638 enable_flat_volume(s, TRUE);
639
640 if (s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER) {
641 pa_sink *root_sink = pa_sink_get_master(s);
642
643 pa_assert(root_sink);
644
645 s->reference_volume = root_sink->reference_volume;
646 pa_cvolume_remap(&s->reference_volume, &root_sink->channel_map, &s->channel_map);
647
648 s->real_volume = root_sink->real_volume;
649 pa_cvolume_remap(&s->real_volume, &root_sink->channel_map, &s->channel_map);
650 } else
651 /* We assume that if the sink implementor changed the default
652 * volume he did so in real_volume, because that is the usual
653 * place where he is supposed to place his changes. */
654 s->reference_volume = s->real_volume;
655
656 s->thread_info.soft_volume = s->soft_volume;
657 s->thread_info.soft_muted = s->muted;
658 pa_sw_cvolume_multiply(&s->thread_info.current_hw_volume, &s->soft_volume, &s->real_volume);
659
660 pa_assert((s->flags & PA_SINK_HW_VOLUME_CTRL)
661 || (s->base_volume == PA_VOLUME_NORM
662 && ((s->flags & PA_SINK_DECIBEL_VOLUME || (s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)))));
663 pa_assert(!(s->flags & PA_SINK_DECIBEL_VOLUME) || s->n_volume_steps == PA_VOLUME_NORM+1);
664 pa_assert(!(s->flags & PA_SINK_DYNAMIC_LATENCY) == (s->thread_info.fixed_latency != 0));
665 pa_assert(!(s->flags & PA_SINK_LATENCY) == !(s->monitor_source->flags & PA_SOURCE_LATENCY));
666 pa_assert(!(s->flags & PA_SINK_DYNAMIC_LATENCY) == !(s->monitor_source->flags & PA_SOURCE_DYNAMIC_LATENCY));
667
668 pa_assert(s->monitor_source->thread_info.fixed_latency == s->thread_info.fixed_latency);
669 pa_assert(s->monitor_source->thread_info.min_latency == s->thread_info.min_latency);
670 pa_assert(s->monitor_source->thread_info.max_latency == s->thread_info.max_latency);
671
672 pa_assert_se(sink_set_state(s, PA_SINK_IDLE) == 0);
673
674 pa_source_put(s->monitor_source);
675
676 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
677 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PUT], s);
678 }
679
680 /* Called from main context */
681 void pa_sink_unlink(pa_sink* s) {
682 pa_bool_t linked;
683 pa_sink_input *i, *j = NULL;
684
685 pa_assert(s);
686 pa_assert_ctl_context();
687
688 /* Please note that pa_sink_unlink() does more than simply
689 * reversing pa_sink_put(). It also undoes the registrations
690 * already done in pa_sink_new()! */
691
692 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
693 * may be called multiple times on the same sink without bad
694 * effects. */
695
696 linked = PA_SINK_IS_LINKED(s->state);
697
698 if (linked)
699 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK], s);
700
701 if (s->state != PA_SINK_UNLINKED)
702 pa_namereg_unregister(s->core, s->name);
703 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
704
705 if (s->card)
706 pa_idxset_remove_by_data(s->card->sinks, s, NULL);
707
708 while ((i = pa_idxset_first(s->inputs, NULL))) {
709 pa_assert(i != j);
710 pa_sink_input_kill(i);
711 j = i;
712 }
713
714 if (linked)
715 sink_set_state(s, PA_SINK_UNLINKED);
716 else
717 s->state = PA_SINK_UNLINKED;
718
719 reset_callbacks(s);
720
721 if (s->monitor_source)
722 pa_source_unlink(s->monitor_source);
723
724 if (linked) {
725 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
726 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], s);
727 }
728 }
729
730 /* Called from main context */
731 static void sink_free(pa_object *o) {
732 pa_sink *s = PA_SINK(o);
733 pa_sink_input *i;
734
735 pa_assert(s);
736 pa_assert_ctl_context();
737 pa_assert(pa_sink_refcnt(s) == 0);
738
739 if (PA_SINK_IS_LINKED(s->state))
740 pa_sink_unlink(s);
741
742 pa_log_info("Freeing sink %u \"%s\"", s->index, s->name);
743
744 if (s->monitor_source) {
745 pa_source_unref(s->monitor_source);
746 s->monitor_source = NULL;
747 }
748
749 pa_idxset_free(s->inputs, NULL, NULL);
750
751 while ((i = pa_hashmap_steal_first(s->thread_info.inputs)))
752 pa_sink_input_unref(i);
753
754 pa_hashmap_free(s->thread_info.inputs, NULL, NULL);
755
756 if (s->silence.memblock)
757 pa_memblock_unref(s->silence.memblock);
758
759 pa_xfree(s->name);
760 pa_xfree(s->driver);
761
762 if (s->proplist)
763 pa_proplist_free(s->proplist);
764
765 if (s->ports) {
766 pa_device_port *p;
767
768 while ((p = pa_hashmap_steal_first(s->ports)))
769 pa_device_port_free(p);
770
771 pa_hashmap_free(s->ports, NULL, NULL);
772 }
773
774 pa_xfree(s);
775 }
776
777 /* Called from main context, and not while the IO thread is active, please */
778 void pa_sink_set_asyncmsgq(pa_sink *s, pa_asyncmsgq *q) {
779 pa_sink_assert_ref(s);
780 pa_assert_ctl_context();
781
782 s->asyncmsgq = q;
783
784 if (s->monitor_source)
785 pa_source_set_asyncmsgq(s->monitor_source, q);
786 }
787
788 /* Called from main context, and not while the IO thread is active, please */
789 void pa_sink_update_flags(pa_sink *s, pa_sink_flags_t mask, pa_sink_flags_t value) {
790 pa_sink_assert_ref(s);
791 pa_assert_ctl_context();
792
793 if (mask == 0)
794 return;
795
796 /* For now, allow only a minimal set of flags to be changed. */
797 pa_assert((mask & ~(PA_SINK_DYNAMIC_LATENCY|PA_SINK_LATENCY)) == 0);
798
799 s->flags = (s->flags & ~mask) | (value & mask);
800
801 pa_source_update_flags(s->monitor_source,
802 ((mask & PA_SINK_LATENCY) ? PA_SOURCE_LATENCY : 0) |
803 ((mask & PA_SINK_DYNAMIC_LATENCY) ? PA_SOURCE_DYNAMIC_LATENCY : 0),
804 ((value & PA_SINK_LATENCY) ? PA_SOURCE_LATENCY : 0) |
805 ((value & PA_SINK_DYNAMIC_LATENCY) ? PA_SINK_DYNAMIC_LATENCY : 0));
806 }
807
808 /* Called from IO context, or before _put() from main context */
809 void pa_sink_set_rtpoll(pa_sink *s, pa_rtpoll *p) {
810 pa_sink_assert_ref(s);
811 pa_sink_assert_io_context(s);
812
813 s->thread_info.rtpoll = p;
814
815 if (s->monitor_source)
816 pa_source_set_rtpoll(s->monitor_source, p);
817 }
818
819 /* Called from main context */
820 int pa_sink_update_status(pa_sink*s) {
821 pa_sink_assert_ref(s);
822 pa_assert_ctl_context();
823 pa_assert(PA_SINK_IS_LINKED(s->state));
824
825 if (s->state == PA_SINK_SUSPENDED)
826 return 0;
827
828 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
829 }
830
831 /* Called from main context */
832 int pa_sink_suspend(pa_sink *s, pa_bool_t suspend, pa_suspend_cause_t cause) {
833 pa_sink_assert_ref(s);
834 pa_assert_ctl_context();
835 pa_assert(PA_SINK_IS_LINKED(s->state));
836 pa_assert(cause != 0);
837
838 if (suspend) {
839 s->suspend_cause |= cause;
840 s->monitor_source->suspend_cause |= cause;
841 } else {
842 s->suspend_cause &= ~cause;
843 s->monitor_source->suspend_cause &= ~cause;
844 }
845
846 if ((pa_sink_get_state(s) == PA_SINK_SUSPENDED) == !!s->suspend_cause)
847 return 0;
848
849 pa_log_debug("Suspend cause of sink %s is 0x%04x, %s", s->name, s->suspend_cause, s->suspend_cause ? "suspending" : "resuming");
850
851 if (s->suspend_cause)
852 return sink_set_state(s, PA_SINK_SUSPENDED);
853 else
854 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
855 }
856
857 /* Called from main context */
858 pa_queue *pa_sink_move_all_start(pa_sink *s, pa_queue *q) {
859 pa_sink_input *i, *n;
860 uint32_t idx;
861
862 pa_sink_assert_ref(s);
863 pa_assert_ctl_context();
864 pa_assert(PA_SINK_IS_LINKED(s->state));
865
866 if (!q)
867 q = pa_queue_new();
868
869 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = n) {
870 n = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx));
871
872 pa_sink_input_ref(i);
873
874 if (pa_sink_input_start_move(i) >= 0)
875 pa_queue_push(q, i);
876 else
877 pa_sink_input_unref(i);
878 }
879
880 return q;
881 }
882
883 /* Called from main context */
884 void pa_sink_move_all_finish(pa_sink *s, pa_queue *q, pa_bool_t save) {
885 pa_sink_input *i;
886
887 pa_sink_assert_ref(s);
888 pa_assert_ctl_context();
889 pa_assert(PA_SINK_IS_LINKED(s->state));
890 pa_assert(q);
891
892 while ((i = PA_SINK_INPUT(pa_queue_pop(q)))) {
893 if (pa_sink_input_finish_move(i, s, save) < 0)
894 pa_sink_input_fail_move(i);
895
896 pa_sink_input_unref(i);
897 }
898
899 pa_queue_free(q, NULL, NULL);
900 }
901
902 /* Called from main context */
903 void pa_sink_move_all_fail(pa_queue *q) {
904 pa_sink_input *i;
905
906 pa_assert_ctl_context();
907 pa_assert(q);
908
909 while ((i = PA_SINK_INPUT(pa_queue_pop(q)))) {
910 pa_sink_input_fail_move(i);
911 pa_sink_input_unref(i);
912 }
913
914 pa_queue_free(q, NULL, NULL);
915 }
916
917 /* Called from IO thread context */
918 void pa_sink_process_rewind(pa_sink *s, size_t nbytes) {
919 pa_sink_input *i;
920 void *state = NULL;
921
922 pa_sink_assert_ref(s);
923 pa_sink_assert_io_context(s);
924 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
925
926 /* If nobody requested this and this is actually no real rewind
927 * then we can short cut this. Please note that this means that
928 * not all rewind requests triggered upstream will always be
929 * translated in actual requests! */
930 if (!s->thread_info.rewind_requested && nbytes <= 0)
931 return;
932
933 s->thread_info.rewind_nbytes = 0;
934 s->thread_info.rewind_requested = FALSE;
935
936 if (s->thread_info.state == PA_SINK_SUSPENDED)
937 return;
938
939 if (nbytes > 0) {
940 pa_log_debug("Processing rewind...");
941 if (s->flags & PA_SINK_DEFERRED_VOLUME)
942 pa_sink_volume_change_rewind(s, nbytes);
943 }
944
945 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state) {
946 pa_sink_input_assert_ref(i);
947 pa_sink_input_process_rewind(i, nbytes);
948 }
949
950 if (nbytes > 0) {
951 if (s->monitor_source && PA_SOURCE_IS_LINKED(s->monitor_source->thread_info.state))
952 pa_source_process_rewind(s->monitor_source, nbytes);
953 }
954 }
955
956 /* Called from IO thread context */
957 static unsigned fill_mix_info(pa_sink *s, size_t *length, pa_mix_info *info, unsigned maxinfo) {
958 pa_sink_input *i;
959 unsigned n = 0;
960 void *state = NULL;
961 size_t mixlength = *length;
962
963 pa_sink_assert_ref(s);
964 pa_sink_assert_io_context(s);
965 pa_assert(info);
966
967 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)) && maxinfo > 0) {
968 pa_sink_input_assert_ref(i);
969
970 pa_sink_input_peek(i, *length, &info->chunk, &info->volume);
971
972 if (mixlength == 0 || info->chunk.length < mixlength)
973 mixlength = info->chunk.length;
974
975 if (pa_memblock_is_silence(info->chunk.memblock)) {
976 pa_memblock_unref(info->chunk.memblock);
977 continue;
978 }
979
980 info->userdata = pa_sink_input_ref(i);
981
982 pa_assert(info->chunk.memblock);
983 pa_assert(info->chunk.length > 0);
984
985 info++;
986 n++;
987 maxinfo--;
988 }
989
990 if (mixlength > 0)
991 *length = mixlength;
992
993 return n;
994 }
995
996 /* Called from IO thread context */
997 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned n, pa_memchunk *result) {
998 pa_sink_input *i;
999 void *state;
1000 unsigned p = 0;
1001 unsigned n_unreffed = 0;
1002
1003 pa_sink_assert_ref(s);
1004 pa_sink_assert_io_context(s);
1005 pa_assert(result);
1006 pa_assert(result->memblock);
1007 pa_assert(result->length > 0);
1008
1009 /* We optimize for the case where the order of the inputs has not changed */
1010
1011 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state) {
1012 unsigned j;
1013 pa_mix_info* m = NULL;
1014
1015 pa_sink_input_assert_ref(i);
1016
1017 /* Let's try to find the matching entry info the pa_mix_info array */
1018 for (j = 0; j < n; j ++) {
1019
1020 if (info[p].userdata == i) {
1021 m = info + p;
1022 break;
1023 }
1024
1025 p++;
1026 if (p >= n)
1027 p = 0;
1028 }
1029
1030 /* Drop read data */
1031 pa_sink_input_drop(i, result->length);
1032
1033 if (s->monitor_source && PA_SOURCE_IS_LINKED(s->monitor_source->thread_info.state)) {
1034
1035 if (pa_hashmap_size(i->thread_info.direct_outputs) > 0) {
1036 void *ostate = NULL;
1037 pa_source_output *o;
1038 pa_memchunk c;
1039
1040 if (m && m->chunk.memblock) {
1041 c = m->chunk;
1042 pa_memblock_ref(c.memblock);
1043 pa_assert(result->length <= c.length);
1044 c.length = result->length;
1045
1046 pa_memchunk_make_writable(&c, 0);
1047 pa_volume_memchunk(&c, &s->sample_spec, &m->volume);
1048 } else {
1049 c = s->silence;
1050 pa_memblock_ref(c.memblock);
1051 pa_assert(result->length <= c.length);
1052 c.length = result->length;
1053 }
1054
1055 while ((o = pa_hashmap_iterate(i->thread_info.direct_outputs, &ostate, NULL))) {
1056 pa_source_output_assert_ref(o);
1057 pa_assert(o->direct_on_input == i);
1058 pa_source_post_direct(s->monitor_source, o, &c);
1059 }
1060
1061 pa_memblock_unref(c.memblock);
1062 }
1063 }
1064
1065 if (m) {
1066 if (m->chunk.memblock)
1067 pa_memblock_unref(m->chunk.memblock);
1068 pa_memchunk_reset(&m->chunk);
1069
1070 pa_sink_input_unref(m->userdata);
1071 m->userdata = NULL;
1072
1073 n_unreffed += 1;
1074 }
1075 }
1076
1077 /* Now drop references to entries that are included in the
1078 * pa_mix_info array but don't exist anymore */
1079
1080 if (n_unreffed < n) {
1081 for (; n > 0; info++, n--) {
1082 if (info->userdata)
1083 pa_sink_input_unref(info->userdata);
1084 if (info->chunk.memblock)
1085 pa_memblock_unref(info->chunk.memblock);
1086 }
1087 }
1088
1089 if (s->monitor_source && PA_SOURCE_IS_LINKED(s->monitor_source->thread_info.state))
1090 pa_source_post(s->monitor_source, result);
1091 }
1092
1093 /* Called from IO thread context */
1094 void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
1095 pa_mix_info info[MAX_MIX_CHANNELS];
1096 unsigned n;
1097 size_t block_size_max;
1098
1099 pa_sink_assert_ref(s);
1100 pa_sink_assert_io_context(s);
1101 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1102 pa_assert(pa_frame_aligned(length, &s->sample_spec));
1103 pa_assert(result);
1104
1105 pa_assert(!s->thread_info.rewind_requested);
1106 pa_assert(s->thread_info.rewind_nbytes == 0);
1107
1108 if (s->thread_info.state == PA_SINK_SUSPENDED) {
1109 result->memblock = pa_memblock_ref(s->silence.memblock);
1110 result->index = s->silence.index;
1111 result->length = PA_MIN(s->silence.length, length);
1112 return;
1113 }
1114
1115 pa_sink_ref(s);
1116
1117 if (length <= 0)
1118 length = pa_frame_align(MIX_BUFFER_LENGTH, &s->sample_spec);
1119
1120 block_size_max = pa_mempool_block_size_max(s->core->mempool);
1121 if (length > block_size_max)
1122 length = pa_frame_align(block_size_max, &s->sample_spec);
1123
1124 pa_assert(length > 0);
1125
1126 n = fill_mix_info(s, &length, info, MAX_MIX_CHANNELS);
1127
1128 if (n == 0) {
1129
1130 *result = s->silence;
1131 pa_memblock_ref(result->memblock);
1132
1133 if (result->length > length)
1134 result->length = length;
1135
1136 } else if (n == 1) {
1137 pa_cvolume volume;
1138
1139 *result = info[0].chunk;
1140 pa_memblock_ref(result->memblock);
1141
1142 if (result->length > length)
1143 result->length = length;
1144
1145 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
1146
1147 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume)) {
1148 pa_memblock_unref(result->memblock);
1149 pa_silence_memchunk_get(&s->core->silence_cache,
1150 s->core->mempool,
1151 result,
1152 &s->sample_spec,
1153 result->length);
1154 } else if (!pa_cvolume_is_norm(&volume)) {
1155 pa_memchunk_make_writable(result, 0);
1156 pa_volume_memchunk(result, &s->sample_spec, &volume);
1157 }
1158 } else {
1159 void *ptr;
1160 result->memblock = pa_memblock_new(s->core->mempool, length);
1161
1162 ptr = pa_memblock_acquire(result->memblock);
1163 result->length = pa_mix(info, n,
1164 ptr, length,
1165 &s->sample_spec,
1166 &s->thread_info.soft_volume,
1167 s->thread_info.soft_muted);
1168 pa_memblock_release(result->memblock);
1169
1170 result->index = 0;
1171 }
1172
1173 inputs_drop(s, info, n, result);
1174
1175 pa_sink_unref(s);
1176 }
1177
1178 /* Called from IO thread context */
1179 void pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
1180 pa_mix_info info[MAX_MIX_CHANNELS];
1181 unsigned n;
1182 size_t length, block_size_max;
1183
1184 pa_sink_assert_ref(s);
1185 pa_sink_assert_io_context(s);
1186 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1187 pa_assert(target);
1188 pa_assert(target->memblock);
1189 pa_assert(target->length > 0);
1190 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
1191
1192 pa_assert(!s->thread_info.rewind_requested);
1193 pa_assert(s->thread_info.rewind_nbytes == 0);
1194
1195 if (s->thread_info.state == PA_SINK_SUSPENDED) {
1196 pa_silence_memchunk(target, &s->sample_spec);
1197 return;
1198 }
1199
1200 pa_sink_ref(s);
1201
1202 length = target->length;
1203 block_size_max = pa_mempool_block_size_max(s->core->mempool);
1204 if (length > block_size_max)
1205 length = pa_frame_align(block_size_max, &s->sample_spec);
1206
1207 pa_assert(length > 0);
1208
1209 n = fill_mix_info(s, &length, info, MAX_MIX_CHANNELS);
1210
1211 if (n == 0) {
1212 if (target->length > length)
1213 target->length = length;
1214
1215 pa_silence_memchunk(target, &s->sample_spec);
1216 } else if (n == 1) {
1217 pa_cvolume volume;
1218
1219 if (target->length > length)
1220 target->length = length;
1221
1222 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
1223
1224 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
1225 pa_silence_memchunk(target, &s->sample_spec);
1226 else {
1227 pa_memchunk vchunk;
1228
1229 vchunk = info[0].chunk;
1230 pa_memblock_ref(vchunk.memblock);
1231
1232 if (vchunk.length > length)
1233 vchunk.length = length;
1234
1235 if (!pa_cvolume_is_norm(&volume)) {
1236 pa_memchunk_make_writable(&vchunk, 0);
1237 pa_volume_memchunk(&vchunk, &s->sample_spec, &volume);
1238 }
1239
1240 pa_memchunk_memcpy(target, &vchunk);
1241 pa_memblock_unref(vchunk.memblock);
1242 }
1243
1244 } else {
1245 void *ptr;
1246
1247 ptr = pa_memblock_acquire(target->memblock);
1248
1249 target->length = pa_mix(info, n,
1250 (uint8_t*) ptr + target->index, length,
1251 &s->sample_spec,
1252 &s->thread_info.soft_volume,
1253 s->thread_info.soft_muted);
1254
1255 pa_memblock_release(target->memblock);
1256 }
1257
1258 inputs_drop(s, info, n, target);
1259
1260 pa_sink_unref(s);
1261 }
1262
1263 /* Called from IO thread context */
1264 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
1265 pa_memchunk chunk;
1266 size_t l, d;
1267
1268 pa_sink_assert_ref(s);
1269 pa_sink_assert_io_context(s);
1270 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1271 pa_assert(target);
1272 pa_assert(target->memblock);
1273 pa_assert(target->length > 0);
1274 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
1275
1276 pa_assert(!s->thread_info.rewind_requested);
1277 pa_assert(s->thread_info.rewind_nbytes == 0);
1278
1279 if (s->thread_info.state == PA_SINK_SUSPENDED) {
1280 pa_silence_memchunk(target, &s->sample_spec);
1281 return;
1282 }
1283
1284 pa_sink_ref(s);
1285
1286 l = target->length;
1287 d = 0;
1288 while (l > 0) {
1289 chunk = *target;
1290 chunk.index += d;
1291 chunk.length -= d;
1292
1293 pa_sink_render_into(s, &chunk);
1294
1295 d += chunk.length;
1296 l -= chunk.length;
1297 }
1298
1299 pa_sink_unref(s);
1300 }
1301
1302 /* Called from IO thread context */
1303 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
1304 pa_sink_assert_ref(s);
1305 pa_sink_assert_io_context(s);
1306 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1307 pa_assert(length > 0);
1308 pa_assert(pa_frame_aligned(length, &s->sample_spec));
1309 pa_assert(result);
1310
1311 pa_assert(!s->thread_info.rewind_requested);
1312 pa_assert(s->thread_info.rewind_nbytes == 0);
1313
1314 pa_sink_ref(s);
1315
1316 pa_sink_render(s, length, result);
1317
1318 if (result->length < length) {
1319 pa_memchunk chunk;
1320
1321 pa_memchunk_make_writable(result, length);
1322
1323 chunk.memblock = result->memblock;
1324 chunk.index = result->index + result->length;
1325 chunk.length = length - result->length;
1326
1327 pa_sink_render_into_full(s, &chunk);
1328
1329 result->length = length;
1330 }
1331
1332 pa_sink_unref(s);
1333 }
1334
1335 /* Called from main thread */
1336 pa_bool_t pa_sink_update_rate(pa_sink *s, uint32_t rate, pa_bool_t passthrough)
1337 {
1338 if (s->update_rate) {
1339 uint32_t desired_rate = rate;
1340 uint32_t default_rate = s->default_sample_rate;
1341 uint32_t alternate_rate = s->alternate_sample_rate;
1342 uint32_t idx;
1343 pa_sink_input *i;
1344 pa_bool_t use_alternate = FALSE;
1345
1346 if (PA_UNLIKELY(default_rate == alternate_rate)) {
1347 pa_log_warn("Default and alternate sample rates are the same.");
1348 return FALSE;
1349 }
1350
1351 if (PA_SINK_IS_RUNNING(s->state)) {
1352 pa_log_info("Cannot update rate, SINK_IS_RUNNING, will keep using %u kHz",
1353 s->sample_spec.rate);
1354 return FALSE;
1355 }
1356
1357 if (s->monitor_source) {
1358 if (PA_SOURCE_IS_RUNNING(s->monitor_source->state) == TRUE) {
1359 pa_log_info("Cannot update rate, monitor source is RUNNING");
1360 return FALSE;
1361 }
1362 }
1363
1364 if (PA_UNLIKELY (desired_rate < 8000 ||
1365 desired_rate > PA_RATE_MAX))
1366 return FALSE;
1367
1368 if (!passthrough) {
1369 pa_assert(default_rate % 4000 || default_rate % 11025);
1370 pa_assert(alternate_rate % 4000 || alternate_rate % 11025);
1371
1372 if (default_rate % 4000) {
1373 /* default is a 11025 multiple */
1374 if ((alternate_rate % 4000 == 0) && (desired_rate % 4000 == 0))
1375 use_alternate=TRUE;
1376 } else {
1377 /* default is 4000 multiple */
1378 if ((alternate_rate % 11025 == 0) && (desired_rate % 11025 == 0))
1379 use_alternate=TRUE;
1380 }
1381
1382 if (use_alternate)
1383 desired_rate = alternate_rate;
1384 else
1385 desired_rate = default_rate;
1386 } else {
1387 desired_rate = rate; /* use stream sampling rate, discard default/alternate settings */
1388 }
1389
1390 if (!passthrough && pa_sink_used_by(s) > 0)
1391 return FALSE;
1392
1393 pa_sink_suspend(s, TRUE, PA_SUSPEND_IDLE); /* needed before rate update, will be resumed automatically */
1394
1395 if (s->update_rate(s, desired_rate) == TRUE) {
1396 /* update monitor source as well */
1397 if (s->monitor_source && !passthrough)
1398 pa_source_update_rate(s->monitor_source, desired_rate, FALSE);
1399 pa_log_info("Changed sampling rate successfully");
1400
1401 PA_IDXSET_FOREACH(i, s->inputs, idx) {
1402 if (i->state == PA_SINK_INPUT_CORKED)
1403 pa_sink_input_update_rate(i);
1404 }
1405
1406 return TRUE;
1407 }
1408 }
1409 return FALSE;
1410 }
1411
1412 /* Called from main thread */
1413 pa_usec_t pa_sink_get_latency(pa_sink *s) {
1414 pa_usec_t usec = 0;
1415
1416 pa_sink_assert_ref(s);
1417 pa_assert_ctl_context();
1418 pa_assert(PA_SINK_IS_LINKED(s->state));
1419
1420 /* The returned value is supposed to be in the time domain of the sound card! */
1421
1422 if (s->state == PA_SINK_SUSPENDED)
1423 return 0;
1424
1425 if (!(s->flags & PA_SINK_LATENCY))
1426 return 0;
1427
1428 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) == 0);
1429
1430 return usec;
1431 }
1432
1433 /* Called from IO thread */
1434 pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s) {
1435 pa_usec_t usec = 0;
1436 pa_msgobject *o;
1437
1438 pa_sink_assert_ref(s);
1439 pa_sink_assert_io_context(s);
1440 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1441
1442 /* The returned value is supposed to be in the time domain of the sound card! */
1443
1444 if (s->thread_info.state == PA_SINK_SUSPENDED)
1445 return 0;
1446
1447 if (!(s->flags & PA_SINK_LATENCY))
1448 return 0;
1449
1450 o = PA_MSGOBJECT(s);
1451
1452 /* FIXME: We probably should make this a proper vtable callback instead of going through process_msg() */
1453
1454 if (o->process_msg(o, PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
1455 return -1;
1456
1457 return usec;
1458 }
1459
1460 /* Called from the main thread (and also from the IO thread while the main
1461 * thread is waiting).
1462 *
1463 * When a sink uses volume sharing, it never has the PA_SINK_FLAT_VOLUME flag
1464 * set. Instead, flat volume mode is detected by checking whether the root sink
1465 * has the flag set. */
1466 pa_bool_t pa_sink_flat_volume_enabled(pa_sink *s) {
1467 pa_sink_assert_ref(s);
1468
1469 s = pa_sink_get_master(s);
1470
1471 if (PA_LIKELY(s))
1472 return (s->flags & PA_SINK_FLAT_VOLUME);
1473 else
1474 return FALSE;
1475 }
1476
1477 /* Called from the main thread (and also from the IO thread while the main
1478 * thread is waiting). */
1479 pa_sink *pa_sink_get_master(pa_sink *s) {
1480 pa_sink_assert_ref(s);
1481
1482 while (s && (s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
1483 if (PA_UNLIKELY(!s->input_to_master))
1484 return NULL;
1485
1486 s = s->input_to_master->sink;
1487 }
1488
1489 return s;
1490 }
1491
1492 /* Called from main context */
1493 pa_bool_t pa_sink_is_passthrough(pa_sink *s) {
1494 pa_sink_input *alt_i;
1495 uint32_t idx;
1496
1497 pa_sink_assert_ref(s);
1498
1499 /* one and only one PASSTHROUGH input can possibly be connected */
1500 if (pa_idxset_size(s->inputs) == 1) {
1501 alt_i = pa_idxset_first(s->inputs, &idx);
1502
1503 if (pa_sink_input_is_passthrough(alt_i))
1504 return TRUE;
1505 }
1506
1507 return FALSE;
1508 }
1509
1510 /* Called from main context */
1511 void pa_sink_enter_passthrough(pa_sink *s) {
1512 pa_cvolume volume;
1513
1514 /* disable the monitor in passthrough mode */
1515 if (s->monitor_source)
1516 pa_source_suspend(s->monitor_source, TRUE, PA_SUSPEND_PASSTHROUGH);
1517
1518 /* set the volume to NORM */
1519 s->saved_volume = *pa_sink_get_volume(s, TRUE);
1520 s->saved_save_volume = s->save_volume;
1521
1522 pa_cvolume_set(&volume, s->sample_spec.channels, PA_MIN(s->base_volume, PA_VOLUME_NORM));
1523 pa_sink_set_volume(s, &volume, TRUE, FALSE);
1524 }
1525
1526 /* Called from main context */
1527 void pa_sink_leave_passthrough(pa_sink *s) {
1528 /* Unsuspend monitor */
1529 if (s->monitor_source)
1530 pa_source_suspend(s->monitor_source, FALSE, PA_SUSPEND_PASSTHROUGH);
1531
1532 /* Restore sink volume to what it was before we entered passthrough mode */
1533 pa_sink_set_volume(s, &s->saved_volume, TRUE, s->saved_save_volume);
1534
1535 pa_cvolume_init(&s->saved_volume);
1536 s->saved_save_volume = FALSE;
1537 }
1538
1539 /* Called from main context. */
1540 static void compute_reference_ratio(pa_sink_input *i) {
1541 unsigned c = 0;
1542 pa_cvolume remapped;
1543
1544 pa_assert(i);
1545 pa_assert(pa_sink_flat_volume_enabled(i->sink));
1546
1547 /*
1548 * Calculates the reference ratio from the sink's reference
1549 * volume. This basically calculates:
1550 *
1551 * i->reference_ratio = i->volume / i->sink->reference_volume
1552 */
1553
1554 remapped = i->sink->reference_volume;
1555 pa_cvolume_remap(&remapped, &i->sink->channel_map, &i->channel_map);
1556
1557 i->reference_ratio.channels = i->sample_spec.channels;
1558
1559 for (c = 0; c < i->sample_spec.channels; c++) {
1560
1561 /* We don't update when the sink volume is 0 anyway */
1562 if (remapped.values[c] <= PA_VOLUME_MUTED)
1563 continue;
1564
1565 /* Don't update the reference ratio unless necessary */
1566 if (pa_sw_volume_multiply(
1567 i->reference_ratio.values[c],
1568 remapped.values[c]) == i->volume.values[c])
1569 continue;
1570
1571 i->reference_ratio.values[c] = pa_sw_volume_divide(
1572 i->volume.values[c],
1573 remapped.values[c]);
1574 }
1575 }
1576
1577 /* Called from main context. Only called for the root sink in volume sharing
1578 * cases, except for internal recursive calls. */
1579 static void compute_reference_ratios(pa_sink *s) {
1580 uint32_t idx;
1581 pa_sink_input *i;
1582
1583 pa_sink_assert_ref(s);
1584 pa_assert_ctl_context();
1585 pa_assert(PA_SINK_IS_LINKED(s->state));
1586 pa_assert(pa_sink_flat_volume_enabled(s));
1587
1588 PA_IDXSET_FOREACH(i, s->inputs, idx) {
1589 compute_reference_ratio(i);
1590
1591 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER))
1592 compute_reference_ratios(i->origin_sink);
1593 }
1594 }
1595
1596 /* Called from main context. Only called for the root sink in volume sharing
1597 * cases, except for internal recursive calls. */
1598 static void compute_real_ratios(pa_sink *s) {
1599 pa_sink_input *i;
1600 uint32_t idx;
1601
1602 pa_sink_assert_ref(s);
1603 pa_assert_ctl_context();
1604 pa_assert(PA_SINK_IS_LINKED(s->state));
1605 pa_assert(pa_sink_flat_volume_enabled(s));
1606
1607 PA_IDXSET_FOREACH(i, s->inputs, idx) {
1608 unsigned c;
1609 pa_cvolume remapped;
1610
1611 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
1612 /* The origin sink uses volume sharing, so this input's real ratio
1613 * is handled as a special case - the real ratio must be 0 dB, and
1614 * as a result i->soft_volume must equal i->volume_factor. */
1615 pa_cvolume_reset(&i->real_ratio, i->real_ratio.channels);
1616 i->soft_volume = i->volume_factor;
1617
1618 compute_real_ratios(i->origin_sink);
1619
1620 continue;
1621 }
1622
1623 /*
1624 * This basically calculates:
1625 *
1626 * i->real_ratio := i->volume / s->real_volume
1627 * i->soft_volume := i->real_ratio * i->volume_factor
1628 */
1629
1630 remapped = s->real_volume;
1631 pa_cvolume_remap(&remapped, &s->channel_map, &i->channel_map);
1632
1633 i->real_ratio.channels = i->sample_spec.channels;
1634 i->soft_volume.channels = i->sample_spec.channels;
1635
1636 for (c = 0; c < i->sample_spec.channels; c++) {
1637
1638 if (remapped.values[c] <= PA_VOLUME_MUTED) {
1639 /* We leave i->real_ratio untouched */
1640 i->soft_volume.values[c] = PA_VOLUME_MUTED;
1641 continue;
1642 }
1643
1644 /* Don't lose accuracy unless necessary */
1645 if (pa_sw_volume_multiply(
1646 i->real_ratio.values[c],
1647 remapped.values[c]) != i->volume.values[c])
1648
1649 i->real_ratio.values[c] = pa_sw_volume_divide(
1650 i->volume.values[c],
1651 remapped.values[c]);
1652
1653 i->soft_volume.values[c] = pa_sw_volume_multiply(
1654 i->real_ratio.values[c],
1655 i->volume_factor.values[c]);
1656 }
1657
1658 /* We don't copy the soft_volume to the thread_info data
1659 * here. That must be done by the caller */
1660 }
1661 }
1662
1663 static pa_cvolume *cvolume_remap_minimal_impact(
1664 pa_cvolume *v,
1665 const pa_cvolume *template,
1666 const pa_channel_map *from,
1667 const pa_channel_map *to) {
1668
1669 pa_cvolume t;
1670
1671 pa_assert(v);
1672 pa_assert(template);
1673 pa_assert(from);
1674 pa_assert(to);
1675 pa_assert(pa_cvolume_compatible_with_channel_map(v, from));
1676 pa_assert(pa_cvolume_compatible_with_channel_map(template, to));
1677
1678 /* Much like pa_cvolume_remap(), but tries to minimize impact when
1679 * mapping from sink input to sink volumes:
1680 *
1681 * If template is a possible remapping from v it is used instead
1682 * of remapping anew.
1683 *
1684 * If the channel maps don't match we set an all-channel volume on
1685 * the sink to ensure that changing a volume on one stream has no
1686 * effect that cannot be compensated for in another stream that
1687 * does not have the same channel map as the sink. */
1688
1689 if (pa_channel_map_equal(from, to))
1690 return v;
1691
1692 t = *template;
1693 if (pa_cvolume_equal(pa_cvolume_remap(&t, to, from), v)) {
1694 *v = *template;
1695 return v;
1696 }
1697
1698 pa_cvolume_set(v, to->channels, pa_cvolume_max(v));
1699 return v;
1700 }
1701
1702 /* Called from main thread. Only called for the root sink in volume sharing
1703 * cases, except for internal recursive calls. */
1704 static void get_maximum_input_volume(pa_sink *s, pa_cvolume *max_volume, const pa_channel_map *channel_map) {
1705 pa_sink_input *i;
1706 uint32_t idx;
1707
1708 pa_sink_assert_ref(s);
1709 pa_assert(max_volume);
1710 pa_assert(channel_map);
1711 pa_assert(pa_sink_flat_volume_enabled(s));
1712
1713 PA_IDXSET_FOREACH(i, s->inputs, idx) {
1714 pa_cvolume remapped;
1715
1716 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
1717 get_maximum_input_volume(i->origin_sink, max_volume, channel_map);
1718
1719 /* Ignore this input. The origin sink uses volume sharing, so this
1720 * input's volume will be set to be equal to the root sink's real
1721 * volume. Obviously this input's current volume must not then
1722 * affect what the root sink's real volume will be. */
1723 continue;
1724 }
1725
1726 remapped = i->volume;
1727 cvolume_remap_minimal_impact(&remapped, max_volume, &i->channel_map, channel_map);
1728 pa_cvolume_merge(max_volume, max_volume, &remapped);
1729 }
1730 }
1731
1732 /* Called from main thread. Only called for the root sink in volume sharing
1733 * cases, except for internal recursive calls. */
1734 static pa_bool_t has_inputs(pa_sink *s) {
1735 pa_sink_input *i;
1736 uint32_t idx;
1737
1738 pa_sink_assert_ref(s);
1739
1740 PA_IDXSET_FOREACH(i, s->inputs, idx) {
1741 if (!i->origin_sink || !(i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER) || has_inputs(i->origin_sink))
1742 return TRUE;
1743 }
1744
1745 return FALSE;
1746 }
1747
1748 /* Called from main thread. Only called for the root sink in volume sharing
1749 * cases, except for internal recursive calls. */
1750 static void update_real_volume(pa_sink *s, const pa_cvolume *new_volume, pa_channel_map *channel_map) {
1751 pa_sink_input *i;
1752 uint32_t idx;
1753
1754 pa_sink_assert_ref(s);
1755 pa_assert(new_volume);
1756 pa_assert(channel_map);
1757
1758 s->real_volume = *new_volume;
1759 pa_cvolume_remap(&s->real_volume, channel_map, &s->channel_map);
1760
1761 PA_IDXSET_FOREACH(i, s->inputs, idx) {
1762 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
1763 if (pa_sink_flat_volume_enabled(s)) {
1764 pa_cvolume old_volume = i->volume;
1765
1766 /* Follow the root sink's real volume. */
1767 i->volume = *new_volume;
1768 pa_cvolume_remap(&i->volume, channel_map, &i->channel_map);
1769 compute_reference_ratio(i);
1770
1771 /* The volume changed, let's tell people so */
1772 if (!pa_cvolume_equal(&old_volume, &i->volume)) {
1773 if (i->volume_changed)
1774 i->volume_changed(i);
1775
1776 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1777 }
1778 }
1779
1780 update_real_volume(i->origin_sink, new_volume, channel_map);
1781 }
1782 }
1783 }
1784
1785 /* Called from main thread. Only called for the root sink in shared volume
1786 * cases. */
1787 static void compute_real_volume(pa_sink *s) {
1788 pa_sink_assert_ref(s);
1789 pa_assert_ctl_context();
1790 pa_assert(PA_SINK_IS_LINKED(s->state));
1791 pa_assert(pa_sink_flat_volume_enabled(s));
1792 pa_assert(!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER));
1793
1794 /* This determines the maximum volume of all streams and sets
1795 * s->real_volume accordingly. */
1796
1797 if (!has_inputs(s)) {
1798 /* In the special case that we have no sink inputs we leave the
1799 * volume unmodified. */
1800 update_real_volume(s, &s->reference_volume, &s->channel_map);
1801 return;
1802 }
1803
1804 pa_cvolume_mute(&s->real_volume, s->channel_map.channels);
1805
1806 /* First let's determine the new maximum volume of all inputs
1807 * connected to this sink */
1808 get_maximum_input_volume(s, &s->real_volume, &s->channel_map);
1809 update_real_volume(s, &s->real_volume, &s->channel_map);
1810
1811 /* Then, let's update the real ratios/soft volumes of all inputs
1812 * connected to this sink */
1813 compute_real_ratios(s);
1814 }
1815
1816 /* Called from main thread. Only called for the root sink in shared volume
1817 * cases, except for internal recursive calls. */
1818 static void propagate_reference_volume(pa_sink *s) {
1819 pa_sink_input *i;
1820 uint32_t idx;
1821
1822 pa_sink_assert_ref(s);
1823 pa_assert_ctl_context();
1824 pa_assert(PA_SINK_IS_LINKED(s->state));
1825 pa_assert(pa_sink_flat_volume_enabled(s));
1826
1827 /* This is called whenever the sink volume changes that is not
1828 * caused by a sink input volume change. We need to fix up the
1829 * sink input volumes accordingly */
1830
1831 PA_IDXSET_FOREACH(i, s->inputs, idx) {
1832 pa_cvolume old_volume;
1833
1834 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
1835 propagate_reference_volume(i->origin_sink);
1836
1837 /* Since the origin sink uses volume sharing, this input's volume
1838 * needs to be updated to match the root sink's real volume, but
1839 * that will be done later in update_shared_real_volume(). */
1840 continue;
1841 }
1842
1843 old_volume = i->volume;
1844
1845 /* This basically calculates:
1846 *
1847 * i->volume := s->reference_volume * i->reference_ratio */
1848
1849 i->volume = s->reference_volume;
1850 pa_cvolume_remap(&i->volume, &s->channel_map, &i->channel_map);
1851 pa_sw_cvolume_multiply(&i->volume, &i->volume, &i->reference_ratio);
1852
1853 /* The volume changed, let's tell people so */
1854 if (!pa_cvolume_equal(&old_volume, &i->volume)) {
1855
1856 if (i->volume_changed)
1857 i->volume_changed(i);
1858
1859 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1860 }
1861 }
1862 }
1863
1864 /* Called from main thread. Only called for the root sink in volume sharing
1865 * cases, except for internal recursive calls. The return value indicates
1866 * whether any reference volume actually changed. */
1867 static pa_bool_t update_reference_volume(pa_sink *s, const pa_cvolume *v, const pa_channel_map *channel_map, pa_bool_t save) {
1868 pa_cvolume volume;
1869 pa_bool_t reference_volume_changed;
1870 pa_sink_input *i;
1871 uint32_t idx;
1872
1873 pa_sink_assert_ref(s);
1874 pa_assert(PA_SINK_IS_LINKED(s->state));
1875 pa_assert(v);
1876 pa_assert(channel_map);
1877 pa_assert(pa_cvolume_valid(v));
1878
1879 volume = *v;
1880 pa_cvolume_remap(&volume, channel_map, &s->channel_map);
1881
1882 reference_volume_changed = !pa_cvolume_equal(&volume, &s->reference_volume);
1883 s->reference_volume = volume;
1884
1885 s->save_volume = (!reference_volume_changed && s->save_volume) || save;
1886
1887 if (reference_volume_changed)
1888 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1889 else if (!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER))
1890 /* If the root sink's volume doesn't change, then there can't be any
1891 * changes in the other sinks in the sink tree either.
1892 *
1893 * It's probably theoretically possible that even if the root sink's
1894 * volume changes slightly, some filter sink doesn't change its volume
1895 * due to rounding errors. If that happens, we still want to propagate
1896 * the changed root sink volume to the sinks connected to the
1897 * intermediate sink that didn't change its volume. This theoretical
1898 * possibility is the reason why we have that !(s->flags &
1899 * PA_SINK_SHARE_VOLUME_WITH_MASTER) condition. Probably nobody would
1900 * notice even if we returned here FALSE always if
1901 * reference_volume_changed is FALSE. */
1902 return FALSE;
1903
1904 PA_IDXSET_FOREACH(i, s->inputs, idx) {
1905 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER))
1906 update_reference_volume(i->origin_sink, v, channel_map, FALSE);
1907 }
1908
1909 return TRUE;
1910 }
1911
1912 /* Called from main thread */
1913 void pa_sink_set_volume(
1914 pa_sink *s,
1915 const pa_cvolume *volume,
1916 pa_bool_t send_msg,
1917 pa_bool_t save) {
1918
1919 pa_cvolume new_reference_volume;
1920 pa_sink *root_sink;
1921
1922 pa_sink_assert_ref(s);
1923 pa_assert_ctl_context();
1924 pa_assert(PA_SINK_IS_LINKED(s->state));
1925 pa_assert(!volume || pa_cvolume_valid(volume));
1926 pa_assert(volume || pa_sink_flat_volume_enabled(s));
1927 pa_assert(!volume || volume->channels == 1 || pa_cvolume_compatible(volume, &s->sample_spec));
1928
1929 /* make sure we don't change the volume when a PASSTHROUGH input is connected ...
1930 * ... *except* if we're being invoked to reset the volume to ensure 0 dB gain */
1931 if (pa_sink_is_passthrough(s) && (!volume || !pa_cvolume_is_norm(volume))) {
1932 pa_log_warn("Cannot change volume, Sink is connected to PASSTHROUGH input");
1933 return;
1934 }
1935
1936 /* In case of volume sharing, the volume is set for the root sink first,
1937 * from which it's then propagated to the sharing sinks. */
1938 root_sink = pa_sink_get_master(s);
1939
1940 if (PA_UNLIKELY(!root_sink))
1941 return;
1942
1943 /* As a special exception we accept mono volumes on all sinks --
1944 * even on those with more complex channel maps */
1945
1946 if (volume) {
1947 if (pa_cvolume_compatible(volume, &s->sample_spec))
1948 new_reference_volume = *volume;
1949 else {
1950 new_reference_volume = s->reference_volume;
1951 pa_cvolume_scale(&new_reference_volume, pa_cvolume_max(volume));
1952 }
1953
1954 pa_cvolume_remap(&new_reference_volume, &s->channel_map, &root_sink->channel_map);
1955
1956 if (update_reference_volume(root_sink, &new_reference_volume, &root_sink->channel_map, save)) {
1957 if (pa_sink_flat_volume_enabled(root_sink)) {
1958 /* OK, propagate this volume change back to the inputs */
1959 propagate_reference_volume(root_sink);
1960
1961 /* And now recalculate the real volume */
1962 compute_real_volume(root_sink);
1963 } else
1964 update_real_volume(root_sink, &root_sink->reference_volume, &root_sink->channel_map);
1965 }
1966
1967 } else {
1968 /* If volume is NULL we synchronize the sink's real and
1969 * reference volumes with the stream volumes. */
1970
1971 pa_assert(pa_sink_flat_volume_enabled(root_sink));
1972
1973 /* Ok, let's determine the new real volume */
1974 compute_real_volume(root_sink);
1975
1976 /* Let's 'push' the reference volume if necessary */
1977 pa_cvolume_merge(&new_reference_volume, &s->reference_volume, &root_sink->real_volume);
1978 /* If the sink and it's root don't have the same number of channels, we need to remap */
1979 if (s != root_sink && !pa_channel_map_equal(&s->channel_map, &root_sink->channel_map))
1980 pa_cvolume_remap(&new_reference_volume, &s->channel_map, &root_sink->channel_map);
1981 update_reference_volume(root_sink, &new_reference_volume, &root_sink->channel_map, save);
1982
1983 /* Now that the reference volume is updated, we can update the streams'
1984 * reference ratios. */
1985 compute_reference_ratios(root_sink);
1986 }
1987
1988 if (root_sink->set_volume) {
1989 /* If we have a function set_volume(), then we do not apply a
1990 * soft volume by default. However, set_volume() is free to
1991 * apply one to root_sink->soft_volume */
1992
1993 pa_cvolume_reset(&root_sink->soft_volume, root_sink->sample_spec.channels);
1994 if (!(root_sink->flags & PA_SINK_DEFERRED_VOLUME))
1995 root_sink->set_volume(root_sink);
1996
1997 } else
1998 /* If we have no function set_volume(), then the soft volume
1999 * becomes the real volume */
2000 root_sink->soft_volume = root_sink->real_volume;
2001
2002 /* This tells the sink that soft volume and/or real volume changed */
2003 if (send_msg)
2004 pa_assert_se(pa_asyncmsgq_send(root_sink->asyncmsgq, PA_MSGOBJECT(root_sink), PA_SINK_MESSAGE_SET_SHARED_VOLUME, NULL, 0, NULL) == 0);
2005 }
2006
2007 /* Called from the io thread if sync volume is used, otherwise from the main thread.
2008 * Only to be called by sink implementor */
2009 void pa_sink_set_soft_volume(pa_sink *s, const pa_cvolume *volume) {
2010
2011 pa_sink_assert_ref(s);
2012 pa_assert(!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER));
2013
2014 if (s->flags & PA_SINK_DEFERRED_VOLUME)
2015 pa_sink_assert_io_context(s);
2016 else
2017 pa_assert_ctl_context();
2018
2019 if (!volume)
2020 pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
2021 else
2022 s->soft_volume = *volume;
2023
2024 if (PA_SINK_IS_LINKED(s->state) && !(s->flags & PA_SINK_DEFERRED_VOLUME))
2025 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
2026 else
2027 s->thread_info.soft_volume = s->soft_volume;
2028 }
2029
2030 /* Called from the main thread. Only called for the root sink in volume sharing
2031 * cases, except for internal recursive calls. */
2032 static void propagate_real_volume(pa_sink *s, const pa_cvolume *old_real_volume) {
2033 pa_sink_input *i;
2034 uint32_t idx;
2035
2036 pa_sink_assert_ref(s);
2037 pa_assert(old_real_volume);
2038 pa_assert_ctl_context();
2039 pa_assert(PA_SINK_IS_LINKED(s->state));
2040
2041 /* This is called when the hardware's real volume changes due to
2042 * some external event. We copy the real volume into our
2043 * reference volume and then rebuild the stream volumes based on
2044 * i->real_ratio which should stay fixed. */
2045
2046 if (!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
2047 if (pa_cvolume_equal(old_real_volume, &s->real_volume))
2048 return;
2049
2050 /* 1. Make the real volume the reference volume */
2051 update_reference_volume(s, &s->real_volume, &s->channel_map, TRUE);
2052 }
2053
2054 if (pa_sink_flat_volume_enabled(s)) {
2055
2056 PA_IDXSET_FOREACH(i, s->inputs, idx) {
2057 pa_cvolume old_volume = i->volume;
2058
2059 /* 2. Since the sink's reference and real volumes are equal
2060 * now our ratios should be too. */
2061 i->reference_ratio = i->real_ratio;
2062
2063 /* 3. Recalculate the new stream reference volume based on the
2064 * reference ratio and the sink's reference volume.
2065 *
2066 * This basically calculates:
2067 *
2068 * i->volume = s->reference_volume * i->reference_ratio
2069 *
2070 * This is identical to propagate_reference_volume() */
2071 i->volume = s->reference_volume;
2072 pa_cvolume_remap(&i->volume, &s->channel_map, &i->channel_map);
2073 pa_sw_cvolume_multiply(&i->volume, &i->volume, &i->reference_ratio);
2074
2075 /* Notify if something changed */
2076 if (!pa_cvolume_equal(&old_volume, &i->volume)) {
2077
2078 if (i->volume_changed)
2079 i->volume_changed(i);
2080
2081 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
2082 }
2083
2084 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER))
2085 propagate_real_volume(i->origin_sink, old_real_volume);
2086 }
2087 }
2088
2089 /* Something got changed in the hardware. It probably makes sense
2090 * to save changed hw settings given that hw volume changes not
2091 * triggered by PA are almost certainly done by the user. */
2092 if (!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER))
2093 s->save_volume = TRUE;
2094 }
2095
2096 /* Called from io thread */
2097 void pa_sink_update_volume_and_mute(pa_sink *s) {
2098 pa_assert(s);
2099 pa_sink_assert_io_context(s);
2100
2101 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_UPDATE_VOLUME_AND_MUTE, NULL, 0, NULL, NULL);
2102 }
2103
2104 /* Called from main thread */
2105 const pa_cvolume *pa_sink_get_volume(pa_sink *s, pa_bool_t force_refresh) {
2106 pa_sink_assert_ref(s);
2107 pa_assert_ctl_context();
2108 pa_assert(PA_SINK_IS_LINKED(s->state));
2109
2110 if (s->refresh_volume || force_refresh) {
2111 struct pa_cvolume old_real_volume;
2112
2113 pa_assert(!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER));
2114
2115 old_real_volume = s->real_volume;
2116
2117 if (!(s->flags & PA_SINK_DEFERRED_VOLUME) && s->get_volume)
2118 s->get_volume(s);
2119
2120 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, NULL, 0, NULL) == 0);
2121
2122 update_real_volume(s, &s->real_volume, &s->channel_map);
2123 propagate_real_volume(s, &old_real_volume);
2124 }
2125
2126 return &s->reference_volume;
2127 }
2128
2129 /* Called from main thread. In volume sharing cases, only the root sink may
2130 * call this. */
2131 void pa_sink_volume_changed(pa_sink *s, const pa_cvolume *new_real_volume) {
2132 pa_cvolume old_real_volume;
2133
2134 pa_sink_assert_ref(s);
2135 pa_assert_ctl_context();
2136 pa_assert(PA_SINK_IS_LINKED(s->state));
2137 pa_assert(!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER));
2138
2139 /* The sink implementor may call this if the volume changed to make sure everyone is notified */
2140
2141 old_real_volume = s->real_volume;
2142 update_real_volume(s, new_real_volume, &s->channel_map);
2143 propagate_real_volume(s, &old_real_volume);
2144 }
2145
2146 /* Called from main thread */
2147 void pa_sink_set_mute(pa_sink *s, pa_bool_t mute, pa_bool_t save) {
2148 pa_bool_t old_muted;
2149
2150 pa_sink_assert_ref(s);
2151 pa_assert_ctl_context();
2152 pa_assert(PA_SINK_IS_LINKED(s->state));
2153
2154 old_muted = s->muted;
2155 s->muted = mute;
2156 s->save_muted = (old_muted == s->muted && s->save_muted) || save;
2157
2158 if (!(s->flags & PA_SINK_DEFERRED_VOLUME) && s->set_mute)
2159 s->set_mute(s);
2160
2161 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, NULL, 0, NULL) == 0);
2162
2163 if (old_muted != s->muted)
2164 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
2165 }
2166
2167 /* Called from main thread */
2168 pa_bool_t pa_sink_get_mute(pa_sink *s, pa_bool_t force_refresh) {
2169
2170 pa_sink_assert_ref(s);
2171 pa_assert_ctl_context();
2172 pa_assert(PA_SINK_IS_LINKED(s->state));
2173
2174 if (s->refresh_muted || force_refresh) {
2175 pa_bool_t old_muted = s->muted;
2176
2177 if (!(s->flags & PA_SINK_DEFERRED_VOLUME) && s->get_mute)
2178 s->get_mute(s);
2179
2180 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MUTE, NULL, 0, NULL) == 0);
2181
2182 if (old_muted != s->muted) {
2183 s->save_muted = TRUE;
2184
2185 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
2186
2187 /* Make sure the soft mute status stays in sync */
2188 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, NULL, 0, NULL) == 0);
2189 }
2190 }
2191
2192 return s->muted;
2193 }
2194
2195 /* Called from main thread */
2196 void pa_sink_mute_changed(pa_sink *s, pa_bool_t new_muted) {
2197 pa_sink_assert_ref(s);
2198 pa_assert_ctl_context();
2199 pa_assert(PA_SINK_IS_LINKED(s->state));
2200
2201 /* The sink implementor may call this if the volume changed to make sure everyone is notified */
2202
2203 if (s->muted == new_muted)
2204 return;
2205
2206 s->muted = new_muted;
2207 s->save_muted = TRUE;
2208
2209 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
2210 }
2211
2212 /* Called from main thread */
2213 pa_bool_t pa_sink_update_proplist(pa_sink *s, pa_update_mode_t mode, pa_proplist *p) {
2214 pa_sink_assert_ref(s);
2215 pa_assert_ctl_context();
2216
2217 if (p)
2218 pa_proplist_update(s->proplist, mode, p);
2219
2220 if (PA_SINK_IS_LINKED(s->state)) {
2221 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], s);
2222 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
2223 }
2224
2225 return TRUE;
2226 }
2227
2228 /* Called from main thread */
2229 /* FIXME -- this should be dropped and be merged into pa_sink_update_proplist() */
2230 void pa_sink_set_description(pa_sink *s, const char *description) {
2231 const char *old;
2232 pa_sink_assert_ref(s);
2233 pa_assert_ctl_context();
2234
2235 if (!description && !pa_proplist_contains(s->proplist, PA_PROP_DEVICE_DESCRIPTION))
2236 return;
2237
2238 old = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
2239
2240 if (old && description && pa_streq(old, description))
2241 return;
2242
2243 if (description)
2244 pa_proplist_sets(s->proplist, PA_PROP_DEVICE_DESCRIPTION, description);
2245 else
2246 pa_proplist_unset(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
2247
2248 if (s->monitor_source) {
2249 char *n;
2250
2251 n = pa_sprintf_malloc("Monitor Source of %s", description ? description : s->name);
2252 pa_source_set_description(s->monitor_source, n);
2253 pa_xfree(n);
2254 }
2255
2256 if (PA_SINK_IS_LINKED(s->state)) {
2257 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
2258 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], s);
2259 }
2260 }
2261
2262 /* Called from main thread */
2263 unsigned pa_sink_linked_by(pa_sink *s) {
2264 unsigned ret;
2265
2266 pa_sink_assert_ref(s);
2267 pa_assert_ctl_context();
2268 pa_assert(PA_SINK_IS_LINKED(s->state));
2269
2270 ret = pa_idxset_size(s->inputs);
2271
2272 /* We add in the number of streams connected to us here. Please
2273 * note the asymmetry to pa_sink_used_by()! */
2274
2275 if (s->monitor_source)
2276 ret += pa_source_linked_by(s->monitor_source);
2277
2278 return ret;
2279 }
2280
2281 /* Called from main thread */
2282 unsigned pa_sink_used_by(pa_sink *s) {
2283 unsigned ret;
2284
2285 pa_sink_assert_ref(s);
2286 pa_assert_ctl_context();
2287 pa_assert(PA_SINK_IS_LINKED(s->state));
2288
2289 ret = pa_idxset_size(s->inputs);
2290 pa_assert(ret >= s->n_corked);
2291
2292 /* Streams connected to our monitor source do not matter for
2293 * pa_sink_used_by()!.*/
2294
2295 return ret - s->n_corked;
2296 }
2297
2298 /* Called from main thread */
2299 unsigned pa_sink_check_suspend(pa_sink *s) {
2300 unsigned ret;
2301 pa_sink_input *i;
2302 uint32_t idx;
2303
2304 pa_sink_assert_ref(s);
2305 pa_assert_ctl_context();
2306
2307 if (!PA_SINK_IS_LINKED(s->state))
2308 return 0;
2309
2310 ret = 0;
2311
2312 PA_IDXSET_FOREACH(i, s->inputs, idx) {
2313 pa_sink_input_state_t st;
2314
2315 st = pa_sink_input_get_state(i);
2316
2317 /* We do not assert here. It is perfectly valid for a sink input to
2318 * be in the INIT state (i.e. created, marked done but not yet put)
2319 * and we should not care if it's unlinked as it won't contribute
2320 * towards our busy status.
2321 */
2322 if (!PA_SINK_INPUT_IS_LINKED(st))
2323 continue;
2324
2325 if (st == PA_SINK_INPUT_CORKED)
2326 continue;
2327
2328 if (i->flags & PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND)
2329 continue;
2330
2331 ret ++;
2332 }
2333
2334 if (s->monitor_source)
2335 ret += pa_source_check_suspend(s->monitor_source);
2336
2337 return ret;
2338 }
2339
2340 /* Called from the IO thread */
2341 static void sync_input_volumes_within_thread(pa_sink *s) {
2342 pa_sink_input *i;
2343 void *state = NULL;
2344
2345 pa_sink_assert_ref(s);
2346 pa_sink_assert_io_context(s);
2347
2348 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state) {
2349 if (pa_cvolume_equal(&i->thread_info.soft_volume, &i->soft_volume))
2350 continue;
2351
2352 i->thread_info.soft_volume = i->soft_volume;
2353 pa_sink_input_request_rewind(i, 0, TRUE, FALSE, FALSE);
2354 }
2355 }
2356
2357 /* Called from the IO thread. Only called for the root sink in volume sharing
2358 * cases, except for internal recursive calls. */
2359 static void set_shared_volume_within_thread(pa_sink *s) {
2360 pa_sink_input *i = NULL;
2361 void *state = NULL;
2362
2363 pa_sink_assert_ref(s);
2364
2365 PA_MSGOBJECT(s)->process_msg(PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME_SYNCED, NULL, 0, NULL);
2366
2367 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state) {
2368 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER))
2369 set_shared_volume_within_thread(i->origin_sink);
2370 }
2371 }
2372
2373 /* Called from IO thread, except when it is not */
2374 int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
2375 pa_sink *s = PA_SINK(o);
2376 pa_sink_assert_ref(s);
2377
2378 switch ((pa_sink_message_t) code) {
2379
2380 case PA_SINK_MESSAGE_ADD_INPUT: {
2381 pa_sink_input *i = PA_SINK_INPUT(userdata);
2382
2383 /* If you change anything here, make sure to change the
2384 * sink input handling a few lines down at
2385 * PA_SINK_MESSAGE_FINISH_MOVE, too. */
2386
2387 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
2388
2389 /* Since the caller sleeps in pa_sink_input_put(), we can
2390 * safely access data outside of thread_info even though
2391 * it is mutable */
2392
2393 if ((i->thread_info.sync_prev = i->sync_prev)) {
2394 pa_assert(i->sink == i->thread_info.sync_prev->sink);
2395 pa_assert(i->sync_prev->sync_next == i);
2396 i->thread_info.sync_prev->thread_info.sync_next = i;
2397 }
2398
2399 if ((i->thread_info.sync_next = i->sync_next)) {
2400 pa_assert(i->sink == i->thread_info.sync_next->sink);
2401 pa_assert(i->sync_next->sync_prev == i);
2402 i->thread_info.sync_next->thread_info.sync_prev = i;
2403 }
2404
2405 pa_assert(!i->thread_info.attached);
2406 i->thread_info.attached = TRUE;
2407
2408 if (i->attach)
2409 i->attach(i);
2410
2411 pa_sink_input_set_state_within_thread(i, i->state);
2412
2413 /* The requested latency of the sink input needs to be
2414 * fixed up and then configured on the sink */
2415
2416 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1)
2417 pa_sink_input_set_requested_latency_within_thread(i, i->thread_info.requested_sink_latency);
2418
2419 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
2420 pa_sink_input_update_max_request(i, s->thread_info.max_request);
2421
2422 /* We don't rewind here automatically. This is left to the
2423 * sink input implementor because some sink inputs need a
2424 * slow start, i.e. need some time to buffer client
2425 * samples before beginning streaming. */
2426
2427 /* FIXME: Actually rewinding should be requested before
2428 * updating the sink requested latency, because updating
2429 * the requested latency updates also max_rewind of the
2430 * sink. Now consider this: a sink has a 10 s buffer and
2431 * nobody has requested anything less. Then a new stream
2432 * appears while the sink buffer is full. The new stream
2433 * requests e.g. 100 ms latency. That request is forwarded
2434 * to the sink, so now max_rewind is 100 ms. When a rewind
2435 * is requested, the sink will only rewind 100 ms, and the
2436 * new stream will have to wait about 10 seconds before it
2437 * becomes audible. */
2438
2439 /* In flat volume mode we need to update the volume as
2440 * well */
2441 return o->process_msg(o, PA_SINK_MESSAGE_SET_SHARED_VOLUME, NULL, 0, NULL);
2442 }
2443
2444 case PA_SINK_MESSAGE_REMOVE_INPUT: {
2445 pa_sink_input *i = PA_SINK_INPUT(userdata);
2446
2447 /* If you change anything here, make sure to change the
2448 * sink input handling a few lines down at
2449 * PA_SINK_MESSAGE_START_MOVE, too. */
2450
2451 if (i->detach)
2452 i->detach(i);
2453
2454 pa_sink_input_set_state_within_thread(i, i->state);
2455
2456 pa_assert(i->thread_info.attached);
2457 i->thread_info.attached = FALSE;
2458
2459 /* Since the caller sleeps in pa_sink_input_unlink(),
2460 * we can safely access data outside of thread_info even
2461 * though it is mutable */
2462
2463 pa_assert(!i->sync_prev);
2464 pa_assert(!i->sync_next);
2465
2466 if (i->thread_info.sync_prev) {
2467 i->thread_info.sync_prev->thread_info.sync_next = i->thread_info.sync_prev->sync_next;
2468 i->thread_info.sync_prev = NULL;
2469 }
2470
2471 if (i->thread_info.sync_next) {
2472 i->thread_info.sync_next->thread_info.sync_prev = i->thread_info.sync_next->sync_prev;
2473 i->thread_info.sync_next = NULL;
2474 }
2475
2476 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
2477 pa_sink_input_unref(i);
2478
2479 pa_sink_invalidate_requested_latency(s, TRUE);
2480 pa_sink_request_rewind(s, (size_t) -1);
2481
2482 /* In flat volume mode we need to update the volume as
2483 * well */
2484 return o->process_msg(o, PA_SINK_MESSAGE_SET_SHARED_VOLUME, NULL, 0, NULL);
2485 }
2486
2487 case PA_SINK_MESSAGE_START_MOVE: {
2488 pa_sink_input *i = PA_SINK_INPUT(userdata);
2489
2490 /* We don't support moving synchronized streams. */
2491 pa_assert(!i->sync_prev);
2492 pa_assert(!i->sync_next);
2493 pa_assert(!i->thread_info.sync_next);
2494 pa_assert(!i->thread_info.sync_prev);
2495
2496 if (i->thread_info.state != PA_SINK_INPUT_CORKED) {
2497 pa_usec_t usec = 0;
2498 size_t sink_nbytes, total_nbytes;
2499
2500 /* The old sink probably has some audio from this
2501 * stream in its buffer. We want to "take it back" as
2502 * much as possible and play it to the new sink. We
2503 * don't know at this point how much the old sink can
2504 * rewind. We have to pick something, and that
2505 * something is the full latency of the old sink here.
2506 * So we rewind the stream buffer by the sink latency
2507 * amount, which may be more than what we should
2508 * rewind. This can result in a chunk of audio being
2509 * played both to the old sink and the new sink.
2510 *
2511 * FIXME: Fix this code so that we don't have to make
2512 * guesses about how much the sink will actually be
2513 * able to rewind. If someone comes up with a solution
2514 * for this, something to note is that the part of the
2515 * latency that the old sink couldn't rewind should
2516 * ideally be compensated after the stream has moved
2517 * to the new sink by adding silence. The new sink
2518 * most likely can't start playing the moved stream
2519 * immediately, and that gap should be removed from
2520 * the "compensation silence" (at least at the time of
2521 * writing this, the move finish code will actually
2522 * already take care of dropping the new sink's
2523 * unrewindable latency, so taking into account the
2524 * unrewindable latency of the old sink is the only
2525 * problem).
2526 *
2527 * The render_memblockq contents are discarded,
2528 * because when the sink changes, the format of the
2529 * audio stored in the render_memblockq may change
2530 * too, making the stored audio invalid. FIXME:
2531 * However, the read and write indices are moved back
2532 * the same amount, so if they are not the same now,
2533 * they won't be the same after the rewind either. If
2534 * the write index of the render_memblockq is ahead of
2535 * the read index, then the render_memblockq will feed
2536 * the new sink some silence first, which it shouldn't
2537 * do. The write index should be flushed to be the
2538 * same as the read index. */
2539
2540 /* Get the latency of the sink */
2541 usec = pa_sink_get_latency_within_thread(s);
2542 sink_nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
2543 total_nbytes = sink_nbytes + pa_memblockq_get_length(i->thread_info.render_memblockq);
2544
2545 if (total_nbytes > 0) {
2546 i->thread_info.rewrite_nbytes = i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, total_nbytes) : total_nbytes;
2547 i->thread_info.rewrite_flush = TRUE;
2548 pa_sink_input_process_rewind(i, sink_nbytes);
2549 }
2550 }
2551
2552 if (i->detach)
2553 i->detach(i);
2554
2555 pa_assert(i->thread_info.attached);
2556 i->thread_info.attached = FALSE;
2557
2558 /* Let's remove the sink input ...*/
2559 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
2560 pa_sink_input_unref(i);
2561
2562 pa_sink_invalidate_requested_latency(s, TRUE);
2563
2564 pa_log_debug("Requesting rewind due to started move");
2565 pa_sink_request_rewind(s, (size_t) -1);
2566
2567 /* In flat volume mode we need to update the volume as
2568 * well */
2569 return o->process_msg(o, PA_SINK_MESSAGE_SET_SHARED_VOLUME, NULL, 0, NULL);
2570 }
2571
2572 case PA_SINK_MESSAGE_FINISH_MOVE: {
2573 pa_sink_input *i = PA_SINK_INPUT(userdata);
2574
2575 /* We don't support moving synchronized streams. */
2576 pa_assert(!i->sync_prev);
2577 pa_assert(!i->sync_next);
2578 pa_assert(!i->thread_info.sync_next);
2579 pa_assert(!i->thread_info.sync_prev);
2580
2581 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
2582
2583 pa_assert(!i->thread_info.attached);
2584 i->thread_info.attached = TRUE;
2585
2586 if (i->attach)
2587 i->attach(i);
2588
2589 if (i->thread_info.state != PA_SINK_INPUT_CORKED) {
2590 pa_usec_t usec = 0;
2591 size_t nbytes;
2592
2593 /* In the ideal case the new sink would start playing
2594 * the stream immediately. That requires the sink to
2595 * be able to rewind all of its latency, which usually
2596 * isn't possible, so there will probably be some gap
2597 * before the moved stream becomes audible. We then
2598 * have two possibilities: 1) start playing the stream
2599 * from where it is now, or 2) drop the unrewindable
2600 * latency of the sink from the stream. With option 1
2601 * we won't lose any audio but the stream will have a
2602 * pause. With option 2 we may lose some audio but the
2603 * stream time will be somewhat in sync with the wall
2604 * clock. Lennart seems to have chosen option 2 (one
2605 * of the reasons might have been that option 1 is
2606 * actually much harder to implement), so we drop the
2607 * latency of the new sink from the moved stream and
2608 * hope that the sink will undo most of that in the
2609 * rewind. */
2610
2611 /* Get the latency of the sink */
2612 usec = pa_sink_get_latency_within_thread(s);
2613 nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
2614
2615 if (nbytes > 0)
2616 pa_sink_input_drop(i, nbytes);
2617
2618 pa_log_debug("Requesting rewind due to finished move");
2619 pa_sink_request_rewind(s, nbytes);
2620 }
2621
2622 /* Updating the requested sink latency has to be done
2623 * after the sink rewind request, not before, because
2624 * otherwise the sink may limit the rewind amount
2625 * needlessly. */
2626
2627 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1)
2628 pa_sink_input_set_requested_latency_within_thread(i, i->thread_info.requested_sink_latency);
2629
2630 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
2631 pa_sink_input_update_max_request(i, s->thread_info.max_request);
2632
2633 return o->process_msg(o, PA_SINK_MESSAGE_SET_SHARED_VOLUME, NULL, 0, NULL);
2634 }
2635
2636 case PA_SINK_MESSAGE_SET_SHARED_VOLUME: {
2637 pa_sink *root_sink = pa_sink_get_master(s);
2638
2639 if (PA_LIKELY(root_sink))
2640 set_shared_volume_within_thread(root_sink);
2641
2642 return 0;
2643 }
2644
2645 case PA_SINK_MESSAGE_SET_VOLUME_SYNCED:
2646
2647 if (s->flags & PA_SINK_DEFERRED_VOLUME) {
2648 s->set_volume(s);
2649 pa_sink_volume_change_push(s);
2650 }
2651 /* Fall through ... */
2652
2653 case PA_SINK_MESSAGE_SET_VOLUME:
2654
2655 if (!pa_cvolume_equal(&s->thread_info.soft_volume, &s->soft_volume)) {
2656 s->thread_info.soft_volume = s->soft_volume;
2657 pa_sink_request_rewind(s, (size_t) -1);
2658 }
2659
2660 /* Fall through ... */
2661
2662 case PA_SINK_MESSAGE_SYNC_VOLUMES:
2663 sync_input_volumes_within_thread(s);
2664 return 0;
2665
2666 case PA_SINK_MESSAGE_GET_VOLUME:
2667
2668 if ((s->flags & PA_SINK_DEFERRED_VOLUME) && s->get_volume) {
2669 s->get_volume(s);
2670 pa_sink_volume_change_flush(s);
2671 pa_sw_cvolume_divide(&s->thread_info.current_hw_volume, &s->real_volume, &s->soft_volume);
2672 }
2673
2674 /* In case sink implementor reset SW volume. */
2675 if (!pa_cvolume_equal(&s->thread_info.soft_volume, &s->soft_volume)) {
2676 s->thread_info.soft_volume = s->soft_volume;
2677 pa_sink_request_rewind(s, (size_t) -1);
2678 }
2679
2680 return 0;
2681
2682 case PA_SINK_MESSAGE_SET_MUTE:
2683
2684 if (s->thread_info.soft_muted != s->muted) {
2685 s->thread_info.soft_muted = s->muted;
2686 pa_sink_request_rewind(s, (size_t) -1);
2687 }
2688
2689 if (s->flags & PA_SINK_DEFERRED_VOLUME && s->set_mute)
2690 s->set_mute(s);
2691
2692 return 0;
2693
2694 case PA_SINK_MESSAGE_GET_MUTE:
2695
2696 if (s->flags & PA_SINK_DEFERRED_VOLUME && s->get_mute)
2697 s->get_mute(s);
2698
2699 return 0;
2700
2701 case PA_SINK_MESSAGE_SET_STATE: {
2702
2703 pa_bool_t suspend_change =
2704 (s->thread_info.state == PA_SINK_SUSPENDED && PA_SINK_IS_OPENED(PA_PTR_TO_UINT(userdata))) ||
2705 (PA_SINK_IS_OPENED(s->thread_info.state) && PA_PTR_TO_UINT(userdata) == PA_SINK_SUSPENDED);
2706
2707 s->thread_info.state = PA_PTR_TO_UINT(userdata);
2708
2709 if (s->thread_info.state == PA_SINK_SUSPENDED) {
2710 s->thread_info.rewind_nbytes = 0;
2711 s->thread_info.rewind_requested = FALSE;
2712 }
2713
2714 if (suspend_change) {
2715 pa_sink_input *i;
2716 void *state = NULL;
2717
2718 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
2719 if (i->suspend_within_thread)
2720 i->suspend_within_thread(i, s->thread_info.state == PA_SINK_SUSPENDED);
2721 }
2722
2723 return 0;
2724 }
2725
2726 case PA_SINK_MESSAGE_DETACH:
2727
2728 /* Detach all streams */
2729 pa_sink_detach_within_thread(s);
2730 return 0;
2731
2732 case PA_SINK_MESSAGE_ATTACH:
2733
2734 /* Reattach all streams */
2735 pa_sink_attach_within_thread(s);
2736 return 0;
2737
2738 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY: {
2739
2740 pa_usec_t *usec = userdata;
2741 *usec = pa_sink_get_requested_latency_within_thread(s);
2742
2743 /* Yes, that's right, the IO thread will see -1 when no
2744 * explicit requested latency is configured, the main
2745 * thread will see max_latency */
2746 if (*usec == (pa_usec_t) -1)
2747 *usec = s->thread_info.max_latency;
2748
2749 return 0;
2750 }
2751
2752 case PA_SINK_MESSAGE_SET_LATENCY_RANGE: {
2753 pa_usec_t *r = userdata;
2754
2755 pa_sink_set_latency_range_within_thread(s, r[0], r[1]);
2756
2757 return 0;
2758 }
2759
2760 case PA_SINK_MESSAGE_GET_LATENCY_RANGE: {
2761 pa_usec_t *r = userdata;
2762
2763 r[0] = s->thread_info.min_latency;
2764 r[1] = s->thread_info.max_latency;
2765
2766 return 0;
2767 }
2768
2769 case PA_SINK_MESSAGE_GET_FIXED_LATENCY:
2770
2771 *((pa_usec_t*) userdata) = s->thread_info.fixed_latency;
2772 return 0;
2773
2774 case PA_SINK_MESSAGE_SET_FIXED_LATENCY:
2775
2776 pa_sink_set_fixed_latency_within_thread(s, (pa_usec_t) offset);
2777 return 0;
2778
2779 case PA_SINK_MESSAGE_GET_MAX_REWIND:
2780
2781 *((size_t*) userdata) = s->thread_info.max_rewind;
2782 return 0;
2783
2784 case PA_SINK_MESSAGE_GET_MAX_REQUEST:
2785
2786 *((size_t*) userdata) = s->thread_info.max_request;
2787 return 0;
2788
2789 case PA_SINK_MESSAGE_SET_MAX_REWIND:
2790
2791 pa_sink_set_max_rewind_within_thread(s, (size_t) offset);
2792 return 0;
2793
2794 case PA_SINK_MESSAGE_SET_MAX_REQUEST:
2795
2796 pa_sink_set_max_request_within_thread(s, (size_t) offset);
2797 return 0;
2798
2799 case PA_SINK_MESSAGE_SET_PORT:
2800
2801 pa_assert(userdata);
2802 if (s->set_port) {
2803 struct sink_message_set_port *msg_data = userdata;
2804 msg_data->ret = s->set_port(s, msg_data->port);
2805 }
2806 return 0;
2807
2808 case PA_SINK_MESSAGE_UPDATE_VOLUME_AND_MUTE:
2809 /* This message is sent from IO-thread and handled in main thread. */
2810 pa_assert_ctl_context();
2811
2812 /* Make sure we're not messing with main thread when no longer linked */
2813 if (!PA_SINK_IS_LINKED(s->state))
2814 return 0;
2815
2816 pa_sink_get_volume(s, TRUE);
2817 pa_sink_get_mute(s, TRUE);
2818 return 0;
2819
2820 case PA_SINK_MESSAGE_GET_LATENCY:
2821 case PA_SINK_MESSAGE_MAX:
2822 ;
2823 }
2824
2825 return -1;
2826 }
2827
2828 /* Called from main thread */
2829 int pa_sink_suspend_all(pa_core *c, pa_bool_t suspend, pa_suspend_cause_t cause) {
2830 pa_sink *sink;
2831 uint32_t idx;
2832 int ret = 0;
2833
2834 pa_core_assert_ref(c);
2835 pa_assert_ctl_context();
2836 pa_assert(cause != 0);
2837
2838 PA_IDXSET_FOREACH(sink, c->sinks, idx) {
2839 int r;
2840
2841 if ((r = pa_sink_suspend(sink, suspend, cause)) < 0)
2842 ret = r;
2843 }
2844
2845 return ret;
2846 }
2847
2848 /* Called from main thread */
2849 void pa_sink_detach(pa_sink *s) {
2850 pa_sink_assert_ref(s);
2851 pa_assert_ctl_context();
2852 pa_assert(PA_SINK_IS_LINKED(s->state));
2853
2854 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_DETACH, NULL, 0, NULL) == 0);
2855 }
2856
2857 /* Called from main thread */
2858 void pa_sink_attach(pa_sink *s) {
2859 pa_sink_assert_ref(s);
2860 pa_assert_ctl_context();
2861 pa_assert(PA_SINK_IS_LINKED(s->state));
2862
2863 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_ATTACH, NULL, 0, NULL) == 0);
2864 }
2865
2866 /* Called from IO thread */
2867 void pa_sink_detach_within_thread(pa_sink *s) {
2868 pa_sink_input *i;
2869 void *state = NULL;
2870
2871 pa_sink_assert_ref(s);
2872 pa_sink_assert_io_context(s);
2873 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
2874
2875 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
2876 if (i->detach)
2877 i->detach(i);
2878
2879 if (s->monitor_source)
2880 pa_source_detach_within_thread(s->monitor_source);
2881 }
2882
2883 /* Called from IO thread */
2884 void pa_sink_attach_within_thread(pa_sink *s) {
2885 pa_sink_input *i;
2886 void *state = NULL;
2887
2888 pa_sink_assert_ref(s);
2889 pa_sink_assert_io_context(s);
2890 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
2891
2892 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
2893 if (i->attach)
2894 i->attach(i);
2895
2896 if (s->monitor_source)
2897 pa_source_attach_within_thread(s->monitor_source);
2898 }
2899
2900 /* Called from IO thread */
2901 void pa_sink_request_rewind(pa_sink*s, size_t nbytes) {
2902 pa_sink_assert_ref(s);
2903 pa_sink_assert_io_context(s);
2904 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
2905
2906 if (s->thread_info.state == PA_SINK_SUSPENDED)
2907 return;
2908
2909 if (nbytes == (size_t) -1)
2910 nbytes = s->thread_info.max_rewind;
2911
2912 nbytes = PA_MIN(nbytes, s->thread_info.max_rewind);
2913
2914 if (s->thread_info.rewind_requested &&
2915 nbytes <= s->thread_info.rewind_nbytes)
2916 return;
2917
2918 s->thread_info.rewind_nbytes = nbytes;
2919 s->thread_info.rewind_requested = TRUE;
2920
2921 if (s->request_rewind)
2922 s->request_rewind(s);
2923 }
2924
2925 /* Called from IO thread */
2926 pa_usec_t pa_sink_get_requested_latency_within_thread(pa_sink *s) {
2927 pa_usec_t result = (pa_usec_t) -1;
2928 pa_sink_input *i;
2929 void *state = NULL;
2930 pa_usec_t monitor_latency;
2931
2932 pa_sink_assert_ref(s);
2933 pa_sink_assert_io_context(s);
2934
2935 if (!(s->flags & PA_SINK_DYNAMIC_LATENCY))
2936 return PA_CLAMP(s->thread_info.fixed_latency, s->thread_info.min_latency, s->thread_info.max_latency);
2937
2938 if (s->thread_info.requested_latency_valid)
2939 return s->thread_info.requested_latency;
2940
2941 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
2942 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1 &&
2943 (result == (pa_usec_t) -1 || result > i->thread_info.requested_sink_latency))
2944 result = i->thread_info.requested_sink_latency;
2945
2946 monitor_latency = pa_source_get_requested_latency_within_thread(s->monitor_source);
2947
2948 if (monitor_latency != (pa_usec_t) -1 &&
2949 (result == (pa_usec_t) -1 || result > monitor_latency))
2950 result = monitor_latency;
2951
2952 if (result != (pa_usec_t) -1)
2953 result = PA_CLAMP(result, s->thread_info.min_latency, s->thread_info.max_latency);
2954
2955 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
2956 /* Only cache if properly initialized */
2957 s->thread_info.requested_latency = result;
2958 s->thread_info.requested_latency_valid = TRUE;
2959 }
2960
2961 return result;
2962 }
2963
2964 /* Called from main thread */
2965 pa_usec_t pa_sink_get_requested_latency(pa_sink *s) {
2966 pa_usec_t usec = 0;
2967
2968 pa_sink_assert_ref(s);
2969 pa_assert_ctl_context();
2970 pa_assert(PA_SINK_IS_LINKED(s->state));
2971
2972 if (s->state == PA_SINK_SUSPENDED)
2973 return 0;
2974
2975 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
2976
2977 return usec;
2978 }
2979
2980 /* Called from IO as well as the main thread -- the latter only before the IO thread started up */
2981 void pa_sink_set_max_rewind_within_thread(pa_sink *s, size_t max_rewind) {
2982 pa_sink_input *i;
2983 void *state = NULL;
2984
2985 pa_sink_assert_ref(s);
2986 pa_sink_assert_io_context(s);
2987
2988 if (max_rewind == s->thread_info.max_rewind)
2989 return;
2990
2991 s->thread_info.max_rewind = max_rewind;
2992
2993 if (PA_SINK_IS_LINKED(s->thread_info.state))
2994 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
2995 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
2996
2997 if (s->monitor_source)
2998 pa_source_set_max_rewind_within_thread(s->monitor_source, s->thread_info.max_rewind);
2999 }
3000
3001 /* Called from main thread */
3002 void pa_sink_set_max_rewind(pa_sink *s, size_t max_rewind) {
3003 pa_sink_assert_ref(s);
3004 pa_assert_ctl_context();
3005
3006 if (PA_SINK_IS_LINKED(s->state))
3007 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MAX_REWIND, NULL, max_rewind, NULL) == 0);
3008 else
3009 pa_sink_set_max_rewind_within_thread(s, max_rewind);
3010 }
3011
3012 /* Called from IO as well as the main thread -- the latter only before the IO thread started up */
3013 void pa_sink_set_max_request_within_thread(pa_sink *s, size_t max_request) {
3014 void *state = NULL;
3015
3016 pa_sink_assert_ref(s);
3017 pa_sink_assert_io_context(s);
3018
3019 if (max_request == s->thread_info.max_request)
3020 return;
3021
3022 s->thread_info.max_request = max_request;
3023
3024 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
3025 pa_sink_input *i;
3026
3027 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
3028 pa_sink_input_update_max_request(i, s->thread_info.max_request);
3029 }
3030 }
3031
3032 /* Called from main thread */
3033 void pa_sink_set_max_request(pa_sink *s, size_t max_request) {
3034 pa_sink_assert_ref(s);
3035 pa_assert_ctl_context();
3036
3037 if (PA_SINK_IS_LINKED(s->state))
3038 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MAX_REQUEST, NULL, max_request, NULL) == 0);
3039 else
3040 pa_sink_set_max_request_within_thread(s, max_request);
3041 }
3042
3043 /* Called from IO thread */
3044 void pa_sink_invalidate_requested_latency(pa_sink *s, pa_bool_t dynamic) {
3045 pa_sink_input *i;
3046 void *state = NULL;
3047
3048 pa_sink_assert_ref(s);
3049 pa_sink_assert_io_context(s);
3050
3051 if ((s->flags & PA_SINK_DYNAMIC_LATENCY))
3052 s->thread_info.requested_latency_valid = FALSE;
3053 else if (dynamic)
3054 return;
3055
3056 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
3057
3058 if (s->update_requested_latency)
3059 s->update_requested_latency(s);
3060
3061 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
3062 if (i->update_sink_requested_latency)
3063 i->update_sink_requested_latency(i);
3064 }
3065 }
3066
3067 /* Called from main thread */
3068 void pa_sink_set_latency_range(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency) {
3069 pa_sink_assert_ref(s);
3070 pa_assert_ctl_context();
3071
3072 /* min_latency == 0: no limit
3073 * min_latency anything else: specified limit
3074 *
3075 * Similar for max_latency */
3076
3077 if (min_latency < ABSOLUTE_MIN_LATENCY)
3078 min_latency = ABSOLUTE_MIN_LATENCY;
3079
3080 if (max_latency <= 0 ||
3081 max_latency > ABSOLUTE_MAX_LATENCY)
3082 max_latency = ABSOLUTE_MAX_LATENCY;
3083
3084 pa_assert(min_latency <= max_latency);
3085
3086 /* Hmm, let's see if someone forgot to set PA_SINK_DYNAMIC_LATENCY here... */
3087 pa_assert((min_latency == ABSOLUTE_MIN_LATENCY &&
3088 max_latency == ABSOLUTE_MAX_LATENCY) ||
3089 (s->flags & PA_SINK_DYNAMIC_LATENCY));
3090
3091 if (PA_SINK_IS_LINKED(s->state)) {
3092 pa_usec_t r[2];
3093
3094 r[0] = min_latency;
3095 r[1] = max_latency;
3096
3097 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_LATENCY_RANGE, r, 0, NULL) == 0);
3098 } else
3099 pa_sink_set_latency_range_within_thread(s, min_latency, max_latency);
3100 }
3101
3102 /* Called from main thread */
3103 void pa_sink_get_latency_range(pa_sink *s, pa_usec_t *min_latency, pa_usec_t *max_latency) {
3104 pa_sink_assert_ref(s);
3105 pa_assert_ctl_context();
3106 pa_assert(min_latency);
3107 pa_assert(max_latency);
3108
3109 if (PA_SINK_IS_LINKED(s->state)) {
3110 pa_usec_t r[2] = { 0, 0 };
3111
3112 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY_RANGE, r, 0, NULL) == 0);
3113
3114 *min_latency = r[0];
3115 *max_latency = r[1];
3116 } else {
3117 *min_latency = s->thread_info.min_latency;
3118 *max_latency = s->thread_info.max_latency;
3119 }
3120 }
3121
3122 /* Called from IO thread */
3123 void pa_sink_set_latency_range_within_thread(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency) {
3124 pa_sink_assert_ref(s);
3125 pa_sink_assert_io_context(s);
3126
3127 pa_assert(min_latency >= ABSOLUTE_MIN_LATENCY);
3128 pa_assert(max_latency <= ABSOLUTE_MAX_LATENCY);
3129 pa_assert(min_latency <= max_latency);
3130
3131 /* Hmm, let's see if someone forgot to set PA_SINK_DYNAMIC_LATENCY here... */
3132 pa_assert((min_latency == ABSOLUTE_MIN_LATENCY &&
3133 max_latency == ABSOLUTE_MAX_LATENCY) ||
3134 (s->flags & PA_SINK_DYNAMIC_LATENCY));
3135
3136 if (s->thread_info.min_latency == min_latency &&
3137 s->thread_info.max_latency == max_latency)
3138 return;
3139
3140 s->thread_info.min_latency = min_latency;
3141 s->thread_info.max_latency = max_latency;
3142
3143 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
3144 pa_sink_input *i;
3145 void *state = NULL;
3146
3147 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
3148 if (i->update_sink_latency_range)
3149 i->update_sink_latency_range(i);
3150 }
3151
3152 pa_sink_invalidate_requested_latency(s, FALSE);
3153
3154 pa_source_set_latency_range_within_thread(s->monitor_source, min_latency, max_latency);
3155 }
3156
3157 /* Called from main thread */
3158 void pa_sink_set_fixed_latency(pa_sink *s, pa_usec_t latency) {
3159 pa_sink_assert_ref(s);
3160 pa_assert_ctl_context();
3161
3162 if (s->flags & PA_SINK_DYNAMIC_LATENCY) {
3163 pa_assert(latency == 0);
3164 return;
3165 }
3166
3167 if (latency < ABSOLUTE_MIN_LATENCY)
3168 latency = ABSOLUTE_MIN_LATENCY;
3169
3170 if (latency > ABSOLUTE_MAX_LATENCY)
3171 latency = ABSOLUTE_MAX_LATENCY;
3172
3173 if (PA_SINK_IS_LINKED(s->state))
3174 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_FIXED_LATENCY, NULL, (int64_t) latency, NULL) == 0);
3175 else
3176 s->thread_info.fixed_latency = latency;
3177
3178 pa_source_set_fixed_latency(s->monitor_source, latency);
3179 }
3180
3181 /* Called from main thread */
3182 pa_usec_t pa_sink_get_fixed_latency(pa_sink *s) {
3183 pa_usec_t latency;
3184
3185 pa_sink_assert_ref(s);
3186 pa_assert_ctl_context();
3187
3188 if (s->flags & PA_SINK_DYNAMIC_LATENCY)
3189 return 0;
3190
3191 if (PA_SINK_IS_LINKED(s->state))
3192 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_FIXED_LATENCY, &latency, 0, NULL) == 0);
3193 else
3194 latency = s->thread_info.fixed_latency;
3195
3196 return latency;
3197 }
3198
3199 /* Called from IO thread */
3200 void pa_sink_set_fixed_latency_within_thread(pa_sink *s, pa_usec_t latency) {
3201 pa_sink_assert_ref(s);
3202 pa_sink_assert_io_context(s);
3203
3204 if (s->flags & PA_SINK_DYNAMIC_LATENCY) {
3205 pa_assert(latency == 0);
3206 return;
3207 }
3208
3209 pa_assert(latency >= ABSOLUTE_MIN_LATENCY);
3210 pa_assert(latency <= ABSOLUTE_MAX_LATENCY);
3211
3212 if (s->thread_info.fixed_latency == latency)
3213 return;
3214
3215 s->thread_info.fixed_latency = latency;
3216
3217 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
3218 pa_sink_input *i;
3219 void *state = NULL;
3220
3221 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
3222 if (i->update_sink_fixed_latency)
3223 i->update_sink_fixed_latency(i);
3224 }
3225
3226 pa_sink_invalidate_requested_latency(s, FALSE);
3227
3228 pa_source_set_fixed_latency_within_thread(s->monitor_source, latency);
3229 }
3230
3231 /* Called from main context */
3232 size_t pa_sink_get_max_rewind(pa_sink *s) {
3233 size_t r;
3234 pa_assert_ctl_context();
3235 pa_sink_assert_ref(s);
3236
3237 if (!PA_SINK_IS_LINKED(s->state))
3238 return s->thread_info.max_rewind;
3239
3240 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MAX_REWIND, &r, 0, NULL) == 0);
3241
3242 return r;
3243 }
3244
3245 /* Called from main context */
3246 size_t pa_sink_get_max_request(pa_sink *s) {
3247 size_t r;
3248 pa_sink_assert_ref(s);
3249 pa_assert_ctl_context();
3250
3251 if (!PA_SINK_IS_LINKED(s->state))
3252 return s->thread_info.max_request;
3253
3254 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MAX_REQUEST, &r, 0, NULL) == 0);
3255
3256 return r;
3257 }
3258
3259 /* Called from main context */
3260 int pa_sink_set_port(pa_sink *s, const char *name, pa_bool_t save) {
3261 pa_device_port *port;
3262 int ret;
3263
3264 pa_sink_assert_ref(s);
3265 pa_assert_ctl_context();
3266
3267 if (!s->set_port) {
3268 pa_log_debug("set_port() operation not implemented for sink %u \"%s\"", s->index, s->name);
3269 return -PA_ERR_NOTIMPLEMENTED;
3270 }
3271
3272 if (!s->ports)
3273 return -PA_ERR_NOENTITY;
3274
3275 if (!(port = pa_hashmap_get(s->ports, name)))
3276 return -PA_ERR_NOENTITY;
3277
3278 if (s->active_port == port) {
3279 s->save_port = s->save_port || save;
3280 return 0;
3281 }
3282
3283 if (s->flags & PA_SINK_DEFERRED_VOLUME) {
3284 struct sink_message_set_port msg = { .port = port, .ret = 0 };
3285 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_PORT, &msg, 0, NULL) == 0);
3286 ret = msg.ret;
3287 }
3288 else
3289 ret = s->set_port(s, port);
3290
3291 if (ret < 0)
3292 return -PA_ERR_NOENTITY;
3293
3294 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
3295
3296 pa_log_info("Changed port of sink %u \"%s\" to %s", s->index, s->name, port->name);
3297
3298 s->active_port = port;
3299 s->save_port = save;
3300
3301 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PORT_CHANGED], s);
3302
3303 return 0;
3304 }
3305
3306 pa_bool_t pa_device_init_icon(pa_proplist *p, pa_bool_t is_sink) {
3307 const char *ff, *c, *t = NULL, *s = "", *profile, *bus;
3308
3309 pa_assert(p);
3310
3311 if (pa_proplist_contains(p, PA_PROP_DEVICE_ICON_NAME))
3312 return TRUE;
3313
3314 if ((ff = pa_proplist_gets(p, PA_PROP_DEVICE_FORM_FACTOR))) {
3315
3316 if (pa_streq(ff, "microphone"))
3317 t = "audio-input-microphone";
3318 else if (pa_streq(ff, "webcam"))
3319 t = "camera-web";
3320 else if (pa_streq(ff, "computer"))
3321 t = "computer";
3322 else if (pa_streq(ff, "handset"))
3323 t = "phone";
3324 else if (pa_streq(ff, "portable"))
3325 t = "multimedia-player";
3326 else if (pa_streq(ff, "tv"))
3327 t = "video-display";
3328
3329 /*
3330 * The following icons are not part of the icon naming spec,
3331 * because Rodney Dawes sucks as the maintainer of that spec.
3332 *
3333 * http://lists.freedesktop.org/archives/xdg/2009-May/010397.html
3334 */
3335 else if (pa_streq(ff, "headset"))
3336 t = "audio-headset";
3337 else if (pa_streq(ff, "headphone"))
3338 t = "audio-headphones";
3339 else if (pa_streq(ff, "speaker"))
3340 t = "audio-speakers";
3341 else if (pa_streq(ff, "hands-free"))
3342 t = "audio-handsfree";
3343 }
3344
3345 if (!t)
3346 if ((c = pa_proplist_gets(p, PA_PROP_DEVICE_CLASS)))
3347 if (pa_streq(c, "modem"))
3348 t = "modem";
3349
3350 if (!t) {
3351 if (is_sink)
3352 t = "audio-card";
3353 else
3354 t = "audio-input-microphone";
3355 }
3356
3357 if ((profile = pa_proplist_gets(p, PA_PROP_DEVICE_PROFILE_NAME))) {
3358 if (strstr(profile, "analog"))
3359 s = "-analog";
3360 else if (strstr(profile, "iec958"))
3361 s = "-iec958";
3362 else if (strstr(profile, "hdmi"))
3363 s = "-hdmi";
3364 }
3365
3366 bus = pa_proplist_gets(p, PA_PROP_DEVICE_BUS);
3367
3368 pa_proplist_setf(p, PA_PROP_DEVICE_ICON_NAME, "%s%s%s%s", t, pa_strempty(s), bus ? "-" : "", pa_strempty(bus));
3369
3370 return TRUE;
3371 }
3372
3373 pa_bool_t pa_device_init_description(pa_proplist *p) {
3374 const char *s, *d = NULL, *k;
3375 pa_assert(p);
3376
3377 if (pa_proplist_contains(p, PA_PROP_DEVICE_DESCRIPTION))
3378 return TRUE;
3379
3380 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_FORM_FACTOR)))
3381 if (pa_streq(s, "internal"))
3382 d = _("Internal Audio");
3383
3384 if (!d)
3385 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_CLASS)))
3386 if (pa_streq(s, "modem"))
3387 d = _("Modem");
3388
3389 if (!d)
3390 d = pa_proplist_gets(p, PA_PROP_DEVICE_PRODUCT_NAME);
3391
3392 if (!d)
3393 return FALSE;
3394
3395 k = pa_proplist_gets(p, PA_PROP_DEVICE_PROFILE_DESCRIPTION);
3396
3397 if (d && k)
3398 pa_proplist_setf(p, PA_PROP_DEVICE_DESCRIPTION, _("%s %s"), d, k);
3399 else if (d)
3400 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, d);
3401
3402 return TRUE;
3403 }
3404
3405 pa_bool_t pa_device_init_intended_roles(pa_proplist *p) {
3406 const char *s;
3407 pa_assert(p);
3408
3409 if (pa_proplist_contains(p, PA_PROP_DEVICE_INTENDED_ROLES))
3410 return TRUE;
3411
3412 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_FORM_FACTOR)))
3413 if (pa_streq(s, "handset") || pa_streq(s, "hands-free")
3414 || pa_streq(s, "headset")) {
3415 pa_proplist_sets(p, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
3416 return TRUE;
3417 }
3418
3419 return FALSE;
3420 }
3421
3422 unsigned pa_device_init_priority(pa_proplist *p) {
3423 const char *s;
3424 unsigned priority = 0;
3425
3426 pa_assert(p);
3427
3428 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_CLASS))) {
3429
3430 if (pa_streq(s, "sound"))
3431 priority += 9000;
3432 else if (!pa_streq(s, "modem"))
3433 priority += 1000;
3434 }
3435
3436 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_FORM_FACTOR))) {
3437
3438 if (pa_streq(s, "internal"))
3439 priority += 900;
3440 else if (pa_streq(s, "speaker"))
3441 priority += 500;
3442 else if (pa_streq(s, "headphone"))
3443 priority += 400;
3444 }
3445
3446 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_BUS))) {
3447
3448 if (pa_streq(s, "pci"))
3449 priority += 50;
3450 else if (pa_streq(s, "usb"))
3451 priority += 40;
3452 else if (pa_streq(s, "bluetooth"))
3453 priority += 30;
3454 }
3455
3456 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_PROFILE_NAME))) {
3457
3458 if (pa_startswith(s, "analog-"))
3459 priority += 9;
3460 else if (pa_startswith(s, "iec958-"))
3461 priority += 8;
3462 }
3463
3464 return priority;
3465 }
3466
3467 PA_STATIC_FLIST_DECLARE(pa_sink_volume_change, 0, pa_xfree);
3468
3469 /* Called from the IO thread. */
3470 static pa_sink_volume_change *pa_sink_volume_change_new(pa_sink *s) {
3471 pa_sink_volume_change *c;
3472 if (!(c = pa_flist_pop(PA_STATIC_FLIST_GET(pa_sink_volume_change))))
3473 c = pa_xnew(pa_sink_volume_change, 1);
3474
3475 PA_LLIST_INIT(pa_sink_volume_change, c);
3476 c->at = 0;
3477 pa_cvolume_reset(&c->hw_volume, s->sample_spec.channels);
3478 return c;
3479 }
3480
3481 /* Called from the IO thread. */
3482 static void pa_sink_volume_change_free(pa_sink_volume_change *c) {
3483 pa_assert(c);
3484 if (pa_flist_push(PA_STATIC_FLIST_GET(pa_sink_volume_change), c) < 0)
3485 pa_xfree(c);
3486 }
3487
3488 /* Called from the IO thread. */
3489 void pa_sink_volume_change_push(pa_sink *s) {
3490 pa_sink_volume_change *c = NULL;
3491 pa_sink_volume_change *nc = NULL;
3492 uint32_t safety_margin = s->thread_info.volume_change_safety_margin;
3493
3494 const char *direction = NULL;
3495
3496 pa_assert(s);
3497 nc = pa_sink_volume_change_new(s);
3498
3499 /* NOTE: There is already more different volumes in pa_sink that I can remember.
3500 * Adding one more volume for HW would get us rid of this, but I am trying
3501 * to survive with the ones we already have. */
3502 pa_sw_cvolume_divide(&nc->hw_volume, &s->real_volume, &s->soft_volume);
3503
3504 if (!s->thread_info.volume_changes && pa_cvolume_equal(&nc->hw_volume, &s->thread_info.current_hw_volume)) {
3505 pa_log_debug("Volume not changing");
3506 pa_sink_volume_change_free(nc);
3507 return;
3508 }
3509
3510 nc->at = pa_sink_get_latency_within_thread(s);
3511 nc->at += pa_rtclock_now() + s->thread_info.volume_change_extra_delay;
3512
3513 if (s->thread_info.volume_changes_tail) {
3514 for (c = s->thread_info.volume_changes_tail; c; c = c->prev) {
3515 /* If volume is going up let's do it a bit late. If it is going
3516 * down let's do it a bit early. */
3517 if (pa_cvolume_avg(&nc->hw_volume) > pa_cvolume_avg(&c->hw_volume)) {
3518 if (nc->at + safety_margin > c->at) {
3519 nc->at += safety_margin;
3520 direction = "up";
3521 break;
3522 }
3523 }
3524 else if (nc->at - safety_margin > c->at) {
3525 nc->at -= safety_margin;
3526 direction = "down";
3527 break;
3528 }
3529 }
3530 }
3531
3532 if (c == NULL) {
3533 if (pa_cvolume_avg(&nc->hw_volume) > pa_cvolume_avg(&s->thread_info.current_hw_volume)) {
3534 nc->at += safety_margin;
3535 direction = "up";
3536 } else {
3537 nc->at -= safety_margin;
3538 direction = "down";
3539 }
3540 PA_LLIST_PREPEND(pa_sink_volume_change, s->thread_info.volume_changes, nc);
3541 }
3542 else {
3543 PA_LLIST_INSERT_AFTER(pa_sink_volume_change, s->thread_info.volume_changes, c, nc);
3544 }
3545
3546 pa_log_debug("Volume going %s to %d at %llu", direction, pa_cvolume_avg(&nc->hw_volume), (long long unsigned) nc->at);
3547
3548 /* We can ignore volume events that came earlier but should happen later than this. */
3549 PA_LLIST_FOREACH(c, nc->next) {
3550 pa_log_debug("Volume change to %d at %llu was dropped", pa_cvolume_avg(&c->hw_volume), (long long unsigned) c->at);
3551 pa_sink_volume_change_free(c);
3552 }
3553 nc->next = NULL;
3554 s->thread_info.volume_changes_tail = nc;
3555 }
3556
3557 /* Called from the IO thread. */
3558 static void pa_sink_volume_change_flush(pa_sink *s) {
3559 pa_sink_volume_change *c = s->thread_info.volume_changes;
3560 pa_assert(s);
3561 s->thread_info.volume_changes = NULL;
3562 s->thread_info.volume_changes_tail = NULL;
3563 while (c) {
3564 pa_sink_volume_change *next = c->next;
3565 pa_sink_volume_change_free(c);
3566 c = next;
3567 }
3568 }
3569
3570 /* Called from the IO thread. */
3571 pa_bool_t pa_sink_volume_change_apply(pa_sink *s, pa_usec_t *usec_to_next) {
3572 pa_usec_t now;
3573 pa_bool_t ret = FALSE;
3574
3575 pa_assert(s);
3576
3577 if (!s->thread_info.volume_changes || !PA_SINK_IS_LINKED(s->state)) {
3578 if (usec_to_next)
3579 *usec_to_next = 0;
3580 return ret;
3581 }
3582
3583 pa_assert(s->write_volume);
3584
3585 now = pa_rtclock_now();
3586
3587 while (s->thread_info.volume_changes && now >= s->thread_info.volume_changes->at) {
3588 pa_sink_volume_change *c = s->thread_info.volume_changes;
3589 PA_LLIST_REMOVE(pa_sink_volume_change, s->thread_info.volume_changes, c);
3590 pa_log_debug("Volume change to %d at %llu was written %llu usec late",
3591 pa_cvolume_avg(&c->hw_volume), (long long unsigned) c->at, (long long unsigned) (now - c->at));
3592 ret = TRUE;
3593 s->thread_info.current_hw_volume = c->hw_volume;
3594 pa_sink_volume_change_free(c);
3595 }
3596
3597 if (ret)
3598 s->write_volume(s);
3599
3600 if (s->thread_info.volume_changes) {
3601 if (usec_to_next)
3602 *usec_to_next = s->thread_info.volume_changes->at - now;
3603 if (pa_log_ratelimit(PA_LOG_DEBUG))
3604 pa_log_debug("Next volume change in %lld usec", (long long) (s->thread_info.volume_changes->at - now));
3605 }
3606 else {
3607 if (usec_to_next)
3608 *usec_to_next = 0;
3609 s->thread_info.volume_changes_tail = NULL;
3610 }
3611 return ret;
3612 }
3613
3614 /* Called from the IO thread. */
3615 static void pa_sink_volume_change_rewind(pa_sink *s, size_t nbytes) {
3616 /* All the queued volume events later than current latency are shifted to happen earlier. */
3617 pa_sink_volume_change *c;
3618 pa_volume_t prev_vol = pa_cvolume_avg(&s->thread_info.current_hw_volume);
3619 pa_usec_t rewound = pa_bytes_to_usec(nbytes, &s->sample_spec);
3620 pa_usec_t limit = pa_sink_get_latency_within_thread(s);
3621
3622 pa_log_debug("latency = %lld", (long long) limit);
3623 limit += pa_rtclock_now() + s->thread_info.volume_change_extra_delay;
3624
3625 PA_LLIST_FOREACH(c, s->thread_info.volume_changes) {
3626 pa_usec_t modified_limit = limit;
3627 if (prev_vol > pa_cvolume_avg(&c->hw_volume))
3628 modified_limit -= s->thread_info.volume_change_safety_margin;
3629 else
3630 modified_limit += s->thread_info.volume_change_safety_margin;
3631 if (c->at > modified_limit) {
3632 c->at -= rewound;
3633 if (c->at < modified_limit)
3634 c->at = modified_limit;
3635 }
3636 prev_vol = pa_cvolume_avg(&c->hw_volume);
3637 }
3638 pa_sink_volume_change_apply(s, NULL);
3639 }
3640
3641 /* Called from the main thread */
3642 /* Gets the list of formats supported by the sink. The members and idxset must
3643 * be freed by the caller. */
3644 pa_idxset* pa_sink_get_formats(pa_sink *s) {
3645 pa_idxset *ret;
3646
3647 pa_assert(s);
3648
3649 if (s->get_formats) {
3650 /* Sink supports format query, all is good */
3651 ret = s->get_formats(s);
3652 } else {
3653 /* Sink doesn't support format query, so assume it does PCM */
3654 pa_format_info *f = pa_format_info_new();
3655 f->encoding = PA_ENCODING_PCM;
3656
3657 ret = pa_idxset_new(NULL, NULL);
3658 pa_idxset_put(ret, f, NULL);
3659 }
3660
3661 return ret;
3662 }
3663
3664 /* Called from the main thread */
3665 /* Allows an external source to set what formats a sink supports if the sink
3666 * permits this. The function makes a copy of the formats on success. */
3667 pa_bool_t pa_sink_set_formats(pa_sink *s, pa_idxset *formats) {
3668 pa_assert(s);
3669 pa_assert(formats);
3670
3671 if (s->set_formats)
3672 /* Sink supports setting formats -- let's give it a shot */
3673 return s->set_formats(s, formats);
3674 else
3675 /* Sink doesn't support setting this -- bail out */
3676 return FALSE;
3677 }
3678
3679 /* Called from the main thread */
3680 /* Checks if the sink can accept this format */
3681 pa_bool_t pa_sink_check_format(pa_sink *s, pa_format_info *f)
3682 {
3683 pa_idxset *formats = NULL;
3684 pa_bool_t ret = FALSE;
3685
3686 pa_assert(s);
3687 pa_assert(f);
3688
3689 formats = pa_sink_get_formats(s);
3690
3691 if (formats) {
3692 pa_format_info *finfo_device;
3693 uint32_t i;
3694
3695 PA_IDXSET_FOREACH(finfo_device, formats, i) {
3696 if (pa_format_info_is_compatible(finfo_device, f)) {
3697 ret = TRUE;
3698 break;
3699 }
3700 }
3701
3702 pa_idxset_free(formats, (pa_free2_cb_t) pa_format_info_free2, NULL);
3703 }
3704
3705 return ret;
3706 }
3707
3708 /* Called from the main thread */
3709 /* Calculates the intersection between formats supported by the sink and
3710 * in_formats, and returns these, in the order of the sink's formats. */
3711 pa_idxset* pa_sink_check_formats(pa_sink *s, pa_idxset *in_formats) {
3712 pa_idxset *out_formats = pa_idxset_new(NULL, NULL), *sink_formats = NULL;
3713 pa_format_info *f_sink, *f_in;
3714 uint32_t i, j;
3715
3716 pa_assert(s);
3717
3718 if (!in_formats || pa_idxset_isempty(in_formats))
3719 goto done;
3720
3721 sink_formats = pa_sink_get_formats(s);
3722
3723 PA_IDXSET_FOREACH(f_sink, sink_formats, i) {
3724 PA_IDXSET_FOREACH(f_in, in_formats, j) {
3725 if (pa_format_info_is_compatible(f_sink, f_in))
3726 pa_idxset_put(out_formats, pa_format_info_copy(f_in), NULL);
3727 }
3728 }
3729
3730 done:
3731 if (sink_formats)
3732 pa_idxset_free(sink_formats, (pa_free2_cb_t) pa_format_info_free2, NULL);
3733
3734 return out_formats;
3735 }