7 #include "sink-input.h"
9 #include "sample-util.h"
10 #include "play-memchunk.h"
12 #include "subscribe.h"
14 static void free_entry(struct pa_scache_entry
*e
) {
16 pa_subscription_post(e
->core
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_REMOVE
, e
->index
);
18 if (e
->memchunk
.memblock
)
19 pa_memblock_unref(e
->memchunk
.memblock
);
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
;
28 if (c
->scache_hashmap
&& (e
= pa_hashmap_get(c
->scache_hashmap
, name
))) {
30 if (e
->memchunk
.memblock
)
31 pa_memblock_unref(e
->memchunk
.memblock
);
35 e
= pa_xmalloc(sizeof(struct pa_scache_entry
));
36 e
->name
= pa_xstrdup(name
);
45 memset(&e
->sample_spec
, 0, sizeof(struct pa_sample_spec
));
49 pa_memblock_ref(e
->memchunk
.memblock
);
51 e
->memchunk
.memblock
= NULL
;
52 e
->memchunk
.index
= e
->memchunk
.length
= 0;
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
);
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
);
66 pa_idxset_put(c
->scache_idxset
, e
, &e
->index
);
67 pa_hashmap_put(c
->scache_hashmap
, e
->name
, e
);
69 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
|PA_SUBSCRIPTION_EVENT_NEW
, e
->index
);
76 int pa_scache_remove_item(struct pa_core
*c
, const char *name
) {
77 struct pa_scache_entry
*e
;
80 if (!c
->scache_hashmap
|| !(e
= pa_hashmap_get(c
->scache_hashmap
, name
)))
83 pa_hashmap_remove(c
->scache_hashmap
, name
);
84 if (pa_idxset_remove_by_data(c
->scache_idxset
, e
, NULL
) != e
)
91 static void free_cb(void *p
, void *userdata
) {
92 struct pa_scache_entry
*e
= p
;
97 void pa_scache_free(struct pa_core
*c
) {
100 if (c
->scache_hashmap
) {
101 pa_hashmap_free(c
->scache_hashmap
, free_cb
, NULL
);
102 c
->scache_hashmap
= NULL
;
105 if (c
->scache_idxset
) {
106 pa_idxset_free(c
->scache_idxset
, NULL
, NULL
);
107 c
->scache_idxset
= NULL
;
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
);
115 if (!c
->scache_hashmap
|| !(e
= pa_hashmap_get(c
->scache_hashmap
, name
)))
118 if (!e
->memchunk
.memblock
)
121 if (pa_play_memchunk(sink
, name
, &e
->sample_spec
, &e
->memchunk
, pa_volume_multiply(volume
, e
->volume
)) < 0)
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
);
131 if (!c
->scache_idxset
|| !(e
= pa_idxset_get_by_index(c
->scache_idxset
, id
)))
138 uint32_t pa_scache_get_id_by_name(struct pa_core
*c
, const char *name
) {
139 struct pa_scache_entry
*e
;
142 if (!c
->scache_hashmap
|| !(e
= pa_hashmap_get(c
->scache_hashmap
, name
)))
143 return PA_IDXSET_INVALID
;