Skip to content

Commit

Permalink
muxer: prepend prefix to segments
Browse files Browse the repository at this point in the history
This is needed to prevent usage of cached segments from previous muxing
sessions
  • Loading branch information
aler9 committed Aug 6, 2023
1 parent fe99dba commit 3df19dd
Show file tree
Hide file tree
Showing 9 changed files with 374 additions and 284 deletions.
31 changes: 27 additions & 4 deletions muxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package gohlslib

import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"net/http"
"time"
Expand All @@ -14,6 +16,18 @@ import (
"github.com/bluenviron/gohlslib/pkg/storage"
)

// a prefix is needed to prevent usage of cached segments
// from previous muxing sessions.
func generatePrefix() (string, error) {
var buf [6]byte
_, err := rand.Read(buf[:])
if err != nil {
return "", err
}

return hex.EncodeToString(buf[:]), nil
}

// MuxerVariant is a muxer variant.
type MuxerVariant int

Expand Down Expand Up @@ -68,6 +82,7 @@ type Muxer struct {
// private
//

prefix string
storageFactory storage.Factory
server *muxerServer
segmenter muxerSegmenter
Expand Down Expand Up @@ -120,18 +135,24 @@ func (m *Muxer) Start() error {
}
}

var err error
m.prefix, err = generatePrefix()
if err != nil {
return err
}

if m.Directory != "" {
m.storageFactory = storage.NewFactoryDisk(m.Directory)
} else {
m.storageFactory = storage.NewFactoryRAM()
}

var err error
m.server, err = newMuxerServer(
m.Variant,
m.SegmentCount,
m.VideoTrack,
m.AudioTrack,
m.prefix,
m.storageFactory,
)
if err != nil {
Expand All @@ -144,8 +165,9 @@ func (m *Muxer) Start() error {
m.SegmentMaxSize,
m.VideoTrack,
m.AudioTrack,
m.prefix,
m.storageFactory,
m.server.onSegmentFinalized,
m.server.publishSegment,
)
} else {
m.segmenter = newMuxerSegmenterFMP4(
Expand All @@ -155,9 +177,10 @@ func (m *Muxer) Start() error {
m.SegmentMaxSize,
m.VideoTrack,
m.AudioTrack,
m.prefix,
m.storageFactory,
m.server.onSegmentFinalized,
m.server.onPartFinalized,
m.server.publishSegment,
m.server.publishPart,
)
}

Expand Down
11 changes: 7 additions & 4 deletions muxer_part.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/bluenviron/mediacommon/pkg/formats/fmp4"
)

