GStreamer for Ethernet AVB

https://iamkate.com/code/binary-file-viewer/
apt-get install manpages-posix-dev

1 AVB MPEG2-TS
1.1 MPEG2-TS
cip_with_sph_payload: 32-byte AVTP header + 4-byte Timestamp + 4-byte MPEG2TS header + 184-byte payload
32 bytes hdr, IEEE 1722-2016 spec
00 uint32_t subtype_data;
04 uint64_t stream_id;
12 uint32_t avtp_time;
16 uint32_t format_specific;
20 uint32_t packet_info; // big-endian MSB 16bit stream_data_length from cip_1
24 uint32_t cip_1;
28 uint32_t cip_2;
32 uint8_t cip_data_payload[0];
CIP means Common Isochronous Packet.

4 bytes MPEG2TS header
PID: offset-1 LSB 5bits + offset-2
SeqId: offset-3 LSB 4bits
PAT: Program Association Table, PID = 0
PMT: Program Map Table, PID = 0x130
CAT: Conditional Access Table

Note: CMMB uses H264 video format. Siano CMMB IC integrated tuner + demodulator, did not include H264 decoder.

1.2 MPEG2-TS File Name Extension
.ts, .mpg or .mpeg, PotPlayer 64 bit or VLC can play it.

2 gst-launch h264
gst-launch命令创建并运行一个pipe,组件之间的管道用“!”作为分隔符,为了跟shell的管道分隔符区分开没有用“|”。

2.1 mp4 to h264 conversion
ffmpeg -i C:\path\to\test.mp4 \
-an -vcodec copy -bsf:v \
h264_mp4toannexb \
C:\path\to\output.h264
其中bsf是Bit Stream Filter的缩写。

gst-launch-1.0 \
filesrc location=C:\\path\\to\\output.h264 ! \
h264parse ! avdec_h264 ! autovideosink
利用了filesrc、h264parse、avdec_h264和autovideosink四个element,其中设置了filesrc的location属性值为文件路径,h264parse的作用是从h264文件中识别出Start_Code_Prefix(0x000001或0x00000001)。
h264parse: gstreamer1.0-plugins-bad
avdec_h264: gstreamer1.0-libav

2.2 cvf format
P1722/D14: Draft 14, August 2015
P1722/D16: Draft 16, Nov 2015
1722-2016: Approved 7 December 2016

28 bytes hdr, IEEE 1722-2016 spec
00 uint32_t subtype_data;
04 uint64_t stream_id; // Stream ID = MAC + Unique ID
12 uint32_t avtp_timestamp; 
16 uint32_t format_specific;
20 uint32_t packet_info; // big-endian MSB 16bit stream_data_length from h264_timestamp
24 uint32_t h264_timestamp;

h264_timestamp:AVTP Presentation Time的作用是DTS(Decoding Time Stamp),在非交叉模式(Non-interleaved mode)下,是可以正常工作的;但是在交叉模式(Interleaved mode)下,由于解码顺序和显示顺序不一致,虽然能按正确的顺序解码,但是不能按正确的顺序显示。为了解决这个问题,才加上了h264_timestamp,它也是遵循RFC 6184规范的,它充当的是PTS(Presentation Time Stamp)的角色,用以指示正确的显示顺序。在非交叉模式下,该值可填充也可不填充。

H264 File Name Extension: .h264, PotPlayer 64 bit or VLC can play it
H265 File Name Extension: .h265, PotPlayer 64 bit or VLC can play it

3 gst-launch wav
gst-launch命令创建并运行一个pipe,组件之间的管道用“!”作为分隔符,为了跟shell的管道分隔符区分开没有用“|”。

3.1 mp4 to wav conversion
ffmpeg -i C:\path\to\test.mp4 \
-ar 48000 -ac 2 -f wav \
C:\path\to\output1.wav

ffprobe output.wav

gst-launch-1.0 \
filesrc location=C:\\path\\to\\output1.wav ! \
wavparse ! autoaudiosink

3.2 mp3 to wav conversion
ffmpeg -i C:\path\to\test.mp3 \
-ar 48000 -ac 2 -acodec pcm_s16le \
C:\path\to\output.wav

3.3 pcm to wav conversion
ffmpeg -i C:\path\to\test.pcm \
-ar 48000 -ac 2 -f s16be \
C:\path\to\output.wav

