]> code.delx.au - pulseaudio/blob - src/pulsecore/sink-input.c
add makefiles to speex/ and ffmpeg/ to easy compilation from emacs
[pulseaudio] / src / pulsecore / sink-input.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <pulse/utf8.h>
34 #include <pulse/xmalloc.h>
35
36 #include <pulsecore/sample-util.h>
37 #include <pulsecore/core-subscribe.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/play-memblockq.h>
40 #include <pulsecore/namereg.h>
41
42 #include "sink-input.h"
43
44 #define CONVERT_BUFFER_LENGTH 4096
45 #define MOVE_BUFFER_LENGTH (1024*1024)
46 #define SILENCE_BUFFER_LENGTH (64*1024)
47
48 static PA_DEFINE_CHECK_TYPE(pa_sink_input, pa_msgobject);
49
50 static void sink_input_free(pa_object *o);
51
52 pa_sink_input_new_data* pa_sink_input_new_data_init(pa_sink_input_new_data *data) {
53 pa_assert(data);
54
55 memset(data, 0, sizeof(*data));
56 data->resample_method = PA_RESAMPLER_INVALID;
57
58 return data;
59 }
60
61 void pa_sink_input_new_data_set_channel_map(pa_sink_input_new_data *data, const pa_channel_map *map) {
62 pa_assert(data);
63
64 if ((data->channel_map_is_set = !!map))
65 data->channel_map = *map;
66 }
67
68 void pa_sink_input_new_data_set_volume(pa_sink_input_new_data *data, const pa_cvolume *volume) {
69 pa_assert(data);
70
71 if ((data->volume_is_set = !!volume))
72 data->volume = *volume;
73 }
74
75 void pa_sink_input_new_data_set_sample_spec(pa_sink_input_new_data *data, const pa_sample_spec *spec) {
76 pa_assert(data);
77
78 if ((data->sample_spec_is_set = !!spec))
79 data->sample_spec = *spec;
80 }
81
82 void pa_sink_input_new_data_set_muted(pa_sink_input_new_data *data, int mute) {
83 pa_assert(data);
84
85 data->muted_is_set = 1;
86 data->muted = !!mute;
87 }
88
89 pa_sink_input* pa_sink_input_new(
90 pa_core *core,
91 pa_sink_input_new_data *data,
92 pa_sink_input_flags_t flags) {
93
94 pa_sink_input *i;
95 pa_resampler *resampler = NULL;
96 char st[PA_SAMPLE_SPEC_SNPRINT_MAX];
97
98 pa_assert(core);
99 pa_assert(data);
100
101 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], data) < 0)
102 return NULL;
103
104 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
105 pa_return_null_if_fail(!data->name || pa_utf8_valid(data->name));
106
107 if (!data->sink)
108 data->sink = pa_namereg_get(core, NULL, PA_NAMEREG_SINK, 1);
109
110 pa_return_null_if_fail(data->sink);
111 pa_return_null_if_fail(pa_sink_get_state(data->sink) != PA_SINK_DISCONNECTED);
112 pa_return_null_if_fail(!data->sync_base || (data->sync_base->sink == data->sink && pa_sink_input_get_state(data->sync_base) == PA_SINK_INPUT_CORKED));
113
114 if (!data->sample_spec_is_set)
115 data->sample_spec = data->sink->sample_spec;
116
117 pa_return_null_if_fail(pa_sample_spec_valid(&data->sample_spec));
118
119 if (!data->channel_map_is_set) {
120 if (data->sink->channel_map.channels == data->sample_spec.channels)
121 data->channel_map = data->sink->channel_map;
122 else
123 pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
124 }
125
126 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
127 pa_return_null_if_fail(data->channel_map.channels == data->sample_spec.channels);
128
129 if (!data->volume_is_set)
130 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
131
132 pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
133 pa_return_null_if_fail(data->volume.channels == data->sample_spec.channels);
134
135 if (!data->muted_is_set)
136 data->muted = 0;
137
138 if (data->resample_method == PA_RESAMPLER_INVALID)
139 data->resample_method = core->resample_method;
140
141 pa_return_null_if_fail(data->resample_method < PA_RESAMPLER_MAX);
142
143 if (pa_idxset_size(data->sink->inputs) >= PA_MAX_INPUTS_PER_SINK) {
144 pa_log_warn("Failed to create sink input: too many inputs per sink.");
145 return NULL;
146 }
147
148 if ((flags & PA_SINK_INPUT_VARIABLE_RATE) ||
149 !pa_sample_spec_equal(&data->sample_spec, &data->sink->sample_spec) ||
150 !pa_channel_map_equal(&data->channel_map, &data->sink->channel_map)) {
151
152 if (!(resampler = pa_resampler_new(
153 core->mempool,
154 &data->sample_spec, &data->channel_map,
155 &data->sink->sample_spec, &data->sink->channel_map,
156 data->resample_method,
157 !!(flags & PA_SINK_INPUT_VARIABLE_RATE)))) {
158 pa_log_warn("Unsupported resampling operation.");
159 return NULL;
160 }
161
162 data->resample_method = pa_resampler_get_method(resampler);
163 }
164
165 i = pa_msgobject_new(pa_sink_input);
166 i->parent.parent.free = sink_input_free;
167 i->parent.process_msg = pa_sink_input_process_msg;
168
169 i->core = core;
170 i->state = data->start_corked ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING;
171 i->flags = flags;
172 i->name = pa_xstrdup(data->name);
173 i->driver = pa_xstrdup(data->driver);
174 i->module = data->module;
175 i->sink = data->sink;
176 i->client = data->client;
177
178 i->resample_method = data->resample_method;
179 i->sample_spec = data->sample_spec;
180 i->channel_map = data->channel_map;
181
182 i->volume = data->volume;
183 i->muted = data->muted;
184
185 if (data->sync_base) {
186 i->sync_next = data->sync_base->sync_next;
187 i->sync_prev = data->sync_base;
188
189 if (data->sync_base->sync_next)
190 data->sync_base->sync_next->sync_prev = i;
191 data->sync_base->sync_next = i;
192 } else
193 i->sync_next = i->sync_prev = NULL;
194
195 i->peek = NULL;
196 i->drop = NULL;
197 i->kill = NULL;
198 i->get_latency = NULL;
199 i->underrun = NULL;
200 i->userdata = NULL;
201
202 i->thread_info.state = i->state;
203 pa_atomic_store(&i->thread_info.drained, 1);
204 i->thread_info.sample_spec = i->sample_spec;
205 i->thread_info.silence_memblock = NULL;
206 i->thread_info.move_silence = 0;
207 pa_memchunk_reset(&i->thread_info.resampled_chunk);
208 i->thread_info.resampler = resampler;
209 i->thread_info.volume = i->volume;
210 i->thread_info.muted = i->muted;
211
212 pa_assert_se(pa_idxset_put(core->sink_inputs, pa_sink_input_ref(i), &i->index) == 0);
213 pa_assert_se(pa_idxset_put(i->sink->inputs, i, NULL) == 0);
214
215 pa_log_info("Created input %u \"%s\" on %s with sample spec %s",
216 i->index,
217 i->name,
218 i->sink->name,
219 pa_sample_spec_snprint(st, sizeof(st), &i->sample_spec));
220
221 /* Don't forget to call pa_sink_input_put! */
222
223 return i;
224 }
225
226 static int sink_input_set_state(pa_sink_input *i, pa_sink_input_state_t state) {
227 pa_sink_input *ssync;
228 pa_assert(i);
229
230 if (state == PA_SINK_INPUT_DRAINED)
231 state = PA_SINK_INPUT_RUNNING;
232
233 if (i->state == state)
234 return 0;
235
236 if (pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) < 0)
237 return -1;
238
239 i->state = state;
240 for (ssync = i->sync_prev; ssync; ssync = ssync->sync_prev)
241 ssync->state = state;
242 for (ssync = i->sync_next; ssync; ssync = ssync->sync_next)
243 ssync->state = state;
244
245 return 0;
246 }
247
248 void pa_sink_input_disconnect(pa_sink_input *i) {
249 pa_assert(i);
250 pa_return_if_fail(i->state != PA_SINK_INPUT_DISCONNECTED);
251
252 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_DISCONNECT], i);
253
254 if (i->sync_prev)
255 i->sync_prev->sync_next = i->sync_next;
256 if (i->sync_next)
257 i->sync_next->sync_prev = i->sync_prev;
258
259 i->sync_prev = i->sync_next = NULL;
260
261 pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_REMOVE_INPUT, i, 0, NULL);
262 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
263 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
264
265 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
266
267 sink_input_set_state(i, PA_SINK_INPUT_DISCONNECTED);
268 pa_sink_update_status(i->sink);
269
270 i->peek = NULL;
271 i->drop = NULL;
272 i->kill = NULL;
273 i->get_latency = NULL;
274 i->underrun = NULL;
275
276 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_DISCONNECT_POST], i);
277 i->sink = NULL;
278 pa_sink_input_unref(i);
279 }
280
281 static void sink_input_free(pa_object *o) {
282 pa_sink_input* i = PA_SINK_INPUT(o);
283
284 pa_assert(i);
285 pa_assert(pa_sink_input_refcnt(i) == 0);
286
287 if (i->state != PA_SINK_INPUT_DISCONNECTED)
288 pa_sink_input_disconnect(i);
289
290 pa_log_info("Freeing output %u \"%s\"", i->index, i->name);
291
292 if (i->thread_info.resampled_chunk.memblock)
293 pa_memblock_unref(i->thread_info.resampled_chunk.memblock);
294
295 if (i->thread_info.resampler)
296 pa_resampler_free(i->thread_info.resampler);
297
298 if (i->thread_info.silence_memblock)
299 pa_memblock_unref(i->thread_info.silence_memblock);
300
301 pa_xfree(i->name);
302 pa_xfree(i->driver);
303 pa_xfree(i);
304 }
305
306 void pa_sink_input_put(pa_sink_input *i) {
307 pa_sink_input_assert_ref(i);
308
309 i->thread_info.volume = i->volume;
310 i->thread_info.muted = i->muted;
311
312 pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_ADD_INPUT, i, 0, NULL);
313 pa_sink_update_status(i->sink);
314
315 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
316 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], i);
317 }
318
319 void pa_sink_input_kill(pa_sink_input*i) {
320 pa_sink_input_assert_ref(i);
321
322 if (i->kill)
323 i->kill(i);
324 }
325
326 pa_usec_t pa_sink_input_get_latency(pa_sink_input *i) {
327 pa_usec_t r = 0;
328
329 pa_sink_input_assert_ref(i);
330
331 if (pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_LATENCY, &r, 0, NULL) < 0)
332 r = 0;
333
334 if (i->get_latency)
335 r += i->get_latency(i);
336
337 return r;
338 }
339
340 /* Called from thread context */
341 int pa_sink_input_peek(pa_sink_input *i, pa_memchunk *chunk, pa_cvolume *volume) {
342 int ret = -1;
343 int do_volume_adj_here;
344 int volume_is_norm;
345
346 pa_sink_input_assert_ref(i);
347 pa_assert(chunk);
348 pa_assert(volume);
349
350 if (!i->peek || !i->drop || i->thread_info.state == PA_SINK_INPUT_DISCONNECTED || i->thread_info.state == PA_SINK_INPUT_CORKED)
351 goto finish;
352
353 pa_assert(i->thread_info.state == PA_SINK_INPUT_RUNNING || i->thread_info.state == PA_SINK_INPUT_DRAINED);
354
355 if (i->thread_info.move_silence > 0) {
356 size_t l;
357
358 /* We have just been moved and shall play some silence for a
359 * while until the old sink has drained its playback buffer */
360
361 if (!i->thread_info.silence_memblock)
362 i->thread_info.silence_memblock = pa_silence_memblock_new(i->sink->core->mempool, &i->sink->sample_spec, SILENCE_BUFFER_LENGTH);
363
364 chunk->memblock = pa_memblock_ref(i->thread_info.silence_memblock);
365 chunk->index = 0;
366 l = pa_memblock_get_length(chunk->memblock);
367 chunk->length = i->thread_info.move_silence < l ? i->thread_info.move_silence : l;
368
369 ret = 0;
370 do_volume_adj_here = 1;
371 goto finish;
372 }
373
374 if (!i->thread_info.resampler) {
375 do_volume_adj_here = 0; /* FIXME??? */
376 ret = i->peek(i, chunk);
377 goto finish;
378 }
379
380 do_volume_adj_here = !pa_channel_map_equal(&i->channel_map, &i->sink->channel_map);
381 volume_is_norm = pa_cvolume_is_norm(&i->thread_info.volume) && !i->thread_info.muted;
382
383 while (!i->thread_info.resampled_chunk.memblock) {
384 pa_memchunk tchunk;
385 size_t l;
386
387 if ((ret = i->peek(i, &tchunk)) < 0)
388 goto finish;
389
390 pa_assert(tchunk.length > 0);
391
392 l = pa_resampler_request(i->thread_info.resampler, CONVERT_BUFFER_LENGTH);
393
394 if (tchunk.length > l)
395 tchunk.length = l;
396
397 i->drop(i, tchunk.length);
398
399 /* It might be necessary to adjust the volume here */
400 if (do_volume_adj_here && !volume_is_norm) {
401 pa_memchunk_make_writable(&tchunk, 0);
402
403 if (i->thread_info.muted)
404 pa_silence_memchunk(&tchunk, &i->thread_info.sample_spec);
405 else
406 pa_volume_memchunk(&tchunk, &i->thread_info.sample_spec, &i->thread_info.volume);
407 }
408
409 pa_resampler_run(i->thread_info.resampler, &tchunk, &i->thread_info.resampled_chunk);
410 pa_memblock_unref(tchunk.memblock);
411 }
412
413 pa_assert(i->thread_info.resampled_chunk.memblock);
414 pa_assert(i->thread_info.resampled_chunk.length > 0);
415
416 *chunk = i->thread_info.resampled_chunk;
417 pa_memblock_ref(i->thread_info.resampled_chunk.memblock);
418
419 ret = 0;
420
421 finish:
422
423 if (ret < 0 && !pa_atomic_load(&i->thread_info.drained) && i->underrun)
424 i->underrun(i);
425
426 if (ret >= 0)
427 pa_atomic_store(&i->thread_info.drained, 0);
428 else if (ret < 0)
429 pa_atomic_store(&i->thread_info.drained, 1);
430
431 if (ret >= 0) {
432 /* Let's see if we had to apply the volume adjustment
433 * ourselves, or if this can be done by the sink for us */
434
435 if (do_volume_adj_here)
436 /* We had different channel maps, so we already did the adjustment */
437 pa_cvolume_reset(volume, i->sink->sample_spec.channels);
438 else if (i->thread_info.muted)
439 /* We've both the same channel map, so let's have the sink do the adjustment for us*/
440 pa_cvolume_mute(volume, i->sink->sample_spec.channels);
441 else
442 *volume = i->thread_info.volume;
443 }
444
445 return ret;
446 }
447
448 /* Called from thread context */
449 void pa_sink_input_drop(pa_sink_input *i, size_t length) {
450 pa_sink_input_assert_ref(i);
451 pa_assert(length > 0);
452
453 if (i->thread_info.move_silence > 0) {
454
455 pa_assert(i->thread_info.move_silence >= length);
456
457 i->thread_info.move_silence -= length;
458
459 if (i->thread_info.move_silence <= 0) {
460 pa_assert(i->thread_info.silence_memblock);
461 pa_memblock_unref(i->thread_info.silence_memblock);
462 i->thread_info.silence_memblock = NULL;
463 }
464
465 return;
466 }
467
468 if (i->thread_info.resampled_chunk.memblock) {
469 size_t l = length;
470
471 if (l > i->thread_info.resampled_chunk.length)
472 l = i->thread_info.resampled_chunk.length;
473
474 i->thread_info.resampled_chunk.index += l;
475 i->thread_info.resampled_chunk.length -= l;
476
477 if (i->thread_info.resampled_chunk.length <= 0) {
478 pa_memblock_unref(i->thread_info.resampled_chunk.memblock);
479 pa_memchunk_reset(&i->thread_info.resampled_chunk);
480 }
481
482 length -= l;
483 }
484
485 if (length > 0) {
486
487 if (i->thread_info.resampler) {
488 /* So, we have a resampler. To avoid discontinuities we
489 * have to actually read all data that could be read and
490 * pass it through the resampler. */
491
492 while (length > 0) {
493 pa_memchunk chunk;
494 pa_cvolume volume;
495
496 if (pa_sink_input_peek(i, &chunk, &volume) >= 0) {
497 size_t l;
498
499 pa_memblock_unref(chunk.memblock);
500
501 l = chunk.length;
502 if (l > length)
503 l = length;
504
505 pa_sink_input_drop(i, l);
506 length -= l;
507
508 } else {
509 size_t l;
510
511 /* Hmmm, peeking failed, so let's at least drop
512 * the right amount of data */
513
514 if ((l = pa_resampler_request(i->thread_info.resampler, length)) > 0)
515 if (i->drop)
516 i->drop(i, l);
517
518 break;
519 }
520 }
521
522 } else {
523
524 /* We have no resampler, hence let's just drop the data */
525
526 if (i->drop)
527 i->drop(i, length);
528 }
529 }
530 }
531
532 void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume) {
533 pa_sink_input_assert_ref(i);
534
535 if (pa_cvolume_equal(&i->volume, volume))
536 return;
537
538 i->volume = *volume;
539
540 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), 0, NULL, pa_xfree);
541 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
542 }
543
544 const pa_cvolume *pa_sink_input_get_volume(pa_sink_input *i) {
545 pa_sink_input_assert_ref(i);
546
547 return &i->volume;
548 }
549
550 void pa_sink_input_set_mute(pa_sink_input *i, int mute) {
551 pa_assert(i);
552 pa_sink_input_assert_ref(i);
553
554 if (!i->muted == !mute)
555 return;
556
557 i->muted = mute;
558
559 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
560 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
561 }
562
563 int pa_sink_input_get_mute(pa_sink_input *i) {
564 pa_sink_input_assert_ref(i);
565
566 return !!i->muted;
567 }
568
569 void pa_sink_input_cork(pa_sink_input *i, int b) {
570 pa_sink_input_assert_ref(i);
571
572 sink_input_set_state(i, b ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING);
573 }
574
575 int pa_sink_input_set_rate(pa_sink_input *i, uint32_t rate) {
576 pa_sink_input_assert_ref(i);
577 pa_return_val_if_fail(i->thread_info.resampler, -1);
578
579 if (i->sample_spec.rate == rate)
580 return 0;
581
582 i->sample_spec.rate = rate;
583
584 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_RATE, PA_UINT_TO_PTR(rate), 0, NULL, NULL);
585
586 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
587 return 0;
588 }
589
590 void pa_sink_input_set_name(pa_sink_input *i, const char *name) {
591 pa_sink_input_assert_ref(i);
592
593 if (!i->name && !name)
594 return;
595
596 if (i->name && name && !strcmp(i->name, name))
597 return;
598
599 pa_xfree(i->name);
600 i->name = pa_xstrdup(name);
601
602 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
603 }
604
605 pa_resample_method_t pa_sink_input_get_resample_method(pa_sink_input *i) {
606 pa_sink_input_assert_ref(i);
607
608 return i->resample_method;
609 }
610
611 int pa_sink_input_move_to(pa_sink_input *i, pa_sink *dest, int immediately) {
612 pa_resampler *new_resampler = NULL;
613 pa_sink *origin;
614 pa_usec_t silence_usec = 0;
615 pa_sink_input_move_info info;
616
617 pa_sink_input_assert_ref(i);
618 pa_sink_assert_ref(dest);
619
620 origin = i->sink;
621
622 if (dest == origin)
623 return 0;
624
625 if (i->sync_next || i->sync_prev) {
626 pa_log_warn("Moving synchronised streams not supported.");
627 return -1;
628 }
629
630 if (pa_idxset_size(dest->inputs) >= PA_MAX_INPUTS_PER_SINK) {
631 pa_log_warn("Failed to move sink input: too many inputs per sink.");
632 return -1;
633 }
634
635 if (i->thread_info.resampler &&
636 pa_sample_spec_equal(&origin->sample_spec, &dest->sample_spec) &&
637 pa_channel_map_equal(&origin->channel_map, &dest->channel_map))
638
639 /* Try to reuse the old resampler if possible */
640 new_resampler = i->thread_info.resampler;
641
642 else if ((i->flags & PA_SINK_INPUT_VARIABLE_RATE) ||
643 !pa_sample_spec_equal(&i->sample_spec, &dest->sample_spec) ||
644 !pa_channel_map_equal(&i->channel_map, &dest->channel_map)) {
645
646 /* Okey, we need a new resampler for the new sink */
647
648 if (!(new_resampler = pa_resampler_new(
649 dest->core->mempool,
650 &i->sample_spec, &i->channel_map,
651 &dest->sample_spec, &dest->channel_map,
652 i->resample_method,
653 !!(i->flags & PA_SINK_INPUT_VARIABLE_RATE)))) {
654 pa_log_warn("Unsupported resampling operation.");
655 return -1;
656 }
657 }
658
659 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE], i);
660
661 memset(&info, 0, sizeof(info));
662 info.sink_input = i;
663
664 if (!immediately) {
665 pa_usec_t old_latency, new_latency;
666
667 /* Let's do a little bit of Voodoo for compensating latency
668 * differences. We assume that the accuracy for our
669 * estimations is still good enough, even though we do these
670 * operations non-atomic. */
671
672 old_latency = pa_sink_get_latency(origin);
673 new_latency = pa_sink_get_latency(dest);
674
675 /* The already resampled data should go to the old sink */
676
677 if (old_latency >= new_latency) {
678
679 /* The latency of the old sink is larger than the latency
680 * of the new sink. Therefore to compensate for the
681 * difference we to play silence on the new one for a
682 * while */
683
684 silence_usec = old_latency - new_latency;
685
686 } else {
687
688 /* The latency of new sink is larger than the latency of
689 * the old sink. Therefore we have to precompute a little
690 * and make sure that this is still played on the old
691 * sink, until we can play the first sample on the new
692 * sink.*/
693
694 info.buffer_bytes = pa_usec_to_bytes(new_latency - old_latency, &origin->sample_spec);
695 }
696
697 /* Okey, let's move it */
698
699 if (info.buffer_bytes > 0) {
700
701 info.ghost_sink_input = pa_memblockq_sink_input_new(
702 origin,
703 "Ghost Stream",
704 &origin->sample_spec,
705 &origin->channel_map,
706 NULL,
707 NULL);
708
709 info.buffer = pa_memblockq_new(0, MOVE_BUFFER_LENGTH, 0, pa_frame_size(&origin->sample_spec), 0, 0, NULL);
710 }
711 }
712
713 pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, &info, 0, NULL);
714
715 if (info.ghost_sink_input) {
716 /* Basically, do what pa_sink_input_put() does ...*/
717 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, info.ghost_sink_input->index);
718 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], info.ghost_sink_input);
719 pa_sink_input_unref(info.ghost_sink_input);
720 }
721
722 pa_idxset_remove_by_data(origin->inputs, i, NULL);
723 pa_idxset_put(dest->inputs, i, NULL);
724 i->sink = dest;
725
726 /* Replace resampler */
727 if (new_resampler != i->thread_info.resampler) {
728 if (i->thread_info.resampler)
729 pa_resampler_free(i->thread_info.resampler);
730 i->thread_info.resampler = new_resampler;
731
732 /* if the resampler changed, the silence memblock is
733 * probably invalid now, too */
734 if (i->thread_info.silence_memblock) {
735 pa_memblock_unref(i->thread_info.silence_memblock);
736 i->thread_info.silence_memblock = NULL;
737 }
738 }
739
740 /* Dump already resampled data */
741 if (i->thread_info.resampled_chunk.memblock) {
742 /* Hmm, this data has already been added to the ghost queue, presumably, hence let's sleep a little bit longer */
743 silence_usec += pa_bytes_to_usec(i->thread_info.resampled_chunk.length, &origin->sample_spec);
744 pa_memblock_unref(i->thread_info.resampled_chunk.memblock);
745 pa_memchunk_reset(&i->thread_info.resampled_chunk);
746 }
747
748 /* Calculate the new sleeping time */
749 if (immediately)
750 i->thread_info.move_silence = 0;
751 else
752 i->thread_info.move_silence = pa_usec_to_bytes(
753 pa_bytes_to_usec(i->thread_info.move_silence, &i->sample_spec) +
754 silence_usec,
755 &i->sample_spec);
756
757 pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_ADD_INPUT, i, 0, NULL);
758
759 pa_sink_update_status(origin);
760 pa_sink_update_status(dest);
761
762 pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_POST], i);
763
764 pa_log_debug("Successfully moved sink input %i from %s to %s.", i->index, origin->name, dest->name);
765
766 /* Notify everyone */
767 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
768
769 return 0;
770 }
771
772 /* Called from thread context */
773 int pa_sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
774 pa_sink_input *i = PA_SINK_INPUT(o);
775
776 pa_sink_input_assert_ref(i);
777
778 switch (code) {
779 case PA_SINK_INPUT_MESSAGE_SET_VOLUME:
780 i->thread_info.volume = *((pa_cvolume*) userdata);
781 return 0;
782
783 case PA_SINK_INPUT_MESSAGE_SET_MUTE:
784 i->thread_info.muted = PA_PTR_TO_UINT(userdata);
785 return 0;
786
787 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
788 pa_usec_t *r = userdata;
789
790 if (i->thread_info.resampled_chunk.memblock)
791 *r += pa_bytes_to_usec(i->thread_info.resampled_chunk.length, &i->sink->sample_spec);
792
793 if (i->thread_info.move_silence)
794 *r += pa_bytes_to_usec(i->thread_info.move_silence, &i->sink->sample_spec);
795
796 return 0;
797 }
798
799 case PA_SINK_INPUT_MESSAGE_SET_RATE:
800
801 i->thread_info.sample_spec.rate = PA_PTR_TO_UINT(userdata);
802 pa_resampler_set_input_rate(i->thread_info.resampler, PA_PTR_TO_UINT(userdata));
803
804 return 0;
805
806 case PA_SINK_INPUT_MESSAGE_SET_STATE: {
807 pa_sink_input *ssync;
808
809 if ((PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_DRAINED || PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_RUNNING) &&
810 (i->thread_info.state != PA_SINK_INPUT_DRAINED) && (i->thread_info.state != PA_SINK_INPUT_RUNNING))
811 pa_atomic_store(&i->thread_info.drained, 1);
812
813 i->thread_info.state = PA_PTR_TO_UINT(userdata);
814
815 for (ssync = i->thread_info.sync_prev; ssync; ssync = ssync->thread_info.sync_prev) {
816 if ((PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_DRAINED || PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_RUNNING) &&
817 (ssync->thread_info.state != PA_SINK_INPUT_DRAINED) && (ssync->thread_info.state != PA_SINK_INPUT_RUNNING))
818 pa_atomic_store(&ssync->thread_info.drained, 1);
819 ssync->thread_info.state = PA_PTR_TO_UINT(userdata);
820 }
821
822 for (ssync = i->thread_info.sync_next; ssync; ssync = ssync->thread_info.sync_next) {
823 if ((PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_DRAINED || PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_RUNNING) &&
824 (ssync->thread_info.state != PA_SINK_INPUT_DRAINED) && (ssync->thread_info.state != PA_SINK_INPUT_RUNNING))
825 pa_atomic_store(&ssync->thread_info.drained, 1);
826 ssync->thread_info.state = PA_PTR_TO_UINT(userdata);
827 }
828
829 return 0;
830 }
831 }
832
833 return -1;
834 }
835
836 pa_sink_input_state_t pa_sink_input_get_state(pa_sink_input *i) {
837 pa_sink_input_assert_ref(i);
838
839 if (i->state == PA_SINK_INPUT_RUNNING || i->state == PA_SINK_INPUT_DRAINED)
840 return pa_atomic_load(&i->thread_info.drained) ? PA_SINK_INPUT_DRAINED : PA_SINK_INPUT_RUNNING;
841
842 return i->state;
843 }