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