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