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