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