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