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