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