]> code.delx.au - pulseaudio/blob - src/polypcore/sink-input.c
Cleaned up the includes after the restructuring. Indicate which headers are
[pulseaudio] / src / polypcore / sink-input.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 <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <polypcore/sample-util.h>
32 #include <polypcore/xmalloc.h>
33 #include <polypcore/core-subscribe.h>
34 #include <polypcore/log.h>
35
36 #include "sink-input.h"
37
38 #define CONVERT_BUFFER_LENGTH 4096
39
40 pa_sink_input* pa_sink_input_new(
41 pa_sink *s,
42 const char *driver,
43 const char *name,
44 const pa_sample_spec *spec,
45 const pa_channel_map *map,
46 int variable_rate,
47 int resample_method) {
48
49 pa_sink_input *i;
50 pa_resampler *resampler = NULL;
51 int r;
52 char st[256];
53 pa_channel_map tmap;
54
55 assert(s);
56 assert(spec);
57 assert(s->state == PA_SINK_RUNNING);
58
59 if (pa_idxset_size(s->inputs) >= PA_MAX_INPUTS_PER_SINK) {
60 pa_log_warn(__FILE__": Failed to create sink input: too many inputs per sink.\n");
61 return NULL;
62 }
63
64 if (resample_method == PA_RESAMPLER_INVALID)
65 resample_method = s->core->resample_method;
66
67 if (!map) {
68 pa_channel_map_init_auto(&tmap, spec->channels);
69 map = &tmap;
70 }
71
72 if (variable_rate || !pa_sample_spec_equal(spec, &s->sample_spec) || !pa_channel_map_equal(map, &s->channel_map))
73 if (!(resampler = pa_resampler_new(spec, map, &s->sample_spec, &s->channel_map, s->core->memblock_stat, resample_method)))
74 return NULL;
75
76 i = pa_xnew(pa_sink_input, 1);
77 i->ref = 1;
78 i->state = PA_SINK_INPUT_RUNNING;
79 i->name = pa_xstrdup(name);
80 i->driver = pa_xstrdup(driver);
81 i->owner = NULL;
82 i->sink = s;
83 i->client = NULL;
84
85 i->sample_spec = *spec;
86 i->channel_map = *map;
87
88 pa_cvolume_reset(&i->volume, spec->channels);
89
90 i->peek = NULL;
91 i->drop = NULL;
92 i->kill = NULL;
93 i->get_latency = NULL;
94 i->underrun = NULL;
95 i->userdata = NULL;
96
97 i->playing = 0;
98
99 pa_memchunk_reset(&i->resampled_chunk);
100 i->resampler = resampler;
101
102 assert(s->core);
103 r = pa_idxset_put(s->core->sink_inputs, i, &i->index);
104 assert(r == 0 && i->index != PA_IDXSET_INVALID);
105 r = pa_idxset_put(s->inputs, i, NULL);
106 assert(r == 0);
107
108 pa_sample_spec_snprint(st, sizeof(st), spec);
109 pa_log_info(__FILE__": created %u \"%s\" on %u with sample spec \"%s\"\n", i->index, i->name, s->index, st);
110
111 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
112
113 return i;
114 }
115
116 void pa_sink_input_disconnect(pa_sink_input *i) {
117 assert(i);
118 assert(i->state != PA_SINK_INPUT_DISCONNECTED);
119 assert(i->sink);
120 assert(i->sink->core);
121
122 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
123 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
124
125 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
126 i->sink = NULL;
127
128 i->peek = NULL;
129 i->drop = NULL;
130 i->kill = NULL;
131 i->get_latency = NULL;
132 i->underrun = NULL;
133
134 i->playing = 0;
135 i->state = PA_SINK_INPUT_DISCONNECTED;
136 }
137
138 static void sink_input_free(pa_sink_input* i) {
139 assert(i);
140
141 if (i->state != PA_SINK_INPUT_DISCONNECTED)
142 pa_sink_input_disconnect(i);
143
144 pa_log_info(__FILE__": freed %u \"%s\"\n", i->index, i->name);
145
146 if (i->resampled_chunk.memblock)
147 pa_memblock_unref(i->resampled_chunk.memblock);
148
149 if (i->resampler)
150 pa_resampler_free(i->resampler);
151
152 pa_xfree(i->name);
153 pa_xfree(i->driver);
154 pa_xfree(i);
155 }
156
157 void pa_sink_input_unref(pa_sink_input *i) {
158 assert(i);
159 assert(i->ref >= 1);
160
161 if (!(--i->ref))
162 sink_input_free(i);
163 }
164
165 pa_sink_input* pa_sink_input_ref(pa_sink_input *i) {
166 assert(i);
167 assert(i->ref >= 1);
168
169 i->ref++;
170 return i;
171 }
172
173 void pa_sink_input_kill(pa_sink_input*i) {
174 assert(i);
175 assert(i->ref >= 1);
176
177 if (i->kill)
178 i->kill(i);
179 }
180
181 pa_usec_t pa_sink_input_get_latency(pa_sink_input *i) {
182 pa_usec_t r = 0;
183
184 assert(i);
185 assert(i->ref >= 1);
186
187 if (i->get_latency)
188 r += i->get_latency(i);
189
190 if (i->resampled_chunk.memblock)
191 r += pa_bytes_to_usec(i->resampled_chunk.length, &i->sample_spec);
192
193 return r;
194 }
195
196 int pa_sink_input_peek(pa_sink_input *i, pa_memchunk *chunk, pa_cvolume *volume) {
197 int ret = -1;
198 int do_volume_adj_here;
199
200 assert(i);
201 assert(i->ref >= 1);
202 assert(chunk);
203 assert(volume);
204
205 pa_sink_input_ref(i);
206
207 if (!i->peek || !i->drop || i->state == PA_SINK_INPUT_CORKED)
208 goto finish;
209
210 if (!i->resampler) {
211 do_volume_adj_here = 0;
212 ret = i->peek(i, chunk);
213 goto finish;
214 }
215
216 do_volume_adj_here = !pa_channel_map_equal(&i->channel_map, &i->sink->channel_map);
217
218 while (!i->resampled_chunk.memblock) {
219 pa_memchunk tchunk;
220 size_t l;
221
222 if ((ret = i->peek(i, &tchunk)) < 0)
223 goto finish;
224
225 assert(tchunk.length);
226
227 l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
228
229 if (l > tchunk.length)
230 l = tchunk.length;
231
232 i->drop(i, &tchunk, l);
233 tchunk.length = l;
234
235 /* It might be necessary to adjust the volume here */
236 if (do_volume_adj_here) {
237 pa_memchunk_make_writable(&tchunk, i->sink->core->memblock_stat, 0);
238 pa_volume_memchunk(&tchunk, &i->sample_spec, &i->volume);
239 }
240
241 pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
242 pa_memblock_unref(tchunk.memblock);
243 }
244
245 assert(i->resampled_chunk.memblock);
246 assert(i->resampled_chunk.length);
247
248 *chunk = i->resampled_chunk;
249 pa_memblock_ref(i->resampled_chunk.memblock);
250
251 ret = 0;
252
253 finish:
254
255 if (ret < 0 && i->playing && i->underrun)
256 i->underrun(i);
257
258 i->playing = ret >= 0;
259
260 if (ret >= 0) {
261 /* Let's see if we had to apply the volume adjustment
262 * ourselves, or if this can be done by the sink for us */
263
264 if (do_volume_adj_here)
265 /* We've both the same channel map, so let's have the sink do the adjustment for us*/
266
267 pa_cvolume_reset(volume, i->sample_spec.channels);
268 else
269 /* We had different channel maps, so we already did the adjustment */
270 *volume = i->volume;
271 }
272
273 pa_sink_input_unref(i);
274
275 return ret;
276 }
277
278 void pa_sink_input_drop(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
279 assert(i);
280 assert(i->ref >= 1);
281 assert(length > 0);
282
283 if (!i->resampler) {
284 if (i->drop)
285 i->drop(i, chunk, length);
286 return;
287 }
288
289 assert(i->resampled_chunk.memblock);
290 assert(i->resampled_chunk.length >= length);
291
292 i->resampled_chunk.index += length;
293 i->resampled_chunk.length -= length;
294
295 if (i->resampled_chunk.length <= 0) {
296 pa_memblock_unref(i->resampled_chunk.memblock);
297 i->resampled_chunk.memblock = NULL;
298 i->resampled_chunk.index = i->resampled_chunk.length = 0;
299 }
300 }
301
302 void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume) {
303 assert(i);
304 assert(i->ref >= 1);
305 assert(i->sink);
306 assert(i->sink->core);
307
308 if (pa_cvolume_equal(&i->volume, volume))
309 return;
310
311 i->volume = *volume;
312 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
313 }
314
315 const pa_cvolume * pa_sink_input_get_volume(pa_sink_input *i) {
316 assert(i);
317 assert(i->ref >= 1);
318
319 return &i->volume;
320 }
321
322 void pa_sink_input_cork(pa_sink_input *i, int b) {
323 int n;
324
325 assert(i);
326 assert(i->ref >= 1);
327
328 if (i->state == PA_SINK_INPUT_DISCONNECTED)
329 return;
330
331 n = i->state == PA_SINK_INPUT_CORKED && !b;
332
333 i->state = b ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING;
334
335 if (n)
336 pa_sink_notify(i->sink);
337 }
338
339 void pa_sink_input_set_rate(pa_sink_input *i, uint32_t rate) {
340 assert(i);
341 assert(i->resampler);
342 assert(i->ref >= 1);
343
344 if (i->sample_spec.rate == rate)
345 return;
346
347 i->sample_spec.rate = rate;
348 pa_resampler_set_input_rate(i->resampler, rate);
349 }
350
351 void pa_sink_input_set_name(pa_sink_input *i, const char *name) {
352 assert(i);
353 assert(i->ref >= 1);
354
355 pa_xfree(i->name);
356 i->name = pa_xstrdup(name);
357
358 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
359 }
360
361 pa_resample_method_t pa_sink_input_get_resample_method(pa_sink_input *i) {
362 assert(i);
363 assert(i->ref >= 1);
364
365 if (!i->resampler)
366 return PA_RESAMPLER_INVALID;
367
368 return pa_resampler_get_method(i->resampler);
369 }