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>
38 #include "sink-input.h"
40 #include "sample-util.h"
41 #include "play-memchunk.h"
43 #include "subscribe.h"
45 #include "sound-file.h"
49 #define UNLOAD_POLL_TIME 2
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
;
54 assert(c
&& c
->mainloop
== m
&& c
->scache_auto_unload_event
== e
);
56 pa_scache_unload_unused(c
);
58 gettimeofday(&ntv
, NULL
);
59 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
60 m
->time_restart(e
, &ntv
);
63 static void free_entry(struct pa_scache_entry
*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
);
68 pa_xfree(e
->filename
);
69 if (e
->memchunk
.memblock
)
70 pa_memblock_unref(e
->memchunk
.memblock
);
74 static struct pa_scache_entry
* scache_add_item(struct pa_core
*c
, const char *name
) {
75 struct pa_scache_entry
*e
;
78 if ((e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0))) {
79 if (e
->memchunk
.memblock
)
80 pa_memblock_unref(e
->memchunk
.memblock
);
82 pa_xfree(e
->filename
);
86 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
88 e
= pa_xmalloc(sizeof(struct pa_scache_entry
));
90 if (!pa_namereg_register(c
, name
, PA_NAMEREG_SAMPLE
, e
, 1)) {
95 e
->name
= pa_xstrdup(name
);
99 c
->scache
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
103 pa_idxset_put(c
->scache
, e
, &e
->index
);
105 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_NEW
, e
->index
);
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;
114 e
->last_used_time
= 0;
116 memset(&e
->sample_spec
, 0, sizeof(struct pa_sample_spec
));
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
;
125 if (!(e
= scache_add_item(c
, name
)))
129 e
->sample_spec
= *ss
;
132 e
->memchunk
= *chunk
;
133 pa_memblock_ref(e
->memchunk
.memblock
);
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
;
147 if (pa_sound_file_load(filename
, &ss
, &chunk
, c
->memblock_stat
) < 0)
150 r
= pa_scache_add_item(c
, name
, &ss
, &chunk
, index
);
151 pa_memblock_unref(chunk
.memblock
);
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
;
160 if (!(e
= scache_add_item(c
, name
)))
164 e
->filename
= pa_xstrdup(filename
);
166 if (!c
->scache_auto_unload_event
) {
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
);
176 int pa_scache_remove_item(struct pa_core
*c
, const char *name
) {
177 struct pa_scache_entry
*e
;
180 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0)))
183 if (pa_idxset_remove_by_data(c
->scache
, e
, NULL
) != e
)
190 static void free_cb(void *p
, void *userdata
) {
191 struct pa_scache_entry
*e
= p
;
196 void pa_scache_free(struct pa_core
*c
) {
200 pa_idxset_free(c
->scache
, free_cb
, NULL
);
204 if (c
->scache_auto_unload_event
)
205 c
->mainloop
->time_free(c
->scache_auto_unload_event
);
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
;
211 assert(c
&& name
&& sink
);
213 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 1)))
216 if (e
->lazy
&& !e
->memchunk
.memblock
) {
217 if (pa_sound_file_load(e
->filename
, &e
->sample_spec
, &e
->memchunk
, c
->memblock_stat
) < 0)
220 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
223 if (!e
->memchunk
.memblock
)
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) {
235 time(&e
->last_used_time
);
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
);
244 if (!c
->scache
|| !(e
= pa_idxset_get_by_index(c
->scache
, id
)))
250 uint32_t pa_scache_get_id_by_name(struct pa_core
*c
, const char *name
) {
251 struct pa_scache_entry
*e
;
254 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0)))
255 return PA_IDXSET_INVALID
;
260 uint32_t pa_scache_total_size(struct pa_core
*c
) {
261 struct pa_scache_entry
*e
;
262 uint32_t index
, sum
= 0;
265 if (!c
->scache
|| !pa_idxset_ncontents(c
->scache
))
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
;
275 void pa_scache_unload_unused(struct pa_core
*c
) {
276 struct pa_scache_entry
*e
;
281 if (!c
->scache
|| !pa_idxset_ncontents(c
->scache
))
286 for (e
= pa_idxset_first(c
->scache
, &index
); e
; e
= pa_idxset_next(c
->scache
, &index
)) {
288 if (!e
->lazy
|| !e
->memchunk
.memblock
)
291 if (e
->last_used_time
+ c
->scache_idle_time
> now
)
294 pa_memblock_unref(e
->memchunk
.memblock
);
295 e
->memchunk
.memblock
= NULL
;
296 e
->memchunk
.index
= e
->memchunk
.length
= 0;
298 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
302 static void add_file(struct pa_core
*c
, const char *pathname
) {
306 if (!(e
= strrchr(pathname
, '/')))
311 if (stat(pathname
, &st
) < 0) {
312 pa_log(__FILE__
": stat('%s') failed: %s\n", pathname
, strerror(errno
));
316 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
317 pa_scache_add_file_lazy(c
, e
, pathname
, NULL
);
320 int pa_scache_add_directory_lazy(struct pa_core
*c
, const char *pathname
) {
322 assert(c
&& pathname
);
324 /* First try to open this as directory */
325 if (!(dir
= opendir(pathname
))) {
328 /* If that fails, try to open it as shell glob */
330 if (glob(pathname
, GLOB_ERR
|GLOB_NOSORT
, NULL
, &p
) < 0) {
331 pa_log(__FILE__
": Failed to open directory: %s\n", strerror(errno
));
335 for (i
= 0; i
< p
.gl_pathc
; i
++)
336 add_file(c
, p
.gl_pathv
[i
]);
342 while ((e
= readdir(dir
))) {
345 if (e
->d_name
[0] == '.')
348 snprintf(p
, sizeof(p
), "%s/%s", pathname
, e
->d_name
);