]> code.delx.au - pulseaudio/blob - src/pulsecore/core.c
move flat volume logic into the core. while doing so add n_volume_steps field to...
[pulseaudio] / 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 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <signal.h>
30
31 #include <pulse/timeval.h>
32 #include <pulse/xmalloc.h>
33
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/core-subscribe.h>
41 #include <pulsecore/shared.h>
42 #include <pulsecore/random.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/macro.h>
45
46 #include "core.h"
47
48 static PA_DEFINE_CHECK_TYPE(pa_core, pa_msgobject);
49
50 static int core_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
51 pa_core *c = PA_CORE(o);
52
53 pa_core_assert_ref(c);
54
55 switch (code) {
56
57 case PA_CORE_MESSAGE_UNLOAD_MODULE:
58 pa_module_unload(c, userdata, TRUE);
59 return 0;
60
61 default:
62 return -1;
63 }
64 }
65
66 static void core_free(pa_object *o);
67
68 pa_core* pa_core_new(pa_mainloop_api *m, pa_bool_t shared, size_t shm_size) {
69 pa_core* c;
70 pa_mempool *pool;
71 int j;
72
73 pa_assert(m);
74
75 if (shared) {
76 if (!(pool = pa_mempool_new(shared, shm_size))) {
77 pa_log_warn("failed to allocate shared memory pool. Falling back to a normal memory pool.");
78 shared = FALSE;
79 }
80 }
81
82 if (!shared) {
83 if (!(pool = pa_mempool_new(shared, shm_size))) {
84 pa_log("pa_mempool_new() failed.");
85 return NULL;
86 }
87 }
88
89 c = pa_msgobject_new(pa_core);
90 c->parent.parent.free = core_free;
91 c->parent.process_msg = core_process_msg;
92
93 c->state = PA_CORE_STARTUP;
94 c->mainloop = m;
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);
100 c->cards = pa_idxset_new(NULL, NULL);
101
102 c->default_source_name = c->default_sink_name = NULL;
103
104 c->modules = NULL;
105 c->namereg = NULL;
106 c->scache = NULL;
107 c->running_as_daemon = FALSE;
108
109 c->default_sample_spec.format = PA_SAMPLE_S16NE;
110 c->default_sample_spec.rate = 44100;
111 c->default_sample_spec.channels = 2;
112 c->default_n_fragments = 4;
113 c->default_fragment_size_msec = 25;
114
115 c->module_defer_unload_event = NULL;
116 c->scache_auto_unload_event = NULL;
117
118 c->subscription_defer_event = NULL;
119 PA_LLIST_HEAD_INIT(pa_subscription, c->subscriptions);
120 PA_LLIST_HEAD_INIT(pa_subscription_event, c->subscription_event_queue);
121 c->subscription_event_last = NULL;
122
123 c->mempool = pool;
124 pa_silence_cache_init(&c->silence_cache);
125
126 c->exit_event = NULL;
127
128 c->exit_idle_time = -1;
129 c->scache_idle_time = 20;
130 c->flat_volumes = TRUE;
131
132 c->resample_method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 3;
133
134 c->disallow_module_loading = FALSE;
135 c->disallow_exit = FALSE;
136 c->realtime_scheduling = FALSE;
137 c->realtime_priority = 5;
138 c->disable_remixing = FALSE;
139 c->disable_lfe_remixing = FALSE;
140
141 for (j = 0; j < PA_CORE_HOOK_MAX; j++)
142 pa_hook_init(&c->hooks[j], c);
143
144 pa_shared_init(c);
145
146 pa_random(&c->cookie, sizeof(c->cookie));
147
148 #ifdef SIGPIPE
149 pa_check_signal_is_blocked(SIGPIPE);
150 #endif
151
152 pa_core_check_idle(c);
153
154 c->state = PA_CORE_RUNNING;
155
156 return c;
157 }
158
159 static void core_free(pa_object *o) {
160 pa_core *c = PA_CORE(o);
161 int j;
162 pa_assert(c);
163
164 c->state = PA_CORE_SHUTDOWN;
165
166 pa_module_unload_all(c);
167 pa_assert(!c->modules);
168
169 pa_assert(pa_idxset_isempty(c->clients));
170 pa_idxset_free(c->clients, NULL, NULL);
171
172 pa_assert(pa_idxset_isempty(c->cards));
173 pa_idxset_free(c->cards, NULL, NULL);
174
175 pa_assert(pa_idxset_isempty(c->sinks));
176 pa_idxset_free(c->sinks, NULL, NULL);
177
178 pa_assert(pa_idxset_isempty(c->sources));
179 pa_idxset_free(c->sources, NULL, NULL);
180
181 pa_assert(pa_idxset_isempty(c->source_outputs));
182 pa_idxset_free(c->source_outputs, NULL, NULL);
183
184 pa_assert(pa_idxset_isempty(c->sink_inputs));
185 pa_idxset_free(c->sink_inputs, NULL, NULL);
186
187 pa_scache_free(c);
188 pa_namereg_free(c);
189 pa_subscription_free_all(c);
190
191 if (c->exit_event)
192 c->mainloop->time_free(c->exit_event);
193
194 pa_xfree(c->default_source_name);
195 pa_xfree(c->default_sink_name);
196
197 pa_silence_cache_done(&c->silence_cache);
198 pa_mempool_free(c->mempool);
199
200 pa_shared_cleanup(c);
201
202 for (j = 0; j < PA_CORE_HOOK_MAX; j++)
203 pa_hook_done(&c->hooks[j]);
204
205 pa_xfree(c);
206 }
207
208 static void exit_callback(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
209 pa_core *c = userdata;
210 pa_assert(c->exit_event == e);
211
212 pa_log_info("We are idle, quitting...");
213 pa_core_exit(c, TRUE, 0);
214 }
215
216 void pa_core_check_idle(pa_core *c) {
217 pa_assert(c);
218
219 if (!c->exit_event &&
220 c->exit_idle_time >= 0 &&
221 pa_idxset_size(c->clients) == 0) {
222
223 struct timeval tv;
224 pa_gettimeofday(&tv);
225 tv.tv_sec+= c->exit_idle_time;
226
227 c->exit_event = c->mainloop->time_new(c->mainloop, &tv, exit_callback, c);
228
229 } else if (c->exit_event && pa_idxset_size(c->clients) > 0) {
230 c->mainloop->time_free(c->exit_event);
231 c->exit_event = NULL;
232 }
233 }
234
235 int pa_core_exit(pa_core *c, pa_bool_t force, int retval) {
236 pa_assert(c);
237
238 if (c->disallow_exit && !force)
239 return -1;
240
241 c->mainloop->quit(c->mainloop, retval);
242 return 0;
243 }