]> code.delx.au - pulseaudio/blob - polyp/scache.c
introduce pa_xmalloc() and friends
[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 #include "sample-util.h"
10 #include "play-memchunk.h"
11 #include "xmalloc.h"
12
13 static void free_entry(struct pa_scache_entry *e) {
14 assert(e);
15 pa_xfree(e->name);
16 if (e->memchunk.memblock)
17 pa_memblock_unref(e->memchunk.memblock);
18 pa_xfree(e);
19 }
20
21 void pa_scache_add_item(struct pa_core *c, const char *name, struct pa_sample_spec *ss, struct pa_memchunk *chunk, uint32_t *index) {
22 struct pa_scache_entry *e;
23 int put;
24 assert(c && name);
25
26 if (c->scache_hashmap && (e = pa_hashmap_get(c->scache_hashmap, name))) {
27 put = 0;
28 if (e->memchunk.memblock)
29 pa_memblock_unref(e->memchunk.memblock);
30 } else {
31 put = 1;
32 e = pa_xmalloc(sizeof(struct pa_scache_entry));
33 e->name = pa_xstrdup(name);
34 }
35
36 e->volume = 0x100;
37
38 if (ss)
39 e->sample_spec = *ss;
40 else
41 memset(&e->sample_spec, 0, sizeof(struct pa_sample_spec));
42
43 if (chunk) {
44 e->memchunk = *chunk;
45 pa_memblock_ref(e->memchunk.memblock);
46 } else {
47 e->memchunk.memblock = NULL;
48 e->memchunk.index = e->memchunk.length = 0;
49 }
50
51 if (put) {
52 if (!c->scache_hashmap) {
53 c->scache_hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
54 assert(c->scache_hashmap);
55 }
56
57 if (!c->scache_idxset) {
58 c->scache_idxset = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
59 assert(c->scache_idxset);
60 }
61
62 pa_idxset_put(c->scache_idxset, e, &e->index);
63 pa_hashmap_put(c->scache_hashmap, e->name, e);
64 }
65
66 if (index)
67 *index = e->index;
68 }
69
70 int pa_scache_remove_item(struct pa_core *c, const char *name) {
71 struct pa_scache_entry *e;
72 assert(c && name);
73
74 if (!c->scache_hashmap || !(e = pa_hashmap_get(c->scache_hashmap, name)))
75 return -1;
76
77 pa_hashmap_remove(c->scache_hashmap, name);
78 if (pa_idxset_remove_by_data(c->scache_idxset, e, NULL) != e)
79 assert(0);
80 free_entry(e);
81 return 0;
82 }
83
84 static void free_cb(void *p, void *userdata) {
85 struct pa_scache_entry *e = p;
86 assert(e);
87 free_entry(e);
88 }
89
90 void pa_scache_free(struct pa_core *c) {
91 assert(c);
92
93 if (c->scache_hashmap) {
94 pa_hashmap_free(c->scache_hashmap, free_cb, NULL);
95 c->scache_hashmap = NULL;
96 }
97
98 if (c->scache_idxset) {
99 pa_idxset_free(c->scache_idxset, NULL, NULL);
100 c->scache_idxset = NULL;
101 }
102 }
103
104 int pa_scache_play_item(struct pa_core *c, const char *name, struct pa_sink *sink, uint32_t volume) {
105 struct pa_scache_entry *e;
106 assert(c && name && sink);
107
108 if (!c->scache_hashmap || !(e = pa_hashmap_get(c->scache_hashmap, name)))
109 return -1;
110
111 if (!e->memchunk.memblock)
112 return -1;
113
114 if (pa_play_memchunk(sink, name, &e->sample_spec, &e->memchunk, pa_volume_multiply(volume, e->volume)) < 0)
115 return -1;
116
117 return 0;
118 }
119
120 const char * pa_scache_get_name_by_id(struct pa_core *c, uint32_t id) {
121 struct pa_scache_entry *e;
122 assert(c && id != PA_IDXSET_INVALID);
123
124 if (!c->scache_idxset || !(e = pa_idxset_get_by_index(c->scache_idxset, id)))
125 return NULL;
126
127 return e->name;
128
129 }
130
131 uint32_t pa_scache_get_id_by_name(struct pa_core *c, const char *name) {
132 struct pa_scache_entry *e;
133 assert(c && name);
134
135 if (!c->scache_hashmap || !(e = pa_hashmap_get(c->scache_hashmap, name)))
136 return PA_IDXSET_INVALID;
137
138 return e->index;
139 }