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