mediamtx/internal/hls/muxer.go

92 lines
2.0 KiB
Go
Raw Normal View History

2021-07-24 16:31:54 +00:00
package hls
import (
"io"
"time"
"github.com/aler9/gortsplib"
)
// Muxer is a HLS muxer.
type Muxer struct {
primaryPlaylist *muxerPrimaryPlaylist
streamPlaylist *muxerStreamPlaylist
tsGenerator *muxerTSGenerator
2021-07-24 16:31:54 +00:00
}
// NewMuxer allocates a Muxer.
func NewMuxer(
hlsSegmentCount int,
hlsSegmentDuration time.Duration,
videoTrack *gortsplib.Track,
audioTrack *gortsplib.Track) (*Muxer, error) {
2021-08-25 17:51:59 +00:00
var h264Conf *gortsplib.TrackConfigH264
if videoTrack != nil {
var err error
2021-08-25 17:51:59 +00:00
h264Conf, err = videoTrack.ExtractConfigH264()
if err != nil {
return nil, err
}
}
2021-08-25 17:51:59 +00:00
var aacConf *gortsplib.TrackConfigAAC
2021-07-24 16:31:54 +00:00
if audioTrack != nil {
2021-08-25 17:51:59 +00:00
var err error
aacConf, err = audioTrack.ExtractConfigAAC()
2021-07-24 16:31:54 +00:00
if err != nil {
return nil, err
}
}
primaryPlaylist := newMuxerPrimaryPlaylist(videoTrack, audioTrack, h264Conf)
streamPlaylist := newMuxerStreamPlaylist(hlsSegmentCount)
tsGenerator := newMuxerTSGenerator(
hlsSegmentCount,
hlsSegmentDuration,
videoTrack,
audioTrack,
h264Conf,
aacConf,
streamPlaylist)
2021-07-24 16:31:54 +00:00
m := &Muxer{
primaryPlaylist: primaryPlaylist,
streamPlaylist: streamPlaylist,
tsGenerator: tsGenerator,
2021-07-24 16:31:54 +00:00
}
return m, nil
}
// Close closes a Muxer.
func (m *Muxer) Close() {
m.streamPlaylist.close()
2021-07-24 16:31:54 +00:00
}
// WriteH264 writes H264 NALUs, grouped by PTS, into the muxer.
func (m *Muxer) WriteH264(pts time.Duration, nalus [][]byte) error {
return m.tsGenerator.writeH264(pts, nalus)
2021-07-24 16:31:54 +00:00
}
// WriteAAC writes AAC AUs, grouped by PTS, into the muxer.
func (m *Muxer) WriteAAC(pts time.Duration, aus [][]byte) error {
return m.tsGenerator.writeAAC(pts, aus)
2021-07-24 16:31:54 +00:00
}
// PrimaryPlaylist returns a reader to read the primary playlist.
2021-08-16 16:07:10 +00:00
func (m *Muxer) PrimaryPlaylist() io.Reader {
return m.primaryPlaylist.reader()
2021-08-16 16:07:10 +00:00
}
2021-07-24 16:31:54 +00:00
2021-08-16 16:07:10 +00:00
// StreamPlaylist returns a reader to read the stream playlist.
func (m *Muxer) StreamPlaylist() io.Reader {
return m.streamPlaylist.reader()
2021-07-24 16:31:54 +00:00
}
// Segment returns a reader to read a segment listed in the stream playlist.
func (m *Muxer) Segment(fname string) io.Reader {
return m.streamPlaylist.segment(fname)
2021-07-24 16:31:54 +00:00
}