]> code.delx.au - pulseaudio/blob - src/pulsecore/sink.c
* introduce new functions pa_sink_set_description() and pa_source_set_description...
[pulseaudio] / src / pulsecore / sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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 <pulse/introspect.h>
32 #include <pulse/utf8.h>
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/sink-input.h>
36 #include <pulsecore/namereg.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/sample-util.h>
39 #include <pulsecore/core-subscribe.h>
40 #include <pulsecore/log.h>
41
42 #include "sink.h"
43
44 #define MAX_MIX_CHANNELS 32
45
46 #define CHECK_VALIDITY_RETURN_NULL(condition) \
47 do {\
48 if (!(condition)) \
49 return NULL; \
50 } while (0)
51
52 pa_sink* pa_sink_new(
53 pa_core *core,
54 const char *driver,
55 const char *name,
56 int fail,
57 const pa_sample_spec *spec,
58 const pa_channel_map *map) {
59
60 pa_sink *s;
61 char *n = NULL;
62 char st[256];
63 int r;
64 pa_channel_map tmap;
65
66 assert(core);
67 assert(name);
68 assert(spec);
69
70 CHECK_VALIDITY_RETURN_NULL(pa_sample_spec_valid(spec));
71
72 if (!map)
73 map = pa_channel_map_init_auto(&tmap, spec->channels, PA_CHANNEL_MAP_DEFAULT);
74
75 CHECK_VALIDITY_RETURN_NULL(map && pa_channel_map_valid(map));
76 CHECK_VALIDITY_RETURN_NULL(map->channels == spec->channels);
77 CHECK_VALIDITY_RETURN_NULL(!driver || pa_utf8_valid(driver));
78 CHECK_VALIDITY_RETURN_NULL(pa_utf8_valid(name) && *name);
79
80 s = pa_xnew(pa_sink, 1);
81
82 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SINK, s, fail))) {
83 pa_xfree(s);
84 return NULL;
85 }
86
87 s->ref = 1;
88 s->core = core;
89 s->state = PA_SINK_RUNNING;
90 s->name = pa_xstrdup(name);
91 s->description = NULL;
92 s->driver = pa_xstrdup(driver);
93 s->owner = NULL;
94
95 s->sample_spec = *spec;
96 s->channel_map = *map;
97
98 s->inputs = pa_idxset_new(NULL, NULL);
99
100 pa_cvolume_reset(&s->sw_volume, spec->channels);
101 pa_cvolume_reset(&s->hw_volume, spec->channels);
102 s->sw_muted = 0;
103 s->hw_muted = 0;
104
105 s->is_hardware = 0;
106
107 s->get_latency = NULL;
108 s->notify = NULL;
109 s->set_hw_volume = NULL;
110 s->get_hw_volume = NULL;
111 s->set_hw_mute = NULL;
112 s->get_hw_mute = NULL;
113 s->userdata = NULL;
114
115 r = pa_idxset_put(core->sinks, s, &s->index);
116 assert(s->index != PA_IDXSET_INVALID && r >= 0);
117
118 pa_sample_spec_snprint(st, sizeof(st), spec);
119 pa_log_info(__FILE__": created %u \"%s\" with sample spec \"%s\"", s->index, s->name, st);
120
121 n = pa_sprintf_malloc("%s.monitor", name);
122
123 if (!(s->monitor_source = pa_source_new(core, driver, n, 0, spec, map)))
124 pa_log_warn(__FILE__": failed to create monitor source.");
125 else {
126 char *d;
127 s->monitor_source->monitor_of = s;
128 d = pa_sprintf_malloc("Monitor Source of %s", s->name);
129 pa_source_set_description(s->monitor_source, d);
130 pa_xfree(d);
131 }
132
133 pa_xfree(n);
134
135 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
136
137 return s;
138 }
139
140 void pa_sink_disconnect(pa_sink* s) {
141 pa_sink_input *i, *j = NULL;
142
143 assert(s);
144 assert(s->state == PA_SINK_RUNNING);
145
146 pa_namereg_unregister(s->core, s->name);
147
148 while ((i = pa_idxset_first(s->inputs, NULL))) {
149 assert(i != j);
150 pa_sink_input_kill(i);
151 j = i;
152 }
153
154 if (s->monitor_source)
155 pa_source_disconnect(s->monitor_source);
156
157 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
158
159 s->get_latency = NULL;
160 s->notify = NULL;
161 s->get_hw_volume = NULL;
162 s->set_hw_volume = NULL;
163 s->set_hw_mute = NULL;
164 s->get_hw_mute = NULL;
165
166 s->state = PA_SINK_DISCONNECTED;
167 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
168 }
169
170 static void sink_free(pa_sink *s) {
171 assert(s);
172 assert(!s->ref);
173
174 if (s->state != PA_SINK_DISCONNECTED)
175 pa_sink_disconnect(s);
176
177 pa_log_info(__FILE__": freed %u \"%s\"", s->index, s->name);
178
179 if (s->monitor_source) {
180 pa_source_unref(s->monitor_source);
181 s->monitor_source = NULL;
182 }
183
184 pa_idxset_free(s->inputs, NULL, NULL);
185
186 pa_xfree(s->name);
187 pa_xfree(s->description);
188 pa_xfree(s->driver);
189 pa_xfree(s);
190 }
191
192 void pa_sink_unref(pa_sink*s) {
193 assert(s);
194 assert(s->ref >= 1);
195
196 if (!(--s->ref))
197 sink_free(s);
198 }
199
200 pa_sink* pa_sink_ref(pa_sink *s) {
201 assert(s);
202 assert(s->ref >= 1);
203
204 s->ref++;
205 return s;
206 }
207
208 void pa_sink_notify(pa_sink*s) {
209 assert(s);
210 assert(s->ref >= 1);
211
212 if (s->notify)
213 s->notify(s);
214 }
215
216 static unsigned fill_mix_info(pa_sink *s, pa_mix_info *info, unsigned maxinfo) {
217 uint32_t idx = PA_IDXSET_INVALID;
218 pa_sink_input *i;
219 unsigned n = 0;
220
221 assert(s);
222 assert(s->ref >= 1);
223 assert(info);
224
225 for (i = pa_idxset_first(s->inputs, &idx); maxinfo > 0 && i; i = pa_idxset_next(s->inputs, &idx)) {
226 /* Increase ref counter, to make sure that this input doesn't
227 * vanish while we still need it */
228 pa_sink_input_ref(i);
229
230 if (pa_sink_input_peek(i, &info->chunk, &info->volume) < 0) {
231 pa_sink_input_unref(i);
232 continue;
233 }
234
235 info->userdata = i;
236
237 assert(info->chunk.memblock);
238 assert(info->chunk.memblock->data);
239 assert(info->chunk.length);
240
241 info++;
242 maxinfo--;
243 n++;
244 }
245
246 return n;
247 }
248
249 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned maxinfo, size_t length) {
250 assert(s);
251 assert(s->ref >= 1);
252 assert(info);
253
254 for (; maxinfo > 0; maxinfo--, info++) {
255 pa_sink_input *i = info->userdata;
256
257 assert(i);
258 assert(info->chunk.memblock);
259
260 /* Drop read data */
261 pa_sink_input_drop(i, &info->chunk, length);
262 pa_memblock_unref(info->chunk.memblock);
263
264 /* Decrease ref counter */
265 pa_sink_input_unref(i);
266 info->userdata = NULL;
267 }
268 }
269
270 int pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
271 pa_mix_info info[MAX_MIX_CHANNELS];
272 unsigned n;
273 int r = -1;
274
275 assert(s);
276 assert(s->ref >= 1);
277 assert(length);
278 assert(result);
279
280 pa_sink_ref(s);
281
282 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
283
284 if (n <= 0)
285 goto finish;
286
287 if (n == 1) {
288 pa_cvolume volume;
289
290 *result = info[0].chunk;
291 pa_memblock_ref(result->memblock);
292
293 if (result->length > length)
294 result->length = length;
295
296 pa_sw_cvolume_multiply(&volume, &s->sw_volume, &info[0].volume);
297
298 if (s->sw_muted || !pa_cvolume_is_norm(&volume)) {
299 pa_memchunk_make_writable(result, s->core->memblock_stat, 0);
300 if (s->sw_muted)
301 pa_silence_memchunk(result, &s->sample_spec);
302 else
303 pa_volume_memchunk(result, &s->sample_spec, &volume);
304 }
305 } else {
306 result->memblock = pa_memblock_new(length, s->core->memblock_stat);
307 assert(result->memblock);
308
309 /* pa_log("mixing %i", n); */
310
311 result->length = pa_mix(info, n, result->memblock->data, length,
312 &s->sample_spec, &s->sw_volume, s->sw_muted);
313 result->index = 0;
314 }
315
316 inputs_drop(s, info, n, result->length);
317
318 if (s->monitor_source)
319 pa_source_post(s->monitor_source, result);
320
321 r = 0;
322
323 finish:
324 pa_sink_unref(s);
325
326 return r;
327 }
328
329 int pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
330 pa_mix_info info[MAX_MIX_CHANNELS];
331 unsigned n;
332 int r = -1;
333
334 assert(s);
335 assert(s->ref >= 1);
336 assert(target);
337 assert(target->memblock);
338 assert(target->length);
339 assert(target->memblock->data);
340
341 pa_sink_ref(s);
342
343 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
344
345 if (n <= 0)
346 goto finish;
347
348 if (n == 1) {
349 pa_cvolume volume;
350
351 if (target->length > info[0].chunk.length)
352 target->length = info[0].chunk.length;
353
354 memcpy((uint8_t*) target->memblock->data + target->index,
355 (uint8_t*) info[0].chunk.memblock->data + info[0].chunk.index,
356 target->length);
357
358 pa_sw_cvolume_multiply(&volume, &s->sw_volume, &info[0].volume);
359
360 if (s->sw_muted)
361 pa_silence_memchunk(target, &s->sample_spec);
362 else if (!pa_cvolume_is_norm(&volume))
363 pa_volume_memchunk(target, &s->sample_spec, &volume);
364 } else
365 target->length = pa_mix(info, n,
366 (uint8_t*) target->memblock->data + target->index,
367 target->length,
368 &s->sample_spec,
369 &s->sw_volume,
370 s->sw_muted);
371
372 inputs_drop(s, info, n, target->length);
373
374 if (s->monitor_source)
375 pa_source_post(s->monitor_source, target);
376
377 r = 0;
378
379 finish:
380 pa_sink_unref(s);
381
382 return r;
383 }
384
385 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
386 pa_memchunk chunk;
387 size_t l, d;
388
389 assert(s);
390 assert(s->ref >= 1);
391 assert(target);
392 assert(target->memblock);
393 assert(target->length);
394 assert(target->memblock->data);
395
396 pa_sink_ref(s);
397
398 l = target->length;
399 d = 0;
400 while (l > 0) {
401 chunk = *target;
402 chunk.index += d;
403 chunk.length -= d;
404
405 if (pa_sink_render_into(s, &chunk) < 0)
406 break;
407
408 d += chunk.length;
409 l -= chunk.length;
410 }
411
412 if (l > 0) {
413 chunk = *target;
414 chunk.index += d;
415 chunk.length -= d;
416 pa_silence_memchunk(&chunk, &s->sample_spec);
417 }
418
419 pa_sink_unref(s);
420 }
421
422 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
423 assert(s);
424 assert(s->ref >= 1);
425 assert(length);
426 assert(result);
427
428 /*** This needs optimization ***/
429
430 result->memblock = pa_memblock_new(result->length = length, s->core->memblock_stat);
431 result->index = 0;
432
433 pa_sink_render_into_full(s, result);
434 }
435
436 pa_usec_t pa_sink_get_latency(pa_sink *s) {
437 assert(s);
438 assert(s->ref >= 1);
439
440 if (!s->get_latency)
441 return 0;
442
443 return s->get_latency(s);
444 }
445
446 void pa_sink_set_owner(pa_sink *s, pa_module *m) {
447 assert(s);
448 assert(s->ref >= 1);
449
450 s->owner = m;
451
452 if (s->monitor_source)
453 pa_source_set_owner(s->monitor_source, m);
454 }
455
456 void pa_sink_set_volume(pa_sink *s, pa_mixer_t m, const pa_cvolume *volume) {
457 pa_cvolume *v;
458
459 assert(s);
460 assert(s->ref >= 1);
461 assert(volume);
462
463 if (m == PA_MIXER_HARDWARE && s->set_hw_volume)
464 v = &s->hw_volume;
465 else
466 v = &s->sw_volume;
467
468 if (pa_cvolume_equal(v, volume))
469 return;
470
471 *v = *volume;
472
473 if (v == &s->hw_volume)
474 if (s->set_hw_volume(s) < 0)
475 s->sw_volume = *volume;
476
477 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
478 }
479
480 const pa_cvolume *pa_sink_get_volume(pa_sink *s, pa_mixer_t m) {
481 assert(s);
482 assert(s->ref >= 1);
483
484 if (m == PA_MIXER_HARDWARE && s->set_hw_volume) {
485
486 if (s->get_hw_volume)
487 s->get_hw_volume(s);
488
489 return &s->hw_volume;
490 } else
491 return &s->sw_volume;
492 }
493
494 void pa_sink_set_mute(pa_sink *s, pa_mixer_t m, int mute) {
495 int *t;
496
497 assert(s);
498 assert(s->ref >= 1);
499
500 if (m == PA_MIXER_HARDWARE && s->set_hw_mute)
501 t = &s->hw_muted;
502 else
503 t = &s->sw_muted;
504
505 if (!!*t == !!mute)
506 return;
507
508 *t = !!mute;
509
510 if (t == &s->hw_muted)
511 if (s->set_hw_mute(s) < 0)
512 s->sw_muted = !!mute;
513
514 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
515 }
516
517 int pa_sink_get_mute(pa_sink *s, pa_mixer_t m) {
518 assert(s);
519 assert(s->ref >= 1);
520
521 if (m == PA_MIXER_HARDWARE && s->set_hw_mute) {
522
523 if (s->get_hw_mute)
524 s->get_hw_mute(s);
525
526 return s->hw_muted;
527 } else
528 return s->sw_muted;
529 }
530
531 void pa_sink_set_description(pa_sink *s, const char *description) {
532 assert(s);
533 assert(s->ref >= 1);
534
535 if (!description && !s->description)
536 return;
537
538 if (description && s->description && !strcmp(description, s->description))
539 return;
540
541 pa_xfree(s->description);
542 s->description = pa_xstrdup(description);
543
544 if (s->monitor_source) {
545 char *n;
546
547 n = pa_sprintf_malloc("Monitor Source of %s", s->description? s->description : s->name);
548 pa_source_set_description(s->monitor_source, n);
549 pa_xfree(n);
550 }
551
552 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
553 }