]> code.delx.au - pulseaudio/blob - src/pulsecore/play-memchunk.c
big s/polyp/pulse/g
[pulseaudio] / src / pulsecore / play-memchunk.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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 <pulse/xmalloc.h>
32
33 #include <pulsecore/sink-input.h>
34 #include <pulsecore/gccmacro.h>
35
36 #include "play-memchunk.h"
37
38 static void sink_input_kill(pa_sink_input *i) {
39 pa_memchunk *c;
40 assert(i && i->userdata);
41 c = i->userdata;
42
43 pa_sink_input_disconnect(i);
44 pa_sink_input_unref(i);
45
46 pa_memblock_unref(c->memblock);
47 pa_xfree(c);
48 }
49
50 static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
51 pa_memchunk *c;
52 assert(i && chunk && i->userdata);
53 c = i->userdata;
54
55 if (c->length <= 0)
56 return -1;
57
58 assert(c->memblock && c->memblock->length);
59 *chunk = *c;
60 pa_memblock_ref(c->memblock);
61
62 return 0;
63 }
64
65 static void si_kill(PA_GCC_UNUSED pa_mainloop_api *m, void *i) {
66 sink_input_kill(i);
67 }
68
69 static void sink_input_drop(pa_sink_input *i, const pa_memchunk*chunk, size_t length) {
70 pa_memchunk *c;
71 assert(i && length && i->userdata);
72 c = i->userdata;
73
74 assert(!memcmp(chunk, c, sizeof(chunk)));
75 assert(length <= c->length);
76
77 c->length -= length;
78 c->index += length;
79
80 if (c->length <= 0)
81 pa_mainloop_api_once(i->sink->core->mainloop, si_kill, i);
82 }
83
84 int pa_play_memchunk(
85 pa_sink *sink,
86 const char *name,
87 const pa_sample_spec *ss,
88 const pa_channel_map *map,
89 const pa_memchunk *chunk,
90 pa_cvolume *cvolume) {
91
92 pa_sink_input *si;
93 pa_memchunk *nchunk;
94
95 assert(sink);
96 assert(ss);
97 assert(chunk);
98
99 if (cvolume && pa_cvolume_is_muted(cvolume))
100 return 0;
101
102 if (!(si = pa_sink_input_new(sink, name, __FILE__, ss, map, cvolume, 0, PA_RESAMPLER_INVALID)))
103 return -1;
104
105 si->peek = sink_input_peek;
106 si->drop = sink_input_drop;
107 si->kill = sink_input_kill;
108
109 si->userdata = nchunk = pa_xnew(pa_memchunk, 1);
110 *nchunk = *chunk;
111
112 pa_memblock_ref(chunk->memblock);
113
114 pa_sink_notify(sink);
115
116 return 0;
117 }