Using Banana Pi M2 Zero, OV5640 image sensor and Cedrus H264 hardware encoder to record & stream video.
Preparation:
- Make sure your power supply can output 2A current at 5V, otherwise the system will hang randomly, but 2A peaks are rare and instantaneous.
- Clean Armbian install (Armbian_21.08.1_Bananapim2zero_buster_current_5.10.60), with root permissions.
- All the following commands should be performed as root.
- Connect to Wi-Fi:
nmtui
- APT:
apt update
apt upgrade
- Reboot and find out kernel version after apt upgrade:
uname -r
- Install Linux sources:
apt install linux-source-<result>
Where result is what uname -r returned. - Unpack Linux sources:
cd /usr/src
mkdir linux
tar xvf linux-source-<version>.tar.xz -C ./linux/
- Copy current distribution config to downloaded sources:
cp /boot/config-<version> /usr/src/linux/arch/arm/configs/bananapi-m2-zero_defconfig
OV5640 driver:
- Copy modified device tree files from this repository to these sources:
cd /usr/src/linux
cp /this/repo/sun8i-h3.dtsi arch/arm/boot/dts/
cp /this/repo/sun8i-h2-plus-bananapi-m2-zero.dts arch/arm/boot/dts/
You can modify them accordingly instead of replacing original ones with mine. - Compile new device tree:
make bananapi-m2-zero_defconfig
make dtbs
- Install new device tree:
cp arch/arm/boot/dts/sun8i-h2-plus-bananapi-m2-zero.dtb /boot/dtb/
- Reboot and make sure OV5640 and CMA reserved memory are working:
dmesg | grep CMA
dmesg | grep OF
dmesg | grep ov5
- Install utils:
apt install v4l-utils libv4l-dev libmp3lame-dev libpulse-dev
- Test camera initialization:
media-ctl -v --device /dev/media0 --set-v4l2 '"ov5640 0-003c":0[fmt:YUYV8_2X8/1280x720@1/15]'
Cedar H264 encoder:
- Compile Linux kernel:
cd /usr/src/linux
make LOCALVERSION="-sunxi" -j4 zImage modules dtbs
- Get Cedar:
cd /usr/src
git clone https://github.com/uboborov/sunxi-cedar-mainline
cd sunxi-cedar-mainline
- Compile Cedar:
make LOCALVERSION="-sunxi"
- Install Cedar:
cp cedar_ve.ko /lib/modules/<version>/
depmod
echo cedar_ve | tee -a /etc/modules
- Reboot and check:
dmesg | grep cedar
- Diagnose CONFIG_CMA if needed.
FFMPEG:
- Install libcedrus:
cd ~
git clone https://github.com/linux-sunxi/libcedrus
cd libcedrus
make && sudo make install
- Install libvdpau (used for decoding):
apt install libvdpau-dev
- Install libx264:
cd ~
wget https://download.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20180121-2245-stable.tar.bz2
tar xf x264-snapshot-20180121-2245-stable.tar.bz2
cd x264-snapshot-20180121-2245-stable/
./configure --enable-static --enable-shared
make -j4
make install
ldconfig
- Install FFMPEG:
cd ~
git clone --depth=1 https://github.com/stulluk/FFmpeg-Cedrus.git
git clone https://github.com/uboborov/ffmpeg_h264_H3.git
cp ffmpeg_h264_H3/cedrus264.c FFmpeg-Cedrus/libavcodec/cedrus264.c
cp -R ffmpeg_h264_H3/sunxi FFmpeg-Cedrus/libavcodec/arm
cd FFmpeg-Cedrus
apt install libasound2-dev
./configure --prefix=/usr --enable-nonfree --enable-gpl --enable-version3 --enable-vdpau --enable-libx264 --enable-libmp3lame --enable-libpulse --enable-libv4l2 --enable-indev=alsa --enable-shared
make
- It will give a relocation error, fix it by:
rm libavcodec/xface.o
make
make install
Testing:
./record.sh
Power Loss Protection:
nano /etc/fstab
Change commit to 60
Full recording (with USB audio):
./record_with_audio.sh
Install services:
cd /this/repo/
cp *.service /etc/systemd/system/
cp *.sh /root/
cp streaming_target.conf /root/
systemctl daemon-reload
Streaming:
Update streaming_target.conf accordingly and then:
systemctl start bpi-streamer
Watching:
./playstream.sh