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