mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-20 07:12:21 +08:00
for rtp server can config pt
This commit is contained in:
@@ -10,21 +10,21 @@
|
||||
|
||||
#if defined(ENABLE_RTPPROXY)
|
||||
#include "GB28181Process.h"
|
||||
#include "Util/File.h"
|
||||
#include "Http/HttpTSPlayer.h"
|
||||
#include "Extension/CommonRtp.h"
|
||||
#include "Extension/H264Rtp.h"
|
||||
#include "Extension/Factory.h"
|
||||
#include "Extension/Opus.h"
|
||||
#include "Extension/G711.h"
|
||||
#include "Extension/H264Rtp.h"
|
||||
#include "Extension/H265.h"
|
||||
#include "Extension/Opus.h"
|
||||
#include "Http/HttpTSPlayer.h"
|
||||
#include "Util/File.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
//判断是否为ts负载
|
||||
// 判断是否为ts负载
|
||||
static inline bool checkTS(const uint8_t *packet, size_t bytes) {
|
||||
return bytes % TS_PACKET_SIZE == 0 && packet[0] == TS_SYNC_BYTE;
|
||||
}
|
||||
@@ -37,13 +37,13 @@ public:
|
||||
_sample_rate = sample_rate;
|
||||
setOnSorted(std::move(cb));
|
||||
setBeforeSorted(std::move(cb_before));
|
||||
//GB28181推流不支持ntp时间戳
|
||||
// GB28181推流不支持ntp时间戳
|
||||
setNtpStamp(0, 0);
|
||||
}
|
||||
|
||||
~RtpReceiverImp() override = default;
|
||||
|
||||
bool inputRtp(TrackType type, uint8_t *ptr, size_t len){
|
||||
bool inputRtp(TrackType type, uint8_t *ptr, size_t len) {
|
||||
return RtpTrack::inputRtp(type, _sample_rate, ptr, len).operator bool();
|
||||
}
|
||||
|
||||
@@ -66,119 +66,101 @@ void GB28181Process::onRtpSorted(RtpPacket::Ptr rtp) {
|
||||
}
|
||||
|
||||
bool GB28181Process::inputRtp(bool, const char *data, size_t data_len) {
|
||||
RtpHeader *header = (RtpHeader *) data;
|
||||
GET_CONFIG(uint32_t, h264_pt, RtpProxy::KH264PT);
|
||||
GET_CONFIG(uint32_t, h265_pt, RtpProxy::KH265PT);
|
||||
GET_CONFIG(uint32_t, ps_pt, RtpProxy::KPSPT);
|
||||
GET_CONFIG(uint32_t, ts_pt, RtpProxy::KTSPT);
|
||||
GET_CONFIG(uint32_t, opus_pt, RtpProxy::KOpusPT);
|
||||
GET_CONFIG(uint32_t, g711u_pt, RtpProxy::KG711UPT);
|
||||
GET_CONFIG(uint32_t, g711a_pt, RtpProxy::KG711APT);
|
||||
|
||||
RtpHeader *header = (RtpHeader *)data;
|
||||
auto pt = header->pt;
|
||||
auto &ref = _rtp_receiver[pt];
|
||||
if (!ref) {
|
||||
if (_rtp_receiver.size() > 2) {
|
||||
//防止pt类型太多导致内存溢出
|
||||
// 防止pt类型太多导致内存溢出
|
||||
throw std::invalid_argument("rtp pt类型不得超过2种!");
|
||||
}
|
||||
switch (pt) {
|
||||
case 100: {
|
||||
//opus负载
|
||||
ref = std::make_shared<RtpReceiverImp>(48000,[this](RtpPacket::Ptr rtp) {
|
||||
onRtpSorted(std::move(rtp));
|
||||
});
|
||||
if (pt == opus_pt) {
|
||||
// opus负载
|
||||
ref = std::make_shared<RtpReceiverImp>(48000, [this](RtpPacket::Ptr rtp) { onRtpSorted(std::move(rtp)); });
|
||||
|
||||
auto track = std::make_shared<OpusTrack>();
|
||||
_interface->addTrack(track);
|
||||
_rtp_decoder[pt] = Factory::getRtpDecoderByTrack(track);
|
||||
break;
|
||||
auto track = std::make_shared<OpusTrack>();
|
||||
_interface->addTrack(track);
|
||||
_rtp_decoder[pt] = Factory::getRtpDecoderByTrack(track);
|
||||
} else if (pt == h265_pt) {
|
||||
// H265负载
|
||||
ref = std::make_shared<RtpReceiverImp>(90000, [this](RtpPacket::Ptr rtp) { onRtpSorted(std::move(rtp)); });
|
||||
|
||||
auto track = std::make_shared<H265Track>();
|
||||
_interface->addTrack(track);
|
||||
_rtp_decoder[pt] = Factory::getRtpDecoderByTrack(track);
|
||||
} else if (pt == h264_pt) {
|
||||
// H264负载
|
||||
ref = std::make_shared<RtpReceiverImp>(90000, [this](RtpPacket::Ptr rtp) { onRtpSorted(std::move(rtp)); });
|
||||
|
||||
auto track = std::make_shared<H264Track>();
|
||||
_interface->addTrack(track);
|
||||
_rtp_decoder[pt] = Factory::getRtpDecoderByTrack(track);
|
||||
} else if (pt == g711u_pt || pt == g711a_pt) {
|
||||
// CodecG711U
|
||||
// CodecG711A
|
||||
ref = std::make_shared<RtpReceiverImp>(8000, [this](RtpPacket::Ptr rtp) { onRtpSorted(std::move(rtp)); });
|
||||
|
||||
auto track = std::make_shared<G711Track>(pt == 0 ? CodecG711U : CodecG711A, 8000, 1, 16);
|
||||
_interface->addTrack(track);
|
||||
_rtp_decoder[pt] = Factory::getRtpDecoderByTrack(track);
|
||||
} else {
|
||||
if (pt != ts_pt && pt != ps_pt) {
|
||||
WarnL << "rtp payload type未识别(" << (int)pt << "),已按ts或ps负载处理";
|
||||
}
|
||||
|
||||
case 99: {
|
||||
//H265负载
|
||||
ref = std::make_shared<RtpReceiverImp>(90000,[this](RtpPacket::Ptr rtp) {
|
||||
onRtpSorted(std::move(rtp));
|
||||
ref = std::make_shared<RtpReceiverImp>(90000, [this](RtpPacket::Ptr rtp) { onRtpSorted(std::move(rtp)); });
|
||||
|
||||
// ts或ps负载
|
||||
_rtp_decoder[pt] = std::make_shared<CommonRtpDecoder>(CodecInvalid, 32 * 1024);
|
||||
// 设置dump目录
|
||||
GET_CONFIG(string, dump_dir, RtpProxy::kDumpDir);
|
||||
if (!dump_dir.empty()) {
|
||||
auto save_path = File::absolutePath(_media_info._streamid + ".mp2", dump_dir);
|
||||
_save_file_ps.reset(File::create_file(save_path.data(), "wb"), [](FILE *fp) {
|
||||
if (fp) {
|
||||
fclose(fp);
|
||||
}
|
||||
});
|
||||
|
||||
auto track = std::make_shared<H265Track>();
|
||||
_interface->addTrack(track);
|
||||
_rtp_decoder[pt] = Factory::getRtpDecoderByTrack(track);
|
||||
break;
|
||||
}
|
||||
case 98: {
|
||||
//H264负载
|
||||
ref = std::make_shared<RtpReceiverImp>(90000,[this](RtpPacket::Ptr rtp) {
|
||||
onRtpSorted(std::move(rtp));
|
||||
});
|
||||
|
||||
auto track = std::make_shared<H264Track>();
|
||||
_interface->addTrack(track);
|
||||
_rtp_decoder[pt] = Factory::getRtpDecoderByTrack(track);
|
||||
break;
|
||||
}
|
||||
|
||||
case 0:
|
||||
//CodecG711U
|
||||
case 8: {
|
||||
//CodecG711A
|
||||
ref = std::make_shared<RtpReceiverImp>(8000,[this](RtpPacket::Ptr rtp) {
|
||||
onRtpSorted(std::move(rtp));
|
||||
});
|
||||
|
||||
auto track = std::make_shared<G711Track>(pt == 0 ? CodecG711U : CodecG711A, 8000, 1, 16);
|
||||
_interface->addTrack(track);
|
||||
_rtp_decoder[pt] = Factory::getRtpDecoderByTrack(track);
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
if (pt != 33 && pt != 96) {
|
||||
WarnL << "rtp payload type未识别(" << (int) pt << "),已按ts或ps负载处理";
|
||||
}
|
||||
|
||||
ref = std::make_shared<RtpReceiverImp>(90000,[this](RtpPacket::Ptr rtp) {
|
||||
onRtpSorted(std::move(rtp));
|
||||
});
|
||||
|
||||
//ts或ps负载
|
||||
_rtp_decoder[pt] = std::make_shared<CommonRtpDecoder>(CodecInvalid, 32 * 1024);
|
||||
//设置dump目录
|
||||
GET_CONFIG(string, dump_dir, RtpProxy::kDumpDir);
|
||||
if (!dump_dir.empty()) {
|
||||
auto save_path = File::absolutePath(_media_info._streamid + ".mp2", dump_dir);
|
||||
_save_file_ps.reset(File::create_file(save_path.data(), "wb"), [](FILE *fp) {
|
||||
if (fp) {
|
||||
fclose(fp);
|
||||
}
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//设置frame回调
|
||||
// 设置frame回调
|
||||
_rtp_decoder[pt]->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame) {
|
||||
onRtpDecode(frame);
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
|
||||
return ref->inputRtp(TrackVideo, (unsigned char *) data, data_len);
|
||||
return ref->inputRtp(TrackVideo, (unsigned char *)data, data_len);
|
||||
}
|
||||
|
||||
void GB28181Process::onRtpDecode(const Frame::Ptr &frame) {
|
||||
if (frame->getCodecId() != CodecInvalid) {
|
||||
//这里不是ps或ts
|
||||
// 这里不是ps或ts
|
||||
_interface->inputFrame(frame);
|
||||
return;
|
||||
}
|
||||
|
||||
//这是TS或PS
|
||||
// 这是TS或PS
|
||||
if (_save_file_ps) {
|
||||
fwrite(frame->data(), frame->size(), 1, _save_file_ps.get());
|
||||
}
|
||||
|
||||
if (!_decoder) {
|
||||
//创建解码器
|
||||
if (checkTS((uint8_t *) frame->data(), frame->size())) {
|
||||
//猜测是ts负载
|
||||
// 创建解码器
|
||||
if (checkTS((uint8_t *)frame->data(), frame->size())) {
|
||||
// 猜测是ts负载
|
||||
InfoL << _media_info._streamid << " judged to be TS";
|
||||
_decoder = DecoderImp::createDecoder(DecoderImp::decoder_ts, _interface);
|
||||
} else {
|
||||
//猜测是ps负载
|
||||
// 猜测是ps负载
|
||||
InfoL << _media_info._streamid << " judged to be PS";
|
||||
_decoder = DecoderImp::createDecoder(DecoderImp::decoder_ps, _interface);
|
||||
}
|
||||
@@ -189,5 +171,5 @@ void GB28181Process::onRtpDecode(const Frame::Ptr &frame) {
|
||||
}
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
#endif//defined(ENABLE_RTPPROXY)
|
||||
} // namespace mediakit
|
||||
#endif // defined(ENABLE_RTPPROXY)
|
||||
|
||||
Reference in New Issue
Block a user