ZLMediaKit/src/Extension/H264.h

355 lines
9.9 KiB
C
Raw Normal View History

2018-10-25 10:00:17 +08:00
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
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"
2018-10-30 17:58:10 +08:00
#include "RtspMuxer/RtspSdp.h"
2018-10-21 21:21:14 +08:00
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);
bool getAVCInfo(const char * sps,int sps_len,int &iVideoWidth, int &iVideoHeight, float &iVideoFps);
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
*/
2018-10-30 14:59:42 +08:00
class H264Frame : public Frame {
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,
NAL_B_P = 1
} NalType;
2018-10-30 14:59:42 +08:00
char *data() const override{
return (char *)buffer.data();
}
uint32_t size() const override {
return buffer.size();
}
uint32_t stamp() const override {
return timeStamp;
}
uint32_t prefixSize() const override{
return iPrefixSize;
}
2018-10-23 14:32:06 +08:00
2018-10-30 14:59:42 +08:00
TrackType getTrackType() const override{
return TrackVideo;
}
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 {
2018-10-30 15:56:00 +08:00
return type == NAL_IDR;
2018-10-30 14:59:42 +08:00
}
public:
uint16_t sequence;
uint32_t timeStamp;
unsigned char type;
string buffer;
uint32_t iPrefixSize = 4;
2018-10-21 21:21:14 +08:00
};
2018-10-30 14:59:42 +08:00
class H264FrameNoCopyAble : public FrameNoCopyAble {
2018-10-21 21:21:14 +08:00
public:
2018-10-30 14:59:42 +08:00
typedef std::shared_ptr<H264FrameNoCopyAble> Ptr;
2018-10-23 18:39:17 +08:00
2018-10-30 14:59:42 +08:00
H264FrameNoCopyAble(char *ptr,uint32_t size,uint32_t stamp,int prefixeSize = 4){
buffer_ptr = ptr;
buffer_size = size;
timeStamp = stamp;
iPrefixSize = prefixeSize;
}
2018-10-23 14:32:06 +08:00
2018-10-30 14:59:42 +08:00
TrackType getTrackType() const override{
return TrackVideo;
}
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 {
2018-10-30 15:56:00 +08:00
return H264_TYPE(buffer_ptr[iPrefixSize]) == H264Frame::NAL_IDR;
2018-10-30 14:59:42 +08:00
}
2018-10-21 21:21:14 +08:00
};
2018-10-30 14:59:42 +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:
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 264340x00 00 00 01
* @param pps_prefix_len 264340x00 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 01sps
* @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 01pps
* @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{
return _width;
}
/**
*
* @return
*/
int getVideoWidth() const override{
return _height;
}
/**
* fps
* @return
*/
float getVideoFps() const override{
return _fps;
}
2018-10-23 16:41:25 +08:00
bool ready() override {
return !_sps.empty() && !_pps.empty();
2018-10-23 16:41:25 +08:00
}
/**
* ,sps pps
* @param frame
*/
2018-10-23 21:41:45 +08:00
void inputFrame(const Frame::Ptr &frame) override{
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
if(!_sps.empty() && _last_frame_type != H264Frame::NAL_IDR){
2018-10-28 01:04:18 +08:00
if(!_spsFrame)
{
2018-10-28 00:15:27 +08:00
H264Frame::Ptr insertFrame = std::make_shared<H264Frame>();
2018-10-30 15:56:00 +08:00
insertFrame->type = H264Frame::NAL_SPS;
2018-10-28 01:04:18 +08:00
insertFrame->timeStamp = frame->stamp();
insertFrame->buffer.assign("\x0\x0\x0\x1",4);
2018-10-28 00:15:27 +08:00
insertFrame->buffer.append(_sps);
insertFrame->iPrefixSize = 4;
_spsFrame = insertFrame;
}
2018-10-28 01:04:18 +08:00
_spsFrame->timeStamp = frame->stamp();
2018-10-28 00:15:27 +08:00
VideoTrack::inputFrame(_spsFrame);
2018-10-23 16:41:25 +08:00
}
if(!_pps.empty() && _last_frame_type != H264Frame::NAL_IDR){
2018-10-28 01:04:18 +08:00
if(!_ppsFrame)
{
2018-10-28 00:15:27 +08:00
H264Frame::Ptr insertFrame = std::make_shared<H264Frame>();
2018-10-30 15:56:00 +08:00
insertFrame->type = H264Frame::NAL_PPS;
2018-10-28 01:04:18 +08:00
insertFrame->timeStamp = frame->stamp();
insertFrame->buffer.assign("\x0\x0\x0\x1",4);
2018-10-28 00:15:27 +08:00
insertFrame->buffer.append(_pps);
insertFrame->iPrefixSize = 4;
_ppsFrame = insertFrame;
}
2018-10-28 01:04:18 +08:00
_ppsFrame->timeStamp = frame->stamp();
2018-10-28 00:15:27 +08:00
VideoTrack::inputFrame(_ppsFrame);
2018-10-23 16:41:25 +08:00
}
2018-10-23 21:41:45 +08:00
VideoTrack::inputFrame(frame);
_last_frame_type = type;
2018-10-23 16:41:25 +08:00
}
break;
2018-10-30 15:56:00 +08:00
case H264Frame::NAL_B_P:{
2018-10-23 16:41:25 +08:00
//B or P
2018-10-23 21:41:45 +08:00
VideoTrack::inputFrame(frame);
_last_frame_type = type;
2018-10-23 16:41:25 +08:00
}
break;
}
2018-10-30 16:12:32 +08:00
if(_width == 0 && ready()){
onReady();
}
2018-10-23 16:41:25 +08:00
}
2018-10-23 14:32:06 +08:00
private:
/**
* sps获取宽高fps
*/
2018-10-30 16:12:32 +08:00
void onReady(){
getAVCInfo(_sps,_width,_height,_fps);
2018-10-23 14:32:06 +08:00
}
2018-10-26 17:14:39 +08:00
Track::Ptr clone() override {
return std::make_shared<std::remove_reference<decltype(*this)>::type >(*this);
}
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;
int _last_frame_type = -1;
2018-10-28 00:15:27 +08:00
H264Frame::Ptr _spsFrame;
H264Frame::Ptr _ppsFrame;
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";
_printer << "a=fmtp:" << playload_type << " packetization-mode=1;profile-level-id=";
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;
_printer << ";sprop-parameter-sets=";
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";
_printer << "a=control:trackID=" << getTrackType() << "\r\n";
}
string getSdp() const override {
return _printer;
}
TrackType getTrackType() const override {
return TrackVideo;
}
CodecId getCodecId() const override {
return CodecH264;
}
private:
_StrPrinter _printer;
};
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
#endif //ZLMEDIAKIT_H264_H