]> code.delx.au - pulseaudio/blob - src/pulsecore/sink-input.c
remove pa_sink_input::variable_rate field since it has been folded into pa_sink_input...
[pulseaudio] / src / pulsecore / sink-input.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <pulse/utf8.h>
32 #include <pulse/xmalloc.h>
33
34 #include <pulsecore/sample-util.h>
35 #include <pulsecore/core-subscribe.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/play-memblockq.h>
38 #include <pulsecore/namereg.h>
39
40 #include "sink-input.h"
41
42 #define CONVERT_BUFFER_LENGTH 4096
43 #define MOVE_BUFFER_LENGTH (1024*1024)
44 #define SILENCE_BUFFER_LENGTH (64*1024)
45
46 #define CHECK_VALIDITY_RETURN_NULL(condition) \
47 do {\
48 if (!(condition)) \
49 return NULL; \
50 } while (0)
51
52 pa_sink_input_new_data* pa_sink_input_new_data_init(pa_sink_input_new_data *data) {
53 assert(data);
54
55 memset(data, 0, sizeof(*data));
56 data->resample_method = PA_RESAMPLER_INVALID;
57 return data;
58 }
59
60 void pa_sink_input_new_data_set_channel_map(pa_sink_input_new_data *data, const pa_channel_map *map) {
61 assert(data);
62
63 if ((data->channel_map_is_set = !!map))
64 data->channel_map = *map;
65 }
66
67 void pa_sink_input_new_data_set_volume(pa_sink_input_new_data *data, const pa_cvolume *volume) {
68 assert(data);
69
70 if ((data->volume_is_set = !!volume))
71 data->volume = *volume;
72 }
73
74 void pa_sink_input_new_data_set_sample_spec(pa_sink_input_new_data *data, const pa_sample_spec *spec) {
75 assert(data);
76
77 if ((data->sample_spec_is_set = !!spec))
78 data->sample_spec = *spec;
79 }
80
81 pa_sink_input* pa_sink_input_new(
82 pa_core *core,
83 pa_sink_input_new_data *data,
84 pa_sink_input_flags_t flags) {
85
86 pa_sink_input *i;
87 pa_resampler *resampler = NULL;
88 int r;
89 char st[PA_SAMPLE_SPEC_SNPRINT_MAX];
90
91 assert(core);
92 assert(data);
93
94 if (!(flags & PA_SINK_INPUT_NO_HOOKS))
95 if (pa_hook_fire(&core->hook_sink_input_new, data) < 0)
96 return NULL;
97
98 CHECK_VALIDITY_RETURN_NULL(!data->driver || pa_utf8_valid(data->driver));
99 CHECK_VALIDITY_RETURN_NULL(!data->name || pa_utf8_valid(data->name));
100
101 if (!data->sink)
102 data->sink = pa_namereg_get(core, NULL, PA_NAMEREG_SINK, 1);
103
104 CHECK_VALIDITY_RETURN_NULL(data->sink && data->sink->state == PA_SINK_RUNNING);
105
106 if (!data->sample_spec_is_set)
107 data->sample_spec = data->sink->sample_spec;
108
109 CHECK_VALIDITY_RETURN_NULL(pa_sample_spec_valid(&data->sample_spec));
110
111 if (!data->channel_map_is_set)
112 pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
113
114 CHECK_VALIDITY_RETURN_NULL(pa_channel_map_valid(&data->channel_map));
115 CHECK_VALIDITY_RETURN_NULL(data->channel_map.channels == data->sample_spec.channels);
116
117 if (!data->volume_is_set)
118 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
119
120 CHECK_VALIDITY_RETURN_NULL(pa_cvolume_valid(&data->volume));
121 CHECK_VALIDITY_RETURN_NULL(data->volume.channels == data->sample_spec.channels);
122
123 if (data->resample_method == PA_RESAMPLER_INVALID)
124 data->resample_method = core->resample_method;
125
126 CHECK_VALIDITY_RETURN_NULL(data->resample_method < PA_RESAMPLER_MAX);
127
128 if (pa_idxset_size(data->sink->inputs) >= PA_MAX_INPUTS_PER_SINK) {
129 pa_log_warn(__FILE__": Failed to create sink input: too many inputs per sink.");
130 return NULL;
131 }
132
133 if ((flags & PA_SINK_INPUT_VARIABLE_RATE) ||
134 !pa_sample_spec_equal(&data->sample_spec, &data->sink->sample_spec) ||
135 !pa_channel_map_equal(&data->channel_map, &data->sink->channel_map))
136
137 if (!(resampler = pa_resampler_new(
138 &data->sample_spec, &data->channel_map,
139 &data->sink->sample_spec, &data->sink->channel_map,
140 core->memblock_stat,
141 data->resample_method))) {
142 pa_log_warn(__FILE__": Unsupported resampling operation.");
143 return NULL;
144 }
145
146 i = pa_xnew(pa_sink_input, 1);
147 i->ref = 1;
148 i->state = PA_SINK_INPUT_DRAINED;
149 i->flags = flags;
150 i->name = pa_xstrdup(data->name);
151 i->driver = pa_xstrdup(data->driver);
152 i->module = data->module;
153 i->sink = data->sink;
154 i->client = data->client;
155
156 i->sample_spec = data->sample_spec;
157 i->channel_map = data->channel_map;
158 i->volume = data->volume;
159
160 i->peek = NULL;
161 i->drop = NULL;
162 i->kill = NULL;
163 i->get_latency = NULL;
164 i->underrun = NULL;
165 i->userdata = NULL;
166
167 i->move_silence = 0;
168
169 pa_memchunk_reset(&i->resampled_chunk);
170 i->resampler = resampler;
171 i->resample_method = data->resample_method;
172 i->silence_memblock = NULL;
173
174 r = pa_idxset_put(core->sink_inputs, i, &i->index);
175 assert(r == 0);
176 r = pa_idxset_put(i->sink->inputs, i, NULL);
177 assert(r == 0);
178
179 pa_log_info(__FILE__": created %u \"%s\" on %s with sample spec %s",
180 i->index,
181 i->name,
182 i->sink->name,
183 pa_sample_spec_snprint(st, sizeof(st), &i->sample_spec));
184
185 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
186
187 /* We do not call pa_sink_notify() here, because the virtual
188 * functions have not yet been initialized */
189
190 return i;
191 }
192
193 void pa_sink_input_disconnect(pa_sink_input *i) {
194 assert(i);
195 assert(i->state != PA_SINK_INPUT_DISCONNECTED);
196 assert(i->sink);
197 assert(i->sink->core);
198
199 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
200 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
201
202 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
203 i->sink = NULL;
204
205 i->peek = NULL;
206 i->drop = NULL;
207 i->kill = NULL;
208 i->get_latency = NULL;
209 i->underrun = NULL;
210
211 i->state = PA_SINK_INPUT_DISCONNECTED;
212 }
213
214 static void sink_input_free(pa_sink_input* i) {
215 assert(i);
216
217 if (i->state != PA_SINK_INPUT_DISCONNECTED)
218 pa_sink_input_disconnect(i);
219
220 pa_log_info(__FILE__": freed %u \"%s\"", i->index, i->name);
221
222 if (i->resampled_chunk.memblock)
223 pa_memblock_unref(i->resampled_chunk.memblock);
224
225 if (i->resampler)
226 pa_resampler_free(i->resampler);
227
228 if (i->silence_memblock)
229 pa_memblock_unref(i->silence_memblock);
230
231 pa_xfree(i->name);
232 pa_xfree(i->driver);
233 pa_xfree(i);
234 }
235
236 void pa_sink_input_unref(pa_sink_input *i) {
237 assert(i);
238 assert(i->ref >= 1);
239
240 if (!(--i->ref))
241 sink_input_free(i);
242 }
243
244 pa_sink_input* pa_sink_input_ref(pa_sink_input *i) {
245 assert(i);
246 assert(i->ref >= 1);
247
248 i->ref++;
249 return i;
250 }
251
252 void pa_sink_input_kill(pa_sink_input*i) {
253 assert(i);
254 assert(i->ref >= 1);
255
256 if (i->kill)
257 i->kill(i);
258 }
259
260 pa_usec_t pa_sink_input_get_latency(pa_sink_input *i) {
261 pa_usec_t r = 0;
262
263 assert(i);
264 assert(i->ref >= 1);
265
266 if (i->get_latency)
267 r += i->get_latency(i);
268
269 if (i->resampled_chunk.memblock)
270 r += pa_bytes_to_usec(i->resampled_chunk.length, &i->sink->sample_spec);
271
272 if (i->move_silence)
273 r += pa_bytes_to_usec(i->move_silence, &i->sink->sample_spec);
274
275 return r;
276 }
277
278 int pa_sink_input_peek(pa_sink_input *i, pa_memchunk *chunk, pa_cvolume *volume) {
279 int ret = -1;
280 int do_volume_adj_here;
281 int volume_is_norm;
282
283 assert(i);
284 assert(i->ref >= 1);
285 assert(chunk);
286 assert(volume);
287
288 pa_sink_input_ref(i);
289
290 if (!i->peek || !i->drop || i->state == PA_SINK_INPUT_CORKED)
291 goto finish;
292
293 assert(i->state == PA_SINK_INPUT_RUNNING || i->state == PA_SINK_INPUT_DRAINED);
294
295 if (i->move_silence > 0) {
296
297 /* We have just been moved and shall play some silence for a
298 * while until the old sink has drained its playback buffer */
299
300 if (!i->silence_memblock)
301 i->silence_memblock = pa_silence_memblock_new(&i->sink->sample_spec, SILENCE_BUFFER_LENGTH, i->sink->core->memblock_stat);
302
303 chunk->memblock = pa_memblock_ref(i->silence_memblock);
304 chunk->index = 0;
305 chunk->length = i->move_silence < chunk->memblock->length ? i->move_silence : chunk->memblock->length;
306
307 ret = 0;
308 do_volume_adj_here = 1;
309 goto finish;
310 }
311
312 if (!i->resampler) {
313 do_volume_adj_here = 0;
314 ret = i->peek(i, chunk);
315 goto finish;
316 }
317
318 do_volume_adj_here = !pa_channel_map_equal(&i->channel_map, &i->sink->channel_map);
319 volume_is_norm = pa_cvolume_is_norm(&i->volume);
320
321 while (!i->resampled_chunk.memblock) {
322 pa_memchunk tchunk;
323 size_t l;
324
325 if ((ret = i->peek(i, &tchunk)) < 0)
326 goto finish;
327
328 assert(tchunk.length);
329
330 l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
331
332 if (l > tchunk.length)
333 l = tchunk.length;
334
335 i->drop(i, &tchunk, l);
336 tchunk.length = l;
337
338 /* It might be necessary to adjust the volume here */
339 if (do_volume_adj_here && !volume_is_norm) {
340 pa_memchunk_make_writable(&tchunk, i->sink->core->memblock_stat, 0);
341 pa_volume_memchunk(&tchunk, &i->sample_spec, &i->volume);
342 }
343
344 pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
345 pa_memblock_unref(tchunk.memblock);
346 }
347
348 assert(i->resampled_chunk.memblock);
349 assert(i->resampled_chunk.length);
350
351 *chunk = i->resampled_chunk;
352 pa_memblock_ref(i->resampled_chunk.memblock);
353
354 ret = 0;
355
356 finish:
357
358 if (ret < 0 && i->state == PA_SINK_INPUT_RUNNING && i->underrun)
359 i->underrun(i);
360
361 if (ret >= 0)
362 i->state = PA_SINK_INPUT_RUNNING;
363 else if (ret < 0 && i->state == PA_SINK_INPUT_RUNNING)
364 i->state = PA_SINK_INPUT_DRAINED;
365
366 if (ret >= 0) {
367 /* Let's see if we had to apply the volume adjustment
368 * ourselves, or if this can be done by the sink for us */
369
370 if (do_volume_adj_here)
371 /* We had different channel maps, so we already did the adjustment */
372 pa_cvolume_reset(volume, i->sink->sample_spec.channels);
373 else
374 /* We've both the same channel map, so let's have the sink do the adjustment for us*/
375 *volume = i->volume;
376 }
377
378 pa_sink_input_unref(i);
379
380 return ret;
381 }
382
383 void pa_sink_input_drop(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
384 assert(i);
385 assert(i->ref >= 1);
386 assert(length > 0);
387
388 if (i->move_silence > 0) {
389
390 if (chunk) {
391
392 if (chunk->memblock != i->silence_memblock ||
393 chunk->index != 0 ||
394 (chunk->memblock && (chunk->length != (i->silence_memblock->length < i->move_silence ? i->silence_memblock->length : i->move_silence))))
395 return;
396
397 }
398
399 assert(i->move_silence >= length);
400
401 i->move_silence -= length;
402
403 if (i->move_silence <= 0) {
404 assert(i->silence_memblock);
405 pa_memblock_unref(i->silence_memblock);
406 i->silence_memblock = NULL;
407 }
408
409 return;
410 }
411
412 if (!i->resampler) {
413 if (i->drop)
414 i->drop(i, chunk, length);
415 return;
416 }
417
418 assert(i->resampled_chunk.memblock);
419 assert(i->resampled_chunk.length >= length);
420
421 i->resampled_chunk.index += length;
422 i->resampled_chunk.length -= length;
423
424 if (i->resampled_chunk.length <= 0) {
425 pa_memblock_unref(i->resampled_chunk.memblock);
426 i->resampled_chunk.memblock = NULL;
427 i->resampled_chunk.index = i->resampled_chunk.length = 0;
428 }
429 }
430
431 void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume) {
432 assert(i);
433 assert(i->ref >= 1);
434 assert(i->sink);
435 assert(i->sink->core);
436
437 if (pa_cvolume_equal(&i->volume, volume))
438 return;
439
440 i->volume = *volume;
441 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
442 }
443
444 const pa_cvolume * pa_sink_input_get_volume(pa_sink_input *i) {
445 assert(i);
446 assert(i->ref >= 1);
447
448 return &i->volume;
449 }
450
451 void pa_sink_input_cork(pa_sink_input *i, int b) {
452 int n;
453
454 assert(i);
455 assert(i->ref >= 1);
456
457 assert(i->state != PA_SINK_INPUT_DISCONNECTED);
458
459 n = i->state == PA_SINK_INPUT_CORKED && !b;
460
461 if (b)
462 i->state = PA_SINK_INPUT_CORKED;
463 else if (i->state == PA_SINK_INPUT_CORKED)
464 i->state = PA_SINK_INPUT_DRAINED;
465
466 if (n)
467 pa_sink_notify(i->sink);
468 }
469
470 void pa_sink_input_set_rate(pa_sink_input *i, uint32_t rate) {
471 assert(i);
472 assert(i->resampler);
473 assert(i->ref >= 1);
474
475 if (i->sample_spec.rate == rate)
476 return;
477
478 i->sample_spec.rate = rate;
479 pa_resampler_set_input_rate(i->resampler, rate);
480
481 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
482 }
483
484 void pa_sink_input_set_name(pa_sink_input *i, const char *name) {
485 assert(i);
486 assert(i->ref >= 1);
487
488 if (!i->name && !name)
489 return;
490
491 if (i->name && name && !strcmp(i->name, name))
492 return;
493
494 pa_xfree(i->name);
495 i->name = pa_xstrdup(name);
496
497 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
498 }
499
500 pa_resample_method_t pa_sink_input_get_resample_method(pa_sink_input *i) {
501 assert(i);
502 assert(i->ref >= 1);
503
504 if (!i->resampler)
505 return i->resample_method;
506
507 return pa_resampler_get_method(i->resampler);
508 }
509
510 int pa_sink_input_move_to(pa_sink_input *i, pa_sink *dest, int immediately) {
511 pa_resampler *new_resampler = NULL;
512 pa_memblockq *buffer = NULL;
513 pa_sink *origin;
514
515 assert(i);
516 assert(dest);
517
518 origin = i->sink;
519
520 if (dest == origin)
521 return 0;
522
523 if (pa_idxset_size(dest->inputs) >= PA_MAX_INPUTS_PER_SINK) {
524 pa_log_warn(__FILE__": Failed to move sink input: too many inputs per sink.");
525 return -1;
526 }
527
528 if (i->resampler &&
529 pa_sample_spec_equal(&origin->sample_spec, &dest->sample_spec) &&
530 pa_channel_map_equal(&origin->channel_map, &dest->channel_map))
531
532 /* Try to reuse the old resampler if possible */
533 new_resampler = i->resampler;
534
535 else if ((i->flags & PA_SINK_INPUT_VARIABLE_RATE) ||
536 !pa_sample_spec_equal(&i->sample_spec, &dest->sample_spec) ||
537 !pa_channel_map_equal(&i->channel_map, &dest->channel_map)) {
538
539 /* Okey, we need a new resampler for the new sink */
540
541 if (!(new_resampler = pa_resampler_new(
542 &i->sample_spec, &i->channel_map,
543 &dest->sample_spec, &dest->channel_map,
544 dest->core->memblock_stat,
545 i->resample_method))) {
546 pa_log_warn(__FILE__": Unsupported resampling operation.");
547 return -1;
548 }
549 }
550
551 if (!immediately) {
552 pa_usec_t old_latency, new_latency;
553 pa_usec_t silence_usec = 0;
554
555 buffer = pa_memblockq_new(0, MOVE_BUFFER_LENGTH, 0, pa_frame_size(&origin->sample_spec), 0, 0, NULL, NULL);
556
557 /* Let's do a little bit of Voodoo for compensating latency
558 * differences */
559
560 old_latency = pa_sink_get_latency(origin);
561 new_latency = pa_sink_get_latency(dest);
562
563 /* The already resampled data should go to the old sink */
564
565 if (old_latency >= new_latency) {
566
567 /* The latency of the old sink is larger than the latency
568 * of the new sink. Therefore to compensate for the
569 * difference we to play silence on the new one for a
570 * while */
571
572 silence_usec = old_latency - new_latency;
573
574 } else {
575 size_t l;
576 int volume_is_norm;
577
578 /* The latency of new sink is larger than the latency of
579 * the old sink. Therefore we have to precompute a little
580 * and make sure that this is still played on the old
581 * sink, until we can play the first sample on the new
582 * sink.*/
583
584 l = pa_usec_to_bytes(new_latency - old_latency, &origin->sample_spec);
585
586 volume_is_norm = pa_cvolume_is_norm(&i->volume);
587
588 while (l > 0) {
589 pa_memchunk chunk;
590 pa_cvolume volume;
591 size_t n;
592
593 if (pa_sink_input_peek(i, &chunk, &volume) < 0)
594 break;
595
596 n = chunk.length > l ? l : chunk.length;
597 pa_sink_input_drop(i, &chunk, n);
598 chunk.length = n;
599
600 if (!volume_is_norm) {
601 pa_memchunk_make_writable(&chunk, origin->core->memblock_stat, 0);
602 pa_volume_memchunk(&chunk, &origin->sample_spec, &volume);
603 }
604
605 if (pa_memblockq_push(buffer, &chunk) < 0) {
606 pa_memblock_unref(chunk.memblock);
607 break;
608 }
609
610 pa_memblock_unref(chunk.memblock);
611 l -= n;
612 }
613 }
614
615 if (i->resampled_chunk.memblock) {
616
617 /* There is still some data left in the already resampled
618 * memory block. Hence, let's output it on the old sink
619 * and sleep so long on the new sink */
620
621 pa_memblockq_push(buffer, &i->resampled_chunk);
622 silence_usec += pa_bytes_to_usec(i->resampled_chunk.length, &origin->sample_spec);
623 }
624
625 /* Calculate the new sleeping time */
626 i->move_silence = pa_usec_to_bytes(
627 pa_bytes_to_usec(i->move_silence, &i->sample_spec) +
628 silence_usec,
629 &i->sample_spec);
630 }
631
632 /* Okey, let's move it */
633 pa_idxset_remove_by_data(origin->inputs, i, NULL);
634 pa_idxset_put(dest->inputs, i, NULL);
635 i->sink = dest;
636
637 /* Replace resampler */
638 if (new_resampler != i->resampler) {
639 if (i->resampler)
640 pa_resampler_free(i->resampler);
641 i->resampler = new_resampler;
642
643 /* if the resampler changed, the silence memblock is
644 * probably invalid now, too */
645 if (i->silence_memblock) {
646 pa_memblock_unref(i->silence_memblock);
647 i->silence_memblock = NULL;
648 }
649 }
650
651 /* Dump already resampled data */
652 if (i->resampled_chunk.memblock) {
653 pa_memblock_unref(i->resampled_chunk.memblock);
654 i->resampled_chunk.memblock = NULL;
655 i->resampled_chunk.index = i->resampled_chunk.length = 0;
656 }
657
658 /* Notify everyone */
659 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
660 pa_sink_notify(i->sink);
661
662 /* Ok, no let's feed the precomputed buffer to the old sink */
663 if (buffer)
664 pa_play_memblockq(origin, "Ghost Stream", &origin->sample_spec, &origin->channel_map, buffer, NULL);
665
666 return 0;
667 }