4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <pulse/timeval.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/module.h>
38 #include <pulsecore/sink.h>
39 #include <pulsecore/source.h>
40 #include <pulsecore/namereg.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/core-scache.h>
43 #include <pulsecore/autoload.h>
44 #include <pulsecore/core-subscribe.h>
45 #include <pulsecore/props.h>
46 #include <pulsecore/random.h>
47 #include <pulsecore/log.h>
48 #include <pulsecore/macro.h>
52 static PA_DEFINE_CHECK_TYPE(pa_core
, pa_msgobject
);
54 static int core_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
55 pa_core
*c
= PA_CORE(o
);
57 pa_core_assert_ref(c
);
61 case PA_CORE_MESSAGE_UNLOAD_MODULE
:
62 pa_module_unload(c
, userdata
);
70 static void core_free(pa_object
*o
);
72 pa_core
* pa_core_new(pa_mainloop_api
*m
, int shared
) {
79 if (!(pool
= pa_mempool_new(shared
))) {
80 pa_log_warn("failed to allocate shared memory pool. Falling back to a normal memory pool.");
86 if (!(pool
= pa_mempool_new(shared
))) {
87 pa_log("pa_mempool_new() failed.");
92 c
= pa_msgobject_new(pa_core
);
93 c
->parent
.parent
.free
= core_free
;
94 c
->parent
.process_msg
= core_process_msg
;
97 c
->clients
= pa_idxset_new(NULL
, NULL
);
98 c
->sinks
= pa_idxset_new(NULL
, NULL
);
99 c
->sources
= pa_idxset_new(NULL
, NULL
);
100 c
->source_outputs
= pa_idxset_new(NULL
, NULL
);
101 c
->sink_inputs
= pa_idxset_new(NULL
, NULL
);
103 c
->default_source_name
= c
->default_sink_name
= NULL
;
108 c
->autoload_idxset
= NULL
;
109 c
->autoload_hashmap
= NULL
;
110 c
->running_as_daemon
= 0;
112 c
->default_sample_spec
.format
= PA_SAMPLE_S16NE
;
113 c
->default_sample_spec
.rate
= 44100;
114 c
->default_sample_spec
.channels
= 2;
115 c
->default_n_fragments
= 4;
116 c
->default_fragment_size_msec
= 25;
118 c
->module_auto_unload_event
= NULL
;
119 c
->module_defer_unload_event
= NULL
;
120 c
->scache_auto_unload_event
= NULL
;
122 c
->subscription_defer_event
= NULL
;
123 PA_LLIST_HEAD_INIT(pa_subscription
, c
->subscriptions
);
124 PA_LLIST_HEAD_INIT(pa_subscription_event
, c
->subscription_event_queue
);
125 c
->subscription_event_last
= NULL
;
129 c
->disallow_module_loading
= 0;
131 c
->quit_event
= NULL
;
133 c
->exit_idle_time
= -1;
134 c
->module_idle_time
= 20;
135 c
->scache_idle_time
= 20;
137 c
->resample_method
= PA_RESAMPLER_SRC_SINC_FASTEST
;
139 c
->is_system_instance
= 0;
141 pa_hook_init(&c
->hook_sink_new
, c
);
142 pa_hook_init(&c
->hook_sink_new_post
, c
);
143 pa_hook_init(&c
->hook_sink_disconnect
, c
);
144 pa_hook_init(&c
->hook_sink_disconnect_post
, c
);
145 pa_hook_init(&c
->hook_source_new
, c
);
146 pa_hook_init(&c
->hook_source_new_post
, c
);
147 pa_hook_init(&c
->hook_source_disconnect
, c
);
148 pa_hook_init(&c
->hook_source_disconnect_post
, c
);
149 pa_hook_init(&c
->hook_sink_input_new
, c
);
150 pa_hook_init(&c
->hook_sink_input_new_post
, c
);
151 pa_hook_init(&c
->hook_sink_input_disconnect
, c
);
152 pa_hook_init(&c
->hook_sink_input_disconnect_post
, c
);
153 pa_hook_init(&c
->hook_source_output_new
, c
);
154 pa_hook_init(&c
->hook_source_output_new_post
, c
);
155 pa_hook_init(&c
->hook_source_output_disconnect
, c
);
156 pa_hook_init(&c
->hook_source_output_disconnect_post
, c
);
160 pa_random(&c
->cookie
, sizeof(c
->cookie
));
163 pa_check_signal_is_blocked(SIGPIPE
);
169 static void core_free(pa_object
*o
) {
170 pa_core
*c
= PA_CORE(o
);
173 pa_module_unload_all(c
);
176 assert(pa_idxset_isempty(c
->clients
));
177 pa_idxset_free(c
->clients
, NULL
, NULL
);
179 assert(pa_idxset_isempty(c
->sinks
));
180 pa_idxset_free(c
->sinks
, NULL
, NULL
);
182 assert(pa_idxset_isempty(c
->sources
));
183 pa_idxset_free(c
->sources
, NULL
, NULL
);
185 assert(pa_idxset_isempty(c
->source_outputs
));
186 pa_idxset_free(c
->source_outputs
, NULL
, NULL
);
188 assert(pa_idxset_isempty(c
->sink_inputs
));
189 pa_idxset_free(c
->sink_inputs
, NULL
, NULL
);
194 pa_subscription_free_all(c
);
197 c
->mainloop
->time_free(c
->quit_event
);
199 pa_xfree(c
->default_source_name
);
200 pa_xfree(c
->default_sink_name
);
202 pa_mempool_free(c
->mempool
);
204 pa_property_cleanup(c
);
206 pa_hook_free(&c
->hook_sink_new
);
207 pa_hook_free(&c
->hook_sink_new_post
);
208 pa_hook_free(&c
->hook_sink_disconnect
);
209 pa_hook_free(&c
->hook_sink_disconnect_post
);
210 pa_hook_free(&c
->hook_source_new
);
211 pa_hook_free(&c
->hook_source_new_post
);
212 pa_hook_free(&c
->hook_source_disconnect
);
213 pa_hook_free(&c
->hook_source_disconnect_post
);
214 pa_hook_free(&c
->hook_sink_input_new
);
215 pa_hook_free(&c
->hook_sink_input_new_post
);
216 pa_hook_free(&c
->hook_sink_input_disconnect
);
217 pa_hook_free(&c
->hook_sink_input_disconnect_post
);
218 pa_hook_free(&c
->hook_source_output_new
);
219 pa_hook_free(&c
->hook_source_output_new_post
);
220 pa_hook_free(&c
->hook_source_output_disconnect
);
221 pa_hook_free(&c
->hook_source_output_disconnect_post
);
226 static void quit_callback(pa_mainloop_api
*m
, pa_time_event
*e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
227 pa_core
*c
= userdata
;
228 pa_assert(c
->quit_event
= e
);
233 void pa_core_check_quit(pa_core
*c
) {
236 if (!c
->quit_event
&& c
->exit_idle_time
>= 0 && pa_idxset_size(c
->clients
) == 0) {
238 pa_gettimeofday(&tv
);
239 tv
.tv_sec
+= c
->exit_idle_time
;
240 c
->quit_event
= c
->mainloop
->time_new(c
->mainloop
, &tv
, quit_callback
, c
);
241 } else if (c
->quit_event
&& pa_idxset_size(c
->clients
) > 0) {
242 c
->mainloop
->time_free(c
->quit_event
);
243 c
->quit_event
= NULL
;