完善rtmp关键帧与配置帧判断逻辑

This commit is contained in:
xia-chu 2023-07-22 19:32:01 +08:00
parent a86398b6db
commit 780a1eb9fc
5 changed files with 26 additions and 19 deletions

View File

@ -21,9 +21,6 @@ static string getAacCfg(const RtmpPacket &thiz) {
if ((RtmpAudioCodec)thiz.getRtmpCodecId() != RtmpAudioCodec::aac) { if ((RtmpAudioCodec)thiz.getRtmpCodecId() != RtmpAudioCodec::aac) {
return ret; return ret;
} }
if (!thiz.isCfgFrame()) {
return ret;
}
if (thiz.buffer.size() < 4) { if (thiz.buffer.size() < 4) {
WarnL << "get aac config failed, rtmp packet is: " << hexdump(thiz.data(), thiz.size()); WarnL << "get aac config failed, rtmp packet is: " << hexdump(thiz.data(), thiz.size());
return ret; return ret;

View File

@ -33,9 +33,6 @@ static bool getH264Config(const RtmpPacket &thiz, string &sps, string &pps) {
if ((RtmpVideoCodec)thiz.getRtmpCodecId() != RtmpVideoCodec::h264) { if ((RtmpVideoCodec)thiz.getRtmpCodecId() != RtmpVideoCodec::h264) {
return false; return false;
} }
if (!thiz.isCfgFrame()) {
return false;
}
if (thiz.buffer.size() < 13) { if (thiz.buffer.size() < 13) {
return false; return false;
} }

View File

@ -53,9 +53,6 @@ static bool getH265ConfigFrame(const RtmpPacket &thiz, string &frame) {
if ((RtmpVideoCodec)thiz.getRtmpCodecId() != RtmpVideoCodec::h265) { if ((RtmpVideoCodec)thiz.getRtmpCodecId() != RtmpVideoCodec::h265) {
return false; return false;
} }
if (!thiz.isCfgFrame()) {
return false;
}
if (thiz.buffer.size() < 6) { if (thiz.buffer.size() < 6) {
WarnL << "bad H265 cfg!"; WarnL << "bad H265 cfg!";
return false; return false;

View File

@ -156,22 +156,36 @@ bool RtmpPacket::isVideoKeyFrame() const {
if (type_id != MSG_VIDEO) { if (type_id != MSG_VIDEO) {
return false; return false;
} }
RtmpPacketInfo info; RtmpFrameType frame_type;
if (CodecInvalid == parseVideoRtmpPacket((uint8_t *)data(), size(), &info)) { if (buffer[0] >> 7 == 1) {
return false; // IsExHeader == 1
frame_type = (RtmpFrameType)((buffer[0] >> 4) & 0x07);
} else {
// IsExHeader == 0
frame_type = (RtmpFrameType)(buffer[0] >> 4);
} }
if (info.is_enhanced) { return frame_type == RtmpFrameType::key_frame;
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;
} }
bool RtmpPacket::isCfgFrame() const { bool RtmpPacket::isCfgFrame() const {
switch (type_id) { switch (type_id) {
case MSG_VIDEO: return (RtmpH264PacketType)buffer[1] == RtmpH264PacketType::h264_config_header;
case MSG_AUDIO: { case MSG_AUDIO: {
switch ((RtmpAudioCodec)getRtmpCodecId()) { return (RtmpAudioCodec)getRtmpCodecId() == RtmpAudioCodec::aac && (RtmpAACPacketType)buffer[1] == RtmpAACPacketType::aac_config_header;
case RtmpAudioCodec::aac: return (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; default: return false;
} }
} }

View File

@ -169,7 +169,9 @@ public:
} }
void clear(); void clear();
// video config frame和key frame都返回true
bool isVideoKeyFrame() const; bool isVideoKeyFrame() const;
// aac config或h264/h265 config
bool isCfgFrame() const; bool isCfgFrame() const;
int getRtmpCodecId() const; int getRtmpCodecId() const;
int getAudioSampleRate() const; int getAudioSampleRate() const;