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