sink-input: Don't resample passthrough inputs
[platform/upstream/pulseaudio.git] / src / pulsecore / sink-input.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.1 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 <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <pulse/utf8.h>
32 #include <pulse/xmalloc.h>
33 #include <pulse/util.h>
34
35 #include <pulsecore/sample-util.h>
36 #include <pulsecore/core-subscribe.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/play-memblockq.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/core-util.h>
41
42 #include "sink-input.h"
43
44 #define MEMBLOCKQ_MAXLENGTH (32*1024*1024)
45 #define CONVERT_BUFFER_LENGTH (PA_PAGE_SIZE)
46
47 PA_DEFINE_PUBLIC_CLASS(pa_sink_input, pa_msgobject);
48
49 static void sink_input_free(pa_object *o);
50 static void set_real_ratio(pa_sink_input *i, const pa_cvolume *v);
51
52 static int check_passthrough_connection(pa_sink_input_flags_t flags, pa_sink *dest) {
53
54     if (dest->flags & PA_SINK_PASSTHROUGH) {
55
56         if (pa_idxset_size(dest->inputs) > 0) {
57
58             pa_sink_input *alt_i;
59             uint32_t idx;
60
61             alt_i = pa_idxset_first(dest->inputs, &idx);
62
63             /* only need to check the first input is not PASSTHROUGH */
64             if (alt_i->flags & PA_SINK_INPUT_PASSTHROUGH) {
65                 pa_log_warn("Sink is already connected to PASSTHROUGH input");
66                 return -PA_ERR_BUSY;
67             }
68
69             /* Current inputs are PCM, check new input is not PASSTHROUGH */
70             if (flags & PA_SINK_INPUT_PASSTHROUGH) {
71                 pa_log_warn("Sink is already connected, cannot accept new PASSTHROUGH INPUT");
72                 return -PA_ERR_BUSY;
73             }
74         }
75
76     } else {
77         if (flags & PA_SINK_INPUT_PASSTHROUGH) {
78             pa_log_warn("Cannot connect PASSTHROUGH sink input to sink without PASSTHROUGH capabilities");
79             return -PA_ERR_INVALID;
80         }
81     }
82     return PA_OK;
83 }
84
85 pa_sink_input_new_data* pa_sink_input_new_data_init(pa_sink_input_new_data *data) {
86     pa_assert(data);
87
88     pa_zero(*data);
89     data->resample_method = PA_RESAMPLER_INVALID;
90     data->proplist = pa_proplist_new();
91     data->volume_writable = TRUE;
92
93     return data;
94 }
95
96 void pa_sink_input_new_data_set_sample_spec(pa_sink_input_new_data *data, const pa_sample_spec *spec) {
97     pa_assert(data);
98
99     if ((data->sample_spec_is_set = !!spec))
100         data->sample_spec = *spec;
101 }
102
103 void pa_sink_input_new_data_set_channel_map(pa_sink_input_new_data *data, const pa_channel_map *map) {
104     pa_assert(data);
105
106     if ((data->channel_map_is_set = !!map))
107         data->channel_map = *map;
108 }
109
110 void pa_sink_input_new_data_set_volume(pa_sink_input_new_data *data, const pa_cvolume *volume) {
111     pa_assert(data);
112     pa_assert(data->volume_writable);
113
114     if ((data->volume_is_set = !!volume))
115         data->volume = *volume;
116 }
117
118 void pa_sink_input_new_data_apply_volume_factor(pa_sink_input_new_data *data, const pa_cvolume *volume_factor) {
119     pa_assert(data);
120     pa_assert(volume_factor);
121
122     if (data->volume_factor_is_set)
123         pa_sw_cvolume_multiply(&data->volume_factor, &data->volume_factor, volume_factor);
124     else {
125         data->volume_factor_is_set = TRUE;
126         data->volume_factor = *volume_factor;
127     }
128 }
129
130 void pa_sink_input_new_data_apply_volume_factor_sink(pa_sink_input_new_data *data, const pa_cvolume *volume_factor) {
131     pa_assert(data);
132     pa_assert(volume_factor);
133
134     if (data->volume_factor_sink_is_set)
135         pa_sw_cvolume_multiply(&data->volume_factor_sink, &data->volume_factor_sink, volume_factor);
136     else {
137         data->volume_factor_sink_is_set = TRUE;
138         data->volume_factor_sink = *volume_factor;
139     }
140 }
141
142 void pa_sink_input_new_data_set_muted(pa_sink_input_new_data *data, pa_bool_t mute) {
143     pa_assert(data);
144
145     data->muted_is_set = TRUE;
146     data->muted = !!mute;
147 }
148
149 void pa_sink_input_new_data_done(pa_sink_input_new_data *data) {
150     pa_assert(data);
151
152     pa_proplist_free(data->proplist);
153 }
154
155 /* Called from main context */
156 static void reset_callbacks(pa_sink_input *i) {
157     pa_assert(i);
158
159     i->pop = NULL;
160     i->process_rewind = NULL;
161     i->update_max_rewind = NULL;
162     i->update_max_request = NULL;
163     i->update_sink_requested_latency = NULL;
164     i->update_sink_latency_range = NULL;
165     i->update_sink_fixed_latency = NULL;
166     i->attach = NULL;
167     i->detach = NULL;
168     i->suspend = NULL;
169     i->suspend_within_thread = NULL;
170     i->moving = NULL;
171     i->kill = NULL;
172     i->get_latency = NULL;
173     i->state_change = NULL;
174     i->may_move_to = NULL;
175     i->send_event = NULL;
176     i->volume_changed = NULL;
177     i->mute_changed = NULL;
178 }
179
180 /* Called from main context */
181 int pa_sink_input_new(
182         pa_sink_input **_i,
183         pa_core *core,
184         pa_sink_input_new_data *data) {
185
186     pa_sink_input *i;
187     pa_resampler *resampler = NULL;
188     char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
189     pa_channel_map original_cm;
190     int r;
191     char *pt;
192
193     pa_assert(_i);
194     pa_assert(core);
195     pa_assert(data);
196     pa_assert_ctl_context();
197
198     if (data->client)
199         pa_proplist_update(data->proplist, PA_UPDATE_MERGE, data->client->proplist);
200
201     if (data->origin_sink && (data->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER))
202         data->volume_writable = FALSE;
203
204     if ((r = pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], data)) < 0)
205         return r;
206
207     pa_return_val_if_fail(!data->driver || pa_utf8_valid(data->driver), -PA_ERR_INVALID);
208
209     if (!data->sink) {
210         data->sink = pa_namereg_get(core, NULL, PA_NAMEREG_SINK);
211         data->save_sink = FALSE;
212     }
213
214     pa_return_val_if_fail(data->sink, -PA_ERR_NOENTITY);
215     pa_return_val_if_fail(PA_SINK_IS_LINKED(pa_sink_get_state(data->sink)), -PA_ERR_BADSTATE);
216     pa_return_val_if_fail(!data->sync_base || (data->sync_base->sink == data->sink && pa_sink_input_get_state(data->sync_base) == PA_SINK_INPUT_CORKED), -PA_ERR_INVALID);
217
218     r = check_passthrough_connection(data->flags, data->sink);
219     pa_return_val_if_fail(r == PA_OK, r);
220
221     if (!data->sample_spec_is_set)
222         data->sample_spec = data->sink->sample_spec;
223
224     pa_return_val_if_fail(pa_sample_spec_valid(&data->sample_spec), -PA_ERR_INVALID);
225
226     if (!data->channel_map_is_set) {
227         if (pa_channel_map_compatible(&data->sink->channel_map, &data->sample_spec))
228             data->channel_map = data->sink->channel_map;
229         else
230             pa_channel_map_init_extend(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
231     }
232
233     pa_return_val_if_fail(pa_channel_map_compatible(&data->channel_map, &data->sample_spec), -PA_ERR_INVALID);
234
235     if (!data->volume_is_set) {
236         pa_cvolume_reset(&data->volume, data->sample_spec.channels);
237         data->volume_is_absolute = FALSE;
238         data->save_volume = FALSE;
239     }
240
241     pa_return_val_if_fail(pa_cvolume_compatible(&data->volume, &data->sample_spec), -PA_ERR_INVALID);
242
243     if (!data->volume_factor_is_set)
244         pa_cvolume_reset(&data->volume_factor, data->sample_spec.channels);
245
246     pa_return_val_if_fail(pa_cvolume_compatible(&data->volume_factor, &data->sample_spec), -PA_ERR_INVALID);
247
248     if (!data->volume_factor_sink_is_set)
249         pa_cvolume_reset(&data->volume_factor_sink, data->sink->sample_spec.channels);
250
251     pa_return_val_if_fail(pa_cvolume_compatible(&data->volume_factor_sink, &data->sink->sample_spec), -PA_ERR_INVALID);
252
253     if (!data->muted_is_set)
254         data->muted = FALSE;
255
256     if (data->flags & PA_SINK_INPUT_FIX_FORMAT)
257         data->sample_spec.format = data->sink->sample_spec.format;
258
259     if (data->flags & PA_SINK_INPUT_FIX_RATE)
260         data->sample_spec.rate = data->sink->sample_spec.rate;
261
262     original_cm = data->channel_map;
263
264     if (data->flags & PA_SINK_INPUT_FIX_CHANNELS) {
265         data->sample_spec.channels = data->sink->sample_spec.channels;
266         data->channel_map = data->sink->channel_map;
267     }
268
269     pa_assert(pa_sample_spec_valid(&data->sample_spec));
270     pa_assert(pa_channel_map_valid(&data->channel_map));
271
272     /* Due to the fixing of the sample spec the volume might not match anymore */
273     pa_cvolume_remap(&data->volume, &original_cm, &data->channel_map);
274
275     if (data->resample_method == PA_RESAMPLER_INVALID)
276         data->resample_method = core->resample_method;
277
278     pa_return_val_if_fail(data->resample_method < PA_RESAMPLER_MAX, -PA_ERR_INVALID);
279
280     if ((r = pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], data)) < 0)
281         return r;
282
283     if ((data->flags & PA_SINK_INPUT_NO_CREATE_ON_SUSPEND) &&
284         pa_sink_get_state(data->sink) == PA_SINK_SUSPENDED) {
285         pa_log_warn("Failed to create sink input: sink is suspended.");
286         return -PA_ERR_BADSTATE;
287     }
288
289     if (pa_idxset_size(data->sink->inputs) >= PA_MAX_INPUTS_PER_SINK) {
290         pa_log_warn("Failed to create sink input: too many inputs per sink.");
291         return -PA_ERR_TOOLARGE;
292     }
293
294     if ((data->flags & PA_SINK_INPUT_VARIABLE_RATE) ||
295         !pa_sample_spec_equal(&data->sample_spec, &data->sink->sample_spec) ||
296         !pa_channel_map_equal(&data->channel_map, &data->sink->channel_map)) {
297
298         /* Note: for passthrough content we need to adjust the output rate to that of the current sink-input */
299         if (!(data->flags & PA_SINK_INPUT_PASSTHROUGH)) /* no resampler for passthrough content */
300             if (!(resampler = pa_resampler_new(
301                           core->mempool,
302                           &data->sample_spec, &data->channel_map,
303                           &data->sink->sample_spec, &data->sink->channel_map,
304                           data->resample_method,
305                           ((data->flags & PA_SINK_INPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
306                           ((data->flags & PA_SINK_INPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
307                           (core->disable_remixing || (data->flags & PA_SINK_INPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0) |
308                           (core->disable_lfe_remixing ? PA_RESAMPLER_NO_LFE : 0)))) {
309                 pa_log_warn("Unsupported resampling operation.");
310                 return -PA_ERR_NOTSUPPORTED;
311             }
312     }
313
314     i = pa_msgobject_new(pa_sink_input);
315     i->parent.parent.free = sink_input_free;
316     i->parent.process_msg = pa_sink_input_process_msg;
317
318     i->core = core;
319     i->state = PA_SINK_INPUT_INIT;
320     i->flags = data->flags;
321     i->proplist = pa_proplist_copy(data->proplist);
322     i->driver = pa_xstrdup(pa_path_get_filename(data->driver));
323     i->module = data->module;
324     i->sink = data->sink;
325     i->origin_sink = data->origin_sink;
326     i->client = data->client;
327
328     i->requested_resample_method = data->resample_method;
329     i->actual_resample_method = resampler ? pa_resampler_get_method(resampler) : PA_RESAMPLER_INVALID;
330     i->sample_spec = data->sample_spec;
331     i->channel_map = data->channel_map;
332
333     if (!data->volume_is_absolute && pa_sink_flat_volume_enabled(i->sink)) {
334         pa_cvolume remapped;
335
336         /* When the 'absolute' bool is not set then we'll treat the volume
337          * as relative to the sink volume even in flat volume mode */
338         remapped = data->sink->reference_volume;
339         pa_cvolume_remap(&remapped, &data->sink->channel_map, &data->channel_map);
340         pa_sw_cvolume_multiply(&i->volume, &data->volume, &remapped);
341     } else
342         i->volume = data->volume;
343
344     i->volume_factor = data->volume_factor;
345     i->volume_factor_sink = data->volume_factor_sink;
346     i->real_ratio = i->reference_ratio = data->volume;
347     pa_cvolume_reset(&i->soft_volume, i->sample_spec.channels);
348     pa_cvolume_reset(&i->real_ratio, i->sample_spec.channels);
349     i->volume_writable = data->volume_writable;
350     i->save_volume = data->save_volume;
351     i->save_sink = data->save_sink;
352     i->save_muted = data->save_muted;
353
354     i->muted = data->muted;
355
356     if (data->sync_base) {
357         i->sync_next = data->sync_base->sync_next;
358         i->sync_prev = data->sync_base;
359
360         if (data->sync_base->sync_next)
361             data->sync_base->sync_next->sync_prev = i;
362         data->sync_base->sync_next = i;
363     } else
364         i->sync_next = i->sync_prev = NULL;
365
366     i->direct_outputs = pa_idxset_new(NULL, NULL);
367
368     reset_callbacks(i);
369     i->userdata = NULL;
370
371     i->thread_info.state = i->state;
372     i->thread_info.attached = FALSE;
373     pa_atomic_store(&i->thread_info.drained, 1);
374     i->thread_info.sample_spec = i->sample_spec;
375     i->thread_info.resampler = resampler;
376     i->thread_info.soft_volume = i->soft_volume;
377     i->thread_info.muted = i->muted;
378     i->thread_info.requested_sink_latency = (pa_usec_t) -1;
379     i->thread_info.rewrite_nbytes = 0;
380     i->thread_info.rewrite_flush = FALSE;
381     i->thread_info.dont_rewind_render = FALSE;
382     i->thread_info.underrun_for = (uint64_t) -1;
383     i->thread_info.playing_for = 0;
384     i->thread_info.direct_outputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
385
386     i->thread_info.render_memblockq = pa_memblockq_new(
387             0,
388             MEMBLOCKQ_MAXLENGTH,
389             0,
390             pa_frame_size(&i->sink->sample_spec),
391             0,
392             1,
393             0,
394             &i->sink->silence);
395
396     pa_assert_se(pa_idxset_put(core->sink_inputs, i, &i->index) == 0);
397     pa_assert_se(pa_idxset_put(i->sink->inputs, pa_sink_input_ref(i), NULL) == 0);
398
399     if (i->client)
400         pa_assert_se(pa_idxset_put(i->client->sink_inputs, i, NULL) >= 0);
401
402     pt = pa_proplist_to_string_sep(i->proplist, "\n    ");
403     pa_log_info("Created input %u \"%s\" on %s with sample spec %s and channel map %s\n    %s",
404                 i->index,
405                 pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME)),
406                 i->sink->name,
407                 pa_sample_spec_snprint(st, sizeof(st), &i->sample_spec),
408                 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
409                 pt);
410     pa_xfree(pt);
411
412     /* Don't forget to call pa_sink_input_put! */
413
414     *_i = i;
415     return 0;
416 }
417
418 /* Called from main context */
419 static void update_n_corked(pa_sink_input *i, pa_sink_input_state_t state) {
420     pa_assert(i);
421     pa_assert_ctl_context();
422
423     if (!i->sink)
424         return;
425
426     if (i->state == PA_SINK_INPUT_CORKED && state != PA_SINK_INPUT_CORKED)
427         pa_assert_se(i->sink->n_corked -- >= 1);
428     else if (i->state != PA_SINK_INPUT_CORKED && state == PA_SINK_INPUT_CORKED)
429         i->sink->n_corked++;
430 }
431
432 /* Called from main context */
433 static void sink_input_set_state(pa_sink_input *i, pa_sink_input_state_t state) {
434     pa_sink_input *ssync;
435     pa_assert(i);
436     pa_assert_ctl_context();
437
438     if (state == PA_SINK_INPUT_DRAINED)
439         state = PA_SINK_INPUT_RUNNING;
440
441     if (i->state == state)
442         return;
443
444     pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) == 0);
445
446     update_n_corked(i, state);
447     i->state = state;
448
449     for (ssync = i->sync_prev; ssync; ssync = ssync->sync_prev) {
450         update_n_corked(ssync, state);
451         ssync->state = state;
452     }
453     for (ssync = i->sync_next; ssync; ssync = ssync->sync_next) {
454         update_n_corked(ssync, state);
455         ssync->state = state;
456     }
457
458     if (state != PA_SINK_INPUT_UNLINKED) {
459         pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], i);
460
461         for (ssync = i->sync_prev; ssync; ssync = ssync->sync_prev)
462             pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], ssync);
463
464         for (ssync = i->sync_next; ssync; ssync = ssync->sync_next)
465             pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], ssync);
466
467         if (PA_SINK_INPUT_IS_LINKED(state))
468             pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
469     }
470
471     pa_sink_update_status(i->sink);
472 }
473
474 /* Called from main context */
475 void pa_sink_input_unlink(pa_sink_input *i) {
476     pa_bool_t linked;
477     pa_source_output *o, *p = NULL;
478
479     pa_assert(i);
480     pa_assert_ctl_context();
481
482     /* See pa_sink_unlink() for a couple of comments how this function
483      * works */
484
485     pa_sink_input_ref(i);
486
487     linked = PA_SINK_INPUT_IS_LINKED(i->state);
488
489     if (linked)
490         pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK], i);
491
492     if (i->sync_prev)
493         i->sync_prev->sync_next = i->sync_next;
494     if (i->sync_next)
495         i->sync_next->sync_prev = i->sync_prev;
496
497     i->sync_prev = i->sync_next = NULL;
498
499     pa_idxset_remove_by_data(i->core->sink_inputs, i, NULL);
500
501     if (i->sink)
502         if (pa_idxset_remove_by_data(i->sink->inputs, i, NULL))
503             pa_sink_input_unref(i);
504
505     if (i->client)
506         pa_idxset_remove_by_data(i->client->sink_inputs, i, NULL);
507
508     while ((o = pa_idxset_first(i->direct_outputs, NULL))) {
509         pa_assert(o != p);
510         pa_source_output_kill(o);
511         p = o;
512     }
513
514     update_n_corked(i, PA_SINK_INPUT_UNLINKED);
515     i->state = PA_SINK_INPUT_UNLINKED;
516
517     if (linked && i->sink) {
518         /* We might need to update the sink's volume if we are in flat volume mode. */
519         if (pa_sink_flat_volume_enabled(i->sink))
520             pa_sink_set_volume(i->sink, NULL, FALSE, FALSE);
521
522         if (i->sink->asyncmsgq)
523             pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_REMOVE_INPUT, i, 0, NULL) == 0);
524     }
525
526     reset_callbacks(i);
527
528     if (linked) {
529         pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
530         pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK_POST], i);
531     }
532
533     if (i->sink) {
534         pa_sink_update_status(i->sink);
535         i->sink = NULL;
536     }
537
538     pa_core_maybe_vacuum(i->core);
539
540     pa_sink_input_unref(i);
541 }
542
543 /* Called from main context */
544 static void sink_input_free(pa_object *o) {
545     pa_sink_input* i = PA_SINK_INPUT(o);
546
547     pa_assert(i);
548     pa_assert_ctl_context();
549     pa_assert(pa_sink_input_refcnt(i) == 0);
550
551     if (PA_SINK_INPUT_IS_LINKED(i->state))
552         pa_sink_input_unlink(i);
553
554     pa_log_info("Freeing input %u \"%s\"", i->index, pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME)));
555
556     /* Side note: this function must be able to destruct properly any
557      * kind of sink input in any state, even those which are
558      * "half-moved" or are connected to sinks that have no asyncmsgq
559      * and are hence half-destructed themselves! */
560
561     if (i->thread_info.render_memblockq)
562         pa_memblockq_free(i->thread_info.render_memblockq);
563
564     if (i->thread_info.resampler)
565         pa_resampler_free(i->thread_info.resampler);
566
567     if (i->proplist)
568         pa_proplist_free(i->proplist);
569
570     if (i->direct_outputs)
571         pa_idxset_free(i->direct_outputs, NULL, NULL);
572
573     if (i->thread_info.direct_outputs)
574         pa_hashmap_free(i->thread_info.direct_outputs, NULL, NULL);
575
576     pa_xfree(i->driver);
577     pa_xfree(i);
578 }
579
580 /* Called from main context */
581 void pa_sink_input_put(pa_sink_input *i) {
582     pa_sink_input_state_t state;
583
584     pa_sink_input_assert_ref(i);
585     pa_assert_ctl_context();
586
587     pa_assert(i->state == PA_SINK_INPUT_INIT);
588
589     /* The following fields must be initialized properly */
590     pa_assert(i->pop);
591     pa_assert(i->process_rewind);
592     pa_assert(i->kill);
593
594     state = i->flags & PA_SINK_INPUT_START_CORKED ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING;
595
596     update_n_corked(i, state);
597     i->state = state;
598
599     /* We might need to update the sink's volume if we are in flat volume mode. */
600     if (pa_sink_flat_volume_enabled(i->sink))
601         pa_sink_set_volume(i->sink, NULL, FALSE, i->save_volume);
602     else {
603         if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
604             pa_assert(pa_cvolume_is_norm(&i->volume));
605             pa_assert(pa_cvolume_is_norm(&i->reference_ratio));
606         }
607
608         set_real_ratio(i, &i->volume);
609     }
610
611     i->thread_info.soft_volume = i->soft_volume;
612     i->thread_info.muted = i->muted;
613
614     pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_ADD_INPUT, i, 0, NULL) == 0);
615
616     pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
617     pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], i);
618
619     pa_sink_update_status(i->sink);
620 }
621
622 /* Called from main context */
623 void pa_sink_input_kill(pa_sink_input*i) {
624     pa_sink_input_assert_ref(i);
625     pa_assert_ctl_context();
626     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
627
628     i->kill(i);
629 }
630
631 /* Called from main context */
632 pa_usec_t pa_sink_input_get_latency(pa_sink_input *i, pa_usec_t *sink_latency) {
633     pa_usec_t r[2] = { 0, 0 };
634
635     pa_sink_input_assert_ref(i);
636     pa_assert_ctl_context();
637     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
638
639     pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_LATENCY, r, 0, NULL) == 0);
640
641     if (i->get_latency)
642         r[0] += i->get_latency(i);
643
644     if (sink_latency)
645         *sink_latency = r[1];
646
647     return r[0];
648 }
649
650 /* Called from thread context */
651 void pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink frames */, pa_memchunk *chunk, pa_cvolume *volume) {
652     pa_bool_t do_volume_adj_here, need_volume_factor_sink;
653     pa_bool_t volume_is_norm;
654     size_t block_size_max_sink, block_size_max_sink_input;
655     size_t ilength;
656
657     pa_sink_input_assert_ref(i);
658     pa_sink_input_assert_io_context(i);
659     pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
660     pa_assert(pa_frame_aligned(slength, &i->sink->sample_spec));
661     pa_assert(chunk);
662     pa_assert(volume);
663
664 /*     pa_log_debug("peek"); */
665
666     pa_assert(i->thread_info.state == PA_SINK_INPUT_RUNNING ||
667               i->thread_info.state == PA_SINK_INPUT_CORKED ||
668               i->thread_info.state == PA_SINK_INPUT_DRAINED);
669
670     block_size_max_sink_input = i->thread_info.resampler ?
671         pa_resampler_max_block_size(i->thread_info.resampler) :
672         pa_frame_align(pa_mempool_block_size_max(i->core->mempool), &i->sample_spec);
673
674     block_size_max_sink = pa_frame_align(pa_mempool_block_size_max(i->core->mempool), &i->sink->sample_spec);
675
676     /* Default buffer size */
677     if (slength <= 0)
678         slength = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sink->sample_spec);
679
680     if (slength > block_size_max_sink)
681         slength = block_size_max_sink;
682
683     if (i->thread_info.resampler) {
684         ilength = pa_resampler_request(i->thread_info.resampler, slength);
685
686         if (ilength <= 0)
687             ilength = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sample_spec);
688     } else
689         ilength = slength;
690
691     if (ilength > block_size_max_sink_input)
692         ilength = block_size_max_sink_input;
693
694     /* If the channel maps of the sink and this stream differ, we need
695      * to adjust the volume *before* we resample. Otherwise we can do
696      * it after and leave it for the sink code */
697
698     do_volume_adj_here = !pa_channel_map_equal(&i->channel_map, &i->sink->channel_map);
699     volume_is_norm = pa_cvolume_is_norm(&i->thread_info.soft_volume) && !i->thread_info.muted;
700     need_volume_factor_sink = !pa_cvolume_is_norm(&i->volume_factor_sink);
701
702     while (!pa_memblockq_is_readable(i->thread_info.render_memblockq)) {
703         pa_memchunk tchunk;
704
705         /* There's nothing in our render queue. We need to fill it up
706          * with data from the implementor. */
707
708         if (i->thread_info.state == PA_SINK_INPUT_CORKED ||
709             i->pop(i, ilength, &tchunk) < 0) {
710
711             /* OK, we're corked or the implementor didn't give us any
712              * data, so let's just hand out silence */
713             pa_atomic_store(&i->thread_info.drained, 1);
714
715             pa_memblockq_seek(i->thread_info.render_memblockq, (int64_t) slength, PA_SEEK_RELATIVE, TRUE);
716             i->thread_info.playing_for = 0;
717             if (i->thread_info.underrun_for != (uint64_t) -1)
718                 i->thread_info.underrun_for += ilength;
719             break;
720         }
721
722         pa_atomic_store(&i->thread_info.drained, 0);
723
724         pa_assert(tchunk.length > 0);
725         pa_assert(tchunk.memblock);
726
727         i->thread_info.underrun_for = 0;
728         i->thread_info.playing_for += tchunk.length;
729
730         while (tchunk.length > 0) {
731             pa_memchunk wchunk;
732             pa_bool_t nvfs = need_volume_factor_sink;
733
734             wchunk = tchunk;
735             pa_memblock_ref(wchunk.memblock);
736
737             if (wchunk.length > block_size_max_sink_input)
738                 wchunk.length = block_size_max_sink_input;
739
740             /* It might be necessary to adjust the volume here */
741             if (do_volume_adj_here && !volume_is_norm) {
742                 pa_memchunk_make_writable(&wchunk, 0);
743
744                 if (i->thread_info.muted) {
745                     pa_silence_memchunk(&wchunk, &i->thread_info.sample_spec);
746                     nvfs = FALSE;
747
748                 } else if (!i->thread_info.resampler && nvfs) {
749                     pa_cvolume v;
750
751                     /* If we don't need a resampler we can merge the
752                      * post and the pre volume adjustment into one */
753
754                     pa_sw_cvolume_multiply(&v, &i->thread_info.soft_volume, &i->volume_factor_sink);
755                     pa_volume_memchunk(&wchunk, &i->thread_info.sample_spec, &v);
756                     nvfs = FALSE;
757
758                 } else
759                     pa_volume_memchunk(&wchunk, &i->thread_info.sample_spec, &i->thread_info.soft_volume);
760             }
761
762             if (!i->thread_info.resampler) {
763
764                 if (nvfs) {
765                     pa_memchunk_make_writable(&wchunk, 0);
766                     pa_volume_memchunk(&wchunk, &i->sink->sample_spec, &i->volume_factor_sink);
767                 }
768
769                 pa_memblockq_push_align(i->thread_info.render_memblockq, &wchunk);
770             } else {
771                 pa_memchunk rchunk;
772                 pa_resampler_run(i->thread_info.resampler, &wchunk, &rchunk);
773
774 /*                 pa_log_debug("pushing %lu", (unsigned long) rchunk.length); */
775
776                 if (rchunk.memblock) {
777
778                     if (nvfs) {
779                         pa_memchunk_make_writable(&rchunk, 0);
780                         pa_volume_memchunk(&rchunk, &i->sink->sample_spec, &i->volume_factor_sink);
781                     }
782
783                     pa_memblockq_push_align(i->thread_info.render_memblockq, &rchunk);
784                     pa_memblock_unref(rchunk.memblock);
785                 }
786             }
787
788             pa_memblock_unref(wchunk.memblock);
789
790             tchunk.index += wchunk.length;
791             tchunk.length -= wchunk.length;
792         }
793
794         pa_memblock_unref(tchunk.memblock);
795     }
796
797     pa_assert_se(pa_memblockq_peek(i->thread_info.render_memblockq, chunk) >= 0);
798
799     pa_assert(chunk->length > 0);
800     pa_assert(chunk->memblock);
801
802 /*     pa_log_debug("peeking %lu", (unsigned long) chunk->length); */
803
804     if (chunk->length > block_size_max_sink)
805         chunk->length = block_size_max_sink;
806
807     /* Let's see if we had to apply the volume adjustment ourselves,
808      * or if this can be done by the sink for us */
809
810     if (do_volume_adj_here)
811         /* We had different channel maps, so we already did the adjustment */
812         pa_cvolume_reset(volume, i->sink->sample_spec.channels);
813     else if (i->thread_info.muted)
814         /* We've both the same channel map, so let's have the sink do the adjustment for us*/
815         pa_cvolume_mute(volume, i->sink->sample_spec.channels);
816     else
817         *volume = i->thread_info.soft_volume;
818 }
819
820 /* Called from thread context */
821 void pa_sink_input_drop(pa_sink_input *i, size_t nbytes /* in sink sample spec */) {
822
823     pa_sink_input_assert_ref(i);
824     pa_sink_input_assert_io_context(i);
825     pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
826     pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
827     pa_assert(nbytes > 0);
828
829 /*     pa_log_debug("dropping %lu", (unsigned long) nbytes); */
830
831     pa_memblockq_drop(i->thread_info.render_memblockq, nbytes);
832 }
833
834 /* Called from thread context */
835 void pa_sink_input_process_rewind(pa_sink_input *i, size_t nbytes /* in sink sample spec */) {
836     size_t lbq;
837     pa_bool_t called = FALSE;
838
839     pa_sink_input_assert_ref(i);
840     pa_sink_input_assert_io_context(i);
841     pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
842     pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
843
844 /*     pa_log_debug("rewind(%lu, %lu)", (unsigned long) nbytes, (unsigned long) i->thread_info.rewrite_nbytes); */
845
846     lbq = pa_memblockq_get_length(i->thread_info.render_memblockq);
847
848     if (nbytes > 0 && !i->thread_info.dont_rewind_render) {
849         pa_log_debug("Have to rewind %lu bytes on render memblockq.", (unsigned long) nbytes);
850         pa_memblockq_rewind(i->thread_info.render_memblockq, nbytes);
851     }
852
853     if (i->thread_info.rewrite_nbytes == (size_t) -1) {
854
855         /* We were asked to drop all buffered data, and rerequest new
856          * data from implementor the next time push() is called */
857
858         pa_memblockq_flush_write(i->thread_info.render_memblockq, TRUE);
859
860     } else if (i->thread_info.rewrite_nbytes > 0) {
861         size_t max_rewrite, amount;
862
863         /* Calculate how much make sense to rewrite at most */
864         max_rewrite = nbytes + lbq;
865
866         /* Transform into local domain */
867         if (i->thread_info.resampler)
868             max_rewrite = pa_resampler_request(i->thread_info.resampler, max_rewrite);
869
870         /* Calculate how much of the rewinded data should actually be rewritten */
871         amount = PA_MIN(i->thread_info.rewrite_nbytes, max_rewrite);
872
873         if (amount > 0) {
874             pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) amount);
875
876             /* Tell the implementor */
877             if (i->process_rewind)
878                 i->process_rewind(i, amount);
879             called = TRUE;
880
881             /* Convert back to to sink domain */
882             if (i->thread_info.resampler)
883                 amount = pa_resampler_result(i->thread_info.resampler, amount);
884
885             if (amount > 0)
886                 /* Ok, now update the write pointer */
887                 pa_memblockq_seek(i->thread_info.render_memblockq, - ((int64_t) amount), PA_SEEK_RELATIVE, TRUE);
888
889             if (i->thread_info.rewrite_flush)
890                 pa_memblockq_silence(i->thread_info.render_memblockq);
891
892             /* And reset the resampler */
893             if (i->thread_info.resampler)
894                 pa_resampler_reset(i->thread_info.resampler);
895         }
896     }
897
898     if (!called)
899         if (i->process_rewind)
900             i->process_rewind(i, 0);
901
902     i->thread_info.rewrite_nbytes = 0;
903     i->thread_info.rewrite_flush = FALSE;
904     i->thread_info.dont_rewind_render = FALSE;
905 }
906
907 /* Called from thread context */
908 size_t pa_sink_input_get_max_rewind(pa_sink_input *i) {
909     pa_sink_input_assert_ref(i);
910     pa_sink_input_assert_io_context(i);
911
912     return i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, i->sink->thread_info.max_rewind) : i->sink->thread_info.max_rewind;
913 }
914
915 /* Called from thread context */
916 size_t pa_sink_input_get_max_request(pa_sink_input *i) {
917     pa_sink_input_assert_ref(i);
918     pa_sink_input_assert_io_context(i);
919
920     /* We're not verifying the status here, to allow this to be called
921      * in the state change handler between _INIT and _RUNNING */
922
923     return i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, i->sink->thread_info.max_request) : i->sink->thread_info.max_request;
924 }
925
926 /* Called from thread context */
927 void pa_sink_input_update_max_rewind(pa_sink_input *i, size_t nbytes  /* in the sink's sample spec */) {
928     pa_sink_input_assert_ref(i);
929     pa_sink_input_assert_io_context(i);
930     pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
931     pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
932
933     pa_memblockq_set_maxrewind(i->thread_info.render_memblockq, nbytes);
934
935     if (i->update_max_rewind)
936         i->update_max_rewind(i, i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, nbytes) : nbytes);
937 }
938
939 /* Called from thread context */
940 void pa_sink_input_update_max_request(pa_sink_input *i, size_t nbytes  /* in the sink's sample spec */) {
941     pa_sink_input_assert_ref(i);
942     pa_sink_input_assert_io_context(i);
943     pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
944     pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
945
946     if (i->update_max_request)
947         i->update_max_request(i, i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, nbytes) : nbytes);
948 }
949
950 /* Called from thread context */
951 pa_usec_t pa_sink_input_set_requested_latency_within_thread(pa_sink_input *i, pa_usec_t usec) {
952     pa_sink_input_assert_ref(i);
953     pa_sink_input_assert_io_context(i);
954
955     if (!(i->sink->flags & PA_SINK_DYNAMIC_LATENCY))
956         usec = i->sink->thread_info.fixed_latency;
957
958     if (usec != (pa_usec_t) -1)
959         usec = PA_CLAMP(usec, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
960
961     i->thread_info.requested_sink_latency = usec;
962     pa_sink_invalidate_requested_latency(i->sink, TRUE);
963
964     return usec;
965 }
966
967 /* Called from main context */
968 pa_usec_t pa_sink_input_set_requested_latency(pa_sink_input *i, pa_usec_t usec) {
969     pa_sink_input_assert_ref(i);
970     pa_assert_ctl_context();
971
972     if (PA_SINK_INPUT_IS_LINKED(i->state) && i->sink) {
973         pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
974         return usec;
975     }
976
977     /* If this sink input is not realized yet or we are being moved,
978      * we have to touch the thread info data directly */
979
980     if (i->sink) {
981         if (!(i->sink->flags & PA_SINK_DYNAMIC_LATENCY))
982             usec = pa_sink_get_fixed_latency(i->sink);
983
984         if (usec != (pa_usec_t) -1) {
985             pa_usec_t min_latency, max_latency;
986             pa_sink_get_latency_range(i->sink, &min_latency, &max_latency);
987             usec = PA_CLAMP(usec, min_latency, max_latency);
988         }
989     }
990
991     i->thread_info.requested_sink_latency = usec;
992
993     return usec;
994 }
995
996 /* Called from main context */
997 pa_usec_t pa_sink_input_get_requested_latency(pa_sink_input *i) {
998     pa_sink_input_assert_ref(i);
999     pa_assert_ctl_context();
1000
1001     if (PA_SINK_INPUT_IS_LINKED(i->state) && i->sink) {
1002         pa_usec_t usec = 0;
1003         pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
1004         return usec;
1005     }
1006
1007     /* If this sink input is not realized yet or we are being moved,
1008      * we have to touch the thread info data directly */
1009
1010     return i->thread_info.requested_sink_latency;
1011 }
1012
1013 /* Called from main context */
1014 void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume, pa_bool_t save, pa_bool_t absolute) {
1015     pa_cvolume v;
1016
1017     pa_sink_input_assert_ref(i);
1018     pa_assert_ctl_context();
1019     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1020     pa_assert(volume);
1021     pa_assert(pa_cvolume_valid(volume));
1022     pa_assert(volume->channels == 1 || pa_cvolume_compatible(volume, &i->sample_spec));
1023     pa_assert(i->volume_writable);
1024
1025     if (!absolute && pa_sink_flat_volume_enabled(i->sink)) {
1026         v = i->sink->reference_volume;
1027         pa_cvolume_remap(&v, &i->sink->channel_map, &i->channel_map);
1028
1029         if (pa_cvolume_compatible(volume, &i->sample_spec))
1030             volume = pa_sw_cvolume_multiply(&v, &v, volume);
1031         else
1032             volume = pa_sw_cvolume_multiply_scalar(&v, &v, pa_cvolume_max(volume));
1033     } else {
1034         if (!pa_cvolume_compatible(volume, &i->sample_spec)) {
1035             v = i->volume;
1036             volume = pa_cvolume_scale(&v, pa_cvolume_max(volume));
1037         }
1038     }
1039
1040     if (pa_cvolume_equal(volume, &i->volume)) {
1041         i->save_volume = i->save_volume || save;
1042         return;
1043     }
1044
1045     i->volume = *volume;
1046     i->save_volume = save;
1047
1048     if (pa_sink_flat_volume_enabled(i->sink)) {
1049         /* We are in flat volume mode, so let's update all sink input
1050          * volumes and update the flat volume of the sink */
1051
1052         pa_sink_set_volume(i->sink, NULL, TRUE, save);
1053
1054     } else {
1055         /* OK, we are in normal volume mode. The volume only affects
1056          * ourselves */
1057         set_real_ratio(i, volume);
1058
1059         /* Copy the new soft_volume to the thread_info struct */
1060         pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_SOFT_VOLUME, NULL, 0, NULL) == 0);
1061     }
1062
1063     /* The volume changed, let's tell people so */
1064     if (i->volume_changed)
1065         i->volume_changed(i);
1066
1067     /* The virtual volume changed, let's tell people so */
1068     pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1069 }
1070
1071 /* Called from main context */
1072 static void set_real_ratio(pa_sink_input *i, const pa_cvolume *v) {
1073     pa_sink_input_assert_ref(i);
1074     pa_assert_ctl_context();
1075     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1076     pa_assert(!v || pa_cvolume_compatible(v, &i->sample_spec));
1077
1078     /* This basically calculates:
1079      *
1080      * i->real_ratio := v
1081      * i->soft_volume := i->real_ratio * i->volume_factor */
1082
1083     if (v)
1084         i->real_ratio = *v;
1085     else
1086         pa_cvolume_reset(&i->real_ratio, i->sample_spec.channels);
1087
1088     pa_sw_cvolume_multiply(&i->soft_volume, &i->real_ratio, &i->volume_factor);
1089     /* We don't copy the data to the thread_info data. That's left for someone else to do */
1090 }
1091
1092 /* Called from main context */
1093 pa_bool_t pa_sink_input_is_volume_readable(pa_sink_input *i) {
1094     pa_sink_input_assert_ref(i);
1095     pa_assert_ctl_context();
1096
1097     return !(i->flags & PA_SINK_INPUT_PASSTHROUGH);
1098 }
1099
1100 /* Called from main context */
1101 pa_cvolume *pa_sink_input_get_volume(pa_sink_input *i, pa_cvolume *volume, pa_bool_t absolute) {
1102     pa_sink_input_assert_ref(i);
1103     pa_assert_ctl_context();
1104     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1105     pa_assert(pa_sink_input_is_volume_readable(i));
1106
1107     if (absolute || !pa_sink_flat_volume_enabled(i->sink))
1108         *volume = i->volume;
1109     else
1110         *volume = i->reference_ratio;
1111
1112     return volume;
1113 }
1114
1115 /* Called from main context */
1116 void pa_sink_input_set_mute(pa_sink_input *i, pa_bool_t mute, pa_bool_t save) {
1117     pa_sink_input_assert_ref(i);
1118     pa_assert_ctl_context();
1119     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1120
1121     if (!i->muted == !mute) {
1122         i->save_muted = i->save_muted || mute;
1123         return;
1124     }
1125
1126     i->muted = mute;
1127     i->save_muted = save;
1128
1129     pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_SOFT_MUTE, NULL, 0, NULL) == 0);
1130
1131     /* The mute status changed, let's tell people so */
1132     if (i->mute_changed)
1133         i->mute_changed(i);
1134
1135     pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1136 }
1137
1138 /* Called from main context */
1139 pa_bool_t pa_sink_input_get_mute(pa_sink_input *i) {
1140     pa_sink_input_assert_ref(i);
1141     pa_assert_ctl_context();
1142     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1143
1144     return i->muted;
1145 }
1146
1147 /* Called from main thread */
1148 void pa_sink_input_update_proplist(pa_sink_input *i, pa_update_mode_t mode, pa_proplist *p) {
1149     pa_sink_input_assert_ref(i);
1150     pa_assert_ctl_context();
1151
1152     if (p)
1153         pa_proplist_update(i->proplist, mode, p);
1154
1155     if (PA_SINK_INPUT_IS_LINKED(i->state)) {
1156         pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_PROPLIST_CHANGED], i);
1157         pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1158     }
1159 }
1160
1161 /* Called from main context */
1162 void pa_sink_input_cork(pa_sink_input *i, pa_bool_t b) {
1163     pa_sink_input_assert_ref(i);
1164     pa_assert_ctl_context();
1165     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1166
1167     sink_input_set_state(i, b ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING);
1168 }
1169
1170 /* Called from main context */
1171 int pa_sink_input_set_rate(pa_sink_input *i, uint32_t rate) {
1172     pa_sink_input_assert_ref(i);
1173     pa_assert_ctl_context();
1174     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1175     pa_return_val_if_fail(i->thread_info.resampler, -PA_ERR_BADSTATE);
1176
1177     if (i->sample_spec.rate == rate)
1178         return 0;
1179
1180     i->sample_spec.rate = rate;
1181
1182     pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_RATE, PA_UINT_TO_PTR(rate), 0, NULL, NULL);
1183
1184     pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1185     return 0;
1186 }
1187
1188 /* Called from main context */
1189 void pa_sink_input_set_name(pa_sink_input *i, const char *name) {
1190     const char *old;
1191     pa_sink_input_assert_ref(i);
1192     pa_assert_ctl_context();
1193
1194     if (!name && !pa_proplist_contains(i->proplist, PA_PROP_MEDIA_NAME))
1195         return;
1196
1197     old = pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME);
1198
1199     if (old && name && pa_streq(old, name))
1200         return;
1201
1202     if (name)
1203         pa_proplist_sets(i->proplist, PA_PROP_MEDIA_NAME, name);
1204     else
1205         pa_proplist_unset(i->proplist, PA_PROP_MEDIA_NAME);
1206
1207     if (PA_SINK_INPUT_IS_LINKED(i->state)) {
1208         pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_PROPLIST_CHANGED], i);
1209         pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1210     }
1211 }
1212
1213 /* Called from main context */
1214 pa_resample_method_t pa_sink_input_get_resample_method(pa_sink_input *i) {
1215     pa_sink_input_assert_ref(i);
1216     pa_assert_ctl_context();
1217
1218     return i->actual_resample_method;
1219 }
1220
1221 /* Called from main context */
1222 pa_bool_t pa_sink_input_may_move(pa_sink_input *i) {
1223     pa_sink_input_assert_ref(i);
1224     pa_assert_ctl_context();
1225     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1226
1227     if (i->flags & PA_SINK_INPUT_DONT_MOVE)
1228         return FALSE;
1229
1230     if (i->sync_next || i->sync_prev) {
1231         pa_log_warn("Moving synchronized streams not supported.");
1232         return FALSE;
1233     }
1234
1235     return TRUE;
1236 }
1237
1238 /* Called from main context */
1239 pa_bool_t pa_sink_input_may_move_to(pa_sink_input *i, pa_sink *dest) {
1240     pa_sink_input_assert_ref(i);
1241     pa_assert_ctl_context();
1242     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1243     pa_sink_assert_ref(dest);
1244
1245     if (dest == i->sink)
1246         return TRUE;
1247
1248     if (!pa_sink_input_may_move(i))
1249         return FALSE;
1250
1251     if (pa_idxset_size(dest->inputs) >= PA_MAX_INPUTS_PER_SINK) {
1252         pa_log_warn("Failed to move sink input: too many inputs per sink.");
1253         return FALSE;
1254     }
1255
1256     if (check_passthrough_connection(i->flags, dest) < 0)
1257         return FALSE;
1258
1259     if (i->may_move_to)
1260         if (!i->may_move_to(i, dest))
1261             return FALSE;
1262
1263     return TRUE;
1264 }
1265
1266 /* Called from main context */
1267 int pa_sink_input_start_move(pa_sink_input *i) {
1268     pa_source_output *o, *p = NULL;
1269     int r;
1270
1271     pa_sink_input_assert_ref(i);
1272     pa_assert_ctl_context();
1273     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1274     pa_assert(i->sink);
1275
1276     if (!pa_sink_input_may_move(i))
1277         return -PA_ERR_NOTSUPPORTED;
1278
1279     if ((r = pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_START], i)) < 0)
1280         return r;
1281
1282     /* Kill directly connected outputs */
1283     while ((o = pa_idxset_first(i->direct_outputs, NULL))) {
1284         pa_assert(o != p);
1285         pa_source_output_kill(o);
1286         p = o;
1287     }
1288     pa_assert(pa_idxset_isempty(i->direct_outputs));
1289
1290     pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
1291
1292     if (pa_sink_input_get_state(i) == PA_SINK_INPUT_CORKED)
1293         pa_assert_se(i->sink->n_corked-- >= 1);
1294
1295     if (pa_sink_flat_volume_enabled(i->sink))
1296         /* We might need to update the sink's volume if we are in flat
1297          * volume mode. */
1298         pa_sink_set_volume(i->sink, NULL, FALSE, FALSE);
1299
1300     pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_START_MOVE, i, 0, NULL) == 0);
1301
1302     pa_sink_update_status(i->sink);
1303     pa_cvolume_remap(&i->volume_factor_sink, &i->sink->channel_map, &i->channel_map);
1304     i->sink = NULL;
1305
1306     pa_sink_input_unref(i);
1307
1308     return 0;
1309 }
1310
1311 /* Called from main context. If i has an origin sink that uses volume sharing,
1312  * then also the origin sink and all streams connected to it need to update
1313  * their volume - this function does all that by using recursion. */
1314 static void update_volume_due_to_moving(pa_sink_input *i, pa_sink *dest) {
1315     pa_cvolume old_volume;
1316
1317     pa_assert(i);
1318     pa_assert(dest);
1319     pa_assert(i->sink); /* The destination sink should already be set. */
1320
1321     if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
1322         pa_sink *root_sink = i->sink;
1323         pa_sink_input *origin_sink_input;
1324         uint32_t idx;
1325
1326         while (root_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)
1327             root_sink = root_sink->input_to_master->sink;
1328
1329         if (pa_sink_flat_volume_enabled(i->sink)) {
1330             /* Ok, so the origin sink uses volume sharing, and flat volume is
1331              * enabled. The volume will have to be updated as follows:
1332              *
1333              *     i->volume := i->sink->real_volume
1334              *         (handled later by pa_sink_set_volume)
1335              *     i->reference_ratio := i->volume / i->sink->reference_volume
1336              *         (handled later by pa_sink_set_volume)
1337              *     i->real_ratio stays unchanged
1338              *         (streams whose origin sink uses volume sharing should
1339              *          always have real_ratio of 0 dB)
1340              *     i->soft_volume stays unchanged
1341              *         (streams whose origin sink uses volume sharing should
1342              *          always have volume_factor as soft_volume, so no change
1343              *          should be needed) */
1344
1345             pa_assert(pa_cvolume_is_norm(&i->real_ratio));
1346             pa_assert(pa_cvolume_equal(&i->soft_volume, &i->volume_factor));
1347
1348             /* Notifications will be sent by pa_sink_set_volume(). */
1349
1350         } else {
1351             /* Ok, so the origin sink uses volume sharing, and flat volume is
1352              * disabled. The volume will have to be updated as follows:
1353              *
1354              *     i->volume := 0 dB
1355              *     i->reference_ratio := 0 dB
1356              *     i->real_ratio stays unchanged
1357              *         (streams whose origin sink uses volume sharing should
1358              *          always have real_ratio of 0 dB)
1359              *     i->soft_volume stays unchanged
1360              *         (streams whose origin sink uses volume sharing should
1361              *          always have volume_factor as soft_volume, so no change
1362              *          should be needed) */
1363
1364             old_volume = i->volume;
1365             pa_cvolume_reset(&i->volume, i->volume.channels);
1366             pa_cvolume_reset(&i->reference_ratio, i->reference_ratio.channels);
1367             pa_assert(pa_cvolume_is_norm(&i->real_ratio));
1368             pa_assert(pa_cvolume_equal(&i->soft_volume, &i->volume_factor));
1369
1370             /* Notify others about the changed sink input volume. */
1371             if (!pa_cvolume_equal(&i->volume, &old_volume)) {
1372                 if (i->volume_changed)
1373                     i->volume_changed(i);
1374
1375                 pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1376             }
1377         }
1378
1379         /* Additionally, the origin sink volume needs updating:
1380          *
1381          *     i->origin_sink->reference_volume := root_sink->reference_volume
1382          *     i->origin_sink->real_volume := root_sink->real_volume
1383          *     i->origin_sink->soft_volume stays unchanged
1384          *         (sinks that use volume sharing should always have
1385          *          soft_volume of 0 dB) */
1386
1387         old_volume = i->origin_sink->reference_volume;
1388
1389         i->origin_sink->reference_volume = root_sink->reference_volume;
1390         pa_cvolume_remap(&i->origin_sink->reference_volume, &root_sink->channel_map, &i->origin_sink->channel_map);
1391
1392         i->origin_sink->real_volume = root_sink->real_volume;
1393         pa_cvolume_remap(&i->origin_sink->real_volume, &root_sink->channel_map, &i->origin_sink->channel_map);
1394
1395         pa_assert(pa_cvolume_is_norm(&i->origin_sink->soft_volume));
1396
1397         /* Notify others about the changed sink volume. If you wonder whether
1398          * i->origin_sink->set_volume() should be called somewhere, that's not
1399          * the case, because sinks that use volume sharing shouldn't have any
1400          * internal volume that set_volume() would update. If you wonder
1401          * whether the thread_info variables should be synced, yes, they
1402          * should, and it's done by the PA_SINK_MESSAGE_FINISH_MOVE message
1403          * handler. */
1404         if (!pa_cvolume_equal(&i->origin_sink->reference_volume, &old_volume))
1405             pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, i->origin_sink->index);
1406
1407         /* Recursively update origin sink inputs. */
1408         PA_IDXSET_FOREACH(origin_sink_input, i->origin_sink->inputs, idx)
1409             update_volume_due_to_moving(origin_sink_input, dest);
1410
1411     } else {
1412         old_volume = i->volume;
1413
1414         if (pa_sink_flat_volume_enabled(i->sink)) {
1415             /* Ok, so this is a regular stream, and flat volume is enabled. The
1416              * volume will have to be updated as follows:
1417              *
1418              *     i->volume := i->reference_ratio * i->sink->reference_volume
1419              *     i->reference_ratio stays unchanged
1420              *     i->real_ratio := i->volume / i->sink->real_volume
1421              *         (handled later by pa_sink_set_volume)
1422              *     i->soft_volume := i->real_ratio * i->volume_factor
1423              *         (handled later by pa_sink_set_volume) */
1424
1425             i->volume = i->sink->reference_volume;
1426             pa_cvolume_remap(&i->volume, &i->sink->channel_map, &i->channel_map);
1427             pa_sw_cvolume_multiply(&i->volume, &i->volume, &i->reference_ratio);
1428
1429         } else {
1430             /* Ok, so this is a regular stream, and flat volume is disabled.
1431              * The volume will have to be updated as follows:
1432              *
1433              *     i->volume := i->reference_ratio
1434              *     i->reference_ratio stays unchanged
1435              *     i->real_ratio := i->reference_ratio
1436              *     i->soft_volume := i->real_ratio * i->volume_factor */
1437
1438             i->volume = i->reference_ratio;
1439             i->real_ratio = i->reference_ratio;
1440             pa_sw_cvolume_multiply(&i->soft_volume, &i->real_ratio, &i->volume_factor);
1441         }
1442
1443         /* Notify others about the changed sink input volume. */
1444         if (!pa_cvolume_equal(&i->volume, &old_volume)) {
1445             /* XXX: In case i->sink has flat volume enabled, then real_ratio
1446              * and soft_volume are not updated yet. Let's hope that the
1447              * callback implementation doesn't care about those variables... */
1448             if (i->volume_changed)
1449                 i->volume_changed(i);
1450
1451             pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1452         }
1453     }
1454
1455     /* If i->sink == dest, then recursion has finished, and we can finally call
1456      * pa_sink_set_volume(), which will do the rest of the updates. */
1457     if ((i->sink == dest) && pa_sink_flat_volume_enabled(i->sink))
1458         pa_sink_set_volume(i->sink, NULL, FALSE, i->save_volume);
1459 }
1460
1461 /* Called from main context */
1462 int pa_sink_input_finish_move(pa_sink_input *i, pa_sink *dest, pa_bool_t save) {
1463     pa_resampler *new_resampler;
1464
1465     pa_sink_input_assert_ref(i);
1466     pa_assert_ctl_context();
1467     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1468     pa_assert(!i->sink);
1469     pa_sink_assert_ref(dest);
1470
1471     if (!pa_sink_input_may_move_to(i, dest))
1472         return -PA_ERR_NOTSUPPORTED;
1473
1474     if (i->thread_info.resampler &&
1475         pa_sample_spec_equal(pa_resampler_output_sample_spec(i->thread_info.resampler), &dest->sample_spec) &&
1476         pa_channel_map_equal(pa_resampler_output_channel_map(i->thread_info.resampler), &dest->channel_map))
1477
1478         /* Try to reuse the old resampler if possible */
1479         new_resampler = i->thread_info.resampler;
1480
1481     else if ((i->flags & PA_SINK_INPUT_VARIABLE_RATE) ||
1482              !pa_sample_spec_equal(&i->sample_spec, &dest->sample_spec) ||
1483              !pa_channel_map_equal(&i->channel_map, &dest->channel_map)) {
1484
1485         /* Okey, we need a new resampler for the new sink */
1486
1487         if (!(new_resampler = pa_resampler_new(
1488                       i->core->mempool,
1489                       &i->sample_spec, &i->channel_map,
1490                       &dest->sample_spec, &dest->channel_map,
1491                       i->requested_resample_method,
1492                       ((i->flags & PA_SINK_INPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
1493                       ((i->flags & PA_SINK_INPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
1494                       (i->core->disable_remixing || (i->flags & PA_SINK_INPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0)))) {
1495             pa_log_warn("Unsupported resampling operation.");
1496             return -PA_ERR_NOTSUPPORTED;
1497         }
1498     } else
1499         new_resampler = NULL;
1500
1501     if (i->moving)
1502         i->moving(i, dest);
1503
1504     i->sink = dest;
1505     i->save_sink = save;
1506     pa_idxset_put(dest->inputs, pa_sink_input_ref(i), NULL);
1507
1508     pa_cvolume_remap(&i->volume_factor_sink, &i->channel_map, &i->sink->channel_map);
1509
1510     if (pa_sink_input_get_state(i) == PA_SINK_INPUT_CORKED)
1511         i->sink->n_corked++;
1512
1513     /* Replace resampler and render queue */
1514     if (new_resampler != i->thread_info.resampler) {
1515
1516         if (i->thread_info.resampler)
1517             pa_resampler_free(i->thread_info.resampler);
1518         i->thread_info.resampler = new_resampler;
1519
1520         pa_memblockq_free(i->thread_info.render_memblockq);
1521
1522         i->thread_info.render_memblockq = pa_memblockq_new(
1523                 0,
1524                 MEMBLOCKQ_MAXLENGTH,
1525                 0,
1526                 pa_frame_size(&i->sink->sample_spec),
1527                 0,
1528                 1,
1529                 0,
1530                 &i->sink->silence);
1531     }
1532     pa_sink_update_status(dest);
1533
1534     update_volume_due_to_moving(i, dest);
1535
1536     pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_FINISH_MOVE, i, 0, NULL) == 0);
1537
1538     pa_log_debug("Successfully moved sink input %i to %s.", i->index, dest->name);
1539
1540     /* Notify everyone */
1541     pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], i);
1542
1543     pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
1544
1545     return 0;
1546 }
1547
1548 /* Called from main context */
1549 void pa_sink_input_fail_move(pa_sink_input *i) {
1550
1551     pa_sink_input_assert_ref(i);
1552     pa_assert_ctl_context();
1553     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1554     pa_assert(!i->sink);
1555
1556     /* Check if someone wants this sink input? */
1557     if (pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FAIL], i) == PA_HOOK_STOP)
1558         return;
1559
1560     if (i->moving)
1561         i->moving(i, NULL);
1562
1563     pa_sink_input_kill(i);
1564 }
1565
1566 /* Called from main context */
1567 int pa_sink_input_move_to(pa_sink_input *i, pa_sink *dest, pa_bool_t save) {
1568     int r;
1569
1570     pa_sink_input_assert_ref(i);
1571     pa_assert_ctl_context();
1572     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
1573     pa_assert(i->sink);
1574     pa_sink_assert_ref(dest);
1575
1576     if (dest == i->sink)
1577         return 0;
1578
1579     if (!pa_sink_input_may_move_to(i, dest))
1580         return -PA_ERR_NOTSUPPORTED;
1581
1582     pa_sink_input_ref(i);
1583
1584     if ((r = pa_sink_input_start_move(i)) < 0) {
1585         pa_sink_input_unref(i);
1586         return r;
1587     }
1588
1589     if ((r = pa_sink_input_finish_move(i, dest, save)) < 0) {
1590         pa_sink_input_fail_move(i);
1591         pa_sink_input_unref(i);
1592         return r;
1593     }
1594
1595     pa_sink_input_unref(i);
1596
1597     return 0;
1598 }
1599
1600 /* Called from IO thread context */
1601 void pa_sink_input_set_state_within_thread(pa_sink_input *i, pa_sink_input_state_t state) {
1602     pa_bool_t corking, uncorking;
1603
1604     pa_sink_input_assert_ref(i);
1605     pa_sink_input_assert_io_context(i);
1606
1607     if (state == i->thread_info.state)
1608         return;
1609
1610     if ((state == PA_SINK_INPUT_DRAINED || state == PA_SINK_INPUT_RUNNING) &&
1611         !(i->thread_info.state == PA_SINK_INPUT_DRAINED || i->thread_info.state != PA_SINK_INPUT_RUNNING))
1612         pa_atomic_store(&i->thread_info.drained, 1);
1613
1614     corking = state == PA_SINK_INPUT_CORKED && i->thread_info.state == PA_SINK_INPUT_RUNNING;
1615     uncorking = i->thread_info.state == PA_SINK_INPUT_CORKED && state == PA_SINK_INPUT_RUNNING;
1616
1617     if (i->state_change)
1618         i->state_change(i, state);
1619
1620     i->thread_info.state = state;
1621
1622     if (corking) {
1623
1624         pa_log_debug("Requesting rewind due to corking");
1625
1626         /* This will tell the implementing sink input driver to rewind
1627          * so that the unplayed already mixed data is not lost */
1628         pa_sink_input_request_rewind(i, 0, TRUE, TRUE, FALSE);
1629
1630     } else if (uncorking) {
1631
1632         i->thread_info.underrun_for = (uint64_t) -1;
1633         i->thread_info.playing_for = 0;
1634
1635         pa_log_debug("Requesting rewind due to uncorking");
1636
1637         /* OK, we're being uncorked. Make sure we're not rewound when
1638          * the hw buffer is remixed and request a remix. */
1639         pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
1640     }
1641 }
1642
1643 /* Called from thread context, except when it is not. */
1644 int pa_sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
1645     pa_sink_input *i = PA_SINK_INPUT(o);
1646     pa_sink_input_assert_ref(i);
1647
1648     switch (code) {
1649
1650         case PA_SINK_INPUT_MESSAGE_SET_SOFT_VOLUME:
1651             if (!pa_cvolume_equal(&i->thread_info.soft_volume, &i->soft_volume)) {
1652                 i->thread_info.soft_volume = i->soft_volume;
1653                 pa_sink_input_request_rewind(i, 0, TRUE, FALSE, FALSE);
1654             }
1655             return 0;
1656
1657         case PA_SINK_INPUT_MESSAGE_SET_SOFT_MUTE:
1658             if (i->thread_info.muted != i->muted) {
1659                 i->thread_info.muted = i->muted;
1660                 pa_sink_input_request_rewind(i, 0, TRUE, FALSE, FALSE);
1661             }
1662             return 0;
1663
1664         case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
1665             pa_usec_t *r = userdata;
1666
1667             r[0] += pa_bytes_to_usec(pa_memblockq_get_length(i->thread_info.render_memblockq), &i->sink->sample_spec);
1668             r[1] += pa_sink_get_latency_within_thread(i->sink);
1669
1670             return 0;
1671         }
1672
1673         case PA_SINK_INPUT_MESSAGE_SET_RATE:
1674
1675             i->thread_info.sample_spec.rate = PA_PTR_TO_UINT(userdata);
1676             pa_resampler_set_input_rate(i->thread_info.resampler, PA_PTR_TO_UINT(userdata));
1677
1678             return 0;
1679
1680         case PA_SINK_INPUT_MESSAGE_SET_STATE: {
1681             pa_sink_input *ssync;
1682
1683             pa_sink_input_set_state_within_thread(i, PA_PTR_TO_UINT(userdata));
1684
1685             for (ssync = i->thread_info.sync_prev; ssync; ssync = ssync->thread_info.sync_prev)
1686                 pa_sink_input_set_state_within_thread(ssync, PA_PTR_TO_UINT(userdata));
1687
1688             for (ssync = i->thread_info.sync_next; ssync; ssync = ssync->thread_info.sync_next)
1689                 pa_sink_input_set_state_within_thread(ssync, PA_PTR_TO_UINT(userdata));
1690
1691             return 0;
1692         }
1693
1694         case PA_SINK_INPUT_MESSAGE_SET_REQUESTED_LATENCY: {
1695             pa_usec_t *usec = userdata;
1696
1697             *usec = pa_sink_input_set_requested_latency_within_thread(i, *usec);
1698             return 0;
1699         }
1700
1701         case PA_SINK_INPUT_MESSAGE_GET_REQUESTED_LATENCY: {
1702             pa_usec_t *r = userdata;
1703
1704             *r = i->thread_info.requested_sink_latency;
1705             return 0;
1706         }
1707     }
1708
1709     return -PA_ERR_NOTIMPLEMENTED;
1710 }
1711
1712 /* Called from main thread */
1713 pa_sink_input_state_t pa_sink_input_get_state(pa_sink_input *i) {
1714     pa_sink_input_assert_ref(i);
1715     pa_assert_ctl_context();
1716
1717     if (i->state == PA_SINK_INPUT_RUNNING || i->state == PA_SINK_INPUT_DRAINED)
1718         return pa_atomic_load(&i->thread_info.drained) ? PA_SINK_INPUT_DRAINED : PA_SINK_INPUT_RUNNING;
1719
1720     return i->state;
1721 }
1722
1723 /* Called from IO context */
1724 pa_bool_t pa_sink_input_safe_to_remove(pa_sink_input *i) {
1725     pa_sink_input_assert_ref(i);
1726     pa_sink_input_assert_io_context(i);
1727
1728     if (PA_SINK_INPUT_IS_LINKED(i->thread_info.state))
1729         return pa_memblockq_is_empty(i->thread_info.render_memblockq);
1730
1731     return TRUE;
1732 }
1733
1734 /* Called from IO context */
1735 void pa_sink_input_request_rewind(
1736         pa_sink_input *i,
1737         size_t nbytes  /* in our sample spec */,
1738         pa_bool_t rewrite,
1739         pa_bool_t flush,
1740         pa_bool_t dont_rewind_render) {
1741
1742     size_t lbq;
1743
1744     /* If 'rewrite' is TRUE the sink is rewound as far as requested
1745      * and possible and the exact value of this is passed back the
1746      * implementor via process_rewind(). If 'flush' is also TRUE all
1747      * already rendered data is also dropped.
1748      *
1749      * If 'rewrite' is FALSE the sink is rewound as far as requested
1750      * and possible and the already rendered data is dropped so that
1751      * in the next iteration we read new data from the
1752      * implementor. This implies 'flush' is TRUE.  If
1753      * dont_rewind_render is TRUE then the render memblockq is not
1754      * rewound. */
1755
1756     /* nbytes = 0 means maximum rewind request */
1757
1758     pa_sink_input_assert_ref(i);
1759     pa_sink_input_assert_io_context(i);
1760     pa_assert(rewrite || flush);
1761     pa_assert(!dont_rewind_render || !rewrite);
1762
1763     /* We don't take rewind requests while we are corked */
1764     if (i->thread_info.state == PA_SINK_INPUT_CORKED)
1765         return;
1766
1767     nbytes = PA_MAX(i->thread_info.rewrite_nbytes, nbytes);
1768
1769     /* pa_log_debug("request rewrite %zu", nbytes); */
1770
1771     /* Calculate how much we can rewind locally without having to
1772      * touch the sink */
1773     if (rewrite)
1774         lbq = pa_memblockq_get_length(i->thread_info.render_memblockq);
1775     else
1776         lbq = 0;
1777
1778     /* Check if rewinding for the maximum is requested, and if so, fix up */
1779     if (nbytes <= 0) {
1780
1781         /* Calculate maximum number of bytes that could be rewound in theory */
1782         nbytes = i->sink->thread_info.max_rewind + lbq;
1783
1784         /* Transform from sink domain */
1785         if (i->thread_info.resampler)
1786             nbytes = pa_resampler_request(i->thread_info.resampler, nbytes);
1787     }
1788
1789     /* Remember how much we actually want to rewrite */
1790     if (i->thread_info.rewrite_nbytes != (size_t) -1) {
1791         if (rewrite) {
1792             /* Make sure to not overwrite over underruns */
1793             if (nbytes > i->thread_info.playing_for)
1794                 nbytes = (size_t) i->thread_info.playing_for;
1795
1796             i->thread_info.rewrite_nbytes = nbytes;
1797         } else
1798             i->thread_info.rewrite_nbytes = (size_t) -1;
1799     }
1800
1801     i->thread_info.rewrite_flush =
1802         i->thread_info.rewrite_flush ||
1803         (flush && i->thread_info.rewrite_nbytes != 0);
1804
1805     i->thread_info.dont_rewind_render =
1806         i->thread_info.dont_rewind_render ||
1807         dont_rewind_render;
1808
1809     if (nbytes != (size_t) -1) {
1810
1811         /* Transform to sink domain */
1812         if (i->thread_info.resampler)
1813             nbytes = pa_resampler_result(i->thread_info.resampler, nbytes);
1814
1815         if (nbytes > lbq)
1816             pa_sink_request_rewind(i->sink, nbytes - lbq);
1817         else
1818             /* This call will make sure process_rewind() is called later */
1819             pa_sink_request_rewind(i->sink, 0);
1820     }
1821 }
1822
1823 /* Called from main context */
1824 pa_memchunk* pa_sink_input_get_silence(pa_sink_input *i, pa_memchunk *ret) {
1825     pa_sink_input_assert_ref(i);
1826     pa_assert_ctl_context();
1827     pa_assert(ret);
1828
1829     /* FIXME: Shouldn't access resampler object from main context! */
1830
1831     pa_silence_memchunk_get(
1832                 &i->core->silence_cache,
1833                 i->core->mempool,
1834                 ret,
1835                 &i->sample_spec,
1836                 i->thread_info.resampler ? pa_resampler_max_block_size(i->thread_info.resampler) : 0);
1837
1838     return ret;
1839 }
1840
1841 /* Called from main context */
1842 void pa_sink_input_send_event(pa_sink_input *i, const char *event, pa_proplist *data) {
1843     pa_proplist *pl = NULL;
1844     pa_sink_input_send_event_hook_data hook_data;
1845
1846     pa_sink_input_assert_ref(i);
1847     pa_assert_ctl_context();
1848     pa_assert(event);
1849
1850     if (!i->send_event)
1851         return;
1852
1853     if (!data)
1854         data = pl = pa_proplist_new();
1855
1856     hook_data.sink_input = i;
1857     hook_data.data = data;
1858     hook_data.event = event;
1859
1860     if (pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_SEND_EVENT], &hook_data) < 0)
1861         goto finish;
1862
1863     i->send_event(i, event, data);
1864
1865 finish:
1866     if (pl)
1867         pa_proplist_free(pl);
1868 }