1、根据PS获取时间戳

2、修复rtp代理可能花屏的问题
This commit is contained in:
xiongziliang 2019-12-16 11:00:40 +08:00
parent bfa9a3b678
commit c353c626c1
5 changed files with 53 additions and 17 deletions

View File

@ -33,6 +33,35 @@
namespace mediakit{
/**
* frame
*/
class FrameMerger {
public:
FrameMerger() = default;
virtual ~FrameMerger() = default;
void inputFrame(const Frame::Ptr &frame,const function<void(uint32_t dts,uint32_t pts,const Buffer::Ptr &buffer)> &cb){
if (!_frameCached.empty() && _frameCached.back()->dts() != frame->dts()) {
Frame::Ptr back = _frameCached.back();
Buffer::Ptr merged_frame = back;
if(_frameCached.size() != 1){
string merged;
_frameCached.for_each([&](const Frame::Ptr &frame){
merged.append(frame->data(),frame->size());
});
merged_frame = std::make_shared<BufferString>(std::move(merged));
}
cb(back->dts(),back->pts(),merged_frame);
_frameCached.clear();
}
_frameCached.emplace_back(Frame::getCacheAbleFrame(frame));
}
private:
List<Frame::Ptr> _frameCached;
};
string printSSRC(uint32_t ui32Ssrc) {
char tmp[9] = { 0 };
ui32Ssrc = htonl(ui32Ssrc);
@ -90,6 +119,7 @@ RtpProcess::RtpProcess(uint32_t ssrc) {
});
}
}
_merger = std::make_shared<FrameMerger>();
}
RtpProcess::~RtpProcess() {
@ -101,7 +131,7 @@ RtpProcess::~RtpProcess() {
}
}
bool RtpProcess::inputRtp(const char *data, int data_len,const struct sockaddr *addr) {
bool RtpProcess::inputRtp(const char *data, int data_len,const struct sockaddr *addr,uint32_t *dts_out) {
GET_CONFIG(bool,check_source,RtpProxy::kCheckSource);
//检查源是否合法
if(!_addr){
@ -116,7 +146,11 @@ bool RtpProcess::inputRtp(const char *data, int data_len,const struct sockaddr *
}
_last_rtp_time.resetTime();
return handleOneRtp(0,_track,(unsigned char *)data,data_len);
bool ret = handleOneRtp(0,_track,(unsigned char *)data,data_len);
if(dts_out){
*dts_out = _dts;
}
return ret;
}
void RtpProcess::onRtpSorted(const RtpPacket::Ptr &rtp, int) {
@ -156,6 +190,7 @@ void RtpProcess::onPSDecode(int stream,
int bytes) {
pts /= 90;
dts /= 90;
_dts = dts;
_stamps[codecid].revise(dts,pts,dts,pts,false);
switch (codecid) {
@ -176,8 +211,10 @@ void RtpProcess::onPSDecode(int stream,
if(_save_file_video){
fwrite((uint8_t *)data,bytes, 1, _save_file_video.get());
}
auto frame = std::make_shared<H264FrameNoCacheAble>((char *) data, bytes, dts, pts,4);
_muxer->inputFrame(frame);
auto frame = std::make_shared<H264FrameNoCacheAble>((char *) data, bytes, dts, pts,0);
_merger->inputFrame(frame,[this](uint32_t dts, uint32_t pts, const Buffer::Ptr &buffer) {
_muxer->inputFrame(std::make_shared<H264FrameNoCacheAble>(buffer->data(), buffer->size(), dts, pts,4));
});
break;
}
@ -196,8 +233,10 @@ void RtpProcess::onPSDecode(int stream,
if(_save_file_video){
fwrite((uint8_t *)data,bytes, 1, _save_file_video.get());
}
auto frame = std::make_shared<H265FrameNoCacheAble>((char *) data, bytes, dts, pts, 4);
_muxer->inputFrame(frame);
auto frame = std::make_shared<H265FrameNoCacheAble>((char *) data, bytes, dts, pts, 0);
_merger->inputFrame(frame,[this](uint32_t dts, uint32_t pts, const Buffer::Ptr &buffer) {
_muxer->inputFrame(std::make_shared<H265FrameNoCacheAble>(buffer->data(), buffer->size(), dts, pts, 4));
});
break;
}

View File

@ -39,13 +39,13 @@ using namespace mediakit;
namespace mediakit{
string printSSRC(uint32_t ui32Ssrc);
class FrameMerger;
class RtpProcess : public RtpReceiver , public RtpDecoder , public PSDecoder {
public:
typedef std::shared_ptr<RtpProcess> Ptr;
RtpProcess(uint32_t ssrc);
~RtpProcess();
bool inputRtp(const char *data,int data_len, const struct sockaddr *addr);
bool inputRtp(const char *data,int data_len, const struct sockaddr *addr , uint32_t *dts_out = nullptr);
bool alive();
string get_peer_ip();
uint16_t get_peer_port();
@ -70,8 +70,10 @@ private:
int _codecid_video = 0;
int _codecid_audio = 0;
MultiMediaSourceMuxer::Ptr _muxer;
std::shared_ptr<FrameMerger> _merger;
Ticker _last_rtp_time;
map<int,Stamp> _stamps;
uint32_t _dts = 0;
};
}//namespace mediakit

View File

@ -31,7 +31,7 @@ namespace mediakit{
INSTANCE_IMP(RtpSelector);
bool RtpSelector::inputRtp(const char *data, int data_len,const struct sockaddr *addr) {
bool RtpSelector::inputRtp(const char *data, int data_len,const struct sockaddr *addr,uint32_t *dts_out) {
if(_last_rtp_time.elapsedTime() > 3000){
_last_rtp_time.resetTime();
onManager();
@ -43,7 +43,7 @@ bool RtpSelector::inputRtp(const char *data, int data_len,const struct sockaddr
}
auto process = getProcess(ssrc, true);
if(process){
return process->inputRtp(data,data_len, addr);
return process->inputRtp(data,data_len, addr,dts_out);
}
return false;
}

View File

@ -41,7 +41,7 @@ public:
~RtpSelector();
static RtpSelector &Instance();
bool inputRtp(const char *data,int data_len,const struct sockaddr *addr);
bool inputRtp(const char *data,int data_len,const struct sockaddr *addr ,uint32_t *dts_out = nullptr );
static uint32_t getSSRC(const char *data,int data_len);
RtpProcess::Ptr getProcess(uint32_t ssrc,bool makeNew);
void delProcess(uint32_t ssrc,const RtpProcess *ptr);

View File

@ -71,19 +71,14 @@ static bool loadFile(const char *path){
}
uint32_t timeStamp;
memcpy(&timeStamp, rtp + 4, 4);
timeStamp = ntohl(timeStamp);
timeStamp /= 90;
RtpSelector::Instance().inputRtp(rtp,len, &addr,&timeStamp);
if(timeStamp_last){
auto diff = timeStamp - timeStamp_last;
if(diff > 0){
usleep(diff * 1000);
}
}
timeStamp_last = timeStamp;
RtpSelector::Instance().inputRtp(rtp,len, &addr);
}
fclose(fp);
return true;