]> code.delx.au - pulseaudio/blob - src/pulsecore/source-output.c
fix IDLE vs. RUNNING state handling of sinks/sources when changing cork status for...
[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 (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], data) < 0)
80 return NULL;
81
82 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
83 pa_return_null_if_fail(!data->name || pa_utf8_valid(data->name));
84
85 if (!data->source)
86 data->source = pa_namereg_get(core, NULL, PA_NAMEREG_SOURCE, 1);
87
88 pa_return_null_if_fail(data->source);
89 pa_return_null_if_fail(pa_source_get_state(data->source) != PA_SOURCE_UNLINKED);
90
91 if (!data->sample_spec_is_set)
92 data->sample_spec = data->source->sample_spec;
93
94 pa_return_null_if_fail(pa_sample_spec_valid(&data->sample_spec));
95
96 if (!data->channel_map_is_set) {
97 if (data->source->channel_map.channels == data->sample_spec.channels)
98 data->channel_map = data->source->channel_map;
99 else
100 pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
101 }
102
103 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
104 pa_return_null_if_fail(data->channel_map.channels == data->sample_spec.channels);
105
106 if (data->resample_method == PA_RESAMPLER_INVALID)
107 data->resample_method = core->resample_method;
108
109 pa_return_null_if_fail(data->resample_method < PA_RESAMPLER_MAX);
110
111 if (pa_idxset_size(data->source->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
112 pa_log("Failed to create source output: too many outputs per source.");
113 return NULL;
114 }
115
116 if ((flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ||
117 !pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec) ||
118 !pa_channel_map_equal(&data->channel_map, &data->source->channel_map)) {
119
120 if (!(resampler = pa_resampler_new(
121 core->mempool,
122 &data->source->sample_spec, &data->source->channel_map,
123 &data->sample_spec, &data->channel_map,
124 data->resample_method,
125 !!(flags & PA_SOURCE_OUTPUT_VARIABLE_RATE)))) {
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 = PA_SOURCE_OUTPUT_INIT;
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->detach = NULL;
154 o->attach = NULL;
155 o->suspend = NULL;
156 o->userdata = NULL;
157
158 o->thread_info.state = o->state;
159 o->thread_info.attached = FALSE;
160 o->thread_info.sample_spec = o->sample_spec;
161 o->thread_info.resampler = resampler;
162
163 pa_assert_se(pa_idxset_put(core->source_outputs, o, &o->index) == 0);
164 pa_assert_se(pa_idxset_put(o->source->outputs, pa_source_output_ref(o), NULL) == 0);
165
166 pa_log_info("Created output %u \"%s\" on %s with sample spec %s",
167 o->index,
168 o->name,
169 o->source->name,
170 pa_sample_spec_snprint(st, sizeof(st), &o->sample_spec));
171
172 /* Don't forget to call pa_source_output_put! */
173
174 return o;
175 }
176
177 static int source_output_set_state(pa_source_output *o, pa_source_output_state_t state) {
178 pa_assert(o);
179
180 if (o->state == state)
181 return 0;
182
183 if (pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) < 0)
184 return -1;
185
186 if (o->state == PA_SOURCE_OUTPUT_CORKED && state != PA_SOURCE_OUTPUT_CORKED)
187 pa_assert_se(o->source->n_corked -- >= 1);
188 else if (o->state != PA_SOURCE_OUTPUT_CORKED && state == PA_SOURCE_OUTPUT_CORKED)
189 o->source->n_corked++;
190
191 pa_source_update_status(o->source);
192
193 o->state = state;
194
195 return 0;
196 }
197
198 void pa_source_output_unlink(pa_source_output*o) {
199 pa_bool_t linked;
200 pa_assert(o);
201
202 /* See pa_sink_unlink() for a couple of comments how this function
203 * works */
204
205 pa_source_output_ref(o);
206
207 linked = PA_SOURCE_OUTPUT_LINKED(o->state);
208
209 if (linked)
210 pa_hook_fire(&o->source->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK], o);
211
212 pa_idxset_remove_by_data(o->source->core->source_outputs, o, NULL);
213 if (pa_idxset_remove_by_data(o->source->outputs, o, NULL))
214 pa_source_output_unref(o);
215
216 if (linked) {
217 pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL);
218 source_output_set_state(o, PA_SOURCE_OUTPUT_UNLINKED);
219 pa_source_update_status(o->source);
220 } else
221 o->state = PA_SOURCE_OUTPUT_UNLINKED;
222
223 o->push = NULL;
224 o->kill = NULL;
225 o->get_latency = NULL;
226 o->attach = NULL;
227 o->detach = NULL;
228 o->suspend = NULL;
229
230 if (linked) {
231 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_REMOVE, o->index);
232 pa_hook_fire(&o->source->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST], o);
233 }
234
235 o->source = NULL;
236 pa_source_output_unref(o);
237 }
238
239 static void source_output_free(pa_object* mo) {
240 pa_source_output *o = PA_SOURCE_OUTPUT(mo);
241
242 pa_assert(pa_source_output_refcnt(o) == 0);
243
244 if (PA_SOURCE_OUTPUT_LINKED(o->state))
245 pa_source_output_unlink(o);
246
247 pa_log_info("Freeing output %u \"%s\"", o->index, o->name);
248
249 pa_assert(!o->thread_info.attached);
250
251 if (o->thread_info.resampler)
252 pa_resampler_free(o->thread_info.resampler);
253
254 pa_xfree(o->name);
255 pa_xfree(o->driver);
256 pa_xfree(o);
257 }
258
259 void pa_source_output_put(pa_source_output *o) {
260 pa_source_output_assert_ref(o);
261
262 pa_assert(o->state == PA_SOURCE_OUTPUT_INIT);
263 pa_assert(o->push);
264
265 o->thread_info.state = o->state = o->flags & PA_SOURCE_OUTPUT_START_CORKED ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING;
266
267 if (o->state == PA_SOURCE_OUTPUT_CORKED)
268 o->source->n_corked++;
269
270 pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_ADD_OUTPUT, o, 0, NULL);
271 pa_source_update_status(o->source);
272
273 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, o->index);
274
275 pa_hook_fire(&o->source->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PUT], o);
276 }
277
278 void pa_source_output_kill(pa_source_output*o) {
279 pa_source_output_assert_ref(o);
280 pa_assert(PA_SOURCE_OUTPUT_LINKED(o->state));
281
282 if (o->kill)
283 o->kill(o);
284 }
285
286 pa_usec_t pa_source_output_get_latency(pa_source_output *o) {
287 pa_usec_t r = 0;
288
289 pa_source_output_assert_ref(o);
290 pa_assert(PA_SOURCE_OUTPUT_LINKED(o->state));
291
292 if (pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY, &r, 0, NULL) < 0)
293 r = 0;
294
295 if (o->get_latency)
296 r += o->get_latency(o);
297
298 return r;
299 }
300
301 /* Called from thread context */
302 void pa_source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
303 pa_memchunk rchunk;
304
305 pa_source_output_assert_ref(o);
306 pa_assert(PA_SOURCE_OUTPUT_LINKED(o->thread_info.state));
307 pa_assert(chunk);
308 pa_assert(chunk->length);
309
310 if (!o->push || o->state == PA_SOURCE_OUTPUT_CORKED)
311 return;
312
313 pa_assert(o->state == PA_SOURCE_OUTPUT_RUNNING);
314
315 if (!o->thread_info.resampler) {
316 o->push(o, chunk);
317 return;
318 }
319
320 pa_resampler_run(o->thread_info.resampler, chunk, &rchunk);
321 if (!rchunk.length)
322 return;
323
324 pa_assert(rchunk.memblock);
325 o->push(o, &rchunk);
326 pa_memblock_unref(rchunk.memblock);
327 }
328
329 void pa_source_output_cork(pa_source_output *o, pa_bool_t b) {
330 pa_source_output_assert_ref(o);
331 pa_assert(PA_SOURCE_OUTPUT_LINKED(o->state));
332
333 source_output_set_state(o, b ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING);
334 }
335
336 int pa_source_output_set_rate(pa_source_output *o, uint32_t rate) {
337 pa_source_output_assert_ref(o);
338 pa_assert(PA_SOURCE_OUTPUT_LINKED(o->state));
339 pa_return_val_if_fail(o->thread_info.resampler, -1);
340
341 if (o->sample_spec.rate == rate)
342 return 0;
343
344 o->sample_spec.rate = rate;
345
346 pa_asyncmsgq_post(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE, PA_UINT_TO_PTR(rate), 0, NULL, NULL);
347
348 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
349 return 0;
350 }
351
352 void pa_source_output_set_name(pa_source_output *o, const char *name) {
353 pa_source_output_assert_ref(o);
354
355 if (!o->name && !name)
356 return;
357
358 if (o->name && name && !strcmp(o->name, name))
359 return;
360
361 pa_xfree(o->name);
362 o->name = pa_xstrdup(name);
363
364 if (PA_SOURCE_OUTPUT_LINKED(o->state)) {
365 pa_hook_fire(&o->source->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NAME_CHANGED], o);
366 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
367 }
368 }
369
370 pa_resample_method_t pa_source_output_get_resample_method(pa_source_output *o) {
371 pa_source_output_assert_ref(o);
372
373 return o->resample_method;
374 }
375
376 int pa_source_output_move_to(pa_source_output *o, pa_source *dest) {
377 pa_source *origin;
378 pa_resampler *new_resampler = NULL;
379
380 pa_source_output_assert_ref(o);
381 pa_assert(PA_SOURCE_OUTPUT_LINKED(o->state));
382 pa_source_assert_ref(dest);
383
384 origin = o->source;
385
386 if (dest == origin)
387 return 0;
388
389 if (o->flags & PA_SOURCE_OUTPUT_DONT_MOVE)
390 return -1;
391
392 if (pa_idxset_size(dest->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
393 pa_log_warn("Failed to move source output: too many outputs per source.");
394 return -1;
395 }
396
397 if (o->thread_info.resampler &&
398 pa_sample_spec_equal(&origin->sample_spec, &dest->sample_spec) &&
399 pa_channel_map_equal(&origin->channel_map, &dest->channel_map))
400
401 /* Try to reuse the old resampler if possible */
402 new_resampler = o->thread_info.resampler;
403
404 else if ((o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ||
405 !pa_sample_spec_equal(&o->sample_spec, &dest->sample_spec) ||
406 !pa_channel_map_equal(&o->channel_map, &dest->channel_map)) {
407
408 /* Okey, we need a new resampler for the new source */
409
410 if (!(new_resampler = pa_resampler_new(
411 dest->core->mempool,
412 &dest->sample_spec, &dest->channel_map,
413 &o->sample_spec, &o->channel_map,
414 o->resample_method,
415 !!(o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE)))) {
416 pa_log_warn("Unsupported resampling operation.");
417 return -1;
418 }
419 }
420
421 pa_hook_fire(&o->source->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE], o);
422
423 /* Okey, let's move it */
424 pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL);
425
426 pa_idxset_remove_by_data(origin->outputs, o, NULL);
427 pa_idxset_put(dest->outputs, o, NULL);
428 o->source = dest;
429
430 /* Replace resampler */
431 if (new_resampler != o->thread_info.resampler) {
432 if (o->thread_info.resampler)
433 pa_resampler_free(o->thread_info.resampler);
434 o->thread_info.resampler = new_resampler;
435 }
436
437 pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_ADD_OUTPUT, o, 0, NULL);
438
439 pa_source_update_status(origin);
440 pa_source_update_status(dest);
441
442 pa_hook_fire(&o->source->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_POST], o);
443
444 pa_log_debug("Successfully moved source output %i from %s to %s.", o->index, origin->name, dest->name);
445
446 /* Notify everyone */
447 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
448
449 return 0;
450 }
451
452 /* Called from thread context */
453 int pa_source_output_process_msg(pa_msgobject *mo, int code, void *userdata, int64_t offset, pa_memchunk* chunk) {
454 pa_source_output *o = PA_SOURCE_OUTPUT(mo);
455
456 pa_source_output_assert_ref(o);
457 pa_assert(PA_SOURCE_OUTPUT_LINKED(o->thread_info.state));
458
459 switch (code) {
460
461 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE: {
462
463 o->thread_info.sample_spec.rate = PA_PTR_TO_UINT(userdata);
464 pa_resampler_set_output_rate(o->thread_info.resampler, PA_PTR_TO_UINT(userdata));
465
466 return 0;
467 }
468
469 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE: {
470 o->thread_info.state = PA_PTR_TO_UINT(userdata);
471
472 return 0;
473 }
474 }
475
476 return -1;
477 }