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