2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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/>.
29 #include <pulse/rtclock.h>
30 #include <pulse/timeval.h>
31 #include <pulse/xmalloc.h>
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>
44 PA_DEFINE_PUBLIC_CLASS(pa_core, pa_msgobject);
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);
49 pa_core_assert_ref(c);
53 case PA_CORE_MESSAGE_UNLOAD_MODULE:
54 pa_module_unload(userdata, true);
62 static void core_free(pa_object *o);
64 pa_core* pa_core_new(pa_mainloop_api *m, bool shared, bool enable_memfd, size_t shm_size) {
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));
82 if (!(pool = pa_mempool_new(PA_MEM_TYPE_PRIVATE, shm_size, false))) {
83 pa_log("pa_mempool_new() failed.");
88 c = pa_msgobject_new(pa_core);
89 c->parent.parent.free = core_free;
90 c->parent.process_msg = core_process_msg;
92 c->state = PA_CORE_STARTUP;
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);
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);
107 c->default_source = NULL;
108 c->default_sink = NULL;
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;
117 c->deferred_volume_safety_margin_usec = 8000;
118 c->deferred_volume_extra_delay_usec = 0;
120 c->module_defer_unload_event = NULL;
121 c->modules_pending_unload = pa_hashmap_new(NULL, NULL);
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;
129 c->shm_size = shm_size;
130 pa_silence_cache_init(&c->silence_cache);
132 c->exit_event = NULL;
133 c->scache_auto_unload_event = NULL;
135 c->exit_idle_time = -1;
136 c->scache_idle_time = 20;
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;
151 for (j = 0; j < PA_CORE_HOOK_MAX; j++)
152 pa_hook_init(&c->hooks[j], c);
154 pa_random(&c->cookie, sizeof(c->cookie));
157 pa_check_signal_is_blocked(SIGPIPE);
160 pa_core_check_idle(c);
162 c->state = PA_CORE_RUNNING;
163 #ifdef TIZEN_PCM_DUMP
165 c->pcm_dump_option = 0;
167 #ifdef TIZEN_EMPTY_POP
168 c->empty_pop_threshold = 10;
174 static void core_free(pa_object *o) {
175 pa_core *c = PA_CORE(o);
179 c->state = PA_CORE_SHUTDOWN;
181 /* Note: All modules and samples in the cache should be unloaded before
184 pa_assert(pa_idxset_isempty(c->scache));
185 pa_idxset_free(c->scache, NULL);
187 pa_assert(pa_idxset_isempty(c->modules));
188 pa_idxset_free(c->modules, NULL);
190 pa_assert(pa_idxset_isempty(c->clients));
191 pa_idxset_free(c->clients, NULL);
193 pa_assert(pa_idxset_isempty(c->cards));
194 pa_idxset_free(c->cards, NULL);
196 pa_assert(pa_idxset_isempty(c->sinks));
197 pa_idxset_free(c->sinks, NULL);
199 pa_assert(pa_idxset_isempty(c->sources));
200 pa_idxset_free(c->sources, NULL);
202 pa_assert(pa_idxset_isempty(c->source_outputs));
203 pa_idxset_free(c->source_outputs, NULL);
205 pa_assert(pa_idxset_isempty(c->sink_inputs));
206 pa_idxset_free(c->sink_inputs, NULL);
208 pa_assert(pa_hashmap_isempty(c->namereg));
209 pa_hashmap_free(c->namereg);
211 pa_assert(pa_hashmap_isempty(c->shared));
212 pa_hashmap_free(c->shared);
214 pa_assert(pa_hashmap_isempty(c->modules_pending_unload));
215 pa_hashmap_free(c->modules_pending_unload);
217 pa_subscription_free_all(c);
220 c->mainloop->time_free(c->exit_event);
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);
227 pa_silence_cache_done(&c->silence_cache);
228 pa_mempool_unref(c->mempool);
230 for (j = 0; j < PA_CORE_HOOK_MAX; j++)
231 pa_hook_done(&c->hooks[j]);
236 void pa_core_set_configured_default_sink(pa_core *core, const char *sink) {
241 old_sink = pa_xstrdup(core->configured_default_sink);
243 if (pa_safe_streq(sink, old_sink))
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)");
251 pa_core_update_default_sink(core);
257 void pa_core_set_configured_default_source(pa_core *core, const char *source) {
262 old_source = pa_xstrdup(core->configured_default_source);
264 if (pa_safe_streq(source, old_source))
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)");
272 pa_core_update_default_source(core);
275 pa_xfree(old_source);
278 /* a < b -> return -1
280 * a > b -> return 1 */
281 static int compare_sinks(pa_sink *a, pa_sink *b) {
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))
290 if (b->active_port && b->active_port->available == PA_AVAILABLE_NO
291 && (!a->active_port || a->active_port->available != PA_AVAILABLE_NO))
294 /* The configured default sink is preferred over any other sink. */
295 if (pa_safe_streq(b->name, core->configured_default_sink))
297 if (pa_safe_streq(a->name, core->configured_default_sink))
300 if (a->priority < b->priority)
302 if (a->priority > b->priority)
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)
310 if (a == core->default_sink)
316 void pa_core_update_default_sink(pa_core *core) {
317 pa_sink *best = NULL;
320 pa_sink *old_default_sink;
324 PA_IDXSET_FOREACH(sink, core->sinks, idx) {
325 if (!PA_SINK_IS_LINKED(sink->state))
333 if (compare_sinks(sink, best) > 0)
337 old_default_sink = core->default_sink;
339 if (best == old_default_sink)
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)");
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);
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);
355 /* a < b -> return -1
357 * a > b -> return 1 */
358 static int compare_sources(pa_source *a, pa_source *b) {
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))
367 if (b->active_port && b->active_port->available == PA_AVAILABLE_NO
368 && (!a->active_port || a->active_port->available != PA_AVAILABLE_NO))
371 /* The configured default source is preferred over any other source. */
372 if (pa_safe_streq(b->name, core->configured_default_source))
374 if (pa_safe_streq(a->name, core->configured_default_source))
377 /* Monitor sources lose to non-monitor sources. */
378 if (a->monitor_of && !b->monitor_of)
380 if (!a->monitor_of && b->monitor_of)
383 if (a->priority < b->priority)
385 if (a->priority > b->priority)
388 /* If the sources are monitors, we can compare the monitored sinks. */
390 return compare_sinks(a->monitor_of, b->monitor_of);
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)
397 if (a == core->default_source)
403 void pa_core_update_default_source(pa_core *core) {
404 pa_source *best = NULL;
407 pa_source *old_default_source;
411 PA_IDXSET_FOREACH(source, core->sources, idx) {
412 if (!PA_SOURCE_IS_LINKED(source->state))
420 if (compare_sources(source, best) > 0)
424 old_default_source = core->default_source;
426 if (best == old_default_source)
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);
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);
440 pa_log_info("We are idle, quitting...");
441 pa_core_exit(c, true, 0);
444 void pa_core_check_idle(pa_core *c) {
447 if (!c->exit_event &&
448 c->exit_idle_time >= 0 &&
449 pa_idxset_size(c->clients) == 0) {
451 c->exit_event = pa_core_rttime_new(c, pa_rtclock_now() + c->exit_idle_time * PA_USEC_PER_SEC, exit_callback, c);
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;
459 int pa_core_exit(pa_core *c, bool force, int retval) {
462 if (c->disallow_exit && !force)
465 c->mainloop->quit(c->mainloop, retval);
469 void pa_core_maybe_vacuum(pa_core *c) {
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.");
480 PA_IDXSET_FOREACH(si, c->sinks, idx)
481 if (pa_sink_get_state(si) != PA_SINK_SUSPENDED)
485 PA_IDXSET_FOREACH(so, c->sources, idx)
486 if (pa_source_get_state(so) != PA_SOURCE_SUSPENDED)
489 pa_log_info("All sinks and sources are suspended, vacuuming memory");
492 pa_mempool_vacuum(c->mempool);
495 pa_time_event* pa_core_rttime_new(pa_core *c, pa_usec_t usec, pa_time_event_cb_t cb, void *userdata) {
499 pa_assert(c->mainloop);
501 return c->mainloop->time_new(c->mainloop, pa_timeval_rtstore(&tv, usec, true), cb, userdata);
504 void pa_core_rttime_restart(pa_core *c, pa_time_event *e, pa_usec_t usec) {
508 pa_assert(c->mainloop);
510 c->mainloop->time_restart(e, pa_timeval_rtstore(&tv, usec, true));