76edd27a0bb63242cf23b90fc71ea59efbb24dc2
[profile/ivi/pulseaudio-panda.git] / src / pulsecore / play-memblockq.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2006 Lennart Poettering
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as published
10   by the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/sink-input.h>
36 #include <pulsecore/gccmacro.h>
37
38 #include "play-memblockq.h"
39
40 static void sink_input_kill(pa_sink_input *i) {
41     pa_memblockq *q;
42     assert(i);
43     assert(i->userdata);
44
45     q = i->userdata;
46
47     pa_sink_input_disconnect(i);
48     pa_sink_input_unref(i);
49
50     pa_memblockq_free(q);
51 }
52
53 static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
54     pa_memblockq *q;
55     assert(i);
56     assert(chunk);
57     assert(i->userdata);
58
59     q = i->userdata;
60
61     return pa_memblockq_peek(q, chunk);
62 }
63
64 static void si_kill(PA_GCC_UNUSED pa_mainloop_api *m, void *i) {
65     sink_input_kill(i);
66 }
67
68 static void sink_input_drop(pa_sink_input *i, const pa_memchunk*chunk, size_t length) {
69     pa_memblockq *q;
70
71     assert(i);
72     assert(length > 0);
73     assert( i->userdata);
74
75     q = i->userdata;
76
77     pa_memblockq_drop(q, chunk, length);
78
79     if (pa_memblockq_get_length(q) <= 0)
80         pa_mainloop_api_once(i->sink->core->mainloop, si_kill, i);
81 }
82
83 int pa_play_memblockq(
84         pa_sink *sink,
85         const char *name,
86         const pa_sample_spec *ss,
87         const pa_channel_map *map,
88         pa_memblockq *q,
89         pa_cvolume *volume) {
90
91     pa_sink_input *si;
92     pa_sink_input_new_data data;
93
94     assert(sink);
95     assert(ss);
96     assert(q);
97
98     if (pa_memblockq_get_length(q) <= 0) {
99         pa_memblockq_free(q);
100         return 0;
101     }
102
103     if (volume && pa_cvolume_is_muted(volume)) {
104         pa_memblockq_free(q);
105         return 0;
106     }
107
108     pa_sink_input_new_data_init(&data);
109     data.sink = sink;
110     data.name = name;
111     data.driver = __FILE__;
112     pa_sink_input_new_data_set_channel_map(&data, map);
113     pa_sink_input_new_data_set_sample_spec(&data, ss);
114     pa_sink_input_new_data_set_volume(&data, volume);
115
116     if (!(si = pa_sink_input_new(sink->core, &data, 0)))
117         return -1;
118
119     si->peek = sink_input_peek;
120     si->drop = sink_input_drop;
121     si->kill = sink_input_kill;
122
123     si->userdata = q;
124
125     pa_sink_notify(si->sink);
126
127     return 0;
128 }