]> code.delx.au - pulseaudio/blob - src/pulsecore/sink-input.c
Revert r1404 and keep it on a development branch until it is fully tested.
[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);
105 CHECK_VALIDITY_RETURN_NULL(data->sink->state == PA_SINK_RUNNING);
106
107 if (!data->sample_spec_is_set)
108 data->sample_spec = data->sink->sample_spec;
109
110 CHECK_VALIDITY_RETURN_NULL(pa_sample_spec_valid(&data->sample_spec));
111
112 if (!data->channel_map_is_set)
113 pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
114
115 CHECK_VALIDITY_RETURN_NULL(pa_channel_map_valid(&data->channel_map));
116 CHECK_VALIDITY_RETURN_NULL(data->channel_map.channels == data->sample_spec.channels);
117
118 if (!data->volume_is_set)
119 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
120
121 CHECK_VALIDITY_RETURN_NULL(pa_cvolume_valid(&data->volume));
122 CHECK_VALIDITY_RETURN_NULL(data->volume.channels == data->sample_spec.channels);
123
124 if (data->resample_method == PA_RESAMPLER_INVALID)
125 data->resample_method = core->resample_method;
126
127 CHECK_VALIDITY_RETURN_NULL(data->resample_method < PA_RESAMPLER_MAX);
128
129 if (pa_idxset_size(data->sink->inputs) >= PA_MAX_INPUTS_PER_SINK) {
130 pa_log_warn("Failed to create sink input: too many inputs per sink.");
131 return NULL;
132 }
133
134 if ((flags & PA_SINK_INPUT_VARIABLE_RATE) ||
135 !pa_sample_spec_equal(&data->sample_spec, &data->sink->sample_spec) ||
136 !pa_channel_map_equal(&data->channel_map, &data->sink->channel_map))
137
138 if (!(resampler = pa_resampler_new(
139 core->mempool,
140 &data->sample_spec, &data->channel_map,
141 &data->sink->sample_spec, &data->sink->channel_map,
142 data->resample_method))) {
143 pa_log_warn("Unsupported resampling operation.");
144 return NULL;
145 }
146
147 i = pa_xnew(pa_sink_input, 1);
148 i->ref = 1;
149 i->state = PA_SINK_INPUT_DRAINED;
150 i->flags = flags;
151 i->name = pa_xstrdup(data->name);
152 i->driver = pa_xstrdup(data->driver);
153 i->module = data->module;
154 i->sink = data->sink;
155 i->client = data->client;
156
157 i->sample_spec = data->sample_spec;
158 i->channel_map = data->channel_map;
159 i->volume = data->volume;
160
161 i->peek = NULL;
162 i->drop = NULL;
163 i->kill = NULL;
164 i->get_latency = NULL;
165 i->underrun = NULL;
166 i->userdata = NULL;
167
168 i->move_silence = 0;
169
170 pa_memchunk_reset(&i->resampled_chunk);
171 i->resampler = resampler;
172 i->resample_method = data->resample_method;
173 i->silence_memblock = NULL;
174
175 r = pa_idxset_put(core->sink_inputs, i, &i->index);
176 assert(r == 0);
177 r = pa_idxset_put(i->sink->inputs, i, NULL);
178 assert(r == 0);
179
180 pa_log_info("created %u \"%s\" on %s with sample spec %s",
181 i->index,
182 i->name,
183 i->sink->name,
184 pa_sample_spec_snprint(st, sizeof(st), &i->sample_spec));
185
186 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
187
188 /* We do not call pa_sink_notify() here, because the virtual
189 * functions have not yet been initialized */
190
191 return i;
192 }
193
194 void pa_sink_input_disconnect(pa_sink_input *i) {
195 assert(i);
196 assert(i->state != PA_SINK_INPUT_DISCONNECTED);
197 assert(i->sink);
198 assert(i->sink->core);
199
200 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
201 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
202
203 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_REMOVE, i->index);
204 i->sink = NULL;
205
206 i->peek = NULL;
207 i->drop = NULL;
208 i->kill = NULL;
209 i->get_latency = NULL;
210 i->underrun = NULL;
211
212 i->state = PA_SINK_INPUT_DISCONNECTED;
213 }
214
215 static void sink_input_free(pa_sink_input* i) {
216 assert(i);
217
218 if (i->state != PA_SINK_INPUT_DISCONNECTED)
219 pa_sink_input_disconnect(i);
220
221 pa_log_info("freed %u \"%s\"", i->index, i->name);
222
223 if (i->resampled_chunk.memblock)
224 pa_memblock_unref(i->resampled_chunk.memblock);
225
226 if (i->resampler)
227 pa_resampler_free(i->resampler);
228
229 if (i->silence_memblock)
230 pa_memblock_unref(i->silence_memblock);
231
232 pa_xfree(i->name);
233 pa_xfree(i->driver);
234 pa_xfree(i);
235 }
236
237 void pa_sink_input_unref(pa_sink_input *i) {
238 assert(i);
239 assert(i->ref >= 1);
240
241 if (!(--i->ref))
242 sink_input_free(i);
243 }
244
245 pa_sink_input* pa_sink_input_ref(pa_sink_input *i) {
246 assert(i);
247 assert(i->ref >= 1);
248
249 i->ref++;
250 return i;
251 }
252
253 void pa_sink_input_kill(pa_sink_input*i) {
254 assert(i);
255 assert(i->ref >= 1);
256
257 if (i->kill)
258 i->kill(i);
259 }
260
261 pa_usec_t pa_sink_input_get_latency(pa_sink_input *i) {
262 pa_usec_t r = 0;
263
264 assert(i);
265 assert(i->ref >= 1);
266
267 if (i->get_latency)
268 r += i->get_latency(i);
269
270 if (i->resampled_chunk.memblock)
271 r += pa_bytes_to_usec(i->resampled_chunk.length, &i->sink->sample_spec);
272
273 if (i->move_silence)
274 r += pa_bytes_to_usec(i->move_silence, &i->sink->sample_spec);
275
276 return r;
277 }
278
279 int pa_sink_input_peek(pa_sink_input *i, pa_memchunk *chunk, pa_cvolume *volume) {
280 int ret = -1;
281 int do_volume_adj_here;
282 int volume_is_norm;
283
284 assert(i);
285 assert(i->ref >= 1);
286 assert(chunk);
287 assert(volume);
288
289 pa_sink_input_ref(i);
290
291 if (!i->peek || !i->drop || i->state == PA_SINK_INPUT_CORKED)
292 goto finish;
293
294 assert(i->state == PA_SINK_INPUT_RUNNING || i->state == PA_SINK_INPUT_DRAINED);
295
296 if (i->move_silence > 0) {
297
298 /* We have just been moved and shall play some silence for a
299 * while until the old sink has drained its playback buffer */
300
301 if (!i->silence_memblock)
302 i->silence_memblock = pa_silence_memblock_new(i->sink->core->mempool, &i->sink->sample_spec, SILENCE_BUFFER_LENGTH);
303
304 chunk->memblock = pa_memblock_ref(i->silence_memblock);
305 chunk->index = 0;
306 chunk->length = i->move_silence < chunk->memblock->length ? i->move_silence : chunk->memblock->length;
307
308 ret = 0;
309 do_volume_adj_here = 1;
310 goto finish;
311 }
312
313 if (!i->resampler) {
314 do_volume_adj_here = 0;
315 ret = i->peek(i, chunk);
316 goto finish;
317 }
318
319 do_volume_adj_here = !pa_channel_map_equal(&i->channel_map, &i->sink->channel_map);
320 volume_is_norm = pa_cvolume_is_norm(&i->volume);
321
322 while (!i->resampled_chunk.memblock) {
323 pa_memchunk tchunk;
324 size_t l;
325
326 if ((ret = i->peek(i, &tchunk)) < 0)
327 goto finish;
328
329 assert(tchunk.length);
330
331 l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
332
333 if (l > tchunk.length)
334 l = tchunk.length;
335
336 i->drop(i, &tchunk, l);
337 tchunk.length = l;
338
339 /* It might be necessary to adjust the volume here */
340 if (do_volume_adj_here && !volume_is_norm) {
341 pa_memchunk_make_writable(&tchunk, 0);
342 pa_volume_memchunk(&tchunk, &i->sample_spec, &i->volume);
343 }
344
345 pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
346 pa_memblock_unref(tchunk.memblock);
347 }
348
349 assert(i->resampled_chunk.memblock);
350 assert(i->resampled_chunk.length);
351
352 *chunk = i->resampled_chunk;
353 pa_memblock_ref(i->resampled_chunk.memblock);
354
355 ret = 0;
356
357 finish:
358
359 if (ret < 0 && i->state == PA_SINK_INPUT_RUNNING && i->underrun)
360 i->underrun(i);
361
362 if (ret >= 0)
363 i->state = PA_SINK_INPUT_RUNNING;
364 else if (ret < 0 && i->state == PA_SINK_INPUT_RUNNING)
365 i->state = PA_SINK_INPUT_DRAINED;
366
367 if (ret >= 0) {
368 /* Let's see if we had to apply the volume adjustment
369 * ourselves, or if this can be done by the sink for us */
370
371 if (do_volume_adj_here)
372 /* We had different channel maps, so we already did the adjustment */
373 pa_cvolume_reset(volume, i->sink->sample_spec.channels);
374 else
375 /* We've both the same channel map, so let's have the sink do the adjustment for us*/
376 *volume = i->volume;
377 }
378
379 pa_sink_input_unref(i);
380
381 return ret;
382 }
383
384 void pa_sink_input_drop(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
385 assert(i);
386 assert(i->ref >= 1);
387 assert(length > 0);
388
389 if (i->move_silence > 0) {
390
391 if (chunk) {
392
393 if (chunk->memblock != i->silence_memblock ||
394 chunk->index != 0 ||
395 (chunk->memblock && (chunk->length != (i->silence_memblock->length < i->move_silence ? i->silence_memblock->length : i->move_silence))))
396 return;
397
398 }
399
400 assert(i->move_silence >= length);
401
402 i->move_silence -= length;
403
404 if (i->move_silence <= 0) {
405 assert(i->silence_memblock);
406 pa_memblock_unref(i->silence_memblock);
407 i->silence_memblock = NULL;
408 }
409
410 return;
411 }
412
413 if (!i->resampler) {
414 if (i->drop)
415 i->drop(i, chunk, length);
416 return;
417 }
418
419 assert(i->resampled_chunk.memblock);
420 assert(i->resampled_chunk.length >= length);
421
422 i->resampled_chunk.index += length;
423 i->resampled_chunk.length -= length;
424
425 if (i->resampled_chunk.length <= 0) {
426 pa_memblock_unref(i->resampled_chunk.memblock);
427 i->resampled_chunk.memblock = NULL;
428 i->resampled_chunk.index = i->resampled_chunk.length = 0;
429 }
430 }
431
432 void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume) {
433 assert(i);
434 assert(i->ref >= 1);
435 assert(i->sink);
436 assert(i->sink->core);
437
438 if (pa_cvolume_equal(&i->volume, volume))
439 return;
440
441 i->volume = *volume;
442 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
443 }
444
445 const pa_cvolume * pa_sink_input_get_volume(pa_sink_input *i) {
446 assert(i);
447 assert(i->ref >= 1);
448
449 return &i->volume;
450 }
451
452 void pa_sink_input_cork(pa_sink_input *i, int b) {
453 int n;
454
455 assert(i);
456 assert(i->ref >= 1);
457
458 assert(i->state != PA_SINK_INPUT_DISCONNECTED);
459
460 n = i->state == PA_SINK_INPUT_CORKED && !b;
461
462 if (b)
463 i->state = PA_SINK_INPUT_CORKED;
464 else if (i->state == PA_SINK_INPUT_CORKED)
465 i->state = PA_SINK_INPUT_DRAINED;
466
467 if (n)
468 pa_sink_notify(i->sink);
469 }
470
471 void pa_sink_input_set_rate(pa_sink_input *i, uint32_t rate) {
472 assert(i);
473 assert(i->resampler);
474 assert(i->ref >= 1);
475
476 if (i->sample_spec.rate == rate)
477 return;
478
479 i->sample_spec.rate = rate;
480 pa_resampler_set_input_rate(i->resampler, rate);
481
482 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
483 }
484
485 void pa_sink_input_set_name(pa_sink_input *i, const char *name) {
486 assert(i);
487 assert(i->ref >= 1);
488
489 if (!i->name && !name)
490 return;
491
492 if (i->name && name && !strcmp(i->name, name))
493 return;
494
495 pa_xfree(i->name);
496 i->name = pa_xstrdup(name);
497
498 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
499 }
500
501 pa_resample_method_t pa_sink_input_get_resample_method(pa_sink_input *i) {
502 assert(i);
503 assert(i->ref >= 1);
504
505 if (!i->resampler)
506 return i->resample_method;
507
508 return pa_resampler_get_method(i->resampler);
509 }
510
511 int pa_sink_input_move_to(pa_sink_input *i, pa_sink *dest, int immediately) {
512 pa_resampler *new_resampler = NULL;
513 pa_memblockq *buffer = NULL;
514 pa_sink *origin;
515
516 assert(i);
517 assert(dest);
518
519 origin = i->sink;
520
521 if (dest == origin)
522 return 0;
523
524 if (pa_idxset_size(dest->inputs) >= PA_MAX_INPUTS_PER_SINK) {
525 pa_log_warn("Failed to move sink input: too many inputs per sink.");
526 return -1;
527 }
528
529 if (i->resampler &&
530 pa_sample_spec_equal(&origin->sample_spec, &dest->sample_spec) &&
531 pa_channel_map_equal(&origin->channel_map, &dest->channel_map))
532
533 /* Try to reuse the old resampler if possible */
534 new_resampler = i->resampler;
535
536 else if ((i->flags & PA_SINK_INPUT_VARIABLE_RATE) ||
537 !pa_sample_spec_equal(&i->sample_spec, &dest->sample_spec) ||
538 !pa_channel_map_equal(&i->channel_map, &dest->channel_map)) {
539
540 /* Okey, we need a new resampler for the new sink */
541
542 if (!(new_resampler = pa_resampler_new(
543 dest->core->mempool,
544 &i->sample_spec, &i->channel_map,
545 &dest->sample_spec, &dest->channel_map,
546 i->resample_method))) {
547 pa_log_warn("Unsupported resampling operation.");
548 return -1;
549 }
550 }
551
552 if (!immediately) {
553 pa_usec_t old_latency, new_latency;
554 pa_usec_t silence_usec = 0;
555
556 buffer = pa_memblockq_new(0, MOVE_BUFFER_LENGTH, 0, pa_frame_size(&origin->sample_spec), 0, 0, NULL);
557
558 /* Let's do a little bit of Voodoo for compensating latency
559 * differences */
560
561 old_latency = pa_sink_get_latency(origin);
562 new_latency = pa_sink_get_latency(dest);
563
564 /* The already resampled data should go to the old sink */
565
566 if (old_latency >= new_latency) {
567
568 /* The latency of the old sink is larger than the latency
569 * of the new sink. Therefore to compensate for the
570 * difference we to play silence on the new one for a
571 * while */
572
573 silence_usec = old_latency - new_latency;
574
575 } else {
576 size_t l;
577 int volume_is_norm;
578
579 /* The latency of new sink is larger than the latency of
580 * the old sink. Therefore we have to precompute a little
581 * and make sure that this is still played on the old
582 * sink, until we can play the first sample on the new
583 * sink.*/
584
585 l = pa_usec_to_bytes(new_latency - old_latency, &origin->sample_spec);
586
587 volume_is_norm = pa_cvolume_is_norm(&i->volume);
588
589 while (l > 0) {
590 pa_memchunk chunk;
591 pa_cvolume volume;
592 size_t n;
593
594 if (pa_sink_input_peek(i, &chunk, &volume) < 0)
595 break;
596
597 n = chunk.length > l ? l : chunk.length;
598 pa_sink_input_drop(i, &chunk, n);
599 chunk.length = n;
600
601 if (!volume_is_norm) {
602 pa_memchunk_make_writable(&chunk, 0);
603 pa_volume_memchunk(&chunk, &origin->sample_spec, &volume);
604 }
605
606 if (pa_memblockq_push(buffer, &chunk) < 0) {
607 pa_memblock_unref(chunk.memblock);
608 break;
609 }
610
611 pa_memblock_unref(chunk.memblock);
612 l -= n;
613 }
614 }
615
616 if (i->resampled_chunk.memblock) {
617
618 /* There is still some data left in the already resampled
619 * memory block. Hence, let's output it on the old sink
620 * and sleep so long on the new sink */
621
622 pa_memblockq_push(buffer, &i->resampled_chunk);
623 silence_usec += pa_bytes_to_usec(i->resampled_chunk.length, &origin->sample_spec);
624 }
625
626 /* Calculate the new sleeping time */
627 i->move_silence = pa_usec_to_bytes(
628 pa_bytes_to_usec(i->move_silence, &i->sample_spec) +
629 silence_usec,
630 &i->sample_spec);
631 }
632
633 /* Okey, let's move it */
634 pa_idxset_remove_by_data(origin->inputs, i, NULL);
635 pa_idxset_put(dest->inputs, i, NULL);
636 i->sink = dest;
637
638 /* Replace resampler */
639 if (new_resampler != i->resampler) {
640 if (i->resampler)
641 pa_resampler_free(i->resampler);
642 i->resampler = new_resampler;
643
644 /* if the resampler changed, the silence memblock is
645 * probably invalid now, too */
646 if (i->silence_memblock) {
647 pa_memblock_unref(i->silence_memblock);
648 i->silence_memblock = NULL;
649 }
650 }
651
652 /* Dump already resampled data */
653 if (i->resampled_chunk.memblock) {
654 pa_memblock_unref(i->resampled_chunk.memblock);
655 i->resampled_chunk.memblock = NULL;
656 i->resampled_chunk.index = i->resampled_chunk.length = 0;
657 }
658
659 /* Notify everyone */
660 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
661 pa_sink_notify(i->sink);
662
663 /* Ok, no let's feed the precomputed buffer to the old sink */
664 if (buffer)
665 pa_play_memblockq(origin, "Ghost Stream", &origin->sample_spec, &origin->channel_map, buffer, NULL);
666
667 return 0;
668 }