]> code.delx.au - pulseaudio/blob - src/pulsecore/sink.c
maintain the attach status in a boolean variable 'attach' accessible from the IO...
[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 <string.h>
31 #include <stdio.h>
32
33 #include <pulse/introspect.h>
34 #include <pulse/utf8.h>
35 #include <pulse/xmalloc.h>
36
37 #include <pulsecore/sink-input.h>
38 #include <pulsecore/namereg.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/sample-util.h>
41 #include <pulsecore/core-subscribe.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/macro.h>
44 #include <pulsecore/play-memblockq.h>
45
46 #include "sink.h"
47
48 #define MAX_MIX_CHANNELS 32
49 #define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
50 #define SILENCE_BUFFER_LENGTH (PA_PAGE_SIZE*12)
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 pa_channel_map tmap;
68
69 pa_assert(core);
70 pa_assert(name);
71 pa_assert(spec);
72
73 pa_return_null_if_fail(pa_sample_spec_valid(spec));
74
75 if (!map)
76 map = pa_channel_map_init_auto(&tmap, spec->channels, PA_CHANNEL_MAP_DEFAULT);
77
78 pa_return_null_if_fail(map && pa_channel_map_valid(map));
79 pa_return_null_if_fail(map->channels == spec->channels);
80 pa_return_null_if_fail(!driver || pa_utf8_valid(driver));
81 pa_return_null_if_fail(name && pa_utf8_valid(name) && *name);
82
83 s = pa_msgobject_new(pa_sink);
84
85 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SINK, s, fail))) {
86 pa_xfree(s);
87 return NULL;
88 }
89
90 s->parent.parent.free = sink_free;
91 s->parent.process_msg = pa_sink_process_msg;
92
93 s->core = core;
94 s->state = PA_SINK_INIT;
95 s->flags = 0;
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 s->n_corked = 0;
106
107 pa_cvolume_reset(&s->volume, spec->channels);
108 s->muted = FALSE;
109 s->refresh_volume = s->refresh_mute = FALSE;
110
111 s->get_latency = NULL;
112 s->set_volume = NULL;
113 s->get_volume = NULL;
114 s->set_mute = NULL;
115 s->get_mute = NULL;
116 s->set_state = NULL;
117 s->userdata = NULL;
118
119 s->asyncmsgq = NULL;
120 s->rtpoll = NULL;
121 s->silence = NULL;
122
123 pa_assert_se(pa_idxset_put(core->sinks, s, &s->index) >= 0);
124
125 pa_sample_spec_snprint(st, sizeof(st), spec);
126 pa_log_info("Created sink %u \"%s\" with sample spec \"%s\"", s->index, s->name, st);
127
128 n = pa_sprintf_malloc("%s.monitor", name);
129
130 if (!(s->monitor_source = pa_source_new(core, driver, n, 0, spec, map)))
131 pa_log_warn("Failed to create monitor source.");
132 else {
133 char *d;
134 s->monitor_source->monitor_of = s;
135 d = pa_sprintf_malloc("Monitor Source of %s", s->name);
136 pa_source_set_description(s->monitor_source, d);
137 pa_xfree(d);
138 }
139
140 pa_xfree(n);
141
142 s->thread_info.inputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
143 s->thread_info.soft_volume = s->volume;
144 s->thread_info.soft_muted = s->muted;
145 s->thread_info.state = s->state;
146
147 return s;
148 }
149
150 void pa_sink_put(pa_sink* s) {
151 pa_sink_assert_ref(s);
152
153 pa_assert(s->state == PA_SINK_INIT);
154 pa_assert(s->asyncmsgq);
155 pa_assert(s->rtpoll);
156
157 s->thread_info.state = s->state = PA_SINK_IDLE;
158
159 pa_source_put(s->monitor_source);
160
161 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
162 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_NEW_POST], s);
163 }
164
165 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
166 int ret;
167
168 pa_assert(s);
169
170 if (s->state == state)
171 return 0;
172
173 if (state == PA_SINK_SUSPENDED && !(s->flags & PA_SINK_CAN_SUSPEND))
174 return -1;
175
176 if ((s->state == PA_SINK_SUSPENDED && PA_SINK_OPENED(state)) ||
177 (PA_SINK_OPENED(s->state) && state == PA_SINK_SUSPENDED)) {
178 pa_sink_input *i;
179 uint32_t idx;
180
181 /* We're suspending or resuming, tell everyone about it */
182
183 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx)))
184 if (i->suspend)
185 i->suspend(i, state == PA_SINK_SUSPENDED);
186 }
187
188 if (s->set_state)
189 if ((ret = s->set_state(s, state)) < 0)
190 return -1;
191
192 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) < 0)
193 return -1;
194
195 s->state = state;
196
197 if (state != PA_SINK_UNLINKED) /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
198 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], s);
199 return 0;
200 }
201
202 void pa_sink_unlink(pa_sink* s) {
203 pa_sink_input *i, *j = NULL;
204
205 pa_assert(s);
206 pa_assert(PA_SINK_LINKED(s->state));
207
208 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK], s);
209
210 pa_namereg_unregister(s->core, s->name);
211 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
212
213 while ((i = pa_idxset_first(s->inputs, NULL))) {
214 pa_assert(i != j);
215 pa_sink_input_kill(i);
216 j = i;
217 }
218
219 sink_set_state(s, PA_SINK_UNLINKED);
220
221 if (s->monitor_source)
222 pa_source_unlink(s->monitor_source);
223
224 s->get_latency = NULL;
225 s->get_volume = NULL;
226 s->set_volume = NULL;
227 s->set_mute = NULL;
228 s->get_mute = NULL;
229 s->set_state = NULL;
230
231 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
232
233 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], s);
234 }
235
236 static void sink_free(pa_object *o) {
237 pa_sink *s = PA_SINK(o);
238 pa_sink_input *i;
239
240 pa_assert(s);
241 pa_assert(pa_sink_refcnt(s) == 0);
242
243 if (PA_SINK_LINKED(s->state))
244 pa_sink_unlink(s);
245
246 pa_log_info("Freeing sink %u \"%s\"", s->index, s->name);
247
248 if (s->monitor_source) {
249 pa_source_unref(s->monitor_source);
250 s->monitor_source = NULL;
251 }
252
253 pa_idxset_free(s->inputs, NULL, NULL);
254
255 while ((i = pa_hashmap_steal_first(s->thread_info.inputs)))
256 pa_sink_input_unref(i);
257
258 pa_hashmap_free(s->thread_info.inputs, NULL, NULL);
259
260 if (s->silence)
261 pa_memblock_unref(s->silence);
262
263 pa_xfree(s->name);
264 pa_xfree(s->description);
265 pa_xfree(s->driver);
266 pa_xfree(s);
267 }
268
269 void pa_sink_set_asyncmsgq(pa_sink *s, pa_asyncmsgq *q) {
270 pa_sink_assert_ref(s);
271 pa_assert(q);
272
273 s->asyncmsgq = q;
274
275 if (s->monitor_source)
276 pa_source_set_asyncmsgq(s->monitor_source, q);
277 }
278
279 void pa_sink_set_rtpoll(pa_sink *s, pa_rtpoll *p) {
280 pa_sink_assert_ref(s);
281 pa_assert(p);
282
283 s->rtpoll = p;
284 if (s->monitor_source)
285 pa_source_set_rtpoll(s->monitor_source, p);
286 }
287
288 int pa_sink_update_status(pa_sink*s) {
289 pa_sink_assert_ref(s);
290 pa_assert(PA_SINK_LINKED(s->state));
291
292 if (s->state == PA_SINK_SUSPENDED)
293 return 0;
294
295 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
296 }
297
298 int pa_sink_suspend(pa_sink *s, pa_bool_t suspend) {
299 pa_sink_assert_ref(s);
300 pa_assert(PA_SINK_LINKED(s->state));
301
302 if (suspend)
303 return sink_set_state(s, PA_SINK_SUSPENDED);
304 else
305 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
306 }
307
308 void pa_sink_ping(pa_sink *s) {
309 pa_sink_assert_ref(s);
310 pa_assert(PA_SINK_LINKED(s->state));
311
312 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_PING, NULL, 0, NULL, NULL);
313 }
314
315 static unsigned fill_mix_info(pa_sink *s, size_t length, pa_mix_info *info, unsigned maxinfo) {
316 pa_sink_input *i;
317 unsigned n = 0;
318 void *state = NULL;
319
320 pa_sink_assert_ref(s);
321 pa_assert(info);
322
323 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)) && maxinfo > 0) {
324 pa_sink_input_assert_ref(i);
325
326 if (pa_sink_input_peek(i, length, &info->chunk, &info->volume) < 0)
327 continue;
328
329 info->userdata = pa_sink_input_ref(i);
330
331 pa_assert(info->chunk.memblock);
332 pa_assert(info->chunk.length > 0);
333
334 info++;
335 n++;
336 maxinfo--;
337 }
338
339 return n;
340 }
341
342 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned n, size_t length) {
343 pa_sink_input *i;
344 void *state = NULL;
345 unsigned p = 0;
346 unsigned n_unreffed = 0;
347
348 pa_sink_assert_ref(s);
349
350 /* We optimize for the case where the order of the inputs has not changed */
351
352 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
353 unsigned j;
354 pa_mix_info* m;
355
356 pa_sink_input_assert_ref(i);
357
358 m = NULL;
359
360 /* Let's try to find the matching entry info the pa_mix_info array */
361 for (j = 0; j < n; j ++) {
362
363 if (info[p].userdata == i) {
364 m = info + p;
365 break;
366 }
367
368 p++;
369 if (p >= n)
370 p = 0;
371 }
372
373 /* Drop read data */
374 pa_sink_input_drop(i, length);
375
376 if (m) {
377 pa_sink_input_unref(m->userdata);
378 m->userdata = NULL;
379 if (m->chunk.memblock)
380 pa_memblock_unref(m->chunk.memblock);
381 pa_memchunk_reset(&m->chunk);
382
383 n_unreffed += 1;
384 }
385 }
386
387 /* Now drop references to entries that are included in the
388 * pa_mix_info array but don't exist anymore */
389
390 if (n_unreffed < n) {
391 for (; n > 0; info++, n--) {
392 if (info->userdata)
393 pa_sink_input_unref(info->userdata);
394 if (info->chunk.memblock)
395 pa_memblock_unref(info->chunk.memblock);
396 }
397 }
398 }
399
400 void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
401 pa_mix_info info[MAX_MIX_CHANNELS];
402 unsigned n;
403 size_t block_size_max;
404
405 pa_sink_assert_ref(s);
406 pa_assert(PA_SINK_OPENED(s->thread_info.state));
407 pa_assert(pa_frame_aligned(length, &s->sample_spec));
408 pa_assert(result);
409
410 pa_sink_ref(s);
411
412 if (length <= 0)
413 length = pa_frame_align(MIX_BUFFER_LENGTH, &s->sample_spec);
414
415 block_size_max = pa_mempool_block_size_max(s->core->mempool);
416 if (length > block_size_max)
417 length = pa_frame_align(block_size_max, &s->sample_spec);
418
419 pa_assert(length > 0);
420
421 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, length, info, MAX_MIX_CHANNELS) : 0;
422
423 if (n == 0) {
424
425 if (length > SILENCE_BUFFER_LENGTH)
426 length = pa_frame_align(SILENCE_BUFFER_LENGTH, &s->sample_spec);
427
428 pa_assert(length > 0);
429
430 if (!s->silence || pa_memblock_get_length(s->silence) < length) {
431 if (s->silence)
432 pa_memblock_unref(s->silence);
433 s->silence = pa_silence_memblock_new(s->core->mempool, &s->sample_spec, length);
434 }
435
436 result->memblock = pa_memblock_ref(s->silence);
437 result->length = length;
438 result->index = 0;
439
440 } else if (n == 1) {
441 pa_cvolume volume;
442
443 *result = info[0].chunk;
444 pa_memblock_ref(result->memblock);
445
446 if (result->length > length)
447 result->length = length;
448
449 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
450
451 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&volume)) {
452 pa_memchunk_make_writable(result, 0);
453 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
454 pa_silence_memchunk(result, &s->sample_spec);
455 else
456 pa_volume_memchunk(result, &s->sample_spec, &volume);
457 }
458 } else {
459 void *ptr;
460 result->memblock = pa_memblock_new(s->core->mempool, length);
461
462 ptr = pa_memblock_acquire(result->memblock);
463 result->length = pa_mix(info, n, ptr, length, &s->sample_spec, &s->thread_info.soft_volume, s->thread_info.soft_muted);
464 pa_memblock_release(result->memblock);
465
466 result->index = 0;
467 }
468
469 if (s->thread_info.state == PA_SINK_RUNNING)
470 inputs_drop(s, info, n, result->length);
471
472 if (s->monitor_source)
473 pa_source_post(s->monitor_source, result);
474
475 pa_sink_unref(s);
476 }
477
478 void pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
479 pa_mix_info info[MAX_MIX_CHANNELS];
480 unsigned n;
481
482 pa_sink_assert_ref(s);
483 pa_assert(PA_SINK_OPENED(s->thread_info.state));
484 pa_assert(target);
485 pa_assert(target->memblock);
486 pa_assert(target->length > 0);
487 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
488
489 pa_sink_ref(s);
490
491 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, target->length, info, MAX_MIX_CHANNELS) : 0;
492
493 if (n == 0) {
494 pa_silence_memchunk(target, &s->sample_spec);
495 } else if (n == 1) {
496 if (target->length > info[0].chunk.length)
497 target->length = info[0].chunk.length;
498
499 if (s->thread_info.soft_muted)
500 pa_silence_memchunk(target, &s->sample_spec);
501 else {
502 void *src, *ptr;
503 pa_cvolume volume;
504
505 ptr = pa_memblock_acquire(target->memblock);
506 src = pa_memblock_acquire(info[0].chunk.memblock);
507
508 memcpy((uint8_t*) ptr + target->index,
509 (uint8_t*) src + info[0].chunk.index,
510 target->length);
511
512 pa_memblock_release(target->memblock);
513 pa_memblock_release(info[0].chunk.memblock);
514
515 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
516
517 if (!pa_cvolume_is_norm(&volume))
518 pa_volume_memchunk(target, &s->sample_spec, &volume);
519 }
520
521 } else {
522 void *ptr;
523
524 ptr = pa_memblock_acquire(target->memblock);
525
526 target->length = pa_mix(info, n,
527 (uint8_t*) ptr + target->index,
528 target->length,
529 &s->sample_spec,
530 &s->thread_info.soft_volume,
531 s->thread_info.soft_muted);
532
533 pa_memblock_release(target->memblock);
534 }
535
536 if (s->thread_info.state == PA_SINK_RUNNING)
537 inputs_drop(s, info, n, target->length);
538
539 if (s->monitor_source)
540 pa_source_post(s->monitor_source, target);
541
542 pa_sink_unref(s);
543 }
544
545 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
546 pa_memchunk chunk;
547 size_t l, d;
548
549 pa_sink_assert_ref(s);
550 pa_assert(PA_SINK_OPENED(s->thread_info.state));
551 pa_assert(target);
552 pa_assert(target->memblock);
553 pa_assert(target->length > 0);
554 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
555
556 pa_sink_ref(s);
557
558 l = target->length;
559 d = 0;
560 while (l > 0) {
561 chunk = *target;
562 chunk.index += d;
563 chunk.length -= d;
564
565 pa_sink_render_into(s, &chunk);
566
567 d += chunk.length;
568 l -= chunk.length;
569 }
570
571 pa_sink_unref(s);
572 }
573
574 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
575 pa_sink_assert_ref(s);
576 pa_assert(PA_SINK_OPENED(s->thread_info.state));
577 pa_assert(length > 0);
578 pa_assert(pa_frame_aligned(length, &s->sample_spec));
579 pa_assert(result);
580
581 /*** This needs optimization ***/
582
583 result->index = 0;
584 result->length = length;
585 result->memblock = pa_memblock_new(s->core->mempool, length);
586
587 pa_sink_render_into_full(s, result);
588 }
589
590 void pa_sink_skip(pa_sink *s, size_t length) {
591 pa_sink_input *i;
592 void *state = NULL;
593
594 pa_sink_assert_ref(s);
595 pa_assert(PA_SINK_OPENED(s->thread_info.state));
596 pa_assert(length > 0);
597 pa_assert(pa_frame_aligned(length, &s->sample_spec));
598
599 if (pa_source_used_by(s->monitor_source)) {
600 pa_memchunk chunk;
601
602 /* If something is connected to our monitor source, we have to
603 * pass valid data to it */
604
605 while (length > 0) {
606 pa_sink_render(s, length, &chunk);
607 pa_memblock_unref(chunk.memblock);
608
609 pa_assert(chunk.length <= length);
610 length -= chunk.length;
611 }
612
613 } else {
614 /* Ok, noone cares about the rendered data, so let's not even render it */
615
616 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
617 pa_sink_input_assert_ref(i);
618 pa_sink_input_drop(i, length);
619 }
620 }
621 }
622
623 pa_usec_t pa_sink_get_latency(pa_sink *s) {
624 pa_usec_t usec = 0;
625
626 pa_sink_assert_ref(s);
627 pa_assert(PA_SINK_LINKED(s->state));
628
629 if (!PA_SINK_OPENED(s->state))
630 return 0;
631
632 if (s->get_latency)
633 return s->get_latency(s);
634
635 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
636 return 0;
637
638 return usec;
639 }
640
641 void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume) {
642 int changed;
643
644 pa_sink_assert_ref(s);
645 pa_assert(PA_SINK_LINKED(s->state));
646 pa_assert(volume);
647
648 changed = !pa_cvolume_equal(volume, &s->volume);
649 s->volume = *volume;
650
651 if (s->set_volume && s->set_volume(s) < 0)
652 s->set_volume = NULL;
653
654 if (!s->set_volume)
655 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), 0, NULL, pa_xfree);
656
657 if (changed)
658 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
659 }
660
661 const pa_cvolume *pa_sink_get_volume(pa_sink *s) {
662 struct pa_cvolume old_volume;
663
664 pa_sink_assert_ref(s);
665 pa_assert(PA_SINK_LINKED(s->state));
666
667 old_volume = s->volume;
668
669 if (s->get_volume && s->get_volume(s) < 0)
670 s->get_volume = NULL;
671
672 if (!s->get_volume && s->refresh_volume)
673 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, &s->volume, 0, NULL);
674
675 if (!pa_cvolume_equal(&old_volume, &s->volume))
676 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
677
678 return &s->volume;
679 }
680
681 void pa_sink_set_mute(pa_sink *s, pa_bool_t mute) {
682 int changed;
683
684 pa_sink_assert_ref(s);
685 pa_assert(PA_SINK_LINKED(s->state));
686
687 changed = s->muted != mute;
688 s->muted = mute;
689
690 if (s->set_mute && s->set_mute(s) < 0)
691 s->set_mute = NULL;
692
693 if (!s->set_mute)
694 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
695
696 if (changed)
697 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
698 }
699
700 pa_bool_t pa_sink_get_mute(pa_sink *s) {
701 pa_bool_t old_muted;
702
703 pa_sink_assert_ref(s);
704 pa_assert(PA_SINK_LINKED(s->state));
705
706 old_muted = s->muted;
707
708 if (s->get_mute && s->get_mute(s) < 0)
709 s->get_mute = NULL;
710
711 if (!s->get_mute && s->refresh_mute)
712 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MUTE, &s->muted, 0, NULL);
713
714 if (old_muted != s->muted)
715 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
716
717 return s->muted;
718 }
719
720 void pa_sink_set_module(pa_sink *s, pa_module *m) {
721 pa_sink_assert_ref(s);
722
723 if (s->module == m)
724 return;
725
726 s->module = m;
727
728 if (s->monitor_source)
729 pa_source_set_module(s->monitor_source, m);
730
731 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
732 }
733
734 void pa_sink_set_description(pa_sink *s, const char *description) {
735 pa_sink_assert_ref(s);
736
737 if (!description && !s->description)
738 return;
739
740 if (description && s->description && !strcmp(description, s->description))
741 return;
742
743 pa_xfree(s->description);
744 s->description = pa_xstrdup(description);
745
746 if (s->monitor_source) {
747 char *n;
748
749 n = pa_sprintf_malloc("Monitor Source of %s", s->description? s->description : s->name);
750 pa_source_set_description(s->monitor_source, n);
751 pa_xfree(n);
752 }
753
754 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
755 }
756
757 unsigned pa_sink_linked_by(pa_sink *s) {
758 unsigned ret;
759
760 pa_sink_assert_ref(s);
761 pa_assert(PA_SINK_LINKED(s->state));
762
763 ret = pa_idxset_size(s->inputs);
764
765 if (s->monitor_source)
766 ret += pa_source_used_by(s->monitor_source);
767
768 return ret;
769 }
770
771 unsigned pa_sink_used_by(pa_sink *s) {
772 unsigned ret;
773
774 pa_sink_assert_ref(s);
775 pa_assert(PA_SINK_LINKED(s->state));
776
777 ret = pa_idxset_size(s->inputs);
778
779 pa_assert(ret >= s->n_corked);
780
781 ret -= s->n_corked;
782
783 if (s->monitor_source)
784 ret += pa_source_used_by(s->monitor_source);
785
786 return ret;
787 }
788
789 int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
790 pa_sink *s = PA_SINK(o);
791 pa_sink_assert_ref(s);
792 pa_assert(PA_SINK_LINKED(s->thread_info.state));
793
794 switch ((pa_sink_message_t) code) {
795
796 case PA_SINK_MESSAGE_ADD_INPUT: {
797 pa_sink_input *i = PA_SINK_INPUT(userdata);
798 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
799
800 /* Since the caller sleeps in pa_sink_input_put(), we can
801 * safely access data outside of thread_info even though
802 * it is mutable */
803
804 if ((i->thread_info.sync_prev = i->sync_prev)) {
805 pa_assert(i->sink == i->thread_info.sync_prev->sink);
806 pa_assert(i->sync_prev->sync_next == i);
807 i->thread_info.sync_prev->thread_info.sync_next = i;
808 }
809
810 if ((i->thread_info.sync_next = i->sync_next)) {
811 pa_assert(i->sink == i->thread_info.sync_next->sink);
812 pa_assert(i->sync_next->sync_prev == i);
813 i->thread_info.sync_next->thread_info.sync_prev = i;
814 }
815
816 pa_assert(!i->thread_info.attached);
817 i->thread_info.attached = TRUE;
818
819 if (i->attach)
820 i->attach(i);
821
822 return 0;
823 }
824
825 case PA_SINK_MESSAGE_REMOVE_INPUT: {
826 pa_sink_input *i = PA_SINK_INPUT(userdata);
827
828 if (i->detach)
829 i->detach(i);
830
831 pa_assert(i->thread_info.attached);
832 i->thread_info.attached = FALSE;
833
834 /* Since the caller sleeps in pa_sink_input_unlink(),
835 * we can safely access data outside of thread_info even
836 * though it is mutable */
837
838 pa_assert(!i->thread_info.sync_prev);
839 pa_assert(!i->thread_info.sync_next);
840
841 if (i->thread_info.sync_prev) {
842 i->thread_info.sync_prev->thread_info.sync_next = i->thread_info.sync_prev->sync_next;
843 i->thread_info.sync_prev = NULL;
844 }
845
846 if (i->thread_info.sync_next) {
847 i->thread_info.sync_next->thread_info.sync_prev = i->thread_info.sync_next->sync_prev;
848 i->thread_info.sync_next = NULL;
849 }
850
851 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
852 pa_sink_input_unref(i);
853
854 return 0;
855 }
856
857 case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER: {
858 pa_sink_input_move_info *info = userdata;
859 int volume_is_norm;
860
861 /* We don't support moving synchronized streams. */
862 pa_assert(!info->sink_input->sync_prev);
863 pa_assert(!info->sink_input->sync_next);
864 pa_assert(!info->sink_input->thread_info.sync_next);
865 pa_assert(!info->sink_input->thread_info.sync_prev);
866
867 if (info->ghost_sink_input) {
868 pa_assert(info->buffer_bytes > 0);
869 pa_assert(info->buffer);
870
871 volume_is_norm = pa_cvolume_is_norm(&info->sink_input->thread_info.volume);
872
873 pa_log_debug("Buffering %lu bytes ...", (unsigned long) info->buffer_bytes);
874
875 while (info->buffer_bytes > 0) {
876 pa_memchunk memchunk;
877 pa_cvolume volume;
878 size_t n;
879
880 if (pa_sink_input_peek(info->sink_input, info->buffer_bytes, &memchunk, &volume) < 0)
881 break;
882
883 n = memchunk.length > info->buffer_bytes ? info->buffer_bytes : memchunk.length;
884 pa_sink_input_drop(info->sink_input, n);
885 memchunk.length = n;
886
887 if (!volume_is_norm) {
888 pa_memchunk_make_writable(&memchunk, 0);
889 pa_volume_memchunk(&memchunk, &s->sample_spec, &volume);
890 }
891
892 if (pa_memblockq_push(info->buffer, &memchunk) < 0) {
893 pa_memblock_unref(memchunk.memblock);
894 break;
895 }
896
897 pa_memblock_unref(memchunk.memblock);
898 info->buffer_bytes -= n;
899 }
900
901 /* Add the remaining already resampled chunk to the buffer */
902 if (info->sink_input->thread_info.resampled_chunk.memblock)
903 pa_memblockq_push(info->buffer, &info->sink_input->thread_info.resampled_chunk);
904
905 pa_memblockq_sink_input_set_queue(info->ghost_sink_input, info->buffer);
906
907 pa_log_debug("Buffered %lu bytes ...", (unsigned long) pa_memblockq_get_length(info->buffer));
908 }
909
910 /* Let's remove the sink input ...*/
911 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(info->sink_input->index)))
912 pa_sink_input_unref(info->sink_input);
913
914 /* .. and add the ghost sink input instead */
915 if (info->ghost_sink_input) {
916 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(info->ghost_sink_input->index), pa_sink_input_ref(info->ghost_sink_input));
917 info->ghost_sink_input->thread_info.sync_prev = info->ghost_sink_input->thread_info.sync_next = NULL;
918 }
919
920 return 0;
921 }
922
923 case PA_SINK_MESSAGE_SET_VOLUME:
924 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
925 return 0;
926
927 case PA_SINK_MESSAGE_SET_MUTE:
928 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
929 return 0;
930
931 case PA_SINK_MESSAGE_GET_VOLUME:
932 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
933 return 0;
934
935 case PA_SINK_MESSAGE_GET_MUTE:
936 *((pa_bool_t*) userdata) = s->thread_info.soft_muted;
937 return 0;
938
939 case PA_SINK_MESSAGE_PING:
940 return 0;
941
942 case PA_SINK_MESSAGE_SET_STATE:
943
944 s->thread_info.state = PA_PTR_TO_UINT(userdata);
945 return 0;
946
947 case PA_SINK_MESSAGE_DETACH:
948
949 /* We're detaching all our input streams so that the
950 * asyncmsgq and rtpoll fields can be changed without
951 * problems */
952 pa_sink_detach_within_thread(s);
953 break;
954
955 case PA_SINK_MESSAGE_ATTACH:
956
957 /* Reattach all streams */
958 pa_sink_attach_within_thread(s);
959 break;
960
961 case PA_SINK_MESSAGE_GET_LATENCY:
962 case PA_SINK_MESSAGE_MAX:
963 ;
964 }
965
966 return -1;
967 }
968
969 int pa_sink_suspend_all(pa_core *c, pa_bool_t suspend) {
970 pa_sink *sink;
971 uint32_t idx;
972 int ret = 0;
973
974 pa_core_assert_ref(c);
975
976 for (sink = PA_SINK(pa_idxset_first(c->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(c->sinks, &idx)))
977 ret -= pa_sink_suspend(sink, suspend) < 0;
978
979 return ret;
980 }
981
982 void pa_sink_detach(pa_sink *s) {
983 pa_sink_assert_ref(s);
984 pa_assert(PA_SINK_LINKED(s->state));
985
986 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_DETACH, NULL, 0, NULL);
987 }
988
989 void pa_sink_attach(pa_sink *s) {
990 pa_sink_assert_ref(s);
991 pa_assert(PA_SINK_LINKED(s->state));
992
993 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_ATTACH, NULL, 0, NULL);
994 }
995
996 void pa_sink_detach_within_thread(pa_sink *s) {
997 pa_sink_input *i;
998 void *state = NULL;
999
1000 pa_sink_assert_ref(s);
1001 pa_assert(PA_SINK_LINKED(s->thread_info.state));
1002
1003 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1004 if (i->detach)
1005 i->detach(i);
1006
1007 if (s->monitor_source)
1008 pa_source_detach_within_thread(s->monitor_source);
1009 }
1010
1011 void pa_sink_attach_within_thread(pa_sink *s) {
1012 pa_sink_input *i;
1013 void *state = NULL;
1014
1015 pa_sink_assert_ref(s);
1016 pa_assert(PA_SINK_LINKED(s->thread_info.state));
1017
1018 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1019 if (i->attach)
1020 i->attach(i);
1021
1022 if (s->monitor_source)
1023 pa_source_attach_within_thread(s->monitor_source);
1024 }