From c90f34880139e509bb620e1489652410e42f38c0 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Mon, 21 Jun 2021 19:58:59 +0800 Subject: [PATCH] =?UTF-8?q?RTP=E6=9C=8D=E5=8A=A1=E5=99=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81ES=E6=B7=B7=E5=90=88=E6=B5=81,=20=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E6=96=B0=E5=A2=9EH265/G711=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Rtp/GB28181Process.cpp | 89 +++++++++++++++++++++++++++++++++----- src/Rtp/GB28181Process.h | 8 ++-- 2 files changed, 82 insertions(+), 15 deletions(-) diff --git a/src/Rtp/GB28181Process.cpp b/src/Rtp/GB28181Process.cpp index ca5b71da..9c611f66 100644 --- a/src/Rtp/GB28181Process.cpp +++ b/src/Rtp/GB28181Process.cpp @@ -14,6 +14,7 @@ #include "Http/HttpTSPlayer.h" #include "Extension/CommonRtp.h" #include "Extension/H264Rtp.h" +#include "Extension/Factory.h" namespace mediakit{ @@ -22,6 +23,38 @@ static inline bool checkTS(const uint8_t *packet, size_t bytes){ return bytes % TS_PACKET_SIZE == 0 && packet[0] == TS_SYNC_BYTE; } +class RtpReceiverImp : public RtpReceiver { +public: + using Ptr = std::shared_ptr; + RtpReceiverImp( function cb, function cb_before = nullptr){ + _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); + } + +protected: + void onRtpSorted(RtpPacket::Ptr rtp, int track_index) override { + _on_sort(std::move(rtp)); + } + + void onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_index) override { + if (_on_before_sort) { + _on_before_sort(rtp); + } + } + +private: + function _on_sort; + function _on_before_sort; +}; + +/////////////////////////////////////////////////////////////////////////////////////////// + GB28181Process::GB28181Process(const MediaInfo &media_info, MediaSinkInterface *interface) { assert(interface); _media_info = media_info; @@ -31,25 +64,57 @@ GB28181Process::GB28181Process(const MediaInfo &media_info, MediaSinkInterface * GB28181Process::~GB28181Process() {} bool GB28181Process::inputRtp(bool, const char *data, size_t data_len) { - return handleOneRtp(0, TrackVideo, 90000, (unsigned char *) data, 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, int) { +void GB28181Process::onRtpSorted(RtpPacket::Ptr rtp) { auto pt = rtp->getHeader()->pt; - if (!_rtp_decoder) { + auto &ref = _rtp_decoder[pt]; + if (!ref) { switch (pt) { - case 98: { - //H264负载 - _rtp_decoder = std::make_shared(); - _interface->addTrack(std::make_shared()); + case 99: { + //H265负载 + auto track = std::make_shared(); + _interface->addTrack(track); + ref = Factory::getRtpDecoderByTrack(track); break; } + case 98: { + //H264负载 + 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); + break; + } + + case 8: { + //CodecG711A + auto track = std::make_shared(CodecG711A, 8000, 1, 16); + _interface->addTrack(track); + ref = Factory::getRtpDecoderByTrack(track); + break; + } + default: { if (pt != 33 && pt != 96) { WarnL << "rtp payload type未识别(" << (int) pt << "),已按ts或ps负载处理"; } //ts或ps负载 - _rtp_decoder = std::make_shared(CodecInvalid, 32 * 1024); + ref = std::make_shared(CodecInvalid, 32 * 1024); //设置dump目录 GET_CONFIG(string, dump_dir, RtpProxy::kDumpDir); if (!dump_dir.empty()) { @@ -65,13 +130,13 @@ void GB28181Process::onRtpSorted(RtpPacket::Ptr rtp, int) { } //设置frame回调 - _rtp_decoder->addDelegate(std::make_shared([this](const Frame::Ptr &frame) { + ref->addDelegate(std::make_shared([this](const Frame::Ptr &frame) { onRtpDecode(frame); })); } //解码rtp - _rtp_decoder->inputRtp(rtp, false); + ref->inputRtp(rtp, false); } const char *GB28181Process::onSearchPacketTail(const char *packet,size_t bytes){ @@ -96,8 +161,8 @@ const char *GB28181Process::onSearchPacketTail(const char *packet,size_t bytes){ } void GB28181Process::onRtpDecode(const Frame::Ptr &frame) { - if (frame->getCodecId() == CodecH264) { - //这是H264 + if (frame->getCodecId() != CodecInvalid) { + //这里不是ps或ts _interface->inputFrame(frame); return; } diff --git a/src/Rtp/GB28181Process.h b/src/Rtp/GB28181Process.h index c4149bc2..4d5d06d4 100644 --- a/src/Rtp/GB28181Process.h +++ b/src/Rtp/GB28181Process.h @@ -21,7 +21,8 @@ namespace mediakit{ -class GB28181Process : public HttpRequestSplitter, public RtpReceiver, public ProcessInterface{ +class RtpReceiverImp; +class GB28181Process : public HttpRequestSplitter, public ProcessInterface{ public: typedef std::shared_ptr Ptr; GB28181Process(const MediaInfo &media_info, MediaSinkInterface *interface); @@ -36,7 +37,7 @@ public: bool inputRtp(bool, const char *data, size_t data_len) override; protected: - void onRtpSorted(RtpPacket::Ptr rtp, int track_index) override ; + void onRtpSorted(RtpPacket::Ptr rtp); const char *onSearchPacketTail(const char *data,size_t len) override; ssize_t onRecvHeader(const char *data,size_t len) override { return 0; }; @@ -48,7 +49,8 @@ private: DecoderImp::Ptr _decoder; MediaSinkInterface *_interface; std::shared_ptr _save_file_ps; - std::shared_ptr _rtp_decoder; + unordered_map > _rtp_decoder; + unordered_map > _rtp_receiver; }; }//namespace mediakit