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