Merge HUGE set of changes temporarily into a branch, to allow me to move them from...
[profile/ivi/pulseaudio-panda.git] / 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
50 static void sink_free(pa_object *s);
51
52 pa_sink* pa_sink_new(
53         pa_core *core,
54         const char *driver,
55         const char *name,
56         int fail,
57         const pa_sample_spec *spec,
58         const pa_channel_map *map) {
59
60     pa_sink *s;
61     char *n = NULL;
62     char st[256];
63     int r;
64     pa_channel_map tmap;
65
66     pa_assert(core);
67     pa_assert(name);
68     pa_assert(spec);
69
70     pa_return_null_if_fail(pa_sample_spec_valid(spec));
71
72     if (!map)
73         map = pa_channel_map_init_auto(&tmap, spec->channels, PA_CHANNEL_MAP_DEFAULT);
74
75     pa_return_null_if_fail(map && pa_channel_map_valid(map));
76     pa_return_null_if_fail(map->channels == spec->channels);
77     pa_return_null_if_fail(!driver || pa_utf8_valid(driver));
78     pa_return_null_if_fail(name && pa_utf8_valid(name) && *name);
79
80     s = pa_msgobject_new(pa_sink);
81
82     if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SINK, s, fail))) {
83         pa_xfree(s);
84         return NULL;
85     }
86
87     s->parent.parent.free = sink_free;
88     s->parent.process_msg = pa_sink_process_msg;
89     
90     s->core = core;
91     pa_atomic_store(&s->state, PA_SINK_IDLE);
92     s->name = pa_xstrdup(name);
93     s->description = NULL;
94     s->driver = pa_xstrdup(driver);
95     s->module = NULL;
96
97     s->sample_spec = *spec;
98     s->channel_map = *map;
99
100     s->inputs = pa_idxset_new(NULL, NULL);
101
102     pa_cvolume_reset(&s->volume, spec->channels);
103     s->muted = 0;
104     s->refresh_volume = s->refresh_mute = 0;
105
106     s->is_hardware = 0;
107
108     s->get_latency = NULL;
109     s->set_volume = NULL;
110     s->get_volume = NULL;
111     s->set_mute = NULL;
112     s->get_mute = NULL;
113     s->start = NULL;
114     s->stop = NULL;
115     s->userdata = NULL;
116
117     pa_assert_se(s->asyncmsgq = pa_asyncmsgq_new(0));
118     
119     r = pa_idxset_put(core->sinks, s, &s->index);
120     pa_assert(s->index != PA_IDXSET_INVALID && r >= 0);
121
122     pa_sample_spec_snprint(st, sizeof(st), spec);
123     pa_log_info("Created sink %u \"%s\" with sample spec \"%s\"", s->index, s->name, st);
124
125     n = pa_sprintf_malloc("%s.monitor", name);
126
127     if (!(s->monitor_source = pa_source_new(core, driver, n, 0, spec, map)))
128         pa_log_warn("failed to create monitor source.");
129     else {
130         char *d;
131         s->monitor_source->monitor_of = s;
132         d = pa_sprintf_malloc("Monitor Source of %s", s->name);
133         pa_source_set_description(s->monitor_source, d);
134         pa_xfree(d);
135     }
136
137     pa_xfree(n);
138
139     s->thread_info.inputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
140     s->thread_info.soft_volume = s->volume;
141     s->thread_info.soft_muted = s->muted;
142     
143     pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
144
145     return s;
146 }
147
148 static void sink_start(pa_sink *s) {
149     pa_sink_state_t state;
150     pa_assert(s);
151
152     state = pa_sink_get_state(s);
153     pa_return_if_fail(state == PA_SINK_IDLE || state == PA_SINK_SUSPENDED);
154
155     pa_atomic_store(&s->state, PA_SINK_RUNNING);
156
157     if (s->start)
158         s->start(s);
159     else
160         pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_START, NULL, NULL, NULL);
161 }
162
163 static void sink_stop(pa_sink *s) {
164     pa_sink_state_t state;
165     int stop;
166     
167     pa_assert(s);
168     state = pa_sink_get_state(s);
169     pa_return_if_fail(state == PA_SINK_RUNNING || state == PA_SINK_SUSPENDED);
170
171     stop = state == PA_SINK_RUNNING;
172     pa_atomic_store(&s->state, PA_SINK_IDLE);
173
174     if (stop) {
175         if (s->stop)
176             s->stop(s);
177         else
178             pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_STOP, NULL, NULL, NULL);
179     }
180 }
181
182 void pa_sink_disconnect(pa_sink* s) {
183     pa_sink_input *i, *j = NULL;
184
185     pa_assert(s);
186     pa_return_if_fail(pa_sink_get_state(s) != PA_SINK_DISCONNECTED);
187
188     sink_stop(s);
189
190     pa_atomic_store(&s->state, PA_SINK_DISCONNECTED);
191     pa_namereg_unregister(s->core, s->name);
192
193     pa_hook_fire(&s->core->hook_sink_disconnect, s);
194
195     while ((i = pa_idxset_first(s->inputs, NULL))) {
196         pa_assert(i != j);
197         pa_sink_input_kill(i);
198         j = i;
199     }
200
201     if (s->monitor_source)
202         pa_source_disconnect(s->monitor_source);
203
204     pa_idxset_remove_by_data(s->core->sinks, s, NULL);
205
206     s->get_latency = NULL;
207     s->get_volume = NULL;
208     s->set_volume = NULL;
209     s->set_mute = NULL;
210     s->get_mute = NULL;
211     s->start = NULL;
212     s->stop = NULL;
213
214     pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
215 }
216
217 static void sink_free(pa_object *o) {
218     pa_sink *s = PA_SINK(o);
219             
220     pa_assert(s);
221     pa_assert(pa_sink_refcnt(s) == 0);
222
223     pa_sink_disconnect(s);
224
225     pa_log_info("Freeing sink %u \"%s\"", s->index, s->name);
226
227     if (s->monitor_source) {
228         pa_source_unref(s->monitor_source);
229         s->monitor_source = NULL;
230     }
231
232     pa_idxset_free(s->inputs, NULL, NULL);
233     
234     pa_hashmap_free(s->thread_info.inputs, (pa_free2_cb_t) pa_sink_input_unref, NULL);
235
236     pa_asyncmsgq_free(s->asyncmsgq);
237     
238     pa_xfree(s->name);
239     pa_xfree(s->description);
240     pa_xfree(s->driver);
241     pa_xfree(s);
242 }
243
244 void pa_sink_update_status(pa_sink*s) {
245     pa_sink_assert_ref(s);
246
247     if (pa_sink_get_state(s) == PA_SINK_SUSPENDED)
248         return;
249     
250     if (pa_sink_used_by(s) > 0)
251         sink_start(s);
252     else
253         sink_stop(s);
254 }
255
256 void pa_sink_suspend(pa_sink *s, int suspend) {
257     pa_sink_state_t state;
258
259     pa_sink_assert_ref(s);
260
261     state = pa_sink_get_state(s);
262     pa_return_if_fail(suspend && (state == PA_SINK_RUNNING || state == PA_SINK_IDLE));
263     pa_return_if_fail(!suspend && (state == PA_SINK_SUSPENDED));
264
265
266     if (suspend) {
267         pa_atomic_store(&s->state, PA_SINK_SUSPENDED);
268
269         if (s->stop)
270             s->stop(s);
271         else
272             pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_STOP, NULL, NULL, NULL);
273         
274     } else {
275         pa_atomic_store(&s->state, PA_SINK_RUNNING);
276
277         if (s->start)
278             s->start(s);
279         else
280             pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_START, NULL, NULL, NULL);
281     }
282 }
283
284 static unsigned fill_mix_info(pa_sink *s, pa_mix_info *info, unsigned maxinfo) {
285     pa_sink_input *i;
286     unsigned n = 0;
287     void *state = NULL;
288
289     pa_sink_assert_ref(s);
290     pa_assert(info);
291
292     while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
293         /* Increase ref counter, to make sure that this input doesn't
294          * vanish while we still need it */
295         pa_sink_input_ref(i);
296
297         if (pa_sink_input_peek(i, &info->chunk, &info->volume) < 0) {
298             pa_sink_input_unref(i);
299             continue;
300         }
301
302         info->userdata = i;
303
304         pa_assert(info->chunk.memblock);
305         pa_assert(info->chunk.length);
306
307         info++;
308         maxinfo--;
309         n++;
310     }
311
312     return n;
313 }
314
315 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned maxinfo, size_t length) {
316     pa_sink_assert_ref(s);
317     pa_assert(info);
318
319     for (; maxinfo > 0; maxinfo--, info++) {
320         pa_sink_input *i = info->userdata;
321
322         pa_assert(i);
323         pa_assert(info->chunk.memblock);
324
325         /* Drop read data */
326         pa_sink_input_drop(i, &info->chunk, length);
327         pa_memblock_unref(info->chunk.memblock);
328
329         /* Decrease ref counter */
330         pa_sink_input_unref(i);
331         info->userdata = NULL;
332     }
333 }
334
335 int pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
336     pa_mix_info info[MAX_MIX_CHANNELS];
337     unsigned n;
338     int r = -1;
339
340     pa_sink_assert_ref(s);
341     pa_assert(length);
342     pa_assert(result);
343
344     pa_sink_ref(s);
345
346     n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
347
348     if (n <= 0)
349         goto finish;
350
351     if (n == 1) {
352         pa_cvolume volume;
353
354         *result = info[0].chunk;
355         pa_memblock_ref(result->memblock);
356
357         if (result->length > length)
358             result->length = length;
359
360         pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
361
362         if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&volume)) {
363             pa_memchunk_make_writable(result, 0);
364             if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
365                 pa_silence_memchunk(result, &s->sample_spec);
366             else
367                 pa_volume_memchunk(result, &s->sample_spec, &volume);
368         }
369     } else {
370         void *ptr;
371         result->memblock = pa_memblock_new(s->core->mempool, length);
372
373         ptr = pa_memblock_acquire(result->memblock);
374         result->length = pa_mix(info, n, ptr, length, &s->sample_spec, &s->thread_info.soft_volume, s->thread_info.soft_muted);
375         pa_memblock_release(result->memblock);
376
377         result->index = 0;
378     }
379
380     inputs_drop(s, info, n, result->length);
381
382     if (s->monitor_source)
383         pa_source_post(s->monitor_source, result);
384
385     r = 0;
386
387 finish:
388     pa_sink_unref(s);
389
390     return r;
391 }
392
393 int pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
394     pa_mix_info info[MAX_MIX_CHANNELS];
395     unsigned n;
396     int r = -1;
397
398     pa_sink_assert_ref(s);
399     pa_assert(target);
400     pa_assert(target->memblock);
401     pa_assert(target->length);
402
403     pa_sink_ref(s);
404
405     n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
406
407     if (n <= 0)
408         goto finish;
409
410
411     if (n == 1) {
412         if (target->length > info[0].chunk.length)
413             target->length = info[0].chunk.length;
414
415         if (s->thread_info.soft_muted)
416             pa_silence_memchunk(target, &s->sample_spec);
417         else {
418             void *src, *ptr;
419             pa_cvolume volume;
420             
421             ptr = pa_memblock_acquire(target->memblock);
422             src = pa_memblock_acquire(info[0].chunk.memblock);
423             
424             memcpy((uint8_t*) ptr + target->index,
425                    (uint8_t*) src + info[0].chunk.index,
426                    target->length);
427             
428             pa_memblock_release(target->memblock);
429             pa_memblock_release(info[0].chunk.memblock);
430             
431             pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
432
433             if (!pa_cvolume_is_norm(&volume))
434                 pa_volume_memchunk(target, &s->sample_spec, &volume);
435         }
436             
437     } else {
438         void *ptr;
439
440         ptr = pa_memblock_acquire(target->memblock);
441         
442         target->length = pa_mix(info, n,
443                                 (uint8_t*) ptr + target->index,
444                                 target->length,
445                                 &s->sample_spec,
446                                 &s->thread_info.soft_volume,
447                                 s->thread_info.soft_muted);
448     
449         pa_memblock_release(target->memblock);
450     }
451     
452     inputs_drop(s, info, n, target->length);
453
454     if (s->monitor_source)
455         pa_source_post(s->monitor_source, target);
456
457     r = 0;
458
459 finish:
460     pa_sink_unref(s);
461
462     return r;
463 }
464
465 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
466     pa_memchunk chunk;
467     size_t l, d;
468
469     pa_sink_assert_ref(s);
470     pa_assert(target);
471     pa_assert(target->memblock);
472     pa_assert(target->length);
473
474     pa_sink_ref(s);
475
476     l = target->length;
477     d = 0;
478     while (l > 0) {
479         chunk = *target;
480         chunk.index += d;
481         chunk.length -= d;
482
483         if (pa_sink_render_into(s, &chunk) < 0)
484             break;
485
486         d += chunk.length;
487         l -= chunk.length;
488     }
489
490     if (l > 0) {
491         chunk = *target;
492         chunk.index += d;
493         chunk.length -= d;
494         pa_silence_memchunk(&chunk, &s->sample_spec);
495     }
496
497     pa_sink_unref(s);
498 }
499
500 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
501     pa_sink_assert_ref(s);
502     pa_assert(length);
503     pa_assert(result);
504
505     /*** This needs optimization ***/
506
507     result->memblock = pa_memblock_new(s->core->mempool, result->length = length);
508     result->index = 0;
509
510     pa_sink_render_into_full(s, result);
511 }
512
513 pa_usec_t pa_sink_get_latency(pa_sink *s) {
514     pa_usec_t usec = 0;
515     
516     pa_sink_assert_ref(s);
517
518     if (s->get_latency)
519         return s->get_latency(s);
520     
521     if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, NULL) < 0)
522         return 0;
523
524     return usec;
525 }
526
527 void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume) {
528     int changed;
529
530     pa_sink_assert_ref(s);
531     pa_assert(volume);
532
533     changed = !pa_cvolume_equal(volume, &s->volume);
534     s->volume = *volume;
535     
536     if (s->set_volume && s->set_volume(s) < 0)
537         s->set_volume = NULL;
538
539     if (!s->set_volume)
540         pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), NULL, pa_xfree);
541
542     if (changed)
543         pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
544 }
545
546 const pa_cvolume *pa_sink_get_volume(pa_sink *s) {
547     struct pa_cvolume old_volume;
548
549     pa_sink_assert_ref(s);
550
551     old_volume = s->volume;
552     
553     if (s->get_volume && s->get_volume(s) < 0)
554         s->get_volume = NULL;
555
556     if (!s->get_volume && s->refresh_volume)
557         pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, &s->volume, NULL);
558
559     if (!pa_cvolume_equal(&old_volume, &s->volume))
560         pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
561     
562     return &s->volume;
563 }
564
565 void pa_sink_set_mute(pa_sink *s, int mute) {
566     int changed;
567     
568     pa_sink_assert_ref(s);
569
570     changed = s->muted != mute;
571
572     if (s->set_mute && s->set_mute(s) < 0)
573         s->set_mute = NULL;
574
575     if (!s->set_mute)
576         pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), NULL, NULL);
577
578     if (changed)
579         pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
580 }
581
582 int pa_sink_get_mute(pa_sink *s) {
583     int old_muted;
584     
585     pa_sink_assert_ref(s);
586
587     old_muted = s->muted;
588     
589     if (s->get_mute && s->get_mute(s) < 0)
590         s->get_mute = NULL;
591
592     if (!s->get_mute && s->refresh_mute)
593         pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MUTE, &s->muted, NULL);
594
595     if (old_muted != s->muted)
596         pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
597     
598     return s->muted;
599 }
600
601 void pa_sink_set_module(pa_sink *s, pa_module *m) {
602     pa_sink_assert_ref(s);
603
604     if (s->module == m)
605         return;
606
607     s->module = m;
608
609     if (s->monitor_source)
610         pa_source_set_module(s->monitor_source, m);
611
612     pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
613 }
614
615 void pa_sink_set_description(pa_sink *s, const char *description) {
616     pa_sink_assert_ref(s);
617
618     if (!description && !s->description)
619         return;
620
621     if (description && s->description && !strcmp(description, s->description))
622         return;
623
624     pa_xfree(s->description);
625     s->description = pa_xstrdup(description);
626
627     if (s->monitor_source) {
628         char *n;
629
630         n = pa_sprintf_malloc("Monitor Source of %s", s->description? s->description : s->name);
631         pa_source_set_description(s->monitor_source, n);
632         pa_xfree(n);
633     }
634
635     pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
636 }
637
638 unsigned pa_sink_used_by(pa_sink *s) {
639     unsigned ret;
640
641     pa_sink_assert_ref(s);
642
643     ret = pa_idxset_size(s->inputs);
644
645     if (s->monitor_source)
646         ret += pa_source_used_by(s->monitor_source);
647
648     return ret;
649 }
650
651 int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, pa_memchunk *chunk) {
652     pa_sink *s = PA_SINK(o);
653     pa_sink_assert_ref(s);
654
655     switch (code) {
656         case PA_SINK_MESSAGE_ADD_INPUT: {
657             pa_sink_input *i = userdata;
658             pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
659             return 0;
660         }
661             
662         case PA_SINK_MESSAGE_REMOVE_INPUT: {
663             pa_sink_input *i = userdata;
664             pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index));
665             return 0;
666         }
667             
668         case PA_SINK_MESSAGE_SET_VOLUME:
669             s->thread_info.soft_volume = *((pa_cvolume*) userdata);
670             return 0;
671             
672         case PA_SINK_MESSAGE_SET_MUTE:
673             s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
674             return 0;
675             
676         case PA_SINK_MESSAGE_GET_VOLUME:
677             *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
678             return 0;
679             
680         case PA_SINK_MESSAGE_GET_MUTE:
681             *((int*) userdata) = s->thread_info.soft_muted;
682             return 0;
683             
684         default:
685             return -1;
686     }
687 }