• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef foocorehfoo
2 #define foocorehfoo
3 
4 /***
5   This file is part of PulseAudio.
6 
7   Copyright 2004-2006 Lennart Poettering
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.1 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, see <http://www.gnu.org/licenses/>.
21 ***/
22 
23 #include <pulsecore/typedefs.h>
24 #include <pulse/mainloop-api.h>
25 #include <pulse/sample.h>
26 #include <pulsecore/cpu.h>
27 
28 /* This is a bitmask that encodes the cause why a sink/source is
29  * suspended.
30  *
31  * When adding new causes, remember to update pa_suspend_cause_to_string() and
32  * PA_SUSPEND_CAUSE_TO_STRING_BUF_SIZE! */
33 typedef enum pa_suspend_cause {
34     PA_SUSPEND_USER = 1,         /* Exposed to the user via some protocol */
35     PA_SUSPEND_APPLICATION = 2,  /* Used by the device reservation logic */
36     PA_SUSPEND_IDLE = 4,         /* Used by module-suspend-on-idle */
37     PA_SUSPEND_SESSION = 8,      /* Used by module-hal for mark inactive sessions */
38     PA_SUSPEND_PASSTHROUGH = 16, /* Used to suspend monitor sources when the sink is in passthrough mode */
39     PA_SUSPEND_INTERNAL = 32,    /* This is used for short period server-internal suspends, such as for sample rate updates */
40     PA_SUSPEND_UNAVAILABLE = 64, /* Used by device implementations that have to suspend when the device is unavailable */
41     PA_SUSPEND_ALL = 0xFFFF      /* Magic cause that can be used to resume forcibly */
42 } pa_suspend_cause_t;
43 
44 #include <pulsecore/idxset.h>
45 #include <pulsecore/hashmap.h>
46 #include <pulsecore/memblock.h>
47 #include <pulsecore/resampler.h>
48 #include <pulsecore/llist.h>
49 #include <pulsecore/hook-list.h>
50 #include <pulsecore/asyncmsgq.h>
51 #include <pulsecore/sample-util.h>
52 #include <pulsecore/sink.h>
53 #include <pulsecore/source.h>
54 #include <pulsecore/core-subscribe.h>
55 #include <pulsecore/msgobject.h>
56 
57 typedef enum pa_server_type {
58     PA_SERVER_TYPE_UNSET,
59     PA_SERVER_TYPE_USER,
60     PA_SERVER_TYPE_SYSTEM,
61     PA_SERVER_TYPE_NONE
62 } pa_server_type_t;
63 
64 typedef enum pa_core_state {
65     PA_CORE_STARTUP,
66     PA_CORE_RUNNING,
67     PA_CORE_SHUTDOWN
68 } pa_core_state_t;
69 
70 typedef enum pa_core_hook {
71     PA_CORE_HOOK_SINK_NEW,
72     PA_CORE_HOOK_SINK_FIXATE,
73     PA_CORE_HOOK_SINK_PUT,
74     PA_CORE_HOOK_SINK_UNLINK,
75     PA_CORE_HOOK_SINK_UNLINK_POST,
76     PA_CORE_HOOK_SINK_STATE_CHANGED,
77     PA_CORE_HOOK_SINK_PROPLIST_CHANGED,
78     PA_CORE_HOOK_SINK_PORT_CHANGED,
79     PA_CORE_HOOK_SINK_FLAGS_CHANGED,
80     PA_CORE_HOOK_SINK_VOLUME_CHANGED,
81     PA_CORE_HOOK_SINK_MUTE_CHANGED,
82     PA_CORE_HOOK_SINK_PORT_LATENCY_OFFSET_CHANGED,
83     PA_CORE_HOOK_SOURCE_NEW,
84     PA_CORE_HOOK_SOURCE_FIXATE,
85     PA_CORE_HOOK_SOURCE_PUT,
86     PA_CORE_HOOK_SOURCE_UNLINK,
87     PA_CORE_HOOK_SOURCE_UNLINK_POST,
88     PA_CORE_HOOK_SOURCE_STATE_CHANGED,
89     PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED,
90     PA_CORE_HOOK_SOURCE_PORT_CHANGED,
91     PA_CORE_HOOK_SOURCE_FLAGS_CHANGED,
92     PA_CORE_HOOK_SOURCE_VOLUME_CHANGED,
93     PA_CORE_HOOK_SOURCE_MUTE_CHANGED,
94     PA_CORE_HOOK_SOURCE_PORT_LATENCY_OFFSET_CHANGED,
95     PA_CORE_HOOK_SINK_INPUT_NEW,
96     PA_CORE_HOOK_SINK_INPUT_FIXATE,
97     PA_CORE_HOOK_SINK_INPUT_PUT,
98     PA_CORE_HOOK_SINK_INPUT_UNLINK,
99     PA_CORE_HOOK_SINK_INPUT_UNLINK_POST,
100     PA_CORE_HOOK_SINK_INPUT_MOVE_START,
101     PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH,
102     PA_CORE_HOOK_SINK_INPUT_MOVE_FAIL,
103     PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED,
104     PA_CORE_HOOK_SINK_INPUT_PROPLIST_CHANGED,
105     PA_CORE_HOOK_SINK_INPUT_VOLUME_CHANGED,
106     PA_CORE_HOOK_SINK_INPUT_MUTE_CHANGED,
107     PA_CORE_HOOK_SINK_INPUT_PREFERRED_SINK_CHANGED,
108     PA_CORE_HOOK_SINK_INPUT_SEND_EVENT,
109     PA_CORE_HOOK_SOURCE_OUTPUT_NEW,
110     PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE,
111     PA_CORE_HOOK_SOURCE_OUTPUT_PUT,
112     PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK,
113     PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST,
114     PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START,
115     PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH,
116     PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL,
117     PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED,
118     PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED,
119     PA_CORE_HOOK_SOURCE_OUTPUT_VOLUME_CHANGED,
120     PA_CORE_HOOK_SOURCE_OUTPUT_MUTE_CHANGED,
121     PA_CORE_HOOK_SOURCE_OUTPUT_PREFERRED_SOURCE_CHANGED,
122     PA_CORE_HOOK_SOURCE_OUTPUT_SEND_EVENT,
123     PA_CORE_HOOK_CLIENT_NEW,
124     PA_CORE_HOOK_CLIENT_PUT,
125     PA_CORE_HOOK_CLIENT_UNLINK,
126     PA_CORE_HOOK_CLIENT_PROPLIST_CHANGED,
127     PA_CORE_HOOK_CLIENT_SEND_EVENT,
128     PA_CORE_HOOK_CARD_NEW,
129     PA_CORE_HOOK_CARD_CHOOSE_INITIAL_PROFILE,
130     PA_CORE_HOOK_CARD_PUT,
131     PA_CORE_HOOK_CARD_UNLINK,
132     PA_CORE_HOOK_CARD_PREFERRED_PORT_CHANGED,
133     PA_CORE_HOOK_CARD_PROFILE_CHANGED,
134     PA_CORE_HOOK_CARD_PROFILE_ADDED,
135     PA_CORE_HOOK_CARD_PROFILE_AVAILABLE_CHANGED,
136     PA_CORE_HOOK_CARD_SUSPEND_CHANGED,
137     PA_CORE_HOOK_PORT_AVAILABLE_CHANGED,
138     PA_CORE_HOOK_PORT_LATENCY_OFFSET_CHANGED,
139     PA_CORE_HOOK_DEFAULT_SINK_CHANGED,
140     PA_CORE_HOOK_DEFAULT_SOURCE_CHANGED,
141     PA_CORE_HOOK_MODULE_NEW,
142     PA_CORE_HOOK_MODULE_PROPLIST_CHANGED,
143     PA_CORE_HOOK_MODULE_UNLINK,
144     PA_CORE_HOOK_SAMPLE_CACHE_NEW,
145     PA_CORE_HOOK_SAMPLE_CACHE_CHANGED,
146     PA_CORE_HOOK_SAMPLE_CACHE_UNLINK,
147     PA_CORE_HOOK_MAX
148 } pa_core_hook_t;
149 
150 /* The core structure of PulseAudio. Every PulseAudio daemon contains
151  * exactly one of these. It is used for storing kind of global
152  * variables for the daemon. */
153 
154 struct pa_core {
155     pa_msgobject parent;
156 
157     pa_core_state_t state;
158 
159     /* A random value which may be used to identify this instance of
160      * PulseAudio. Not cryptographically secure in any way. */
161     uint32_t cookie;
162 
163     pa_mainloop_api *mainloop;
164 
165     /* idxset of all kinds of entities */
166     pa_idxset *clients, *cards, *sinks, *sources, *sink_inputs, *source_outputs, *modules, *scache;
167 
168     /* Some hashmaps for all sorts of entities */
169     pa_hashmap *namereg, *shared, *message_handlers;
170 
171     /* The default sink/source as configured by the user. If the user hasn't
172      * explicitly configured anything, these are set to NULL. These are strings
173      * instead of sink/source pointers, because that allows us to reference
174      * devices that don't currently exist. That's useful for remembering that
175      * a hotplugged USB sink was previously set as the default sink. */
176     char *configured_default_sink;
177     char *configured_default_source;
178 
179     /* The default sink/source set by some policy module. This will override
180      * the user configured default sink/source, so that the default will
181      * return to the user configured sink/source once the sink/source set by
182      * the policy module is no longer available. */
183     char *policy_default_sink;
184     char *policy_default_source;
185 
186     /* The effective default sink/source. If no sink or source is explicitly
187      * configured as the default, we pick the device that ranks highest
188      * according to the compare_sinks() and compare_sources() functions in
189      * core.c. pa_core_update_default_sink/source() has to be called whenever
190      * anything changes that might change the comparison results. */
191     pa_sink *default_sink;
192     pa_source *default_source;
193 
194     pa_channel_map default_channel_map;
195     pa_sample_spec default_sample_spec;
196     uint32_t alternate_sample_rate;
197     unsigned default_n_fragments, default_fragment_size_msec;
198     unsigned deferred_volume_safety_margin_usec;
199     int deferred_volume_extra_delay_usec;
200     unsigned lfe_crossover_freq;
201 
202     pa_defer_event *module_defer_unload_event;
203     pa_hashmap *modules_pending_unload; /* pa_module -> pa_module (hashmap-as-a-set) */
204 
205     pa_defer_event *subscription_defer_event;
206     PA_LLIST_HEAD(pa_subscription, subscriptions);
207     PA_LLIST_HEAD(pa_subscription_event, subscription_event_queue);
208     pa_subscription_event *subscription_event_last;
209 
210     /* The mempool is used for data we write to, it's readonly for the client. */
211     pa_mempool *mempool;
212 
213     /* Shared memory size, as specified either by daemon configuration
214      * or PA daemon defaults (~ 64 MiB). */
215     size_t shm_size;
216 
217     pa_silence_cache silence_cache;
218 
219     pa_time_event *exit_event;
220     pa_time_event *scache_auto_unload_event;
221 
222     int exit_idle_time, scache_idle_time;
223 
224     bool flat_volumes:1;
225     bool rescue_streams:1;
226     bool disallow_module_loading:1;
227     bool disallow_exit:1;
228     bool running_as_daemon:1;
229     bool realtime_scheduling:1;
230     bool avoid_resampling:1;
231     bool disable_remixing:1;
232     bool remixing_use_all_sink_channels:1;
233     bool remixing_produce_lfe:1;
234     bool remixing_consume_lfe:1;
235     bool deferred_volume:1;
236 
237     pa_resample_method_t resample_method;
238     int realtime_priority;
239 
240     pa_server_type_t server_type;
241     pa_cpu_info cpu_info;
242 
243     /* hooks */
244     pa_hook hooks[PA_CORE_HOOK_MAX];
245 };
246 
247 PA_DECLARE_PUBLIC_CLASS(pa_core);
248 #define PA_CORE(o) pa_core_cast(o)
249 
250 enum {
251     PA_CORE_MESSAGE_UNLOAD_MODULE,
252     PA_CORE_MESSAGE_MAX
253 };
254 
255 pa_core* pa_core_new(pa_mainloop_api *m, bool shared, bool enable_memfd, size_t shm_size);
256 
257 void pa_core_set_configured_default_sink(pa_core *core, const char *sink);
258 void pa_core_set_configured_default_source(pa_core *core, const char *source);
259 void pa_core_set_policy_default_sink(pa_core *core, const char *sink);
260 void pa_core_set_policy_default_source(pa_core *core, const char *source);
261 
262 /* These should be called whenever something changes that may affect the
263  * default sink or source choice.
264  *
265  * If the default source choice happens between two monitor sources, the
266  * monitored sinks are compared, so if the default sink changes, the default
267  * source may change too. However, pa_core_update_default_sink() calls
268  * pa_core_update_default_source() internally, so it's sufficient to only call
269  * pa_core_update_default_sink() when something happens that affects the sink
270  * ordering. */
271 void pa_core_update_default_sink(pa_core *core);
272 void pa_core_update_default_source(pa_core *core);
273 
274 void pa_core_set_exit_idle_time(pa_core *core, int time);
275 
276 /* Check whether no one is connected to this core */
277 void pa_core_check_idle(pa_core *c);
278 
279 int pa_core_exit(pa_core *c, bool force, int retval);
280 
281 void pa_core_maybe_vacuum(pa_core *c);
282 
283 /* wrapper for c->mainloop->time_*() RT time events */
284 pa_time_event* pa_core_rttime_new(pa_core *c, pa_usec_t usec, pa_time_event_cb_t cb, void *userdata);
285 void pa_core_rttime_restart(pa_core *c, pa_time_event *e, pa_usec_t usec);
286 
287 static const size_t PA_SUSPEND_CAUSE_TO_STRING_BUF_SIZE =
288     sizeof("USER|APPLICATION|IDLE|SESSION|PASSTHROUGH|INTERNAL|UNAVAILABLE");
289 
290 /* Converts the given suspend cause to a string. The string is written to the
291  * provided buffer. The same buffer is the return value of this function. */
292 const char *pa_suspend_cause_to_string(pa_suspend_cause_t cause, char buf[PA_SUSPEND_CAUSE_TO_STRING_BUF_SIZE]);
293 
294 void pa_core_move_streams_to_newly_available_preferred_sink(pa_core *c, pa_sink *s);
295 
296 void pa_core_move_streams_to_newly_available_preferred_source(pa_core *c, pa_source *s);
297 
298 #endif