Commit 92ea9567 by xiongziliang

添加工厂类 统一管理

parent 68fc3bdc
//
// Created by xzl on 2018/10/21.
// Created by xzl on 2018/10/24.
//
#include "Track.h"
#include "Util/util.h"
#include "Util/base64.h"
#include "Factory.h"
Sdp::Ptr Factory::getSdpByTrack(const Track::Ptr &track) {
switch (track->getCodecId()){
case CodecH264:{
H264Track::Ptr h264Track = dynamic_pointer_cast<H264Track>(track);
if(!h264Track){
return nullptr;
}
return std::make_shared<H264Sdp>(h264Track->getSps(),h264Track->getPps());
}
case CodecAAC:{
AACTrack::Ptr aacTrack = dynamic_pointer_cast<AACTrack>(track);
if(!aacTrack){
return nullptr;
}
return std::make_shared<AACSdp>(aacTrack->getAacCfg(),aacTrack->getAudioSampleRate());
}
default:
return nullptr;
}
}
using namespace ZL::Util;
Track::Ptr Track::getTrackBySdp(const string &sdp) {
Track::Ptr Factory::getTrackBySdp(const string &sdp) {
if (strcasestr(sdp.data(), "mpeg4-generic") != nullptr) {
string aac_cfg_str = FindField(sdp.c_str(), "config=", "\r\n");
if (aac_cfg_str.size() != 4) {
......@@ -51,3 +71,33 @@ Track::Ptr Track::getTrackBySdp(const string &sdp) {
return nullptr;
}
RtpCodec::Ptr Factory::getRtpEncoderById(CodecId codecId,
uint32_t ui32Ssrc,
uint32_t ui32MtuSize,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType,
uint8_t ui8Interleaved) {
switch (codecId){
case CodecH264:
return std::make_shared<H264RtpEncoder>(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved);
case CodecAAC:
return std::make_shared<AACRtpEncoder>(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved);
default:
return nullptr;
}
}
RtpCodec::Ptr Factory::getRtpDecoderById(CodecId codecId, uint32_t ui32SampleRate) {
switch (codecId){
case CodecH264:
return std::make_shared<H264RtpDecoder>();
case CodecAAC:
return std::make_shared<AACRtpDecoder>(ui32SampleRate);
default:
return nullptr;
}
}
//
// Created by xzl on 2018/10/24.
//
#ifndef ZLMEDIAKIT_FACTORY_H
#define ZLMEDIAKIT_FACTORY_H
#include <string>
#include "Player/Track.h"
#include "Rtsp/RtspSdp.h"
using namespace std;
using namespace ZL::Rtsp;
class Factory {
public:
/**
* 根据sdp生成Track对象
*/
static Track::Ptr getTrackBySdp(const string &sdp);
/**
* 根据Track生成SDP对象
* @param track 媒体信息
* @return 返回sdp对象
*/
static Sdp::Ptr getSdpByTrack(const Track::Ptr &track);
/**
* 根据CodecId生成Rtp打包器
* @param codecId
* @param ui32Ssrc
* @param ui32MtuSize
* @param ui32SampleRate
* @param ui8PlayloadType
* @param ui8Interleaved
* @return
*/
static RtpCodec::Ptr getRtpEncoderById(CodecId codecId,
uint32_t ui32Ssrc,
uint32_t ui32MtuSize,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType,
uint8_t ui8Interleaved);
/**
* 根据CodecId生成Rtp解包器
* @param codecId
* @param ui32SampleRate
* @return
*/
static RtpCodec::Ptr getRtpDecoderById(CodecId codecId, uint32_t ui32SampleRate);
};
#endif //ZLMEDIAKIT_FACTORY_H
......@@ -20,11 +20,6 @@ public:
typedef std::shared_ptr<Track> Ptr;
Track(){}
virtual ~Track(){}
/**
* 根据sdp生成Track对象
*/
static Ptr getTrackBySdp(const string &sdp);
};
class VideoTrack : public Track {
......
//
// Created by xzl on 2018/10/18.
//
#include "RtpCodec.h"
#include "AACRtpCodec.h"
#include "H264RtpCodec.h"
RtpCodec::Ptr RtpCodec::getRtpEncoderById(CodecId codecId,
uint32_t ui32Ssrc,
uint32_t ui32MtuSize,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType,
uint8_t ui8Interleaved) {
switch (codecId){
case CodecH264:
return std::make_shared<H264RtpEncoder>(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved);
case CodecAAC:
return std::make_shared<AACRtpEncoder>(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved);
default:
return nullptr;
}
}
RtpCodec::Ptr RtpCodec::getRtpDecoderById(CodecId codecId, uint32_t ui32SampleRate) {
switch (codecId){
case CodecH264:
return std::make_shared<H264RtpDecoder>();
case CodecAAC:
return std::make_shared<AACRtpDecoder>(ui32SampleRate);
default:
return nullptr;
}
}
......@@ -161,31 +161,6 @@ public:
typedef std::shared_ptr<RtpCodec> Ptr;
RtpCodec(){}
virtual ~RtpCodec(){}
/**
* 根据CodecId生成Rtp打包器
* @param codecId
* @param ui32Ssrc
* @param ui32MtuSize
* @param ui32SampleRate
* @param ui8PlayloadType
* @param ui8Interleaved
* @return
*/
static Ptr getRtpEncoderById(CodecId codecId,
uint32_t ui32Ssrc,
uint32_t ui32MtuSize,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType,
uint8_t ui8Interleaved);
/**
* 根据CodecId生成Rtp解包器
* @param codecId
* @param ui32SampleRate
* @return
*/
static Ptr getRtpDecoderById(CodecId codecId,uint32_t ui32SampleRate);
};
......
......@@ -29,6 +29,7 @@
#include "RtpParser.h"
#include "Util/base64.h"
#include "H264/SPSParser.h"
#include "Common/Factory.h"
using namespace std;
......@@ -97,10 +98,10 @@ inline bool RtpParser::inputAudio(const RtpPacket::Ptr &rtp) {
inline void RtpParser::onGetAudioTrack(const RtspTrack& audio) {
//生成Track对象
_audioTrack = dynamic_pointer_cast<AudioTrack>(Track::getTrackBySdp(audio.trackSdp));
_audioTrack = dynamic_pointer_cast<AudioTrack>(Factory::getTrackBySdp(audio.trackSdp));
if(_audioTrack){
//生成RtpCodec对象以便解码rtp
_audioRtpDecoder = RtpCodec::getRtpDecoderById(_audioTrack->getCodecId(),_audioTrack->getAudioSampleRate());
_audioRtpDecoder = Factory::getRtpDecoderById(_audioTrack->getCodecId(),_audioTrack->getAudioSampleRate());
if(_audioRtpDecoder){
//设置rtp解码器代理,生成的frame写入该Track
_audioRtpDecoder->setDelegate(_audioTrack);
......@@ -110,10 +111,10 @@ inline void RtpParser::onGetAudioTrack(const RtspTrack& audio) {
inline void RtpParser::onGetVideoTrack(const RtspTrack& video) {
//生成Track对象
_videoTrack = dynamic_pointer_cast<VideoTrack>(Track::getTrackBySdp(video.trackSdp));
_videoTrack = dynamic_pointer_cast<VideoTrack>(Factory::getTrackBySdp(video.trackSdp));
if(_videoTrack){
//生成RtpCodec对象以便解码rtp
_videoRtpDecoder = RtpCodec::getRtpDecoderById(_videoTrack->getCodecId(),90000);
_videoRtpDecoder = Factory::getRtpDecoderById(_videoTrack->getCodecId(),90000);
if(_videoRtpDecoder){
//设置rtp解码器代理,生成的frame写入该Track
_videoRtpDecoder->setDelegate(_videoTrack);
......
//
// Created by xzl on 2018/10/23.
//
#include "RtspSdp.h"
namespace ZL{
namespace Rtsp{
Sdp::Ptr Sdp::getSdpByTrack(const Track::Ptr &track) {
switch (track->getCodecId()){
case CodecH264:{
H264Track::Ptr h264Track = dynamic_pointer_cast<H264Track>(track);
if(!h264Track){
return nullptr;
}
return std::make_shared<H264Sdp>(h264Track->getSps(),h264Track->getPps());
}
#include "RtspSdp.h"
#include "Common/Factory.h"
void Sdp::createRtpEncoder(uint32_t ssrc, int mtu) {
_encoder = Factory::getRtpEncoderById(getCodecId(),
ssrc,
mtu,
_sample_rate,
_playload_type,
getTrackType() * 2);
}
case CodecAAC:{
AACTrack::Ptr aacTrack = dynamic_pointer_cast<AACTrack>(track);
if(!aacTrack){
return nullptr;
}
return std::make_shared<AACSdp>(aacTrack->getAacCfg(),aacTrack->getAudioSampleRate());
void RtspMaker::addTrack(const Track::Ptr &track, uint32_t ssrc, int mtu) {
if (track->getCodecId() == CodecInvalid) {
addTrack(std::make_shared<TitleSdp>(), ssrc, mtu);
} else {
Sdp::Ptr sdp = Factory::getSdpByTrack(track);
if (sdp) {
addTrack(sdp, ssrc, mtu);
}
default:
return nullptr;
}
}
}
}
......@@ -30,13 +30,6 @@ public:
_playload_type = playload_type;
}
/**
* 根据Track生成SDP对象
* @param track 媒体信息
* @return 返回sdp对象
*/
static Ptr getSdpByTrack(const Track::Ptr &track);
virtual ~Sdp(){}
/**
......@@ -115,14 +108,7 @@ public:
* @param ssrc 打包器ssrc,可以为0
* @param mtu mtu大小,一般小于1500字节,推荐1400
*/
virtual void createRtpEncoder(uint32_t ssrc, int mtu) {
_encoder = RtpCodec::getRtpEncoderById(getCodecId(),
ssrc,
mtu,
_sample_rate,
_playload_type,
getTrackType() * 2);
}
virtual void createRtpEncoder(uint32_t ssrc, int mtu);
private:
RtpCodec::Ptr _encoder;
uint8_t _playload_type;
......@@ -311,16 +297,7 @@ public:
* @param ssrc 媒体rtp ssrc
* @param mtu 媒体rtp mtu
*/
void addTrack(const Track::Ptr & track,uint32_t ssrc = 0,int mtu = 1400) {
if(track->getCodecId() == CodecInvalid){
addTrack(std::make_shared<TitleSdp>(),ssrc,mtu);
} else {
Sdp::Ptr sdp = Sdp::getSdpByTrack(track);
if(sdp){
addTrack(sdp,ssrc,mtu);
}
}
}
void addTrack(const Track::Ptr & track,uint32_t ssrc = 0,int mtu = 1400) ;
/**
* 获取完整的SDP字符串
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论