]> code.delx.au - pulseaudio/blob - src/pulsecore/source.c
Merge dead branch 'glitch-free'
[pulseaudio] / src / pulsecore / source.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <pulse/utf8.h>
32 #include <pulse/xmalloc.h>
33 #include <pulse/timeval.h>
34
35 #include <pulsecore/source-output.h>
36 #include <pulsecore/namereg.h>
37 #include <pulsecore/core-subscribe.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/sample-util.h>
40
41 #include "source.h"
42
43 #define DEFAULT_MIN_LATENCY (4*PA_USEC_PER_MSEC)
44
45 static PA_DEFINE_CHECK_TYPE(pa_source, pa_msgobject);
46
47 static void source_free(pa_object *o);
48
49 pa_source_new_data* pa_source_new_data_init(pa_source_new_data *data) {
50 pa_assert(data);
51
52 memset(data, 0, sizeof(*data));
53 data->proplist = pa_proplist_new();
54
55 return data;
56 }
57
58 void pa_source_new_data_set_name(pa_source_new_data *data, const char *name) {
59 pa_assert(data);
60
61 pa_xfree(data->name);
62 data->name = pa_xstrdup(name);
63 }
64
65 void pa_source_new_data_set_sample_spec(pa_source_new_data *data, const pa_sample_spec *spec) {
66 pa_assert(data);
67
68 if ((data->sample_spec_is_set = !!spec))
69 data->sample_spec = *spec;
70 }
71
72 void pa_source_new_data_set_channel_map(pa_source_new_data *data, const pa_channel_map *map) {
73 pa_assert(data);
74
75 if ((data->channel_map_is_set = !!map))
76 data->channel_map = *map;
77 }
78
79 void pa_source_new_data_set_volume(pa_source_new_data *data, const pa_cvolume *volume) {
80 pa_assert(data);
81
82 if ((data->volume_is_set = !!volume))
83 data->volume = *volume;
84 }
85
86 void pa_source_new_data_set_muted(pa_source_new_data *data, pa_bool_t mute) {
87 pa_assert(data);
88
89 data->muted_is_set = TRUE;
90 data->muted = !!mute;
91 }
92
93 void pa_source_new_data_done(pa_source_new_data *data) {
94 pa_assert(data);
95
96 pa_xfree(data->name);
97 pa_proplist_free(data->proplist);
98 }
99
100 /* Called from main context */
101 static void reset_callbacks(pa_source *s) {
102 pa_assert(s);
103
104 s->set_state = NULL;
105 s->get_volume = NULL;
106 s->set_volume = NULL;
107 s->get_mute = NULL;
108 s->set_mute = NULL;
109 s->update_requested_latency = NULL;
110 }
111
112 /* Called from main context */
113 pa_source* pa_source_new(
114 pa_core *core,
115 pa_source_new_data *data,
116 pa_source_flags_t flags) {
117
118 pa_source *s;
119 const char *name;
120 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
121
122 pa_assert(core);
123 pa_assert(data);
124 pa_assert(data->name);
125
126 s = pa_msgobject_new(pa_source);
127
128 if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_SOURCE, s, data->namereg_fail))) {
129 pa_xfree(s);
130 return NULL;
131 }
132
133 pa_source_new_data_set_name(data, name);
134
135 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_NEW], data) < 0) {
136 pa_xfree(s);
137 pa_namereg_unregister(core, name);
138 return NULL;
139 }
140
141 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
142 pa_return_null_if_fail(data->name && pa_utf8_valid(data->name) && data->name[0]);
143
144 pa_return_null_if_fail(data->sample_spec_is_set && pa_sample_spec_valid(&data->sample_spec));
145
146 if (!data->channel_map_is_set)
147 pa_return_null_if_fail(pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT));
148
149 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
150 pa_return_null_if_fail(data->channel_map.channels == data->sample_spec.channels);
151
152 if (!data->volume_is_set)
153 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
154
155 pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
156 pa_return_null_if_fail(data->volume.channels == data->sample_spec.channels);
157
158 if (!data->muted_is_set)
159 data->muted = FALSE;
160
161 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_FIXATE], data) < 0) {
162 pa_xfree(s);
163 pa_namereg_unregister(core, name);
164 return NULL;
165 }
166
167 s->parent.parent.free = source_free;
168 s->parent.process_msg = pa_source_process_msg;
169
170 s->core = core;
171 s->state = PA_SOURCE_INIT;
172 s->flags = flags;
173 s->name = pa_xstrdup(name);
174 s->proplist = pa_proplist_copy(data->proplist);
175 s->driver = pa_xstrdup(data->driver);
176 s->module = data->module;
177
178 s->sample_spec = data->sample_spec;
179 s->channel_map = data->channel_map;
180
181 s->outputs = pa_idxset_new(NULL, NULL);
182 s->n_corked = 0;
183 s->monitor_of = NULL;
184
185 s->volume = data->volume;
186 s->muted = data->muted;
187 s->refresh_volume = s->refresh_muted = FALSE;
188
189 reset_callbacks(s);
190 s->userdata = NULL;
191
192 s->asyncmsgq = NULL;
193 s->rtpoll = NULL;
194
195 pa_silence_memchunk_get(
196 &core->silence_cache,
197 core->mempool,
198 &s->silence,
199 &s->sample_spec,
200 0);
201
202 s->thread_info.outputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
203 pa_cvolume_reset(&s->thread_info.soft_volume, s->sample_spec.channels);
204 s->thread_info.soft_muted = FALSE;
205 s->thread_info.state = s->state;
206 s->thread_info.max_rewind = 0;
207 s->thread_info.requested_latency_valid = FALSE;
208 s->thread_info.requested_latency = 0;
209 s->thread_info.min_latency = DEFAULT_MIN_LATENCY;
210 s->thread_info.max_latency = 0;
211
212 pa_assert_se(pa_idxset_put(core->sources, s, &s->index) >= 0);
213
214 pa_log_info("Created source %u \"%s\" with sample spec %s and channel map %s",
215 s->index,
216 s->name,
217 pa_sample_spec_snprint(st, sizeof(st), &s->sample_spec),
218 pa_channel_map_snprint(cm, sizeof(cm), &s->channel_map));
219
220 return s;
221 }
222
223 /* Called from main context */
224 static int source_set_state(pa_source *s, pa_source_state_t state) {
225 int ret;
226 pa_bool_t suspend_change;
227
228 pa_assert(s);
229
230 if (s->state == state)
231 return 0;
232
233 suspend_change =
234 (s->state == PA_SOURCE_SUSPENDED && PA_SOURCE_IS_OPENED(state)) ||
235 (PA_SOURCE_IS_OPENED(s->state) && state == PA_SOURCE_SUSPENDED);
236
237 if (s->set_state)
238 if ((ret = s->set_state(s, state)) < 0)
239 return -1;
240
241 if (s->asyncmsgq)
242 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) == 0);
243
244 s->state = state;
245
246 if (suspend_change) {
247 pa_source_output *o;
248 uint32_t idx;
249
250 /* We're suspending or resuming, tell everyone about it */
251
252 for (o = PA_SOURCE_OUTPUT(pa_idxset_first(s->outputs, &idx)); o; o = PA_SOURCE_OUTPUT(pa_idxset_next(s->outputs, &idx)))
253 if (o->suspend)
254 o->suspend(o, state == PA_SINK_SUSPENDED);
255 }
256
257 if (state != PA_SOURCE_UNLINKED) /* if we enter UNLINKED state pa_source_unlink() will fire the apropriate events */
258 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], s);
259
260 return 0;
261 }
262
263 /* Called from main context */
264 void pa_source_put(pa_source *s) {
265 pa_source_assert_ref(s);
266
267 pa_assert(s->state == PA_SINK_INIT);
268
269 /* The following fields must be initialized properly when calling _put() */
270 pa_assert(s->asyncmsgq);
271 pa_assert(s->rtpoll);
272 pa_assert(!s->thread_info.min_latency || !s->thread_info.max_latency ||
273 s->thread_info.min_latency <= s->thread_info.max_latency);
274
275 if (!(s->flags & PA_SOURCE_HW_VOLUME_CTRL)) {
276 s->flags |= PA_SOURCE_DECIBEL_VOLUME;
277
278 s->thread_info.soft_volume = s->volume;
279 s->thread_info.soft_muted = s->muted;
280 }
281
282 pa_assert_se(source_set_state(s, PA_SOURCE_IDLE) == 0);
283
284 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
285 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_PUT], s);
286 }
287
288 /* Called from main context */
289 void pa_source_unlink(pa_source *s) {
290 pa_bool_t linked;
291 pa_source_output *o, *j = NULL;
292
293 pa_assert(s);
294
295 /* See pa_sink_unlink() for a couple of comments how this function
296 * works. */
297
298 linked = PA_SOURCE_IS_LINKED(s->state);
299
300 if (linked)
301 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], s);
302
303 if (s->state != PA_SOURCE_UNLINKED)
304 pa_namereg_unregister(s->core, s->name);
305 pa_idxset_remove_by_data(s->core->sources, s, NULL);
306
307 while ((o = pa_idxset_first(s->outputs, NULL))) {
308 pa_assert(o != j);
309 pa_source_output_kill(o);
310 j = o;
311 }
312
313 if (linked)
314 source_set_state(s, PA_SOURCE_UNLINKED);
315 else
316 s->state = PA_SOURCE_UNLINKED;
317
318 reset_callbacks(s);
319
320 if (linked) {
321 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
322 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK_POST], s);
323 }
324 }
325
326 /* Called from main context */
327 static void source_free(pa_object *o) {
328 pa_source_output *so;
329 pa_source *s = PA_SOURCE(o);
330
331 pa_assert(s);
332 pa_assert(pa_source_refcnt(s) == 0);
333
334 if (PA_SOURCE_IS_LINKED(s->state))
335 pa_source_unlink(s);
336
337 pa_log_info("Freeing source %u \"%s\"", s->index, s->name);
338
339 pa_idxset_free(s->outputs, NULL, NULL);
340
341 while ((so = pa_hashmap_steal_first(s->thread_info.outputs)))
342 pa_source_output_unref(so);
343
344 pa_hashmap_free(s->thread_info.outputs, NULL, NULL);
345
346 if (s->silence.memblock)
347 pa_memblock_unref(s->silence.memblock);
348
349 pa_xfree(s->name);
350 pa_xfree(s->driver);
351
352 if (s->proplist)
353 pa_proplist_free(s->proplist);
354
355 pa_xfree(s);
356 }
357
358 /* Called from main context */
359 void pa_source_set_asyncmsgq(pa_source *s, pa_asyncmsgq *q) {
360 pa_source_assert_ref(s);
361
362 s->asyncmsgq = q;
363 }
364
365 /* Called from main context */
366 void pa_source_set_rtpoll(pa_source *s, pa_rtpoll *p) {
367 pa_source_assert_ref(s);
368
369 s->rtpoll = p;
370 }
371
372 /* Called from main context */
373 int pa_source_update_status(pa_source*s) {
374 pa_source_assert_ref(s);
375 pa_assert(PA_SOURCE_IS_LINKED(s->state));
376
377 if (s->state == PA_SOURCE_SUSPENDED)
378 return 0;
379
380 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
381 }
382
383 /* Called from main context */
384 int pa_source_suspend(pa_source *s, pa_bool_t suspend) {
385 pa_source_assert_ref(s);
386 pa_assert(PA_SOURCE_IS_LINKED(s->state));
387
388 if (suspend)
389 return source_set_state(s, PA_SOURCE_SUSPENDED);
390 else
391 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
392 }
393
394 /* Called from IO thread context */
395 void pa_source_process_rewind(pa_source *s, size_t nbytes) {
396 pa_source_output *o;
397 void *state = NULL;
398
399 pa_source_assert_ref(s);
400 pa_assert(PA_SOURCE_IS_OPENED(s->thread_info.state));
401
402 if (nbytes <= 0)
403 return;
404
405 pa_log_debug("Processing rewind...");
406
407 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL))) {
408 pa_source_output_assert_ref(o);
409 pa_source_output_process_rewind(o, nbytes);
410 }
411 }
412
413 /* Called from IO thread context */
414 void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
415 pa_source_output *o;
416 void *state = NULL;
417
418 pa_source_assert_ref(s);
419 pa_assert(PA_SOURCE_IS_OPENED(s->thread_info.state));
420 pa_assert(chunk);
421
422 if (s->thread_info.state != PA_SOURCE_RUNNING)
423 return;
424
425 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&s->thread_info.soft_volume)) {
426 pa_memchunk vchunk = *chunk;
427
428 pa_memblock_ref(vchunk.memblock);
429 pa_memchunk_make_writable(&vchunk, 0);
430
431 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&s->thread_info.soft_volume))
432 pa_silence_memchunk(&vchunk, &s->sample_spec);
433 else
434 pa_volume_memchunk(&vchunk, &s->sample_spec, &s->thread_info.soft_volume);
435
436 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL))) {
437 pa_source_output_assert_ref(o);
438
439 if (!o->thread_info.direct_on_input)
440 pa_source_output_push(o, &vchunk);
441 }
442
443 pa_memblock_unref(vchunk.memblock);
444 } else {
445
446 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL))) {
447 pa_source_output_assert_ref(o);
448
449 if (!o->thread_info.direct_on_input)
450 pa_source_output_push(o, chunk);
451 }
452 }
453 }
454
455 /* Called from IO thread context */
456 void pa_source_post_direct(pa_source*s, pa_source_output *o, const pa_memchunk *chunk) {
457 pa_source_assert_ref(s);
458 pa_assert(PA_SOURCE_IS_OPENED(s->thread_info.state));
459 pa_source_output_assert_ref(o);
460 pa_assert(o->thread_info.direct_on_input);
461 pa_assert(chunk);
462
463 if (s->thread_info.state != PA_SOURCE_RUNNING)
464 return;
465
466 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&s->thread_info.soft_volume)) {
467 pa_memchunk vchunk = *chunk;
468
469 pa_memblock_ref(vchunk.memblock);
470 pa_memchunk_make_writable(&vchunk, 0);
471
472 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&s->thread_info.soft_volume))
473 pa_silence_memchunk(&vchunk, &s->sample_spec);
474 else
475 pa_volume_memchunk(&vchunk, &s->sample_spec, &s->thread_info.soft_volume);
476
477 pa_source_output_push(o, &vchunk);
478
479 pa_memblock_unref(vchunk.memblock);
480 } else
481 pa_source_output_push(o, chunk);
482 }
483
484 /* Called from main thread */
485 pa_usec_t pa_source_get_latency(pa_source *s) {
486 pa_usec_t usec;
487
488 pa_source_assert_ref(s);
489 pa_assert(PA_SOURCE_IS_LINKED(s->state));
490
491 if (!PA_SOURCE_IS_OPENED(s->state))
492 return 0;
493
494 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_LATENCY, &usec, 0, NULL) == 0);
495
496 return usec;
497 }
498
499 /* Called from main thread */
500 void pa_source_set_volume(pa_source *s, const pa_cvolume *volume) {
501 pa_bool_t changed;
502
503 pa_source_assert_ref(s);
504 pa_assert(PA_SOURCE_IS_LINKED(s->state));
505 pa_assert(volume);
506
507 changed = !pa_cvolume_equal(volume, &s->volume);
508 s->volume = *volume;
509
510 if (s->set_volume && s->set_volume(s) < 0)
511 s->set_volume = NULL;
512
513 if (!s->set_volume)
514 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_VOLUME, volume, 0, NULL);
515
516 if (changed)
517 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
518 }
519
520 /* Called from main thread */
521 const pa_cvolume *pa_source_get_volume(pa_source *s) {
522 pa_source_assert_ref(s);
523 pa_assert(PA_SOURCE_IS_LINKED(s->state));
524
525 if (s->refresh_volume) {
526 pa_cvolume old_volume = s->volume;
527
528 if (s->get_volume && s->get_volume(s) < 0)
529 s->get_volume = NULL;
530
531 if (!s->get_volume)
532 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_VOLUME, &s->volume, 0, NULL);
533
534 if (!pa_cvolume_equal(&old_volume, &s->volume))
535 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
536 }
537
538 return &s->volume;
539 }
540
541 /* Called from main thread */
542 void pa_source_set_mute(pa_source *s, pa_bool_t mute) {
543 pa_bool_t changed;
544
545 pa_source_assert_ref(s);
546 pa_assert(PA_SOURCE_IS_LINKED(s->state));
547
548 changed = s->muted != mute;
549 s->muted = mute;
550
551 if (s->set_mute && s->set_mute(s) < 0)
552 s->set_mute = NULL;
553
554 if (!s->set_mute)
555 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
556
557 if (changed)
558 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
559 }
560
561 /* Called from main thread */
562 pa_bool_t pa_source_get_mute(pa_source *s) {
563
564 pa_source_assert_ref(s);
565 pa_assert(PA_SOURCE_IS_LINKED(s->state));
566
567 if (s->refresh_muted) {
568 pa_bool_t old_muted = s->muted;
569
570 if (s->get_mute && s->get_mute(s) < 0)
571 s->get_mute = NULL;
572
573 if (!s->get_mute)
574 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_MUTE, &s->muted, 0, NULL);
575
576 if (old_muted != s->muted)
577 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
578 }
579
580 return s->muted;
581 }
582
583 /* Called from main thread */
584 void pa_source_set_description(pa_source *s, const char *description) {
585 const char *old;
586 pa_source_assert_ref(s);
587
588 if (!description && !pa_proplist_contains(s->proplist, PA_PROP_DEVICE_DESCRIPTION))
589 return;
590
591 old = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
592
593 if (old && description && !strcmp(old, description))
594 return;
595
596 if (description)
597 pa_proplist_sets(s->proplist, PA_PROP_DEVICE_DESCRIPTION, description);
598 else
599 pa_proplist_unset(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
600
601 if (PA_SOURCE_IS_LINKED(s->state)) {
602 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
603 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED], s);
604 }
605 }
606
607 /* Called from main thread */
608 unsigned pa_source_linked_by(pa_source *s) {
609 pa_source_assert_ref(s);
610 pa_assert(PA_SOURCE_IS_LINKED(s->state));
611
612 return pa_idxset_size(s->outputs);
613 }
614
615 /* Called from main thread */
616 unsigned pa_source_used_by(pa_source *s) {
617 unsigned ret;
618
619 pa_source_assert_ref(s);
620 pa_assert(PA_SOURCE_IS_LINKED(s->state));
621
622 ret = pa_idxset_size(s->outputs);
623 pa_assert(ret >= s->n_corked);
624
625 return ret - s->n_corked;
626 }
627
628 /* Called from IO thread, except when it is not */
629 int pa_source_process_msg(pa_msgobject *object, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
630 pa_source *s = PA_SOURCE(object);
631 pa_source_assert_ref(s);
632
633 switch ((pa_source_message_t) code) {
634
635 case PA_SOURCE_MESSAGE_ADD_OUTPUT: {
636 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
637
638 pa_hashmap_put(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index), pa_source_output_ref(o));
639
640 if (o->direct_on_input) {
641 o->thread_info.direct_on_input = o->direct_on_input;
642 pa_hashmap_put(o->thread_info.direct_on_input->thread_info.direct_outputs, PA_UINT32_TO_PTR(o->index), o);
643 }
644
645 pa_assert(!o->thread_info.attached);
646 o->thread_info.attached = TRUE;
647
648 if (o->attach)
649 o->attach(o);
650
651 pa_source_output_set_state_within_thread(o, o->state);
652
653 pa_source_output_update_max_rewind(o, s->thread_info.max_rewind);
654
655 /* We don't just invalidate the requested latency here,
656 * because if we are in a move we might need to fix up the
657 * requested latency. */
658 pa_source_output_set_requested_latency_within_thread(o, o->thread_info.requested_source_latency);
659
660 return 0;
661 }
662
663 case PA_SOURCE_MESSAGE_REMOVE_OUTPUT: {
664 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
665
666 pa_source_output_set_state_within_thread(o, o->state);
667
668 if (o->detach)
669 o->detach(o);
670
671 pa_assert(o->thread_info.attached);
672 o->thread_info.attached = FALSE;
673
674 if (o->thread_info.direct_on_input) {
675 pa_hashmap_remove(o->thread_info.direct_on_input->thread_info.direct_outputs, PA_UINT32_TO_PTR(o->index));
676 o->thread_info.direct_on_input = NULL;
677 }
678
679 if (pa_hashmap_remove(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index)))
680 pa_source_output_unref(o);
681
682 pa_source_invalidate_requested_latency(s);
683
684 return 0;
685 }
686
687 case PA_SOURCE_MESSAGE_SET_VOLUME:
688 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
689 return 0;
690
691 case PA_SOURCE_MESSAGE_SET_MUTE:
692 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
693 return 0;
694
695 case PA_SOURCE_MESSAGE_GET_VOLUME:
696 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
697 return 0;
698
699 case PA_SOURCE_MESSAGE_GET_MUTE:
700 *((pa_bool_t*) userdata) = s->thread_info.soft_muted;
701 return 0;
702
703 case PA_SOURCE_MESSAGE_SET_STATE:
704 s->thread_info.state = PA_PTR_TO_UINT(userdata);
705 return 0;
706
707 case PA_SOURCE_MESSAGE_DETACH:
708
709 /* Detach all streams */
710 pa_source_detach_within_thread(s);
711 return 0;
712
713 case PA_SOURCE_MESSAGE_ATTACH:
714
715 /* Reattach all streams */
716 pa_source_attach_within_thread(s);
717 return 0;
718
719 case PA_SOURCE_MESSAGE_GET_REQUESTED_LATENCY: {
720
721 pa_usec_t *usec = userdata;
722 *usec = pa_source_get_requested_latency_within_thread(s);
723
724 if (*usec == (pa_usec_t) -1)
725 *usec = s->thread_info.max_latency;
726
727 return 0;
728 }
729
730 case PA_SOURCE_MESSAGE_SET_LATENCY_RANGE: {
731 pa_usec_t *r = userdata;
732
733 pa_source_update_latency_range(s, r[0], r[1]);
734
735 return 0;
736 }
737
738 case PA_SOURCE_MESSAGE_GET_LATENCY_RANGE: {
739 pa_usec_t *r = userdata;
740
741 r[0] = s->thread_info.min_latency;
742 r[1] = s->thread_info.max_latency;
743
744 return 0;
745 }
746
747 case PA_SOURCE_MESSAGE_GET_MAX_REWIND:
748
749 *((size_t*) userdata) = s->thread_info.max_rewind;
750 return 0;
751
752 case PA_SOURCE_MESSAGE_GET_LATENCY:
753
754 if (s->monitor_of) {
755 *((pa_usec_t*) userdata) = 0;
756 return 0;
757 }
758
759 /* Implementors need to overwrite this implementation! */
760 return -1;
761
762 case PA_SOURCE_MESSAGE_MAX:
763 ;
764 }
765
766 return -1;
767 }
768
769 /* Called from main thread */
770 int pa_source_suspend_all(pa_core *c, pa_bool_t suspend) {
771 uint32_t idx;
772 pa_source *source;
773 int ret = 0;
774
775 pa_core_assert_ref(c);
776
777 for (source = PA_SOURCE(pa_idxset_first(c->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(c->sources, &idx)))
778 ret -= pa_source_suspend(source, suspend) < 0;
779
780 return ret;
781 }
782
783 /* Called from main thread */
784 void pa_source_detach(pa_source *s) {
785 pa_source_assert_ref(s);
786 pa_assert(PA_SOURCE_IS_LINKED(s->state));
787
788 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_DETACH, NULL, 0, NULL) == 0);
789 }
790
791 /* Called from main thread */
792 void pa_source_attach(pa_source *s) {
793 pa_source_assert_ref(s);
794 pa_assert(PA_SOURCE_IS_LINKED(s->state));
795
796 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_ATTACH, NULL, 0, NULL) == 0);
797 }
798
799 /* Called from IO thread */
800 void pa_source_detach_within_thread(pa_source *s) {
801 pa_source_output *o;
802 void *state = NULL;
803
804 pa_source_assert_ref(s);
805 pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
806
807 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
808 if (o->detach)
809 o->detach(o);
810 }
811
812 /* Called from IO thread */
813 void pa_source_attach_within_thread(pa_source *s) {
814 pa_source_output *o;
815 void *state = NULL;
816
817 pa_source_assert_ref(s);
818 pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
819
820 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
821 if (o->attach)
822 o->attach(o);
823 }
824
825 /* Called from IO thread */
826 pa_usec_t pa_source_get_requested_latency_within_thread(pa_source *s) {
827 pa_usec_t result = (pa_usec_t) -1;
828 pa_source_output *o;
829 void *state = NULL;
830
831 pa_source_assert_ref(s);
832
833 if (s->thread_info.requested_latency_valid)
834 return s->thread_info.requested_latency;
835
836 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
837
838 if (o->thread_info.requested_source_latency != (pa_usec_t) -1 &&
839 (result == (pa_usec_t) -1 || result > o->thread_info.requested_source_latency))
840 result = o->thread_info.requested_source_latency;
841
842 if (result != (pa_usec_t) -1) {
843 if (s->thread_info.max_latency > 0 && result > s->thread_info.max_latency)
844 result = s->thread_info.max_latency;
845
846 if (s->thread_info.min_latency > 0 && result < s->thread_info.min_latency)
847 result = s->thread_info.min_latency;
848 }
849
850 s->thread_info.requested_latency = result;
851 s->thread_info.requested_latency_valid = TRUE;
852
853 return result;
854 }
855
856 /* Called from main thread */
857 pa_usec_t pa_source_get_requested_latency(pa_source *s) {
858 pa_usec_t usec;
859
860 pa_source_assert_ref(s);
861 pa_assert(PA_SOURCE_IS_LINKED(s->state));
862
863 if (!PA_SOURCE_IS_OPENED(s->state))
864 return 0;
865
866 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
867
868 return usec;
869 }
870
871 /* Called from IO thread */
872 void pa_source_set_max_rewind(pa_source *s, size_t max_rewind) {
873 pa_source_output *o;
874 void *state = NULL;
875
876 pa_source_assert_ref(s);
877
878 if (max_rewind == s->thread_info.max_rewind)
879 return;
880
881 s->thread_info.max_rewind = max_rewind;
882
883 if (PA_SOURCE_IS_LINKED(s->thread_info.state)) {
884 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
885 pa_source_output_update_max_rewind(o, s->thread_info.max_rewind);
886 }
887 }
888
889 void pa_source_invalidate_requested_latency(pa_source *s) {
890 pa_source_output *o;
891 void *state = NULL;
892
893 pa_source_assert_ref(s);
894
895 s->thread_info.requested_latency_valid = FALSE;
896
897 if (s->update_requested_latency)
898 s->update_requested_latency(s);
899
900 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
901 o->update_source_requested_latency(o);
902
903 if (s->monitor_of)
904 pa_sink_invalidate_requested_latency(s->monitor_of);
905 }
906
907 void pa_source_set_latency_range(pa_source *s, pa_usec_t min_latency, pa_usec_t max_latency) {
908 pa_source_assert_ref(s);
909
910 /* min_latency == 0: no limit
911 * min_latency == (size_t) -1: default limit
912 * min_latency anything else: specified limit
913 *
914 * Similar for max_latency */
915
916 if (min_latency == (pa_usec_t) -1)
917 min_latency = DEFAULT_MIN_LATENCY;
918
919 if (max_latency == (pa_usec_t) -1)
920 max_latency = min_latency;
921
922 pa_assert(!min_latency || !max_latency ||
923 min_latency <= max_latency);
924
925 if (PA_SINK_IS_LINKED(s->state)) {
926 pa_usec_t r[2];
927
928 r[0] = min_latency;
929 r[1] = max_latency;
930
931 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_LATENCY_RANGE, r, 0, NULL) == 0);
932 } else {
933 s->thread_info.min_latency = min_latency;
934 s->thread_info.max_latency = max_latency;
935
936 s->thread_info.requested_latency_valid = FALSE;
937 }
938 }
939
940 void pa_source_get_latency_range(pa_source *s, pa_usec_t *min_latency, pa_usec_t *max_latency) {
941 pa_source_assert_ref(s);
942 pa_assert(min_latency);
943 pa_assert(max_latency);
944
945 if (PA_SOURCE_IS_LINKED(s->state)) {
946 pa_usec_t r[2] = { 0, 0 };
947
948 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_LATENCY_RANGE, r, 0, NULL) == 0);
949
950 *min_latency = r[0];
951 *max_latency = r[1];
952 } else {
953 *min_latency = s->thread_info.min_latency;
954 *max_latency = s->thread_info.max_latency;
955 }
956 }
957
958 /* Called from IO thread */
959 void pa_source_update_latency_range(pa_source *s, pa_usec_t min_latency, pa_usec_t max_latency) {
960 pa_source_output *o;
961 void *state = NULL;
962
963 pa_source_assert_ref(s);
964
965 s->thread_info.min_latency = min_latency;
966 s->thread_info.max_latency = max_latency;
967
968 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
969 if (o->update_source_latency_range)
970 o->update_source_latency_range(o);
971
972 pa_source_invalidate_requested_latency(s);
973 }
974
975 size_t pa_source_get_max_rewind(pa_source *s) {
976 size_t r;
977 pa_source_assert_ref(s);
978
979 if (!PA_SOURCE_IS_LINKED(s->state))
980 return s->thread_info.max_rewind;
981
982 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_MAX_REWIND, &r, 0, NULL) == 0);
983
984 return r;
985 }