1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <pulse/utf8.h>
29 #include <pulse/xmalloc.h>
30 #include <pulse/util.h>
31 #include <pulse/internal.h>
32
33 #include <pulsecore/core-format.h>
34 #include <pulsecore/mix.h>
35 #include <pulsecore/stream-util.h>
36 #include <pulsecore/core-subscribe.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/namereg.h>
39 #include <pulsecore/core-util.h>
40
41 #include "source-output.h"
42
43 #define MEMBLOCKQ_MAXLENGTH (32*1024*1024)
44
45 PA_DEFINE_PUBLIC_CLASS(pa_source_output, pa_msgobject);
46
47 static void source_output_free(pa_object* mo);
48 static void set_real_ratio(pa_source_output *o, const pa_cvolume *v);
49
pa_source_output_new_data_init(pa_source_output_new_data * data)50 pa_source_output_new_data* pa_source_output_new_data_init(pa_source_output_new_data *data) {
51 pa_assert(data);
52
53 pa_zero(*data);
54 data->resample_method = PA_RESAMPLER_INVALID;
55 data->proplist = pa_proplist_new();
56 data->volume_writable = true;
57
58 return data;
59 }
60
pa_source_output_new_data_set_sample_spec(pa_source_output_new_data * data,const pa_sample_spec * spec)61 void pa_source_output_new_data_set_sample_spec(pa_source_output_new_data *data, const pa_sample_spec *spec) {
62 pa_assert(data);
63
64 if ((data->sample_spec_is_set = !!spec))
65 data->sample_spec = *spec;
66 }
67
pa_source_output_new_data_set_channel_map(pa_source_output_new_data * data,const pa_channel_map * map)68 void pa_source_output_new_data_set_channel_map(pa_source_output_new_data *data, const pa_channel_map *map) {
69 pa_assert(data);
70
71 if ((data->channel_map_is_set = !!map))
72 data->channel_map = *map;
73 }
74
pa_source_output_new_data_is_passthrough(pa_source_output_new_data * data)75 bool pa_source_output_new_data_is_passthrough(pa_source_output_new_data *data) {
76 pa_assert(data);
77
78 if (PA_LIKELY(data->format) && PA_UNLIKELY(!pa_format_info_is_pcm(data->format)))
79 return true;
80
81 if (PA_UNLIKELY(data->flags & PA_SOURCE_OUTPUT_PASSTHROUGH))
82 return true;
83
84 return false;
85 }
86
pa_source_output_new_data_set_volume(pa_source_output_new_data * data,const pa_cvolume * volume)87 void pa_source_output_new_data_set_volume(pa_source_output_new_data *data, const pa_cvolume *volume) {
88 pa_assert(data);
89 pa_assert(data->volume_writable);
90
91 if ((data->volume_is_set = !!volume))
92 data->volume = *volume;
93 }
94
pa_source_output_new_data_apply_volume_factor(pa_source_output_new_data * data,const pa_cvolume * volume_factor)95 void pa_source_output_new_data_apply_volume_factor(pa_source_output_new_data *data, const pa_cvolume *volume_factor) {
96 pa_assert(data);
97 pa_assert(volume_factor);
98
99 if (data->volume_factor_is_set)
100 pa_sw_cvolume_multiply(&data->volume_factor, &data->volume_factor, volume_factor);
101 else {
102 data->volume_factor_is_set = true;
103 data->volume_factor = *volume_factor;
104 }
105 }
106
pa_source_output_new_data_apply_volume_factor_source(pa_source_output_new_data * data,const pa_cvolume * volume_factor)107 void pa_source_output_new_data_apply_volume_factor_source(pa_source_output_new_data *data, const pa_cvolume *volume_factor) {
108 pa_assert(data);
109 pa_assert(volume_factor);
110
111 if (data->volume_factor_source_is_set)
112 pa_sw_cvolume_multiply(&data->volume_factor_source, &data->volume_factor_source, volume_factor);
113 else {
114 data->volume_factor_source_is_set = true;
115 data->volume_factor_source = *volume_factor;
116 }
117 }
118
pa_source_output_new_data_set_muted(pa_source_output_new_data * data,bool mute)119 void pa_source_output_new_data_set_muted(pa_source_output_new_data *data, bool mute) {
120 pa_assert(data);
121
122 data->muted_is_set = true;
123 data->muted = mute;
124 }
125
pa_source_output_new_data_set_source(pa_source_output_new_data * data,pa_source * s,bool save,bool requested_by_application)126 bool pa_source_output_new_data_set_source(pa_source_output_new_data *data, pa_source *s, bool save,
127 bool requested_by_application) {
128 bool ret = true;
129 pa_idxset *formats = NULL;
130
131 pa_assert(data);
132 pa_assert(s);
133
134 if (!data->req_formats) {
135 /* We're not working with the extended API */
136 data->source = s;
137 if (save) {
138 pa_xfree(data->preferred_source);
139 data->preferred_source = pa_xstrdup(s->name);
140 }
141 data->source_requested_by_application = requested_by_application;
142 } else {
143 /* Extended API: let's see if this source supports the formats the client would like */
144 formats = pa_source_check_formats(s, data->req_formats);
145
146 if (formats && !pa_idxset_isempty(formats)) {
147 /* Source supports at least one of the requested formats */
148 data->source = s;
149 if (save) {
150 pa_xfree(data->preferred_source);
151 data->preferred_source = pa_xstrdup(s->name);
152 }
153 data->source_requested_by_application = requested_by_application;
154 if (data->nego_formats)
155 pa_idxset_free(data->nego_formats, (pa_free_cb_t) pa_format_info_free);
156 data->nego_formats = formats;
157 } else {
158 /* Source doesn't support any of the formats requested by the client */
159 if (formats)
160 pa_idxset_free(formats, (pa_free_cb_t) pa_format_info_free);
161 ret = false;
162 }
163 }
164
165 return ret;
166 }
167
pa_source_output_new_data_set_formats(pa_source_output_new_data * data,pa_idxset * formats)168 bool pa_source_output_new_data_set_formats(pa_source_output_new_data *data, pa_idxset *formats) {
169 pa_assert(data);
170 pa_assert(formats);
171
172 if (data->req_formats)
173 pa_idxset_free(data->req_formats, (pa_free_cb_t) pa_format_info_free);
174
175 data->req_formats = formats;
176
177 if (data->source) {
178 /* Trigger format negotiation */
179 return pa_source_output_new_data_set_source(data, data->source, (data->preferred_source != NULL),
180 data->source_requested_by_application);
181 }
182
183 return true;
184 }
185
pa_source_output_new_data_done(pa_source_output_new_data * data)186 void pa_source_output_new_data_done(pa_source_output_new_data *data) {
187 pa_assert(data);
188
189 if (data->req_formats)
190 pa_idxset_free(data->req_formats, (pa_free_cb_t) pa_format_info_free);
191
192 if (data->nego_formats)
193 pa_idxset_free(data->nego_formats, (pa_free_cb_t) pa_format_info_free);
194
195 if (data->format)
196 pa_format_info_free(data->format);
197
198 if (data->preferred_source)
199 pa_xfree(data->preferred_source);
200
201 pa_proplist_free(data->proplist);
202 }
203
204 /* Called from main context */
reset_callbacks(pa_source_output * o)205 static void reset_callbacks(pa_source_output *o) {
206 pa_assert(o);
207
208 o->push = NULL;
209 o->process_rewind = NULL;
210 o->update_max_rewind = NULL;
211 o->update_source_requested_latency = NULL;
212 o->update_source_latency_range = NULL;
213 o->update_source_fixed_latency = NULL;
214 o->attach = NULL;
215 o->detach = NULL;
216 o->suspend = NULL;
217 o->suspend_within_thread = NULL;
218 o->moving = NULL;
219 o->kill = NULL;
220 o->get_latency = NULL;
221 o->state_change = NULL;
222 o->may_move_to = NULL;
223 o->send_event = NULL;
224 o->volume_changed = NULL;
225 o->mute_changed = NULL;
226 }
227
228 /* Called from main context */
pa_source_output_new(pa_source_output ** _o,pa_core * core,pa_source_output_new_data * data)229 int pa_source_output_new(
230 pa_source_output**_o,
231 pa_core *core,
232 pa_source_output_new_data *data) {
233
234 pa_source_output *o;
235 pa_resampler *resampler = NULL;
236 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX], fmt[PA_FORMAT_INFO_SNPRINT_MAX];
237 pa_channel_map volume_map;
238 int r;
239 char *pt;
240
241 pa_assert(_o);
242 pa_assert(core);
243 pa_assert(data);
244 pa_assert_ctl_context();
245
246 if (data->client)
247 pa_proplist_update(data->proplist, PA_UPDATE_MERGE, data->client->proplist);
248
249 if (data->destination_source && (data->destination_source->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER))
250 data->volume_writable = false;
251
252 if (!data->req_formats) {
253 /* From this point on, we want to work only with formats, and get back
254 * to using the sample spec and channel map after all decisions w.r.t.
255 * routing are complete. */
256 pa_format_info *f;
257 pa_idxset *formats;
258
259 f = pa_format_info_from_sample_spec2(&data->sample_spec, data->channel_map_is_set ? &data->channel_map : NULL,
260 !(data->flags & PA_SOURCE_OUTPUT_FIX_FORMAT),
261 !(data->flags & PA_SOURCE_OUTPUT_FIX_RATE),
262 !(data->flags & PA_SOURCE_OUTPUT_FIX_CHANNELS));
263 if (!f)
264 return -PA_ERR_INVALID;
265
266 formats = pa_idxset_new(NULL, NULL);
267 pa_idxset_put(formats, f, NULL);
268 pa_source_output_new_data_set_formats(data, formats);
269 }
270
271 if ((r = pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], data)) < 0)
272 return r;
273
274 pa_return_val_if_fail(!data->driver || pa_utf8_valid(data->driver), -PA_ERR_INVALID);
275
276 if (!data->source) {
277 pa_source *source;
278
279 if (data->direct_on_input) {
280 source = data->direct_on_input->sink->monitor_source;
281 pa_return_val_if_fail(source, -PA_ERR_INVALID);
282 } else {
283 source = pa_namereg_get(core, NULL, PA_NAMEREG_SOURCE);
284 pa_return_val_if_fail(source, -PA_ERR_NOENTITY);
285 }
286
287 pa_source_output_new_data_set_source(data, source, false, false);
288 }
289
290 /* If something didn't pick a format for us, pick the top-most format since
291 * we assume this is sorted in priority order */
292 if (!data->format && data->nego_formats && !pa_idxset_isempty(data->nego_formats))
293 data->format = pa_format_info_copy(pa_idxset_first(data->nego_formats, NULL));
294
295 if (PA_LIKELY(data->format)) {
296 /* We know that data->source is set, because data->format has been set.
297 * data->format is set after a successful format negotiation, and that
298 * can't happen before data->source has been set. */
299 pa_assert(data->source);
300
301 pa_log_debug("Negotiated format: %s", pa_format_info_snprint(fmt, sizeof(fmt), data->format));
302 } else {
303 pa_format_info *format;
304 uint32_t idx;
305
306 pa_log_info("Source does not support any requested format:");
307 PA_IDXSET_FOREACH(format, data->req_formats, idx)
308 pa_log_info(" -- %s", pa_format_info_snprint(fmt, sizeof(fmt), format));
309
310 return -PA_ERR_NOTSUPPORTED;
311 }
312
313 pa_return_val_if_fail(PA_SOURCE_IS_LINKED(data->source->state), -PA_ERR_BADSTATE);
314 pa_return_val_if_fail(!data->direct_on_input || data->direct_on_input->sink == data->source->monitor_of, -PA_ERR_INVALID);
315
316 /* Routing is done. We have a source and a format. */
317
318 if (data->volume_is_set && !pa_source_output_new_data_is_passthrough(data)) {
319 /* If volume is set, we need to save the original data->channel_map,
320 * so that we can remap the volume from the original channel map to the
321 * final channel map of the stream in case data->channel_map gets
322 * modified in pa_format_info_to_sample_spec2(). */
323 r = pa_stream_get_volume_channel_map(&data->volume, data->channel_map_is_set ? &data->channel_map : NULL, data->format, &volume_map);
324 if (r < 0)
325 return r;
326 } else {
327 /* Initialize volume_map to invalid state. We check the state later to
328 * determine if volume remapping is needed. */
329 pa_channel_map_init(&volume_map);
330 }
331
332 /* Now populate the sample spec and channel map according to the final
333 * format that we've negotiated */
334 r = pa_format_info_to_sample_spec2(data->format, &data->sample_spec, &data->channel_map, &data->source->sample_spec,
335 &data->source->channel_map);
336 if (r < 0)
337 return r;
338
339 /* Don't restore (or save) stream volume for passthrough streams and
340 * prevent attenuation/gain */
341 if (pa_source_output_new_data_is_passthrough(data)) {
342 data->volume_is_set = true;
343 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
344 data->volume_is_absolute = true;
345 data->save_volume = false;
346 }
347
348 if (!data->volume_is_set) {
349 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
350 data->volume_is_absolute = false;
351 data->save_volume = false;
352 }
353
354 if (!data->volume_writable)
355 data->save_volume = false;
356
357 if (pa_channel_map_valid(&volume_map))
358 /* The original volume channel map may be different than the final
359 * stream channel map, so remapping may be needed. */
360 pa_cvolume_remap(&data->volume, &volume_map, &data->channel_map);
361
362 if (!data->volume_factor_is_set)
363 pa_cvolume_reset(&data->volume_factor, data->sample_spec.channels);
364
365 pa_return_val_if_fail(pa_cvolume_compatible(&data->volume_factor, &data->sample_spec), -PA_ERR_INVALID);
366
367 if (!data->volume_factor_source_is_set)
368 pa_cvolume_reset(&data->volume_factor_source, data->source->sample_spec.channels);
369
370 pa_return_val_if_fail(pa_cvolume_compatible(&data->volume_factor_source, &data->source->sample_spec), -PA_ERR_INVALID);
371
372 if (!data->muted_is_set)
373 data->muted = false;
374
375 if (!(data->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) &&
376 !pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec)) {
377 /* try to change source format and rate. This is done before the FIXATE hook since
378 module-suspend-on-idle can resume a source */
379
380 pa_log_info("Trying to change sample spec");
381 pa_source_reconfigure(data->source, &data->sample_spec, pa_source_output_new_data_is_passthrough(data));
382 }
383
384 if (pa_source_output_new_data_is_passthrough(data) &&
385 !pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec)) {
386 /* rate update failed, or other parts of sample spec didn't match */
387
388 pa_log_debug("Could not update source sample spec to match passthrough stream");
389 return -PA_ERR_NOTSUPPORTED;
390 }
391
392 if (data->resample_method == PA_RESAMPLER_INVALID)
393 data->resample_method = core->resample_method;
394
395 pa_return_val_if_fail(data->resample_method < PA_RESAMPLER_MAX, -PA_ERR_INVALID);
396
397 if ((r = pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE], data)) < 0)
398 return r;
399
400 if ((data->flags & PA_SOURCE_OUTPUT_NO_CREATE_ON_SUSPEND) &&
401 data->source->state == PA_SOURCE_SUSPENDED) {
402 pa_log("Failed to create source output: source is suspended.");
403 return -PA_ERR_BADSTATE;
404 }
405
406 if (pa_idxset_size(data->source->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
407 pa_log("Failed to create source output: too many outputs per source.");
408 return -PA_ERR_TOOLARGE;
409 }
410
411 if ((data->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ||
412 !pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec) ||
413 !pa_channel_map_equal(&data->channel_map, &data->source->channel_map)) {
414
415 if (!pa_source_output_new_data_is_passthrough(data)) /* no resampler for passthrough content */
416 if (!(resampler = pa_resampler_new(
417 core->mempool,
418 &data->source->sample_spec, &data->source->channel_map,
419 &data->sample_spec, &data->channel_map,
420 core->lfe_crossover_freq,
421 data->resample_method,
422 ((data->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
423 ((data->flags & PA_SOURCE_OUTPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
424 (core->disable_remixing || (data->flags & PA_SOURCE_OUTPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0) |
425 (core->remixing_use_all_sink_channels ? 0 : PA_RESAMPLER_NO_FILL_SINK) |
426 (core->remixing_produce_lfe ? PA_RESAMPLER_PRODUCE_LFE : 0) |
427 (core->remixing_consume_lfe ? PA_RESAMPLER_CONSUME_LFE : 0)))) {
428 pa_log_warn("Unsupported resampling operation.");
429 return -PA_ERR_NOTSUPPORTED;
430 }
431 }
432
433 o = pa_msgobject_new(pa_source_output);
434 o->parent.parent.free = source_output_free;
435 o->parent.process_msg = pa_source_output_process_msg;
436
437 o->core = core;
438 o->state = PA_SOURCE_OUTPUT_INIT;
439 o->flags = data->flags;
440 o->proplist = pa_proplist_copy(data->proplist);
441 o->driver = pa_xstrdup(pa_path_get_filename(data->driver));
442 o->module = data->module;
443 o->source = data->source;
444 o->source_requested_by_application = data->source_requested_by_application;
445 o->destination_source = data->destination_source;
446 o->client = data->client;
447
448 o->requested_resample_method = data->resample_method;
449 o->actual_resample_method = resampler ? pa_resampler_get_method(resampler) : PA_RESAMPLER_INVALID;
450 o->sample_spec = data->sample_spec;
451 o->channel_map = data->channel_map;
452 o->format = pa_format_info_copy(data->format);
453
454 if (!data->volume_is_absolute && pa_source_flat_volume_enabled(o->source)) {
455 pa_cvolume remapped;
456
457 /* When the 'absolute' bool is not set then we'll treat the volume
458 * as relative to the source volume even in flat volume mode */
459 remapped = data->source->reference_volume;
460 pa_cvolume_remap(&remapped, &data->source->channel_map, &data->channel_map);
461 pa_sw_cvolume_multiply(&o->volume, &data->volume, &remapped);
462 } else
463 o->volume = data->volume;
464
465 o->volume_factor = data->volume_factor;
466 o->volume_factor_source = data->volume_factor_source;
467 o->real_ratio = o->reference_ratio = data->volume;
468 pa_cvolume_reset(&o->soft_volume, o->sample_spec.channels);
469 pa_cvolume_reset(&o->real_ratio, o->sample_spec.channels);
470 o->volume_writable = data->volume_writable;
471 o->save_volume = data->save_volume;
472 o->preferred_source = pa_xstrdup(data->preferred_source);
473 o->save_muted = data->save_muted;
474
475 o->muted = data->muted;
476
477 o->direct_on_input = data->direct_on_input;
478
479 reset_callbacks(o);
480 o->userdata = NULL;
481
482 o->thread_info.state = o->state;
483 o->thread_info.attached = false;
484 o->thread_info.sample_spec = o->sample_spec;
485 o->thread_info.resampler = resampler;
486 o->thread_info.soft_volume = o->soft_volume;
487 o->thread_info.muted = o->muted;
488 o->thread_info.requested_source_latency = (pa_usec_t) -1;
489 o->thread_info.direct_on_input = o->direct_on_input;
490
491 o->thread_info.delay_memblockq = pa_memblockq_new(
492 "source output delay_memblockq",
493 0,
494 MEMBLOCKQ_MAXLENGTH,
495 0,
496 &o->source->sample_spec,
497 0,
498 1,
499 0,
500 &o->source->silence);
501
502 pa_assert_se(pa_idxset_put(core->source_outputs, o, &o->index) == 0);
503 pa_assert_se(pa_idxset_put(o->source->outputs, pa_source_output_ref(o), NULL) == 0);
504
505 if (o->client)
506 pa_assert_se(pa_idxset_put(o->client->source_outputs, o, NULL) >= 0);
507
508 if (o->direct_on_input)
509 pa_assert_se(pa_idxset_put(o->direct_on_input->direct_outputs, o, NULL) == 0);
510
511 pt = pa_proplist_to_string_sep(o->proplist, "\n ");
512 pa_log_info("Created output %u \"%s\" on %s with sample spec %s and channel map %s\n %s",
513 o->index,
514 pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_MEDIA_NAME)),
515 o->source->name,
516 pa_sample_spec_snprint(st, sizeof(st), &o->sample_spec),
517 pa_channel_map_snprint(cm, sizeof(cm), &o->channel_map),
518 pt);
519 pa_xfree(pt);
520
521 /* Don't forget to call pa_source_output_put! */
522
523 *_o = o;
524 return 0;
525 }
526
527 /* Called from main context */
update_n_corked(pa_source_output * o,pa_source_output_state_t state)528 static void update_n_corked(pa_source_output *o, pa_source_output_state_t state) {
529 pa_assert(o);
530 pa_assert_ctl_context();
531
532 if (!o->source)
533 return;
534
535 if (o->state == PA_SOURCE_OUTPUT_CORKED && state != PA_SOURCE_OUTPUT_CORKED)
536 pa_assert_se(o->source->n_corked -- >= 1);
537 else if (o->state != PA_SOURCE_OUTPUT_CORKED && state == PA_SOURCE_OUTPUT_CORKED)
538 o->source->n_corked++;
539 }
540
541 /* Called from main context */
source_output_set_state(pa_source_output * o,pa_source_output_state_t state)542 static void source_output_set_state(pa_source_output *o, pa_source_output_state_t state) {
543
544 pa_assert(o);
545 pa_assert_ctl_context();
546
547 if (o->state == state)
548 return;
549
550 if (o->source) {
551 if (o->state == PA_SOURCE_OUTPUT_CORKED && state == PA_SOURCE_OUTPUT_RUNNING && pa_source_used_by(o->source) == 0 &&
552 !pa_sample_spec_equal(&o->sample_spec, &o->source->sample_spec)) {
553 /* We were uncorked and the source was not playing anything -- let's try
554 * to update the sample format and rate to avoid resampling */
555 pa_source_reconfigure(o->source, &o->sample_spec, pa_source_output_is_passthrough(o));
556 }
557
558 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) == 0);
559 } else
560 /* If the source is not valid, pa_source_output_set_state_within_thread() must be called directly */
561 pa_source_output_set_state_within_thread(o, state);
562
563 update_n_corked(o, state);
564 o->state = state;
565
566 if (state != PA_SOURCE_OUTPUT_UNLINKED) {
567 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED], o);
568
569 if (PA_SOURCE_OUTPUT_IS_LINKED(state))
570 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
571 }
572
573 if (o->source)
574 pa_source_update_status(o->source);
575 }
576
577 /* Called from main context */
pa_source_output_unlink(pa_source_output * o)578 void pa_source_output_unlink(pa_source_output*o) {
579 bool linked;
580
581 pa_source_output_assert_ref(o);
582 pa_assert_ctl_context();
583
584 /* See pa_sink_unlink() for a couple of comments how this function
585 * works */
586
587 pa_source_output_ref(o);
588
589 linked = PA_SOURCE_OUTPUT_IS_LINKED(o->state);
590
591 if (linked)
592 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK], o);
593
594 if (o->direct_on_input)
595 pa_idxset_remove_by_data(o->direct_on_input->direct_outputs, o, NULL);
596
597 pa_idxset_remove_by_data(o->core->source_outputs, o, NULL);
598
599 if (o->source)
600 if (pa_idxset_remove_by_data(o->source->outputs, o, NULL))
601 pa_source_output_unref(o);
602
603 if (o->client)
604 pa_idxset_remove_by_data(o->client->source_outputs, o, NULL);
605
606 update_n_corked(o, PA_SOURCE_OUTPUT_UNLINKED);
607 o->state = PA_SOURCE_OUTPUT_UNLINKED;
608
609 if (linked && o->source) {
610 if (pa_source_output_is_passthrough(o))
611 pa_source_leave_passthrough(o->source);
612
613 /* We might need to update the source's volume if we are in flat volume mode. */
614 if (pa_source_flat_volume_enabled(o->source))
615 pa_source_set_volume(o->source, NULL, false, false);
616
617 if (o->source->asyncmsgq)
618 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL) == 0);
619 }
620
621 reset_callbacks(o);
622
623 if (o->source) {
624 if (PA_SOURCE_IS_LINKED(o->source->state))
625 pa_source_update_status(o->source);
626
627 o->source = NULL;
628 }
629
630 if (linked) {
631 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_REMOVE, o->index);
632 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST], o);
633 }
634
635 pa_core_maybe_vacuum(o->core);
636
637 pa_source_output_unref(o);
638 }
639
640 /* Called from main context */
source_output_free(pa_object * mo)641 static void source_output_free(pa_object* mo) {
642 pa_source_output *o = PA_SOURCE_OUTPUT(mo);
643
644 pa_assert(o);
645 pa_assert_ctl_context();
646 pa_assert(pa_source_output_refcnt(o) == 0);
647 pa_assert(!PA_SOURCE_OUTPUT_IS_LINKED(o->state));
648
649 pa_log_info("Freeing output %u \"%s\"", o->index,
650 o->proplist ? pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_MEDIA_NAME)) : "");
651
652 if (o->thread_info.delay_memblockq)
653 pa_memblockq_free(o->thread_info.delay_memblockq);
654
655 if (o->thread_info.resampler)
656 pa_resampler_free(o->thread_info.resampler);
657
658 if (o->format)
659 pa_format_info_free(o->format);
660
661 if (o->proplist)
662 pa_proplist_free(o->proplist);
663
664 if (o->preferred_source)
665 pa_xfree(o->preferred_source);
666
667 pa_xfree(o->driver);
668 pa_xfree(o);
669 }
670
671 /* Called from main context */
pa_source_output_put(pa_source_output * o)672 void pa_source_output_put(pa_source_output *o) {
673 pa_source_output_state_t state;
674
675 pa_source_output_assert_ref(o);
676 pa_assert_ctl_context();
677
678 pa_assert(o->state == PA_SOURCE_OUTPUT_INIT);
679
680 /* The following fields must be initialized properly */
681 pa_assert(o->push);
682 pa_assert(o->kill);
683
684 state = o->flags & PA_SOURCE_OUTPUT_START_CORKED ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING;
685
686 update_n_corked(o, state);
687 o->state = state;
688
689 /* We might need to update the source's volume if we are in flat volume mode. */
690 if (pa_source_flat_volume_enabled(o->source))
691 pa_source_set_volume(o->source, NULL, false, o->save_volume);
692 else {
693 if (o->destination_source && (o->destination_source->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER)) {
694 pa_assert(pa_cvolume_is_norm(&o->volume));
695 pa_assert(pa_cvolume_is_norm(&o->reference_ratio));
696 }
697
698 set_real_ratio(o, &o->volume);
699 }
700
701 if (pa_source_output_is_passthrough(o))
702 pa_source_enter_passthrough(o->source);
703
704 o->thread_info.soft_volume = o->soft_volume;
705 o->thread_info.muted = o->muted;
706
707 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_ADD_OUTPUT, o, 0, NULL) == 0);
708
709 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, o->index);
710 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PUT], o);
711
712 pa_source_update_status(o->source);
713 }
714
715 /* Called from main context */
pa_source_output_kill(pa_source_output * o)716 void pa_source_output_kill(pa_source_output*o) {
717 pa_source_output_assert_ref(o);
718 pa_assert_ctl_context();
719 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
720
721 o->kill(o);
722 }
723
724 /* Called from main context */
pa_source_output_get_latency(pa_source_output * o,pa_usec_t * source_latency)725 pa_usec_t pa_source_output_get_latency(pa_source_output *o, pa_usec_t *source_latency) {
726 pa_usec_t r[2] = { 0, 0 };
727
728 pa_source_output_assert_ref(o);
729 pa_assert_ctl_context();
730 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
731
732 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY, r, 0, NULL) == 0);
733
734 if (o->get_latency)
735 r[0] += o->get_latency(o);
736
737 if (source_latency)
738 *source_latency = r[1];
739
740 return r[0];
741 }
742
743 /* Called from thread context */
pa_source_output_push(pa_source_output * o,const pa_memchunk * chunk)744 void pa_source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
745 bool need_volume_factor_source;
746 bool volume_is_norm;
747 size_t length;
748 size_t limit, mbs = 0;
749
750 pa_source_output_assert_ref(o);
751 pa_source_output_assert_io_context(o);
752 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
753 pa_assert(chunk);
754 pa_assert(pa_frame_aligned(chunk->length, &o->source->sample_spec));
755
756 if (!o->push || o->thread_info.state == PA_SOURCE_OUTPUT_CORKED)
757 return;
758
759 pa_assert(o->thread_info.state == PA_SOURCE_OUTPUT_RUNNING);
760
761 if (pa_memblockq_push(o->thread_info.delay_memblockq, chunk) < 0) {
762 pa_log_debug("Delay queue overflow!");
763 pa_memblockq_seek(o->thread_info.delay_memblockq, (int64_t) chunk->length, PA_SEEK_RELATIVE, true);
764 }
765
766 limit = o->process_rewind ? 0 : o->source->thread_info.max_rewind;
767
768 volume_is_norm = pa_cvolume_is_norm(&o->thread_info.soft_volume) && !o->thread_info.muted;
769 need_volume_factor_source = !pa_cvolume_is_norm(&o->volume_factor_source);
770
771 if (limit > 0 && o->source->monitor_of) {
772 pa_usec_t latency;
773 size_t n;
774
775 /* Hmm, check the latency for knowing how much of the buffered
776 * data is actually still unplayed and might hence still
777 * change. This is suboptimal. Ideally we'd have a call like
778 * pa_sink_get_changeable_size() or so that tells us how much
779 * of the queued data is actually still changeable. Hence
780 * FIXME! */
781
782 latency = pa_sink_get_latency_within_thread(o->source->monitor_of, false);
783
784 n = pa_usec_to_bytes(latency, &o->source->sample_spec);
785
786 if (n < limit)
787 limit = n;
788 }
789
790 /* Implement the delay queue */
791 while ((length = pa_memblockq_get_length(o->thread_info.delay_memblockq)) > limit) {
792 pa_memchunk qchunk;
793 bool nvfs = need_volume_factor_source;
794
795 length -= limit;
796
797 pa_assert_se(pa_memblockq_peek(o->thread_info.delay_memblockq, &qchunk) >= 0);
798
799 if (qchunk.length > length)
800 qchunk.length = length;
801
802 pa_assert(qchunk.length > 0);
803
804 /* It might be necessary to adjust the volume here */
805 if (!volume_is_norm) {
806 pa_memchunk_make_writable(&qchunk, 0);
807
808 if (o->thread_info.muted) {
809 pa_silence_memchunk(&qchunk, &o->source->sample_spec);
810 nvfs = false;
811
812 } else if (!o->thread_info.resampler && nvfs) {
813 pa_cvolume v;
814
815 /* If we don't need a resampler we can merge the
816 * post and the pre volume adjustment into one */
817
818 pa_sw_cvolume_multiply(&v, &o->thread_info.soft_volume, &o->volume_factor_source);
819 pa_volume_memchunk(&qchunk, &o->source->sample_spec, &v);
820 nvfs = false;
821
822 } else
823 pa_volume_memchunk(&qchunk, &o->source->sample_spec, &o->thread_info.soft_volume);
824 }
825
826 if (nvfs) {
827 pa_memchunk_make_writable(&qchunk, 0);
828 pa_volume_memchunk(&qchunk, &o->source->sample_spec, &o->volume_factor_source);
829 }
830
831 if (!o->thread_info.resampler)
832 o->push(o, &qchunk);
833 else {
834 pa_memchunk rchunk;
835
836 if (mbs == 0)
837 mbs = pa_resampler_max_block_size(o->thread_info.resampler);
838
839 if (qchunk.length > mbs)
840 qchunk.length = mbs;
841
842 pa_resampler_run(o->thread_info.resampler, &qchunk, &rchunk);
843
844 if (rchunk.length > 0)
845 o->push(o, &rchunk);
846
847 if (rchunk.memblock)
848 pa_memblock_unref(rchunk.memblock);
849 }
850
851 pa_memblock_unref(qchunk.memblock);
852 pa_memblockq_drop(o->thread_info.delay_memblockq, qchunk.length);
853 }
854 }
855
856 /* Called from thread context */
pa_source_output_process_rewind(pa_source_output * o,size_t nbytes)857 void pa_source_output_process_rewind(pa_source_output *o, size_t nbytes /* in source sample spec */) {
858
859 pa_source_output_assert_ref(o);
860 pa_source_output_assert_io_context(o);
861 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
862 pa_assert(pa_frame_aligned(nbytes, &o->source->sample_spec));
863
864 if (nbytes <= 0)
865 return;
866
867 if (o->process_rewind) {
868 pa_assert(pa_memblockq_get_length(o->thread_info.delay_memblockq) == 0);
869
870 if (o->thread_info.resampler)
871 nbytes = pa_resampler_result(o->thread_info.resampler, nbytes);
872
873 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) nbytes);
874
875 if (nbytes > 0)
876 o->process_rewind(o, nbytes);
877
878 if (o->thread_info.resampler)
879 pa_resampler_rewind(o->thread_info.resampler, nbytes);
880
881 } else
882 pa_memblockq_seek(o->thread_info.delay_memblockq, - ((int64_t) nbytes), PA_SEEK_RELATIVE, true);
883 }
884
885 /* Called from thread context */
pa_source_output_get_max_rewind(pa_source_output * o)886 size_t pa_source_output_get_max_rewind(pa_source_output *o) {
887 pa_source_output_assert_ref(o);
888 pa_source_output_assert_io_context(o);
889
890 return o->thread_info.resampler ? pa_resampler_request(o->thread_info.resampler, o->source->thread_info.max_rewind) : o->source->thread_info.max_rewind;
891 }
892
893 /* Called from thread context */
pa_source_output_update_max_rewind(pa_source_output * o,size_t nbytes)894 void pa_source_output_update_max_rewind(pa_source_output *o, size_t nbytes /* in the source's sample spec */) {
895 pa_source_output_assert_ref(o);
896 pa_source_output_assert_io_context(o);
897 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
898 pa_assert(pa_frame_aligned(nbytes, &o->source->sample_spec));
899
900 if (o->update_max_rewind)
901 o->update_max_rewind(o, o->thread_info.resampler ? pa_resampler_result(o->thread_info.resampler, nbytes) : nbytes);
902 }
903
904 /* Called from thread context */
pa_source_output_set_requested_latency_within_thread(pa_source_output * o,pa_usec_t usec)905 pa_usec_t pa_source_output_set_requested_latency_within_thread(pa_source_output *o, pa_usec_t usec) {
906 pa_source_output_assert_ref(o);
907 pa_source_output_assert_io_context(o);
908
909 if (!(o->source->flags & PA_SOURCE_DYNAMIC_LATENCY))
910 usec = o->source->thread_info.fixed_latency;
911
912 if (usec != (pa_usec_t) -1)
913 usec = PA_CLAMP(usec, o->source->thread_info.min_latency, o->source->thread_info.max_latency);
914
915 o->thread_info.requested_source_latency = usec;
916 pa_source_invalidate_requested_latency(o->source, true);
917
918 return usec;
919 }
920
921 /* Called from main context */
pa_source_output_set_requested_latency(pa_source_output * o,pa_usec_t usec)922 pa_usec_t pa_source_output_set_requested_latency(pa_source_output *o, pa_usec_t usec) {
923 pa_source_output_assert_ref(o);
924 pa_assert_ctl_context();
925
926 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state) && o->source) {
927 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
928 return usec;
929 }
930
931 /* If this source output is not realized yet or is being moved, we
932 * have to touch the thread info data directly */
933
934 if (o->source) {
935 if (!(o->source->flags & PA_SOURCE_DYNAMIC_LATENCY))
936 usec = pa_source_get_fixed_latency(o->source);
937
938 if (usec != (pa_usec_t) -1) {
939 pa_usec_t min_latency, max_latency;
940 pa_source_get_latency_range(o->source, &min_latency, &max_latency);
941 usec = PA_CLAMP(usec, min_latency, max_latency);
942 }
943 }
944
945 o->thread_info.requested_source_latency = usec;
946
947 return usec;
948 }
949
950 /* Called from main context */
pa_source_output_get_requested_latency(pa_source_output * o)951 pa_usec_t pa_source_output_get_requested_latency(pa_source_output *o) {
952 pa_source_output_assert_ref(o);
953 pa_assert_ctl_context();
954
955 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state) && o->source) {
956 pa_usec_t usec = 0;
957 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
958 return usec;
959 }
960
961 /* If this source output is not realized yet or is being moved, we
962 * have to touch the thread info data directly */
963
964 return o->thread_info.requested_source_latency;
965 }
966
967 /* Called from main context */
pa_source_output_set_volume(pa_source_output * o,const pa_cvolume * volume,bool save,bool absolute)968 void pa_source_output_set_volume(pa_source_output *o, const pa_cvolume *volume, bool save, bool absolute) {
969 pa_cvolume v;
970
971 pa_source_output_assert_ref(o);
972 pa_assert_ctl_context();
973 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
974 pa_assert(volume);
975 pa_assert(pa_cvolume_valid(volume));
976 pa_assert(volume->channels == 1 || pa_cvolume_compatible(volume, &o->sample_spec));
977 pa_assert(o->volume_writable);
978
979 if (!absolute && pa_source_flat_volume_enabled(o->source)) {
980 v = o->source->reference_volume;
981 pa_cvolume_remap(&v, &o->source->channel_map, &o->channel_map);
982
983 if (pa_cvolume_compatible(volume, &o->sample_spec))
984 volume = pa_sw_cvolume_multiply(&v, &v, volume);
985 else
986 volume = pa_sw_cvolume_multiply_scalar(&v, &v, pa_cvolume_max(volume));
987 } else {
988 if (!pa_cvolume_compatible(volume, &o->sample_spec)) {
989 v = o->volume;
990 volume = pa_cvolume_scale(&v, pa_cvolume_max(volume));
991 }
992 }
993
994 if (pa_cvolume_equal(volume, &o->volume)) {
995 o->save_volume = o->save_volume || save;
996 return;
997 }
998
999 pa_source_output_set_volume_direct(o, volume);
1000 o->save_volume = save;
1001
1002 if (pa_source_flat_volume_enabled(o->source)) {
1003 /* We are in flat volume mode, so let's update all source input
1004 * volumes and update the flat volume of the source */
1005
1006 pa_source_set_volume(o->source, NULL, true, save);
1007
1008 } else {
1009 /* OK, we are in normal volume mode. The volume only affects
1010 * ourselves */
1011 set_real_ratio(o, volume);
1012
1013 /* Copy the new soft_volume to the thread_info struct */
1014 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_VOLUME, NULL, 0, NULL) == 0);
1015 }
1016
1017 /* The volume changed, let's tell people so */
1018 if (o->volume_changed)
1019 o->volume_changed(o);
1020
1021 /* The virtual volume changed, let's tell people so */
1022 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1023 }
1024
1025 /* Called from main context */
set_real_ratio(pa_source_output * o,const pa_cvolume * v)1026 static void set_real_ratio(pa_source_output *o, const pa_cvolume *v) {
1027 pa_source_output_assert_ref(o);
1028 pa_assert_ctl_context();
1029 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1030 pa_assert(!v || pa_cvolume_compatible(v, &o->sample_spec));
1031
1032 /* This basically calculates:
1033 *
1034 * o->real_ratio := v
1035 * o->soft_volume := o->real_ratio * o->volume_factor */
1036
1037 if (v)
1038 o->real_ratio = *v;
1039 else
1040 pa_cvolume_reset(&o->real_ratio, o->sample_spec.channels);
1041
1042 pa_sw_cvolume_multiply(&o->soft_volume, &o->real_ratio, &o->volume_factor);
1043 /* We don't copy the data to the thread_info data. That's left for someone else to do */
1044 }
1045
1046 /* Called from main or I/O context */
pa_source_output_is_passthrough(pa_source_output * o)1047 bool pa_source_output_is_passthrough(pa_source_output *o) {
1048 pa_source_output_assert_ref(o);
1049
1050 if (PA_UNLIKELY(!pa_format_info_is_pcm(o->format)))
1051 return true;
1052
1053 if (PA_UNLIKELY(o->flags & PA_SOURCE_OUTPUT_PASSTHROUGH))
1054 return true;
1055
1056 return false;
1057 }
1058
1059 /* Called from main context */
pa_source_output_is_volume_readable(pa_source_output * o)1060 bool pa_source_output_is_volume_readable(pa_source_output *o) {
1061 pa_source_output_assert_ref(o);
1062 pa_assert_ctl_context();
1063
1064 return !pa_source_output_is_passthrough(o);
1065 }
1066
1067 /* Called from main context */
pa_source_output_get_volume(pa_source_output * o,pa_cvolume * volume,bool absolute)1068 pa_cvolume *pa_source_output_get_volume(pa_source_output *o, pa_cvolume *volume, bool absolute) {
1069 pa_source_output_assert_ref(o);
1070 pa_assert_ctl_context();
1071 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1072 pa_assert(pa_source_output_is_volume_readable(o));
1073
1074 if (absolute || !pa_source_flat_volume_enabled(o->source))
1075 *volume = o->volume;
1076 else
1077 *volume = o->reference_ratio;
1078
1079 return volume;
1080 }
1081
1082 /* Called from main context */
pa_source_output_set_mute(pa_source_output * o,bool mute,bool save)1083 void pa_source_output_set_mute(pa_source_output *o, bool mute, bool save) {
1084 bool old_mute;
1085
1086 pa_source_output_assert_ref(o);
1087 pa_assert_ctl_context();
1088 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1089
1090 old_mute = o->muted;
1091
1092 if (mute == old_mute) {
1093 o->save_muted |= save;
1094 return;
1095 }
1096
1097 o->muted = mute;
1098 pa_log_debug("The mute of source output %u changed from %s to %s.", o->index, pa_yes_no(old_mute), pa_yes_no(mute));
1099
1100 o->save_muted = save;
1101
1102 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE, NULL, 0, NULL) == 0);
1103
1104 /* The mute status changed, let's tell people so */
1105 if (o->mute_changed)
1106 o->mute_changed(o);
1107
1108 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1109 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MUTE_CHANGED], o);
1110 }
1111
pa_source_output_set_property(pa_source_output * o,const char * key,const char * value)1112 void pa_source_output_set_property(pa_source_output *o, const char *key, const char *value) {
1113 char *old_value = NULL;
1114 const char *new_value;
1115
1116 pa_assert(o);
1117 pa_assert(key);
1118
1119 if (pa_proplist_contains(o->proplist, key)) {
1120 old_value = pa_xstrdup(pa_proplist_gets(o->proplist, key));
1121 if (value && old_value && pa_streq(value, old_value))
1122 goto finish;
1123
1124 if (!old_value)
1125 old_value = pa_xstrdup("(data)");
1126 } else {
1127 if (!value)
1128 goto finish;
1129
1130 old_value = pa_xstrdup("(unset)");
1131 }
1132
1133 if (value) {
1134 pa_proplist_sets(o->proplist, key, value);
1135 new_value = value;
1136 } else {
1137 pa_proplist_unset(o->proplist, key);
1138 new_value = "(unset)";
1139 }
1140
1141 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state)) {
1142 pa_log_debug("Source output %u: proplist[%s]: %s -> %s", o->index, key, old_value, new_value);
1143 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED], o);
1144 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT | PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1145 }
1146
1147 finish:
1148 pa_xfree(old_value);
1149 }
1150
pa_source_output_set_property_arbitrary(pa_source_output * o,const char * key,const uint8_t * value,size_t nbytes)1151 void pa_source_output_set_property_arbitrary(pa_source_output *o, const char *key, const uint8_t *value, size_t nbytes) {
1152 const uint8_t *old_value;
1153 size_t old_nbytes;
1154 const char *old_value_str;
1155 const char *new_value_str;
1156
1157 pa_assert(o);
1158 pa_assert(key);
1159
1160 if (pa_proplist_get(o->proplist, key, (const void **) &old_value, &old_nbytes) >= 0) {
1161 if (value && nbytes == old_nbytes && !memcmp(value, old_value, nbytes))
1162 return;
1163
1164 old_value_str = "(data)";
1165
1166 } else {
1167 if (!value)
1168 return;
1169
1170 old_value_str = "(unset)";
1171 }
1172
1173 if (value) {
1174 pa_proplist_set(o->proplist, key, value, nbytes);
1175 new_value_str = "(data)";
1176 } else {
1177 pa_proplist_unset(o->proplist, key);
1178 new_value_str = "(unset)";
1179 }
1180
1181 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state)) {
1182 pa_log_debug("Source output %u: proplist[%s]: %s -> %s", o->index, key, old_value_str, new_value_str);
1183 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED], o);
1184 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT | PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1185 }
1186 }
1187
1188 /* Called from main thread */
pa_source_output_update_proplist(pa_source_output * o,pa_update_mode_t mode,pa_proplist * p)1189 void pa_source_output_update_proplist(pa_source_output *o, pa_update_mode_t mode, pa_proplist *p) {
1190 void *state;
1191 const char *key;
1192 const uint8_t *value;
1193 size_t nbytes;
1194
1195 pa_source_output_assert_ref(o);
1196 pa_assert(p);
1197 pa_assert_ctl_context();
1198
1199 switch (mode) {
1200 case PA_UPDATE_SET:
1201 /* Delete everything that is not in p. */
1202 for (state = NULL; (key = pa_proplist_iterate(o->proplist, &state));) {
1203 if (!pa_proplist_contains(p, key))
1204 pa_source_output_set_property(o, key, NULL);
1205 }
1206
1207 /* Fall through. */
1208 case PA_UPDATE_REPLACE:
1209 for (state = NULL; (key = pa_proplist_iterate(p, &state));) {
1210 pa_proplist_get(p, key, (const void **) &value, &nbytes);
1211 pa_source_output_set_property_arbitrary(o, key, value, nbytes);
1212 }
1213
1214 break;
1215 case PA_UPDATE_MERGE:
1216 for (state = NULL; (key = pa_proplist_iterate(p, &state));) {
1217 if (pa_proplist_contains(o->proplist, key))
1218 continue;
1219
1220 pa_proplist_get(p, key, (const void **) &value, &nbytes);
1221 pa_source_output_set_property_arbitrary(o, key, value, nbytes);
1222 }
1223
1224 break;
1225 }
1226 }
1227
1228 /* Called from main context */
pa_source_output_cork(pa_source_output * o,bool b)1229 void pa_source_output_cork(pa_source_output *o, bool b) {
1230 pa_source_output_assert_ref(o);
1231 pa_assert_ctl_context();
1232 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1233
1234 source_output_set_state(o, b ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING);
1235 }
1236
1237 /* Called from main context */
pa_source_output_set_rate(pa_source_output * o,uint32_t rate)1238 int pa_source_output_set_rate(pa_source_output *o, uint32_t rate) {
1239 pa_source_output_assert_ref(o);
1240 pa_assert_ctl_context();
1241 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1242 pa_return_val_if_fail(o->thread_info.resampler, -PA_ERR_BADSTATE);
1243
1244 if (o->sample_spec.rate == rate)
1245 return 0;
1246
1247 o->sample_spec.rate = rate;
1248
1249 pa_asyncmsgq_post(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE, PA_UINT_TO_PTR(rate), 0, NULL, NULL);
1250
1251 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1252 return 0;
1253 }
1254
1255 /* Called from main context */
pa_source_output_get_resample_method(pa_source_output * o)1256 pa_resample_method_t pa_source_output_get_resample_method(pa_source_output *o) {
1257 pa_source_output_assert_ref(o);
1258 pa_assert_ctl_context();
1259
1260 return o->actual_resample_method;
1261 }
1262
1263 /* Called from main context */
pa_source_output_may_move(pa_source_output * o)1264 bool pa_source_output_may_move(pa_source_output *o) {
1265 pa_source_output_assert_ref(o);
1266 pa_assert_ctl_context();
1267 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1268
1269 if (o->flags & PA_SOURCE_OUTPUT_DONT_MOVE)
1270 return false;
1271
1272 if (o->direct_on_input)
1273 return false;
1274
1275 return true;
1276 }
1277
find_filter_source_output(pa_source_output * target,pa_source * s)1278 static bool find_filter_source_output(pa_source_output *target, pa_source *s) {
1279 unsigned PA_UNUSED i = 0;
1280 while (s && s->output_from_master) {
1281 if (s->output_from_master == target)
1282 return true;
1283 s = s->output_from_master->source;
1284 pa_assert(i++ < 100);
1285 }
1286 return false;
1287 }
1288
is_filter_source_moving(pa_source_output * o)1289 static bool is_filter_source_moving(pa_source_output *o) {
1290 pa_source *source = o->source;
1291
1292 if (!source)
1293 return false;
1294
1295 while (source->output_from_master) {
1296 source = source->output_from_master->source;
1297
1298 if (!source)
1299 return true;
1300 }
1301
1302 return false;
1303 }
1304
1305 /* Called from main context */
pa_source_output_may_move_to(pa_source_output * o,pa_source * dest)1306 bool pa_source_output_may_move_to(pa_source_output *o, pa_source *dest) {
1307 pa_source_output_assert_ref(o);
1308 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1309 pa_source_assert_ref(dest);
1310
1311 if (dest == o->source)
1312 return true;
1313
1314 if (dest->unlink_requested)
1315 return false;
1316
1317 if (!pa_source_output_may_move(o))
1318 return false;
1319
1320 /* Make sure we're not creating a filter source cycle */
1321 if (find_filter_source_output(o, dest)) {
1322 pa_log_debug("Can't connect output to %s, as that would create a cycle.", dest->name);
1323 return false;
1324 }
1325
1326 /* If this source output is connected to a filter source that itself is
1327 * moving, then don't allow the move. Moving requires sending a message to
1328 * the IO thread of the old source, and if the old source is a filter
1329 * source that is moving, there's no IO thread associated to the old
1330 * source. */
1331 if (is_filter_source_moving(o)) {
1332 pa_log_debug("Can't move output from filter source %s, because the filter source itself is currently moving.",
1333 o->source->name);
1334 return false;
1335 }
1336
1337 if (pa_idxset_size(dest->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
1338 pa_log_warn("Failed to move source output: too many outputs per source.");
1339 return false;
1340 }
1341
1342 if (o->may_move_to)
1343 if (!o->may_move_to(o, dest))
1344 return false;
1345
1346 return true;
1347 }
1348
1349 /* Called from main context */
pa_source_output_start_move(pa_source_output * o)1350 int pa_source_output_start_move(pa_source_output *o) {
1351 pa_source *origin;
1352 int r;
1353
1354 pa_source_output_assert_ref(o);
1355 pa_assert_ctl_context();
1356 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1357 pa_assert(o->source);
1358
1359 if (!pa_source_output_may_move(o))
1360 return -PA_ERR_NOTSUPPORTED;
1361
1362 if ((r = pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START], o)) < 0)
1363 return r;
1364
1365 pa_log_debug("Starting to move source output %u from '%s'", (unsigned) o->index, o->source->name);
1366
1367 origin = o->source;
1368
1369 pa_idxset_remove_by_data(o->source->outputs, o, NULL);
1370
1371 if (o->state == PA_SOURCE_OUTPUT_CORKED)
1372 pa_assert_se(origin->n_corked-- >= 1);
1373
1374 if (pa_source_output_is_passthrough(o))
1375 pa_source_leave_passthrough(o->source);
1376
1377 if (pa_source_flat_volume_enabled(o->source))
1378 /* We might need to update the source's volume if we are in flat
1379 * volume mode. */
1380 pa_source_set_volume(o->source, NULL, false, false);
1381
1382 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL) == 0);
1383
1384 pa_source_update_status(o->source);
1385
1386 pa_cvolume_remap(&o->volume_factor_source, &o->source->channel_map, &o->channel_map);
1387
1388 o->source = NULL;
1389 o->source_requested_by_application = false;
1390
1391 pa_source_output_unref(o);
1392
1393 return 0;
1394 }
1395
1396 /* Called from main context. If it has an origin source that uses volume sharing,
1397 * then also the origin source and all streams connected to it need to update
1398 * their volume - this function does all that by using recursion. */
update_volume_due_to_moving(pa_source_output * o,pa_source * dest)1399 static void update_volume_due_to_moving(pa_source_output *o, pa_source *dest) {
1400 pa_cvolume new_volume;
1401
1402 pa_assert(o);
1403 pa_assert(dest);
1404 pa_assert(o->source); /* The destination source should already be set. */
1405
1406 if (o->destination_source && (o->destination_source->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER)) {
1407 pa_source *root_source;
1408 pa_source_output *destination_source_output;
1409 uint32_t idx;
1410
1411 root_source = pa_source_get_master(o->source);
1412
1413 if (PA_UNLIKELY(!root_source))
1414 return;
1415
1416 if (pa_source_flat_volume_enabled(o->source)) {
1417 /* Ok, so the origin source uses volume sharing, and flat volume is
1418 * enabled. The volume will have to be updated as follows:
1419 *
1420 * o->volume := o->source->real_volume
1421 * (handled later by pa_source_set_volume)
1422 * o->reference_ratio := o->volume / o->source->reference_volume
1423 * (handled later by pa_source_set_volume)
1424 * o->real_ratio stays unchanged
1425 * (streams whose origin source uses volume sharing should
1426 * always have real_ratio of 0 dB)
1427 * o->soft_volume stays unchanged
1428 * (streams whose origin source uses volume sharing should
1429 * always have volume_factor as soft_volume, so no change
1430 * should be needed) */
1431
1432 pa_assert(pa_cvolume_is_norm(&o->real_ratio));
1433 pa_assert(pa_cvolume_equal(&o->soft_volume, &o->volume_factor));
1434
1435 /* Notifications will be sent by pa_source_set_volume(). */
1436
1437 } else {
1438 /* Ok, so the origin source uses volume sharing, and flat volume is
1439 * disabled. The volume will have to be updated as follows:
1440 *
1441 * o->volume := 0 dB
1442 * o->reference_ratio := 0 dB
1443 * o->real_ratio stays unchanged
1444 * (streams whose origin source uses volume sharing should
1445 * always have real_ratio of 0 dB)
1446 * o->soft_volume stays unchanged
1447 * (streams whose origin source uses volume sharing should
1448 * always have volume_factor as soft_volume, so no change
1449 * should be needed) */
1450
1451 pa_cvolume_reset(&new_volume, o->volume.channels);
1452 pa_source_output_set_volume_direct(o, &new_volume);
1453 pa_source_output_set_reference_ratio(o, &new_volume);
1454 pa_assert(pa_cvolume_is_norm(&o->real_ratio));
1455 pa_assert(pa_cvolume_equal(&o->soft_volume, &o->volume_factor));
1456 }
1457
1458 /* Additionally, the origin source volume needs updating:
1459 *
1460 * o->destination_source->reference_volume := root_source->reference_volume
1461 * o->destination_source->real_volume := root_source->real_volume
1462 * o->destination_source->soft_volume stays unchanged
1463 * (sources that use volume sharing should always have
1464 * soft_volume of 0 dB) */
1465
1466 new_volume = root_source->reference_volume;
1467 pa_cvolume_remap(&new_volume, &root_source->channel_map, &o->destination_source->channel_map);
1468 pa_source_set_reference_volume_direct(o->destination_source, &new_volume);
1469
1470 o->destination_source->real_volume = root_source->real_volume;
1471 pa_cvolume_remap(&o->destination_source->real_volume, &root_source->channel_map, &o->destination_source->channel_map);
1472
1473 pa_assert(pa_cvolume_is_norm(&o->destination_source->soft_volume));
1474
1475 /* If you wonder whether o->destination_source->set_volume() should be
1476 * called somewhere, that's not the case, because sources that use
1477 * volume sharing shouldn't have any internal volume that set_volume()
1478 * would update. If you wonder whether the thread_info variables should
1479 * be synced, yes, they should, and it's done by the
1480 * PA_SOURCE_MESSAGE_FINISH_MOVE message handler. */
1481
1482 /* Recursively update origin source outputs. */
1483 PA_IDXSET_FOREACH(destination_source_output, o->destination_source->outputs, idx)
1484 update_volume_due_to_moving(destination_source_output, dest);
1485
1486 } else {
1487 if (pa_source_flat_volume_enabled(o->source)) {
1488 /* Ok, so this is a regular stream, and flat volume is enabled. The
1489 * volume will have to be updated as follows:
1490 *
1491 * o->volume := o->reference_ratio * o->source->reference_volume
1492 * o->reference_ratio stays unchanged
1493 * o->real_ratio := o->volume / o->source->real_volume
1494 * (handled later by pa_source_set_volume)
1495 * o->soft_volume := o->real_ratio * o->volume_factor
1496 * (handled later by pa_source_set_volume) */
1497
1498 new_volume = o->source->reference_volume;
1499 pa_cvolume_remap(&new_volume, &o->source->channel_map, &o->channel_map);
1500 pa_sw_cvolume_multiply(&new_volume, &new_volume, &o->reference_ratio);
1501 pa_source_output_set_volume_direct(o, &new_volume);
1502
1503 } else {
1504 /* Ok, so this is a regular stream, and flat volume is disabled.
1505 * The volume will have to be updated as follows:
1506 *
1507 * o->volume := o->reference_ratio
1508 * o->reference_ratio stays unchanged
1509 * o->real_ratio := o->reference_ratio
1510 * o->soft_volume := o->real_ratio * o->volume_factor */
1511
1512 pa_source_output_set_volume_direct(o, &o->reference_ratio);
1513 o->real_ratio = o->reference_ratio;
1514 pa_sw_cvolume_multiply(&o->soft_volume, &o->real_ratio, &o->volume_factor);
1515 }
1516 }
1517
1518 /* If o->source == dest, then recursion has finished, and we can finally call
1519 * pa_source_set_volume(), which will do the rest of the updates. */
1520 if ((o->source == dest) && pa_source_flat_volume_enabled(o->source))
1521 pa_source_set_volume(o->source, NULL, false, o->save_volume);
1522 }
1523
1524 /* Called from main context */
pa_source_output_finish_move(pa_source_output * o,pa_source * dest,bool save)1525 int pa_source_output_finish_move(pa_source_output *o, pa_source *dest, bool save) {
1526 pa_source_output_assert_ref(o);
1527 pa_assert_ctl_context();
1528 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1529 pa_assert(!o->source);
1530 pa_source_assert_ref(dest);
1531
1532 if (!pa_source_output_may_move_to(o, dest))
1533 return -PA_ERR_NOTSUPPORTED;
1534
1535 if (pa_source_output_is_passthrough(o) && !pa_source_check_format(dest, o->format)) {
1536 pa_proplist *p = pa_proplist_new();
1537 pa_log_debug("New source doesn't support stream format, sending format-changed and killing");
1538 /* Tell the client what device we want to be on if it is going to
1539 * reconnect */
1540 pa_proplist_sets(p, "device", dest->name);
1541 pa_source_output_send_event(o, PA_STREAM_EVENT_FORMAT_LOST, p);
1542 pa_proplist_free(p);
1543 return -PA_ERR_NOTSUPPORTED;
1544 }
1545
1546 if (!(o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) &&
1547 !pa_sample_spec_equal(&o->sample_spec, &dest->sample_spec)) {
1548 /* try to change dest source format and rate if possible without glitches.
1549 module-suspend-on-idle resumes destination source with
1550 SOURCE_OUTPUT_MOVE_FINISH hook */
1551
1552 pa_log_info("Trying to change sample spec");
1553 pa_source_reconfigure(dest, &o->sample_spec, pa_source_output_is_passthrough(o));
1554 }
1555
1556 if (o->moving)
1557 o->moving(o, dest);
1558
1559 o->source = dest;
1560 /* save == true, means user is calling the move_to() and want to
1561 save the preferred_source */
1562 if (save) {
1563 pa_xfree(o->preferred_source);
1564 if (dest == dest->core->default_source)
1565 o->preferred_source = NULL;
1566 else
1567 o->preferred_source = pa_xstrdup(dest->name);
1568 }
1569
1570 pa_idxset_put(o->source->outputs, pa_source_output_ref(o), NULL);
1571
1572 pa_cvolume_remap(&o->volume_factor_source, &o->channel_map, &o->source->channel_map);
1573
1574 if (o->state == PA_SOURCE_OUTPUT_CORKED)
1575 o->source->n_corked++;
1576
1577 pa_source_output_update_resampler(o);
1578
1579 pa_source_update_status(dest);
1580
1581 update_volume_due_to_moving(o, dest);
1582
1583 if (pa_source_output_is_passthrough(o))
1584 pa_source_enter_passthrough(o->source);
1585
1586 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_ADD_OUTPUT, o, 0, NULL) == 0);
1587
1588 pa_log_debug("Successfully moved source output %i to %s.", o->index, dest->name);
1589
1590 /* Notify everyone */
1591 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH], o);
1592 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1593
1594 return 0;
1595 }
1596
1597 /* Called from main context */
pa_source_output_fail_move(pa_source_output * o)1598 void pa_source_output_fail_move(pa_source_output *o) {
1599
1600 pa_source_output_assert_ref(o);
1601 pa_assert_ctl_context();
1602 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1603 pa_assert(!o->source);
1604
1605 /* Check if someone wants this source output? */
1606 if (pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL], o) == PA_HOOK_STOP)
1607 return;
1608
1609 /* Can we move the source output to the default source? */
1610 if (o->core->rescue_streams && pa_source_output_may_move_to(o, o->core->default_source)) {
1611 if (pa_source_output_finish_move(o, o->core->default_source, false) >= 0)
1612 return;
1613 }
1614
1615 if (o->moving)
1616 o->moving(o, NULL);
1617
1618 pa_source_output_kill(o);
1619 }
1620
1621 /* Called from main context */
pa_source_output_move_to(pa_source_output * o,pa_source * dest,bool save)1622 int pa_source_output_move_to(pa_source_output *o, pa_source *dest, bool save) {
1623 int r;
1624
1625 pa_source_output_assert_ref(o);
1626 pa_assert_ctl_context();
1627 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1628 pa_assert(o->source);
1629 pa_source_assert_ref(dest);
1630
1631 if (dest == o->source)
1632 return 0;
1633
1634 if (!pa_source_output_may_move_to(o, dest))
1635 return -PA_ERR_NOTSUPPORTED;
1636
1637 pa_source_output_ref(o);
1638
1639 if ((r = pa_source_output_start_move(o)) < 0) {
1640 pa_source_output_unref(o);
1641 return r;
1642 }
1643
1644 if ((r = pa_source_output_finish_move(o, dest, save)) < 0) {
1645 pa_source_output_fail_move(o);
1646 pa_source_output_unref(o);
1647 return r;
1648 }
1649
1650 pa_source_output_unref(o);
1651
1652 return 0;
1653 }
1654
1655 /* Called from IO thread context except when cork() is called without a valid source. */
pa_source_output_set_state_within_thread(pa_source_output * o,pa_source_output_state_t state)1656 void pa_source_output_set_state_within_thread(pa_source_output *o, pa_source_output_state_t state) {
1657 pa_source_output_assert_ref(o);
1658
1659 if (state == o->thread_info.state)
1660 return;
1661
1662 if (o->state_change)
1663 o->state_change(o, state);
1664
1665 o->thread_info.state = state;
1666 }
1667
1668 /* Called from IO thread context, except when it is not */
pa_source_output_process_msg(pa_msgobject * mo,int code,void * userdata,int64_t offset,pa_memchunk * chunk)1669 int pa_source_output_process_msg(pa_msgobject *mo, int code, void *userdata, int64_t offset, pa_memchunk* chunk) {
1670 pa_source_output *o = PA_SOURCE_OUTPUT(mo);
1671 pa_source_output_assert_ref(o);
1672
1673 switch (code) {
1674
1675 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY: {
1676 pa_usec_t *r = userdata;
1677
1678 r[0] += pa_bytes_to_usec(pa_memblockq_get_length(o->thread_info.delay_memblockq), &o->source->sample_spec);
1679 r[1] += pa_source_get_latency_within_thread(o->source, false);
1680
1681 return 0;
1682 }
1683
1684 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE:
1685
1686 o->thread_info.sample_spec.rate = PA_PTR_TO_UINT(userdata);
1687 pa_resampler_set_output_rate(o->thread_info.resampler, PA_PTR_TO_UINT(userdata));
1688 return 0;
1689
1690 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE:
1691
1692 pa_source_output_set_state_within_thread(o, PA_PTR_TO_UINT(userdata));
1693
1694 return 0;
1695
1696 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY: {
1697 pa_usec_t *usec = userdata;
1698
1699 *usec = pa_source_output_set_requested_latency_within_thread(o, *usec);
1700
1701 return 0;
1702 }
1703
1704 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY: {
1705 pa_usec_t *r = userdata;
1706
1707 *r = o->thread_info.requested_source_latency;
1708 return 0;
1709 }
1710
1711 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_VOLUME:
1712 if (!pa_cvolume_equal(&o->thread_info.soft_volume, &o->soft_volume)) {
1713 o->thread_info.soft_volume = o->soft_volume;
1714 }
1715 return 0;
1716
1717 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE:
1718 if (o->thread_info.muted != o->muted) {
1719 o->thread_info.muted = o->muted;
1720 }
1721 return 0;
1722 }
1723
1724 return -PA_ERR_NOTIMPLEMENTED;
1725 }
1726
1727 /* Called from main context */
pa_source_output_send_event(pa_source_output * o,const char * event,pa_proplist * data)1728 void pa_source_output_send_event(pa_source_output *o, const char *event, pa_proplist *data) {
1729 pa_proplist *pl = NULL;
1730 pa_source_output_send_event_hook_data hook_data;
1731
1732 pa_source_output_assert_ref(o);
1733 pa_assert_ctl_context();
1734 pa_assert(event);
1735
1736 if (!o->send_event)
1737 return;
1738
1739 if (!data)
1740 data = pl = pa_proplist_new();
1741
1742 hook_data.source_output = o;
1743 hook_data.data = data;
1744 hook_data.event = event;
1745
1746 if (pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_SEND_EVENT], &hook_data) < 0)
1747 goto finish;
1748
1749 o->send_event(o, event, data);
1750
1751 finish:
1752 if (pl)
1753 pa_proplist_free(pl);
1754 }
1755
1756 /* Called from main context */
1757 /* Updates the source output's resampler with whatever the current source
1758 * requires -- useful when the underlying source's sample spec might have changed */
pa_source_output_update_resampler(pa_source_output * o)1759 int pa_source_output_update_resampler(pa_source_output *o) {
1760 pa_resampler *new_resampler;
1761 char *memblockq_name;
1762
1763 pa_source_output_assert_ref(o);
1764 pa_assert_ctl_context();
1765
1766 if (o->thread_info.resampler &&
1767 pa_sample_spec_equal(pa_resampler_input_sample_spec(o->thread_info.resampler), &o->source->sample_spec) &&
1768 pa_channel_map_equal(pa_resampler_input_channel_map(o->thread_info.resampler), &o->source->channel_map))
1769
1770 new_resampler = o->thread_info.resampler;
1771
1772 else if (!pa_source_output_is_passthrough(o) &&
1773 ((o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ||
1774 !pa_sample_spec_equal(&o->sample_spec, &o->source->sample_spec) ||
1775 !pa_channel_map_equal(&o->channel_map, &o->source->channel_map))) {
1776
1777 new_resampler = pa_resampler_new(o->core->mempool,
1778 &o->source->sample_spec, &o->source->channel_map,
1779 &o->sample_spec, &o->channel_map,
1780 o->core->lfe_crossover_freq,
1781 o->requested_resample_method,
1782 ((o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
1783 ((o->flags & PA_SOURCE_OUTPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
1784 (o->core->disable_remixing || (o->flags & PA_SOURCE_OUTPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0) |
1785 (o->core->remixing_use_all_sink_channels ? 0 : PA_RESAMPLER_NO_FILL_SINK) |
1786 (o->core->remixing_produce_lfe ? PA_RESAMPLER_PRODUCE_LFE : 0) |
1787 (o->core->remixing_consume_lfe ? PA_RESAMPLER_CONSUME_LFE : 0));
1788
1789 if (!new_resampler) {
1790 pa_log_warn("Unsupported resampling operation.");
1791 return -PA_ERR_NOTSUPPORTED;
1792 }
1793 } else
1794 new_resampler = NULL;
1795
1796 if (new_resampler == o->thread_info.resampler)
1797 return 0;
1798
1799 if (o->thread_info.resampler)
1800 pa_resampler_free(o->thread_info.resampler);
1801
1802 o->thread_info.resampler = new_resampler;
1803
1804 pa_memblockq_free(o->thread_info.delay_memblockq);
1805
1806 memblockq_name = pa_sprintf_malloc("source output delay_memblockq [%u]", o->index);
1807 o->thread_info.delay_memblockq = pa_memblockq_new(
1808 memblockq_name,
1809 0,
1810 MEMBLOCKQ_MAXLENGTH,
1811 0,
1812 &o->source->sample_spec,
1813 0,
1814 1,
1815 0,
1816 &o->source->silence);
1817 pa_xfree(memblockq_name);
1818
1819 o->actual_resample_method = new_resampler ? pa_resampler_get_method(new_resampler) : PA_RESAMPLER_INVALID;
1820
1821 pa_log_debug("Updated resampler for source output %d", o->index);
1822
1823 return 0;
1824 }
1825
1826 /* Called from the IO thread. */
pa_source_output_attach(pa_source_output * o)1827 void pa_source_output_attach(pa_source_output *o) {
1828 pa_assert(o);
1829 pa_assert(!o->thread_info.attached);
1830
1831 o->thread_info.attached = true;
1832
1833 if (o->attach)
1834 o->attach(o);
1835 }
1836
1837 /* Called from the IO thread. */
pa_source_output_detach(pa_source_output * o)1838 void pa_source_output_detach(pa_source_output *o) {
1839 pa_assert(o);
1840
1841 if (!o->thread_info.attached)
1842 return;
1843
1844 o->thread_info.attached = false;
1845
1846 if (o->detach)
1847 o->detach(o);
1848 }
1849
1850 /* Called from the main thread. */
pa_source_output_set_volume_direct(pa_source_output * o,const pa_cvolume * volume)1851 void pa_source_output_set_volume_direct(pa_source_output *o, const pa_cvolume *volume) {
1852 pa_cvolume old_volume;
1853 char old_volume_str[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
1854 char new_volume_str[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
1855
1856 pa_assert(o);
1857 pa_assert(volume);
1858
1859 old_volume = o->volume;
1860
1861 if (pa_cvolume_equal(volume, &old_volume))
1862 return;
1863
1864 o->volume = *volume;
1865 pa_log_debug("The volume of source output %u changed from %s to %s.", o->index,
1866 pa_cvolume_snprint_verbose(old_volume_str, sizeof(old_volume_str), &old_volume, &o->channel_map, true),
1867 pa_cvolume_snprint_verbose(new_volume_str, sizeof(new_volume_str), volume, &o->channel_map, true));
1868
1869 if (o->volume_changed)
1870 o->volume_changed(o);
1871
1872 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1873 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_VOLUME_CHANGED], o);
1874 }
1875
1876 /* Called from the main thread. */
pa_source_output_set_reference_ratio(pa_source_output * o,const pa_cvolume * ratio)1877 void pa_source_output_set_reference_ratio(pa_source_output *o, const pa_cvolume *ratio) {
1878 pa_cvolume old_ratio;
1879 char old_ratio_str[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
1880 char new_ratio_str[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
1881
1882 pa_assert(o);
1883 pa_assert(ratio);
1884
1885 old_ratio = o->reference_ratio;
1886
1887 if (pa_cvolume_equal(ratio, &old_ratio))
1888 return;
1889
1890 o->reference_ratio = *ratio;
1891
1892 if (!PA_SOURCE_OUTPUT_IS_LINKED(o->state))
1893 return;
1894
1895 pa_log_debug("Source output %u reference ratio changed from %s to %s.", o->index,
1896 pa_cvolume_snprint_verbose(old_ratio_str, sizeof(old_ratio_str), &old_ratio, &o->channel_map, true),
1897 pa_cvolume_snprint_verbose(new_ratio_str, sizeof(new_ratio_str), ratio, &o->channel_map, true));
1898 }
1899
1900 /* Called from the main thread. */
pa_source_output_set_preferred_source(pa_source_output * o,pa_source * s)1901 void pa_source_output_set_preferred_source(pa_source_output *o, pa_source *s) {
1902 pa_assert(o);
1903
1904 pa_xfree(o->preferred_source);
1905 if (s) {
1906 o->preferred_source = pa_xstrdup(s->name);
1907 pa_source_output_move_to(o, s, false);
1908 } else {
1909 o->preferred_source = NULL;
1910 pa_source_output_move_to(o, o->core->default_source, false);
1911 }
1912 }
1913