解决metadata无媒体信息导致不能转换格式的问题

This commit is contained in:
xiongziliang 2018-03-01 14:32:11 +08:00
parent f5c7f4328c
commit 418c110893
2 changed files with 67 additions and 42 deletions

View File

@ -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) {

View File

@ -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<void(const H264Frame &frame)> onVideo;
mutable Ticker m_ticker;
function<void(const H264Frame &frame)> onVideo;
function<void(const AdtsFrame &frame)> onAudio;
recursive_mutex m_mtxCB;
};
} /* namespace Rtmp */