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