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"
42 #define UNLOAD_POLL_TIME 2
44 static void timeout_callback(struct pa_mainloop_api
*m
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
45 struct pa_core
*c
= userdata
;
47 assert(c
&& c
->mainloop
== m
&& c
->scache_auto_unload_event
== e
);
49 pa_scache_unload_unused(c
);
51 gettimeofday(&ntv
, NULL
);
52 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
53 m
->time_restart(e
, &ntv
);
56 static void free_entry(struct pa_scache_entry
*e
) {
58 pa_namereg_unregister(e
->core
, e
->name
);
59 pa_subscription_post(e
->core
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_REMOVE
, e
->index
);
61 pa_xfree(e
->filename
);
62 if (e
->memchunk
.memblock
)
63 pa_memblock_unref(e
->memchunk
.memblock
);
67 static struct pa_scache_entry
* scache_add_item(struct pa_core
*c
, const char *name
) {
68 struct pa_scache_entry
*e
;
71 if ((e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0))) {
72 if (e
->memchunk
.memblock
)
73 pa_memblock_unref(e
->memchunk
.memblock
);
75 pa_xfree(e
->filename
);
79 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
81 e
= pa_xmalloc(sizeof(struct pa_scache_entry
));
83 if (!pa_namereg_register(c
, name
, PA_NAMEREG_SAMPLE
, e
, 1)) {
88 e
->name
= pa_xstrdup(name
);
92 c
->scache
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
96 pa_idxset_put(c
->scache
, e
, &e
->index
);
98 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_NEW
, e
->index
);
101 e
->volume
= PA_VOLUME_NORM
;
102 e
->last_used_time
= 0;
103 e
->memchunk
.memblock
= NULL
;
104 e
->memchunk
.index
= e
->memchunk
.length
= 0;
107 e
->last_used_time
= 0;
109 memset(&e
->sample_spec
, 0, sizeof(struct pa_sample_spec
));
114 int pa_scache_add_item(struct pa_core
*c
, const char *name
, struct pa_sample_spec
*ss
, struct pa_memchunk
*chunk
, uint32_t *index
) {
115 struct pa_scache_entry
*e
;
118 if (!(e
= scache_add_item(c
, name
)))
122 e
->sample_spec
= *ss
;
125 e
->memchunk
= *chunk
;
126 pa_memblock_ref(e
->memchunk
.memblock
);
135 int pa_scache_add_file(struct pa_core
*c
, const char *name
, const char *filename
, uint32_t *index
) {
136 struct pa_sample_spec ss
;
137 struct pa_memchunk chunk
;
140 if (pa_sound_file_load(filename
, &ss
, &chunk
, c
->memblock_stat
) < 0)
143 r
= pa_scache_add_item(c
, name
, &ss
, &chunk
, index
);
144 pa_memblock_unref(chunk
.memblock
);
149 int pa_scache_add_file_lazy(struct pa_core
*c
, const char *name
, const char *filename
, uint32_t *index
) {
150 struct pa_scache_entry
*e
;
153 if (!(e
= scache_add_item(c
, name
)))
157 e
->filename
= pa_xstrdup(filename
);
159 if (!c
->scache_auto_unload_event
) {
161 gettimeofday(&ntv
, NULL
);
162 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
163 c
->scache_auto_unload_event
= c
->mainloop
->time_new(c
->mainloop
, &ntv
, timeout_callback
, c
);
169 int pa_scache_remove_item(struct pa_core
*c
, const char *name
) {
170 struct pa_scache_entry
*e
;
173 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0)))
176 if (pa_idxset_remove_by_data(c
->scache
, e
, NULL
) != e
)
183 static void free_cb(void *p
, void *userdata
) {
184 struct pa_scache_entry
*e
= p
;
189 void pa_scache_free(struct pa_core
*c
) {
193 pa_idxset_free(c
->scache
, free_cb
, NULL
);
197 if (c
->scache_auto_unload_event
)
198 c
->mainloop
->time_free(c
->scache_auto_unload_event
);
201 int pa_scache_play_item(struct pa_core
*c
, const char *name
, struct pa_sink
*sink
, uint32_t volume
) {
202 struct pa_scache_entry
*e
;
204 assert(c
&& name
&& sink
);
206 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 1)))
209 if (e
->lazy
&& !e
->memchunk
.memblock
) {
210 if (pa_sound_file_load(e
->filename
, &e
->sample_spec
, &e
->memchunk
, c
->memblock_stat
) < 0)
213 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);
216 if (!e
->memchunk
.memblock
)
219 t
= pa_sprintf_malloc("sample:%s", name
);
220 if (pa_play_memchunk(sink
, t
, &e
->sample_spec
, &e
->memchunk
, pa_volume_multiply(volume
, e
->volume
)) < 0) {
228 time(&e
->last_used_time
);
233 const char * pa_scache_get_name_by_id(struct pa_core
*c
, uint32_t id
) {
234 struct pa_scache_entry
*e
;
235 assert(c
&& id
!= PA_IDXSET_INVALID
);
237 if (!c
->scache
|| !(e
= pa_idxset_get_by_index(c
->scache
, id
)))
243 uint32_t pa_scache_get_id_by_name(struct pa_core
*c
, const char *name
) {
244 struct pa_scache_entry
*e
;
247 if (!(e
= pa_namereg_get(c
, name
, PA_NAMEREG_SAMPLE
, 0)))
248 return PA_IDXSET_INVALID
;
253 uint32_t pa_scache_total_size(struct pa_core
*c
) {
254 struct pa_scache_entry
*e
;
255 uint32_t index
, sum
= 0;
258 if (!c
->scache
|| !pa_idxset_ncontents(c
->scache
))
261 for (e
= pa_idxset_first(c
->scache
, &index
); e
; e
= pa_idxset_next(c
->scache
, &index
))
262 if (e
->memchunk
.memblock
)
263 sum
+= e
->memchunk
.length
;
268 void pa_scache_unload_unused(struct pa_core
*c
) {
269 struct pa_scache_entry
*e
;
274 if (!c
->scache
|| !pa_idxset_ncontents(c
->scache
))
279 for (e
= pa_idxset_first(c
->scache
, &index
); e
; e
= pa_idxset_next(c
->scache
, &index
)) {
281 if (!e
->lazy
|| !e
->memchunk
.memblock
)
284 if (e
->last_used_time
+ c
->scache_idle_time
> now
)
287 pa_memblock_unref(e
->memchunk
.memblock
);
288 e
->memchunk
.memblock
= NULL
;
289 e
->memchunk
.index
= e
->memchunk
.length
= 0;
291 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_CHANGE
, e
->index
);