Merge HUGE set of changes temporarily into a branch, to allow me to move them from...
[profile/ivi/pulseaudio-panda.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 <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 int core_process_msg(pa_msgobject *o, int code, void *userdata, pa_memchunk *chunk) {
53     pa_core *c = PA_CORE(o);
54
55     pa_core_assert_ref(c);
56     
57     switch (code) {
58         
59         case PA_CORE_MESSAGE_UNLOAD_MODULE:
60             pa_module_unload(c, userdata);
61             return 0;
62
63         default:
64             return -1;
65     }
66 }
67
68 static void asyncmsgq_cb(pa_mainloop_api*api, pa_io_event* e, int fd, pa_io_event_flags_t events, void *userdata) {
69     pa_core *c = userdata;
70     
71     pa_assert(pa_asyncmsgq_get_fd(c->asyncmsgq) == fd);
72     pa_assert(events == PA_IO_EVENT_INPUT);
73
74     pa_asyncmsgq_after_poll(c->asyncmsgq);
75
76     for (;;) {
77         pa_msgobject *object;
78         int code;
79         void *data;
80         pa_memchunk chunk;
81
82         /* Check whether there is a message for us to process */
83         while (pa_asyncmsgq_get(c->asyncmsgq, &object, &code, &data, &chunk, 0) == 0) {
84             pa_asyncmsgq_dispatch(object, code, data, &chunk);
85             pa_asyncmsgq_done(c->asyncmsgq, 0);
86         }
87         
88         if (pa_asyncmsgq_before_poll(c->asyncmsgq) == 0)
89             break;
90     }
91 }
92
93 static void core_free(pa_object *o);
94
95 pa_core* pa_core_new(pa_mainloop_api *m, int shared) {
96     pa_core* c;
97     pa_mempool *pool;
98
99     pa_assert(m);
100     
101     if (shared) {
102         if (!(pool = pa_mempool_new(shared))) {
103             pa_log_warn("failed to allocate shared memory pool. Falling back to a normal memory pool.");
104             shared = 0;
105         }
106     }
107
108     if (!shared) {
109         if (!(pool = pa_mempool_new(shared))) {
110             pa_log("pa_mempool_new() failed.");
111             return NULL;
112         }
113     }
114
115     c = pa_msgobject_new(pa_core);
116     c->parent.parent.free = core_free;
117     c->parent.process_msg = core_process_msg;
118
119     c->mainloop = m;
120     c->clients = pa_idxset_new(NULL, NULL);
121     c->sinks = pa_idxset_new(NULL, NULL);
122     c->sources = pa_idxset_new(NULL, NULL);
123     c->source_outputs = pa_idxset_new(NULL, NULL);
124     c->sink_inputs = pa_idxset_new(NULL, NULL);
125
126     c->default_source_name = c->default_sink_name = NULL;
127
128     c->modules = NULL;
129     c->namereg = NULL;
130     c->scache = NULL;
131     c->autoload_idxset = NULL;
132     c->autoload_hashmap = NULL;
133     c->running_as_daemon = 0;
134
135     c->default_sample_spec.format = PA_SAMPLE_S16NE;
136     c->default_sample_spec.rate = 44100;
137     c->default_sample_spec.channels = 2;
138
139     c->module_auto_unload_event = NULL;
140     c->module_defer_unload_event = NULL;
141     c->scache_auto_unload_event = NULL;
142
143     c->subscription_defer_event = NULL;
144     PA_LLIST_HEAD_INIT(pa_subscription, c->subscriptions);
145     PA_LLIST_HEAD_INIT(pa_subscription_event, c->subscription_event_queue);
146     c->subscription_event_last = NULL;
147
148     c->mempool = pool;
149
150     c->disallow_module_loading = 0;
151
152     c->quit_event = NULL;
153
154     c->exit_idle_time = -1;
155     c->module_idle_time = 20;
156     c->scache_idle_time = 20;
157
158     c->resample_method = PA_RESAMPLER_SRC_SINC_FASTEST;
159
160     c->is_system_instance = 0;
161
162     pa_hook_init(&c->hook_sink_input_new, c);
163     pa_hook_init(&c->hook_sink_disconnect, c);
164     pa_hook_init(&c->hook_source_output_new, c);
165     pa_hook_init(&c->hook_source_disconnect, c);
166
167     pa_property_init(c);
168
169     pa_random(&c->cookie, sizeof(c->cookie));
170
171 #ifdef SIGPIPE
172     pa_check_signal_is_blocked(SIGPIPE);
173 #endif
174
175     pa_assert_se(c->asyncmsgq = pa_asyncmsgq_new(0));
176     pa_assert_se(pa_asyncmsgq_before_poll(c->asyncmsgq) == 0);
177     pa_assert_se(c->asyncmsgq_event = c->mainloop->io_new(c->mainloop, pa_asyncmsgq_get_fd(c->asyncmsgq), PA_IO_EVENT_INPUT, asyncmsgq_cb, c));
178             
179     return c;
180 }
181
182 static void core_free(pa_object *o) {
183     pa_core *c = PA_CORE(o);
184     pa_core_assert_ref(c);
185
186     pa_module_unload_all(c);
187     assert(!c->modules);
188
189     assert(pa_idxset_isempty(c->clients));
190     pa_idxset_free(c->clients, NULL, NULL);
191
192     assert(pa_idxset_isempty(c->sinks));
193     pa_idxset_free(c->sinks, NULL, NULL);
194
195     assert(pa_idxset_isempty(c->sources));
196     pa_idxset_free(c->sources, NULL, NULL);
197
198     assert(pa_idxset_isempty(c->source_outputs));
199     pa_idxset_free(c->source_outputs, NULL, NULL);
200
201     assert(pa_idxset_isempty(c->sink_inputs));
202     pa_idxset_free(c->sink_inputs, NULL, NULL);
203
204     pa_scache_free(c);
205     pa_namereg_free(c);
206     pa_autoload_free(c);
207     pa_subscription_free_all(c);
208
209     if (c->quit_event)
210         c->mainloop->time_free(c->quit_event);
211
212     pa_xfree(c->default_source_name);
213     pa_xfree(c->default_sink_name);
214
215     pa_mempool_free(c->mempool);
216
217     pa_property_cleanup(c);
218
219     c->mainloop->io_free(c->asyncmsgq_event);
220     pa_asyncmsgq_after_poll(c->asyncmsgq);
221     pa_asyncmsgq_free(c->asyncmsgq);
222
223     pa_hook_free(&c->hook_sink_input_new);
224     pa_hook_free(&c->hook_sink_disconnect);
225     pa_hook_free(&c->hook_source_output_new);
226     pa_hook_free(&c->hook_source_disconnect);
227
228     pa_xfree(c);
229 }
230
231 static void quit_callback(pa_mainloop_api*m, pa_time_event *e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
232     pa_core *c = userdata;
233     assert(c->quit_event = e);
234
235     m->quit(m, 0);
236 }
237
238 void pa_core_check_quit(pa_core *c) {
239     assert(c);
240
241     if (!c->quit_event && c->exit_idle_time >= 0 && pa_idxset_size(c->clients) == 0) {
242         struct timeval tv;
243         pa_gettimeofday(&tv);
244         tv.tv_sec+= c->exit_idle_time;
245         c->quit_event = c->mainloop->time_new(c->mainloop, &tv, quit_callback, c);
246     } else if (c->quit_event && pa_idxset_size(c->clients) > 0) {
247         c->mainloop->time_free(c->quit_event);
248         c->quit_event = NULL;
249     }
250 }
251