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