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