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