]> code.delx.au - pulseaudio/blob - polyp/sink.c
Make the whole stuff LGPL only
[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 Lesser 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 Lesser 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->ref = 1;
60 s->state = PA_SINK_RUNNING;
61
62 s->owner = NULL;
63 s->core = core;
64 s->sample_spec = *spec;
65 s->inputs = pa_idxset_new(NULL, NULL);
66
67 n = pa_sprintf_malloc("%s_monitor", name);
68 s->monitor_source = pa_source_new(core, n, 0, spec);
69 assert(s->monitor_source);
70 pa_xfree(n);
71 s->monitor_source->monitor_of = s;
72 s->monitor_source->description = pa_sprintf_malloc("Monitor source of sink '%s'", s->name);
73
74 s->volume = PA_VOLUME_NORM;
75
76 s->notify = NULL;
77 s->get_latency = NULL;
78 s->userdata = NULL;
79
80 r = pa_idxset_put(core->sinks, s, &s->index);
81 assert(s->index != PA_IDXSET_INVALID && r >= 0);
82
83 pa_sample_spec_snprint(st, sizeof(st), spec);
84 pa_log(__FILE__": created %u \"%s\" with sample spec \"%s\"\n", s->index, s->name, st);
85
86 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
87
88 return s;
89 }
90
91 void pa_sink_disconnect(struct pa_sink* s) {
92 struct pa_sink_input *i, *j = NULL;
93 assert(s && s->state == PA_SINK_RUNNING);
94
95 pa_namereg_unregister(s->core, s->name);
96
97 while ((i = pa_idxset_first(s->inputs, NULL))) {
98 assert(i != j);
99 pa_sink_input_kill(i);
100 j = i;
101 }
102
103 pa_source_disconnect(s->monitor_source);
104
105 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
106 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
107
108 s->notify = NULL;
109 s->get_latency = NULL;
110
111 s->state = PA_SINK_DISCONNECTED;
112 }
113
114 static void sink_free(struct pa_sink *s) {
115 assert(s && s->ref == 0);
116
117 if (s->state != PA_SINK_DISCONNECTED)
118 pa_sink_disconnect(s);
119
120 pa_log(__FILE__": freed %u \"%s\"\n", s->index, s->name);
121
122 pa_source_unref(s->monitor_source);
123 s->monitor_source = NULL;
124
125 pa_idxset_free(s->inputs, NULL, NULL);
126
127 pa_xfree(s->name);
128 pa_xfree(s->description);
129 pa_xfree(s);
130 }
131
132 void pa_sink_unref(struct pa_sink*s) {
133 assert(s && s->ref >= 1);
134
135 if (!(--s->ref))
136 sink_free(s);
137 }
138
139 struct pa_sink* pa_sink_ref(struct pa_sink *s) {
140 assert(s && s->ref >= 1);
141 s->ref++;
142 return s;
143 }
144
145 void pa_sink_notify(struct pa_sink*s) {
146 assert(s && s->ref >= 1);
147
148 if (s->notify)
149 s->notify(s);
150 }
151
152 static unsigned fill_mix_info(struct pa_sink *s, struct pa_mix_info *info, unsigned maxinfo) {
153 uint32_t index = PA_IDXSET_INVALID;
154 struct pa_sink_input *i;
155 unsigned n = 0;
156
157 assert(s && s->ref >= 1 && info);
158
159 for (i = pa_idxset_first(s->inputs, &index); maxinfo > 0 && i; i = pa_idxset_next(s->inputs, &index)) {
160 pa_sink_input_ref(i);
161
162 if (pa_sink_input_peek(i, &info->chunk) < 0) {
163 pa_sink_input_unref(i);
164 continue;
165 }
166
167 info->volume = i->volume;
168 info->userdata = i;
169
170 assert(info->chunk.memblock && info->chunk.memblock->data && info->chunk.length);
171
172 info++;
173 maxinfo--;
174 n++;
175 }
176
177 return n;
178 }
179
180 static void inputs_drop(struct pa_sink *s, struct pa_mix_info *info, unsigned maxinfo, size_t length) {
181 assert(s && s->ref >= 1 && info);
182
183 for (; maxinfo > 0; maxinfo--, info++) {
184 struct pa_sink_input *i = info->userdata;
185 assert(i && info->chunk.memblock);
186
187 pa_sink_input_drop(i, &info->chunk, length);
188 pa_memblock_unref(info->chunk.memblock);
189
190 pa_sink_input_unref(i);
191 info->userdata = NULL;
192 }
193 }
194
195 int pa_sink_render(struct pa_sink*s, size_t length, struct pa_memchunk *result) {
196 struct pa_mix_info info[MAX_MIX_CHANNELS];
197 unsigned n;
198 size_t l;
199 int r = -1;
200 assert(s);
201 assert(s->ref >= 1);
202 assert(length);
203 assert(result);
204
205 pa_sink_ref(s);
206
207 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
208
209 if (n <= 0)
210 goto finish;
211
212 if (n == 1) {
213 uint32_t volume = PA_VOLUME_NORM;
214 struct pa_sink_input *i = info[0].userdata;
215 assert(i);
216 *result = info[0].chunk;
217 pa_memblock_ref(result->memblock);
218
219 if (result->length > length)
220 result->length = length;
221
222 l = result->length;
223
224 if (s->volume != PA_VOLUME_NORM || info[0].volume != PA_VOLUME_NORM)
225 volume = pa_volume_multiply(s->volume, info[0].volume);
226
227 if (volume != PA_VOLUME_NORM) {
228 pa_memchunk_make_writable(result, s->core->memblock_stat);
229 pa_volume_memchunk(result, &s->sample_spec, volume);
230 }
231 } else {
232 result->memblock = pa_memblock_new(length, s->core->memblock_stat);
233 assert(result->memblock);
234
235 result->length = l = pa_mix(info, n, result->memblock->data, length, &s->sample_spec, s->volume);
236 result->index = 0;
237
238 assert(l);
239 }
240
241 inputs_drop(s, info, n, l);
242
243 assert(s->monitor_source);
244 pa_source_post(s->monitor_source, result);
245
246 r = 0;
247
248 finish:
249 pa_sink_unref(s);
250
251 return r;
252 }
253
254 int pa_sink_render_into(struct pa_sink*s, struct pa_memchunk *target) {
255 struct pa_mix_info info[MAX_MIX_CHANNELS];
256 unsigned n;
257 size_t l;
258 int r = -1;
259 assert(s && s->ref >= 1 && target && target->length && target->memblock && target->memblock->data);
260
261 pa_sink_ref(s);
262
263 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
264
265 if (n <= 0)
266 goto finish;
267
268 if (n == 1) {
269 uint32_t volume = PA_VOLUME_NORM;
270 struct pa_sink_info *i = info[0].userdata;
271 assert(i);
272
273 l = target->length;
274 if (l > info[0].chunk.length)
275 l = info[0].chunk.length;
276
277 memcpy((uint8_t*) target->memblock->data+target->index, (uint8_t*) info[0].chunk.memblock->data + info[0].chunk.index, l);
278 target->length = l;
279
280 if (s->volume != PA_VOLUME_NORM || info[0].volume != PA_VOLUME_NORM)
281 volume = pa_volume_multiply(s->volume, info[0].volume);
282
283 if (volume != PA_VOLUME_NORM)
284 pa_volume_memchunk(target, &s->sample_spec, volume);
285 } else
286 target->length = l = pa_mix(info, n, (uint8_t*) target->memblock->data+target->index, target->length, &s->sample_spec, s->volume);
287
288 assert(l);
289 inputs_drop(s, info, n, l);
290
291 assert(s->monitor_source);
292 pa_source_post(s->monitor_source, target);
293
294 r = 0;
295
296 finish:
297 pa_sink_unref(s);
298
299 return r;
300 }
301
302 void pa_sink_render_into_full(struct pa_sink *s, struct pa_memchunk *target) {
303 struct pa_memchunk chunk;
304 size_t l, d;
305 assert(s && s->ref >= 1 && target && target->memblock && target->length && target->memblock->data);
306
307 pa_sink_ref(s);
308
309 l = target->length;
310 d = 0;
311 while (l > 0) {
312 chunk = *target;
313 chunk.index += d;
314 chunk.length -= d;
315
316 if (pa_sink_render_into(s, &chunk) < 0)
317 break;
318
319 d += chunk.length;
320 l -= chunk.length;
321 }
322
323 if (l > 0) {
324 chunk = *target;
325 chunk.index += d;
326 chunk.length -= d;
327 pa_silence_memchunk(&chunk, &s->sample_spec);
328 }
329
330 pa_sink_unref(s);
331 }
332
333 void pa_sink_render_full(struct pa_sink *s, size_t length, struct pa_memchunk *result) {
334 assert(s && s->ref >= 1 && length && result);
335
336 /*** This needs optimization ***/
337
338 result->memblock = pa_memblock_new(result->length = length, s->core->memblock_stat);
339 result->index = 0;
340
341 pa_sink_render_into_full(s, result);
342 }
343
344 pa_usec_t pa_sink_get_latency(struct pa_sink *s) {
345 assert(s && s->ref >= 1);
346
347 if (!s->get_latency)
348 return 0;
349
350 return s->get_latency(s);
351 }
352
353 void pa_sink_set_owner(struct pa_sink *s, struct pa_module *m) {
354 assert(s && s->ref >= 1);
355
356 s->owner = m;
357
358 if (s->monitor_source)
359 pa_source_set_owner(s->monitor_source, m);
360 }
361
362 void pa_sink_set_volume(struct pa_sink *s, pa_volume_t volume) {
363 assert(s && s->ref >= 1);
364
365 if (s->volume != volume) {
366 s->volume = volume;
367 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
368 }
369 }