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 <[email protected]> --- A hardware-free reproducer follows in a reply to this patch. It uses GNU ld wrappers and is supplied separately from FATE. libavdevice/alsa_dec.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libavdevice/alsa_dec.c b/libavdevice/alsa_dec.c index 7411fcc..78ced66 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; } base-commit: 3e6cd5e7603cb81b028b9c17530697b8986bf69a -- 2.54.0 (Apple Git-157) _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
