今天有個為 FFmpeg 貢獻高質量 patch 的朋友問到了一個問題,如何編寫一個 FATE 測試用例,當時簡單地回復了操作步驟,那麼接下來我將會在這裡詳細介紹一下如何編寫 FFmpeg 的自動化測試用例。
一. 測試用例結果參考數據說明
首先看一下測試用例參考數據內容大概的展現形態
bogon:ffmpeg_up liuqi$ cat tests/ref/fate/hls-segment-size#tb 0: 1/44100#media_type 0: audio#codec_id 0: pcm_s16le#sample_rate 0: 44100#channel_layout 0: 4#channel_layout_name 0: mono0, 0, 0, 1152, 2304, 0x907cb7fa0, 1152, 1152, 1152, 2304, 0xb8dc75250, 2304, 2304, 1152, 2304, 0x3e7d69050, 3456, 3456, 1152, 2304, 0xef47877b0, 4608, 4608, 1152, 2304, 0xfe916b7e0, 5760, 5760, 1152, 2304, 0xe3d08cde0, 6912, 6912, 1152, 2304, 0xff7f86cf0, 8064, 8064, 1152, 2304, 0x843e6f950, 9216, 9216, 1152, 2304, 0x81577c260, 10368, 10368, 1152, 2304, 0x04a085d50, 11520, 11520, 1152, 2304, 0x1c5a76f50, 12672, 12672, 1152, 2304, 0x4ee786230, 13824, 13824, 1152, 2304, 0x8ec861dc0, 14976, 14976, 1152, 2304, 0x0ca179d80, 16128, 16128, 1152, 2304, 0xc6da750f0, 17280, 17280, 1152, 2304, 0xf6bf79b50, 18432, 18432, 1152, 2304, 0x97b88a430, 19584, 19584, 1152, 2304, 0xf13c7b9c0, 20736, 20736, 1152, 2304, 0xdfba83af0, 21888, 21888, 1152, 2304, 0xc9467d4b0, 23040, 23040, 1152, 2304, 0xbbb58e2b0, 24192, 24192, 1152, 2304, 0x3a1078ea0, 25344, 25344, 1152, 2304, 0xe9587a5c此後省略好幾百行0, 880128, 880128, 1152, 2304, 0x3cb185300, 881280, 881280, 1152, 2304, 0x5a0c5e7bbogon:ffmpeg_up liuqi$從內容上看,這是一個音頻數據的測試文件,timebase是1/44100,採樣率是44100,媒體類型是音頻,音頻格式是pcm_s16le,音頻通道布局是mono。然後下面就是每一幀音頻數據的輸出信息,一共 6 列,分別代表,
當前 packet 的流所在的索引就是 packet->stream_index
當前 packet 的 dts
當前 packet 的 pts
當前 packet 的 duration
當前 packet 的數據包大小
當前數據包的 crc 值
二. 實際操作
那麼這一堆數據是怎麼生成的呢?下面從FATE中找到一個樣例看一下
tests/data/hls_segment_size.m3u8: TAG = GENtests/data/hls_segment_size.m3u8: ffmpeg$(PROGSSUF)$(EXESUF) | tests/data $(M)$(TARGET_EXEC) $(TARGET_PATH)/$< \ -f lavfi -i "aevalsrc=cos(2*PI*t)*sin(2*PI*(440+4*t)*t):d=20" -f hls -hls_segment_size 300000 -map 0 \ -hls_list_size 0 -codec:a mp2fixed -hls_segment_filename $(TARGET_PATH)/tests/data/hls_segment_size_%d.ts \ $(TARGET_PATH)/tests/data/hls_segment_size.m3u8 2>/dev/null
FATE_AFILTER-$(call ALLYES, HLS_DEMUXER MPEGTS_MUXER MPEGTS_DEMUXER AEVALSRC_FILTER LAVFI_INDEV MP2FIXED_ENCODER) += fate-hls-segment-sizefate-hls-segment-size: tests/data/hls_segment_size.m3u8fate-hls-segment-size: CMD = framecrc -flags +bitexact -i $(TARGET_PATH)/tests/data/hls_segment_size.m3u8 -vf setpts=N*23從例子中可以看到,首先是生成一個m3u8列表,也就是這一段
tests/data/hls_segment_size.m3u8: TAG = GENtests/data/hls_segment_size.m3u8: ffmpeg$(PROGSSUF)$(EXESUF) | tests/data $(M)$(TARGET_EXEC) $(TARGET_PATH)/$< \ -f lavfi -i "aevalsrc=cos(2*PI*t)*sin(2*PI*(440+4*t)*t):d=20" -f hls -hls_segment_size 300000 -map 0 \ -hls_list_size 0 -codec:a mp2fixed -hls_segment_filename $(TARGET_PATH)/tests/data/hls_segment_size_%d.ts \ $(TARGET_PATH)/tests/data/hls_segment_size.m3u8這一段最終執行時也就是這樣的
/Users/liuqi/project/ffmpeg_up/dash/ffmpeg \ -f lavfi -i "aevalsrc=cos(2*PI*t)*sin(2*PI*(440+4*t)*t):d=20" -f hls -hls_segment_size 300000 -map 0 \ -hls_list_size 0 -codec:a mp2fixed -hls_segment_filename /Users/liuqi/project/ffmpeg_up/dash/tests/data/hls_segment_size_%d.ts \ /Users/liuqi/project/ffmpeg_up/dash/tests/data/hls_segment_size.m3u8 2>/dev/null然後是下面那一段生成framecrc數據的操作
FATE_AFILTER-$(call ALLYES, HLS_DEMUXER MPEGTS_MUXER MPEGTS_DEMUXER AEVALSRC_FILTER LAVFI_INDEV MP2FIXED_ENCODER) += fate-hls-segment-sizefate-hls-segment-size: tests/data/hls_segment_size.m3u8fate-hls-segment-size: CMD = framecrc -flags +bitexact -i $(TARGET_PATH)/tests/data/hls_segment_size.m3u8 -vf setpts=N*23內容也比較好理解,首先是執行一句tests/data/hls_segment_size.m3u8,在make fate-hls-segment-size的時候首先會執行tests/data/hls_segment_size.m3u8,然後會執行這一句
CMD = framecrc -flags +bitexact -i $(TARGET_PATH)/tests/data/hls_segment_size.m3u8 -vf setpts=N*23這裡的framecrc是一個shell封裝函數,這個函數是封裝在tests/fate-run.sh文件中
124 ffmpeg(){125 dec_opts="-hwaccel $hwaccel -threads $threads -thread_type $thread_type"126 ffmpeg_args="-nostdin -nostats -cpuflags $cpuflags"127 for arg in $@; do128 [ x${arg} = x-i ] && ffmpeg_args="${ffmpeg_args} ${dec_opts}"129 ffmpeg_args="${ffmpeg_args} ${arg}"130 done131 run ffmpeg${PROGSUF}${EXECSUF} ${ffmpeg_args}132 }133134 framecrc(){135 ffmpeg "$@" -bitexact -f framecrc -136 }137138 ffmetadata(){139 ffmpeg "$@" -bitexact -f ffmetadata -140 }141142 framemd5(){143 ffmpeg "$@" -bitexact -f framemd5 -144 }145146 crc(){147 ffmpeg "$@" -f crc -148 }149150 md5pipe(){151 ffmpeg "$@" md5:152 }153154 md5(){155 encfile="${outdir}/${test}.out"156 cleanfiles="$cleanfiles $encfile"157 ffmpeg "$@" $encfile158 do_md5sum $encfile | awk '{print $1}'159 }最後拼出來的fate執行的效果大概是這樣的
/Users/liuqi/project/ffmpeg_up/dash/ffmpeg -nostdin -nostats -cpuflags all -flags +bitexact -hwaccel none -threads 1 -thread_type frame+slice -i /Users/liuqi/project/ffmpeg_up/dash/tests/data/hls_segment_size.m3u8 -vf setpts=N*23 -bitexact -f framecrc -然後將最後一個字符'-'替換成自己的文件名即可,那麼這裡可以替換成tests/ref/fate/hls-segment-size,也就基本上大功告成了。
然後剩下的FATE相關信息,可以參考FFmpeg的官方文檔:
FFmpeg Automated Testing Environment
Table of Contents1 Introduction2 Using FATE from your FFmpeg source directory3 Submitting the results to the FFmpeg result aggregation server4 Uploading new samples to the fate suite5 FATE makefile targets and variables5.1 Makefile targets5.2 Makefile variables5.3 Examples1 IntroductionFATE is an extended regression suite on the client-side and a means for results aggregation and presentation on the server-side.
The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary. The second part describes how you can run FATE to submit the results to FFmpeg’s FATE server.
In any way you can have a look at the publicly viewable FATE results by visiting this website:
http://fate.ffmpeg.org/
This is especially recommended for all people contributing source code to FFmpeg, as it can be seen if some test on some platform broke with their recent contribution. This usually happens on the platforms the developers could not test on.
The second part of this document describes how you can run FATE to submit your results to FFmpeg’s FATE server. If you want to submit your results be sure to check that your combination of CPU, OS and compiler is not already listed on the above mentioned website.
In the third part you can find a comprehensive listing of FATE makefile targets and variables.
2 Using FATE from your FFmpeg source directoryIf you want to run FATE on your machine you need to have the samples in place. You can get the samples via the build target fate-rsync. Use this command from the top-level source directory:
make fate-rsync SAMPLES=fate-suite/make fate SAMPLES=fate-suite/The above commands set the samples location by passing a makefile variable via command line. It is also possible to set the samples location at source configuration time by invoking configure with
./configure make fate-rsyncmake fateYet another way to tell FATE about the location of the sample directory is by making sure the environment variable FATE_SAMPLES contains the path to your samples directory. This can be achieved by e.g. putting that variable in your shell profile or by setting it in your interactive session.
FATE_SAMPLES=fate-suite/ make fateDo not put a 』~』 character in the samples path to indicate a home directory. Because of shell nuances, this will cause FATE to fail.
To use a custom wrapper to run the test, pass
3 Submitting the results to the FFmpeg result aggregation serverTo submit your results to the server you should run fate through the shell script tests/fate.sh from the FFmpeg sources. This script needs to be invoked with a configuration file as its first argument.
tests/fate.sh /path/to/fate_configA configuration file template with comments describing the individual configuration variables can be found at doc/fate_config.sh.template.
The mentioned configuration template is also available here:
slot= repo=git://source.ffmpeg.org/ffmpeg.git samples= workdir= comment= build_only= ignore_tests=
arch=cpu=cross_prefix=as=cc=ld=target_os=sysroot=target_exec=target_path=target_samples=extra_cflags=extra_ldflags=extra_libs=extra_conf=
makeopts= Create a configuration that suits your needs, based on the configuration template. The slot configuration variable can be any string that is not yet used, but it is suggested that you name it adhering to the following pattern 『arch-os-compiler-compiler version』. The configuration file itself will be sourced in a shell script, therefore all shell features may be used. This enables you to setup the environment as you need it for your build.
For your first test runs the fate_recv variable should be empty or commented out. This will run everything as normal except that it will omit the submission of the results to the server. The following files should be present in $workdir as specified in the configuration file:
configure.logcompile.logtest.logreportversionWhen you have everything working properly you can create an SSH key pair and send the public key to the FATE server administrator who can be contacted at the email address fate-admin@ffmpeg.org.
Configure your SSH client to use public key authentication with that key when connecting to the FATE server. Also do not forget to check the identity of the server and to accept its host key. This can usually be achieved by running your SSH client manually and killing it after you accepted the key. The FATE server’s fingerprint is:
『RSA』d3:f1:83:97:a4:75:2b:a6:fb:d6:e8:aa:81:93:97:51
『ECDSA』76:9f:68:32:04:1e:d5:d4:ec:47:3f:dc:fc:18:17:86
If you have problems connecting to the FATE server, it may help to try out the ssh command with one or more -v options. You should get detailed output concerning your SSH configuration and the authentication process.
The only thing left is to automate the execution of the fate.sh script and the synchronisation of the samples directory.
4 Uploading new samples to the fate suiteIf you need a sample uploaded send a mail to samples-request.
This is for developers who have an account on the fate suite server. If you upload new samples, please make sure they are as small as possible, space on each client, network bandwidth and so on benefit from smaller test cases. Also keep in mind older checkouts use existing sample files, that means in practice generally do not replace, remove or overwrite files as it likely would break older checkouts or releases. Also all needed samples for a commit should be uploaded, ideally 24 hours, before the push. If you need an account for frequently uploading samples or you wish to help others by doing that send a mail to ffmpeg-devel.
rsync -vauL
rsync -vanL
rsync -vaL 5 FATE makefile targets and variables5.1 Makefile targetsfate-rsyncDownload/synchronize sample files to the configured samples directory.
fate-listWill list all fate/regression test targets.
fateRun the FATE test suite (requires the fate-suite dataset).
5.2 Makefile variablesVVerbosity level, can be set to 0, 1 or 2.
0: show just the test arguments1: show just the command used in the test2: show everythingSAMPLESSpecify or override the path to the FATE samples at make time, it has a meaning only while running the regression tests.
THREADSSpecify how many threads to use while running regression tests, it is quite useful to detect thread-related regressions.
THREAD_TYPESpecify which threading strategy test, either 『slice』 or 『frame』, by default 『slice+frame』
CPUFLAGSSpecify CPU flags.
TARGET_EXECSpecify or override the wrapper used to run the tests. The TARGET_EXEC option provides a way to run FATE wrapped in valgrind, qemu-user or wine or on remote targets through ssh.
GENSet to 『1』 to generate the missing or mismatched references.
HWACCELSpecify which hardware acceleration to use while running regression tests, by default 『none』 is used.
KEEPSet to 『1』 to keep temp files generated by fate test(s) when test is successful. Default is 『0』, which removes these files. Files are always kept when a test fails.
5.3 Examplesmake V=1 SAMPLES=/var/fate/samples THREADS=2 CPUFLAGS=mmx fate三 結語
其實要看過程的話,make 自己的tag就可以了,例如這樣
bogon:dash liuqi$ make fate-hls-segment-size V=1
/Users/liuqi/project/ffmpeg_up/dash/ffmpeg \ -f lavfi -i "aevalsrc=cos(2*PI*t)*sin(2*PI*(440+4*t)*t):d=20" -f hls -hls_segment_size 300000 -map 0 \ -hls_list_size 0 -codec:a mp2fixed -hls_segment_filename /Users/liuqi/project/ffmpeg_up/dash/tests/data/hls_segment_size_%d.ts \ /Users/liuqi/project/ffmpeg_up/dash/tests/data/hls_segment_size.m3u8 2>/dev/nullTEST hls-segment-sizesrc/tests/fate-run.sh fate-hls-segment-size "" "" "/Users/liuqi/project/ffmpeg_up/dash" 'framecrc -flags +bitexact -i /Users/liuqi/project/ffmpeg_up/dash/tests/data/hls_segment_size.m3u8 -vf setpts=N*23' '' '' '' '1' '' '' '' '' '' '' '' '' '' '' /Users/liuqi/project/ffmpeg_up/dash/ffmpeg -nostdin -nostats -cpuflags all -flags +bitexact -hwaccel none -threads 1 -thread_type frame+slice -i /Users/liuqi/project/ffmpeg_up/dash/tests/data/hls_segment_size.m3u8 -vf setpts=N*23 -bitexact -f framecrc -bogon:dash liuqi$讀這篇內容需要一些基礎:
Makefile 基本原理和用法
bash 腳本基本用法