]> code.delx.au - pulseaudio/blob - polyp/scache.c
add new module "module-x11-bell"
[pulseaudio] / polyp / scache.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5
6 #include "scache.h"
7 #include "sink-input.h"
8 #include "mainloop.h"
9
10 static void free_entry(struct pa_scache_entry *e) {
11 assert(e);
12 free(e->name);
13 if (e->memchunk.memblock)
14 pa_memblock_unref(e->memchunk.memblock);
15 free(e);
16 }
17
18 void pa_scache_add_item(struct pa_core *c, const char *name, struct pa_sample_spec *ss, struct pa_memchunk *chunk, uint32_t *index) {
19 struct pa_scache_entry *e;
20 int put;
21 assert(c && name);
22
23 if (c->scache_hashmap && (e = pa_hashmap_get(c->scache_hashmap, name))) {
24 put = 0;
25 if (e->memchunk.memblock)
26 pa_memblock_unref(e->memchunk.memblock);
27 } else {
28 put = 1;
29 e = malloc(sizeof(struct pa_scache_entry));
30 assert(e);
31 e->name = strdup(name);
32 assert(e->name);
33 }
34
35 if (ss)
36 e->sample_spec = *ss;
37 else
38 memset(&e->sample_spec, 0, sizeof(struct pa_sample_spec));
39
40 if (chunk) {
41 e->memchunk = *chunk;
42 pa_memblock_ref(e->memchunk.memblock);
43 } else {
44 e->memchunk.memblock = NULL;
45 e->memchunk.index = e->memchunk.length = 0;
46 }
47
48 if (put) {
49 if (!c->scache_hashmap) {
50 c->scache_hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
51 assert(c->scache_hashmap);
52 }
53
54 if (!c->scache_idxset) {
55 c->scache_idxset = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
56 assert(c->scache_idxset);
57 }
58
59 pa_idxset_put(c->scache_idxset, e, &e->index);
60 pa_hashmap_put(c->scache_hashmap, e->name, e);
61 }
62
63 if (index)
64 *index = e->index;
65 }
66
67 int pa_scache_remove_item(struct pa_core *c, const char *name) {
68 struct pa_scache_entry *e;
69 assert(c && name);
70
71 if (!c->scache_hashmap || !(e = pa_hashmap_get(c->scache_hashmap, name)))
72 return -1;
73
74 pa_hashmap_remove(c->scache_hashmap, name);
75 if (pa_idxset_remove_by_data(c->scache_idxset, e, NULL) != e)
76 assert(0);
77 free_entry(e);
78 return 0;
79 }
80
81 static void free_cb(void *p, void *userdata) {
82 struct pa_scache_entry *e = p;
83 assert(e);
84 free_entry(e);
85 }
86
87 void pa_scache_free(struct pa_core *c) {
88 assert(c);
89
90 if (c->scache_hashmap) {
91 pa_hashmap_free(c->scache_hashmap, free_cb, NULL);
92 c->scache_hashmap = NULL;
93 }
94
95 if (c->scache_idxset) {
96 pa_idxset_free(c->scache_idxset, NULL, NULL);
97 c->scache_idxset = NULL;
98 }
99 }
100
101 static void sink_input_kill(struct pa_sink_input *i) {
102 struct pa_memchunk *c;
103 assert(i && i->userdata);
104 c = i->userdata;
105
106 pa_memblock_unref(c->memblock);
107 free(c);
108 pa_sink_input_free(i);
109 }
110
111 static int sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
112 struct pa_memchunk *c;
113 assert(i && chunk && i->userdata);
114 c = i->userdata;
115
116 assert(c->length && c->memblock && c->memblock->length);
117 *chunk = *c;
118 pa_memblock_ref(c->memblock);
119
120 return 0;
121 }
122
123 static void si_kill(void *i) {
124 sink_input_kill(i);
125 }
126
127 static void sink_input_drop(struct pa_sink_input *i, size_t length) {
128 struct pa_memchunk *c;
129 assert(i && length && i->userdata);
130 c = i->userdata;
131
132 assert(length <= c->length);
133
134 c->length -= length;
135 c->index += length;
136
137 if (c->length <= 0)
138 pa_mainloop_api_once(i->sink->core->mainloop, si_kill, i);
139 }
140
141 int pa_scache_play_item(struct pa_core *c, const char *name, struct pa_sink *sink, uint32_t volume) {
142 struct pa_sink_input *si;
143 struct pa_scache_entry *e;
144 struct pa_memchunk *chunk;
145 assert(c && name && sink);
146
147 if (!c->scache_hashmap || !(e = pa_hashmap_get(c->scache_hashmap, name)))
148 return -1;
149
150 if (!e->memchunk.memblock)
151 return -1;
152
153 if (!(si = pa_sink_input_new(sink, name, &e->sample_spec)))
154 return -1;
155
156 si->volume = volume;
157
158 si->peek = sink_input_peek;
159 si->drop = sink_input_drop;
160 si->kill = sink_input_kill;
161 si->userdata = chunk = malloc(sizeof(struct pa_memchunk));
162 assert(chunk);
163 *chunk = e->memchunk;
164 pa_memblock_ref(chunk->memblock);
165
166 return 0;
167 }
168
169 const char * pa_scache_get_name_by_id(struct pa_core *c, uint32_t id) {
170 struct pa_scache_entry *e;
171 assert(c && id != PA_IDXSET_INVALID);
172
173 if (!c->scache_idxset || !(e = pa_idxset_get_by_index(c->scache_idxset, id)))
174 return NULL;
175
176 return e->name;
177
178 }
179
180 uint32_t pa_scache_get_id_by_name(struct pa_core *c, const char *name) {
181 struct pa_scache_entry *e;
182 assert(c && name);
183
184 if (!c->scache_hashmap || !(e = pa_hashmap_get(c->scache_hashmap, name)))
185 return PA_IDXSET_INVALID;
186
187 return e->index;
188 }