]> code.delx.au - pulseaudio/blob - src/pulsecore/sink.c
properly implement a pa_sink_disconnect() hook
[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 s->state = PA_SINK_DISCONNECTED;
147 pa_namereg_unregister(s->core, s->name);
148
149 pa_hook_fire(&s->core->hook_sink_disconnect, s);
150
151 while ((i = pa_idxset_first(s->inputs, NULL))) {
152 assert(i != j);
153 pa_sink_input_kill(i);
154 j = i;
155 }
156
157 if (s->monitor_source)
158 pa_source_disconnect(s->monitor_source);
159
160 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
161
162 s->get_latency = NULL;
163 s->notify = NULL;
164 s->get_hw_volume = NULL;
165 s->set_hw_volume = NULL;
166 s->set_hw_mute = NULL;
167 s->get_hw_mute = NULL;
168
169 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
170 }
171
172 static void sink_free(pa_sink *s) {
173 assert(s);
174 assert(!s->ref);
175
176 if (s->state != PA_SINK_DISCONNECTED)
177 pa_sink_disconnect(s);
178
179 pa_log_info(__FILE__": freed %u \"%s\"", s->index, s->name);
180
181 if (s->monitor_source) {
182 pa_source_unref(s->monitor_source);
183 s->monitor_source = NULL;
184 }
185
186 pa_idxset_free(s->inputs, NULL, NULL);
187
188 pa_xfree(s->name);
189 pa_xfree(s->description);
190 pa_xfree(s->driver);
191 pa_xfree(s);
192 }
193
194 void pa_sink_unref(pa_sink*s) {
195 assert(s);
196 assert(s->ref >= 1);
197
198 if (!(--s->ref))
199 sink_free(s);
200 }
201
202 pa_sink* pa_sink_ref(pa_sink *s) {
203 assert(s);
204 assert(s->ref >= 1);
205
206 s->ref++;
207 return s;
208 }
209
210 void pa_sink_notify(pa_sink*s) {
211 assert(s);
212 assert(s->ref >= 1);
213
214 if (s->notify)
215 s->notify(s);
216 }
217
218 static unsigned fill_mix_info(pa_sink *s, pa_mix_info *info, unsigned maxinfo) {
219 uint32_t idx = PA_IDXSET_INVALID;
220 pa_sink_input *i;
221 unsigned n = 0;
222
223 assert(s);
224 assert(s->ref >= 1);
225 assert(info);
226
227 for (i = pa_idxset_first(s->inputs, &idx); maxinfo > 0 && i; i = pa_idxset_next(s->inputs, &idx)) {
228 /* Increase ref counter, to make sure that this input doesn't
229 * vanish while we still need it */
230 pa_sink_input_ref(i);
231
232 if (pa_sink_input_peek(i, &info->chunk, &info->volume) < 0) {
233 pa_sink_input_unref(i);
234 continue;
235 }
236
237 info->userdata = i;
238
239 assert(info->chunk.memblock);
240 assert(info->chunk.memblock->data);
241 assert(info->chunk.length);
242
243 info++;
244 maxinfo--;
245 n++;
246 }
247
248 return n;
249 }
250
251 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned maxinfo, size_t length) {
252 assert(s);
253 assert(s->ref >= 1);
254 assert(info);
255
256 for (; maxinfo > 0; maxinfo--, info++) {
257 pa_sink_input *i = info->userdata;
258
259 assert(i);
260 assert(info->chunk.memblock);
261
262 /* Drop read data */
263 pa_sink_input_drop(i, &info->chunk, length);
264 pa_memblock_unref(info->chunk.memblock);
265
266 /* Decrease ref counter */
267 pa_sink_input_unref(i);
268 info->userdata = NULL;
269 }
270 }
271
272 int pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
273 pa_mix_info info[MAX_MIX_CHANNELS];
274 unsigned n;
275 int r = -1;
276
277 assert(s);
278 assert(s->ref >= 1);
279 assert(length);
280 assert(result);
281
282 pa_sink_ref(s);
283
284 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
285
286 if (n <= 0)
287 goto finish;
288
289 if (n == 1) {
290 pa_cvolume volume;
291
292 *result = info[0].chunk;
293 pa_memblock_ref(result->memblock);
294
295 if (result->length > length)
296 result->length = length;
297
298 pa_sw_cvolume_multiply(&volume, &s->sw_volume, &info[0].volume);
299
300 if (s->sw_muted || !pa_cvolume_is_norm(&volume)) {
301 pa_memchunk_make_writable(result, s->core->memblock_stat, 0);
302 if (s->sw_muted)
303 pa_silence_memchunk(result, &s->sample_spec);
304 else
305 pa_volume_memchunk(result, &s->sample_spec, &volume);
306 }
307 } else {
308 result->memblock = pa_memblock_new(length, s->core->memblock_stat);
309 assert(result->memblock);
310
311 /* pa_log("mixing %i", n); */
312
313 result->length = pa_mix(info, n, result->memblock->data, length,
314 &s->sample_spec, &s->sw_volume, s->sw_muted);
315 result->index = 0;
316 }
317
318 inputs_drop(s, info, n, result->length);
319
320 if (s->monitor_source)
321 pa_source_post(s->monitor_source, result);
322
323 r = 0;
324
325 finish:
326 pa_sink_unref(s);
327
328 return r;
329 }
330
331 int pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
332 pa_mix_info info[MAX_MIX_CHANNELS];
333 unsigned n;
334 int r = -1;
335
336 assert(s);
337 assert(s->ref >= 1);
338 assert(target);
339 assert(target->memblock);
340 assert(target->length);
341 assert(target->memblock->data);
342
343 pa_sink_ref(s);
344
345 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
346
347 if (n <= 0)
348 goto finish;
349
350 if (n == 1) {
351 pa_cvolume volume;
352
353 if (target->length > info[0].chunk.length)
354 target->length = info[0].chunk.length;
355
356 memcpy((uint8_t*) target->memblock->data + target->index,
357 (uint8_t*) info[0].chunk.memblock->data + info[0].chunk.index,
358 target->length);
359
360 pa_sw_cvolume_multiply(&volume, &s->sw_volume, &info[0].volume);
361
362 if (s->sw_muted)
363 pa_silence_memchunk(target, &s->sample_spec);
364 else if (!pa_cvolume_is_norm(&volume))
365 pa_volume_memchunk(target, &s->sample_spec, &volume);
366 } else
367 target->length = pa_mix(info, n,
368 (uint8_t*) target->memblock->data + target->index,
369 target->length,
370 &s->sample_spec,
371 &s->sw_volume,
372 s->sw_muted);
373
374 inputs_drop(s, info, n, target->length);
375
376 if (s->monitor_source)
377 pa_source_post(s->monitor_source, target);
378
379 r = 0;
380
381 finish:
382 pa_sink_unref(s);
383
384 return r;
385 }
386
387 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
388 pa_memchunk chunk;
389 size_t l, d;
390
391 assert(s);
392 assert(s->ref >= 1);
393 assert(target);
394 assert(target->memblock);
395 assert(target->length);
396 assert(target->memblock->data);
397
398 pa_sink_ref(s);
399
400 l = target->length;
401 d = 0;
402 while (l > 0) {
403 chunk = *target;
404 chunk.index += d;
405 chunk.length -= d;
406
407 if (pa_sink_render_into(s, &chunk) < 0)
408 break;
409
410 d += chunk.length;
411 l -= chunk.length;
412 }
413
414 if (l > 0) {
415 chunk = *target;
416 chunk.index += d;
417 chunk.length -= d;
418 pa_silence_memchunk(&chunk, &s->sample_spec);
419 }
420
421 pa_sink_unref(s);
422 }
423
424 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
425 assert(s);
426 assert(s->ref >= 1);
427 assert(length);
428 assert(result);
429
430 /*** This needs optimization ***/
431
432 result->memblock = pa_memblock_new(result->length = length, s->core->memblock_stat);
433 result->index = 0;
434
435 pa_sink_render_into_full(s, result);
436 }
437
438 pa_usec_t pa_sink_get_latency(pa_sink *s) {
439 assert(s);
440 assert(s->ref >= 1);
441
442 if (!s->get_latency)
443 return 0;
444
445 return s->get_latency(s);
446 }
447
448 void pa_sink_set_owner(pa_sink *s, pa_module *m) {
449 assert(s);
450 assert(s->ref >= 1);
451
452 if (s->owner == m)
453 return;
454
455 s->owner = m;
456
457 if (s->monitor_source)
458 pa_source_set_owner(s->monitor_source, m);
459
460 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
461 }
462
463 void pa_sink_set_volume(pa_sink *s, pa_mixer_t m, const pa_cvolume *volume) {
464 pa_cvolume *v;
465
466 assert(s);
467 assert(s->ref >= 1);
468 assert(volume);
469
470 if (m == PA_MIXER_HARDWARE && s->set_hw_volume)
471 v = &s->hw_volume;
472 else
473 v = &s->sw_volume;
474
475 if (pa_cvolume_equal(v, volume))
476 return;
477
478 *v = *volume;
479
480 if (v == &s->hw_volume)
481 if (s->set_hw_volume(s) < 0)
482 s->sw_volume = *volume;
483
484 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
485 }
486
487 const pa_cvolume *pa_sink_get_volume(pa_sink *s, pa_mixer_t m) {
488 assert(s);
489 assert(s->ref >= 1);
490
491 if (m == PA_MIXER_HARDWARE && s->set_hw_volume) {
492
493 if (s->get_hw_volume)
494 s->get_hw_volume(s);
495
496 return &s->hw_volume;
497 } else
498 return &s->sw_volume;
499 }
500
501 void pa_sink_set_mute(pa_sink *s, pa_mixer_t m, int mute) {
502 int *t;
503
504 assert(s);
505 assert(s->ref >= 1);
506
507 if (m == PA_MIXER_HARDWARE && s->set_hw_mute)
508 t = &s->hw_muted;
509 else
510 t = &s->sw_muted;
511
512 if (!!*t == !!mute)
513 return;
514
515 *t = !!mute;
516
517 if (t == &s->hw_muted)
518 if (s->set_hw_mute(s) < 0)
519 s->sw_muted = !!mute;
520
521 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
522 }
523
524 int pa_sink_get_mute(pa_sink *s, pa_mixer_t m) {
525 assert(s);
526 assert(s->ref >= 1);
527
528 if (m == PA_MIXER_HARDWARE && s->set_hw_mute) {
529
530 if (s->get_hw_mute)
531 s->get_hw_mute(s);
532
533 return s->hw_muted;
534 } else
535 return s->sw_muted;
536 }
537
538 void pa_sink_set_description(pa_sink *s, const char *description) {
539 assert(s);
540 assert(s->ref >= 1);
541
542 if (!description && !s->description)
543 return;
544
545 if (description && s->description && !strcmp(description, s->description))
546 return;
547
548 pa_xfree(s->description);
549 s->description = pa_xstrdup(description);
550
551 if (s->monitor_source) {
552 char *n;
553
554 n = pa_sprintf_malloc("Monitor Source of %s", s->description? s->description : s->name);
555 pa_source_set_description(s->monitor_source, n);
556 pa_xfree(n);
557 }
558
559 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
560 }
561
562 unsigned pa_sink_used_by(pa_sink *s) {
563 unsigned ret;
564
565 assert(s);
566 assert(s->ref >= 1);
567
568 ret = pa_idxset_size(s->inputs);
569
570 if (s->monitor_source)
571 ret += pa_source_used_by(s->monitor_source);
572
573 return ret;
574 }