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