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