Commit a32c97f8 by xiongziliang

优化性能

parent 882210ab
Subproject commit dd0bc0b226bdf2997e3c8bcb53be4705839256c9
#!/bin/bash #!/bin/bash
path=`pwd`
wget https://raw.githubusercontent.com/xiongziliang/ZLToolKit/master/build_for_ios.sh -O toolkit_build.sh
sudo chmod +x ./toolkit_build.sh
./toolkit_build.sh
cd $path
cd ..
git clone --depth=50 https://github.com/xiongziliang/ZLMediaKit.git
cd ZLMediaKit
mkdir -p ios_build mkdir -p ios_build
rm -rf ./build rm -rf ./build
ln -s ./ios_build build ln -s ./ios_build build
......
...@@ -55,11 +55,11 @@ namespace Rtmp { ...@@ -55,11 +55,11 @@ namespace Rtmp {
class RtmpMediaSource: public enable_shared_from_this<RtmpMediaSource> { class RtmpMediaSource: public enable_shared_from_this<RtmpMediaSource> {
public: public:
typedef std::shared_ptr<RtmpMediaSource> Ptr; typedef std::shared_ptr<RtmpMediaSource> Ptr;
typedef RingBuffer<RtmpPacket> RingType; typedef RingBuffer<RtmpPacket::Ptr> RingType;
RtmpMediaSource(const string &strApp, const string &strId) : RtmpMediaSource(const string &strApp, const string &strId) :
m_strApp(strApp), m_strApp(strApp),
m_strId(strId), m_strId(strId),
m_pRing(new RingBuffer<RtmpPacket>()), m_pRing(new RingBuffer<RtmpPacket::Ptr>()),
m_thPool( MediaSender::sendThread()) { m_thPool( MediaSender::sendThread()) {
} }
virtual ~RtmpMediaSource() { virtual ~RtmpMediaSource() {
...@@ -130,15 +130,14 @@ public: ...@@ -130,15 +130,14 @@ public:
virtual void onGetMetaData(const AMFValue &_metadata) { virtual void onGetMetaData(const AMFValue &_metadata) {
m_metadata = _metadata; m_metadata = _metadata;
} }
virtual void onGetMedia(const RtmpPacket &_pkt) { virtual void onGetMedia(const RtmpPacket::Ptr &pkt) {
RtmpPacket & pkt = const_cast<RtmpPacket &>(_pkt); if (pkt->isCfgFrame()) {
if (pkt.isCfgFrame()) {
lock_guard<recursive_mutex> lock(m_mtxMap); lock_guard<recursive_mutex> lock(m_mtxMap);
m_mapCfgFrame.emplace(pkt.typeId, pkt); m_mapCfgFrame.emplace(pkt->typeId, pkt);
} }
auto _ring = m_pRing; auto _ring = m_pRing;
m_thPool.async([_ring,pkt]() { m_thPool.async([_ring,pkt]() {
_ring->write(pkt,pkt.isVideoKeyFrame()); _ring->write(pkt,pkt->isVideoKeyFrame());
}); });
} }
bool seekTo(uint32_t ui32Stamp) { bool seekTo(uint32_t ui32Stamp) {
...@@ -164,11 +163,11 @@ protected: ...@@ -164,11 +163,11 @@ protected:
function<uint32_t()> m_onStamp; function<uint32_t()> m_onStamp;
private: private:
AMFValue m_metadata; AMFValue m_metadata;
unordered_map<int, RtmpPacket> m_mapCfgFrame; unordered_map<int, RtmpPacket::Ptr> m_mapCfgFrame;
mutable recursive_mutex m_mtxMap; mutable recursive_mutex m_mtxMap;
string m_strApp; //媒体app string m_strApp; //媒体app
string m_strId; //媒体id string m_strId; //媒体id
RingBuffer<RtmpPacket>::Ptr m_pRing; //rtp环形缓冲 RingBuffer<RtmpPacket::Ptr>::Ptr m_pRing; //rtp环形缓冲
ThreadPool &m_thPool; ThreadPool &m_thPool;
static unordered_map<string, unordered_map<string,weak_ptr<RtmpMediaSource> > > g_mapMediaSrc; //静态的媒体源表 static unordered_map<string, unordered_map<string,weak_ptr<RtmpMediaSource> > > g_mapMediaSrc; //静态的媒体源表
static recursive_mutex g_mtxMediaSrc; ///访问静态的媒体源表的互斥锁 static recursive_mutex g_mtxMediaSrc; ///访问静态的媒体源表的互斥锁
......
...@@ -78,8 +78,8 @@ RtmpParser::~RtmpParser() { ...@@ -78,8 +78,8 @@ RtmpParser::~RtmpParser() {
// TODO Auto-generated destructor stub // TODO Auto-generated destructor stub
} }
bool RtmpParser::inputRtmp(const RtmpPacket &pkt) { bool RtmpParser::inputRtmp(const RtmpPacket::Ptr &pkt) {
switch (pkt.typeId) { switch (pkt->typeId) {
case MSG_VIDEO: case MSG_VIDEO:
if (m_bHaveVideo) { if (m_bHaveVideo) {
return inputVideo(pkt); return inputVideo(pkt);
...@@ -95,38 +95,38 @@ bool RtmpParser::inputRtmp(const RtmpPacket &pkt) { ...@@ -95,38 +95,38 @@ bool RtmpParser::inputRtmp(const RtmpPacket &pkt) {
} }
} }
inline bool RtmpParser::inputVideo(const RtmpPacket& pkt) { inline bool RtmpParser::inputVideo(const RtmpPacket::Ptr &pkt) {
if (pkt.isCfgFrame()) { if (pkt->isCfgFrame()) {
//WarnL << " got h264 cfg"; //WarnL << " got h264 cfg";
if (m_strSPS.size()) { if (m_strSPS.size()) {
return false; return false;
} }
m_strSPS.assign("\x00\x00\x00\x01", 4); m_strSPS.assign("\x00\x00\x00\x01", 4);
m_strSPS.append(pkt.getH264SPS()); m_strSPS.append(pkt->getH264SPS());
m_strPPS.assign("\x00\x00\x00\x01", 4); m_strPPS.assign("\x00\x00\x00\x01", 4);
m_strPPS.append(pkt.getH264PPS()); m_strPPS.append(pkt->getH264PPS());
getAVCInfo(pkt.getH264SPS(), m_iVideoWidth, m_iVideoHeight, m_fVideoFps); getAVCInfo(pkt->getH264SPS(), m_iVideoWidth, m_iVideoHeight, m_fVideoFps);
return false; return false;
} }
if (m_strSPS.size()) { if (m_strSPS.size()) {
uint32_t iTotalLen = pkt.strBuf.size(); uint32_t iTotalLen = pkt->strBuf.size();
uint32_t iOffset = 5; uint32_t iOffset = 5;
while(iOffset + 4 < iTotalLen){ while(iOffset + 4 < iTotalLen){
uint32_t iFrameLen; uint32_t iFrameLen;
memcpy(&iFrameLen, pkt.strBuf.data() + iOffset, 4); memcpy(&iFrameLen, pkt->strBuf.data() + iOffset, 4);
iFrameLen = ntohl(iFrameLen); iFrameLen = ntohl(iFrameLen);
iOffset += 4; iOffset += 4;
if(iFrameLen + iOffset > iTotalLen){ if(iFrameLen + iOffset > iTotalLen){
break; break;
} }
_onGetH264(pkt.strBuf.data() + iOffset, iFrameLen, pkt.timeStamp); _onGetH264(pkt->strBuf.data() + iOffset, iFrameLen, pkt->timeStamp);
iOffset += iFrameLen; iOffset += iFrameLen;
} }
} }
return pkt.isVideoKeyFrame(); return pkt->isVideoKeyFrame();
} }
inline void RtmpParser::_onGetH264(const char* pcData, int iLen, uint32_t ui32TimeStamp) { inline void RtmpParser::_onGetH264(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
switch (pcData[0] & 0x1F) { switch (pcData[0] & 0x1F) {
...@@ -157,19 +157,19 @@ inline void RtmpParser::onGetH264(const char* pcData, int iLen, uint32_t ui32Tim ...@@ -157,19 +157,19 @@ inline void RtmpParser::onGetH264(const char* pcData, int iLen, uint32_t ui32Tim
m_h264frame.data.clear(); m_h264frame.data.clear();
} }
inline bool RtmpParser::inputAudio(const RtmpPacket& pkt) { inline bool RtmpParser::inputAudio(const RtmpPacket::Ptr &pkt) {
if (pkt.isCfgFrame()) { if (pkt->isCfgFrame()) {
if (m_strAudioCfg.size()) { if (m_strAudioCfg.size()) {
return false; return false;
} }
m_strAudioCfg = pkt.getAacCfg(); m_strAudioCfg = pkt->getAacCfg();
m_iSampleBit = pkt.getAudioSampleBit(); m_iSampleBit = pkt->getAudioSampleBit();
makeAdtsHeader(m_strAudioCfg,m_adts); makeAdtsHeader(m_strAudioCfg,m_adts);
getAACInfo(m_adts, m_iSampleRate, m_iChannel); getAACInfo(m_adts, m_iSampleRate, m_iChannel);
return false; return false;
} }
if (m_strAudioCfg.size()) { if (m_strAudioCfg.size()) {
onGetAAC(pkt.strBuf.data() + 2, pkt.strBuf.size() - 2, pkt.timeStamp); onGetAAC(pkt->strBuf.data() + 2, pkt->strBuf.size() - 2, pkt->timeStamp);
} }
return false; return false;
} }
......
...@@ -48,7 +48,7 @@ public: ...@@ -48,7 +48,7 @@ public:
RtmpParser(const AMFValue &val); RtmpParser(const AMFValue &val);
virtual ~RtmpParser(); virtual ~RtmpParser();
bool inputRtmp(const RtmpPacket &pkt); bool inputRtmp(const RtmpPacket::Ptr &pkt);
void setOnVideoCB(const function<void(const H264Frame &frame)> &cb) override{ void setOnVideoCB(const function<void(const H264Frame &frame)> &cb) override{
lock_guard<recursive_mutex> lck(m_mtxCB); lock_guard<recursive_mutex> lck(m_mtxCB);
...@@ -116,8 +116,8 @@ private: ...@@ -116,8 +116,8 @@ private:
inline void onCheckMedia(const AMFValue &obj); inline void onCheckMedia(const AMFValue &obj);
//返回值:true 代表是i帧第一个rtp包 //返回值:true 代表是i帧第一个rtp包
inline bool inputVideo(const RtmpPacket &pkt); inline bool inputVideo(const RtmpPacket::Ptr &pkt);
inline bool inputAudio(const RtmpPacket &pkt); inline bool inputAudio(const RtmpPacket::Ptr &pkt);
inline void _onGetH264(const char *pcData, int iLen, uint32_t ui32TimeStamp); inline void _onGetH264(const char *pcData, int iLen, uint32_t ui32TimeStamp);
inline void onGetH264(const char *pcData, int iLen, uint32_t ui32TimeStamp); inline void onGetH264(const char *pcData, int iLen, uint32_t ui32TimeStamp);
inline void onGetAAC(const char *pcData, int iLen, uint32_t ui32TimeStamp); inline void onGetAAC(const char *pcData, int iLen, uint32_t ui32TimeStamp);
......
...@@ -312,7 +312,7 @@ void RtmpPlayer::onRtmpChunk(RtmpPacket &chunkData) { ...@@ -312,7 +312,7 @@ void RtmpPlayer::onRtmpChunk(RtmpPacket &chunkData) {
if (m_aNowStampTicker[idx].elapsedTime() > 500) { if (m_aNowStampTicker[idx].elapsedTime() > 500) {
m_adNowStamp[idx] = chunkData.timeStamp; m_adNowStamp[idx] = chunkData.timeStamp;
} }
_onMediaData(chunkData); _onMediaData(std::make_shared<RtmpPacket>(chunkData));
} }
break; break;
default: default:
......
...@@ -59,7 +59,7 @@ public: ...@@ -59,7 +59,7 @@ public:
void teardown() override; void teardown() override;
protected: protected:
virtual bool onCheckMeta(AMFValue &val) =0; virtual bool onCheckMeta(AMFValue &val) =0;
virtual void onMediaData(RtmpPacket &chunkData) =0; virtual void onMediaData(const RtmpPacket::Ptr &chunkData) =0;
float getProgressTime() const; float getProgressTime() const;
void seekToTime(float fTime); void seekToTime(float fTime);
private: private:
...@@ -70,7 +70,7 @@ private: ...@@ -70,7 +70,7 @@ private:
m_pBeatTimer.reset(); m_pBeatTimer.reset();
onShutdown(ex); onShutdown(ex);
} }
void _onMediaData(RtmpPacket &chunkData) { void _onMediaData(const RtmpPacket::Ptr &chunkData) {
m_mediaTicker.resetTime(); m_mediaTicker.resetTime();
onMediaData(chunkData); onMediaData(chunkData);
} }
...@@ -108,6 +108,10 @@ private: ...@@ -108,6 +108,10 @@ private:
void onSendRawData(const char *pcRawData, int iSize) override { void onSendRawData(const char *pcRawData, int iSize) override {
send(pcRawData, iSize); send(pcRawData, iSize);
} }
void onSendRawData(string &&strData) override {
send(std::move(strData));
}
template<typename FUN> template<typename FUN>
inline void addOnResultCB(const FUN &fun) { inline void addOnResultCB(const FUN &fun) {
......
...@@ -69,7 +69,7 @@ private: ...@@ -69,7 +69,7 @@ private:
return false; return false;
} }
} }
void onMediaData(RtmpPacket &chunkData) override { void onMediaData(const RtmpPacket::Ptr &chunkData) override {
if(m_parser){ if(m_parser){
m_parser->inputRtmp(chunkData); m_parser->inputRtmp(chunkData);
} }
......
...@@ -221,7 +221,7 @@ void RtmpProtocol::sendRtmp(uint8_t ui8Type, uint32_t ui32StreamId, ...@@ -221,7 +221,7 @@ void RtmpProtocol::sendRtmp(uint8_t ui8Type, uint32_t ui32StreamId,
strSend.append(strBuf, pos, chunk); strSend.append(strBuf, pos, chunk);
pos += chunk; pos += chunk;
} }
onSendRawData(strSend.data(),strSend.size()); onSendRawData(std::move(strSend));
m_ui32ByteSent += strSend.size(); m_ui32ByteSent += strSend.size();
if (m_ui32WinSize > 0 && m_ui32ByteSent - m_ui32LastSent >= m_ui32WinSize) { if (m_ui32WinSize > 0 && m_ui32ByteSent - m_ui32LastSent >= m_ui32WinSize) {
m_ui32LastSent = m_ui32ByteSent; m_ui32LastSent = m_ui32ByteSent;
......
...@@ -54,6 +54,10 @@ public: ...@@ -54,6 +54,10 @@ public:
void reset(); void reset();
protected: protected:
virtual void onSendRawData(const char *pcRawData,int iSize) = 0; virtual void onSendRawData(const char *pcRawData,int iSize) = 0;
virtual void onSendRawData(string &&strData) {
onSendRawData(strData.data(),strData.size());
};
virtual void onRtmpChunk(RtmpPacket &chunkData) = 0; virtual void onRtmpChunk(RtmpPacket &chunkData) = 0;
virtual void onStreamBegin(uint32_t ui32StreamId){ virtual void onStreamBegin(uint32_t ui32StreamId){
......
...@@ -202,18 +202,18 @@ inline void RtmpPusher::send_metaData(){ ...@@ -202,18 +202,18 @@ inline void RtmpPusher::send_metaData(){
enc << "@setDataFrame" << "onMetaData" << src->getMetaData(); enc << "@setDataFrame" << "onMetaData" << src->getMetaData();
sendRequest(MSG_DATA, enc.data()); sendRequest(MSG_DATA, enc.data());
src->getConfigFrame([&](const RtmpPacket &pkt){ src->getConfigFrame([&](const RtmpPacket::Ptr &pkt){
sendRtmp(pkt.typeId, m_ui32StreamId, pkt.strBuf, pkt.timeStamp, pkt.chunkId); sendRtmp(pkt->typeId, m_ui32StreamId, pkt->strBuf, pkt->timeStamp, pkt->chunkId);
}); });
m_pRtmpReader = src->getRing()->attach(); m_pRtmpReader = src->getRing()->attach();
weak_ptr<RtmpPusher> weakSelf = dynamic_pointer_cast<RtmpPusher>(shared_from_this()); weak_ptr<RtmpPusher> weakSelf = dynamic_pointer_cast<RtmpPusher>(shared_from_this());
m_pRtmpReader->setReadCB([weakSelf](const RtmpPacket &pkt){ m_pRtmpReader->setReadCB([weakSelf](const RtmpPacket::Ptr &pkt){
auto strongSelf = weakSelf.lock(); auto strongSelf = weakSelf.lock();
if(!strongSelf) { if(!strongSelf) {
return; return;
} }
strongSelf->sendRtmp(pkt.typeId, strongSelf->m_ui32StreamId, pkt.strBuf, pkt.timeStamp, pkt.chunkId); strongSelf->sendRtmp(pkt->typeId, strongSelf->m_ui32StreamId, pkt->strBuf, pkt->timeStamp, pkt->chunkId);
}); });
m_pRtmpReader->setDetachCB([weakSelf](){ m_pRtmpReader->setDetachCB([weakSelf](){
auto strongSelf = weakSelf.lock(); auto strongSelf = weakSelf.lock();
......
...@@ -65,6 +65,9 @@ protected: ...@@ -65,6 +65,9 @@ protected:
void onSendRawData(const char *pcRawData, int iSize) override { void onSendRawData(const char *pcRawData, int iSize) override {
send(pcRawData, iSize); send(pcRawData, iSize);
} }
void onSendRawData(string &&strData) override {
send(std::move(strData));
}
private: private:
void init(const RtmpMediaSource::Ptr &src); void init(const RtmpMediaSource::Ptr &src);
void onShutdown(const SockException &ex) { void onShutdown(const SockException &ex) {
......
...@@ -207,14 +207,14 @@ void RtmpSession::doPlay(){ ...@@ -207,14 +207,14 @@ void RtmpSession::doPlay(){
invoke << "onMetaData" << src->getMetaData(); invoke << "onMetaData" << src->getMetaData();
sendResponse(MSG_DATA, invoke.data()); sendResponse(MSG_DATA, invoke.data());
src->getConfigFrame([&](const RtmpPacket &pkt) { src->getConfigFrame([&](const RtmpPacket::Ptr &pkt) {
//DebugL<<"send initial frame"; //DebugL<<"send initial frame";
onSendMedia(pkt); onSendMedia(pkt);
}); });
m_pRingReader = src->getRing()->attach(); m_pRingReader = src->getRing()->attach();
weak_ptr<RtmpSession> weakSelf = dynamic_pointer_cast<RtmpSession>(shared_from_this()); weak_ptr<RtmpSession> weakSelf = dynamic_pointer_cast<RtmpSession>(shared_from_this());
m_pRingReader->setReadCB([weakSelf](const RtmpPacket& pkt){ m_pRingReader->setReadCB([weakSelf](const RtmpPacket::Ptr &pkt){
auto strongSelf = weakSelf.lock(); auto strongSelf = weakSelf.lock();
if(!strongSelf) { if(!strongSelf) {
return; return;
...@@ -271,7 +271,7 @@ void RtmpSession::onCmd_pause(AMFDecoder &dec) { ...@@ -271,7 +271,7 @@ void RtmpSession::onCmd_pause(AMFDecoder &dec) {
m_pRingReader->setReadCB(nullptr); m_pRingReader->setReadCB(nullptr);
} else { } else {
weak_ptr<RtmpSession> weakSelf = dynamic_pointer_cast<RtmpSession>(shared_from_this()); weak_ptr<RtmpSession> weakSelf = dynamic_pointer_cast<RtmpSession>(shared_from_this());
m_pRingReader->setReadCB([weakSelf](const RtmpPacket& pkt) { m_pRingReader->setReadCB([weakSelf](const RtmpPacket::Ptr &pkt) {
auto strongSelf = weakSelf.lock(); auto strongSelf = weakSelf.lock();
if(!strongSelf) { if(!strongSelf) {
return; return;
...@@ -334,7 +334,7 @@ void RtmpSession::onRtmpChunk(RtmpPacket &chunkData) { ...@@ -334,7 +334,7 @@ void RtmpSession::onRtmpChunk(RtmpPacket &chunkData) {
if (!m_pPublisherSrc) { if (!m_pPublisherSrc) {
throw std::runtime_error("Not a rtmp publisher!"); throw std::runtime_error("Not a rtmp publisher!");
} }
m_pPublisherSrc->onGetMedia(chunkData); m_pPublisherSrc->onGetMedia(std::make_shared<RtmpPacket>(chunkData));
if(!m_bPublisherSrcRegisted && m_pPublisherSrc->ready()){ if(!m_bPublisherSrcRegisted && m_pPublisherSrc->ready()){
m_bPublisherSrcRegisted = true; m_bPublisherSrcRegisted = true;
m_pPublisherSrc->regist(); m_pPublisherSrc->regist();
...@@ -363,9 +363,9 @@ void RtmpSession::onCmd_seek(AMFDecoder &dec) { ...@@ -363,9 +363,9 @@ void RtmpSession::onCmd_seek(AMFDecoder &dec) {
sendReply("onStatus", nullptr, status); sendReply("onStatus", nullptr, status);
} }
void RtmpSession::onSendMedia(const RtmpPacket& pkt) { void RtmpSession::onSendMedia(const RtmpPacket::Ptr &pkt) {
auto modifiedStamp = pkt.timeStamp; auto modifiedStamp = pkt->timeStamp;
auto &firstStamp = m_aui32FirstStamp[pkt.typeId % 2]; auto &firstStamp = m_aui32FirstStamp[pkt->typeId % 2];
if(!firstStamp){ if(!firstStamp){
firstStamp = modifiedStamp; firstStamp = modifiedStamp;
} }
...@@ -377,7 +377,7 @@ void RtmpSession::onSendMedia(const RtmpPacket& pkt) { ...@@ -377,7 +377,7 @@ void RtmpSession::onSendMedia(const RtmpPacket& pkt) {
CLEAR_ARR(m_aui32FirstStamp); CLEAR_ARR(m_aui32FirstStamp);
modifiedStamp = 0; modifiedStamp = 0;
} }
sendRtmp(pkt.typeId, pkt.streamId, pkt.strBuf, modifiedStamp, pkt.chunkId); sendRtmp(pkt->typeId, pkt->streamId, pkt->strBuf, modifiedStamp, pkt->chunkId);
} }
} /* namespace Rtmp */ } /* namespace Rtmp */
......
...@@ -60,7 +60,7 @@ private: ...@@ -60,7 +60,7 @@ private:
typedef void (RtmpSession::*rtmpCMDHandle)(AMFDecoder &dec); typedef void (RtmpSession::*rtmpCMDHandle)(AMFDecoder &dec);
static unordered_map<string, rtmpCMDHandle> g_mapCmd; static unordered_map<string, rtmpCMDHandle> g_mapCmd;
RingBuffer<RtmpPacket>::RingReader::Ptr m_pRingReader; RingBuffer<RtmpPacket::Ptr>::RingReader::Ptr m_pRingReader;
std::shared_ptr<RtmpMediaSource> m_pPublisherSrc; std::shared_ptr<RtmpMediaSource> m_pPublisherSrc;
bool m_bPublisherSrcRegisted = false; bool m_bPublisherSrcRegisted = false;
std::weak_ptr<RtmpMediaSource> m_pPlayerSrc; std::weak_ptr<RtmpMediaSource> m_pPlayerSrc;
...@@ -80,10 +80,13 @@ private: ...@@ -80,10 +80,13 @@ private:
void onCmd_pause(AMFDecoder &dec); void onCmd_pause(AMFDecoder &dec);
void setMetaData(AMFDecoder &dec); void setMetaData(AMFDecoder &dec);
void onSendMedia(const RtmpPacket &pkt); void onSendMedia(const RtmpPacket::Ptr &pkt);
void onSendRawData(const char *pcRawData,int iSize) override{ void onSendRawData(const char *pcRawData,int iSize) override{
send(pcRawData, iSize); send(pcRawData, iSize);
} }
void onSendRawData(string &&strData) override{
send(std::move(strData));
}
void onRtmpChunk(RtmpPacket &chunkData) override; void onRtmpChunk(RtmpPacket &chunkData) override;
template<typename first, typename second> template<typename first, typename second>
......
...@@ -73,7 +73,7 @@ public: ...@@ -73,7 +73,7 @@ public:
RtmpMediaSource::onGetMetaData(_metadata); RtmpMediaSource::onGetMetaData(_metadata);
} }
virtual void onGetMedia(const RtmpPacket &pkt) override { virtual void onGetMedia(const RtmpPacket::Ptr &pkt) override {
if (m_pParser) { if (m_pParser) {
if (!m_pRtspSrc && m_pParser->isInited()) { if (!m_pRtspSrc && m_pParser->isInited()) {
makeSDP(); makeSDP();
......
...@@ -62,6 +62,9 @@ private: ...@@ -62,6 +62,9 @@ private:
int send(const string &strBuf) override { int send(const string &strBuf) override {
return m_pSender->send(strBuf); return m_pSender->send(strBuf);
} }
int send(string &&strBuf) override {
return m_pSender->send(std::move(strBuf));
}
int send(const char *pcBuf, int iSize) override { int send(const char *pcBuf, int iSize) override {
return m_pSender->send(pcBuf, iSize); return m_pSender->send(pcBuf, iSize);
} }
......
...@@ -66,40 +66,40 @@ void RtspToRtmpMediaSource::makeVideoConfigPkt() { ...@@ -66,40 +66,40 @@ void RtspToRtmpMediaSource::makeVideoConfigPkt() {
flags |= (FLV_KEY_FRAME << 4); flags |= (FLV_KEY_FRAME << 4);
bool is_config = true; bool is_config = true;
m_rtmpPkt.strBuf.clear(); RtmpPacket::Ptr rtmpPkt(new RtmpPacket);
//////////header //////////header
m_rtmpPkt.strBuf.push_back(flags); rtmpPkt->strBuf.push_back(flags);
m_rtmpPkt.strBuf.push_back(!is_config); rtmpPkt->strBuf.push_back(!is_config);
m_rtmpPkt.strBuf.append("\x0\x0\x0", 3); rtmpPkt->strBuf.append("\x0\x0\x0", 3);
////////////sps ////////////sps
m_rtmpPkt.strBuf.push_back(1); // version rtmpPkt->strBuf.push_back(1); // version
string m_sps = m_pParser->getSps().substr(4); string m_sps = m_pParser->getSps().substr(4);
string m_pps = m_pParser->getPps().substr(4); string m_pps = m_pParser->getPps().substr(4);
//DebugL<<hexdump(m_sps.data(), m_sps.size()); //DebugL<<hexdump(m_sps.data(), m_sps.size());
m_rtmpPkt.strBuf.push_back(m_sps[1]); // profile rtmpPkt->strBuf.push_back(m_sps[1]); // profile
m_rtmpPkt.strBuf.push_back(m_sps[2]); // compat rtmpPkt->strBuf.push_back(m_sps[2]); // compat
m_rtmpPkt.strBuf.push_back(m_sps[3]); // level rtmpPkt->strBuf.push_back(m_sps[3]); // level
m_rtmpPkt.strBuf.push_back(0xff); // 6 bits reserved + 2 bits nal size length - 1 (11) rtmpPkt->strBuf.push_back(0xff); // 6 bits reserved + 2 bits nal size length - 1 (11)
m_rtmpPkt.strBuf.push_back(0xe1); // 3 bits reserved + 5 bits number of sps (00001) rtmpPkt->strBuf.push_back(0xe1); // 3 bits reserved + 5 bits number of sps (00001)
uint16_t size = m_sps.size(); uint16_t size = m_sps.size();
size = htons(size); size = htons(size);
m_rtmpPkt.strBuf.append((char *) &size, 2); rtmpPkt->strBuf.append((char *) &size, 2);
m_rtmpPkt.strBuf.append(m_sps); rtmpPkt->strBuf.append(m_sps);
/////////////pps /////////////pps
m_rtmpPkt.strBuf.push_back(1); // version rtmpPkt->strBuf.push_back(1); // version
size = m_pps.size(); size = m_pps.size();
size = htons(size); size = htons(size);
m_rtmpPkt.strBuf.append((char *) &size, 2); rtmpPkt->strBuf.append((char *) &size, 2);
m_rtmpPkt.strBuf.append(m_pps); rtmpPkt->strBuf.append(m_pps);
m_rtmpPkt.bodySize = m_rtmpPkt.strBuf.size(); rtmpPkt->bodySize = rtmpPkt->strBuf.size();
m_rtmpPkt.chunkId = CHUNK_VIDEO; rtmpPkt->chunkId = CHUNK_VIDEO;
m_rtmpPkt.streamId = STREAM_MEDIA; rtmpPkt->streamId = STREAM_MEDIA;
m_rtmpPkt.timeStamp = 0; rtmpPkt->timeStamp = 0;
m_rtmpPkt.typeId = MSG_VIDEO; rtmpPkt->typeId = MSG_VIDEO;
m_pRtmpSrc->onGetMedia(m_rtmpPkt); m_pRtmpSrc->onGetMedia(rtmpPkt);
} }
void RtspToRtmpMediaSource::onGetH264(const H264Frame& frame) { void RtspToRtmpMediaSource::onGetH264(const H264Frame& frame) {
if(m_pRecorder){ if(m_pRecorder){
...@@ -119,40 +119,40 @@ void RtspToRtmpMediaSource::onGetH264(const H264Frame& frame) { ...@@ -119,40 +119,40 @@ void RtspToRtmpMediaSource::onGetH264(const H264Frame& frame) {
flags |= (FLV_INTER_FRAME << 4); flags |= (FLV_INTER_FRAME << 4);
break; break;
} }
m_rtmpPkt.strBuf.clear(); RtmpPacket::Ptr rtmpPkt(new RtmpPacket);
m_rtmpPkt.strBuf.push_back(flags); rtmpPkt->strBuf.push_back(flags);
m_rtmpPkt.strBuf.push_back(!is_config); rtmpPkt->strBuf.push_back(!is_config);
m_rtmpPkt.strBuf.append("\x0\x0\x0", 3); rtmpPkt->strBuf.append("\x0\x0\x0", 3);
uint32_t size = frame.data.size() - 4; uint32_t size = frame.data.size() - 4;
size = htonl(size); size = htonl(size);
m_rtmpPkt.strBuf.append((char *) &size, 4); rtmpPkt->strBuf.append((char *) &size, 4);
m_rtmpPkt.strBuf.append(&frame.data[4], frame.data.size() - 4); rtmpPkt->strBuf.append(&frame.data[4], frame.data.size() - 4);
m_rtmpPkt.bodySize = m_rtmpPkt.strBuf.size(); rtmpPkt->bodySize = rtmpPkt->strBuf.size();
m_rtmpPkt.chunkId = CHUNK_VIDEO; rtmpPkt->chunkId = CHUNK_VIDEO;
m_rtmpPkt.streamId = STREAM_MEDIA; rtmpPkt->streamId = STREAM_MEDIA;
m_rtmpPkt.timeStamp = frame.timeStamp; rtmpPkt->timeStamp = frame.timeStamp;
m_rtmpPkt.typeId = MSG_VIDEO; rtmpPkt->typeId = MSG_VIDEO;
m_pRtmpSrc->onGetMedia(m_rtmpPkt); m_pRtmpSrc->onGetMedia(rtmpPkt);
} }
void RtspToRtmpMediaSource::onGetAdts(const AdtsFrame& frame) { void RtspToRtmpMediaSource::onGetAdts(const AdtsFrame& frame) {
if(m_pRecorder){ if(m_pRecorder){
m_pRecorder->inputAAC((char *) frame.data, frame.aac_frame_length, frame.timeStamp); m_pRecorder->inputAAC((char *) frame.data, frame.aac_frame_length, frame.timeStamp);
} }
m_rtmpPkt.strBuf.clear(); RtmpPacket::Ptr rtmpPkt(new RtmpPacket);
//////////header //////////header
uint8_t is_config = false; uint8_t is_config = false;
m_rtmpPkt.strBuf.push_back(m_ui8AudioFlags); rtmpPkt->strBuf.push_back(m_ui8AudioFlags);
m_rtmpPkt.strBuf.push_back(!is_config); rtmpPkt->strBuf.push_back(!is_config);
m_rtmpPkt.strBuf.append((char *) frame.data + 7, frame.aac_frame_length - 7); rtmpPkt->strBuf.append((char *) frame.data + 7, frame.aac_frame_length - 7);
m_rtmpPkt.bodySize = m_rtmpPkt.strBuf.size(); rtmpPkt->bodySize = rtmpPkt->strBuf.size();
m_rtmpPkt.chunkId = CHUNK_AUDIO; rtmpPkt->chunkId = CHUNK_AUDIO;
m_rtmpPkt.streamId = STREAM_MEDIA; rtmpPkt->streamId = STREAM_MEDIA;
m_rtmpPkt.timeStamp = frame.timeStamp; rtmpPkt->timeStamp = frame.timeStamp;
m_rtmpPkt.typeId = MSG_AUDIO; rtmpPkt->typeId = MSG_AUDIO;
m_pRtmpSrc->onGetMedia(m_rtmpPkt); m_pRtmpSrc->onGetMedia(rtmpPkt);
} }
void RtspToRtmpMediaSource::makeAudioConfigPkt() { void RtspToRtmpMediaSource::makeAudioConfigPkt() {
...@@ -180,19 +180,19 @@ void RtspToRtmpMediaSource::makeAudioConfigPkt() { ...@@ -180,19 +180,19 @@ void RtspToRtmpMediaSource::makeAudioConfigPkt() {
m_ui8AudioFlags = (flvAudioType << 4) | (flvSampleRate << 2) | (flvSampleBit << 1) | flvStereoOrMono; m_ui8AudioFlags = (flvAudioType << 4) | (flvSampleRate << 2) | (flvSampleBit << 1) | flvStereoOrMono;
m_rtmpPkt.strBuf.clear(); RtmpPacket::Ptr rtmpPkt(new RtmpPacket);
//////////header //////////header
uint8_t is_config = true; uint8_t is_config = true;
m_rtmpPkt.strBuf.push_back(m_ui8AudioFlags); rtmpPkt->strBuf.push_back(m_ui8AudioFlags);
m_rtmpPkt.strBuf.push_back(!is_config); rtmpPkt->strBuf.push_back(!is_config);
m_rtmpPkt.strBuf.append(m_pParser->getAudioCfg()); rtmpPkt->strBuf.append(m_pParser->getAudioCfg());
m_rtmpPkt.bodySize = m_rtmpPkt.strBuf.size(); rtmpPkt->bodySize = rtmpPkt->strBuf.size();
m_rtmpPkt.chunkId = CHUNK_AUDIO; rtmpPkt->chunkId = CHUNK_AUDIO;
m_rtmpPkt.streamId = STREAM_MEDIA; rtmpPkt->streamId = STREAM_MEDIA;
m_rtmpPkt.timeStamp = 0; rtmpPkt->timeStamp = 0;
m_rtmpPkt.typeId = MSG_AUDIO; rtmpPkt->typeId = MSG_AUDIO;
m_pRtmpSrc->onGetMedia(m_rtmpPkt); m_pRtmpSrc->onGetMedia(rtmpPkt);
} }
void RtspToRtmpMediaSource::makeMetaData() { void RtspToRtmpMediaSource::makeMetaData() {
......
...@@ -101,7 +101,6 @@ public: ...@@ -101,7 +101,6 @@ public:
private: private:
RtpParser::Ptr m_pParser; RtpParser::Ptr m_pParser;
RtmpMediaSource::Ptr m_pRtmpSrc; RtmpMediaSource::Ptr m_pRtmpSrc;
RtmpPacket m_rtmpPkt;
uint8_t m_ui8AudioFlags = 0; uint8_t m_ui8AudioFlags = 0;
MediaRecorder::Ptr m_pRecorder; MediaRecorder::Ptr m_pRecorder;
bool m_bEnableFile = true; bool m_bEnableFile = true;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论