func fmp4PartName(id uint64) string {
return "part" + strconv.FormatUint(id, 10)
func partName(prefix string, id uint64) string {
return prefix + "_part" + strconv.FormatUint(id, 10) + ".mp4"
}

type muxerPart struct {
Expand All @@ -21,6 +21,7 @@ type muxerPart struct {
id uint64
storage storage.Part

name string
isIndependent bool
videoSamples []*fmp4.PartSample
audioSamples []*fmp4.PartSample
Expand All @@ -36,6 +37,7 @@ func newMuxerPart(
videoTrack *Track,
audioTrack *Track,
audioTrackTimeScale uint32,
prefix string,
id uint64,
storage storage.Part,
) *muxerPart {
Expand All @@ -46,6 +48,7 @@ func newMuxerPart(
audioTrackTimeScale: audioTrackTimeScale,
id: id,
storage: storage,
name: partName(prefix, id),
}

if videoTrack == nil {
Expand All @@ -55,8 +58,8 @@ func newMuxerPart(
return p
}

func (p *muxerPart) name() string {
return fmp4PartName(p.id)
func (p *muxerPart) getName() string {
return p.name
}

func (p *muxerPart) reader() (io.ReadCloser, error) {
Expand Down
8 changes: 8 additions & 0 deletions muxer_segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ package gohlslib
import (
"fmt"
"io"
"strconv"
"time"
)

func segmentName(prefix string, id uint64, mp4 bool) string {
if mp4 {
return prefix + "_seg" + strconv.FormatUint(id, 10) + ".mp4"
}
return prefix + "_seg" + strconv.FormatUint(id, 10) + ".ts"
}

type muxerSegment interface {
close()
getName() string
Expand Down
23 changes: 14 additions & 9 deletions muxer_segment_fmp4.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gohlslib
import (
"fmt"
"io"
"strconv"
"time"

"github.com/bluenviron/gohlslib/pkg/storage"
Expand All @@ -18,8 +17,9 @@ type muxerSegmentFMP4 struct {
videoTrack *Track
audioTrack *Track
audioTrackTimeScale uint32
prefix string
genPartID func() uint64
onPartFinalized func(*muxerPart)
publishPart func(*muxerPart)

name string
storage storage.File
Expand All @@ -38,9 +38,10 @@ func newMuxerSegmentFMP4(
videoTrack *Track,
audioTrack *Track,
audioTrackTimeScale uint32,
prefix string,
factory storage.Factory,
genPartID func() uint64,
onPartFinalized func(*muxerPart),
publishPart func(*muxerPart),
) (*muxerSegmentFMP4, error) {
s := &muxerSegmentFMP4{
lowLatency: lowLatency,
Expand All @@ -51,13 +52,14 @@ func newMuxerSegmentFMP4(
videoTrack: videoTrack,
audioTrack: audioTrack,
audioTrackTimeScale: audioTrackTimeScale,
prefix: prefix,
genPartID: genPartID,
onPartFinalized: onPartFinalized,
name: "seg" + strconv.FormatUint(id, 10),
publishPart: publishPart,
name: segmentName(prefix, id, true),
}

var err error
s.storage, err = factory.NewFile(s.name + ".mp4")
s.storage, err = factory.NewFile(s.name)
if err != nil {
return nil, err
}
Expand All @@ -67,6 +69,7 @@ func newMuxerSegmentFMP4(
s.videoTrack,
s.audioTrack,
s.audioTrackTimeScale,
prefix,
s.genPartID(),
s.storage.NewPart(),
)
Expand Down Expand Up @@ -101,7 +104,7 @@ func (s *muxerSegmentFMP4) finalize(nextDTS time.Duration) error {
return err
}

s.onPartFinalized(s.currentPart)
s.publishPart(s.currentPart)
s.parts = append(s.parts, s.currentPart)
}
s.currentPart = nil
Expand Down Expand Up @@ -135,13 +138,14 @@ func (s *muxerSegmentFMP4) writeVideo(
}

s.parts = append(s.parts, s.currentPart)
s.onPartFinalized(s.currentPart)
s.publishPart(s.currentPart)

s.currentPart = newMuxerPart(
nextDTS,
s.videoTrack,
s.audioTrack,
s.audioTrackTimeScale,
s.prefix,
s.genPartID(),
s.storage.NewPart(),
)
Expand Down Expand Up @@ -172,13 +176,14 @@ func (s *muxerSegmentFMP4) writeAudio(
}

s.parts = append(s.parts, s.currentPart)
s.onPartFinalized(s.currentPart)
s.publishPart(s.currentPart)

s.currentPart = newMuxerPart(
nextAudioSampleDTS,
s.videoTrack,
s.audioTrack,
s.audioTrackTimeScale,
s.prefix,
s.genPartID(),
s.storage.NewPart(),
)
Expand Down
6 changes: 3 additions & 3 deletions muxer_segment_mpegts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"fmt"
"io"
"strconv"
"time"

"github.com/bluenviron/gohlslib/pkg/storage"
Expand Down Expand Up @@ -42,6 +41,7 @@ func newMuxerSegmentMPEGTS(
writerAudioTrack *mpegts.Track,
switchableWriter *switchableWriter,
writer *mpegts.Writer,
prefix string,
factory storage.Factory,
) (*muxerSegmentMPEGTS, error) {
t := &muxerSegmentMPEGTS{
Expand All @@ -50,11 +50,11 @@ func newMuxerSegmentMPEGTS(
writerAudioTrack: writerAudioTrack,
writer: writer,
startNTP: startNTP,
name: "seg" + strconv.FormatUint(id, 10),
name: segmentName(prefix, id, false),
}

var err error
t.storage, err = factory.NewFile(t.name + ".ts")
t.storage, err = factory.NewFile(t.name)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 3df19dd

Please sign in to comment.