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