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