From 510d908bc400fc0dc0211e63b48e502814652f08 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Mon, 21 Jun 2021 20:32:13 +0800 Subject: [PATCH] =?UTF-8?q?rtp=E6=9C=8D=E5=8A=A1=E5=99=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81opus=E6=8E=A8=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Rtp/GB28181Process.cpp | 84 ++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 30 deletions(-) diff --git a/src/Rtp/GB28181Process.cpp b/src/Rtp/GB28181Process.cpp index 9c611f66..6400c98f 100644 --- a/src/Rtp/GB28181Process.cpp +++ b/src/Rtp/GB28181Process.cpp @@ -15,6 +15,7 @@ #include "Extension/CommonRtp.h" #include "Extension/H264Rtp.h" #include "Extension/Factory.h" +#include "Extension/Opus.h" namespace mediakit{ @@ -26,15 +27,16 @@ static inline bool checkTS(const uint8_t *packet, size_t bytes){ class RtpReceiverImp : public RtpReceiver { public: using Ptr = std::shared_ptr; - RtpReceiverImp( function cb, function cb_before = nullptr){ + RtpReceiverImp(int sample_rate, function cb, function cb_before = nullptr){ + _sample_rate = sample_rate; _on_sort = std::move(cb); _on_before_sort = std::move(cb_before); } ~RtpReceiverImp() override = default; - bool inputRtp(TrackType type, int samplerate, uint8_t *ptr, size_t len){ - return handleOneRtp((int) type, type, samplerate, ptr, len); + bool inputRtp(TrackType type, uint8_t *ptr, size_t len){ + return handleOneRtp((int) type, type, _sample_rate, ptr, len); } protected: @@ -49,6 +51,7 @@ protected: } private: + int _sample_rate; function _on_sort; function _on_before_sort; }; @@ -63,49 +66,66 @@ GB28181Process::GB28181Process(const MediaInfo &media_info, MediaSinkInterface * GB28181Process::~GB28181Process() {} -bool GB28181Process::inputRtp(bool, const char *data, size_t data_len) { - RtpHeader *header = (RtpHeader *) data; - auto &ref = _rtp_receiver[header->pt]; - if (!ref) { - ref = std::make_shared([this](RtpPacket::Ptr rtp) { - onRtpSorted(std::move(rtp)); - }); - } - return ref->inputRtp(TrackVideo, RtpPayload::getClockRate(header->pt), (unsigned char *) data, data_len); +void GB28181Process::onRtpSorted(RtpPacket::Ptr rtp) { + _rtp_decoder[rtp->getHeader()->pt]->inputRtp(rtp, false); } -void GB28181Process::onRtpSorted(RtpPacket::Ptr rtp) { - auto pt = rtp->getHeader()->pt; - auto &ref = _rtp_decoder[pt]; +bool GB28181Process::inputRtp(bool, const char *data, size_t data_len) { + RtpHeader *header = (RtpHeader *) data; + auto pt = header->pt; + auto &ref = _rtp_receiver[pt]; if (!ref) { + if (_rtp_receiver.size() > 2) { + //防止pt类型太多导致内存溢出 + throw std::invalid_argument("rtp pt类型不得超过2种!"); + } switch (pt) { + case 100: { + //opus负载 + ref = std::make_shared(48000,[this](RtpPacket::Ptr rtp) { + onRtpSorted(std::move(rtp)); + }); + + auto track = std::make_shared(); + _interface->addTrack(track); + _rtp_decoder[pt] = Factory::getRtpDecoderByTrack(track); + break; + } + case 99: { //H265负载 + ref = std::make_shared(90000,[this](RtpPacket::Ptr rtp) { + onRtpSorted(std::move(rtp)); + }); + auto track = std::make_shared(); _interface->addTrack(track); - ref = Factory::getRtpDecoderByTrack(track); + _rtp_decoder[pt] = Factory::getRtpDecoderByTrack(track); break; } case 98: { //H264负载 + ref = std::make_shared(90000,[this](RtpPacket::Ptr rtp) { + onRtpSorted(std::move(rtp)); + }); + auto track = std::make_shared(); _interface->addTrack(track); - ref = Factory::getRtpDecoderByTrack(track); - break; - } - case 0: { - //CodecG711U - auto track = std::make_shared(CodecG711U, 8000, 1, 16); - _interface->addTrack(track); - ref = Factory::getRtpDecoderByTrack(track); + _rtp_decoder[pt] = Factory::getRtpDecoderByTrack(track); break; } + case 0: + //CodecG711U case 8: { //CodecG711A - auto track = std::make_shared(CodecG711A, 8000, 1, 16); + ref = std::make_shared(8000,[this](RtpPacket::Ptr rtp) { + onRtpSorted(std::move(rtp)); + }); + + auto track = std::make_shared(pt == 0 ? CodecG711U : CodecG711A, 8000, 1, 16); _interface->addTrack(track); - ref = Factory::getRtpDecoderByTrack(track); + _rtp_decoder[pt] = Factory::getRtpDecoderByTrack(track); break; } @@ -113,8 +133,13 @@ void GB28181Process::onRtpSorted(RtpPacket::Ptr rtp) { if (pt != 33 && pt != 96) { WarnL << "rtp payload type未识别(" << (int) pt << "),已按ts或ps负载处理"; } + + ref = std::make_shared(90000,[this](RtpPacket::Ptr rtp) { + onRtpSorted(std::move(rtp)); + }); + //ts或ps负载 - ref = std::make_shared(CodecInvalid, 32 * 1024); + _rtp_decoder[pt] = std::make_shared(CodecInvalid, 32 * 1024); //设置dump目录 GET_CONFIG(string, dump_dir, RtpProxy::kDumpDir); if (!dump_dir.empty()) { @@ -130,13 +155,12 @@ void GB28181Process::onRtpSorted(RtpPacket::Ptr rtp) { } //设置frame回调 - ref->addDelegate(std::make_shared([this](const Frame::Ptr &frame) { + _rtp_decoder[pt]->addDelegate(std::make_shared([this](const Frame::Ptr &frame) { onRtpDecode(frame); })); } - //解码rtp - ref->inputRtp(rtp, false); + return ref->inputRtp(TrackVideo, (unsigned char *) data, data_len); } const char *GB28181Process::onSearchPacketTail(const char *packet,size_t bytes){