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