mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-07-06 11:38:11 +08:00
初始提交
This commit is contained in:
85
src/RTP/RtpMaker.h
Normal file
85
src/RTP/RtpMaker.h
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* RtpMaker.h
|
||||
*
|
||||
* Created on: 2016年8月12日
|
||||
* Author: xzl
|
||||
*/
|
||||
|
||||
#ifndef RTP_RTPMAKER_H_
|
||||
#define RTP_RTPMAKER_H_
|
||||
|
||||
#include "Rtsp/RtspMediaSource.h"
|
||||
#include "Util/ResourcePool.h"
|
||||
#include "Util/logger.h"
|
||||
#include "Util/RingBuffer.hpp"
|
||||
#include "Util/TimeTicker.h"
|
||||
#include "Thread/ThreadPool.hpp"
|
||||
|
||||
using namespace ZL::Util;
|
||||
using namespace ZL::Thread;
|
||||
|
||||
namespace ZL {
|
||||
namespace Rtsp {
|
||||
|
||||
class RtpMaker {
|
||||
public:
|
||||
typedef function<void(const RtpPacket::Ptr &pPkt, bool bKeyPos)> onGetRTP;
|
||||
RtpMaker(const onGetRTP &cb, uint32_t ui32Ssrc, int iMtuSize,int iSampleRate,
|
||||
uint8_t ui8PlayloadType, uint8_t ui8Interleaved) {
|
||||
callBack = cb;
|
||||
m_ui32Ssrc = ui32Ssrc;
|
||||
m_ui32SampleRate = iSampleRate;
|
||||
m_iMtuSize = iMtuSize;
|
||||
m_ui8PlayloadType = ui8PlayloadType;
|
||||
m_ui8Interleaved = ui8Interleaved;
|
||||
}
|
||||
virtual ~RtpMaker() {
|
||||
}
|
||||
|
||||
virtual void makeRtp(const char *pcData, int iDataLen, uint32_t uiStamp)=0;
|
||||
|
||||
int getInterleaved() const {
|
||||
return m_ui8Interleaved;
|
||||
}
|
||||
|
||||
int getPlayloadType() const {
|
||||
return m_ui8PlayloadType;
|
||||
}
|
||||
|
||||
int getSampleRate() const {
|
||||
return m_ui32SampleRate;
|
||||
}
|
||||
|
||||
uint32_t getSsrc() const {
|
||||
return m_ui32Ssrc;
|
||||
}
|
||||
|
||||
uint16_t getSeqence() const {
|
||||
return m_ui16Sequence;
|
||||
}
|
||||
uint32_t getTimestamp() const {
|
||||
return m_ui32TimeStamp;
|
||||
}
|
||||
protected:
|
||||
uint32_t m_ui32Ssrc;
|
||||
uint32_t m_ui32SampleRate;
|
||||
int m_iMtuSize;
|
||||
uint8_t m_ui8PlayloadType;
|
||||
uint8_t m_ui8Interleaved;
|
||||
uint16_t m_ui16Sequence = 0;
|
||||
uint32_t m_ui32TimeStamp = 0;
|
||||
virtual void onMakeRtp(const RtpPacket::Ptr &pkt, bool bKeyPos = true) {
|
||||
callBack(pkt, bKeyPos);
|
||||
}
|
||||
inline RtpPacket::Ptr obtainPkt() {
|
||||
return m_pktPool.obtain();
|
||||
}
|
||||
private:
|
||||
RtspMediaSource::PoolType m_pktPool;
|
||||
onGetRTP callBack;
|
||||
};
|
||||
|
||||
} /* namespace RTP */
|
||||
} /* namespace ZL */
|
||||
|
||||
#endif /* RTP_RTPMAKER_H_ */
|
||||
81
src/RTP/RtpMakerAAC.cpp
Normal file
81
src/RTP/RtpMakerAAC.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* RtpMakerAAC.cpp
|
||||
*
|
||||
* Created on: 2016年8月12日
|
||||
* Author: xzl
|
||||
*/
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include "RtpMakerAAC.h"
|
||||
#include "config.h"
|
||||
#include "Util/mini.hpp"
|
||||
|
||||
using namespace ZL::Util;
|
||||
|
||||
|
||||
namespace ZL {
|
||||
namespace Rtsp {
|
||||
|
||||
void RtpMaker_AAC::makeRtp(const char *pcData, int iLen, uint32_t uiStamp) {
|
||||
static uint32_t cycleMS = mINI::Instance()[Config::Rtp::kCycleMS].as<uint32_t>();
|
||||
uiStamp %= cycleMS;
|
||||
char *ptr = (char *) pcData;
|
||||
int iSize = iLen;
|
||||
while (iSize > 0 ) {
|
||||
if (iSize <= m_iMtuSize - 20) {
|
||||
m_aucSectionBuf[0] = 0;
|
||||
m_aucSectionBuf[1] = 16;
|
||||
m_aucSectionBuf[2] = iLen >> 5;
|
||||
m_aucSectionBuf[3] = (iLen & 0x1F) << 3;
|
||||
memcpy(m_aucSectionBuf + 4, ptr, iSize);
|
||||
makeAACRtp(m_aucSectionBuf, iSize + 4, true, uiStamp);
|
||||
break;
|
||||
}
|
||||
m_aucSectionBuf[0] = 0;
|
||||
m_aucSectionBuf[1] = 16;
|
||||
m_aucSectionBuf[2] = (iLen) >> 5;
|
||||
m_aucSectionBuf[3] = (iLen & 0x1F) << 3;
|
||||
memcpy(m_aucSectionBuf + 4, ptr, m_iMtuSize - 20);
|
||||
makeAACRtp(m_aucSectionBuf, m_iMtuSize - 16, false, uiStamp);
|
||||
ptr += (m_iMtuSize - 20);
|
||||
iSize -= (m_iMtuSize - 20);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
inline void RtpMaker_AAC::makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp) {
|
||||
uint16_t u16RtpLen = uiLen + 12;
|
||||
m_ui32TimeStamp = (m_ui32SampleRate / 1000) * uiStamp;
|
||||
uint32_t ts = htonl(m_ui32TimeStamp);
|
||||
uint16_t sq = htons(m_ui16Sequence);
|
||||
uint32_t sc = htonl(m_ui32Ssrc);
|
||||
auto pRtppkt = obtainPkt();
|
||||
auto &rtppkt = *(pRtppkt.get());
|
||||
unsigned char *pucRtp = rtppkt.payload;
|
||||
pucRtp[0] = '$';
|
||||
pucRtp[1] = m_ui8Interleaved;
|
||||
pucRtp[2] = u16RtpLen >> 8;
|
||||
pucRtp[3] = u16RtpLen & 0x00FF;
|
||||
pucRtp[4] = 0x80;
|
||||
pucRtp[5] = (bMark << 7) | m_ui8PlayloadType;
|
||||
memcpy(&pucRtp[6], &sq, 2);
|
||||
memcpy(&pucRtp[8], &ts, 4);
|
||||
//ssrc
|
||||
memcpy(&pucRtp[12], &sc, 4);
|
||||
memcpy(&pucRtp[16], pData, uiLen);
|
||||
rtppkt.PT = m_ui8PlayloadType;
|
||||
rtppkt.interleaved = m_ui8Interleaved;
|
||||
rtppkt.mark = bMark;
|
||||
rtppkt.length = uiLen + 16;
|
||||
rtppkt.sequence = m_ui16Sequence;
|
||||
rtppkt.timeStamp = m_ui32TimeStamp;
|
||||
rtppkt.ssrc = m_ui32Ssrc;
|
||||
rtppkt.type = TrackAudio;
|
||||
|
||||
memcpy(rtppkt.payload + 16, pData, uiLen);
|
||||
onMakeRtp(pRtppkt, false);
|
||||
m_ui16Sequence++;
|
||||
}
|
||||
|
||||
} /* namespace RTP */
|
||||
} /* namespace ZL */
|
||||
42
src/RTP/RtpMakerAAC.h
Normal file
42
src/RTP/RtpMakerAAC.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* RtpMakerAAC.h
|
||||
*
|
||||
* Created on: 2016年8月12日
|
||||
* Author: xzl
|
||||
*/
|
||||
|
||||
#ifndef RTP_RTPMAKERAAC_H_
|
||||
#define RTP_RTPMAKERAAC_H_
|
||||
#include "Rtsp/RtspMediaSource.h"
|
||||
#include "Util/logger.h"
|
||||
#include "Util/RingBuffer.hpp"
|
||||
#include "RtpMaker.h"
|
||||
#include <memory>
|
||||
#include "Util/ResourcePool.h"
|
||||
using namespace std;
|
||||
|
||||
using namespace ZL::Util;
|
||||
|
||||
namespace ZL {
|
||||
namespace Rtsp {
|
||||
|
||||
class RtpMaker_AAC: public RtpMaker {
|
||||
public:
|
||||
typedef std::shared_ptr<RtpMaker_AAC> Ptr;
|
||||
RtpMaker_AAC(const onGetRTP &cb,
|
||||
uint32_t ui32Ssrc, int iMtuSize , int iSampleRate, uint8_t ui8PlayloadType = 97,
|
||||
uint8_t ui8Interleaved = 2) :
|
||||
RtpMaker(cb, ui32Ssrc, iMtuSize,iSampleRate, ui8PlayloadType, ui8Interleaved) {
|
||||
}
|
||||
virtual ~RtpMaker_AAC() {
|
||||
}
|
||||
void makeRtp(const char *pcData, int iDataLen, uint32_t uiStamp) override;
|
||||
private:
|
||||
inline void makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
|
||||
unsigned char m_aucSectionBuf[1600];
|
||||
};
|
||||
|
||||
} /* namespace RTP */
|
||||
} /* namespace ZL */
|
||||
|
||||
#endif /* RTP_RTPMAKERAAC_H_ */
|
||||
98
src/RTP/RtpMakerH264.cpp
Normal file
98
src/RTP/RtpMakerH264.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* RtpMakerH264.cpp
|
||||
*
|
||||
* Created on: 2016年8月12日
|
||||
* Author: xzl
|
||||
*/
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include "RtpMakerH264.h"
|
||||
#include "config.h"
|
||||
#include "Util/mini.hpp"
|
||||
|
||||
using namespace ZL::Util;
|
||||
|
||||
namespace ZL {
|
||||
namespace Rtsp {
|
||||
|
||||
void RtpMaker_H264::makeRtp(const char* pcData, int iLen, uint32_t uiStamp) {
|
||||
static uint32_t cycleMS = mINI::Instance()[Config::Rtp::kCycleMS].as<uint32_t>();
|
||||
uiStamp %= cycleMS;
|
||||
int iSize = m_iMtuSize - 2;
|
||||
if (iLen > iSize) { //超过MTU
|
||||
const unsigned char s_e_r_Start = 0x80;
|
||||
const unsigned char s_e_r_Mid = 0x00;
|
||||
const unsigned char s_e_r_End = 0x40;
|
||||
//获取帧头数据,1byte
|
||||
unsigned char naluType = *((unsigned char *) pcData) & 0x1f; //获取NALU的5bit 帧类型
|
||||
|
||||
unsigned char nal_ref_idc = *((unsigned char *) pcData) & 0x60; //获取NALU的2bit 帧重要程度 00 可以丢 11不能丢
|
||||
//nal_ref_idc = 0x60;
|
||||
//组装FU-A帧头数据 2byte
|
||||
unsigned char f_nri_type = nal_ref_idc + 28;//F为0 1bit,nri上面获取到2bit,28为FU-A分片类型5bit
|
||||
unsigned char s_e_r_type = naluType;
|
||||
bool bFirst = true;
|
||||
bool mark = false;
|
||||
int nOffset = 1;
|
||||
while (!mark) {
|
||||
if (iLen < nOffset + iSize) { //是否拆分结束
|
||||
iSize = iLen - nOffset;
|
||||
mark = true;
|
||||
s_e_r_type = s_e_r_End + naluType;
|
||||
} else {
|
||||
if (bFirst == true) {
|
||||
s_e_r_type = s_e_r_Start + naluType;
|
||||
bFirst = false;
|
||||
} else {
|
||||
s_e_r_type = s_e_r_Mid + naluType;
|
||||
}
|
||||
}
|
||||
memcpy(aucSectionBuf, &f_nri_type, 1);
|
||||
memcpy(aucSectionBuf + 1, &s_e_r_type, 1);
|
||||
memcpy(aucSectionBuf + 2, (unsigned char *) pcData + nOffset, iSize);
|
||||
nOffset += iSize;
|
||||
makeH264Rtp(aucSectionBuf, iSize + 2, mark, uiStamp);
|
||||
}
|
||||
} else {
|
||||
makeH264Rtp(pcData, iLen, true, uiStamp);
|
||||
}
|
||||
}
|
||||
|
||||
inline void RtpMaker_H264::makeH264Rtp(const void* data, unsigned int len, bool mark, uint32_t uiStamp) {
|
||||
uint16_t ui16RtpLen = len + 12;
|
||||
m_ui32TimeStamp = (m_ui32SampleRate / 1000) * uiStamp;
|
||||
uint32_t ts = htonl(m_ui32TimeStamp);
|
||||
uint16_t sq = htons(m_ui16Sequence);
|
||||
uint32_t sc = htonl(m_ui32Ssrc);
|
||||
|
||||
auto pRtppkt = obtainPkt();
|
||||
auto &rtppkt = *(pRtppkt.get());
|
||||
unsigned char *pucRtp = rtppkt.payload;
|
||||
pucRtp[0] = '$';
|
||||
pucRtp[1] = m_ui8Interleaved;
|
||||
pucRtp[2] = ui16RtpLen >> 8;
|
||||
pucRtp[3] = ui16RtpLen & 0x00FF;
|
||||
pucRtp[4] = 0x80;
|
||||
pucRtp[5] = (mark << 7) | m_ui8PlayloadType;
|
||||
memcpy(&pucRtp[6], &sq, 2);
|
||||
memcpy(&pucRtp[8], &ts, 4);
|
||||
//ssrc
|
||||
memcpy(&pucRtp[12], &sc, 4);
|
||||
memcpy(&pucRtp[16], data, len);
|
||||
rtppkt.PT = m_ui8PlayloadType;
|
||||
rtppkt.interleaved = m_ui8Interleaved;
|
||||
rtppkt.mark = mark;
|
||||
rtppkt.length = len + 16;
|
||||
rtppkt.sequence = m_ui16Sequence;
|
||||
rtppkt.timeStamp = m_ui32TimeStamp;
|
||||
rtppkt.ssrc = m_ui32Ssrc;
|
||||
rtppkt.type = TrackVideo;
|
||||
uint8_t type = ((uint8_t *) (data))[0] & 0x1F;
|
||||
memcpy(rtppkt.payload + 16, data, len);
|
||||
onMakeRtp(pRtppkt, type == 5);
|
||||
m_ui16Sequence++;
|
||||
//InfoL<<timeStamp<<" "<<time<<" "<<sampleRate;
|
||||
}
|
||||
|
||||
} /* namespace RTP */
|
||||
} /* namespace ZL */
|
||||
42
src/RTP/RtpMakerH264.h
Normal file
42
src/RTP/RtpMakerH264.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* RtpMakerH264.h
|
||||
*
|
||||
* Created on: 2016年8月12日
|
||||
* Author: xzl
|
||||
*/
|
||||
|
||||
#ifndef RTP_RTPMAKERH264_H_
|
||||
#define RTP_RTPMAKERH264_H_
|
||||
|
||||
#include "Rtsp/RtspMediaSource.h"
|
||||
#include "Util/logger.h"
|
||||
#include "Util/RingBuffer.hpp"
|
||||
#include "RtpMaker.h"
|
||||
#include <memory>
|
||||
using namespace std;
|
||||
using namespace ZL::Util;
|
||||
using namespace ZL::Rtsp;
|
||||
|
||||
namespace ZL {
|
||||
namespace Rtsp {
|
||||
|
||||
class RtpMaker_H264: public RtpMaker {
|
||||
public:
|
||||
typedef std::shared_ptr<RtpMaker_H264> Ptr;
|
||||
RtpMaker_H264(const onGetRTP &cb, uint32_t ui32Ssrc,int iMtuSize = 1400,int iSampleRate = 90000,
|
||||
uint8_t ui8PlayloadType = 96, uint8_t ui8Interleaved = 0) :
|
||||
RtpMaker(cb, ui32Ssrc, iMtuSize,iSampleRate, ui8PlayloadType, ui8Interleaved) {
|
||||
}
|
||||
virtual ~RtpMaker_H264() {
|
||||
}
|
||||
|
||||
void makeRtp(const char *pcData, int iDataLen, uint32_t uiStamp) override;
|
||||
private:
|
||||
inline void makeH264Rtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
|
||||
unsigned char aucSectionBuf[1600];
|
||||
};
|
||||
|
||||
} /* namespace RTP */
|
||||
} /* namespace ZL */
|
||||
|
||||
#endif /* RTP_RTPMAKERH264_H_ */
|
||||
Reference in New Issue
Block a user