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