Commit 37554da8 by xiongziliang

常数改成枚举

parent 4cb74454
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "Frame.h" #include "Frame.h"
#include "Track.h" #include "Track.h"
#define H264_TYPE(v) ((uint8_t)(v) & 0x1F)
namespace mediakit{ namespace mediakit{
bool getAVCInfo(const string &strSps,int &iVideoWidth, int &iVideoHeight, float &iVideoFps); bool getAVCInfo(const string &strSps,int &iVideoWidth, int &iVideoHeight, float &iVideoFps);
...@@ -42,6 +44,13 @@ class H264Frame : public Frame { ...@@ -42,6 +44,13 @@ class H264Frame : public Frame {
public: public:
typedef std::shared_ptr<H264Frame> Ptr; typedef std::shared_ptr<H264Frame> Ptr;
typedef enum {
NAL_SPS = 7,
NAL_PPS = 8,
NAL_IDR = 5,
NAL_B_P = 1
} NalType;
char *data() const override{ char *data() const override{
return (char *)buffer.data(); return (char *)buffer.data();
} }
...@@ -64,7 +73,7 @@ public: ...@@ -64,7 +73,7 @@ public:
} }
bool keyFrame() const override { bool keyFrame() const override {
return type == 5; return type == NAL_IDR;
} }
public: public:
uint16_t sequence; uint16_t sequence;
...@@ -95,7 +104,7 @@ public: ...@@ -95,7 +104,7 @@ public:
} }
bool keyFrame() const override { bool keyFrame() const override {
return (buffer_ptr[iPrefixSize] & 0x1F) == 5; return H264_TYPE(buffer_ptr[iPrefixSize]) == H264Frame::NAL_IDR;
} }
}; };
...@@ -193,9 +202,10 @@ public: ...@@ -193,9 +202,10 @@ public:
* @param frame 数据帧 * @param frame 数据帧
*/ */
void inputFrame(const Frame::Ptr &frame) override{ void inputFrame(const Frame::Ptr &frame) override{
int type = (*((uint8_t *)frame->data() + frame->prefixSize())) & 0x1F; int type = H264_TYPE(*((uint8_t *)frame->data() + frame->prefixSize()));
switch (type){ switch (type){
case 7:{ case H264Frame::NAL_SPS:{
//sps //sps
bool flag = _sps.empty(); bool flag = _sps.empty();
_sps = string(frame->data() + frame->prefixSize(),frame->size() - frame->prefixSize()); _sps = string(frame->data() + frame->prefixSize(),frame->size() - frame->prefixSize());
...@@ -204,19 +214,19 @@ public: ...@@ -204,19 +214,19 @@ public:
} }
} }
break; break;
case 8:{ case H264Frame::NAL_PPS:{
//pps //pps
_pps = string(frame->data() + frame->prefixSize(),frame->size() - frame->prefixSize()); _pps = string(frame->data() + frame->prefixSize(),frame->size() - frame->prefixSize());
} }
break; break;
case 5:{ case H264Frame::NAL_IDR:{
//I //I
if(!_sps.empty()){ if(!_sps.empty()){
if(!_spsFrame) if(!_spsFrame)
{ {
H264Frame::Ptr insertFrame = std::make_shared<H264Frame>(); H264Frame::Ptr insertFrame = std::make_shared<H264Frame>();
insertFrame->type = 7; insertFrame->type = H264Frame::NAL_SPS;
insertFrame->timeStamp = frame->stamp(); insertFrame->timeStamp = frame->stamp();
insertFrame->buffer.assign("\x0\x0\x0\x1",4); insertFrame->buffer.assign("\x0\x0\x0\x1",4);
insertFrame->buffer.append(_sps); insertFrame->buffer.append(_sps);
...@@ -231,7 +241,7 @@ public: ...@@ -231,7 +241,7 @@ public:
if(!_ppsFrame) if(!_ppsFrame)
{ {
H264Frame::Ptr insertFrame = std::make_shared<H264Frame>(); H264Frame::Ptr insertFrame = std::make_shared<H264Frame>();
insertFrame->type = 8; insertFrame->type = H264Frame::NAL_PPS;
insertFrame->timeStamp = frame->stamp(); insertFrame->timeStamp = frame->stamp();
insertFrame->buffer.assign("\x0\x0\x0\x1",4); insertFrame->buffer.assign("\x0\x0\x0\x1",4);
insertFrame->buffer.append(_pps); insertFrame->buffer.append(_pps);
...@@ -245,7 +255,7 @@ public: ...@@ -245,7 +255,7 @@ public:
} }
break; break;
case 1:{ case H264Frame::NAL_B_P:{
//B or P //B or P
VideoTrack::inputFrame(frame); VideoTrack::inputFrame(frame);
} }
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include "HLSMaker.h" #include "HLSMaker.h"
#include "Util/File.h" #include "Util/File.h"
#include "Util/uv_errno.h" #include "Util/uv_errno.h"
#include "Extension/H264.h"
using namespace toolkit; using namespace toolkit;
namespace mediakit { namespace mediakit {
...@@ -136,9 +137,10 @@ void HLSMaker::inputH264(void *data, uint32_t length, uint32_t timeStamp) { ...@@ -136,9 +137,10 @@ void HLSMaker::inputH264(void *data, uint32_t length, uint32_t timeStamp) {
_ui32LastStamp = timeStamp; _ui32LastStamp = timeStamp;
} }
int stampInc = timeStamp - _ui32LastStamp; int stampInc = timeStamp - _ui32LastStamp;
auto type = ((uint8_t*)data)[4] & 0x1F; auto type = H264_TYPE(((uint8_t*)data)[4]);
switch (type) { switch (type) {
case 7: //SPS case H264Frame::NAL_SPS: //SPS
if (stampInc >= _ui32SegmentDuration * 1000) { if (stampInc >= _ui32SegmentDuration * 1000) {
_ui32LastStamp = timeStamp; _ui32LastStamp = timeStamp;
//关闭文件 //关闭文件
...@@ -156,11 +158,11 @@ void HLSMaker::inputH264(void *data, uint32_t length, uint32_t timeStamp) { ...@@ -156,11 +158,11 @@ void HLSMaker::inputH264(void *data, uint32_t length, uint32_t timeStamp) {
} }
write_index_file(_ui64TsCnt - _ui32NumSegments, _ui64TsCnt, 0); write_index_file(_ui64TsCnt - _ui32NumSegments, _ui64TsCnt, 0);
} }
case 1: //P case H264Frame::NAL_B_P: //P
//insert aud frame before p and SPS frame //insert aud frame before p and SPS frame
_ts.inputH264("\x0\x0\x0\x1\x9\xf0", 6, timeStamp * 90); _ts.inputH264("\x0\x0\x0\x1\x9\xf0", 6, timeStamp * 90);
case 5: //IDR case H264Frame::NAL_IDR: //IDR
case 8: //PPS case H264Frame::NAL_PPS: //PPS
_ts.inputH264((char *) data, length, timeStamp * 90); _ts.inputH264((char *) data, length, timeStamp * 90);
break; break;
default: default:
......
...@@ -76,10 +76,10 @@ Mp4Maker::~Mp4Maker() { ...@@ -76,10 +76,10 @@ Mp4Maker::~Mp4Maker() {
} }
void Mp4Maker::inputH264(void *pData, uint32_t ui32Length, uint32_t ui32TimeStamp){ void Mp4Maker::inputH264(void *pData, uint32_t ui32Length, uint32_t ui32TimeStamp){
auto iType = ((uint8_t*)pData)[0] & 0x1F; auto iType = H264_TYPE(((uint8_t*)pData)[0]);
switch (iType) { switch (iType) {
case 1: //P case H264Frame::NAL_B_P: //P
case 5: { //IDR case H264Frame::NAL_IDR: { //IDR
if (_strLastVideo.size()) { if (_strLastVideo.size()) {
int64_t iTimeInc = (int64_t)ui32TimeStamp - (int64_t)_ui32LastVideoTime; int64_t iTimeInc = (int64_t)ui32TimeStamp - (int64_t)_ui32LastVideoTime;
iTimeInc = MAX(0,MIN(iTimeInc,500)); iTimeInc = MAX(0,MIN(iTimeInc,500));
...@@ -115,9 +115,8 @@ void Mp4Maker::inputAAC(void *pData, uint32_t ui32Length, uint32_t ui32TimeStamp ...@@ -115,9 +115,8 @@ void Mp4Maker::inputAAC(void *pData, uint32_t ui32Length, uint32_t ui32TimeStamp
void Mp4Maker::inputH264_l(void *pData, uint32_t ui32Length, uint32_t ui32Duration) { void Mp4Maker::inputH264_l(void *pData, uint32_t ui32Length, uint32_t ui32Duration) {
GET_CONFIG_AND_REGISTER(uint32_t,recordSec,Record::kFileSecond); GET_CONFIG_AND_REGISTER(uint32_t,recordSec,Record::kFileSecond);
auto iType = H264_TYPE(((uint8_t*)pData)[4]);
auto iType = ((uint8_t*)pData)[4] & 0x1F; if(iType == H264Frame::NAL_IDR && (_hMp4 == MP4_INVALID_FILE_HANDLE || _ticker.elapsedTime() > recordSec * 1000)){
if(iType == 5 && (_hMp4 == MP4_INVALID_FILE_HANDLE || _ticker.elapsedTime() > recordSec * 1000)){
//在I帧率处新建MP4文件 //在I帧率处新建MP4文件
//如果文件未创建或者文件超过10分钟则创建新文件 //如果文件未创建或者文件超过10分钟则创建新文件
createFile(); createFile();
......
...@@ -74,24 +74,23 @@ bool H264RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) { ...@@ -74,24 +74,23 @@ bool H264RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) {
inline void H264RtmpDecoder::onGetH264_l(const char* pcData, int iLen, uint32_t ui32TimeStamp) { inline void H264RtmpDecoder::onGetH264_l(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
switch (pcData[0] & 0x1F) { switch (H264_TYPE(pcData[0])) {
case 5: { case H264Frame::NAL_IDR: {
//I frame //I frame
onGetH264(_sps.data(), _sps.length(), ui32TimeStamp); onGetH264(_sps.data(), _sps.length(), ui32TimeStamp);
onGetH264(_pps.data(), _pps.length(), ui32TimeStamp); onGetH264(_pps.data(), _pps.length(), ui32TimeStamp);
} }
case 1: { case H264Frame::NAL_B_P: {
//I or P or B frame //I or P or B frame
onGetH264(pcData, iLen, ui32TimeStamp); onGetH264(pcData, iLen, ui32TimeStamp);
} }
break; break;
default: default:
//WarnL <<(int)(pcData[0] & 0x1F);
break; break;
} }
} }
inline void H264RtmpDecoder::onGetH264(const char* pcData, int iLen, uint32_t ui32TimeStamp) { inline void H264RtmpDecoder::onGetH264(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
_h264frame->type = pcData[0] & 0x1F; _h264frame->type = H264_TYPE(pcData[0]);
_h264frame->timeStamp = ui32TimeStamp; _h264frame->timeStamp = ui32TimeStamp;
_h264frame->buffer.assign("\x0\x0\x0\x1", 4); //添加264头 _h264frame->buffer.assign("\x0\x0\x0\x1", 4); //添加264头
_h264frame->buffer.append(pcData, iLen); _h264frame->buffer.append(pcData, iLen);
...@@ -114,12 +113,12 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) { ...@@ -114,12 +113,12 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
auto pcData = frame->data() + frame->prefixSize(); auto pcData = frame->data() + frame->prefixSize();
auto iLen = frame->size() - frame->prefixSize(); auto iLen = frame->size() - frame->prefixSize();
auto type = ((uint8_t*)pcData)[0] & 0x1F; auto type = H264_TYPE(((uint8_t*)pcData)[0]);
if(!_gotSpsPps){ if(!_gotSpsPps){
//尝试从frame中获取sps pps //尝试从frame中获取sps pps
switch (type){ switch (type){
case 7:{ case H264Frame::NAL_SPS:{
//sps //sps
if(_sps.empty()){ if(_sps.empty()){
_sps = string(pcData,iLen); _sps = string(pcData,iLen);
...@@ -129,7 +128,7 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) { ...@@ -129,7 +128,7 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
} }
} }
break; break;
case 8:{ case H264Frame::NAL_PPS:{
//pps //pps
if(_pps.empty()){ if(_pps.empty()){
_pps = string(pcData,iLen); _pps = string(pcData,iLen);
...@@ -152,8 +151,8 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) { ...@@ -152,8 +151,8 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
} }
switch (type){ switch (type){
case 5: case H264Frame::NAL_IDR:
case 1:{ case H264Frame::NAL_B_P:{
//I or P or B frame //I or P or B frame
int8_t flags = 7; //h.264 int8_t flags = 7; //h.264
bool is_config = false; bool is_config = false;
......
...@@ -103,7 +103,7 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) { ...@@ -103,7 +103,7 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
_h264frame->type = nal.type; _h264frame->type = nal.type;
_h264frame->timeStamp = rtppack->timeStamp; _h264frame->timeStamp = rtppack->timeStamp;
_h264frame->sequence = rtppack->sequence; _h264frame->sequence = rtppack->sequence;
auto isIDR = _h264frame->type == 5; auto isIDR = _h264frame->type == H264Frame::NAL_IDR;
onGetH264(_h264frame); onGetH264(_h264frame);
return (isIDR); //i frame return (isIDR); //i frame
} }
...@@ -121,7 +121,7 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) { ...@@ -121,7 +121,7 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
_h264frame->type = fu.type; _h264frame->type = fu.type;
_h264frame->timeStamp = rtppack->timeStamp; _h264frame->timeStamp = rtppack->timeStamp;
_h264frame->sequence = rtppack->sequence; _h264frame->sequence = rtppack->sequence;
return (_h264frame->type == 5); //i frame return (_h264frame->type == H264Frame::NAL_IDR); //i frame
} }
if (rtppack->sequence != (uint16_t)(_h264frame->sequence + 1)) { if (rtppack->sequence != (uint16_t)(_h264frame->sequence + 1)) {
...@@ -134,7 +134,7 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) { ...@@ -134,7 +134,7 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
//FU-A end //FU-A end
_h264frame->buffer.append((char *)frame + 2, length - 2); _h264frame->buffer.append((char *)frame + 2, length - 2);
_h264frame->timeStamp = rtppack->timeStamp; _h264frame->timeStamp = rtppack->timeStamp;
auto isIDR = _h264frame->type == 5; auto isIDR = _h264frame->type == H264Frame::NAL_IDR;
onGetH264(_h264frame); onGetH264(_h264frame);
return isIDR; return isIDR;
} }
...@@ -256,8 +256,8 @@ void H264RtpEncoder::makeH264Rtp(const void* data, unsigned int len, bool mark, ...@@ -256,8 +256,8 @@ void H264RtpEncoder::makeH264Rtp(const void* data, unsigned int len, bool mark,
rtppkt->type = TrackVideo; rtppkt->type = TrackVideo;
rtppkt->offset = 16; rtppkt->offset = 16;
uint8_t type = ((uint8_t *) (data))[0] & 0x1F; uint8_t type = H264_TYPE(((uint8_t *) (data))[0]);
RtpCodec::inputRtp(rtppkt,type == 7); RtpCodec::inputRtp(rtppkt,type == H264Frame::NAL_IDR);
_ui16Sequence++; _ui16Sequence++;
_ui32TimeStamp = uiStamp; _ui32TimeStamp = uiStamp;
} }
......
...@@ -256,8 +256,8 @@ void H265RtpEncoder::makeH265Rtp(const void* data, unsigned int len, bool mark, ...@@ -256,8 +256,8 @@ void H265RtpEncoder::makeH265Rtp(const void* data, unsigned int len, bool mark,
rtppkt->type = TrackVideo; rtppkt->type = TrackVideo;
rtppkt->offset = 16; rtppkt->offset = 16;
uint8_t type = ((uint8_t *) (data))[0] & 0x1F; uint8_t type = H265_TYPE(((uint8_t *) (data))[0]);
RtpCodec::inputRtp(rtppkt,type == 7); RtpCodec::inputRtp(rtppkt,type == H265Frame::isKeyFrame(type));
_ui16Sequence++; _ui16Sequence++;
_ui32TimeStamp = uiStamp; _ui32TimeStamp = uiStamp;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论