ff8ec0810dbaeaffe0a222db9185ae44b60b739c
[profile/ivi/pulseaudio.git] / src / polypcore / core.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
6   polypaudio 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   polypaudio 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 polypaudio; 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 <polyp/xmalloc.h>
32
33 #include <polypcore/module.h>
34 #include <polypcore/sink.h>
35 #include <polypcore/source.h>
36 #include <polypcore/namereg.h>
37 #include <polypcore/util.h>
38 #include <polypcore/core-scache.h>
39 #include <polypcore/autoload.h>
40 #include <polypcore/core-subscribe.h>
41 #include <polypcore/props.h>
42 #include <polypcore/random.h>
43
44 #include "core.h"
45
46 pa_core* pa_core_new(pa_mainloop_api *m) {
47     pa_core* c;
48     c = pa_xmalloc(sizeof(pa_core));
49
50     c->mainloop = m;
51     c->clients = pa_idxset_new(NULL, NULL);
52     c->sinks = pa_idxset_new(NULL, NULL);
53     c->sources = pa_idxset_new(NULL, NULL);
54     c->source_outputs = pa_idxset_new(NULL, NULL);
55     c->sink_inputs = pa_idxset_new(NULL, NULL);
56
57     c->default_source_name = c->default_sink_name = NULL;
58
59     c->modules = NULL;
60     c->namereg = NULL;
61     c->scache = NULL;
62     c->autoload_idxset = NULL;
63     c->autoload_hashmap = NULL;
64     c->running_as_daemon = 0;
65
66     c->default_sample_spec.format = PA_SAMPLE_S16NE;
67     c->default_sample_spec.rate = 44100;
68     c->default_sample_spec.channels = 2;
69
70     c->module_auto_unload_event = NULL;
71     c->module_defer_unload_event = NULL;
72     c->scache_auto_unload_event = NULL;
73
74     c->subscription_defer_event = NULL;
75     c->subscription_event_queue = NULL;
76     c->subscriptions = NULL;
77
78     c->memblock_stat = pa_memblock_stat_new();
79
80     c->disallow_module_loading = 0;
81
82     c->quit_event = NULL;
83
84     c->exit_idle_time = -1;
85     c->module_idle_time = 20;
86     c->scache_idle_time = 20;
87
88     c->resample_method = PA_RESAMPLER_SRC_SINC_FASTEST;
89
90     pa_property_init(c);
91
92     pa_random(&c->cookie, sizeof(c->cookie));
93     
94 #ifdef SIGPIPE
95     pa_check_signal_is_blocked(SIGPIPE);
96 #endif
97     return c;
98 }
99
100 void pa_core_free(pa_core *c) {
101     assert(c);
102
103     pa_module_unload_all(c);
104     assert(!c->modules);
105
106     assert(pa_idxset_isempty(c->clients));
107     pa_idxset_free(c->clients, NULL, NULL);
108     
109     assert(pa_idxset_isempty(c->sinks));
110     pa_idxset_free(c->sinks, NULL, NULL);
111
112     assert(pa_idxset_isempty(c->sources));
113     pa_idxset_free(c->sources, NULL, NULL);
114     
115     assert(pa_idxset_isempty(c->source_outputs));
116     pa_idxset_free(c->source_outputs, NULL, NULL);
117     
118     assert(pa_idxset_isempty(c->sink_inputs));
119     pa_idxset_free(c->sink_inputs, NULL, NULL);
120
121     pa_scache_free(c);
122     pa_namereg_free(c);
123     pa_autoload_free(c);
124     pa_subscription_free_all(c);
125
126     if (c->quit_event)
127         c->mainloop->time_free(c->quit_event);
128
129     pa_xfree(c->default_source_name);
130     pa_xfree(c->default_sink_name);
131
132     pa_memblock_stat_unref(c->memblock_stat);
133
134     pa_property_cleanup(c);
135     
136     pa_xfree(c);    
137 }
138
139 static void quit_callback(pa_mainloop_api*m, pa_time_event *e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
140     pa_core *c = userdata;
141     assert(c->quit_event = e);
142
143     m->quit(m, 0);
144 }
145
146 void pa_core_check_quit(pa_core *c) {
147     assert(c);
148
149     if (!c->quit_event && c->exit_idle_time >= 0 && pa_idxset_size(c->clients) == 0) {
150         struct timeval tv;
151         pa_gettimeofday(&tv);
152         tv.tv_sec+= c->exit_idle_time;
153         c->quit_event = c->mainloop->time_new(c->mainloop, &tv, quit_callback, c);
154     } else if (c->quit_event && pa_idxset_size(c->clients) > 0) {
155         c->mainloop->time_free(c->quit_event);
156         c->quit_event = NULL;
157     }
158 }
159