]> code.delx.au - pulseaudio/blob - src/pulsecore/sink.c
allow sinks to be created with max_request initialized to 0, so that the data can...
[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 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
36 #include <pulsecore/sink-input.h>
37 #include <pulsecore/namereg.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/sample-util.h>
40 #include <pulsecore/core-subscribe.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/macro.h>
43 #include <pulsecore/play-memblockq.h>
44
45 #include "sink.h"
46
47 #define MAX_MIX_CHANNELS 32
48 #define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
49 #define DEFAULT_MIN_LATENCY (4*PA_USEC_PER_MSEC)
50
51 static PA_DEFINE_CHECK_TYPE(pa_sink, pa_msgobject);
52
53 static void sink_free(pa_object *s);
54
55 pa_sink_new_data* pa_sink_new_data_init(pa_sink_new_data *data) {
56 pa_assert(data);
57
58 memset(data, 0, sizeof(*data));
59 data->proplist = pa_proplist_new();
60
61 return data;
62 }
63
64 void pa_sink_new_data_set_name(pa_sink_new_data *data, const char *name) {
65 pa_assert(data);
66
67 pa_xfree(data->name);
68 data->name = pa_xstrdup(name);
69 }
70
71 void pa_sink_new_data_set_sample_spec(pa_sink_new_data *data, const pa_sample_spec *spec) {
72 pa_assert(data);
73
74 if ((data->sample_spec_is_set = !!spec))
75 data->sample_spec = *spec;
76 }
77
78 void pa_sink_new_data_set_channel_map(pa_sink_new_data *data, const pa_channel_map *map) {
79 pa_assert(data);
80
81 if ((data->channel_map_is_set = !!map))
82 data->channel_map = *map;
83 }
84
85 void pa_sink_new_data_set_volume(pa_sink_new_data *data, const pa_cvolume *volume) {
86 pa_assert(data);
87
88 if ((data->volume_is_set = !!volume))
89 data->volume = *volume;
90 }
91
92 void pa_sink_new_data_set_muted(pa_sink_new_data *data, pa_bool_t mute) {
93 pa_assert(data);
94
95 data->muted_is_set = TRUE;
96 data->muted = !!mute;
97 }
98
99 void pa_sink_new_data_done(pa_sink_new_data *data) {
100 pa_assert(data);
101
102 pa_xfree(data->name);
103 pa_proplist_free(data->proplist);
104 }
105
106 /* Called from main context */
107 static void reset_callbacks(pa_sink *s) {
108 pa_assert(s);
109
110 s->set_state = NULL;
111 s->get_volume = NULL;
112 s->set_volume = NULL;
113 s->get_mute = NULL;
114 s->set_mute = NULL;
115 s->request_rewind = NULL;
116 s->update_requested_latency = NULL;
117 }
118
119 /* Called from main context */
120 pa_sink* pa_sink_new(
121 pa_core *core,
122 pa_sink_new_data *data,
123 pa_sink_flags_t flags) {
124
125 pa_sink *s;
126 const char *name;
127 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
128 pa_source_new_data source_data;
129 const char *dn;
130
131 pa_assert(core);
132 pa_assert(data);
133 pa_assert(data->name);
134
135 s = pa_msgobject_new(pa_sink);
136
137 if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_SINK, s, data->namereg_fail))) {
138 pa_xfree(s);
139 return NULL;
140 }
141
142 pa_sink_new_data_set_name(data, name);
143
144 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_NEW], data) < 0) {
145 pa_xfree(s);
146 pa_namereg_unregister(core, name);
147 return NULL;
148 }
149
150 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
151 pa_return_null_if_fail(data->name && pa_utf8_valid(data->name) && data->name[0]);
152
153 pa_return_null_if_fail(data->sample_spec_is_set && pa_sample_spec_valid(&data->sample_spec));
154
155 if (!data->channel_map_is_set)
156 pa_return_null_if_fail(pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT));
157
158 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
159 pa_return_null_if_fail(data->channel_map.channels == data->sample_spec.channels);
160
161 if (!data->volume_is_set)
162 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
163
164 pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
165 pa_return_null_if_fail(data->volume.channels == data->sample_spec.channels);
166
167 if (!data->muted_is_set)
168 data->muted = FALSE;
169
170 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_FIXATE], data) < 0) {
171 pa_xfree(s);
172 pa_namereg_unregister(core, name);
173 return NULL;
174 }
175
176 s->parent.parent.free = sink_free;
177 s->parent.process_msg = pa_sink_process_msg;
178
179 s->core = core;
180 s->state = PA_SINK_INIT;
181 s->flags = flags;
182 s->name = pa_xstrdup(name);
183 s->proplist = pa_proplist_copy(data->proplist);
184 s->driver = pa_xstrdup(data->driver);
185 s->module = data->module;
186
187 s->sample_spec = data->sample_spec;
188 s->channel_map = data->channel_map;
189
190 s->inputs = pa_idxset_new(NULL, NULL);
191 s->n_corked = 0;
192
193 s->volume = data->volume;
194 s->muted = data->muted;
195 s->refresh_volume = s->refresh_muted = FALSE;
196
197 reset_callbacks(s);
198 s->userdata = NULL;
199
200 s->asyncmsgq = NULL;
201 s->rtpoll = NULL;
202
203 pa_silence_memchunk_get(
204 &core->silence_cache,
205 core->mempool,
206 &s->silence,
207 &s->sample_spec,
208 0);
209
210 s->thread_info.inputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
211 pa_cvolume_reset(&s->thread_info.soft_volume, s->sample_spec.channels);
212 s->thread_info.soft_muted = FALSE;
213 s->thread_info.state = s->state;
214 s->thread_info.rewind_nbytes = 0;
215 s->thread_info.max_rewind = 0;
216 s->thread_info.max_request = 0;
217 s->thread_info.requested_latency_valid = FALSE;
218 s->thread_info.requested_latency = 0;
219 s->thread_info.min_latency = DEFAULT_MIN_LATENCY;
220 s->thread_info.max_latency = 0;
221
222 pa_assert_se(pa_idxset_put(core->sinks, s, &s->index) >= 0);
223
224 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s",
225 s->index,
226 s->name,
227 pa_sample_spec_snprint(st, sizeof(st), &s->sample_spec),
228 pa_channel_map_snprint(cm, sizeof(cm), &s->channel_map));
229
230 pa_source_new_data_init(&source_data);
231 pa_source_new_data_set_sample_spec(&source_data, &s->sample_spec);
232 pa_source_new_data_set_channel_map(&source_data, &s->channel_map);
233 source_data.name = pa_sprintf_malloc("%s.monitor", name);
234 source_data.driver = data->driver;
235 source_data.module = data->module;
236
237 dn = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
238 pa_proplist_setf(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Monitor of %s", dn ? dn : s->name);
239 pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_CLASS, "monitor");
240
241 s->monitor_source = pa_source_new(core, &source_data, 0);
242
243 pa_source_new_data_done(&source_data);
244
245 if (!s->monitor_source) {
246 pa_sink_unlink(s);
247 pa_sink_unref(s);
248 return NULL;
249 }
250
251 s->monitor_source->monitor_of = s;
252
253 pa_source_set_latency_range(s->monitor_source, s->thread_info.min_latency, s->thread_info.max_latency);
254 pa_source_set_max_rewind(s->monitor_source, s->thread_info.max_rewind);
255
256 return s;
257 }
258
259 /* Called from main context */
260 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
261 int ret;
262 pa_bool_t suspend_change;
263
264 pa_assert(s);
265
266 if (s->state == state)
267 return 0;
268
269 suspend_change =
270 (s->state == PA_SINK_SUSPENDED && PA_SINK_IS_OPENED(state)) ||
271 (PA_SINK_IS_OPENED(s->state) && state == PA_SINK_SUSPENDED);
272
273 if (s->set_state)
274 if ((ret = s->set_state(s, state)) < 0)
275 return -1;
276
277 if (s->asyncmsgq)
278 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) == 0);
279
280 s->state = state;
281
282 if (suspend_change) {
283 pa_sink_input *i;
284 uint32_t idx;
285
286 /* We're suspending or resuming, tell everyone about it */
287
288 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx)))
289 if (i->suspend)
290 i->suspend(i, state == PA_SINK_SUSPENDED);
291 }
292
293 if (state != PA_SINK_UNLINKED) /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
294 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], s);
295
296 return 0;
297 }
298
299 /* Called from main context */
300 void pa_sink_put(pa_sink* s) {
301 pa_sink_assert_ref(s);
302
303 pa_assert(s->state == PA_SINK_INIT);
304
305 /* The following fields must be initialized properly when calling _put() */
306 pa_assert(s->asyncmsgq);
307 pa_assert(s->rtpoll);
308 pa_assert(!s->thread_info.min_latency || !s->thread_info.max_latency ||
309 s->thread_info.min_latency <= s->thread_info.max_latency);
310
311 if (!(s->flags & PA_SINK_HW_VOLUME_CTRL)) {
312 s->flags |= PA_SINK_DECIBEL_VOLUME;
313
314 s->thread_info.soft_volume = s->volume;
315 s->thread_info.soft_muted = s->muted;
316 }
317
318 pa_assert_se(sink_set_state(s, PA_SINK_IDLE) == 0);
319
320 pa_source_put(s->monitor_source);
321
322 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
323 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PUT], s);
324 }
325
326 /* Called from main context */
327 void pa_sink_unlink(pa_sink* s) {
328 pa_bool_t linked;
329 pa_sink_input *i, *j = NULL;
330
331 pa_assert(s);
332
333 /* Please note that pa_sink_unlink() does more than simply
334 * reversing pa_sink_put(). It also undoes the registrations
335 * already done in pa_sink_new()! */
336
337 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
338 * may be called multiple times on the same sink without bad
339 * effects. */
340
341 linked = PA_SINK_IS_LINKED(s->state);
342
343 if (linked)
344 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK], s);
345
346 if (s->state != PA_SINK_UNLINKED)
347 pa_namereg_unregister(s->core, s->name);
348 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
349
350 while ((i = pa_idxset_first(s->inputs, NULL))) {
351 pa_assert(i != j);
352 pa_sink_input_kill(i);
353 j = i;
354 }
355
356 if (linked)
357 sink_set_state(s, PA_SINK_UNLINKED);
358 else
359 s->state = PA_SINK_UNLINKED;
360
361 reset_callbacks(s);
362
363 if (s->monitor_source)
364 pa_source_unlink(s->monitor_source);
365
366 if (linked) {
367 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
368 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], s);
369 }
370 }
371
372 /* Called from main context */
373 static void sink_free(pa_object *o) {
374 pa_sink *s = PA_SINK(o);
375 pa_sink_input *i;
376
377 pa_assert(s);
378 pa_assert(pa_sink_refcnt(s) == 0);
379
380 if (PA_SINK_IS_LINKED(s->state))
381 pa_sink_unlink(s);
382
383 pa_log_info("Freeing sink %u \"%s\"", s->index, s->name);
384
385 if (s->monitor_source) {
386 pa_source_unref(s->monitor_source);
387 s->monitor_source = NULL;
388 }
389
390 pa_idxset_free(s->inputs, NULL, NULL);
391
392 while ((i = pa_hashmap_steal_first(s->thread_info.inputs)))
393 pa_sink_input_unref(i);
394
395 pa_hashmap_free(s->thread_info.inputs, NULL, NULL);
396
397 if (s->silence.memblock)
398 pa_memblock_unref(s->silence.memblock);
399
400 pa_xfree(s->name);
401 pa_xfree(s->driver);
402
403 if (s->proplist)
404 pa_proplist_free(s->proplist);
405
406 pa_xfree(s);
407 }
408
409 /* Called from main context */
410 void pa_sink_set_asyncmsgq(pa_sink *s, pa_asyncmsgq *q) {
411 pa_sink_assert_ref(s);
412
413 s->asyncmsgq = q;
414
415 if (s->monitor_source)
416 pa_source_set_asyncmsgq(s->monitor_source, q);
417 }
418
419 /* Called from main context */
420 void pa_sink_set_rtpoll(pa_sink *s, pa_rtpoll *p) {
421 pa_sink_assert_ref(s);
422
423 s->rtpoll = p;
424 if (s->monitor_source)
425 pa_source_set_rtpoll(s->monitor_source, p);
426 }
427
428 /* Called from main context */
429 int pa_sink_update_status(pa_sink*s) {
430 pa_sink_assert_ref(s);
431 pa_assert(PA_SINK_IS_LINKED(s->state));
432
433 if (s->state == PA_SINK_SUSPENDED)
434 return 0;
435
436 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
437 }
438
439 /* Called from main context */
440 int pa_sink_suspend(pa_sink *s, pa_bool_t suspend) {
441 pa_sink_assert_ref(s);
442 pa_assert(PA_SINK_IS_LINKED(s->state));
443
444 if (suspend)
445 return sink_set_state(s, PA_SINK_SUSPENDED);
446 else
447 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
448 }
449
450 /* Called from IO thread context */
451 void pa_sink_process_rewind(pa_sink *s, size_t nbytes) {
452 pa_sink_input *i;
453 void *state = NULL;
454 pa_sink_assert_ref(s);
455 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
456
457 /* Make sure the sink code already reset the counter! */
458 pa_assert(s->thread_info.rewind_nbytes <= 0);
459
460 if (nbytes <= 0)
461 return;
462
463 pa_log_debug("Processing rewind...");
464
465 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
466 pa_sink_input_assert_ref(i);
467 pa_sink_input_process_rewind(i, nbytes);
468 }
469
470 if (s->monitor_source && PA_SOURCE_IS_OPENED(s->monitor_source->thread_info.state))
471 pa_source_process_rewind(s->monitor_source, nbytes);
472 }
473
474 /* Called from IO thread context */
475 static unsigned fill_mix_info(pa_sink *s, size_t *length, pa_mix_info *info, unsigned maxinfo) {
476 pa_sink_input *i;
477 unsigned n = 0;
478 void *state = NULL;
479 size_t mixlength = *length;
480
481 pa_sink_assert_ref(s);
482 pa_assert(info);
483
484 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)) && maxinfo > 0) {
485 pa_sink_input_assert_ref(i);
486
487 if (pa_sink_input_peek(i, *length, &info->chunk, &info->volume) < 0)
488 continue;
489
490 if (mixlength == 0 || info->chunk.length < mixlength)
491 mixlength = info->chunk.length;
492
493 if (pa_memblock_is_silence(info->chunk.memblock)) {
494 pa_memblock_unref(info->chunk.memblock);
495 continue;
496 }
497
498 info->userdata = pa_sink_input_ref(i);
499
500 pa_assert(info->chunk.memblock);
501 pa_assert(info->chunk.length > 0);
502
503 info++;
504 n++;
505 maxinfo--;
506 }
507
508 if (mixlength > 0)
509 *length = mixlength;
510
511 return n;
512 }
513
514 /* Called from IO thread context */
515 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned n, pa_memchunk *result) {
516 pa_sink_input *i;
517 void *state = NULL;
518 unsigned p = 0;
519 unsigned n_unreffed = 0;
520
521 pa_sink_assert_ref(s);
522 pa_assert(result);
523 pa_assert(result->memblock);
524 pa_assert(result->length > 0);
525
526 /* We optimize for the case where the order of the inputs has not changed */
527
528 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
529 unsigned j;
530 pa_mix_info* m = NULL;
531
532 pa_sink_input_assert_ref(i);
533
534 /* Let's try to find the matching entry info the pa_mix_info array */
535 for (j = 0; j < n; j ++) {
536
537 if (info[p].userdata == i) {
538 m = info + p;
539 break;
540 }
541
542 p++;
543 if (p >= n)
544 p = 0;
545 }
546
547 /* Drop read data */
548 pa_sink_input_drop(i, result->length);
549
550 if (s->monitor_source && PA_SOURCE_IS_OPENED(pa_source_get_state(s->monitor_source))) {
551
552 if (pa_hashmap_size(i->thread_info.direct_outputs) > 0) {
553 void *ostate = NULL;
554 pa_source_output *o;
555 pa_memchunk c;
556
557 if (m && m->chunk.memblock) {
558 c = m->chunk;
559 pa_memblock_ref(c.memblock);
560 pa_assert(result->length <= c.length);
561 c.length = result->length;
562
563 pa_memchunk_make_writable(&c, 0);
564 pa_volume_memchunk(&c, &s->sample_spec, &m->volume);
565 } else {
566 c = s->silence;
567 pa_memblock_ref(c.memblock);
568 pa_assert(result->length <= c.length);
569 c.length = result->length;
570 }
571
572 while ((o = pa_hashmap_iterate(i->thread_info.direct_outputs, &ostate, NULL))) {
573 pa_source_output_assert_ref(o);
574 pa_assert(o->direct_on_input == i);
575 pa_source_post_direct(s->monitor_source, o, &c);
576 }
577
578 pa_memblock_unref(c.memblock);
579 }
580 }
581
582 if (m) {
583 if (m->chunk.memblock)
584 pa_memblock_unref(m->chunk.memblock);
585 pa_memchunk_reset(&m->chunk);
586
587 pa_sink_input_unref(m->userdata);
588 m->userdata = NULL;
589
590 n_unreffed += 1;
591 }
592 }
593
594 /* Now drop references to entries that are included in the
595 * pa_mix_info array but don't exist anymore */
596
597 if (n_unreffed < n) {
598 for (; n > 0; info++, n--) {
599 if (info->userdata)
600 pa_sink_input_unref(info->userdata);
601 if (info->chunk.memblock)
602 pa_memblock_unref(info->chunk.memblock);
603 }
604 }
605
606 if (s->monitor_source && PA_SOURCE_IS_OPENED(pa_source_get_state(s->monitor_source)))
607 pa_source_post(s->monitor_source, result);
608 }
609
610 /* Called from IO thread context */
611 void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
612 pa_mix_info info[MAX_MIX_CHANNELS];
613 unsigned n;
614 size_t block_size_max;
615
616 pa_sink_assert_ref(s);
617 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
618 pa_assert(pa_frame_aligned(length, &s->sample_spec));
619 pa_assert(result);
620
621 pa_sink_ref(s);
622
623 s->thread_info.rewind_nbytes = 0;
624
625 if (length <= 0)
626 length = pa_frame_align(MIX_BUFFER_LENGTH, &s->sample_spec);
627
628 block_size_max = pa_mempool_block_size_max(s->core->mempool);
629 if (length > block_size_max)
630 length = pa_frame_align(block_size_max, &s->sample_spec);
631
632 pa_assert(length > 0);
633
634 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, &length, info, MAX_MIX_CHANNELS) : 0;
635
636 if (n == 0) {
637
638 *result = s->silence;
639 pa_memblock_ref(result->memblock);
640
641 if (result->length > length)
642 result->length = length;
643
644 } else if (n == 1) {
645 pa_cvolume volume;
646
647 *result = info[0].chunk;
648 pa_memblock_ref(result->memblock);
649
650 if (result->length > length)
651 result->length = length;
652
653 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
654
655 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&volume)) {
656 pa_log("adjusting volume ");
657 pa_memchunk_make_writable(result, 0);
658 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
659 pa_silence_memchunk(result, &s->sample_spec);
660 else
661 pa_volume_memchunk(result, &s->sample_spec, &volume);
662 }
663 } else {
664 void *ptr;
665 result->memblock = pa_memblock_new(s->core->mempool, length);
666
667 ptr = pa_memblock_acquire(result->memblock);
668 result->length = pa_mix(info, n,
669 ptr, length,
670 &s->sample_spec,
671 &s->thread_info.soft_volume,
672 s->thread_info.soft_muted);
673 pa_memblock_release(result->memblock);
674
675 result->index = 0;
676 }
677
678 if (s->thread_info.state == PA_SINK_RUNNING)
679 inputs_drop(s, info, n, result);
680
681 pa_sink_unref(s);
682 }
683
684 /* Called from IO thread context */
685 void pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
686 pa_mix_info info[MAX_MIX_CHANNELS];
687 unsigned n;
688 size_t length, block_size_max;
689
690 pa_sink_assert_ref(s);
691 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
692 pa_assert(target);
693 pa_assert(target->memblock);
694 pa_assert(target->length > 0);
695 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
696
697 pa_sink_ref(s);
698
699 s->thread_info.rewind_nbytes = 0;
700
701 length = target->length;
702 block_size_max = pa_mempool_block_size_max(s->core->mempool);
703 if (length > block_size_max)
704 length = pa_frame_align(block_size_max, &s->sample_spec);
705
706 pa_assert(length > 0);
707
708 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, &length, info, MAX_MIX_CHANNELS) : 0;
709
710 if (n == 0) {
711 if (target->length > length)
712 target->length = length;
713
714 pa_silence_memchunk(target, &s->sample_spec);
715 } else if (n == 1) {
716 pa_cvolume volume;
717
718 if (target->length > length)
719 target->length = length;
720
721 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
722
723 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
724 pa_silence_memchunk(target, &s->sample_spec);
725 else {
726 pa_memchunk vchunk;
727
728 vchunk = info[0].chunk;
729 pa_memblock_ref(vchunk.memblock);
730
731 if (vchunk.length > length)
732 vchunk.length = length;
733
734 if (!pa_cvolume_is_norm(&volume)) {
735 pa_memchunk_make_writable(&vchunk, 0);
736 pa_volume_memchunk(&vchunk, &s->sample_spec, &volume);
737 }
738
739 pa_memchunk_memcpy(target, &vchunk);
740 pa_memblock_unref(vchunk.memblock);
741 }
742
743 } else {
744 void *ptr;
745
746 ptr = pa_memblock_acquire(target->memblock);
747
748 target->length = pa_mix(info, n,
749 (uint8_t*) ptr + target->index, length,
750 &s->sample_spec,
751 &s->thread_info.soft_volume,
752 s->thread_info.soft_muted);
753
754 pa_memblock_release(target->memblock);
755 }
756
757 if (s->thread_info.state == PA_SINK_RUNNING)
758 inputs_drop(s, info, n, target);
759
760 pa_sink_unref(s);
761 }
762
763 /* Called from IO thread context */
764 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
765 pa_memchunk chunk;
766 size_t l, d;
767
768 pa_sink_assert_ref(s);
769 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
770 pa_assert(target);
771 pa_assert(target->memblock);
772 pa_assert(target->length > 0);
773 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
774
775 pa_sink_ref(s);
776
777 s->thread_info.rewind_nbytes = 0;
778
779 l = target->length;
780 d = 0;
781 while (l > 0) {
782 chunk = *target;
783 chunk.index += d;
784 chunk.length -= d;
785
786 pa_sink_render_into(s, &chunk);
787
788 d += chunk.length;
789 l -= chunk.length;
790 }
791
792 pa_sink_unref(s);
793 }
794
795 /* Called from IO thread context */
796 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
797 pa_sink_assert_ref(s);
798 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
799 pa_assert(length > 0);
800 pa_assert(pa_frame_aligned(length, &s->sample_spec));
801 pa_assert(result);
802
803 s->thread_info.rewind_nbytes = 0;
804
805 /*** This needs optimization ***/
806
807 result->index = 0;
808 result->length = length;
809 result->memblock = pa_memblock_new(s->core->mempool, length);
810
811 pa_sink_render_into_full(s, result);
812 }
813
814 /* Called from main thread */
815 pa_usec_t pa_sink_get_latency(pa_sink *s) {
816 pa_usec_t usec = 0;
817
818 pa_sink_assert_ref(s);
819 pa_assert(PA_SINK_IS_LINKED(s->state));
820
821 /* The returned value is supposed to be in the time domain of the sound card! */
822
823 if (!PA_SINK_IS_OPENED(s->state))
824 return 0;
825
826 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) == 0);
827
828 return usec;
829 }
830
831 /* Called from main thread */
832 void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume) {
833 pa_bool_t changed;
834
835 pa_sink_assert_ref(s);
836 pa_assert(PA_SINK_IS_LINKED(s->state));
837 pa_assert(volume);
838
839 changed = !pa_cvolume_equal(volume, &s->volume);
840 s->volume = *volume;
841
842 if (s->set_volume && s->set_volume(s) < 0)
843 s->set_volume = NULL;
844
845 if (!s->set_volume)
846 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, volume, 0, NULL);
847
848 if (changed)
849 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
850 }
851
852 /* Called from main thread */
853 const pa_cvolume *pa_sink_get_volume(pa_sink *s) {
854 pa_sink_assert_ref(s);
855 pa_assert(PA_SINK_IS_LINKED(s->state));
856
857 if (s->refresh_volume) {
858 struct pa_cvolume old_volume = s->volume;
859
860 if (s->get_volume && s->get_volume(s) < 0)
861 s->get_volume = NULL;
862
863 if (!s->get_volume)
864 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, &s->volume, 0, NULL);
865
866 if (!pa_cvolume_equal(&old_volume, &s->volume))
867 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
868 }
869
870 return &s->volume;
871 }
872
873 /* Called from main thread */
874 void pa_sink_set_mute(pa_sink *s, pa_bool_t mute) {
875 pa_bool_t changed;
876
877 pa_sink_assert_ref(s);
878 pa_assert(PA_SINK_IS_LINKED(s->state));
879
880 changed = s->muted != mute;
881 s->muted = mute;
882
883 if (s->set_mute && s->set_mute(s) < 0)
884 s->set_mute = NULL;
885
886 if (!s->set_mute)
887 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
888
889 if (changed)
890 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
891 }
892
893 /* Called from main thread */
894 pa_bool_t pa_sink_get_mute(pa_sink *s) {
895
896 pa_sink_assert_ref(s);
897 pa_assert(PA_SINK_IS_LINKED(s->state));
898
899 if (s->refresh_muted) {
900 pa_bool_t old_muted = s->muted;
901
902 if (s->get_mute && s->get_mute(s) < 0)
903 s->get_mute = NULL;
904
905 if (!s->get_mute)
906 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MUTE, &s->muted, 0, NULL);
907
908 if (old_muted != s->muted)
909 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
910 }
911
912 return s->muted;
913 }
914
915 /* Called from main thread */
916 void pa_sink_set_description(pa_sink *s, const char *description) {
917 const char *old;
918 pa_sink_assert_ref(s);
919
920 if (!description && !pa_proplist_contains(s->proplist, PA_PROP_DEVICE_DESCRIPTION))
921 return;
922
923 old = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
924
925 if (old && description && !strcmp(old, description))
926 return;
927
928 if (description)
929 pa_proplist_sets(s->proplist, PA_PROP_DEVICE_DESCRIPTION, description);
930 else
931 pa_proplist_unset(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
932
933 if (s->monitor_source) {
934 char *n;
935
936 n = pa_sprintf_malloc("Monitor Source of %s", description ? description : s->name);
937 pa_source_set_description(s->monitor_source, n);
938 pa_xfree(n);
939 }
940
941 if (PA_SINK_IS_LINKED(s->state)) {
942 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
943 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], s);
944 }
945 }
946
947 /* Called from main thread */
948 unsigned pa_sink_linked_by(pa_sink *s) {
949 unsigned ret;
950
951 pa_sink_assert_ref(s);
952 pa_assert(PA_SINK_IS_LINKED(s->state));
953
954 ret = pa_idxset_size(s->inputs);
955
956 /* We add in the number of streams connected to us here. Please
957 * not the asymmmetry to pa_sink_used_by()! */
958
959 if (s->monitor_source)
960 ret += pa_source_linked_by(s->monitor_source);
961
962 return ret;
963 }
964
965 /* Called from main thread */
966 unsigned pa_sink_used_by(pa_sink *s) {
967 unsigned ret;
968
969 pa_sink_assert_ref(s);
970 pa_assert(PA_SINK_IS_LINKED(s->state));
971
972 ret = pa_idxset_size(s->inputs);
973 pa_assert(ret >= s->n_corked);
974
975 /* Streams connected to our monitor source do not matter for
976 * pa_sink_used_by()!.*/
977
978 return ret - s->n_corked;
979 }
980
981 /* Called from IO thread, except when it is not */
982 int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
983 pa_sink *s = PA_SINK(o);
984 pa_sink_assert_ref(s);
985
986 switch ((pa_sink_message_t) code) {
987
988 case PA_SINK_MESSAGE_ADD_INPUT: {
989 pa_sink_input *i = PA_SINK_INPUT(userdata);
990
991 /* If you change anything here, make sure to change the
992 * sink input handling a few lines down at
993 * PA_SINK_MESSAGE_FINISH_MOVE, too. */
994
995 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
996
997 /* Since the caller sleeps in pa_sink_input_put(), we can
998 * safely access data outside of thread_info even though
999 * it is mutable */
1000
1001 if ((i->thread_info.sync_prev = i->sync_prev)) {
1002 pa_assert(i->sink == i->thread_info.sync_prev->sink);
1003 pa_assert(i->sync_prev->sync_next == i);
1004 i->thread_info.sync_prev->thread_info.sync_next = i;
1005 }
1006
1007 if ((i->thread_info.sync_next = i->sync_next)) {
1008 pa_assert(i->sink == i->thread_info.sync_next->sink);
1009 pa_assert(i->sync_next->sync_prev == i);
1010 i->thread_info.sync_next->thread_info.sync_prev = i;
1011 }
1012
1013 pa_assert(!i->thread_info.attached);
1014 i->thread_info.attached = TRUE;
1015
1016 if (i->attach)
1017 i->attach(i);
1018
1019 pa_sink_input_set_state_within_thread(i, i->state);
1020
1021 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
1022 pa_sink_input_update_max_request(i, s->thread_info.max_request);
1023
1024 pa_sink_invalidate_requested_latency(s);
1025
1026 /* We don't rewind here automatically. This is left to the
1027 * sink input implementor because some sink inputs need a
1028 * slow start, i.e. need some time to buffer client
1029 * samples before beginning streaming. */
1030
1031 return 0;
1032 }
1033
1034 case PA_SINK_MESSAGE_REMOVE_INPUT: {
1035 pa_sink_input *i = PA_SINK_INPUT(userdata);
1036
1037 /* If you change anything here, make sure to change the
1038 * sink input handling a few lines down at
1039 * PA_SINK_MESSAGE_PREPAPRE_MOVE, too. */
1040
1041 pa_sink_input_set_state_within_thread(i, i->state);
1042
1043 if (i->detach)
1044 i->detach(i);
1045
1046 pa_assert(i->thread_info.attached);
1047 i->thread_info.attached = FALSE;
1048
1049 /* Since the caller sleeps in pa_sink_input_unlink(),
1050 * we can safely access data outside of thread_info even
1051 * though it is mutable */
1052
1053 pa_assert(!i->thread_info.sync_prev);
1054 pa_assert(!i->thread_info.sync_next);
1055
1056 if (i->thread_info.sync_prev) {
1057 i->thread_info.sync_prev->thread_info.sync_next = i->thread_info.sync_prev->sync_next;
1058 i->thread_info.sync_prev = NULL;
1059 }
1060
1061 if (i->thread_info.sync_next) {
1062 i->thread_info.sync_next->thread_info.sync_prev = i->thread_info.sync_next->sync_prev;
1063 i->thread_info.sync_next = NULL;
1064 }
1065
1066 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
1067 pa_sink_input_unref(i);
1068
1069 pa_sink_invalidate_requested_latency(s);
1070 pa_sink_request_rewind(s, 0);
1071
1072 return 0;
1073 }
1074
1075 case PA_SINK_MESSAGE_START_MOVE: {
1076 pa_sink_input *i = PA_SINK_INPUT(userdata);
1077
1078 /* We don't support moving synchronized streams. */
1079 pa_assert(!i->sync_prev);
1080 pa_assert(!i->sync_next);
1081 pa_assert(!i->thread_info.sync_next);
1082 pa_assert(!i->thread_info.sync_prev);
1083
1084 if (i->thread_info.state != PA_SINK_INPUT_CORKED) {
1085 pa_usec_t usec = 0;
1086 size_t sink_nbytes, total_nbytes;
1087
1088 /* Get the latency of the sink */
1089 if (PA_MSGOBJECT(s)->process_msg(PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
1090 usec = 0;
1091
1092 sink_nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
1093 total_nbytes = sink_nbytes + pa_memblockq_get_length(i->thread_info.render_memblockq);
1094
1095 if (total_nbytes > 0) {
1096 i->thread_info.rewrite_nbytes = i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, total_nbytes) : total_nbytes;
1097 i->thread_info.rewrite_flush = TRUE;
1098 pa_sink_input_process_rewind(i, sink_nbytes);
1099 }
1100 }
1101
1102 if (i->detach)
1103 i->detach(i);
1104
1105 pa_assert(i->thread_info.attached);
1106 i->thread_info.attached = FALSE;
1107
1108 /* Let's remove the sink input ...*/
1109 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
1110 pa_sink_input_unref(i);
1111
1112 pa_sink_invalidate_requested_latency(s);
1113
1114 pa_log_debug("Requesting rewind due to started move");
1115 pa_sink_request_rewind(s, 0);
1116
1117 return 0;
1118 }
1119
1120 case PA_SINK_MESSAGE_FINISH_MOVE: {
1121 pa_sink_input *i = PA_SINK_INPUT(userdata);
1122
1123 /* We don't support moving synchronized streams. */
1124 pa_assert(!i->sync_prev);
1125 pa_assert(!i->sync_next);
1126 pa_assert(!i->thread_info.sync_next);
1127 pa_assert(!i->thread_info.sync_prev);
1128
1129 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
1130
1131 pa_assert(!i->thread_info.attached);
1132 i->thread_info.attached = TRUE;
1133
1134 if (i->attach)
1135 i->attach(i);
1136
1137 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
1138 pa_sink_input_update_max_request(i, s->thread_info.max_request);
1139
1140 pa_sink_input_set_requested_latency_within_thread(i, i->thread_info.requested_sink_latency);
1141
1142 if (i->thread_info.state != PA_SINK_INPUT_CORKED) {
1143 pa_usec_t usec = 0;
1144 size_t nbytes;
1145
1146 /* Get the latency of the sink */
1147 if (PA_MSGOBJECT(s)->process_msg(PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
1148 usec = 0;
1149
1150 nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
1151
1152 if (nbytes > 0)
1153 pa_sink_input_drop(i, nbytes);
1154
1155 pa_log_debug("Requesting rewind due to finished move");
1156 pa_sink_request_rewind(s, nbytes);
1157 }
1158
1159 return 0;
1160 }
1161
1162 case PA_SINK_MESSAGE_SET_VOLUME:
1163 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
1164
1165 pa_sink_request_rewind(s, 0);
1166 return 0;
1167
1168 case PA_SINK_MESSAGE_SET_MUTE:
1169 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
1170
1171 pa_sink_request_rewind(s, 0);
1172 return 0;
1173
1174 case PA_SINK_MESSAGE_GET_VOLUME:
1175 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
1176 return 0;
1177
1178 case PA_SINK_MESSAGE_GET_MUTE:
1179 *((pa_bool_t*) userdata) = s->thread_info.soft_muted;
1180 return 0;
1181
1182 case PA_SINK_MESSAGE_SET_STATE:
1183
1184 s->thread_info.state = PA_PTR_TO_UINT(userdata);
1185 return 0;
1186
1187 case PA_SINK_MESSAGE_DETACH:
1188
1189 /* Detach all streams */
1190 pa_sink_detach_within_thread(s);
1191 return 0;
1192
1193 case PA_SINK_MESSAGE_ATTACH:
1194
1195 /* Reattach all streams */
1196 pa_sink_attach_within_thread(s);
1197 return 0;
1198
1199 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY: {
1200
1201 pa_usec_t *usec = userdata;
1202 *usec = pa_sink_get_requested_latency_within_thread(s);
1203
1204 if (*usec == (pa_usec_t) -1)
1205 *usec = s->thread_info.max_latency;
1206
1207 return 0;
1208 }
1209
1210 case PA_SINK_MESSAGE_SET_LATENCY_RANGE: {
1211 pa_usec_t *r = userdata;
1212
1213 pa_sink_update_latency_range(s, r[0], r[1]);
1214
1215 return 0;
1216 }
1217
1218 case PA_SINK_MESSAGE_GET_LATENCY_RANGE: {
1219 pa_usec_t *r = userdata;
1220
1221 r[0] = s->thread_info.min_latency;
1222 r[1] = s->thread_info.max_latency;
1223
1224 return 0;
1225 }
1226
1227 case PA_SINK_MESSAGE_GET_MAX_REWIND:
1228
1229 *((size_t*) userdata) = s->thread_info.max_rewind;
1230 return 0;
1231
1232 case PA_SINK_MESSAGE_GET_MAX_REQUEST:
1233
1234 *((size_t*) userdata) = s->thread_info.max_request;
1235 return 0;
1236
1237 case PA_SINK_MESSAGE_GET_LATENCY:
1238 case PA_SINK_MESSAGE_MAX:
1239 ;
1240 }
1241
1242 return -1;
1243 }
1244
1245 /* Called from main thread */
1246 int pa_sink_suspend_all(pa_core *c, pa_bool_t suspend) {
1247 pa_sink *sink;
1248 uint32_t idx;
1249 int ret = 0;
1250
1251 pa_core_assert_ref(c);
1252
1253 for (sink = PA_SINK(pa_idxset_first(c->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(c->sinks, &idx)))
1254 ret -= pa_sink_suspend(sink, suspend) < 0;
1255
1256 return ret;
1257 }
1258
1259 /* Called from main thread */
1260 void pa_sink_detach(pa_sink *s) {
1261 pa_sink_assert_ref(s);
1262 pa_assert(PA_SINK_IS_LINKED(s->state));
1263
1264 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_DETACH, NULL, 0, NULL) == 0);
1265 }
1266
1267 /* Called from main thread */
1268 void pa_sink_attach(pa_sink *s) {
1269 pa_sink_assert_ref(s);
1270 pa_assert(PA_SINK_IS_LINKED(s->state));
1271
1272 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_ATTACH, NULL, 0, NULL) == 0);
1273 }
1274
1275 /* Called from IO thread */
1276 void pa_sink_detach_within_thread(pa_sink *s) {
1277 pa_sink_input *i;
1278 void *state = NULL;
1279
1280 pa_sink_assert_ref(s);
1281 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1282
1283 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1284 if (i->detach)
1285 i->detach(i);
1286
1287 if (s->monitor_source)
1288 pa_source_detach_within_thread(s->monitor_source);
1289 }
1290
1291 /* Called from IO thread */
1292 void pa_sink_attach_within_thread(pa_sink *s) {
1293 pa_sink_input *i;
1294 void *state = NULL;
1295
1296 pa_sink_assert_ref(s);
1297 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1298
1299 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1300 if (i->attach)
1301 i->attach(i);
1302
1303 if (s->monitor_source)
1304 pa_source_attach_within_thread(s->monitor_source);
1305 }
1306
1307 /* Called from IO thread */
1308 void pa_sink_request_rewind(pa_sink*s, size_t nbytes) {
1309 pa_sink_assert_ref(s);
1310 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1311
1312 if (nbytes <= 0)
1313 nbytes = s->thread_info.max_rewind;
1314
1315 nbytes = PA_MIN(nbytes, s->thread_info.max_rewind);
1316
1317 if (nbytes <= s->thread_info.rewind_nbytes)
1318 return;
1319
1320 s->thread_info.rewind_nbytes = nbytes;
1321
1322 if (s->request_rewind)
1323 s->request_rewind(s);
1324 }
1325
1326 /* Called from IO thread */
1327 pa_usec_t pa_sink_get_requested_latency_within_thread(pa_sink *s) {
1328 pa_usec_t result = (pa_usec_t) -1;
1329 pa_sink_input *i;
1330 void *state = NULL;
1331 pa_usec_t monitor_latency;
1332
1333 pa_sink_assert_ref(s);
1334
1335 if (s->thread_info.requested_latency_valid)
1336 return s->thread_info.requested_latency;
1337
1338 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1339
1340 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1 &&
1341 (result == (pa_usec_t) -1 || result > i->thread_info.requested_sink_latency))
1342 result = i->thread_info.requested_sink_latency;
1343
1344 monitor_latency = pa_source_get_requested_latency_within_thread(s->monitor_source);
1345
1346 if (monitor_latency != (pa_usec_t) -1 &&
1347 (result == (pa_usec_t) -1 || result > monitor_latency))
1348 result = monitor_latency;
1349
1350 if (result != (pa_usec_t) -1) {
1351 if (s->thread_info.max_latency > 0 && result > s->thread_info.max_latency)
1352 result = s->thread_info.max_latency;
1353
1354 if (s->thread_info.min_latency > 0 && result < s->thread_info.min_latency)
1355 result = s->thread_info.min_latency;
1356 }
1357
1358 s->thread_info.requested_latency = result;
1359 s->thread_info.requested_latency_valid = TRUE;
1360
1361 return result;
1362 }
1363
1364 /* Called from main thread */
1365 pa_usec_t pa_sink_get_requested_latency(pa_sink *s) {
1366 pa_usec_t usec = 0;
1367
1368 pa_sink_assert_ref(s);
1369 pa_assert(PA_SINK_IS_LINKED(s->state));
1370
1371 if (!PA_SINK_IS_OPENED(s->state))
1372 return 0;
1373
1374 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
1375 return usec;
1376 }
1377
1378 /* Called from IO thread */
1379 void pa_sink_set_max_rewind(pa_sink *s, size_t max_rewind) {
1380 pa_sink_input *i;
1381 void *state = NULL;
1382
1383 pa_sink_assert_ref(s);
1384
1385 if (max_rewind == s->thread_info.max_rewind)
1386 return;
1387
1388 s->thread_info.max_rewind = max_rewind;
1389
1390 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
1391 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1392 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
1393 }
1394
1395 if (s->monitor_source)
1396 pa_source_set_max_rewind(s->monitor_source, s->thread_info.max_rewind);
1397 }
1398
1399 /* Called from IO thread */
1400 void pa_sink_set_max_request(pa_sink *s, size_t max_request) {
1401 pa_sink_input *i;
1402 void *state = NULL;
1403
1404 pa_sink_assert_ref(s);
1405
1406 if (max_request == s->thread_info.max_request)
1407 return;
1408
1409 s->thread_info.max_request = max_request;
1410
1411 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
1412 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1413 pa_sink_input_update_max_request(i, s->thread_info.max_request);
1414 }
1415 }
1416
1417 /* Called from IO thread */
1418 void pa_sink_invalidate_requested_latency(pa_sink *s) {
1419 pa_sink_input *i;
1420 void *state = NULL;
1421
1422 pa_sink_assert_ref(s);
1423
1424 s->thread_info.requested_latency_valid = FALSE;
1425
1426 if (s->update_requested_latency)
1427 s->update_requested_latency(s);
1428
1429 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1430 if (i->update_sink_requested_latency)
1431 i->update_sink_requested_latency(i);
1432 }
1433
1434 /* Called from main thread */
1435 void pa_sink_set_latency_range(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency) {
1436 pa_sink_assert_ref(s);
1437
1438 /* min_latency == 0: no limit
1439 * min_latency == (size_t) -1: default limit
1440 * min_latency anything else: specified limit
1441 *
1442 * Similar for max_latency */
1443
1444 if (min_latency == (pa_usec_t) -1)
1445 min_latency = DEFAULT_MIN_LATENCY;
1446
1447 if (max_latency == (pa_usec_t) -1)
1448 max_latency = min_latency;
1449
1450 pa_assert(!min_latency || !max_latency ||
1451 min_latency <= max_latency);
1452
1453 if (PA_SINK_IS_LINKED(s->state)) {
1454 pa_usec_t r[2];
1455
1456 r[0] = min_latency;
1457 r[1] = max_latency;
1458
1459 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_LATENCY_RANGE, r, 0, NULL) == 0);
1460 } else {
1461 s->thread_info.min_latency = min_latency;
1462 s->thread_info.max_latency = max_latency;
1463
1464 s->monitor_source->thread_info.min_latency = min_latency;
1465 s->monitor_source->thread_info.max_latency = max_latency;
1466
1467 s->thread_info.requested_latency_valid = s->monitor_source->thread_info.requested_latency_valid = FALSE;
1468 }
1469 }
1470
1471 /* Called from main thread */
1472 void pa_sink_get_latency_range(pa_sink *s, pa_usec_t *min_latency, pa_usec_t *max_latency) {
1473 pa_sink_assert_ref(s);
1474 pa_assert(min_latency);
1475 pa_assert(max_latency);
1476
1477 if (PA_SINK_IS_LINKED(s->state)) {
1478 pa_usec_t r[2] = { 0, 0 };
1479
1480 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY_RANGE, r, 0, NULL) == 0);
1481
1482 *min_latency = r[0];
1483 *max_latency = r[1];
1484 } else {
1485 *min_latency = s->thread_info.min_latency;
1486 *max_latency = s->thread_info.max_latency;
1487 }
1488 }
1489
1490 /* Called from IO thread */
1491 void pa_sink_update_latency_range(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency) {
1492 pa_sink_input *i;
1493 void *state = NULL;
1494
1495 pa_sink_assert_ref(s);
1496
1497 s->thread_info.min_latency = min_latency;
1498 s->thread_info.max_latency = max_latency;
1499
1500 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1501 if (i->update_sink_latency_range)
1502 i->update_sink_latency_range(i);
1503
1504 pa_sink_invalidate_requested_latency(s);
1505
1506 pa_source_update_latency_range(s->monitor_source, min_latency, max_latency);
1507 }
1508
1509 size_t pa_sink_get_max_rewind(pa_sink *s) {
1510 size_t r;
1511 pa_sink_assert_ref(s);
1512
1513 if (!PA_SINK_IS_LINKED(s->state))
1514 return s->thread_info.max_rewind;
1515
1516 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MAX_REWIND, &r, 0, NULL) == 0);
1517
1518 return r;
1519 }
1520
1521 size_t pa_sink_get_max_request(pa_sink *s) {
1522 size_t r;
1523 pa_sink_assert_ref(s);
1524
1525 if (!PA_SINK_IS_LINKED(s->state))
1526 return s->thread_info.max_request;
1527
1528 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MAX_REQUEST, &r, 0, NULL) == 0);
1529
1530 return r;
1531 }