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