refactor: 恢复上次推流时间戳时, 通过duration累加

This commit is contained in:
ydajiang
2024-12-23 20:28:27 +08:00
parent cdc4f84ffe
commit 3ebda750a3
4 changed files with 20 additions and 19 deletions

View File

@@ -24,8 +24,8 @@ func NewStreamEndInfo(source stream.Source) *stream.StreamEndInfo {
for _, track := range tracks {
var timestamp [2]int64
timestamp[0] = track.Dts
timestamp[1] = track.Pts
timestamp[0] = track.Dts + int64(track.FrameDuration)
timestamp[1] = track.Pts + int64(track.FrameDuration)
info.Timestamps[track.Stream.CodecId()] = timestamp
}

View File

@@ -809,23 +809,23 @@ func (s *PublishSource) CorrectTimestamp(packet utils.AVPacket) {
s.timestampModeDecided = true
timestamps := s.streamEndInfo.Timestamps[packet.CodecId()]
if packet.Dts() < timestamps[0] || packet.Pts() < timestamps[1] {
s.accumulateTimestamps = true
log.Sugar.Infof("累加时间戳 上次推流dts: %d, pts: %d", timestamps[0], timestamps[1])
}
s.accumulateTimestamps = true
log.Sugar.Infof("累加时间戳 上次推流dts: %d, pts: %d", timestamps[0], timestamps[1])
}
// 累加时间戳
if s.accumulateTimestamps {
timestamps := s.streamEndInfo.Timestamps[packet.CodecId()]
packet.SetDts(timestamps[0] + packet.Dts())
packet.SetPts(timestamps[1] + packet.Pts())
}
// 更新track最近的时间戳
track := s.originTracks.Find(packet.CodecId())
duration := packet.Duration(packet.Timebase())
// 根据duration来累加时间戳
if s.accumulateTimestamps {
offset := packet.Pts() - packet.Dts()
packet.SetDts(track.Dts + duration)
packet.SetPts(packet.Dts() + offset)
}
track.Dts = packet.Dts()
track.Pts = packet.Pts()
track.FrameDuration = int(duration)
}
func (s *PublishSource) OnDeMuxPacket(packet utils.AVPacket) {

View File

@@ -19,7 +19,7 @@ func init() {
// 如果在此之前陆续有拉流端断开直至sink计数为0则会不再保留该信息。
type StreamEndInfo struct {
ID string
Timestamps map[utils.AVCodecID][2]int64 // 每路track最后的时间戳
Timestamps map[utils.AVCodecID][2]int64 // 每路track结束时间戳
M3U8Writer libhls.M3U8Writer
PlaylistFormat *string
RtspTracks map[byte]uint16

View File

@@ -3,11 +3,12 @@ package stream
import "github.com/lkmio/avformat/utils"
type Track struct {
Stream utils.AVStream
Pts int64 // 最新的PTS
Dts int64 // 最新的DTS
Stream utils.AVStream
Pts int64 // 最新的PTS
Dts int64 // 最新的DTS
FrameDuration int // 单帧时长, timebase和推流一致
}
func NewTrack(stream utils.AVStream, dts, pts int64) *Track {
return &Track{stream, dts, pts}
return &Track{stream, dts, pts, 0}
}