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