]> code.delx.au - pulseaudio/blob - src/polypcore/core-scache.c
Wrap strerror() in a function that makes it thread safe and converts the
[pulseaudio] / src / polypcore / core-scache.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 <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <dirent.h>
32 #include <sys/stat.h>
33 #include <errno.h>
34 #include <limits.h>
35
36 #ifdef HAVE_GLOB_H
37 #include <glob.h>
38 #endif
39
40 #ifdef HAVE_WINDOWS_H
41 #include <windows.h>
42 #endif
43
44 #include <polyp/error.h>
45 #include <polyp/mainloop.h>
46 #include <polyp/channelmap.h>
47 #include <polyp/timeval.h>
48 #include <polyp/util.h>
49 #include <polyp/volume.h>
50 #include <polyp/xmalloc.h>
51
52 #include <polypcore/sink-input.h>
53 #include <polypcore/sample-util.h>
54 #include <polypcore/play-memchunk.h>
55 #include <polypcore/core-subscribe.h>
56 #include <polypcore/namereg.h>
57 #include <polypcore/sound-file.h>
58 #include <polypcore/core-util.h>
59 #include <polypcore/log.h>
60
61 #include "core-scache.h"
62
63 #define UNLOAD_POLL_TIME 2
64
65 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
66 pa_core *c = userdata;
67 struct timeval ntv;
68 assert(c && c->mainloop == m && c->scache_auto_unload_event == e);
69
70 pa_scache_unload_unused(c);
71
72 pa_gettimeofday(&ntv);
73 ntv.tv_sec += UNLOAD_POLL_TIME;
74 m->time_restart(e, &ntv);
75 }
76
77 static void free_entry(pa_scache_entry *e) {
78 assert(e);
79 pa_namereg_unregister(e->core, e->name);
80 pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_REMOVE, e->index);
81 pa_xfree(e->name);
82 pa_xfree(e->filename);
83 if (e->memchunk.memblock)
84 pa_memblock_unref(e->memchunk.memblock);
85 pa_xfree(e);
86 }
87
88 static pa_scache_entry* scache_add_item(pa_core *c, const char *name) {
89 pa_scache_entry *e;
90 assert(c && name);
91
92 if ((e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0))) {
93 if (e->memchunk.memblock)
94 pa_memblock_unref(e->memchunk.memblock);
95
96 pa_xfree(e->filename);
97
98 assert(e->core == c);
99
100 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
101 } else {
102 e = pa_xmalloc(sizeof(pa_scache_entry));
103
104 if (!pa_namereg_register(c, name, PA_NAMEREG_SAMPLE, e, 1)) {
105 pa_xfree(e);
106 return NULL;
107 }
108
109 e->name = pa_xstrdup(name);
110 e->core = c;
111
112 if (!c->scache) {
113 c->scache = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
114 assert(c->scache);
115 }
116
117 pa_idxset_put(c->scache, e, &e->index);
118
119 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_NEW, e->index);
120 }
121
122 e->last_used_time = 0;
123 e->memchunk.memblock = NULL;
124 e->memchunk.index = e->memchunk.length = 0;
125 e->filename = NULL;
126 e->lazy = 0;
127 e->last_used_time = 0;
128
129 memset(&e->sample_spec, 0, sizeof(e->sample_spec));
130 pa_channel_map_init(&e->channel_map);
131 pa_cvolume_reset(&e->volume, PA_CHANNELS_MAX);
132
133 return e;
134 }
135
136 int pa_scache_add_item(pa_core *c, const char *name, const pa_sample_spec *ss, const pa_channel_map *map, const pa_memchunk *chunk, uint32_t *idx) {
137 pa_scache_entry *e;
138 assert(c && name);
139
140 if (chunk && chunk->length > PA_SCACHE_ENTRY_SIZE_MAX)
141 return -1;
142
143 if (!(e = scache_add_item(c, name)))
144 return -1;
145
146 if (ss) {
147 e->sample_spec = *ss;
148 pa_channel_map_init_auto(&e->channel_map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
149 e->volume.channels = e->sample_spec.channels;
150 }
151
152 if (map)
153 e->channel_map = *map;
154
155 if (chunk) {
156 e->memchunk = *chunk;
157 pa_memblock_ref(e->memchunk.memblock);
158 }
159
160 if (idx)
161 *idx = e->index;
162
163 return 0;
164 }
165
166 int pa_scache_add_file(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
167 pa_sample_spec ss;
168 pa_channel_map map;
169 pa_memchunk chunk;
170 int r;
171
172 #ifdef OS_IS_WIN32
173 char buf[MAX_PATH];
174
175 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
176 filename = buf;
177 #endif
178
179 if (pa_sound_file_load(filename, &ss, &map, &chunk, c->memblock_stat) < 0)
180 return -1;
181
182 r = pa_scache_add_item(c, name, &ss, &map, &chunk, idx);
183 pa_memblock_unref(chunk.memblock);
184
185 return r;
186 }
187
188 int pa_scache_add_file_lazy(pa_core *c, const char *name, const char *filename, uint32_t *idx) {
189 pa_scache_entry *e;
190
191 #ifdef OS_IS_WIN32
192 char buf[MAX_PATH];
193
194 if (ExpandEnvironmentStrings(filename, buf, MAX_PATH))
195 filename = buf;
196 #endif
197
198 assert(c && name);
199
200 if (!(e = scache_add_item(c, name)))
201 return -1;
202
203 e->lazy = 1;
204 e->filename = pa_xstrdup(filename);
205
206 if (!c->scache_auto_unload_event) {
207 struct timeval ntv;
208 pa_gettimeofday(&ntv);
209 ntv.tv_sec += UNLOAD_POLL_TIME;
210 c->scache_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
211 }
212
213 if (idx)
214 *idx = e->index;
215
216 return 0;
217 }
218
219 int pa_scache_remove_item(pa_core *c, const char *name) {
220 pa_scache_entry *e;
221 assert(c && name);
222
223 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
224 return -1;
225
226 if (pa_idxset_remove_by_data(c->scache, e, NULL) != e)
227 assert(0);
228
229 free_entry(e);
230 return 0;
231 }
232
233 static void free_cb(void *p, PA_GCC_UNUSED void *userdata) {
234 pa_scache_entry *e = p;
235 assert(e);
236 free_entry(e);
237 }
238
239 void pa_scache_free(pa_core *c) {
240 assert(c);
241
242 if (c->scache) {
243 pa_idxset_free(c->scache, free_cb, NULL);
244 c->scache = NULL;
245 }
246
247 if (c->scache_auto_unload_event)
248 c->mainloop->time_free(c->scache_auto_unload_event);
249 }
250
251 int pa_scache_play_item(pa_core *c, const char *name, pa_sink *sink, pa_volume_t volume) {
252 pa_scache_entry *e;
253 char *t;
254 pa_cvolume r;
255
256 assert(c);
257 assert(name);
258 assert(sink);
259
260 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
261 return -1;
262
263 if (e->lazy && !e->memchunk.memblock) {
264 if (pa_sound_file_load(e->filename, &e->sample_spec, &e->channel_map, &e->memchunk, c->memblock_stat) < 0)
265 return -1;
266
267 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
268
269 if (e->volume.channels > e->sample_spec.channels)
270 e->volume.channels = e->sample_spec.channels;
271 }
272
273 if (!e->memchunk.memblock)
274 return -1;
275
276 t = pa_sprintf_malloc("sample:%s", name);
277
278 pa_cvolume_set(&r, e->volume.channels, volume);
279 pa_sw_cvolume_multiply(&r, &r, &e->volume);
280
281 if (pa_play_memchunk(sink, t, &e->sample_spec, &e->channel_map, &e->memchunk, &r) < 0) {
282 pa_xfree(t);
283 return -1;
284 }
285
286 pa_xfree(t);
287
288 if (e->lazy)
289 time(&e->last_used_time);
290
291 return 0;
292 }
293
294 const char * pa_scache_get_name_by_id(pa_core *c, uint32_t id) {
295 pa_scache_entry *e;
296 assert(c && id != PA_IDXSET_INVALID);
297
298 if (!c->scache || !(e = pa_idxset_get_by_index(c->scache, id)))
299 return NULL;
300
301 return e->name;
302 }
303
304 uint32_t pa_scache_get_id_by_name(pa_core *c, const char *name) {
305 pa_scache_entry *e;
306 assert(c && name);
307
308 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
309 return PA_IDXSET_INVALID;
310
311 return e->index;
312 }
313
314 uint32_t pa_scache_total_size(pa_core *c) {
315 pa_scache_entry *e;
316 uint32_t idx, sum = 0;
317 assert(c);
318
319 if (!c->scache || !pa_idxset_size(c->scache))
320 return 0;
321
322 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx))
323 if (e->memchunk.memblock)
324 sum += e->memchunk.length;
325
326 return sum;
327 }
328
329 void pa_scache_unload_unused(pa_core *c) {
330 pa_scache_entry *e;
331 time_t now;
332 uint32_t idx;
333 assert(c);
334
335 if (!c->scache || !pa_idxset_size(c->scache))
336 return;
337
338 time(&now);
339
340 for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx)) {
341
342 if (!e->lazy || !e->memchunk.memblock)
343 continue;
344
345 if (e->last_used_time + c->scache_idle_time > now)
346 continue;
347
348 pa_memblock_unref(e->memchunk.memblock);
349 e->memchunk.memblock = NULL;
350 e->memchunk.index = e->memchunk.length = 0;
351
352 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
353 }
354 }
355
356 static void add_file(pa_core *c, const char *pathname) {
357 struct stat st;
358 const char *e;
359
360 e = pa_path_get_filename(pathname);
361
362 if (stat(pathname, &st) < 0) {
363 pa_log(__FILE__": stat('%s'): %s", pathname, pa_cstrerror(errno));
364 return;
365 }
366
367 #if defined(S_ISREG) && defined(S_ISLNK)
368 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
369 #endif
370 pa_scache_add_file_lazy(c, e, pathname, NULL);
371 }
372
373 int pa_scache_add_directory_lazy(pa_core *c, const char *pathname) {
374 DIR *dir;
375 assert(c && pathname);
376
377 /* First try to open this as directory */
378 if (!(dir = opendir(pathname))) {
379 #ifdef HAVE_GLOB_H
380 glob_t p;
381 unsigned int i;
382 /* If that fails, try to open it as shell glob */
383
384 if (glob(pathname, GLOB_ERR|GLOB_NOSORT, NULL, &p) < 0) {
385 pa_log(__FILE__": failed to open directory '%s': %s", pathname, pa_cstrerror(errno));
386 return -1;
387 }
388
389 for (i = 0; i < p.gl_pathc; i++)
390 add_file(c, p.gl_pathv[i]);
391
392 globfree(&p);
393 #else
394 return -1;
395 #endif
396 } else {
397 struct dirent *e;
398
399 while ((e = readdir(dir))) {
400 char p[PATH_MAX];
401
402 if (e->d_name[0] == '.')
403 continue;
404
405 snprintf(p, sizeof(p), "%s/%s", pathname, e->d_name);
406 add_file(c, p);
407 }
408 }
409
410 closedir(dir);
411 return 0;
412 }