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