添加注释,完善接口

This commit is contained in:
xiongziliang 2018-10-23 14:32:06 +08:00
parent 4af9927e8c
commit c5e372c155
4 changed files with 245 additions and 17 deletions

View File

@ -118,12 +118,15 @@ void getAACInfo(const AACFrame &adts,int &iSampleRate,int &iChannel){
iChannel = adts.channel_configuration; iChannel = adts.channel_configuration;
} }
bool getAVCInfo(const string& strSps,int &iVideoWidth, int &iVideoHeight, float &iVideoFps) { bool getAVCInfo(const string& strSps,int &iVideoWidth, int &iVideoHeight, float &iVideoFps) {
return getAVCInfo(strSps.data(),strSps.size(),iVideoWidth,iVideoHeight,iVideoFps);
}
bool getAVCInfo(const char * sps,int sps_len,int &iVideoWidth, int &iVideoHeight, float &iVideoFps){
T_GetBitContext tGetBitBuf; T_GetBitContext tGetBitBuf;
T_SPS tH264SpsInfo; T_SPS tH264SpsInfo;
memset(&tGetBitBuf,0,sizeof(tGetBitBuf)); memset(&tGetBitBuf,0,sizeof(tGetBitBuf));
memset(&tH264SpsInfo,0,sizeof(tH264SpsInfo)); memset(&tH264SpsInfo,0,sizeof(tH264SpsInfo));
tGetBitBuf.pu8Buf = (uint8_t *)strSps.data() + 1; tGetBitBuf.pu8Buf = (uint8_t*)sps + 1;
tGetBitBuf.iBufSize = strSps.size() - 1; tGetBitBuf.iBufSize = sps_len - 1;
if(0 != h264DecSeqParameterSet((void *) &tGetBitBuf, &tH264SpsInfo)){ if(0 != h264DecSeqParameterSet((void *) &tGetBitBuf, &tH264SpsInfo)){
return false; return false;
} }

View File

@ -45,5 +45,7 @@ void writeAdtsHeader(const AACFrame &adts, uint8_t *pcAdts) ;
string makeAdtsConfig(const uint8_t *pcAdts); string makeAdtsConfig(const uint8_t *pcAdts);
void getAACInfo(const AACFrame &adts,int &iSampleRate,int &iChannel); void getAACInfo(const AACFrame &adts,int &iSampleRate,int &iChannel);
bool getAVCInfo(const string &strSps,int &iVideoWidth, int &iVideoHeight, float &iVideoFps); 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);
#endif /* SRC_PLAYER_PLAYER_H_ */ #endif /* SRC_PLAYER_PLAYER_H_ */

View File

@ -10,11 +10,12 @@
#include "Frame.h" #include "Frame.h"
#include "Util/RingBuffer.h" #include "Util/RingBuffer.h"
#include "Rtsp/Rtsp.h" #include "Rtsp/Rtsp.h"
#include "Player.h"
using namespace std; using namespace std;
using namespace ZL::Util; using namespace ZL::Util;
class Track : public FrameRingInterface , public CodecInfo{ class Track : public FrameRing , public CodecInfo{
public: public:
typedef std::shared_ptr<Track> Ptr; typedef std::shared_ptr<Track> Ptr;
Track(){} Track(){}
@ -24,52 +25,231 @@ public:
class VideoTrack : public Track { class VideoTrack : public Track {
public: public:
TrackType getTrackType() const override { return TrackVideo;}; TrackType getTrackType() const override { return TrackVideo;};
/**
*
* @return
*/
virtual int getVideoHeight() const = 0; virtual int getVideoHeight() const = 0;
/**
*
* @return
*/
virtual int getVideoWidth() const = 0; virtual int getVideoWidth() const = 0;
/**
* fps
* @return
*/
virtual float getVideoFps() const = 0; virtual float getVideoFps() const = 0;
}; };
class AudioTrack : public Track { class AudioTrack : public Track {
public: public:
TrackType getTrackType() const override { return TrackAudio;}; TrackType getTrackType() const override { return TrackAudio;};
/**
*
* @return
*/
virtual int getAudioSampleRate() const = 0; virtual int getAudioSampleRate() const = 0;
/**
* 168
* @return
*/
virtual int getAudioSampleBit() const = 0; virtual int getAudioSampleBit() const = 0;
/**
*
* @return
*/
virtual int getAudioChannel() const = 0; virtual int getAudioChannel() const = 0;
}; };
class H264Track : public VideoTrack{ class H264Track : public VideoTrack{
public: public:
H264Track(const string &sps,const string &pps){
_sps = sps; /**
_pps = pps; * 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);
}
/**
* 0x00 00 00 01sps
* @return
*/
const string &getSps() const{ const string &getSps() const{
return _sps; return _sps;
} }
/**
* 0x00 00 00 01pps
* @return
*/
const string &getPps() const{ const string &getPps() const{
return _pps; return _pps;
} }
CodecId getCodecId() const override { CodecId getCodecId() const override {
return CodecH264; return CodecH264;
} }
/**
*
* @return
*/
int getVideoHeight() const override{
return _width;
}
/**
*
* @return
*/
int getVideoWidth() const override{
return _height;
}
/**
* fps
* @return
*/
float getVideoFps() const override{
return _fps;
}
private:
/**
* sps获取宽高fps
* @param sps sps不含头数据
*/
void parseSps(const string &sps){
getAVCInfo(sps,_width,_height,_fps);
}
private: private:
string _sps; string _sps;
string _pps; string _pps;
int _width = 0;
int _height = 0;
float _fps = 0;
}; };
class AACTrack : public AudioTrack{ class AACTrack : public AudioTrack{
public: public:
/**
* aac类型的媒体
* @param aac_cfg aac两个字节的配置信息
*/
AACTrack(const string &aac_cfg){ AACTrack(const string &aac_cfg){
_cfg = aac_cfg; if(aac_cfg.size() != 2){
throw std::invalid_argument("adts配置必须为2个字节");
} }
_cfg = aac_cfg;
parseAacCfg(_cfg);
}
/**
* 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
*/
const string &getAacCfg() const{ const string &getAacCfg() const{
return _cfg; return _cfg;
} }
/**
*
* @return
*/
CodecId getCodecId() const override{ CodecId getCodecId() const override{
return CodecAAC; return CodecAAC;
} }
/**
*
* @return
*/
int getAudioSampleRate() const override{
return _sampleRate;
}
/**
* 168
* @return
*/
int getAudioSampleBit() const override{
return _sampleBit;
}
/**
*
* @return
*/
int getAudioChannel() const override{
return _channel;
}
private:
/**
* 2aac配置
* @param aac_cfg
*/
void parseAacCfg(const string &aac_cfg){
AACFrame aacFrame;
makeAdtsHeader(aac_cfg,aacFrame);
getAACInfo(aacFrame,_sampleRate,_channel);
}
private: private:
string _cfg; string _cfg;
int _sampleRate = 0;
int _sampleBit = 16;
int _channel = 0;
}; };

View File

@ -15,51 +15,98 @@ namespace Rtsp{
/** /**
* sdp基类 * sdp基类
*/ */
class Sdp : public Track , public RtpRingInterface{ class Sdp : public FrameRingInterface , public RtpRingInterface , public CodecInfo{
public: public:
typedef std::shared_ptr<Sdp> Ptr; typedef std::shared_ptr<Sdp> Ptr;
/**
* sdp
* @param sample_rate
* @param playload_type pt类型
*/
Sdp(uint32_t sample_rate, uint8_t playload_type){ Sdp(uint32_t sample_rate, uint8_t playload_type){
_sample_rate = sample_rate; _sample_rate = sample_rate;
_playload_type = playload_type; _playload_type = playload_type;
} }
virtual ~Sdp(){} virtual ~Sdp(){}
/** /**
* sdp字符串 * sdp字符串
* @return * @return
*/ */
virtual string getSdp() const = 0; virtual string getSdp() const = 0;
/**
*
* @return
*/
TrackType getTrackType() const override { TrackType getTrackType() const override {
return TrackInvalid; return TrackInvalid;
} }
/**
* id
* @return
*/
CodecId getCodecId() const override{ CodecId getCodecId() const override{
return CodecInvalid; return CodecInvalid;
} }
/**
*
* @return
*/
FrameRingInterface::RingType::Ptr getFrameRing() const override { FrameRingInterface::RingType::Ptr getFrameRing() const override {
return _encoder->getFrameRing(); return _encoder->getFrameRing();
} }
/**
* rtp环形缓存
* @return
*/
RtpRingInterface::RingType::Ptr getRtpRing() const override{ RtpRingInterface::RingType::Ptr getRtpRing() const override{
return _encoder->getRtpRing(); return _encoder->getRtpRing();
} }
/**
* rtp打包
* @param frame
* @param key_pos
*/
void inputFrame(const Frame::Ptr &frame,bool key_pos) override{ void inputFrame(const Frame::Ptr &frame,bool key_pos) override{
_encoder->inputFrame(frame,key_pos); _encoder->inputFrame(frame,key_pos);
} }
/**
* rtp后直接输入rtp
* @param rtp rtp数据包
* @param key_pos rtp包
*/
void inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) override{ void inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) override{
_encoder->inputRtp(rtp,key_pos); _encoder->inputRtp(rtp,key_pos);
} }
/**
* rtp打包器共用一个环形缓存
* @param ring
*/
void setFrameRing(const FrameRingInterface::RingType::Ptr &ring) override{ void setFrameRing(const FrameRingInterface::RingType::Ptr &ring) override{
_encoder->setFrameRing(ring); _encoder->setFrameRing(ring);
} }
/**
* rtp打包器共用一个环形缓存
* @param ring
*/
void setRtpRing(const RtpRingInterface::RingType::Ptr &ring) override{ void setRtpRing(const RtpRingInterface::RingType::Ptr &ring) override{
_encoder->setRtpRing(ring); _encoder->setRtpRing(ring);
} }
/**
* Rtp打包器
* @param ssrc ssrc0
* @param mtu mtu大小15001400
*/
virtual void createRtpEncoder(uint32_t ssrc, int mtu) { virtual void createRtpEncoder(uint32_t ssrc, int mtu) {
_encoder = RtpCodec::getRtpCodec (getCodecId(), _encoder = RtpCodec::getRtpCodec (getCodecId(),
ssrc, ssrc,
@ -127,20 +174,17 @@ public:
* *
* @param sps 264 sps,0x00000001 * @param sps 264 sps,0x00000001
* @param pps 264 pps,0x00000001 * @param pps 264 pps,0x00000001
* @param sample_rate 90000
* @param playload_type rtp playload type 96 * @param playload_type rtp playload type 96
* @param track_id trackID TrackVideo
* @param bitrate * @param bitrate
*/ */
H264Sdp(const string &sps, H264Sdp(const string &sps,
const string &pps, const string &pps,
int sample_rate = 90000,
int playload_type = 96, int playload_type = 96,
int bitrate = 4000) : Sdp(sample_rate,playload_type) { int bitrate = 4000) : Sdp(90000,playload_type) {
//视频通道 //视频通道
_printer << "m=video 0 RTP/AVP " << playload_type << "\r\n"; _printer << "m=video 0 RTP/AVP " << playload_type << "\r\n";
_printer << "b=AS:" << bitrate << "\r\n"; _printer << "b=AS:" << bitrate << "\r\n";
_printer << "a=rtpmap:" << playload_type << " H264/" << sample_rate << "\r\n"; _printer << "a=rtpmap:" << playload_type << " H264/" << 90000 << "\r\n";
_printer << "a=fmtp:" << playload_type << " packetization-mode=1;profile-level-id="; _printer << "a=fmtp:" << playload_type << " packetization-mode=1;profile-level-id=";
char strTemp[100]; char strTemp[100];
@ -186,11 +230,10 @@ class AACSdp : public Sdp {
public: public:
/** /**
* aac sdp *
* @param aac_cfg aac两个字节的配置描述 * @param aac_cfg aac两个字节的配置描述
* @param sample_rate * @param sample_rate
* @param playload_type rtp playload type 96 * @param playload_type rtp playload type 98
* @param track_id trackID TrackVideo
* @param bitrate * @param bitrate
*/ */
AACSdp(const string &aac_cfg, AACSdp(const string &aac_cfg,