mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-07-07 04:08:13 +08:00
整理文件 规范命名
This commit is contained in:
133
src/RtspMuxer/AACRtpCodec.cpp
Normal file
133
src/RtspMuxer/AACRtpCodec.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
//
|
||||
// Created by xzl on 2018/10/18.
|
||||
//
|
||||
|
||||
#include "AACRtpCodec.h"
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
AACRtpEncoder::AACRtpEncoder(uint32_t ui32Ssrc,
|
||||
uint32_t ui32MtuSize,
|
||||
uint32_t ui32SampleRate,
|
||||
uint8_t ui8PlayloadType,
|
||||
uint8_t ui8Interleaved) :
|
||||
RtpInfo(ui32Ssrc,
|
||||
ui32MtuSize,
|
||||
ui32SampleRate,
|
||||
ui8PlayloadType,
|
||||
ui8Interleaved),
|
||||
AACRtpDecoder(ui32SampleRate){
|
||||
}
|
||||
|
||||
void AACRtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
RtpCodec::inputFrame(frame);
|
||||
|
||||
GET_CONFIG_AND_REGISTER(uint32_t, cycleMS, Rtp::kCycleMS);
|
||||
auto uiStamp = frame->stamp();
|
||||
auto pcData = frame->data() + frame->prefixSize();
|
||||
auto iLen = frame->size() - frame->prefixSize();
|
||||
|
||||
uiStamp %= cycleMS;
|
||||
char *ptr = (char *) pcData;
|
||||
int iSize = iLen;
|
||||
while (iSize > 0) {
|
||||
if (iSize <= _ui32MtuSize - 20) {
|
||||
_aucSectionBuf[0] = 0;
|
||||
_aucSectionBuf[1] = 16;
|
||||
_aucSectionBuf[2] = iLen >> 5;
|
||||
_aucSectionBuf[3] = (iLen & 0x1F) << 3;
|
||||
memcpy(_aucSectionBuf + 4, ptr, iSize);
|
||||
makeAACRtp(_aucSectionBuf, iSize + 4, true, uiStamp);
|
||||
break;
|
||||
}
|
||||
_aucSectionBuf[0] = 0;
|
||||
_aucSectionBuf[1] = 16;
|
||||
_aucSectionBuf[2] = (iLen) >> 5;
|
||||
_aucSectionBuf[3] = (iLen & 0x1F) << 3;
|
||||
memcpy(_aucSectionBuf + 4, ptr, _ui32MtuSize - 20);
|
||||
makeAACRtp(_aucSectionBuf, _ui32MtuSize - 16, false, uiStamp);
|
||||
ptr += (_ui32MtuSize - 20);
|
||||
iSize -= (_ui32MtuSize - 20);
|
||||
}
|
||||
}
|
||||
|
||||
void AACRtpEncoder::makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp) {
|
||||
uint16_t u16RtpLen = uiLen + 12;
|
||||
_ui32TimeStamp = (_ui32SampleRate / 1000) * uiStamp;
|
||||
uint32_t ts = htonl(_ui32TimeStamp);
|
||||
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 = _ui32TimeStamp;
|
||||
rtppkt.ssrc = _ui32Ssrc;
|
||||
rtppkt.type = TrackAudio;
|
||||
rtppkt.offset = 16;
|
||||
|
||||
RtpCodec::inputRtp(pRtppkt, false);
|
||||
_ui16Sequence++;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
AACRtpDecoder::AACRtpDecoder(uint32_t ui32SampleRate) {
|
||||
_adts = obtainFrame();
|
||||
_sampleRate = ui32SampleRate;
|
||||
}
|
||||
|
||||
AACFrame::Ptr AACRtpDecoder::obtainFrame() {
|
||||
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
|
||||
auto frame = ResourcePoolHelper<AACFrame>::obtainObj();
|
||||
frame->aac_frame_length = 7;
|
||||
frame->iPrefixSize = 7;
|
||||
return frame;
|
||||
}
|
||||
|
||||
bool AACRtpDecoder::inputRtp(const RtpPacket::Ptr &rtppack, bool key_pos) {
|
||||
RtpCodec::inputRtp(rtppack, false);
|
||||
|
||||
int length = rtppack->length - rtppack->offset;
|
||||
if (_adts->aac_frame_length + length - 4 > sizeof(AACFrame::buffer)) {
|
||||
_adts->aac_frame_length = 7;
|
||||
WarnL << "aac负载数据太长";
|
||||
return false;
|
||||
}
|
||||
memcpy(_adts->buffer + _adts->aac_frame_length, rtppack->payload + rtppack->offset + 4, length - 4);
|
||||
_adts->aac_frame_length += (length - 4);
|
||||
if (rtppack->mark == true) {
|
||||
_adts->sequence = rtppack->sequence;
|
||||
_adts->timeStamp = rtppack->timeStamp * (1000.0 / _sampleRate);
|
||||
writeAdtsHeader(*_adts, _adts->buffer);
|
||||
onGetAAC(_adts);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void AACRtpDecoder::onGetAAC(const AACFrame::Ptr &frame) {
|
||||
//写入环形缓存
|
||||
RtpCodec::inputFrame(frame);
|
||||
_adts = obtainFrame();
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
|
||||
|
||||
81
src/RtspMuxer/AACRtpCodec.h
Normal file
81
src/RtspMuxer/AACRtpCodec.h
Normal file
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// Created by xzl on 2018/10/18.
|
||||
//
|
||||
|
||||
#ifndef ZLMEDIAKIT_AACRTPCODEC_H
|
||||
#define ZLMEDIAKIT_AACRTPCODEC_H
|
||||
|
||||
#include "RtpCodec.h"
|
||||
|
||||
namespace mediakit{
|
||||
/**
|
||||
* aac rtp转adts类
|
||||
*/
|
||||
class AACRtpDecoder : public RtpCodec , public ResourcePoolHelper<AACFrame> {
|
||||
public:
|
||||
typedef std::shared_ptr<AACRtpDecoder> Ptr;
|
||||
|
||||
/**
|
||||
* @param ui32SampleRate 采样率,用于时间戳转换用
|
||||
*/
|
||||
AACRtpDecoder(uint32_t ui32SampleRate);
|
||||
~AACRtpDecoder() {}
|
||||
|
||||
/**
|
||||
* 输入rtp并解码
|
||||
* @param rtp rtp数据包
|
||||
* @param key_pos 此参数内部强制转换为false,请忽略之
|
||||
*/
|
||||
bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = false) override;
|
||||
|
||||
TrackType getTrackType() const override{
|
||||
return TrackAudio;
|
||||
}
|
||||
|
||||
CodecId getCodecId() const override{
|
||||
return CodecAAC;
|
||||
}
|
||||
private:
|
||||
void onGetAAC(const AACFrame::Ptr &frame);
|
||||
AACFrame::Ptr obtainFrame();
|
||||
private:
|
||||
AACFrame::Ptr _adts;
|
||||
uint32_t _sampleRate;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* aac adts转rtp类
|
||||
*/
|
||||
class AACRtpEncoder : public AACRtpDecoder , public RtpInfo {
|
||||
public:
|
||||
typedef std::shared_ptr<AACRtpEncoder> Ptr;
|
||||
|
||||
/**
|
||||
* @param ui32Ssrc ssrc
|
||||
* @param ui32MtuSize mtu 大小
|
||||
* @param ui32SampleRate 采样率
|
||||
* @param ui8PlayloadType pt类型
|
||||
* @param ui8Interleaved rtsp interleaved 值
|
||||
*/
|
||||
AACRtpEncoder(uint32_t ui32Ssrc,
|
||||
uint32_t ui32MtuSize,
|
||||
uint32_t ui32SampleRate,
|
||||
uint8_t ui8PlayloadType = 97,
|
||||
uint8_t ui8Interleaved = TrackAudio * 2);
|
||||
~AACRtpEncoder() {}
|
||||
|
||||
/**
|
||||
* 输入aac 数据,必须带dats头
|
||||
* @param frame 带dats头的aac数据
|
||||
*/
|
||||
void inputFrame(const Frame::Ptr &frame) override;
|
||||
private:
|
||||
void makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
|
||||
private:
|
||||
unsigned char _aucSectionBuf[1600];
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
#endif //ZLMEDIAKIT_AACRTPCODEC_H
|
||||
208
src/RtspMuxer/H264RtpCodec.cpp
Normal file
208
src/RtspMuxer/H264RtpCodec.cpp
Normal file
@@ -0,0 +1,208 @@
|
||||
//
|
||||
// Created by xzl on 2018/10/18.
|
||||
//
|
||||
|
||||
#include "H264RtpCodec.h"
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
H264RtpDecoder::H264RtpDecoder() {
|
||||
_h264frame = obtainFrame();
|
||||
}
|
||||
|
||||
H264Frame::Ptr H264RtpDecoder::obtainFrame() {
|
||||
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
|
||||
auto frame = ResourcePoolHelper<H264Frame>::obtainObj();
|
||||
frame->buffer.clear();
|
||||
frame->iPrefixSize = 4;
|
||||
return frame;
|
||||
}
|
||||
|
||||
bool H264RtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
|
||||
key_pos = decodeRtp(rtp);
|
||||
RtpCodec::inputRtp(rtp, key_pos);
|
||||
return key_pos;
|
||||
}
|
||||
|
||||
bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
|
||||
/**
|
||||
* h264帧类型
|
||||
* Type==1:P/B frame
|
||||
* Type==5:IDR frame
|
||||
* Type==6:SEI frame
|
||||
* Type==7:SPS frame
|
||||
* Type==8:PPS frame
|
||||
*/
|
||||
|
||||
const uint8_t *frame = (uint8_t *) rtppack->payload + rtppack->offset;
|
||||
int length = rtppack->length - rtppack->offset;
|
||||
NALU nal;
|
||||
MakeNalu(*frame, nal);
|
||||
|
||||
if (nal.type >= 0 && nal.type < 24) {
|
||||
//a full frame
|
||||
_h264frame->buffer.assign("\x0\x0\x0\x1", 4);
|
||||
_h264frame->buffer.append((char *)frame, length);
|
||||
_h264frame->type = nal.type;
|
||||
_h264frame->timeStamp = rtppack->timeStamp / 90;
|
||||
_h264frame->sequence = rtppack->sequence;
|
||||
auto isIDR = _h264frame->type == 5;
|
||||
onGetH264(_h264frame);
|
||||
return (isIDR); //i frame
|
||||
}
|
||||
|
||||
if (nal.type == 28) {
|
||||
//FU-A
|
||||
FU fu;
|
||||
MakeFU(frame[1], fu);
|
||||
if (fu.S == 1) {
|
||||
//FU-A start
|
||||
char tmp = (nal.forbidden_zero_bit << 7 | nal.nal_ref_idc << 5 | fu.type);
|
||||
_h264frame->buffer.assign("\x0\x0\x0\x1", 4);
|
||||
_h264frame->buffer.push_back(tmp);
|
||||
_h264frame->buffer.append((char *)frame + 2, length - 2);
|
||||
_h264frame->type = fu.type;
|
||||
_h264frame->timeStamp = rtppack->timeStamp / 90;
|
||||
_h264frame->sequence = rtppack->sequence;
|
||||
return (_h264frame->type == 5); //i frame
|
||||
}
|
||||
|
||||
if (rtppack->sequence != (uint16_t)(_h264frame->sequence + 1)) {
|
||||
_h264frame->buffer.clear();
|
||||
WarnL << "丢包,帧废弃:" << rtppack->sequence << "," << _h264frame->sequence;
|
||||
return false;
|
||||
}
|
||||
_h264frame->sequence = rtppack->sequence;
|
||||
if (fu.E == 1) {
|
||||
//FU-A end
|
||||
_h264frame->buffer.append((char *)frame + 2, length - 2);
|
||||
_h264frame->timeStamp = rtppack->timeStamp / 90;
|
||||
auto isIDR = _h264frame->type == 5;
|
||||
onGetH264(_h264frame);
|
||||
return isIDR;
|
||||
}
|
||||
//FU-A mid
|
||||
_h264frame->buffer.append((char *)frame + 2, length - 2);
|
||||
return false;
|
||||
}
|
||||
|
||||
WarnL << "不支持的rtp类型:" << nal.type << " " << rtppack->sequence;
|
||||
return false;
|
||||
// 29 FU-B 单NAL单元B模式
|
||||
// 24 STAP-A 单一时间的组合包
|
||||
// 25 STAP-B 单一时间的组合包
|
||||
// 26 MTAP16 多个时间的组合包
|
||||
// 27 MTAP24 多个时间的组合包
|
||||
// 0 udef
|
||||
// 30 udef
|
||||
// 31 udef
|
||||
}
|
||||
|
||||
void H264RtpDecoder::onGetH264(const H264Frame::Ptr &frame) {
|
||||
//写入环形缓存
|
||||
RtpCodec::inputFrame(frame);
|
||||
_h264frame = obtainFrame();
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
H264RtpEncoder::H264RtpEncoder(uint32_t ui32Ssrc,
|
||||
uint32_t ui32MtuSize,
|
||||
uint32_t ui32SampleRate,
|
||||
uint8_t ui8PlayloadType,
|
||||
uint8_t ui8Interleaved) :
|
||||
RtpInfo(ui32Ssrc,
|
||||
ui32MtuSize,
|
||||
ui32SampleRate,
|
||||
ui8PlayloadType,
|
||||
ui8Interleaved) {
|
||||
}
|
||||
|
||||
void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
RtpCodec::inputFrame(frame);
|
||||
|
||||
GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Rtp::kCycleMS);
|
||||
auto pcData = frame->data() + frame->prefixSize();
|
||||
auto uiStamp = frame->stamp();
|
||||
auto iLen = frame->size() - frame->prefixSize();
|
||||
|
||||
uiStamp %= cycleMS;
|
||||
int iSize = _ui32MtuSize - 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);
|
||||
}
|
||||
}
|
||||
|
||||
void H264RtpEncoder::makeH264Rtp(const void* data, unsigned int len, bool mark, uint32_t uiStamp) {
|
||||
uint16_t ui16RtpLen = len + 12;
|
||||
_ui32TimeStamp = (_ui32SampleRate / 1000) * uiStamp;
|
||||
uint32_t ts = htonl(_ui32TimeStamp);
|
||||
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 = _ui32TimeStamp;
|
||||
rtppkt->ssrc = _ui32Ssrc;
|
||||
rtppkt->type = TrackVideo;
|
||||
rtppkt->offset = 16;
|
||||
|
||||
uint8_t type = ((uint8_t *) (data))[0] & 0x1F;
|
||||
RtpCodec::inputRtp(rtppkt,type == 5);
|
||||
_ui16Sequence++;
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
80
src/RtspMuxer/H264RtpCodec.h
Normal file
80
src/RtspMuxer/H264RtpCodec.h
Normal file
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// Created by xzl on 2018/10/18.
|
||||
//
|
||||
|
||||
#ifndef ZLMEDIAKIT_H264RTPCODEC_H
|
||||
#define ZLMEDIAKIT_H264RTPCODEC_H
|
||||
|
||||
#include "RtpCodec.h"
|
||||
#include "Util/ResourcePool.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
/**
|
||||
* h264 rtp解码类
|
||||
*/
|
||||
class H264RtpDecoder : public RtpCodec , public ResourcePoolHelper<H264Frame> {
|
||||
public:
|
||||
typedef std::shared_ptr<H264RtpDecoder> Ptr;
|
||||
|
||||
H264RtpDecoder();
|
||||
~H264RtpDecoder() {}
|
||||
|
||||
/**
|
||||
* 输入264 rtp包
|
||||
* @param rtp rtp包
|
||||
* @param key_pos 此参数忽略之
|
||||
*/
|
||||
bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = true) override;
|
||||
|
||||
TrackType getTrackType() const override{
|
||||
return TrackVideo;
|
||||
}
|
||||
|
||||
CodecId getCodecId() const override{
|
||||
return CodecH264;
|
||||
}
|
||||
private:
|
||||
bool decodeRtp(const RtpPacket::Ptr &rtp);
|
||||
void onGetH264(const H264Frame::Ptr &frame);
|
||||
H264Frame::Ptr obtainFrame();
|
||||
private:
|
||||
H264Frame::Ptr _h264frame;
|
||||
};
|
||||
|
||||
/**
|
||||
* 264 rtp打包类
|
||||
*/
|
||||
class H264RtpEncoder : public H264RtpDecoder ,public RtpInfo{
|
||||
public:
|
||||
typedef std::shared_ptr<H264RtpEncoder> Ptr;
|
||||
|
||||
/**
|
||||
* @param ui32Ssrc ssrc
|
||||
* @param ui32MtuSize mtu大小
|
||||
* @param ui32SampleRate 采样率,强制为90000
|
||||
* @param ui8PlayloadType pt类型
|
||||
* @param ui8Interleaved rtsp interleaved
|
||||
*/
|
||||
H264RtpEncoder(uint32_t ui32Ssrc,
|
||||
uint32_t ui32MtuSize = 1400,
|
||||
uint32_t ui32SampleRate = 90000,
|
||||
uint8_t ui8PlayloadType = 96,
|
||||
uint8_t ui8Interleaved = TrackVideo * 2);
|
||||
~H264RtpEncoder() {}
|
||||
|
||||
/**
|
||||
* 输入264帧
|
||||
* @param frame 帧数据,必须
|
||||
*/
|
||||
void inputFrame(const Frame::Ptr &frame) override;
|
||||
private:
|
||||
void makeH264Rtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
|
||||
private:
|
||||
unsigned char _aucSectionBuf[1600];
|
||||
};
|
||||
|
||||
}//namespace mediakit{
|
||||
|
||||
#endif //ZLMEDIAKIT_H264RTPCODEC_H
|
||||
163
src/RtspMuxer/RtpCodec.h
Normal file
163
src/RtspMuxer/RtpCodec.h
Normal file
@@ -0,0 +1,163 @@
|
||||
//
|
||||
// Created by xzl on 2018/10/18.
|
||||
//
|
||||
|
||||
#ifndef ZLMEDIAKIT_RTPCODEC_H
|
||||
#define ZLMEDIAKIT_RTPCODEC_H
|
||||
|
||||
#include <memory>
|
||||
#include "Util/RingBuffer.h"
|
||||
#include "Rtsp/Rtsp.h"
|
||||
#include "Player/PlayerBase.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
class RtpPacket : public CodecInfo {
|
||||
public:
|
||||
typedef std::shared_ptr<RtpPacket> Ptr;
|
||||
|
||||
TrackType getTrackType() const {
|
||||
return type;
|
||||
}
|
||||
|
||||
CodecId getCodecId() const {
|
||||
return CodecInvalid;
|
||||
}
|
||||
public:
|
||||
uint8_t interleaved;
|
||||
uint8_t PT;
|
||||
bool mark;
|
||||
uint32_t length;
|
||||
uint32_t timeStamp;
|
||||
uint16_t sequence;
|
||||
uint32_t ssrc;
|
||||
uint8_t payload[1560];
|
||||
uint8_t offset;
|
||||
TrackType type;
|
||||
};
|
||||
|
||||
class RtpRingInterface {
|
||||
public:
|
||||
typedef RingBuffer<RtpPacket::Ptr> RingType;
|
||||
typedef std::shared_ptr<RtpRingInterface> Ptr;
|
||||
|
||||
RtpRingInterface(){}
|
||||
virtual ~RtpRingInterface(){}
|
||||
|
||||
/**
|
||||
* 获取rtp环形缓存
|
||||
* @return
|
||||
*/
|
||||
virtual RingType::Ptr getRtpRing() const = 0;
|
||||
|
||||
/**
|
||||
* 设置rtp环形缓存
|
||||
* @param ring
|
||||
*/
|
||||
virtual void setRtpRing(const RingType::Ptr &ring) = 0;
|
||||
|
||||
/**
|
||||
* 输入rtp包
|
||||
* @param rtp rtp包
|
||||
* @param key_pos 是否为关键帧第一个rtp包
|
||||
* @return 是否为关键帧第一个rtp包
|
||||
*/
|
||||
virtual bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) = 0;
|
||||
};
|
||||
|
||||
class RtpRing : public RtpRingInterface {
|
||||
public:
|
||||
typedef std::shared_ptr<RtpRing> Ptr;
|
||||
|
||||
RtpRing(){
|
||||
_rtpRing = std::make_shared<RingType>();
|
||||
}
|
||||
virtual ~RtpRing(){}
|
||||
|
||||
RingType::Ptr getRtpRing() const override {
|
||||
return _rtpRing;
|
||||
}
|
||||
|
||||
void setRtpRing(const RingType::Ptr &ring) override {
|
||||
_rtpRing = ring;
|
||||
}
|
||||
|
||||
bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) override{
|
||||
_rtpRing->write(rtp,key_pos);
|
||||
return key_pos;
|
||||
}
|
||||
protected:
|
||||
RingType::Ptr _rtpRing;
|
||||
};
|
||||
|
||||
|
||||
class RtpInfo{
|
||||
public:
|
||||
typedef std::shared_ptr<RtpInfo> Ptr;
|
||||
|
||||
RtpInfo(uint32_t ui32Ssrc,
|
||||
uint32_t ui32MtuSize,
|
||||
uint32_t ui32SampleRate,
|
||||
uint8_t ui8PlayloadType,
|
||||
uint8_t ui8Interleaved) {
|
||||
if(ui32Ssrc == 0){
|
||||
ui32Ssrc = ((uint64_t)this) & 0xFFFFFFFF;
|
||||
}
|
||||
_ui32Ssrc = ui32Ssrc;
|
||||
_ui32SampleRate = ui32SampleRate;
|
||||
_ui32MtuSize = ui32MtuSize;
|
||||
_ui8PlayloadType = ui8PlayloadType;
|
||||
_ui8Interleaved = ui8Interleaved;
|
||||
}
|
||||
|
||||
virtual ~RtpInfo(){}
|
||||
|
||||
int getInterleaved() const {
|
||||
return _ui8Interleaved;
|
||||
}
|
||||
|
||||
int getPlayloadType() const {
|
||||
return _ui8PlayloadType;
|
||||
}
|
||||
|
||||
int getSampleRate() const {
|
||||
return _ui32SampleRate;
|
||||
}
|
||||
|
||||
uint32_t getSsrc() const {
|
||||
return _ui32Ssrc;
|
||||
}
|
||||
|
||||
uint16_t getSeqence() const {
|
||||
return _ui16Sequence;
|
||||
}
|
||||
uint32_t getTimestamp() const {
|
||||
return _ui32TimeStamp;
|
||||
}
|
||||
uint32_t getMtuSize() const {
|
||||
return _ui32MtuSize;
|
||||
}
|
||||
protected:
|
||||
uint32_t _ui32Ssrc;
|
||||
uint32_t _ui32SampleRate;
|
||||
uint32_t _ui32MtuSize;
|
||||
uint8_t _ui8PlayloadType;
|
||||
uint8_t _ui8Interleaved;
|
||||
uint16_t _ui16Sequence = 0;
|
||||
uint32_t _ui32TimeStamp = 0;
|
||||
};
|
||||
|
||||
class RtpCodec : public RtpRing, public FrameRingInterfaceDelegate , public CodecInfo , public ResourcePoolHelper<RtpPacket>{
|
||||
public:
|
||||
typedef std::shared_ptr<RtpCodec> Ptr;
|
||||
RtpCodec(){}
|
||||
virtual ~RtpCodec(){}
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
|
||||
|
||||
|
||||
#endif //ZLMEDIAKIT_RTPCODEC_H
|
||||
105
src/RtspMuxer/RtpMaker.h
Normal file
105
src/RtspMuxer/RtpMaker.h
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef RTP_RTPMAKER_H_
|
||||
#define RTP_RTPMAKER_H_
|
||||
|
||||
#include "Rtsp/RtspMediaSource.h"
|
||||
#include "Rtsp/Rtsp.h"
|
||||
#include "Util/logger.h"
|
||||
#include "Util/RingBuffer.h"
|
||||
#include "Util/TimeTicker.h"
|
||||
#include "Util/ResourcePool.h"
|
||||
#include "Thread/ThreadPool.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
class RtpMaker {
|
||||
public:
|
||||
typedef function<void(const RtpPacket::Ptr &pPkt, bool bKeyPos)> onGetRTP;
|
||||
typedef std::shared_ptr<RtpMaker> Ptr;
|
||||
|
||||
RtpMaker(const onGetRTP &cb, uint32_t ui32Ssrc, int iMtuSize,int iSampleRate,
|
||||
uint8_t ui8PlayloadType, uint8_t ui8Interleaved) {
|
||||
callBack = cb;
|
||||
_ui32Ssrc = ui32Ssrc;
|
||||
_ui32SampleRate = iSampleRate;
|
||||
_iMtuSize = iMtuSize;
|
||||
_ui8PlayloadType = ui8PlayloadType;
|
||||
_ui8Interleaved = ui8Interleaved;
|
||||
_pktPool.setSize(64);
|
||||
}
|
||||
virtual ~RtpMaker() {
|
||||
}
|
||||
|
||||
virtual void makeRtp(const char *pcData, int iDataLen, uint32_t uiStamp)=0;
|
||||
|
||||
int getInterleaved() const {
|
||||
return _ui8Interleaved;
|
||||
}
|
||||
|
||||
int getPlayloadType() const {
|
||||
return _ui8PlayloadType;
|
||||
}
|
||||
|
||||
int getSampleRate() const {
|
||||
return _ui32SampleRate;
|
||||
}
|
||||
|
||||
uint32_t getSsrc() const {
|
||||
return _ui32Ssrc;
|
||||
}
|
||||
|
||||
uint16_t getSeqence() const {
|
||||
return _ui16Sequence;
|
||||
}
|
||||
uint32_t getTimestamp() const {
|
||||
return _ui32TimeStamp;
|
||||
}
|
||||
protected:
|
||||
uint32_t _ui32Ssrc;
|
||||
uint32_t _ui32SampleRate;
|
||||
int _iMtuSize;
|
||||
uint8_t _ui8PlayloadType;
|
||||
uint8_t _ui8Interleaved;
|
||||
uint16_t _ui16Sequence = 0;
|
||||
uint32_t _ui32TimeStamp = 0;
|
||||
virtual void onMakeRtp(const RtpPacket::Ptr &pkt, bool bKeyPos = true) {
|
||||
callBack(pkt, bKeyPos);
|
||||
}
|
||||
inline RtpPacket::Ptr obtainPkt() {
|
||||
return _pktPool.obtain();
|
||||
}
|
||||
private:
|
||||
RtspMediaSource::PoolType _pktPool;
|
||||
onGetRTP callBack;
|
||||
};
|
||||
|
||||
} //namespace mediakit
|
||||
|
||||
|
||||
#endif /* RTP_RTPMAKER_H_ */
|
||||
99
src/RtspMuxer/RtpMakerAAC.cpp
Normal file
99
src/RtspMuxer/RtpMakerAAC.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* 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 "Common/config.h"
|
||||
#include "RtpMakerAAC.h"
|
||||
#include "Util/mini.h"
|
||||
#include "Network/sockutil.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
void RtpMaker_AAC::makeRtp(const char *pcData, int iLen, uint32_t uiStamp) {
|
||||
GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Rtp::kCycleMS);
|
||||
|
||||
uiStamp %= cycleMS;
|
||||
char *ptr = (char *) pcData;
|
||||
int iSize = iLen;
|
||||
while (iSize > 0 ) {
|
||||
if (iSize <= _iMtuSize - 20) {
|
||||
_aucSectionBuf[0] = 0;
|
||||
_aucSectionBuf[1] = 16;
|
||||
_aucSectionBuf[2] = iLen >> 5;
|
||||
_aucSectionBuf[3] = (iLen & 0x1F) << 3;
|
||||
memcpy(_aucSectionBuf + 4, ptr, iSize);
|
||||
makeAACRtp(_aucSectionBuf, iSize + 4, true, uiStamp);
|
||||
break;
|
||||
}
|
||||
_aucSectionBuf[0] = 0;
|
||||
_aucSectionBuf[1] = 16;
|
||||
_aucSectionBuf[2] = (iLen) >> 5;
|
||||
_aucSectionBuf[3] = (iLen & 0x1F) << 3;
|
||||
memcpy(_aucSectionBuf + 4, ptr, _iMtuSize - 20);
|
||||
makeAACRtp(_aucSectionBuf, _iMtuSize - 16, false, uiStamp);
|
||||
ptr += (_iMtuSize - 20);
|
||||
iSize -= (_iMtuSize - 20);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
inline void RtpMaker_AAC::makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp) {
|
||||
uint16_t u16RtpLen = uiLen + 12;
|
||||
_ui32TimeStamp = (_ui32SampleRate / 1000) * uiStamp;
|
||||
uint32_t ts = htonl(_ui32TimeStamp);
|
||||
uint16_t sq = htons(_ui16Sequence);
|
||||
uint32_t sc = htonl(_ui32Ssrc);
|
||||
auto pRtppkt = obtainPkt();
|
||||
auto &rtppkt = *(pRtppkt.get());
|
||||
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 = _ui32TimeStamp;
|
||||
rtppkt.ssrc = _ui32Ssrc;
|
||||
rtppkt.type = TrackAudio;
|
||||
rtppkt.offset = 16;
|
||||
|
||||
onMakeRtp(pRtppkt, false);
|
||||
_ui16Sequence++;
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
60
src/RtspMuxer/RtpMakerAAC.h
Normal file
60
src/RtspMuxer/RtpMakerAAC.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef RTP_RTPMAKERAAC_H_
|
||||
#define RTP_RTPMAKERAAC_H_
|
||||
|
||||
#include <memory>
|
||||
#include "RtpMaker.h"
|
||||
#include "Rtsp/RtspMediaSource.h"
|
||||
#include "Util/logger.h"
|
||||
#include "Util/RingBuffer.h"
|
||||
#include "Util/ResourcePool.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
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 = TrackAudio * 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 _aucSectionBuf[1600];
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
#endif /* RTP_RTPMAKERAAC_H_ */
|
||||
118
src/RtspMuxer/RtpMakerH264.cpp
Normal file
118
src/RtspMuxer/RtpMakerH264.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* 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 "Common/config.h"
|
||||
#include "RtpMakerH264.h"
|
||||
#include "Util/mini.h"
|
||||
#include "Network/sockutil.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
void RtpMaker_H264::makeRtp(const char* pcData, int iLen, uint32_t uiStamp) {
|
||||
GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Rtp::kCycleMS);
|
||||
|
||||
uiStamp %= cycleMS;
|
||||
int iSize = _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;
|
||||
_ui32TimeStamp = (_ui32SampleRate / 1000) * uiStamp;
|
||||
uint32_t ts = htonl(_ui32TimeStamp);
|
||||
uint16_t sq = htons(_ui16Sequence);
|
||||
uint32_t sc = htonl(_ui32Ssrc);
|
||||
|
||||
auto pRtppkt = obtainPkt();
|
||||
auto &rtppkt = *(pRtppkt.get());
|
||||
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 = _ui32TimeStamp;
|
||||
rtppkt.ssrc = _ui32Ssrc;
|
||||
rtppkt.type = TrackVideo;
|
||||
rtppkt.offset = 16;
|
||||
|
||||
uint8_t type = ((uint8_t *) (data))[0] & 0x1F;
|
||||
onMakeRtp(pRtppkt, type == 5);
|
||||
_ui16Sequence++;
|
||||
//InfoL<<timeStamp<<" "<<time<<" "<<sampleRate;
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
60
src/RtspMuxer/RtpMakerH264.h
Normal file
60
src/RtspMuxer/RtpMakerH264.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef RTP_RTPMAKERH264_H_
|
||||
#define RTP_RTPMAKERH264_H_
|
||||
|
||||
#include <memory>
|
||||
#include "RtpMaker.h"
|
||||
#include "Rtsp/RtspMediaSource.h"
|
||||
#include "Util/logger.h"
|
||||
#include "Util/RingBuffer.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
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 = TrackVideo * 2) :
|
||||
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 mediakit
|
||||
|
||||
|
||||
#endif /* RTP_RTPMAKERH264_H_ */
|
||||
129
src/RtspMuxer/RtspDemuxer.cpp
Normal file
129
src/RtspMuxer/RtspDemuxer.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* 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 <cctype>
|
||||
#include <algorithm>
|
||||
#include "RtspDemuxer.h"
|
||||
#include "Util/base64.h"
|
||||
#include "H264/SPSParser.h"
|
||||
#include "Common/Factory.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
static int getTimeInSDP(const string &sdp) {
|
||||
auto strRange = FindField(sdp.data(), "a=range:npt=", "\r\n");
|
||||
strRange.append(" ");
|
||||
auto iPos = strRange.find('-');
|
||||
if (iPos == string::npos) {
|
||||
return 0;
|
||||
}
|
||||
auto strStart = strRange.substr(0, iPos);
|
||||
auto strEnd = strRange.substr(iPos + 1);
|
||||
strEnd.pop_back();
|
||||
if (strStart == "now") {
|
||||
strStart = "0";
|
||||
}
|
||||
return atof(strEnd.data()) - atof(strStart.data());
|
||||
}
|
||||
RtspDemuxer::RtspDemuxer(const string& sdp) {
|
||||
RtspTrack tmp[2];
|
||||
int cnt = parserSDP(sdp, tmp);
|
||||
for (int i = 0; i < cnt; i++) {
|
||||
switch (tmp[i].type) {
|
||||
case TrackVideo: {
|
||||
onGetVideoTrack(tmp[i]);
|
||||
}
|
||||
break;
|
||||
case TrackAudio: {
|
||||
onGetAudioTrack(tmp[i]);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
_fDuration = getTimeInSDP(sdp);
|
||||
}
|
||||
|
||||
bool RtspDemuxer::inputRtp(const RtpPacket::Ptr & rtp) {
|
||||
switch (rtp->getTrackType()) {
|
||||
case TrackVideo:{
|
||||
if(_videoRtpDecoder){
|
||||
return _videoRtpDecoder->inputRtp(rtp, true);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
case TrackAudio:{
|
||||
_audioRtpDecoder->inputRtp(rtp, false);
|
||||
return false;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline void RtspDemuxer::onGetAudioTrack(const RtspTrack& audio) {
|
||||
//生成Track对象
|
||||
_audioTrack = dynamic_pointer_cast<AudioTrack>(Factory::getTrackBySdp(audio.trackSdp));
|
||||
if(_audioTrack){
|
||||
//生成RtpCodec对象以便解码rtp
|
||||
_audioRtpDecoder = Factory::getRtpDecoderById(_audioTrack->getCodecId(),_audioTrack->getAudioSampleRate());
|
||||
if(_audioRtpDecoder){
|
||||
//设置rtp解码器代理,生成的frame写入该Track
|
||||
_audioRtpDecoder->setDelegate(_audioTrack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void RtspDemuxer::onGetVideoTrack(const RtspTrack& video) {
|
||||
//生成Track对象
|
||||
_videoTrack = dynamic_pointer_cast<VideoTrack>(Factory::getTrackBySdp(video.trackSdp));
|
||||
if(_videoTrack){
|
||||
//生成RtpCodec对象以便解码rtp
|
||||
_videoRtpDecoder = Factory::getRtpDecoderById(_videoTrack->getCodecId(),90000);
|
||||
if(_videoRtpDecoder){
|
||||
//设置rtp解码器代理,生成的frame写入该Track
|
||||
_videoRtpDecoder->setDelegate(_videoTrack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector<Track::Ptr> RtspDemuxer::getTracks() const {
|
||||
vector<Track::Ptr> ret;
|
||||
if(_videoTrack){
|
||||
ret.emplace_back(_videoTrack);
|
||||
}
|
||||
if(_audioTrack){
|
||||
ret.emplace_back(_audioTrack);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
} /* namespace mediakit */
|
||||
86
src/RtspMuxer/RtspDemuxer.h
Normal file
86
src/RtspMuxer/RtspDemuxer.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef SRC_RTP_RTPPARSER_H_
|
||||
#define SRC_RTP_RTPPARSER_H_
|
||||
|
||||
#include <unordered_map>
|
||||
#include "Rtsp/Rtsp.h"
|
||||
#include "Player/Player.h"
|
||||
#include "Player/PlayerBase.h"
|
||||
#include "Util/TimeTicker.h"
|
||||
#include "RtspMuxer/RtpCodec.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
class RtspDemuxer : public PlayerBase{
|
||||
public:
|
||||
typedef std::shared_ptr<RtspDemuxer> Ptr;
|
||||
RtspDemuxer(const string &sdp);
|
||||
virtual ~RtspDemuxer(){};
|
||||
|
||||
//返回值:true 代表是i帧第一个rtp包
|
||||
bool inputRtp(const RtpPacket::Ptr &rtp);
|
||||
|
||||
float getDuration() const override {
|
||||
return _fDuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回是否完成初始化完毕
|
||||
* 由于有些rtsp的sdp不包含sps pps信息
|
||||
* 所以要等待接收到到sps的rtp包后才能完成
|
||||
* @return
|
||||
*/
|
||||
bool isInited() const override{
|
||||
bool ret = true;
|
||||
if(ret && _audioTrack){
|
||||
ret = _audioTrack->getTrackType() != TrackInvalid;
|
||||
}
|
||||
if(ret && _videoTrack){
|
||||
ret = _videoTrack->getTrackType() != TrackInvalid;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
vector<Track::Ptr> getTracks() const override;
|
||||
private:
|
||||
inline void onGetAudioTrack(const RtspTrack &audio);
|
||||
inline void onGetVideoTrack(const RtspTrack &video);
|
||||
private:
|
||||
float _fDuration = 0;
|
||||
AudioTrack::Ptr _audioTrack;
|
||||
VideoTrack::Ptr _videoTrack;
|
||||
RtpCodec::Ptr _audioRtpDecoder;
|
||||
RtpCodec::Ptr _videoRtpDecoder;
|
||||
};
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
#endif /* SRC_RTP_RTPPARSER_H_ */
|
||||
21
src/RtspMuxer/RtspMuxer.cpp
Normal file
21
src/RtspMuxer/RtspMuxer.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by xzl on 2018/10/24.
|
||||
//
|
||||
|
||||
#include "RtspMuxer.h"
|
||||
#include "Common/Factory.h"
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
void RtspMuxer::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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} /* namespace mediakit */
|
||||
135
src/RtspMuxer/RtspMuxer.h
Normal file
135
src/RtspMuxer/RtspMuxer.h
Normal file
@@ -0,0 +1,135 @@
|
||||
//
|
||||
// Created by xzl on 2018/10/24.
|
||||
//
|
||||
|
||||
#ifndef ZLMEDIAKIT_RTSPMAKER_H
|
||||
#define ZLMEDIAKIT_RTSPMAKER_H
|
||||
|
||||
#include "RtspSdp.h"
|
||||
|
||||
namespace mediakit{
|
||||
/**
|
||||
* rtsp生成器
|
||||
*/
|
||||
class RtspMuxer : public FrameRingInterface , public RtpRingInterface{
|
||||
public:
|
||||
/**
|
||||
* 构成函数
|
||||
*/
|
||||
RtspMuxer(){
|
||||
_rtpRing = std::make_shared<RtpRingInterface::RingType>();
|
||||
_frameRing = std::make_shared<FrameRingInterface::RingType>();
|
||||
}
|
||||
virtual ~RtspMuxer(){}
|
||||
|
||||
/**
|
||||
* 添加音视频或title 媒体
|
||||
* @param sdp 媒体描述对象
|
||||
* @param ssrc 媒体rtp ssrc
|
||||
* @param mtu 媒体rtp mtu
|
||||
*/
|
||||
void addTrack(const Sdp::Ptr & sdp,uint32_t ssrc = 0,int mtu = 1400){
|
||||
if(ssrc == 0){
|
||||
ssrc = ((uint64_t) sdp.get()) & 0xFFFFFFFF;
|
||||
}
|
||||
sdp->createRtpEncoder(ssrc, mtu);
|
||||
sdp->setFrameRing(_frameRing);
|
||||
sdp->setRtpRing(_rtpRing);
|
||||
_sdp_map[sdp->getTrackType()] = sdp;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加音视频或title 媒体
|
||||
* @param track 媒体描述
|
||||
* @param ssrc 媒体rtp ssrc
|
||||
* @param mtu 媒体rtp mtu
|
||||
*/
|
||||
void addTrack(const Track::Ptr & track,uint32_t ssrc = 0,int mtu = 1400) ;
|
||||
|
||||
/**
|
||||
* 获取完整的SDP字符串
|
||||
* @return SDP字符串
|
||||
*/
|
||||
string getSdp() {
|
||||
_StrPrinter printer;
|
||||
for(auto &pr : _sdp_map){
|
||||
printer << pr.second->getSdp() ;
|
||||
}
|
||||
return printer;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 写入帧数据然后打包rtp
|
||||
* @param frame 帧数据
|
||||
*/
|
||||
void inputFrame(const Frame::Ptr &frame) override {
|
||||
auto it = _sdp_map.find(frame->getTrackType());
|
||||
if(it == _sdp_map.end()){
|
||||
return ;
|
||||
}
|
||||
it->second->inputFrame(frame);
|
||||
}
|
||||
|
||||
/**
|
||||
* 也可以在外部打包好rtp然后再写入
|
||||
* @param rtp rtp包
|
||||
* @param key_pos 是否为关键帧的第一个rtp包
|
||||
*/
|
||||
bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = true) override {
|
||||
auto it = _sdp_map.find(rtp->getTrackType());
|
||||
if(it == _sdp_map.end()){
|
||||
return false;
|
||||
}
|
||||
return it->second->inputRtp(rtp,key_pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取rtp环形缓存
|
||||
* @return
|
||||
*/
|
||||
RtpRingInterface::RingType::Ptr getRtpRing() const override{
|
||||
return _rtpRing;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取帧环形缓存
|
||||
* @return
|
||||
*/
|
||||
FrameRingInterface::RingType::Ptr getFrameRing() const override{
|
||||
return _frameRing;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置帧环形缓存
|
||||
* @param ring
|
||||
*/
|
||||
void setFrameRing(const FrameRingInterface::RingType::Ptr &ring) override{
|
||||
_frameRing = ring;
|
||||
for(auto &pr : _sdp_map){
|
||||
pr.second->setFrameRing(ring);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置rtp环形缓存
|
||||
* @param ring
|
||||
*/
|
||||
void setRtpRing(const RtpRingInterface::RingType::Ptr &ring) override{
|
||||
_rtpRing = ring;
|
||||
for(auto &pr : _sdp_map){
|
||||
pr.second->setRtpRing(ring);
|
||||
}
|
||||
}
|
||||
private:
|
||||
map<int,Sdp::Ptr> _sdp_map;
|
||||
RtpRingInterface::RingType::Ptr _rtpRing;
|
||||
FrameRingInterface::RingType::Ptr _frameRing;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
#endif //ZLMEDIAKIT_RTSPMAKER_H
|
||||
17
src/RtspMuxer/RtspSdp.cpp
Normal file
17
src/RtspMuxer/RtspSdp.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "RtspSdp.h"
|
||||
#include "Common/Factory.h"
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
void Sdp::createRtpEncoder(uint32_t ssrc, int mtu) {
|
||||
_encoder = Factory::getRtpEncoderById(getCodecId(),
|
||||
ssrc,
|
||||
mtu,
|
||||
_sample_rate,
|
||||
_playload_type,
|
||||
getTrackType() * 2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
265
src/RtspMuxer/RtspSdp.h
Normal file
265
src/RtspMuxer/RtspSdp.h
Normal file
@@ -0,0 +1,265 @@
|
||||
//
|
||||
// Created by xzl on 2018/9/18.
|
||||
//
|
||||
|
||||
#ifndef ZLMEDIAKIT_RTSPSDP_H
|
||||
#define ZLMEDIAKIT_RTSPSDP_H
|
||||
|
||||
#include "RtspMuxer/H264RtpCodec.h"
|
||||
#include "RtspMuxer/AACRtpCodec.h"
|
||||
#include "Util/base64.h"
|
||||
#include "Player/Track.h"
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
/**
|
||||
* sdp基类
|
||||
*/
|
||||
class Sdp : public FrameRingInterface , public RtpRingInterface , public CodecInfo{
|
||||
public:
|
||||
typedef std::shared_ptr<Sdp> Ptr;
|
||||
|
||||
/**
|
||||
* 构造sdp
|
||||
* @param sample_rate 采样率
|
||||
* @param playload_type pt类型
|
||||
*/
|
||||
Sdp(uint32_t sample_rate, uint8_t playload_type){
|
||||
_sample_rate = sample_rate;
|
||||
_playload_type = playload_type;
|
||||
}
|
||||
|
||||
virtual ~Sdp(){}
|
||||
|
||||
/**
|
||||
* 获取sdp字符串
|
||||
* @return
|
||||
*/
|
||||
virtual string getSdp() const = 0;
|
||||
|
||||
/**
|
||||
* 返回音频或视频类型
|
||||
* @return
|
||||
*/
|
||||
TrackType getTrackType() const override {
|
||||
return TrackInvalid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回编码器id
|
||||
* @return
|
||||
*/
|
||||
CodecId getCodecId() const override{
|
||||
return CodecInvalid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取帧环形缓存
|
||||
* @return
|
||||
*/
|
||||
FrameRingInterface::RingType::Ptr getFrameRing() const override {
|
||||
return _encoder->getFrameRing();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取rtp环形缓存
|
||||
* @return
|
||||
*/
|
||||
RtpRingInterface::RingType::Ptr getRtpRing() const override{
|
||||
return _encoder->getRtpRing();
|
||||
}
|
||||
|
||||
/**
|
||||
* 输入帧数据,驱动rtp打包
|
||||
* @param frame 帧数据
|
||||
*/
|
||||
void inputFrame(const Frame::Ptr &frame) override{
|
||||
_encoder->inputFrame(frame);
|
||||
}
|
||||
|
||||
/**
|
||||
* 也可以在外部打包rtp后直接输入rtp
|
||||
* @param rtp rtp数据包
|
||||
* @param key_pos 是否为关键帧第一个rtp包
|
||||
*/
|
||||
bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) override{
|
||||
return _encoder->inputRtp(rtp,key_pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换帧环形缓存,目的是多个rtp打包器共用一个环形缓存
|
||||
* @param ring
|
||||
*/
|
||||
void setFrameRing(const FrameRingInterface::RingType::Ptr &ring) override{
|
||||
_encoder->setFrameRing(ring);
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换帧环形缓存,目的是多个rtp打包器共用一个环形缓存
|
||||
* @param ring
|
||||
*/
|
||||
void setRtpRing(const RtpRingInterface::RingType::Ptr &ring) override{
|
||||
_encoder->setRtpRing(ring);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Rtp打包器
|
||||
* @param ssrc 打包器ssrc,可以为0
|
||||
* @param mtu mtu大小,一般小于1500字节,推荐1400
|
||||
*/
|
||||
virtual void createRtpEncoder(uint32_t ssrc, int mtu);
|
||||
private:
|
||||
RtpCodec::Ptr _encoder;
|
||||
uint8_t _playload_type;
|
||||
uint32_t _sample_rate;
|
||||
};
|
||||
|
||||
/**
|
||||
* sdp中除音视频外的其他描述部分
|
||||
*/
|
||||
class TitleSdp : public Sdp{
|
||||
public:
|
||||
|
||||
/**
|
||||
* 构造title类型sdp
|
||||
* @param dur_sec rtsp点播时长,0代表直播,单位秒
|
||||
* @param header 自定义sdp描述
|
||||
* @param version sdp版本
|
||||
*/
|
||||
TitleSdp(float dur_sec = 0,
|
||||
const map<string,string> &header = map<string,string>(),
|
||||
int version = 0) : Sdp(0,0){
|
||||
_printer << "v=" << version << "\r\n";
|
||||
|
||||
if(!header.empty()){
|
||||
for (auto &pr : header){
|
||||
_printer << pr.first << "=" << pr.second << "\r\n";
|
||||
}
|
||||
} else {
|
||||
_printer << "o=- 1383190487994921 1 IN IP4 0.0.0.0\r\n";
|
||||
_printer << "s=RTSP Session, streamed by the ZLMediaKit\r\n";
|
||||
_printer << "i=ZLMediaKit Live Stream\r\n";
|
||||
_printer << "c=IN IP4 0.0.0.0\r\n";
|
||||
_printer << "t=0 0\r\n";
|
||||
}
|
||||
|
||||
if(dur_sec <= 0){
|
||||
_printer << "a=range:npt=0-\r\n";
|
||||
}else{
|
||||
_printer << "a=range:npt=0-" << dur_sec << "\r\n";
|
||||
}
|
||||
_printer << "a=control:*\r\n";
|
||||
}
|
||||
string getSdp() const override {
|
||||
return _printer;
|
||||
}
|
||||
private:
|
||||
_StrPrinter _printer;
|
||||
};
|
||||
|
||||
/**
|
||||
* h264类型sdp
|
||||
*/
|
||||
class H264Sdp : public Sdp {
|
||||
public:
|
||||
|
||||
/**
|
||||
*
|
||||
* @param sps 264 sps,不带0x00000001头
|
||||
* @param pps 264 pps,不带0x00000001头
|
||||
* @param playload_type rtp playload type 默认96
|
||||
* @param bitrate 比特率
|
||||
*/
|
||||
H264Sdp(const string &strSPS,
|
||||
const string &strPPS,
|
||||
int playload_type = 96,
|
||||
int bitrate = 4000) : Sdp(90000,playload_type) {
|
||||
//视频通道
|
||||
_printer << "m=video 0 RTP/AVP " << playload_type << "\r\n";
|
||||
_printer << "b=AS:" << bitrate << "\r\n";
|
||||
_printer << "a=rtpmap:" << playload_type << " H264/" << 90000 << "\r\n";
|
||||
_printer << "a=fmtp:" << playload_type << " packetization-mode=1;profile-level-id=";
|
||||
|
||||
char strTemp[100];
|
||||
int profile_level_id = 0;
|
||||
if (strSPS.length() >= 4) { // sanity check
|
||||
profile_level_id = (strSPS[1] << 16) | (strSPS[2] << 8) | strSPS[3]; // profile_idc|constraint_setN_flag|level_idc
|
||||
}
|
||||
memset(strTemp, 0, 100);
|
||||
sprintf(strTemp, "%06X", profile_level_id);
|
||||
_printer << strTemp;
|
||||
_printer << ";sprop-parameter-sets=";
|
||||
memset(strTemp, 0, 100);
|
||||
av_base64_encode(strTemp, 100, (uint8_t *) strSPS.data(), strSPS.size());
|
||||
_printer << strTemp << ",";
|
||||
memset(strTemp, 0, 100);
|
||||
av_base64_encode(strTemp, 100, (uint8_t *) strPPS.data(), strPPS.size());
|
||||
_printer << strTemp << "\r\n";
|
||||
_printer << "a=control:trackID=" << getTrackType() << "\r\n";
|
||||
}
|
||||
|
||||
string getSdp() const override {
|
||||
return _printer;
|
||||
}
|
||||
|
||||
TrackType getTrackType() const override {
|
||||
return TrackVideo;
|
||||
}
|
||||
|
||||
CodecId getCodecId() const override {
|
||||
return CodecH264;
|
||||
}
|
||||
private:
|
||||
_StrPrinter _printer;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* aac类型SDP
|
||||
*/
|
||||
class AACSdp : public Sdp {
|
||||
public:
|
||||
|
||||
/**
|
||||
*
|
||||
* @param aac_cfg aac两个字节的配置描述
|
||||
* @param sample_rate 音频采样率
|
||||
* @param playload_type rtp playload type 默认98
|
||||
* @param bitrate 比特率
|
||||
*/
|
||||
AACSdp(const string &aac_cfg,
|
||||
int sample_rate,
|
||||
int playload_type = 98,
|
||||
int bitrate = 128) : Sdp(sample_rate,playload_type){
|
||||
_printer << "m=audio 0 RTP/AVP " << playload_type << "\r\n";
|
||||
_printer << "b=AS:" << bitrate << "\r\n";
|
||||
_printer << "a=rtpmap:" << playload_type << " MPEG4-GENERIC/" << sample_rate << "\r\n";
|
||||
|
||||
char configStr[32] = {0};
|
||||
snprintf(configStr, sizeof(configStr), "%02X%02x", aac_cfg[0], aac_cfg[1]);
|
||||
_printer << "a=fmtp:" << playload_type << " streamtype=5;profile-level-id=1;mode=AAC-hbr;"
|
||||
<< "sizelength=13;indexlength=3;indexdeltalength=3;config="
|
||||
<< configStr << "\r\n";
|
||||
_printer << "a=control:trackID=" << getTrackType() << "\r\n";
|
||||
}
|
||||
|
||||
string getSdp() const override {
|
||||
return _printer;
|
||||
}
|
||||
|
||||
TrackType getTrackType() const override {
|
||||
return TrackAudio;
|
||||
};
|
||||
CodecId getCodecId() const override {
|
||||
return CodecAAC;
|
||||
}
|
||||
private:
|
||||
_StrPrinter _printer;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
|
||||
|
||||
#endif //ZLMEDIAKIT_RTSPSDP_H
|
||||
Reference in New Issue
Block a user