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