]> code.delx.au - pulseaudio/blob - src/pulsecore/sink.c
Make pa_sink_render_* and pa_source_post work only when in RUNNING state, to fix...
[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 if (++p > n)
324 p = 0;
325 }
326
327 /* Drop read data */
328 pa_sink_input_drop(i, m ? &m->chunk : NULL, length);
329
330 if (m) {
331 pa_sink_input_unref(m->userdata);
332 m->userdata = NULL;
333 if (m->chunk.memblock)
334 pa_memblock_unref(m->chunk.memblock);
335 pa_memchunk_reset(&m->chunk);
336
337 n_unreffed += 1;
338 }
339 }
340
341 /* Now drop references to entries that are included in the
342 * pa_mix_info array but don't exist anymore */
343
344 if (n_unreffed < n) {
345 for (; n > 0; info++, n--) {
346 if (info->userdata)
347 pa_sink_input_unref(info->userdata);
348 if (info->chunk.memblock)
349 pa_memblock_unref(info->chunk.memblock);
350 }
351 }
352 }
353
354 void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
355 pa_mix_info info[MAX_MIX_CHANNELS];
356 unsigned n;
357
358 pa_sink_assert_ref(s);
359 pa_assert(length);
360 pa_assert(result);
361
362 pa_sink_ref(s);
363
364 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, info, MAX_MIX_CHANNELS) : 0;
365
366 if (n == 0) {
367
368 if (length > SILENCE_BUFFER_LENGTH)
369 length = SILENCE_BUFFER_LENGTH;
370
371 if (!s->silence || pa_memblock_get_length(s->silence) < length) {
372 if (s->silence)
373 pa_memblock_unref(s->silence);
374 s->silence = pa_silence_memblock_new(s->core->mempool, &s->sample_spec, length);
375 }
376
377 result->memblock = pa_memblock_ref(s->silence);
378 result->length = length;
379 result->index = 0;
380
381 } else if (n == 1) {
382 pa_cvolume volume;
383
384 *result = info[0].chunk;
385 pa_memblock_ref(result->memblock);
386
387 if (result->length > length)
388 result->length = length;
389
390 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
391
392 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&volume)) {
393 pa_memchunk_make_writable(result, 0);
394 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
395 pa_silence_memchunk(result, &s->sample_spec);
396 else
397 pa_volume_memchunk(result, &s->sample_spec, &volume);
398 }
399 } else {
400 void *ptr;
401 result->memblock = pa_memblock_new(s->core->mempool, length);
402
403 ptr = pa_memblock_acquire(result->memblock);
404 result->length = pa_mix(info, n, ptr, length, &s->sample_spec, &s->thread_info.soft_volume, s->thread_info.soft_muted);
405 pa_memblock_release(result->memblock);
406
407 result->index = 0;
408 }
409
410 inputs_drop(s, info, n, result->length);
411
412 if (s->monitor_source)
413 pa_source_post(s->monitor_source, result);
414
415 pa_sink_unref(s);
416 }
417
418 void pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
419 pa_mix_info info[MAX_MIX_CHANNELS];
420 unsigned n;
421
422 pa_sink_assert_ref(s);
423 pa_assert(target);
424 pa_assert(target->memblock);
425 pa_assert(target->length);
426
427 pa_sink_ref(s);
428
429 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, info, MAX_MIX_CHANNELS) : 0;
430
431 if (n == 0) {
432 pa_silence_memchunk(target, &s->sample_spec);
433 } else if (n == 1) {
434 if (target->length > info[0].chunk.length)
435 target->length = info[0].chunk.length;
436
437 if (s->thread_info.soft_muted)
438 pa_silence_memchunk(target, &s->sample_spec);
439 else {
440 void *src, *ptr;
441 pa_cvolume volume;
442
443 ptr = pa_memblock_acquire(target->memblock);
444 src = pa_memblock_acquire(info[0].chunk.memblock);
445
446 memcpy((uint8_t*) ptr + target->index,
447 (uint8_t*) src + info[0].chunk.index,
448 target->length);
449
450 pa_memblock_release(target->memblock);
451 pa_memblock_release(info[0].chunk.memblock);
452
453 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
454
455 if (!pa_cvolume_is_norm(&volume))
456 pa_volume_memchunk(target, &s->sample_spec, &volume);
457 }
458
459 } else {
460 void *ptr;
461
462 ptr = pa_memblock_acquire(target->memblock);
463
464 target->length = pa_mix(info, n,
465 (uint8_t*) ptr + target->index,
466 target->length,
467 &s->sample_spec,
468 &s->thread_info.soft_volume,
469 s->thread_info.soft_muted);
470
471 pa_memblock_release(target->memblock);
472 }
473
474 inputs_drop(s, info, n, target->length);
475
476 if (s->monitor_source)
477 pa_source_post(s->monitor_source, target);
478
479 pa_sink_unref(s);
480 }
481
482 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
483 pa_memchunk chunk;
484 size_t l, d;
485
486 pa_sink_assert_ref(s);
487 pa_assert(target);
488 pa_assert(target->memblock);
489 pa_assert(target->length);
490
491 pa_sink_ref(s);
492
493 l = target->length;
494 d = 0;
495 while (l > 0) {
496 chunk = *target;
497 chunk.index += d;
498 chunk.length -= d;
499
500 pa_sink_render_into(s, &chunk);
501
502 d += chunk.length;
503 l -= chunk.length;
504 }
505
506 pa_sink_unref(s);
507 }
508
509 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
510 pa_sink_assert_ref(s);
511 pa_assert(length);
512 pa_assert(result);
513
514 /*** This needs optimization ***/
515
516 result->memblock = pa_memblock_new(s->core->mempool, result->length = length);
517 result->index = 0;
518
519 pa_sink_render_into_full(s, result);
520 }
521
522 pa_usec_t pa_sink_get_latency(pa_sink *s) {
523 pa_usec_t usec = 0;
524
525 pa_sink_assert_ref(s);
526
527 if (s->get_latency)
528 return s->get_latency(s);
529
530 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, NULL) < 0)
531 return 0;
532
533 return usec;
534 }
535
536 void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume) {
537 int changed;
538
539 pa_sink_assert_ref(s);
540 pa_assert(volume);
541
542 changed = !pa_cvolume_equal(volume, &s->volume);
543 s->volume = *volume;
544
545 if (s->set_volume && s->set_volume(s) < 0)
546 s->set_volume = NULL;
547
548 if (!s->set_volume)
549 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), NULL, pa_xfree);
550
551 if (changed)
552 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
553 }
554
555 const pa_cvolume *pa_sink_get_volume(pa_sink *s) {
556 struct pa_cvolume old_volume;
557
558 pa_sink_assert_ref(s);
559
560 old_volume = s->volume;
561
562 if (s->get_volume && s->get_volume(s) < 0)
563 s->get_volume = NULL;
564
565 if (!s->get_volume && s->refresh_volume)
566 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, &s->volume, NULL);
567
568 if (!pa_cvolume_equal(&old_volume, &s->volume))
569 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
570
571 return &s->volume;
572 }
573
574 void pa_sink_set_mute(pa_sink *s, int mute) {
575 int changed;
576
577 pa_sink_assert_ref(s);
578
579 changed = s->muted != mute;
580
581 if (s->set_mute && s->set_mute(s) < 0)
582 s->set_mute = NULL;
583
584 if (!s->set_mute)
585 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), NULL, NULL);
586
587 if (changed)
588 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
589 }
590
591 int pa_sink_get_mute(pa_sink *s) {
592 int old_muted;
593
594 pa_sink_assert_ref(s);
595
596 old_muted = s->muted;
597
598 if (s->get_mute && s->get_mute(s) < 0)
599 s->get_mute = NULL;
600
601 if (!s->get_mute && s->refresh_mute)
602 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MUTE, &s->muted, NULL);
603
604 if (old_muted != s->muted)
605 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
606
607 return s->muted;
608 }
609
610 void pa_sink_set_module(pa_sink *s, pa_module *m) {
611 pa_sink_assert_ref(s);
612
613 if (s->module == m)
614 return;
615
616 s->module = m;
617
618 if (s->monitor_source)
619 pa_source_set_module(s->monitor_source, m);
620
621 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
622 }
623
624 void pa_sink_set_description(pa_sink *s, const char *description) {
625 pa_sink_assert_ref(s);
626
627 if (!description && !s->description)
628 return;
629
630 if (description && s->description && !strcmp(description, s->description))
631 return;
632
633 pa_xfree(s->description);
634 s->description = pa_xstrdup(description);
635
636 if (s->monitor_source) {
637 char *n;
638
639 n = pa_sprintf_malloc("Monitor Source of %s", s->description? s->description : s->name);
640 pa_source_set_description(s->monitor_source, n);
641 pa_xfree(n);
642 }
643
644 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
645 }
646
647 unsigned pa_sink_used_by(pa_sink *s) {
648 unsigned ret;
649
650 pa_sink_assert_ref(s);
651
652 ret = pa_idxset_size(s->inputs);
653
654 if (s->monitor_source)
655 ret += pa_source_used_by(s->monitor_source);
656
657 return ret;
658 }
659
660 int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, pa_memchunk *chunk) {
661 pa_sink *s = PA_SINK(o);
662 pa_sink_assert_ref(s);
663
664 switch ((pa_sink_message_t) code) {
665 case PA_SINK_MESSAGE_ADD_INPUT: {
666 pa_sink_input *i = userdata;
667 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
668 return 0;
669 }
670
671 case PA_SINK_MESSAGE_REMOVE_INPUT: {
672 pa_sink_input *i = userdata;
673 pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index));
674 return 0;
675 }
676
677 case PA_SINK_MESSAGE_SET_VOLUME:
678 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
679 return 0;
680
681 case PA_SINK_MESSAGE_SET_MUTE:
682 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
683 return 0;
684
685 case PA_SINK_MESSAGE_GET_VOLUME:
686 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
687 return 0;
688
689 case PA_SINK_MESSAGE_GET_MUTE:
690 *((int*) userdata) = s->thread_info.soft_muted;
691 return 0;
692
693 case PA_SINK_MESSAGE_PING:
694 return 0;
695
696 case PA_SINK_MESSAGE_SET_STATE:
697 s->thread_info.state = PA_PTR_TO_UINT(userdata);
698 return 0;
699
700 case PA_SINK_MESSAGE_GET_LATENCY:
701 case PA_SINK_MESSAGE_MAX:
702 ;
703 }
704
705 return -1;
706 }