]> code.delx.au - pulseaudio/blob - src/pulsecore/sink.c
core: memory leak, fix ref counting when moving streams
[pulseaudio] / src / pulsecore / sink.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <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 pa_sink_input_ref(i);
519
520 if (pa_sink_input_start_move(i) >= 0)
521 pa_queue_push(q, i);
522 else
523 pa_sink_input_unref(i);
524 }
525
526 return q;
527 }
528
529 /* Called from main context */
530 void pa_sink_move_all_finish(pa_sink *s, pa_queue *q, pa_bool_t save) {
531 pa_sink_input *i;
532
533 pa_sink_assert_ref(s);
534 pa_assert(PA_SINK_IS_LINKED(s->state));
535 pa_assert(q);
536
537 while ((i = PA_SINK_INPUT(pa_queue_pop(q)))) {
538 if (pa_sink_input_finish_move(i, s, save) < 0)
539 pa_sink_input_kill(i);
540
541 pa_sink_input_unref(i);
542 }
543
544 pa_queue_free(q, NULL, NULL);
545 }
546
547 /* Called from main context */
548 void pa_sink_move_all_fail(pa_queue *q) {
549 pa_sink_input *i;
550 pa_assert(q);
551
552 while ((i = PA_SINK_INPUT(pa_queue_pop(q)))) {
553 if (pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FAIL], i) == PA_HOOK_OK) {
554 pa_sink_input_kill(i);
555 pa_sink_input_unref(i);
556 }
557 }
558
559 pa_queue_free(q, NULL, NULL);
560 }
561
562 /* Called from IO thread context */
563 void pa_sink_process_rewind(pa_sink *s, size_t nbytes) {
564 pa_sink_input *i;
565 void *state = NULL;
566 pa_sink_assert_ref(s);
567 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
568
569 /* If nobody requested this and this is actually no real rewind
570 * then we can short cut this */
571 if (!s->thread_info.rewind_requested && nbytes <= 0)
572 return;
573
574 s->thread_info.rewind_nbytes = 0;
575 s->thread_info.rewind_requested = FALSE;
576
577 if (s->thread_info.state == PA_SINK_SUSPENDED)
578 return;
579
580 if (nbytes > 0)
581 pa_log_debug("Processing rewind...");
582
583 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
584 pa_sink_input_assert_ref(i);
585 pa_sink_input_process_rewind(i, nbytes);
586 }
587
588 if (nbytes > 0)
589 if (s->monitor_source && PA_SOURCE_IS_LINKED(s->monitor_source->thread_info.state))
590 pa_source_process_rewind(s->monitor_source, nbytes);
591 }
592
593 /* Called from IO thread context */
594 static unsigned fill_mix_info(pa_sink *s, size_t *length, pa_mix_info *info, unsigned maxinfo) {
595 pa_sink_input *i;
596 unsigned n = 0;
597 void *state = NULL;
598 size_t mixlength = *length;
599
600 pa_sink_assert_ref(s);
601 pa_assert(info);
602
603 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)) && maxinfo > 0) {
604 pa_sink_input_assert_ref(i);
605
606 pa_sink_input_peek(i, *length, &info->chunk, &info->volume);
607
608 if (mixlength == 0 || info->chunk.length < mixlength)
609 mixlength = info->chunk.length;
610
611 if (pa_memblock_is_silence(info->chunk.memblock)) {
612 pa_memblock_unref(info->chunk.memblock);
613 continue;
614 }
615
616 info->userdata = pa_sink_input_ref(i);
617
618 pa_assert(info->chunk.memblock);
619 pa_assert(info->chunk.length > 0);
620
621 info++;
622 n++;
623 maxinfo--;
624 }
625
626 if (mixlength > 0)
627 *length = mixlength;
628
629 return n;
630 }
631
632 /* Called from IO thread context */
633 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned n, pa_memchunk *result) {
634 pa_sink_input *i;
635 void *state = NULL;
636 unsigned p = 0;
637 unsigned n_unreffed = 0;
638
639 pa_sink_assert_ref(s);
640 pa_assert(result);
641 pa_assert(result->memblock);
642 pa_assert(result->length > 0);
643
644 /* We optimize for the case where the order of the inputs has not changed */
645
646 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
647 unsigned j;
648 pa_mix_info* m = NULL;
649
650 pa_sink_input_assert_ref(i);
651
652 /* Let's try to find the matching entry info the pa_mix_info array */
653 for (j = 0; j < n; j ++) {
654
655 if (info[p].userdata == i) {
656 m = info + p;
657 break;
658 }
659
660 p++;
661 if (p >= n)
662 p = 0;
663 }
664
665 /* Drop read data */
666 pa_sink_input_drop(i, result->length);
667
668 if (s->monitor_source && PA_SOURCE_IS_LINKED(s->monitor_source->thread_info.state)) {
669
670 if (pa_hashmap_size(i->thread_info.direct_outputs) > 0) {
671 void *ostate = NULL;
672 pa_source_output *o;
673 pa_memchunk c;
674
675 if (m && m->chunk.memblock) {
676 c = m->chunk;
677 pa_memblock_ref(c.memblock);
678 pa_assert(result->length <= c.length);
679 c.length = result->length;
680
681 pa_memchunk_make_writable(&c, 0);
682 pa_volume_memchunk(&c, &s->sample_spec, &m->volume);
683 } else {
684 c = s->silence;
685 pa_memblock_ref(c.memblock);
686 pa_assert(result->length <= c.length);
687 c.length = result->length;
688 }
689
690 while ((o = pa_hashmap_iterate(i->thread_info.direct_outputs, &ostate, NULL))) {
691 pa_source_output_assert_ref(o);
692 pa_assert(o->direct_on_input == i);
693 pa_source_post_direct(s->monitor_source, o, &c);
694 }
695
696 pa_memblock_unref(c.memblock);
697 }
698 }
699
700 if (m) {
701 if (m->chunk.memblock)
702 pa_memblock_unref(m->chunk.memblock);
703 pa_memchunk_reset(&m->chunk);
704
705 pa_sink_input_unref(m->userdata);
706 m->userdata = NULL;
707
708 n_unreffed += 1;
709 }
710 }
711
712 /* Now drop references to entries that are included in the
713 * pa_mix_info array but don't exist anymore */
714
715 if (n_unreffed < n) {
716 for (; n > 0; info++, n--) {
717 if (info->userdata)
718 pa_sink_input_unref(info->userdata);
719 if (info->chunk.memblock)
720 pa_memblock_unref(info->chunk.memblock);
721 }
722 }
723
724 if (s->monitor_source && PA_SOURCE_IS_LINKED(s->monitor_source->thread_info.state))
725 pa_source_post(s->monitor_source, result);
726 }
727
728 /* Called from IO thread context */
729 void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
730 pa_mix_info info[MAX_MIX_CHANNELS];
731 unsigned n;
732 size_t block_size_max;
733
734 pa_sink_assert_ref(s);
735 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
736 pa_assert(pa_frame_aligned(length, &s->sample_spec));
737 pa_assert(result);
738
739 pa_sink_ref(s);
740
741 pa_assert(!s->thread_info.rewind_requested);
742 pa_assert(s->thread_info.rewind_nbytes == 0);
743
744 if (s->thread_info.state == PA_SINK_SUSPENDED) {
745 result->memblock = pa_memblock_ref(s->silence.memblock);
746 result->index = s->silence.index;
747 result->length = PA_MIN(s->silence.length, length);
748 return;
749 }
750
751 if (length <= 0)
752 length = pa_frame_align(MIX_BUFFER_LENGTH, &s->sample_spec);
753
754 block_size_max = pa_mempool_block_size_max(s->core->mempool);
755 if (length > block_size_max)
756 length = pa_frame_align(block_size_max, &s->sample_spec);
757
758 pa_assert(length > 0);
759
760 n = fill_mix_info(s, &length, info, MAX_MIX_CHANNELS);
761
762 if (n == 0) {
763
764 *result = s->silence;
765 pa_memblock_ref(result->memblock);
766
767 if (result->length > length)
768 result->length = length;
769
770 } else if (n == 1) {
771 pa_cvolume volume;
772
773 *result = info[0].chunk;
774 pa_memblock_ref(result->memblock);
775
776 if (result->length > length)
777 result->length = length;
778
779 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
780
781 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&volume)) {
782 pa_memchunk_make_writable(result, 0);
783 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
784 pa_silence_memchunk(result, &s->sample_spec);
785 else
786 pa_volume_memchunk(result, &s->sample_spec, &volume);
787 }
788 } else {
789 void *ptr;
790 result->memblock = pa_memblock_new(s->core->mempool, length);
791
792 ptr = pa_memblock_acquire(result->memblock);
793 result->length = pa_mix(info, n,
794 ptr, length,
795 &s->sample_spec,
796 &s->thread_info.soft_volume,
797 s->thread_info.soft_muted);
798 pa_memblock_release(result->memblock);
799
800 result->index = 0;
801 }
802
803 inputs_drop(s, info, n, result);
804
805 pa_sink_unref(s);
806 }
807
808 /* Called from IO thread context */
809 void pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
810 pa_mix_info info[MAX_MIX_CHANNELS];
811 unsigned n;
812 size_t length, block_size_max;
813
814 pa_sink_assert_ref(s);
815 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
816 pa_assert(target);
817 pa_assert(target->memblock);
818 pa_assert(target->length > 0);
819 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
820
821 pa_sink_ref(s);
822
823 pa_assert(!s->thread_info.rewind_requested);
824 pa_assert(s->thread_info.rewind_nbytes == 0);
825
826 if (s->thread_info.state == PA_SINK_SUSPENDED) {
827 pa_silence_memchunk(target, &s->sample_spec);
828 return;
829 }
830
831 length = target->length;
832 block_size_max = pa_mempool_block_size_max(s->core->mempool);
833 if (length > block_size_max)
834 length = pa_frame_align(block_size_max, &s->sample_spec);
835
836 pa_assert(length > 0);
837
838 n = fill_mix_info(s, &length, info, MAX_MIX_CHANNELS);
839
840 if (n == 0) {
841 if (target->length > length)
842 target->length = length;
843
844 pa_silence_memchunk(target, &s->sample_spec);
845 } else if (n == 1) {
846 pa_cvolume volume;
847
848 if (target->length > length)
849 target->length = length;
850
851 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
852
853 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
854 pa_silence_memchunk(target, &s->sample_spec);
855 else {
856 pa_memchunk vchunk;
857
858 vchunk = info[0].chunk;
859 pa_memblock_ref(vchunk.memblock);
860
861 if (vchunk.length > length)
862 vchunk.length = length;
863
864 if (!pa_cvolume_is_norm(&volume)) {
865 pa_memchunk_make_writable(&vchunk, 0);
866 pa_volume_memchunk(&vchunk, &s->sample_spec, &volume);
867 }
868
869 pa_memchunk_memcpy(target, &vchunk);
870 pa_memblock_unref(vchunk.memblock);
871 }
872
873 } else {
874 void *ptr;
875
876 ptr = pa_memblock_acquire(target->memblock);
877
878 target->length = pa_mix(info, n,
879 (uint8_t*) ptr + target->index, length,
880 &s->sample_spec,
881 &s->thread_info.soft_volume,
882 s->thread_info.soft_muted);
883
884 pa_memblock_release(target->memblock);
885 }
886
887 inputs_drop(s, info, n, target);
888
889 pa_sink_unref(s);
890 }
891
892 /* Called from IO thread context */
893 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
894 pa_memchunk chunk;
895 size_t l, d;
896
897 pa_sink_assert_ref(s);
898 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
899 pa_assert(target);
900 pa_assert(target->memblock);
901 pa_assert(target->length > 0);
902 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
903
904 pa_sink_ref(s);
905
906 pa_assert(!s->thread_info.rewind_requested);
907 pa_assert(s->thread_info.rewind_nbytes == 0);
908
909 l = target->length;
910 d = 0;
911 while (l > 0) {
912 chunk = *target;
913 chunk.index += d;
914 chunk.length -= d;
915
916 pa_sink_render_into(s, &chunk);
917
918 d += chunk.length;
919 l -= chunk.length;
920 }
921
922 pa_sink_unref(s);
923 }
924
925 /* Called from IO thread context */
926 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
927 pa_sink_assert_ref(s);
928 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
929 pa_assert(length > 0);
930 pa_assert(pa_frame_aligned(length, &s->sample_spec));
931 pa_assert(result);
932
933 pa_assert(!s->thread_info.rewind_requested);
934 pa_assert(s->thread_info.rewind_nbytes == 0);
935
936 /*** This needs optimization ***/
937
938 result->index = 0;
939 result->length = length;
940 result->memblock = pa_memblock_new(s->core->mempool, length);
941
942 pa_sink_render_into_full(s, result);
943 }
944
945 /* Called from main thread */
946 pa_usec_t pa_sink_get_latency(pa_sink *s) {
947 pa_usec_t usec = 0;
948
949 pa_sink_assert_ref(s);
950 pa_assert(PA_SINK_IS_LINKED(s->state));
951
952 /* The returned value is supposed to be in the time domain of the sound card! */
953
954 if (s->state == PA_SINK_SUSPENDED)
955 return 0;
956
957 if (!(s->flags & PA_SINK_LATENCY))
958 return 0;
959
960 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) == 0);
961
962 return usec;
963 }
964
965 /* Called from IO thread */
966 pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s) {
967 pa_usec_t usec = 0;
968 pa_msgobject *o;
969
970 pa_sink_assert_ref(s);
971 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
972
973 /* The returned value is supposed to be in the time domain of the sound card! */
974
975 if (s->thread_info.state == PA_SINK_SUSPENDED)
976 return 0;
977
978 if (!(s->flags & PA_SINK_LATENCY))
979 return 0;
980
981 o = PA_MSGOBJECT(s);
982
983 /* We probably should make this a proper vtable callback instead of going through process_msg() */
984
985 if (o->process_msg(o, PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
986 return -1;
987
988 return usec;
989 }
990
991 static void compute_new_soft_volume(pa_sink_input *i, const pa_cvolume *new_volume) {
992 unsigned c;
993
994 pa_sink_input_assert_ref(i);
995 pa_assert(new_volume->channels == i->sample_spec.channels);
996
997 /*
998 * This basically calculates:
999 *
1000 * i->relative_volume := i->virtual_volume / new_volume
1001 * i->soft_volume := i->relative_volume * i->volume_factor
1002 */
1003
1004 /* The new sink volume passed in here must already be remapped to
1005 * the sink input's channel map! */
1006
1007 i->soft_volume.channels = i->sample_spec.channels;
1008
1009 for (c = 0; c < i->sample_spec.channels; c++)
1010
1011 if (new_volume->values[c] <= PA_VOLUME_MUTED)
1012 /* We leave i->relative_volume untouched */
1013 i->soft_volume.values[c] = PA_VOLUME_MUTED;
1014 else {
1015 i->relative_volume[c] =
1016 pa_sw_volume_to_linear(i->virtual_volume.values[c]) /
1017 pa_sw_volume_to_linear(new_volume->values[c]);
1018
1019 i->soft_volume.values[c] = pa_sw_volume_from_linear(
1020 i->relative_volume[c] *
1021 pa_sw_volume_to_linear(i->volume_factor.values[c]));
1022 }
1023
1024 /* Hooks have the ability to play games with i->soft_volume */
1025 pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_SET_VOLUME], i);
1026
1027 /* We don't copy the soft_volume to the thread_info data
1028 * here. That must be done by the caller */
1029 }
1030
1031 /* Called from main thread */
1032 void pa_sink_update_flat_volume(pa_sink *s, pa_cvolume *new_volume) {
1033 pa_sink_input *i;
1034 uint32_t idx;
1035
1036 pa_sink_assert_ref(s);
1037 pa_assert(new_volume);
1038 pa_assert(PA_SINK_IS_LINKED(s->state));
1039 pa_assert(s->flags & PA_SINK_FLAT_VOLUME);
1040
1041 /* This is called whenever a sink input volume changes and we
1042 * might need to fix up the sink volume accordingly. Please note
1043 * that we don't actually update the sinks volume here, we only
1044 * return how it needs to be updated. The caller should then call
1045 * pa_sink_set_volume().*/
1046
1047 if (pa_idxset_isempty(s->inputs)) {
1048 /* In the special case that we have no sink input we leave the
1049 * volume unmodified. */
1050 *new_volume = s->virtual_volume;
1051 return;
1052 }
1053
1054 pa_cvolume_mute(new_volume, s->channel_map.channels);
1055
1056 /* First let's determine the new maximum volume of all inputs
1057 * connected to this sink */
1058 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
1059 unsigned c;
1060 pa_cvolume remapped_volume;
1061
1062 remapped_volume = i->virtual_volume;
1063 pa_cvolume_remap(&remapped_volume, &i->channel_map, &s->channel_map);
1064
1065 for (c = 0; c < new_volume->channels; c++)
1066 if (remapped_volume.values[c] > new_volume->values[c])
1067 new_volume->values[c] = remapped_volume.values[c];
1068 }
1069
1070 /* Then, let's update the soft volumes of all inputs connected
1071 * to this sink */
1072 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
1073 pa_cvolume remapped_new_volume;
1074
1075 remapped_new_volume = *new_volume;
1076 pa_cvolume_remap(&remapped_new_volume, &s->channel_map, &i->channel_map);
1077 compute_new_soft_volume(i, &remapped_new_volume);
1078
1079 /* We don't copy soft_volume to the thread_info data here
1080 * (i.e. issue PA_SINK_INPUT_MESSAGE_SET_VOLUME) because we
1081 * want the update to be atomically with the sink volume
1082 * update, hence we do it within the pa_sink_set_volume() call
1083 * below */
1084 }
1085 }
1086
1087 /* Called from main thread */
1088 void pa_sink_propagate_flat_volume(pa_sink *s) {
1089 pa_sink_input *i;
1090 uint32_t idx;
1091
1092 pa_sink_assert_ref(s);
1093 pa_assert(PA_SINK_IS_LINKED(s->state));
1094 pa_assert(s->flags & PA_SINK_FLAT_VOLUME);
1095
1096 /* This is called whenever the sink volume changes that is not
1097 * caused by a sink input volume change. We need to fix up the
1098 * sink input volumes accordingly */
1099
1100 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
1101 pa_cvolume sink_volume, new_virtual_volume;
1102 unsigned c;
1103
1104 /* This basically calculates i->virtual_volume := i->relative_volume * s->virtual_volume */
1105
1106 sink_volume = s->virtual_volume;
1107 pa_cvolume_remap(&sink_volume, &s->channel_map, &i->channel_map);
1108
1109 for (c = 0; c < i->sample_spec.channels; c++)
1110 new_virtual_volume.values[c] = pa_sw_volume_from_linear(
1111 i->relative_volume[c] *
1112 pa_sw_volume_to_linear(sink_volume.values[c]));
1113
1114 new_virtual_volume.channels = i->sample_spec.channels;
1115
1116 if (!pa_cvolume_equal(&new_virtual_volume, &i->virtual_volume)) {
1117 i->virtual_volume = new_virtual_volume;
1118
1119 /* Hmm, the soft volume might no longer actually match
1120 * what has been chosen as new virtual volume here,
1121 * especially when the old volume was
1122 * PA_VOLUME_MUTED. Hence let's recalculate the soft
1123 * volumes here. */
1124 compute_new_soft_volume(i, &sink_volume);
1125
1126 /* The virtual volume changed, let's tell people so */
1127 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1128 }
1129 }
1130
1131 /* If the soft_volume of any of the sink inputs got changed, let's
1132 * make sure the thread copies are synced up. */
1133 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SYNC_VOLUMES, NULL, 0, NULL) == 0);
1134 }
1135
1136 /* Called from main thread */
1137 void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume, pa_bool_t propagate, pa_bool_t sendmsg) {
1138 pa_bool_t virtual_volume_changed;
1139
1140 pa_sink_assert_ref(s);
1141 pa_assert(PA_SINK_IS_LINKED(s->state));
1142 pa_assert(volume);
1143 pa_assert(pa_cvolume_valid(volume));
1144 pa_assert(pa_cvolume_compatible(volume, &s->sample_spec));
1145
1146 virtual_volume_changed = !pa_cvolume_equal(volume, &s->virtual_volume);
1147 s->virtual_volume = *volume;
1148
1149 /* Propagate this volume change back to the inputs */
1150 if (virtual_volume_changed)
1151 if (propagate && (s->flags & PA_SINK_FLAT_VOLUME))
1152 pa_sink_propagate_flat_volume(s);
1153
1154 if (s->set_volume) {
1155 /* If we have a function set_volume(), then we do not apply a
1156 * soft volume by default. However, set_volume() is apply one
1157 * to s->soft_volume */
1158
1159 pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
1160 s->set_volume(s);
1161
1162 } else
1163 /* If we have no function set_volume(), then the soft volume
1164 * becomes the virtual volume */
1165 s->soft_volume = s->virtual_volume;
1166
1167 /* This tells the sink that soft and/or virtual volume changed */
1168 if (sendmsg)
1169 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
1170
1171 if (virtual_volume_changed)
1172 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1173 }
1174
1175 /* Called from main thread. Only to be called by sink implementor */
1176 void pa_sink_set_soft_volume(pa_sink *s, const pa_cvolume *volume) {
1177 pa_sink_assert_ref(s);
1178 pa_assert(volume);
1179
1180 s->soft_volume = *volume;
1181
1182 if (PA_SINK_IS_LINKED(s->state))
1183 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
1184 else
1185 s->thread_info.soft_volume = *volume;
1186 }
1187
1188 /* Called from main thread */
1189 const pa_cvolume *pa_sink_get_volume(pa_sink *s, pa_bool_t force_refresh) {
1190 pa_sink_assert_ref(s);
1191
1192 if (s->refresh_volume || force_refresh) {
1193 struct pa_cvolume old_virtual_volume = s->virtual_volume;
1194
1195 if (s->get_volume)
1196 s->get_volume(s);
1197
1198 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, NULL, 0, NULL) == 0);
1199
1200 if (!pa_cvolume_equal(&old_virtual_volume, &s->virtual_volume)) {
1201
1202 if (s->flags & PA_SINK_FLAT_VOLUME)
1203 pa_sink_propagate_flat_volume(s);
1204
1205 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1206 }
1207 }
1208
1209 return &s->virtual_volume;
1210 }
1211
1212 /* Called from main thread */
1213 void pa_sink_volume_changed(pa_sink *s, const pa_cvolume *new_volume) {
1214 pa_sink_assert_ref(s);
1215
1216 /* The sink implementor may call this if the volume changed to make sure everyone is notified */
1217
1218 if (pa_cvolume_equal(&s->virtual_volume, new_volume))
1219 return;
1220
1221 s->virtual_volume = *new_volume;
1222 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1223 }
1224
1225 /* Called from main thread */
1226 void pa_sink_set_mute(pa_sink *s, pa_bool_t mute) {
1227 pa_bool_t old_muted;
1228
1229 pa_sink_assert_ref(s);
1230 pa_assert(PA_SINK_IS_LINKED(s->state));
1231
1232 old_muted = s->muted;
1233 s->muted = mute;
1234
1235 if (s->set_mute)
1236 s->set_mute(s);
1237
1238 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, NULL, 0, NULL) == 0);
1239
1240 if (old_muted != s->muted)
1241 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1242 }
1243
1244 /* Called from main thread */
1245 pa_bool_t pa_sink_get_mute(pa_sink *s, pa_bool_t force_refresh) {
1246
1247 pa_sink_assert_ref(s);
1248
1249 if (s->refresh_muted || force_refresh) {
1250 pa_bool_t old_muted = s->muted;
1251
1252 if (s->get_mute)
1253 s->get_mute(s);
1254
1255 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MUTE, NULL, 0, NULL) == 0);
1256
1257 if (old_muted != s->muted)
1258 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1259 }
1260
1261 return s->muted;
1262 }
1263
1264 /* Called from main thread */
1265 void pa_sink_mute_changed(pa_sink *s, pa_bool_t new_muted) {
1266 pa_sink_assert_ref(s);
1267
1268 /* The sink implementor may call this if the volume changed to make sure everyone is notified */
1269
1270 if (s->muted == new_muted)
1271 return;
1272
1273 s->muted = new_muted;
1274 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1275 }
1276
1277 /* Called from main thread */
1278 pa_bool_t pa_sink_update_proplist(pa_sink *s, pa_update_mode_t mode, pa_proplist *p) {
1279 pa_sink_assert_ref(s);
1280
1281 if (p)
1282 pa_proplist_update(s->proplist, mode, p);
1283
1284 if (PA_SINK_IS_LINKED(s->state)) {
1285 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], s);
1286 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1287 }
1288
1289 return TRUE;
1290 }
1291
1292 /* Called from main thread */
1293 void pa_sink_set_description(pa_sink *s, const char *description) {
1294 const char *old;
1295 pa_sink_assert_ref(s);
1296
1297 if (!description && !pa_proplist_contains(s->proplist, PA_PROP_DEVICE_DESCRIPTION))
1298 return;
1299
1300 old = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
1301
1302 if (old && description && !strcmp(old, description))
1303 return;
1304
1305 if (description)
1306 pa_proplist_sets(s->proplist, PA_PROP_DEVICE_DESCRIPTION, description);
1307 else
1308 pa_proplist_unset(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
1309
1310 if (s->monitor_source) {
1311 char *n;
1312
1313 n = pa_sprintf_malloc("Monitor Source of %s", description ? description : s->name);
1314 pa_source_set_description(s->monitor_source, n);
1315 pa_xfree(n);
1316 }
1317
1318 if (PA_SINK_IS_LINKED(s->state)) {
1319 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
1320 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], s);
1321 }
1322 }
1323
1324 /* Called from main thread */
1325 unsigned pa_sink_linked_by(pa_sink *s) {
1326 unsigned ret;
1327
1328 pa_sink_assert_ref(s);
1329 pa_assert(PA_SINK_IS_LINKED(s->state));
1330
1331 ret = pa_idxset_size(s->inputs);
1332
1333 /* We add in the number of streams connected to us here. Please
1334 * note the asymmmetry to pa_sink_used_by()! */
1335
1336 if (s->monitor_source)
1337 ret += pa_source_linked_by(s->monitor_source);
1338
1339 return ret;
1340 }
1341
1342 /* Called from main thread */
1343 unsigned pa_sink_used_by(pa_sink *s) {
1344 unsigned ret;
1345
1346 pa_sink_assert_ref(s);
1347 pa_assert(PA_SINK_IS_LINKED(s->state));
1348
1349 ret = pa_idxset_size(s->inputs);
1350 pa_assert(ret >= s->n_corked);
1351
1352 /* Streams connected to our monitor source do not matter for
1353 * pa_sink_used_by()!.*/
1354
1355 return ret - s->n_corked;
1356 }
1357
1358 /* Called from main thread */
1359 unsigned pa_sink_check_suspend(pa_sink *s) {
1360 unsigned ret;
1361 pa_sink_input *i;
1362 uint32_t idx;
1363
1364 pa_sink_assert_ref(s);
1365
1366 if (!PA_SINK_IS_LINKED(s->state))
1367 return 0;
1368
1369 ret = 0;
1370
1371 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
1372 pa_sink_input_state_t st;
1373
1374 st = pa_sink_input_get_state(i);
1375 pa_assert(PA_SINK_INPUT_IS_LINKED(st));
1376
1377 if (st == PA_SINK_INPUT_CORKED)
1378 continue;
1379
1380 if (i->flags & PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND)
1381 continue;
1382
1383 ret ++;
1384 }
1385
1386 if (s->monitor_source)
1387 ret += pa_source_check_suspend(s->monitor_source);
1388
1389 return ret;
1390 }
1391
1392 /* Called from the IO thread */
1393 static void sync_input_volumes_within_thread(pa_sink *s) {
1394 pa_sink_input *i;
1395 void *state = NULL;
1396
1397 pa_sink_assert_ref(s);
1398
1399 while ((i = PA_SINK_INPUT(pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))) {
1400 if (pa_cvolume_equal(&i->thread_info.soft_volume, &i->soft_volume))
1401 continue;
1402
1403 i->thread_info.soft_volume = i->soft_volume;
1404 pa_sink_input_request_rewind(i, 0, TRUE, FALSE, FALSE);
1405 }
1406 }
1407
1408 /* Called from IO thread, except when it is not */
1409 int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
1410 pa_sink *s = PA_SINK(o);
1411 pa_sink_assert_ref(s);
1412
1413 switch ((pa_sink_message_t) code) {
1414
1415 case PA_SINK_MESSAGE_ADD_INPUT: {
1416 pa_sink_input *i = PA_SINK_INPUT(userdata);
1417
1418 /* If you change anything here, make sure to change the
1419 * sink input handling a few lines down at
1420 * PA_SINK_MESSAGE_FINISH_MOVE, too. */
1421
1422 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
1423
1424 /* Since the caller sleeps in pa_sink_input_put(), we can
1425 * safely access data outside of thread_info even though
1426 * it is mutable */
1427
1428 if ((i->thread_info.sync_prev = i->sync_prev)) {
1429 pa_assert(i->sink == i->thread_info.sync_prev->sink);
1430 pa_assert(i->sync_prev->sync_next == i);
1431 i->thread_info.sync_prev->thread_info.sync_next = i;
1432 }
1433
1434 if ((i->thread_info.sync_next = i->sync_next)) {
1435 pa_assert(i->sink == i->thread_info.sync_next->sink);
1436 pa_assert(i->sync_next->sync_prev == i);
1437 i->thread_info.sync_next->thread_info.sync_prev = i;
1438 }
1439
1440 pa_assert(!i->thread_info.attached);
1441 i->thread_info.attached = TRUE;
1442
1443 if (i->attach)
1444 i->attach(i);
1445
1446 pa_sink_input_set_state_within_thread(i, i->state);
1447
1448 /* The requested latency of the sink input needs to be
1449 * fixed up and then configured on the sink */
1450
1451 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1)
1452 pa_sink_input_set_requested_latency_within_thread(i, i->thread_info.requested_sink_latency);
1453
1454 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
1455 pa_sink_input_update_max_request(i, s->thread_info.max_request);
1456
1457 /* We don't rewind here automatically. This is left to the
1458 * sink input implementor because some sink inputs need a
1459 * slow start, i.e. need some time to buffer client
1460 * samples before beginning streaming. */
1461
1462 /* In flat volume mode we need to update the volume as
1463 * well */
1464 return o->process_msg(o, PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL);
1465 }
1466
1467 case PA_SINK_MESSAGE_REMOVE_INPUT: {
1468 pa_sink_input *i = PA_SINK_INPUT(userdata);
1469
1470 /* If you change anything here, make sure to change the
1471 * sink input handling a few lines down at
1472 * PA_SINK_MESSAGE_PREPAPRE_MOVE, too. */
1473
1474 if (i->detach)
1475 i->detach(i);
1476
1477 pa_sink_input_set_state_within_thread(i, i->state);
1478
1479 pa_assert(i->thread_info.attached);
1480 i->thread_info.attached = FALSE;
1481
1482 /* Since the caller sleeps in pa_sink_input_unlink(),
1483 * we can safely access data outside of thread_info even
1484 * though it is mutable */
1485
1486 pa_assert(!i->sync_prev);
1487 pa_assert(!i->sync_next);
1488
1489 if (i->thread_info.sync_prev) {
1490 i->thread_info.sync_prev->thread_info.sync_next = i->thread_info.sync_prev->sync_next;
1491 i->thread_info.sync_prev = NULL;
1492 }
1493
1494 if (i->thread_info.sync_next) {
1495 i->thread_info.sync_next->thread_info.sync_prev = i->thread_info.sync_next->sync_prev;
1496 i->thread_info.sync_next = NULL;
1497 }
1498
1499 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
1500 pa_sink_input_unref(i);
1501
1502 pa_sink_invalidate_requested_latency(s);
1503 pa_sink_request_rewind(s, (size_t) -1);
1504
1505 /* In flat volume mode we need to update the volume as
1506 * well */
1507 return o->process_msg(o, PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL);
1508 }
1509
1510 case PA_SINK_MESSAGE_START_MOVE: {
1511 pa_sink_input *i = PA_SINK_INPUT(userdata);
1512
1513 /* We don't support moving synchronized streams. */
1514 pa_assert(!i->sync_prev);
1515 pa_assert(!i->sync_next);
1516 pa_assert(!i->thread_info.sync_next);
1517 pa_assert(!i->thread_info.sync_prev);
1518
1519 if (i->thread_info.state != PA_SINK_INPUT_CORKED) {
1520 pa_usec_t usec = 0;
1521 size_t sink_nbytes, total_nbytes;
1522
1523 /* Get the latency of the sink */
1524 if (!(s->flags & PA_SINK_LATENCY) ||
1525 PA_MSGOBJECT(s)->process_msg(PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
1526 usec = 0;
1527
1528 sink_nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
1529 total_nbytes = sink_nbytes + pa_memblockq_get_length(i->thread_info.render_memblockq);
1530
1531 if (total_nbytes > 0) {
1532 i->thread_info.rewrite_nbytes = i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, total_nbytes) : total_nbytes;
1533 i->thread_info.rewrite_flush = TRUE;
1534 pa_sink_input_process_rewind(i, sink_nbytes);
1535 }
1536 }
1537
1538 if (i->detach)
1539 i->detach(i);
1540
1541 pa_assert(i->thread_info.attached);
1542 i->thread_info.attached = FALSE;
1543
1544 /* Let's remove the sink input ...*/
1545 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
1546 pa_sink_input_unref(i);
1547
1548 pa_sink_invalidate_requested_latency(s);
1549
1550 pa_log_debug("Requesting rewind due to started move");
1551 pa_sink_request_rewind(s, (size_t) -1);
1552
1553 /* In flat volume mode we need to update the volume as
1554 * well */
1555 return o->process_msg(o, PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL);
1556 }
1557
1558 case PA_SINK_MESSAGE_FINISH_MOVE: {
1559 pa_sink_input *i = PA_SINK_INPUT(userdata);
1560
1561 /* We don't support moving synchronized streams. */
1562 pa_assert(!i->sync_prev);
1563 pa_assert(!i->sync_next);
1564 pa_assert(!i->thread_info.sync_next);
1565 pa_assert(!i->thread_info.sync_prev);
1566
1567 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
1568
1569 pa_assert(!i->thread_info.attached);
1570 i->thread_info.attached = TRUE;
1571
1572 if (i->attach)
1573 i->attach(i);
1574
1575 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1)
1576 pa_sink_input_set_requested_latency_within_thread(i, i->thread_info.requested_sink_latency);
1577
1578 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
1579 pa_sink_input_update_max_request(i, s->thread_info.max_request);
1580
1581 if (i->thread_info.state != PA_SINK_INPUT_CORKED) {
1582 pa_usec_t usec = 0;
1583 size_t nbytes;
1584
1585 /* Get the latency of the sink */
1586 if (!(s->flags & PA_SINK_LATENCY) ||
1587 PA_MSGOBJECT(s)->process_msg(PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
1588 usec = 0;
1589
1590 nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
1591
1592 if (nbytes > 0)
1593 pa_sink_input_drop(i, nbytes);
1594
1595 pa_log_debug("Requesting rewind due to finished move");
1596 pa_sink_request_rewind(s, nbytes);
1597 }
1598
1599 /* In flat volume mode we need to update the volume as
1600 * well */
1601 return o->process_msg(o, PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL);
1602 }
1603
1604 case PA_SINK_MESSAGE_SET_VOLUME:
1605
1606 if (!pa_cvolume_equal(&s->thread_info.soft_volume, &s->soft_volume)) {
1607 s->thread_info.soft_volume = s->soft_volume;
1608 pa_sink_request_rewind(s, (size_t) -1);
1609 }
1610
1611 if (!(s->flags & PA_SINK_FLAT_VOLUME))
1612 return 0;
1613
1614 /* Fall through ... */
1615
1616 case PA_SINK_MESSAGE_SYNC_VOLUMES:
1617 sync_input_volumes_within_thread(s);
1618 return 0;
1619
1620 case PA_SINK_MESSAGE_GET_VOLUME:
1621 return 0;
1622
1623 case PA_SINK_MESSAGE_SET_MUTE:
1624
1625 if (s->thread_info.soft_muted != s->muted) {
1626 s->thread_info.soft_muted = s->muted;
1627 pa_sink_request_rewind(s, (size_t) -1);
1628 }
1629
1630 return 0;
1631
1632 case PA_SINK_MESSAGE_GET_MUTE:
1633 return 0;
1634
1635 case PA_SINK_MESSAGE_SET_STATE: {
1636
1637 pa_bool_t suspend_change =
1638 (s->thread_info.state == PA_SINK_SUSPENDED && PA_SINK_IS_OPENED(PA_PTR_TO_UINT(userdata))) ||
1639 (PA_SINK_IS_OPENED(s->thread_info.state) && PA_PTR_TO_UINT(userdata) == PA_SINK_SUSPENDED);
1640
1641 s->thread_info.state = PA_PTR_TO_UINT(userdata);
1642
1643 if (s->thread_info.state == PA_SINK_SUSPENDED) {
1644 s->thread_info.rewind_nbytes = 0;
1645 s->thread_info.rewind_requested = FALSE;
1646 }
1647
1648 if (suspend_change) {
1649 pa_sink_input *i;
1650 void *state = NULL;
1651
1652 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1653 if (i->suspend_within_thread)
1654 i->suspend_within_thread(i, s->thread_info.state == PA_SINK_SUSPENDED);
1655 }
1656
1657 return 0;
1658 }
1659
1660 case PA_SINK_MESSAGE_DETACH:
1661
1662 /* Detach all streams */
1663 pa_sink_detach_within_thread(s);
1664 return 0;
1665
1666 case PA_SINK_MESSAGE_ATTACH:
1667
1668 /* Reattach all streams */
1669 pa_sink_attach_within_thread(s);
1670 return 0;
1671
1672 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY: {
1673
1674 pa_usec_t *usec = userdata;
1675 *usec = pa_sink_get_requested_latency_within_thread(s);
1676
1677 if (*usec == (pa_usec_t) -1)
1678 *usec = s->thread_info.max_latency;
1679
1680 return 0;
1681 }
1682
1683 case PA_SINK_MESSAGE_SET_LATENCY_RANGE: {
1684 pa_usec_t *r = userdata;
1685
1686 pa_sink_set_latency_range_within_thread(s, r[0], r[1]);
1687
1688 return 0;
1689 }
1690
1691 case PA_SINK_MESSAGE_GET_LATENCY_RANGE: {
1692 pa_usec_t *r = userdata;
1693
1694 r[0] = s->thread_info.min_latency;
1695 r[1] = s->thread_info.max_latency;
1696
1697 return 0;
1698 }
1699
1700 case PA_SINK_MESSAGE_GET_MAX_REWIND:
1701
1702 *((size_t*) userdata) = s->thread_info.max_rewind;
1703 return 0;
1704
1705 case PA_SINK_MESSAGE_GET_MAX_REQUEST:
1706
1707 *((size_t*) userdata) = s->thread_info.max_request;
1708 return 0;
1709
1710 case PA_SINK_MESSAGE_SET_MAX_REWIND:
1711
1712 pa_sink_set_max_rewind_within_thread(s, (size_t) offset);
1713 return 0;
1714
1715 case PA_SINK_MESSAGE_SET_MAX_REQUEST:
1716
1717 pa_sink_set_max_request_within_thread(s, (size_t) offset);
1718 return 0;
1719
1720 case PA_SINK_MESSAGE_GET_LATENCY:
1721 case PA_SINK_MESSAGE_MAX:
1722 ;
1723 }
1724
1725 return -1;
1726 }
1727
1728 /* Called from main thread */
1729 int pa_sink_suspend_all(pa_core *c, pa_bool_t suspend) {
1730 pa_sink *sink;
1731 uint32_t idx;
1732 int ret = 0;
1733
1734 pa_core_assert_ref(c);
1735
1736 for (sink = PA_SINK(pa_idxset_first(c->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(c->sinks, &idx))) {
1737 int r;
1738
1739 if ((r = pa_sink_suspend(sink, suspend)) < 0)
1740 ret = r;
1741 }
1742
1743 return ret;
1744 }
1745
1746 /* Called from main thread */
1747 void pa_sink_detach(pa_sink *s) {
1748 pa_sink_assert_ref(s);
1749 pa_assert(PA_SINK_IS_LINKED(s->state));
1750
1751 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_DETACH, NULL, 0, NULL) == 0);
1752 }
1753
1754 /* Called from main thread */
1755 void pa_sink_attach(pa_sink *s) {
1756 pa_sink_assert_ref(s);
1757 pa_assert(PA_SINK_IS_LINKED(s->state));
1758
1759 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_ATTACH, NULL, 0, NULL) == 0);
1760 }
1761
1762 /* Called from IO thread */
1763 void pa_sink_detach_within_thread(pa_sink *s) {
1764 pa_sink_input *i;
1765 void *state = NULL;
1766
1767 pa_sink_assert_ref(s);
1768 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1769
1770 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1771 if (i->detach)
1772 i->detach(i);
1773
1774 if (s->monitor_source)
1775 pa_source_detach_within_thread(s->monitor_source);
1776 }
1777
1778 /* Called from IO thread */
1779 void pa_sink_attach_within_thread(pa_sink *s) {
1780 pa_sink_input *i;
1781 void *state = NULL;
1782
1783 pa_sink_assert_ref(s);
1784 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1785
1786 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1787 if (i->attach)
1788 i->attach(i);
1789
1790 if (s->monitor_source)
1791 pa_source_attach_within_thread(s->monitor_source);
1792 }
1793
1794 /* Called from IO thread */
1795 void pa_sink_request_rewind(pa_sink*s, size_t nbytes) {
1796 pa_sink_assert_ref(s);
1797 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1798
1799 if (s->thread_info.state == PA_SINK_SUSPENDED)
1800 return;
1801
1802 if (nbytes == (size_t) -1)
1803 nbytes = s->thread_info.max_rewind;
1804
1805 nbytes = PA_MIN(nbytes, s->thread_info.max_rewind);
1806
1807 if (s->thread_info.rewind_requested &&
1808 nbytes <= s->thread_info.rewind_nbytes)
1809 return;
1810
1811 s->thread_info.rewind_nbytes = nbytes;
1812 s->thread_info.rewind_requested = TRUE;
1813
1814 if (s->request_rewind)
1815 s->request_rewind(s);
1816 }
1817
1818 /* Called from IO thread */
1819 pa_usec_t pa_sink_get_requested_latency_within_thread(pa_sink *s) {
1820 pa_usec_t result = (pa_usec_t) -1;
1821 pa_sink_input *i;
1822 void *state = NULL;
1823 pa_usec_t monitor_latency;
1824
1825 pa_sink_assert_ref(s);
1826
1827 if (s->thread_info.requested_latency_valid)
1828 return s->thread_info.requested_latency;
1829
1830 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1831
1832 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1 &&
1833 (result == (pa_usec_t) -1 || result > i->thread_info.requested_sink_latency))
1834 result = i->thread_info.requested_sink_latency;
1835
1836 monitor_latency = pa_source_get_requested_latency_within_thread(s->monitor_source);
1837
1838 if (monitor_latency != (pa_usec_t) -1 &&
1839 (result == (pa_usec_t) -1 || result > monitor_latency))
1840 result = monitor_latency;
1841
1842 if (result != (pa_usec_t) -1) {
1843 if (result > s->thread_info.max_latency)
1844 result = s->thread_info.max_latency;
1845
1846 if (result < s->thread_info.min_latency)
1847 result = s->thread_info.min_latency;
1848 }
1849
1850 s->thread_info.requested_latency = result;
1851 s->thread_info.requested_latency_valid = TRUE;
1852
1853 return result;
1854 }
1855
1856 /* Called from main thread */
1857 pa_usec_t pa_sink_get_requested_latency(pa_sink *s) {
1858 pa_usec_t usec = 0;
1859
1860 pa_sink_assert_ref(s);
1861 pa_assert(PA_SINK_IS_LINKED(s->state));
1862
1863 if (s->state == PA_SINK_SUSPENDED)
1864 return 0;
1865
1866 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
1867 return usec;
1868 }
1869
1870 /* Called from IO as well as the main thread -- the latter only before the IO thread started up */
1871 void pa_sink_set_max_rewind_within_thread(pa_sink *s, size_t max_rewind) {
1872 pa_sink_input *i;
1873 void *state = NULL;
1874
1875 pa_sink_assert_ref(s);
1876
1877 if (max_rewind == s->thread_info.max_rewind)
1878 return;
1879
1880 s->thread_info.max_rewind = max_rewind;
1881
1882 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
1883 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1884 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
1885 }
1886
1887 if (s->monitor_source)
1888 pa_source_set_max_rewind_within_thread(s->monitor_source, s->thread_info.max_rewind);
1889 }
1890
1891 /* Called from main thread */
1892 void pa_sink_set_max_rewind(pa_sink *s, size_t max_rewind) {
1893 pa_sink_assert_ref(s);
1894
1895 if (PA_SINK_IS_LINKED(s->state))
1896 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MAX_REWIND, NULL, max_rewind, NULL) == 0);
1897 else
1898 pa_sink_set_max_rewind_within_thread(s, max_rewind);
1899 }
1900
1901 /* Called from IO as well as the main thread -- the latter only before the IO thread started up */
1902 void pa_sink_set_max_request_within_thread(pa_sink *s, size_t max_request) {
1903 void *state = NULL;
1904
1905 pa_sink_assert_ref(s);
1906
1907 if (max_request == s->thread_info.max_request)
1908 return;
1909
1910 s->thread_info.max_request = max_request;
1911
1912 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
1913 pa_sink_input *i;
1914
1915 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1916 pa_sink_input_update_max_request(i, s->thread_info.max_request);
1917 }
1918 }
1919
1920 /* Called from main thread */
1921 void pa_sink_set_max_request(pa_sink *s, size_t max_request) {
1922 pa_sink_assert_ref(s);
1923
1924 if (PA_SINK_IS_LINKED(s->state))
1925 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MAX_REQUEST, NULL, max_request, NULL) == 0);
1926 else
1927 pa_sink_set_max_request_within_thread(s, max_request);
1928 }
1929
1930 /* Called from IO thread */
1931 void pa_sink_invalidate_requested_latency(pa_sink *s) {
1932 pa_sink_input *i;
1933 void *state = NULL;
1934
1935 pa_sink_assert_ref(s);
1936
1937 s->thread_info.requested_latency_valid = FALSE;
1938
1939 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
1940
1941 if (s->update_requested_latency)
1942 s->update_requested_latency(s);
1943
1944 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1945 if (i->update_sink_requested_latency)
1946 i->update_sink_requested_latency(i);
1947 }
1948 }
1949
1950 /* Called from main thread */
1951 void pa_sink_set_latency_range(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency) {
1952 pa_sink_assert_ref(s);
1953
1954 /* min_latency == 0: no limit
1955 * min_latency anything else: specified limit
1956 *
1957 * Similar for max_latency */
1958
1959 if (min_latency < ABSOLUTE_MIN_LATENCY)
1960 min_latency = ABSOLUTE_MIN_LATENCY;
1961
1962 if (max_latency <= 0 ||
1963 max_latency > ABSOLUTE_MAX_LATENCY)
1964 max_latency = ABSOLUTE_MAX_LATENCY;
1965
1966 pa_assert(min_latency <= max_latency);
1967
1968 /* Hmm, let's see if someone forgot to set PA_SINK_DYNAMIC_LATENCY here... */
1969 pa_assert((min_latency == ABSOLUTE_MIN_LATENCY &&
1970 max_latency == ABSOLUTE_MAX_LATENCY) ||
1971 (s->flags & PA_SINK_DYNAMIC_LATENCY));
1972
1973 if (PA_SINK_IS_LINKED(s->state)) {
1974 pa_usec_t r[2];
1975
1976 r[0] = min_latency;
1977 r[1] = max_latency;
1978
1979 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_LATENCY_RANGE, r, 0, NULL) == 0);
1980 } else
1981 pa_sink_set_latency_range_within_thread(s, min_latency, max_latency);
1982 }
1983
1984 /* Called from main thread */
1985 void pa_sink_get_latency_range(pa_sink *s, pa_usec_t *min_latency, pa_usec_t *max_latency) {
1986 pa_sink_assert_ref(s);
1987 pa_assert(min_latency);
1988 pa_assert(max_latency);
1989
1990 if (PA_SINK_IS_LINKED(s->state)) {
1991 pa_usec_t r[2] = { 0, 0 };
1992
1993 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY_RANGE, r, 0, NULL) == 0);
1994
1995 *min_latency = r[0];
1996 *max_latency = r[1];
1997 } else {
1998 *min_latency = s->thread_info.min_latency;
1999 *max_latency = s->thread_info.max_latency;
2000 }
2001 }
2002
2003 /* Called from IO thread */
2004 void pa_sink_set_latency_range_within_thread(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency) {
2005 void *state = NULL;
2006
2007 pa_sink_assert_ref(s);
2008
2009 pa_assert(min_latency >= ABSOLUTE_MIN_LATENCY);
2010 pa_assert(max_latency <= ABSOLUTE_MAX_LATENCY);
2011 pa_assert(min_latency <= max_latency);
2012
2013 /* Hmm, let's see if someone forgot to set PA_SINK_DYNAMIC_LATENCY here... */
2014 pa_assert((min_latency == ABSOLUTE_MIN_LATENCY &&
2015 max_latency == ABSOLUTE_MAX_LATENCY) ||
2016 (s->flags & PA_SINK_DYNAMIC_LATENCY));
2017
2018 s->thread_info.min_latency = min_latency;
2019 s->thread_info.max_latency = max_latency;
2020
2021 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
2022 pa_sink_input *i;
2023
2024 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
2025 if (i->update_sink_latency_range)
2026 i->update_sink_latency_range(i);
2027 }
2028
2029 pa_sink_invalidate_requested_latency(s);
2030
2031 pa_source_set_latency_range_within_thread(s->monitor_source, min_latency, max_latency);
2032 }
2033
2034 /* Called from main context */
2035 size_t pa_sink_get_max_rewind(pa_sink *s) {
2036 size_t r;
2037 pa_sink_assert_ref(s);
2038
2039 if (!PA_SINK_IS_LINKED(s->state))
2040 return s->thread_info.max_rewind;
2041
2042 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MAX_REWIND, &r, 0, NULL) == 0);
2043
2044 return r;
2045 }
2046
2047 /* Called from main context */
2048 size_t pa_sink_get_max_request(pa_sink *s) {
2049 size_t r;
2050 pa_sink_assert_ref(s);
2051
2052 if (!PA_SINK_IS_LINKED(s->state))
2053 return s->thread_info.max_request;
2054
2055 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MAX_REQUEST, &r, 0, NULL) == 0);
2056
2057 return r;
2058 }
2059
2060 /* Called from main context */
2061 pa_bool_t pa_device_init_icon(pa_proplist *p, pa_bool_t is_sink) {
2062 const char *ff, *c, *t = NULL, *s = "", *profile, *bus;
2063
2064 pa_assert(p);
2065
2066 if (pa_proplist_contains(p, PA_PROP_DEVICE_ICON_NAME))
2067 return TRUE;
2068
2069 if ((ff = pa_proplist_gets(p, PA_PROP_DEVICE_FORM_FACTOR))) {
2070
2071 if (pa_streq(ff, "microphone"))
2072 t = "audio-input-microphone";
2073 else if (pa_streq(ff, "webcam"))
2074 t = "camera-web";
2075 else if (pa_streq(ff, "computer"))
2076 t = "computer";
2077 else if (pa_streq(ff, "handset"))
2078 t = "phone";
2079 else if (pa_streq(ff, "portable"))
2080 t = "multimedia-player";
2081 else if (pa_streq(ff, "tv"))
2082 t = "video-display";
2083 }
2084
2085 if (!t)
2086 if ((c = pa_proplist_gets(p, PA_PROP_DEVICE_CLASS)))
2087 if (pa_streq(c, "modem"))
2088 t = "modem";
2089
2090 if (!t) {
2091 if (is_sink)
2092 t = "audio-card";
2093 else
2094 t = "audio-input-microphone";
2095 }
2096
2097 if ((profile = pa_proplist_gets(p, PA_PROP_DEVICE_PROFILE_NAME))) {
2098 if (strstr(profile, "analog"))
2099 s = "-analog";
2100 else if (strstr(profile, "iec958"))
2101 s = "-iec958";
2102 else if (strstr(profile, "hdmi"))
2103 s = "-hdmi";
2104 }
2105
2106 bus = pa_proplist_gets(p, PA_PROP_DEVICE_BUS);
2107
2108 pa_proplist_setf(p, PA_PROP_DEVICE_ICON_NAME, "%s%s%s%s", t, pa_strempty(s), bus ? "-" : "", pa_strempty(bus));
2109
2110 return TRUE;
2111 }
2112
2113 pa_bool_t pa_device_init_description(pa_proplist *p) {
2114 const char *s;
2115 pa_assert(p);
2116
2117 if (pa_proplist_contains(p, PA_PROP_DEVICE_DESCRIPTION))
2118 return TRUE;
2119
2120 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_FORM_FACTOR)))
2121 if (pa_streq(s, "internal")) {
2122 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, _("Internal Audio"));
2123 return TRUE;
2124 }
2125
2126 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_CLASS)))
2127 if (pa_streq(s, "modem")) {
2128 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, _("Modem"));
2129 return TRUE;
2130 }
2131
2132 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_PRODUCT_NAME))) {
2133 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, s);
2134 return TRUE;
2135 }
2136
2137 return FALSE;
2138 }