22dcd7526ccee84a053b9a4299b0949d8ed34225
[platform/upstream/pulseaudio.git] / src / pulsecore / core.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.1 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, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <signal.h>
28
29 #include <pulse/rtclock.h>
30 #include <pulse/timeval.h>
31 #include <pulse/xmalloc.h>
32
33 #include <pulsecore/module.h>
34 #include <pulsecore/core-rtclock.h>
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/core-scache.h>
37 #include <pulsecore/core-subscribe.h>
38 #include <pulsecore/random.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/macro.h>
41
42 #include "core.h"
43
44 PA_DEFINE_PUBLIC_CLASS(pa_core, pa_msgobject);
45
46 static int core_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
47     pa_core *c = PA_CORE(o);
48
49     pa_core_assert_ref(c);
50
51     switch (code) {
52
53         case PA_CORE_MESSAGE_UNLOAD_MODULE:
54             pa_module_unload(userdata, true);
55             return 0;
56
57         default:
58             return -1;
59     }
60 }
61
62 static void core_free(pa_object *o);
63
64 pa_core* pa_core_new(pa_mainloop_api *m, bool shared, bool enable_memfd, size_t shm_size) {
65     pa_core* c;
66     pa_mempool *pool;
67     pa_mem_type_t type;
68     int j;
69
70     pa_assert(m);
71
72     if (shared) {
73         type = (enable_memfd) ? PA_MEM_TYPE_SHARED_MEMFD : PA_MEM_TYPE_SHARED_POSIX;
74         if (!(pool = pa_mempool_new(type, shm_size, false))) {
75             pa_log_warn("Failed to allocate %s memory pool. Falling back to a normal memory pool.",
76                         pa_mem_type_to_string(type));
77             shared = false;
78         }
79     }
80
81     if (!shared) {
82         if (!(pool = pa_mempool_new(PA_MEM_TYPE_PRIVATE, shm_size, false))) {
83             pa_log("pa_mempool_new() failed.");
84             return NULL;
85         }
86     }
87
88     c = pa_msgobject_new(pa_core);
89     c->parent.parent.free = core_free;
90     c->parent.process_msg = core_process_msg;
91
92     c->state = PA_CORE_STARTUP;
93     c->mainloop = m;
94
95     c->clients = pa_idxset_new(NULL, NULL);
96     c->cards = pa_idxset_new(NULL, NULL);
97     c->sinks = pa_idxset_new(NULL, NULL);
98     c->sources = pa_idxset_new(NULL, NULL);
99     c->sink_inputs = pa_idxset_new(NULL, NULL);
100     c->source_outputs = pa_idxset_new(NULL, NULL);
101     c->modules = pa_idxset_new(NULL, NULL);
102     c->scache = pa_idxset_new(NULL, NULL);
103
104     c->namereg = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
105     c->shared = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
106
107     c->default_source = NULL;
108     c->default_sink = NULL;
109
110     c->default_sample_spec.format = PA_SAMPLE_S16NE;
111     c->default_sample_spec.rate = 44100;
112     c->default_sample_spec.channels = 2;
113     pa_channel_map_init_extend(&c->default_channel_map, c->default_sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
114     c->default_n_fragments = 4;
115     c->default_fragment_size_msec = 25;
116
117     c->deferred_volume_safety_margin_usec = 8000;
118     c->deferred_volume_extra_delay_usec = 0;
119
120     c->module_defer_unload_event = NULL;
121     c->modules_pending_unload = pa_hashmap_new(NULL, NULL);
122
123     c->subscription_defer_event = NULL;
124     PA_LLIST_HEAD_INIT(pa_subscription, c->subscriptions);
125     PA_LLIST_HEAD_INIT(pa_subscription_event, c->subscription_event_queue);
126     c->subscription_event_last = NULL;
127
128     c->mempool = pool;
129     c->shm_size = shm_size;
130     pa_silence_cache_init(&c->silence_cache);
131
132     c->exit_event = NULL;
133     c->scache_auto_unload_event = NULL;
134
135     c->exit_idle_time = -1;
136     c->scache_idle_time = 20;
137
138     c->flat_volumes = true;
139     c->disallow_module_loading = false;
140     c->disallow_exit = false;
141     c->running_as_daemon = false;
142     c->realtime_scheduling = false;
143     c->realtime_priority = 5;
144     c->disable_remixing = false;
145     c->remixing_use_all_sink_channels = true;
146     c->disable_lfe_remixing = true;
147     c->lfe_crossover_freq = 0;
148     c->deferred_volume = true;
149     c->resample_method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 1;
150
151     for (j = 0; j < PA_CORE_HOOK_MAX; j++)
152         pa_hook_init(&c->hooks[j], c);
153
154     pa_random(&c->cookie, sizeof(c->cookie));
155
156 #ifdef SIGPIPE
157     pa_check_signal_is_blocked(SIGPIPE);
158 #endif
159
160     pa_core_check_idle(c);
161
162     c->state = PA_CORE_RUNNING;
163 #ifdef TIZEN_PCM_DUMP
164     c->pcm_dump = 0;
165     c->pcm_dump_option = 0;
166 #endif
167 #ifdef TIZEN_EMPTY_POP
168     c->empty_pop_threshold = 10;
169 #endif
170
171     return c;
172 }
173
174 static void core_free(pa_object *o) {
175     pa_core *c = PA_CORE(o);
176     int j;
177     pa_assert(c);
178
179     c->state = PA_CORE_SHUTDOWN;
180
181     /* Note: All modules and samples in the cache should be unloaded before
182      * we get here */
183
184     pa_assert(pa_idxset_isempty(c->scache));
185     pa_idxset_free(c->scache, NULL);
186
187     pa_assert(pa_idxset_isempty(c->modules));
188     pa_idxset_free(c->modules, NULL);
189
190     pa_assert(pa_idxset_isempty(c->clients));
191     pa_idxset_free(c->clients, NULL);
192
193     pa_assert(pa_idxset_isempty(c->cards));
194     pa_idxset_free(c->cards, NULL);
195
196     pa_assert(pa_idxset_isempty(c->sinks));
197     pa_idxset_free(c->sinks, NULL);
198
199     pa_assert(pa_idxset_isempty(c->sources));
200     pa_idxset_free(c->sources, NULL);
201
202     pa_assert(pa_idxset_isempty(c->source_outputs));
203     pa_idxset_free(c->source_outputs, NULL);
204
205     pa_assert(pa_idxset_isempty(c->sink_inputs));
206     pa_idxset_free(c->sink_inputs, NULL);
207
208     pa_assert(pa_hashmap_isempty(c->namereg));
209     pa_hashmap_free(c->namereg);
210
211     pa_assert(pa_hashmap_isempty(c->shared));
212     pa_hashmap_free(c->shared);
213
214     pa_assert(pa_hashmap_isempty(c->modules_pending_unload));
215     pa_hashmap_free(c->modules_pending_unload);
216
217     pa_subscription_free_all(c);
218
219     if (c->exit_event)
220         c->mainloop->time_free(c->exit_event);
221
222     pa_assert(!c->default_source);
223     pa_assert(!c->default_sink);
224     pa_xfree(c->configured_default_source);
225     pa_xfree(c->configured_default_sink);
226
227     pa_silence_cache_done(&c->silence_cache);
228     pa_mempool_unref(c->mempool);
229
230     for (j = 0; j < PA_CORE_HOOK_MAX; j++)
231         pa_hook_done(&c->hooks[j]);
232
233     pa_xfree(c);
234 }
235
236 void pa_core_set_configured_default_sink(pa_core *core, const char *sink) {
237     char *old_sink;
238
239     pa_assert(core);
240
241     old_sink = pa_xstrdup(core->configured_default_sink);
242
243     if (pa_safe_streq(sink, old_sink))
244         goto finish;
245
246     pa_xfree(core->configured_default_sink);
247     core->configured_default_sink = pa_xstrdup(sink);
248     pa_log_info("configured_default_sink: %s -> %s",
249                 old_sink ? old_sink : "(unset)", sink ? sink : "(unset)");
250
251     pa_core_update_default_sink(core);
252
253 finish:
254     pa_xfree(old_sink);
255 }
256
257 void pa_core_set_configured_default_source(pa_core *core, const char *source) {
258     char *old_source;
259
260     pa_assert(core);
261
262     old_source = pa_xstrdup(core->configured_default_source);
263
264     if (pa_safe_streq(source, old_source))
265         goto finish;
266
267     pa_xfree(core->configured_default_source);
268     core->configured_default_source = pa_xstrdup(source);
269     pa_log_info("configured_default_source: %s -> %s",
270                 old_source ? old_source : "(unset)", source ? source : "(unset)");
271
272     pa_core_update_default_source(core);
273
274 finish:
275     pa_xfree(old_source);
276 }
277
278 /* a  < b  ->  return -1
279  * a == b  ->  return  0
280  * a  > b  ->  return  1 */
281 static int compare_sinks(pa_sink *a, pa_sink *b) {
282     pa_core *core;
283
284     core = a->core;
285
286     /* Available sinks always beat unavailable sinks. */
287     if (a->active_port && a->active_port->available == PA_AVAILABLE_NO
288             && (!b->active_port || b->active_port->available != PA_AVAILABLE_NO))
289         return -1;
290     if (b->active_port && b->active_port->available == PA_AVAILABLE_NO
291             && (!a->active_port || a->active_port->available != PA_AVAILABLE_NO))
292         return 1;
293
294     /* The configured default sink is preferred over any other sink. */
295     if (pa_safe_streq(b->name, core->configured_default_sink))
296         return -1;
297     if (pa_safe_streq(a->name, core->configured_default_sink))
298         return 1;
299
300     if (a->priority < b->priority)
301         return -1;
302     if (a->priority > b->priority)
303         return 1;
304
305     /* It's hard to find any difference between these sinks, but maybe one of
306      * them is already the default sink? If so, it's best to keep it as the
307      * default to avoid changing the routing for no good reason. */
308     if (b == core->default_sink)
309         return -1;
310     if (a == core->default_sink)
311         return 1;
312
313     return 0;
314 }
315
316 void pa_core_update_default_sink(pa_core *core) {
317     pa_sink *best = NULL;
318     pa_sink *sink;
319     uint32_t idx;
320     pa_sink *old_default_sink;
321
322     pa_assert(core);
323
324     PA_IDXSET_FOREACH(sink, core->sinks, idx) {
325         if (!PA_SINK_IS_LINKED(sink->state))
326             continue;
327
328         if (!best) {
329             best = sink;
330             continue;
331         }
332
333         if (compare_sinks(sink, best) > 0)
334             best = sink;
335     }
336
337     old_default_sink = core->default_sink;
338
339     if (best == old_default_sink)
340         return;
341
342     core->default_sink = best;
343     pa_log_info("default_sink: %s -> %s",
344                 old_default_sink ? old_default_sink->name : "(unset)", best ? best->name : "(unset)");
345
346     /* If the default sink changed, it may be that the default source has to be
347      * changed too, because monitor sources are prioritized partly based on the
348      * priorities of the monitored sinks. */
349     pa_core_update_default_source(core);
350
351     pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SERVER | PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
352     pa_hook_fire(&core->hooks[PA_CORE_HOOK_DEFAULT_SINK_CHANGED], core->default_sink);
353 }
354
355 /* a  < b  ->  return -1
356  * a == b  ->  return  0
357  * a  > b  ->  return  1 */
358 static int compare_sources(pa_source *a, pa_source *b) {
359     pa_core *core;
360
361     core = a->core;
362
363     /* Available sources always beat unavailable sources. */
364     if (a->active_port && a->active_port->available == PA_AVAILABLE_NO
365             && (!b->active_port || b->active_port->available != PA_AVAILABLE_NO))
366         return -1;
367     if (b->active_port && b->active_port->available == PA_AVAILABLE_NO
368             && (!a->active_port || a->active_port->available != PA_AVAILABLE_NO))
369         return 1;
370
371     /* The configured default source is preferred over any other source. */
372     if (pa_safe_streq(b->name, core->configured_default_source))
373         return -1;
374     if (pa_safe_streq(a->name, core->configured_default_source))
375         return 1;
376
377     /* Monitor sources lose to non-monitor sources. */
378     if (a->monitor_of && !b->monitor_of)
379         return -1;
380     if (!a->monitor_of && b->monitor_of)
381         return 1;
382
383     if (a->priority < b->priority)
384         return -1;
385     if (a->priority > b->priority)
386         return 1;
387
388     /* If the sources are monitors, we can compare the monitored sinks. */
389     if (a->monitor_of)
390         return compare_sinks(a->monitor_of, b->monitor_of);
391
392     /* It's hard to find any difference between these sources, but maybe one of
393      * them is already the default source? If so, it's best to keep it as the
394      * default to avoid changing the routing for no good reason. */
395     if (b == core->default_source)
396         return -1;
397     if (a == core->default_source)
398         return 1;
399
400     return 0;
401 }
402
403 void pa_core_update_default_source(pa_core *core) {
404     pa_source *best = NULL;
405     pa_source *source;
406     uint32_t idx;
407     pa_source *old_default_source;
408
409     pa_assert(core);
410
411     PA_IDXSET_FOREACH(source, core->sources, idx) {
412         if (!PA_SOURCE_IS_LINKED(source->state))
413             continue;
414
415         if (!best) {
416             best = source;
417             continue;
418         }
419
420         if (compare_sources(source, best) > 0)
421             best = source;
422     }
423
424     old_default_source = core->default_source;
425
426     if (best == old_default_source)
427         return;
428
429     core->default_source = best;
430     pa_log_info("default_source: %s -> %s",
431                 old_default_source ? old_default_source->name : "(unset)", best ? best->name : "(unset)");
432     pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SERVER | PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
433     pa_hook_fire(&core->hooks[PA_CORE_HOOK_DEFAULT_SOURCE_CHANGED], core->default_source);
434 }
435
436 static void exit_callback(pa_mainloop_api *m, pa_time_event *e, const struct timeval *t, void *userdata) {
437     pa_core *c = userdata;
438     pa_assert(c->exit_event == e);
439
440     pa_log_info("We are idle, quitting...");
441     pa_core_exit(c, true, 0);
442 }
443
444 void pa_core_check_idle(pa_core *c) {
445     pa_assert(c);
446
447     if (!c->exit_event &&
448         c->exit_idle_time >= 0 &&
449         pa_idxset_size(c->clients) == 0) {
450
451         c->exit_event = pa_core_rttime_new(c, pa_rtclock_now() + c->exit_idle_time * PA_USEC_PER_SEC, exit_callback, c);
452
453     } else if (c->exit_event && pa_idxset_size(c->clients) > 0) {
454         c->mainloop->time_free(c->exit_event);
455         c->exit_event = NULL;
456     }
457 }
458
459 int pa_core_exit(pa_core *c, bool force, int retval) {
460     pa_assert(c);
461
462     if (c->disallow_exit && !force)
463         return -1;
464
465     c->mainloop->quit(c->mainloop, retval);
466     return 0;
467 }
468
469 void pa_core_maybe_vacuum(pa_core *c) {
470     pa_assert(c);
471
472     if (pa_idxset_isempty(c->sink_inputs) && pa_idxset_isempty(c->source_outputs)) {
473         pa_log_debug("Hmm, no streams around, trying to vacuum.");
474     } else {
475         pa_sink *si;
476         pa_source *so;
477         uint32_t idx;
478
479         idx = 0;
480         PA_IDXSET_FOREACH(si, c->sinks, idx)
481             if (pa_sink_get_state(si) != PA_SINK_SUSPENDED)
482                 return;
483
484         idx = 0;
485         PA_IDXSET_FOREACH(so, c->sources, idx)
486             if (pa_source_get_state(so) != PA_SOURCE_SUSPENDED)
487                 return;
488
489         pa_log_info("All sinks and sources are suspended, vacuuming memory");
490     }
491
492     pa_mempool_vacuum(c->mempool);
493 }
494
495 pa_time_event* pa_core_rttime_new(pa_core *c, pa_usec_t usec, pa_time_event_cb_t cb, void *userdata) {
496     struct timeval tv;
497
498     pa_assert(c);
499     pa_assert(c->mainloop);
500
501     return c->mainloop->time_new(c->mainloop, pa_timeval_rtstore(&tv, usec, true), cb, userdata);
502 }
503
504 void pa_core_rttime_restart(pa_core *c, pa_time_event *e, pa_usec_t usec) {
505     struct timeval tv;
506
507     pa_assert(c);
508     pa_assert(c->mainloop);
509
510     c->mainloop->time_restart(e, pa_timeval_rtstore(&tv, usec, true));
511 }