]> code.delx.au - pulseaudio/blob - src/pulsecore/play-memchunk.c
9132e294519ec20eb90f829289156d3b7e7bb74d
[pulseaudio] / src / pulsecore / play-memchunk.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/sink-input.h>
36 #include <pulsecore/gccmacro.h>
37
38 #include "play-memchunk.h"
39
40 static void sink_input_kill(pa_sink_input *i) {
41 pa_memchunk *c;
42 assert(i && i->userdata);
43 c = i->userdata;
44
45 pa_sink_input_disconnect(i);
46 pa_sink_input_unref(i);
47
48 pa_memblock_unref(c->memblock);
49 pa_xfree(c);
50 }
51
52 static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
53 pa_memchunk *c;
54 assert(i && chunk && i->userdata);
55 c = i->userdata;
56
57 if (c->length <= 0)
58 return -1;
59
60 assert(c->memblock && c->memblock->length);
61 *chunk = *c;
62 pa_memblock_ref(c->memblock);
63
64 return 0;
65 }
66
67 static void si_kill(PA_GCC_UNUSED pa_mainloop_api *m, void *i) {
68 sink_input_kill(i);
69 }
70
71 static void sink_input_drop(pa_sink_input *i, const pa_memchunk*chunk, size_t length) {
72 pa_memchunk *c;
73 assert(i && length && i->userdata);
74 c = i->userdata;
75
76 assert(!memcmp(chunk, c, sizeof(chunk)));
77 assert(length <= c->length);
78
79 c->length -= length;
80 c->index += length;
81
82 if (c->length <= 0)
83 pa_mainloop_api_once(i->sink->core->mainloop, si_kill, i);
84 }
85
86 int pa_play_memchunk(
87 pa_sink *sink,
88 const char *name,
89 const pa_sample_spec *ss,
90 const pa_channel_map *map,
91 const pa_memchunk *chunk,
92 pa_cvolume *volume) {
93
94 pa_sink_input *si;
95 pa_memchunk *nchunk;
96 pa_sink_input_new_data data;
97
98 assert(sink);
99 assert(ss);
100 assert(chunk);
101
102 if (volume && pa_cvolume_is_muted(volume))
103 return 0;
104
105 pa_sink_input_new_data_init(&data);
106 data.sink = sink;
107 data.name = name;
108 data.driver = __FILE__;
109 pa_sink_input_new_data_set_sample_spec(&data, ss);
110 pa_sink_input_new_data_set_channel_map(&data, map);
111 pa_sink_input_new_data_set_volume(&data, volume);
112
113 if (!(si = pa_sink_input_new(sink->core, &data, 0)))
114 return -1;
115
116 si->peek = sink_input_peek;
117 si->drop = sink_input_drop;
118 si->kill = sink_input_kill;
119
120 si->userdata = nchunk = pa_xnew(pa_memchunk, 1);
121 *nchunk = *chunk;
122
123 pa_memblock_ref(chunk->memblock);
124
125 pa_sink_notify(si->sink);
126
127 return 0;
128 }