mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2025-02-23 13:39:13 +08:00
尝试修复时间戳回环或乱序导致的问题
This commit is contained in:
parent
36803ccedc
commit
6a9eb8d54b
@ -107,20 +107,38 @@ void MP4Muxer::onTrackFrame(const Frame::Ptr &frame) {
|
||||
if(!track_info.start_dts){
|
||||
track_info.start_dts = frame->dts();
|
||||
}
|
||||
if(!track_info.start_pts){
|
||||
track_info.start_pts = frame->pts();
|
||||
}
|
||||
|
||||
//相对时间戳
|
||||
int dts_inc = frame->dts() - track_info.start_dts;
|
||||
int pts_inc = frame->pts() - track_info.start_pts;
|
||||
int64_t dts_inc = frame->dts() - track_info.start_dts;
|
||||
//pts和dts的差值
|
||||
int pts_dts_diff = frame->pts() - frame->dts();
|
||||
if(pts_dts_diff > 200 || pts_dts_diff < -200){
|
||||
//如果差值大于200毫秒,则认为由于回环导致时间戳错乱了
|
||||
pts_dts_diff = 0;
|
||||
}
|
||||
|
||||
if(dts_inc < track_info.dts_inc){
|
||||
//本次相对时间戳竟然小于上次?
|
||||
if(dts_inc < 0){
|
||||
//时间戳回环,保证下次相对时间戳与本次相对合理增长
|
||||
track_info.start_dts = frame->dts() - track_info.dts_inc;
|
||||
//本次时间戳强制等于上次时间戳+10
|
||||
dts_inc = track_info.dts_inc + 10;
|
||||
}else{
|
||||
//时间戳变小了?,那么取上次时间戳+10
|
||||
dts_inc = track_info.dts_inc + 10;
|
||||
}
|
||||
}
|
||||
|
||||
//保留上次相对时间戳
|
||||
track_info.dts_inc = dts_inc;
|
||||
|
||||
mov_writer_write_l(_mov_writter.get(),
|
||||
track_info.track_id,
|
||||
frame->data() + frame->prefixSize(),
|
||||
frame->size() - frame->prefixSize(),
|
||||
pts_inc >= 0 ? pts_inc : 0,
|
||||
dts_inc >= 0 ? dts_inc : 0,
|
||||
pts_dts_diff + dts_inc,
|
||||
dts_inc,
|
||||
frame->keyFrame() ? MOV_AV_FLAG_KEYFREAME : 0,
|
||||
with_nalu_size);
|
||||
}
|
||||
|
@ -76,8 +76,8 @@ protected:
|
||||
private:
|
||||
struct track_info{
|
||||
int track_id = -1;
|
||||
uint32_t start_dts = 0;
|
||||
uint32_t start_pts = 0;
|
||||
int64_t start_dts = 0;
|
||||
int64_t dts_inc = 0;
|
||||
};
|
||||
map<CodecId,track_info> _codec_to_trackid;
|
||||
bool _started = false;
|
||||
|
Loading…
Reference in New Issue
Block a user