diff --git a/src/Rtmp/FlvSplitter.cpp b/src/Rtmp/FlvSplitter.cpp index 7898e233..05f4f744 100644 --- a/src/Rtmp/FlvSplitter.cpp +++ b/src/Rtmp/FlvSplitter.cpp @@ -97,24 +97,19 @@ void FlvSplitter::onRecvContent(const char *data, size_t len) { case MSG_DATA3: { BufferLikeString buffer(string(data, len)); AMFDecoder dec(buffer, _type == MSG_DATA3 ? 3 : 0); - auto first = dec.load(); + auto type = amfLoadLeadingString(dec); bool flag = true; - if (first.type() == AMFType::AMF_STRING) { - auto type = first.as_string(); - if (type == "@setDataFrame") { - type = dec.load(); - if (type == "onMetaData") { - flag = onRecvMetadata(dec.load()); - } else { - WarnL << "unknown type:" << type; - } - } else if (type == "onMetaData") { + if (type == "@setDataFrame") { + type = amfLoadLeadingString(dec); + if (type == "onMetaData") { flag = onRecvMetadata(dec.load()); } else { - WarnL << "unknown notify:" << type; + WarnL << "unknown type:" << type; } + } else if (type == "onMetaData") { + flag = onRecvMetadata(dec.load()); } else { - WarnL << "Parse flv script data failed, invalid amf value: " << first.to_string(); + WarnL << "unknown notify:" << type; } if (!flag) { throw std::invalid_argument("check rtmp metadata failed"); diff --git a/src/Rtmp/RtmpPlayer.cpp b/src/Rtmp/RtmpPlayer.cpp index 359030da..1dc76fdc 100644 --- a/src/Rtmp/RtmpPlayer.cpp +++ b/src/Rtmp/RtmpPlayer.cpp @@ -391,7 +391,7 @@ void RtmpPlayer::onRtmpChunk(RtmpPacket::Ptr packet) { case MSG_DATA: case MSG_DATA3: { AMFDecoder dec(chunk_data.buffer, 0, (chunk_data.type_id == MSG_DATA3 || chunk_data.type_id == MSG_CMD3) ? 3 : 0); - std::string type = dec.load(); + std::string type = amfLoadLeadingString(dec); auto it = s_func_map.find(type); if (it != s_func_map.end()) { auto fun = it->second; diff --git a/src/Rtmp/RtmpSession.cpp b/src/Rtmp/RtmpSession.cpp index 67219aa0..70024983 100644 --- a/src/Rtmp/RtmpSession.cpp +++ b/src/Rtmp/RtmpSession.cpp @@ -515,7 +515,7 @@ void RtmpSession::onRtmpChunk(RtmpPacket::Ptr packet) { case MSG_DATA: case MSG_DATA3: { AMFDecoder dec(chunk_data.buffer, chunk_data.type_id == MSG_DATA3 ? 3 : 0); - std::string type = dec.load(); + std::string type = amfLoadLeadingString(dec); if (type == "@setDataFrame") { setMetaData(dec); } else if (type == "onMetaData") { diff --git a/src/Rtmp/amf.cpp b/src/Rtmp/amf.cpp index 2cb4c994..0644ef22 100644 --- a/src/Rtmp/amf.cpp +++ b/src/Rtmp/amf.cpp @@ -463,6 +463,10 @@ AMFDecoder::DepthGuard::~DepthGuard() { --_decoder.depth; } +size_t AMFDecoder::remain() const { + return pos < buf.size() ? buf.size() - pos : 0; +} + uint8_t AMFDecoder::front() { if (pos >= buf.size()) { throw std::runtime_error("Not enough data"); @@ -703,3 +707,13 @@ AMFValue AMFDecoder::load_arr() { AMFDecoder::AMFDecoder(const BufferLikeString &buf_in, size_t pos_in, int version_in) : buf(buf_in), pos(pos_in), version(version_in) { } + +std::string amfLoadLeadingString(AMFDecoder &dec) { + while (dec.remain()) { + auto val = dec.load(); + if (val.type() == AMF_STRING) { + return val.as_string(); + } + } + return ""; +} diff --git a/src/Rtmp/amf.h b/src/Rtmp/amf.h index 9130451d..aa0c5a6b 100644 --- a/src/Rtmp/amf.h +++ b/src/Rtmp/amf.h @@ -88,6 +88,10 @@ public: template TP load(); + // 剩余未解析的字节数 + // Number of bytes not yet parsed + size_t remain() const; + private: class DepthGuard { public: @@ -118,6 +122,14 @@ private: static constexpr size_t kMaxDepth = 64; }; +// 加载首个字符串, 自动跳过前置的非字符串AMF值; 若不存在字符串则返回空字符串 +// 兼容某些不规范的推流器(例如宇视摄像机在@setDataFrame前插入多余的AMF number) +// Load the first string, skipping any leading non-string AMF values; +// returns an empty string if the stream contains none. +// Compatible with some non-standard publishers (e.g. Uniview cameras insert +// redundant AMF numbers before @setDataFrame) +std::string amfLoadLeadingString(AMFDecoder &dec); + class AMFEncoder { public: AMFEncoder & operator <<(const char *s);