]> code.delx.au - pulseaudio/blob - polyp/sink.c
Documentation work
[pulseaudio] / polyp / sink.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 #include <string.h>
29 #include <stdio.h>
30
31 #include "sink.h"
32 #include "sink-input.h"
33 #include "namereg.h"
34 #include "util.h"
35 #include "sample-util.h"
36 #include "xmalloc.h"
37 #include "subscribe.h"
38
39 #define MAX_MIX_CHANNELS 32
40
41 struct pa_sink* pa_sink_new(struct pa_core *core, const char *name, int fail, const struct pa_sample_spec *spec) {
42 struct pa_sink *s;
43 char *n = NULL;
44 char st[256];
45 int r;
46 assert(core && name && *name && spec);
47
48 s = pa_xmalloc(sizeof(struct pa_sink));
49
50 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SINK, s, fail))) {
51 pa_xfree(s);
52 return NULL;
53 }
54
55 s->name = pa_xstrdup(name);
56 s->description = NULL;
57
58 s->owner = NULL;
59 s->core = core;
60 s->sample_spec = *spec;
61 s->inputs = pa_idxset_new(NULL, NULL);
62
63 n = pa_sprintf_malloc("%s_monitor", name);
64 s->monitor_source = pa_source_new(core, n, 0, spec);
65 assert(s->monitor_source);
66 pa_xfree(n);
67 s->monitor_source->monitor_of = s;
68 s->monitor_source->description = pa_sprintf_malloc("Monitor source of sink '%s'", s->name);
69
70 s->volume = PA_VOLUME_NORM;
71
72 s->notify = NULL;
73 s->get_latency = NULL;
74 s->userdata = NULL;
75
76 r = pa_idxset_put(core->sinks, s, &s->index);
77 assert(s->index != PA_IDXSET_INVALID && r >= 0);
78
79 pa_sample_snprint(st, sizeof(st), spec);
80 fprintf(stderr, "sink: created %u \"%s\" with sample spec \"%s\"\n", s->index, s->name, st);
81
82 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
83
84 return s;
85 }
86
87 void pa_sink_free(struct pa_sink *s) {
88 struct pa_sink_input *i, *j = NULL;
89 assert(s);
90
91 pa_namereg_unregister(s->core, s->name);
92
93 while ((i = pa_idxset_first(s->inputs, NULL))) {
94 assert(i != j);
95 pa_sink_input_kill(i);
96 j = i;
97 }
98 pa_idxset_free(s->inputs, NULL, NULL);
99
100 pa_source_free(s->monitor_source);
101 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
102
103 fprintf(stderr, "sink: freed %u \"%s\"\n", s->index, s->name);
104
105 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
106
107 pa_xfree(s->name);
108 pa_xfree(s->description);
109 pa_xfree(s);
110 }
111
112 void pa_sink_notify(struct pa_sink*s) {
113 assert(s);
114
115 if (s->notify)
116 s->notify(s);
117 }
118
119 static unsigned fill_mix_info(struct pa_sink *s, struct pa_mix_info *info, unsigned maxinfo) {
120 uint32_t index = PA_IDXSET_INVALID;
121 struct pa_sink_input *i;
122 unsigned n = 0;
123
124 assert(s && info);
125
126 for (i = pa_idxset_first(s->inputs, &index); maxinfo > 0 && i; i = pa_idxset_next(s->inputs, &index)) {
127 if (pa_sink_input_peek(i, &info->chunk) < 0)
128 continue;
129
130 info->volume = i->volume;
131
132 assert(info->chunk.memblock && info->chunk.memblock->data && info->chunk.length);
133 info->userdata = i;
134
135 info++;
136 maxinfo--;
137 n++;
138 }
139
140 return n;
141 }
142
143 static void inputs_drop(struct pa_sink *s, struct pa_mix_info *info, unsigned maxinfo, size_t length) {
144 assert(s && info);
145
146 for (; maxinfo > 0; maxinfo--, info++) {
147 struct pa_sink_input *i = info->userdata;
148 assert(i && info->chunk.memblock);
149
150 pa_memblock_unref(info->chunk.memblock);
151 pa_sink_input_drop(i, length);
152 }
153 }
154
155 int pa_sink_render(struct pa_sink*s, size_t length, struct pa_memchunk *result) {
156 struct pa_mix_info info[MAX_MIX_CHANNELS];
157 unsigned n;
158 size_t l;
159 assert(s && length && result);
160
161 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
162
163 if (n <= 0)
164 return -1;
165
166 if (n == 1) {
167 uint32_t volume = PA_VOLUME_NORM;
168 struct pa_sink_input *i = info[0].userdata;
169 assert(i);
170 *result = info[0].chunk;
171 pa_memblock_ref(result->memblock);
172
173 if (result->length > length)
174 result->length = length;
175
176 l = result->length;
177
178 if (s->volume != PA_VOLUME_NORM || info[0].volume != PA_VOLUME_NORM)
179 volume = pa_volume_multiply(s->volume, info[0].volume);
180
181 if (volume != PA_VOLUME_NORM) {
182 pa_memchunk_make_writable(result);
183 pa_volume_memchunk(result, &s->sample_spec, volume);
184 }
185 } else {
186 result->memblock = pa_memblock_new(length);
187 assert(result->memblock);
188
189 result->length = l = pa_mix(info, n, result->memblock->data, length, &s->sample_spec, s->volume);
190 result->index = 0;
191
192 assert(l);
193 }
194
195 inputs_drop(s, info, n, l);
196
197 assert(s->monitor_source);
198 pa_source_post(s->monitor_source, result);
199
200 return 0;
201 }
202
203 int pa_sink_render_into(struct pa_sink*s, struct pa_memchunk *target) {
204 struct pa_mix_info info[MAX_MIX_CHANNELS];
205 unsigned n;
206 size_t l;
207 assert(s && target && target->length && target->memblock && target->memblock->data);
208
209 n = fill_mix_info(s, info, MAX_MIX_CHANNELS);
210
211 if (n <= 0)
212 return -1;
213
214 if (n == 1) {
215 uint32_t volume = PA_VOLUME_NORM;
216 struct pa_sink_info *i = info[0].userdata;
217 assert(i);
218
219 l = target->length;
220 if (l > info[0].chunk.length)
221 l = info[0].chunk.length;
222
223 memcpy(target->memblock->data+target->index, info[0].chunk.memblock->data + info[0].chunk.index, l);
224 target->length = l;
225
226 if (s->volume != PA_VOLUME_NORM || info[0].volume != PA_VOLUME_NORM)
227 volume = pa_volume_multiply(s->volume, info[0].volume);
228
229 if (volume != PA_VOLUME_NORM)
230 pa_volume_memchunk(target, &s->sample_spec, volume);
231 } else
232 target->length = l = pa_mix(info, n, target->memblock->data+target->index, target->length, &s->sample_spec, s->volume);
233
234 assert(l);
235 inputs_drop(s, info, n, l);
236
237 assert(s->monitor_source);
238 pa_source_post(s->monitor_source, target);
239
240 return 0;
241 }
242
243 void pa_sink_render_into_full(struct pa_sink *s, struct pa_memchunk *target) {
244 struct pa_memchunk chunk;
245 size_t l, d;
246 assert(s && target && target->memblock && target->length && target->memblock->data);
247
248 l = target->length;
249 d = 0;
250 while (l > 0) {
251 chunk = *target;
252 chunk.index += d;
253 chunk.length -= d;
254
255 if (pa_sink_render_into(s, &chunk) < 0)
256 break;
257
258 d += chunk.length;
259 l -= chunk.length;
260 }
261
262 if (l > 0) {
263 chunk = *target;
264 chunk.index += d;
265 chunk.length -= d;
266 pa_silence_memchunk(&chunk, &s->sample_spec);
267 }
268 }
269
270 uint32_t pa_sink_get_latency(struct pa_sink *s) {
271 assert(s);
272
273 if (!s->get_latency)
274 return 0;
275
276 return s->get_latency(s);
277 }
278
279 void pa_sink_set_owner(struct pa_sink *sink, struct pa_module *m) {
280 sink->owner = m;
281
282 if (sink->monitor_source)
283 pa_source_set_owner(sink->monitor_source, m);
284 }
285
286 void pa_sink_set_volume(struct pa_sink *sink, pa_volume_t volume) {
287 assert(sink);
288
289 if (sink->volume != volume) {
290 sink->volume = volume;
291 pa_subscription_post(sink->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, sink->index);
292 }
293 }