]> code.delx.au - pulseaudio/blob - polyp/sink.c
implement proper logging
[pulseaudio] / polyp / sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <stdio.h>
30
31 #include "sink.h"
32 #include "sink-input.h"
33 #include "namereg.h"
34 #include "util.h"
35 #include "sample-util.h"
36 #include "xmalloc.h"
37 #include "subscribe.h"
38 #include "log.h"
39
40 #define MAX_MIX_CHANNELS 32
41
42 struct pa_sink* pa_sink_new(struct pa_core *core, const char *name, int fail, const struct pa_sample_spec *spec) {
43 struct pa_sink *s;
44 char *n = NULL;
45 char st[256];
46 int r;
47 assert(core && name && *name && spec);
48
49 s = pa_xmalloc(sizeof(struct pa_sink));
50
51 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SINK, s, fail))) {
52 pa_xfree(s);
53 return NULL;
54 }
55
56 s->name = pa_xstrdup(name);
57 s->description = NULL;
58
59 s->owner = NULL;
60 s->core = core;
61 s->sample_spec = *spec;
62 s->inputs = pa_idxset_new(NULL, NULL);
63
64 n = pa_sprintf_malloc("%s_monitor", name);
65 s->monitor_source = pa_source_new(core, n, 0, spec);
66 assert(s->monitor_source);
67 pa_xfree(n);
68 s->monitor_source->monitor_of = s;
69 s->monitor_source->description = pa_sprintf_malloc("Monitor source of sink '%s'", s->name);
70
71 s->volume = PA_VOLUME_NORM;
72
73 s->notify = NULL;
74 s->get_latency = NULL;
75 s->userdata = NULL;
76
77 r = pa_idxset_put(core->sinks, s, &s->index);
78 assert(s->index != PA_IDXSET_INVALID && r >= 0);
79
80 pa_sample_spec_snprint(st, sizeof(st), spec);
81 pa_log(__FILE__": created %u \"%s\" with sample spec \"%s\"\n", s->index, s->name, st);
82
83 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
84
85 return s;
86 }
87
88 void pa_sink_free(struct pa_sink *s) {
89 struct pa_sink_input *i, *j = NULL;
90 assert(s);
91
92 pa_namereg_unregister(s->core, s->name);
93
94 while ((i = pa_idxset_first(s->inputs, NULL))) {
95 assert(i != j);
96 pa_sink_input_kill(i);
97 j = i;
98 }
99 pa_idxset_free(s->inputs, NULL, NULL);
100
101 pa_source_free(s->monitor_source);
102 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
103
104 pa_log(__FILE__": freed %u \"%s\"\n", s->index, s->name);
105
106 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
107
108 pa_xfree(s->name);
109 pa_xfree(s->description);
110 pa_xfree(s);
111 }
112
113 void pa_sink_notify(struct pa_sink*s) {
114 assert(s);
115
116 if (s->notify)
117 s->notify(s);
118 }
119
120 static unsigned fill_mix_info(struct pa_sink *s, struct pa_mix_info *info, unsigned maxinfo) {
121 uint32_t index = PA_IDXSET_INVALID;
122 struct pa_sink_input *i;
123 unsigned n = 0;
124
125 assert(s && info);
126
127 for (i = pa_idxset_first(s->inputs, &index); maxinfo > 0 && i; i = pa_idxset_next(s->inputs, &index)) {
128 if (pa_sink_input_peek(i, &info->chunk) < 0)
129 continue;
130
131 info->volume = i->volume;
132
133 assert(info->chunk.memblock && info->chunk.memblock->data && info->chunk.length);
134 info->userdata = i;
135
136 info++;
137 maxinfo--;
138 n++;
139 }
140
141 return n;
142 }
143
144 static void inputs_drop(struct pa_sink *s, struct pa_mix_info *info, unsigned maxinfo, size_t length) {
145 assert(s && info);
146
147 for (; maxinfo > 0; maxinfo--, info++) {
148 struct pa_sink_input *i = info->userdata;
149 assert(i && info->chunk.memblock);
150
151 pa_sink_input_drop(i, &info->chunk, length);
152 pa_memblock_unref(info->chunk.memblock);
153 }
154 }
155
156 int pa_sink_render(struct pa_sink*s, size_t length, struct pa_memchunk *result) {
157 struct pa_mix_info info[MAX_MIX_CHANNELS];
158 unsigned n;
159 size_t l;
160 assert(s && length && result);
161
162 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
163
164 if (n <= 0)
165 return -1;
166
167 if (n == 1) {
168 uint32_t volume = PA_VOLUME_NORM;
169 struct pa_sink_input *i = info[0].userdata;
170 assert(i);
171 *result = info[0].chunk;
172 pa_memblock_ref(result->memblock);
173
174 if (result->length > length)
175 result->length = length;
176
177 l = result->length;
178
179 if (s->volume != PA_VOLUME_NORM || info[0].volume != PA_VOLUME_NORM)
180 volume = pa_volume_multiply(s->volume, info[0].volume);
181
182 if (volume != PA_VOLUME_NORM) {
183 pa_memchunk_make_writable(result, s->core->memblock_stat);
184 pa_volume_memchunk(result, &s->sample_spec, volume);
185 }
186 } else {
187 result->memblock = pa_memblock_new(length, s->core->memblock_stat);
188 assert(result->memblock);
189
190 result->length = l = pa_mix(info, n, result->memblock->data, length, &s->sample_spec, s->volume);
191 result->index = 0;
192
193 assert(l);
194 }
195
196 inputs_drop(s, info, n, l);
197
198 assert(s->monitor_source);
199 pa_source_post(s->monitor_source, result);
200
201 return 0;
202 }
203
204 int pa_sink_render_into(struct pa_sink*s, struct pa_memchunk *target) {
205 struct pa_mix_info info[MAX_MIX_CHANNELS];
206 unsigned n;
207 size_t l;
208 assert(s && target && target->length && target->memblock && target->memblock->data);
209
210 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
211
212 if (n <= 0)
213 return -1;
214
215 if (n == 1) {
216 uint32_t volume = PA_VOLUME_NORM;
217 struct pa_sink_info *i = info[0].userdata;
218 assert(i);
219
220 l = target->length;
221 if (l > info[0].chunk.length)
222 l = info[0].chunk.length;
223
224 memcpy((uint8_t*) target->memblock->data+target->index, (uint8_t*) info[0].chunk.memblock->data + info[0].chunk.index, l);
225 target->length = l;
226
227 if (s->volume != PA_VOLUME_NORM || info[0].volume != PA_VOLUME_NORM)
228 volume = pa_volume_multiply(s->volume, info[0].volume);
229
230 if (volume != PA_VOLUME_NORM)
231 pa_volume_memchunk(target, &s->sample_spec, volume);
232 } else
233 target->length = l = pa_mix(info, n, (uint8_t*) target->memblock->data+target->index, target->length, &s->sample_spec, s->volume);
234
235 assert(l);
236 inputs_drop(s, info, n, l);
237
238 assert(s->monitor_source);
239 pa_source_post(s->monitor_source, target);
240
241 return 0;
242 }
243
244 void pa_sink_render_into_full(struct pa_sink *s, struct pa_memchunk *target) {
245 struct pa_memchunk chunk;
246 size_t l, d;
247 assert(s && target && target->memblock && target->length && target->memblock->data);
248
249 l = target->length;
250 d = 0;
251 while (l > 0) {
252 chunk = *target;
253 chunk.index += d;
254 chunk.length -= d;
255
256 if (pa_sink_render_into(s, &chunk) < 0)
257 break;
258
259 d += chunk.length;
260 l -= chunk.length;
261 }
262
263 if (l > 0) {
264 chunk = *target;
265 chunk.index += d;
266 chunk.length -= d;
267 pa_silence_memchunk(&chunk, &s->sample_spec);
268 }
269 }
270
271 void pa_sink_render_full(struct pa_sink *s, size_t length, struct pa_memchunk *result) {
272 assert(s && length && result);
273
274 /*** This needs optimization ***/
275
276 result->memblock = pa_memblock_new(result->length = length, s->core->memblock_stat);
277 result->index = 0;
278
279 pa_sink_render_into_full(s, result);
280 }
281
282 pa_usec_t pa_sink_get_latency(struct pa_sink *s) {
283 assert(s);
284
285 if (!s->get_latency)
286 return 0;
287
288 return s->get_latency(s);
289 }
290
291 void pa_sink_set_owner(struct pa_sink *sink, struct pa_module *m) {
292 sink->owner = m;
293
294 if (sink->monitor_source)
295 pa_source_set_owner(sink->monitor_source, m);
296 }
297
298 void pa_sink_set_volume(struct pa_sink *sink, pa_volume_t volume) {
299 assert(sink);
300
301 if (sink->volume != volume) {
302 sink->volume = volume;
303 pa_subscription_post(sink->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, sink->index);
304 }
305 }