]> code.delx.au - pulseaudio/blob - src/pulsecore/sink.c
- deprecate autoload stuff
[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 pa_sink* pa_sink_new(
109 pa_core *core,
110 pa_sink_new_data *data,
111 pa_sink_flags_t flags) {
112
113 pa_sink *s;
114 char *d;
115 const char *name;
116 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
117 pa_source_new_data source_data;
118
119 pa_assert(core);
120 pa_assert(data);
121
122 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_NEW], data) < 0)
123 return NULL;
124
125 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
126 pa_return_null_if_fail(data->name && pa_utf8_valid(data->name) && data->name[0]);
127
128 pa_return_null_if_fail(data->sample_spec_is_set && pa_sample_spec_valid(&data->sample_spec));
129
130 if (!data->channel_map_is_set)
131 pa_return_null_if_fail(pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT));
132
133 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
134 pa_return_null_if_fail(data->channel_map.channels == data->sample_spec.channels);
135
136 if (!data->volume_is_set)
137 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
138
139 pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
140 pa_return_null_if_fail(data->volume.channels == data->sample_spec.channels);
141
142 if (!data->muted_is_set)
143 data->muted = FALSE;
144
145 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_FIXATE], data) < 0)
146 return NULL;
147
148 s = pa_msgobject_new(pa_sink);
149
150 if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_SINK, s, data->namereg_fail))) {
151 pa_xfree(s);
152 return NULL;
153 }
154
155 s->parent.parent.free = sink_free;
156 s->parent.process_msg = pa_sink_process_msg;
157
158 s->core = core;
159 s->state = PA_SINK_INIT;
160 s->flags = flags;
161 s->name = pa_xstrdup(name);
162 s->proplist = pa_proplist_copy(data->proplist);
163 s->driver = pa_xstrdup(data->driver);
164 s->module = data->module;
165
166 s->sample_spec = data->sample_spec;
167 s->channel_map = data->channel_map;
168
169 s->inputs = pa_idxset_new(NULL, NULL);
170 s->n_corked = 0;
171
172 s->volume = data->volume;
173 s->muted = data->muted;
174 s->refresh_volume = s->refresh_mute = FALSE;
175
176 s->get_latency = NULL;
177 s->set_volume = NULL;
178 s->get_volume = NULL;
179 s->set_mute = NULL;
180 s->get_mute = NULL;
181 s->set_state = NULL;
182 s->request_rewind = NULL;
183 s->update_requested_latency = NULL;
184 s->userdata = NULL;
185
186 s->asyncmsgq = NULL;
187 s->rtpoll = NULL;
188 s->silence = pa_silence_memblock_new(core->mempool, &s->sample_spec, 0);
189
190 s->min_latency = DEFAULT_MIN_LATENCY;
191
192 s->thread_info.inputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
193 s->thread_info.soft_volume = s->volume;
194 s->thread_info.soft_muted = s->muted;
195 s->thread_info.state = s->state;
196 s->thread_info.rewind_nbytes = 0;
197 s->thread_info.max_rewind = 0;
198 s->thread_info.requested_latency_valid = TRUE;
199 s->thread_info.requested_latency = 0;
200
201 pa_assert_se(pa_idxset_put(core->sinks, s, &s->index) >= 0);
202
203 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s",
204 s->index,
205 s->name,
206 pa_sample_spec_snprint(st, sizeof(st), &s->sample_spec),
207 pa_channel_map_snprint(cm, sizeof(cm), &s->channel_map));
208
209 pa_source_new_data_init(&source_data);
210 pa_source_new_data_set_sample_spec(&source_data, &s->sample_spec);
211 pa_source_new_data_set_channel_map(&source_data, &s->channel_map);
212 source_data.name = pa_sprintf_malloc("%s.monitor", name);
213 source_data.driver = data->driver;
214
215 d = pa_sprintf_malloc("Monitor Source of %s", s->name);
216 pa_proplist_sets(data->proplist, PA_PROP_DEVICE_DESCRIPTION, d);
217 pa_xfree(d);
218 pa_proplist_sets(data->proplist, PA_PROP_DEVICE_CLASS, "monitor");
219
220 s->monitor_source = pa_source_new(core, &source_data, 0);
221
222 pa_source_new_data_done(&source_data);
223
224 if (!s->monitor_source) {
225 pa_sink_unlink(s);
226 pa_sink_unref(s);
227 return NULL;
228 }
229
230 s->monitor_source->monitor_of = s;
231
232 return s;
233 }
234
235 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
236 int ret;
237 pa_bool_t suspend_change;
238
239 pa_assert(s);
240
241 if (s->state == state)
242 return 0;
243
244 suspend_change =
245 (s->state == PA_SINK_SUSPENDED && PA_SINK_OPENED(state)) ||
246 (PA_SINK_OPENED(s->state) && state == PA_SINK_SUSPENDED);
247
248 if (s->set_state)
249 if ((ret = s->set_state(s, state)) < 0)
250 return -1;
251
252 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) < 0)
253 return -1;
254
255 s->state = state;
256
257 if (suspend_change) {
258 pa_sink_input *i;
259 uint32_t idx;
260
261 /* We're suspending or resuming, tell everyone about it */
262
263 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx)))
264 if (i->suspend)
265 i->suspend(i, state == PA_SINK_SUSPENDED);
266 }
267
268 if (state != PA_SINK_UNLINKED) /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
269 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], s);
270
271 return 0;
272 }
273
274 void pa_sink_put(pa_sink* s) {
275 pa_sink_assert_ref(s);
276
277 pa_assert(s->state == PA_SINK_INIT);
278 pa_assert(s->asyncmsgq);
279 pa_assert(s->rtpoll);
280
281 if (s->get_volume && s->set_volume)
282 s->flags |= PA_SINK_HW_VOLUME_CTRL;
283 else
284 s->flags = (s->flags & ~PA_SINK_HW_VOLUME_CTRL) | PA_SINK_DECIBEL_VOLUME;
285
286 if (s->get_mute && s->set_mute)
287 s->flags |= PA_SINK_HW_MUTE_CTRL;
288 else
289 s->flags &= ~PA_SINK_HW_MUTE_CTRL;
290
291 pa_assert_se(sink_set_state(s, PA_SINK_IDLE) == 0);
292
293 pa_source_put(s->monitor_source);
294
295 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
296 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PUT], s);
297 }
298
299 void pa_sink_unlink(pa_sink* s) {
300 pa_bool_t linked;
301 pa_sink_input *i, *j = NULL;
302
303 pa_assert(s);
304
305 /* Please note that pa_sink_unlink() does more than simply
306 * reversing pa_sink_put(). It also undoes the registrations
307 * already done in pa_sink_new()! */
308
309 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
310 * may be called multiple times on the same sink without bad
311 * effects. */
312
313 linked = PA_SINK_LINKED(s->state);
314
315 if (linked)
316 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK], s);
317
318 if (s->state != PA_SINK_UNLINKED)
319 pa_namereg_unregister(s->core, s->name);
320 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
321
322 while ((i = pa_idxset_first(s->inputs, NULL))) {
323 pa_assert(i != j);
324 pa_sink_input_kill(i);
325 j = i;
326 }
327
328 if (linked)
329 sink_set_state(s, PA_SINK_UNLINKED);
330 else
331 s->state = PA_SINK_UNLINKED;
332
333 s->get_latency = NULL;
334 s->get_volume = NULL;
335 s->set_volume = NULL;
336 s->set_mute = NULL;
337 s->get_mute = NULL;
338 s->set_state = NULL;
339 s->request_rewind = NULL;
340 s->update_requested_latency = NULL;
341
342 if (s->monitor_source)
343 pa_source_unlink(s->monitor_source);
344
345 if (linked) {
346 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
347 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], s);
348 }
349 }
350
351 static void sink_free(pa_object *o) {
352 pa_sink *s = PA_SINK(o);
353 pa_sink_input *i;
354
355 pa_assert(s);
356 pa_assert(pa_sink_refcnt(s) == 0);
357
358 if (PA_SINK_LINKED(s->state))
359 pa_sink_unlink(s);
360
361 pa_log_info("Freeing sink %u \"%s\"", s->index, s->name);
362
363 if (s->monitor_source) {
364 pa_source_unref(s->monitor_source);
365 s->monitor_source = NULL;
366 }
367
368 pa_idxset_free(s->inputs, NULL, NULL);
369
370 while ((i = pa_hashmap_steal_first(s->thread_info.inputs)))
371 pa_sink_input_unref(i);
372
373 pa_hashmap_free(s->thread_info.inputs, NULL, NULL);
374
375 if (s->silence)
376 pa_memblock_unref(s->silence);
377
378 pa_xfree(s->name);
379 pa_xfree(s->driver);
380
381 if (s->proplist)
382 pa_proplist_free(s->proplist);
383
384 pa_xfree(s);
385 }
386
387 void pa_sink_set_asyncmsgq(pa_sink *s, pa_asyncmsgq *q) {
388 pa_sink_assert_ref(s);
389 pa_assert(q);
390
391 s->asyncmsgq = q;
392
393 if (s->monitor_source)
394 pa_source_set_asyncmsgq(s->monitor_source, q);
395 }
396
397 void pa_sink_set_rtpoll(pa_sink *s, pa_rtpoll *p) {
398 pa_sink_assert_ref(s);
399 pa_assert(p);
400
401 s->rtpoll = p;
402 if (s->monitor_source)
403 pa_source_set_rtpoll(s->monitor_source, p);
404 }
405
406 int pa_sink_update_status(pa_sink*s) {
407 pa_sink_assert_ref(s);
408 pa_assert(PA_SINK_LINKED(s->state));
409
410 if (s->state == PA_SINK_SUSPENDED)
411 return 0;
412
413 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
414 }
415
416 int pa_sink_suspend(pa_sink *s, pa_bool_t suspend) {
417 pa_sink_assert_ref(s);
418 pa_assert(PA_SINK_LINKED(s->state));
419
420 if (suspend)
421 return sink_set_state(s, PA_SINK_SUSPENDED);
422 else
423 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE);
424 }
425
426 void pa_sink_ping(pa_sink *s) {
427 pa_sink_assert_ref(s);
428 pa_assert(PA_SINK_LINKED(s->state));
429
430 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_PING, NULL, 0, NULL, NULL);
431 }
432
433 void pa_sink_process_rewind(pa_sink *s) {
434 pa_sink_input *i;
435 void *state = NULL;
436 pa_sink_assert_ref(s);
437 pa_assert(PA_SINK_LINKED(s->state));
438
439 if (s->thread_info.rewind_nbytes <= 0)
440 return;
441
442 pa_log_debug("Processing rewind...");
443
444 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
445 pa_sink_input_assert_ref(i);
446
447 pa_sink_input_rewind(i, s->thread_info.rewind_nbytes);
448 }
449
450 s->thread_info.rewind_nbytes = 0;
451 }
452
453 static unsigned fill_mix_info(pa_sink *s, size_t length, pa_mix_info *info, unsigned maxinfo) {
454 pa_sink_input *i;
455 unsigned n = 0;
456 void *state = NULL;
457
458 pa_sink_assert_ref(s);
459 pa_assert(info);
460
461 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)) && maxinfo > 0) {
462 pa_sink_input_assert_ref(i);
463
464 if (pa_sink_input_peek(i, length, &info->chunk, &info->volume) < 0)
465 continue;
466
467 if (pa_memblock_is_silence(info->chunk.memblock)) {
468 pa_memblock_unref(info->chunk.memblock);
469 continue;
470 }
471
472 info->userdata = pa_sink_input_ref(i);
473
474 pa_assert(info->chunk.memblock);
475 pa_assert(info->chunk.length > 0);
476
477 info++;
478 n++;
479 maxinfo--;
480 }
481
482 return n;
483 }
484
485 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned n, size_t length) {
486 pa_sink_input *i;
487 void *state = NULL;
488 unsigned p = 0;
489 unsigned n_unreffed = 0;
490
491 pa_sink_assert_ref(s);
492
493 /* We optimize for the case where the order of the inputs has not changed */
494
495 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
496 unsigned j;
497 pa_mix_info* m;
498
499 pa_sink_input_assert_ref(i);
500
501 m = NULL;
502
503 /* Let's try to find the matching entry info the pa_mix_info array */
504 for (j = 0; j < n; j ++) {
505
506 if (info[p].userdata == i) {
507 m = info + p;
508 break;
509 }
510
511 p++;
512 if (p >= n)
513 p = 0;
514 }
515
516 /* Drop read data */
517 pa_sink_input_drop(i, length);
518
519 if (m) {
520 pa_sink_input_unref(m->userdata);
521 m->userdata = NULL;
522 if (m->chunk.memblock)
523 pa_memblock_unref(m->chunk.memblock);
524 pa_memchunk_reset(&m->chunk);
525
526 n_unreffed += 1;
527 }
528 }
529
530 /* Now drop references to entries that are included in the
531 * pa_mix_info array but don't exist anymore */
532
533 if (n_unreffed < n) {
534 for (; n > 0; info++, n--) {
535 if (info->userdata)
536 pa_sink_input_unref(info->userdata);
537 if (info->chunk.memblock)
538 pa_memblock_unref(info->chunk.memblock);
539 }
540 }
541 }
542
543 void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
544 pa_mix_info info[MAX_MIX_CHANNELS];
545 unsigned n;
546 size_t block_size_max;
547
548 pa_sink_assert_ref(s);
549 pa_assert(PA_SINK_OPENED(s->thread_info.state));
550 pa_assert(pa_frame_aligned(length, &s->sample_spec));
551 pa_assert(result);
552
553 pa_sink_ref(s);
554
555 s->thread_info.rewind_nbytes = 0;
556
557 if (length <= 0)
558 length = pa_frame_align(MIX_BUFFER_LENGTH, &s->sample_spec);
559
560 block_size_max = pa_mempool_block_size_max(s->core->mempool);
561 if (length > block_size_max)
562 length = pa_frame_align(block_size_max, &s->sample_spec);
563
564 pa_assert(length > 0);
565
566 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, length, info, MAX_MIX_CHANNELS) : 0;
567
568 if (n == 0) {
569
570 result->memblock = pa_memblock_ref(s->silence);
571 result->length = PA_MIN(pa_memblock_get_length(s->silence), length);
572 result->index = 0;
573
574 } else if (n == 1) {
575 pa_cvolume volume;
576
577 *result = info[0].chunk;
578 pa_memblock_ref(result->memblock);
579
580 if (result->length > length)
581 result->length = length;
582
583 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
584
585 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&volume)) {
586 pa_memchunk_make_writable(result, 0);
587 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
588 pa_silence_memchunk(result, &s->sample_spec);
589 else
590 pa_volume_memchunk(result, &s->sample_spec, &volume);
591 }
592 } else {
593 void *ptr;
594 result->memblock = pa_memblock_new(s->core->mempool, length);
595
596 ptr = pa_memblock_acquire(result->memblock);
597 result->length = pa_mix(info, n, ptr, length, &s->sample_spec, &s->thread_info.soft_volume, s->thread_info.soft_muted);
598 pa_memblock_release(result->memblock);
599
600 result->index = 0;
601 }
602
603 if (s->thread_info.state == PA_SINK_RUNNING)
604 inputs_drop(s, info, n, result->length);
605
606 if (s->monitor_source && PA_SOURCE_OPENED(pa_source_get_state(s->monitor_source)))
607 pa_source_post(s->monitor_source, result);
608
609 pa_sink_unref(s);
610 }
611
612 void pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
613 pa_mix_info info[MAX_MIX_CHANNELS];
614 unsigned n;
615
616 pa_sink_assert_ref(s);
617 pa_assert(PA_SINK_OPENED(s->thread_info.state));
618 pa_assert(target);
619 pa_assert(target->memblock);
620 pa_assert(target->length > 0);
621 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
622
623 pa_sink_ref(s);
624
625 s->thread_info.rewind_nbytes = 0;
626
627 n = s->thread_info.state == PA_SINK_RUNNING ? fill_mix_info(s, target->length, info, MAX_MIX_CHANNELS) : 0;
628
629 if (n == 0)
630 pa_silence_memchunk(target, &s->sample_spec);
631 else if (n == 1) {
632 if (target->length > info[0].chunk.length)
633 target->length = info[0].chunk.length;
634
635 if (s->thread_info.soft_muted)
636 pa_silence_memchunk(target, &s->sample_spec);
637 else {
638 void *src, *ptr;
639 pa_cvolume volume;
640
641 ptr = pa_memblock_acquire(target->memblock);
642 src = pa_memblock_acquire(info[0].chunk.memblock);
643
644 memcpy((uint8_t*) ptr + target->index,
645 (uint8_t*) src + info[0].chunk.index,
646 target->length);
647
648 pa_memblock_release(target->memblock);
649 pa_memblock_release(info[0].chunk.memblock);
650
651 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
652
653 if (!pa_cvolume_is_norm(&volume))
654 pa_volume_memchunk(target, &s->sample_spec, &volume);
655 }
656
657 } else {
658 void *ptr;
659
660 ptr = pa_memblock_acquire(target->memblock);
661
662 target->length = pa_mix(info, n,
663 (uint8_t*) ptr + target->index,
664 target->length,
665 &s->sample_spec,
666 &s->thread_info.soft_volume,
667 s->thread_info.soft_muted);
668
669 pa_memblock_release(target->memblock);
670 }
671
672 if (s->thread_info.state == PA_SINK_RUNNING)
673 inputs_drop(s, info, n, target->length);
674
675 if (s->monitor_source && PA_SOURCE_OPENED(pa_source_get_state(s->monitor_source)))
676 pa_source_post(s->monitor_source, target);
677
678 pa_sink_unref(s);
679 }
680
681 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
682 pa_memchunk chunk;
683 size_t l, d;
684
685 pa_sink_assert_ref(s);
686 pa_assert(PA_SINK_OPENED(s->thread_info.state));
687 pa_assert(target);
688 pa_assert(target->memblock);
689 pa_assert(target->length > 0);
690 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
691
692 pa_sink_ref(s);
693
694 s->thread_info.rewind_nbytes = 0;
695
696 l = target->length;
697 d = 0;
698 while (l > 0) {
699 chunk = *target;
700 chunk.index += d;
701 chunk.length -= d;
702
703 pa_sink_render_into(s, &chunk);
704
705 d += chunk.length;
706 l -= chunk.length;
707 }
708
709 pa_sink_unref(s);
710 }
711
712 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
713 pa_sink_assert_ref(s);
714 pa_assert(PA_SINK_OPENED(s->thread_info.state));
715 pa_assert(length > 0);
716 pa_assert(pa_frame_aligned(length, &s->sample_spec));
717 pa_assert(result);
718
719 s->thread_info.rewind_nbytes = 0;
720
721 /*** This needs optimization ***/
722
723 result->index = 0;
724 result->length = length;
725 result->memblock = pa_memblock_new(s->core->mempool, length);
726
727 pa_sink_render_into_full(s, result);
728 }
729
730 void pa_sink_skip(pa_sink *s, size_t length) {
731 pa_sink_input *i;
732 void *state = NULL;
733
734 pa_sink_assert_ref(s);
735 pa_assert(PA_SINK_OPENED(s->thread_info.state));
736 pa_assert(length > 0);
737 pa_assert(pa_frame_aligned(length, &s->sample_spec));
738
739 s->thread_info.rewind_nbytes = 0;
740
741 if (pa_source_used_by(s->monitor_source)) {
742 pa_memchunk chunk;
743
744 /* If something is connected to our monitor source, we have to
745 * pass valid data to it */
746
747 while (length > 0) {
748 pa_sink_render(s, length, &chunk);
749 pa_memblock_unref(chunk.memblock);
750
751 pa_assert(chunk.length <= length);
752 length -= chunk.length;
753 }
754
755 } else {
756 /* Ok, noone cares about the rendered data, so let's not even render it */
757
758 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) {
759 pa_sink_input_assert_ref(i);
760 pa_sink_input_drop(i, length);
761 }
762 }
763 }
764
765 pa_usec_t pa_sink_get_latency(pa_sink *s) {
766 pa_usec_t usec = 0;
767
768 pa_sink_assert_ref(s);
769 pa_assert(PA_SINK_LINKED(s->state));
770
771 /* The returned value is supposed to be in the time domain of the sound card! */
772
773 if (!PA_SINK_OPENED(s->state))
774 return 0;
775
776 if (s->get_latency)
777 return s->get_latency(s);
778
779 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
780 return 0;
781
782 return usec;
783 }
784
785 void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume) {
786 int changed;
787
788 pa_sink_assert_ref(s);
789 pa_assert(PA_SINK_LINKED(s->state));
790 pa_assert(volume);
791
792 changed = !pa_cvolume_equal(volume, &s->volume);
793 s->volume = *volume;
794
795 if (s->set_volume && s->set_volume(s) < 0)
796 s->set_volume = NULL;
797
798 if (!s->set_volume)
799 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), 0, NULL, pa_xfree);
800
801 if (changed)
802 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
803 }
804
805 const pa_cvolume *pa_sink_get_volume(pa_sink *s) {
806 struct pa_cvolume old_volume;
807
808 pa_sink_assert_ref(s);
809 pa_assert(PA_SINK_LINKED(s->state));
810
811 old_volume = s->volume;
812
813 if (s->get_volume && s->get_volume(s) < 0)
814 s->get_volume = NULL;
815
816 if (!s->get_volume && s->refresh_volume)
817 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, &s->volume, 0, NULL);
818
819 if (!pa_cvolume_equal(&old_volume, &s->volume))
820 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
821
822 return &s->volume;
823 }
824
825 void pa_sink_set_mute(pa_sink *s, pa_bool_t mute) {
826 int changed;
827
828 pa_sink_assert_ref(s);
829 pa_assert(PA_SINK_LINKED(s->state));
830
831 changed = s->muted != mute;
832 s->muted = mute;
833
834 if (s->set_mute && s->set_mute(s) < 0)
835 s->set_mute = NULL;
836
837 if (!s->set_mute)
838 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
839
840 if (changed)
841 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
842 }
843
844 pa_bool_t pa_sink_get_mute(pa_sink *s) {
845 pa_bool_t old_muted;
846
847 pa_sink_assert_ref(s);
848 pa_assert(PA_SINK_LINKED(s->state));
849
850 old_muted = s->muted;
851
852 if (s->get_mute && s->get_mute(s) < 0)
853 s->get_mute = NULL;
854
855 if (!s->get_mute && s->refresh_mute)
856 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MUTE, &s->muted, 0, NULL);
857
858 if (old_muted != s->muted)
859 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
860
861 return s->muted;
862 }
863
864 void pa_sink_set_description(pa_sink *s, const char *description) {
865 const char *old;
866 pa_sink_assert_ref(s);
867
868 if (!description && !pa_proplist_contains(s->proplist, PA_PROP_DEVICE_DESCRIPTION))
869 return;
870
871 old = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
872
873 if (old && description && !strcmp(old, description))
874 return;
875
876 if (description)
877 pa_proplist_sets(s->proplist, PA_PROP_DEVICE_DESCRIPTION, description);
878 else
879 pa_proplist_unset(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
880
881 if (s->monitor_source) {
882 char *n;
883
884 n = pa_sprintf_malloc("Monitor Source of %s", description ? description : s->name);
885 pa_source_set_description(s->monitor_source, n);
886 pa_xfree(n);
887 }
888
889 if (PA_SINK_LINKED(s->state)) {
890 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
891 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], s);
892 }
893 }
894
895 unsigned pa_sink_linked_by(pa_sink *s) {
896 unsigned ret;
897
898 pa_sink_assert_ref(s);
899 pa_assert(PA_SINK_LINKED(s->state));
900
901 ret = pa_idxset_size(s->inputs);
902
903 /* We add in the number of streams connected to us here. Please
904 * not the asymmmetry to pa_sink_used_by()! */
905
906 if (s->monitor_source)
907 ret += pa_source_linked_by(s->monitor_source);
908
909 return ret;
910 }
911
912 unsigned pa_sink_used_by(pa_sink *s) {
913 unsigned ret;
914
915 pa_sink_assert_ref(s);
916 pa_assert(PA_SINK_LINKED(s->state));
917
918 ret = pa_idxset_size(s->inputs);
919 pa_assert(ret >= s->n_corked);
920 ret -= s->n_corked;
921
922 /* Streams connected to our monitor source do not matter for
923 * pa_sink_used_by()!.*/
924
925 return ret;
926 }
927
928 int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
929 pa_sink *s = PA_SINK(o);
930 pa_sink_assert_ref(s);
931 pa_assert(s->thread_info.state != PA_SINK_UNLINKED);
932
933 switch ((pa_sink_message_t) code) {
934
935 case PA_SINK_MESSAGE_ADD_INPUT: {
936 pa_sink_input *i = PA_SINK_INPUT(userdata);
937
938 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
939
940 /* Since the caller sleeps in pa_sink_input_put(), we can
941 * safely access data outside of thread_info even though
942 * it is mutable */
943
944 if ((i->thread_info.sync_prev = i->sync_prev)) {
945 pa_assert(i->sink == i->thread_info.sync_prev->sink);
946 pa_assert(i->sync_prev->sync_next == i);
947 i->thread_info.sync_prev->thread_info.sync_next = i;
948 }
949
950 if ((i->thread_info.sync_next = i->sync_next)) {
951 pa_assert(i->sink == i->thread_info.sync_next->sink);
952 pa_assert(i->sync_next->sync_prev == i);
953 i->thread_info.sync_next->thread_info.sync_prev = i;
954 }
955
956 pa_sink_input_set_max_rewind(i, s->thread_info.max_rewind);
957
958 pa_assert(!i->thread_info.attached);
959 i->thread_info.attached = TRUE;
960
961 if (i->attach)
962 i->attach(i);
963
964 /* If you change anything here, make sure to change the
965 * ghost sink input handling a few lines down at
966 * PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, too. */
967
968 pa_sink_invalidate_requested_latency(s);
969
970 /* i->thread_info.ignore_rewind = TRUE; */
971 /* pa_sink_request_rewind(s, 0); */
972
973 return 0;
974 }
975
976 case PA_SINK_MESSAGE_REMOVE_INPUT: {
977 pa_sink_input *i = PA_SINK_INPUT(userdata);
978
979 /* If you change anything here, make sure to change the
980 * sink input handling a few lines down at
981 * PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, too. */
982
983 if (i->detach)
984 i->detach(i);
985
986 pa_assert(i->thread_info.attached);
987 i->thread_info.attached = FALSE;
988
989 /* Since the caller sleeps in pa_sink_input_unlink(),
990 * we can safely access data outside of thread_info even
991 * though it is mutable */
992
993 pa_assert(!i->thread_info.sync_prev);
994 pa_assert(!i->thread_info.sync_next);
995
996 if (i->thread_info.sync_prev) {
997 i->thread_info.sync_prev->thread_info.sync_next = i->thread_info.sync_prev->sync_next;
998 i->thread_info.sync_prev = NULL;
999 }
1000
1001 if (i->thread_info.sync_next) {
1002 i->thread_info.sync_next->thread_info.sync_prev = i->thread_info.sync_next->sync_prev;
1003 i->thread_info.sync_next = NULL;
1004 }
1005
1006 if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
1007 pa_sink_input_unref(i);
1008
1009 pa_sink_invalidate_requested_latency(s);
1010
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_set_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
1094 pa_sink_invalidate_requested_latency(s);
1095
1096 pa_sink_request_rewind(s, 0);
1097
1098 return 0;
1099 }
1100
1101 case PA_SINK_MESSAGE_SET_VOLUME:
1102 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
1103
1104 pa_sink_request_rewind(s, 0);
1105 return 0;
1106
1107 case PA_SINK_MESSAGE_SET_MUTE:
1108 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
1109
1110 pa_sink_request_rewind(s, 0);
1111 return 0;
1112
1113 case PA_SINK_MESSAGE_GET_VOLUME:
1114 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
1115 return 0;
1116
1117 case PA_SINK_MESSAGE_GET_MUTE:
1118 *((pa_bool_t*) userdata) = s->thread_info.soft_muted;
1119 return 0;
1120
1121 case PA_SINK_MESSAGE_PING:
1122 return 0;
1123
1124 case PA_SINK_MESSAGE_SET_STATE:
1125
1126 s->thread_info.state = PA_PTR_TO_UINT(userdata);
1127 return 0;
1128
1129 case PA_SINK_MESSAGE_DETACH:
1130
1131 /* We're detaching all our input streams so that the
1132 * asyncmsgq and rtpoll fields can be changed without
1133 * problems */
1134 pa_sink_detach_within_thread(s);
1135 break;
1136
1137 case PA_SINK_MESSAGE_ATTACH:
1138
1139 /* Reattach all streams */
1140 pa_sink_attach_within_thread(s);
1141 break;
1142
1143 case PA_SINK_MESSAGE_GET_LATENCY:
1144 case PA_SINK_MESSAGE_MAX:
1145 ;
1146 }
1147
1148 return -1;
1149 }
1150
1151 int pa_sink_suspend_all(pa_core *c, pa_bool_t suspend) {
1152 pa_sink *sink;
1153 uint32_t idx;
1154 int ret = 0;
1155
1156 pa_core_assert_ref(c);
1157
1158 for (sink = PA_SINK(pa_idxset_first(c->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(c->sinks, &idx)))
1159 ret -= pa_sink_suspend(sink, suspend) < 0;
1160
1161 return ret;
1162 }
1163
1164 void pa_sink_detach(pa_sink *s) {
1165 pa_sink_assert_ref(s);
1166 pa_assert(PA_SINK_LINKED(s->state));
1167
1168 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_DETACH, NULL, 0, NULL);
1169 }
1170
1171 void pa_sink_attach(pa_sink *s) {
1172 pa_sink_assert_ref(s);
1173 pa_assert(PA_SINK_LINKED(s->state));
1174
1175 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_ATTACH, NULL, 0, NULL);
1176 }
1177
1178 void pa_sink_detach_within_thread(pa_sink *s) {
1179 pa_sink_input *i;
1180 void *state = NULL;
1181
1182 pa_sink_assert_ref(s);
1183 pa_assert(PA_SINK_LINKED(s->thread_info.state));
1184
1185 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1186 if (i->detach)
1187 i->detach(i);
1188
1189 if (s->monitor_source)
1190 pa_source_detach_within_thread(s->monitor_source);
1191 }
1192
1193 void pa_sink_attach_within_thread(pa_sink *s) {
1194 pa_sink_input *i;
1195 void *state = NULL;
1196
1197 pa_sink_assert_ref(s);
1198 pa_assert(PA_SINK_LINKED(s->thread_info.state));
1199
1200 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1201 if (i->attach)
1202 i->attach(i);
1203
1204 if (s->monitor_source)
1205 pa_source_attach_within_thread(s->monitor_source);
1206 }
1207
1208 void pa_sink_request_rewind(pa_sink*s, size_t nbytes) {
1209 pa_sink_assert_ref(s);
1210 pa_assert(PA_SINK_LINKED(s->thread_info.state));
1211
1212 if (nbytes <= 0)
1213 nbytes = s->thread_info.max_rewind;
1214
1215 nbytes = PA_MIN(nbytes, s->thread_info.max_rewind);
1216
1217 if (nbytes <= s->thread_info.rewind_nbytes)
1218 return;
1219
1220 s->thread_info.rewind_nbytes = nbytes;
1221
1222 if (s->request_rewind)
1223 s->request_rewind(s);
1224 }
1225
1226 pa_usec_t pa_sink_get_requested_latency(pa_sink *s) {
1227 pa_usec_t result = 0;
1228 pa_sink_input *i;
1229 void *state = NULL;
1230
1231 pa_sink_assert_ref(s);
1232
1233 if (s->thread_info.requested_latency_valid)
1234 return s->thread_info.requested_latency;
1235
1236 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1237
1238 if (i->thread_info.requested_sink_latency > 0 &&
1239 (!result || result > i->thread_info.requested_sink_latency))
1240 result = i->thread_info.requested_sink_latency;
1241
1242 s->thread_info.requested_latency = result;
1243 s->thread_info.requested_latency_valid = TRUE;
1244
1245 return result;
1246 }
1247
1248 void pa_sink_set_max_rewind(pa_sink *s, size_t max_rewind) {
1249 pa_sink_input *i;
1250 void *state = NULL;
1251
1252 pa_sink_assert_ref(s);
1253 pa_assert(PA_SINK_LINKED(s->thread_info.state));
1254
1255 if (max_rewind == s->thread_info.max_rewind)
1256 return;
1257
1258 s->thread_info.max_rewind = max_rewind;
1259
1260 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
1261 pa_sink_input_set_max_rewind(i, s->thread_info.max_rewind);
1262 }
1263
1264 void pa_sink_invalidate_requested_latency(pa_sink *s) {
1265
1266 pa_sink_assert_ref(s);
1267 pa_assert(PA_SINK_LINKED(s->thread_info.state));
1268
1269 if (!s->thread_info.requested_latency_valid)
1270 return;
1271
1272 s->thread_info.requested_latency_valid = FALSE;
1273
1274 if (s->update_requested_latency)
1275 s->update_requested_latency(s);
1276 }