]> code.delx.au - pulseaudio/blob - polyp/play-memchunk.c
add initial glib mainloop adapter
[pulseaudio] / polyp / play-memchunk.c
1 #include <stdlib.h>
2 #include <assert.h>
3
4 #include "play-memchunk.h"
5 #include "sink-input.h"
6 #include "xmalloc.h"
7
8 static void sink_input_kill(struct pa_sink_input *i) {
9 struct pa_memchunk *c;
10 assert(i && i->userdata);
11 c = i->userdata;
12
13 pa_memblock_unref(c->memblock);
14 pa_xfree(c);
15 pa_sink_input_free(i);
16 }
17
18 static int sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
19 struct pa_memchunk *c;
20 assert(i && chunk && i->userdata);
21 c = i->userdata;
22
23 if (c->length <= 0)
24 return -1;
25
26 assert(c->memblock && c->memblock->length);
27 *chunk = *c;
28 pa_memblock_ref(c->memblock);
29
30 return 0;
31 }
32
33 static void si_kill(struct pa_mainloop_api *m, void *i) {
34 sink_input_kill(i);
35 }
36
37 static void sink_input_drop(struct pa_sink_input *i, size_t length) {
38 struct pa_memchunk *c;
39 assert(i && length && i->userdata);
40 c = i->userdata;
41
42 assert(length <= c->length);
43
44 c->length -= length;
45 c->index += length;
46
47 if (c->length <= 0)
48 pa_mainloop_api_once(i->sink->core->mainloop, si_kill, i);
49 }
50
51 int pa_play_memchunk(struct pa_sink *sink, const char *name, const struct pa_sample_spec *ss, const struct pa_memchunk *chunk, uint32_t volume) {
52 struct pa_sink_input *si;
53 struct pa_memchunk *nchunk;
54
55 assert(sink && chunk);
56
57 if (volume <= 0)
58 return 0;
59
60 if (!(si = pa_sink_input_new(sink, name, ss)))
61 return -1;
62
63 si->volume = volume;
64 si->peek = sink_input_peek;
65 si->drop = sink_input_drop;
66 si->kill = sink_input_kill;
67
68 si->userdata = nchunk = pa_xmalloc(sizeof(struct pa_memchunk));
69 *nchunk = *chunk;
70
71 pa_memblock_ref(chunk->memblock);
72
73 pa_sink_notify(sink);
74
75 return 0;
76 }