ZLMediaKit/src/Extension/H264.h

124 lines
3.2 KiB
C++
Raw Normal View History

2019-08-08 19:01:45 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2018-10-25 10:00:17 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2018-10-25 10:00:17 +08:00
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2018-10-25 10:00:17 +08:00
*/
2018-10-21 21:21:14 +08:00
2018-10-30 14:59:42 +08:00
#ifndef ZLMEDIAKIT_H264_H
#define ZLMEDIAKIT_H264_H
2018-10-21 21:21:14 +08:00
#include "Frame.h"
2018-10-30 14:59:42 +08:00
#include "Track.h"
2019-06-28 16:12:39 +08:00
#include "Util/base64.h"
2018-10-30 15:56:00 +08:00
#define H264_TYPE(v) ((uint8_t)(v) & 0x1F)
2021-02-05 11:51:16 +08:00
using namespace toolkit;
2018-10-30 15:56:00 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit{
2018-10-21 21:21:14 +08:00
bool getAVCInfo(const string &strSps,int &iVideoWidth, int &iVideoHeight, float &iVideoFps);
void splitH264(const char *ptr, size_t len, size_t prefix, const std::function<void(const char *, size_t, size_t)> &cb);
size_t prefixSize(const char *ptr, size_t len);
2021-02-05 11:51:16 +08:00
2018-10-27 22:54:16 +08:00
/**
2018-10-30 14:59:42 +08:00
* 264
2018-10-27 22:54:16 +08:00
*/
2020-05-11 22:33:10 +08:00
class H264Frame : public FrameImp {
2018-10-21 21:21:14 +08:00
public:
2021-02-05 11:51:16 +08:00
using Ptr = std::shared_ptr<H264Frame>;
2018-10-23 18:39:17 +08:00
2021-02-05 11:51:16 +08:00
enum {
2018-10-30 15:56:00 +08:00
NAL_IDR = 5,
NAL_SEI = 6,
2020-09-12 13:45:16 +08:00
NAL_SPS = 7,
NAL_PPS = 8,
NAL_AUD = 9,
NAL_B_P = 1,
2021-02-05 11:51:16 +08:00
};
2018-10-30 15:56:00 +08:00
2021-02-05 11:51:16 +08:00
bool keyFrame() const override;
bool configFrame() const override;
2018-10-23 14:32:06 +08:00
2021-02-05 11:51:16 +08:00
protected:
friend class FrameImp;
friend class ResourcePool_l<H264Frame>;
H264Frame();
2018-10-21 21:21:14 +08:00
};
/**
* H264类
* Frame类
* DevChannel中有使用
*/
2020-05-11 22:33:10 +08:00
class H264FrameNoCacheAble : public FrameFromPtr {
2018-10-21 21:21:14 +08:00
public:
2021-02-05 11:51:16 +08:00
using Ptr = std::shared_ptr<H264FrameNoCacheAble>;
2019-08-01 18:49:04 +08:00
2021-02-05 11:51:16 +08:00
H264FrameNoCacheAble(char *ptr,size_t size,uint32_t dts , uint32_t pts ,size_t prefix_size = 4);
bool keyFrame() const override;
bool configFrame() const override;
2018-10-21 21:21:14 +08:00
};
2018-10-27 22:54:16 +08:00
/**
* 264
*/
2018-10-23 11:47:27 +08:00
class H264Track : public VideoTrack{
2018-10-21 21:21:14 +08:00
public:
2021-02-05 11:51:16 +08:00
using Ptr = std::shared_ptr<H264Track>;
2018-10-23 14:32:06 +08:00
2018-10-23 16:41:25 +08:00
/**
* sps pps构造h264类型的媒体
* inputFrame中获取sps pps
*/
2021-02-05 11:51:16 +08:00
H264Track() = default;
2018-10-23 14:32:06 +08:00
/**
* h264类型的媒体
* @param sps sps帧数据
* @param pps pps帧数据
* @param sps_prefix_len 264340x00 00 00 01
* @param pps_prefix_len 264340x00 00 00 01
*/
2021-02-05 11:51:16 +08:00
H264Track(const string &sps,const string &pps,int sps_prefix_len = 4,int pps_prefix_len = 4);
2018-10-23 14:32:06 +08:00
/**
* h264类型的媒体
* @param sps sps帧
* @param pps pps帧
*/
2021-02-05 11:51:16 +08:00
H264Track(const Frame::Ptr &sps,const Frame::Ptr &pps);
2018-10-23 14:32:06 +08:00
/**
2021-02-05 11:51:16 +08:00
* 0x00 00 00 01sps/pps
2018-10-23 14:32:06 +08:00
*/
2021-02-05 11:51:16 +08:00
const string &getSps() const;
const string &getPps() const;
2018-10-23 14:32:06 +08:00
2021-02-05 11:51:16 +08:00
bool ready() override;
CodecId getCodecId() const override;
int getVideoHeight() const override;
int getVideoWidth() const override;
float getVideoFps() const override;
void inputFrame(const Frame::Ptr &frame) override;
2018-10-23 16:41:25 +08:00
private:
2021-02-05 11:51:16 +08:00
void onReady();
Sdp::Ptr getSdp() override;
Track::Ptr clone() override;
void inputFrame_l(const Frame::Ptr &frame);
void insertConfigFrame(const Frame::Ptr &frame);
2018-10-23 16:41:25 +08:00
2019-04-09 12:39:38 +08:00
private:
2021-02-05 11:51:16 +08:00
bool _is_idr = false;
2018-10-23 14:32:06 +08:00
int _width = 0;
int _height = 0;
float _fps = 0;
2021-02-05 11:51:16 +08:00
string _sps;
string _pps;
2018-10-30 17:58:10 +08:00
};
2018-10-24 17:17:55 +08:00
}//namespace mediakit
#endif //ZLMEDIAKIT_H264_H