]> code.delx.au - pulseaudio/blob - src/pulsecore/sink-input.c
Increase ref counter of sink input as long as it is included in the sink idxset
[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, sink_input_check_type, pa_msgobject_check_type);
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 (!(flags & PA_SINK_INPUT_NO_HOOKS))
102 if (pa_hook_fire(&core->hook_sink_input_new, data) < 0)
103 return NULL;
104
105 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
106 pa_return_null_if_fail(!data->name || pa_utf8_valid(data->name));
107
108 if (!data->sink)
109 data->sink = pa_namereg_get(core, NULL, PA_NAMEREG_SINK, 1);
110
111 pa_return_null_if_fail(data->sink);
112 pa_return_null_if_fail(pa_sink_get_state(data->sink) != PA_SINK_DISCONNECTED);
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 pa_log_warn("Unsupported resampling operation.");
158 return NULL;
159 }
160
161 data->resample_method = pa_resampler_get_method(resampler);
162 }
163
164 i = pa_msgobject_new(pa_sink_input, sink_input_check_type);
165 i->parent.parent.free = sink_input_free;
166 i->parent.process_msg = pa_sink_input_process_msg;
167
168 i->core = core;
169 i->state = PA_SINK_INPUT_RUNNING;
170 i->flags = flags;
171 i->name = pa_xstrdup(data->name);
172 i->driver = pa_xstrdup(data->driver);
173 i->module = data->module;
174 i->sink = data->sink;
175 i->client = data->client;
176
177 i->resample_method = data->resample_method;
178 i->sample_spec = data->sample_spec;
179 i->channel_map = data->channel_map;
180
181 i->volume = data->volume;
182 i->muted = data->muted;
183
184 i->peek = NULL;
185 i->drop = NULL;
186 i->kill = NULL;
187 i->get_latency = NULL;
188 i->underrun = NULL;
189 i->userdata = NULL;
190
191 i->thread_info.state = i->state;
192 pa_atomic_store(&i->thread_info.drained, 1);
193 i->thread_info.sample_spec = i->sample_spec;
194 i->thread_info.silence_memblock = NULL;
195 /* i->thread_info.move_silence = 0; */
196 pa_memchunk_reset(&i->thread_info.resampled_chunk);
197 i->thread_info.resampler = resampler;
198 i->thread_info.volume = i->volume;
199 i->thread_info.muted = i->muted;
200
201 pa_assert_se(pa_idxset_put(core->sink_inputs, pa_sink_input_ref(i), &i->index) == 0);
202 pa_assert_se(pa_idxset_put(i->sink->inputs, i, NULL) == 0);
203
204 pa_log_info("Created input %u \"%s\" on %s with sample spec %s",
205 i->index,
206 i->name,
207 i->sink->name,
208 pa_sample_spec_snprint(st, sizeof(st), &i->sample_spec));
209
210 /* Don't forget to call pa_sink_input_put! */
211
212 return i;
213 }
214
215 static int sink_input_set_state(pa_sink_input *i, pa_sink_input_state_t state) {
216 pa_assert(i);
217
218 if (state == PA_SINK_INPUT_DRAINED)
219 state = PA_SINK_INPUT_RUNNING;
220
221 if (i->state == state)
222 return 0;
223
224 if (pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), NULL) < 0)
225 return -1;
226
227 i->state = state;
228 return 0;
229 }
230
231 void pa_sink_input_disconnect(pa_sink_input *i) {
232 pa_assert(i);
233 pa_return_if_fail(i->state != PA_SINK_INPUT_DISCONNECTED);
234
235 pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_REMOVE_INPUT, i, NULL);
236 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
237 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
238 pa_sink_input_unref(i);
239
240 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
241
242 sink_input_set_state(i, PA_SINK_INPUT_DISCONNECTED);
243 pa_sink_update_status(i->sink);
244
245 i->sink = NULL;
246 i->peek = NULL;
247 i->drop = NULL;
248 i->kill = NULL;
249 i->get_latency = NULL;
250 i->underrun = NULL;
251 }
252
253 static void sink_input_free(pa_object *o) {
254 pa_sink_input* i = PA_SINK_INPUT(o);
255
256 pa_assert(i);
257 pa_assert(pa_sink_input_refcnt(i) == 0);
258
259 if (i->state != PA_SINK_INPUT_DISCONNECTED)
260 pa_sink_input_disconnect(i);
261
262 pa_log_info("Freeing output %u \"%s\"", i->index, i->name);
263
264 if (i->thread_info.resampled_chunk.memblock)
265 pa_memblock_unref(i->thread_info.resampled_chunk.memblock);
266
267 if (i->thread_info.resampler)
268 pa_resampler_free(i->thread_info.resampler);
269
270 if (i->thread_info.silence_memblock)
271 pa_memblock_unref(i->thread_info.silence_memblock);
272
273 pa_xfree(i->name);
274 pa_xfree(i->driver);
275 pa_xfree(i);
276 }
277
278 void pa_sink_input_put(pa_sink_input *i) {
279 pa_sink_input_assert_ref(i);
280
281 i->thread_info.volume = i->volume;
282 i->thread_info.muted = i->muted;
283
284 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_ADD_INPUT, pa_sink_input_ref(i), NULL, (pa_free_cb_t) pa_sink_input_unref);
285 pa_sink_update_status(i->sink);
286
287 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
288 }
289
290 void pa_sink_input_kill(pa_sink_input*i) {
291 pa_sink_input_assert_ref(i);
292
293 if (i->kill)
294 i->kill(i);
295 }
296
297 pa_usec_t pa_sink_input_get_latency(pa_sink_input *i) {
298 pa_usec_t r = 0;
299
300 pa_sink_input_assert_ref(i);
301
302 if (pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_LATENCY, &r, NULL) < 0)
303 r = 0;
304
305 if (i->get_latency)
306 r += i->get_latency(i);
307
308 return r;
309 }
310
311 int pa_sink_input_peek(pa_sink_input *i, pa_memchunk *chunk, pa_cvolume *volume) {
312 int ret = -1;
313 int do_volume_adj_here;
314 int volume_is_norm;
315
316 pa_sink_input_assert_ref(i);
317 pa_assert(chunk);
318 pa_assert(volume);
319
320 if (!i->peek || !i->drop || i->thread_info.state == PA_SINK_INPUT_DISCONNECTED || i->thread_info.state == PA_SINK_INPUT_CORKED)
321 goto finish;
322
323 pa_assert(i->thread_info.state == PA_SINK_INPUT_RUNNING || i->thread_info.state == PA_SINK_INPUT_DRAINED);
324
325 /* if (i->thread_info.move_silence > 0) { */
326 /* size_t l; */
327
328 /* /\* We have just been moved and shall play some silence for a */
329 /* * while until the old sink has drained its playback buffer *\/ */
330
331 /* if (!i->thread_info.silence_memblock) */
332 /* i->thread_info.silence_memblock = pa_silence_memblock_new(i->sink->core->mempool, &i->sink->sample_spec, SILENCE_BUFFER_LENGTH); */
333
334 /* chunk->memblock = pa_memblock_ref(i->thread_info.silence_memblock); */
335 /* chunk->index = 0; */
336 /* l = pa_memblock_get_length(chunk->memblock); */
337 /* chunk->length = i->move_silence < l ? i->move_silence : l; */
338
339 /* ret = 0; */
340 /* do_volume_adj_here = 1; */
341 /* goto finish; */
342 /* } */
343
344 if (!i->thread_info.resampler) {
345 do_volume_adj_here = 0; /* FIXME??? */
346 ret = i->peek(i, chunk);
347 goto finish;
348 }
349
350 do_volume_adj_here = !pa_channel_map_equal(&i->channel_map, &i->sink->channel_map);
351 volume_is_norm = pa_cvolume_is_norm(&i->thread_info.volume) && !i->thread_info.muted;
352
353 while (!i->thread_info.resampled_chunk.memblock) {
354 pa_memchunk tchunk;
355 size_t l;
356
357 if ((ret = i->peek(i, &tchunk)) < 0)
358 goto finish;
359
360 pa_assert(tchunk.length > 0);
361
362 l = pa_resampler_request(i->thread_info.resampler, CONVERT_BUFFER_LENGTH);
363
364 if (tchunk.length > l)
365 tchunk.length = l;
366
367 i->drop(i, tchunk.length);
368
369 /* It might be necessary to adjust the volume here */
370 if (do_volume_adj_here && !volume_is_norm) {
371 pa_memchunk_make_writable(&tchunk, 0);
372 pa_volume_memchunk(&tchunk, &i->thread_info.sample_spec, &i->thread_info.volume);
373 }
374
375 pa_resampler_run(i->thread_info.resampler, &tchunk, &i->thread_info.resampled_chunk);
376 pa_memblock_unref(tchunk.memblock);
377 }
378
379 pa_assert(i->thread_info.resampled_chunk.memblock);
380 pa_assert(i->thread_info.resampled_chunk.length > 0);
381
382 *chunk = i->thread_info.resampled_chunk;
383 pa_memblock_ref(i->thread_info.resampled_chunk.memblock);
384
385 ret = 0;
386
387 finish:
388
389 if (ret < 0 && !pa_atomic_load(&i->thread_info.drained) && i->underrun)
390 i->underrun(i);
391
392 if (ret >= 0)
393 pa_atomic_store(&i->thread_info.drained, 0);
394 else if (ret < 0)
395 pa_atomic_store(&i->thread_info.drained, 1);
396
397 if (ret >= 0) {
398 /* Let's see if we had to apply the volume adjustment
399 * ourselves, or if this can be done by the sink for us */
400
401 if (do_volume_adj_here)
402 /* We had different channel maps, so we already did the adjustment */
403 pa_cvolume_reset(volume, i->sink->sample_spec.channels);
404 else
405 /* We've both the same channel map, so let's have the sink do the adjustment for us*/
406 *volume = i->thread_info.volume;
407 }
408
409 return ret;
410 }
411
412 void pa_sink_input_drop(pa_sink_input *i, size_t length) {
413 pa_sink_input_assert_ref(i);
414 pa_assert(length > 0);
415
416 /* if (i->move_silence > 0) { */
417
418 /* if (chunk) { */
419 /* size_t l; */
420
421 /* l = pa_memblock_get_length(i->silence_memblock); */
422
423 /* if (chunk->memblock != i->silence_memblock || */
424 /* chunk->index != 0 || */
425 /* (chunk->memblock && (chunk->length != (l < i->move_silence ? l : i->move_silence)))) */
426 /* return; */
427
428 /* } */
429
430 /* pa_assert(i->move_silence >= length); */
431
432 /* i->move_silence -= length; */
433
434 /* if (i->move_silence <= 0) { */
435 /* pa_assert(i->silence_memblock); */
436 /* pa_memblock_unref(i->silence_memblock); */
437 /* i->silence_memblock = NULL; */
438 /* } */
439
440 /* return; */
441 /* } */
442
443 pa_log("dropping %u", length);
444
445 if (i->thread_info.resampled_chunk.memblock) {
446 size_t l = length;
447
448 if (l > i->thread_info.resampled_chunk.length)
449 l = i->thread_info.resampled_chunk.length;
450
451 pa_log("really dropping %u", l);
452
453 i->thread_info.resampled_chunk.index += l;
454 i->thread_info.resampled_chunk.length -= l;
455
456 if (i->thread_info.resampled_chunk.length <= 0) {
457 pa_memblock_unref(i->thread_info.resampled_chunk.memblock);
458 pa_memchunk_reset(&i->thread_info.resampled_chunk);
459 }
460
461 length -= l;
462 }
463
464 pa_log("really remaining %u", length);
465
466 if (length > 0) {
467
468 if (i->thread_info.resampler) {
469 /* So, we have a resampler. To avoid discontinuities we
470 * have to actually read all data that could be read and
471 * pass it through the resampler. */
472
473 while (length > 0) {
474 pa_memchunk chunk;
475 pa_cvolume volume;
476
477 if (pa_sink_input_peek(i, &chunk, &volume) >= 0) {
478 size_t l = chunk.length;
479
480 if (l > length)
481 l = length;
482
483 pa_sink_input_drop(i, l);
484 length -= l;
485
486 } else {
487 /* Hmmm, peeking failed, so let's at least drop
488 * the right amount of data */
489
490 if (i->drop)
491 i->drop(i, pa_resampler_request(i->thread_info.resampler, length));
492
493 break;
494 }
495 }
496
497 } else {
498
499 /* We have no resampler, hence let's just drop the data */
500
501 if (i->drop)
502 i->drop(i, length);
503 }
504 }
505 }
506
507 void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume) {
508 pa_sink_input_assert_ref(i);
509
510 if (pa_cvolume_equal(&i->volume, volume))
511 return;
512
513 i->volume = *volume;
514
515 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), NULL, pa_xfree);
516 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
517 }
518
519 const pa_cvolume *pa_sink_input_get_volume(pa_sink_input *i) {
520 pa_sink_input_assert_ref(i);
521
522 return &i->volume;
523 }
524
525 void pa_sink_input_set_mute(pa_sink_input *i, int mute) {
526 pa_assert(i);
527 pa_sink_input_assert_ref(i);
528
529 if (!i->muted == !mute)
530 return;
531
532 i->muted = mute;
533
534 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), NULL, NULL);
535 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
536 }
537
538 int pa_sink_input_get_mute(pa_sink_input *i) {
539 pa_sink_input_assert_ref(i);
540
541 return !!i->muted;
542 }
543
544 void pa_sink_input_cork(pa_sink_input *i, int b) {
545 pa_sink_input_assert_ref(i);
546
547 sink_input_set_state(i, b ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING);
548 }
549
550 int pa_sink_input_set_rate(pa_sink_input *i, uint32_t rate) {
551 pa_sink_input_assert_ref(i);
552 pa_return_val_if_fail(i->thread_info.resampler, -1);
553
554 if (i->sample_spec.rate == rate)
555 return 0;
556
557 i->sample_spec.rate = rate;
558
559 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_RATE, PA_UINT_TO_PTR(rate), NULL, NULL);
560
561 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
562 return 0;
563 }
564
565 void pa_sink_input_set_name(pa_sink_input *i, const char *name) {
566 pa_sink_input_assert_ref(i);
567
568 if (!i->name && !name)
569 return;
570
571 if (i->name && name && !strcmp(i->name, name))
572 return;
573
574 pa_xfree(i->name);
575 i->name = pa_xstrdup(name);
576
577 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
578 }
579
580 pa_resample_method_t pa_sink_input_get_resample_method(pa_sink_input *i) {
581 pa_sink_input_assert_ref(i);
582
583 return i->resample_method;
584 }
585
586 int pa_sink_input_move_to(pa_sink_input *i, pa_sink *dest, int immediately) {
587 /* pa_resampler *new_resampler = NULL; */
588 /* pa_memblockq *buffer = NULL; */
589 /* pa_sink *origin; */
590
591 pa_sink_input_assert_ref(i);
592 pa_sink_assert_ref(dest);
593
594 return -1;
595
596 /* origin = i->sink; */
597
598 /* if (dest == origin) */
599 /* return 0; */
600
601 /* if (pa_idxset_size(dest->inputs) >= PA_MAX_INPUTS_PER_SINK) { */
602 /* pa_log_warn("Failed to move sink input: too many inputs per sink."); */
603 /* return -1; */
604 /* } */
605
606 /* if (i->resampler && */
607 /* pa_sample_spec_equal(&origin->sample_spec, &dest->sample_spec) && */
608 /* pa_channel_map_equal(&origin->channel_map, &dest->channel_map)) */
609
610 /* /\* Try to reuse the old resampler if possible *\/ */
611 /* new_resampler = i->resampler; */
612
613 /* else if ((i->flags & PA_SINK_INPUT_VARIABLE_RATE) || */
614 /* !pa_sample_spec_equal(&i->sample_spec, &dest->sample_spec) || */
615 /* !pa_channel_map_equal(&i->channel_map, &dest->channel_map)) { */
616
617 /* /\* Okey, we need a new resampler for the new sink *\/ */
618
619 /* if (!(new_resampler = pa_resampler_new( */
620 /* dest->core->mempool, */
621 /* &i->sample_spec, &i->channel_map, */
622 /* &dest->sample_spec, &dest->channel_map, */
623 /* i->resample_method))) { */
624 /* pa_log_warn("Unsupported resampling operation."); */
625 /* return -1; */
626 /* } */
627 /* } */
628
629 /* if (!immediately) { */
630 /* pa_usec_t old_latency, new_latency; */
631 /* pa_usec_t silence_usec = 0; */
632
633 /* buffer = pa_memblockq_new(0, MOVE_BUFFER_LENGTH, 0, pa_frame_size(&origin->sample_spec), 0, 0, NULL); */
634
635 /* /\* Let's do a little bit of Voodoo for compensating latency */
636 /* * differences *\/ */
637
638 /* old_latency = pa_sink_get_latency(origin); */
639 /* new_latency = pa_sink_get_latency(dest); */
640
641 /* /\* The already resampled data should go to the old sink *\/ */
642
643 /* if (old_latency >= new_latency) { */
644
645 /* /\* The latency of the old sink is larger than the latency */
646 /* * of the new sink. Therefore to compensate for the */
647 /* * difference we to play silence on the new one for a */
648 /* * while *\/ */
649
650 /* silence_usec = old_latency - new_latency; */
651
652 /* } else { */
653 /* size_t l; */
654 /* int volume_is_norm; */
655
656 /* /\* The latency of new sink is larger than the latency of */
657 /* * the old sink. Therefore we have to precompute a little */
658 /* * and make sure that this is still played on the old */
659 /* * sink, until we can play the first sample on the new */
660 /* * sink.*\/ */
661
662 /* l = pa_usec_to_bytes(new_latency - old_latency, &origin->sample_spec); */
663
664 /* volume_is_norm = pa_cvolume_is_norm(&i->volume); */
665
666 /* while (l > 0) { */
667 /* pa_memchunk chunk; */
668 /* pa_cvolume volume; */
669 /* size_t n; */
670
671 /* if (pa_sink_input_peek(i, &chunk, &volume) < 0) */
672 /* break; */
673
674 /* n = chunk.length > l ? l : chunk.length; */
675 /* pa_sink_input_drop(i, &chunk, n); */
676 /* chunk.length = n; */
677
678 /* if (!volume_is_norm) { */
679 /* pa_memchunk_make_writable(&chunk, 0); */
680 /* pa_volume_memchunk(&chunk, &origin->sample_spec, &volume); */
681 /* } */
682
683 /* if (pa_memblockq_push(buffer, &chunk) < 0) { */
684 /* pa_memblock_unref(chunk.memblock); */
685 /* break; */
686 /* } */
687
688 /* pa_memblock_unref(chunk.memblock); */
689 /* l -= n; */
690 /* } */
691 /* } */
692
693 /* if (i->resampled_chunk.memblock) { */
694
695 /* /\* There is still some data left in the already resampled */
696 /* * memory block. Hence, let's output it on the old sink */
697 /* * and sleep so long on the new sink *\/ */
698
699 /* pa_memblockq_push(buffer, &i->resampled_chunk); */
700 /* silence_usec += pa_bytes_to_usec(i->resampled_chunk.length, &origin->sample_spec); */
701 /* } */
702
703 /* /\* Calculate the new sleeping time *\/ */
704 /* i->move_silence = pa_usec_to_bytes( */
705 /* pa_bytes_to_usec(i->move_silence, &i->sample_spec) + */
706 /* silence_usec, */
707 /* &i->sample_spec); */
708 /* } */
709
710 /* /\* Okey, let's move it *\/ */
711 /* pa_idxset_remove_by_data(origin->inputs, i, NULL); */
712 /* pa_idxset_put(dest->inputs, i, NULL); */
713 /* i->sink = dest; */
714
715 /* /\* Replace resampler *\/ */
716 /* if (new_resampler != i->resampler) { */
717 /* if (i->resampler) */
718 /* pa_resampler_free(i->resampler); */
719 /* i->resampler = new_resampler; */
720
721 /* /\* if the resampler changed, the silence memblock is */
722 /* * probably invalid now, too *\/ */
723 /* if (i->silence_memblock) { */
724 /* pa_memblock_unref(i->silence_memblock); */
725 /* i->silence_memblock = NULL; */
726 /* } */
727 /* } */
728
729 /* /\* Dump already resampled data *\/ */
730 /* if (i->resampled_chunk.memblock) { */
731 /* pa_memblock_unref(i->resampled_chunk.memblock); */
732 /* i->resampled_chunk.memblock = NULL; */
733 /* i->resampled_chunk.index = i->resampled_chunk.length = 0; */
734 /* } */
735
736 /* /\* Notify everyone *\/ */
737 /* pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index); */
738 /* pa_sink_notify(i->sink); */
739
740 /* /\* Ok, now let's feed the precomputed buffer to the old sink *\/ */
741 /* if (buffer) */
742 /* pa_play_memblockq(origin, "Ghost Stream", &origin->sample_spec, &origin->channel_map, buffer, NULL); */
743
744 /* return 0; */
745 }
746
747 int pa_sink_input_process_msg(pa_msgobject *o, int code, void *userdata, pa_memchunk *chunk) {
748 pa_sink_input *i = PA_SINK_INPUT(o);
749
750 pa_sink_input_assert_ref(i);
751
752 switch (code) {
753 case PA_SINK_INPUT_MESSAGE_SET_VOLUME:
754 i->thread_info.volume = *((pa_cvolume*) userdata);
755 return 0;
756
757 case PA_SINK_INPUT_MESSAGE_SET_MUTE:
758 i->thread_info.muted = PA_PTR_TO_UINT(userdata);
759 return 0;
760
761 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
762 pa_usec_t *r = userdata;
763
764 if (i->thread_info.resampled_chunk.memblock)
765 *r += pa_bytes_to_usec(i->thread_info.resampled_chunk.length, &i->sink->sample_spec);
766
767 /* if (i->move_silence) */
768 /* r += pa_bytes_to_usec(i->move_silence, &i->sink->sample_spec); */
769
770 return 0;
771 }
772
773 case PA_SINK_INPUT_MESSAGE_SET_RATE: {
774
775 i->thread_info.sample_spec.rate = PA_PTR_TO_UINT(userdata);
776 pa_resampler_set_input_rate(i->thread_info.resampler, PA_PTR_TO_UINT(userdata));
777
778 return 0;
779 }
780
781 case PA_SINK_INPUT_MESSAGE_SET_STATE: {
782 if ((PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_DRAINED || PA_PTR_TO_UINT(userdata) == PA_SINK_INPUT_RUNNING) &&
783 (i->thread_info.state != PA_SINK_INPUT_DRAINED) && (i->thread_info.state != PA_SINK_INPUT_RUNNING))
784 pa_atomic_store(&i->thread_info.drained, 1);
785
786 i->thread_info.state = PA_PTR_TO_UINT(userdata);
787
788 return 0;
789 }
790 }
791
792 return -1;
793 }
794
795 pa_sink_input_state_t pa_sink_input_get_state(pa_sink_input *i) {
796 pa_sink_input_assert_ref(i);
797
798 if (i->state == PA_SINK_INPUT_RUNNING || i->state == PA_SINK_INPUT_DRAINED)
799 return pa_atomic_load(&i->thread_info.drained) ? PA_SINK_INPUT_DRAINED : PA_SINK_INPUT_RUNNING;
800
801 return i->state;
802 }