mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-16 04:55:58 +08:00
整理rtmp相关代码
This commit is contained in:
@@ -9,7 +9,6 @@
|
||||
*/
|
||||
|
||||
#include "H264Rtmp.h"
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
H264RtmpDecoder::H264RtmpDecoder() {
|
||||
@@ -28,11 +27,74 @@ bool H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos) {
|
||||
return decodeRtmp(rtmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回不带0x00 00 00 01头的sps
|
||||
* @return
|
||||
*/
|
||||
static string getH264SPS(const RtmpPacket &thiz) {
|
||||
string ret;
|
||||
if (thiz.getMediaType() != FLV_CODEC_H264) {
|
||||
return ret;
|
||||
}
|
||||
if (!thiz.isCfgFrame()) {
|
||||
return ret;
|
||||
}
|
||||
if (thiz.strBuf.size() < 13) {
|
||||
WarnL << "bad H264 cfg!";
|
||||
return ret;
|
||||
}
|
||||
uint16_t sps_size ;
|
||||
memcpy(&sps_size, thiz.strBuf.data() + 11,2);
|
||||
sps_size = ntohs(sps_size);
|
||||
if ((int) thiz.strBuf.size() < 13 + sps_size) {
|
||||
WarnL << "bad H264 cfg!";
|
||||
return ret;
|
||||
}
|
||||
ret.assign(thiz.strBuf.data() + 13, sps_size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回不带0x00 00 00 01头的pps
|
||||
* @return
|
||||
*/
|
||||
static string getH264PPS(const RtmpPacket &thiz) {
|
||||
string ret;
|
||||
if (thiz.getMediaType() != FLV_CODEC_H264) {
|
||||
return ret;
|
||||
}
|
||||
if (!thiz.isCfgFrame()) {
|
||||
return ret;
|
||||
}
|
||||
if (thiz.strBuf.size() < 13) {
|
||||
WarnL << "bad H264 cfg!";
|
||||
return ret;
|
||||
}
|
||||
uint16_t sps_size ;
|
||||
memcpy(&sps_size,thiz.strBuf.data() + 11,2);
|
||||
sps_size = ntohs(sps_size);
|
||||
|
||||
if ((int) thiz.strBuf.size() < 13 + sps_size + 1 + 2) {
|
||||
WarnL << "bad H264 cfg!";
|
||||
return ret;
|
||||
}
|
||||
uint16_t pps_size ;
|
||||
memcpy(&pps_size, thiz.strBuf.data() + 13 + sps_size + 1,2);
|
||||
pps_size = ntohs(pps_size);
|
||||
|
||||
if ((int) thiz.strBuf.size() < 13 + sps_size + 1 + 2 + pps_size) {
|
||||
WarnL << "bad H264 cfg!";
|
||||
return ret;
|
||||
}
|
||||
ret.assign(thiz.strBuf.data() + 13 + sps_size + 1 + 2, pps_size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool H264RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
if (pkt->isCfgFrame()) {
|
||||
//缓存sps pps,后续插入到I帧之前
|
||||
_sps = pkt->getH264SPS();
|
||||
_pps = pkt->getH264PPS();
|
||||
_sps = getH264SPS(*pkt);
|
||||
_pps = getH264PPS(*pkt);
|
||||
onGetH264(_sps.data(), _sps.size(), pkt->timeStamp , pkt->timeStamp);
|
||||
onGetH264(_pps.data(), _pps.size(), pkt->timeStamp , pkt->timeStamp);
|
||||
return false;
|
||||
@@ -61,6 +123,9 @@ bool H264RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
}
|
||||
|
||||
inline void H264RtmpDecoder::onGetH264(const char* pcData, int iLen, uint32_t dts,uint32_t pts) {
|
||||
if(iLen == 0){
|
||||
return;
|
||||
}
|
||||
#if 1
|
||||
_h264frame->_dts = dts;
|
||||
_h264frame->_pts = pts;
|
||||
@@ -77,8 +142,6 @@ inline void H264RtmpDecoder::onGetH264(const char* pcData, int iLen, uint32_t dt
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
H264RtmpEncoder::H264RtmpEncoder(const Track::Ptr &track) {
|
||||
@@ -135,7 +198,7 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
|
||||
if(!_lastPacket) {
|
||||
//I or P or B frame
|
||||
int8_t flags = 7; //h.264
|
||||
int8_t flags = FLV_CODEC_H264;
|
||||
bool is_config = false;
|
||||
flags |= (((frame->configFrame() || frame->keyFrame()) ? FLV_KEY_FRAME : FLV_INTER_FRAME) << 4);
|
||||
|
||||
@@ -159,35 +222,33 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
_lastPacket->bodySize = _lastPacket->strBuf.size();
|
||||
}
|
||||
|
||||
|
||||
void H264RtmpEncoder::makeVideoConfigPkt() {
|
||||
int8_t flags = 7; //h.264
|
||||
int8_t flags = FLV_CODEC_H264;
|
||||
flags |= (FLV_KEY_FRAME << 4);
|
||||
bool is_config = true;
|
||||
|
||||
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
|
||||
rtmpPkt->strBuf.clear();
|
||||
|
||||
//////////header
|
||||
//header
|
||||
rtmpPkt->strBuf.push_back(flags);
|
||||
rtmpPkt->strBuf.push_back(!is_config);
|
||||
//cts
|
||||
rtmpPkt->strBuf.append("\x0\x0\x0", 3);
|
||||
|
||||
////////////sps
|
||||
//AVCDecoderConfigurationRecord start
|
||||
rtmpPkt->strBuf.push_back(1); // version
|
||||
|
||||
//DebugL<<hexdump(_sps.data(), _sps.size());
|
||||
rtmpPkt->strBuf.push_back(_sps[1]); // profile
|
||||
rtmpPkt->strBuf.push_back(_sps[2]); // compat
|
||||
rtmpPkt->strBuf.push_back(_sps[3]); // level
|
||||
rtmpPkt->strBuf.push_back(0xff); // 6 bits reserved + 2 bits nal size length - 1 (11)
|
||||
rtmpPkt->strBuf.push_back(0xe1); // 3 bits reserved + 5 bits number of sps (00001)
|
||||
//sps
|
||||
uint16_t size = _sps.size();
|
||||
size = htons(size);
|
||||
rtmpPkt->strBuf.append((char *) &size, 2);
|
||||
rtmpPkt->strBuf.append(_sps);
|
||||
|
||||
/////////////pps
|
||||
//pps
|
||||
rtmpPkt->strBuf.push_back(1); // version
|
||||
size = _pps.size();
|
||||
size = htons(size);
|
||||
@@ -202,10 +263,4 @@ void H264RtmpEncoder::makeVideoConfigPkt() {
|
||||
RtmpCodec::inputRtmp(rtmpPkt, false);
|
||||
}
|
||||
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user