3.4 sample rate conversion
ffmpeg -i in_48k.wav \
-ar 24000 -ac 2 -acodec pcm_s16le \
out_24k.wav

3.5 crf format
20 bytes hdr, IEEE 1722-2016 spec
00 uint32_t subtype_data;
04 uint64_t stream_id;
12 uint64_t packet_info;
20 uint64_t crf_data[0];

Tcrf = Ts + (MTT/p)*p + Tc
Ts:采样时间点
MTT: Maximum Transit Time, also known as Presentation Time Offset, Class C is 15ms from AutoCDS v1.4
Tc:buffer里面的个数乘以采样周期

3.6 aaf format
24 bytes hdr, IEEE 1722-2016 spec
00 uint32_t subtype_data;
04 uint64_t stream_id; // Stream ID = MAC + Unique ID
12 uint32_t avtp_time; // CRF + MTT, MTT = 2ms
16 uint32_t format_specific;
20 uint32_t packet_info;

4 14nm iMX 8M Plus EVK
FEC: 0x30be_0000
DWMAC_v5.10a: 0x30bf_0000
USB0: 0x3810_0000
USB1: 0x3820_0000
Refer to Getting Started with AVB on Linux

1) Ubuntu Desktop 20.04.2.0
2) meson
github meson 0.59.0.rc1
cd meson
python3 setup.py build
python3 setup.py install
dep_threads = dependency('threads')
dependencies : [dep_threads,
    libgl_dep],
3) 3rd libraries
github libavtp:编译gst-plugins-bad前需要先编译安装github libavtp,否则gst-plugins-bad不编译avtp plugin。
libglib2.0-dev: apt install libglib2.0-dev
h264parse: gstreamer1.0-plugins-bad
avdec_h264: gstreamer1.0-libav
autoaudiosink: apt install libalsa-ocaml-dev, gst-plugins-base
autovideosink: apt install libx11-dev, ximagesink, gst-plugins-base
pango: for clockoverlay, gst-plugins-base, never build and install it
cairo: never update cairo, otherwise GTK apps cannot boot.
4) gstreamer build
gstreamer, gst-plugins-base, gst-plugins-bad: build
gst-plugins-good, gst-plugins-ugly, gst-libav: after install them, autoaudiosink and autovideosink may not work, refer to 3)
5) check avtp
gst-inspect-1.0 avtp
6) vlan
ip link add link eth0 name eth0.5 type vlan id 5 \
    egress-qos-map 2:2 3:3
ip link set eth0.5 up
7) GST_DEBUG
copy libavtp calculate_avtp_time(&tstamp, 5) to gst-plugins-bad avtp plugin to generate local avtp time for gst-launch-1.0 test, argument 5 means MTT (max_transit_time).
streamid的前48个字节是avb的组播地址,后16位根据需求制定。
GST_DEBUG=avtpsrc:6 gst-launch-1.0 xxx

sudo ./aaf-listener -d <mac> -i <eth0.N> | \
gst-launch-1.0 filesrc location=/dev/stdin ! \
rawaudioparse use-sink-caps=false \
format=pcm pcm-format=s16be \
sample-rate=48000 num-channels=2 ! \
audioconvert ! audioresample ! autoaudiosink

sudo ./cvf-listener -d <mac> -i <eth0.N> | \
gst-launch-1.0 filesrc location=/dev/stdin ! \
h264parse ! avdec_h264 ! autovideosink

5 Huawei IPCamera
HiIspTool.sh
Winows PQ Tools: Picture Quality

6 Abbreviations
AM824 Protocol: FireWire "Adaptation Layer" for MIDI
DWC sbd_intr: sideband interrupt
enp0s10: ethernet (en), prefix 0 (p0), slot 10 (s10). PCI BDF and physical/geographical location of the connector. systemd udev-builtin-net_id.c, /etc/systemd/network/99-default.link
GStreamer: based on GObject, GStreamer cerbero build for Android
Linux usr: Unix System Resource
Milan: Avnu Media-integrated local area network, for AVB interoperability
Network TAP:Test Access Point
SPAN:Switch Port Analyzer,也叫Port Mirror
UFIPC:Intel Ultra-Fast Inter Process Communication
VP8: Video Profile 8
XMOS: ex-INMOS. represented both the International founding of the company as well as the basis for VLSI components: n-channel MOS, or NMOS technology. Thus, it became known as International MOS or INMOS