4 This file is part of polypaudio.
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.
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.
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
30 #include <sys/types.h>
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>
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>
59 #include "core-scache.h"
61 #define UNLOAD_POLL_TIME 2
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
;
66 assert(c
&& c
->mainloop
== m
&& c
->scache_auto_unload_event
== e
);
68 pa_scache_unload_unused(c
);
70 pa_gettimeofday(&ntv
);
71 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
72 m
->time_restart(e
, &ntv
);
75 static void free_entry(pa_scache_entry
*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
);
80 pa_xfree(e
->filename
);
81 if (e
->memchunk
.memblock
)
82 pa_memblock_unref(e
->memchunk
.memblock
);
86 static pa_scache_entry
* scache_add_item(pa_core
*c
, const char *name
) {
90 if ((e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0))) {
91 if (e
->memchunk
.memblock
)
92 pa_memblock_unref(e
->memchunk
.memblock
);
94 pa_xfree(e
->filename
);
98 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
100 e
= pa_xmalloc(sizeof(pa_scache_entry
));
102 if (!pa_namereg_register(c
, name
, PA_NAMEREG_SAMPLE
, e
, 1)) {
107 e
->name
= pa_xstrdup(name
);
111 c
->scache
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
115 pa_idxset_put(c
->scache
, e
, &e
->index
);
117 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_NEW
, e
->index
);
120 e
->last_used_time
= 0;
121 e
->memchunk
.memblock
= NULL
;
122 e
->memchunk
.index
= e
->memchunk
.length
= 0;
125 e
->last_used_time
= 0;
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
);
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
) {
138 if (chunk
&& chunk
->length
> PA_SCACHE_ENTRY_SIZE_MAX
)
141 if (!(e
= scache_add_item(c
, name
)))
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
;
151 e
->channel_map
= *map
;
154 e
->memchunk
= *chunk
;
155 pa_memblock_ref(e
->memchunk
.memblock
);
164 int pa_scache_add_file(pa_core
*c
, const char *name
, const char *filename
, uint32_t *idx
) {
173 if (ExpandEnvironmentStrings(filename
, buf
, MAX_PATH
))
177 if (pa_sound_file_load(filename
, &ss
, &map
, &chunk
, c
->memblock_stat
) < 0)
180 r
= pa_scache_add_item(c
, name
, &ss
, &map
, &chunk
, idx
);
181 pa_memblock_unref(chunk
.memblock
);
186 int pa_scache_add_file_lazy(pa_core
*c
, const char *name
, const char *filename
, uint32_t *idx
) {
192 if (ExpandEnvironmentStrings(filename
, buf
, MAX_PATH
))
198 if (!(e
= scache_add_item(c
, name
)))
202 e
->filename
= pa_xstrdup(filename
);
204 if (!c
->scache_auto_unload_event
) {
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
);
217 int pa_scache_remove_item(pa_core
*c
, const char *name
) {
221 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0)))
224 if (pa_idxset_remove_by_data(c
->scache
, e
, NULL
) != e
)
231 static void free_cb(void *p
, PA_GCC_UNUSED
void *userdata
) {
232 pa_scache_entry
*e
= p
;
237 void pa_scache_free(pa_core
*c
) {
241 pa_idxset_free(c
->scache
, free_cb
, NULL
);
245 if (c
->scache_auto_unload_event
)
246 c
->mainloop
->time_free(c
->scache_auto_unload_event
);
249 int pa_scache_play_item(pa_core
*c
, const char *name
, pa_sink
*sink
, pa_volume_t volume
) {
258 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 1)))
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)
265 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
267 if (e
->volume
.channels
> e
->sample_spec
.channels
)
268 e
->volume
.channels
= e
->sample_spec
.channels
;
271 if (!e
->memchunk
.memblock
)
274 t
= pa_sprintf_malloc("sample:%s", name
);
276 pa_cvolume_set(&r
, e
->volume
.channels
, volume
);
277 pa_sw_cvolume_multiply(&r
, &r
, &e
->volume
);
279 if (pa_play_memchunk(sink
, t
, &e
->sample_spec
, &e
->channel_map
, &e
->memchunk
, &r
) < 0) {
287 time(&e
->last_used_time
);
292 const char * pa_scache_get_name_by_id(pa_core
*c
, uint32_t id
) {
294 assert(c
&& id
!= PA_IDXSET_INVALID
);
296 if (!c
->scache
|| !(e
= pa_idxset_get_by_index(c
->scache
, id
)))
302 uint32_t pa_scache_get_id_by_name(pa_core
*c
, const char *name
) {
306 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0)))
307 return PA_IDXSET_INVALID
;
312 uint32_t pa_scache_total_size(pa_core
*c
) {
314 uint32_t idx
, sum
= 0;
317 if (!c
->scache
|| !pa_idxset_size(c
->scache
))
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
;
327 void pa_scache_unload_unused(pa_core
*c
) {
333 if (!c
->scache
|| !pa_idxset_size(c
->scache
))
338 for (e
= pa_idxset_first(c
->scache
, &idx
); e
; e
= pa_idxset_next(c
->scache
, &idx
)) {
340 if (!e
->lazy
|| !e
->memchunk
.memblock
)
343 if (e
->last_used_time
+ c
->scache_idle_time
> now
)
346 pa_memblock_unref(e
->memchunk
.memblock
);
347 e
->memchunk
.memblock
= NULL
;
348 e
->memchunk
.index
= e
->memchunk
.length
= 0;
350 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
354 static void add_file(pa_core
*c
, const char *pathname
) {
358 e
= pa_path_get_filename(pathname
);
360 if (stat(pathname
, &st
) < 0) {
361 pa_log(__FILE__
": stat('%s') failed: %s", pathname
, strerror(errno
));
365 #if defined(S_ISREG) && defined(S_ISLNK)
366 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
368 pa_scache_add_file_lazy(c
, e
, pathname
, NULL
);
371 int pa_scache_add_directory_lazy(pa_core
*c
, const char *pathname
) {
373 assert(c
&& pathname
);
375 /* First try to open this as directory */
376 if (!(dir
= opendir(pathname
))) {
380 /* If that fails, try to open it as shell glob */
382 if (glob(pathname
, GLOB_ERR
|GLOB_NOSORT
, NULL
, &p
) < 0) {
383 pa_log(__FILE__
": Failed to open directory: %s", strerror(errno
));
387 for (i
= 0; i
< p
.gl_pathc
; i
++)
388 add_file(c
, p
.gl_pathv
[i
]);
397 while ((e
= readdir(dir
))) {
400 if (e
->d_name
[0] == '.')
403 snprintf(p
, sizeof(p
), "%s/%s", pathname
, e
->d_name
);