Skip to content

Commit d2bd4b2

Browse files
committed
fix: when rtmp sequence header change,mp4 record two diffent sequence header into the same mp4 file.
1 parent 6693676 commit d2bd4b2

File tree

3 files changed

+297
-39
lines changed

3 files changed

+297
-39
lines changed

plugin/mp4/pkg/record.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,18 +296,18 @@ func (r *Recorder) Run() (err error) {
296296

297297
if vt == nil {
298298
vt = sub.VideoReader.Track
299-
switch vt.ICodecCtx.GetBase().(type) {
299+
switch video.ICodecCtx.GetBase().(type) {
300300
case *codec.H264Ctx:
301301
track := r.muxer.AddTrack(box.MP4_CODEC_H264)
302302
videoTrack = track
303-
track.ICodecCtx = vt.ICodecCtx
303+
track.ICodecCtx = video.ICodecCtx
304304
case *codec.H265Ctx:
305305
track := r.muxer.AddTrack(box.MP4_CODEC_H265)
306306
videoTrack = track
307-
track.ICodecCtx = vt.ICodecCtx
307+
track.ICodecCtx = video.ICodecCtx
308308
}
309309
}
310-
ctx := vt.ICodecCtx.(pkg.IVideoCodecCtx)
310+
ctx := video.ICodecCtx.(pkg.IVideoCodecCtx)
311311
if videoTrackCtx, ok := videoTrack.ICodecCtx.(pkg.IVideoCodecCtx); ok && videoTrackCtx != ctx {
312312
width, height := uint32(ctx.Width()), uint32(ctx.Height())
313313
oldWidth, oldHeight := uint32(videoTrackCtx.Width()), uint32(videoTrackCtx.Height())
@@ -322,6 +322,17 @@ func (r *Recorder) Run() (err error) {
322322
at, vt = nil, nil
323323
if vr := sub.VideoReader; vr != nil {
324324
vr.ResetAbsTime()
325+
vt = vr.Track
326+
switch video.ICodecCtx.GetBase().(type) {
327+
case *codec.H264Ctx:
328+
track := r.muxer.AddTrack(box.MP4_CODEC_H264)
329+
videoTrack = track
330+
track.ICodecCtx = video.ICodecCtx
331+
case *codec.H265Ctx:
332+
track := r.muxer.AddTrack(box.MP4_CODEC_H265)
333+
videoTrack = track
334+
track.ICodecCtx = video.ICodecCtx
335+
}
325336
}
326337
if ar := sub.AudioReader; ar != nil {
327338
ar.ResetAbsTime()

plugin/mp4/pkg/track.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,14 @@ func (track *Track) makeElstBox() *EditListBox {
7373
// elst.entrys.entrys[0].mediaRateFraction = 0
7474
// }
7575

76-
//简单起见,mediaTime先固定为0,即不延迟播放
76+
//使用第一个sample的时间戳作为MediaTime,确保时间轴对齐
77+
firstTimestamp := track.Samplelist[0].Timestamp
78+
firstCTS := track.Samplelist[0].CTS
79+
mediaTime := int64(firstTimestamp) + int64(firstCTS)
80+
7781
entrys[entryCount-1].SegmentDuration = uint64(track.Duration)
78-
entrys[entryCount-1].MediaTime = 0
82+
// MediaTime应该是第一个sample的PTS (DTS + CTS)
83+
entrys[entryCount-1].MediaTime = mediaTime
7984
entrys[entryCount-1].MediaRateInteger = 0x0001
8085
entrys[entryCount-1].MediaRateFraction = 0
8186

0 commit comments

Comments
 (0)