From fe7da4f6380390df641ac51eed12df414bb028af Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Wed, 28 Aug 2024 14:54:06 +0800 Subject: [PATCH] feat add init check --- CMakeLists.txt | 7 ++++++- src/base/frame_manager.cpp | 15 +++++++++++++-- src/base/frame_manager.h | 2 ++ src/base/hk_sign.cpp | 5 +++++ src/base/rtp_manager.cpp | 37 ++++++++++++++++++++++++++++++++----- src/base/rtp_packet.cpp | 8 ++++++-- 6 files changed, 64 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e6937c5..0b314a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,8 @@ set(CMAKE_C_VISIBILITY_PRESET hidden) set(CMAKE_CXX_VISIBILITY_PRESET hidden) # set(CMAKE_C_FLAGS$ "${CMAKE_C_FLAGS} -fvisibility = hidden") # set(CMAKE_CXX_FLAGS$ "${CMAKE_CXX_FLAGS} -fvisibility = hidden") +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address +# -fno-omit-frame-pointer") set(SecMedia_Root ${CMAKE_CURRENT_SOURCE_DIR}/src) add_compile_options(-fPIC) # add_compile_options(-fvisibility=hidden) add_compile_options(-std=c++11) @@ -105,10 +107,13 @@ file(GLOB SecMedia_api_list ${CMAKE_CURRENT_SOURCE_DIR}/include/common.h) # # target_compile_options(${PROJECT_NAME} PRIVATE -fvisibility=hidden) # list(APPEND LINK_LIB_LIST ${LINK_LIB_SVAC_LIST}) +# +find_package(Threads REQUIRED) add_library(${PROJECT_NAME} SHARED ${SecMedia_src_list}) # add_library(${PROJECT_NAME} STATIC ${SecMedia_src_list}) -target_link_libraries(${PROJECT_NAME} PUBLIC ${LINK_LIB_SVAC_LIST} rtp) +target_link_libraries(${PROJECT_NAME} PUBLIC ${LINK_LIB_SVAC_LIST} rtp + Threads::Threads) target_include_directories( ${PROJECT_NAME} PRIVATE ${SecMedia_Root} diff --git a/src/base/frame_manager.cpp b/src/base/frame_manager.cpp index 930750f..eeb68c1 100644 --- a/src/base/frame_manager.cpp +++ b/src/base/frame_manager.cpp @@ -3,20 +3,21 @@ namespace sign { -FrameManager::FrameManager() { Init(); } +FrameManager::FrameManager() {} FrameManager::~FrameManager() {} bool FrameManager::Initialize() { + Init(); return _rtp_manager.Initialize(); } bool FrameManager::Enqueue(Frame::Ptr frame) { - + CheckMagicNumber(); std::lock_guard _(_frame_queue_lock); OnFrameEnqueue(frame); _frame_queue.push(frame); @@ -26,6 +27,7 @@ FrameManager::Enqueue(Frame::Ptr frame) Frame::Ptr FrameManager::Dequeue() { + CheckMagicNumber(); Frame::Ptr frame{nullptr}; auto start = std::chrono::system_clock::now(); { @@ -54,4 +56,13 @@ FrameManager::Init() OnFrameEnqueue.connect(&_rtp_manager, &RTPManager::OnFrameEnqueue); } +void +FrameManager::CheckMagicNumber() +{ + if (memcmp(magic_number, "MAGI", 4) != 0) { + ERROL("Please Init FrameManager.\n"); + exit(-1); + } +} + }// namespace sign diff --git a/src/base/frame_manager.h b/src/base/frame_manager.h index 2e2d71f..81c6bbc 100644 --- a/src/base/frame_manager.h +++ b/src/base/frame_manager.h @@ -28,8 +28,10 @@ public: private: void Init(); + void CheckMagicNumber(); private: + char magic_number[4] = {'M', 'A', 'G', 'I'}; RTPManager _rtp_manager; std::mutex _frame_queue_lock; diff --git a/src/base/hk_sign.cpp b/src/base/hk_sign.cpp index 9488ad8..8f8b88e 100644 --- a/src/base/hk_sign.cpp +++ b/src/base/hk_sign.cpp @@ -1,3 +1,4 @@ +#include "HuaWei/HWcommon.h" #include "HuaWei/HWsign.h" #include "base/frame_manager.h" @@ -18,6 +19,10 @@ HK_udp_in(void *Handle, const char *buf, const uint32_t len, void *param) { sign::FrameManager *fm = (sign::FrameManager *) Handle; auto frame = sign::Frame::CreateUDPFrame(buf, len); + if (!frame) { + ERROL("Create frame failed\n"); + return -1; + } fm->Enqueue(frame); return 1; } diff --git a/src/base/rtp_manager.cpp b/src/base/rtp_manager.cpp index 1d3d95d..cb9c6f8 100644 --- a/src/base/rtp_manager.cpp +++ b/src/base/rtp_manager.cpp @@ -73,8 +73,15 @@ RTPManager::InitVerifySet(void *info) void RTPManager::ProcessUDPFrame(Frame::Ptr frame) { - if (!IsRTPHeader(frame->data(), frame->size())) { return; } + if (!IsRTPHeader(frame->data(), frame->size())) { + INFOL("Not a RTP packet. discard it.\n"); + return; + } auto packet = RTPPacket::CreateCompleteRTPPacket(this, frame); + if (packet->payload_type() == 112) { + INFOL("RTCP packet pt=112. discard it.\n"); + return; + } ProcessPacket(packet); } @@ -86,7 +93,7 @@ void RTPManager::ProcessPacket(RTPPacket::Ptr packet) { if (!packet->IsComplete()) { - // ERROL("RTP packet is not complete, discard it.\n"); + ERROL("RTP packet is not complete, discard it.\n"); return; } @@ -99,7 +106,7 @@ RTPManager::ProcessPacket(RTPPacket::Ptr packet) MaybeSetSeq(packet->seq()); if (packet->seq() != _cur_rtp_seq) { - // WRNGL("RTP packet seq is not continuous. cur_seq=%ld, packet->seq=%d\n", _cur_rtp_seq, packet->seq()); + WRNGL("RTP packet seq is not continuous. cur_seq=%ld, packet->seq=%d\n", _cur_rtp_seq, packet->seq()); SetCurSeq(packet->seq()); } IncCurSeq(); @@ -110,7 +117,7 @@ RTPManager::ProcessPacket(RTPPacket::Ptr packet) } if (_decoder) { _decoder->Input(packet); } - MaybeReplaceSeq(packet); + // MaybeReplaceSeq(packet); } void @@ -156,6 +163,25 @@ RTPManager::GetNextSeq() return (_cur_rtp_seq + 1) & 0xFFFF; } +static const char * +ToString(H264Nal::H264Nal_t type) +{ + switch (type) { + case H264Nal::NAL_IDR: + return "IDR"; + case H264Nal::NAL_SEI: + return "SEI"; + case H264Nal::NAL_PPS: + return "PPS"; + case H264Nal::NAL_SPS: + return "SPS"; + case H264Nal::NAL_P_B: + return "P_B"; + default: + return "Unknown"; + } +} + void RTPManager::OnRTPDecoded(RTPDecoder::DecodedPacket packet) { @@ -169,7 +195,8 @@ RTPManager::OnRTPDecoded(RTPDecoder::DecodedPacket packet) } auto str = ss.str(); if (!str.empty()) str.pop_back(); - INFOL("RTP decoded. type=%2d, size=%5u seq=[%s]\n", h264_type, packet.bytes, str.c_str()); + INFOL("RTP decoded. type=%s, size=%5u seq=[%s]\n", ToString((H264Nal::H264Nal_t) h264_type), packet.bytes, + str.c_str()); if (h264_type == H264Nal::NAL_IDR) { // remove old sign ResetSignData(); diff --git a/src/base/rtp_packet.cpp b/src/base/rtp_packet.cpp index 0e34a97..748d957 100644 --- a/src/base/rtp_packet.cpp +++ b/src/base/rtp_packet.cpp @@ -141,8 +141,12 @@ RTPPacket::RTPPacket(RTPManager *owner, Frame::Ptr udp_frame) rtp_packet_t pkt; if (rtp_packet_deserialize(&pkt, udp_frame->data(), udp_frame->size()) == 0) { _state = kComplete; - uint8_t h264_type = ((uint8_t *) pkt.payload)[0] & 0x1F; - if (h264_type == H264Nal::NAL_SEI) { udp_frame->set_paused(true); } + if (pkt.payloadlen > 0 && pkt.payload) { + uint8_t h264_type = ((uint8_t *) pkt.payload)[0] & 0x1F; + if (h264_type == H264Nal::NAL_SEI) { udp_frame->set_paused(true); } + } else { + // WRNGL("rtp packet payload is empty\n"); + } } else { _state = kError; }