343
385
pa_sink_input_unref(i);
388
/* Called from main context */
346
389
static void sink_input_free(pa_object *o) {
347
390
pa_sink_input* i = PA_SINK_INPUT(o);
350
393
pa_assert(pa_sink_input_refcnt(i) == 0);
352
if (PA_SINK_INPUT_LINKED(i->state))
395
if (PA_SINK_INPUT_IS_LINKED(i->state))
353
396
pa_sink_input_unlink(i);
355
pa_log_info("Freeing output %u \"%s\"", i->index, i->name);
398
pa_log_info("Freeing input %u \"%s\"", i->index, pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME)));
357
400
pa_assert(!i->thread_info.attached);
359
if (i->thread_info.resampled_chunk.memblock)
360
pa_memblock_unref(i->thread_info.resampled_chunk.memblock);
402
if (i->thread_info.render_memblockq)
403
pa_memblockq_free(i->thread_info.render_memblockq);
362
405
if (i->thread_info.resampler)
363
406
pa_resampler_free(i->thread_info.resampler);
365
if (i->thread_info.silence_memblock)
366
pa_memblock_unref(i->thread_info.silence_memblock);
409
pa_proplist_free(i->proplist);
411
if (i->direct_outputs)
412
pa_idxset_free(i->direct_outputs, NULL, NULL);
414
if (i->thread_info.direct_outputs)
415
pa_hashmap_free(i->thread_info.direct_outputs, NULL, NULL);
369
417
pa_xfree(i->driver);
421
/* Called from main context */
373
422
void pa_sink_input_put(pa_sink_input *i) {
423
pa_sink_input_state_t state;
374
424
pa_sink_input_assert_ref(i);
376
426
pa_assert(i->state == PA_SINK_INPUT_INIT);
380
i->thread_info.state = i->state = i->flags & PA_SINK_INPUT_START_CORKED ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING;
428
/* The following fields must be initialized properly */
430
pa_assert(i->process_rewind);
381
433
i->thread_info.volume = i->volume;
382
434
i->thread_info.muted = i->muted;
384
if (i->state == PA_SINK_INPUT_CORKED)
387
pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_ADD_INPUT, i, 0, NULL);
388
pa_sink_update_status(i->sink);
436
state = i->flags & PA_SINK_INPUT_START_CORKED ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING;
438
update_n_corked(i, state);
441
pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_ADD_INPUT, i, 0, NULL) == 0);
390
443
pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, i->index);
391
444
pa_hook_fire(&i->sink->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], i);
393
/* Please note that if you change something here, you have to
394
change something in pa_sink_input_move() with the ghost stream
447
/* Called from main context */
398
448
void pa_sink_input_kill(pa_sink_input*i) {
399
449
pa_sink_input_assert_ref(i);
400
pa_assert(PA_SINK_INPUT_LINKED(i->state));
450
pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
406
pa_usec_t pa_sink_input_get_latency(pa_sink_input *i) {
455
/* Called from main context */
456
pa_usec_t pa_sink_input_get_latency(pa_sink_input *i, pa_usec_t *sink_latency) {
457
pa_usec_t r[2] = { 0, 0 };
409
459
pa_sink_input_assert_ref(i);
410
pa_assert(PA_SINK_INPUT_LINKED(i->state));
460
pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
412
if (pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_LATENCY, &r, 0, NULL) < 0)
462
pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_LATENCY, r, 0, NULL) == 0);
415
464
if (i->get_latency)
416
r += i->get_latency(i);
465
r[0] += i->get_latency(i);
468
*sink_latency = r[1];
421
473
/* Called from thread context */
422
int pa_sink_input_peek(pa_sink_input *i, size_t length, pa_memchunk *chunk, pa_cvolume *volume) {
424
int do_volume_adj_here;
426
size_t block_size_max;
474
int pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink frames */, pa_memchunk *chunk, pa_cvolume *volume) {
475
pa_bool_t do_volume_adj_here;
476
pa_bool_t volume_is_norm;
477
size_t block_size_max_sink, block_size_max_sink_input;
428
480
pa_sink_input_assert_ref(i);
429
pa_assert(PA_SINK_INPUT_LINKED(i->thread_info.state));
430
pa_assert(pa_frame_aligned(length, &i->sink->sample_spec));
481
pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
482
pa_assert(pa_frame_aligned(slength, &i->sink->sample_spec));
431
483
pa_assert(chunk);
432
484
pa_assert(volume);
434
if (!i->peek || !i->drop || i->thread_info.state == PA_SINK_INPUT_CORKED)
437
pa_assert(i->thread_info.state == PA_SINK_INPUT_RUNNING || i->thread_info.state == PA_SINK_INPUT_DRAINED);
486
/* pa_log_debug("peek"); */
491
pa_assert(i->thread_info.state == PA_SINK_INPUT_RUNNING ||
492
i->thread_info.state == PA_SINK_INPUT_CORKED ||
493
i->thread_info.state == PA_SINK_INPUT_DRAINED);
495
block_size_max_sink_input = i->thread_info.resampler ?
496
pa_resampler_max_block_size(i->thread_info.resampler) :
497
pa_frame_align(pa_mempool_block_size_max(i->sink->core->mempool), &i->sample_spec);
499
block_size_max_sink = pa_frame_align(pa_mempool_block_size_max(i->sink->core->mempool), &i->sink->sample_spec);
439
501
/* Default buffer size */
441
length = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sink->sample_spec);
443
/* Make sure the buffer fits in the mempool tile */
444
block_size_max = pa_mempool_block_size_max(i->sink->core->mempool);
445
if (length > block_size_max)
446
length = pa_frame_align(block_size_max, &i->sink->sample_spec);
448
if (i->thread_info.move_silence > 0) {
451
/* We have just been moved and shall play some silence for a
452
* while until the old sink has drained its playback buffer */
454
if (!i->thread_info.silence_memblock)
455
i->thread_info.silence_memblock = pa_silence_memblock_new(
456
i->sink->core->mempool,
457
&i->sink->sample_spec,
458
pa_frame_align(SILENCE_BUFFER_LENGTH, &i->sink->sample_spec));
460
chunk->memblock = pa_memblock_ref(i->thread_info.silence_memblock);
462
l = pa_memblock_get_length(chunk->memblock);
463
chunk->length = i->thread_info.move_silence < l ? i->thread_info.move_silence : l;
466
do_volume_adj_here = 1;
470
if (!i->thread_info.resampler) {
471
do_volume_adj_here = 0; /* FIXME??? */
472
ret = i->peek(i, length, chunk);
503
slength = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sink->sample_spec);
505
if (slength > block_size_max_sink)
506
slength = block_size_max_sink;
508
if (i->thread_info.resampler) {
509
ilength = pa_resampler_request(i->thread_info.resampler, slength);
512
ilength = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sample_spec);
516
if (ilength > block_size_max_sink_input)
517
ilength = block_size_max_sink_input;
519
/* If the channel maps of the sink and this stream differ, we need
520
* to adjust the volume *before* we resample. Otherwise we can do
521
* it after and leave it for the sink code */
476
523
do_volume_adj_here = !pa_channel_map_equal(&i->channel_map, &i->sink->channel_map);
477
524
volume_is_norm = pa_cvolume_is_norm(&i->thread_info.volume) && !i->thread_info.muted;
479
while (!i->thread_info.resampled_chunk.memblock) {
526
while (!pa_memblockq_is_readable(i->thread_info.render_memblockq)) {
480
527
pa_memchunk tchunk;
483
l = pa_resampler_request(i->thread_info.resampler, length);
486
l = pa_frame_align(CONVERT_BUFFER_LENGTH, &i->sample_spec);
488
rmbs = pa_resampler_max_block_size(i->thread_info.resampler);
492
if ((ret = i->peek(i, l, &tchunk)) < 0)
529
/* There's nothing in our render queue. We need to fill it up
530
* with data from the implementor. */
532
if (i->thread_info.state == PA_SINK_INPUT_CORKED ||
533
i->pop(i, ilength, &tchunk) < 0) {
535
/* OK, we're corked or the implementor didn't give us any
536
* data, so let's just hand out silence */
537
pa_atomic_store(&i->thread_info.drained, 1);
539
pa_memblockq_seek(i->thread_info.render_memblockq, (int64_t) slength, PA_SEEK_RELATIVE);
540
i->thread_info.playing_for = 0;
541
if (i->thread_info.underrun_for != (uint64_t) -1)
542
i->thread_info.underrun_for += ilength;
546
pa_atomic_store(&i->thread_info.drained, 0);
495
548
pa_assert(tchunk.length > 0);
497
if (tchunk.length > l)
500
i->drop(i, tchunk.length);
502
/* It might be necessary to adjust the volume here */
503
if (do_volume_adj_here && !volume_is_norm) {
504
pa_memchunk_make_writable(&tchunk, 0);
506
if (i->thread_info.muted)
507
pa_silence_memchunk(&tchunk, &i->thread_info.sample_spec);
509
pa_volume_memchunk(&tchunk, &i->thread_info.sample_spec, &i->thread_info.volume);
549
pa_assert(tchunk.memblock);
551
i->thread_info.underrun_for = 0;
552
i->thread_info.playing_for += tchunk.length;
554
while (tchunk.length > 0) {
558
pa_memblock_ref(wchunk.memblock);
560
if (wchunk.length > block_size_max_sink_input)
561
wchunk.length = block_size_max_sink_input;
563
/* It might be necessary to adjust the volume here */
564
if (do_volume_adj_here && !volume_is_norm) {
565
pa_memchunk_make_writable(&wchunk, 0);
567
if (i->thread_info.muted)
568
pa_silence_memchunk(&wchunk, &i->thread_info.sample_spec);
570
pa_volume_memchunk(&wchunk, &i->thread_info.sample_spec, &i->thread_info.volume);
573
if (!i->thread_info.resampler)
574
pa_memblockq_push_align(i->thread_info.render_memblockq, &wchunk);
577
pa_resampler_run(i->thread_info.resampler, &wchunk, &rchunk);
579
/* pa_log_debug("pushing %lu", (unsigned long) rchunk.length); */
581
if (rchunk.memblock) {
582
pa_memblockq_push_align(i->thread_info.render_memblockq, &rchunk);
583
pa_memblock_unref(rchunk.memblock);
587
pa_memblock_unref(wchunk.memblock);
589
tchunk.index += wchunk.length;
590
tchunk.length -= wchunk.length;
512
pa_resampler_run(i->thread_info.resampler, &tchunk, &i->thread_info.resampled_chunk);
513
593
pa_memblock_unref(tchunk.memblock);
516
pa_assert(i->thread_info.resampled_chunk.memblock);
517
pa_assert(i->thread_info.resampled_chunk.length > 0);
519
*chunk = i->thread_info.resampled_chunk;
520
pa_memblock_ref(i->thread_info.resampled_chunk.memblock);
527
pa_atomic_store(&i->thread_info.drained, 0);
529
pa_atomic_store(&i->thread_info.drained, 1);
532
/* Let's see if we had to apply the volume adjustment
533
* ourselves, or if this can be done by the sink for us */
535
if (do_volume_adj_here)
536
/* We had different channel maps, so we already did the adjustment */
537
pa_cvolume_reset(volume, i->sink->sample_spec.channels);
538
else if (i->thread_info.muted)
539
/* We've both the same channel map, so let's have the sink do the adjustment for us*/
540
pa_cvolume_mute(volume, i->sink->sample_spec.channels);
542
*volume = i->thread_info.volume;
548
/* Called from thread context */
549
void pa_sink_input_drop(pa_sink_input *i, size_t length) {
550
pa_sink_input_assert_ref(i);
551
pa_assert(PA_SINK_INPUT_LINKED(i->thread_info.state));
552
pa_assert(pa_frame_aligned(length, &i->sink->sample_spec));
553
pa_assert(length > 0);
555
if (!i->peek || !i->drop || i->thread_info.state == PA_SINK_INPUT_CORKED)
558
if (i->thread_info.move_silence > 0) {
560
if (i->thread_info.move_silence >= length) {
561
i->thread_info.move_silence -= length;
564
length -= i->thread_info.move_silence;
565
i->thread_info.move_silence = 0;
568
if (i->thread_info.move_silence <= 0) {
569
if (i->thread_info.silence_memblock) {
570
pa_memblock_unref(i->thread_info.silence_memblock);
571
i->thread_info.silence_memblock = NULL;
579
if (i->thread_info.resampled_chunk.memblock) {
582
if (l > i->thread_info.resampled_chunk.length)
583
l = i->thread_info.resampled_chunk.length;
585
i->thread_info.resampled_chunk.index += l;
586
i->thread_info.resampled_chunk.length -= l;
588
if (i->thread_info.resampled_chunk.length <= 0) {
589
pa_memblock_unref(i->thread_info.resampled_chunk.memblock);
590
pa_memchunk_reset(&i->thread_info.resampled_chunk);
598
if (i->thread_info.resampler) {
599
/* So, we have a resampler. To avoid discontinuities we
600
* have to actually read all data that could be read and
601
* pass it through the resampler. */
607
if (pa_sink_input_peek(i, length, &chunk, &volume) >= 0) {
610
pa_memblock_unref(chunk.memblock);
616
pa_sink_input_drop(i, l);
622
l = pa_resampler_request(i->thread_info.resampler, length);
624
/* Hmmm, peeking failed, so let's at least drop
625
* the right amount of data */
637
/* We have no resampler, hence let's just drop the data */
596
pa_assert_se(pa_memblockq_peek(i->thread_info.render_memblockq, chunk) >= 0);
598
pa_assert(chunk->length > 0);
599
pa_assert(chunk->memblock);
601
/* pa_log_debug("peeking %lu", (unsigned long) chunk->length); */
603
if (chunk->length > block_size_max_sink)
604
chunk->length = block_size_max_sink;
606
/* Let's see if we had to apply the volume adjustment ourselves,
607
* or if this can be done by the sink for us */
609
if (do_volume_adj_here)
610
/* We had different channel maps, so we already did the adjustment */
611
pa_cvolume_reset(volume, i->sink->sample_spec.channels);
612
else if (i->thread_info.muted)
613
/* We've both the same channel map, so let's have the sink do the adjustment for us*/
614
pa_cvolume_mute(volume, i->sink->sample_spec.channels);
616
*volume = i->thread_info.volume;
621
/* Called from thread context */
622
void pa_sink_input_drop(pa_sink_input *i, size_t nbytes /* in sink sample spec */) {
623
pa_sink_input_assert_ref(i);
625
pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
626
pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
627
pa_assert(nbytes > 0);
629
/* pa_log_debug("dropping %lu", (unsigned long) nbytes); */
631
pa_memblockq_drop(i->thread_info.render_memblockq, nbytes);
634
/* Called from thread context */
635
void pa_sink_input_process_rewind(pa_sink_input *i, size_t nbytes /* in sink sample spec */) {
637
pa_bool_t called = FALSE;
638
pa_sink_input_assert_ref(i);
640
pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
641
pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
643
/* pa_log_debug("rewind(%lu, %lu)", (unsigned long) nbytes, (unsigned long) i->thread_info.rewrite_nbytes); */
645
lbq = pa_memblockq_get_length(i->thread_info.render_memblockq);
648
pa_log_debug("Have to rewind %lu bytes on render memblockq.", (unsigned long) nbytes);
649
pa_memblockq_rewind(i->thread_info.render_memblockq, nbytes);
652
if (i->thread_info.rewrite_nbytes == (size_t) -1) {
654
/* We were asked to drop all buffered data, and rerequest new
655
* data from implementor the next time push() is called */
657
pa_memblockq_flush_write(i->thread_info.render_memblockq);
659
} else if (i->thread_info.rewrite_nbytes > 0) {
660
size_t max_rewrite, amount;
662
/* Calculate how much make sense to rewrite at most */
663
max_rewrite = nbytes + lbq;
665
/* Transform into local domain */
666
if (i->thread_info.resampler)
667
max_rewrite = pa_resampler_request(i->thread_info.resampler, max_rewrite);
669
/* Calculate how much of the rewinded data should actually be rewritten */
670
amount = PA_MIN(i->thread_info.rewrite_nbytes, max_rewrite);
673
pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) amount);
675
/* Tell the implementor */
676
if (i->process_rewind)
677
i->process_rewind(i, amount);
680
/* Convert back to to sink domain */
681
if (i->thread_info.resampler)
682
amount = pa_resampler_result(i->thread_info.resampler, amount);
685
/* Ok, now update the write pointer */
686
pa_memblockq_seek(i->thread_info.render_memblockq, - ((int64_t) amount), PA_SEEK_RELATIVE);
688
if (i->thread_info.rewrite_flush)
689
pa_memblockq_silence(i->thread_info.render_memblockq);
691
/* And reset the resampler */
692
if (i->thread_info.resampler)
693
pa_resampler_reset(i->thread_info.resampler);
698
if (i->process_rewind)
699
i->process_rewind(i, 0);
701
i->thread_info.rewrite_nbytes = 0;
702
i->thread_info.rewrite_flush = FALSE;
705
/* Called from thread context */
706
void pa_sink_input_update_max_rewind(pa_sink_input *i, size_t nbytes /* in the sink's sample spec */) {
707
pa_sink_input_assert_ref(i);
708
pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
709
pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
711
pa_memblockq_set_maxrewind(i->thread_info.render_memblockq, nbytes);
713
if (i->update_max_rewind)
714
i->update_max_rewind(i, i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, nbytes) : nbytes);
717
/* Called from thread context */
718
void pa_sink_input_update_max_request(pa_sink_input *i, size_t nbytes /* in the sink's sample spec */) {
719
pa_sink_input_assert_ref(i);
720
pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
721
pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
723
if (i->update_max_request)
724
i->update_max_request(i, i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, nbytes) : nbytes);
727
/* Called from thread context */
728
static pa_usec_t fixup_latency(pa_sink *s, pa_usec_t usec) {
729
pa_sink_assert_ref(s);
731
if (usec == (pa_usec_t) -1)
734
if (s->thread_info.max_latency > 0 && usec > s->thread_info.max_latency)
735
usec = s->thread_info.max_latency;
737
if (s->thread_info.min_latency > 0 && usec < s->thread_info.min_latency)
738
usec = s->thread_info.min_latency;
743
/* Called from thread context */
744
pa_usec_t pa_sink_input_set_requested_latency_within_thread(pa_sink_input *i, pa_usec_t usec) {
745
pa_sink_input_assert_ref(i);
747
usec = fixup_latency(i->sink, usec);
748
i->thread_info.requested_sink_latency = usec;
749
pa_sink_invalidate_requested_latency(i->sink);
754
/* Called from main context */
755
pa_usec_t pa_sink_input_set_requested_latency(pa_sink_input *i, pa_usec_t usec) {
756
pa_sink_input_assert_ref(i);
758
if (PA_SINK_INPUT_IS_LINKED(i->state))
759
pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
761
/* If this sink input is not realized yet, we have to touch
762
* the thread info data directly */
764
i->thread_info.requested_sink_latency = usec;
769
/* Called from main context */
770
pa_usec_t pa_sink_input_get_requested_latency(pa_sink_input *i) {
773
pa_sink_input_assert_ref(i);
775
if (PA_SINK_INPUT_IS_LINKED(i->state))
776
pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
778
/* If this sink input is not realized yet, we have to touch
779
* the thread info data directly */
780
usec = i->thread_info.requested_sink_latency;
785
/* Called from main context */
645
786
void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume) {
646
787
pa_sink_input_assert_ref(i);
647
pa_assert(PA_SINK_INPUT_LINKED(i->state));
788
pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
649
791
if (pa_cvolume_equal(&i->volume, volume))
652
794
i->volume = *volume;
654
pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), 0, NULL, pa_xfree);
796
pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_VOLUME, &i->volume, 0, NULL) == 0);
655
797
pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
800
/* Called from main context */
658
801
const pa_cvolume *pa_sink_input_get_volume(pa_sink_input *i) {
659
802
pa_sink_input_assert_ref(i);
660
pa_assert(PA_SINK_INPUT_LINKED(i->state));
803
pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
662
805
return &i->volume;
808
/* Called from main context */
665
809
void pa_sink_input_set_mute(pa_sink_input *i, pa_bool_t mute) {
667
811
pa_sink_input_assert_ref(i);
668
pa_assert(PA_SINK_INPUT_LINKED(i->state));
812
pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
670
814
if (!i->muted == !mute)