]> code.delx.au - pulseaudio/blob - src/pulsecore/sink.c
make sure to call process_rewind() under all circumstances before we do the next...
[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.rewind_requested = FALSE;
216 s->thread_info.max_rewind = 0;
217 s->thread_info.max_request = 0;
218 s->thread_info.requested_latency_valid = FALSE;
219 s->thread_info.requested_latency = 0;
220 s->thread_info.min_latency = DEFAULT_MIN_LATENCY;
221 s->thread_info.max_latency = 0;
222
223 pa_assert_se(pa_idxset_put(core->sinks, s, &s->index) >= 0);
224
225 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s",
226 s->index,
227 s->name,
228 pa_sample_spec_snprint(st, sizeof(st), &s->sample_spec),
229 pa_channel_map_snprint(cm, sizeof(cm), &s->channel_map));
230
231 pa_source_new_data_init(&source_data);
232 pa_source_new_data_set_sample_spec(&source_data, &s->sample_spec);
233 pa_source_new_data_set_channel_map(&source_data, &s->channel_map);
234 source_data.name = pa_sprintf_malloc("%s.monitor", name);
235 source_data.driver = data->driver;
236 source_data.module = data->module;
237
238 dn = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
239 pa_proplist_setf(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Monitor of %s", dn ? dn : s->name);
240 pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_CLASS, "monitor");
241
242 s->monitor_source = pa_source_new(core, &source_data, 0);
243
244 pa_source_new_data_done(&source_data);
245
246 if (!s->monitor_source) {
247 pa_sink_unlink(s);
248 pa_sink_unref(s);
249 return NULL;
250 }
251
252 s->monitor_source->monitor_of = s;
253
254 pa_source_set_latency_range(s->monitor_source, s->thread_info.min_latency, s->thread_info.max_latency);
255 pa_source_set_max_rewind(s->monitor_source, s->thread_info.max_rewind);
256
257 return s;
258 }
259
260 /* Called from main context */
261 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
262 int ret;
263 pa_bool_t suspend_change;
264
265 pa_assert(s);
266
267 if (s->state == state)
268 return 0;
269
270 suspend_change =
271 (s->state == PA_SINK_SUSPENDED && PA_SINK_IS_OPENED(state)) ||
272 (PA_SINK_IS_OPENED(s->state) && state == PA_SINK_SUSPENDED);
273
274 if (s->set_state)
275 if ((ret = s->set_state(s, state)) < 0)
276 return -1;
277
278 if (s->asyncmsgq)
279 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) == 0);
280
281 s->state = state;
282
283 if (suspend_change) {
284 pa_sink_input *i;
285 uint32_t idx;
286
287 /* We're suspending or resuming, tell everyone about it */
288
289 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx)))
290 if (i->suspend)
291 i->suspend(i, state == PA_SINK_SUSPENDED);
292 }
293
294 if (state != PA_SINK_UNLINKED) /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
295 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], s);
296
297 return 0;
298 }
299
300 /* Called from main context */
301 void pa_sink_put(pa_sink* s) {
302 pa_sink_assert_ref(s);
303
304 pa_assert(s->state == PA_SINK_INIT);
305
306 /* The following fields must be initialized properly when calling _put() */
307 pa_assert(s->asyncmsgq);
308 pa_assert(s->rtpoll);
309 pa_assert(!s->thread_info.min_latency || !s->thread_info.max_latency ||
310 s->thread_info.min_latency <= s->thread_info.max_latency);
311
312 if (!(s->flags & PA_SINK_HW_VOLUME_CTRL)) {
313 s->flags |= PA_SINK_DECIBEL_VOLUME;
314
315 s->thread_info.soft_volume = s->volume;
316 s->thread_info.soft_muted = s->muted;
317 }
318
319 pa_assert_se(sink_set_state(s, PA_SINK_IDLE) == 0);
320
321 pa_source_put(s->monitor_source);
322
323 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
324 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PUT], s);
325 }
326
327 /* Called from main context */
328 void pa_sink_unlink(pa_sink* s) {
329 pa_bool_t linked;
330 pa_sink_input *i, *j = NULL;
331
332 pa_assert(s);
333
334 /* Please note that pa_sink_unlink() does more than simply
335 * reversing pa_sink_put(). It also undoes the registrations
336 * already done in pa_sink_new()! */
337
338 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
339 * may be called multiple times on the same sink without bad
340 * effects. */
341
342 linked = PA_SINK_IS_LINKED(s->state);
343
344 if (linked)
345 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK], s);
346
347 if (s->state != PA_SINK_UNLINKED)
348 pa_namereg_unregister(s->core, s->name);
349 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
350
351 while ((i = pa_idxset_first(s->inputs, NULL))) {
352 pa_assert(i != j);
353 pa_sink_input_kill(i);
354 j = i;
355 }
356
357 if (linked)
358 sink_set_state(s, PA_SINK_UNLINKED);
359 else
360 s->state = PA_SINK_UNLINKED;
361
362 reset_callbacks(s);
363
364 if (s->monitor_source)
365 pa_source_unlink(s->monitor_source);
366
367 if (linked) {
368 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
369 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], s);
370 }
371 }
372
373 /* Called from main context */
374 static void sink_free(pa_object *o) {
375 pa_sink *s = PA_SINK(o);
376 pa_sink_input *i;
377
378 pa_assert(s);
379 pa_assert(pa_sink_refcnt(s) == 0);
380
381 if (PA_SINK_IS_LINKED(s->state))
382 pa_sink_unlink(s);
383
384 pa_log_info("Freeing sink %u \"%s\"", s->index, s->name);
385
386 if (s->monitor_source) {
387 pa_source_unref(s->monitor_source);
388 s->monitor_source = NULL;
389 }
390
391 pa_idxset_free(s->inputs, NULL, NULL);
392
393 while ((i = pa_hashmap_steal_first(s->thread_info.inputs)))
394 pa_sink_input_unref(i);
395
396 pa_hashmap_free(s->thread_info.inputs, NULL, NULL);
397
398 if (s->silence.memblock)
399 pa_memblock_unref(s->silence.memblock);
400
401 pa_xfree(s->name);
402 pa_xfree(s->driver);
403
404 if (s->proplist)
405 pa_proplist_free(s->proplist);
406
407 pa_xfree(s);
408 }
409
410 /* Called from main context */
411 void pa_sink_set_asyncmsgq(pa_sink *s, pa_asyncmsgq *q) {
412 pa_sink_assert_ref(s);
413
414 s->asyncmsgq = q;
415
416 if (s->monitor_source)
417 pa_source_set_asyncmsgq(s->monitor_source, q);
418 }
419
420 /* Called from main context */
421 void pa_sink_set_rtpoll(pa_sink *s, pa_rtpoll *p) {
422 pa_sink_assert_ref(s);
423
424 s->rtpoll = p;
425 if (s->monitor_source)
426 pa_source_set_rtpoll(s->monitor_source, p);
427 }
428
429 /* Called from main context */
430 int pa_sink_update_status(pa_sink*s) {
431 pa_sink_assert_ref(s);
432 pa_assert(PA_SINK_IS_LINKED(s->state));
433
434 if (s->state == PA_SINK_SUSPENDED)
435 return 0;
436
437 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
438 }
439
440 /* Called from main context */
441 int pa_sink_suspend(pa_sink *s, pa_bool_t suspend) {
442 pa_sink_assert_ref(s);
443 pa_assert(PA_SINK_IS_LINKED(s->state));
444
445 if (suspend)
446 return sink_set_state(s, PA_SINK_SUSPENDED);
447 else
448 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
449 }
450
451 /* Called from IO thread context */
452 void pa_sink_process_rewind(pa_sink *s, size_t nbytes) {
453 pa_sink_input *i;
454 void *state = NULL;
455 pa_sink_assert_ref(s);
456 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
457
458 s->thread_info.rewind_nbytes = 0;
459 s->thread_info.rewind_requested = FALSE;
460
461 if (nbytes > 0)
462 pa_log_debug("Processing rewind...");
463
464 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
465 pa_sink_input_assert_ref(i);
466 pa_sink_input_process_rewind(i, nbytes);
467 }
468
469 if (nbytes > 0)
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 pa_assert(!s->thread_info.rewind_requested);
624 pa_assert(s->thread_info.rewind_nbytes == 0);
625
626 if (length <= 0)
627 length = pa_frame_align(MIX_BUFFER_LENGTH, &s->sample_spec);
628
629 block_size_max = pa_mempool_block_size_max(s->core->mempool);
630 if (length > block_size_max)
631 length = pa_frame_align(block_size_max, &s->sample_spec);
632
633 pa_assert(length > 0);
634
635 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, &length, info, MAX_MIX_CHANNELS) : 0;
636
637 if (n == 0) {
638
639 *result = s->silence;
640 pa_memblock_ref(result->memblock);
641
642 if (result->length > length)
643 result->length = length;
644
645 } else if (n == 1) {
646 pa_cvolume volume;
647
648 *result = info[0].chunk;
649 pa_memblock_ref(result->memblock);
650
651 if (result->length > length)
652 result->length = length;
653
654 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
655
656 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&volume)) {
657 pa_log("adjusting volume ");
658 pa_memchunk_make_writable(result, 0);
659 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
660 pa_silence_memchunk(result, &s->sample_spec);
661 else
662 pa_volume_memchunk(result, &s->sample_spec, &volume);
663 }
664 } else {
665 void *ptr;
666 result->memblock = pa_memblock_new(s->core->mempool, length);
667
668 ptr = pa_memblock_acquire(result->memblock);
669 result->length = pa_mix(info, n,
670 ptr, length,
671 &s->sample_spec,
672 &s->thread_info.soft_volume,
673 s->thread_info.soft_muted);
674 pa_memblock_release(result->memblock);
675
676 result->index = 0;
677 }
678
679 if (s->thread_info.state == PA_SINK_RUNNING)
680 inputs_drop(s, info, n, result);
681
682 pa_sink_unref(s);
683 }
684
685 /* Called from IO thread context */
686 void pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
687 pa_mix_info info[MAX_MIX_CHANNELS];
688 unsigned n;
689 size_t length, block_size_max;
690
691 pa_sink_assert_ref(s);
692 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
693 pa_assert(target);
694 pa_assert(target->memblock);
695 pa_assert(target->length > 0);
696 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
697
698 pa_sink_ref(s);
699
700 pa_assert(!s->thread_info.rewind_requested);
701 pa_assert(s->thread_info.rewind_nbytes == 0);
702
703 length = target->length;
704 block_size_max = pa_mempool_block_size_max(s->core->mempool);
705 if (length > block_size_max)
706 length = pa_frame_align(block_size_max, &s->sample_spec);
707
708 pa_assert(length > 0);
709
710 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, &length, info, MAX_MIX_CHANNELS) : 0;
711
712 if (n == 0) {
713 if (target->length > length)
714 target->length = length;
715
716 pa_silence_memchunk(target, &s->sample_spec);
717 } else if (n == 1) {
718 pa_cvolume volume;
719
720 if (target->length > length)
721 target->length = length;
722
723 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
724
725 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
726 pa_silence_memchunk(target, &s->sample_spec);
727 else {
728 pa_memchunk vchunk;
729
730 vchunk = info[0].chunk;
731 pa_memblock_ref(vchunk.memblock);
732
733 if (vchunk.length > length)
734 vchunk.length = length;
735
736 if (!pa_cvolume_is_norm(&volume)) {
737 pa_memchunk_make_writable(&vchunk, 0);
738 pa_volume_memchunk(&vchunk, &s->sample_spec, &volume);
739 }
740
741 pa_memchunk_memcpy(target, &vchunk);
742 pa_memblock_unref(vchunk.memblock);
743 }
744
745 } else {
746 void *ptr;
747
748 ptr = pa_memblock_acquire(target->memblock);
749
750 target->length = pa_mix(info, n,
751 (uint8_t*) ptr + target->index, length,
752 &s->sample_spec,
753 &s->thread_info.soft_volume,
754 s->thread_info.soft_muted);
755
756 pa_memblock_release(target->memblock);
757 }
758
759 if (s->thread_info.state == PA_SINK_RUNNING)
760 inputs_drop(s, info, n, target);
761
762 pa_sink_unref(s);
763 }
764
765 /* Called from IO thread context */
766 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
767 pa_memchunk chunk;
768 size_t l, d;
769
770 pa_sink_assert_ref(s);
771 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
772 pa_assert(target);
773 pa_assert(target->memblock);
774 pa_assert(target->length > 0);
775 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
776
777 pa_sink_ref(s);
778
779 pa_assert(!s->thread_info.rewind_requested);
780 pa_assert(s->thread_info.rewind_nbytes == 0);
781
782 l = target->length;
783 d = 0;
784 while (l > 0) {
785 chunk = *target;
786 chunk.index += d;
787 chunk.length -= d;
788
789 pa_sink_render_into(s, &chunk);
790
791 d += chunk.length;
792 l -= chunk.length;
793 }
794
795 pa_sink_unref(s);
796 }
797
798 /* Called from IO thread context */
799 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
800 pa_sink_assert_ref(s);
801 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
802 pa_assert(length > 0);
803 pa_assert(pa_frame_aligned(length, &s->sample_spec));
804 pa_assert(result);
805
806 pa_assert(!s->thread_info.rewind_requested);
807 pa_assert(s->thread_info.rewind_nbytes == 0);
808
809 /*** This needs optimization ***/
810
811 result->index = 0;
812 result->length = length;
813 result->memblock = pa_memblock_new(s->core->mempool, length);
814
815 pa_sink_render_into_full(s, result);
816 }
817
818 /* Called from main thread */
819 pa_usec_t pa_sink_get_latency(pa_sink *s) {
820 pa_usec_t usec = 0;
821
822 pa_sink_assert_ref(s);
823 pa_assert(PA_SINK_IS_LINKED(s->state));
824
825 /* The returned value is supposed to be in the time domain of the sound card! */
826
827 if (!PA_SINK_IS_OPENED(s->state))
828 return 0;
829
830 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) == 0);
831
832 return usec;
833 }
834
835 /* Called from main thread */
836 void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume) {
837 pa_bool_t changed;
838
839 pa_sink_assert_ref(s);
840 pa_assert(PA_SINK_IS_LINKED(s->state));
841 pa_assert(volume);
842
843 changed = !pa_cvolume_equal(volume, &s->volume);
844 s->volume = *volume;
845
846 if (s->set_volume && s->set_volume(s) < 0)
847 s->set_volume = NULL;
848
849 if (!s->set_volume)
850 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, volume, 0, NULL);
851
852 if (changed)
853 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
854 }
855
856 /* Called from main thread */
857 const pa_cvolume *pa_sink_get_volume(pa_sink *s) {
858 pa_sink_assert_ref(s);
859 pa_assert(PA_SINK_IS_LINKED(s->state));
860
861 if (s->refresh_volume) {
862 struct pa_cvolume old_volume = s->volume;
863
864 if (s->get_volume && s->get_volume(s) < 0)
865 s->get_volume = NULL;
866
867 if (!s->get_volume)
868 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, &s->volume, 0, NULL);
869
870 if (!pa_cvolume_equal(&old_volume, &s->volume))
871 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
872 }
873
874 return &s->volume;
875 }
876
877 /* Called from main thread */
878 void pa_sink_set_mute(pa_sink *s, pa_bool_t mute) {
879 pa_bool_t changed;
880
881 pa_sink_assert_ref(s);
882 pa_assert(PA_SINK_IS_LINKED(s->state));
883
884 changed = s->muted != mute;
885 s->muted = mute;
886
887 if (s->set_mute && s->set_mute(s) < 0)
888 s->set_mute = NULL;
889
890 if (!s->set_mute)
891 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
892
893 if (changed)
894 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
895 }
896
897 /* Called from main thread */
898 pa_bool_t pa_sink_get_mute(pa_sink *s) {
899
900 pa_sink_assert_ref(s);
901 pa_assert(PA_SINK_IS_LINKED(s->state));
902
903 if (s->refresh_muted) {
904 pa_bool_t old_muted = s->muted;
905
906 if (s->get_mute && s->get_mute(s) < 0)
907 s->get_mute = NULL;
908
909 if (!s->get_mute)
910 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MUTE, &s->muted, 0, NULL);
911
912 if (old_muted != s->muted)
913 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
914 }
915
916 return s->muted;
917 }
918
919 /* Called from main thread */
920 void pa_sink_set_description(pa_sink *s, const char *description) {
921 const char *old;
922 pa_sink_assert_ref(s);
923
924 if (!description && !pa_proplist_contains(s->proplist, PA_PROP_DEVICE_DESCRIPTION))
925 return;
926
927 old = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
928
929 if (old && description && !strcmp(old, description))
930 return;
931
932 if (description)
933 pa_proplist_sets(s->proplist, PA_PROP_DEVICE_DESCRIPTION, description);
934 else
935 pa_proplist_unset(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
936
937 if (s->monitor_source) {
938 char *n;
939
940 n = pa_sprintf_malloc("Monitor Source of %s", description ? description : s->name);
941 pa_source_set_description(s->monitor_source, n);
942 pa_xfree(n);
943 }
944
945 if (PA_SINK_IS_LINKED(s->state)) {
946 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
947 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], s);
948 }
949 }
950
951 /* Called from main thread */
952 unsigned pa_sink_linked_by(pa_sink *s) {
953 unsigned ret;
954
955 pa_sink_assert_ref(s);
956 pa_assert(PA_SINK_IS_LINKED(s->state));
957
958 ret = pa_idxset_size(s->inputs);
959
960 /* We add in the number of streams connected to us here. Please
961 * not the asymmmetry to pa_sink_used_by()! */
962
963 if (s->monitor_source)
964 ret += pa_source_linked_by(s->monitor_source);
965
966 return ret;
967 }
968
969 /* Called from main thread */
970 unsigned pa_sink_used_by(pa_sink *s) {
971 unsigned ret;
972
973 pa_sink_assert_ref(s);
974 pa_assert(PA_SINK_IS_LINKED(s->state));
975
976 ret = pa_idxset_size(s->inputs);
977 pa_assert(ret >= s->n_corked);
978
979 /* Streams connected to our monitor source do not matter for
980 * pa_sink_used_by()!.*/
981
982 return ret - s->n_corked;
983 }
984
985 /* Called from IO thread, except when it is not */
986 int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
987 pa_sink *s = PA_SINK(o);
988 pa_sink_assert_ref(s);
989
990 switch ((pa_sink_message_t) code) {
991
992 case PA_SINK_MESSAGE_ADD_INPUT: {
993 pa_sink_input *i = PA_SINK_INPUT(userdata);
994
995 /* If you change anything here, make sure to change the
996 * sink input handling a few lines down at
997 * PA_SINK_MESSAGE_FINISH_MOVE, too. */
998
999 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
1000
1001 /* Since the caller sleeps in pa_sink_input_put(), we can
1002 * safely access data outside of thread_info even though
1003 * it is mutable */
1004
1005 if ((i->thread_info.sync_prev = i->sync_prev)) {
1006 pa_assert(i->sink == i->thread_info.sync_prev->sink);
1007 pa_assert(i->sync_prev->sync_next == i);
1008 i->thread_info.sync_prev->thread_info.sync_next = i;
1009 }
1010
1011 if ((i->thread_info.sync_next = i->sync_next)) {
1012 pa_assert(i->sink == i->thread_info.sync_next->sink);
1013 pa_assert(i->sync_next->sync_prev == i);
1014 i->thread_info.sync_next->thread_info.sync_prev = i;
1015 }
1016
1017 pa_assert(!i->thread_info.attached);
1018 i->thread_info.attached = TRUE;
1019
1020 if (i->attach)
1021 i->attach(i);
1022
1023 pa_sink_input_set_state_within_thread(i, i->state);
1024
1025 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
1026 pa_sink_input_update_max_request(i, s->thread_info.max_request);
1027
1028 pa_sink_invalidate_requested_latency(s);
1029
1030 /* We don't rewind here automatically. This is left to the
1031 * sink input implementor because some sink inputs need a
1032 * slow start, i.e. need some time to buffer client
1033 * samples before beginning streaming. */
1034
1035 return 0;
1036 }
1037
1038 case PA_SINK_MESSAGE_REMOVE_INPUT: {
1039 pa_sink_input *i = PA_SINK_INPUT(userdata);
1040
1041 /* If you change anything here, make sure to change the
1042 * sink input handling a few lines down at
1043 * PA_SINK_MESSAGE_PREPAPRE_MOVE, too. */
1044
1045 if (i->detach)
1046 i->detach(i);
1047
1048 pa_sink_input_set_state_within_thread(i, i->state);
1049
1050 pa_assert(i->thread_info.attached);
1051 i->thread_info.attached = FALSE;
1052
1053 /* Since the caller sleeps in pa_sink_input_unlink(),
1054 * we can safely access data outside of thread_info even
1055 * though it is mutable */
1056
1057 pa_assert(!i->thread_info.sync_prev);
1058 pa_assert(!i->thread_info.sync_next);
1059
1060 if (i->thread_info.sync_prev) {
1061 i->thread_info.sync_prev->thread_info.sync_next = i->thread_info.sync_prev->sync_next;
1062 i->thread_info.sync_prev = NULL;
1063 }
1064
1065 if (i->thread_info.sync_next) {
1066 i->thread_info.sync_next->thread_info.sync_prev = i->thread_info.sync_next->sync_prev;
1067 i->thread_info.sync_next = NULL;
1068 }
1069
1070 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
1071 pa_sink_input_unref(i);
1072
1073 pa_sink_invalidate_requested_latency(s);
1074 pa_sink_request_rewind(s, (size_t) -1);
1075
1076 return 0;
1077 }
1078
1079 case PA_SINK_MESSAGE_START_MOVE: {
1080 pa_sink_input *i = PA_SINK_INPUT(userdata);
1081
1082 /* We don't support moving synchronized streams. */
1083 pa_assert(!i->sync_prev);
1084 pa_assert(!i->sync_next);
1085 pa_assert(!i->thread_info.sync_next);
1086 pa_assert(!i->thread_info.sync_prev);
1087
1088 if (i->thread_info.state != PA_SINK_INPUT_CORKED) {
1089 pa_usec_t usec = 0;
1090 size_t sink_nbytes, total_nbytes;
1091
1092 /* Get the latency of the sink */
1093 if (PA_MSGOBJECT(s)->process_msg(PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
1094 usec = 0;
1095
1096 sink_nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
1097 total_nbytes = sink_nbytes + pa_memblockq_get_length(i->thread_info.render_memblockq);
1098
1099 if (total_nbytes > 0) {
1100 i->thread_info.rewrite_nbytes = i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, total_nbytes) : total_nbytes;
1101 i->thread_info.rewrite_flush = TRUE;
1102 pa_sink_input_process_rewind(i, sink_nbytes);
1103 }
1104 }
1105
1106 if (i->detach)
1107 i->detach(i);
1108
1109 pa_assert(i->thread_info.attached);
1110 i->thread_info.attached = FALSE;
1111
1112 /* Let's remove the sink input ...*/
1113 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
1114 pa_sink_input_unref(i);
1115
1116 pa_sink_invalidate_requested_latency(s);
1117
1118 pa_log_debug("Requesting rewind due to started move");
1119 pa_sink_request_rewind(s, (size_t) -1);
1120
1121 return 0;
1122 }
1123
1124 case PA_SINK_MESSAGE_FINISH_MOVE: {
1125 pa_sink_input *i = PA_SINK_INPUT(userdata);
1126
1127 /* We don't support moving synchronized streams. */
1128 pa_assert(!i->sync_prev);
1129 pa_assert(!i->sync_next);
1130 pa_assert(!i->thread_info.sync_next);
1131 pa_assert(!i->thread_info.sync_prev);
1132
1133 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
1134
1135 pa_assert(!i->thread_info.attached);
1136 i->thread_info.attached = TRUE;
1137
1138 if (i->attach)
1139 i->attach(i);
1140
1141 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
1142 pa_sink_input_update_max_request(i, s->thread_info.max_request);
1143
1144 pa_sink_input_set_requested_latency_within_thread(i, i->thread_info.requested_sink_latency);
1145
1146 if (i->thread_info.state != PA_SINK_INPUT_CORKED) {
1147 pa_usec_t usec = 0;
1148 size_t nbytes;
1149
1150 /* Get the latency of the sink */
1151 if (PA_MSGOBJECT(s)->process_msg(PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
1152 usec = 0;
1153
1154 nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
1155
1156 if (nbytes > 0)
1157 pa_sink_input_drop(i, nbytes);
1158
1159 pa_log_debug("Requesting rewind due to finished move");
1160 pa_sink_request_rewind(s, nbytes);
1161 }
1162
1163 return 0;
1164 }
1165
1166 case PA_SINK_MESSAGE_SET_VOLUME:
1167 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
1168
1169 pa_sink_request_rewind(s, (size_t) -1);
1170 return 0;
1171
1172 case PA_SINK_MESSAGE_SET_MUTE:
1173 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
1174
1175 pa_sink_request_rewind(s, (size_t) -1);
1176 return 0;
1177
1178 case PA_SINK_MESSAGE_GET_VOLUME:
1179 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
1180 return 0;
1181
1182 case PA_SINK_MESSAGE_GET_MUTE:
1183 *((pa_bool_t*) userdata) = s->thread_info.soft_muted;
1184 return 0;
1185
1186 case PA_SINK_MESSAGE_SET_STATE:
1187
1188 s->thread_info.state = PA_PTR_TO_UINT(userdata);
1189 return 0;
1190
1191 case PA_SINK_MESSAGE_DETACH:
1192
1193 /* Detach all streams */
1194 pa_sink_detach_within_thread(s);
1195 return 0;
1196
1197 case PA_SINK_MESSAGE_ATTACH:
1198
1199 /* Reattach all streams */
1200 pa_sink_attach_within_thread(s);
1201 return 0;
1202
1203 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY: {
1204
1205 pa_usec_t *usec = userdata;
1206 *usec = pa_sink_get_requested_latency_within_thread(s);
1207
1208 if (*usec == (pa_usec_t) -1)
1209 *usec = s->thread_info.max_latency;
1210
1211 return 0;
1212 }
1213
1214 case PA_SINK_MESSAGE_SET_LATENCY_RANGE: {
1215 pa_usec_t *r = userdata;
1216
1217 pa_sink_update_latency_range(s, r[0], r[1]);
1218
1219 return 0;
1220 }
1221
1222 case PA_SINK_MESSAGE_GET_LATENCY_RANGE: {
1223 pa_usec_t *r = userdata;
1224
1225 r[0] = s->thread_info.min_latency;
1226 r[1] = s->thread_info.max_latency;
1227
1228 return 0;
1229 }
1230
1231 case PA_SINK_MESSAGE_GET_MAX_REWIND:
1232
1233 *((size_t*) userdata) = s->thread_info.max_rewind;
1234 return 0;
1235
1236 case PA_SINK_MESSAGE_GET_MAX_REQUEST:
1237
1238 *((size_t*) userdata) = s->thread_info.max_request;
1239 return 0;
1240
1241 case PA_SINK_MESSAGE_GET_LATENCY:
1242 case PA_SINK_MESSAGE_MAX:
1243 ;
1244 }
1245
1246 return -1;
1247 }
1248
1249 /* Called from main thread */
1250 int pa_sink_suspend_all(pa_core *c, pa_bool_t suspend) {
1251 pa_sink *sink;
1252 uint32_t idx;
1253 int ret = 0;
1254
1255 pa_core_assert_ref(c);
1256
1257 for (sink = PA_SINK(pa_idxset_first(c->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(c->sinks, &idx)))
1258 ret -= pa_sink_suspend(sink, suspend) < 0;
1259
1260 return ret;
1261 }
1262
1263 /* Called from main thread */
1264 void pa_sink_detach(pa_sink *s) {
1265 pa_sink_assert_ref(s);
1266 pa_assert(PA_SINK_IS_LINKED(s->state));
1267
1268 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_DETACH, NULL, 0, NULL) == 0);
1269 }
1270
1271 /* Called from main thread */
1272 void pa_sink_attach(pa_sink *s) {
1273 pa_sink_assert_ref(s);
1274 pa_assert(PA_SINK_IS_LINKED(s->state));
1275
1276 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_ATTACH, NULL, 0, NULL) == 0);
1277 }
1278
1279 /* Called from IO thread */
1280 void pa_sink_detach_within_thread(pa_sink *s) {
1281 pa_sink_input *i;
1282 void *state = NULL;
1283
1284 pa_sink_assert_ref(s);
1285 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1286
1287 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1288 if (i->detach)
1289 i->detach(i);
1290
1291 if (s->monitor_source)
1292 pa_source_detach_within_thread(s->monitor_source);
1293 }
1294
1295 /* Called from IO thread */
1296 void pa_sink_attach_within_thread(pa_sink *s) {
1297 pa_sink_input *i;
1298 void *state = NULL;
1299
1300 pa_sink_assert_ref(s);
1301 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1302
1303 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1304 if (i->attach)
1305 i->attach(i);
1306
1307 if (s->monitor_source)
1308 pa_source_attach_within_thread(s->monitor_source);
1309 }
1310
1311 /* Called from IO thread */
1312 void pa_sink_request_rewind(pa_sink*s, size_t nbytes) {
1313 pa_sink_assert_ref(s);
1314 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1315
1316 if (nbytes == (size_t) -1)
1317 nbytes = s->thread_info.max_rewind;
1318
1319 nbytes = PA_MIN(nbytes, s->thread_info.max_rewind);
1320
1321 if (s->thread_info.rewind_requested &&
1322 nbytes <= s->thread_info.rewind_nbytes)
1323 return;
1324
1325 s->thread_info.rewind_nbytes = nbytes;
1326 s->thread_info.rewind_requested = TRUE;
1327
1328 if (s->request_rewind)
1329 s->request_rewind(s);
1330 }
1331
1332 /* Called from IO thread */
1333 pa_usec_t pa_sink_get_requested_latency_within_thread(pa_sink *s) {
1334 pa_usec_t result = (pa_usec_t) -1;
1335 pa_sink_input *i;
1336 void *state = NULL;
1337 pa_usec_t monitor_latency;
1338
1339 pa_sink_assert_ref(s);
1340
1341 if (s->thread_info.requested_latency_valid)
1342 return s->thread_info.requested_latency;
1343
1344 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1345
1346 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1 &&
1347 (result == (pa_usec_t) -1 || result > i->thread_info.requested_sink_latency))
1348 result = i->thread_info.requested_sink_latency;
1349
1350 monitor_latency = pa_source_get_requested_latency_within_thread(s->monitor_source);
1351
1352 if (monitor_latency != (pa_usec_t) -1 &&
1353 (result == (pa_usec_t) -1 || result > monitor_latency))
1354 result = monitor_latency;
1355
1356 if (result != (pa_usec_t) -1) {
1357 if (s->thread_info.max_latency > 0 && result > s->thread_info.max_latency)
1358 result = s->thread_info.max_latency;
1359
1360 if (s->thread_info.min_latency > 0 && result < s->thread_info.min_latency)
1361 result = s->thread_info.min_latency;
1362 }
1363
1364 s->thread_info.requested_latency = result;
1365 s->thread_info.requested_latency_valid = TRUE;
1366
1367 return result;
1368 }
1369
1370 /* Called from main thread */
1371 pa_usec_t pa_sink_get_requested_latency(pa_sink *s) {
1372 pa_usec_t usec = 0;
1373
1374 pa_sink_assert_ref(s);
1375 pa_assert(PA_SINK_IS_LINKED(s->state));
1376
1377 if (!PA_SINK_IS_OPENED(s->state))
1378 return 0;
1379
1380 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
1381 return usec;
1382 }
1383
1384 /* Called from IO thread */
1385 void pa_sink_set_max_rewind(pa_sink *s, size_t max_rewind) {
1386 pa_sink_input *i;
1387 void *state = NULL;
1388
1389 pa_sink_assert_ref(s);
1390
1391 if (max_rewind == s->thread_info.max_rewind)
1392 return;
1393
1394 s->thread_info.max_rewind = max_rewind;
1395
1396 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
1397 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1398 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
1399 }
1400
1401 if (s->monitor_source)
1402 pa_source_set_max_rewind(s->monitor_source, s->thread_info.max_rewind);
1403 }
1404
1405 /* Called from IO thread */
1406 void pa_sink_set_max_request(pa_sink *s, size_t max_request) {
1407 pa_sink_input *i;
1408 void *state = NULL;
1409
1410 pa_sink_assert_ref(s);
1411
1412 if (max_request == s->thread_info.max_request)
1413 return;
1414
1415 s->thread_info.max_request = max_request;
1416
1417 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
1418 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1419 pa_sink_input_update_max_request(i, s->thread_info.max_request);
1420 }
1421 }
1422
1423 /* Called from IO thread */
1424 void pa_sink_invalidate_requested_latency(pa_sink *s) {
1425 pa_sink_input *i;
1426 void *state = NULL;
1427
1428 pa_sink_assert_ref(s);
1429
1430 s->thread_info.requested_latency_valid = FALSE;
1431
1432 if (s->update_requested_latency)
1433 s->update_requested_latency(s);
1434
1435 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1436 if (i->update_sink_requested_latency)
1437 i->update_sink_requested_latency(i);
1438 }
1439
1440 /* Called from main thread */
1441 void pa_sink_set_latency_range(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency) {
1442 pa_sink_assert_ref(s);
1443
1444 /* min_latency == 0: no limit
1445 * min_latency == (size_t) -1: default limit
1446 * min_latency anything else: specified limit
1447 *
1448 * Similar for max_latency */
1449
1450 if (min_latency == (pa_usec_t) -1)
1451 min_latency = DEFAULT_MIN_LATENCY;
1452
1453 if (max_latency == (pa_usec_t) -1)
1454 max_latency = min_latency;
1455
1456 pa_assert(!min_latency || !max_latency ||
1457 min_latency <= max_latency);
1458
1459 if (PA_SINK_IS_LINKED(s->state)) {
1460 pa_usec_t r[2];
1461
1462 r[0] = min_latency;
1463 r[1] = max_latency;
1464
1465 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_LATENCY_RANGE, r, 0, NULL) == 0);
1466 } else {
1467 s->thread_info.min_latency = min_latency;
1468 s->thread_info.max_latency = max_latency;
1469
1470 s->monitor_source->thread_info.min_latency = min_latency;
1471 s->monitor_source->thread_info.max_latency = max_latency;
1472
1473 s->thread_info.requested_latency_valid = s->monitor_source->thread_info.requested_latency_valid = FALSE;
1474 }
1475 }
1476
1477 /* Called from main thread */
1478 void pa_sink_get_latency_range(pa_sink *s, pa_usec_t *min_latency, pa_usec_t *max_latency) {
1479 pa_sink_assert_ref(s);
1480 pa_assert(min_latency);
1481 pa_assert(max_latency);
1482
1483 if (PA_SINK_IS_LINKED(s->state)) {
1484 pa_usec_t r[2] = { 0, 0 };
1485
1486 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY_RANGE, r, 0, NULL) == 0);
1487
1488 *min_latency = r[0];
1489 *max_latency = r[1];
1490 } else {
1491 *min_latency = s->thread_info.min_latency;
1492 *max_latency = s->thread_info.max_latency;
1493 }
1494 }
1495
1496 /* Called from IO thread */
1497 void pa_sink_update_latency_range(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency) {
1498 pa_sink_input *i;
1499 void *state = NULL;
1500
1501 pa_sink_assert_ref(s);
1502
1503 s->thread_info.min_latency = min_latency;
1504 s->thread_info.max_latency = max_latency;
1505
1506 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1507 if (i->update_sink_latency_range)
1508 i->update_sink_latency_range(i);
1509
1510 pa_sink_invalidate_requested_latency(s);
1511
1512 pa_source_update_latency_range(s->monitor_source, min_latency, max_latency);
1513 }
1514
1515 size_t pa_sink_get_max_rewind(pa_sink *s) {
1516 size_t r;
1517 pa_sink_assert_ref(s);
1518
1519 if (!PA_SINK_IS_LINKED(s->state))
1520 return s->thread_info.max_rewind;
1521
1522 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MAX_REWIND, &r, 0, NULL) == 0);
1523
1524 return r;
1525 }
1526
1527 size_t pa_sink_get_max_request(pa_sink *s) {
1528 size_t r;
1529 pa_sink_assert_ref(s);
1530
1531 if (!PA_SINK_IS_LINKED(s->state))
1532 return s->thread_info.max_request;
1533
1534 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MAX_REQUEST, &r, 0, NULL) == 0);
1535
1536 return r;
1537 }