Commit 10884340 by xiongziliang

优化rtp推流相关代码

parent ae45c24f
media-server @ 1603194d
Subproject commit 8d40dad3dbdce171756691d4511aca49fcf2a231 Subproject commit 1603194dafdcecda9036e5741ec8d3e8c9ab1e01
...@@ -137,6 +137,7 @@ void MediaSink::emitAllTrackReady() { ...@@ -137,6 +137,7 @@ void MediaSink::emitAllTrackReady() {
//移除未准备好的Track //移除未准备好的Track
for (auto it = _track_map.begin(); it != _track_map.end();) { for (auto it = _track_map.begin(); it != _track_map.end();) {
if (!it->second->ready()) { if (!it->second->ready()) {
WarnL << "该track长时间未被初始化,已忽略:" << it->second->getCodecName();
it = _track_map.erase(it); it = _track_map.erase(it);
continue; continue;
} }
......
...@@ -155,7 +155,7 @@ RtpCodec::Ptr Factory::getRtpDecoderByTrack(const Track::Ptr &track) { ...@@ -155,7 +155,7 @@ RtpCodec::Ptr Factory::getRtpDecoderByTrack(const Track::Ptr &track) {
case CodecAAC: case CodecAAC:
return std::make_shared<AACRtpDecoder>(track->clone()); return std::make_shared<AACRtpDecoder>(track->clone());
default: default:
WarnL << "暂不支持该CodecId:" << track->getCodecId(); WarnL << "暂不支持该CodecId:" << track->getCodecName();
return nullptr; return nullptr;
} }
} }
...@@ -212,7 +212,7 @@ RtmpCodec::Ptr Factory::getRtmpCodecByTrack(const Track::Ptr &track) { ...@@ -212,7 +212,7 @@ RtmpCodec::Ptr Factory::getRtmpCodecByTrack(const Track::Ptr &track) {
case CodecAAC: case CodecAAC:
return std::make_shared<AACRtmpEncoder>(track); return std::make_shared<AACRtmpEncoder>(track);
default: default:
WarnL << "暂不支持该CodecId:" << track->getCodecId(); WarnL << "暂不支持该CodecId:" << track->getCodecName();
return nullptr; return nullptr;
} }
} }
......
...@@ -38,5 +38,16 @@ Frame::Ptr Frame::getCacheAbleFrame(const Frame::Ptr &frame){ ...@@ -38,5 +38,16 @@ Frame::Ptr Frame::getCacheAbleFrame(const Frame::Ptr &frame){
return std::make_shared<FrameCacheAble>(frame); return std::make_shared<FrameCacheAble>(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 }//namespace mediakit
...@@ -72,6 +72,12 @@ public: ...@@ -72,6 +72,12 @@ public:
* 获取编解码器类型 * 获取编解码器类型
*/ */
virtual CodecId getCodecId() const = 0; virtual CodecId getCodecId() const = 0;
/**
* 获取编码器名称
* @return 编码器名称
*/
char *getCodecName();
}; };
/** /**
......
...@@ -258,7 +258,7 @@ void MP4Muxer::addTrack(const Track::Ptr &track) { ...@@ -258,7 +258,7 @@ void MP4Muxer::addTrack(const Track::Ptr &track) {
} }
break; break;
default: default:
WarnL << "MP4录制不支持该编码格式:" << track->getCodecId(); WarnL << "MP4录制不支持该编码格式:" << track->getCodecName();
break; break;
} }
} }
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
*/ */
#if defined(ENABLE_RTPPROXY) #if defined(ENABLE_RTPPROXY)
#include <assert.h>
#include "Util/logger.h" #include "Util/logger.h"
#include "RtpDecoder.h" #include "RtpDecoder.h"
#include "rtp-payload.h" #include "rtp-payload.h"
...@@ -44,13 +43,7 @@ RtpDecoder::~RtpDecoder() { ...@@ -44,13 +43,7 @@ RtpDecoder::~RtpDecoder() {
} }
} }
void RtpDecoder::decodeRtp(const void *data, int bytes,const string &type_name) { void RtpDecoder::decodeRtp(const void *data, int bytes) {
if(_rtp_type != type_name && _rtp_decoder){
//rtp类型发生变化,切换之
rtp_payload_decode_destroy(_rtp_decoder);
_rtp_decoder = nullptr;
}
if(!_rtp_decoder){ if(!_rtp_decoder){
static rtp_payload_t s_func= { static rtp_payload_t s_func= {
[](void* param, int bytes){ [](void* param, int bytes){
...@@ -69,11 +62,9 @@ void RtpDecoder::decodeRtp(const void *data, int bytes,const string &type_name) ...@@ -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]; uint8_t rtp_type = 0x7F & ((uint8_t *) data)[1];
InfoL << "rtp type:" << (int) rtp_type; 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) { if (!_rtp_decoder) {
WarnL << "unsupported rtp type:" << (int) rtp_type << ",size:" << bytes << ",hexdump" << hexdump(data, bytes > 16 ? 16 : bytes); WarnL << "unsupported rtp type:" << (int) rtp_type << ",size:" << bytes << ",hexdump" << hexdump(data, bytes > 16 ? 16 : bytes);
}else{
_rtp_type = type_name;
} }
} }
......
...@@ -38,12 +38,11 @@ public: ...@@ -38,12 +38,11 @@ public:
RtpDecoder(); RtpDecoder();
virtual ~RtpDecoder(); virtual ~RtpDecoder();
protected: 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; virtual void onRtpDecode(const uint8_t *packet, int bytes, uint32_t timestamp, int flags) = 0;
private: private:
void *_rtp_decoder = nullptr; void *_rtp_decoder = nullptr;
BufferRaw::Ptr _buffer; BufferRaw::Ptr _buffer;
string _rtp_type;
}; };
}//namespace mediakit }//namespace mediakit
......
...@@ -33,8 +33,6 @@ ...@@ -33,8 +33,6 @@
namespace mediakit{ namespace mediakit{
static const vector<string> kRtpTypes = {"MP2P","MP4V-ES"};
/** /**
* 合并一些时间戳相同的frame * 合并一些时间戳相同的frame
*/ */
...@@ -85,7 +83,6 @@ RtpProcess::RtpProcess(uint32_t ssrc) { ...@@ -85,7 +83,6 @@ RtpProcess::RtpProcess(uint32_t ssrc) {
_track->_samplerate = 90000; _track->_samplerate = 90000;
_track->_type = TrackVideo; _track->_type = TrackVideo;
_track->_ssrc = _ssrc; _track->_ssrc = _ssrc;
getNextRtpType();
DebugL << printSSRC(_ssrc); DebugL << printSSRC(_ssrc);
GET_CONFIG(bool,toRtxp,General::kPublishToRtxp); GET_CONFIG(bool,toRtxp,General::kPublishToRtxp);
...@@ -155,12 +152,9 @@ bool RtpProcess::inputRtp(const char *data, int data_len,const struct sockaddr * ...@@ -155,12 +152,9 @@ bool RtpProcess::inputRtp(const char *data, int data_len,const struct sockaddr *
return ret; return ret;
} }
void RtpProcess::getNextRtpType(){ //判断是否为ts负载
_rtp_type = kRtpTypes[_rtp_type_idx++]; static inline bool checkTS(const uint8_t *packet, int bytes){
_rtp_dec_failed_cnt = 0; return bytes % 188 == 0 && packet[0] == 0x47;
if(_rtp_type_idx == kRtpTypes.size()){
_rtp_type_idx = 0;
}
} }
void RtpProcess::onRtpSorted(const RtpPacket::Ptr &rtp, int) { void RtpProcess::onRtpSorted(const RtpPacket::Ptr &rtp, int) {
...@@ -168,25 +162,23 @@ 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"; WarnL << rtp->sequence << " != " << _sequence << "+1";
} }
_sequence = rtp->sequence; _sequence = rtp->sequence;
if(_save_file_rtp){ if(_save_file_rtp){
uint16_t size = rtp->size() - 4; uint16_t size = rtp->size() - 4;
size = htons(size); size = htons(size);
fwrite((uint8_t *) &size, 2, 1, _save_file_rtp.get()); fwrite((uint8_t *) &size, 2, 1, _save_file_rtp.get());
fwrite((uint8_t *) rtp->data() + 4, rtp->size() - 4, 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);
decodeRtp(rtp->data() + 4 ,rtp->size() - 4,_rtp_type);
} }
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){ if(_save_file_ps){
fwrite((uint8_t *)packet,bytes, 1, _save_file_ps.get()); fwrite((uint8_t *)packet,bytes, 1, _save_file_ps.get());
} }
if(!_decoder){ if(!_decoder){
//创建解码器 //创建解码器
if(bytes % 188 == 0 && packet[0] == 0x47){ if(checkTS(packet, bytes)){
//猜测是ts负载 //猜测是ts负载
_decoder = Decoder::createDecoder(Decoder::decoder_ts); _decoder = Decoder::createDecoder(Decoder::decoder_ts);
}else{ }else{
...@@ -201,12 +193,6 @@ void RtpProcess::onRtpDecode(const uint8_t *packet, int bytes, uint32_t, int fla ...@@ -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); auto ret = _decoder->input((uint8_t *)packet,bytes);
if(ret != bytes){ if(ret != bytes){
WarnL << ret << " != " << bytes << " " << flags; 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;
} }
} }
......
...@@ -49,7 +49,6 @@ public: ...@@ -49,7 +49,6 @@ public:
bool alive(); bool alive();
string get_peer_ip(); string get_peer_ip();
uint16_t get_peer_port(); uint16_t get_peer_port();
int totalReaderCount(); int totalReaderCount();
void setListener(const std::weak_ptr<MediaSourceEvent> &listener); void setListener(const std::weak_ptr<MediaSourceEvent> &listener);
protected: protected:
...@@ -73,9 +72,6 @@ private: ...@@ -73,9 +72,6 @@ private:
Ticker _last_rtp_time; Ticker _last_rtp_time;
map<int,Stamp> _stamps; map<int,Stamp> _stamps;
uint32_t _dts = 0; uint32_t _dts = 0;
int _rtp_type_idx = 0;
string _rtp_type;
int _rtp_dec_failed_cnt = 0;
Decoder::Ptr _decoder; Decoder::Ptr _decoder;
}; };
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论