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/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
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"
|
|
|
|
|
using namespace toolkit;
|
2018-10-30 15:56:00 +08:00
|
|
|
|
#define H264_TYPE(v) ((uint8_t)(v) & 0x1F)
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit{
|
2018-10-21 21:21:14 +08:00
|
|
|
|
|
2018-10-30 14:59:42 +08:00
|
|
|
|
bool getAVCInfo(const string &strSps,int &iVideoWidth, int &iVideoHeight, float &iVideoFps);
|
2020-04-30 10:00:55 +08:00
|
|
|
|
void splitH264(const char *ptr, int len, int prefix, const std::function<void(const char *, int, int)> &cb);
|
2018-10-21 21:21:14 +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:
|
2018-10-30 14:59:42 +08:00
|
|
|
|
typedef std::shared_ptr<H264Frame> Ptr;
|
2018-10-23 18:39:17 +08:00
|
|
|
|
|
2018-10-30 15:56:00 +08:00
|
|
|
|
typedef enum {
|
|
|
|
|
NAL_SPS = 7,
|
|
|
|
|
NAL_PPS = 8,
|
|
|
|
|
NAL_IDR = 5,
|
2020-01-13 11:51:29 +08:00
|
|
|
|
NAL_SEI = 6,
|
2018-10-30 15:56:00 +08:00
|
|
|
|
} NalType;
|
|
|
|
|
|
2020-05-11 22:33:10 +08:00
|
|
|
|
H264Frame(){
|
|
|
|
|
_codecid = CodecH264;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
2018-10-23 14:32:06 +08:00
|
|
|
|
|
2018-10-30 14:59:42 +08:00
|
|
|
|
bool keyFrame() const override {
|
2020-01-14 10:04:24 +08:00
|
|
|
|
return H264_TYPE(_buffer[_prefix_size]) == H264Frame::NAL_IDR;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
2019-08-01 18:49:04 +08:00
|
|
|
|
|
|
|
|
|
bool configFrame() const override{
|
2020-01-14 10:04:24 +08:00
|
|
|
|
switch(H264_TYPE(_buffer[_prefix_size]) ){
|
2019-08-01 18:49:04 +08:00
|
|
|
|
case H264Frame::NAL_SPS:
|
2020-05-11 22:33:10 +08:00
|
|
|
|
case H264Frame::NAL_PPS:return true;
|
|
|
|
|
default:return false;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-21 21:21:14 +08:00
|
|
|
|
};
|
|
|
|
|
|
2019-07-25 12:09:36 +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:
|
2019-07-24 18:02:55 +08:00
|
|
|
|
typedef std::shared_ptr<H264FrameNoCacheAble> Ptr;
|
2018-10-23 18:39:17 +08:00
|
|
|
|
|
2020-05-11 22:33:10 +08:00
|
|
|
|
H264FrameNoCacheAble(char *ptr,uint32_t size,uint32_t dts , uint32_t pts ,int prefix_size = 4){
|
2019-07-03 16:22:12 +08:00
|
|
|
|
_ptr = ptr;
|
|
|
|
|
_size = size;
|
|
|
|
|
_dts = dts;
|
|
|
|
|
_pts = pts;
|
2020-05-11 22:33:10 +08:00
|
|
|
|
_prefix_size = prefix_size;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
2018-10-23 14:32:06 +08:00
|
|
|
|
|
2018-10-30 14:59:42 +08:00
|
|
|
|
CodecId getCodecId() const override{
|
|
|
|
|
return CodecH264;
|
|
|
|
|
}
|
2018-10-23 14:32:06 +08:00
|
|
|
|
|
2018-10-30 14:59:42 +08:00
|
|
|
|
bool keyFrame() const override {
|
2020-05-11 22:33:10 +08:00
|
|
|
|
return H264_TYPE(_ptr[_prefix_size]) == H264Frame::NAL_IDR;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
2019-08-01 18:49:04 +08:00
|
|
|
|
|
|
|
|
|
bool configFrame() const override{
|
2020-05-11 22:33:10 +08:00
|
|
|
|
switch(H264_TYPE(_ptr[_prefix_size])){
|
2019-08-01 18:49:04 +08:00
|
|
|
|
case H264Frame::NAL_SPS:
|
2020-05-11 22:33:10 +08:00
|
|
|
|
case H264Frame::NAL_PPS:return true;
|
|
|
|
|
default:return false;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-21 21:21:14 +08:00
|
|
|
|
};
|
|
|
|
|
|
2019-07-30 13:54:19 +08:00
|
|
|
|
typedef FrameInternal<H264FrameNoCacheAble> H264FrameInternal;
|
|
|
|
|
|
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:
|
2018-10-23 18:39:17 +08:00
|
|
|
|
typedef std::shared_ptr<H264Track> Ptr;
|
2018-10-23 14:32:06 +08:00
|
|
|
|
|
2018-10-23 16:41:25 +08:00
|
|
|
|
/**
|
|
|
|
|
* 不指定sps pps构造h264类型的媒体
|
|
|
|
|
* 在随后的inputFrame中获取sps pps
|
|
|
|
|
*/
|
|
|
|
|
H264Track(){}
|
2018-10-23 14:32:06 +08:00
|
|
|
|
/**
|
|
|
|
|
* 构造h264类型的媒体
|
|
|
|
|
* @param sps sps帧数据
|
|
|
|
|
* @param pps pps帧数据
|
|
|
|
|
* @param sps_prefix_len 264头长度,可以为3个或4个字节,一般为0x00 00 00 01
|
|
|
|
|
* @param pps_prefix_len 264头长度,可以为3个或4个字节,一般为0x00 00 00 01
|
|
|
|
|
*/
|
|
|
|
|
H264Track(const string &sps,const string &pps,int sps_prefix_len = 4,int pps_prefix_len = 4){
|
|
|
|
|
_sps = sps.substr(sps_prefix_len);
|
|
|
|
|
_pps = pps.substr(pps_prefix_len);
|
2018-10-30 16:12:32 +08:00
|
|
|
|
onReady();
|
2018-10-23 14:32:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造h264类型的媒体
|
|
|
|
|
* @param sps sps帧
|
|
|
|
|
* @param pps pps帧
|
|
|
|
|
*/
|
|
|
|
|
H264Track(const Frame::Ptr &sps,const Frame::Ptr &pps){
|
|
|
|
|
if(sps->getCodecId() != CodecH264 || pps->getCodecId() != CodecH264 ){
|
|
|
|
|
throw std::invalid_argument("必须输入H264类型的帧");
|
|
|
|
|
}
|
|
|
|
|
_sps = string(sps->data() + sps->prefixSize(),sps->size() - sps->prefixSize());
|
|
|
|
|
_pps = string(pps->data() + pps->prefixSize(),pps->size() - pps->prefixSize());
|
2018-10-30 16:12:32 +08:00
|
|
|
|
onReady();
|
2018-10-21 21:21:14 +08:00
|
|
|
|
}
|
2018-10-23 14:32:06 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回不带0x00 00 00 01头的sps
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2018-10-21 21:21:14 +08:00
|
|
|
|
const string &getSps() const{
|
|
|
|
|
return _sps;
|
|
|
|
|
}
|
2018-10-23 14:32:06 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回不带0x00 00 00 01头的pps
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2018-10-21 21:21:14 +08:00
|
|
|
|
const string &getPps() const{
|
|
|
|
|
return _pps;
|
|
|
|
|
}
|
2018-10-23 14:32:06 +08:00
|
|
|
|
|
|
|
|
|
CodecId getCodecId() const override {
|
2018-10-23 11:09:21 +08:00
|
|
|
|
return CodecH264;
|
2018-10-21 21:21:14 +08:00
|
|
|
|
}
|
2018-10-23 14:32:06 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回视频高度
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
int getVideoHeight() const override{
|
2019-01-21 12:02:44 +08:00
|
|
|
|
return _height ;
|
2018-10-23 14:32:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回视频宽度
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
int getVideoWidth() const override{
|
2019-01-21 12:02:44 +08:00
|
|
|
|
return _width;
|
2018-10-23 14:32:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回视频fps
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
float getVideoFps() const override{
|
|
|
|
|
return _fps;
|
|
|
|
|
}
|
2018-10-23 16:41:25 +08:00
|
|
|
|
|
2018-10-24 22:29:19 +08:00
|
|
|
|
bool ready() override {
|
|
|
|
|
return !_sps.empty() && !_pps.empty();
|
2018-10-23 16:41:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 11:44:41 +08:00
|
|
|
|
/**
|
|
|
|
|
* 输入数据帧,并获取sps pps
|
|
|
|
|
* @param frame 数据帧
|
|
|
|
|
*/
|
|
|
|
|
void inputFrame(const Frame::Ptr &frame) override{
|
|
|
|
|
int type = H264_TYPE(*((uint8_t *)frame->data() + frame->prefixSize()));
|
2020-04-03 23:03:47 +08:00
|
|
|
|
if(type == H264Frame::NAL_SPS || type == H264Frame::NAL_SEI){
|
2019-01-30 11:44:41 +08:00
|
|
|
|
//有些设备会把SPS PPS IDR帧当做一个帧打包,所以我们要split一下
|
2020-04-30 10:00:55 +08:00
|
|
|
|
splitH264(frame->data(), frame->size(), frame->prefixSize(), [&](const char *ptr, int len, int prefix) {
|
|
|
|
|
H264FrameInternal::Ptr sub_frame = std::make_shared<H264FrameInternal>(frame, (char *)ptr, len, prefix);
|
|
|
|
|
inputFrame_l(sub_frame);
|
|
|
|
|
});
|
2019-01-30 11:44:41 +08:00
|
|
|
|
} else{
|
|
|
|
|
inputFrame_l(frame);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
/**
|
|
|
|
|
* 解析sps获取宽高fps
|
|
|
|
|
*/
|
|
|
|
|
void onReady(){
|
|
|
|
|
getAVCInfo(_sps,_width,_height,_fps);
|
|
|
|
|
}
|
|
|
|
|
Track::Ptr clone() override {
|
|
|
|
|
return std::make_shared<std::remove_reference<decltype(*this)>::type >(*this);
|
|
|
|
|
}
|
2018-10-23 16:41:25 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 输入数据帧,并获取sps pps
|
|
|
|
|
* @param frame 数据帧
|
|
|
|
|
*/
|
2019-01-30 11:44:41 +08:00
|
|
|
|
void inputFrame_l(const Frame::Ptr &frame){
|
2018-10-30 15:56:00 +08:00
|
|
|
|
int type = H264_TYPE(*((uint8_t *)frame->data() + frame->prefixSize()));
|
2018-10-23 16:41:25 +08:00
|
|
|
|
switch (type){
|
2018-10-30 15:56:00 +08:00
|
|
|
|
case H264Frame::NAL_SPS:{
|
2018-10-23 16:41:25 +08:00
|
|
|
|
//sps
|
|
|
|
|
_sps = string(frame->data() + frame->prefixSize(),frame->size() - frame->prefixSize());
|
|
|
|
|
}
|
|
|
|
|
break;
|
2018-10-30 15:56:00 +08:00
|
|
|
|
case H264Frame::NAL_PPS:{
|
2018-10-23 16:41:25 +08:00
|
|
|
|
//pps
|
|
|
|
|
_pps = string(frame->data() + frame->prefixSize(),frame->size() - frame->prefixSize());
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2018-10-30 15:56:00 +08:00
|
|
|
|
case H264Frame::NAL_IDR:{
|
2018-10-23 16:41:25 +08:00
|
|
|
|
//I
|
2019-04-09 12:39:38 +08:00
|
|
|
|
insertConfigFrame(frame);
|
2018-10-23 21:41:45 +08:00
|
|
|
|
VideoTrack::inputFrame(frame);
|
2018-10-23 16:41:25 +08:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2020-05-17 19:06:14 +08:00
|
|
|
|
case H264Frame::NAL_SEI:{
|
|
|
|
|
//忽略SEI
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-13 15:42:58 +08:00
|
|
|
|
default:
|
2018-10-23 21:41:45 +08:00
|
|
|
|
VideoTrack::inputFrame(frame);
|
2018-10-23 16:41:25 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2018-10-30 16:12:32 +08:00
|
|
|
|
|
2019-12-13 15:42:58 +08:00
|
|
|
|
_last_frame_is_idr = type == H264Frame::NAL_IDR;
|
2018-10-30 16:12:32 +08:00
|
|
|
|
if(_width == 0 && ready()){
|
|
|
|
|
onReady();
|
|
|
|
|
}
|
2018-10-23 16:41:25 +08:00
|
|
|
|
}
|
2019-04-09 12:39:38 +08:00
|
|
|
|
|
2019-06-28 16:12:39 +08:00
|
|
|
|
//生成sdp
|
|
|
|
|
Sdp::Ptr getSdp() override ;
|
2019-04-09 12:39:38 +08:00
|
|
|
|
private:
|
|
|
|
|
//在idr帧前插入sps pps帧
|
|
|
|
|
void insertConfigFrame(const Frame::Ptr &frame){
|
|
|
|
|
if(_last_frame_is_idr){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!_sps.empty()){
|
2019-07-03 16:42:20 +08:00
|
|
|
|
auto spsFrame = std::make_shared<H264Frame>();
|
2020-01-14 10:04:24 +08:00
|
|
|
|
spsFrame->_prefix_size = 4;
|
|
|
|
|
spsFrame->_buffer.assign("\x0\x0\x0\x1",4);
|
|
|
|
|
spsFrame->_buffer.append(_sps);
|
|
|
|
|
spsFrame->_dts = frame->dts();
|
2019-07-03 16:42:20 +08:00
|
|
|
|
VideoTrack::inputFrame(spsFrame);
|
2019-04-09 12:39:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!_pps.empty()){
|
2019-07-03 16:42:20 +08:00
|
|
|
|
auto ppsFrame = std::make_shared<H264Frame>();
|
2020-01-14 10:04:24 +08:00
|
|
|
|
ppsFrame->_prefix_size = 4;
|
|
|
|
|
ppsFrame->_buffer.assign("\x0\x0\x0\x1",4);
|
|
|
|
|
ppsFrame->_buffer.append(_pps);
|
|
|
|
|
ppsFrame->_dts = frame->dts();
|
2019-07-03 16:42:20 +08:00
|
|
|
|
VideoTrack::inputFrame(ppsFrame);
|
2019-04-09 12:39:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-21 21:21:14 +08:00
|
|
|
|
private:
|
|
|
|
|
string _sps;
|
|
|
|
|
string _pps;
|
2018-10-23 14:32:06 +08:00
|
|
|
|
int _width = 0;
|
|
|
|
|
int _height = 0;
|
|
|
|
|
float _fps = 0;
|
2019-04-09 12:39:38 +08:00
|
|
|
|
bool _last_frame_is_idr = false;
|
2018-10-21 21:21:14 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-30 17:58:10 +08:00
|
|
|
|
/**
|
|
|
|
|
* h264类型sdp
|
|
|
|
|
*/
|
|
|
|
|
class H264Sdp : public Sdp {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param sps 264 sps,不带0x00000001头
|
|
|
|
|
* @param pps 264 pps,不带0x00000001头
|
|
|
|
|
* @param playload_type rtp playload type 默认96
|
|
|
|
|
* @param bitrate 比特率
|
|
|
|
|
*/
|
|
|
|
|
H264Sdp(const string &strSPS,
|
|
|
|
|
const string &strPPS,
|
|
|
|
|
int playload_type = 96,
|
|
|
|
|
int bitrate = 4000) : Sdp(90000,playload_type) {
|
|
|
|
|
//视频通道
|
|
|
|
|
_printer << "m=video 0 RTP/AVP " << playload_type << "\r\n";
|
|
|
|
|
_printer << "b=AS:" << bitrate << "\r\n";
|
|
|
|
|
_printer << "a=rtpmap:" << playload_type << " H264/" << 90000 << "\r\n";
|
2019-10-08 09:45:43 +08:00
|
|
|
|
_printer << "a=fmtp:" << playload_type << " packetization-mode=1; profile-level-id=";
|
2018-10-30 17:58:10 +08:00
|
|
|
|
|
|
|
|
|
char strTemp[100];
|
|
|
|
|
uint32_t profile_level_id = 0;
|
|
|
|
|
if (strSPS.length() >= 4) { // sanity check
|
|
|
|
|
profile_level_id = (uint8_t(strSPS[1]) << 16) |
|
|
|
|
|
(uint8_t(strSPS[2]) << 8) |
|
|
|
|
|
(uint8_t(strSPS[3])); // profile_idc|constraint_setN_flag|level_idc
|
|
|
|
|
}
|
|
|
|
|
memset(strTemp, 0, 100);
|
|
|
|
|
sprintf(strTemp, "%06X", profile_level_id);
|
|
|
|
|
_printer << strTemp;
|
2019-10-08 09:45:43 +08:00
|
|
|
|
_printer << "; sprop-parameter-sets=";
|
2018-10-30 17:58:10 +08:00
|
|
|
|
memset(strTemp, 0, 100);
|
|
|
|
|
av_base64_encode(strTemp, 100, (uint8_t *) strSPS.data(), strSPS.size());
|
|
|
|
|
_printer << strTemp << ",";
|
|
|
|
|
memset(strTemp, 0, 100);
|
|
|
|
|
av_base64_encode(strTemp, 100, (uint8_t *) strPPS.data(), strPPS.size());
|
|
|
|
|
_printer << strTemp << "\r\n";
|
2020-05-11 22:33:10 +08:00
|
|
|
|
_printer << "a=control:trackID=" << (int)TrackVideo << "\r\n";
|
2018-10-30 17:58:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string getSdp() const override {
|
|
|
|
|
return _printer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodecId getCodecId() const override {
|
|
|
|
|
return CodecH264;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
_StrPrinter _printer;
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
}//namespace mediakit
|
2020-05-11 22:33:10 +08:00
|
|
|
|
#endif //ZLMEDIAKIT_H264_H
|