]> code.delx.au - pulseaudio/blob - src/pulsecore/source-output.c
5df950a87d22e7e7b5281af2ce012382f210b45d
[pulseaudio] / src / pulsecore / source-output.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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 <stdlib.h>
28 #include <string.h>
29
30 #include <pulse/utf8.h>
31 #include <pulse/xmalloc.h>
32
33 #include <pulsecore/sample-util.h>
34 #include <pulsecore/core-subscribe.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/namereg.h>
37 #include <pulsecore/core-util.h>
38
39 #include "source-output.h"
40
41 #define MEMBLOCKQ_MAXLENGTH (32*1024*1024)
42
43 static PA_DEFINE_CHECK_TYPE(pa_source_output, pa_msgobject);
44
45 static void source_output_free(pa_object* mo);
46
47 pa_source_output_new_data* pa_source_output_new_data_init(pa_source_output_new_data *data) {
48 pa_assert(data);
49
50 memset(data, 0, sizeof(*data));
51 data->resample_method = PA_RESAMPLER_INVALID;
52 data->proplist = pa_proplist_new();
53
54 return data;
55 }
56
57 void pa_source_output_new_data_set_sample_spec(pa_source_output_new_data *data, const pa_sample_spec *spec) {
58 pa_assert(data);
59
60 if ((data->sample_spec_is_set = !!spec))
61 data->sample_spec = *spec;
62 }
63
64 void pa_source_output_new_data_set_channel_map(pa_source_output_new_data *data, const pa_channel_map *map) {
65 pa_assert(data);
66
67 if ((data->channel_map_is_set = !!map))
68 data->channel_map = *map;
69 }
70
71 void pa_source_output_new_data_done(pa_source_output_new_data *data) {
72 pa_assert(data);
73
74 pa_proplist_free(data->proplist);
75 }
76
77 /* Called from main context */
78 static void reset_callbacks(pa_source_output *o) {
79 pa_assert(o);
80
81 o->push = NULL;
82 o->process_rewind = NULL;
83 o->update_max_rewind = NULL;
84 o->update_source_requested_latency = NULL;
85 o->update_source_latency_range = NULL;
86 o->attach = NULL;
87 o->detach = NULL;
88 o->suspend = NULL;
89 o->moved = NULL;
90 o->kill = NULL;
91 o->get_latency = NULL;
92 o->state_change = NULL;
93 }
94
95 /* Called from main context */
96 pa_source_output* pa_source_output_new(
97 pa_core *core,
98 pa_source_output_new_data *data,
99 pa_source_output_flags_t flags) {
100
101 pa_source_output *o;
102 pa_resampler *resampler = NULL;
103 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
104
105 pa_assert(core);
106 pa_assert(data);
107
108 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], data) < 0)
109 return NULL;
110
111 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
112
113 if (!data->source)
114 data->source = pa_namereg_get(core, NULL, PA_NAMEREG_SOURCE, TRUE);
115
116 pa_return_null_if_fail(data->source);
117 pa_return_null_if_fail(pa_source_get_state(data->source) != PA_SOURCE_UNLINKED);
118
119 pa_return_null_if_fail(!data->direct_on_input || data->direct_on_input->sink == data->source->monitor_of);
120
121 if (!data->sample_spec_is_set)
122 data->sample_spec = data->source->sample_spec;
123
124 pa_return_null_if_fail(pa_sample_spec_valid(&data->sample_spec));
125
126 if (!data->channel_map_is_set) {
127 if (data->source->channel_map.channels == data->sample_spec.channels)
128 data->channel_map = data->source->channel_map;
129 else
130 pa_return_null_if_fail(pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT));
131 }
132
133 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
134 pa_return_null_if_fail(data->channel_map.channels == data->sample_spec.channels);
135
136 if (flags & PA_SOURCE_OUTPUT_FIX_FORMAT)
137 data->sample_spec.format = data->source->sample_spec.format;
138
139 if (flags & PA_SOURCE_OUTPUT_FIX_RATE)
140 data->sample_spec.rate = data->source->sample_spec.rate;
141
142 if (flags & PA_SOURCE_OUTPUT_FIX_CHANNELS) {
143 data->sample_spec.channels = data->source->sample_spec.channels;
144 data->channel_map = data->source->channel_map;
145 }
146
147 pa_assert(pa_sample_spec_valid(&data->sample_spec));
148 pa_assert(pa_channel_map_valid(&data->channel_map));
149
150 if (data->resample_method == PA_RESAMPLER_INVALID)
151 data->resample_method = core->resample_method;
152
153 pa_return_null_if_fail(data->resample_method < PA_RESAMPLER_MAX);
154
155 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE], data) < 0)
156 return NULL;
157
158 if (pa_idxset_size(data->source->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
159 pa_log("Failed to create source output: too many outputs per source.");
160 return NULL;
161 }
162
163 if ((flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ||
164 !pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec) ||
165 !pa_channel_map_equal(&data->channel_map, &data->source->channel_map)) {
166
167 if (!(resampler = pa_resampler_new(
168 core->mempool,
169 &data->source->sample_spec, &data->source->channel_map,
170 &data->sample_spec, &data->channel_map,
171 data->resample_method,
172 ((flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
173 ((flags & PA_SOURCE_OUTPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
174 (core->disable_remixing || (flags & PA_SOURCE_OUTPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0)))) {
175 pa_log_warn("Unsupported resampling operation.");
176 return NULL;
177 }
178
179 data->resample_method = pa_resampler_get_method(resampler);
180 }
181
182 o = pa_msgobject_new(pa_source_output);
183 o->parent.parent.free = source_output_free;
184 o->parent.process_msg = pa_source_output_process_msg;
185
186 o->core = core;
187 o->state = PA_SOURCE_OUTPUT_INIT;
188 o->flags = flags;
189 o->proplist = pa_proplist_copy(data->proplist);
190 o->driver = pa_xstrdup(data->driver);
191 o->module = data->module;
192 o->source = data->source;
193 o->client = data->client;
194
195 o->resample_method = data->resample_method;
196 o->sample_spec = data->sample_spec;
197 o->channel_map = data->channel_map;
198
199 o->direct_on_input = data->direct_on_input;
200
201 reset_callbacks(o);
202 o->userdata = NULL;
203
204 o->thread_info.state = o->state;
205 o->thread_info.attached = FALSE;
206 o->thread_info.sample_spec = o->sample_spec;
207 o->thread_info.resampler = resampler;
208 o->thread_info.requested_source_latency = (pa_usec_t) -1;
209 o->thread_info.direct_on_input = o->direct_on_input;
210
211 o->thread_info.delay_memblockq = pa_memblockq_new(
212 0,
213 MEMBLOCKQ_MAXLENGTH,
214 0,
215 pa_frame_size(&o->source->sample_spec),
216 0,
217 1,
218 0,
219 &o->source->silence);
220
221 pa_assert_se(pa_idxset_put(core->source_outputs, o, &o->index) == 0);
222 pa_assert_se(pa_idxset_put(o->source->outputs, pa_source_output_ref(o), NULL) == 0);
223
224 if (o->direct_on_input)
225 pa_assert_se(pa_idxset_put(o->direct_on_input->direct_outputs, o, NULL) == 0);
226
227 pa_log_info("Created output %u \"%s\" on %s with sample spec %s and channel map %s",
228 o->index,
229 pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_MEDIA_NAME)),
230 o->source->name,
231 pa_sample_spec_snprint(st, sizeof(st), &o->sample_spec),
232 pa_channel_map_snprint(cm, sizeof(cm), &o->channel_map));
233
234 /* Don't forget to call pa_source_output_put! */
235
236 return o;
237 }
238
239 /* Called from main context */
240 static void update_n_corked(pa_source_output *o, pa_source_output_state_t state) {
241 pa_assert(o);
242
243 if (o->state == PA_SOURCE_OUTPUT_CORKED && state != PA_SOURCE_OUTPUT_CORKED)
244 pa_assert_se(o->source->n_corked -- >= 1);
245 else if (o->state != PA_SOURCE_OUTPUT_CORKED && state == PA_SOURCE_OUTPUT_CORKED)
246 o->source->n_corked++;
247
248 pa_source_update_status(o->source);
249 }
250
251 /* Called from main context */
252 static int source_output_set_state(pa_source_output *o, pa_source_output_state_t state) {
253 pa_assert(o);
254
255 if (o->state == state)
256 return 0;
257
258 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) == 0);
259
260 update_n_corked(o, state);
261 o->state = state;
262
263 if (state != PA_SOURCE_OUTPUT_UNLINKED)
264 pa_hook_fire(&o->source->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED], o);
265
266 return 0;
267 }
268
269 /* Called from main context */
270 void pa_source_output_unlink(pa_source_output*o) {
271 pa_bool_t linked;
272 pa_assert(o);
273
274 /* See pa_sink_unlink() for a couple of comments how this function
275 * works */
276
277 pa_source_output_ref(o);
278
279 linked = PA_SOURCE_OUTPUT_IS_LINKED(o->state);
280
281 if (linked)
282 pa_hook_fire(&o->source->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK], o);
283
284 if (o->direct_on_input)
285 pa_idxset_remove_by_data(o->direct_on_input->direct_outputs, o, NULL);
286 pa_idxset_remove_by_data(o->source->core->source_outputs, o, NULL);
287 if (pa_idxset_remove_by_data(o->source->outputs, o, NULL))
288 pa_source_output_unref(o);
289
290 update_n_corked(o, PA_SOURCE_OUTPUT_UNLINKED);
291 o->state = PA_SOURCE_OUTPUT_UNLINKED;
292
293 if (linked)
294 if (o->source->asyncmsgq)
295 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL) == 0);
296
297 reset_callbacks(o);
298
299 if (linked) {
300 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_REMOVE, o->index);
301 pa_hook_fire(&o->source->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST], o);
302 }
303
304 o->source = NULL;
305 pa_source_output_unref(o);
306 }
307
308 /* Called from main context */
309 static void source_output_free(pa_object* mo) {
310 pa_source_output *o = PA_SOURCE_OUTPUT(mo);
311
312 pa_assert(o);
313 pa_assert(pa_source_output_refcnt(o) == 0);
314
315 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state))
316 pa_source_output_unlink(o);
317
318 pa_log_info("Freeing output %u \"%s\"", o->index, pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_MEDIA_NAME)));
319
320 pa_assert(!o->thread_info.attached);
321
322 if (o->thread_info.delay_memblockq)
323 pa_memblockq_free(o->thread_info.delay_memblockq);
324
325 if (o->thread_info.resampler)
326 pa_resampler_free(o->thread_info.resampler);
327
328 if (o->proplist)
329 pa_proplist_free(o->proplist);
330
331 pa_xfree(o->driver);
332 pa_xfree(o);
333 }
334
335 /* Called from main context */
336 void pa_source_output_put(pa_source_output *o) {
337 pa_source_output_state_t state;
338 pa_source_output_assert_ref(o);
339
340 pa_assert(o->state == PA_SOURCE_OUTPUT_INIT);
341
342 /* The following fields must be initialized properly */
343 pa_assert(o->push);
344 pa_assert(o->kill);
345
346 state = o->flags & PA_SOURCE_OUTPUT_START_CORKED ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING;
347
348 update_n_corked(o, state);
349 o->state = state;
350
351 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_ADD_OUTPUT, o, 0, NULL) == 0);
352
353 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, o->index);
354 pa_hook_fire(&o->source->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PUT], o);
355 }
356
357 /* Called from main context */
358 void pa_source_output_kill(pa_source_output*o) {
359 pa_source_output_assert_ref(o);
360 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
361
362 o->kill(o);
363 }
364
365 /* Called from main context */
366 pa_usec_t pa_source_output_get_latency(pa_source_output *o, pa_usec_t *source_latency) {
367 pa_usec_t r[2] = { 0, 0 };
368
369 pa_source_output_assert_ref(o);
370 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
371
372 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY, r, 0, NULL) == 0);
373
374 if (o->get_latency)
375 r[0] += o->get_latency(o);
376
377 if (source_latency)
378 *source_latency = r[1];
379
380 return r[0];
381 }
382
383 /* Called from thread context */
384 void pa_source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
385 size_t length;
386 size_t limit, mbs = 0;
387
388 pa_source_output_assert_ref(o);
389 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
390 pa_assert(chunk);
391 pa_assert(pa_frame_aligned(chunk->length, &o->source->sample_spec));
392
393 if (!o->push || o->thread_info.state == PA_SOURCE_OUTPUT_CORKED)
394 return;
395
396 pa_assert(o->thread_info.state == PA_SOURCE_OUTPUT_RUNNING);
397
398 if (pa_memblockq_push(o->thread_info.delay_memblockq, chunk) < 0) {
399 pa_log_debug("Delay queue overflow!");
400 pa_memblockq_seek(o->thread_info.delay_memblockq, (int64_t) chunk->length, PA_SEEK_RELATIVE);
401 }
402
403 limit = o->process_rewind ? 0 : o->source->thread_info.max_rewind;
404
405 /* Implement the delay queue */
406 while ((length = pa_memblockq_get_length(o->thread_info.delay_memblockq)) > limit) {
407 pa_memchunk qchunk;
408
409 length -= limit;
410
411 pa_assert_se(pa_memblockq_peek(o->thread_info.delay_memblockq, &qchunk) >= 0);
412
413 if (qchunk.length > length)
414 qchunk.length = length;
415
416 pa_assert(qchunk.length > 0);
417
418 if (!o->thread_info.resampler)
419 o->push(o, &qchunk);
420 else {
421 pa_memchunk rchunk;
422
423 if (mbs == 0)
424 mbs = pa_resampler_max_block_size(o->thread_info.resampler);
425
426 if (qchunk.length > mbs)
427 qchunk.length = mbs;
428
429 pa_resampler_run(o->thread_info.resampler, &qchunk, &rchunk);
430
431 if (rchunk.length > 0)
432 o->push(o, &rchunk);
433
434 if (rchunk.memblock)
435 pa_memblock_unref(rchunk.memblock);
436 }
437
438 pa_memblock_unref(qchunk.memblock);
439 pa_memblockq_drop(o->thread_info.delay_memblockq, qchunk.length);
440 }
441 }
442
443 /* Called from thread context */
444 void pa_source_output_process_rewind(pa_source_output *o, size_t nbytes /* in source sample spec */) {
445 pa_source_output_assert_ref(o);
446
447 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
448 pa_assert(pa_frame_aligned(nbytes, &o->source->sample_spec));
449
450 if (nbytes <= 0)
451 return;
452
453 if (o->process_rewind) {
454 pa_assert(pa_memblockq_get_length(o->thread_info.delay_memblockq) == 0);
455
456 if (o->thread_info.resampler)
457 nbytes = pa_resampler_result(o->thread_info.resampler, nbytes);
458
459 pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) nbytes);
460
461 if (nbytes > 0)
462 o->process_rewind(o, nbytes);
463
464 if (o->thread_info.resampler)
465 pa_resampler_reset(o->thread_info.resampler);
466
467 } else
468 pa_memblockq_rewind(o->thread_info.delay_memblockq, nbytes);
469 }
470
471 /* Called from thread context */
472 void pa_source_output_update_max_rewind(pa_source_output *o, size_t nbytes /* in the source's sample spec */) {
473 pa_source_output_assert_ref(o);
474 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
475 pa_assert(pa_frame_aligned(nbytes, &o->source->sample_spec));
476
477 if (o->update_max_rewind)
478 o->update_max_rewind(o, o->thread_info.resampler ? pa_resampler_result(o->thread_info.resampler, nbytes) : nbytes);
479 }
480
481 /* Called from thread context */
482 static pa_usec_t fixup_latency(pa_source *s, pa_usec_t usec) {
483 pa_source_assert_ref(s);
484
485 if (usec == (pa_usec_t) -1)
486 return usec;
487
488 if (s->thread_info.max_latency > 0 && usec > s->thread_info.max_latency)
489 usec = s->thread_info.max_latency;
490
491 if (s->thread_info.min_latency > 0 && usec < s->thread_info.min_latency)
492 usec = s->thread_info.min_latency;
493
494 return usec;
495 }
496
497 /* Called from thread context */
498 pa_usec_t pa_source_output_set_requested_latency_within_thread(pa_source_output *o, pa_usec_t usec) {
499 pa_source_output_assert_ref(o);
500
501 usec = fixup_latency(o->source, usec);
502 o->thread_info.requested_source_latency = usec;
503 pa_source_invalidate_requested_latency(o->source);
504
505 return usec;
506 }
507
508 /* Called from main context */
509 pa_usec_t pa_source_output_set_requested_latency(pa_source_output *o, pa_usec_t usec) {
510 pa_source_output_assert_ref(o);
511
512 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state))
513 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
514 else
515 /* If this source output is not realized yet, we have to touch
516 * the thread info data directly */
517
518 o->thread_info.requested_source_latency = usec;
519
520 return usec;
521 }
522
523 /* Called from main context */
524 pa_usec_t pa_source_output_get_requested_latency(pa_source_output *o) {
525 pa_usec_t usec = 0;
526
527 pa_source_output_assert_ref(o);
528
529 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state))
530 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
531 else
532 /* If this source output is not realized yet, we have to touch
533 * the thread info data directly */
534 usec = o->thread_info.requested_source_latency;
535
536 return usec;
537 }
538
539 /* Called from main context */
540 void pa_source_output_cork(pa_source_output *o, pa_bool_t b) {
541 pa_source_output_assert_ref(o);
542 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
543
544 source_output_set_state(o, b ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING);
545 }
546
547 /* Called from main context */
548 int pa_source_output_set_rate(pa_source_output *o, uint32_t rate) {
549 pa_source_output_assert_ref(o);
550 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
551 pa_return_val_if_fail(o->thread_info.resampler, -1);
552
553 if (o->sample_spec.rate == rate)
554 return 0;
555
556 o->sample_spec.rate = rate;
557
558 pa_asyncmsgq_post(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_RATE, PA_UINT_TO_PTR(rate), 0, NULL, NULL);
559
560 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
561 return 0;
562 }
563
564 /* Called from main context */
565 void pa_source_output_set_name(pa_source_output *o, const char *name) {
566 const char *old;
567 pa_source_output_assert_ref(o);
568
569 if (!name && !pa_proplist_contains(o->proplist, PA_PROP_MEDIA_NAME))
570 return;
571
572 old = pa_proplist_gets(o->proplist, PA_PROP_MEDIA_NAME);
573
574 if (old && name && !strcmp(old, name))
575 return;
576
577 if (name)
578 pa_proplist_sets(o->proplist, PA_PROP_MEDIA_NAME, name);
579 else
580 pa_proplist_unset(o->proplist, PA_PROP_MEDIA_NAME);
581
582 if (PA_SOURCE_OUTPUT_IS_LINKED(o->state)) {
583 pa_hook_fire(&o->source->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED], o);
584 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
585 }
586 }
587
588 /* Called from main context */
589 pa_resample_method_t pa_source_output_get_resample_method(pa_source_output *o) {
590 pa_source_output_assert_ref(o);
591
592 return o->resample_method;
593 }
594
595 /* Called from main context */
596 int pa_source_output_move_to(pa_source_output *o, pa_source *dest) {
597 pa_source *origin;
598 pa_resampler *new_resampler;
599 pa_source_output_move_hook_data hook_data;
600
601 pa_source_output_assert_ref(o);
602 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
603 pa_source_assert_ref(dest);
604
605 origin = o->source;
606
607 if (dest == origin)
608 return 0;
609
610 if (o->flags & PA_SOURCE_OUTPUT_DONT_MOVE)
611 return -1;
612
613 if (o->direct_on_input)
614 return -1;
615
616 if (pa_idxset_size(dest->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
617 pa_log_warn("Failed to move source output: too many outputs per source.");
618 return -1;
619 }
620
621 if (o->thread_info.resampler &&
622 pa_sample_spec_equal(&origin->sample_spec, &dest->sample_spec) &&
623 pa_channel_map_equal(&origin->channel_map, &dest->channel_map))
624
625 /* Try to reuse the old resampler if possible */
626 new_resampler = o->thread_info.resampler;
627
628 else if ((o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ||
629 !pa_sample_spec_equal(&o->sample_spec, &dest->sample_spec) ||
630 !pa_channel_map_equal(&o->channel_map, &dest->channel_map)) {
631
632 /* Okey, we need a new resampler for the new source */
633
634 if (!(new_resampler = pa_resampler_new(
635 dest->core->mempool,
636 &dest->sample_spec, &dest->channel_map,
637 &o->sample_spec, &o->channel_map,
638 o->resample_method,
639 ((o->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) |
640 ((o->flags & PA_SOURCE_OUTPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) |
641 (o->core->disable_remixing || (o->flags & PA_SOURCE_OUTPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0)))) {
642 pa_log_warn("Unsupported resampling operation.");
643 return -1;
644 }
645 } else
646 new_resampler = NULL;
647
648 hook_data.source_output = o;
649 hook_data.destination = dest;
650 pa_hook_fire(&o->source->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE], &hook_data);
651
652 /* Okey, let's move it */
653 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL) == 0);
654
655 pa_idxset_remove_by_data(origin->outputs, o, NULL);
656 pa_idxset_put(dest->outputs, o, NULL);
657 o->source = dest;
658
659 if (pa_source_output_get_state(o) == PA_SOURCE_OUTPUT_CORKED) {
660 pa_assert_se(origin->n_corked-- >= 1);
661 dest->n_corked++;
662 }
663
664 /* Replace resampler */
665 if (new_resampler != o->thread_info.resampler) {
666 if (o->thread_info.resampler)
667 pa_resampler_free(o->thread_info.resampler);
668 o->thread_info.resampler = new_resampler;
669
670 pa_memblockq_free(o->thread_info.delay_memblockq);
671
672 o->thread_info.delay_memblockq = pa_memblockq_new(
673 0,
674 MEMBLOCKQ_MAXLENGTH,
675 0,
676 pa_frame_size(&o->source->sample_spec),
677 0,
678 1,
679 0,
680 &o->source->silence);
681 }
682
683 pa_source_update_status(origin);
684 pa_source_update_status(dest);
685
686 pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_ADD_OUTPUT, o, 0, NULL) == 0);
687
688 if (o->moved)
689 o->moved(o);
690
691 pa_hook_fire(&o->source->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_POST], o);
692
693 pa_log_debug("Successfully moved source output %i from %s to %s.", o->index, origin->name, dest->name);
694
695 /* Notify everyone */
696 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
697
698 return 0;
699 }
700
701 /* Called from IO thread context */
702 void pa_source_output_set_state_within_thread(pa_source_output *o, pa_source_output_state_t state) {
703 pa_source_output_assert_ref(o);
704
705 if (state == o->thread_info.state)
706 return;
707
708 if (o->state_change)
709 o->state_change(o, state);
710
711 o->thread_info.state = state;
712 }
713
714 /* Called from IO thread context, except when it is not */
715 int pa_source_output_process_msg(pa_msgobject *mo, int code, void *userdata, int64_t offset, pa_memchunk* chunk) {
716 pa_source_output *o = PA_SOURCE_OUTPUT(mo);
717 pa_source_output_assert_ref(o);
718
719 switch (code) {
720
721 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY: {
722 pa_usec_t *r = userdata;
723 pa_usec_t source_usec = 0;
724
725 r[0] += pa_bytes_to_usec(pa_memblockq_get_length(o->thread_info.delay_memblockq), &o->source->sample_spec);
726
727 if (o->source->parent.process_msg(PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_GET_LATENCY, &source_usec, 0, NULL) >= 0)
728 r[1] += source_usec;
729
730 return 0;
731 }
732
733 case PA_SOURCE_OUTPUT_MESSAGE_SET_RATE:
734
735 o->thread_info.sample_spec.rate = PA_PTR_TO_UINT(userdata);
736 pa_resampler_set_output_rate(o->thread_info.resampler, PA_PTR_TO_UINT(userdata));
737 return 0;
738
739 case PA_SOURCE_OUTPUT_MESSAGE_SET_STATE:
740
741 pa_source_output_set_state_within_thread(o, PA_PTR_TO_UINT(userdata));
742 return 0;
743
744 case PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY: {
745 pa_usec_t *usec = userdata;
746
747 *usec = pa_source_output_set_requested_latency_within_thread(o, *usec);
748
749 return 0;
750 }
751
752 case PA_SOURCE_OUTPUT_MESSAGE_GET_REQUESTED_LATENCY: {
753 pa_usec_t *r = userdata;
754
755 *r = o->thread_info.requested_source_latency;
756 return 0;
757 }
758 }
759
760 return -1;
761 }