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