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