]> code.delx.au - pulseaudio/blob - src/pulsecore/core.c
31b6c18816de648a39ad05019134e4680314e468
[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
49 #include "core.h"
50
51 pa_core* pa_core_new(pa_mainloop_api *m, int shared) {
52 pa_core* c;
53 pa_mempool *pool;
54
55 if (shared) {
56 if (!(pool = pa_mempool_new(shared))) {
57 pa_log_warn("failed to allocate shared memory pool. Falling back to a normal memory pool.");
58 shared = 0;
59 }
60 }
61
62 if (!shared) {
63 if (!(pool = pa_mempool_new(shared))) {
64 pa_log("pa_mempool_new() failed.");
65 return NULL;
66 }
67 }
68
69 c = pa_xnew(pa_core, 1);
70
71 c->mainloop = m;
72 c->clients = pa_idxset_new(NULL, NULL);
73 c->sinks = pa_idxset_new(NULL, NULL);
74 c->sources = pa_idxset_new(NULL, NULL);
75 c->source_outputs = pa_idxset_new(NULL, NULL);
76 c->sink_inputs = pa_idxset_new(NULL, NULL);
77
78 c->default_source_name = c->default_sink_name = NULL;
79
80 c->modules = NULL;
81 c->namereg = NULL;
82 c->scache = NULL;
83 c->autoload_idxset = NULL;
84 c->autoload_hashmap = NULL;
85 c->running_as_daemon = 0;
86
87 c->default_sample_spec.format = PA_SAMPLE_S16NE;
88 c->default_sample_spec.rate = 44100;
89 c->default_sample_spec.channels = 2;
90
91 c->module_auto_unload_event = NULL;
92 c->module_defer_unload_event = NULL;
93 c->scache_auto_unload_event = NULL;
94
95 c->subscription_defer_event = NULL;
96 PA_LLIST_HEAD_INIT(pa_subscription, c->subscriptions);
97 PA_LLIST_HEAD_INIT(pa_subscription_event, c->subscription_event_queue);
98 c->subscription_event_last = NULL;
99
100 c->mempool = pool;
101
102 c->disallow_module_loading = 0;
103
104 c->quit_event = NULL;
105
106 c->exit_idle_time = -1;
107 c->module_idle_time = 20;
108 c->scache_idle_time = 20;
109
110 c->resample_method = PA_RESAMPLER_SRC_SINC_FASTEST;
111
112 c->is_system_instance = 0;
113
114 pa_hook_init(&c->hook_sink_input_new, c);
115 pa_hook_init(&c->hook_sink_disconnect, c);
116 pa_hook_init(&c->hook_source_output_new, c);
117 pa_hook_init(&c->hook_source_disconnect, c);
118
119 pa_property_init(c);
120
121 pa_random(&c->cookie, sizeof(c->cookie));
122
123 #ifdef SIGPIPE
124 pa_check_signal_is_blocked(SIGPIPE);
125 #endif
126 return c;
127 }
128
129 void pa_core_free(pa_core *c) {
130 assert(c);
131
132 pa_module_unload_all(c);
133 assert(!c->modules);
134
135 assert(pa_idxset_isempty(c->clients));
136 pa_idxset_free(c->clients, NULL, NULL);
137
138 assert(pa_idxset_isempty(c->sinks));
139 pa_idxset_free(c->sinks, NULL, NULL);
140
141 assert(pa_idxset_isempty(c->sources));
142 pa_idxset_free(c->sources, NULL, NULL);
143
144 assert(pa_idxset_isempty(c->source_outputs));
145 pa_idxset_free(c->source_outputs, NULL, NULL);
146
147 assert(pa_idxset_isempty(c->sink_inputs));
148 pa_idxset_free(c->sink_inputs, NULL, NULL);
149
150 pa_scache_free(c);
151 pa_namereg_free(c);
152 pa_autoload_free(c);
153 pa_subscription_free_all(c);
154
155 if (c->quit_event)
156 c->mainloop->time_free(c->quit_event);
157
158 pa_xfree(c->default_source_name);
159 pa_xfree(c->default_sink_name);
160
161 pa_mempool_free(c->mempool);
162
163 pa_property_cleanup(c);
164
165 pa_hook_free(&c->hook_sink_input_new);
166 pa_hook_free(&c->hook_sink_disconnect);
167 pa_hook_free(&c->hook_source_output_new);
168 pa_hook_free(&c->hook_source_disconnect);
169
170 pa_xfree(c);
171 }
172
173 static void quit_callback(pa_mainloop_api*m, pa_time_event *e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
174 pa_core *c = userdata;
175 assert(c->quit_event = e);
176
177 m->quit(m, 0);
178 }
179
180 void pa_core_check_quit(pa_core *c) {
181 assert(c);
182
183 if (!c->quit_event && c->exit_idle_time >= 0 && pa_idxset_size(c->clients) == 0) {
184 struct timeval tv;
185 pa_gettimeofday(&tv);
186 tv.tv_sec+= c->exit_idle_time;
187 c->quit_event = c->mainloop->time_new(c->mainloop, &tv, quit_callback, c);
188 } else if (c->quit_event && pa_idxset_size(c->clients) > 0) {
189 c->mainloop->time_free(c->quit_event);
190 c->quit_event = NULL;
191 }
192 }
193