]> code.delx.au - pulseaudio/blob - src/pulsecore/sink.c
If the sink volume is lowered to 0 and then increased again, make sure all stream...
[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 <stdlib.h>
28 #include <string.h>
29 #include <stdio.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
38 #include <pulsecore/sink-input.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/sample-util.h>
42 #include <pulsecore/core-subscribe.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/macro.h>
45 #include <pulsecore/play-memblockq.h>
46
47 #include "sink.h"
48
49 #define MAX_MIX_CHANNELS 32
50 #define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
51 #define ABSOLUTE_MIN_LATENCY (500)
52 #define ABSOLUTE_MAX_LATENCY (10*PA_USEC_PER_SEC)
53
54 static PA_DEFINE_CHECK_TYPE(pa_sink, pa_msgobject);
55
56 static void sink_free(pa_object *s);
57
58 pa_sink_new_data* pa_sink_new_data_init(pa_sink_new_data *data) {
59 pa_assert(data);
60
61 memset(data, 0, sizeof(*data));
62 data->proplist = pa_proplist_new();
63
64 return data;
65 }
66
67 void pa_sink_new_data_set_name(pa_sink_new_data *data, const char *name) {
68 pa_assert(data);
69
70 pa_xfree(data->name);
71 data->name = pa_xstrdup(name);
72 }
73
74 void pa_sink_new_data_set_sample_spec(pa_sink_new_data *data, const pa_sample_spec *spec) {
75 pa_assert(data);
76
77 if ((data->sample_spec_is_set = !!spec))
78 data->sample_spec = *spec;
79 }
80
81 void pa_sink_new_data_set_channel_map(pa_sink_new_data *data, const pa_channel_map *map) {
82 pa_assert(data);
83
84 if ((data->channel_map_is_set = !!map))
85 data->channel_map = *map;
86 }
87
88 void pa_sink_new_data_set_volume(pa_sink_new_data *data, const pa_cvolume *volume) {
89 pa_assert(data);
90
91 if ((data->volume_is_set = !!volume))
92 data->volume = *volume;
93 }
94
95 void pa_sink_new_data_set_muted(pa_sink_new_data *data, pa_bool_t mute) {
96 pa_assert(data);
97
98 data->muted_is_set = TRUE;
99 data->muted = !!mute;
100 }
101
102 void pa_sink_new_data_done(pa_sink_new_data *data) {
103 pa_assert(data);
104
105 pa_xfree(data->name);
106 pa_proplist_free(data->proplist);
107 }
108
109 /* Called from main context */
110 static void reset_callbacks(pa_sink *s) {
111 pa_assert(s);
112
113 s->set_state = NULL;
114 s->get_volume = NULL;
115 s->set_volume = NULL;
116 s->get_mute = NULL;
117 s->set_mute = NULL;
118 s->request_rewind = NULL;
119 s->update_requested_latency = NULL;
120 }
121
122 /* Called from main context */
123 pa_sink* pa_sink_new(
124 pa_core *core,
125 pa_sink_new_data *data,
126 pa_sink_flags_t flags) {
127
128 pa_sink *s;
129 const char *name;
130 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
131 pa_source_new_data source_data;
132 const char *dn;
133 char *pt;
134
135 pa_assert(core);
136 pa_assert(data);
137 pa_assert(data->name);
138
139 s = pa_msgobject_new(pa_sink);
140
141 if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_SINK, s, data->namereg_fail))) {
142 pa_xfree(s);
143 return NULL;
144 }
145
146 pa_sink_new_data_set_name(data, name);
147
148 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_NEW], data) < 0) {
149 pa_xfree(s);
150 pa_namereg_unregister(core, name);
151 return NULL;
152 }
153
154 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
155 pa_return_null_if_fail(data->name && pa_utf8_valid(data->name) && data->name[0]);
156
157 pa_return_null_if_fail(data->sample_spec_is_set && pa_sample_spec_valid(&data->sample_spec));
158
159 if (!data->channel_map_is_set)
160 pa_return_null_if_fail(pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT));
161
162 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
163 pa_return_null_if_fail(data->channel_map.channels == data->sample_spec.channels);
164
165 if (!data->volume_is_set)
166 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
167
168 pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
169 pa_return_null_if_fail(data->volume.channels == data->sample_spec.channels);
170
171 if (!data->muted_is_set)
172 data->muted = FALSE;
173
174 if (data->card)
175 pa_proplist_update(data->proplist, PA_UPDATE_MERGE, data->card->proplist);
176
177 pa_device_init_description(data->proplist);
178 pa_device_init_icon(data->proplist, TRUE);
179
180 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_FIXATE], data) < 0) {
181 pa_xfree(s);
182 pa_namereg_unregister(core, name);
183 return NULL;
184 }
185
186 s->parent.parent.free = sink_free;
187 s->parent.process_msg = pa_sink_process_msg;
188
189 s->core = core;
190 s->state = PA_SINK_INIT;
191 s->flags = flags;
192 s->name = pa_xstrdup(name);
193 s->proplist = pa_proplist_copy(data->proplist);
194 s->driver = pa_xstrdup(pa_path_get_filename(data->driver));
195 s->module = data->module;
196 s->card = data->card;
197
198 s->sample_spec = data->sample_spec;
199 s->channel_map = data->channel_map;
200
201 s->inputs = pa_idxset_new(NULL, NULL);
202 s->n_corked = 0;
203
204 s->virtual_volume = data->volume;
205 pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
206 s->base_volume = PA_VOLUME_NORM;
207 s->n_volume_steps = PA_VOLUME_NORM+1;
208 s->muted = data->muted;
209 s->refresh_volume = s->refresh_muted = FALSE;
210
211 reset_callbacks(s);
212 s->userdata = NULL;
213
214 s->asyncmsgq = NULL;
215 s->rtpoll = NULL;
216
217 pa_silence_memchunk_get(
218 &core->silence_cache,
219 core->mempool,
220 &s->silence,
221 &s->sample_spec,
222 0);
223
224 s->thread_info.inputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
225 s->thread_info.soft_volume = s->soft_volume;
226 s->thread_info.soft_muted = s->muted;
227 s->thread_info.state = s->state;
228 s->thread_info.rewind_nbytes = 0;
229 s->thread_info.rewind_requested = FALSE;
230 s->thread_info.max_rewind = 0;
231 s->thread_info.max_request = 0;
232 s->thread_info.requested_latency_valid = FALSE;
233 s->thread_info.requested_latency = 0;
234 s->thread_info.min_latency = ABSOLUTE_MIN_LATENCY;
235 s->thread_info.max_latency = ABSOLUTE_MAX_LATENCY;
236
237 pa_assert_se(pa_idxset_put(core->sinks, s, &s->index) >= 0);
238
239 if (s->card)
240 pa_assert_se(pa_idxset_put(s->card->sinks, s, NULL) >= 0);
241
242 pt = pa_proplist_to_string_sep(s->proplist, "\n ");
243 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s\n %s",
244 s->index,
245 s->name,
246 pa_sample_spec_snprint(st, sizeof(st), &s->sample_spec),
247 pa_channel_map_snprint(cm, sizeof(cm), &s->channel_map),
248 pt);
249 pa_xfree(pt);
250
251 pa_source_new_data_init(&source_data);
252 pa_source_new_data_set_sample_spec(&source_data, &s->sample_spec);
253 pa_source_new_data_set_channel_map(&source_data, &s->channel_map);
254 source_data.name = pa_sprintf_malloc("%s.monitor", name);
255 source_data.driver = data->driver;
256 source_data.module = data->module;
257 source_data.card = data->card;
258
259 dn = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
260 pa_proplist_setf(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Monitor of %s", dn ? dn : s->name);
261 pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_CLASS, "monitor");
262
263 s->monitor_source = pa_source_new(core, &source_data, 0);
264
265 pa_source_new_data_done(&source_data);
266
267 if (!s->monitor_source) {
268 pa_sink_unlink(s);
269 pa_sink_unref(s);
270 return NULL;
271 }
272
273 s->monitor_source->monitor_of = s;
274
275 pa_source_set_latency_range(s->monitor_source, s->thread_info.min_latency, s->thread_info.max_latency);
276 pa_source_set_max_rewind(s->monitor_source, s->thread_info.max_rewind);
277
278 return s;
279 }
280
281 /* Called from main context */
282 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
283 int ret;
284 pa_bool_t suspend_change;
285 pa_sink_state_t original_state;
286
287 pa_assert(s);
288
289 if (s->state == state)
290 return 0;
291
292 original_state = s->state;
293
294 suspend_change =
295 (original_state == PA_SINK_SUSPENDED && PA_SINK_IS_OPENED(state)) ||
296 (PA_SINK_IS_OPENED(original_state) && state == PA_SINK_SUSPENDED);
297
298 if (s->set_state)
299 if ((ret = s->set_state(s, state)) < 0)
300 return ret;
301
302 if (s->asyncmsgq)
303 if ((ret = pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL)) < 0) {
304
305 if (s->set_state)
306 s->set_state(s, original_state);
307
308 return ret;
309 }
310
311 s->state = state;
312
313 if (state != PA_SINK_UNLINKED) { /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
314 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], s);
315 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
316 }
317
318 if (suspend_change) {
319 pa_sink_input *i;
320 uint32_t idx;
321
322 /* We're suspending or resuming, tell everyone about it */
323
324 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx)))
325 if (s->state == PA_SINK_SUSPENDED &&
326 (i->flags & PA_SINK_INPUT_FAIL_ON_SUSPEND))
327 pa_sink_input_kill(i);
328 else if (i->suspend)
329 i->suspend(i, state == PA_SINK_SUSPENDED);
330
331 if (s->monitor_source)
332 pa_source_sync_suspend(s->monitor_source);
333 }
334
335 return 0;
336 }
337
338 /* Called from main context */
339 void pa_sink_put(pa_sink* s) {
340 pa_sink_assert_ref(s);
341
342 pa_assert(s->state == PA_SINK_INIT);
343
344 /* The following fields must be initialized properly when calling _put() */
345 pa_assert(s->asyncmsgq);
346 pa_assert(s->rtpoll);
347 pa_assert(s->thread_info.min_latency <= s->thread_info.max_latency);
348
349 if (!(s->flags & PA_SINK_HW_VOLUME_CTRL)) {
350 s->flags |= PA_SINK_DECIBEL_VOLUME;
351
352 s->thread_info.soft_volume = s->soft_volume;
353 s->thread_info.soft_muted = s->muted;
354 }
355
356 if (s->flags & PA_SINK_DECIBEL_VOLUME)
357 s->n_volume_steps = PA_VOLUME_NORM+1;
358
359 if (s->core->flat_volumes)
360 if (s->flags & PA_SINK_DECIBEL_VOLUME)
361 s->flags |= PA_SINK_FLAT_VOLUME;
362
363 if (s->flags & PA_SINK_LATENCY)
364 s->monitor_source->flags |= PA_SOURCE_LATENCY;
365
366 if (s->flags & PA_SINK_DYNAMIC_LATENCY)
367 s->monitor_source->flags |= PA_SOURCE_DYNAMIC_LATENCY;
368
369 pa_assert_se(sink_set_state(s, PA_SINK_IDLE) == 0);
370
371 pa_source_put(s->monitor_source);
372
373 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
374 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PUT], s);
375 }
376
377 /* Called from main context */
378 void pa_sink_unlink(pa_sink* s) {
379 pa_bool_t linked;
380 pa_sink_input *i, *j = NULL;
381
382 pa_assert(s);
383
384 /* Please note that pa_sink_unlink() does more than simply
385 * reversing pa_sink_put(). It also undoes the registrations
386 * already done in pa_sink_new()! */
387
388 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
389 * may be called multiple times on the same sink without bad
390 * effects. */
391
392 linked = PA_SINK_IS_LINKED(s->state);
393
394 if (linked)
395 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK], s);
396
397 if (s->state != PA_SINK_UNLINKED)
398 pa_namereg_unregister(s->core, s->name);
399 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
400
401 if (s->card)
402 pa_idxset_remove_by_data(s->card->sinks, s, NULL);
403
404 while ((i = pa_idxset_first(s->inputs, NULL))) {
405 pa_assert(i != j);
406 pa_sink_input_kill(i);
407 j = i;
408 }
409
410 if (linked)
411 sink_set_state(s, PA_SINK_UNLINKED);
412 else
413 s->state = PA_SINK_UNLINKED;
414
415 reset_callbacks(s);
416
417 if (s->monitor_source)
418 pa_source_unlink(s->monitor_source);
419
420 if (linked) {
421 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
422 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], s);
423 }
424 }
425
426 /* Called from main context */
427 static void sink_free(pa_object *o) {
428 pa_sink *s = PA_SINK(o);
429 pa_sink_input *i;
430
431 pa_assert(s);
432 pa_assert(pa_sink_refcnt(s) == 0);
433
434 if (PA_SINK_IS_LINKED(s->state))
435 pa_sink_unlink(s);
436
437 pa_log_info("Freeing sink %u \"%s\"", s->index, s->name);
438
439 if (s->monitor_source) {
440 pa_source_unref(s->monitor_source);
441 s->monitor_source = NULL;
442 }
443
444 pa_idxset_free(s->inputs, NULL, NULL);
445
446 while ((i = pa_hashmap_steal_first(s->thread_info.inputs)))
447 pa_sink_input_unref(i);
448
449 pa_hashmap_free(s->thread_info.inputs, NULL, NULL);
450
451 if (s->silence.memblock)
452 pa_memblock_unref(s->silence.memblock);
453
454 pa_xfree(s->name);
455 pa_xfree(s->driver);
456
457 if (s->proplist)
458 pa_proplist_free(s->proplist);
459
460 pa_xfree(s);
461 }
462
463 /* Called from main context */
464 void pa_sink_set_asyncmsgq(pa_sink *s, pa_asyncmsgq *q) {
465 pa_sink_assert_ref(s);
466
467 s->asyncmsgq = q;
468
469 if (s->monitor_source)
470 pa_source_set_asyncmsgq(s->monitor_source, q);
471 }
472
473 /* Called from main context */
474 void pa_sink_set_rtpoll(pa_sink *s, pa_rtpoll *p) {
475 pa_sink_assert_ref(s);
476
477 s->rtpoll = p;
478 if (s->monitor_source)
479 pa_source_set_rtpoll(s->monitor_source, p);
480 }
481
482 /* Called from main context */
483 int pa_sink_update_status(pa_sink*s) {
484 pa_sink_assert_ref(s);
485 pa_assert(PA_SINK_IS_LINKED(s->state));
486
487 if (s->state == PA_SINK_SUSPENDED)
488 return 0;
489
490 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
491 }
492
493 /* Called from main context */
494 int pa_sink_suspend(pa_sink *s, pa_bool_t suspend) {
495 pa_sink_assert_ref(s);
496 pa_assert(PA_SINK_IS_LINKED(s->state));
497
498 if (suspend)
499 return sink_set_state(s, PA_SINK_SUSPENDED);
500 else
501 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
502 }
503
504 /* Called from main context */
505 pa_queue *pa_sink_move_all_start(pa_sink *s) {
506 pa_queue *q;
507 pa_sink_input *i, *n;
508 uint32_t idx;
509
510 pa_sink_assert_ref(s);
511 pa_assert(PA_SINK_IS_LINKED(s->state));
512
513 q = pa_queue_new();
514
515 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = n) {
516 n = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx));
517
518 if (pa_sink_input_start_move(i) >= 0)
519 pa_queue_push(q, pa_sink_input_ref(i));
520 }
521
522 return q;
523 }
524
525 /* Called from main context */
526 void pa_sink_move_all_finish(pa_sink *s, pa_queue *q, pa_bool_t save) {
527 pa_sink_input *i;
528
529 pa_sink_assert_ref(s);
530 pa_assert(PA_SINK_IS_LINKED(s->state));
531 pa_assert(q);
532
533 while ((i = PA_SINK_INPUT(pa_queue_pop(q)))) {
534 if (pa_sink_input_finish_move(i, s, save) < 0)
535 pa_sink_input_kill(i);
536
537 pa_sink_input_unref(i);
538 }
539
540 pa_queue_free(q, NULL, NULL);
541 }
542
543 /* Called from main context */
544 void pa_sink_move_all_fail(pa_queue *q) {
545 pa_sink_input *i;
546 pa_assert(q);
547
548 while ((i = PA_SINK_INPUT(pa_queue_pop(q)))) {
549 if (pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FAIL], i) == PA_HOOK_OK) {
550 pa_sink_input_kill(i);
551 pa_sink_input_unref(i);
552 }
553 }
554
555 pa_queue_free(q, NULL, NULL);
556 }
557
558 /* Called from IO thread context */
559 void pa_sink_process_rewind(pa_sink *s, size_t nbytes) {
560 pa_sink_input *i;
561 void *state = NULL;
562 pa_sink_assert_ref(s);
563 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
564
565 /* If nobody requested this and this is actually no real rewind
566 * then we can short cut this */
567 if (!s->thread_info.rewind_requested && nbytes <= 0)
568 return;
569
570 s->thread_info.rewind_nbytes = 0;
571 s->thread_info.rewind_requested = FALSE;
572
573 if (s->thread_info.state == PA_SINK_SUSPENDED)
574 return;
575
576 if (nbytes > 0)
577 pa_log_debug("Processing rewind...");
578
579 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
580 pa_sink_input_assert_ref(i);
581 pa_sink_input_process_rewind(i, nbytes);
582 }
583
584 if (nbytes > 0)
585 if (s->monitor_source && PA_SOURCE_IS_LINKED(s->monitor_source->thread_info.state))
586 pa_source_process_rewind(s->monitor_source, nbytes);
587 }
588
589 /* Called from IO thread context */
590 static unsigned fill_mix_info(pa_sink *s, size_t *length, pa_mix_info *info, unsigned maxinfo) {
591 pa_sink_input *i;
592 unsigned n = 0;
593 void *state = NULL;
594 size_t mixlength = *length;
595
596 pa_sink_assert_ref(s);
597 pa_assert(info);
598
599 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)) && maxinfo > 0) {
600 pa_sink_input_assert_ref(i);
601
602 pa_sink_input_peek(i, *length, &info->chunk, &info->volume);
603
604 if (mixlength == 0 || info->chunk.length < mixlength)
605 mixlength = info->chunk.length;
606
607 if (pa_memblock_is_silence(info->chunk.memblock)) {
608 pa_memblock_unref(info->chunk.memblock);
609 continue;
610 }
611
612 info->userdata = pa_sink_input_ref(i);
613
614 pa_assert(info->chunk.memblock);
615 pa_assert(info->chunk.length > 0);
616
617 info++;
618 n++;
619 maxinfo--;
620 }
621
622 if (mixlength > 0)
623 *length = mixlength;
624
625 return n;
626 }
627
628 /* Called from IO thread context */
629 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned n, pa_memchunk *result) {
630 pa_sink_input *i;
631 void *state = NULL;
632 unsigned p = 0;
633 unsigned n_unreffed = 0;
634
635 pa_sink_assert_ref(s);
636 pa_assert(result);
637 pa_assert(result->memblock);
638 pa_assert(result->length > 0);
639
640 /* We optimize for the case where the order of the inputs has not changed */
641
642 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
643 unsigned j;
644 pa_mix_info* m = NULL;
645
646 pa_sink_input_assert_ref(i);
647
648 /* Let's try to find the matching entry info the pa_mix_info array */
649 for (j = 0; j < n; j ++) {
650
651 if (info[p].userdata == i) {
652 m = info + p;
653 break;
654 }
655
656 p++;
657 if (p >= n)
658 p = 0;
659 }
660
661 /* Drop read data */
662 pa_sink_input_drop(i, result->length);
663
664 if (s->monitor_source && PA_SOURCE_IS_LINKED(s->monitor_source->thread_info.state)) {
665
666 if (pa_hashmap_size(i->thread_info.direct_outputs) > 0) {
667 void *ostate = NULL;
668 pa_source_output *o;
669 pa_memchunk c;
670
671 if (m && m->chunk.memblock) {
672 c = m->chunk;
673 pa_memblock_ref(c.memblock);
674 pa_assert(result->length <= c.length);
675 c.length = result->length;
676
677 pa_memchunk_make_writable(&c, 0);
678 pa_volume_memchunk(&c, &s->sample_spec, &m->volume);
679 } else {
680 c = s->silence;
681 pa_memblock_ref(c.memblock);
682 pa_assert(result->length <= c.length);
683 c.length = result->length;
684 }
685
686 while ((o = pa_hashmap_iterate(i->thread_info.direct_outputs, &ostate, NULL))) {
687 pa_source_output_assert_ref(o);
688 pa_assert(o->direct_on_input == i);
689 pa_source_post_direct(s->monitor_source, o, &c);
690 }
691
692 pa_memblock_unref(c.memblock);
693 }
694 }
695
696 if (m) {
697 if (m->chunk.memblock)
698 pa_memblock_unref(m->chunk.memblock);
699 pa_memchunk_reset(&m->chunk);
700
701 pa_sink_input_unref(m->userdata);
702 m->userdata = NULL;
703
704 n_unreffed += 1;
705 }
706 }
707
708 /* Now drop references to entries that are included in the
709 * pa_mix_info array but don't exist anymore */
710
711 if (n_unreffed < n) {
712 for (; n > 0; info++, n--) {
713 if (info->userdata)
714 pa_sink_input_unref(info->userdata);
715 if (info->chunk.memblock)
716 pa_memblock_unref(info->chunk.memblock);
717 }
718 }
719
720 if (s->monitor_source && PA_SOURCE_IS_LINKED(s->monitor_source->thread_info.state))
721 pa_source_post(s->monitor_source, result);
722 }
723
724 /* Called from IO thread context */
725 void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
726 pa_mix_info info[MAX_MIX_CHANNELS];
727 unsigned n;
728 size_t block_size_max;
729
730 pa_sink_assert_ref(s);
731 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
732 pa_assert(pa_frame_aligned(length, &s->sample_spec));
733 pa_assert(result);
734
735 pa_sink_ref(s);
736
737 pa_assert(!s->thread_info.rewind_requested);
738 pa_assert(s->thread_info.rewind_nbytes == 0);
739
740 if (s->thread_info.state == PA_SINK_SUSPENDED) {
741 result->memblock = pa_memblock_ref(s->silence.memblock);
742 result->index = s->silence.index;
743 result->length = PA_MIN(s->silence.length, length);
744 return;
745 }
746
747 if (length <= 0)
748 length = pa_frame_align(MIX_BUFFER_LENGTH, &s->sample_spec);
749
750 block_size_max = pa_mempool_block_size_max(s->core->mempool);
751 if (length > block_size_max)
752 length = pa_frame_align(block_size_max, &s->sample_spec);
753
754 pa_assert(length > 0);
755
756 n = fill_mix_info(s, &length, info, MAX_MIX_CHANNELS);
757
758 if (n == 0) {
759
760 *result = s->silence;
761 pa_memblock_ref(result->memblock);
762
763 if (result->length > length)
764 result->length = length;
765
766 } else if (n == 1) {
767 pa_cvolume volume;
768
769 *result = info[0].chunk;
770 pa_memblock_ref(result->memblock);
771
772 if (result->length > length)
773 result->length = length;
774
775 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
776
777 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&volume)) {
778 pa_memchunk_make_writable(result, 0);
779 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
780 pa_silence_memchunk(result, &s->sample_spec);
781 else
782 pa_volume_memchunk(result, &s->sample_spec, &volume);
783 }
784 } else {
785 void *ptr;
786 result->memblock = pa_memblock_new(s->core->mempool, length);
787
788 ptr = pa_memblock_acquire(result->memblock);
789 result->length = pa_mix(info, n,
790 ptr, length,
791 &s->sample_spec,
792 &s->thread_info.soft_volume,
793 s->thread_info.soft_muted);
794 pa_memblock_release(result->memblock);
795
796 result->index = 0;
797 }
798
799 inputs_drop(s, info, n, result);
800
801 pa_sink_unref(s);
802 }
803
804 /* Called from IO thread context */
805 void pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
806 pa_mix_info info[MAX_MIX_CHANNELS];
807 unsigned n;
808 size_t length, block_size_max;
809
810 pa_sink_assert_ref(s);
811 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
812 pa_assert(target);
813 pa_assert(target->memblock);
814 pa_assert(target->length > 0);
815 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
816
817 pa_sink_ref(s);
818
819 pa_assert(!s->thread_info.rewind_requested);
820 pa_assert(s->thread_info.rewind_nbytes == 0);
821
822 if (s->thread_info.state == PA_SINK_SUSPENDED) {
823 pa_silence_memchunk(target, &s->sample_spec);
824 return;
825 }
826
827 length = target->length;
828 block_size_max = pa_mempool_block_size_max(s->core->mempool);
829 if (length > block_size_max)
830 length = pa_frame_align(block_size_max, &s->sample_spec);
831
832 pa_assert(length > 0);
833
834 n = fill_mix_info(s, &length, info, MAX_MIX_CHANNELS);
835
836 if (n == 0) {
837 if (target->length > length)
838 target->length = length;
839
840 pa_silence_memchunk(target, &s->sample_spec);
841 } else if (n == 1) {
842 pa_cvolume volume;
843
844 if (target->length > length)
845 target->length = length;
846
847 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
848
849 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
850 pa_silence_memchunk(target, &s->sample_spec);
851 else {
852 pa_memchunk vchunk;
853
854 vchunk = info[0].chunk;
855 pa_memblock_ref(vchunk.memblock);
856
857 if (vchunk.length > length)
858 vchunk.length = length;
859
860 if (!pa_cvolume_is_norm(&volume)) {
861 pa_memchunk_make_writable(&vchunk, 0);
862 pa_volume_memchunk(&vchunk, &s->sample_spec, &volume);
863 }
864
865 pa_memchunk_memcpy(target, &vchunk);
866 pa_memblock_unref(vchunk.memblock);
867 }
868
869 } else {
870 void *ptr;
871
872 ptr = pa_memblock_acquire(target->memblock);
873
874 target->length = pa_mix(info, n,
875 (uint8_t*) ptr + target->index, length,
876 &s->sample_spec,
877 &s->thread_info.soft_volume,
878 s->thread_info.soft_muted);
879
880 pa_memblock_release(target->memblock);
881 }
882
883 inputs_drop(s, info, n, target);
884
885 pa_sink_unref(s);
886 }
887
888 /* Called from IO thread context */
889 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
890 pa_memchunk chunk;
891 size_t l, d;
892
893 pa_sink_assert_ref(s);
894 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
895 pa_assert(target);
896 pa_assert(target->memblock);
897 pa_assert(target->length > 0);
898 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
899
900 pa_sink_ref(s);
901
902 pa_assert(!s->thread_info.rewind_requested);
903 pa_assert(s->thread_info.rewind_nbytes == 0);
904
905 l = target->length;
906 d = 0;
907 while (l > 0) {
908 chunk = *target;
909 chunk.index += d;
910 chunk.length -= d;
911
912 pa_sink_render_into(s, &chunk);
913
914 d += chunk.length;
915 l -= chunk.length;
916 }
917
918 pa_sink_unref(s);
919 }
920
921 /* Called from IO thread context */
922 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
923 pa_sink_assert_ref(s);
924 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
925 pa_assert(length > 0);
926 pa_assert(pa_frame_aligned(length, &s->sample_spec));
927 pa_assert(result);
928
929 pa_assert(!s->thread_info.rewind_requested);
930 pa_assert(s->thread_info.rewind_nbytes == 0);
931
932 /*** This needs optimization ***/
933
934 result->index = 0;
935 result->length = length;
936 result->memblock = pa_memblock_new(s->core->mempool, length);
937
938 pa_sink_render_into_full(s, result);
939 }
940
941 /* Called from main thread */
942 pa_usec_t pa_sink_get_latency(pa_sink *s) {
943 pa_usec_t usec = 0;
944
945 pa_sink_assert_ref(s);
946 pa_assert(PA_SINK_IS_LINKED(s->state));
947
948 /* The returned value is supposed to be in the time domain of the sound card! */
949
950 if (s->state == PA_SINK_SUSPENDED)
951 return 0;
952
953 if (!(s->flags & PA_SINK_LATENCY))
954 return 0;
955
956 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) == 0);
957
958 return usec;
959 }
960
961 /* Called from IO thread */
962 pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s) {
963 pa_usec_t usec = 0;
964 pa_msgobject *o;
965
966 pa_sink_assert_ref(s);
967 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
968
969 /* The returned value is supposed to be in the time domain of the sound card! */
970
971 if (s->thread_info.state == PA_SINK_SUSPENDED)
972 return 0;
973
974 if (!(s->flags & PA_SINK_LATENCY))
975 return 0;
976
977 o = PA_MSGOBJECT(s);
978
979 /* We probably should make this a proper vtable callback instead of going through process_msg() */
980
981 if (o->process_msg(o, PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
982 return -1;
983
984 return usec;
985 }
986
987 /* Called from main thread */
988 void pa_sink_update_flat_volume(pa_sink *s, pa_cvolume *new_volume) {
989 pa_sink_input *i;
990 uint32_t idx;
991
992 pa_sink_assert_ref(s);
993 pa_assert(new_volume);
994 pa_assert(PA_SINK_IS_LINKED(s->state));
995 pa_assert(s->flags & PA_SINK_FLAT_VOLUME);
996
997 /* This is called whenever a sink input volume changes and we
998 * might need to fix up the sink volume accordingly. Please note
999 * that we don't actually update the sinks volume here, we only
1000 * return how it needs to be updated. The caller should then call
1001 * pa_sink_set_flat_volume().*/
1002
1003 if (pa_idxset_isempty(s->inputs)) {
1004 /* In the special case that we have no sink input we leave the
1005 * volume unmodified. */
1006 *new_volume = s->virtual_volume;
1007 return;
1008 }
1009
1010 pa_cvolume_mute(new_volume, s->channel_map.channels);
1011
1012 /* First let's determine the new maximum volume of all inputs
1013 * connected to this sink */
1014 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
1015 unsigned c;
1016 pa_cvolume remapped_volume;
1017
1018 remapped_volume = i->virtual_volume;
1019 pa_cvolume_remap(&remapped_volume, &i->channel_map, &s->channel_map);
1020
1021 for (c = 0; c < new_volume->channels; c++)
1022 if (remapped_volume.values[c] > new_volume->values[c])
1023 new_volume->values[c] = remapped_volume.values[c];
1024 }
1025
1026 /* Then, let's update the soft volumes of all inputs connected
1027 * to this sink */
1028 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
1029 pa_cvolume remapped_new_volume;
1030
1031 /* This basically calculates i->soft_volume := i->virtual_volume / new_volume * i->volume_factor */
1032
1033 remapped_new_volume = *new_volume;
1034 pa_cvolume_remap(&remapped_new_volume, &s->channel_map, &i->channel_map);
1035 pa_sw_cvolume_divide(&i->soft_volume, &i->virtual_volume, &remapped_new_volume);
1036 pa_sw_cvolume_multiply(&i->soft_volume, &i->soft_volume, &i->volume_factor);
1037
1038 /* Hooks have the ability to play games with i->soft_volume */
1039 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_INPUT_SET_VOLUME], i);
1040
1041 /* We don't issue PA_SINK_INPUT_MESSAGE_SET_VOLUME because
1042 * we want the update to have atomically with the sink
1043 * volume update, hence we do it within the
1044 * pa_sink_set_flat_volume() call below */
1045 }
1046 }
1047
1048 /* Called from main thread */
1049 void pa_sink_propagate_flat_volume(pa_sink *s, const pa_cvolume *old_volume) {
1050 pa_sink_input *i;
1051 uint32_t idx;
1052
1053 pa_sink_assert_ref(s);
1054 pa_assert(old_volume);
1055 pa_assert(PA_SINK_IS_LINKED(s->state));
1056 pa_assert(s->flags & PA_SINK_FLAT_VOLUME);
1057
1058 /* This is called whenever the sink volume changes that is not
1059 * caused by a sink input volume change. We need to fix up the
1060 * sink input volumes accordingly */
1061
1062 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
1063 pa_cvolume remapped_old_volume, remapped_new_volume, fixed_volume;
1064 unsigned c;
1065
1066 /* This basically calculates i->virtual_volume := i->virtual_volume * s->virtual_volume / old_volume */
1067
1068 remapped_new_volume = s->virtual_volume;
1069 pa_cvolume_remap(&remapped_new_volume, &s->channel_map, &i->channel_map);
1070
1071 remapped_old_volume = *old_volume;
1072 pa_cvolume_remap(&remapped_old_volume, &s->channel_map, &i->channel_map);
1073
1074 for (c = 0; c < i->sample_spec.channels; c++)
1075
1076 if (remapped_old_volume.values[c] == PA_VOLUME_MUTED)
1077 fixed_volume.values[c] = remapped_new_volume.values[c];
1078 else
1079 fixed_volume.values[c] = (pa_volume_t)
1080 ((uint64_t) i->virtual_volume.values[c] *
1081 (uint64_t) remapped_new_volume.values[c] /
1082 (uint64_t) remapped_old_volume.values[c]);
1083
1084 fixed_volume.channels = i->virtual_volume.channels;
1085
1086 if (!pa_cvolume_equal(&fixed_volume, &i->virtual_volume)) {
1087 i->virtual_volume = fixed_volume;
1088
1089 /* The virtual volume changed, let's tell people so */
1090 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1091 }
1092 }
1093 }
1094
1095 /* Called from main thread */
1096 void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume, pa_bool_t propagate, pa_bool_t sendmsg) {
1097 pa_cvolume old_virtual_volume;
1098 pa_bool_t virtual_volume_changed;
1099
1100 pa_sink_assert_ref(s);
1101 pa_assert(PA_SINK_IS_LINKED(s->state));
1102 pa_assert(volume);
1103 pa_assert(pa_cvolume_valid(volume));
1104 pa_assert(pa_cvolume_compatible(volume, &s->sample_spec));
1105
1106 old_virtual_volume = s->virtual_volume;
1107 s->virtual_volume = *volume;
1108 virtual_volume_changed = !pa_cvolume_equal(&old_virtual_volume, &s->virtual_volume);
1109
1110 /* Propagate this volume change back to the inputs */
1111 if (virtual_volume_changed)
1112 if (propagate && (s->flags & PA_SINK_FLAT_VOLUME))
1113 pa_sink_propagate_flat_volume(s, &old_virtual_volume);
1114
1115 if (s->set_volume) {
1116 /* If we have a function set_volume(), then we do not apply a
1117 * soft volume by default. However, set_volume() is apply one
1118 * to s->soft_volume */
1119
1120 pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
1121 s->set_volume(s);
1122
1123 } else
1124 /* If we have no function set_volume(), then the soft volume
1125 * becomes the virtual volume */
1126 s->soft_volume = s->virtual_volume;
1127
1128 /* This tells the sink that soft and/or virtual volume changed */
1129 if (sendmsg)
1130 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
1131
1132 if (virtual_volume_changed)
1133 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1134 }
1135
1136 /* Called from main thread. Only to be called by sink implementor */
1137 void pa_sink_set_soft_volume(pa_sink *s, const pa_cvolume *volume) {
1138 pa_sink_assert_ref(s);
1139 pa_assert(volume);
1140
1141 s->soft_volume = *volume;
1142
1143 if (PA_SINK_IS_LINKED(s->state))
1144 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
1145 else
1146 s->thread_info.soft_volume = *volume;
1147 }
1148
1149 /* Called from main thread */
1150 const pa_cvolume *pa_sink_get_volume(pa_sink *s, pa_bool_t force_refresh) {
1151 pa_sink_assert_ref(s);
1152
1153 if (s->refresh_volume || force_refresh) {
1154 struct pa_cvolume old_virtual_volume = s->virtual_volume;
1155
1156 if (s->get_volume)
1157 s->get_volume(s);
1158
1159 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, NULL, 0, NULL) == 0);
1160
1161 if (!pa_cvolume_equal(&old_virtual_volume, &s->virtual_volume)) {
1162
1163 if (s->flags & PA_SINK_FLAT_VOLUME)
1164 pa_sink_propagate_flat_volume(s, &old_virtual_volume);
1165
1166 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1167 }
1168 }
1169
1170 return &s->virtual_volume;
1171 }
1172
1173 /* Called from main thread */
1174 void pa_sink_volume_changed(pa_sink *s, const pa_cvolume *new_volume) {
1175 pa_sink_assert_ref(s);
1176
1177 /* The sink implementor may call this if the volume changed to make sure everyone is notified */
1178
1179 if (pa_cvolume_equal(&s->virtual_volume, new_volume))
1180 return;
1181
1182 s->virtual_volume = *new_volume;
1183 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1184 }
1185
1186 /* Called from main thread */
1187 void pa_sink_set_mute(pa_sink *s, pa_bool_t mute) {
1188 pa_bool_t old_muted;
1189
1190 pa_sink_assert_ref(s);
1191 pa_assert(PA_SINK_IS_LINKED(s->state));
1192
1193 old_muted = s->muted;
1194 s->muted = mute;
1195
1196 if (s->set_mute)
1197 s->set_mute(s);
1198
1199 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, NULL, 0, NULL) == 0);
1200
1201 if (old_muted != s->muted)
1202 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1203 }
1204
1205 /* Called from main thread */
1206 pa_bool_t pa_sink_get_mute(pa_sink *s, pa_bool_t force_refresh) {
1207
1208 pa_sink_assert_ref(s);
1209
1210 if (s->refresh_muted || force_refresh) {
1211 pa_bool_t old_muted = s->muted;
1212
1213 if (s->get_mute)
1214 s->get_mute(s);
1215
1216 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MUTE, NULL, 0, NULL) == 0);
1217
1218 if (old_muted != s->muted)
1219 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1220 }
1221
1222 return s->muted;
1223 }
1224
1225 /* Called from main thread */
1226 void pa_sink_mute_changed(pa_sink *s, pa_bool_t new_muted) {
1227 pa_sink_assert_ref(s);
1228
1229 /* The sink implementor may call this if the volume changed to make sure everyone is notified */
1230
1231 if (s->muted == new_muted)
1232 return;
1233
1234 s->muted = new_muted;
1235 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1236 }
1237
1238 /* Called from main thread */
1239 pa_bool_t pa_sink_update_proplist(pa_sink *s, pa_update_mode_t mode, pa_proplist *p) {
1240 pa_sink_assert_ref(s);
1241
1242 if (p)
1243 pa_proplist_update(s->proplist, mode, p);
1244
1245 if (PA_SINK_IS_LINKED(s->state)) {
1246 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], s);
1247 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1248 }
1249
1250 return TRUE;
1251 }
1252
1253 /* Called from main thread */
1254 void pa_sink_set_description(pa_sink *s, const char *description) {
1255 const char *old;
1256 pa_sink_assert_ref(s);
1257
1258 if (!description && !pa_proplist_contains(s->proplist, PA_PROP_DEVICE_DESCRIPTION))
1259 return;
1260
1261 old = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
1262
1263 if (old && description && !strcmp(old, description))
1264 return;
1265
1266 if (description)
1267 pa_proplist_sets(s->proplist, PA_PROP_DEVICE_DESCRIPTION, description);
1268 else
1269 pa_proplist_unset(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
1270
1271 if (s->monitor_source) {
1272 char *n;
1273
1274 n = pa_sprintf_malloc("Monitor Source of %s", description ? description : s->name);
1275 pa_source_set_description(s->monitor_source, n);
1276 pa_xfree(n);
1277 }
1278
1279 if (PA_SINK_IS_LINKED(s->state)) {
1280 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1281 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], s);
1282 }
1283 }
1284
1285 /* Called from main thread */
1286 unsigned pa_sink_linked_by(pa_sink *s) {
1287 unsigned ret;
1288
1289 pa_sink_assert_ref(s);
1290 pa_assert(PA_SINK_IS_LINKED(s->state));
1291
1292 ret = pa_idxset_size(s->inputs);
1293
1294 /* We add in the number of streams connected to us here. Please
1295 * note the asymmmetry to pa_sink_used_by()! */
1296
1297 if (s->monitor_source)
1298 ret += pa_source_linked_by(s->monitor_source);
1299
1300 return ret;
1301 }
1302
1303 /* Called from main thread */
1304 unsigned pa_sink_used_by(pa_sink *s) {
1305 unsigned ret;
1306
1307 pa_sink_assert_ref(s);
1308 pa_assert(PA_SINK_IS_LINKED(s->state));
1309
1310 ret = pa_idxset_size(s->inputs);
1311 pa_assert(ret >= s->n_corked);
1312
1313 /* Streams connected to our monitor source do not matter for
1314 * pa_sink_used_by()!.*/
1315
1316 return ret - s->n_corked;
1317 }
1318
1319 /* Called from main thread */
1320 unsigned pa_sink_check_suspend(pa_sink *s) {
1321 unsigned ret;
1322 pa_sink_input *i;
1323 uint32_t idx;
1324
1325 pa_sink_assert_ref(s);
1326
1327 if (!PA_SINK_IS_LINKED(s->state))
1328 return 0;
1329
1330 ret = 0;
1331
1332 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
1333 pa_sink_input_state_t st;
1334
1335 st = pa_sink_input_get_state(i);
1336 pa_assert(PA_SINK_INPUT_IS_LINKED(st));
1337
1338 if (st == PA_SINK_INPUT_CORKED)
1339 continue;
1340
1341 if (i->flags & PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND)
1342 continue;
1343
1344 ret ++;
1345 }
1346
1347 if (s->monitor_source)
1348 ret += pa_source_check_suspend(s->monitor_source);
1349
1350 return ret;
1351 }
1352
1353 /* Called from the IO thread */
1354 static void sync_input_volumes_within_thread(pa_sink *s) {
1355 pa_sink_input *i;
1356 void *state = NULL;
1357
1358 pa_sink_assert_ref(s);
1359
1360 while ((i = PA_SINK_INPUT(pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))) {
1361 if (pa_cvolume_equal(&i->thread_info.soft_volume, &i->soft_volume))
1362 continue;
1363
1364 i->thread_info.soft_volume = i->soft_volume;
1365 pa_sink_input_request_rewind(i, 0, TRUE, FALSE, FALSE);
1366 }
1367 }
1368
1369 /* Called from IO thread, except when it is not */
1370 int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
1371 pa_sink *s = PA_SINK(o);
1372 pa_sink_assert_ref(s);
1373
1374 switch ((pa_sink_message_t) code) {
1375
1376 case PA_SINK_MESSAGE_ADD_INPUT: {
1377 pa_sink_input *i = PA_SINK_INPUT(userdata);
1378
1379 /* If you change anything here, make sure to change the
1380 * sink input handling a few lines down at
1381 * PA_SINK_MESSAGE_FINISH_MOVE, too. */
1382
1383 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
1384
1385 /* Since the caller sleeps in pa_sink_input_put(), we can
1386 * safely access data outside of thread_info even though
1387 * it is mutable */
1388
1389 if ((i->thread_info.sync_prev = i->sync_prev)) {
1390 pa_assert(i->sink == i->thread_info.sync_prev->sink);
1391 pa_assert(i->sync_prev->sync_next == i);
1392 i->thread_info.sync_prev->thread_info.sync_next = i;
1393 }
1394
1395 if ((i->thread_info.sync_next = i->sync_next)) {
1396 pa_assert(i->sink == i->thread_info.sync_next->sink);
1397 pa_assert(i->sync_next->sync_prev == i);
1398 i->thread_info.sync_next->thread_info.sync_prev = i;
1399 }
1400
1401 pa_assert(!i->thread_info.attached);
1402 i->thread_info.attached = TRUE;
1403
1404 if (i->attach)
1405 i->attach(i);
1406
1407 pa_sink_input_set_state_within_thread(i, i->state);
1408
1409 /* The requested latency of the sink input needs to be
1410 * fixed up and then configured on the sink */
1411
1412 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1)
1413 pa_sink_input_set_requested_latency_within_thread(i, i->thread_info.requested_sink_latency);
1414
1415 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
1416 pa_sink_input_update_max_request(i, s->thread_info.max_request);
1417
1418 /* We don't rewind here automatically. This is left to the
1419 * sink input implementor because some sink inputs need a
1420 * slow start, i.e. need some time to buffer client
1421 * samples before beginning streaming. */
1422
1423 /* In flat volume mode we need to update the volume as
1424 * well */
1425 return o->process_msg(o, PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL);
1426 }
1427
1428 case PA_SINK_MESSAGE_REMOVE_INPUT: {
1429 pa_sink_input *i = PA_SINK_INPUT(userdata);
1430
1431 /* If you change anything here, make sure to change the
1432 * sink input handling a few lines down at
1433 * PA_SINK_MESSAGE_PREPAPRE_MOVE, too. */
1434
1435 if (i->detach)
1436 i->detach(i);
1437
1438 pa_sink_input_set_state_within_thread(i, i->state);
1439
1440 pa_assert(i->thread_info.attached);
1441 i->thread_info.attached = FALSE;
1442
1443 /* Since the caller sleeps in pa_sink_input_unlink(),
1444 * we can safely access data outside of thread_info even
1445 * though it is mutable */
1446
1447 pa_assert(!i->sync_prev);
1448 pa_assert(!i->sync_next);
1449
1450 if (i->thread_info.sync_prev) {
1451 i->thread_info.sync_prev->thread_info.sync_next = i->thread_info.sync_prev->sync_next;
1452 i->thread_info.sync_prev = NULL;
1453 }
1454
1455 if (i->thread_info.sync_next) {
1456 i->thread_info.sync_next->thread_info.sync_prev = i->thread_info.sync_next->sync_prev;
1457 i->thread_info.sync_next = NULL;
1458 }
1459
1460 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
1461 pa_sink_input_unref(i);
1462
1463 pa_sink_invalidate_requested_latency(s);
1464 pa_sink_request_rewind(s, (size_t) -1);
1465
1466 /* In flat volume mode we need to update the volume as
1467 * well */
1468 return o->process_msg(o, PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL);
1469 }
1470
1471 case PA_SINK_MESSAGE_START_MOVE: {
1472 pa_sink_input *i = PA_SINK_INPUT(userdata);
1473
1474 /* We don't support moving synchronized streams. */
1475 pa_assert(!i->sync_prev);
1476 pa_assert(!i->sync_next);
1477 pa_assert(!i->thread_info.sync_next);
1478 pa_assert(!i->thread_info.sync_prev);
1479
1480 if (i->thread_info.state != PA_SINK_INPUT_CORKED) {
1481 pa_usec_t usec = 0;
1482 size_t sink_nbytes, total_nbytes;
1483
1484 /* Get the latency of the sink */
1485 if (!(s->flags & PA_SINK_LATENCY) ||
1486 PA_MSGOBJECT(s)->process_msg(PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
1487 usec = 0;
1488
1489 sink_nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
1490 total_nbytes = sink_nbytes + pa_memblockq_get_length(i->thread_info.render_memblockq);
1491
1492 if (total_nbytes > 0) {
1493 i->thread_info.rewrite_nbytes = i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, total_nbytes) : total_nbytes;
1494 i->thread_info.rewrite_flush = TRUE;
1495 pa_sink_input_process_rewind(i, sink_nbytes);
1496 }
1497 }
1498
1499 if (i->detach)
1500 i->detach(i);
1501
1502 pa_assert(i->thread_info.attached);
1503 i->thread_info.attached = FALSE;
1504
1505 /* Let's remove the sink input ...*/
1506 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
1507 pa_sink_input_unref(i);
1508
1509 pa_sink_invalidate_requested_latency(s);
1510
1511 pa_log_debug("Requesting rewind due to started move");
1512 pa_sink_request_rewind(s, (size_t) -1);
1513
1514 /* In flat volume mode we need to update the volume as
1515 * well */
1516 return o->process_msg(o, PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL);
1517 }
1518
1519 case PA_SINK_MESSAGE_FINISH_MOVE: {
1520 pa_sink_input *i = PA_SINK_INPUT(userdata);
1521
1522 /* We don't support moving synchronized streams. */
1523 pa_assert(!i->sync_prev);
1524 pa_assert(!i->sync_next);
1525 pa_assert(!i->thread_info.sync_next);
1526 pa_assert(!i->thread_info.sync_prev);
1527
1528 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
1529
1530 pa_assert(!i->thread_info.attached);
1531 i->thread_info.attached = TRUE;
1532
1533 if (i->attach)
1534 i->attach(i);
1535
1536 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1)
1537 pa_sink_input_set_requested_latency_within_thread(i, i->thread_info.requested_sink_latency);
1538
1539 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
1540 pa_sink_input_update_max_request(i, s->thread_info.max_request);
1541
1542 if (i->thread_info.state != PA_SINK_INPUT_CORKED) {
1543 pa_usec_t usec = 0;
1544 size_t nbytes;
1545
1546 /* Get the latency of the sink */
1547 if (!(s->flags & PA_SINK_LATENCY) ||
1548 PA_MSGOBJECT(s)->process_msg(PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
1549 usec = 0;
1550
1551 nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
1552
1553 if (nbytes > 0)
1554 pa_sink_input_drop(i, nbytes);
1555
1556 pa_log_debug("Requesting rewind due to finished move");
1557 pa_sink_request_rewind(s, nbytes);
1558 }
1559
1560 /* In flat volume mode we need to update the volume as
1561 * well */
1562 return o->process_msg(o, PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL);
1563 }
1564
1565 case PA_SINK_MESSAGE_SET_VOLUME:
1566
1567 if (!pa_cvolume_equal(&s->thread_info.soft_volume, &s->soft_volume)) {
1568 s->thread_info.soft_volume = s->soft_volume;
1569 pa_sink_request_rewind(s, (size_t) -1);
1570 }
1571
1572 if (s->flags & PA_SINK_FLAT_VOLUME)
1573 sync_input_volumes_within_thread(s);
1574
1575 return 0;
1576
1577 case PA_SINK_MESSAGE_GET_VOLUME:
1578 return 0;
1579
1580 case PA_SINK_MESSAGE_SET_MUTE:
1581
1582 if (s->thread_info.soft_muted != s->muted) {
1583 s->thread_info.soft_muted = s->muted;
1584 pa_sink_request_rewind(s, (size_t) -1);
1585 }
1586
1587 return 0;
1588
1589 case PA_SINK_MESSAGE_GET_MUTE:
1590 return 0;
1591
1592 case PA_SINK_MESSAGE_SET_STATE: {
1593
1594 pa_bool_t suspend_change =
1595 (s->thread_info.state == PA_SINK_SUSPENDED && PA_SINK_IS_OPENED(PA_PTR_TO_UINT(userdata))) ||
1596 (PA_SINK_IS_OPENED(s->thread_info.state) && PA_PTR_TO_UINT(userdata) == PA_SINK_SUSPENDED);
1597
1598 s->thread_info.state = PA_PTR_TO_UINT(userdata);
1599
1600 if (s->thread_info.state == PA_SINK_SUSPENDED) {
1601 s->thread_info.rewind_nbytes = 0;
1602 s->thread_info.rewind_requested = FALSE;
1603 }
1604
1605 if (suspend_change) {
1606 pa_sink_input *i;
1607 void *state = NULL;
1608
1609 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1610 if (i->suspend_within_thread)
1611 i->suspend_within_thread(i, s->thread_info.state == PA_SINK_SUSPENDED);
1612 }
1613
1614 return 0;
1615 }
1616
1617 case PA_SINK_MESSAGE_DETACH:
1618
1619 /* Detach all streams */
1620 pa_sink_detach_within_thread(s);
1621 return 0;
1622
1623 case PA_SINK_MESSAGE_ATTACH:
1624
1625 /* Reattach all streams */
1626 pa_sink_attach_within_thread(s);
1627 return 0;
1628
1629 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY: {
1630
1631 pa_usec_t *usec = userdata;
1632 *usec = pa_sink_get_requested_latency_within_thread(s);
1633
1634 if (*usec == (pa_usec_t) -1)
1635 *usec = s->thread_info.max_latency;
1636
1637 return 0;
1638 }
1639
1640 case PA_SINK_MESSAGE_SET_LATENCY_RANGE: {
1641 pa_usec_t *r = userdata;
1642
1643 pa_sink_set_latency_range_within_thread(s, r[0], r[1]);
1644
1645 return 0;
1646 }
1647
1648 case PA_SINK_MESSAGE_GET_LATENCY_RANGE: {
1649 pa_usec_t *r = userdata;
1650
1651 r[0] = s->thread_info.min_latency;
1652 r[1] = s->thread_info.max_latency;
1653
1654 return 0;
1655 }
1656
1657 case PA_SINK_MESSAGE_GET_MAX_REWIND:
1658
1659 *((size_t*) userdata) = s->thread_info.max_rewind;
1660 return 0;
1661
1662 case PA_SINK_MESSAGE_GET_MAX_REQUEST:
1663
1664 *((size_t*) userdata) = s->thread_info.max_request;
1665 return 0;
1666
1667 case PA_SINK_MESSAGE_SET_MAX_REWIND:
1668
1669 pa_sink_set_max_rewind_within_thread(s, (size_t) offset);
1670 return 0;
1671
1672 case PA_SINK_MESSAGE_SET_MAX_REQUEST:
1673
1674 pa_sink_set_max_request_within_thread(s, (size_t) offset);
1675 return 0;
1676
1677 case PA_SINK_MESSAGE_GET_LATENCY:
1678 case PA_SINK_MESSAGE_MAX:
1679 ;
1680 }
1681
1682 return -1;
1683 }
1684
1685 /* Called from main thread */
1686 int pa_sink_suspend_all(pa_core *c, pa_bool_t suspend) {
1687 pa_sink *sink;
1688 uint32_t idx;
1689 int ret = 0;
1690
1691 pa_core_assert_ref(c);
1692
1693 for (sink = PA_SINK(pa_idxset_first(c->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(c->sinks, &idx))) {
1694 int r;
1695
1696 if ((r = pa_sink_suspend(sink, suspend)) < 0)
1697 ret = r;
1698 }
1699
1700 return ret;
1701 }
1702
1703 /* Called from main thread */
1704 void pa_sink_detach(pa_sink *s) {
1705 pa_sink_assert_ref(s);
1706 pa_assert(PA_SINK_IS_LINKED(s->state));
1707
1708 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_DETACH, NULL, 0, NULL) == 0);
1709 }
1710
1711 /* Called from main thread */
1712 void pa_sink_attach(pa_sink *s) {
1713 pa_sink_assert_ref(s);
1714 pa_assert(PA_SINK_IS_LINKED(s->state));
1715
1716 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_ATTACH, NULL, 0, NULL) == 0);
1717 }
1718
1719 /* Called from IO thread */
1720 void pa_sink_detach_within_thread(pa_sink *s) {
1721 pa_sink_input *i;
1722 void *state = NULL;
1723
1724 pa_sink_assert_ref(s);
1725 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1726
1727 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1728 if (i->detach)
1729 i->detach(i);
1730
1731 if (s->monitor_source)
1732 pa_source_detach_within_thread(s->monitor_source);
1733 }
1734
1735 /* Called from IO thread */
1736 void pa_sink_attach_within_thread(pa_sink *s) {
1737 pa_sink_input *i;
1738 void *state = NULL;
1739
1740 pa_sink_assert_ref(s);
1741 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1742
1743 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1744 if (i->attach)
1745 i->attach(i);
1746
1747 if (s->monitor_source)
1748 pa_source_attach_within_thread(s->monitor_source);
1749 }
1750
1751 /* Called from IO thread */
1752 void pa_sink_request_rewind(pa_sink*s, size_t nbytes) {
1753 pa_sink_assert_ref(s);
1754 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1755
1756 if (s->thread_info.state == PA_SINK_SUSPENDED)
1757 return;
1758
1759 if (nbytes == (size_t) -1)
1760 nbytes = s->thread_info.max_rewind;
1761
1762 nbytes = PA_MIN(nbytes, s->thread_info.max_rewind);
1763
1764 if (s->thread_info.rewind_requested &&
1765 nbytes <= s->thread_info.rewind_nbytes)
1766 return;
1767
1768 s->thread_info.rewind_nbytes = nbytes;
1769 s->thread_info.rewind_requested = TRUE;
1770
1771 if (s->request_rewind)
1772 s->request_rewind(s);
1773 }
1774
1775 /* Called from IO thread */
1776 pa_usec_t pa_sink_get_requested_latency_within_thread(pa_sink *s) {
1777 pa_usec_t result = (pa_usec_t) -1;
1778 pa_sink_input *i;
1779 void *state = NULL;
1780 pa_usec_t monitor_latency;
1781
1782 pa_sink_assert_ref(s);
1783
1784 if (s->thread_info.requested_latency_valid)
1785 return s->thread_info.requested_latency;
1786
1787 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1788
1789 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1 &&
1790 (result == (pa_usec_t) -1 || result > i->thread_info.requested_sink_latency))
1791 result = i->thread_info.requested_sink_latency;
1792
1793 monitor_latency = pa_source_get_requested_latency_within_thread(s->monitor_source);
1794
1795 if (monitor_latency != (pa_usec_t) -1 &&
1796 (result == (pa_usec_t) -1 || result > monitor_latency))
1797 result = monitor_latency;
1798
1799 if (result != (pa_usec_t) -1) {
1800 if (result > s->thread_info.max_latency)
1801 result = s->thread_info.max_latency;
1802
1803 if (result < s->thread_info.min_latency)
1804 result = s->thread_info.min_latency;
1805 }
1806
1807 s->thread_info.requested_latency = result;
1808 s->thread_info.requested_latency_valid = TRUE;
1809
1810 return result;
1811 }
1812
1813 /* Called from main thread */
1814 pa_usec_t pa_sink_get_requested_latency(pa_sink *s) {
1815 pa_usec_t usec = 0;
1816
1817 pa_sink_assert_ref(s);
1818 pa_assert(PA_SINK_IS_LINKED(s->state));
1819
1820 if (s->state == PA_SINK_SUSPENDED)
1821 return 0;
1822
1823 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
1824 return usec;
1825 }
1826
1827 /* Called from IO as well as the main thread -- the latter only before the IO thread started up */
1828 void pa_sink_set_max_rewind_within_thread(pa_sink *s, size_t max_rewind) {
1829 pa_sink_input *i;
1830 void *state = NULL;
1831
1832 pa_sink_assert_ref(s);
1833
1834 if (max_rewind == s->thread_info.max_rewind)
1835 return;
1836
1837 s->thread_info.max_rewind = max_rewind;
1838
1839 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
1840 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1841 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
1842 }
1843
1844 if (s->monitor_source)
1845 pa_source_set_max_rewind_within_thread(s->monitor_source, s->thread_info.max_rewind);
1846 }
1847
1848 /* Called from main thread */
1849 void pa_sink_set_max_rewind(pa_sink *s, size_t max_rewind) {
1850 pa_sink_assert_ref(s);
1851
1852 if (PA_SINK_IS_LINKED(s->state))
1853 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MAX_REWIND, NULL, max_rewind, NULL) == 0);
1854 else
1855 pa_sink_set_max_rewind_within_thread(s, max_rewind);
1856 }
1857
1858 /* Called from IO as well as the main thread -- the latter only before the IO thread started up */
1859 void pa_sink_set_max_request_within_thread(pa_sink *s, size_t max_request) {
1860 void *state = NULL;
1861
1862 pa_sink_assert_ref(s);
1863
1864 if (max_request == s->thread_info.max_request)
1865 return;
1866
1867 s->thread_info.max_request = max_request;
1868
1869 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
1870 pa_sink_input *i;
1871
1872 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1873 pa_sink_input_update_max_request(i, s->thread_info.max_request);
1874 }
1875 }
1876
1877 /* Called from main thread */
1878 void pa_sink_set_max_request(pa_sink *s, size_t max_request) {
1879 pa_sink_assert_ref(s);
1880
1881 if (PA_SINK_IS_LINKED(s->state))
1882 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MAX_REQUEST, NULL, max_request, NULL) == 0);
1883 else
1884 pa_sink_set_max_request_within_thread(s, max_request);
1885 }
1886
1887 /* Called from IO thread */
1888 void pa_sink_invalidate_requested_latency(pa_sink *s) {
1889 pa_sink_input *i;
1890 void *state = NULL;
1891
1892 pa_sink_assert_ref(s);
1893
1894 s->thread_info.requested_latency_valid = FALSE;
1895
1896 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
1897
1898 if (s->update_requested_latency)
1899 s->update_requested_latency(s);
1900
1901 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1902 if (i->update_sink_requested_latency)
1903 i->update_sink_requested_latency(i);
1904 }
1905 }
1906
1907 /* Called from main thread */
1908 void pa_sink_set_latency_range(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency) {
1909 pa_sink_assert_ref(s);
1910
1911 /* min_latency == 0: no limit
1912 * min_latency anything else: specified limit
1913 *
1914 * Similar for max_latency */
1915
1916 if (min_latency < ABSOLUTE_MIN_LATENCY)
1917 min_latency = ABSOLUTE_MIN_LATENCY;
1918
1919 if (max_latency <= 0 ||
1920 max_latency > ABSOLUTE_MAX_LATENCY)
1921 max_latency = ABSOLUTE_MAX_LATENCY;
1922
1923 pa_assert(min_latency <= max_latency);
1924
1925 /* Hmm, let's see if someone forgot to set PA_SINK_DYNAMIC_LATENCY here... */
1926 pa_assert((min_latency == ABSOLUTE_MIN_LATENCY &&
1927 max_latency == ABSOLUTE_MAX_LATENCY) ||
1928 (s->flags & PA_SINK_DYNAMIC_LATENCY));
1929
1930 if (PA_SINK_IS_LINKED(s->state)) {
1931 pa_usec_t r[2];
1932
1933 r[0] = min_latency;
1934 r[1] = max_latency;
1935
1936 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_LATENCY_RANGE, r, 0, NULL) == 0);
1937 } else
1938 pa_sink_set_latency_range_within_thread(s, min_latency, max_latency);
1939 }
1940
1941 /* Called from main thread */
1942 void pa_sink_get_latency_range(pa_sink *s, pa_usec_t *min_latency, pa_usec_t *max_latency) {
1943 pa_sink_assert_ref(s);
1944 pa_assert(min_latency);
1945 pa_assert(max_latency);
1946
1947 if (PA_SINK_IS_LINKED(s->state)) {
1948 pa_usec_t r[2] = { 0, 0 };
1949
1950 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY_RANGE, r, 0, NULL) == 0);
1951
1952 *min_latency = r[0];
1953 *max_latency = r[1];
1954 } else {
1955 *min_latency = s->thread_info.min_latency;
1956 *max_latency = s->thread_info.max_latency;
1957 }
1958 }
1959
1960 /* Called from IO thread */
1961 void pa_sink_set_latency_range_within_thread(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency) {
1962 void *state = NULL;
1963
1964 pa_sink_assert_ref(s);
1965
1966 pa_assert(min_latency >= ABSOLUTE_MIN_LATENCY);
1967 pa_assert(max_latency <= ABSOLUTE_MAX_LATENCY);
1968 pa_assert(min_latency <= max_latency);
1969
1970 /* Hmm, let's see if someone forgot to set PA_SINK_DYNAMIC_LATENCY here... */
1971 pa_assert((min_latency == ABSOLUTE_MIN_LATENCY &&
1972 max_latency == ABSOLUTE_MAX_LATENCY) ||
1973 (s->flags & PA_SINK_DYNAMIC_LATENCY));
1974
1975 s->thread_info.min_latency = min_latency;
1976 s->thread_info.max_latency = max_latency;
1977
1978 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
1979 pa_sink_input *i;
1980
1981 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1982 if (i->update_sink_latency_range)
1983 i->update_sink_latency_range(i);
1984 }
1985
1986 pa_sink_invalidate_requested_latency(s);
1987
1988 pa_source_set_latency_range_within_thread(s->monitor_source, min_latency, max_latency);
1989 }
1990
1991 /* Called from main context */
1992 size_t pa_sink_get_max_rewind(pa_sink *s) {
1993 size_t r;
1994 pa_sink_assert_ref(s);
1995
1996 if (!PA_SINK_IS_LINKED(s->state))
1997 return s->thread_info.max_rewind;
1998
1999 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MAX_REWIND, &r, 0, NULL) == 0);
2000
2001 return r;
2002 }
2003
2004 /* Called from main context */
2005 size_t pa_sink_get_max_request(pa_sink *s) {
2006 size_t r;
2007 pa_sink_assert_ref(s);
2008
2009 if (!PA_SINK_IS_LINKED(s->state))
2010 return s->thread_info.max_request;
2011
2012 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MAX_REQUEST, &r, 0, NULL) == 0);
2013
2014 return r;
2015 }
2016
2017 /* Called from main context */
2018 pa_bool_t pa_device_init_icon(pa_proplist *p, pa_bool_t is_sink) {
2019 const char *ff, *c, *t = NULL, *s = "", *profile, *bus;
2020
2021 pa_assert(p);
2022
2023 if (pa_proplist_contains(p, PA_PROP_DEVICE_ICON_NAME))
2024 return TRUE;
2025
2026 if ((ff = pa_proplist_gets(p, PA_PROP_DEVICE_FORM_FACTOR))) {
2027
2028 if (pa_streq(ff, "microphone"))
2029 t = "audio-input-microphone";
2030 else if (pa_streq(ff, "webcam"))
2031 t = "camera-web";
2032 else if (pa_streq(ff, "computer"))
2033 t = "computer";
2034 else if (pa_streq(ff, "handset"))
2035 t = "phone";
2036 else if (pa_streq(ff, "portable"))
2037 t = "multimedia-player";
2038 else if (pa_streq(ff, "tv"))
2039 t = "video-display";
2040 }
2041
2042 if (!t)
2043 if ((c = pa_proplist_gets(p, PA_PROP_DEVICE_CLASS)))
2044 if (pa_streq(c, "modem"))
2045 t = "modem";
2046
2047 if (!t) {
2048 if (is_sink)
2049 t = "audio-card";
2050 else
2051 t = "audio-input-microphone";
2052 }
2053
2054 if ((profile = pa_proplist_gets(p, PA_PROP_DEVICE_PROFILE_NAME))) {
2055 if (strstr(profile, "analog"))
2056 s = "-analog";
2057 else if (strstr(profile, "iec958"))
2058 s = "-iec958";
2059 else if (strstr(profile, "hdmi"))
2060 s = "-hdmi";
2061 }
2062
2063 bus = pa_proplist_gets(p, PA_PROP_DEVICE_BUS);
2064
2065 pa_proplist_setf(p, PA_PROP_DEVICE_ICON_NAME, "%s%s%s%s", t, pa_strempty(s), bus ? "-" : "", pa_strempty(bus));
2066
2067 return TRUE;
2068 }
2069
2070 pa_bool_t pa_device_init_description(pa_proplist *p) {
2071 const char *s;
2072 pa_assert(p);
2073
2074 if (pa_proplist_contains(p, PA_PROP_DEVICE_DESCRIPTION))
2075 return TRUE;
2076
2077 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_FORM_FACTOR)))
2078 if (pa_streq(s, "internal")) {
2079 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, _("Internal Audio"));
2080 return TRUE;
2081 }
2082
2083 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_CLASS)))
2084 if (pa_streq(s, "modem")) {
2085 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, _("Modem"));
2086 return TRUE;
2087 }
2088
2089 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_PRODUCT_NAME))) {
2090 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, s);
2091 return TRUE;
2092 }
2093
2094 return FALSE;
2095 }