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