9588c2c3d72caee5119535ef7f507c18a529c69d
[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
45 #include "sink.h"
46
47 #define MAX_MIX_CHANNELS 32
48
49 #define CHECK_VALIDITY_RETURN_NULL(condition) \
50 do {\
51 if (!(condition)) \
52     return NULL; \
53 } while (0)
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     assert(core);
70     assert(name);
71     assert(spec);
72
73     CHECK_VALIDITY_RETURN_NULL(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     CHECK_VALIDITY_RETURN_NULL(map && pa_channel_map_valid(map));
79     CHECK_VALIDITY_RETURN_NULL(map->channels == spec->channels);
80     CHECK_VALIDITY_RETURN_NULL(!driver || pa_utf8_valid(driver));
81     CHECK_VALIDITY_RETURN_NULL(pa_utf8_valid(name) && *name);
82
83     s = pa_xnew(pa_sink, 1);
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->ref = 1;
91     s->core = core;
92     s->state = PA_SINK_RUNNING;
93     s->name = pa_xstrdup(name);
94     s->description = NULL;
95     s->driver = pa_xstrdup(driver);
96     s->owner = NULL;
97
98     s->sample_spec = *spec;
99     s->channel_map = *map;
100
101     s->inputs = pa_idxset_new(NULL, NULL);
102
103     pa_cvolume_reset(&s->sw_volume, spec->channels);
104     pa_cvolume_reset(&s->hw_volume, spec->channels);
105     s->sw_muted = 0;
106     s->hw_muted = 0;
107
108     s->is_hardware = 0;
109
110     s->get_latency = NULL;
111     s->notify = NULL;
112     s->set_hw_volume = NULL;
113     s->get_hw_volume = NULL;
114     s->set_hw_mute = NULL;
115     s->get_hw_mute = NULL;
116     s->userdata = NULL;
117
118     r = pa_idxset_put(core->sinks, s, &s->index);
119     assert(s->index != PA_IDXSET_INVALID && r >= 0);
120
121     pa_sample_spec_snprint(st, sizeof(st), spec);
122     pa_log_info("created %u \"%s\" with sample spec \"%s\"", s->index, s->name, st);
123
124     n = pa_sprintf_malloc("%s.monitor", name);
125
126     if (!(s->monitor_source = pa_source_new(core, driver, n, 0, spec, map)))
127         pa_log_warn("failed to create monitor source.");
128     else {
129         char *d;
130         s->monitor_source->monitor_of = s;
131         d = pa_sprintf_malloc("Monitor Source of %s", s->name);
132         pa_source_set_description(s->monitor_source, d);
133         pa_xfree(d);
134     }
135
136     pa_xfree(n);
137
138     pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
139
140     return s;
141 }
142
143 void pa_sink_disconnect(pa_sink* s) {
144     pa_sink_input *i, *j = NULL;
145
146     assert(s);
147     assert(s->state == PA_SINK_RUNNING);
148
149     s->state = PA_SINK_DISCONNECTED;
150     pa_namereg_unregister(s->core, s->name);
151
152     pa_hook_fire(&s->core->hook_sink_disconnect, s);
153
154     while ((i = pa_idxset_first(s->inputs, NULL))) {
155         assert(i != j);
156         pa_sink_input_kill(i);
157         j = i;
158     }
159
160     if (s->monitor_source)
161         pa_source_disconnect(s->monitor_source);
162
163     pa_idxset_remove_by_data(s->core->sinks, s, NULL);
164
165     s->get_latency = NULL;
166     s->notify = NULL;
167     s->get_hw_volume = NULL;
168     s->set_hw_volume = NULL;
169     s->set_hw_mute = NULL;
170     s->get_hw_mute = NULL;
171
172     pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
173 }
174
175 static void sink_free(pa_sink *s) {
176     assert(s);
177     assert(!s->ref);
178
179     if (s->state != PA_SINK_DISCONNECTED)
180         pa_sink_disconnect(s);
181
182     pa_log_info("freed %u \"%s\"", s->index, s->name);
183
184     if (s->monitor_source) {
185         pa_source_unref(s->monitor_source);
186         s->monitor_source = NULL;
187     }
188
189     pa_idxset_free(s->inputs, NULL, NULL);
190
191     pa_xfree(s->name);
192     pa_xfree(s->description);
193     pa_xfree(s->driver);
194     pa_xfree(s);
195 }
196
197 void pa_sink_unref(pa_sink*s) {
198     assert(s);
199     assert(s->ref >= 1);
200
201     if (!(--s->ref))
202         sink_free(s);
203 }
204
205 pa_sink* pa_sink_ref(pa_sink *s) {
206     assert(s);
207     assert(s->ref >= 1);
208
209     s->ref++;
210     return s;
211 }
212
213 void pa_sink_notify(pa_sink*s) {
214     assert(s);
215     assert(s->ref >= 1);
216
217     if (s->notify)
218         s->notify(s);
219 }
220
221 static unsigned fill_mix_info(pa_sink *s, pa_mix_info *info, unsigned maxinfo) {
222     uint32_t idx = PA_IDXSET_INVALID;
223     pa_sink_input *i;
224     unsigned n = 0;
225
226     assert(s);
227     assert(s->ref >= 1);
228     assert(info);
229
230     for (i = pa_idxset_first(s->inputs, &idx); maxinfo > 0 && i; i = pa_idxset_next(s->inputs, &idx)) {
231         /* Increase ref counter, to make sure that this input doesn't
232          * vanish while we still need it */
233         pa_sink_input_ref(i);
234
235         if (pa_sink_input_peek(i, &info->chunk, &info->volume) < 0) {
236             pa_sink_input_unref(i);
237             continue;
238         }
239
240         info->userdata = i;
241
242         assert(info->chunk.memblock);
243         assert(info->chunk.memblock->data);
244         assert(info->chunk.length);
245
246         info++;
247         maxinfo--;
248         n++;
249     }
250
251     return n;
252 }
253
254 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned maxinfo, size_t length) {
255     assert(s);
256     assert(s->ref >= 1);
257     assert(info);
258
259     for (; maxinfo > 0; maxinfo--, info++) {
260         pa_sink_input *i = info->userdata;
261
262         assert(i);
263         assert(info->chunk.memblock);
264
265         /* Drop read data */
266         pa_sink_input_drop(i, &info->chunk, length);
267         pa_memblock_unref(info->chunk.memblock);
268
269         /* Decrease ref counter */
270         pa_sink_input_unref(i);
271         info->userdata = NULL;
272     }
273 }
274
275 int pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
276     pa_mix_info info[MAX_MIX_CHANNELS];
277     unsigned n;
278     int r = -1;
279
280     assert(s);
281     assert(s->ref >= 1);
282     assert(length);
283     assert(result);
284
285     pa_sink_ref(s);
286
287     n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
288
289     if (n <= 0)
290         goto finish;
291
292     if (n == 1) {
293         pa_cvolume volume;
294
295         *result = info[0].chunk;
296         pa_memblock_ref(result->memblock);
297
298         if (result->length > length)
299             result->length = length;
300
301         pa_sw_cvolume_multiply(&volume, &s->sw_volume, &info[0].volume);
302
303         if (s->sw_muted || !pa_cvolume_is_norm(&volume)) {
304             pa_memchunk_make_writable(result, 0);
305             if (s->sw_muted)
306                 pa_silence_memchunk(result, &s->sample_spec);
307             else
308                 pa_volume_memchunk(result, &s->sample_spec, &volume);
309         }
310     } else {
311         result->memblock = pa_memblock_new(s->core->mempool, length);
312         assert(result->memblock);
313
314 /*          pa_log("mixing %i", n);  */
315
316         result->length = pa_mix(info, n, result->memblock->data, length,
317             &s->sample_spec, &s->sw_volume, s->sw_muted);
318         result->index = 0;
319     }
320
321     inputs_drop(s, info, n, result->length);
322
323     if (s->monitor_source)
324         pa_source_post(s->monitor_source, result);
325
326     r = 0;
327
328 finish:
329     pa_sink_unref(s);
330
331     return r;
332 }
333
334 int pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
335     pa_mix_info info[MAX_MIX_CHANNELS];
336     unsigned n;
337     int r = -1;
338
339     assert(s);
340     assert(s->ref >= 1);
341     assert(target);
342     assert(target->memblock);
343     assert(target->length);
344     assert(target->memblock->data);
345
346     pa_sink_ref(s);
347
348     n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
349
350     if (n <= 0)
351         goto finish;
352
353     if (n == 1) {
354         pa_cvolume volume;
355
356         if (target->length > info[0].chunk.length)
357             target->length = info[0].chunk.length;
358
359         memcpy((uint8_t*) target->memblock->data + target->index,
360                (uint8_t*) info[0].chunk.memblock->data + info[0].chunk.index,
361                target->length);
362
363         pa_sw_cvolume_multiply(&volume, &s->sw_volume, &info[0].volume);
364
365         if (s->sw_muted)
366             pa_silence_memchunk(target, &s->sample_spec);
367         else if (!pa_cvolume_is_norm(&volume))
368             pa_volume_memchunk(target, &s->sample_spec, &volume);
369     } else
370         target->length = pa_mix(info, n,
371                                 (uint8_t*) target->memblock->data + target->index,
372                                 target->length,
373                                 &s->sample_spec,
374                                 &s->sw_volume,
375                                 s->sw_muted);
376
377     inputs_drop(s, info, n, target->length);
378
379     if (s->monitor_source)
380         pa_source_post(s->monitor_source, target);
381
382     r = 0;
383
384 finish:
385     pa_sink_unref(s);
386
387     return r;
388 }
389
390 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
391     pa_memchunk chunk;
392     size_t l, d;
393
394     assert(s);
395     assert(s->ref >= 1);
396     assert(target);
397     assert(target->memblock);
398     assert(target->length);
399     assert(target->memblock->data);
400
401     pa_sink_ref(s);
402
403     l = target->length;
404     d = 0;
405     while (l > 0) {
406         chunk = *target;
407         chunk.index += d;
408         chunk.length -= d;
409
410         if (pa_sink_render_into(s, &chunk) < 0)
411             break;
412
413         d += chunk.length;
414         l -= chunk.length;
415     }
416
417     if (l > 0) {
418         chunk = *target;
419         chunk.index += d;
420         chunk.length -= d;
421         pa_silence_memchunk(&chunk, &s->sample_spec);
422     }
423
424     pa_sink_unref(s);
425 }
426
427 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
428     assert(s);
429     assert(s->ref >= 1);
430     assert(length);
431     assert(result);
432
433     /*** This needs optimization ***/
434
435     result->memblock = pa_memblock_new(s->core->mempool, result->length = length);
436     result->index = 0;
437
438     pa_sink_render_into_full(s, result);
439 }
440
441 pa_usec_t pa_sink_get_latency(pa_sink *s) {
442     assert(s);
443     assert(s->ref >= 1);
444
445     if (!s->get_latency)
446         return 0;
447
448     return s->get_latency(s);
449 }
450
451 void pa_sink_set_owner(pa_sink *s, pa_module *m) {
452     assert(s);
453     assert(s->ref >= 1);
454
455     if (s->owner == m)
456         return;
457
458     s->owner = m;
459
460     if (s->monitor_source)
461         pa_source_set_owner(s->monitor_source, m);
462
463     pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
464 }
465
466 void pa_sink_set_volume(pa_sink *s, pa_mixer_t m, const pa_cvolume *volume) {
467     pa_cvolume *v;
468
469     assert(s);
470     assert(s->ref >= 1);
471     assert(volume);
472
473     if (m == PA_MIXER_HARDWARE && s->set_hw_volume)
474         v = &s->hw_volume;
475     else
476         v = &s->sw_volume;
477
478     if (pa_cvolume_equal(v, volume))
479         return;
480
481     *v = *volume;
482
483     if (v == &s->hw_volume)
484         if (s->set_hw_volume(s) < 0)
485             s->sw_volume =  *volume;
486
487     pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
488 }
489
490 const pa_cvolume *pa_sink_get_volume(pa_sink *s, pa_mixer_t m) {
491     assert(s);
492     assert(s->ref >= 1);
493
494     if (m == PA_MIXER_HARDWARE && s->set_hw_volume) {
495
496         if (s->get_hw_volume)
497             s->get_hw_volume(s);
498
499         return &s->hw_volume;
500     } else
501         return &s->sw_volume;
502 }
503
504 void pa_sink_set_mute(pa_sink *s, pa_mixer_t m, int mute) {
505     int *t;
506
507     assert(s);
508     assert(s->ref >= 1);
509
510     if (m == PA_MIXER_HARDWARE && s->set_hw_mute)
511         t = &s->hw_muted;
512     else
513         t = &s->sw_muted;
514
515     if (!!*t == !!mute)
516         return;
517
518     *t = !!mute;
519
520     if (t == &s->hw_muted)
521         if (s->set_hw_mute(s) < 0)
522             s->sw_muted = !!mute;
523
524     pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
525 }
526
527 int pa_sink_get_mute(pa_sink *s, pa_mixer_t m) {
528     assert(s);
529     assert(s->ref >= 1);
530
531     if (m == PA_MIXER_HARDWARE && s->set_hw_mute) {
532
533         if (s->get_hw_mute)
534             s->get_hw_mute(s);
535
536         return s->hw_muted;
537     } else
538         return s->sw_muted;
539 }
540
541 void pa_sink_set_description(pa_sink *s, const char *description) {
542     assert(s);
543     assert(s->ref >= 1);
544
545     if (!description && !s->description)
546         return;
547
548     if (description && s->description && !strcmp(description, s->description))
549         return;
550
551     pa_xfree(s->description);
552     s->description = pa_xstrdup(description);
553
554     if (s->monitor_source) {
555         char *n;
556
557         n = pa_sprintf_malloc("Monitor Source of %s", s->description? s->description : s->name);
558         pa_source_set_description(s->monitor_source, n);
559         pa_xfree(n);
560     }
561
562     pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
563 }
564
565 unsigned pa_sink_used_by(pa_sink *s) {
566     unsigned ret;
567
568     assert(s);
569     assert(s->ref >= 1);
570
571     ret = pa_idxset_size(s->inputs);
572
573     if (s->monitor_source)
574         ret += pa_source_used_by(s->monitor_source);
575
576     return ret;
577 }