merge glitch-free branch back into trunk
[profile/ivi/pulseaudio.git] / 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 <stdio.h>
31 #include <signal.h>
32
33 #include <pulse/timeval.h>
34 #include <pulse/xmalloc.h>
35
36 #include <pulsecore/module.h>
37 #include <pulsecore/sink.h>
38 #include <pulsecore/source.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/core-scache.h>
42 #include <pulsecore/autoload.h>
43 #include <pulsecore/core-subscribe.h>
44 #include <pulsecore/props.h>
45 #include <pulsecore/random.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/macro.h>
48
49 #include "core.h"
50
51 static PA_DEFINE_CHECK_TYPE(pa_core, pa_msgobject);
52
53 static int core_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
54     pa_core *c = PA_CORE(o);
55
56     pa_core_assert_ref(c);
57
58     switch (code) {
59
60         case PA_CORE_MESSAGE_UNLOAD_MODULE:
61             pa_module_unload(c, userdata);
62             return 0;
63
64         default:
65             return -1;
66     }
67 }
68
69 static void core_free(pa_object *o);
70
71 pa_core* pa_core_new(pa_mainloop_api *m, int shared) {
72     pa_core* c;
73     pa_mempool *pool;
74     int j;
75
76     pa_assert(m);
77
78     if (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.");
81             shared = 0;
82         }
83     }
84
85     if (!shared) {
86         if (!(pool = pa_mempool_new(shared))) {
87             pa_log("pa_mempool_new() failed.");
88             return NULL;
89         }
90     }
91
92     c = pa_msgobject_new(pa_core);
93     c->parent.parent.free = core_free;
94     c->parent.process_msg = core_process_msg;
95
96     c->mainloop = m;
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);
102
103     c->default_source_name = c->default_sink_name = NULL;
104
105     c->modules = NULL;
106     c->namereg = NULL;
107     c->scache = NULL;
108     c->autoload_idxset = NULL;
109     c->autoload_hashmap = NULL;
110     c->running_as_daemon = FALSE;
111
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;
117
118     c->module_auto_unload_event = NULL;
119     c->module_defer_unload_event = NULL;
120     c->scache_auto_unload_event = NULL;
121
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;
126
127     c->mempool = pool;
128     pa_silence_cache_init(&c->silence_cache);
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_SPEEX_FLOAT_BASE + 3;
137
138     c->is_system_instance = FALSE;
139     c->disallow_module_loading = FALSE;
140     c->realtime_scheduling = FALSE;
141     c->realtime_priority = 5;
142     c->disable_remixing = FALSE;
143
144     for (j = 0; j < PA_CORE_HOOK_MAX; j++)
145         pa_hook_init(&c->hooks[j], c);
146
147     pa_property_init(c);
148
149     pa_random(&c->cookie, sizeof(c->cookie));
150
151 #ifdef SIGPIPE
152     pa_check_signal_is_blocked(SIGPIPE);
153 #endif
154
155     return c;
156 }
157
158 static void core_free(pa_object *o) {
159     pa_core *c = PA_CORE(o);
160     int j;
161     pa_assert(c);
162
163     pa_module_unload_all(c);
164     pa_assert(!c->modules);
165
166     pa_assert(pa_idxset_isempty(c->clients));
167     pa_idxset_free(c->clients, NULL, NULL);
168
169     pa_assert(pa_idxset_isempty(c->sinks));
170     pa_idxset_free(c->sinks, NULL, NULL);
171
172     pa_assert(pa_idxset_isempty(c->sources));
173     pa_idxset_free(c->sources, NULL, NULL);
174
175     pa_assert(pa_idxset_isempty(c->source_outputs));
176     pa_idxset_free(c->source_outputs, NULL, NULL);
177
178     pa_assert(pa_idxset_isempty(c->sink_inputs));
179     pa_idxset_free(c->sink_inputs, NULL, NULL);
180
181     pa_scache_free(c);
182     pa_namereg_free(c);
183     pa_autoload_free(c);
184     pa_subscription_free_all(c);
185
186     if (c->quit_event)
187         c->mainloop->time_free(c->quit_event);
188
189     pa_xfree(c->default_source_name);
190     pa_xfree(c->default_sink_name);
191
192     pa_silence_cache_done(&c->silence_cache);
193     pa_mempool_free(c->mempool);
194
195     pa_property_cleanup(c);
196
197     for (j = 0; j < PA_CORE_HOOK_MAX; j++)
198         pa_hook_free(&c->hooks[j]);
199
200     pa_xfree(c);
201 }
202
203 static void quit_callback(pa_mainloop_api*m, pa_time_event *e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
204     pa_core *c = userdata;
205     pa_assert(c->quit_event == e);
206
207     m->quit(m, 0);
208 }
209
210 void pa_core_check_quit(pa_core *c) {
211     pa_assert(c);
212
213     if (!c->quit_event && c->exit_idle_time >= 0 && pa_idxset_size(c->clients) == 0) {
214         struct timeval tv;
215         pa_gettimeofday(&tv);
216         tv.tv_sec+= c->exit_idle_time;
217         c->quit_event = c->mainloop->time_new(c->mainloop, &tv, quit_callback, c);
218     } else if (c->quit_event && pa_idxset_size(c->clients) > 0) {
219         c->mainloop->time_free(c->quit_event);
220         c->quit_event = NULL;
221     }
222 }