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