]> code.delx.au - pulseaudio/blob - src/pulsecore/sink.c
make sure to call sink->update_requested_latency() always when we change latency...
[pulseaudio] / src / pulsecore / sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32
33 #include <pulse/introspect.h>
34 #include <pulse/utf8.h>
35 #include <pulse/xmalloc.h>
36 #include <pulse/timeval.h>
37
38 #include <pulsecore/sink-input.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/sample-util.h>
42 #include <pulsecore/core-subscribe.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/macro.h>
45 #include <pulsecore/play-memblockq.h>
46
47 #include "sink.h"
48
49 #define MAX_MIX_CHANNELS 32
50 #define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
51 #define DEFAULT_MIN_LATENCY (4*PA_USEC_PER_MSEC)
52
53 static PA_DEFINE_CHECK_TYPE(pa_sink, pa_msgobject);
54
55 static void sink_free(pa_object *s);
56
57 pa_sink_new_data* pa_sink_new_data_init(pa_sink_new_data *data) {
58 pa_assert(data);
59
60 memset(data, 0, sizeof(*data));
61 data->proplist = pa_proplist_new();
62
63 return data;
64 }
65
66 void pa_sink_new_data_set_name(pa_sink_new_data *data, const char *name) {
67 pa_assert(data);
68
69 pa_xfree(data->name);
70 data->name = pa_xstrdup(name);
71 }
72
73 void pa_sink_new_data_set_sample_spec(pa_sink_new_data *data, const pa_sample_spec *spec) {
74 pa_assert(data);
75
76 if ((data->sample_spec_is_set = !!spec))
77 data->sample_spec = *spec;
78 }
79
80 void pa_sink_new_data_set_channel_map(pa_sink_new_data *data, const pa_channel_map *map) {
81 pa_assert(data);
82
83 if ((data->channel_map_is_set = !!map))
84 data->channel_map = *map;
85 }
86
87 void pa_sink_new_data_set_volume(pa_sink_new_data *data, const pa_cvolume *volume) {
88 pa_assert(data);
89
90 if ((data->volume_is_set = !!volume))
91 data->volume = *volume;
92 }
93
94 void pa_sink_new_data_set_muted(pa_sink_new_data *data, pa_bool_t mute) {
95 pa_assert(data);
96
97 data->muted_is_set = TRUE;
98 data->muted = !!mute;
99 }
100
101 void pa_sink_new_data_done(pa_sink_new_data *data) {
102 pa_assert(data);
103
104 pa_xfree(data->name);
105 pa_proplist_free(data->proplist);
106 }
107
108 static void reset_callbacks(pa_sink *s) {
109 pa_assert(s);
110
111 s->set_state = NULL;
112 s->get_volume = NULL;
113 s->set_volume = NULL;
114 s->get_mute = NULL;
115 s->set_mute = NULL;
116 s->get_latency = NULL;
117 s->request_rewind = NULL;
118 s->update_requested_latency = NULL;
119 }
120
121 pa_sink* pa_sink_new(
122 pa_core *core,
123 pa_sink_new_data *data,
124 pa_sink_flags_t flags) {
125
126 pa_sink *s;
127 const char *name;
128 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
129 pa_source_new_data source_data;
130 const char *dn;
131
132 pa_assert(core);
133 pa_assert(data);
134 pa_assert(data->name);
135
136 s = pa_msgobject_new(pa_sink);
137
138 if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_SINK, s, data->namereg_fail))) {
139 pa_xfree(s);
140 return NULL;
141 }
142
143 pa_sink_new_data_set_name(data, name);
144
145 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_NEW], data) < 0) {
146 pa_xfree(s);
147 pa_namereg_unregister(core, name);
148 return NULL;
149 }
150
151 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
152 pa_return_null_if_fail(data->name && pa_utf8_valid(data->name) && data->name[0]);
153
154 pa_return_null_if_fail(data->sample_spec_is_set && pa_sample_spec_valid(&data->sample_spec));
155
156 if (!data->channel_map_is_set)
157 pa_return_null_if_fail(pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT));
158
159 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
160 pa_return_null_if_fail(data->channel_map.channels == data->sample_spec.channels);
161
162 if (!data->volume_is_set)
163 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
164
165 pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
166 pa_return_null_if_fail(data->volume.channels == data->sample_spec.channels);
167
168 if (!data->muted_is_set)
169 data->muted = FALSE;
170
171 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_FIXATE], data) < 0) {
172 pa_xfree(s);
173 pa_namereg_unregister(core, name);
174 return NULL;
175 }
176
177 s->parent.parent.free = sink_free;
178 s->parent.process_msg = pa_sink_process_msg;
179
180 s->core = core;
181 s->state = PA_SINK_INIT;
182 s->flags = flags;
183 s->name = pa_xstrdup(name);
184 s->proplist = pa_proplist_copy(data->proplist);
185 s->driver = pa_xstrdup(data->driver);
186 s->module = data->module;
187
188 s->sample_spec = data->sample_spec;
189 s->channel_map = data->channel_map;
190
191 s->inputs = pa_idxset_new(NULL, NULL);
192 s->n_corked = 0;
193
194 s->volume = data->volume;
195 s->muted = data->muted;
196 s->refresh_volume = s->refresh_mute = FALSE;
197
198 reset_callbacks(s);
199 s->userdata = NULL;
200
201 s->asyncmsgq = NULL;
202 s->rtpoll = NULL;
203
204 pa_silence_memchunk_get(
205 &core->silence_cache,
206 core->mempool,
207 &s->silence,
208 &s->sample_spec,
209 0);
210
211 s->min_latency = DEFAULT_MIN_LATENCY;
212 s->max_latency = s->min_latency;
213
214 s->thread_info.inputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
215 s->thread_info.soft_volume = s->volume;
216 s->thread_info.soft_muted = s->muted;
217 s->thread_info.state = s->state;
218 s->thread_info.rewind_nbytes = 0;
219 s->thread_info.max_rewind = 0;
220 s->thread_info.requested_latency_valid = FALSE;
221 s->thread_info.requested_latency = 0;
222
223 pa_assert_se(pa_idxset_put(core->sinks, s, &s->index) >= 0);
224
225 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s",
226 s->index,
227 s->name,
228 pa_sample_spec_snprint(st, sizeof(st), &s->sample_spec),
229 pa_channel_map_snprint(cm, sizeof(cm), &s->channel_map));
230
231 pa_source_new_data_init(&source_data);
232 pa_source_new_data_set_sample_spec(&source_data, &s->sample_spec);
233 pa_source_new_data_set_channel_map(&source_data, &s->channel_map);
234 source_data.name = pa_sprintf_malloc("%s.monitor", name);
235 source_data.driver = data->driver;
236 source_data.module = data->module;
237
238 dn = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
239 pa_proplist_setf(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Monitor of %s", dn ? dn : s->name);
240 pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_CLASS, "monitor");
241
242 s->monitor_source = pa_source_new(core, &source_data, 0);
243
244 pa_source_new_data_done(&source_data);
245
246 if (!s->monitor_source) {
247 pa_sink_unlink(s);
248 pa_sink_unref(s);
249 return NULL;
250 }
251
252 s->monitor_source->monitor_of = s;
253 pa_source_set_max_rewind(s->monitor_source, s->thread_info.max_rewind);
254
255 return s;
256 }
257
258 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
259 int ret;
260 pa_bool_t suspend_change;
261
262 pa_assert(s);
263
264 if (s->state == state)
265 return 0;
266
267 suspend_change =
268 (s->state == PA_SINK_SUSPENDED && PA_SINK_IS_OPENED(state)) ||
269 (PA_SINK_IS_OPENED(s->state) && state == PA_SINK_SUSPENDED);
270
271 if (s->set_state)
272 if ((ret = s->set_state(s, state)) < 0)
273 return -1;
274
275 if (s->asyncmsgq)
276 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) < 0)
277 return -1;
278
279 s->state = state;
280
281 if (suspend_change) {
282 pa_sink_input *i;
283 uint32_t idx;
284
285 /* We're suspending or resuming, tell everyone about it */
286
287 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx)))
288 if (i->suspend)
289 i->suspend(i, state == PA_SINK_SUSPENDED);
290 }
291
292 if (state != PA_SINK_UNLINKED) /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
293 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], s);
294
295 return 0;
296 }
297
298 void pa_sink_put(pa_sink* s) {
299 pa_sink_assert_ref(s);
300
301 pa_assert(s->state == PA_SINK_INIT);
302 pa_assert(s->asyncmsgq);
303 pa_assert(s->rtpoll);
304
305 pa_assert(!s->min_latency || !s->max_latency || s->min_latency <= s->max_latency);
306
307 if (!(s->flags & PA_SINK_HW_VOLUME_CTRL))
308 s->flags |= PA_SINK_DECIBEL_VOLUME;
309
310 pa_assert_se(sink_set_state(s, PA_SINK_IDLE) == 0);
311
312 pa_source_put(s->monitor_source);
313
314 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
315 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PUT], s);
316 }
317
318 void pa_sink_unlink(pa_sink* s) {
319 pa_bool_t linked;
320 pa_sink_input *i, *j = NULL;
321
322 pa_assert(s);
323
324 /* Please note that pa_sink_unlink() does more than simply
325 * reversing pa_sink_put(). It also undoes the registrations
326 * already done in pa_sink_new()! */
327
328 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
329 * may be called multiple times on the same sink without bad
330 * effects. */
331
332 linked = PA_SINK_IS_LINKED(s->state);
333
334 if (linked)
335 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK], s);
336
337 if (s->state != PA_SINK_UNLINKED)
338 pa_namereg_unregister(s->core, s->name);
339 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
340
341 while ((i = pa_idxset_first(s->inputs, NULL))) {
342 pa_assert(i != j);
343 pa_sink_input_kill(i);
344 j = i;
345 }
346
347 if (linked)
348 sink_set_state(s, PA_SINK_UNLINKED);
349 else
350 s->state = PA_SINK_UNLINKED;
351
352 reset_callbacks(s);
353
354 if (s->monitor_source)
355 pa_source_unlink(s->monitor_source);
356
357 if (linked) {
358 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
359 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], s);
360 }
361 }
362
363 static void sink_free(pa_object *o) {
364 pa_sink *s = PA_SINK(o);
365 pa_sink_input *i;
366
367 pa_assert(s);
368 pa_assert(pa_sink_refcnt(s) == 0);
369
370 if (PA_SINK_IS_LINKED(s->state))
371 pa_sink_unlink(s);
372
373 pa_log_info("Freeing sink %u \"%s\"", s->index, s->name);
374
375 if (s->monitor_source) {
376 pa_source_unref(s->monitor_source);
377 s->monitor_source = NULL;
378 }
379
380 pa_idxset_free(s->inputs, NULL, NULL);
381
382 while ((i = pa_hashmap_steal_first(s->thread_info.inputs)))
383 pa_sink_input_unref(i);
384
385 pa_hashmap_free(s->thread_info.inputs, NULL, NULL);
386
387 if (s->silence.memblock)
388 pa_memblock_unref(s->silence.memblock);
389
390 pa_xfree(s->name);
391 pa_xfree(s->driver);
392
393 if (s->proplist)
394 pa_proplist_free(s->proplist);
395
396 pa_xfree(s);
397 }
398
399 void pa_sink_set_asyncmsgq(pa_sink *s, pa_asyncmsgq *q) {
400 pa_sink_assert_ref(s);
401
402 s->asyncmsgq = q;
403
404 if (s->monitor_source)
405 pa_source_set_asyncmsgq(s->monitor_source, q);
406 }
407
408 void pa_sink_set_rtpoll(pa_sink *s, pa_rtpoll *p) {
409 pa_sink_assert_ref(s);
410
411 s->rtpoll = p;
412 if (s->monitor_source)
413 pa_source_set_rtpoll(s->monitor_source, p);
414 }
415
416 int pa_sink_update_status(pa_sink*s) {
417 pa_sink_assert_ref(s);
418 pa_assert(PA_SINK_IS_LINKED(s->state));
419
420 if (s->state == PA_SINK_SUSPENDED)
421 return 0;
422
423 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
424 }
425
426 int pa_sink_suspend(pa_sink *s, pa_bool_t suspend) {
427 pa_sink_assert_ref(s);
428 pa_assert(PA_SINK_IS_LINKED(s->state));
429
430 if (suspend)
431 return sink_set_state(s, PA_SINK_SUSPENDED);
432 else
433 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
434 }
435
436 void pa_sink_process_rewind(pa_sink *s, size_t nbytes) {
437 pa_sink_input *i;
438 void *state = NULL;
439 pa_sink_assert_ref(s);
440 pa_assert(PA_SINK_IS_LINKED(s->state));
441
442 /* Make sure the sink code already reset the counter! */
443 pa_assert(s->thread_info.rewind_nbytes <= 0);
444
445 if (nbytes <= 0)
446 return;
447
448 pa_log_debug("Processing rewind...");
449
450 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
451 pa_sink_input_assert_ref(i);
452 pa_sink_input_process_rewind(i, nbytes);
453 }
454
455 if (s->monitor_source && PA_SOURCE_IS_OPENED(pa_source_get_state(s->monitor_source)))
456 pa_source_process_rewind(s->monitor_source, nbytes);
457
458 }
459
460 static unsigned fill_mix_info(pa_sink *s, size_t *length, pa_mix_info *info, unsigned maxinfo) {
461 pa_sink_input *i;
462 unsigned n = 0;
463 void *state = NULL;
464 size_t mixlength = *length;
465
466 pa_sink_assert_ref(s);
467 pa_assert(info);
468
469 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)) && maxinfo > 0) {
470 pa_sink_input_assert_ref(i);
471
472 if (pa_sink_input_peek(i, *length, &info->chunk, &info->volume) < 0)
473 continue;
474
475 if (mixlength == 0 || info->chunk.length < mixlength)
476 mixlength = info->chunk.length;
477
478 if (pa_memblock_is_silence(info->chunk.memblock)) {
479 pa_memblock_unref(info->chunk.memblock);
480 continue;
481 }
482
483 info->userdata = pa_sink_input_ref(i);
484
485 pa_assert(info->chunk.memblock);
486 pa_assert(info->chunk.length > 0);
487
488 info++;
489 n++;
490 maxinfo--;
491 }
492
493 if (mixlength > 0)
494 *length = mixlength;
495
496 return n;
497 }
498
499 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned n, size_t length) {
500 pa_sink_input *i;
501 void *state = NULL;
502 unsigned p = 0;
503 unsigned n_unreffed = 0;
504
505 pa_sink_assert_ref(s);
506
507 /* We optimize for the case where the order of the inputs has not changed */
508
509 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
510 unsigned j;
511 pa_mix_info* m;
512
513 pa_sink_input_assert_ref(i);
514
515 m = NULL;
516
517 /* Let's try to find the matching entry info the pa_mix_info array */
518 for (j = 0; j < n; j ++) {
519
520 if (info[p].userdata == i) {
521 m = info + p;
522 break;
523 }
524
525 p++;
526 if (p >= n)
527 p = 0;
528 }
529
530 /* Drop read data */
531 pa_sink_input_drop(i, length);
532
533 if (m) {
534 pa_sink_input_unref(m->userdata);
535 m->userdata = NULL;
536 if (m->chunk.memblock)
537 pa_memblock_unref(m->chunk.memblock);
538 pa_memchunk_reset(&m->chunk);
539
540 n_unreffed += 1;
541 }
542 }
543
544 /* Now drop references to entries that are included in the
545 * pa_mix_info array but don't exist anymore */
546
547 if (n_unreffed < n) {
548 for (; n > 0; info++, n--) {
549 if (info->userdata)
550 pa_sink_input_unref(info->userdata);
551 if (info->chunk.memblock)
552 pa_memblock_unref(info->chunk.memblock);
553 }
554 }
555 }
556
557 void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
558 pa_mix_info info[MAX_MIX_CHANNELS];
559 unsigned n;
560 size_t block_size_max;
561
562 pa_sink_assert_ref(s);
563 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
564 pa_assert(pa_frame_aligned(length, &s->sample_spec));
565 pa_assert(result);
566
567 pa_sink_ref(s);
568
569 s->thread_info.rewind_nbytes = 0;
570
571 if (length <= 0)
572 length = pa_frame_align(MIX_BUFFER_LENGTH, &s->sample_spec);
573
574 block_size_max = pa_mempool_block_size_max(s->core->mempool);
575 if (length > block_size_max)
576 length = pa_frame_align(block_size_max, &s->sample_spec);
577
578 pa_assert(length > 0);
579
580 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, &length, info, MAX_MIX_CHANNELS) : 0;
581
582 if (n == 0) {
583
584 *result = s->silence;
585 pa_memblock_ref(result->memblock);
586
587 if (result->length > length)
588 result->length = length;
589
590 } else if (n == 1) {
591 pa_cvolume volume;
592
593 *result = info[0].chunk;
594 pa_memblock_ref(result->memblock);
595
596 if (result->length > length)
597 result->length = length;
598
599 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
600
601 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&volume)) {
602 pa_log("adjusting volume ");
603 pa_memchunk_make_writable(result, 0);
604 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
605 pa_silence_memchunk(result, &s->sample_spec);
606 else
607 pa_volume_memchunk(result, &s->sample_spec, &volume);
608 }
609 } else {
610 void *ptr;
611 result->memblock = pa_memblock_new(s->core->mempool, length);
612
613 ptr = pa_memblock_acquire(result->memblock);
614 result->length = pa_mix(info, n,
615 ptr, length,
616 &s->sample_spec,
617 &s->thread_info.soft_volume,
618 s->thread_info.soft_muted);
619 pa_memblock_release(result->memblock);
620
621 result->index = 0;
622 }
623
624 if (s->thread_info.state == PA_SINK_RUNNING)
625 inputs_drop(s, info, n, result->length);
626
627 if (s->monitor_source && PA_SOURCE_IS_OPENED(pa_source_get_state(s->monitor_source)))
628 pa_source_post(s->monitor_source, result);
629
630 pa_sink_unref(s);
631 }
632
633 void pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
634 pa_mix_info info[MAX_MIX_CHANNELS];
635 unsigned n;
636 size_t length, block_size_max;
637
638 pa_sink_assert_ref(s);
639 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
640 pa_assert(target);
641 pa_assert(target->memblock);
642 pa_assert(target->length > 0);
643 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
644
645 pa_sink_ref(s);
646
647 s->thread_info.rewind_nbytes = 0;
648
649 length = target->length;
650 block_size_max = pa_mempool_block_size_max(s->core->mempool);
651 if (length > block_size_max)
652 length = pa_frame_align(block_size_max, &s->sample_spec);
653
654 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, &length, info, MAX_MIX_CHANNELS) : 0;
655
656 if (n == 0) {
657 if (target->length > length)
658 target->length = length;
659
660 pa_silence_memchunk(target, &s->sample_spec);
661 } else if (n == 1) {
662 pa_cvolume volume;
663
664 if (target->length > length)
665 target->length = length;
666
667 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
668
669 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
670 pa_silence_memchunk(target, &s->sample_spec);
671 else {
672 pa_memchunk vchunk;
673
674 vchunk = info[0].chunk;
675 pa_memblock_ref(vchunk.memblock);
676
677 if (vchunk.length > target->length)
678 vchunk.length = target->length;
679
680 if (!pa_cvolume_is_norm(&volume)) {
681 pa_memchunk_make_writable(&vchunk, 0);
682 pa_volume_memchunk(&vchunk, &s->sample_spec, &volume);
683 }
684
685 pa_memchunk_memcpy(target, &vchunk);
686 pa_memblock_unref(vchunk.memblock);
687 }
688
689 } else {
690 void *ptr;
691
692 ptr = pa_memblock_acquire(target->memblock);
693
694 target->length = pa_mix(info, n,
695 (uint8_t*) ptr + target->index, length,
696 &s->sample_spec,
697 &s->thread_info.soft_volume,
698 s->thread_info.soft_muted);
699
700 pa_memblock_release(target->memblock);
701 }
702
703 if (s->thread_info.state == PA_SINK_RUNNING)
704 inputs_drop(s, info, n, target->length);
705
706 if (s->monitor_source && PA_SOURCE_IS_OPENED(pa_source_get_state(s->monitor_source)))
707 pa_source_post(s->monitor_source, target);
708
709 pa_sink_unref(s);
710 }
711
712 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
713 pa_memchunk chunk;
714 size_t l, d;
715
716 pa_sink_assert_ref(s);
717 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
718 pa_assert(target);
719 pa_assert(target->memblock);
720 pa_assert(target->length > 0);
721 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
722
723 pa_sink_ref(s);
724
725 s->thread_info.rewind_nbytes = 0;
726
727 l = target->length;
728 d = 0;
729 while (l > 0) {
730 chunk = *target;
731 chunk.index += d;
732 chunk.length -= d;
733
734 pa_sink_render_into(s, &chunk);
735
736 d += chunk.length;
737 l -= chunk.length;
738 }
739
740 pa_sink_unref(s);
741 }
742
743 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
744 pa_sink_assert_ref(s);
745 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
746 pa_assert(length > 0);
747 pa_assert(pa_frame_aligned(length, &s->sample_spec));
748 pa_assert(result);
749
750 s->thread_info.rewind_nbytes = 0;
751
752 /*** This needs optimization ***/
753
754 result->index = 0;
755 result->length = length;
756 result->memblock = pa_memblock_new(s->core->mempool, length);
757
758 pa_sink_render_into_full(s, result);
759 }
760
761 pa_usec_t pa_sink_get_latency(pa_sink *s) {
762 pa_usec_t usec = 0;
763
764 pa_sink_assert_ref(s);
765 pa_assert(PA_SINK_IS_LINKED(s->state));
766
767 /* The returned value is supposed to be in the time domain of the sound card! */
768
769 if (!PA_SINK_IS_OPENED(s->state))
770 return 0;
771
772 if (s->get_latency)
773 return s->get_latency(s);
774
775 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
776 return 0;
777
778 return usec;
779 }
780
781 void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume) {
782 int changed;
783
784 pa_sink_assert_ref(s);
785 pa_assert(PA_SINK_IS_LINKED(s->state));
786 pa_assert(volume);
787
788 changed = !pa_cvolume_equal(volume, &s->volume);
789 s->volume = *volume;
790
791 if (s->set_volume && s->set_volume(s) < 0)
792 s->set_volume = NULL;
793
794 if (!s->set_volume)
795 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), 0, NULL, pa_xfree);
796
797 if (changed)
798 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
799 }
800
801 const pa_cvolume *pa_sink_get_volume(pa_sink *s) {
802 struct pa_cvolume old_volume;
803
804 pa_sink_assert_ref(s);
805 pa_assert(PA_SINK_IS_LINKED(s->state));
806
807 old_volume = s->volume;
808
809 if (s->get_volume && s->get_volume(s) < 0)
810 s->get_volume = NULL;
811
812 if (!s->get_volume && s->refresh_volume)
813 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, &s->volume, 0, NULL);
814
815 if (!pa_cvolume_equal(&old_volume, &s->volume))
816 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
817
818 return &s->volume;
819 }
820
821 void pa_sink_set_mute(pa_sink *s, pa_bool_t mute) {
822 int changed;
823
824 pa_sink_assert_ref(s);
825 pa_assert(PA_SINK_IS_LINKED(s->state));
826
827 changed = s->muted != mute;
828 s->muted = mute;
829
830 if (s->set_mute && s->set_mute(s) < 0)
831 s->set_mute = NULL;
832
833 if (!s->set_mute)
834 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
835
836 if (changed)
837 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
838 }
839
840 pa_bool_t pa_sink_get_mute(pa_sink *s) {
841 pa_bool_t old_muted;
842
843 pa_sink_assert_ref(s);
844 pa_assert(PA_SINK_IS_LINKED(s->state));
845
846 old_muted = s->muted;
847
848 if (s->get_mute && s->get_mute(s) < 0)
849 s->get_mute = NULL;
850
851 if (!s->get_mute && s->refresh_mute)
852 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MUTE, &s->muted, 0, NULL);
853
854 if (old_muted != s->muted)
855 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
856
857 return s->muted;
858 }
859
860 void pa_sink_set_description(pa_sink *s, const char *description) {
861 const char *old;
862 pa_sink_assert_ref(s);
863
864 if (!description && !pa_proplist_contains(s->proplist, PA_PROP_DEVICE_DESCRIPTION))
865 return;
866
867 old = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
868
869 if (old && description && !strcmp(old, description))
870 return;
871
872 if (description)
873 pa_proplist_sets(s->proplist, PA_PROP_DEVICE_DESCRIPTION, description);
874 else
875 pa_proplist_unset(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
876
877 if (s->monitor_source) {
878 char *n;
879
880 n = pa_sprintf_malloc("Monitor Source of %s", description ? description : s->name);
881 pa_source_set_description(s->monitor_source, n);
882 pa_xfree(n);
883 }
884
885 if (PA_SINK_IS_LINKED(s->state)) {
886 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
887 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], s);
888 }
889 }
890
891 unsigned pa_sink_linked_by(pa_sink *s) {
892 unsigned ret;
893
894 pa_sink_assert_ref(s);
895 pa_assert(PA_SINK_IS_LINKED(s->state));
896
897 ret = pa_idxset_size(s->inputs);
898
899 /* We add in the number of streams connected to us here. Please
900 * not the asymmmetry to pa_sink_used_by()! */
901
902 if (s->monitor_source)
903 ret += pa_source_linked_by(s->monitor_source);
904
905 return ret;
906 }
907
908 unsigned pa_sink_used_by(pa_sink *s) {
909 unsigned ret;
910
911 pa_sink_assert_ref(s);
912 pa_assert(PA_SINK_IS_LINKED(s->state));
913
914 ret = pa_idxset_size(s->inputs);
915 pa_assert(ret >= s->n_corked);
916
917 /* Streams connected to our monitor source do not matter for
918 * pa_sink_used_by()!.*/
919
920 return ret - s->n_corked;
921 }
922
923 int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
924 pa_sink *s = PA_SINK(o);
925 pa_sink_assert_ref(s);
926 pa_assert(s->thread_info.state != PA_SINK_UNLINKED);
927
928 switch ((pa_sink_message_t) code) {
929
930 case PA_SINK_MESSAGE_ADD_INPUT: {
931 pa_sink_input *i = PA_SINK_INPUT(userdata);
932
933 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
934
935 /* Since the caller sleeps in pa_sink_input_put(), we can
936 * safely access data outside of thread_info even though
937 * it is mutable */
938
939 if ((i->thread_info.sync_prev = i->sync_prev)) {
940 pa_assert(i->sink == i->thread_info.sync_prev->sink);
941 pa_assert(i->sync_prev->sync_next == i);
942 i->thread_info.sync_prev->thread_info.sync_next = i;
943 }
944
945 if ((i->thread_info.sync_next = i->sync_next)) {
946 pa_assert(i->sink == i->thread_info.sync_next->sink);
947 pa_assert(i->sync_next->sync_prev == i);
948 i->thread_info.sync_next->thread_info.sync_prev = i;
949 }
950
951 pa_assert(!i->thread_info.attached);
952 i->thread_info.attached = TRUE;
953
954 if (i->attach)
955 i->attach(i);
956
957 pa_sink_input_set_state_within_thread(i, i->state);
958
959 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
960
961 pa_sink_invalidate_requested_latency(s);
962
963 /* We don't rewind here automatically. This is left to the
964 * sink input implementor because some sink inputs need a
965 * slow start, i.e. need some time to buffer client
966 * samples before beginning streaming. */
967
968 /* If you change anything here, make sure to change the
969 * ghost sink input handling a few lines down at
970 * PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, too. */
971
972 return 0;
973 }
974
975 case PA_SINK_MESSAGE_REMOVE_INPUT: {
976 pa_sink_input *i = PA_SINK_INPUT(userdata);
977
978 /* If you change anything here, make sure to change the
979 * sink input handling a few lines down at
980 * PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, too. */
981
982 pa_sink_input_set_state_within_thread(i, i->state);
983
984 if (i->detach)
985 i->detach(i);
986
987 pa_assert(i->thread_info.attached);
988 i->thread_info.attached = FALSE;
989
990 /* Since the caller sleeps in pa_sink_input_unlink(),
991 * we can safely access data outside of thread_info even
992 * though it is mutable */
993
994 pa_assert(!i->thread_info.sync_prev);
995 pa_assert(!i->thread_info.sync_next);
996
997 if (i->thread_info.sync_prev) {
998 i->thread_info.sync_prev->thread_info.sync_next = i->thread_info.sync_prev->sync_next;
999 i->thread_info.sync_prev = NULL;
1000 }
1001
1002 if (i->thread_info.sync_next) {
1003 i->thread_info.sync_next->thread_info.sync_prev = i->thread_info.sync_next->sync_prev;
1004 i->thread_info.sync_next = NULL;
1005 }
1006
1007 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
1008 pa_sink_input_unref(i);
1009
1010 pa_sink_invalidate_requested_latency(s);
1011 pa_sink_request_rewind(s, 0);
1012
1013 return 0;
1014 }
1015
1016 case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER: {
1017 pa_sink_input_move_info *info = userdata;
1018 int volume_is_norm;
1019
1020 /* We don't support moving synchronized streams. */
1021 pa_assert(!info->sink_input->sync_prev);
1022 pa_assert(!info->sink_input->sync_next);
1023 pa_assert(!info->sink_input->thread_info.sync_next);
1024 pa_assert(!info->sink_input->thread_info.sync_prev);
1025
1026 if (info->sink_input->detach)
1027 info->sink_input->detach(info->sink_input);
1028
1029 pa_assert(info->sink_input->thread_info.attached);
1030 info->sink_input->thread_info.attached = FALSE;
1031 pa_sink_invalidate_requested_latency(info->sink_input->sink);
1032
1033 if (info->ghost_sink_input) {
1034 pa_assert(info->buffer_bytes > 0);
1035 pa_assert(info->buffer);
1036
1037 volume_is_norm = pa_cvolume_is_norm(&info->sink_input->thread_info.volume);
1038
1039 pa_log_debug("Buffering %lu bytes ...", (unsigned long) info->buffer_bytes);
1040
1041 while (info->buffer_bytes > 0) {
1042 pa_memchunk memchunk;
1043 pa_cvolume volume;
1044 size_t n;
1045
1046 if (pa_sink_input_peek(info->sink_input, info->buffer_bytes, &memchunk, &volume) < 0)
1047 break;
1048
1049 n = memchunk.length > info->buffer_bytes ? info->buffer_bytes : memchunk.length;
1050 pa_sink_input_drop(info->sink_input, n);
1051 memchunk.length = n;
1052
1053 if (!volume_is_norm) {
1054 pa_memchunk_make_writable(&memchunk, 0);
1055 pa_volume_memchunk(&memchunk, &s->sample_spec, &volume);
1056 }
1057
1058 if (pa_memblockq_push(info->buffer, &memchunk) < 0) {
1059 pa_memblock_unref(memchunk.memblock);
1060 break;
1061 }
1062
1063 pa_memblock_unref(memchunk.memblock);
1064 info->buffer_bytes -= n;
1065 }
1066
1067 /* Add the remaining already resampled chunks to the buffer */
1068 pa_memblockq_splice(info->buffer, info->sink_input->thread_info.render_memblockq);
1069
1070 pa_memblockq_sink_input_set_queue(info->ghost_sink_input, info->buffer);
1071
1072 pa_log_debug("Buffered %lu bytes ...", (unsigned long) pa_memblockq_get_length(info->buffer));
1073 }
1074
1075 /* Let's remove the sink input ...*/
1076 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(info->sink_input->index)))
1077 pa_sink_input_unref(info->sink_input);
1078
1079 /* .. and add the ghost sink input instead */
1080 if (info->ghost_sink_input) {
1081 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(info->ghost_sink_input->index), pa_sink_input_ref(info->ghost_sink_input));
1082 info->ghost_sink_input->thread_info.sync_prev = info->ghost_sink_input->thread_info.sync_next = NULL;
1083
1084 pa_sink_input_update_max_rewind(info->ghost_sink_input, s->thread_info.max_rewind);
1085
1086 pa_assert(!info->ghost_sink_input->thread_info.attached);
1087 info->ghost_sink_input->thread_info.attached = TRUE;
1088
1089 if (info->ghost_sink_input->attach)
1090 info->ghost_sink_input->attach(info->ghost_sink_input);
1091 }
1092
1093 pa_sink_invalidate_requested_latency(s);
1094 pa_sink_request_rewind(s, 0);
1095
1096 return 0;
1097 }
1098
1099 case PA_SINK_MESSAGE_SET_VOLUME:
1100 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
1101
1102 pa_sink_request_rewind(s, 0);
1103 return 0;
1104
1105 case PA_SINK_MESSAGE_SET_MUTE:
1106 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
1107
1108 pa_sink_request_rewind(s, 0);
1109 return 0;
1110
1111 case PA_SINK_MESSAGE_GET_VOLUME:
1112 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
1113 return 0;
1114
1115 case PA_SINK_MESSAGE_GET_MUTE:
1116 *((pa_bool_t*) userdata) = s->thread_info.soft_muted;
1117 return 0;
1118
1119 case PA_SINK_MESSAGE_SET_STATE:
1120
1121 s->thread_info.state = PA_PTR_TO_UINT(userdata);
1122 return 0;
1123
1124 case PA_SINK_MESSAGE_DETACH:
1125
1126 /* We're detaching all our input streams so that the
1127 * asyncmsgq and rtpoll fields can be changed without
1128 * problems */
1129 pa_sink_detach_within_thread(s);
1130 return 0;
1131
1132 case PA_SINK_MESSAGE_ATTACH:
1133
1134 /* Reattach all streams */
1135 pa_sink_attach_within_thread(s);
1136 return 0;
1137
1138 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY: {
1139
1140 pa_usec_t *usec = userdata;
1141 *usec = pa_sink_get_requested_latency_within_thread(s);
1142 return 0;
1143 }
1144
1145 case PA_SINK_MESSAGE_GET_LATENCY:
1146 case PA_SINK_MESSAGE_MAX:
1147 ;
1148 }
1149
1150 return -1;
1151 }
1152
1153 int pa_sink_suspend_all(pa_core *c, pa_bool_t suspend) {
1154 pa_sink *sink;
1155 uint32_t idx;
1156 int ret = 0;
1157
1158 pa_core_assert_ref(c);
1159
1160 for (sink = PA_SINK(pa_idxset_first(c->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(c->sinks, &idx)))
1161 ret -= pa_sink_suspend(sink, suspend) < 0;
1162
1163 return ret;
1164 }
1165
1166 void pa_sink_detach(pa_sink *s) {
1167 pa_sink_assert_ref(s);
1168 pa_assert(PA_SINK_IS_LINKED(s->state));
1169
1170 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_DETACH, NULL, 0, NULL);
1171 }
1172
1173 void pa_sink_attach(pa_sink *s) {
1174 pa_sink_assert_ref(s);
1175 pa_assert(PA_SINK_IS_LINKED(s->state));
1176
1177 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_ATTACH, NULL, 0, NULL);
1178 }
1179
1180 void pa_sink_detach_within_thread(pa_sink *s) {
1181 pa_sink_input *i;
1182 void *state = NULL;
1183
1184 pa_sink_assert_ref(s);
1185 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1186
1187 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1188 if (i->detach)
1189 i->detach(i);
1190
1191 if (s->monitor_source)
1192 pa_source_detach_within_thread(s->monitor_source);
1193 }
1194
1195 void pa_sink_attach_within_thread(pa_sink *s) {
1196 pa_sink_input *i;
1197 void *state = NULL;
1198
1199 pa_sink_assert_ref(s);
1200 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1201
1202 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1203 if (i->attach)
1204 i->attach(i);
1205
1206 if (s->monitor_source)
1207 pa_source_attach_within_thread(s->monitor_source);
1208 }
1209
1210 void pa_sink_request_rewind(pa_sink*s, size_t nbytes) {
1211 pa_sink_assert_ref(s);
1212 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1213
1214 if (nbytes <= 0)
1215 nbytes = s->thread_info.max_rewind;
1216
1217 nbytes = PA_MIN(nbytes, s->thread_info.max_rewind);
1218
1219 if (nbytes <= s->thread_info.rewind_nbytes)
1220 return;
1221
1222 s->thread_info.rewind_nbytes = nbytes;
1223
1224 if (s->request_rewind)
1225 s->request_rewind(s);
1226 }
1227
1228 pa_usec_t pa_sink_get_requested_latency_within_thread(pa_sink *s) {
1229 pa_usec_t result = (pa_usec_t) -1;
1230 pa_sink_input *i;
1231 void *state = NULL;
1232
1233 pa_sink_assert_ref(s);
1234
1235 if (s->thread_info.requested_latency_valid)
1236 return s->thread_info.requested_latency;
1237
1238 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1239
1240 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1 &&
1241 (result == (pa_usec_t) -1 || result > i->thread_info.requested_sink_latency))
1242 result = i->thread_info.requested_sink_latency;
1243
1244 if (result != (pa_usec_t) -1) {
1245 if (s->max_latency > 0 && result > s->max_latency)
1246 result = s->max_latency;
1247
1248 if (s->min_latency > 0 && result < s->min_latency)
1249 result = s->min_latency;
1250 }
1251
1252 s->thread_info.requested_latency = result;
1253 s->thread_info.requested_latency_valid = TRUE;
1254
1255 return result;
1256 }
1257
1258 pa_usec_t pa_sink_get_requested_latency(pa_sink *s) {
1259 pa_usec_t usec = 0;
1260
1261 pa_sink_assert_ref(s);
1262 pa_assert(PA_SINK_IS_LINKED(s->state));
1263
1264 if (!PA_SINK_IS_OPENED(s->state))
1265 return 0;
1266
1267 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) < 0)
1268 return 0;
1269
1270 if (usec == (pa_usec_t) -1)
1271 usec = s->max_latency;
1272
1273 return usec;
1274 }
1275
1276 void pa_sink_set_max_rewind(pa_sink *s, size_t max_rewind) {
1277 pa_sink_input *i;
1278 void *state = NULL;
1279
1280 pa_sink_assert_ref(s);
1281
1282 if (max_rewind == s->thread_info.max_rewind)
1283 return;
1284
1285 s->thread_info.max_rewind = max_rewind;
1286
1287 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1288 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
1289
1290 if (s->monitor_source)
1291 pa_source_set_max_rewind(s->monitor_source, s->thread_info.max_rewind);
1292 }
1293
1294 void pa_sink_invalidate_requested_latency(pa_sink *s) {
1295
1296 pa_sink_assert_ref(s);
1297 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1298
1299 s->thread_info.requested_latency_valid = FALSE;
1300
1301 if (s->update_requested_latency)
1302 s->update_requested_latency(s);
1303 }