]> code.delx.au - pulseaudio/blob - src/pulsecore/sink.c
Resurrect ability to move streams between sinks
[pulseaudio] / src / pulsecore / sink.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 <stdlib.h>
30 #include <assert.h>
31 #include <string.h>
32 #include <stdio.h>
33
34 #include <pulse/introspect.h>
35 #include <pulse/utf8.h>
36 #include <pulse/xmalloc.h>
37
38 #include <pulsecore/sink-input.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/sample-util.h>
42 #include <pulsecore/core-subscribe.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/macro.h>
45 #include <pulsecore/play-memblockq.h>
46
47 #include "sink.h"
48
49 #define MAX_MIX_CHANNELS 32
50 #define SILENCE_BUFFER_LENGTH (64*1024)
51
52 static PA_DEFINE_CHECK_TYPE(pa_sink, pa_msgobject);
53
54 static void sink_free(pa_object *s);
55
56 pa_sink* pa_sink_new(
57 pa_core *core,
58 const char *driver,
59 const char *name,
60 int fail,
61 const pa_sample_spec *spec,
62 const pa_channel_map *map) {
63
64 pa_sink *s;
65 char *n = NULL;
66 char st[256];
67 int r;
68 pa_channel_map tmap;
69
70 pa_assert(core);
71 pa_assert(name);
72 pa_assert(spec);
73
74 pa_return_null_if_fail(pa_sample_spec_valid(spec));
75
76 if (!map)
77 map = pa_channel_map_init_auto(&tmap, spec->channels, PA_CHANNEL_MAP_DEFAULT);
78
79 pa_return_null_if_fail(map && pa_channel_map_valid(map));
80 pa_return_null_if_fail(map->channels == spec->channels);
81 pa_return_null_if_fail(!driver || pa_utf8_valid(driver));
82 pa_return_null_if_fail(name && pa_utf8_valid(name) && *name);
83
84 s = pa_msgobject_new(pa_sink);
85
86 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SINK, s, fail))) {
87 pa_xfree(s);
88 return NULL;
89 }
90
91 s->parent.parent.free = sink_free;
92 s->parent.process_msg = pa_sink_process_msg;
93
94 s->core = core;
95 s->state = PA_SINK_IDLE;
96 s->name = pa_xstrdup(name);
97 s->description = NULL;
98 s->driver = pa_xstrdup(driver);
99 s->module = NULL;
100
101 s->sample_spec = *spec;
102 s->channel_map = *map;
103
104 s->inputs = pa_idxset_new(NULL, NULL);
105
106 pa_cvolume_reset(&s->volume, spec->channels);
107 s->muted = 0;
108 s->refresh_volume = s->refresh_mute = 0;
109
110 s->is_hardware = 0;
111
112 s->get_latency = NULL;
113 s->set_volume = NULL;
114 s->get_volume = NULL;
115 s->set_mute = NULL;
116 s->get_mute = NULL;
117 s->set_state = NULL;
118 s->userdata = NULL;
119
120 s->asyncmsgq = NULL;
121 s->silence = NULL;
122
123 r = pa_idxset_put(core->sinks, s, &s->index);
124 pa_assert(s->index != PA_IDXSET_INVALID && r >= 0);
125
126 pa_sample_spec_snprint(st, sizeof(st), spec);
127 pa_log_info("Created sink %u \"%s\" with sample spec \"%s\"", s->index, s->name, st);
128
129 n = pa_sprintf_malloc("%s.monitor", name);
130
131 if (!(s->monitor_source = pa_source_new(core, driver, n, 0, spec, map)))
132 pa_log_warn("failed to create monitor source.");
133 else {
134 char *d;
135 s->monitor_source->monitor_of = s;
136 d = pa_sprintf_malloc("Monitor Source of %s", s->name);
137 pa_source_set_description(s->monitor_source, d);
138 pa_xfree(d);
139 }
140
141 pa_xfree(n);
142
143 s->thread_info.inputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
144 s->thread_info.soft_volume = s->volume;
145 s->thread_info.soft_muted = s->muted;
146 s->thread_info.state = s->state;
147
148 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
149
150 pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_NEW_POST], s);
151
152 return s;
153 }
154
155 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
156 int ret;
157
158 pa_assert(s);
159
160 if (s->state == state)
161 return 0;
162
163 if (s->set_state)
164 if ((ret = s->set_state(s, state)) < 0)
165 return -1;
166
167 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) < 0)
168 return -1;
169
170 s->state = state;
171
172 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], s);
173 return 0;
174 }
175
176 void pa_sink_disconnect(pa_sink* s) {
177 pa_sink_input *i, *j = NULL;
178
179 pa_assert(s);
180 pa_return_if_fail(s->state != PA_SINK_DISCONNECTED);
181
182 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_DISCONNECT], s);
183
184 pa_namereg_unregister(s->core, s->name);
185 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
186
187 while ((i = pa_idxset_first(s->inputs, NULL))) {
188 pa_assert(i != j);
189 pa_sink_input_kill(i);
190 j = i;
191 }
192
193 if (s->monitor_source)
194 pa_source_disconnect(s->monitor_source);
195
196 sink_set_state(s, PA_SINK_DISCONNECTED);
197
198 s->get_latency = NULL;
199 s->get_volume = NULL;
200 s->set_volume = NULL;
201 s->set_mute = NULL;
202 s->get_mute = NULL;
203 s->set_state = NULL;
204
205 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
206
207 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_DISCONNECT_POST], s);
208 }
209
210 static void sink_free(pa_object *o) {
211 pa_sink *s = PA_SINK(o);
212 pa_sink_input *i;
213
214 pa_assert(s);
215 pa_assert(pa_sink_refcnt(s) == 0);
216
217 if (s->state != PA_SINK_DISCONNECTED)
218 pa_sink_disconnect(s);
219
220 pa_log_info("Freeing sink %u \"%s\"", s->index, s->name);
221
222 if (s->monitor_source) {
223 pa_source_unref(s->monitor_source);
224 s->monitor_source = NULL;
225 }
226
227 pa_idxset_free(s->inputs, NULL, NULL);
228
229 while ((i = pa_hashmap_steal_first(s->thread_info.inputs)))
230 pa_sink_input_unref(i);
231
232 pa_hashmap_free(s->thread_info.inputs, NULL, NULL);
233
234 if (s->silence)
235 pa_memblock_unref(s->silence);
236
237 pa_xfree(s->name);
238 pa_xfree(s->description);
239 pa_xfree(s->driver);
240 pa_xfree(s);
241 }
242
243 void pa_sink_set_asyncmsgq(pa_sink *s, pa_asyncmsgq *q) {
244 pa_sink_assert_ref(s);
245 pa_assert(q);
246
247 s->asyncmsgq = q;
248
249 if (s->monitor_source)
250 pa_source_set_asyncmsgq(s->monitor_source, q);
251 }
252
253 int pa_sink_update_status(pa_sink*s) {
254 pa_sink_assert_ref(s);
255
256 if (s->state == PA_SINK_SUSPENDED)
257 return 0;
258
259 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
260 }
261
262 int pa_sink_suspend(pa_sink *s, int suspend) {
263 pa_sink_assert_ref(s);
264
265 if (suspend)
266 return sink_set_state(s, PA_SINK_SUSPENDED);
267 else
268 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
269 }
270
271 void pa_sink_ping(pa_sink *s) {
272 pa_sink_assert_ref(s);
273
274 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_PING, NULL, 0, NULL, NULL);
275 }
276
277 static unsigned fill_mix_info(pa_sink *s, pa_mix_info *info, unsigned maxinfo) {
278 pa_sink_input *i;
279 unsigned n = 0;
280 void *state = NULL;
281
282 pa_sink_assert_ref(s);
283 pa_assert(info);
284
285 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)) && maxinfo > 0) {
286 pa_sink_input_assert_ref(i);
287
288 if (pa_sink_input_peek(i, &info->chunk, &info->volume) < 0)
289 continue;
290
291 info->userdata = pa_sink_input_ref(i);
292
293 pa_assert(info->chunk.memblock);
294 pa_assert(info->chunk.length > 0);
295
296 info++;
297 n++;
298 maxinfo--;
299 }
300
301 return n;
302 }
303
304 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned n, size_t length) {
305 pa_sink_input *i;
306 void *state = NULL;
307 unsigned p = 0;
308 unsigned n_unreffed = 0;
309
310 pa_sink_assert_ref(s);
311
312 /* We optimize for the case where the order of the inputs has not changed */
313
314 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
315 unsigned j;
316 pa_mix_info* m;
317
318 pa_sink_input_assert_ref(i);
319
320 m = NULL;
321
322 /* Let's try to find the matching entry info the pa_mix_info array */
323 for (j = 0; j < n; j ++) {
324
325 if (info[p].userdata == i) {
326 m = info + p;
327 break;
328 }
329
330 p++;
331 if (p >= n)
332 p = 0;
333 }
334
335 /* Drop read data */
336 pa_sink_input_drop(i, length);
337
338 if (m) {
339 pa_sink_input_unref(m->userdata);
340 m->userdata = NULL;
341 if (m->chunk.memblock)
342 pa_memblock_unref(m->chunk.memblock);
343 pa_memchunk_reset(&m->chunk);
344
345 n_unreffed += 1;
346 }
347 }
348
349 /* Now drop references to entries that are included in the
350 * pa_mix_info array but don't exist anymore */
351
352 if (n_unreffed < n) {
353 for (; n > 0; info++, n--) {
354 if (info->userdata)
355 pa_sink_input_unref(info->userdata);
356 if (info->chunk.memblock)
357 pa_memblock_unref(info->chunk.memblock);
358 }
359 }
360 }
361
362 void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
363 pa_mix_info info[MAX_MIX_CHANNELS];
364 unsigned n;
365
366 pa_sink_assert_ref(s);
367 pa_assert(length);
368 pa_assert(result);
369
370 pa_sink_ref(s);
371
372 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, info, MAX_MIX_CHANNELS) : 0;
373
374 if (n == 0) {
375
376 if (length > SILENCE_BUFFER_LENGTH)
377 length = SILENCE_BUFFER_LENGTH;
378
379 if (!s->silence || pa_memblock_get_length(s->silence) < length) {
380 if (s->silence)
381 pa_memblock_unref(s->silence);
382 s->silence = pa_silence_memblock_new(s->core->mempool, &s->sample_spec, length);
383 }
384
385 result->memblock = pa_memblock_ref(s->silence);
386 result->length = length;
387 result->index = 0;
388
389 } else if (n == 1) {
390 pa_cvolume volume;
391
392 *result = info[0].chunk;
393 pa_memblock_ref(result->memblock);
394
395 if (result->length > length)
396 result->length = length;
397
398 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
399
400 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&volume)) {
401 pa_memchunk_make_writable(result, 0);
402 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
403 pa_silence_memchunk(result, &s->sample_spec);
404 else
405 pa_volume_memchunk(result, &s->sample_spec, &volume);
406 }
407 } else {
408 void *ptr;
409 result->memblock = pa_memblock_new(s->core->mempool, length);
410
411 ptr = pa_memblock_acquire(result->memblock);
412 result->length = pa_mix(info, n, ptr, length, &s->sample_spec, &s->thread_info.soft_volume, s->thread_info.soft_muted);
413 pa_memblock_release(result->memblock);
414
415 result->index = 0;
416 }
417
418 if (s->thread_info.state == PA_SINK_RUNNING)
419 inputs_drop(s, info, n, result->length);
420
421 if (s->monitor_source)
422 pa_source_post(s->monitor_source, result);
423
424 pa_sink_unref(s);
425 }
426
427 void pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
428 pa_mix_info info[MAX_MIX_CHANNELS];
429 unsigned n;
430
431 pa_sink_assert_ref(s);
432 pa_assert(target);
433 pa_assert(target->memblock);
434 pa_assert(target->length);
435
436 pa_sink_ref(s);
437
438 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, info, MAX_MIX_CHANNELS) : 0;
439
440 if (n == 0) {
441 pa_silence_memchunk(target, &s->sample_spec);
442 } else if (n == 1) {
443 if (target->length > info[0].chunk.length)
444 target->length = info[0].chunk.length;
445
446 if (s->thread_info.soft_muted)
447 pa_silence_memchunk(target, &s->sample_spec);
448 else {
449 void *src, *ptr;
450 pa_cvolume volume;
451
452 ptr = pa_memblock_acquire(target->memblock);
453 src = pa_memblock_acquire(info[0].chunk.memblock);
454
455 memcpy((uint8_t*) ptr + target->index,
456 (uint8_t*) src + info[0].chunk.index,
457 target->length);
458
459 pa_memblock_release(target->memblock);
460 pa_memblock_release(info[0].chunk.memblock);
461
462 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
463
464 if (!pa_cvolume_is_norm(&volume))
465 pa_volume_memchunk(target, &s->sample_spec, &volume);
466 }
467
468 } else {
469 void *ptr;
470
471 ptr = pa_memblock_acquire(target->memblock);
472
473 target->length = pa_mix(info, n,
474 (uint8_t*) ptr + target->index,
475 target->length,
476 &s->sample_spec,
477 &s->thread_info.soft_volume,
478 s->thread_info.soft_muted);
479
480 pa_memblock_release(target->memblock);
481 }
482
483 if (s->thread_info.state == PA_SINK_RUNNING)
484 inputs_drop(s, info, n, target->length);
485
486 if (s->monitor_source)
487 pa_source_post(s->monitor_source, target);
488
489 pa_sink_unref(s);
490 }
491
492 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
493 pa_memchunk chunk;
494 size_t l, d;
495
496 pa_sink_assert_ref(s);
497 pa_assert(target);
498 pa_assert(target->memblock);
499 pa_assert(target->length);
500
501 pa_sink_ref(s);
502
503 l = target->length;
504 d = 0;
505 while (l > 0) {
506 chunk = *target;
507 chunk.index += d;
508 chunk.length -= d;
509
510 pa_sink_render_into(s, &chunk);
511
512 d += chunk.length;
513 l -= chunk.length;
514 }
515
516 pa_sink_unref(s);
517 }
518
519 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
520 pa_sink_assert_ref(s);
521 pa_assert(length);
522 pa_assert(result);
523
524 /*** This needs optimization ***/
525
526 result->memblock = pa_memblock_new(s->core->mempool, result->length = length);
527 result->index = 0;
528
529 pa_sink_render_into_full(s, result);
530 }
531
532 pa_usec_t pa_sink_get_latency(pa_sink *s) {
533 pa_usec_t usec = 0;
534
535 pa_sink_assert_ref(s);
536
537 if (s->get_latency)
538 return s->get_latency(s);
539
540 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
541 return 0;
542
543 return usec;
544 }
545
546 void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume) {
547 int changed;
548
549 pa_sink_assert_ref(s);
550 pa_assert(volume);
551
552 changed = !pa_cvolume_equal(volume, &s->volume);
553 s->volume = *volume;
554
555 if (s->set_volume && s->set_volume(s) < 0)
556 s->set_volume = NULL;
557
558 if (!s->set_volume)
559 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), 0, NULL, pa_xfree);
560
561 if (changed)
562 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
563 }
564
565 const pa_cvolume *pa_sink_get_volume(pa_sink *s) {
566 struct pa_cvolume old_volume;
567
568 pa_sink_assert_ref(s);
569
570 old_volume = s->volume;
571
572 if (s->get_volume && s->get_volume(s) < 0)
573 s->get_volume = NULL;
574
575 if (!s->get_volume && s->refresh_volume)
576 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, &s->volume, 0, NULL);
577
578 if (!pa_cvolume_equal(&old_volume, &s->volume))
579 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
580
581 return &s->volume;
582 }
583
584 void pa_sink_set_mute(pa_sink *s, int mute) {
585 int changed;
586
587 pa_sink_assert_ref(s);
588
589 changed = s->muted != mute;
590
591 if (s->set_mute && s->set_mute(s) < 0)
592 s->set_mute = NULL;
593
594 if (!s->set_mute)
595 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
596
597 if (changed)
598 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
599 }
600
601 int pa_sink_get_mute(pa_sink *s) {
602 int old_muted;
603
604 pa_sink_assert_ref(s);
605
606 old_muted = s->muted;
607
608 if (s->get_mute && s->get_mute(s) < 0)
609 s->get_mute = NULL;
610
611 if (!s->get_mute && s->refresh_mute)
612 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MUTE, &s->muted, 0, NULL);
613
614 if (old_muted != s->muted)
615 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
616
617 return s->muted;
618 }
619
620 void pa_sink_set_module(pa_sink *s, pa_module *m) {
621 pa_sink_assert_ref(s);
622
623 if (s->module == m)
624 return;
625
626 s->module = m;
627
628 if (s->monitor_source)
629 pa_source_set_module(s->monitor_source, m);
630
631 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
632 }
633
634 void pa_sink_set_description(pa_sink *s, const char *description) {
635 pa_sink_assert_ref(s);
636
637 if (!description && !s->description)
638 return;
639
640 if (description && s->description && !strcmp(description, s->description))
641 return;
642
643 pa_xfree(s->description);
644 s->description = pa_xstrdup(description);
645
646 if (s->monitor_source) {
647 char *n;
648
649 n = pa_sprintf_malloc("Monitor Source of %s", s->description? s->description : s->name);
650 pa_source_set_description(s->monitor_source, n);
651 pa_xfree(n);
652 }
653
654 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
655 }
656
657 unsigned pa_sink_used_by(pa_sink *s) {
658 unsigned ret;
659
660 pa_sink_assert_ref(s);
661
662 ret = pa_idxset_size(s->inputs);
663
664 if (s->monitor_source)
665 ret += pa_source_used_by(s->monitor_source);
666
667 return ret;
668 }
669
670 int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
671 pa_sink *s = PA_SINK(o);
672 pa_sink_assert_ref(s);
673
674 switch ((pa_sink_message_t) code) {
675
676 case PA_SINK_MESSAGE_ADD_INPUT: {
677 pa_sink_input *i = PA_SINK_INPUT(userdata);
678 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
679
680 /* Since the caller sleeps in pa_sink_input_put(), we can
681 * safely access data outside of thread_info even though
682 * it is mutable */
683
684 if ((i->thread_info.sync_prev = i->sync_prev)) {
685 pa_assert(i->sink == i->thread_info.sync_prev->sink);
686 pa_assert(i->sync_prev->sync_next == i);
687 i->thread_info.sync_prev->thread_info.sync_next = i;
688 }
689
690 if ((i->thread_info.sync_next = i->sync_next)) {
691 pa_assert(i->sink == i->thread_info.sync_next->sink);
692 pa_assert(i->sync_next->sync_prev == i);
693 i->thread_info.sync_next->thread_info.sync_prev = i;
694 }
695
696 return 0;
697 }
698
699 case PA_SINK_MESSAGE_REMOVE_INPUT: {
700 pa_sink_input *i = PA_SINK_INPUT(userdata);
701
702 /* Since the caller sleeps in pa_sink_input_disconnect(),
703 * we can safely access data outside of thread_info even
704 * though it is mutable */
705
706 pa_assert(!i->thread_info.sync_prev);
707 pa_assert(!i->thread_info.sync_next);
708
709 if (i->thread_info.sync_prev) {
710 i->thread_info.sync_prev->thread_info.sync_next = i->thread_info.sync_prev->sync_next;
711 i->thread_info.sync_prev = NULL;
712 }
713
714 if (i->thread_info.sync_next) {
715 i->thread_info.sync_next->thread_info.sync_prev = i->thread_info.sync_next->sync_prev;
716 i->thread_info.sync_next = NULL;
717 }
718
719 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
720 pa_sink_input_unref(i);
721
722 return 0;
723 }
724
725 case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER: {
726 pa_sink_input_move_info *info = userdata;
727 int volume_is_norm;
728
729 /* We don't support moving synchronized streams. */
730 pa_assert(!info->sink_input->sync_prev);
731 pa_assert(!info->sink_input->sync_next);
732 pa_assert(!info->sink_input->thread_info.sync_next);
733 pa_assert(!info->sink_input->thread_info.sync_prev);
734
735 if (info->ghost_sink_input) {
736 pa_assert(info->buffer_bytes > 0);
737 pa_assert(info->buffer);
738
739 volume_is_norm = pa_cvolume_is_norm(&info->sink_input->thread_info.volume);
740
741 pa_log_debug("Buffering %u bytes ...", info->buffer_bytes);
742
743 while (info->buffer_bytes > 0) {
744 pa_memchunk memchunk;
745 pa_cvolume volume;
746 size_t n;
747
748 if (pa_sink_input_peek(info->sink_input, &memchunk, &volume) < 0)
749 break;
750
751 n = memchunk.length > info->buffer_bytes ? info->buffer_bytes : memchunk.length;
752 pa_sink_input_drop(info->sink_input, n);
753 memchunk.length = n;
754
755 if (!volume_is_norm) {
756 pa_memchunk_make_writable(&memchunk, 0);
757 pa_volume_memchunk(&memchunk, &s->sample_spec, &volume);
758 }
759
760 if (pa_memblockq_push(info->buffer, &memchunk) < 0) {
761 pa_memblock_unref(memchunk.memblock);
762 break;
763 }
764
765 pa_memblock_unref(memchunk.memblock);
766 info->buffer_bytes -= n;
767 }
768
769 /* Add the remaining already resampled chunk to the buffer */
770 if (info->sink_input->thread_info.resampled_chunk.memblock)
771 pa_memblockq_push(info->buffer, &info->sink_input->thread_info.resampled_chunk);
772
773 pa_memblockq_sink_input_set_queue(info->ghost_sink_input, info->buffer);
774
775 pa_log_debug("Buffered %u bytes ...", pa_memblockq_get_length(info->buffer));
776 }
777
778 /* Let's remove the sink input ...*/
779 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(info->sink_input->index)))
780 pa_sink_input_unref(info->sink_input);
781
782 /* .. and add the ghost sink input instead */
783 if (info->ghost_sink_input) {
784 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(info->ghost_sink_input->index), pa_sink_input_ref(info->ghost_sink_input));
785 info->ghost_sink_input->thread_info.sync_prev = info->ghost_sink_input->thread_info.sync_next = NULL;
786 }
787
788 return 0;
789 }
790
791 case PA_SINK_MESSAGE_SET_VOLUME:
792 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
793 return 0;
794
795 case PA_SINK_MESSAGE_SET_MUTE:
796 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
797 return 0;
798
799 case PA_SINK_MESSAGE_GET_VOLUME:
800 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
801 return 0;
802
803 case PA_SINK_MESSAGE_GET_MUTE:
804 *((int*) userdata) = s->thread_info.soft_muted;
805 return 0;
806
807 case PA_SINK_MESSAGE_PING:
808 return 0;
809
810 case PA_SINK_MESSAGE_SET_STATE:
811
812 s->thread_info.state = PA_PTR_TO_UINT(userdata);
813 return 0;
814
815 case PA_SINK_MESSAGE_GET_LATENCY:
816 case PA_SINK_MESSAGE_MAX:
817 ;
818 }
819
820 return -1;
821 }