Browse Source

hls muxer: simplify code

pull/909/head
aler9 4 years ago
parent
commit
bdf80a0dd3
  1. 45
      internal/hls/muxer_ts_generator.go
  2. 12
      internal/hls/muxer_ts_segment.go
  3. 51
      internal/hls/muxer_ts_writer.go

45
internal/hls/muxer_ts_generator.go

@ -1,11 +1,13 @@
package hls package hls
import ( import (
"context"
"time" "time"
"github.com/aler9/gortsplib" "github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/aac" "github.com/aler9/gortsplib/pkg/aac"
"github.com/aler9/gortsplib/pkg/h264" "github.com/aler9/gortsplib/pkg/h264"
"github.com/asticode/go-astits"
) )
const ( const (
@ -25,6 +27,12 @@ func idrPresent(nalus [][]byte) bool {
return false return false
} }
type writerFunc func(p []byte) (int, error)
func (f writerFunc) Write(p []byte) (int, error) {
return f(p)
}
type muxerTSGenerator struct { type muxerTSGenerator struct {
hlsSegmentCount int hlsSegmentCount int
hlsSegmentDuration time.Duration hlsSegmentDuration time.Duration
@ -33,7 +41,7 @@ type muxerTSGenerator struct {
audioTrack *gortsplib.TrackAAC audioTrack *gortsplib.TrackAAC
streamPlaylist *muxerStreamPlaylist streamPlaylist *muxerStreamPlaylist
writer *muxerTSWriter writer *astits.Muxer
currentSegment *muxerTSSegment currentSegment *muxerTSSegment
videoDTSEst *h264.DTSEstimator videoDTSEst *h264.DTSEstimator
startPCR time.Time startPCR time.Time
@ -55,7 +63,32 @@ func newMuxerTSGenerator(
videoTrack: videoTrack, videoTrack: videoTrack,
audioTrack: audioTrack, audioTrack: audioTrack,
streamPlaylist: streamPlaylist, streamPlaylist: streamPlaylist,
writer: newMuxerTSWriter(videoTrack, audioTrack), }
m.writer = astits.NewMuxer(
context.Background(),
writerFunc(func(p []byte) (int, error) {
return m.currentSegment.write(p)
}))
if videoTrack != nil {
m.writer.AddElementaryStream(astits.PMTElementaryStream{
ElementaryPID: 256,
StreamType: astits.StreamTypeH264Video,
})
}
if audioTrack != nil {
m.writer.AddElementaryStream(astits.PMTElementaryStream{
ElementaryPID: 257,
StreamType: astits.StreamTypeAACAudio,
})
}
if videoTrack != nil {
m.writer.SetPCRPID(256)
} else {
m.writer.SetPCRPID(257)
} }
return m return m
@ -71,7 +104,7 @@ func (m *muxerTSGenerator) writeH264(pts time.Duration, nalus [][]byte) error {
} }
// create first segment // create first segment
m.currentSegment = newMuxerTSSegment(m.hlsSegmentMaxSize, m.videoTrack, m.writer) m.currentSegment = newMuxerTSSegment(m.hlsSegmentMaxSize, m.videoTrack, m.writer.WriteData)
m.startPCR = time.Now() m.startPCR = time.Now()
m.startPTS = pts m.startPTS = pts
m.videoDTSEst = h264.NewDTSEstimator() m.videoDTSEst = h264.NewDTSEstimator()
@ -85,7 +118,7 @@ func (m *muxerTSGenerator) writeH264(pts time.Duration, nalus [][]byte) error {
(pts-*m.currentSegment.startPTS) >= m.hlsSegmentDuration { (pts-*m.currentSegment.startPTS) >= m.hlsSegmentDuration {
m.currentSegment.endPTS = pts m.currentSegment.endPTS = pts
m.streamPlaylist.pushSegment(m.currentSegment) m.streamPlaylist.pushSegment(m.currentSegment)
m.currentSegment = newMuxerTSSegment(m.hlsSegmentMaxSize, m.videoTrack, m.writer) m.currentSegment = newMuxerTSSegment(m.hlsSegmentMaxSize, m.videoTrack, m.writer.WriteData)
} }
} }
@ -136,7 +169,7 @@ func (m *muxerTSGenerator) writeAAC(pts time.Duration, aus [][]byte) error {
if m.videoTrack == nil { if m.videoTrack == nil {
if m.currentSegment == nil { if m.currentSegment == nil {
// create first segment // create first segment
m.currentSegment = newMuxerTSSegment(m.hlsSegmentMaxSize, m.videoTrack, m.writer) m.currentSegment = newMuxerTSSegment(m.hlsSegmentMaxSize, m.videoTrack, m.writer.WriteData)
m.startPCR = time.Now() m.startPCR = time.Now()
m.startPTS = pts m.startPTS = pts
pts = pcrOffset pts = pcrOffset
@ -149,7 +182,7 @@ func (m *muxerTSGenerator) writeAAC(pts time.Duration, aus [][]byte) error {
(pts-*m.currentSegment.startPTS) >= m.hlsSegmentDuration { (pts-*m.currentSegment.startPTS) >= m.hlsSegmentDuration {
m.currentSegment.endPTS = pts m.currentSegment.endPTS = pts
m.streamPlaylist.pushSegment(m.currentSegment) m.streamPlaylist.pushSegment(m.currentSegment)
m.currentSegment = newMuxerTSSegment(m.hlsSegmentMaxSize, m.videoTrack, m.writer) m.currentSegment = newMuxerTSSegment(m.hlsSegmentMaxSize, m.videoTrack, m.writer.WriteData)
} }
} }
} else { } else {

12
internal/hls/muxer_ts_segment.go

@ -14,7 +14,7 @@ import (
type muxerTSSegment struct { type muxerTSSegment struct {
hlsSegmentMaxSize uint64 hlsSegmentMaxSize uint64
videoTrack *gortsplib.TrackH264 videoTrack *gortsplib.TrackH264
writer *muxerTSWriter writeData func(*astits.MuxerData) (int, error)
startTime time.Time startTime time.Time
name string name string
@ -28,14 +28,14 @@ type muxerTSSegment struct {
func newMuxerTSSegment( func newMuxerTSSegment(
hlsSegmentMaxSize uint64, hlsSegmentMaxSize uint64,
videoTrack *gortsplib.TrackH264, videoTrack *gortsplib.TrackH264,
writer *muxerTSWriter, writeData func(*astits.MuxerData) (int, error),
) *muxerTSSegment { ) *muxerTSSegment {
now := time.Now() now := time.Now()
t := &muxerTSSegment{ t := &muxerTSSegment{
hlsSegmentMaxSize: hlsSegmentMaxSize, hlsSegmentMaxSize: hlsSegmentMaxSize,
videoTrack: videoTrack, videoTrack: videoTrack,
writer: writer, writeData: writeData,
startTime: now, startTime: now,
name: strconv.FormatInt(now.Unix(), 10), name: strconv.FormatInt(now.Unix(), 10),
} }
@ -45,8 +45,6 @@ func newMuxerTSSegment(
// - AdaptationField != nil // - AdaptationField != nil
// - RandomAccessIndicator = true // - RandomAccessIndicator = true
writer.currentSegment = t
return t return t
} }
@ -103,7 +101,7 @@ func (t *muxerTSSegment) writeH264(
oh.PTS = &astits.ClockReference{Base: int64(pts.Seconds() * 90000)} oh.PTS = &astits.ClockReference{Base: int64(pts.Seconds() * 90000)}
} }
_, err := t.writer.WriteData(&astits.MuxerData{ _, err := t.writeData(&astits.MuxerData{
PID: 256, PID: 256,
AdaptationField: af, AdaptationField: af,
PES: &astits.PESData{ PES: &astits.PESData{
@ -147,7 +145,7 @@ func (t *muxerTSSegment) writeAAC(
} }
} }
_, err := t.writer.WriteData(&astits.MuxerData{ _, err := t.writeData(&astits.MuxerData{
PID: 257, PID: 257,
AdaptationField: af, AdaptationField: af,
PES: &astits.PESData{ PES: &astits.PESData{

51
internal/hls/muxer_ts_writer.go

@ -1,51 +0,0 @@
package hls
import (
"context"
"github.com/aler9/gortsplib"
"github.com/asticode/go-astits"
)
type muxerTSWriter struct {
innerMuxer *astits.Muxer
currentSegment *muxerTSSegment
}
func newMuxerTSWriter(
videoTrack *gortsplib.TrackH264,
audioTrack *gortsplib.TrackAAC) *muxerTSWriter {
w := &muxerTSWriter{}
w.innerMuxer = astits.NewMuxer(context.Background(), w)
if videoTrack != nil {
w.innerMuxer.AddElementaryStream(astits.PMTElementaryStream{
ElementaryPID: 256,
StreamType: astits.StreamTypeH264Video,
})
}
if audioTrack != nil {
w.innerMuxer.AddElementaryStream(astits.PMTElementaryStream{
ElementaryPID: 257,
StreamType: astits.StreamTypeAACAudio,
})
}
if videoTrack != nil {
w.innerMuxer.SetPCRPID(256)
} else {
w.innerMuxer.SetPCRPID(257)
}
return w
}
func (mt *muxerTSWriter) Write(p []byte) (int, error) {
return mt.currentSegment.write(p)
}
func (mt *muxerTSWriter) WriteData(d *astits.MuxerData) (int, error) {
return mt.innerMuxer.WriteData(d)
}
Loading…
Cancel
Save