From 10884340b09e87846f66c4dfae7c62e5c86414cd Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sun, 8 Mar 2020 21:19:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96rtp=E6=8E=A8=E6=B5=81?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdpart/media-server | 2 +- src/Common/MediaSink.cpp | 1 + src/Extension/Factory.cpp | 4 ++-- src/Extension/Frame.cpp | 11 +++++++++++ src/Extension/Frame.h | 6 ++++++ src/Record/MP4Muxer.cpp | 2 +- src/Rtp/RtpDecoder.cpp | 13 ++----------- src/Rtp/RtpDecoder.h | 3 +-- src/Rtp/RtpProcess.cpp | 26 ++++++-------------------- src/Rtp/RtpProcess.h | 4 ---- 10 files changed, 31 insertions(+), 41 deletions(-) diff --git a/3rdpart/media-server b/3rdpart/media-server index 8d40dad3..1603194d 160000 --- a/3rdpart/media-server +++ b/3rdpart/media-server @@ -1 +1 @@ -Subproject commit 8d40dad3dbdce171756691d4511aca49fcf2a231 +Subproject commit 1603194dafdcecda9036e5741ec8d3e8c9ab1e01 diff --git a/src/Common/MediaSink.cpp b/src/Common/MediaSink.cpp index 9d224ac2..90cb1797 100644 --- a/src/Common/MediaSink.cpp +++ b/src/Common/MediaSink.cpp @@ -137,6 +137,7 @@ void MediaSink::emitAllTrackReady() { //移除未准备好的Track for (auto it = _track_map.begin(); it != _track_map.end();) { if (!it->second->ready()) { + WarnL << "该track长时间未被初始化,已忽略:" << it->second->getCodecName(); it = _track_map.erase(it); continue; } diff --git a/src/Extension/Factory.cpp b/src/Extension/Factory.cpp index cff9a4c0..6aedc4b5 100644 --- a/src/Extension/Factory.cpp +++ b/src/Extension/Factory.cpp @@ -155,7 +155,7 @@ RtpCodec::Ptr Factory::getRtpDecoderByTrack(const Track::Ptr &track) { case CodecAAC: return std::make_shared(track->clone()); default: - WarnL << "暂不支持该CodecId:" << track->getCodecId(); + WarnL << "暂不支持该CodecId:" << track->getCodecName(); return nullptr; } } @@ -212,7 +212,7 @@ RtmpCodec::Ptr Factory::getRtmpCodecByTrack(const Track::Ptr &track) { case CodecAAC: return std::make_shared(track); default: - WarnL << "暂不支持该CodecId:" << track->getCodecId(); + WarnL << "暂不支持该CodecId:" << track->getCodecName(); return nullptr; } } diff --git a/src/Extension/Frame.cpp b/src/Extension/Frame.cpp index 0b6f5232..05eaee3e 100644 --- a/src/Extension/Frame.cpp +++ b/src/Extension/Frame.cpp @@ -38,5 +38,16 @@ Frame::Ptr Frame::getCacheAbleFrame(const Frame::Ptr &frame){ return std::make_shared(frame); } +#define SWITCH_CASE(codec_id) case codec_id : return #codec_id +char *CodecInfo::getCodecName() { + switch (getCodecId()) { + SWITCH_CASE(CodecH264); + SWITCH_CASE(CodecH265); + SWITCH_CASE(CodecAAC); + default: + return "unknown codec"; + } +} + }//namespace mediakit diff --git a/src/Extension/Frame.h b/src/Extension/Frame.h index d4cbca6b..4819c5f1 100644 --- a/src/Extension/Frame.h +++ b/src/Extension/Frame.h @@ -72,6 +72,12 @@ public: * 获取编解码器类型 */ virtual CodecId getCodecId() const = 0; + + /** + * 获取编码器名称 + * @return 编码器名称 + */ + char *getCodecName(); }; /** diff --git a/src/Record/MP4Muxer.cpp b/src/Record/MP4Muxer.cpp index 785fedbd..a8dd967a 100644 --- a/src/Record/MP4Muxer.cpp +++ b/src/Record/MP4Muxer.cpp @@ -258,7 +258,7 @@ void MP4Muxer::addTrack(const Track::Ptr &track) { } break; default: - WarnL << "MP4录制不支持该编码格式:" << track->getCodecId(); + WarnL << "MP4录制不支持该编码格式:" << track->getCodecName(); break; } } diff --git a/src/Rtp/RtpDecoder.cpp b/src/Rtp/RtpDecoder.cpp index af64f0d5..c0bebb8a 100644 --- a/src/Rtp/RtpDecoder.cpp +++ b/src/Rtp/RtpDecoder.cpp @@ -25,7 +25,6 @@ */ #if defined(ENABLE_RTPPROXY) -#include #include "Util/logger.h" #include "RtpDecoder.h" #include "rtp-payload.h" @@ -44,13 +43,7 @@ RtpDecoder::~RtpDecoder() { } } -void RtpDecoder::decodeRtp(const void *data, int bytes,const string &type_name) { - if(_rtp_type != type_name && _rtp_decoder){ - //rtp类型发生变化,切换之 - rtp_payload_decode_destroy(_rtp_decoder); - _rtp_decoder = nullptr; - } - +void RtpDecoder::decodeRtp(const void *data, int bytes) { if(!_rtp_decoder){ static rtp_payload_t s_func= { [](void* param, int bytes){ @@ -69,11 +62,9 @@ void RtpDecoder::decodeRtp(const void *data, int bytes,const string &type_name) uint8_t rtp_type = 0x7F & ((uint8_t *) data)[1]; InfoL << "rtp type:" << (int) rtp_type; - _rtp_decoder = rtp_payload_decode_create(rtp_type, type_name.data(), &s_func, this); + _rtp_decoder = rtp_payload_decode_create(rtp_type, "MP4V-ES", &s_func, this); if (!_rtp_decoder) { WarnL << "unsupported rtp type:" << (int) rtp_type << ",size:" << bytes << ",hexdump" << hexdump(data, bytes > 16 ? 16 : bytes); - }else{ - _rtp_type = type_name; } } diff --git a/src/Rtp/RtpDecoder.h b/src/Rtp/RtpDecoder.h index a6d78a65..52a32022 100644 --- a/src/Rtp/RtpDecoder.h +++ b/src/Rtp/RtpDecoder.h @@ -38,12 +38,11 @@ public: RtpDecoder(); virtual ~RtpDecoder(); protected: - void decodeRtp(const void *data, int bytes,const string &type_name); + void decodeRtp(const void *data, int bytes); virtual void onRtpDecode(const uint8_t *packet, int bytes, uint32_t timestamp, int flags) = 0; private: void *_rtp_decoder = nullptr; BufferRaw::Ptr _buffer; - string _rtp_type; }; }//namespace mediakit diff --git a/src/Rtp/RtpProcess.cpp b/src/Rtp/RtpProcess.cpp index ad25d870..eeba54b7 100644 --- a/src/Rtp/RtpProcess.cpp +++ b/src/Rtp/RtpProcess.cpp @@ -33,8 +33,6 @@ namespace mediakit{ -static const vector kRtpTypes = {"MP2P","MP4V-ES"}; - /** * 合并一些时间戳相同的frame */ @@ -85,7 +83,6 @@ RtpProcess::RtpProcess(uint32_t ssrc) { _track->_samplerate = 90000; _track->_type = TrackVideo; _track->_ssrc = _ssrc; - getNextRtpType(); DebugL << printSSRC(_ssrc); GET_CONFIG(bool,toRtxp,General::kPublishToRtxp); @@ -155,12 +152,9 @@ bool RtpProcess::inputRtp(const char *data, int data_len,const struct sockaddr * return ret; } -void RtpProcess::getNextRtpType(){ - _rtp_type = kRtpTypes[_rtp_type_idx++]; - _rtp_dec_failed_cnt = 0; - if(_rtp_type_idx == kRtpTypes.size()){ - _rtp_type_idx = 0; - } +//判断是否为ts负载 +static inline bool checkTS(const uint8_t *packet, int bytes){ + return bytes % 188 == 0 && packet[0] == 0x47; } void RtpProcess::onRtpSorted(const RtpPacket::Ptr &rtp, int) { @@ -168,25 +162,23 @@ void RtpProcess::onRtpSorted(const RtpPacket::Ptr &rtp, int) { WarnL << rtp->sequence << " != " << _sequence << "+1"; } _sequence = rtp->sequence; - if(_save_file_rtp){ uint16_t size = rtp->size() - 4; size = htons(size); fwrite((uint8_t *) &size, 2, 1, _save_file_rtp.get()); fwrite((uint8_t *) rtp->data() + 4, rtp->size() - 4, 1, _save_file_rtp.get()); } - - decodeRtp(rtp->data() + 4 ,rtp->size() - 4,_rtp_type); + decodeRtp(rtp->data() + 4 ,rtp->size() - 4); } -void RtpProcess::onRtpDecode(const uint8_t *packet, int bytes, uint32_t, int flags) { +void RtpProcess::onRtpDecode(const uint8_t *packet, int bytes, uint32_t timestamp, int flags) { if(_save_file_ps){ fwrite((uint8_t *)packet,bytes, 1, _save_file_ps.get()); } if(!_decoder){ //创建解码器 - if(bytes % 188 == 0 && packet[0] == 0x47){ + if(checkTS(packet, bytes)){ //猜测是ts负载 _decoder = Decoder::createDecoder(Decoder::decoder_ts); }else{ @@ -201,12 +193,6 @@ void RtpProcess::onRtpDecode(const uint8_t *packet, int bytes, uint32_t, int fla auto ret = _decoder->input((uint8_t *)packet,bytes); if(ret != bytes){ WarnL << ret << " != " << bytes << " " << flags; - if(++_rtp_dec_failed_cnt == 10){ - getNextRtpType(); - InfoL << "rtp of ssrc " << printSSRC(_ssrc) << " change to type: " << _rtp_type ; - } - } else{ - _rtp_dec_failed_cnt = 0; } } diff --git a/src/Rtp/RtpProcess.h b/src/Rtp/RtpProcess.h index e8295969..d4395ecf 100644 --- a/src/Rtp/RtpProcess.h +++ b/src/Rtp/RtpProcess.h @@ -49,7 +49,6 @@ public: bool alive(); string get_peer_ip(); uint16_t get_peer_port(); - int totalReaderCount(); void setListener(const std::weak_ptr &listener); protected: @@ -73,9 +72,6 @@ private: Ticker _last_rtp_time; map _stamps; uint32_t _dts = 0; - int _rtp_type_idx = 0; - string _rtp_type; - int _rtp_dec_failed_cnt = 0; Decoder::Ptr _decoder; };