ZLMediaKit/src/Player/Track.h

358 lines
8.5 KiB
C
Raw Normal View History

2018-10-24 15:49:51 +08:00
//
2018-10-21 21:21:14 +08:00
// Created by xzl on 2018/10/21.
//
#ifndef ZLMEDIAKIT_TRACK_H
#define ZLMEDIAKIT_TRACK_H
#include <memory>
#include <string>
#include "Frame.h"
#include "Util/RingBuffer.h"
#include "Rtsp/Rtsp.h"
2018-10-23 14:32:06 +08:00
#include "Player.h"
2018-10-24 17:17:55 +08:00
using namespace toolkit;
2018-10-21 21:21:14 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit{
2018-10-21 21:21:14 +08:00
2018-10-23 14:32:06 +08:00
class Track : public FrameRing , public CodecInfo{
2018-10-21 21:21:14 +08:00
public:
2018-10-23 11:47:27 +08:00
typedef std::shared_ptr<Track> Ptr;
Track(){}
virtual ~Track(){}
2018-10-21 21:21:14 +08:00
};
2018-10-23 11:47:27 +08:00
class VideoTrack : public Track {
2018-10-21 21:21:14 +08:00
public:
2018-10-23 18:39:17 +08:00
typedef std::shared_ptr<VideoTrack> Ptr;
2018-10-21 21:21:14 +08:00
TrackType getTrackType() const override { return TrackVideo;};
2018-10-23 14:32:06 +08:00
/**
*
* @return
*/
2018-10-21 21:21:14 +08:00
virtual int getVideoHeight() const = 0;
2018-10-23 14:32:06 +08:00
/**
*
* @return
*/
2018-10-21 21:21:14 +08:00
virtual int getVideoWidth() const = 0;
2018-10-23 14:32:06 +08:00
/**
* fps
* @return
*/
2018-10-21 21:21:14 +08:00
virtual float getVideoFps() const = 0;
};
2018-10-23 11:47:27 +08:00
class AudioTrack : public Track {
2018-10-21 21:21:14 +08:00
public:
2018-10-23 18:39:17 +08:00
typedef std::shared_ptr<AudioTrack> Ptr;
2018-10-21 21:21:14 +08:00
TrackType getTrackType() const override { return TrackAudio;};
2018-10-23 14:32:06 +08:00
/**
*
* @return
*/
2018-10-21 21:21:14 +08:00
virtual int getAudioSampleRate() const = 0;
2018-10-23 14:32:06 +08:00
/**
* 168
* @return
*/
2018-10-21 21:21:14 +08:00
virtual int getAudioSampleBit() const = 0;
2018-10-23 14:32:06 +08:00
/**
*
* @return
*/
2018-10-21 21:21:14 +08:00
virtual int getAudioChannel() const = 0;
};
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);
parseSps(_sps);
}
/**
* 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());
parseSps(_sps);
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
TrackType getTrackType() const override {
if(_sps.empty() || _pps.empty()){
return TrackInvalid;
}
2018-10-24 10:03:51 +08:00
return TrackVideo;
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-23 16:41:25 +08:00
int type = (*((uint8_t *)frame->data() + frame->prefixSize())) & 0x1F;
switch (type){
case 7:{
//sps
bool flag = _sps.empty();
_sps = string(frame->data() + frame->prefixSize(),frame->size() - frame->prefixSize());
if(flag && _width == 0){
parseSps(_sps);
}
}
break;
case 8:{
//pps
_pps = string(frame->data() + frame->prefixSize(),frame->size() - frame->prefixSize());
}
break;
case 5:{
//I
if(!_sps.empty()){
H264Frame::Ptr insertFrame = std::make_shared<H264Frame>();
insertFrame->timeStamp = frame->stamp();
insertFrame->type = 7;
insertFrame->buffer = _sps;
insertFrame->iPrefixSize = 0;
2018-10-23 21:41:45 +08:00
VideoTrack::inputFrame(insertFrame);
2018-10-23 16:41:25 +08:00
}
if(!_pps.empty()){
H264Frame::Ptr insertFrame = std::make_shared<H264Frame>();
insertFrame->timeStamp = frame->stamp();
insertFrame->type = 8;
insertFrame->buffer = _pps;
insertFrame->iPrefixSize = 0;
2018-10-23 21:41:45 +08:00
VideoTrack::inputFrame(insertFrame);
2018-10-23 16:41:25 +08:00
}
2018-10-23 21:41:45 +08:00
VideoTrack::inputFrame(frame);
2018-10-23 16:41:25 +08:00
}
break;
case 1:{
//B or P
2018-10-23 21:41:45 +08:00
VideoTrack::inputFrame(frame);
2018-10-23 16:41:25 +08:00
}
break;
}
}
2018-10-23 14:32:06 +08:00
private:
/**
* sps获取宽高fps
* @param sps sps不含头数据
*/
void parseSps(const string &sps){
getAVCInfo(sps,_width,_height,_fps);
}
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;
2018-10-21 21:21:14 +08:00
};
2018-10-23 11:47:27 +08:00
class AACTrack : public AudioTrack{
2018-10-21 21:21:14 +08:00
public:
2018-10-23 18:39:17 +08:00
typedef std::shared_ptr<AACTrack> Ptr;
2018-10-24 18:41:37 +08:00
/**
* adts头信息
* inputFrame中获取adts头信息
*/
AACTrack(){}
2018-10-23 14:32:06 +08:00
/**
* aac类型的媒体
* @param aac_cfg aac两个字节的配置信息
*/
2018-10-23 11:47:27 +08:00
AACTrack(const string &aac_cfg){
2018-10-23 14:32:06 +08:00
if(aac_cfg.size() != 2){
throw std::invalid_argument("adts配置必须为2个字节");
}
2018-10-21 21:21:14 +08:00
_cfg = aac_cfg;
2018-10-23 14:32:06 +08:00
parseAacCfg(_cfg);
2018-10-21 21:21:14 +08:00
}
2018-10-23 14:32:06 +08:00
/**
* aac类型的媒体
* @param adts_header adts头7
* @param adts_header_len adts头长度7
*/
AACTrack(const char *adts_header,int adts_header_len = 7){
if(adts_header_len < 7){
throw std::invalid_argument("adts头必须不少于7个字节");
}
_cfg = makeAdtsConfig((uint8_t*)adts_header);
parseAacCfg(_cfg);
}
/**
* aac类型的媒体
* @param aac_frame_with_adts adts头的aac帧
*/
AACTrack(const Frame::Ptr &aac_frame_with_adts){
if(aac_frame_with_adts->getCodecId() != CodecAAC || aac_frame_with_adts->prefixSize() < 7){
throw std::invalid_argument("必须输入带adts头的aac帧");
}
_cfg = makeAdtsConfig((uint8_t*)aac_frame_with_adts->data());
parseAacCfg(_cfg);
}
/**
* aac两个字节的配置
* @return
*/
2018-10-21 21:21:14 +08:00
const string &getAacCfg() const{
return _cfg;
}
2018-10-23 14:32:06 +08:00
/**
*
* @return
*/
2018-10-23 11:09:21 +08:00
CodecId getCodecId() const override{
return CodecAAC;
2018-10-21 21:21:14 +08:00
}
2018-10-23 14:32:06 +08:00
2018-10-24 18:41:37 +08:00
/**
* aac_cfg前是无效的Track
* @return
*/
TrackType getTrackType() const override {
if(_cfg.empty()){
return TrackInvalid;
}
return TrackAudio;
}
2018-10-23 14:32:06 +08:00
/**
*
* @return
*/
int getAudioSampleRate() const override{
return _sampleRate;
}
/**
* 168
* @return
*/
int getAudioSampleBit() const override{
return _sampleBit;
}
/**
*
* @return
*/
int getAudioChannel() const override{
return _channel;
}
2018-10-24 18:41:37 +08:00
/**
* ,aac_cfg
* @param frame
*/
void inputFrame(const Frame::Ptr &frame) override{
if(_cfg.empty() && frame->prefixSize() == 7){
//7个字节的adts头
_cfg = makeAdtsConfig(reinterpret_cast<const uint8_t *>(frame->data()));
parseAacCfg(_cfg);
}
AudioTrack::inputFrame(frame);
}
2018-10-23 14:32:06 +08:00
private:
/**
* 2aac配置
* @param aac_cfg
*/
void parseAacCfg(const string &aac_cfg){
AACFrame aacFrame;
makeAdtsHeader(aac_cfg,aacFrame);
getAACInfo(aacFrame,_sampleRate,_channel);
}
2018-10-21 21:21:14 +08:00
private:
string _cfg;
2018-10-23 14:32:06 +08:00
int _sampleRate = 0;
int _sampleBit = 16;
int _channel = 0;
2018-10-21 21:21:14 +08:00
};
2018-10-24 17:17:55 +08:00
}//namespace mediakit
2018-10-21 21:21:14 +08:00
#endif //ZLMEDIAKIT_TRACK_H