]> code.delx.au - pulseaudio/blob - src/pulsecore/source-output.c
c7a9858c49b66283aae4414c8ab635b50686d7b2
[pulseaudio] / src / pulsecore / source-output.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 <stdio.h>
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <pulse/utf8.h>
34 #include <pulse/xmalloc.h>
35
36 #include <pulsecore/core-subscribe.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/namereg.h>
39
40 #include "source-output.h"
41
42 #define CHECK_VALIDITY_RETURN_NULL(condition) \
43 do {\
44 if (!(condition)) \
45 return NULL; \
46 } while (0)
47
48 pa_source_output_new_data* pa_source_output_new_data_init(pa_source_output_new_data *data) {
49 assert(data);
50
51 memset(data, 0, sizeof(*data));
52 data->resample_method = PA_RESAMPLER_INVALID;
53 return data;
54 }
55
56 void pa_source_output_new_data_set_channel_map(pa_source_output_new_data *data, const pa_channel_map *map) {
57 assert(data);
58
59 if ((data->channel_map_is_set = !!map))
60 data->channel_map = *map;
61 }
62
63 void pa_source_output_new_data_set_sample_spec(pa_source_output_new_data *data, const pa_sample_spec *spec) {
64 assert(data);
65
66 if ((data->sample_spec_is_set = !!spec))
67 data->sample_spec = *spec;
68 }
69
70 pa_source_output* pa_source_output_new(
71 pa_core *core,
72 pa_source_output_new_data *data,
73 pa_source_output_flags_t flags) {
74
75 pa_source_output *o;
76 pa_resampler *resampler = NULL;
77 int r;
78 char st[PA_SAMPLE_SPEC_SNPRINT_MAX];
79
80 assert(core);
81 assert(data);
82
83 if (!(flags & PA_SOURCE_OUTPUT_NO_HOOKS))
84 if (pa_hook_fire(&core->hook_source_output_new, data) < 0)
85 return NULL;
86
87 CHECK_VALIDITY_RETURN_NULL(!data->driver || pa_utf8_valid(data->driver));
88 CHECK_VALIDITY_RETURN_NULL(!data->name || pa_utf8_valid(data->name));
89
90 if (!data->source)
91 data->source = pa_namereg_get(core, NULL, PA_NAMEREG_SOURCE, 1);
92
93 CHECK_VALIDITY_RETURN_NULL(data->source);
94 CHECK_VALIDITY_RETURN_NULL(data->source->state == PA_SOURCE_RUNNING);
95
96 if (!data->sample_spec_is_set)
97 data->sample_spec = data->source->sample_spec;
98
99 CHECK_VALIDITY_RETURN_NULL(pa_sample_spec_valid(&data->sample_spec));
100
101 if (!data->channel_map_is_set)
102 pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
103
104 CHECK_VALIDITY_RETURN_NULL(pa_channel_map_valid(&data->channel_map));
105 CHECK_VALIDITY_RETURN_NULL(data->channel_map.channels == data->sample_spec.channels);
106
107 if (data->resample_method == PA_RESAMPLER_INVALID)
108 data->resample_method = core->resample_method;
109
110 CHECK_VALIDITY_RETURN_NULL(data->resample_method < PA_RESAMPLER_MAX);
111
112 if (pa_idxset_size(data->source->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
113 pa_log("Failed to create source output: too many outputs per source.");
114 return NULL;
115 }
116
117 if (!pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec) ||
118 !pa_channel_map_equal(&data->channel_map, &data->source->channel_map))
119 if (!(resampler = pa_resampler_new(
120 core->mempool,
121 &data->source->sample_spec, &data->source->channel_map,
122 &data->sample_spec, &data->channel_map,
123 data->resample_method))) {
124 pa_log_warn("Unsupported resampling operation.");
125 return NULL;
126 }
127
128 o = pa_xnew(pa_source_output, 1);
129 o->ref = 1;
130 o->state = PA_SOURCE_OUTPUT_RUNNING;
131 o->name = pa_xstrdup(data->name);
132 o->driver = pa_xstrdup(data->driver);
133 o->module = data->module;
134 o->source = data->source;
135 o->client = data->client;
136
137 o->sample_spec = data->sample_spec;
138 o->channel_map = data->channel_map;
139
140 o->push = NULL;
141 o->kill = NULL;
142 o->get_latency = NULL;
143 o->userdata = NULL;
144
145 o->resampler = resampler;
146 o->resample_method = data->resample_method;
147
148 r = pa_idxset_put(core->source_outputs, o, &o->index);
149 assert(r == 0);
150 r = pa_idxset_put(o->source->outputs, o, NULL);
151 assert(r == 0);
152
153 pa_log_info("created %u \"%s\" on %s with sample spec %s",
154 o->index,
155 o->name,
156 o->source->name,
157 pa_sample_spec_snprint(st, sizeof(st), &o->sample_spec));
158
159 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, o->index);
160
161 /* We do not call pa_source_notify() here, because the virtual
162 * functions have not yet been initialized */
163
164 return o;
165 }
166
167 void pa_source_output_disconnect(pa_source_output*o) {
168 assert(o);
169 assert(o->state != PA_SOURCE_OUTPUT_DISCONNECTED);
170 assert(o->source);
171 assert(o->source->core);
172
173 pa_idxset_remove_by_data(o->source->core->source_outputs, o, NULL);
174 pa_idxset_remove_by_data(o->source->outputs, o, NULL);
175
176 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_REMOVE, o->index);
177 o->source = NULL;
178
179 o->push = NULL;
180 o->kill = NULL;
181 o->get_latency = NULL;
182
183 o->state = PA_SOURCE_OUTPUT_DISCONNECTED;
184 }
185
186 static void source_output_free(pa_source_output* o) {
187 assert(o);
188
189 if (o->state != PA_SOURCE_OUTPUT_DISCONNECTED)
190 pa_source_output_disconnect(o);
191
192 pa_log_info("freed %u \"%s\"", o->index, o->name);
193
194 if (o->resampler)
195 pa_resampler_free(o->resampler);
196
197 pa_xfree(o->name);
198 pa_xfree(o->driver);
199 pa_xfree(o);
200 }
201
202 void pa_source_output_unref(pa_source_output* o) {
203 assert(o);
204 assert(o->ref >= 1);
205
206 if (!(--o->ref))
207 source_output_free(o);
208 }
209
210 pa_source_output* pa_source_output_ref(pa_source_output *o) {
211 assert(o);
212 assert(o->ref >= 1);
213
214 o->ref++;
215 return o;
216 }
217
218 void pa_source_output_kill(pa_source_output*o) {
219 assert(o);
220 assert(o->ref >= 1);
221
222 if (o->kill)
223 o->kill(o);
224 }
225
226 void pa_source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
227 pa_memchunk rchunk;
228
229 assert(o);
230 assert(chunk);
231 assert(chunk->length);
232 assert(o->push);
233
234 if (o->state == PA_SOURCE_OUTPUT_CORKED)
235 return;
236
237 if (!o->resampler) {
238 o->push(o, chunk);
239 return;
240 }
241
242 pa_resampler_run(o->resampler, chunk, &rchunk);
243 if (!rchunk.length)
244 return;
245
246 assert(rchunk.memblock);
247 o->push(o, &rchunk);
248 pa_memblock_unref(rchunk.memblock);
249 }
250
251 void pa_source_output_set_name(pa_source_output *o, const char *name) {
252 assert(o);
253 assert(o->ref >= 1);
254
255 if (!o->name && !name)
256 return;
257
258 if (o->name && name && !strcmp(o->name, name))
259 return;
260
261 pa_xfree(o->name);
262 o->name = pa_xstrdup(name);
263
264 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
265 }
266
267 pa_usec_t pa_source_output_get_latency(pa_source_output *o) {
268 assert(o);
269 assert(o->ref >= 1);
270
271 if (o->get_latency)
272 return o->get_latency(o);
273
274 return 0;
275 }
276
277 void pa_source_output_cork(pa_source_output *o, int b) {
278 int n;
279
280 assert(o);
281 assert(o->ref >= 1);
282
283 if (o->state == PA_SOURCE_OUTPUT_DISCONNECTED)
284 return;
285
286 n = o->state == PA_SOURCE_OUTPUT_CORKED && !b;
287
288 o->state = b ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING;
289
290 if (n)
291 pa_source_notify(o->source);
292 }
293
294 pa_resample_method_t pa_source_output_get_resample_method(pa_source_output *o) {
295 assert(o);
296 assert(o->ref >= 1);
297
298 if (!o->resampler)
299 return o->resample_method;
300
301 return pa_resampler_get_method(o->resampler);
302 }
303
304 int pa_source_output_move_to(pa_source_output *o, pa_source *dest) {
305 pa_source *origin;
306 pa_resampler *new_resampler = NULL;
307
308 assert(o);
309 assert(o->ref >= 1);
310 assert(dest);
311
312 origin = o->source;
313
314 if (dest == origin)
315 return 0;
316
317 if (pa_idxset_size(dest->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
318 pa_log_warn("Failed to move source output: too many outputs per source.");
319 return -1;
320 }
321
322 if (o->resampler &&
323 pa_sample_spec_equal(&origin->sample_spec, &dest->sample_spec) &&
324 pa_channel_map_equal(&origin->channel_map, &dest->channel_map))
325
326 /* Try to reuse the old resampler if possible */
327 new_resampler = o->resampler;
328
329 else if (!pa_sample_spec_equal(&o->sample_spec, &dest->sample_spec) ||
330 !pa_channel_map_equal(&o->channel_map, &dest->channel_map)) {
331
332 /* Okey, we need a new resampler for the new sink */
333
334 if (!(new_resampler = pa_resampler_new(
335 dest->core->mempool,
336 &dest->sample_spec, &dest->channel_map,
337 &o->sample_spec, &o->channel_map,
338 o->resample_method))) {
339 pa_log_warn("Unsupported resampling operation.");
340 return -1;
341 }
342 }
343
344 /* Okey, let's move it */
345 pa_idxset_remove_by_data(origin->outputs, o, NULL);
346 pa_idxset_put(dest->outputs, o, NULL);
347 o->source = dest;
348
349 /* Replace resampler */
350 if (new_resampler != o->resampler) {
351 if (o->resampler)
352 pa_resampler_free(o->resampler);
353 o->resampler = new_resampler;
354 }
355
356 /* Notify everyone */
357 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
358 pa_source_notify(o->source);
359
360 return 0;
361 }