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