From 780a1eb9fc5d55529fa1f67fd8606cd484c01b00 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sat, 22 Jul 2023 19:32:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84rtmp=E5=85=B3=E9=94=AE?= =?UTF-8?q?=E5=B8=A7=E4=B8=8E=E9=85=8D=E7=BD=AE=E5=B8=A7=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Extension/AACRtmp.cpp | 3 --- src/Extension/H264Rtmp.cpp | 3 --- src/Extension/H265Rtmp.cpp | 3 --- src/Rtmp/Rtmp.cpp | 34 ++++++++++++++++++++++++---------- src/Rtmp/Rtmp.h | 2 ++ 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/Extension/AACRtmp.cpp b/src/Extension/AACRtmp.cpp index d18d9d25..9fbd8fd1 100644 --- a/src/Extension/AACRtmp.cpp +++ b/src/Extension/AACRtmp.cpp @@ -21,9 +21,6 @@ static string getAacCfg(const RtmpPacket &thiz) { if ((RtmpAudioCodec)thiz.getRtmpCodecId() != RtmpAudioCodec::aac) { return ret; } - if (!thiz.isCfgFrame()) { - return ret; - } if (thiz.buffer.size() < 4) { WarnL << "get aac config failed, rtmp packet is: " << hexdump(thiz.data(), thiz.size()); return ret; diff --git a/src/Extension/H264Rtmp.cpp b/src/Extension/H264Rtmp.cpp index f93c5a00..eb6e5c4d 100644 --- a/src/Extension/H264Rtmp.cpp +++ b/src/Extension/H264Rtmp.cpp @@ -33,9 +33,6 @@ static bool getH264Config(const RtmpPacket &thiz, string &sps, string &pps) { if ((RtmpVideoCodec)thiz.getRtmpCodecId() != RtmpVideoCodec::h264) { return false; } - if (!thiz.isCfgFrame()) { - return false; - } if (thiz.buffer.size() < 13) { return false; } diff --git a/src/Extension/H265Rtmp.cpp b/src/Extension/H265Rtmp.cpp index 451310b1..6c2af5fd 100644 --- a/src/Extension/H265Rtmp.cpp +++ b/src/Extension/H265Rtmp.cpp @@ -53,9 +53,6 @@ static bool getH265ConfigFrame(const RtmpPacket &thiz, string &frame) { if ((RtmpVideoCodec)thiz.getRtmpCodecId() != RtmpVideoCodec::h265) { return false; } - if (!thiz.isCfgFrame()) { - return false; - } if (thiz.buffer.size() < 6) { WarnL << "bad H265 cfg!"; return false; diff --git a/src/Rtmp/Rtmp.cpp b/src/Rtmp/Rtmp.cpp index 84eb5dbc..92c5f905 100644 --- a/src/Rtmp/Rtmp.cpp +++ b/src/Rtmp/Rtmp.cpp @@ -156,22 +156,36 @@ bool RtmpPacket::isVideoKeyFrame() const { if (type_id != MSG_VIDEO) { return false; } - RtmpPacketInfo info; - if (CodecInvalid == parseVideoRtmpPacket((uint8_t *)data(), size(), &info)) { - return false; + RtmpFrameType frame_type; + if (buffer[0] >> 7 == 1) { + // IsExHeader == 1 + frame_type = (RtmpFrameType)((buffer[0] >> 4) & 0x07); + } else { + // IsExHeader == 0 + frame_type = (RtmpFrameType)(buffer[0] >> 4); } - if (info.is_enhanced) { - return info.video.frame_type == RtmpFrameType::key_frame && info.video.pkt_type == RtmpPacketType::PacketTypeCodedFramesX; - } - return info.video.frame_type == RtmpFrameType::key_frame && info.video.h264_pkt_type == RtmpH264PacketType::h264_nalu; + return frame_type == RtmpFrameType::key_frame; } bool RtmpPacket::isCfgFrame() const { switch (type_id) { - case MSG_VIDEO: return (RtmpH264PacketType)buffer[1] == RtmpH264PacketType::h264_config_header; case MSG_AUDIO: { - switch ((RtmpAudioCodec)getRtmpCodecId()) { - case RtmpAudioCodec::aac: return (RtmpAACPacketType)buffer[1] == RtmpAACPacketType::aac_config_header; + return (RtmpAudioCodec)getRtmpCodecId() == RtmpAudioCodec::aac && (RtmpAACPacketType)buffer[1] == RtmpAACPacketType::aac_config_header; + } + case MSG_VIDEO: { + if (!isVideoKeyFrame()) { + return false; + } + if (buffer[0] >> 7 == 1) { + // IsExHeader == 1 + return (RtmpPacketType)(buffer[0] & 0x0f) == RtmpPacketType::PacketTypeSequenceStart; + } + // IsExHeader == 0 + switch ((RtmpVideoCodec)getRtmpCodecId()) { + case RtmpVideoCodec::h265: + case RtmpVideoCodec::h264: { + return (RtmpH264PacketType)buffer[1] == RtmpH264PacketType::h264_config_header; + } default: return false; } } diff --git a/src/Rtmp/Rtmp.h b/src/Rtmp/Rtmp.h index 01fc2fbf..9d83ffa9 100644 --- a/src/Rtmp/Rtmp.h +++ b/src/Rtmp/Rtmp.h @@ -169,7 +169,9 @@ public: } void clear(); + // video config frame和key frame都返回true bool isVideoKeyFrame() const; + // aac config或h264/h265 config bool isCfgFrame() const; int getRtmpCodecId() const; int getAudioSampleRate() const;