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