优化rtp推流相关代码

This commit is contained in:
xiongziliang 2020-03-08 21:19:20 +08:00
parent ae45c24fa3
commit 10884340b0
10 changed files with 31 additions and 41 deletions

@ -1 +1 @@
Subproject commit 8d40dad3dbdce171756691d4511aca49fcf2a231
Subproject commit 1603194dafdcecda9036e5741ec8d3e8c9ab1e01

View File

@ -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;
}

View File

@ -155,7 +155,7 @@ RtpCodec::Ptr Factory::getRtpDecoderByTrack(const Track::Ptr &track) {
case CodecAAC:
return std::make_shared<AACRtpDecoder>(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<AACRtmpEncoder>(track);
default:
WarnL << "暂不支持该CodecId:" << track->getCodecId();
WarnL << "暂不支持该CodecId:" << track->getCodecName();
return nullptr;
}
}

View File

@ -38,5 +38,16 @@ Frame::Ptr Frame::getCacheAbleFrame(const Frame::Ptr &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

View File

@ -72,6 +72,12 @@ public:
*
*/
virtual CodecId getCodecId() const = 0;
/**
*
* @return
*/
char *getCodecName();
};
/**

View File

@ -258,7 +258,7 @@ void MP4Muxer::addTrack(const Track::Ptr &track) {
}
break;
default:
WarnL << "MP4录制不支持该编码格式:" << track->getCodecId();
WarnL << "MP4录制不支持该编码格式:" << track->getCodecName();
break;
}
}

View File

@ -25,7 +25,6 @@
*/
#if defined(ENABLE_RTPPROXY)
#include <assert.h>
#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;
}
}

View File

@ -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

View File

@ -33,8 +33,6 @@
namespace mediakit{
static const vector<string> 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;
}
}

View File

@ -49,7 +49,6 @@ public:
bool alive();
string get_peer_ip();
uint16_t get_peer_port();
int totalReaderCount();
void setListener(const std::weak_ptr<MediaSourceEvent> &listener);
protected:
@ -73,9 +72,6 @@ private:
Ticker _last_rtp_time;
map<int,Stamp> _stamps;
uint32_t _dts = 0;
int _rtp_type_idx = 0;
string _rtp_type;
int _rtp_dec_failed_cnt = 0;
Decoder::Ptr _decoder;
};