mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2025-02-11 13:19:42 +08:00
提炼rtp打包代码
This commit is contained in:
parent
addf90b88a
commit
53ba766c2a
@ -71,40 +71,8 @@ void AACRtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
}
|
||||
}
|
||||
|
||||
void AACRtpEncoder::makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp) {
|
||||
uint16_t u16RtpLen = uiLen + 12;
|
||||
uint32_t ts = htonl((_ui32SampleRate / 1000) * uiStamp);
|
||||
uint16_t sq = htons(_ui16Sequence);
|
||||
uint32_t sc = htonl(_ui32Ssrc);
|
||||
auto pRtppkt = ResourcePoolHelper<RtpPacket>::obtainObj();
|
||||
auto &rtppkt = *pRtppkt;
|
||||
unsigned char *pucRtp = rtppkt.payload;
|
||||
pucRtp[0] = '$';
|
||||
pucRtp[1] = _ui8Interleaved;
|
||||
pucRtp[2] = u16RtpLen >> 8;
|
||||
pucRtp[3] = u16RtpLen & 0x00FF;
|
||||
pucRtp[4] = 0x80;
|
||||
pucRtp[5] = (bMark << 7) | _ui8PlayloadType;
|
||||
memcpy(&pucRtp[6], &sq, 2);
|
||||
memcpy(&pucRtp[8], &ts, 4);
|
||||
//ssrc
|
||||
memcpy(&pucRtp[12], &sc, 4);
|
||||
//playload
|
||||
memcpy(&pucRtp[16], pData, uiLen);
|
||||
|
||||
rtppkt.PT = _ui8PlayloadType;
|
||||
rtppkt.interleaved = _ui8Interleaved;
|
||||
rtppkt.mark = bMark;
|
||||
rtppkt.length = uiLen + 16;
|
||||
rtppkt.sequence = _ui16Sequence;
|
||||
rtppkt.timeStamp = uiStamp;
|
||||
rtppkt.ssrc = _ui32Ssrc;
|
||||
rtppkt.type = TrackAudio;
|
||||
rtppkt.offset = 16;
|
||||
|
||||
RtpCodec::inputRtp(pRtppkt, false);
|
||||
_ui16Sequence++;
|
||||
_ui32TimeStamp = uiStamp;
|
||||
void AACRtpEncoder::makeAACRtp(const void *data, unsigned int len, bool mark, uint32_t uiStamp) {
|
||||
RtpCodec::inputRtp(makeRtp(getTrackType(),data,len,mark,uiStamp), false);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -184,6 +184,7 @@ void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
auto pcData = frame->data() + frame->prefixSize();
|
||||
auto uiStamp = frame->stamp();
|
||||
auto iLen = frame->size() - frame->prefixSize();
|
||||
unsigned char naluType = H264_TYPE(pcData[0]); //获取NALU的5bit 帧类型
|
||||
|
||||
uiStamp %= cycleMS;
|
||||
int iSize = _ui32MtuSize - 2;
|
||||
@ -192,8 +193,6 @@ void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
const unsigned char s_e_r_Mid = 0x00;
|
||||
const unsigned char s_e_r_End = 0x40;
|
||||
//获取帧头数据,1byte
|
||||
unsigned char naluType = H264_TYPE(pcData[0]); //获取NALU的5bit 帧类型
|
||||
|
||||
unsigned char nal_ref_idc = *((unsigned char *) pcData) & 0x60; //获取NALU的2bit 帧重要程度 00 可以丢 11不能丢
|
||||
//nal_ref_idc = 0x60;
|
||||
//组装FU-A帧头数据 2byte
|
||||
@ -219,48 +218,15 @@ void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
memcpy(_aucSectionBuf + 1, &s_e_r_type, 1);
|
||||
memcpy(_aucSectionBuf + 2, (unsigned char *) pcData + nOffset, iSize);
|
||||
nOffset += iSize;
|
||||
makeH264Rtp(_aucSectionBuf, iSize + 2, mark, uiStamp);
|
||||
makeH264Rtp(naluType,_aucSectionBuf, iSize + 2, mark, uiStamp);
|
||||
}
|
||||
} else {
|
||||
makeH264Rtp(pcData, iLen, true, uiStamp);
|
||||
makeH264Rtp(naluType,pcData, iLen, true, uiStamp);
|
||||
}
|
||||
}
|
||||
|
||||
void H264RtpEncoder::makeH264Rtp(const void* data, unsigned int len, bool mark, uint32_t uiStamp) {
|
||||
uint16_t ui16RtpLen = len + 12;
|
||||
uint32_t ts = htonl((_ui32SampleRate / 1000) * uiStamp);
|
||||
uint16_t sq = htons(_ui16Sequence);
|
||||
uint32_t sc = htonl(_ui32Ssrc);
|
||||
|
||||
auto rtppkt = ResourcePoolHelper<RtpPacket>::obtainObj();
|
||||
unsigned char *pucRtp = rtppkt->payload;
|
||||
pucRtp[0] = '$';
|
||||
pucRtp[1] = _ui8Interleaved;
|
||||
pucRtp[2] = ui16RtpLen >> 8;
|
||||
pucRtp[3] = ui16RtpLen & 0x00FF;
|
||||
pucRtp[4] = 0x80;
|
||||
pucRtp[5] = (mark << 7) | _ui8PlayloadType;
|
||||
memcpy(&pucRtp[6], &sq, 2);
|
||||
memcpy(&pucRtp[8], &ts, 4);
|
||||
//ssrc
|
||||
memcpy(&pucRtp[12], &sc, 4);
|
||||
//playload
|
||||
memcpy(&pucRtp[16], data, len);
|
||||
|
||||
rtppkt->PT = _ui8PlayloadType;
|
||||
rtppkt->interleaved = _ui8Interleaved;
|
||||
rtppkt->mark = mark;
|
||||
rtppkt->length = len + 16;
|
||||
rtppkt->sequence = _ui16Sequence;
|
||||
rtppkt->timeStamp = uiStamp;
|
||||
rtppkt->ssrc = _ui32Ssrc;
|
||||
rtppkt->type = TrackVideo;
|
||||
rtppkt->offset = 16;
|
||||
|
||||
uint8_t type = H264_TYPE(((uint8_t *) (data))[0]);
|
||||
RtpCodec::inputRtp(rtppkt,type == H264Frame::NAL_SPS);
|
||||
_ui16Sequence++;
|
||||
_ui32TimeStamp = uiStamp;
|
||||
void H264RtpEncoder::makeH264Rtp(int nal_type,const void* data, unsigned int len, bool mark, uint32_t uiStamp) {
|
||||
RtpCodec::inputRtp(makeRtp(getTrackType(),data,len,mark,uiStamp),nal_type == H264Frame::NAL_SPS);
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
@ -93,7 +93,7 @@ public:
|
||||
*/
|
||||
void inputFrame(const Frame::Ptr &frame) override;
|
||||
private:
|
||||
void makeH264Rtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
|
||||
void makeH264Rtp(int nal_type,const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
|
||||
private:
|
||||
unsigned char _aucSectionBuf[1600];
|
||||
};
|
||||
|
@ -210,38 +210,7 @@ void H265RtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
}
|
||||
|
||||
void H265RtpEncoder::makeH265Rtp(int nal_type,const void* data, unsigned int len, bool mark, uint32_t uiStamp) {
|
||||
uint16_t ui16RtpLen = len + 12;
|
||||
uint32_t ts = htonl((_ui32SampleRate / 1000) * uiStamp);
|
||||
uint16_t sq = htons(_ui16Sequence);
|
||||
uint32_t sc = htonl(_ui32Ssrc);
|
||||
|
||||
auto rtppkt = ResourcePoolHelper<RtpPacket>::obtainObj();
|
||||
unsigned char *pucRtp = rtppkt->payload;
|
||||
pucRtp[0] = '$';
|
||||
pucRtp[1] = _ui8Interleaved;
|
||||
pucRtp[2] = ui16RtpLen >> 8;
|
||||
pucRtp[3] = ui16RtpLen & 0x00FF;
|
||||
pucRtp[4] = 0x80;
|
||||
pucRtp[5] = (mark << 7) | _ui8PlayloadType;
|
||||
memcpy(&pucRtp[6], &sq, 2);
|
||||
memcpy(&pucRtp[8], &ts, 4);
|
||||
//ssrc
|
||||
memcpy(&pucRtp[12], &sc, 4);
|
||||
//playload
|
||||
memcpy(&pucRtp[16], data, len);
|
||||
|
||||
rtppkt->PT = _ui8PlayloadType;
|
||||
rtppkt->interleaved = _ui8Interleaved;
|
||||
rtppkt->mark = mark;
|
||||
rtppkt->length = len + 16;
|
||||
rtppkt->sequence = _ui16Sequence;
|
||||
rtppkt->timeStamp = uiStamp;
|
||||
rtppkt->ssrc = _ui32Ssrc;
|
||||
rtppkt->type = TrackVideo;
|
||||
rtppkt->offset = 16;
|
||||
RtpCodec::inputRtp(rtppkt,nal_type == H265Frame::NAL_VPS);
|
||||
_ui16Sequence++;
|
||||
_ui32TimeStamp = uiStamp;
|
||||
RtpCodec::inputRtp(makeRtp(getTrackType(),data,len,mark,uiStamp),nal_type == H265Frame::NAL_VPS);
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
68
src/RtspMuxer/RtpCodec.cpp
Normal file
68
src/RtspMuxer/RtpCodec.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "RtpCodec.h"
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
RtpPacket::Ptr RtpInfo::makeRtp(TrackType type, const void* data, unsigned int len, bool mark, uint32_t uiStamp) {
|
||||
uint16_t ui16RtpLen = len + 12;
|
||||
uint32_t ts = htonl((_ui32SampleRate / 1000) * uiStamp);
|
||||
uint16_t sq = htons(_ui16Sequence);
|
||||
uint32_t sc = htonl(_ui32Ssrc);
|
||||
|
||||
auto rtppkt = ResourcePoolHelper<RtpPacket>::obtainObj();
|
||||
unsigned char *pucRtp = rtppkt->payload;
|
||||
pucRtp[0] = '$';
|
||||
pucRtp[1] = _ui8Interleaved;
|
||||
pucRtp[2] = ui16RtpLen >> 8;
|
||||
pucRtp[3] = ui16RtpLen & 0x00FF;
|
||||
pucRtp[4] = 0x80;
|
||||
pucRtp[5] = (mark << 7) | _ui8PlayloadType;
|
||||
memcpy(&pucRtp[6], &sq, 2);
|
||||
memcpy(&pucRtp[8], &ts, 4);
|
||||
//ssrc
|
||||
memcpy(&pucRtp[12], &sc, 4);
|
||||
//playload
|
||||
memcpy(&pucRtp[16], data, len);
|
||||
|
||||
rtppkt->PT = _ui8PlayloadType;
|
||||
rtppkt->interleaved = _ui8Interleaved;
|
||||
rtppkt->mark = mark;
|
||||
rtppkt->length = len + 16;
|
||||
rtppkt->sequence = _ui16Sequence;
|
||||
rtppkt->timeStamp = uiStamp;
|
||||
rtppkt->ssrc = _ui32Ssrc;
|
||||
rtppkt->type = type;
|
||||
rtppkt->offset = 16;
|
||||
_ui16Sequence++;
|
||||
_ui32TimeStamp = uiStamp;
|
||||
return rtppkt;
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
|
@ -107,7 +107,7 @@ protected:
|
||||
};
|
||||
|
||||
|
||||
class RtpInfo{
|
||||
class RtpInfo : public ResourcePoolHelper<RtpPacket>{
|
||||
public:
|
||||
typedef std::shared_ptr<RtpInfo> Ptr;
|
||||
|
||||
@ -153,6 +153,7 @@ public:
|
||||
uint32_t getMtuSize() const {
|
||||
return _ui32MtuSize;
|
||||
}
|
||||
RtpPacket::Ptr makeRtp(TrackType type,const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
|
||||
protected:
|
||||
uint32_t _ui32Ssrc;
|
||||
uint32_t _ui32SampleRate;
|
||||
@ -163,7 +164,7 @@ protected:
|
||||
uint32_t _ui32TimeStamp = 0;
|
||||
};
|
||||
|
||||
class RtpCodec : public RtpRing, public FrameRingInterfaceDelegate , public CodecInfo , public ResourcePoolHelper<RtpPacket>{
|
||||
class RtpCodec : public RtpRing, public FrameRingInterfaceDelegate , public CodecInfo{
|
||||
public:
|
||||
typedef std::shared_ptr<RtpCodec> Ptr;
|
||||
RtpCodec(){}
|
||||
|
Loading…
Reference in New Issue
Block a user