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 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 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
32 #include "sink-input.h"
34 #include "sample-util.h"
35 #include "play-memchunk.h"
37 #include "subscribe.h"
39 #include "sound-file.h"
41 #define UNLOAD_POLL_TIME 2
43 static void timeout_callback(struct pa_mainloop_api
*m
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
44 struct pa_core
*c
= userdata
;
46 assert(c
&& c
->mainloop
== m
&& c
->scache_auto_unload_event
== e
);
48 pa_scache_unload_unused(c
);
50 gettimeofday(&ntv
, NULL
);
51 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
52 m
->time_restart(e
, &ntv
);
55 static void free_entry(struct pa_scache_entry
*e
) {
57 pa_namereg_unregister(e
->core
, e
->name
);
58 pa_subscription_post(e
->core
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_REMOVE
, e
->index
);
60 pa_xfree(e
->filename
);
61 if (e
->memchunk
.memblock
)
62 pa_memblock_unref(e
->memchunk
.memblock
);
66 static struct pa_scache_entry
* scache_add_item(struct pa_core
*c
, const char *name
) {
67 struct pa_scache_entry
*e
;
70 if ((e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0))) {
71 if (e
->memchunk
.memblock
)
72 pa_memblock_unref(e
->memchunk
.memblock
);
74 pa_xfree(e
->filename
);
78 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
80 e
= pa_xmalloc(sizeof(struct pa_scache_entry
));
82 if (!pa_namereg_register(c
, name
, PA_NAMEREG_SAMPLE
, e
, 1)) {
87 e
->name
= pa_xstrdup(name
);
91 c
->scache
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
95 pa_idxset_put(c
->scache
, e
, &e
->index
);
97 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_NEW
, e
->index
);
100 e
->volume
= PA_VOLUME_NORM
;
101 e
->last_used_time
= 0;
102 e
->memchunk
.memblock
= NULL
;
103 e
->memchunk
.index
= e
->memchunk
.length
= 0;
106 e
->last_used_time
= 0;
108 memset(&e
->sample_spec
, 0, sizeof(struct pa_sample_spec
));
113 int pa_scache_add_item(struct pa_core
*c
, const char *name
, struct pa_sample_spec
*ss
, struct pa_memchunk
*chunk
, uint32_t *index
) {
114 struct pa_scache_entry
*e
;
117 if (!(e
= scache_add_item(c
, name
)))
121 e
->sample_spec
= *ss
;
124 e
->memchunk
= *chunk
;
125 pa_memblock_ref(e
->memchunk
.memblock
);
134 int pa_scache_add_file(struct pa_core
*c
, const char *name
, const char *filename
, uint32_t *index
) {
135 struct pa_sample_spec ss
;
136 struct pa_memchunk chunk
;
139 if (pa_sound_file_load(filename
, &ss
, &chunk
, c
->memblock_stat
) < 0)
142 r
= pa_scache_add_item(c
, name
, &ss
, &chunk
, index
);
143 pa_memblock_unref(chunk
.memblock
);
147 int pa_scache_add_file_lazy(struct pa_core
*c
, const char *name
, const char *filename
, uint32_t *index
) {
148 struct pa_scache_entry
*e
;
151 if (!(e
= scache_add_item(c
, name
)))
155 e
->filename
= pa_xstrdup(filename
);
157 if (!c
->scache_auto_unload_event
) {
159 gettimeofday(&ntv
, NULL
);
160 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
161 c
->scache_auto_unload_event
= c
->mainloop
->time_new(c
->mainloop
, &ntv
, timeout_callback
, c
);
167 int pa_scache_remove_item(struct pa_core
*c
, const char *name
) {
168 struct pa_scache_entry
*e
;
171 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0)))
174 if (pa_idxset_remove_by_data(c
->scache
, e
, NULL
) != e
)
181 static void free_cb(void *p
, void *userdata
) {
182 struct pa_scache_entry
*e
= p
;
187 void pa_scache_free(struct pa_core
*c
) {
191 pa_idxset_free(c
->scache
, free_cb
, NULL
);
195 if (c
->scache_auto_unload_event
)
196 c
->mainloop
->time_free(c
->scache_auto_unload_event
);
199 int pa_scache_play_item(struct pa_core
*c
, const char *name
, struct pa_sink
*sink
, uint32_t volume
) {
200 struct pa_scache_entry
*e
;
201 assert(c
&& name
&& sink
);
203 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 1)))
206 if (e
->lazy
&& !e
->memchunk
.memblock
)
207 if (pa_sound_file_load(e
->filename
, &e
->sample_spec
, &e
->memchunk
, c
->memblock_stat
) < 0)
210 if (!e
->memchunk
.memblock
)
213 if (pa_play_memchunk(sink
, name
, &e
->sample_spec
, &e
->memchunk
, pa_volume_multiply(volume
, e
->volume
)) < 0)
217 time(&e
->last_used_time
);
222 const char * pa_scache_get_name_by_id(struct pa_core
*c
, uint32_t id
) {
223 struct pa_scache_entry
*e
;
224 assert(c
&& id
!= PA_IDXSET_INVALID
);
226 if (!c
->scache
|| !(e
= pa_idxset_get_by_index(c
->scache
, id
)))
232 uint32_t pa_scache_get_id_by_name(struct pa_core
*c
, const char *name
) {
233 struct pa_scache_entry
*e
;
236 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 1)))
237 return PA_IDXSET_INVALID
;
242 uint32_t pa_scache_total_size(struct pa_core
*c
) {
243 struct pa_scache_entry
*e
;
244 uint32_t index
, sum
= 0;
247 if (!c
->scache
|| !pa_idxset_ncontents(c
->scache
))
250 for (e
= pa_idxset_first(c
->scache
, &index
); e
; e
= pa_idxset_next(c
->scache
, &index
))
251 if (e
->memchunk
.memblock
)
252 sum
+= e
->memchunk
.length
;
257 void pa_scache_unload_unused(struct pa_core
*c
) {
258 struct pa_scache_entry
*e
;
263 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
)) {
270 if (!e
->lazy
|| !e
->memchunk
.memblock
)
273 if (e
->last_used_time
+ c
->scache_idle_time
> now
)
276 pa_memblock_unref(e
->memchunk
.memblock
);
277 e
->memchunk
.memblock
= NULL
;
278 e
->memchunk
.index
= e
->memchunk
.length
= 0;