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 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/timeval.h>
32 #include <pulse/xmalloc.h>
34 #include <pulsecore/module.h>
35 #include <pulsecore/sink.h>
36 #include <pulsecore/source.h>
37 #include <pulsecore/namereg.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/core-scache.h>
40 #include <pulsecore/autoload.h>
41 #include <pulsecore/core-subscribe.h>
42 #include <pulsecore/shared.h>
43 #include <pulsecore/random.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/macro.h>
49 static PA_DEFINE_CHECK_TYPE(pa_core
, pa_msgobject
);
51 static int core_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
52 pa_core
*c
= PA_CORE(o
);
54 pa_core_assert_ref(c
);
58 case PA_CORE_MESSAGE_UNLOAD_MODULE
:
59 pa_module_unload(c
, userdata
);
67 static void core_free(pa_object
*o
);
69 pa_core
* pa_core_new(pa_mainloop_api
*m
, int shared
) {
77 if (!(pool
= pa_mempool_new(shared
))) {
78 pa_log_warn("failed to allocate shared memory pool. Falling back to a normal memory pool.");
84 if (!(pool
= pa_mempool_new(shared
))) {
85 pa_log("pa_mempool_new() failed.");
90 c
= pa_msgobject_new(pa_core
);
91 c
->parent
.parent
.free
= core_free
;
92 c
->parent
.process_msg
= core_process_msg
;
95 c
->clients
= pa_idxset_new(NULL
, NULL
);
96 c
->sinks
= pa_idxset_new(NULL
, NULL
);
97 c
->sources
= pa_idxset_new(NULL
, NULL
);
98 c
->source_outputs
= pa_idxset_new(NULL
, NULL
);
99 c
->sink_inputs
= pa_idxset_new(NULL
, NULL
);
101 c
->default_source_name
= c
->default_sink_name
= NULL
;
106 c
->autoload_idxset
= NULL
;
107 c
->autoload_hashmap
= NULL
;
108 c
->running_as_daemon
= FALSE
;
110 c
->default_sample_spec
.format
= PA_SAMPLE_S16NE
;
111 c
->default_sample_spec
.rate
= 44100;
112 c
->default_sample_spec
.channels
= 2;
113 c
->default_n_fragments
= 4;
114 c
->default_fragment_size_msec
= 25;
116 c
->module_auto_unload_event
= NULL
;
117 c
->module_defer_unload_event
= NULL
;
118 c
->scache_auto_unload_event
= NULL
;
120 c
->subscription_defer_event
= NULL
;
121 PA_LLIST_HEAD_INIT(pa_subscription
, c
->subscriptions
);
122 PA_LLIST_HEAD_INIT(pa_subscription_event
, c
->subscription_event_queue
);
123 c
->subscription_event_last
= NULL
;
126 pa_silence_cache_init(&c
->silence_cache
);
128 c
->quit_event
= NULL
;
130 c
->exit_idle_time
= -1;
131 c
->module_idle_time
= 20;
132 c
->scache_idle_time
= 20;
134 c
->resample_method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 3;
136 c
->disallow_module_loading
= FALSE
;
137 c
->realtime_scheduling
= FALSE
;
138 c
->realtime_priority
= 5;
139 c
->disable_remixing
= FALSE
;
141 for (j
= 0; j
< PA_CORE_HOOK_MAX
; j
++)
142 pa_hook_init(&c
->hooks
[j
], c
);
146 pa_random(&c
->cookie
, sizeof(c
->cookie
));
149 pa_check_signal_is_blocked(SIGPIPE
);
152 pa_core_check_quit(c
);
157 static void core_free(pa_object
*o
) {
158 pa_core
*c
= PA_CORE(o
);
162 pa_module_unload_all(c
);
163 pa_assert(!c
->modules
);
165 pa_assert(pa_idxset_isempty(c
->clients
));
166 pa_idxset_free(c
->clients
, NULL
, NULL
);
168 pa_assert(pa_idxset_isempty(c
->sinks
));
169 pa_idxset_free(c
->sinks
, NULL
, NULL
);
171 pa_assert(pa_idxset_isempty(c
->sources
));
172 pa_idxset_free(c
->sources
, NULL
, NULL
);
174 pa_assert(pa_idxset_isempty(c
->source_outputs
));
175 pa_idxset_free(c
->source_outputs
, NULL
, NULL
);
177 pa_assert(pa_idxset_isempty(c
->sink_inputs
));
178 pa_idxset_free(c
->sink_inputs
, NULL
, NULL
);
183 pa_subscription_free_all(c
);
186 c
->mainloop
->time_free(c
->quit_event
);
188 pa_xfree(c
->default_source_name
);
189 pa_xfree(c
->default_sink_name
);
191 pa_silence_cache_done(&c
->silence_cache
);
192 pa_mempool_free(c
->mempool
);
194 pa_shared_cleanup(c
);
196 for (j
= 0; j
< PA_CORE_HOOK_MAX
; j
++)
197 pa_hook_done(&c
->hooks
[j
]);
202 static void quit_callback(pa_mainloop_api
*m
, pa_time_event
*e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
203 pa_core
*c
= userdata
;
204 pa_assert(c
->quit_event
== e
);
209 void pa_core_check_quit(pa_core
*c
) {
212 if (!c
->quit_event
&&
213 c
->exit_idle_time
>= 0 &&
214 pa_idxset_size(c
->clients
) == 0) {
217 pa_gettimeofday(&tv
);
218 tv
.tv_sec
+= c
->exit_idle_time
;
220 c
->quit_event
= c
->mainloop
->time_new(c
->mainloop
, &tv
, quit_callback
, c
);
222 } else if (c
->quit_event
&& pa_idxset_size(c
->clients
) > 0) {
223 c
->mainloop
->time_free(c
->quit_event
);
224 c
->quit_event
= NULL
;