]> code.delx.au - pulseaudio/blob - src/pulsecore/source-output.c
sink-input, source-output: Remove redundant get_mute() functions
[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 bool old_mute;
1061
1062 pa_source_output_assert_ref(o);
1063 pa_assert_ctl_context();
1064 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1065
1066 old_mute = o->muted;
1067
1068 if (mute == old_mute) {
1069 o->save_muted |= save;
1070 return;
1071 }
1072
1073 o->muted = mute;
1074 pa_log_debug("The mute of source output %u changed from %s to %s.", o->index, pa_yes_no(old_mute), pa_yes_no(mute));
1075
1076 o->save_muted = save;
1077
1078 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE, NULL, 0, NULL) == 0);
1079
1080 /* The mute status changed, let's tell people so */
1081 if (o->mute_changed)
1082 o->mute_changed(o);
1083
1084 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1085 }
1086
1087 /* Called from main thread */
1088 void pa_source_output_update_proplist(pa_source_output *o, pa_update_mode_t mode, pa_proplist *p) {
1089 pa_source_output_assert_ref(o);
1090 pa_assert_ctl_context();
1091
1092 if (p)
1093 pa_proplist_update(o->proplist, mode, p);
1094
1095 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state)) {
1096 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED], o);
1097 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1098 }
1099 }
1100
1101 /* Called from main context */
1102 void pa_source_output_cork(pa_source_output *o, bool b) {
1103 pa_source_output_assert_ref(o);
1104 pa_assert_ctl_context();
1105 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1106
1107 source_output_set_state(o, b ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING);
1108 }
1109
1110 /* Called from main context */
1111 int pa_source_output_set_rate(pa_source_output *o, uint32_t rate) {
1112 pa_source_output_assert_ref(o);
1113 pa_assert_ctl_context();
1114 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1115 pa_return_val_if_fail(o->thread_info.resampler, -PA_ERR_BADSTATE);
1116
1117 if (o->sample_spec.rate == rate)
1118 return 0;
1119
1120 o->sample_spec.rate = rate;
1121
1122 pa_asyncmsgq_post(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE, PA_UINT_TO_PTR(rate), 0, NULL, NULL);
1123
1124 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1125 return 0;
1126 }
1127
1128 /* Called from main context */
1129 void pa_source_output_set_name(pa_source_output *o, const char *name) {
1130 const char *old;
1131 pa_assert_ctl_context();
1132 pa_source_output_assert_ref(o);
1133
1134 if (!name && !pa_proplist_contains(o->proplist, PA_PROP_MEDIA_NAME))
1135 return;
1136
1137 old = pa_proplist_gets(o->proplist, PA_PROP_MEDIA_NAME);
1138
1139 if (old && name && pa_streq(old, name))
1140 return;
1141
1142 if (name)
1143 pa_proplist_sets(o->proplist, PA_PROP_MEDIA_NAME, name);
1144 else
1145 pa_proplist_unset(o->proplist, PA_PROP_MEDIA_NAME);
1146
1147 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state)) {
1148 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED], o);
1149 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1150 }
1151 }
1152
1153 /* Called from main context */
1154 pa_resample_method_t pa_source_output_get_resample_method(pa_source_output *o) {
1155 pa_source_output_assert_ref(o);
1156 pa_assert_ctl_context();
1157
1158 return o->actual_resample_method;
1159 }
1160
1161 /* Called from main context */
1162 bool pa_source_output_may_move(pa_source_output *o) {
1163 pa_source_output_assert_ref(o);
1164 pa_assert_ctl_context();
1165 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1166
1167 if (o->flags & PA_SOURCE_OUTPUT_DONT_MOVE)
1168 return false;
1169
1170 if (o->direct_on_input)
1171 return false;
1172
1173 return true;
1174 }
1175
1176 static bool find_filter_source_output(pa_source_output *target, pa_source *s) {
1177 int i = 0;
1178 while (s && s->output_from_master) {
1179 if (s->output_from_master == target)
1180 return true;
1181 s = s->output_from_master->source;
1182 pa_assert(i++ < 100);
1183 }
1184 return false;
1185 }
1186
1187 /* Called from main context */
1188 bool pa_source_output_may_move_to(pa_source_output *o, pa_source *dest) {
1189 pa_source_output_assert_ref(o);
1190 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1191 pa_source_assert_ref(dest);
1192
1193 if (dest == o->source)
1194 return true;
1195
1196 if (!pa_source_output_may_move(o))
1197 return false;
1198
1199 /* Make sure we're not creating a filter source cycle */
1200 if (find_filter_source_output(o, dest)) {
1201 pa_log_debug("Can't connect output to %s, as that would create a cycle.", dest->name);
1202 return false;
1203 }
1204
1205 if (pa_idxset_size(dest->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
1206 pa_log_warn("Failed to move source output: too many outputs per source.");
1207 return false;
1208 }
1209
1210 if (o->may_move_to)
1211 if (!o->may_move_to(o, dest))
1212 return false;
1213
1214 return true;
1215 }
1216
1217 /* Called from main context */
1218 int pa_source_output_start_move(pa_source_output *o) {
1219 pa_source *origin;
1220 int r;
1221
1222 pa_source_output_assert_ref(o);
1223 pa_assert_ctl_context();
1224 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1225 pa_assert(o->source);
1226
1227 if (!pa_source_output_may_move(o))
1228 return -PA_ERR_NOTSUPPORTED;
1229
1230 if ((r = pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START], o)) < 0)
1231 return r;
1232
1233 origin = o->source;
1234
1235 pa_idxset_remove_by_data(o->source->outputs, o, NULL);
1236
1237 if (pa_source_output_get_state(o) == PA_SOURCE_OUTPUT_CORKED)
1238 pa_assert_se(origin->n_corked-- >= 1);
1239
1240 if (pa_source_output_is_passthrough(o))
1241 pa_source_leave_passthrough(o->source);
1242
1243 if (pa_source_flat_volume_enabled(o->source))
1244 /* We might need to update the source's volume if we are in flat
1245 * volume mode. */
1246 pa_source_set_volume(o->source, NULL, false, false);
1247
1248 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL) == 0);
1249
1250 pa_source_update_status(o->source);
1251 o->source = NULL;
1252
1253 pa_source_output_unref(o);
1254
1255 return 0;
1256 }
1257
1258 /* Called from main context. If it has an origin source that uses volume sharing,
1259 * then also the origin source and all streams connected to it need to update
1260 * their volume - this function does all that by using recursion. */
1261 static void update_volume_due_to_moving(pa_source_output *o, pa_source *dest) {
1262 pa_cvolume new_volume;
1263
1264 pa_assert(o);
1265 pa_assert(dest);
1266 pa_assert(o->source); /* The destination source should already be set. */
1267
1268 if (o->destination_source && (o->destination_source->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER)) {
1269 pa_source *root_source;
1270 pa_source_output *destination_source_output;
1271 uint32_t idx;
1272
1273 root_source = pa_source_get_master(o->source);
1274
1275 if (PA_UNLIKELY(!root_source))
1276 return;
1277
1278 if (pa_source_flat_volume_enabled(o->source)) {
1279 /* Ok, so the origin source uses volume sharing, and flat volume is
1280 * enabled. The volume will have to be updated as follows:
1281 *
1282 * o->volume := o->source->real_volume
1283 * (handled later by pa_source_set_volume)
1284 * o->reference_ratio := o->volume / o->source->reference_volume
1285 * (handled later by pa_source_set_volume)
1286 * o->real_ratio stays unchanged
1287 * (streams whose origin source uses volume sharing should
1288 * always have real_ratio of 0 dB)
1289 * o->soft_volume stays unchanged
1290 * (streams whose origin source uses volume sharing should
1291 * always have volume_factor as soft_volume, so no change
1292 * should be needed) */
1293
1294 pa_assert(pa_cvolume_is_norm(&o->real_ratio));
1295 pa_assert(pa_cvolume_equal(&o->soft_volume, &o->volume_factor));
1296
1297 /* Notifications will be sent by pa_source_set_volume(). */
1298
1299 } else {
1300 /* Ok, so the origin source uses volume sharing, and flat volume is
1301 * disabled. The volume will have to be updated as follows:
1302 *
1303 * o->volume := 0 dB
1304 * o->reference_ratio := 0 dB
1305 * o->real_ratio stays unchanged
1306 * (streams whose origin source uses volume sharing should
1307 * always have real_ratio of 0 dB)
1308 * o->soft_volume stays unchanged
1309 * (streams whose origin source uses volume sharing should
1310 * always have volume_factor as soft_volume, so no change
1311 * should be needed) */
1312
1313 pa_cvolume_reset(&new_volume, o->volume.channels);
1314 pa_source_output_set_volume_direct(o, &new_volume);
1315 pa_cvolume_reset(&o->reference_ratio, o->reference_ratio.channels);
1316 pa_assert(pa_cvolume_is_norm(&o->real_ratio));
1317 pa_assert(pa_cvolume_equal(&o->soft_volume, &o->volume_factor));
1318 }
1319
1320 /* Additionally, the origin source volume needs updating:
1321 *
1322 * o->destination_source->reference_volume := root_source->reference_volume
1323 * o->destination_source->real_volume := root_source->real_volume
1324 * o->destination_source->soft_volume stays unchanged
1325 * (sources that use volume sharing should always have
1326 * soft_volume of 0 dB) */
1327
1328 new_volume = root_source->reference_volume;
1329 pa_cvolume_remap(&new_volume, &root_source->channel_map, &o->destination_source->channel_map);
1330 pa_source_set_reference_volume_direct(o->destination_source, &new_volume);
1331
1332 o->destination_source->real_volume = root_source->real_volume;
1333 pa_cvolume_remap(&o->destination_source->real_volume, &root_source->channel_map, &o->destination_source->channel_map);
1334
1335 pa_assert(pa_cvolume_is_norm(&o->destination_source->soft_volume));
1336
1337 /* If you wonder whether o->destination_source->set_volume() should be
1338 * called somewhere, that's not the case, because sources that use
1339 * volume sharing shouldn't have any internal volume that set_volume()
1340 * would update. If you wonder whether the thread_info variables should
1341 * be synced, yes, they should, and it's done by the
1342 * PA_SOURCE_MESSAGE_FINISH_MOVE message handler. */
1343
1344 /* Recursively update origin source outputs. */
1345 PA_IDXSET_FOREACH(destination_source_output, o->destination_source->outputs, idx)
1346 update_volume_due_to_moving(destination_source_output, dest);
1347
1348 } else {
1349 if (pa_source_flat_volume_enabled(o->source)) {
1350 /* Ok, so this is a regular stream, and flat volume is enabled. The
1351 * volume will have to be updated as follows:
1352 *
1353 * o->volume := o->reference_ratio * o->source->reference_volume
1354 * o->reference_ratio stays unchanged
1355 * o->real_ratio := o->volume / o->source->real_volume
1356 * (handled later by pa_source_set_volume)
1357 * o->soft_volume := o->real_ratio * o->volume_factor
1358 * (handled later by pa_source_set_volume) */
1359
1360 new_volume = o->source->reference_volume;
1361 pa_cvolume_remap(&new_volume, &o->source->channel_map, &o->channel_map);
1362 pa_sw_cvolume_multiply(&new_volume, &new_volume, &o->reference_ratio);
1363 pa_source_output_set_volume_direct(o, &new_volume);
1364
1365 } else {
1366 /* Ok, so this is a regular stream, and flat volume is disabled.
1367 * The volume will have to be updated as follows:
1368 *
1369 * o->volume := o->reference_ratio
1370 * o->reference_ratio stays unchanged
1371 * o->real_ratio := o->reference_ratio
1372 * o->soft_volume := o->real_ratio * o->volume_factor */
1373
1374 pa_source_output_set_volume_direct(o, &o->reference_ratio);
1375 o->real_ratio = o->reference_ratio;
1376 pa_sw_cvolume_multiply(&o->soft_volume, &o->real_ratio, &o->volume_factor);
1377 }
1378 }
1379
1380 /* If o->source == dest, then recursion has finished, and we can finally call
1381 * pa_source_set_volume(), which will do the rest of the updates. */
1382 if ((o->source == dest) && pa_source_flat_volume_enabled(o->source))
1383 pa_source_set_volume(o->source, NULL, false, o->save_volume);
1384 }
1385
1386 /* Called from main context */
1387 int pa_source_output_finish_move(pa_source_output *o, pa_source *dest, bool save) {
1388 pa_source_output_assert_ref(o);
1389 pa_assert_ctl_context();
1390 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1391 pa_assert(!o->source);
1392 pa_source_assert_ref(dest);
1393
1394 if (!pa_source_output_may_move_to(o, dest))
1395 return -PA_ERR_NOTSUPPORTED;
1396
1397 if (pa_source_output_is_passthrough(o) && !pa_source_check_format(dest, o->format)) {
1398 pa_proplist *p = pa_proplist_new();
1399 pa_log_debug("New source doesn't support stream format, sending format-changed and killing");
1400 /* Tell the client what device we want to be on if it is going to
1401 * reconnect */
1402 pa_proplist_sets(p, "device", dest->name);
1403 pa_source_output_send_event(o, PA_STREAM_EVENT_FORMAT_LOST, p);
1404 pa_proplist_free(p);
1405 return -PA_ERR_NOTSUPPORTED;
1406 }
1407
1408 if (!(o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) &&
1409 !pa_sample_spec_equal(&o->sample_spec, &dest->sample_spec)) {
1410 /* try to change dest sink rate if possible without glitches.
1411 module-suspend-on-idle resumes destination source with
1412 SOURCE_OUTPUT_MOVE_FINISH hook */
1413
1414 pa_log_info("Trying to change sample rate");
1415 if (pa_source_update_rate(dest, o->sample_spec.rate, pa_source_output_is_passthrough(o)) >= 0)
1416 pa_log_info("Rate changed to %u Hz", dest->sample_spec.rate);
1417 }
1418
1419 if (o->moving)
1420 o->moving(o, dest);
1421
1422 o->source = dest;
1423 o->save_source = save;
1424 pa_idxset_put(o->source->outputs, pa_source_output_ref(o), NULL);
1425
1426 pa_cvolume_remap(&o->volume_factor_source, &o->channel_map, &o->source->channel_map);
1427
1428 if (pa_source_output_get_state(o) == PA_SOURCE_OUTPUT_CORKED)
1429 o->source->n_corked++;
1430
1431 pa_source_output_update_rate(o);
1432
1433 pa_source_update_status(dest);
1434
1435 update_volume_due_to_moving(o, dest);
1436
1437 if (pa_source_output_is_passthrough(o))
1438 pa_source_enter_passthrough(o->source);
1439
1440 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_ADD_OUTPUT, o, 0, NULL) == 0);
1441
1442 pa_log_debug("Successfully moved source output %i to %s.", o->index, dest->name);
1443
1444 /* Notify everyone */
1445 pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH], o);
1446 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1447
1448 return 0;
1449 }
1450
1451 /* Called from main context */
1452 void pa_source_output_fail_move(pa_source_output *o) {
1453
1454 pa_source_output_assert_ref(o);
1455 pa_assert_ctl_context();
1456 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1457 pa_assert(!o->source);
1458
1459 /* Check if someone wants this source output? */
1460 if (pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL], o) == PA_HOOK_STOP)
1461 return;
1462
1463 if (o->moving)
1464 o->moving(o, NULL);
1465
1466 pa_source_output_kill(o);
1467 }
1468
1469 /* Called from main context */
1470 int pa_source_output_move_to(pa_source_output *o, pa_source *dest, bool save) {
1471 int r;
1472
1473 pa_source_output_assert_ref(o);
1474 pa_assert_ctl_context();
1475 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
1476 pa_assert(o->source);
1477 pa_source_assert_ref(dest);
1478
1479 if (dest == o->source)
1480 return 0;
1481
1482 if (!pa_source_output_may_move_to(o, dest))
1483 return -PA_ERR_NOTSUPPORTED;
1484
1485 pa_source_output_ref(o);
1486
1487 if ((r = pa_source_output_start_move(o)) < 0) {
1488 pa_source_output_unref(o);
1489 return r;
1490 }
1491
1492 if ((r = pa_source_output_finish_move(o, dest, save)) < 0) {
1493 pa_source_output_fail_move(o);
1494 pa_source_output_unref(o);
1495 return r;
1496 }
1497
1498 pa_source_output_unref(o);
1499
1500 return 0;
1501 }
1502
1503 /* Called from IO thread context */
1504 void pa_source_output_set_state_within_thread(pa_source_output *o, pa_source_output_state_t state) {
1505 pa_source_output_assert_ref(o);
1506 pa_source_output_assert_io_context(o);
1507
1508 if (state == o->thread_info.state)
1509 return;
1510
1511 if (o->state_change)
1512 o->state_change(o, state);
1513
1514 o->thread_info.state = state;
1515 }
1516
1517 /* Called from IO thread context, except when it is not */
1518 int pa_source_output_process_msg(pa_msgobject *mo, int code, void *userdata, int64_t offset, pa_memchunk* chunk) {
1519 pa_source_output *o = PA_SOURCE_OUTPUT(mo);
1520 pa_source_output_assert_ref(o);
1521
1522 switch (code) {
1523
1524 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY: {
1525 pa_usec_t *r = userdata;
1526
1527 r[0] += pa_bytes_to_usec(pa_memblockq_get_length(o->thread_info.delay_memblockq), &o->source->sample_spec);
1528 r[1] += pa_source_get_latency_within_thread(o->source);
1529
1530 return 0;
1531 }
1532
1533 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE:
1534
1535 o->thread_info.sample_spec.rate = PA_PTR_TO_UINT(userdata);
1536 pa_resampler_set_output_rate(o->thread_info.resampler, PA_PTR_TO_UINT(userdata));
1537 return 0;
1538
1539 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE:
1540
1541 pa_source_output_set_state_within_thread(o, PA_PTR_TO_UINT(userdata));
1542
1543 return 0;
1544
1545 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY: {
1546 pa_usec_t *usec = userdata;
1547
1548 *usec = pa_source_output_set_requested_latency_within_thread(o, *usec);
1549
1550 return 0;
1551 }
1552
1553 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY: {
1554 pa_usec_t *r = userdata;
1555
1556 *r = o->thread_info.requested_source_latency;
1557 return 0;
1558 }
1559
1560 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_VOLUME:
1561 if (!pa_cvolume_equal(&o->thread_info.soft_volume, &o->soft_volume)) {
1562 o->thread_info.soft_volume = o->soft_volume;
1563 }
1564 return 0;
1565
1566 case PA_SOURCE_OUTPUT_MESSAGE_SET_SOFT_MUTE:
1567 if (o->thread_info.muted != o->muted) {
1568 o->thread_info.muted = o->muted;
1569 }
1570 return 0;
1571 }
1572
1573 return -PA_ERR_NOTIMPLEMENTED;
1574 }
1575
1576 /* Called from main context */
1577 void pa_source_output_send_event(pa_source_output *o, const char *event, pa_proplist *data) {
1578 pa_proplist *pl = NULL;
1579 pa_source_output_send_event_hook_data hook_data;
1580
1581 pa_source_output_assert_ref(o);
1582 pa_assert_ctl_context();
1583 pa_assert(event);
1584
1585 if (!o->send_event)
1586 return;
1587
1588 if (!data)
1589 data = pl = pa_proplist_new();
1590
1591 hook_data.source_output = o;
1592 hook_data.data = data;
1593 hook_data.event = event;
1594
1595 if (pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_SEND_EVENT], &hook_data) < 0)
1596 goto finish;
1597
1598 o->send_event(o, event, data);
1599
1600 finish:
1601 if (pl)
1602 pa_proplist_free(pl);
1603 }
1604
1605 /* Called from main context */
1606 /* Updates the source output's resampler with whatever the current source
1607 * requires -- useful when the underlying source's rate might have changed */
1608 int pa_source_output_update_rate(pa_source_output *o) {
1609 pa_resampler *new_resampler;
1610 char *memblockq_name;
1611
1612 pa_source_output_assert_ref(o);
1613 pa_assert_ctl_context();
1614
1615 if (o->thread_info.resampler &&
1616 pa_sample_spec_equal(pa_resampler_input_sample_spec(o->thread_info.resampler), &o->source->sample_spec) &&
1617 pa_channel_map_equal(pa_resampler_input_channel_map(o->thread_info.resampler), &o->source->channel_map))
1618
1619 new_resampler = o->thread_info.resampler;
1620
1621 else if (!pa_source_output_is_passthrough(o) &&
1622 ((o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ||
1623 !pa_sample_spec_equal(&o->sample_spec, &o->source->sample_spec) ||
1624 !pa_channel_map_equal(&o->channel_map, &o->source->channel_map))) {
1625
1626 new_resampler = pa_resampler_new(o->core->mempool,
1627 &o->source->sample_spec, &o->source->channel_map,
1628 &o->sample_spec, &o->channel_map,
1629 o->requested_resample_method,
1630 ((o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
1631 ((o->flags & PA_SOURCE_OUTPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
1632 (o->core->disable_remixing || (o->flags & PA_SOURCE_OUTPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0) |
1633 (o->core->disable_lfe_remixing ? PA_RESAMPLER_NO_LFE : 0));
1634
1635 if (!new_resampler) {
1636 pa_log_warn("Unsupported resampling operation.");
1637 return -PA_ERR_NOTSUPPORTED;
1638 }
1639 } else
1640 new_resampler = NULL;
1641
1642 if (new_resampler == o->thread_info.resampler)
1643 return 0;
1644
1645 if (o->thread_info.resampler)
1646 pa_resampler_free(o->thread_info.resampler);
1647
1648 o->thread_info.resampler = new_resampler;
1649
1650 pa_memblockq_free(o->thread_info.delay_memblockq);
1651
1652 memblockq_name = pa_sprintf_malloc("source output delay_memblockq [%u]", o->index);
1653 o->thread_info.delay_memblockq = pa_memblockq_new(
1654 memblockq_name,
1655 0,
1656 MEMBLOCKQ_MAXLENGTH,
1657 0,
1658 &o->source->sample_spec,
1659 0,
1660 1,
1661 0,
1662 &o->source->silence);
1663 pa_xfree(memblockq_name);
1664
1665 o->actual_resample_method = new_resampler ? pa_resampler_get_method(new_resampler) : PA_RESAMPLER_INVALID;
1666
1667 pa_log_debug("Updated resampler for source output %d", o->index);
1668
1669 return 0;
1670 }
1671
1672 /* Called from the main thread. */
1673 void pa_source_output_set_volume_direct(pa_source_output *o, const pa_cvolume *volume) {
1674 pa_cvolume old_volume;
1675 char old_volume_str[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
1676 char new_volume_str[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
1677
1678 pa_assert(o);
1679 pa_assert(volume);
1680
1681 old_volume = o->volume;
1682
1683 if (pa_cvolume_equal(volume, &old_volume))
1684 return;
1685
1686 o->volume = *volume;
1687 pa_log_debug("The volume of source output %u changed from %s to %s.", o->index,
1688 pa_cvolume_snprint_verbose(old_volume_str, sizeof(old_volume_str), &old_volume, &o->channel_map, true),
1689 pa_cvolume_snprint_verbose(new_volume_str, sizeof(new_volume_str), volume, &o->channel_map, true));
1690
1691 if (o->volume_changed)
1692 o->volume_changed(o);
1693
1694 pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
1695 }