]> code.delx.au - pulseaudio/blob - polyp/play-memchunk.c
b94a052420b78491bc7aa5d11b8907bee80ba9a8
[pulseaudio] / polyp / play-memchunk.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <assert.h>
28
29 #include "play-memchunk.h"
30 #include "sink-input.h"
31 #include "xmalloc.h"
32
33 static void sink_input_kill(struct pa_sink_input *i) {
34 struct pa_memchunk *c;
35 assert(i && i->userdata);
36 c = i->userdata;
37
38 pa_memblock_unref(c->memblock);
39 pa_xfree(c);
40 pa_sink_input_free(i);
41 }
42
43 static int sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
44 struct pa_memchunk *c;
45 assert(i && chunk && i->userdata);
46 c = i->userdata;
47
48 if (c->length <= 0)
49 return -1;
50
51 assert(c->memblock && c->memblock->length);
52 *chunk = *c;
53 pa_memblock_ref(c->memblock);
54
55 return 0;
56 }
57
58 static void si_kill(struct pa_mainloop_api *m, void *i) {
59 sink_input_kill(i);
60 }
61
62 static void sink_input_drop(struct pa_sink_input *i, const struct pa_memchunk*chunk, size_t length) {
63 struct pa_memchunk *c;
64 assert(i && length && i->userdata);
65 c = i->userdata;
66
67 assert(chunk == c);
68 assert(length <= c->length);
69
70 c->length -= length;
71 c->index += length;
72
73 if (c->length <= 0)
74 pa_mainloop_api_once(i->sink->core->mainloop, si_kill, i);
75 }
76
77 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) {
78 struct pa_sink_input *si;
79 struct pa_memchunk *nchunk;
80
81 assert(sink && chunk);
82
83 if (volume <= 0)
84 return 0;
85
86 if (!(si = pa_sink_input_new(sink, name, ss)))
87 return -1;
88
89 si->volume = volume;
90 si->peek = sink_input_peek;
91 si->drop = sink_input_drop;
92 si->kill = sink_input_kill;
93
94 si->userdata = nchunk = pa_xmalloc(sizeof(struct pa_memchunk));
95 *nchunk = *chunk;
96
97 pa_memblock_ref(chunk->memblock);
98
99 pa_sink_notify(sink);
100
101 return 0;
102 }