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