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