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 "core-scache.h"
45 #include "sink-input.h"
46 #include <polyp/mainloop.h>
47 #include "sample-util.h"
48 #include "play-memchunk.h"
50 #include "core-subscribe.h"
52 #include "sound-file.h"
55 #include <polyp/channelmap.h>
56 #include <polyp/volume.h>
58 #define UNLOAD_POLL_TIME 2
60 static void timeout_callback(pa_mainloop_api
*m
, pa_time_event
*e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
61 pa_core
*c
= userdata
;
63 assert(c
&& c
->mainloop
== m
&& c
->scache_auto_unload_event
== e
);
65 pa_scache_unload_unused(c
);
67 pa_gettimeofday(&ntv
);
68 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
69 m
->time_restart(e
, &ntv
);
72 static void free_entry(pa_scache_entry
*e
) {
74 pa_namereg_unregister(e
->core
, e
->name
);
75 pa_subscription_post(e
->core
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_REMOVE
, e
->index
);
77 pa_xfree(e
->filename
);
78 if (e
->memchunk
.memblock
)
79 pa_memblock_unref(e
->memchunk
.memblock
);
83 static pa_scache_entry
* scache_add_item(pa_core
*c
, const char *name
) {
87 if ((e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0))) {
88 if (e
->memchunk
.memblock
)
89 pa_memblock_unref(e
->memchunk
.memblock
);
91 pa_xfree(e
->filename
);
95 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
97 e
= pa_xmalloc(sizeof(pa_scache_entry
));
99 if (!pa_namereg_register(c
, name
, PA_NAMEREG_SAMPLE
, e
, 1)) {
104 e
->name
= pa_xstrdup(name
);
108 c
->scache
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
112 pa_idxset_put(c
->scache
, e
, &e
->index
);
114 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_NEW
, e
->index
);
117 pa_cvolume_reset(&e
->volume
, PA_CHANNELS_MAX
);
118 e
->last_used_time
= 0;
119 e
->memchunk
.memblock
= NULL
;
120 e
->memchunk
.index
= e
->memchunk
.length
= 0;
123 e
->last_used_time
= 0;
125 memset(&e
->sample_spec
, 0, sizeof(pa_sample_spec
));
130 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
) {
134 if (!(e
= scache_add_item(c
, name
)))
138 e
->sample_spec
= *ss
;
139 pa_channel_map_init_auto(&e
->channel_map
, ss
->channels
);
143 e
->channel_map
= *map
;
146 e
->memchunk
= *chunk
;
147 pa_memblock_ref(e
->memchunk
.memblock
);
156 int pa_scache_add_file(pa_core
*c
, const char *name
, const char *filename
, uint32_t *idx
) {
164 if (ExpandEnvironmentStrings(filename
, buf
, MAX_PATH
))
168 if (pa_sound_file_load(filename
, &ss
, &chunk
, c
->memblock_stat
) < 0)
171 r
= pa_scache_add_item(c
, name
, &ss
, NULL
, &chunk
, idx
);
172 pa_memblock_unref(chunk
.memblock
);
177 int pa_scache_add_file_lazy(pa_core
*c
, const char *name
, const char *filename
, uint32_t *idx
) {
183 if (ExpandEnvironmentStrings(filename
, buf
, MAX_PATH
))
189 if (!(e
= scache_add_item(c
, name
)))
193 e
->filename
= pa_xstrdup(filename
);
195 if (!c
->scache_auto_unload_event
) {
197 pa_gettimeofday(&ntv
);
198 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
199 c
->scache_auto_unload_event
= c
->mainloop
->time_new(c
->mainloop
, &ntv
, timeout_callback
, c
);
208 int pa_scache_remove_item(pa_core
*c
, const char *name
) {
212 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0)))
215 if (pa_idxset_remove_by_data(c
->scache
, e
, NULL
) != e
)
222 static void free_cb(void *p
, PA_GCC_UNUSED
void *userdata
) {
223 pa_scache_entry
*e
= p
;
228 void pa_scache_free(pa_core
*c
) {
232 pa_idxset_free(c
->scache
, free_cb
, NULL
);
236 if (c
->scache_auto_unload_event
)
237 c
->mainloop
->time_free(c
->scache_auto_unload_event
);
240 int pa_scache_play_item(pa_core
*c
, const char *name
, pa_sink
*sink
, const pa_cvolume
*volume
) {
244 assert(c
&& name
&& sink
);
246 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 1)))
249 if (e
->lazy
&& !e
->memchunk
.memblock
) {
250 if (pa_sound_file_load(e
->filename
, &e
->sample_spec
, &e
->memchunk
, c
->memblock_stat
) < 0)
253 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
256 if (!e
->memchunk
.memblock
)
259 t
= pa_sprintf_malloc("sample:%s", name
);
261 if (pa_play_memchunk(sink
, t
, &e
->sample_spec
, &e
->channel_map
, &e
->memchunk
, pa_sw_cvolume_multiply(&r
, volume
, &e
->volume
)) < 0) {
269 time(&e
->last_used_time
);
274 const char * pa_scache_get_name_by_id(pa_core
*c
, uint32_t id
) {
276 assert(c
&& id
!= PA_IDXSET_INVALID
);
278 if (!c
->scache
|| !(e
= pa_idxset_get_by_index(c
->scache
, id
)))
284 uint32_t pa_scache_get_id_by_name(pa_core
*c
, const char *name
) {
288 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0)))
289 return PA_IDXSET_INVALID
;
294 uint32_t pa_scache_total_size(pa_core
*c
) {
296 uint32_t idx
, sum
= 0;
299 if (!c
->scache
|| !pa_idxset_size(c
->scache
))
302 for (e
= pa_idxset_first(c
->scache
, &idx
); e
; e
= pa_idxset_next(c
->scache
, &idx
))
303 if (e
->memchunk
.memblock
)
304 sum
+= e
->memchunk
.length
;
309 void pa_scache_unload_unused(pa_core
*c
) {
315 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
)) {
322 if (!e
->lazy
|| !e
->memchunk
.memblock
)
325 if (e
->last_used_time
+ c
->scache_idle_time
> now
)
328 pa_memblock_unref(e
->memchunk
.memblock
);
329 e
->memchunk
.memblock
= NULL
;
330 e
->memchunk
.index
= e
->memchunk
.length
= 0;
332 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
336 static void add_file(pa_core
*c
, const char *pathname
) {
340 e
= pa_path_get_filename(pathname
);
342 if (stat(pathname
, &st
) < 0) {
343 pa_log(__FILE__
": stat('%s') failed: %s\n", pathname
, strerror(errno
));
347 #if defined(S_ISREG) && defined(S_ISLNK)
348 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
350 pa_scache_add_file_lazy(c
, e
, pathname
, NULL
);
353 int pa_scache_add_directory_lazy(pa_core
*c
, const char *pathname
) {
355 assert(c
&& pathname
);
357 /* First try to open this as directory */
358 if (!(dir
= opendir(pathname
))) {
362 /* If that fails, try to open it as shell glob */
364 if (glob(pathname
, GLOB_ERR
|GLOB_NOSORT
, NULL
, &p
) < 0) {
365 pa_log(__FILE__
": Failed to open directory: %s\n", strerror(errno
));
369 for (i
= 0; i
< p
.gl_pathc
; i
++)
370 add_file(c
, p
.gl_pathv
[i
]);
379 while ((e
= readdir(dir
))) {
382 if (e
->d_name
[0] == '.')
385 snprintf(p
, sizeof(p
), "%s/%s", pathname
, e
->d_name
);