2013年12月1日 星期日

Rtsp server for H264 / H265 / AAC

Refer to:
https://github.com/PHZ76/RtspServer
https://github.com/ImSjt/RtspServer
https://gitee.com/marcode/RtspServer-1
https://github.com/ChristianFeldmann/h264Bitstream
https://github.com/Dillon14281118/h265bitstream
https://github.com/BobCromwell/aac-stream-analyzer

Preinstall:
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null
sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ xenial main'
sudo apt update
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DE19EB17684BA42D
apt install cmake cmake-qt-gui cmake-curses-gui
apt install libavcodec-dev libavutil-dev libavformat-dev libswscale-dev libavdevice-dev libsdl1.2-dev

Source:
https://github.com/fatalfeel/rtsp_server
https://www.mediafire.com/file/9pmw5aov8fe6zai/RtspServer_ffmpeg.tar.gz

####################### bug fix #####################
###Invalid NAL unit 0, skipping in VLC and ffplay play failed ###
1. rtp.h
//refer to live555 H264or5VideoRTPSink.cpp MultiFramedRTPSink.cpp
//RTP hdr size is 12, ourMaxPacketSize()-12 = 1452 -12
#define MAX_RTP_PAYLOAD_SIZE   1420
~~~change to
#define MAX_RTP_PAYLOAD_SIZE   1440

//#define RTP_TCP_HEAD_SIZE    4 //when transport_mode_ = RTP_OVER_TCP
~~~change to
#define RTP_TCP_HEAD_SIZE      0 //when transport_mode_ = RTP_OVER_UDP

2. H264Source.cpp
if (frame_size <= MAX_RTP_PAYLOAD_SIZE)
{
    frame_buf  += 4; //shift to nalu position
    frame_size -= 4;
    RtpPacket rtp_pkt;
    rtp_pkt.type = frame.type;
    rtp_pkt.timestamp = frame.timestamp;
    rtp_pkt.size = frame_size + RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE;
    rtp_pkt.last = 1;
    ...
}
else
{
    frame_buf  += 4; //shift to nalu position
    frame_size -= 4;
    char FU_A[2] = {0};
    FU_A[0] = (frame_buf[0] & 0xE0) | 28;
    FU_A[1] = 0x80 | (frame_buf[0] & 0x1F);
}

3. H265Source.cpp
if (frame_size <= MAX_RTP_PAYLOAD_SIZE)
{
    frame_buf  += 4; //shift to nalu position
    frame_size -= 4;
    RtpPacket rtp_pkt;
    rtp_pkt.type = frame.type;
    rtp_pkt.timestamp = frame.timestamp;
    rtp_pkt.size = frame_size + RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE;
    rtp_pkt.last = 1;
    ...
}
else
{
    frame_buf  += 4; //shift to nalu position
    frame_size -= 4;
    uint8_t PL_FU[3] = {0};
    uint8_t nalUnitType = (frame_buf[0] & 0x7E) >> 1;
    PL_FU[0] = (frame_buf[0] & 0x81) | (49<<1);
    PL_FU[1] = frame_buf[1];
    PL_FU[2] = 0x80 | nalUnitType;
}

4. new add h265 file player server
https://www.mediafire.com/file/qj96kqkhfkmmwfc/rtsp_h265_file.tar.gz

5. new add aac file player server
https://www.mediafire.com/file/8erkmf5f25vd9fz/rtsp_aac_file.tar.gz

6. fixed h264 bug
https://www.mediafire.com/file/yh45wmb8z6d2q71/rtsp_h264_file.tar.gz

7. fixed 1280x720 4 clients broken block
https://www.mediafire.com/file/0lhsi7ttyf4sx2k/MediaSource.tar.gz

If source have CMakeLists.txt do follows
CPU_NUM=$(grep processor /proc/cpuinfo | awk '{field=$NF};END{print field+1}')
echo "CPU number = "$CPU_NUM

mkdir -p build && cd build

cmake .. -G"Unix Makefiles" -DCMAKE_INSTALL_PREFIX=../rtsp_server -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS_RELEASE="-O3 -g" -DCMAKE_CXX_FLAGS_RELEASE="-O3 -g"

cmake --build . --config Release --target install -- -j$CPU_NUM VERBOSE=1

Tools:
ue(v), se(v), te(v) decode
https://github.com/Gosivn/Exp-Golomb-C-implementation
h264 parser
https://www.mediafire.com/file/0qvirp4bcroh5qc/H264Bitstream.tar.gz
h265 parser
https://www.mediafire.com/file/xo69cltp8aqihmc/h265Bitstream.tar.gz
aac parser
https://www.mediafire.com/file/2fyggla3bt7jwle/AACBitstream.tar.gz

(a) live555 release version
wget  http://www.live555.com/liveMedia/public/live555-latest.tar.gz
(b) live555 debug version, -O0 -g build
H264or5VideoRTPSink.cpp remove comment to enable debug 
https://www.mediafire.com/file/o1t4fnriih1nbi2/live.tar.gz
tar -xvf ./live555-latest.tar.gz
cd live
./genMakefiles linux-64bit
make -j8

VLC player for rtsp:
apt install vlc
If meet "VLC is not supposed to be run as root"
cp /usr/bin/vlc /usr/bin/vlc-backup
needle=$(objdump -d /usr/bin/vlc | grep euid | tail -1 | awk '{print "\\x"$2"\\x"$3"\\x"$4"\\x"$5"\\x"$6;}')
sed -ir "s/$needle/\xb8\x01\x00\x00\x00/" /usr/bin/vlc

Throry:
https://blog.csdn.net/weixin_42462202/article/details/99111635

Demo 1: H265 950x540 16ch with ffplay
https://drive.google.com/file/d/1ZlD7SWmovdHL2XCxAmtjuEziGfH8YdJv/view
Demo 2: H265 1280x720 8ch  with ffplay
https://drive.google.com/file/d/1oPijs48OEjvXHaZSHVcBgGIovCu0polF/view
Demo 3: H265 1280x720 4ch + AAC with ffplay
https://drive.google.com/file/d/1pfFUd1JjvGyzRbxodJAvoTtX2GMVEz2F/view

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Tesla told in New York Herald: I prefer to be remembered as the inventor who succeeded in abolishing war. That will be my highest pride.
http://www.teslacollection.com/tesla_articles/1898/new_york_herald/f_l_christman/tesla_declares_he_will_abolish_war (in middle section)

Albert Einstein: The release of atom power has changed everything except our way of thinking... the solution to this problem lies in the heart of mankind. If only I had known, I should have become a watchmaker.
https://atomictrauma.wordpress.com/the-scientists/albert-einstein

Artificial Intelligence Has an Enormous Carbon Footprint
https://www.analyticsvidhya.com/blog/2022/03/the-carbon-footprint-of-ai-and-deep-learning

沒有留言:

張貼留言