]> code.delx.au - pulseaudio/blob - polyp/scache.c
make module-combine autoloadable
[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
40 #define UNLOAD_POLL_TIME 2
41
42 static void timeout_callback(struct pa_mainloop_api *m, struct pa_time_event*e, const struct timeval *tv, void *userdata) {
43 struct pa_core *c = userdata;
44 struct timeval ntv;
45 assert(c && c->mainloop == m && c->scache_auto_unload_event == e);
46
47 pa_scache_unload_unused(c);
48
49 gettimeofday(&ntv, NULL);
50 ntv.tv_sec += UNLOAD_POLL_TIME;
51 m->time_restart(e, &ntv);
52 }
53
54 static void free_entry(struct pa_scache_entry *e) {
55 assert(e);
56 pa_namereg_unregister(e->core, e->name);
57 pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_REMOVE, e->index);
58 pa_xfree(e->name);
59 if (e->memchunk.memblock)
60 pa_memblock_unref(e->memchunk.memblock);
61 pa_xfree(e);
62 }
63
64 int pa_scache_add_item(struct pa_core *c, const char *name, struct pa_sample_spec *ss, struct pa_memchunk *chunk, uint32_t *index, int auto_unload) {
65 struct pa_scache_entry *e;
66 int put;
67 assert(c && name);
68
69 if ((e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0))) {
70 put = 0;
71 if (e->memchunk.memblock)
72 pa_memblock_unref(e->memchunk.memblock);
73 assert(e->core == c);
74 } else {
75
76 put = 1;
77 e = pa_xmalloc(sizeof(struct pa_scache_entry));
78
79 if (!pa_namereg_register(c, name, PA_NAMEREG_SAMPLE, e, 1)) {
80 pa_xfree(e);
81 return -1;
82 }
83
84 e->name = pa_xstrdup(name);
85 e->core = c;
86 }
87
88 e->volume = PA_VOLUME_NORM;
89 e->auto_unload = auto_unload;
90 e->last_used_time = 0;
91
92 if (ss)
93 e->sample_spec = *ss;
94 else
95 memset(&e->sample_spec, 0, sizeof(struct pa_sample_spec));
96
97 if (chunk) {
98 e->memchunk = *chunk;
99 pa_memblock_ref(e->memchunk.memblock);
100 } else {
101 e->memchunk.memblock = NULL;
102 e->memchunk.index = e->memchunk.length = 0;
103 }
104
105 if (put) {
106 if (!c->scache) {
107 c->scache = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
108 assert(c->scache);
109 }
110
111 pa_idxset_put(c->scache, e, &e->index);
112
113 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_NEW, e->index);
114 } else
115 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE|PA_SUBSCRIPTION_EVENT_CHANGE, e->index);
116
117 if (index)
118 *index = e->index;
119
120 if (!c->scache_auto_unload_event) {
121 struct timeval ntv;
122 gettimeofday(&ntv, NULL);
123 ntv.tv_sec += UNLOAD_POLL_TIME;
124 c->scache_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
125 }
126
127 return 0;
128 }
129
130 int pa_scache_remove_item(struct pa_core *c, const char *name) {
131 struct pa_scache_entry *e;
132 assert(c && name);
133
134 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 0)))
135 return -1;
136
137 if (pa_idxset_remove_by_data(c->scache, e, NULL) != e)
138 assert(0);
139
140 free_entry(e);
141 return 0;
142 }
143
144 static void free_cb(void *p, void *userdata) {
145 struct pa_scache_entry *e = p;
146 assert(e);
147 free_entry(e);
148 }
149
150 void pa_scache_free(struct pa_core *c) {
151 assert(c);
152
153 if (c->scache) {
154 pa_idxset_free(c->scache, free_cb, NULL);
155 c->scache = NULL;
156 }
157
158 if (c->scache_auto_unload_event)
159 c->mainloop->time_free(c->scache_auto_unload_event);
160 }
161
162 int pa_scache_play_item(struct pa_core *c, const char *name, struct pa_sink *sink, uint32_t volume) {
163 struct pa_scache_entry *e;
164 assert(c && name && sink);
165
166 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
167 return -1;
168
169 if (!e->memchunk.memblock)
170 return -1;
171
172 if (pa_play_memchunk(sink, name, &e->sample_spec, &e->memchunk, pa_volume_multiply(volume, e->volume)) < 0)
173 return -1;
174
175 if (e->auto_unload)
176 time(&e->last_used_time);
177
178 return 0;
179 }
180
181 const char * pa_scache_get_name_by_id(struct pa_core *c, uint32_t id) {
182 struct pa_scache_entry *e;
183 assert(c && id != PA_IDXSET_INVALID);
184
185 if (!c->scache || !(e = pa_idxset_get_by_index(c->scache, id)))
186 return NULL;
187
188 return e->name;
189 }
190
191 uint32_t pa_scache_get_id_by_name(struct pa_core *c, const char *name) {
192 struct pa_scache_entry *e;
193 assert(c && name);
194
195 if (!(e = pa_namereg_get(c, name, PA_NAMEREG_SAMPLE, 1)))
196 return PA_IDXSET_INVALID;
197
198 return e->index;
199 }
200
201 uint32_t pa_scache_total_size(struct pa_core *c) {
202 struct pa_scache_entry *e;
203 uint32_t index;
204 uint32_t sum = 0;
205
206 if (!c->scache)
207 return 0;
208
209 for (e = pa_idxset_first(c->scache, &index); e; e = pa_idxset_next(c->scache, &index))
210 sum += e->memchunk.length;
211
212 return sum;
213 }
214
215 static int unload_func(void *p, uint32_t index, int *del, void *userdata) {
216 struct pa_scache_entry *e = p;
217 time_t *now = userdata;
218 assert(e);
219
220 if (!e->auto_unload)
221 return 0;
222
223 if (e->last_used_time + e->core->scache_idle_time > *now)
224 return 0;
225
226 free_entry(e);
227 *del = 1;
228 return 0;
229 }
230
231 void pa_scache_unload_unused(struct pa_core *c) {
232 time_t now;
233 assert(c);
234
235 if (!c->scache)
236 return;
237
238 time(&now);
239
240 pa_idxset_foreach(c->scache, unload_func, &now);
241 }