]> code.delx.au - pulseaudio/blob - src/pulsecore/core.c
add new option to pa_core stating whether we are running as high prio process
[pulseaudio] / src / pulsecore / core.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
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.
13
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.
18
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
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdlib.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <signal.h>
33
34 #include <pulse/timeval.h>
35 #include <pulse/xmalloc.h>
36
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>
49
50 #include "core.h"
51
52 static PA_DEFINE_CHECK_TYPE(pa_core, pa_msgobject);
53
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);
56
57 pa_core_assert_ref(c);
58
59 switch (code) {
60
61 case PA_CORE_MESSAGE_UNLOAD_MODULE:
62 pa_module_unload(c, userdata);
63 return 0;
64
65 default:
66 return -1;
67 }
68 }
69
70 static void core_free(pa_object *o);
71
72 pa_core* pa_core_new(pa_mainloop_api *m, int shared) {
73 pa_core* c;
74 pa_mempool *pool;
75 int j;
76
77 pa_assert(m);
78
79 if (shared) {
80 if (!(pool = pa_mempool_new(shared))) {
81 pa_log_warn("failed to allocate shared memory pool. Falling back to a normal memory pool.");
82 shared = 0;
83 }
84 }
85
86 if (!shared) {
87 if (!(pool = pa_mempool_new(shared))) {
88 pa_log("pa_mempool_new() failed.");
89 return NULL;
90 }
91 }
92
93 c = pa_msgobject_new(pa_core);
94 c->parent.parent.free = core_free;
95 c->parent.process_msg = core_process_msg;
96
97 c->mainloop = m;
98 c->clients = pa_idxset_new(NULL, NULL);
99 c->sinks = pa_idxset_new(NULL, NULL);
100 c->sources = pa_idxset_new(NULL, NULL);
101 c->source_outputs = pa_idxset_new(NULL, NULL);
102 c->sink_inputs = pa_idxset_new(NULL, NULL);
103
104 c->default_source_name = c->default_sink_name = NULL;
105
106 c->modules = NULL;
107 c->namereg = NULL;
108 c->scache = NULL;
109 c->autoload_idxset = NULL;
110 c->autoload_hashmap = NULL;
111 c->running_as_daemon = 0;
112
113 c->default_sample_spec.format = PA_SAMPLE_S16NE;
114 c->default_sample_spec.rate = 44100;
115 c->default_sample_spec.channels = 2;
116 c->default_n_fragments = 4;
117 c->default_fragment_size_msec = 25;
118
119 c->module_auto_unload_event = NULL;
120 c->module_defer_unload_event = NULL;
121 c->scache_auto_unload_event = NULL;
122
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;
127
128 c->mempool = pool;
129
130 c->quit_event = NULL;
131
132 c->exit_idle_time = -1;
133 c->module_idle_time = 20;
134 c->scache_idle_time = 20;
135
136 c->resample_method = PA_RESAMPLER_SRC_SINC_FASTEST;
137
138 c->is_system_instance = 0;
139 c->disallow_module_loading = 0;
140 c->high_priority = 0;
141
142
143 for (j = 0; j < PA_CORE_HOOK_MAX; j++)
144 pa_hook_init(&c->hooks[j], c);
145
146 pa_property_init(c);
147
148 pa_random(&c->cookie, sizeof(c->cookie));
149
150 #ifdef SIGPIPE
151 pa_check_signal_is_blocked(SIGPIPE);
152 #endif
153
154 return c;
155 }
156
157 static void core_free(pa_object *o) {
158 pa_core *c = PA_CORE(o);
159 int j;
160 pa_assert(c);
161
162 pa_module_unload_all(c);
163 assert(!c->modules);
164
165 assert(pa_idxset_isempty(c->clients));
166 pa_idxset_free(c->clients, NULL, NULL);
167
168 assert(pa_idxset_isempty(c->sinks));
169 pa_idxset_free(c->sinks, NULL, NULL);
170
171 assert(pa_idxset_isempty(c->sources));
172 pa_idxset_free(c->sources, NULL, NULL);
173
174 assert(pa_idxset_isempty(c->source_outputs));
175 pa_idxset_free(c->source_outputs, NULL, NULL);
176
177 assert(pa_idxset_isempty(c->sink_inputs));
178 pa_idxset_free(c->sink_inputs, NULL, NULL);
179
180 pa_scache_free(c);
181 pa_namereg_free(c);
182 pa_autoload_free(c);
183 pa_subscription_free_all(c);
184
185 if (c->quit_event)
186 c->mainloop->time_free(c->quit_event);
187
188 pa_xfree(c->default_source_name);
189 pa_xfree(c->default_sink_name);
190
191 pa_mempool_free(c->mempool);
192
193 pa_property_cleanup(c);
194
195 for (j = 0; j < PA_CORE_HOOK_MAX; j++)
196 pa_hook_free(&c->hooks[j]);
197
198 pa_xfree(c);
199 }
200
201 static void quit_callback(pa_mainloop_api*m, pa_time_event *e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
202 pa_core *c = userdata;
203 pa_assert(c->quit_event = e);
204
205 m->quit(m, 0);
206 }
207
208 void pa_core_check_quit(pa_core *c) {
209 pa_assert(c);
210
211 if (!c->quit_event && c->exit_idle_time >= 0 && pa_idxset_size(c->clients) == 0) {
212 struct timeval tv;
213 pa_gettimeofday(&tv);
214 tv.tv_sec+= c->exit_idle_time;
215 c->quit_event = c->mainloop->time_new(c->mainloop, &tv, quit_callback, c);
216 } else if (c->quit_event && pa_idxset_size(c->clients) > 0) {
217 c->mainloop->time_free(c->quit_event);
218 c->quit_event = NULL;
219 }
220 }
221