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
core_process_msg(pa_msgobject * o,int code,void * userdata,int64_t offset,pa_memchunk * chunk)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
pa_core_new(pa_mainloop_api * m,bool shared,bool enable_memfd,size_t shm_size)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 c->message_handlers = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
107
108 c->default_source = NULL;
109 c->default_sink = NULL;
110
111 c->default_sample_spec.format = PA_SAMPLE_S16NE;
112 c->default_sample_spec.rate = 44100;
113 c->default_sample_spec.channels = 2;
114 pa_channel_map_init_extend(&c->default_channel_map, c->default_sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
115 c->default_n_fragments = 4;
116 c->default_fragment_size_msec = 25;
117
118 c->deferred_volume_safety_margin_usec = 8000;
119 c->deferred_volume_extra_delay_usec = 0;
120
121 c->module_defer_unload_event = NULL;
122 c->modules_pending_unload = pa_hashmap_new(NULL, NULL);
123
124 c->subscription_defer_event = NULL;
125 PA_LLIST_HEAD_INIT(pa_subscription, c->subscriptions);
126 PA_LLIST_HEAD_INIT(pa_subscription_event, c->subscription_event_queue);
127 c->subscription_event_last = NULL;
128
129 c->mempool = pool;
130 c->shm_size = shm_size;
131 pa_silence_cache_init(&c->silence_cache);
132
133 c->exit_event = NULL;
134 c->scache_auto_unload_event = NULL;
135
136 c->exit_idle_time = -1;
137 c->scache_idle_time = 20;
138
139 c->flat_volumes = true;
140 c->disallow_module_loading = false;
141 c->disallow_exit = false;
142 c->running_as_daemon = false;
143 c->realtime_scheduling = false;
144 c->realtime_priority = 5;
145 c->disable_remixing = false;
146 c->remixing_use_all_sink_channels = true;
147 c->remixing_produce_lfe = false;
148 c->remixing_consume_lfe = false;
149 c->lfe_crossover_freq = 0;
150 c->deferred_volume = true;
151 c->resample_method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 1;
152
153 for (j = 0; j < PA_CORE_HOOK_MAX; j++)
154 pa_hook_init(&c->hooks[j], c);
155
156 pa_random(&c->cookie, sizeof(c->cookie));
157
158 #ifdef SIGPIPE
159 pa_check_signal_is_blocked(SIGPIPE);
160 #endif
161
162 return c;
163 }
164
core_free(pa_object * o)165 static void core_free(pa_object *o) {
166 pa_core *c = PA_CORE(o);
167 int j;
168 pa_assert(c);
169
170 c->state = PA_CORE_SHUTDOWN;
171
172 /* Note: All modules and samples in the cache should be unloaded before
173 * we get here */
174
175 pa_assert(pa_idxset_isempty(c->scache));
176 pa_idxset_free(c->scache, NULL);
177
178 pa_assert(pa_idxset_isempty(c->modules));
179 pa_idxset_free(c->modules, NULL);
180
181 pa_assert(pa_idxset_isempty(c->clients));
182 pa_idxset_free(c->clients, NULL);
183
184 pa_assert(pa_idxset_isempty(c->cards));
185 pa_idxset_free(c->cards, NULL);
186
187 pa_assert(pa_idxset_isempty(c->sinks));
188 pa_idxset_free(c->sinks, NULL);
189
190 pa_assert(pa_idxset_isempty(c->sources));
191 pa_idxset_free(c->sources, NULL);
192
193 pa_assert(pa_idxset_isempty(c->source_outputs));
194 pa_idxset_free(c->source_outputs, NULL);
195
196 pa_assert(pa_idxset_isempty(c->sink_inputs));
197 pa_idxset_free(c->sink_inputs, NULL);
198
199 pa_assert(pa_hashmap_isempty(c->namereg));
200 pa_hashmap_free(c->namereg);
201
202 pa_assert(pa_hashmap_isempty(c->shared));
203 pa_hashmap_free(c->shared);
204
205 pa_assert(pa_hashmap_isempty(c->message_handlers));
206 pa_hashmap_free(c->message_handlers);
207
208 pa_assert(pa_hashmap_isempty(c->modules_pending_unload));
209 pa_hashmap_free(c->modules_pending_unload);
210
211 pa_subscription_free_all(c);
212
213 if (c->exit_event)
214 c->mainloop->time_free(c->exit_event);
215
216 pa_assert(!c->default_source);
217 pa_assert(!c->default_sink);
218 pa_xfree(c->configured_default_source);
219 pa_xfree(c->configured_default_sink);
220
221 pa_silence_cache_done(&c->silence_cache);
222 pa_mempool_unref(c->mempool);
223
224 for (j = 0; j < PA_CORE_HOOK_MAX; j++)
225 pa_hook_done(&c->hooks[j]);
226
227 pa_xfree(c);
228 }
229
pa_core_set_configured_default_sink(pa_core * core,const char * sink)230 void pa_core_set_configured_default_sink(pa_core *core, const char *sink) {
231 char *old_sink;
232
233 pa_assert(core);
234
235 old_sink = pa_xstrdup(core->configured_default_sink);
236
237 if (pa_safe_streq(sink, old_sink))
238 goto finish;
239
240 pa_xfree(core->configured_default_sink);
241 core->configured_default_sink = pa_xstrdup(sink);
242 pa_log_info("configured_default_sink: %s -> %s",
243 old_sink ? old_sink : "(unset)", sink ? sink : "(unset)");
244 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SERVER | PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
245
246 pa_core_update_default_sink(core);
247
248 finish:
249 pa_xfree(old_sink);
250 }
251
pa_core_set_configured_default_source(pa_core * core,const char * source)252 void pa_core_set_configured_default_source(pa_core *core, const char *source) {
253 char *old_source;
254
255 pa_assert(core);
256
257 old_source = pa_xstrdup(core->configured_default_source);
258
259 if (pa_safe_streq(source, old_source))
260 goto finish;
261
262 pa_xfree(core->configured_default_source);
263 core->configured_default_source = pa_xstrdup(source);
264 pa_log_info("configured_default_source: %s -> %s",
265 old_source ? old_source : "(unset)", source ? source : "(unset)");
266 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SERVER | PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
267
268 pa_core_update_default_source(core);
269
270 finish:
271 pa_xfree(old_source);
272 }
273
274 /* a < b -> return -1
275 * a == b -> return 0
276 * a > b -> return 1 */
compare_sinks(pa_sink * a,pa_sink * b)277 static int compare_sinks(pa_sink *a, pa_sink *b) {
278 pa_core *core;
279
280 core = a->core;
281
282 /* Available sinks always beat unavailable sinks. */
283 if (a->active_port && a->active_port->available == PA_AVAILABLE_NO
284 && (!b->active_port || b->active_port->available != PA_AVAILABLE_NO))
285 return -1;
286 if (b->active_port && b->active_port->available == PA_AVAILABLE_NO
287 && (!a->active_port || a->active_port->available != PA_AVAILABLE_NO))
288 return 1;
289
290 /* The configured default sink is preferred over any other sink. */
291 if (pa_safe_streq(b->name, core->configured_default_sink))
292 return -1;
293 if (pa_safe_streq(a->name, core->configured_default_sink))
294 return 1;
295
296 if (a->priority < b->priority)
297 return -1;
298 if (a->priority > b->priority)
299 return 1;
300
301 /* It's hard to find any difference between these sinks, but maybe one of
302 * them is already the default sink? If so, it's best to keep it as the
303 * default to avoid changing the routing for no good reason. */
304 if (b == core->default_sink)
305 return -1;
306 if (a == core->default_sink)
307 return 1;
308
309 return 0;
310 }
311
pa_core_update_default_sink(pa_core * core)312 void pa_core_update_default_sink(pa_core *core) {
313 pa_sink *best = NULL;
314 pa_sink *sink;
315 uint32_t idx;
316 pa_sink *old_default_sink;
317
318 pa_assert(core);
319
320 PA_IDXSET_FOREACH(sink, core->sinks, idx) {
321 if (!PA_SINK_IS_LINKED(sink->state))
322 continue;
323
324 if (!best) {
325 best = sink;
326 continue;
327 }
328
329 if (compare_sinks(sink, best) > 0)
330 best = sink;
331 }
332
333 old_default_sink = core->default_sink;
334
335 if (best == old_default_sink)
336 return;
337
338 core->default_sink = best;
339 pa_log_info("default_sink: %s -> %s",
340 old_default_sink ? old_default_sink->name : "(unset)", best ? best->name : "(unset)");
341
342 /* If the default sink changed, it may be that the default source has to be
343 * changed too, because monitor sources are prioritized partly based on the
344 * priorities of the monitored sinks. */
345 pa_core_update_default_source(core);
346
347 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SERVER | PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
348 pa_hook_fire(&core->hooks[PA_CORE_HOOK_DEFAULT_SINK_CHANGED], core->default_sink);
349
350 /* try to move the streams from old_default_sink to the new default_sink conditionally */
351 if (old_default_sink)
352 pa_sink_move_streams_to_default_sink(core, old_default_sink, true);
353 }
354
355 /* a < b -> return -1
356 * a == b -> return 0
357 * a > b -> return 1 */
compare_sources(pa_source * a,pa_source * b)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
pa_core_update_default_source(pa_core * core)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 /* try to move the streams from old_default_source to the new default_source conditionally */
436 if (old_default_source)
437 pa_source_move_streams_to_default_source(core, old_default_source, true);
438 }
439
pa_core_set_exit_idle_time(pa_core * core,int time)440 void pa_core_set_exit_idle_time(pa_core *core, int time) {
441 pa_assert(core);
442
443 if (time == core->exit_idle_time)
444 return;
445
446 pa_log_info("exit_idle_time: %i -> %i", core->exit_idle_time, time);
447 core->exit_idle_time = time;
448 }
449
exit_callback(pa_mainloop_api * m,pa_time_event * e,const struct timeval * t,void * userdata)450 static void exit_callback(pa_mainloop_api *m, pa_time_event *e, const struct timeval *t, void *userdata) {
451 pa_core *c = userdata;
452 pa_assert(c->exit_event == e);
453
454 pa_log_info("We are idle, quitting...");
455 pa_core_exit(c, true, 0);
456 }
457
pa_core_check_idle(pa_core * c)458 void pa_core_check_idle(pa_core *c) {
459 pa_assert(c);
460
461 if (!c->exit_event &&
462 c->exit_idle_time >= 0 &&
463 pa_idxset_size(c->clients) == 0) {
464
465 c->exit_event = pa_core_rttime_new(c, pa_rtclock_now() + c->exit_idle_time * PA_USEC_PER_SEC, exit_callback, c);
466
467 } else if (c->exit_event && pa_idxset_size(c->clients) > 0) {
468 c->mainloop->time_free(c->exit_event);
469 c->exit_event = NULL;
470 }
471 }
472
pa_core_exit(pa_core * c,bool force,int retval)473 int pa_core_exit(pa_core *c, bool force, int retval) {
474 pa_assert(c);
475
476 if (c->disallow_exit && !force)
477 return -1;
478
479 c->mainloop->quit(c->mainloop, retval);
480 return 0;
481 }
482
pa_core_maybe_vacuum(pa_core * c)483 void pa_core_maybe_vacuum(pa_core *c) {
484 pa_assert(c);
485
486 if (pa_idxset_isempty(c->sink_inputs) && pa_idxset_isempty(c->source_outputs)) {
487 pa_log_debug("Hmm, no streams around, trying to vacuum.");
488 } else {
489 pa_sink *si;
490 pa_source *so;
491 uint32_t idx;
492
493 idx = 0;
494 PA_IDXSET_FOREACH(si, c->sinks, idx)
495 if (si->state != PA_SINK_SUSPENDED)
496 return;
497
498 idx = 0;
499 PA_IDXSET_FOREACH(so, c->sources, idx)
500 if (so->state != PA_SOURCE_SUSPENDED)
501 return;
502
503 pa_log_info("All sinks and sources are suspended, vacuuming memory");
504 }
505
506 pa_mempool_vacuum(c->mempool);
507 }
508
pa_core_rttime_new(pa_core * c,pa_usec_t usec,pa_time_event_cb_t cb,void * userdata)509 pa_time_event* pa_core_rttime_new(pa_core *c, pa_usec_t usec, pa_time_event_cb_t cb, void *userdata) {
510 struct timeval tv;
511
512 pa_assert(c);
513 pa_assert(c->mainloop);
514
515 return c->mainloop->time_new(c->mainloop, pa_timeval_rtstore(&tv, usec, true), cb, userdata);
516 }
517
pa_core_rttime_restart(pa_core * c,pa_time_event * e,pa_usec_t usec)518 void pa_core_rttime_restart(pa_core *c, pa_time_event *e, pa_usec_t usec) {
519 struct timeval tv;
520
521 pa_assert(c);
522 pa_assert(c->mainloop);
523
524 c->mainloop->time_restart(e, pa_timeval_rtstore(&tv, usec, true));
525 }
526
pa_core_move_streams_to_newly_available_preferred_sink(pa_core * c,pa_sink * s)527 void pa_core_move_streams_to_newly_available_preferred_sink(pa_core *c, pa_sink *s) {
528 pa_sink_input *si;
529 uint32_t idx;
530
531 pa_assert(c);
532 pa_assert(s);
533
534 PA_IDXSET_FOREACH(si, c->sink_inputs, idx) {
535 if (si->sink == s)
536 continue;
537
538 if (!si->sink)
539 continue;
540
541 /* Skip this sink input if it is connecting a filter sink to
542 * the master */
543 if (si->origin_sink)
544 continue;
545
546 /* It might happen that a stream and a sink are set up at the
547 same time, in which case we want to make sure we don't
548 interfere with that */
549 if (!PA_SINK_INPUT_IS_LINKED(si->state))
550 continue;
551
552 if (pa_safe_streq(si->preferred_sink, s->name))
553 pa_sink_input_move_to(si, s, false);
554 }
555
556 }
557
pa_core_move_streams_to_newly_available_preferred_source(pa_core * c,pa_source * s)558 void pa_core_move_streams_to_newly_available_preferred_source(pa_core *c, pa_source *s) {
559 pa_source_output *so;
560 uint32_t idx;
561
562 pa_assert(c);
563 pa_assert(s);
564
565 PA_IDXSET_FOREACH(so, c->source_outputs, idx) {
566 if (so->source == s)
567 continue;
568
569 if (so->direct_on_input)
570 continue;
571
572 if (!so->source)
573 continue;
574
575 /* Skip this source output if it is connecting a filter source to
576 * the master */
577 if (so->destination_source)
578 continue;
579
580 /* It might happen that a stream and a source are set up at the
581 same time, in which case we want to make sure we don't
582 interfere with that */
583 if (!PA_SOURCE_OUTPUT_IS_LINKED(so->state))
584 continue;
585
586 if (pa_safe_streq(so->preferred_source, s->name))
587 pa_source_output_move_to(so, s, false);
588 }
589
590 }
591
592
593 /* Helper macro to reduce repetition in pa_suspend_cause_to_string().
594 * Parameters:
595 * char *p: the current position in the write buffer
596 * bool first: is cause_to_check the first cause to be written?
597 * pa_suspend_cause_t cause_bitfield: the causes given to pa_suspend_cause_to_string()
598 * pa_suspend_cause_t cause_to_check: the cause whose presence in cause_bitfield is to be checked
599 */
600 #define CHECK_CAUSE(p, first, cause_bitfield, cause_to_check) \
601 if (cause_bitfield & PA_SUSPEND_##cause_to_check) { \
602 size_t len = sizeof(#cause_to_check) - 1; \
603 if (!first) { \
604 *p = '|'; \
605 p++; \
606 } \
607 first = false; \
608 memcpy(p, #cause_to_check, len); \
609 p += len; \
610 }
611
pa_suspend_cause_to_string(pa_suspend_cause_t cause_bitfield,char buf[PA_SUSPEND_CAUSE_TO_STRING_BUF_SIZE])612 const char *pa_suspend_cause_to_string(pa_suspend_cause_t cause_bitfield, char buf[PA_SUSPEND_CAUSE_TO_STRING_BUF_SIZE]) {
613 char *p = buf;
614 bool first = true;
615
616 CHECK_CAUSE(p, first, cause_bitfield, USER);
617 CHECK_CAUSE(p, first, cause_bitfield, APPLICATION);
618 CHECK_CAUSE(p, first, cause_bitfield, IDLE);
619 CHECK_CAUSE(p, first, cause_bitfield, SESSION);
620 CHECK_CAUSE(p, first, cause_bitfield, PASSTHROUGH);
621 CHECK_CAUSE(p, first, cause_bitfield, INTERNAL);
622 CHECK_CAUSE(p, first, cause_bitfield, UNAVAILABLE);
623
624 if (p == buf) {
625 memcpy(p, "(none)", 6);
626 p += 6;
627 }
628
629 *p = 0;
630
631 return buf;
632 }
633