Commit 46f23be8b1 for ffmpeg
commit 46f23be8b1976ed4c8ec85584daabcf0e57a7b9e
Author: whatisyourname0 <mynameisjune111@gmail.com>
Date: Sat Oct 17 04:17:44 2026 +0000
fix(avdevice/alsa): timestamp the complete capture packet after short reads
ALSA capture can assemble a packet from multiple successful reads, possibly
across EAGAIN returns. The timestamp calculation still uses the frame count
from the final read, so it subtracts too little audio time. The same count
also supplies the wrong interval to the next time-filter update.
Use the completed packet's frame count for both calculations. A 7680-frame
packet assembled from two 3840-frame reads at 48 kHz otherwise receives a
first PTS 80 ms too late. Single-read packets retain the same calculation.
Reproduced with real ALSA short returns under ARM64 QEMU: a capture PCM
was primed by half a period, then read in nonblocking mode or interrupted
by SIGALRM while waiting for the remainder. No read return values were
changed in those tests. The fix also passes deterministic sample-order,
PTS, EAGAIN and XRUN recovery checks at 44.1, 48, 96 and 192 kHz.
Signed-off-by: Joonho Lee <mynameisjune111@gmail.com>
diff --git a/libavdevice/alsa_dec.c b/libavdevice/alsa_dec.c
index 7411fcc5fb..78ced66de9 100644
--- a/libavdevice/alsa_dec.c
+++ b/libavdevice/alsa_dec.c
@@ -106,7 +106,7 @@ fail:
static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
{
AlsaData *s = s1->priv_data;
- int res;
+ int res, frames;
int64_t dts;
snd_pcm_sframes_t delay = 0;
@@ -134,11 +134,13 @@ static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
} while (s->pkt->size < s->period_size * s->frame_size);
av_packet_move_ref(pkt, s->pkt);
+ /* A packet may have been assembled from multiple short reads. */
+ frames = pkt->size / s->frame_size;
dts = av_gettime();
snd_pcm_delay(s->h, &delay);
- dts -= av_rescale(delay + res, 1000000, s->sample_rate);
+ dts -= av_rescale(delay + frames, 1000000, s->sample_rate);
pkt->pts = ff_timefilter_update(s->timefilter, dts, s->last_period);
- s->last_period = res;
+ s->last_period = frames;
return 0;
}