]> code.delx.au - pulseaudio/blob - src/pulsecore/sink.c
5a79a41c0fd38315908781a1a6031a1d0671f50e
[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 <assert.h>
31 #include <string.h>
32 #include <stdio.h>
33
34 #include <pulse/introspect.h>
35 #include <pulse/utf8.h>
36 #include <pulse/xmalloc.h>
37
38 #include <pulsecore/sink-input.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/sample-util.h>
42 #include <pulsecore/core-subscribe.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/macro.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, sink_check_type, pa_msgobject_check_type);
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 int r;
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, sink_check_type);
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_IDLE;
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->is_hardware = 0;
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->silence = NULL;
121
122 r = pa_idxset_put(core->sinks, s, &s->index);
123 pa_assert(s->index != PA_IDXSET_INVALID && r >= 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 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
148
149 return s;
150 }
151
152 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
153 int ret;
154
155 pa_assert(s);
156
157 if (s->state == state)
158 return 0;
159
160 if (s->set_state)
161 if ((ret = s->set_state(s, state)) < 0)
162 return -1;
163
164 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), NULL) < 0)
165 return -1;
166
167 s->state = state;
168 return 0;
169 }
170
171 void pa_sink_disconnect(pa_sink* s) {
172 pa_sink_input *i, *j = NULL;
173
174 pa_assert(s);
175 pa_return_if_fail(s->state != PA_SINK_DISCONNECTED);
176
177 pa_namereg_unregister(s->core, s->name);
178 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
179
180 pa_hook_fire(&s->core->hook_sink_disconnect, s);
181
182 while ((i = pa_idxset_first(s->inputs, NULL))) {
183 pa_assert(i != j);
184 pa_sink_input_kill(i);
185 j = i;
186 }
187
188 if (s->monitor_source)
189 pa_source_disconnect(s->monitor_source);
190
191 sink_set_state(s, PA_SINK_DISCONNECTED);
192
193 s->get_latency = NULL;
194 s->get_volume = NULL;
195 s->set_volume = NULL;
196 s->set_mute = NULL;
197 s->get_mute = NULL;
198 s->set_state = NULL;
199
200 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
201 }
202
203 static void sink_free(pa_object *o) {
204 pa_sink *s = PA_SINK(o);
205 pa_sink_input *i;
206
207 pa_assert(s);
208 pa_assert(pa_sink_refcnt(s) == 0);
209
210 if (s->state != PA_SINK_DISCONNECTED)
211 pa_sink_disconnect(s);
212
213 pa_log_info("Freeing sink %u \"%s\"", s->index, s->name);
214
215 if (s->monitor_source) {
216 pa_source_unref(s->monitor_source);
217 s->monitor_source = NULL;
218 }
219
220 pa_idxset_free(s->inputs, NULL, NULL);
221
222 while ((i = pa_hashmap_steal_first(s->thread_info.inputs)))
223 pa_sink_input_unref(i);
224
225 pa_hashmap_free(s->thread_info.inputs, NULL, NULL);
226
227 if (s->silence)
228 pa_memblock_unref(s->silence);
229
230 pa_xfree(s->name);
231 pa_xfree(s->description);
232 pa_xfree(s->driver);
233 pa_xfree(s);
234 }
235
236 void pa_sink_set_asyncmsgq(pa_sink *s, pa_asyncmsgq *q) {
237 pa_sink_assert_ref(s);
238 pa_assert(q);
239
240 s->asyncmsgq = q;
241
242 if (s->monitor_source)
243 pa_source_set_asyncmsgq(s->monitor_source, q);
244 }
245
246 int pa_sink_update_status(pa_sink*s) {
247 pa_sink_assert_ref(s);
248
249 if (s->state == PA_SINK_SUSPENDED)
250 return 0;
251
252 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
253 }
254
255 int pa_sink_suspend(pa_sink *s, int suspend) {
256 pa_sink_assert_ref(s);
257
258 if (suspend)
259 return sink_set_state(s, PA_SINK_SUSPENDED);
260 else
261 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
262 }
263
264 void pa_sink_ping(pa_sink *s) {
265 pa_sink_assert_ref(s);
266
267 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_PING, NULL, NULL, NULL);
268 }
269
270 static unsigned fill_mix_info(pa_sink *s, pa_mix_info *info, unsigned maxinfo) {
271 pa_sink_input *i;
272 unsigned n = 0;
273 void *state = NULL;
274
275 pa_sink_assert_ref(s);
276 pa_assert(info);
277
278 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)) && maxinfo > 0) {
279 pa_sink_input_assert_ref(i);
280
281 if (pa_sink_input_peek(i, &info->chunk, &info->volume) < 0)
282 continue;
283
284 info->userdata = pa_sink_input_ref(i);
285
286 pa_assert(info->chunk.memblock);
287 pa_assert(info->chunk.length > 0);
288
289 info++;
290 n++;
291 maxinfo--;
292 }
293
294 return n;
295 }
296
297 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned n, size_t length) {
298 pa_sink_input *i;
299 void *state = NULL;
300 unsigned p = 0;
301 unsigned n_unreffed = 0;
302
303 pa_sink_assert_ref(s);
304
305 /* We optimize for the case where the order of the inputs has not changed */
306
307 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
308 unsigned j;
309 pa_mix_info* m;
310
311 pa_sink_input_assert_ref(i);
312
313 m = NULL;
314
315 /* Let's try to find the matching entry info the pa_mix_info array */
316 for (j = 0; j < n; j ++) {
317
318 if (info[p].userdata == i) {
319 m = info + p;
320 break;
321 }
322
323 p++;
324 if (p >= n)
325 p = 0;
326 }
327
328 /* Drop read data */
329 pa_sink_input_drop(i, m ? &m->chunk : NULL, length);
330
331 if (m) {
332 pa_sink_input_unref(m->userdata);
333 m->userdata = NULL;
334 if (m->chunk.memblock)
335 pa_memblock_unref(m->chunk.memblock);
336 pa_memchunk_reset(&m->chunk);
337
338 n_unreffed += 1;
339 }
340 }
341
342 /* Now drop references to entries that are included in the
343 * pa_mix_info array but don't exist anymore */
344
345 if (n_unreffed < n) {
346 for (; n > 0; info++, n--) {
347 if (info->userdata)
348 pa_sink_input_unref(info->userdata);
349 if (info->chunk.memblock)
350 pa_memblock_unref(info->chunk.memblock);
351 }
352 }
353 }
354
355 void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
356 pa_mix_info info[MAX_MIX_CHANNELS];
357 unsigned n;
358
359 pa_sink_assert_ref(s);
360 pa_assert(length);
361 pa_assert(result);
362
363 pa_sink_ref(s);
364
365 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, info, MAX_MIX_CHANNELS) : 0;
366
367 if (n == 0) {
368
369 if (length > SILENCE_BUFFER_LENGTH)
370 length = SILENCE_BUFFER_LENGTH;
371
372 if (!s->silence || pa_memblock_get_length(s->silence) < length) {
373 if (s->silence)
374 pa_memblock_unref(s->silence);
375 s->silence = pa_silence_memblock_new(s->core->mempool, &s->sample_spec, length);
376 }
377
378 result->memblock = pa_memblock_ref(s->silence);
379 result->length = length;
380 result->index = 0;
381
382 } else if (n == 1) {
383 pa_cvolume volume;
384
385 *result = info[0].chunk;
386 pa_memblock_ref(result->memblock);
387
388 if (result->length > length)
389 result->length = length;
390
391 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
392
393 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&volume)) {
394 pa_memchunk_make_writable(result, 0);
395 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
396 pa_silence_memchunk(result, &s->sample_spec);
397 else
398 pa_volume_memchunk(result, &s->sample_spec, &volume);
399 }
400 } else {
401 void *ptr;
402 result->memblock = pa_memblock_new(s->core->mempool, length);
403
404 ptr = pa_memblock_acquire(result->memblock);
405 result->length = pa_mix(info, n, ptr, length, &s->sample_spec, &s->thread_info.soft_volume, s->thread_info.soft_muted);
406 pa_memblock_release(result->memblock);
407
408 result->index = 0;
409 }
410
411 inputs_drop(s, info, n, result->length);
412
413 if (s->monitor_source)
414 pa_source_post(s->monitor_source, result);
415
416 pa_sink_unref(s);
417 }
418
419 void pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
420 pa_mix_info info[MAX_MIX_CHANNELS];
421 unsigned n;
422
423 pa_sink_assert_ref(s);
424 pa_assert(target);
425 pa_assert(target->memblock);
426 pa_assert(target->length);
427
428 pa_sink_ref(s);
429
430 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, info, MAX_MIX_CHANNELS) : 0;
431
432 if (n == 0) {
433 pa_silence_memchunk(target, &s->sample_spec);
434 } else if (n == 1) {
435 if (target->length > info[0].chunk.length)
436 target->length = info[0].chunk.length;
437
438 if (s->thread_info.soft_muted)
439 pa_silence_memchunk(target, &s->sample_spec);
440 else {
441 void *src, *ptr;
442 pa_cvolume volume;
443
444 ptr = pa_memblock_acquire(target->memblock);
445 src = pa_memblock_acquire(info[0].chunk.memblock);
446
447 memcpy((uint8_t*) ptr + target->index,
448 (uint8_t*) src + info[0].chunk.index,
449 target->length);
450
451 pa_memblock_release(target->memblock);
452 pa_memblock_release(info[0].chunk.memblock);
453
454 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
455
456 if (!pa_cvolume_is_norm(&volume))
457 pa_volume_memchunk(target, &s->sample_spec, &volume);
458 }
459
460 } else {
461 void *ptr;
462
463 ptr = pa_memblock_acquire(target->memblock);
464
465 target->length = pa_mix(info, n,
466 (uint8_t*) ptr + target->index,
467 target->length,
468 &s->sample_spec,
469 &s->thread_info.soft_volume,
470 s->thread_info.soft_muted);
471
472 pa_memblock_release(target->memblock);
473 }
474
475 inputs_drop(s, info, n, target->length);
476
477 if (s->monitor_source)
478 pa_source_post(s->monitor_source, target);
479
480 pa_sink_unref(s);
481 }
482
483 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
484 pa_memchunk chunk;
485 size_t l, d;
486
487 pa_sink_assert_ref(s);
488 pa_assert(target);
489 pa_assert(target->memblock);
490 pa_assert(target->length);
491
492 pa_sink_ref(s);
493
494 l = target->length;
495 d = 0;
496 while (l > 0) {
497 chunk = *target;
498 chunk.index += d;
499 chunk.length -= d;
500
501 pa_sink_render_into(s, &chunk);
502
503 d += chunk.length;
504 l -= chunk.length;
505 }
506
507 pa_sink_unref(s);
508 }
509
510 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
511 pa_sink_assert_ref(s);
512 pa_assert(length);
513 pa_assert(result);
514
515 /*** This needs optimization ***/
516
517 result->memblock = pa_memblock_new(s->core->mempool, result->length = length);
518 result->index = 0;
519
520 pa_sink_render_into_full(s, result);
521 }
522
523 pa_usec_t pa_sink_get_latency(pa_sink *s) {
524 pa_usec_t usec = 0;
525
526 pa_sink_assert_ref(s);
527
528 if (s->get_latency)
529 return s->get_latency(s);
530
531 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, NULL) < 0)
532 return 0;
533
534 return usec;
535 }
536
537 void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume) {
538 int changed;
539
540 pa_sink_assert_ref(s);
541 pa_assert(volume);
542
543 changed = !pa_cvolume_equal(volume, &s->volume);
544 s->volume = *volume;
545
546 if (s->set_volume && s->set_volume(s) < 0)
547 s->set_volume = NULL;
548
549 if (!s->set_volume)
550 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), NULL, pa_xfree);
551
552 if (changed)
553 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
554 }
555
556 const pa_cvolume *pa_sink_get_volume(pa_sink *s) {
557 struct pa_cvolume old_volume;
558
559 pa_sink_assert_ref(s);
560
561 old_volume = s->volume;
562
563 if (s->get_volume && s->get_volume(s) < 0)
564 s->get_volume = NULL;
565
566 if (!s->get_volume && s->refresh_volume)
567 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, &s->volume, NULL);
568
569 if (!pa_cvolume_equal(&old_volume, &s->volume))
570 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
571
572 return &s->volume;
573 }
574
575 void pa_sink_set_mute(pa_sink *s, int mute) {
576 int changed;
577
578 pa_sink_assert_ref(s);
579
580 changed = s->muted != mute;
581
582 if (s->set_mute && s->set_mute(s) < 0)
583 s->set_mute = NULL;
584
585 if (!s->set_mute)
586 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), NULL, NULL);
587
588 if (changed)
589 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
590 }
591
592 int pa_sink_get_mute(pa_sink *s) {
593 int old_muted;
594
595 pa_sink_assert_ref(s);
596
597 old_muted = s->muted;
598
599 if (s->get_mute && s->get_mute(s) < 0)
600 s->get_mute = NULL;
601
602 if (!s->get_mute && s->refresh_mute)
603 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MUTE, &s->muted, NULL);
604
605 if (old_muted != s->muted)
606 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
607
608 return s->muted;
609 }
610
611 void pa_sink_set_module(pa_sink *s, pa_module *m) {
612 pa_sink_assert_ref(s);
613
614 if (s->module == m)
615 return;
616
617 s->module = m;
618
619 if (s->monitor_source)
620 pa_source_set_module(s->monitor_source, m);
621
622 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
623 }
624
625 void pa_sink_set_description(pa_sink *s, const char *description) {
626 pa_sink_assert_ref(s);
627
628 if (!description && !s->description)
629 return;
630
631 if (description && s->description && !strcmp(description, s->description))
632 return;
633
634 pa_xfree(s->description);
635 s->description = pa_xstrdup(description);
636
637 if (s->monitor_source) {
638 char *n;
639
640 n = pa_sprintf_malloc("Monitor Source of %s", s->description? s->description : s->name);
641 pa_source_set_description(s->monitor_source, n);
642 pa_xfree(n);
643 }
644
645 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
646 }
647
648 unsigned pa_sink_used_by(pa_sink *s) {
649 unsigned ret;
650
651 pa_sink_assert_ref(s);
652
653 ret = pa_idxset_size(s->inputs);
654
655 if (s->monitor_source)
656 ret += pa_source_used_by(s->monitor_source);
657
658 return ret;
659 }
660
661 int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, pa_memchunk *chunk) {
662 pa_sink *s = PA_SINK(o);
663 pa_sink_assert_ref(s);
664
665 switch ((pa_sink_message_t) code) {
666 case PA_SINK_MESSAGE_ADD_INPUT: {
667 pa_sink_input *i = userdata;
668 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
669 return 0;
670 }
671
672 case PA_SINK_MESSAGE_REMOVE_INPUT: {
673 pa_sink_input *i = userdata;
674 pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index));
675 return 0;
676 }
677
678 case PA_SINK_MESSAGE_SET_VOLUME:
679 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
680 return 0;
681
682 case PA_SINK_MESSAGE_SET_MUTE:
683 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
684 return 0;
685
686 case PA_SINK_MESSAGE_GET_VOLUME:
687 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
688 return 0;
689
690 case PA_SINK_MESSAGE_GET_MUTE:
691 *((int*) userdata) = s->thread_info.soft_muted;
692 return 0;
693
694 case PA_SINK_MESSAGE_PING:
695 return 0;
696
697 case PA_SINK_MESSAGE_SET_STATE:
698 s->thread_info.state = PA_PTR_TO_UINT(userdata);
699 return 0;
700
701 case PA_SINK_MESSAGE_GET_LATENCY:
702 case PA_SINK_MESSAGE_MAX:
703 ;
704 }
705
706 return -1;
707 }