Hi,
Here is a standalone regression test for the ALSA capture timestamp fix.
It calls the built demuxer's read_packet entry point and the FFmpeg time
filter. GNU ld wrappers supply PCM read results and wall-clock readings;
no sound device is required. Each frame carries a sequence marker, so the
test also checks sample order across partial reads and EAGAIN.
On the parent commit, full reads and the prepare-error case pass, while
short-read cases fail their PTS and period checks. With the patch, all six
cases pass at 44.1, 48, 96 and 192 kHz. The EPIPE/EIO warnings are expected
from recovery tests.
Build static FFmpeg with ALSA enabled (Linux, ALSA development headers,
GCC and GNU ld):
mkdir build
cd build
/path/to/ffmpeg/configure --disable-everything --disable-doc \
--disable-autodetect --enable-alsa --enable-indev=alsa
make -j2
Save the two files below together, then run:
sh run-regression.sh /path/to/ffmpeg /path/to/build
I also reproduced actual short returns using a QEMU virtio-sound PCM at
48 kHz with a 7680-frame period. After priming it by reading 3840 frames,
a nonblocking read returned 3840, followed by EAGAIN until more data was
available. A blocking read interrupted by SIGALRM returned the same short
count. No return values were replaced in those device tests.
Across three runs of each mode, the first packet's error was about 80 ms
before the patch. After the patch it matched the full packet duration,
within observation overhead. This was silent virtual input; physical
microphone testing and field-device frequency remain unverified.
Thanks,
Joonho
Save as packet-regression.c:
/* Local regression harness; exercises the linked, unmodified demuxer
entrypoint.
* GNU ld wrappers replace only ALSA reads/prepare/delay and the wall clock.
* No physical PCM or copied implementation of audio_read_packet is involved. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include "libavdevice/alsa.h"
#include "libavformat/demux.h"
#include "libavutil/mem.h"
#include "libavutil/time.h"
#ifndef TEST_RATE
#define TEST_RATE 48000
#endif
#define FRAMES (TEST_RATE * 16 / 100)
extern const FFInputFormat ff_alsa_demuxer;
static const int *sequence;
static int sequence_len, sequence_pos, prep_count, prep_error, sample_index;
static int failures, checks;
static int64_t now;
static snd_pcm_sframes_t delay_frames;
#define CHECK(c) do { checks++; if (!(c)) { failures++; \
fprintf(stderr,"FAIL line=%d expr=%s\n",__LINE__,#c); } } while(0)
snd_pcm_sframes_t __wrap_snd_pcm_readi(snd_pcm_t *h, void *dst,
snd_pcm_uframes_t requested)
{
snd_pcm_sframes_t n = sequence_pos < sequence_len ?
sequence[sequence_pos++] : (snd_pcm_sframes_t)requested;
(void)h;
if(n < 0) return n;
if(n > (snd_pcm_sframes_t)requested) {fprintf(stderr,"invalid
fixture\n");exit(99);}
for(int i=0;i<n;i++) ((int32_t *)dst)[i]=sample_index++;
return n;
}
int __wrap_snd_pcm_prepare(snd_pcm_t *h) { (void)h;prep_count++; return
prep_error; }
int __wrap_snd_pcm_delay(snd_pcm_t *h,snd_pcm_sframes_t *delay)
{ (void)h; *delay=delay_frames;return 0; }
int64_t __wrap_av_gettime(void) { return now; }
static AVFormatContext *setup(void)
{
AVFormatContext *ctx=avformat_alloc_context();
AlsaData *s=av_mallocz(sizeof(*s));
if(!ctx || !s) exit(99);
ctx->iformat=&ff_alsa_demuxer.p; ctx->priv_data=s;
s->class=(AVClass *)ff_alsa_demuxer.p.priv_class;
s->frame_size=4; s->period_size=FRAMES; s->sample_rate=TEST_RATE;
s->pkt=av_packet_alloc();
s->timefilter=ff_timefilter_new(1000000.0/TEST_RATE,FRAMES,1.5E-6);
if(!s->pkt || !s->timefilter) exit(99);
sequence=NULL;sequence_pos=sequence_len=prep_count=prep_error=sample_index=0;
now=1160000;delay_frames=0;return ctx;
}
static void cleanup(AVFormatContext *ctx)
{
AlsaData *s=ctx->priv_data;
av_packet_free(&s->pkt); ff_timefilter_destroy(s->timefilter);
av_freep(&ctx->priv_data);avformat_free_context(ctx);
}
static void feed(const int *seq,int len)
{sequence=seq;sequence_len=len;sequence_pos=0;}
static void packet(AVFormatContext *ctx,int64_t pts,int first_sample)
{
AVPacket *pkt=av_packet_alloc();
int ret=ff_alsa_demuxer.read_packet(ctx,pkt);
CHECK(ret==0); CHECK(pkt->size==FRAMES*4);
CHECK(llabs(pkt->pts-pts)<=1);
CHECK(((AlsaData *)ctx->priv_data)->last_period==FRAMES);
if(ret==0 && pkt->size==FRAMES*4)
for(int i=0;i<FRAMES;i++) CHECK(((int32_t
*)pkt->data)[i]==first_sample+i);
av_packet_free(&pkt);
}
static void run_shape(const char *name,const int *seq,int len,int delay)
{
AVFormatContext *ctx=setup();
int before=failures;
delay_frames=delay;
for(int i=0;i<32;i++) {
feed(seq,len);
now=1160000+(int64_t)i*160000+delay*1000000LL/TEST_RATE;
packet(ctx,1000000+(int64_t)i*160000,i*FRAMES);
}
CHECK(prep_count==0);cleanup(ctx);
printf("CASE %s %s\n",name,failures==before?"PASS":"FAIL");
}
int main(void)
{
const int full[]={FRAMES},half[]={(FRAMES / 2),(FRAMES /
2)},uneven[]={1,127,(FRAMES - 128)};
run_shape("full",full,1,0);
run_shape("halves",half,2,0);
run_shape("uneven-with-delay",uneven,3,(TEST_RATE / 50));
AVFormatContext *ctx=setup();AVPacket *pkt=av_packet_alloc();int
before=failures;
const int again[]={2000,-EAGAIN,-EAGAIN,(FRAMES - 2000)};feed(again,4);
CHECK(ff_alsa_demuxer.read_packet(ctx,pkt)==AVERROR(EAGAIN));
CHECK(((AlsaData *)ctx->priv_data)->pkt->size==8000);
CHECK(ff_alsa_demuxer.read_packet(ctx,pkt)==AVERROR(EAGAIN));
packet(ctx,1000000,0);CHECK(prep_count==0);cleanup(ctx);
printf("CASE partial-eagain %s\n",failures==before?"PASS":"FAIL");
ctx=setup();before=failures;packet(ctx,1000000,0);
for(int i=1;i<=3;i++) {
const int xrun[]={1000,-EPIPE,(FRAMES / 2),(FRAMES / 2)};feed(xrun,4);
now=1160000+(int64_t)i*3160000;
packet(ctx,1000000+(int64_t)i*3160000,FRAMES+(i-1)*(FRAMES +
1000)+1000);
CHECK(prep_count==i);
}
now+=160000;packet(ctx,10640000,sample_index);
cleanup(ctx);printf("CASE repeated-partial-xrun
%s\n",failures==before?"PASS":"FAIL");
ctx=setup();before=failures;
const int fail[]={-EPIPE};feed(fail,1);prep_error=-EIO;
CHECK(ff_alsa_demuxer.read_packet(ctx,pkt)==AVERROR(EIO));CHECK(prep_count==1);
cleanup(ctx);printf("CASE recovery-error
%s\n",failures==before?"PASS":"FAIL");
av_packet_free(&pkt);
printf("CHECKS=%d FAILURES=%d\n",checks,failures);
return failures?1:0;
}
Save as run-regression.sh:
#!/bin/sh
set -eu
if [ "$#" -ne 2 ]; then echo "Usage: $0 FFMPEG_SOURCE FFMPEG_BUILD" >&2; exit
2; fi
source_dir=$(cd "$1" && pwd)
build_dir=$(cd "$2" && pwd)
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
work_dir=$(mktemp -d)
trap 'rm -rf "$work_dir"' EXIT HUP INT TERM
for rate in 44100 48000 96000 192000; do
"${CC:-cc}" -Wall -Wextra -Werror -DTEST_RATE="$rate" -I"$build_dir"
-I"$source_dir" \
"$script_dir/packet-regression.c" -o "$work_dir/packet-test" \
-L"$build_dir/libavdevice" -L"$build_dir/libavfilter"
-L"$build_dir/libavformat" \
-L"$build_dir/libavcodec" -L"$build_dir/libswresample"
-L"$build_dir/libswscale" -L"$build_dir/libavutil" \
-Wl,--wrap=snd_pcm_readi,--wrap=snd_pcm_prepare,--wrap=snd_pcm_delay,--wrap=av_gettime
\
-lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavutil
-lasound -lm -lpthread
echo "RATE=$rate"
"$work_dir/packet-test"
done
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]