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