]> code.delx.au - pulseaudio/blob - src/pulsecore/sink-input.c
add a simple fully-automatic fully-linearupmixer/downmixer and enable it by default
[pulseaudio] / src / pulsecore / sink-input.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <pulse/utf8.h>
34 #include <pulse/xmalloc.h>
35
36 #include <pulsecore/sample-util.h>
37 #include <pulsecore/core-subscribe.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/play-memblockq.h>
40 #include <pulsecore/namereg.h>
41
42 #include "sink-input.h"
43
44 #define CONVERT_BUFFER_LENGTH (PA_PAGE_SIZE)
45 #define SILENCE_BUFFER_LENGTH (PA_PAGE_SIZE*12)
46 #define MOVE_BUFFER_LENGTH (PA_PAGE_SIZE*256)
47
48 static PA_DEFINE_CHECK_TYPE(pa_sink_input, pa_msgobject);
49
50 static void sink_input_free(pa_object *o);
51
52 pa_sink_input_new_data* pa_sink_input_new_data_init(pa_sink_input_new_data *data) {
53 pa_assert(data);
54
55 memset(data, 0, sizeof(*data));
56 data->resample_method = PA_RESAMPLER_INVALID;
57
58 return data;
59 }
60
61 void pa_sink_input_new_data_set_channel_map(pa_sink_input_new_data *data, const pa_channel_map *map) {
62 pa_assert(data);
63
64 if ((data->channel_map_is_set = !!map))
65 data->channel_map = *map;
66 }
67
68 void pa_sink_input_new_data_set_volume(pa_sink_input_new_data *data, const pa_cvolume *volume) {
69 pa_assert(data);
70
71 if ((data->volume_is_set = !!volume))
72 data->volume = *volume;
73 }
74
75 void pa_sink_input_new_data_set_sample_spec(pa_sink_input_new_data *data, const pa_sample_spec *spec) {
76 pa_assert(data);
77
78 if ((data->sample_spec_is_set = !!spec))
79 data->sample_spec = *spec;
80 }
81
82 void pa_sink_input_new_data_set_muted(pa_sink_input_new_data *data, pa_bool_t mute) {
83 pa_assert(data);
84
85 data->muted_is_set = TRUE;
86 data->muted = !!mute;
87 }
88
89 pa_sink_input* pa_sink_input_new(
90 pa_core *core,
91 pa_sink_input_new_data *data,
92 pa_sink_input_flags_t flags) {
93
94 pa_sink_input *i;
95 pa_resampler *resampler = NULL;
96 char st[PA_SAMPLE_SPEC_SNPRINT_MAX];
97
98 pa_assert(core);
99 pa_assert(data);
100
101 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], data) < 0)
102 return NULL;
103
104 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
105 pa_return_null_if_fail(!data->name || pa_utf8_valid(data->name));
106
107 if (!data->sink)
108 data->sink = pa_namereg_get(core, NULL, PA_NAMEREG_SINK, 1);
109
110 pa_return_null_if_fail(data->sink);
111 pa_return_null_if_fail(pa_sink_get_state(data->sink) != PA_SINK_UNLINKED);
112 pa_return_null_if_fail(!data->sync_base || (data->sync_base->sink == data->sink && pa_sink_input_get_state(data->sync_base) == PA_SINK_INPUT_CORKED));
113
114 if (!data->sample_spec_is_set)
115 data->sample_spec = data->sink->sample_spec;
116
117 pa_return_null_if_fail(pa_sample_spec_valid(&data->sample_spec));
118
119 if (!data->channel_map_is_set) {
120 if (data->sink->channel_map.channels == data->sample_spec.channels)
121 data->channel_map = data->sink->channel_map;
122 else
123 pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
124 }
125
126 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
127 pa_return_null_if_fail(data->channel_map.channels == data->sample_spec.channels);
128
129 if (!data->volume_is_set)
130 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
131
132 pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
133 pa_return_null_if_fail(data->volume.channels == data->sample_spec.channels);
134
135 if (!data->muted_is_set)
136 data->muted = 0;
137
138 if (data->resample_method == PA_RESAMPLER_INVALID)
139 data->resample_method = core->resample_method;
140
141 pa_return_null_if_fail(data->resample_method < PA_RESAMPLER_MAX);
142
143 if (pa_idxset_size(data->sink->inputs) >= PA_MAX_INPUTS_PER_SINK) {
144 pa_log_warn("Failed to create sink input: too many inputs per sink.");
145 return NULL;
146 }
147
148 if ((flags & PA_SINK_INPUT_VARIABLE_RATE) ||
149 !pa_sample_spec_equal(&data->sample_spec, &data->sink->sample_spec) ||
150 !pa_channel_map_equal(&data->channel_map, &data->sink->channel_map)) {
151
152 if (!(resampler = pa_resampler_new(
153 core->mempool,
154 &data->sample_spec, &data->channel_map,
155 &data->sink->sample_spec, &data->sink->channel_map,
156 data->resample_method,
157 (flags & PA_SINK_INPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0))) {
158 pa_log_warn("Unsupported resampling operation.");
159 return NULL;
160 }
161
162 data->resample_method = pa_resampler_get_method(resampler);
163 }
164
165 i = pa_msgobject_new(pa_sink_input);
166 i->parent.parent.free = sink_input_free;
167 i->parent.process_msg = pa_sink_input_process_msg;
168
169 i->core = core;
170 i->state = PA_SINK_INPUT_INIT;
171 i->flags = flags;
172 i->name = pa_xstrdup(data->name);
173 i->driver = pa_xstrdup(data->driver);
174 i->module = data->module;
175 i->sink = data->sink;
176 i->client = data->client;
177
178 i->resample_method = data->resample_method;
179 i->sample_spec = data->sample_spec;
180 i->channel_map = data->channel_map;
181
182 i->volume = data->volume;
183 i->muted = data->muted;
184
185 if (data->sync_base) {
186 i->sync_next = data->sync_base->sync_next;
187 i->sync_prev = data->sync_base;
188
189 if (data->sync_base->sync_next)
190 data->sync_base->sync_next->sync_prev = i;
191 data->sync_base->sync_next = i;
192 } else
193 i->sync_next = i->sync_prev = NULL;
194
195 i->peek = NULL;
196 i->drop = NULL;
197 i->kill = NULL;
198 i->get_latency = NULL;
199 i->attach = NULL;
200 i->detach = NULL;
201 i->suspend = NULL;
202 i->userdata = NULL;
203
204 i->thread_info.state = i->state;
205 pa_atomic_store(&i->thread_info.drained, 1);
206 i->thread_info.sample_spec = i->sample_spec;
207 i->thread_info.silence_memblock = NULL;
208 i->thread_info.move_silence = 0;
209 pa_memchunk_reset(&i->thread_info.resampled_chunk);
210 i->thread_info.resampler = resampler;
211 i->thread_info.volume = i->volume;
212 i->thread_info.muted = i->muted;
213 i->thread_info.attached = FALSE;
214
215 pa_assert_se(pa_idxset_put(core->sink_inputs, pa_sink_input_ref(i), &i->index) == 0);
216 pa_assert_se(pa_idxset_put(i->sink->inputs, i, NULL) == 0);
217
218 pa_log_info("Created input %u \"%s\" on %s with sample spec %s",
219 i->index,
220 i->name,
221 i->sink->name,
222 pa_sample_spec_snprint(st, sizeof(st), &i->sample_spec));
223
224 /* Don't forget to call pa_sink_input_put! */
225
226 return i;
227 }
228
229 static void update_n_corked(pa_sink_input *i, pa_sink_input_state_t state) {
230 pa_assert(i);
231
232 if (i->state == PA_SINK_INPUT_CORKED && state != PA_SINK_INPUT_CORKED)
233 pa_assert_se(i->sink->n_corked -- >= 1);
234 else if (i->state != PA_SINK_INPUT_CORKED && state == PA_SINK_INPUT_CORKED)
235 i->sink->n_corked++;
236
237 pa_sink_update_status(i->sink);
238 }
239
240 static int sink_input_set_state(pa_sink_input *i, pa_sink_input_state_t state) {
241 pa_sink_input *ssync;
242 pa_assert(i);
243
244 if (state == PA_SINK_INPUT_DRAINED)
245 state = PA_SINK_INPUT_RUNNING;
246
247 if (i->state == state)
248 return 0;
249
250 if (pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) < 0)
251 return -1;
252
253 update_n_corked(i, state);
254 i->state = state;
255
256 for (ssync = i->sync_prev; ssync; ssync = ssync->sync_prev) {
257 update_n_corked(ssync, state);
258 ssync->state = state;
259 }
260 for (ssync = i->sync_next; ssync; ssync = ssync->sync_next) {
261 update_n_corked(ssync, state);
262 ssync->state = state;
263 }
264
265 if (state != PA_SINK_INPUT_UNLINKED)
266 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], i);
267
268 return 0;
269 }
270
271 void pa_sink_input_unlink(pa_sink_input *i) {
272 pa_bool_t linked;
273 pa_assert(i);
274
275 /* See pa_sink_unlink() for a couple of comments how this function
276 * works */
277
278 pa_sink_input_ref(i);
279
280 linked = PA_SINK_INPUT_LINKED(i->state);
281
282 if (linked)
283 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK], i);
284
285 if (i->sync_prev)
286 i->sync_prev->sync_next = i->sync_next;
287 if (i->sync_next)
288 i->sync_next->sync_prev = i->sync_prev;
289
290 i->sync_prev = i->sync_next = NULL;
291
292 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
293 if (pa_idxset_remove_by_data(i->sink->inputs, i, NULL))
294 pa_sink_input_unref(i);
295
296 if (linked) {
297 pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_REMOVE_INPUT, i, 0, NULL);
298 sink_input_set_state(i, PA_SINK_INPUT_UNLINKED);
299 pa_sink_update_status(i->sink);
300 } else
301 i->state = PA_SINK_INPUT_UNLINKED;
302
303 i->peek = NULL;
304 i->drop = NULL;
305 i->kill = NULL;
306 i->get_latency = NULL;
307 i->attach = NULL;
308 i->detach = NULL;
309 i->suspend = NULL;
310
311 if (linked) {
312 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
313 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK_POST], i);
314 }
315
316 i->sink = NULL;
317 pa_sink_input_unref(i);
318 }
319
320 static void sink_input_free(pa_object *o) {
321 pa_sink_input* i = PA_SINK_INPUT(o);
322
323 pa_assert(i);
324 pa_assert(pa_sink_input_refcnt(i) == 0);
325
326 if (PA_SINK_INPUT_LINKED(i->state))
327 pa_sink_input_unlink(i);
328
329 pa_log_info("Freeing output %u \"%s\"", i->index, i->name);
330
331 pa_assert(!i->thread_info.attached);
332
333 if (i->thread_info.resampled_chunk.memblock)
334 pa_memblock_unref(i->thread_info.resampled_chunk.memblock);
335
336 if (i->thread_info.resampler)
337 pa_resampler_free(i->thread_info.resampler);
338
339 if (i->thread_info.silence_memblock)
340 pa_memblock_unref(i->thread_info.silence_memblock);
341
342 pa_xfree(i->name);
343 pa_xfree(i->driver);
344 pa_xfree(i);
345 }
346
347 void pa_sink_input_put(pa_sink_input *i) {
348 pa_sink_input_assert_ref(i);
349
350 pa_assert(i->state == PA_SINK_INPUT_INIT);
351 pa_assert(i->peek);
352 pa_assert(i->drop);
353
354 i->thread_info.state = i->state = i->flags & PA_SINK_INPUT_START_CORKED ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING;
355 i->thread_info.volume = i->volume;
356 i->thread_info.muted = i->muted;
357
358 if (i->state == PA_SINK_INPUT_CORKED)
359 i->sink->n_corked++;
360
361 pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_ADD_INPUT, i, 0, NULL);
362 pa_sink_update_status(i->sink);
363
364 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
365 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], i);
366
367 /* Please note that if you change something here, you have to
368 change something in pa_sink_input_move() with the ghost stream
369 registration too. */
370 }
371
372 void pa_sink_input_kill(pa_sink_input*i) {
373 pa_sink_input_assert_ref(i);
374 pa_assert(PA_SINK_INPUT_LINKED(i->state));
375
376 if (i->kill)
377 i->kill(i);
378 }
379
380 pa_usec_t pa_sink_input_get_latency(pa_sink_input *i) {
381 pa_usec_t r = 0;
382
383 pa_sink_input_assert_ref(i);
384 pa_assert(PA_SINK_INPUT_LINKED(i->state));
385
386 if (pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_LATENCY, &r, 0, NULL) < 0)
387 r = 0;
388
389 if (i->get_latency)
390 r += i->get_latency(i);
391
392 return r;
393 }
394
395 /* Called from thread context */
396 int pa_sink_input_peek(pa_sink_input *i, size_t length, pa_memchunk *chunk, pa_cvolume *volume) {
397 int ret = -1;
398 int do_volume_adj_here;
399 int volume_is_norm;
400 size_t block_size_max;
401
402 pa_sink_input_assert_ref(i);
403 pa_assert(PA_SINK_INPUT_LINKED(i->thread_info.state));
404 pa_assert(pa_frame_aligned(length, &i->sink->sample_spec));
405 pa_assert(chunk);
406 pa_assert(volume);
407
408 if (!i->peek || !i->drop || i->thread_info.state == PA_SINK_INPUT_CORKED)
409 goto finish;
410
411 pa_assert(i->thread_info.state == PA_SINK_INPUT_RUNNING || i->thread_info.state == PA_SINK_INPUT_DRAINED);
412
413 /* Default buffer size */
414 if (length <= 0)
415 length = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sink->sample_spec);
416
417 /* Make sure the buffer fits in the mempool tile */
418 block_size_max = pa_mempool_block_size_max(i->sink->core->mempool);
419 if (length > block_size_max)
420 length = pa_frame_align(block_size_max, &i->sink->sample_spec);
421
422 if (i->thread_info.move_silence > 0) {
423 size_t l;
424
425 /* We have just been moved and shall play some silence for a
426 * while until the old sink has drained its playback buffer */
427
428 if (!i->thread_info.silence_memblock)
429 i->thread_info.silence_memblock = pa_silence_memblock_new(
430 i->sink->core->mempool,
431 &i->sink->sample_spec,
432 pa_frame_align(SILENCE_BUFFER_LENGTH, &i->sink->sample_spec));
433
434 chunk->memblock = pa_memblock_ref(i->thread_info.silence_memblock);
435 chunk->index = 0;
436 l = pa_memblock_get_length(chunk->memblock);
437 chunk->length = i->thread_info.move_silence < l ? i->thread_info.move_silence : l;
438
439 ret = 0;
440 do_volume_adj_here = 1;
441 goto finish;
442 }
443
444 if (!i->thread_info.resampler) {
445 do_volume_adj_here = 0; /* FIXME??? */
446 ret = i->peek(i, length, chunk);
447 goto finish;
448 }
449
450 do_volume_adj_here = !pa_channel_map_equal(&i->channel_map, &i->sink->channel_map);
451 volume_is_norm = pa_cvolume_is_norm(&i->thread_info.volume) && !i->thread_info.muted;
452
453 while (!i->thread_info.resampled_chunk.memblock) {
454 pa_memchunk tchunk;
455 size_t l, rmbs;
456
457 l = pa_resampler_request(i->thread_info.resampler, length);
458
459 if (l <= 0)
460 l = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sample_spec);
461
462 rmbs = pa_resampler_max_block_size(i->thread_info.resampler);
463 if (l > rmbs)
464 l = rmbs;
465
466 if ((ret = i->peek(i, l, &tchunk)) < 0)
467 goto finish;
468
469 pa_assert(tchunk.length > 0);
470
471 if (tchunk.length > l)
472 tchunk.length = l;
473
474 i->drop(i, tchunk.length);
475
476 /* It might be necessary to adjust the volume here */
477 if (do_volume_adj_here && !volume_is_norm) {
478 pa_memchunk_make_writable(&tchunk, 0);
479
480 if (i->thread_info.muted)
481 pa_silence_memchunk(&tchunk, &i->thread_info.sample_spec);
482 else
483 pa_volume_memchunk(&tchunk, &i->thread_info.sample_spec, &i->thread_info.volume);
484 }
485
486 pa_resampler_run(i->thread_info.resampler, &tchunk, &i->thread_info.resampled_chunk);
487 pa_memblock_unref(tchunk.memblock);
488 }
489
490 pa_assert(i->thread_info.resampled_chunk.memblock);
491 pa_assert(i->thread_info.resampled_chunk.length > 0);
492
493 *chunk = i->thread_info.resampled_chunk;
494 pa_memblock_ref(i->thread_info.resampled_chunk.memblock);
495
496 ret = 0;
497
498 finish:
499
500 if (ret >= 0)
501 pa_atomic_store(&i->thread_info.drained, 0);
502 else if (ret < 0)
503 pa_atomic_store(&i->thread_info.drained, 1);
504
505 if (ret >= 0) {
506 /* Let's see if we had to apply the volume adjustment
507 * ourselves, or if this can be done by the sink for us */
508
509 if (do_volume_adj_here)
510 /* We had different channel maps, so we already did the adjustment */
511 pa_cvolume_reset(volume, i->sink->sample_spec.channels);
512 else if (i->thread_info.muted)
513 /* We've both the same channel map, so let's have the sink do the adjustment for us*/
514 pa_cvolume_mute(volume, i->sink->sample_spec.channels);
515 else
516 *volume = i->thread_info.volume;
517 }
518
519 return ret;
520 }
521
522 /* Called from thread context */
523 void pa_sink_input_drop(pa_sink_input *i, size_t length) {
524 pa_sink_input_assert_ref(i);
525 pa_assert(PA_SINK_INPUT_LINKED(i->thread_info.state));
526 pa_assert(pa_frame_aligned(length, &i->sink->sample_spec));
527 pa_assert(length > 0);
528
529 if (!i->peek || !i->drop || i->thread_info.state == PA_SINK_INPUT_CORKED)
530 return;
531
532 if (i->thread_info.move_silence > 0) {
533
534 if (i->thread_info.move_silence >= length) {
535 i->thread_info.move_silence -= length;
536 length = 0;
537 } else {
538 length -= i->thread_info.move_silence;
539 i->thread_info.move_silence = 0;
540 }
541
542 if (i->thread_info.move_silence <= 0) {
543 if (i->thread_info.silence_memblock) {
544 pa_memblock_unref(i->thread_info.silence_memblock);
545 i->thread_info.silence_memblock = NULL;
546 }
547 }
548
549 if (length <= 0)
550 return;
551 }
552
553 if (i->thread_info.resampled_chunk.memblock) {
554 size_t l = length;
555
556 if (l > i->thread_info.resampled_chunk.length)
557 l = i->thread_info.resampled_chunk.length;
558
559 i->thread_info.resampled_chunk.index += l;
560 i->thread_info.resampled_chunk.length -= l;
561
562 if (i->thread_info.resampled_chunk.length <= 0) {
563 pa_memblock_unref(i->thread_info.resampled_chunk.memblock);
564 pa_memchunk_reset(&i->thread_info.resampled_chunk);
565 }
566
567 length -= l;
568 }
569
570 if (length > 0) {
571
572 if (i->thread_info.resampler) {
573 /* So, we have a resampler. To avoid discontinuities we
574 * have to actually read all data that could be read and
575 * pass it through the resampler. */
576
577 while (length > 0) {
578 pa_memchunk chunk;
579 pa_cvolume volume;
580
581 if (pa_sink_input_peek(i, length, &chunk, &volume) >= 0) {
582 size_t l;
583
584 pa_memblock_unref(chunk.memblock);
585
586 l = chunk.length;
587 if (l > length)
588 l = length;
589
590 pa_sink_input_drop(i, l);
591 length -= l;
592
593 } else {
594 size_t l;
595
596 l = pa_resampler_request(i->thread_info.resampler, length);
597
598 /* Hmmm, peeking failed, so let's at least drop
599 * the right amount of data */
600
601 if (l > 0)
602 if (i->drop)
603 i->drop(i, l);
604
605 break;
606 }
607 }
608
609 } else {
610
611 /* We have no resampler, hence let's just drop the data */
612
613 if (i->drop)
614 i->drop(i, length);
615 }
616 }
617 }
618
619 void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume) {
620 pa_sink_input_assert_ref(i);
621 pa_assert(PA_SINK_INPUT_LINKED(i->state));
622
623 if (pa_cvolume_equal(&i->volume, volume))
624 return;
625
626 i->volume = *volume;
627
628 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), 0, NULL, pa_xfree);
629 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
630 }
631
632 const pa_cvolume *pa_sink_input_get_volume(pa_sink_input *i) {
633 pa_sink_input_assert_ref(i);
634 pa_assert(PA_SINK_INPUT_LINKED(i->state));
635
636 return &i->volume;
637 }
638
639 void pa_sink_input_set_mute(pa_sink_input *i, pa_bool_t mute) {
640 pa_assert(i);
641 pa_sink_input_assert_ref(i);
642 pa_assert(PA_SINK_INPUT_LINKED(i->state));
643
644 if (!i->muted == !mute)
645 return;
646
647 i->muted = mute;
648
649 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
650 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
651 }
652
653 int pa_sink_input_get_mute(pa_sink_input *i) {
654 pa_sink_input_assert_ref(i);
655 pa_assert(PA_SINK_INPUT_LINKED(i->state));
656
657 return !!i->muted;
658 }
659
660 void pa_sink_input_cork(pa_sink_input *i, pa_bool_t b) {
661 pa_sink_input_assert_ref(i);
662 pa_assert(PA_SINK_INPUT_LINKED(i->state));
663
664 sink_input_set_state(i, b ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING);
665 }
666
667 int pa_sink_input_set_rate(pa_sink_input *i, uint32_t rate) {
668 pa_sink_input_assert_ref(i);
669 pa_assert(PA_SINK_INPUT_LINKED(i->state));
670 pa_return_val_if_fail(i->thread_info.resampler, -1);
671
672 if (i->sample_spec.rate == rate)
673 return 0;
674
675 i->sample_spec.rate = rate;
676
677 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_RATE, PA_UINT_TO_PTR(rate), 0, NULL, NULL);
678
679 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
680 return 0;
681 }
682
683 void pa_sink_input_set_name(pa_sink_input *i, const char *name) {
684 pa_sink_input_assert_ref(i);
685
686 if (!i->name && !name)
687 return;
688
689 if (i->name && name && !strcmp(i->name, name))
690 return;
691
692 pa_xfree(i->name);
693 i->name = pa_xstrdup(name);
694
695 if (PA_SINK_INPUT_LINKED(i->state)) {
696 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_NAME_CHANGED], i);
697 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
698 }
699 }
700
701 pa_resample_method_t pa_sink_input_get_resample_method(pa_sink_input *i) {
702 pa_sink_input_assert_ref(i);
703
704 return i->resample_method;
705 }
706
707 int pa_sink_input_move_to(pa_sink_input *i, pa_sink *dest, int immediately) {
708 pa_resampler *new_resampler;
709 pa_sink *origin;
710 pa_usec_t silence_usec = 0;
711 pa_sink_input_move_info info;
712
713 pa_sink_input_assert_ref(i);
714 pa_assert(PA_SINK_INPUT_LINKED(i->state));
715 pa_sink_assert_ref(dest);
716
717 origin = i->sink;
718
719 if (dest == origin)
720 return 0;
721
722 if (i->flags & PA_SINK_INPUT_DONT_MOVE)
723 return -1;
724
725 if (i->sync_next || i->sync_prev) {
726 pa_log_warn("Moving synchronised streams not supported.");
727 return -1;
728 }
729
730 if (pa_idxset_size(dest->inputs) >= PA_MAX_INPUTS_PER_SINK) {
731 pa_log_warn("Failed to move sink input: too many inputs per sink.");
732 return -1;
733 }
734
735 if (i->thread_info.resampler &&
736 pa_sample_spec_equal(&origin->sample_spec, &dest->sample_spec) &&
737 pa_channel_map_equal(&origin->channel_map, &dest->channel_map))
738
739 /* Try to reuse the old resampler if possible */
740 new_resampler = i->thread_info.resampler;
741
742 else if ((i->flags & PA_SINK_INPUT_VARIABLE_RATE) ||
743 !pa_sample_spec_equal(&i->sample_spec, &dest->sample_spec) ||
744 !pa_channel_map_equal(&i->channel_map, &dest->channel_map)) {
745
746 /* Okey, we need a new resampler for the new sink */
747
748 if (!(new_resampler = pa_resampler_new(
749 dest->core->mempool,
750 &i->sample_spec, &i->channel_map,
751 &dest->sample_spec, &dest->channel_map,
752 i->resample_method,
753 (i->flags & PA_SINK_INPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0))) {
754 pa_log_warn("Unsupported resampling operation.");
755 return -1;
756 }
757 } else
758 new_resampler = NULL;
759
760 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE], i);
761
762 memset(&info, 0, sizeof(info));
763 info.sink_input = i;
764
765 if (!immediately) {
766 pa_usec_t old_latency, new_latency;
767
768 /* Let's do a little bit of Voodoo for compensating latency
769 * differences. We assume that the accuracy for our
770 * estimations is still good enough, even though we do these
771 * operations non-atomic. */
772
773 old_latency = pa_sink_get_latency(origin);
774 new_latency = pa_sink_get_latency(dest);
775
776 /* The already resampled data should go to the old sink */
777
778 if (old_latency >= new_latency) {
779
780 /* The latency of the old sink is larger than the latency
781 * of the new sink. Therefore to compensate for the
782 * difference we to play silence on the new one for a
783 * while */
784
785 silence_usec = old_latency - new_latency;
786
787 } else {
788
789 /* The latency of new sink is larger than the latency of
790 * the old sink. Therefore we have to precompute a little
791 * and make sure that this is still played on the old
792 * sink, until we can play the first sample on the new
793 * sink.*/
794
795 info.buffer_bytes = pa_usec_to_bytes(new_latency - old_latency, &origin->sample_spec);
796 }
797
798 /* Okey, let's move it */
799
800 if (info.buffer_bytes > 0) {
801
802 info.ghost_sink_input = pa_memblockq_sink_input_new(
803 origin,
804 "Ghost Stream",
805 &origin->sample_spec,
806 &origin->channel_map,
807 NULL,
808 NULL);
809
810 info.ghost_sink_input->thread_info.state = info.ghost_sink_input->state = PA_SINK_INPUT_RUNNING;
811 info.ghost_sink_input->thread_info.volume = info.ghost_sink_input->volume;
812 info.ghost_sink_input->thread_info.muted = info.ghost_sink_input->muted;
813
814 info.buffer = pa_memblockq_new(0, MOVE_BUFFER_LENGTH, 0, pa_frame_size(&origin->sample_spec), 0, 0, NULL);
815 }
816 }
817
818 pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, &info, 0, NULL);
819
820 if (info.ghost_sink_input) {
821 /* Basically, do what pa_sink_input_put() does ...*/
822
823 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, info.ghost_sink_input->index);
824 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], info.ghost_sink_input);
825 pa_sink_input_unref(info.ghost_sink_input);
826 }
827
828 pa_idxset_remove_by_data(origin->inputs, i, NULL);
829 pa_idxset_put(dest->inputs, i, NULL);
830 i->sink = dest;
831
832 if (pa_sink_input_get_state(i) == PA_SINK_INPUT_CORKED) {
833 pa_assert_se(origin->n_corked-- >= 1);
834 dest->n_corked++;
835 }
836
837 /* Replace resampler */
838 if (new_resampler != i->thread_info.resampler) {
839 if (i->thread_info.resampler)
840 pa_resampler_free(i->thread_info.resampler);
841 i->thread_info.resampler = new_resampler;
842
843 /* if the resampler changed, the silence memblock is
844 * probably invalid now, too */
845 if (i->thread_info.silence_memblock) {
846 pa_memblock_unref(i->thread_info.silence_memblock);
847 i->thread_info.silence_memblock = NULL;
848 }
849 }
850
851 /* Dump already resampled data */
852 if (i->thread_info.resampled_chunk.memblock) {
853 /* Hmm, this data has already been added to the ghost queue, presumably, hence let's sleep a little bit longer */
854 silence_usec += pa_bytes_to_usec(i->thread_info.resampled_chunk.length, &origin->sample_spec);
855 pa_memblock_unref(i->thread_info.resampled_chunk.memblock);
856 pa_memchunk_reset(&i->thread_info.resampled_chunk);
857 }
858
859 /* Calculate the new sleeping time */
860 if (immediately)
861 i->thread_info.move_silence = 0;
862 else
863 i->thread_info.move_silence = pa_usec_to_bytes(
864 pa_bytes_to_usec(i->thread_info.move_silence, &origin->sample_spec) +
865 silence_usec,
866 &dest->sample_spec);
867
868 pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_ADD_INPUT, i, 0, NULL);
869
870 pa_sink_update_status(origin);
871 pa_sink_update_status(dest);
872
873 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_POST], i);
874
875 pa_log_debug("Successfully moved sink input %i from %s to %s.", i->index, origin->name, dest->name);
876
877 /* Notify everyone */
878 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
879
880 return 0;
881 }
882
883 /* Called from thread context */
884 int pa_sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
885 pa_sink_input *i = PA_SINK_INPUT(o);
886
887 pa_sink_input_assert_ref(i);
888 pa_assert(PA_SINK_INPUT_LINKED(i->thread_info.state));
889
890 switch (code) {
891 case PA_SINK_INPUT_MESSAGE_SET_VOLUME:
892 i->thread_info.volume = *((pa_cvolume*) userdata);
893 return 0;
894
895 case PA_SINK_INPUT_MESSAGE_SET_MUTE:
896 i->thread_info.muted = PA_PTR_TO_UINT(userdata);
897 return 0;
898
899 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
900 pa_usec_t *r = userdata;
901
902 if (i->thread_info.resampled_chunk.memblock)
903 *r += pa_bytes_to_usec(i->thread_info.resampled_chunk.length, &i->sink->sample_spec);
904
905 if (i->thread_info.move_silence)
906 *r += pa_bytes_to_usec(i->thread_info.move_silence, &i->sink->sample_spec);
907
908 return 0;
909 }
910
911 case PA_SINK_INPUT_MESSAGE_SET_RATE:
912
913 i->thread_info.sample_spec.rate = PA_PTR_TO_UINT(userdata);
914 pa_resampler_set_input_rate(i->thread_info.resampler, PA_PTR_TO_UINT(userdata));
915
916 return 0;
917
918 case PA_SINK_INPUT_MESSAGE_SET_STATE: {
919 pa_sink_input *ssync;
920
921 if ((PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_DRAINED || PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_RUNNING) &&
922 (i->thread_info.state != PA_SINK_INPUT_DRAINED) && (i->thread_info.state != PA_SINK_INPUT_RUNNING))
923 pa_atomic_store(&i->thread_info.drained, 1);
924
925 i->thread_info.state = PA_PTR_TO_UINT(userdata);
926
927 for (ssync = i->thread_info.sync_prev; ssync; ssync = ssync->thread_info.sync_prev) {
928 if ((PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_DRAINED || PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_RUNNING) &&
929 (ssync->thread_info.state != PA_SINK_INPUT_DRAINED) && (ssync->thread_info.state != PA_SINK_INPUT_RUNNING))
930 pa_atomic_store(&ssync->thread_info.drained, 1);
931 ssync->thread_info.state = PA_PTR_TO_UINT(userdata);
932 }
933
934 for (ssync = i->thread_info.sync_next; ssync; ssync = ssync->thread_info.sync_next) {
935 if ((PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_DRAINED || PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_RUNNING) &&
936 (ssync->thread_info.state != PA_SINK_INPUT_DRAINED) && (ssync->thread_info.state != PA_SINK_INPUT_RUNNING))
937 pa_atomic_store(&ssync->thread_info.drained, 1);
938 ssync->thread_info.state = PA_PTR_TO_UINT(userdata);
939 }
940
941 return 0;
942 }
943 }
944
945 return -1;
946 }
947
948 pa_sink_input_state_t pa_sink_input_get_state(pa_sink_input *i) {
949 pa_sink_input_assert_ref(i);
950
951 if (i->state == PA_SINK_INPUT_RUNNING || i->state == PA_SINK_INPUT_DRAINED)
952 return pa_atomic_load(&i->thread_info.drained) ? PA_SINK_INPUT_DRAINED : PA_SINK_INPUT_RUNNING;
953
954 return i->state;
955 }