]> code.delx.au - pulseaudio/blob - src/pulsecore/source-output.c
add a couple of additional hooks for modules to use
[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 <stdlib.h>
30 #include <string.h>
31
32 #include <pulse/utf8.h>
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/core-subscribe.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/namereg.h>
38
39 #include "source-output.h"
40
41 static PA_DEFINE_CHECK_TYPE(pa_source_output, pa_msgobject);
42
43 static void source_output_free(pa_object* mo);
44
45 pa_source_output_new_data* pa_source_output_new_data_init(pa_source_output_new_data *data) {
46 pa_assert(data);
47
48 memset(data, 0, sizeof(*data));
49 data->resample_method = PA_RESAMPLER_INVALID;
50 return data;
51 }
52
53 void pa_source_output_new_data_set_channel_map(pa_source_output_new_data *data, const pa_channel_map *map) {
54 pa_assert(data);
55
56 if ((data->channel_map_is_set = !!map))
57 data->channel_map = *map;
58 }
59
60 void pa_source_output_new_data_set_sample_spec(pa_source_output_new_data *data, const pa_sample_spec *spec) {
61 pa_assert(data);
62
63 if ((data->sample_spec_is_set = !!spec))
64 data->sample_spec = *spec;
65 }
66
67 pa_source_output* pa_source_output_new(
68 pa_core *core,
69 pa_source_output_new_data *data,
70 pa_source_output_flags_t flags) {
71
72 pa_source_output *o;
73 pa_resampler *resampler = NULL;
74 char st[PA_SAMPLE_SPEC_SNPRINT_MAX];
75
76 pa_assert(core);
77 pa_assert(data);
78
79 if (!(flags & PA_SOURCE_OUTPUT_NO_HOOKS))
80 if (pa_hook_fire(&core->hook_source_output_new, data) < 0)
81 return NULL;
82
83 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
84 pa_return_null_if_fail(!data->name || pa_utf8_valid(data->name));
85
86 if (!data->source)
87 data->source = pa_namereg_get(core, NULL, PA_NAMEREG_SOURCE, 1);
88
89 pa_return_null_if_fail(data->source);
90 pa_return_null_if_fail(pa_source_get_state(data->source) != PA_SOURCE_DISCONNECTED);
91
92 if (!data->sample_spec_is_set)
93 data->sample_spec = data->source->sample_spec;
94
95 pa_return_null_if_fail(pa_sample_spec_valid(&data->sample_spec));
96
97 if (!data->channel_map_is_set) {
98 if (data->source->channel_map.channels == data->sample_spec.channels)
99 data->channel_map = data->source->channel_map;
100 else
101 pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
102 }
103
104 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
105 pa_return_null_if_fail(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 pa_return_null_if_fail(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 ((flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ||
118 !pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec) ||
119 !pa_channel_map_equal(&data->channel_map, &data->source->channel_map)) {
120
121 if (!(resampler = pa_resampler_new(
122 core->mempool,
123 &data->source->sample_spec, &data->source->channel_map,
124 &data->sample_spec, &data->channel_map,
125 data->resample_method))) {
126 pa_log_warn("Unsupported resampling operation.");
127 return NULL;
128 }
129
130 data->resample_method = pa_resampler_get_method(resampler);
131 }
132
133 o = pa_msgobject_new(pa_source_output);
134 o->parent.parent.free = source_output_free;
135 o->parent.process_msg = pa_source_output_process_msg;
136
137 o->core = core;
138 o->state = data->start_corked ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING;
139 o->flags = flags;
140 o->name = pa_xstrdup(data->name);
141 o->driver = pa_xstrdup(data->driver);
142 o->module = data->module;
143 o->source = data->source;
144 o->client = data->client;
145
146 o->resample_method = data->resample_method;
147 o->sample_spec = data->sample_spec;
148 o->channel_map = data->channel_map;
149
150 o->push = NULL;
151 o->kill = NULL;
152 o->get_latency = NULL;
153 o->userdata = NULL;
154
155 o->thread_info.state = o->state;
156 o->thread_info.sample_spec = o->sample_spec;
157 o->thread_info.resampler = resampler;
158
159 pa_assert_se(pa_idxset_put(core->source_outputs, o, &o->index) == 0);
160 pa_assert_se(pa_idxset_put(o->source->outputs, pa_source_output_ref(o), NULL) == 0);
161
162 pa_log_info("Created output %u \"%s\" on %s with sample spec %s",
163 o->index,
164 o->name,
165 o->source->name,
166 pa_sample_spec_snprint(st, sizeof(st), &o->sample_spec));
167
168 /* Don't forget to call pa_source_output_put! */
169
170 return o;
171 }
172
173 static int source_output_set_state(pa_source_output *o, pa_source_output_state_t state) {
174 pa_assert(o);
175
176 if (o->state == state)
177 return 0;
178
179 if (pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) < 0)
180 return -1;
181
182 o->state = state;
183 return 0;
184 }
185
186 void pa_source_output_disconnect(pa_source_output*o) {
187 pa_assert(o);
188 pa_return_if_fail(o->state != PA_SOURCE_OUTPUT_DISCONNECTED);
189
190 pa_hook_fire(&o->source->core->hook_source_output_disconnect, o);
191
192 pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL);
193
194 pa_idxset_remove_by_data(o->source->core->source_outputs, o, NULL);
195 pa_idxset_remove_by_data(o->source->outputs, o, NULL);
196
197 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_REMOVE, o->index);
198
199 source_output_set_state(o, PA_SOURCE_OUTPUT_DISCONNECTED);
200 pa_source_update_status(o->source);
201
202 o->push = NULL;
203 o->kill = NULL;
204 o->get_latency = NULL;
205
206 pa_hook_fire(&o->source->core->hook_source_output_disconnect_post, o);
207
208 o->source = NULL;
209 pa_source_output_unref(o);
210 }
211
212 static void source_output_free(pa_object* mo) {
213 pa_source_output *o = PA_SOURCE_OUTPUT(mo);
214
215 pa_assert(pa_source_output_refcnt(o) == 0);
216
217 if (o->state != PA_SOURCE_OUTPUT_DISCONNECTED)
218 pa_source_output_disconnect(o);
219
220 pa_log_info("Freeing output %u \"%s\"", o->index, o->name);
221
222 if (o->thread_info.resampler)
223 pa_resampler_free(o->thread_info.resampler);
224
225 pa_xfree(o->name);
226 pa_xfree(o->driver);
227 pa_xfree(o);
228 }
229
230 void pa_source_output_put(pa_source_output *o) {
231 pa_source_output_assert_ref(o);
232
233 pa_asyncmsgq_post(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_ADD_OUTPUT, pa_source_output_ref(o), 0, NULL, (pa_free_cb_t) pa_source_output_unref);
234 pa_source_update_status(o->source);
235
236 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, o->index);
237
238 pa_hook_fire(&o->source->core->hook_source_output_new_post, o);
239 }
240
241 void pa_source_output_kill(pa_source_output*o) {
242 pa_source_output_assert_ref(o);
243
244 if (o->kill)
245 o->kill(o);
246 }
247
248 pa_usec_t pa_source_output_get_latency(pa_source_output *o) {
249 pa_usec_t r = 0;
250
251 pa_source_output_assert_ref(o);
252
253 if (pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY, &r, 0, NULL) < 0)
254 r = 0;
255
256 if (o->get_latency)
257 r += o->get_latency(o);
258
259 return r;
260 }
261
262 void pa_source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
263 pa_memchunk rchunk;
264
265 pa_source_output_assert_ref(o);
266 pa_assert(chunk);
267 pa_assert(chunk->length);
268
269 if (!o->push || o->state == PA_SOURCE_OUTPUT_DISCONNECTED || o->state == PA_SOURCE_OUTPUT_CORKED)
270 return;
271
272 pa_assert(o->state == PA_SOURCE_OUTPUT_RUNNING);
273
274 if (!o->thread_info.resampler) {
275 o->push(o, chunk);
276 return;
277 }
278
279 pa_resampler_run(o->thread_info.resampler, chunk, &rchunk);
280 if (!rchunk.length)
281 return;
282
283 pa_assert(rchunk.memblock);
284 o->push(o, &rchunk);
285 pa_memblock_unref(rchunk.memblock);
286 }
287
288 void pa_source_output_cork(pa_source_output *o, int b) {
289 pa_source_output_assert_ref(o);
290
291 source_output_set_state(o, b ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING);
292 }
293
294 int pa_source_output_set_rate(pa_source_output *o, uint32_t rate) {
295 pa_source_output_assert_ref(o);
296 pa_return_val_if_fail(o->thread_info.resampler, -1);
297
298 if (o->sample_spec.rate == rate)
299 return 0;
300
301 o->sample_spec.rate = rate;
302
303 pa_asyncmsgq_post(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE, PA_UINT_TO_PTR(rate), 0, NULL, NULL);
304
305 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
306 return 0;
307 }
308
309 void pa_source_output_set_name(pa_source_output *o, const char *name) {
310 pa_source_output_assert_ref(o);
311
312 if (!o->name && !name)
313 return;
314
315 if (o->name && name && !strcmp(o->name, name))
316 return;
317
318 pa_xfree(o->name);
319 o->name = pa_xstrdup(name);
320
321 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
322 }
323
324 pa_resample_method_t pa_source_output_get_resample_method(pa_source_output *o) {
325 pa_source_output_assert_ref(o);
326
327 return o->resample_method;
328 }
329
330 int pa_source_output_move_to(pa_source_output *o, pa_source *dest) {
331 /* pa_source *origin; */
332 /* pa_resampler *new_resampler = NULL; */
333
334 pa_source_output_assert_ref(o);
335 pa_source_assert_ref(dest);
336
337 return -1;
338
339 /* origin = o->source; */
340
341 /* if (dest == origin) */
342 /* return 0; */
343
344 /* if (pa_idxset_size(dest->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) { */
345 /* pa_log_warn("Failed to move source output: too many outputs per source."); */
346 /* return -1; */
347 /* } */
348
349 /* if (o->resampler && */
350 /* pa_sample_spec_equal(&origin->sample_spec, &dest->sample_spec) && */
351 /* pa_channel_map_equal(&origin->channel_map, &dest->channel_map)) */
352
353 /* /\* Try to reuse the old resampler if possible *\/ */
354 /* new_resampler = o->resampler; */
355
356 /* else if (!pa_sample_spec_equal(&o->sample_spec, &dest->sample_spec) || */
357 /* !pa_channel_map_equal(&o->channel_map, &dest->channel_map)) { */
358
359 /* /\* Okey, we need a new resampler for the new source *\/ */
360
361 /* if (!(new_resampler = pa_resampler_new( */
362 /* dest->core->mempool, */
363 /* &dest->sample_spec, &dest->channel_map, */
364 /* &o->sample_spec, &o->channel_map, */
365 /* o->resample_method))) { */
366 /* pa_log_warn("Unsupported resampling operation."); */
367 /* return -1; */
368 /* } */
369 /* } */
370
371 /* /\* Okey, let's move it *\/ */
372 /* pa_idxset_remove_by_data(origin->outputs, o, NULL); */
373 /* pa_idxset_put(dest->outputs, o, NULL); */
374 /* o->source = dest; */
375
376 /* /\* Replace resampler *\/ */
377 /* if (new_resampler != o->resampler) { */
378 /* if (o->resampler) */
379 /* pa_resampler_free(o->resampler); */
380 /* o->resampler = new_resampler; */
381 /* } */
382
383 /* /\* Notify everyone *\/ */
384 /* pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index); */
385 /* pa_source_notify(o->source); */
386
387 /* return 0; */
388 }
389
390 int pa_source_output_process_msg(pa_msgobject *mo, int code, void *userdata, int64_t offset, pa_memchunk* chunk) {
391 pa_source_output *o = PA_SOURCE_OUTPUT(mo);
392
393 pa_source_output_assert_ref(o);
394
395 switch (code) {
396
397 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE: {
398
399 o->thread_info.sample_spec.rate = PA_PTR_TO_UINT(userdata);
400 pa_resampler_set_output_rate(o->thread_info.resampler, PA_PTR_TO_UINT(userdata));
401
402 return 0;
403 }
404
405 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE: {
406 o->thread_info.state = PA_PTR_TO_UINT(userdata);
407
408 return 0;
409 }
410
411 }
412
413 return -1;
414 }