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