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