mediamtx/internal/hls/muxer.go
Alessandro Ros c778c049ce
switch to gortsplib v2 (#1301)
Fixes #1103

gortsplib/v2 supports multiple formats inside a single track (media). This allows to apply the resizing algorithm to single formats inside medias.

For instance, if a media contains a a proprietary format and an H264 format, and the latter has oversized packets, they can now be resized.
2022-12-13 20:54:17 +01:00

97 lines
2.0 KiB
Go

// Package hls contains a HLS muxer and client implementation.
package hls
import (
"io"
"time"
"github.com/aler9/gortsplib/v2/pkg/format"
)
// MuxerFileResponse is a response of the Muxer's File() func.
type MuxerFileResponse struct {
Status int
Header map[string]string
Body io.Reader
}
// Muxer is a HLS muxer.
type Muxer struct {
primaryPlaylist *muxerPrimaryPlaylist
variant muxerVariant
}
// NewMuxer allocates a Muxer.
func NewMuxer(
variant MuxerVariant,
segmentCount int,
segmentDuration time.Duration,
partDuration time.Duration,
segmentMaxSize uint64,
videoTrack *format.H264,
audioTrack *format.MPEG4Audio,
) (*Muxer, error) {
m := &Muxer{}
switch variant {
case MuxerVariantMPEGTS:
m.variant = newMuxerVariantMPEGTS(
segmentCount,
segmentDuration,
segmentMaxSize,
videoTrack,
audioTrack,
)
case MuxerVariantFMP4:
m.variant = newMuxerVariantFMP4(
false,
segmentCount,
segmentDuration,
partDuration,
segmentMaxSize,
videoTrack,
audioTrack,
)
default: // MuxerVariantLowLatency
m.variant = newMuxerVariantFMP4(
true,
segmentCount,
segmentDuration,
partDuration,
segmentMaxSize,
videoTrack,
audioTrack,
)
}
m.primaryPlaylist = newMuxerPrimaryPlaylist(variant != MuxerVariantMPEGTS, videoTrack, audioTrack)
return m, nil
}
// Close closes a Muxer.
func (m *Muxer) Close() {
m.variant.close()
}
// WriteH264 writes H264 NALUs, grouped by timestamp.
func (m *Muxer) WriteH264(ntp time.Time, pts time.Duration, nalus [][]byte) error {
return m.variant.writeH264(ntp, pts, nalus)
}
// WriteAAC writes AAC AUs, grouped by timestamp.
func (m *Muxer) WriteAAC(ntp time.Time, pts time.Duration, au []byte) error {
return m.variant.writeAAC(ntp, pts, au)
}
// File returns a file reader.
func (m *Muxer) File(name string, msn string, part string, skip string) *MuxerFileResponse {
if name == "index.m3u8" {
return m.primaryPlaylist.file()
}
return m.variant.file(name, msn, part, skip)
}