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