]> code.delx.au - pulseaudio/blob - src/polypcore/sink.c
Reorganised the source tree. We now have src/ with a couple of subdirs:
[pulseaudio] / src / polypcore / 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 #include <polyp/polyplib-introspect.h>
40
41 #define MAX_MIX_CHANNELS 32
42
43 pa_sink* pa_sink_new(
44 pa_core *core,
45 const char *driver,
46 const char *name,
47 int fail,
48 const pa_sample_spec *spec,
49 const pa_channel_map *map) {
50
51 pa_sink *s;
52 char *n = NULL;
53 char st[256];
54 int r;
55
56 assert(core);
57 assert(name);
58 assert(*name);
59 assert(spec);
60
61 s = pa_xnew(pa_sink, 1);
62
63 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SINK, s, fail))) {
64 pa_xfree(s);
65 return NULL;
66 }
67
68 s->ref = 1;
69 s->core = core;
70 s->state = PA_SINK_RUNNING;
71 s->name = pa_xstrdup(name);
72 s->description = NULL;
73 s->driver = pa_xstrdup(driver);
74 s->owner = NULL;
75
76 s->sample_spec = *spec;
77 if (map)
78 s->channel_map = *map;
79 else
80 pa_channel_map_init_auto(&s->channel_map, spec->channels);
81
82 s->inputs = pa_idxset_new(NULL, NULL);
83
84 pa_cvolume_reset(&s->sw_volume, spec->channels);
85 pa_cvolume_reset(&s->hw_volume, spec->channels);
86
87 s->get_latency = NULL;
88 s->notify = NULL;
89 s->set_hw_volume = NULL;
90 s->get_hw_volume = NULL;
91 s->userdata = NULL;
92
93 r = pa_idxset_put(core->sinks, s, &s->index);
94 assert(s->index != PA_IDXSET_INVALID && r >= 0);
95
96 pa_sample_spec_snprint(st, sizeof(st), spec);
97 pa_log_info(__FILE__": created %u \"%s\" with sample spec \"%s\"\n", s->index, s->name, st);
98
99 n = pa_sprintf_malloc("%s_monitor", name);
100 s->monitor_source = pa_source_new(core, driver, n, 0, spec, map);
101 assert(s->monitor_source);
102 pa_xfree(n);
103 s->monitor_source->monitor_of = s;
104 s->monitor_source->description = pa_sprintf_malloc("Monitor source of sink '%s'", s->name);
105
106 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
107
108 return s;
109 }
110
111 void pa_sink_disconnect(pa_sink* s) {
112 pa_sink_input *i, *j = NULL;
113
114 assert(s);
115 assert(s->state == PA_SINK_RUNNING);
116
117 pa_namereg_unregister(s->core, s->name);
118
119 while ((i = pa_idxset_first(s->inputs, NULL))) {
120 assert(i != j);
121 pa_sink_input_kill(i);
122 j = i;
123 }
124
125 pa_source_disconnect(s->monitor_source);
126
127 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
128
129 s->get_latency = NULL;
130 s->notify = NULL;
131 s->get_hw_volume = NULL;
132 s->set_hw_volume = NULL;
133
134 s->state = PA_SINK_DISCONNECTED;
135 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
136 }
137
138 static void sink_free(pa_sink *s) {
139 assert(s);
140 assert(!s->ref);
141
142 if (s->state != PA_SINK_DISCONNECTED)
143 pa_sink_disconnect(s);
144
145 pa_log_info(__FILE__": freed %u \"%s\"\n", s->index, s->name);
146
147 pa_source_unref(s->monitor_source);
148 s->monitor_source = NULL;
149
150 pa_idxset_free(s->inputs, NULL, NULL);
151
152 pa_xfree(s->name);
153 pa_xfree(s->description);
154 pa_xfree(s->driver);
155 pa_xfree(s);
156 }
157
158 void pa_sink_unref(pa_sink*s) {
159 assert(s);
160 assert(s->ref >= 1);
161
162 if (!(--s->ref))
163 sink_free(s);
164 }
165
166 pa_sink* pa_sink_ref(pa_sink *s) {
167 assert(s);
168 assert(s->ref >= 1);
169
170 s->ref++;
171 return s;
172 }
173
174 void pa_sink_notify(pa_sink*s) {
175 assert(s);
176 assert(s->ref >= 1);
177
178 if (s->notify)
179 s->notify(s);
180 }
181
182 static unsigned fill_mix_info(pa_sink *s, pa_mix_info *info, unsigned maxinfo) {
183 uint32_t idx = PA_IDXSET_INVALID;
184 pa_sink_input *i;
185 unsigned n = 0;
186
187 assert(s);
188 assert(s->ref >= 1);
189 assert(info);
190
191 for (i = pa_idxset_first(s->inputs, &idx); maxinfo > 0 && i; i = pa_idxset_next(s->inputs, &idx)) {
192 /* Increase ref counter, to make sure that this input doesn't
193 * vanish while we still need it */
194 pa_sink_input_ref(i);
195
196 if (pa_sink_input_peek(i, &info->chunk, &info->volume) < 0) {
197 pa_sink_input_unref(i);
198 continue;
199 }
200
201 info->userdata = i;
202
203 assert(info->chunk.memblock);
204 assert(info->chunk.memblock->data);
205 assert(info->chunk.length);
206
207 info++;
208 maxinfo--;
209 n++;
210 }
211
212 return n;
213 }
214
215 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned maxinfo, size_t length) {
216 assert(s);
217 assert(s->ref >= 1);
218 assert(info);
219
220 for (; maxinfo > 0; maxinfo--, info++) {
221 pa_sink_input *i = info->userdata;
222
223 assert(i);
224 assert(info->chunk.memblock);
225
226 /* Drop read data */
227 pa_sink_input_drop(i, &info->chunk, length);
228 pa_memblock_unref(info->chunk.memblock);
229
230 /* Decrease ref counter */
231 pa_sink_input_unref(i);
232 info->userdata = NULL;
233 }
234 }
235
236 int pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
237 pa_mix_info info[MAX_MIX_CHANNELS];
238 unsigned n;
239 int r = -1;
240
241 assert(s);
242 assert(s->ref >= 1);
243 assert(length);
244 assert(result);
245
246 pa_sink_ref(s);
247
248 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
249
250 if (n <= 0)
251 goto finish;
252
253 if (n == 1) {
254 pa_cvolume volume;
255
256 *result = info[0].chunk;
257 pa_memblock_ref(result->memblock);
258
259 if (result->length > length)
260 result->length = length;
261
262 pa_sw_cvolume_multiply(&volume, &s->sw_volume, &info[0].volume);
263
264 if (!pa_cvolume_is_norm(&volume)) {
265 pa_memchunk_make_writable(result, s->core->memblock_stat, 0);
266 pa_volume_memchunk(result, &s->sample_spec, &volume);
267 }
268 } else {
269 result->memblock = pa_memblock_new(length, s->core->memblock_stat);
270 assert(result->memblock);
271
272 result->length = pa_mix(info, n, result->memblock->data, length, &s->sample_spec, &s->sw_volume);
273 result->index = 0;
274 }
275
276 inputs_drop(s, info, n, result->length);
277 pa_source_post(s->monitor_source, result);
278
279 r = 0;
280
281 finish:
282 pa_sink_unref(s);
283
284 return r;
285 }
286
287 int pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
288 pa_mix_info info[MAX_MIX_CHANNELS];
289 unsigned n;
290 int r = -1;
291
292 assert(s);
293 assert(s->ref >= 1);
294 assert(target);
295 assert(target->memblock);
296 assert(target->length);
297 assert(target->memblock->data);
298
299 pa_sink_ref(s);
300
301 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
302
303 if (n <= 0)
304 goto finish;
305
306 if (n == 1) {
307 pa_cvolume volume;
308
309 if (target->length > info[0].chunk.length)
310 target->length = info[0].chunk.length;
311
312 memcpy((uint8_t*) target->memblock->data + target->index,
313 (uint8_t*) info[0].chunk.memblock->data + info[0].chunk.index,
314 target->length);
315
316 pa_sw_cvolume_multiply(&volume, &s->sw_volume, &info[0].volume);
317
318 if (!pa_cvolume_is_norm(&volume))
319 pa_volume_memchunk(target, &s->sample_spec, &volume);
320 } else
321 target->length = pa_mix(info, n,
322 (uint8_t*) target->memblock->data + target->index,
323 target->length,
324 &s->sample_spec,
325 &s->sw_volume);
326
327 inputs_drop(s, info, n, target->length);
328 pa_source_post(s->monitor_source, target);
329
330 r = 0;
331
332 finish:
333 pa_sink_unref(s);
334
335 return r;
336 }
337
338 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
339 pa_memchunk chunk;
340 size_t l, d;
341
342 assert(s);
343 assert(s->ref >= 1);
344 assert(target);
345 assert(target->memblock);
346 assert(target->length);
347 assert(target->memblock->data);
348
349 pa_sink_ref(s);
350
351 l = target->length;
352 d = 0;
353 while (l > 0) {
354 chunk = *target;
355 chunk.index += d;
356 chunk.length -= d;
357
358 if (pa_sink_render_into(s, &chunk) < 0)
359 break;
360
361 d += chunk.length;
362 l -= chunk.length;
363 }
364
365 if (l > 0) {
366 chunk = *target;
367 chunk.index += d;
368 chunk.length -= d;
369 pa_silence_memchunk(&chunk, &s->sample_spec);
370 }
371
372 pa_sink_unref(s);
373 }
374
375 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
376 assert(s);
377 assert(s->ref >= 1);
378 assert(length);
379 assert(result);
380
381 /*** This needs optimization ***/
382
383 result->memblock = pa_memblock_new(result->length = length, s->core->memblock_stat);
384 result->index = 0;
385
386 pa_sink_render_into_full(s, result);
387 }
388
389 pa_usec_t pa_sink_get_latency(pa_sink *s) {
390 assert(s);
391 assert(s->ref >= 1);
392
393 if (!s->get_latency)
394 return 0;
395
396 return s->get_latency(s);
397 }
398
399 void pa_sink_set_owner(pa_sink *s, pa_module *m) {
400 assert(s);
401 assert(s->ref >= 1);
402
403 s->owner = m;
404
405 if (s->monitor_source)
406 pa_source_set_owner(s->monitor_source, m);
407 }
408
409 void pa_sink_set_volume(pa_sink *s, pa_mixer_t m, const pa_cvolume *volume) {
410 pa_cvolume *v;
411
412 assert(s);
413 assert(s->ref >= 1);
414 assert(volume);
415
416 if (m == PA_MIXER_HARDWARE && s->set_hw_volume)
417 v = &s->hw_volume;
418 else
419 v = &s->sw_volume;
420
421 if (pa_cvolume_equal(v, volume))
422 return;
423
424 *v = *volume;
425
426 if (v == &s->hw_volume)
427 if (s->set_hw_volume(s) < 0)
428 s->sw_volume = *volume;
429
430 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
431 }
432
433 const pa_cvolume *pa_sink_get_volume(pa_sink *s, pa_mixer_t m) {
434 assert(s);
435 assert(s->ref >= 1);
436
437 if (m == PA_MIXER_HARDWARE && s->set_hw_volume) {
438
439 if (s->get_hw_volume)
440 s->get_hw_volume(s);
441
442 return &s->hw_volume;
443 } else
444 return &s->sw_volume;
445 }