diff --git a/src/Rtmp/RtmpParser.cpp b/src/Rtmp/RtmpParser.cpp index 2a7e9113..67605b50 100644 --- a/src/Rtmp/RtmpParser.cpp +++ b/src/Rtmp/RtmpParser.cpp @@ -35,64 +35,70 @@ RtmpParser::RtmpParser(const AMFValue &val) { if (videoCodec.type() == AMF_STRING) { if (videoCodec.as_string() == "avc1") { - //264 - m_bHaveVideo = true; + //h264 + m_iVideoCodecID = H264_CODEC_ID; } else { InfoL << "不支持RTMP视频格式:" << videoCodec.as_string(); } }else if (videoCodec.type() != AMF_NULL){ - if (videoCodec.as_integer() == 7) { - //264 - m_bHaveVideo = true; - } else { + m_iVideoCodecID = videoCodec.as_integer(); + if (m_iVideoCodecID != H264_CODEC_ID) { InfoL << "不支持RTMP视频格式:" << videoCodec.as_integer(); } } - - + if (audioCodec.type() == AMF_STRING) { if (audioCodec.as_string() == "mp4a") { //aac - m_bHaveAudio = true; + m_iAudioCodecID = AAC_CODEC_ID; } else { InfoL << "不支持RTMP音频格式:" << audioCodec.as_string(); } }else if (audioCodec.type() != AMF_NULL) { - if (audioCodec.as_integer() == 10) { - //aac - m_bHaveAudio = true; - } else { + m_iAudioCodecID = audioCodec.as_integer(); + if (m_iAudioCodecID != AAC_CODEC_ID) { InfoL << "不支持RTMP音频格式:" << audioCodec.as_integer(); } } - - - if (!m_bHaveVideo && !m_bHaveAudio) { - throw std::runtime_error("不支持该RTMP媒体格式"); - } - onCheckMedia(val); } RtmpParser::~RtmpParser() { - // TODO Auto-generated destructor stub } bool RtmpParser::inputRtmp(const RtmpPacket::Ptr &pkt) { - switch (pkt->typeId) { - case MSG_VIDEO: - if (m_bHaveVideo) { - return inputVideo(pkt); - } - return false; - case MSG_AUDIO: - if (m_bHaveAudio) { - return inputAudio(pkt); - } - return false; - default: - return false; - } + switch (pkt->typeId) { + case MSG_VIDEO:{ + if(m_iVideoCodecID == 0){ + //未初始化视频 + m_iVideoCodecID = pkt->getMediaType(); + if(m_iVideoCodecID != H264_CODEC_ID){ + InfoL << "不支持RTMP视频格式:" << m_iVideoCodecID; + } + } + if(m_iVideoCodecID == H264_CODEC_ID){ + return inputVideo(pkt); + } + return false; + } + + case MSG_AUDIO: { + if(m_iAudioCodecID == 0){ + //未初始化音频 + m_iAudioCodecID = pkt->getMediaType(); + if(m_iAudioCodecID != AAC_CODEC_ID){ + InfoL << "不支持RTMP音频格式:" << m_iAudioCodecID; + } + } + if (m_iAudioCodecID == AAC_CODEC_ID) { + return inputAudio(pkt); + } + return false; + } + + default: + return false; + } } inline bool RtmpParser::inputVideo(const RtmpPacket::Ptr &pkt) { diff --git a/src/Rtmp/RtmpParser.h b/src/Rtmp/RtmpParser.h index c189f99d..ac687576 100644 --- a/src/Rtmp/RtmpParser.h +++ b/src/Rtmp/RtmpParser.h @@ -39,6 +39,9 @@ using namespace std; using namespace ZL::Util; using namespace ZL::Player; +#define H264_CODEC_ID 7 +#define AAC_CODEC_ID 10 + namespace ZL { namespace Rtmp { @@ -95,18 +98,31 @@ public: return m_strAudioCfg; } bool containAudio() const override{ - return m_bHaveAudio; + //音频只支持aac + return m_iAudioCodecID == AAC_CODEC_ID; } bool containVideo () const override{ - return m_bHaveVideo; + //视频只支持264 + return m_iVideoCodecID == H264_CODEC_ID; } bool isInited() const override{ - if (m_bHaveAudio && !m_strAudioCfg.size()) { + if((m_iAudioCodecID | m_iVideoCodecID) == 0){ + //音视频codec_id都未获取到,说明还未初始化成功 + return false; + } + if((m_iAudioCodecID & m_iVideoCodecID) == 0 && m_ticker.elapsedTime() < 300){ + //音视频codec_id有其一未获取到,且最少分析300ms才能断定没有音频或视频 + return false; + } + if (m_iAudioCodecID && !m_strAudioCfg.size()) { + //如果音频是aac但是还未获取aac config ,则未初始化成功 return false; } - if (m_bHaveVideo && !m_strSPS.size()) { - return false; + if (m_iVideoCodecID && !m_strSPS.size()) { + //如果视频是h264但是还未获取sps ,则未初始化成功 + return false; } + //初始化成功 return true; } float getDuration() const override{ @@ -136,13 +152,16 @@ private: int m_iVideoWidth = 0; int m_iVideoHeight = 0; float m_fVideoFps = 0; - bool m_bHaveAudio = false; - bool m_bHaveVideo = false; + //音视频codec_id初始为0代表尚未获取到 + int m_iAudioCodecID = 0; + int m_iVideoCodecID = 0; float m_fDuration = 0; - function onVideo; + mutable Ticker m_ticker; + function onVideo; function onAudio; recursive_mutex m_mtxCB; + }; } /* namespace Rtmp */