]> code.delx.au - pulseaudio/blob - src/pulsecore/core.c
big s/polyp/pulse/g
[pulseaudio] / src / pulsecore / core.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <assert.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/autoload.h>
41 #include <pulsecore/core-subscribe.h>
42 #include <pulsecore/props.h>
43 #include <pulsecore/random.h>
44
45 #include "core.h"
46
47 pa_core* pa_core_new(pa_mainloop_api *m) {
48 pa_core* c;
49 c = pa_xmalloc(sizeof(pa_core));
50
51 c->mainloop = m;
52 c->clients = pa_idxset_new(NULL, NULL);
53 c->sinks = pa_idxset_new(NULL, NULL);
54 c->sources = pa_idxset_new(NULL, NULL);
55 c->source_outputs = pa_idxset_new(NULL, NULL);
56 c->sink_inputs = pa_idxset_new(NULL, NULL);
57
58 c->default_source_name = c->default_sink_name = NULL;
59
60 c->modules = NULL;
61 c->namereg = NULL;
62 c->scache = NULL;
63 c->autoload_idxset = NULL;
64 c->autoload_hashmap = NULL;
65 c->running_as_daemon = 0;
66
67 c->default_sample_spec.format = PA_SAMPLE_S16NE;
68 c->default_sample_spec.rate = 44100;
69 c->default_sample_spec.channels = 2;
70
71 c->module_auto_unload_event = NULL;
72 c->module_defer_unload_event = NULL;
73 c->scache_auto_unload_event = NULL;
74
75 c->subscription_defer_event = NULL;
76 c->subscription_event_queue = NULL;
77 c->subscriptions = NULL;
78
79 c->memblock_stat = pa_memblock_stat_new();
80
81 c->disallow_module_loading = 0;
82
83 c->quit_event = NULL;
84
85 c->exit_idle_time = -1;
86 c->module_idle_time = 20;
87 c->scache_idle_time = 20;
88
89 c->resample_method = PA_RESAMPLER_SRC_SINC_FASTEST;
90
91 pa_property_init(c);
92
93 pa_random(&c->cookie, sizeof(c->cookie));
94
95 #ifdef SIGPIPE
96 pa_check_signal_is_blocked(SIGPIPE);
97 #endif
98 return c;
99 }
100
101 void pa_core_free(pa_core *c) {
102 assert(c);
103
104 pa_module_unload_all(c);
105 assert(!c->modules);
106
107 assert(pa_idxset_isempty(c->clients));
108 pa_idxset_free(c->clients, NULL, NULL);
109
110 assert(pa_idxset_isempty(c->sinks));
111 pa_idxset_free(c->sinks, NULL, NULL);
112
113 assert(pa_idxset_isempty(c->sources));
114 pa_idxset_free(c->sources, NULL, NULL);
115
116 assert(pa_idxset_isempty(c->source_outputs));
117 pa_idxset_free(c->source_outputs, NULL, NULL);
118
119 assert(pa_idxset_isempty(c->sink_inputs));
120 pa_idxset_free(c->sink_inputs, NULL, NULL);
121
122 pa_scache_free(c);
123 pa_namereg_free(c);
124 pa_autoload_free(c);
125 pa_subscription_free_all(c);
126
127 if (c->quit_event)
128 c->mainloop->time_free(c->quit_event);
129
130 pa_xfree(c->default_source_name);
131 pa_xfree(c->default_sink_name);
132
133 pa_memblock_stat_unref(c->memblock_stat);
134
135 pa_property_cleanup(c);
136
137 pa_xfree(c);
138 }
139
140 static void quit_callback(pa_mainloop_api*m, pa_time_event *e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
141 pa_core *c = userdata;
142 assert(c->quit_event = e);
143
144 m->quit(m, 0);
145 }
146
147 void pa_core_check_quit(pa_core *c) {
148 assert(c);
149
150 if (!c->quit_event && c->exit_idle_time >= 0 && pa_idxset_size(c->clients) == 0) {
151 struct timeval tv;
152 pa_gettimeofday(&tv);
153 tv.tv_sec+= c->exit_idle_time;
154 c->quit_event = c->mainloop->time_new(c->mainloop, &tv, quit_callback, c);
155 } else if (c->quit_event && pa_idxset_size(c->clients) > 0) {
156 c->mainloop->time_free(c->quit_event);
157 c->quit_event = NULL;
158 }
159 }
160