]> code.delx.au - pulseaudio/blob - src/pulsecore/source.c
make proplist inheritance scheme automatic and implicit
[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 (data->card)
162 pa_proplist_update(data->proplist, PA_UPDATE_MERGE, data->card->proplist);
163
164 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_FIXATE], data) < 0) {
165 pa_xfree(s);
166 pa_namereg_unregister(core, name);
167 return NULL;
168 }
169
170 s->parent.parent.free = source_free;
171 s->parent.process_msg = pa_source_process_msg;
172
173 s->core = core;
174 s->state = PA_SOURCE_INIT;
175 s->flags = flags;
176 s->name = pa_xstrdup(name);
177 s->proplist = pa_proplist_copy(data->proplist);
178 s->driver = pa_xstrdup(data->driver);
179 s->module = data->module;
180 s->card = data->card;
181
182 s->sample_spec = data->sample_spec;
183 s->channel_map = data->channel_map;
184
185 s->outputs = pa_idxset_new(NULL, NULL);
186 s->n_corked = 0;
187 s->monitor_of = NULL;
188
189 s->volume = data->volume;
190 s->muted = data->muted;
191 s->refresh_volume = s->refresh_muted = FALSE;
192 s->base_volume = PA_VOLUME_NORM;
193
194 reset_callbacks(s);
195 s->userdata = NULL;
196
197 s->asyncmsgq = NULL;
198 s->rtpoll = NULL;
199
200 pa_silence_memchunk_get(
201 &core->silence_cache,
202 core->mempool,
203 &s->silence,
204 &s->sample_spec,
205 0);
206
207 s->thread_info.outputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
208 pa_cvolume_reset(&s->thread_info.soft_volume, s->sample_spec.channels);
209 s->thread_info.soft_muted = FALSE;
210 s->thread_info.state = s->state;
211 s->thread_info.max_rewind = 0;
212 s->thread_info.requested_latency_valid = FALSE;
213 s->thread_info.requested_latency = 0;
214 s->thread_info.min_latency = DEFAULT_MIN_LATENCY;
215 s->thread_info.max_latency = 0;
216
217 pa_assert_se(pa_idxset_put(core->sources, s, &s->index) >= 0);
218
219 if (s->card)
220 pa_assert_se(pa_idxset_put(s->card->sources, s, NULL) >= 0);
221
222 pa_log_info("Created source %u \"%s\" with sample spec %s and channel map %s",
223 s->index,
224 s->name,
225 pa_sample_spec_snprint(st, sizeof(st), &s->sample_spec),
226 pa_channel_map_snprint(cm, sizeof(cm), &s->channel_map));
227
228 return s;
229 }
230
231 /* Called from main context */
232 static int source_set_state(pa_source *s, pa_source_state_t state) {
233 int ret;
234 pa_bool_t suspend_change;
235 pa_source_state_t original_state;
236
237 pa_assert(s);
238
239 if (s->state == state)
240 return 0;
241
242 original_state = s->state;
243
244 suspend_change =
245 (original_state == PA_SOURCE_SUSPENDED && PA_SOURCE_IS_OPENED(state)) ||
246 (PA_SOURCE_IS_OPENED(original_state) && state == PA_SOURCE_SUSPENDED);
247
248 if (s->set_state)
249 if ((ret = s->set_state(s, state)) < 0)
250 return ret;
251
252 if (s->asyncmsgq)
253 if ((ret = pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL)) < 0) {
254
255 if (s->set_state)
256 s->set_state(s, original_state);
257
258 return ret;
259 }
260
261 s->state = state;
262
263 if (suspend_change) {
264 pa_source_output *o;
265 uint32_t idx;
266
267 /* We're suspending or resuming, tell everyone about it */
268
269 for (o = PA_SOURCE_OUTPUT(pa_idxset_first(s->outputs, &idx)); o; o = PA_SOURCE_OUTPUT(pa_idxset_next(s->outputs, &idx)))
270 if (o->suspend)
271 o->suspend(o, state == PA_SOURCE_SUSPENDED);
272 }
273
274 if (state != PA_SOURCE_UNLINKED) /* if we enter UNLINKED state pa_source_unlink() will fire the apropriate events */
275 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], s);
276
277 return 0;
278 }
279
280 /* Called from main context */
281 void pa_source_put(pa_source *s) {
282 pa_source_assert_ref(s);
283
284 pa_assert(s->state == PA_SOURCE_INIT);
285
286 /* The following fields must be initialized properly when calling _put() */
287 pa_assert(s->asyncmsgq);
288 pa_assert(s->rtpoll);
289 pa_assert(!s->thread_info.min_latency || !s->thread_info.max_latency ||
290 s->thread_info.min_latency <= s->thread_info.max_latency);
291
292 if (!(s->flags & PA_SOURCE_HW_VOLUME_CTRL)) {
293 s->flags |= PA_SOURCE_DECIBEL_VOLUME;
294
295 s->thread_info.soft_volume = s->volume;
296 s->thread_info.soft_muted = s->muted;
297 }
298
299 pa_assert_se(source_set_state(s, PA_SOURCE_IDLE) == 0);
300
301 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
302 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_PUT], s);
303 }
304
305 /* Called from main context */
306 void pa_source_unlink(pa_source *s) {
307 pa_bool_t linked;
308 pa_source_output *o, *j = NULL;
309
310 pa_assert(s);
311
312 /* See pa_sink_unlink() for a couple of comments how this function
313 * works. */
314
315 linked = PA_SOURCE_IS_LINKED(s->state);
316
317 if (linked)
318 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], s);
319
320 if (s->state != PA_SOURCE_UNLINKED)
321 pa_namereg_unregister(s->core, s->name);
322 pa_idxset_remove_by_data(s->core->sources, s, NULL);
323
324 if (s->card)
325 pa_idxset_remove_by_data(s->card->sinks, s, NULL);
326
327 while ((o = pa_idxset_first(s->outputs, NULL))) {
328 pa_assert(o != j);
329 pa_source_output_kill(o);
330 j = o;
331 }
332
333 if (linked)
334 source_set_state(s, PA_SOURCE_UNLINKED);
335 else
336 s->state = PA_SOURCE_UNLINKED;
337
338 reset_callbacks(s);
339
340 if (linked) {
341 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
342 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK_POST], s);
343 }
344 }
345
346 /* Called from main context */
347 static void source_free(pa_object *o) {
348 pa_source_output *so;
349 pa_source *s = PA_SOURCE(o);
350
351 pa_assert(s);
352 pa_assert(pa_source_refcnt(s) == 0);
353
354 if (PA_SOURCE_IS_LINKED(s->state))
355 pa_source_unlink(s);
356
357 pa_log_info("Freeing source %u \"%s\"", s->index, s->name);
358
359 pa_idxset_free(s->outputs, NULL, NULL);
360
361 while ((so = pa_hashmap_steal_first(s->thread_info.outputs)))
362 pa_source_output_unref(so);
363
364 pa_hashmap_free(s->thread_info.outputs, NULL, NULL);
365
366 if (s->silence.memblock)
367 pa_memblock_unref(s->silence.memblock);
368
369 pa_xfree(s->name);
370 pa_xfree(s->driver);
371
372 if (s->proplist)
373 pa_proplist_free(s->proplist);
374
375 pa_xfree(s);
376 }
377
378 /* Called from main context */
379 void pa_source_set_asyncmsgq(pa_source *s, pa_asyncmsgq *q) {
380 pa_source_assert_ref(s);
381
382 s->asyncmsgq = q;
383 }
384
385 /* Called from main context */
386 void pa_source_set_rtpoll(pa_source *s, pa_rtpoll *p) {
387 pa_source_assert_ref(s);
388
389 s->rtpoll = p;
390 }
391
392 /* Called from main context */
393 int pa_source_update_status(pa_source*s) {
394 pa_source_assert_ref(s);
395 pa_assert(PA_SOURCE_IS_LINKED(s->state));
396
397 if (s->state == PA_SOURCE_SUSPENDED)
398 return 0;
399
400 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
401 }
402
403 /* Called from main context */
404 int pa_source_suspend(pa_source *s, pa_bool_t suspend) {
405 pa_source_assert_ref(s);
406 pa_assert(PA_SOURCE_IS_LINKED(s->state));
407
408 if (suspend)
409 return source_set_state(s, PA_SOURCE_SUSPENDED);
410 else
411 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
412 }
413
414 /* Called from IO thread context */
415 void pa_source_process_rewind(pa_source *s, size_t nbytes) {
416 pa_source_output *o;
417 void *state = NULL;
418
419 pa_source_assert_ref(s);
420 pa_assert(PA_SOURCE_IS_OPENED(s->thread_info.state));
421
422 if (nbytes <= 0)
423 return;
424
425 pa_log_debug("Processing rewind...");
426
427 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL))) {
428 pa_source_output_assert_ref(o);
429 pa_source_output_process_rewind(o, nbytes);
430 }
431 }
432
433 /* Called from IO thread context */
434 void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
435 pa_source_output *o;
436 void *state = NULL;
437
438 pa_source_assert_ref(s);
439 pa_assert(PA_SOURCE_IS_OPENED(s->thread_info.state));
440 pa_assert(chunk);
441
442 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&s->thread_info.soft_volume)) {
443 pa_memchunk vchunk = *chunk;
444
445 pa_memblock_ref(vchunk.memblock);
446 pa_memchunk_make_writable(&vchunk, 0);
447
448 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&s->thread_info.soft_volume))
449 pa_silence_memchunk(&vchunk, &s->sample_spec);
450 else
451 pa_volume_memchunk(&vchunk, &s->sample_spec, &s->thread_info.soft_volume);
452
453 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL))) {
454 pa_source_output_assert_ref(o);
455
456 if (!o->thread_info.direct_on_input)
457 pa_source_output_push(o, &vchunk);
458 }
459
460 pa_memblock_unref(vchunk.memblock);
461 } else {
462
463 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL))) {
464 pa_source_output_assert_ref(o);
465
466 if (!o->thread_info.direct_on_input)
467 pa_source_output_push(o, chunk);
468 }
469 }
470 }
471
472 /* Called from IO thread context */
473 void pa_source_post_direct(pa_source*s, pa_source_output *o, const pa_memchunk *chunk) {
474 pa_source_assert_ref(s);
475 pa_assert(PA_SOURCE_IS_OPENED(s->thread_info.state));
476 pa_source_output_assert_ref(o);
477 pa_assert(o->thread_info.direct_on_input);
478 pa_assert(chunk);
479
480 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&s->thread_info.soft_volume)) {
481 pa_memchunk vchunk = *chunk;
482
483 pa_memblock_ref(vchunk.memblock);
484 pa_memchunk_make_writable(&vchunk, 0);
485
486 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&s->thread_info.soft_volume))
487 pa_silence_memchunk(&vchunk, &s->sample_spec);
488 else
489 pa_volume_memchunk(&vchunk, &s->sample_spec, &s->thread_info.soft_volume);
490
491 pa_source_output_push(o, &vchunk);
492
493 pa_memblock_unref(vchunk.memblock);
494 } else
495 pa_source_output_push(o, chunk);
496 }
497
498 /* Called from main thread */
499 pa_usec_t pa_source_get_latency(pa_source *s) {
500 pa_usec_t usec;
501
502 pa_source_assert_ref(s);
503 pa_assert(PA_SOURCE_IS_LINKED(s->state));
504
505 if (!PA_SOURCE_IS_OPENED(s->state))
506 return 0;
507
508 if (!(s->flags & PA_SOURCE_LATENCY))
509 return 0;
510
511 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_LATENCY, &usec, 0, NULL) == 0);
512
513 return usec;
514 }
515
516 /* Called from main thread */
517 void pa_source_set_volume(pa_source *s, const pa_cvolume *volume) {
518 pa_bool_t changed;
519
520 pa_source_assert_ref(s);
521 pa_assert(PA_SOURCE_IS_LINKED(s->state));
522 pa_assert(volume);
523
524 changed = !pa_cvolume_equal(volume, &s->volume);
525 s->volume = *volume;
526
527 if (s->set_volume && s->set_volume(s) < 0)
528 s->set_volume = NULL;
529
530 if (!s->set_volume)
531 pa_source_set_soft_volume(s, volume);
532
533 if (changed)
534 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
535 }
536
537 /* Called from main thread */
538 void pa_source_set_soft_volume(pa_source *s, const pa_cvolume *volume) {
539 pa_source_assert_ref(s);
540 pa_assert(volume);
541
542 if (PA_SOURCE_IS_LINKED(s->state))
543 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_VOLUME, volume, 0, NULL);
544 else
545 s->thread_info.soft_volume = *volume;
546 }
547
548 /* Called from main thread */
549 const pa_cvolume *pa_source_get_volume(pa_source *s, pa_bool_t force_refresh) {
550 pa_source_assert_ref(s);
551 pa_assert(PA_SOURCE_IS_LINKED(s->state));
552
553 if (s->refresh_volume || force_refresh) {
554 pa_cvolume old_volume = s->volume;
555
556 if (s->get_volume && s->get_volume(s) < 0)
557 s->get_volume = NULL;
558
559 if (!s->get_volume)
560 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_VOLUME, &s->volume, 0, NULL);
561
562 if (!pa_cvolume_equal(&old_volume, &s->volume))
563 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
564 }
565
566 return &s->volume;
567 }
568
569 /* Called from main thread */
570 void pa_source_set_mute(pa_source *s, pa_bool_t mute) {
571 pa_bool_t changed;
572
573 pa_source_assert_ref(s);
574 pa_assert(PA_SOURCE_IS_LINKED(s->state));
575
576 changed = s->muted != mute;
577 s->muted = mute;
578
579 if (s->set_mute && s->set_mute(s) < 0)
580 s->set_mute = NULL;
581
582 if (!s->set_mute)
583 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
584
585 if (changed)
586 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
587 }
588
589 /* Called from main thread */
590 pa_bool_t pa_source_get_mute(pa_source *s, pa_bool_t force_refresh) {
591
592 pa_source_assert_ref(s);
593 pa_assert(PA_SOURCE_IS_LINKED(s->state));
594
595 if (s->refresh_muted || force_refresh) {
596 pa_bool_t old_muted = s->muted;
597
598 if (s->get_mute && s->get_mute(s) < 0)
599 s->get_mute = NULL;
600
601 if (!s->get_mute)
602 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_MUTE, &s->muted, 0, NULL);
603
604 if (old_muted != s->muted)
605 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
606 }
607
608 return s->muted;
609 }
610
611 pa_bool_t pa_source_update_proplist(pa_source *s, pa_update_mode_t mode, pa_proplist *p) {
612
613 pa_source_assert_ref(s);
614
615 pa_proplist_update(s->proplist, mode, p);
616
617 if (PA_SOURCE_IS_LINKED(s->state)) {
618 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED], s);
619 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
620 }
621
622 return TRUE;
623 }
624
625 /* Called from main thread */
626 void pa_source_set_description(pa_source *s, const char *description) {
627 const char *old;
628 pa_source_assert_ref(s);
629
630 if (!description && !pa_proplist_contains(s->proplist, PA_PROP_DEVICE_DESCRIPTION))
631 return;
632
633 old = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
634
635 if (old && description && !strcmp(old, description))
636 return;
637
638 if (description)
639 pa_proplist_sets(s->proplist, PA_PROP_DEVICE_DESCRIPTION, description);
640 else
641 pa_proplist_unset(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
642
643 if (PA_SOURCE_IS_LINKED(s->state)) {
644 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
645 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED], s);
646 }
647 }
648
649 /* Called from main thread */
650 unsigned pa_source_linked_by(pa_source *s) {
651 pa_source_assert_ref(s);
652 pa_assert(PA_SOURCE_IS_LINKED(s->state));
653
654 return pa_idxset_size(s->outputs);
655 }
656
657 /* Called from main thread */
658 unsigned pa_source_used_by(pa_source *s) {
659 unsigned ret;
660
661 pa_source_assert_ref(s);
662 pa_assert(PA_SOURCE_IS_LINKED(s->state));
663
664 ret = pa_idxset_size(s->outputs);
665 pa_assert(ret >= s->n_corked);
666
667 return ret - s->n_corked;
668 }
669
670 /* Called from main thread */
671 unsigned pa_source_check_suspend(pa_source *s) {
672 unsigned ret;
673 pa_source_output *o;
674 uint32_t idx;
675
676 pa_source_assert_ref(s);
677
678 if (!PA_SOURCE_IS_LINKED(s->state))
679 return 0;
680
681 ret = 0;
682
683 for (o = PA_SOURCE_OUTPUT(pa_idxset_first(s->outputs, &idx)); o; o = PA_SOURCE_OUTPUT(pa_idxset_next(s->outputs, &idx))) {
684 pa_source_output_state_t st;
685
686 st = pa_source_output_get_state(o);
687 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(st));
688
689 if (st == PA_SOURCE_OUTPUT_CORKED)
690 continue;
691
692 if (o->flags & PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND)
693 continue;
694
695 ret ++;
696 }
697
698 return ret;
699 }
700
701 /* Called from IO thread, except when it is not */
702 int pa_source_process_msg(pa_msgobject *object, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
703 pa_source *s = PA_SOURCE(object);
704 pa_source_assert_ref(s);
705
706 switch ((pa_source_message_t) code) {
707
708 case PA_SOURCE_MESSAGE_ADD_OUTPUT: {
709 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
710
711 pa_hashmap_put(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index), pa_source_output_ref(o));
712
713 if (o->direct_on_input) {
714 o->thread_info.direct_on_input = o->direct_on_input;
715 pa_hashmap_put(o->thread_info.direct_on_input->thread_info.direct_outputs, PA_UINT32_TO_PTR(o->index), o);
716 }
717
718 pa_assert(!o->thread_info.attached);
719 o->thread_info.attached = TRUE;
720
721 if (o->attach)
722 o->attach(o);
723
724 pa_source_output_set_state_within_thread(o, o->state);
725
726 if (o->thread_info.requested_source_latency != (pa_usec_t) -1)
727 pa_source_output_set_requested_latency_within_thread(o, o->thread_info.requested_source_latency);
728
729 pa_source_output_update_max_rewind(o, s->thread_info.max_rewind);
730
731 /* We don't just invalidate the requested latency here,
732 * because if we are in a move we might need to fix up the
733 * requested latency. */
734 pa_source_output_set_requested_latency_within_thread(o, o->thread_info.requested_source_latency);
735
736 return 0;
737 }
738
739 case PA_SOURCE_MESSAGE_REMOVE_OUTPUT: {
740 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
741
742 pa_source_output_set_state_within_thread(o, o->state);
743
744 if (o->detach)
745 o->detach(o);
746
747 pa_assert(o->thread_info.attached);
748 o->thread_info.attached = FALSE;
749
750 if (o->thread_info.direct_on_input) {
751 pa_hashmap_remove(o->thread_info.direct_on_input->thread_info.direct_outputs, PA_UINT32_TO_PTR(o->index));
752 o->thread_info.direct_on_input = NULL;
753 }
754
755 if (pa_hashmap_remove(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index)))
756 pa_source_output_unref(o);
757
758 pa_source_invalidate_requested_latency(s);
759
760 return 0;
761 }
762
763 case PA_SOURCE_MESSAGE_SET_VOLUME:
764 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
765 return 0;
766
767 case PA_SOURCE_MESSAGE_SET_MUTE:
768 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
769 return 0;
770
771 case PA_SOURCE_MESSAGE_GET_VOLUME:
772 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
773 return 0;
774
775 case PA_SOURCE_MESSAGE_GET_MUTE:
776 *((pa_bool_t*) userdata) = s->thread_info.soft_muted;
777 return 0;
778
779 case PA_SOURCE_MESSAGE_SET_STATE:
780 s->thread_info.state = PA_PTR_TO_UINT(userdata);
781 return 0;
782
783 case PA_SOURCE_MESSAGE_DETACH:
784
785 /* Detach all streams */
786 pa_source_detach_within_thread(s);
787 return 0;
788
789 case PA_SOURCE_MESSAGE_ATTACH:
790
791 /* Reattach all streams */
792 pa_source_attach_within_thread(s);
793 return 0;
794
795 case PA_SOURCE_MESSAGE_GET_REQUESTED_LATENCY: {
796
797 pa_usec_t *usec = userdata;
798 *usec = pa_source_get_requested_latency_within_thread(s);
799
800 if (*usec == (pa_usec_t) -1)
801 *usec = s->thread_info.max_latency;
802
803 return 0;
804 }
805
806 case PA_SOURCE_MESSAGE_SET_LATENCY_RANGE: {
807 pa_usec_t *r = userdata;
808
809 pa_source_update_latency_range(s, r[0], r[1]);
810
811 return 0;
812 }
813
814 case PA_SOURCE_MESSAGE_GET_LATENCY_RANGE: {
815 pa_usec_t *r = userdata;
816
817 r[0] = s->thread_info.min_latency;
818 r[1] = s->thread_info.max_latency;
819
820 return 0;
821 }
822
823 case PA_SOURCE_MESSAGE_GET_MAX_REWIND:
824
825 *((size_t*) userdata) = s->thread_info.max_rewind;
826 return 0;
827
828 case PA_SOURCE_MESSAGE_GET_LATENCY:
829
830 if (s->monitor_of) {
831 *((pa_usec_t*) userdata) = 0;
832 return 0;
833 }
834
835 /* Implementors need to overwrite this implementation! */
836 return -1;
837
838 case PA_SOURCE_MESSAGE_MAX:
839 ;
840 }
841
842 return -1;
843 }
844
845 /* Called from main thread */
846 int pa_source_suspend_all(pa_core *c, pa_bool_t suspend) {
847 uint32_t idx;
848 pa_source *source;
849 int ret = 0;
850
851 pa_core_assert_ref(c);
852
853 for (source = PA_SOURCE(pa_idxset_first(c->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(c->sources, &idx)))
854 ret -= pa_source_suspend(source, suspend) < 0;
855
856 return ret;
857 }
858
859 /* Called from main thread */
860 void pa_source_detach(pa_source *s) {
861 pa_source_assert_ref(s);
862 pa_assert(PA_SOURCE_IS_LINKED(s->state));
863
864 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_DETACH, NULL, 0, NULL) == 0);
865 }
866
867 /* Called from main thread */
868 void pa_source_attach(pa_source *s) {
869 pa_source_assert_ref(s);
870 pa_assert(PA_SOURCE_IS_LINKED(s->state));
871
872 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_ATTACH, NULL, 0, NULL) == 0);
873 }
874
875 /* Called from IO thread */
876 void pa_source_detach_within_thread(pa_source *s) {
877 pa_source_output *o;
878 void *state = NULL;
879
880 pa_source_assert_ref(s);
881 pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
882
883 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
884 if (o->detach)
885 o->detach(o);
886 }
887
888 /* Called from IO thread */
889 void pa_source_attach_within_thread(pa_source *s) {
890 pa_source_output *o;
891 void *state = NULL;
892
893 pa_source_assert_ref(s);
894 pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
895
896 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
897 if (o->attach)
898 o->attach(o);
899 }
900
901 /* Called from IO thread */
902 pa_usec_t pa_source_get_requested_latency_within_thread(pa_source *s) {
903 pa_usec_t result = (pa_usec_t) -1;
904 pa_source_output *o;
905 void *state = NULL;
906
907 pa_source_assert_ref(s);
908
909 if (s->thread_info.requested_latency_valid)
910 return s->thread_info.requested_latency;
911
912 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
913
914 if (o->thread_info.requested_source_latency != (pa_usec_t) -1 &&
915 (result == (pa_usec_t) -1 || result > o->thread_info.requested_source_latency))
916 result = o->thread_info.requested_source_latency;
917
918 if (result != (pa_usec_t) -1) {
919 if (s->thread_info.max_latency > 0 && result > s->thread_info.max_latency)
920 result = s->thread_info.max_latency;
921
922 if (s->thread_info.min_latency > 0 && result < s->thread_info.min_latency)
923 result = s->thread_info.min_latency;
924 }
925
926 s->thread_info.requested_latency = result;
927 s->thread_info.requested_latency_valid = TRUE;
928
929 return result;
930 }
931
932 /* Called from main thread */
933 pa_usec_t pa_source_get_requested_latency(pa_source *s) {
934 pa_usec_t usec;
935
936 pa_source_assert_ref(s);
937 pa_assert(PA_SOURCE_IS_LINKED(s->state));
938
939 if (!PA_SOURCE_IS_OPENED(s->state))
940 return 0;
941
942 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
943
944 return usec;
945 }
946
947 /* Called from IO thread */
948 void pa_source_set_max_rewind(pa_source *s, size_t max_rewind) {
949 pa_source_output *o;
950 void *state = NULL;
951
952 pa_source_assert_ref(s);
953
954 if (max_rewind == s->thread_info.max_rewind)
955 return;
956
957 s->thread_info.max_rewind = max_rewind;
958
959 if (PA_SOURCE_IS_LINKED(s->thread_info.state)) {
960 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
961 pa_source_output_update_max_rewind(o, s->thread_info.max_rewind);
962 }
963 }
964
965 void pa_source_invalidate_requested_latency(pa_source *s) {
966 pa_source_output *o;
967 void *state = NULL;
968
969 pa_source_assert_ref(s);
970
971 s->thread_info.requested_latency_valid = FALSE;
972
973 if (s->update_requested_latency)
974 s->update_requested_latency(s);
975
976 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
977 if (o->update_source_requested_latency)
978 o->update_source_requested_latency(o);
979
980 if (s->monitor_of)
981 pa_sink_invalidate_requested_latency(s->monitor_of);
982 }
983
984 void pa_source_set_latency_range(pa_source *s, pa_usec_t min_latency, pa_usec_t max_latency) {
985 pa_source_assert_ref(s);
986
987 /* min_latency == 0: no limit
988 * min_latency == (size_t) -1: default limit
989 * min_latency anything else: specified limit
990 *
991 * Similar for max_latency */
992
993 if (min_latency == (pa_usec_t) -1)
994 min_latency = DEFAULT_MIN_LATENCY;
995
996 if (max_latency == (pa_usec_t) -1)
997 max_latency = min_latency;
998
999 pa_assert(!min_latency || !max_latency ||
1000 min_latency <= max_latency);
1001
1002 if (PA_SOURCE_IS_LINKED(s->state)) {
1003 pa_usec_t r[2];
1004
1005 r[0] = min_latency;
1006 r[1] = max_latency;
1007
1008 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_LATENCY_RANGE, r, 0, NULL) == 0);
1009 } else {
1010 s->thread_info.min_latency = min_latency;
1011 s->thread_info.max_latency = max_latency;
1012
1013 s->thread_info.requested_latency_valid = FALSE;
1014 }
1015 }
1016
1017 void pa_source_get_latency_range(pa_source *s, pa_usec_t *min_latency, pa_usec_t *max_latency) {
1018 pa_source_assert_ref(s);
1019 pa_assert(min_latency);
1020 pa_assert(max_latency);
1021
1022 if (PA_SOURCE_IS_LINKED(s->state)) {
1023 pa_usec_t r[2] = { 0, 0 };
1024
1025 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_LATENCY_RANGE, r, 0, NULL) == 0);
1026
1027 *min_latency = r[0];
1028 *max_latency = r[1];
1029 } else {
1030 *min_latency = s->thread_info.min_latency;
1031 *max_latency = s->thread_info.max_latency;
1032 }
1033 }
1034
1035 /* Called from IO thread */
1036 void pa_source_update_latency_range(pa_source *s, pa_usec_t min_latency, pa_usec_t max_latency) {
1037 pa_source_output *o;
1038 void *state = NULL;
1039
1040 pa_source_assert_ref(s);
1041
1042 s->thread_info.min_latency = min_latency;
1043 s->thread_info.max_latency = max_latency;
1044
1045 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
1046 if (o->update_source_latency_range)
1047 o->update_source_latency_range(o);
1048
1049 pa_source_invalidate_requested_latency(s);
1050 }
1051
1052 size_t pa_source_get_max_rewind(pa_source *s) {
1053 size_t r;
1054 pa_source_assert_ref(s);
1055
1056 if (!PA_SOURCE_IS_LINKED(s->state))
1057 return s->thread_info.max_rewind;
1058
1059 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_MAX_REWIND, &r, 0, NULL) == 0);
1060
1061 return r;
1062 }