]> code.delx.au - pulseaudio/blob - polyp/sink.c
6d3b59c77b16d35ca4d034396431966d259119c7
[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->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 && s->ref >= 1 && length && result);
201
202 pa_sink_ref(s);
203
204 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
205
206 if (n <= 0)
207 goto finish;
208
209 if (n == 1) {
210 uint32_t volume = PA_VOLUME_NORM;
211 struct pa_sink_input *i = info[0].userdata;
212 assert(i);
213 *result = info[0].chunk;
214 pa_memblock_ref(result->memblock);
215
216 if (result->length > length)
217 result->length = length;
218
219 l = result->length;
220
221 if (s->volume != PA_VOLUME_NORM || info[0].volume != PA_VOLUME_NORM)
222 volume = pa_volume_multiply(s->volume, info[0].volume);
223
224 if (volume != PA_VOLUME_NORM) {
225 pa_memchunk_make_writable(result, s->core->memblock_stat);
226 pa_volume_memchunk(result, &s->sample_spec, volume);
227 }
228 } else {
229 result->memblock = pa_memblock_new(length, s->core->memblock_stat);
230 assert(result->memblock);
231
232 result->length = l = pa_mix(info, n, result->memblock->data, length, &s->sample_spec, s->volume);
233 result->index = 0;
234
235 assert(l);
236 }
237
238 inputs_drop(s, info, n, l);
239
240 assert(s->monitor_source);
241 pa_source_post(s->monitor_source, result);
242
243 r = 0;
244
245 finish:
246 pa_sink_unref(s);
247
248 return r;
249 }
250
251 int pa_sink_render_into(struct pa_sink*s, struct pa_memchunk *target) {
252 struct pa_mix_info info[MAX_MIX_CHANNELS];
253 unsigned n;
254 size_t l;
255 int r = -1;
256 assert(s && s->ref >= 1 && target && target->length && target->memblock && target->memblock->data);
257
258 pa_sink_ref(s);
259
260 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
261
262 if (n <= 0)
263 goto finish;
264
265 if (n == 1) {
266 uint32_t volume = PA_VOLUME_NORM;
267 struct pa_sink_info *i = info[0].userdata;
268 assert(i);
269
270 l = target->length;
271 if (l > info[0].chunk.length)
272 l = info[0].chunk.length;
273
274 memcpy((uint8_t*) target->memblock->data+target->index, (uint8_t*) info[0].chunk.memblock->data + info[0].chunk.index, l);
275 target->length = l;
276
277 if (s->volume != PA_VOLUME_NORM || info[0].volume != PA_VOLUME_NORM)
278 volume = pa_volume_multiply(s->volume, info[0].volume);
279
280 if (volume != PA_VOLUME_NORM)
281 pa_volume_memchunk(target, &s->sample_spec, volume);
282 } else
283 target->length = l = pa_mix(info, n, (uint8_t*) target->memblock->data+target->index, target->length, &s->sample_spec, s->volume);
284
285 assert(l);
286 inputs_drop(s, info, n, l);
287
288 assert(s->monitor_source);
289 pa_source_post(s->monitor_source, target);
290
291 r = 0;
292
293 finish:
294 pa_sink_unref(s);
295
296 return r;
297 }
298
299 void pa_sink_render_into_full(struct pa_sink *s, struct pa_memchunk *target) {
300 struct pa_memchunk chunk;
301 size_t l, d;
302 assert(s && s->ref >= 1 && target && target->memblock && target->length && target->memblock->data);
303
304 pa_sink_ref(s);
305
306 l = target->length;
307 d = 0;
308 while (l > 0) {
309 chunk = *target;
310 chunk.index += d;
311 chunk.length -= d;
312
313 if (pa_sink_render_into(s, &chunk) < 0)
314 break;
315
316 d += chunk.length;
317 l -= chunk.length;
318 }
319
320 if (l > 0) {
321 chunk = *target;
322 chunk.index += d;
323 chunk.length -= d;
324 pa_silence_memchunk(&chunk, &s->sample_spec);
325 }
326
327 pa_sink_unref(s);
328 }
329
330 void pa_sink_render_full(struct pa_sink *s, size_t length, struct pa_memchunk *result) {
331 assert(s && s->ref >= 1 && length && result);
332
333 /*** This needs optimization ***/
334
335 result->memblock = pa_memblock_new(result->length = length, s->core->memblock_stat);
336 result->index = 0;
337
338 pa_sink_render_into_full(s, result);
339 }
340
341 pa_usec_t pa_sink_get_latency(struct pa_sink *s) {
342 assert(s && s->ref >= 1);
343
344 if (!s->get_latency)
345 return 0;
346
347 return s->get_latency(s);
348 }
349
350 void pa_sink_set_owner(struct pa_sink *s, struct pa_module *m) {
351 assert(s && s->ref >= 1);
352
353 s->owner = m;
354
355 if (s->monitor_source)
356 pa_source_set_owner(s->monitor_source, m);
357 }
358
359 void pa_sink_set_volume(struct pa_sink *s, pa_volume_t volume) {
360 assert(s && s->ref >= 1);
361
362 if (s->volume != volume) {
363 s->volume = volume;
364 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
365 }
366 }