]> code.delx.au - pulseaudio/blob - polyp/play-memchunk.c
Make the whole stuff LGPL only
[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 Lesser 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 Lesser 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 #include <stdio.h>
29 #include <string.h>
30
31 #include "play-memchunk.h"
32 #include "sink-input.h"
33 #include "xmalloc.h"
34
35 static void sink_input_kill(struct pa_sink_input *i) {
36 struct pa_memchunk *c;
37 assert(i && i->userdata);
38 c = i->userdata;
39
40 pa_sink_input_disconnect(i);
41 pa_sink_input_unref(i);
42
43 pa_memblock_unref(c->memblock);
44 pa_xfree(c);
45
46 }
47
48 static int sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
49 struct pa_memchunk *c;
50 assert(i && chunk && i->userdata);
51 c = i->userdata;
52
53 if (c->length <= 0)
54 return -1;
55
56 assert(c->memblock && c->memblock->length);
57 *chunk = *c;
58 pa_memblock_ref(c->memblock);
59
60 return 0;
61 }
62
63 static void si_kill(struct pa_mainloop_api *m, void *i) {
64 sink_input_kill(i);
65 }
66
67 static void sink_input_drop(struct pa_sink_input *i, const struct pa_memchunk*chunk, size_t length) {
68 struct pa_memchunk *c;
69 assert(i && length && i->userdata);
70 c = i->userdata;
71
72 assert(!memcmp(chunk, c, sizeof(chunk)));
73 assert(length <= c->length);
74
75 c->length -= length;
76 c->index += length;
77
78 if (c->length <= 0)
79 pa_mainloop_api_once(i->sink->core->mainloop, si_kill, i);
80 }
81
82 int pa_play_memchunk(struct pa_sink *sink, const char *name, const struct pa_sample_spec *ss, const struct pa_memchunk *chunk, pa_volume_t volume) {
83 struct pa_sink_input *si;
84 struct pa_memchunk *nchunk;
85
86 assert(sink && chunk);
87
88 if (volume <= 0)
89 return 0;
90
91 if (!(si = pa_sink_input_new(sink, name, ss, 0, -1)))
92 return -1;
93
94 si->volume = volume;
95 si->peek = sink_input_peek;
96 si->drop = sink_input_drop;
97 si->kill = sink_input_kill;
98
99 si->userdata = nchunk = pa_xmalloc(sizeof(struct pa_memchunk));
100 *nchunk = *chunk;
101
102 pa_memblock_ref(chunk->memblock);
103
104 pa_sink_notify(sink);
105
106 return 0;
107 }