]> code.delx.au - pulseaudio/blob - polyp/scache.c
remove auto-load-sample stuff
[pulseaudio] / polyp / scache.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30
31 #include "scache.h"
32 #include "sink-input.h"
33 #include "mainloop.h"
34 #include "sample-util.h"
35 #include "play-memchunk.h"
36 #include "xmalloc.h"
37 #include "subscribe.h"
38 #include "namereg.h"
39 #include "sound-file.h"
40
41 #define UNLOAD_POLL_TIME 2
42
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;
45 struct timeval ntv;
46 assert(c && c->mainloop == m && c->scache_auto_unload_event == e);
47
48 pa_scache_unload_unused(c);
49
50 gettimeofday(&ntv, NULL);
51 ntv.tv_sec += UNLOAD_POLL_TIME;
52 m->time_restart(e, &ntv);
53 }
54
55 static void free_entry(struct pa_scache_entry *e) {
56 assert(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);
59 pa_xfree(e->name);
60 pa_xfree(e->filename);
61 if (e->memchunk.memblock)
62 pa_memblock_unref(e->memchunk.memblock);
63 pa_xfree(e);
64 }
65
66 static struct pa_scache_entry* scache_add_item(struct pa_core *c, const char *name) {
67 struct pa_scache_entry *e;
68 assert(c && name);
69
70 if ((e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0))) {
71 if (e->memchunk.memblock)
72 pa_memblock_unref(e->memchunk.memblock);
73
74 pa_xfree(e->filename);
75
76 assert(e->core == c);
77
78 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
79 } else {
80 e = pa_xmalloc(sizeof(struct pa_scache_entry));
81
82 if (!pa_namereg_register(c, name, PA_NAMEREG_SAMPLE, e, 1)) {
83 pa_xfree(e);
84 return NULL;
85 }
86
87 e->name = pa_xstrdup(name);
88 e->core = c;
89
90 if (!c->scache) {
91 c->scache = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
92 assert(c->scache);
93 }
94
95 pa_idxset_put(c->scache, e, &e->index);
96
97 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_NEW, e->index);
98 }
99
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;
104 e->filename = NULL;
105 e->lazy = 0;
106 e->last_used_time = 0;
107
108 memset(&e->sample_spec, 0, sizeof(struct pa_sample_spec));
109
110 return e;
111 }
112
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;
115 assert(c && name);
116
117 if (!(e = scache_add_item(c, name)))
118 return -1;
119
120 if (ss)
121 e->sample_spec = *ss;
122
123 if (chunk) {
124 e->memchunk = *chunk;
125 pa_memblock_ref(e->memchunk.memblock);
126 }
127
128 if (index)
129 *index = e->index;
130
131 return 0;
132 }
133
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;
137 int r;
138
139 if (pa_sound_file_load(filename, &ss, &chunk, c->memblock_stat) < 0)
140 return -1;
141
142 r = pa_scache_add_item(c, name, &ss, &chunk, index);
143 pa_memblock_unref(chunk.memblock);
144 return r;
145 }
146
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;
149 assert(c && name);
150
151 if (!(e = scache_add_item(c, name)))
152 return -1;
153
154 e->lazy = 1;
155 e->filename = pa_xstrdup(filename);
156
157 if (!c->scache_auto_unload_event) {
158 struct timeval ntv;
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);
162 }
163
164 return 0;
165 }
166
167 int pa_scache_remove_item(struct pa_core *c, const char *name) {
168 struct pa_scache_entry *e;
169 assert(c && name);
170
171 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
172 return -1;
173
174 if (pa_idxset_remove_by_data(c->scache, e, NULL) != e)
175 assert(0);
176
177 free_entry(e);
178 return 0;
179 }
180
181 static void free_cb(void *p, void *userdata) {
182 struct pa_scache_entry *e = p;
183 assert(e);
184 free_entry(e);
185 }
186
187 void pa_scache_free(struct pa_core *c) {
188 assert(c);
189
190 if (c->scache) {
191 pa_idxset_free(c->scache, free_cb, NULL);
192 c->scache = NULL;
193 }
194
195 if (c->scache_auto_unload_event)
196 c->mainloop->time_free(c->scache_auto_unload_event);
197 }
198
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);
202
203 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
204 return -1;
205
206 if (e->lazy && !e->memchunk.memblock)
207 if (pa_sound_file_load(e->filename, &e->sample_spec, &e->memchunk, c->memblock_stat) < 0)
208 return -1;
209
210 if (!e->memchunk.memblock)
211 return -1;
212
213 if (pa_play_memchunk(sink, name, &e->sample_spec, &e->memchunk, pa_volume_multiply(volume, e->volume)) < 0)
214 return -1;
215
216 if (e->lazy)
217 time(&e->last_used_time);
218
219 return 0;
220 }
221
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);
225
226 if (!c->scache || !(e = pa_idxset_get_by_index(c->scache, id)))
227 return NULL;
228
229 return e->name;
230 }
231
232 uint32_t pa_scache_get_id_by_name(struct pa_core *c, const char *name) {
233 struct pa_scache_entry *e;
234 assert(c && name);
235
236 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
237 return PA_IDXSET_INVALID;
238
239 return e->index;
240 }
241
242 uint32_t pa_scache_total_size(struct pa_core *c) {
243 struct pa_scache_entry *e;
244 uint32_t index, sum = 0;
245 assert(c);
246
247 if (!c->scache || !pa_idxset_ncontents(c->scache))
248 return 0;
249
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;
253
254 return sum;
255 }
256
257 void pa_scache_unload_unused(struct pa_core *c) {
258 struct pa_scache_entry *e;
259 time_t now;
260 uint32_t index;
261 assert(c);
262
263 if (!c->scache || !pa_idxset_ncontents(c->scache))
264 return;
265
266 time(&now);
267
268 for (e = pa_idxset_first(c->scache, &index); e; e = pa_idxset_next(c->scache, &index)) {
269
270 if (!e->lazy || !e->memchunk.memblock)
271 continue;
272
273 if (e->last_used_time + c->scache_idle_time > now)
274 continue;
275
276 pa_memblock_unref(e->memchunk.memblock);
277 e->memchunk.memblock = NULL;
278 e->memchunk.index = e->memchunk.length = 0;
279 }
280 }