Skip to content

Commit

Permalink
treat different formats as different tracks in logs and API
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Jan 14, 2024
1 parent 397c58a commit 933029f
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 104 deletions.
2 changes: 1 addition & 1 deletion internal/core/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ func (pa *path) doAPIPathsGet(req pathAPIPathsGetReq) {
if pa.stream == nil {
return []string{}
}
return defs.MediasDescription(pa.stream.Desc().Medias)
return defs.MediasToCodecs(pa.stream.Desc().Medias)
}(),
BytesReceived: func() uint64 {
if pa.stream == nil {
Expand Down
47 changes: 30 additions & 17 deletions internal/defs/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/bluenviron/gortsplib/v4/pkg/description"
"github.com/bluenviron/gortsplib/v4/pkg/format"

"github.com/bluenviron/mediamtx/internal/logger"
)
Expand All @@ -19,32 +20,44 @@ type Source interface {
APISourceDescribe() APIPathSourceOrReader
}

func mediaDescription(media *description.Media) string {
ret := make([]string, len(media.Formats))
for i, forma := range media.Formats {
// FormatsToCodecs returns the name of codecs of given formats.
func FormatsToCodecs(formats []format.Format) []string {
ret := make([]string, len(formats))
for i, forma := range formats {
ret[i] = forma.Codec()
}
return strings.Join(ret, "/")
}

// MediasDescription returns the description of medias.
func MediasDescription(medias []*description.Media) []string {
ret := make([]string, len(medias))
for i, media := range medias {
ret[i] = mediaDescription(media)
}
return ret
}

// MediasInfo returns the description of medias.
func MediasInfo(medias []*description.Media) string {
// FormatsInfo returns a description of formats.
func FormatsInfo(formats []format.Format) string {
return fmt.Sprintf("%d %s (%s)",
len(medias),
len(formats),
func() string {
if len(medias) == 1 {
if len(formats) == 1 {
return "track"
}
return "tracks"
}(),
strings.Join(MediasDescription(medias), ", "))
strings.Join(FormatsToCodecs(formats), ", "))
}

// MediasToCodecs returns the name of codecs of given formats.
func MediasToCodecs(medias []*description.Media) []string {
var formats []format.Format
for _, media := range medias {
formats = append(formats, media.Formats...)
}

return FormatsToCodecs(formats)
}

// MediasInfo returns a description of medias.
func MediasInfo(medias []*description.Media) string {
var formats []format.Format
for _, media := range medias {
formats = append(formats, media.Formats...)
}

return FormatsInfo(formats)
}
48 changes: 24 additions & 24 deletions internal/record/format_fmp4.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/bluenviron/mediacommon/pkg/codecs/vp9"
"github.com/bluenviron/mediacommon/pkg/formats/fmp4"

"github.com/bluenviron/mediamtx/internal/defs"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/unit"
)
Expand Down Expand Up @@ -108,18 +109,23 @@ type formatFMP4 struct {

func (f *formatFMP4) initialize() {
nextID := 1
var formats []rtspformat.Format

addTrack := func(codec fmp4.Codec) *formatFMP4Track {
addTrack := func(format rtspformat.Format, codec fmp4.Codec) *formatFMP4Track {
initTrack := &fmp4.InitTrack{
TimeScale: 90000,
Codec: codec,
}
initTrack.ID = nextID
nextID++

track := newFormatFMP4Track(f, initTrack)
f.tracks = append(f.tracks, track)
track := &formatFMP4Track{
f: f,
initTrack: initTrack,
}

f.tracks = append(f.tracks, track)
formats = append(formats, format)
return track
}

Expand All @@ -142,7 +148,7 @@ func (f *formatFMP4) initialize() {
8, 0, 0, 0, 66, 167, 191, 228, 96, 13, 0, 64,
},
}
track := addTrack(codec)
track := addTrack(forma, codec)

Check warning on line 151 in internal/record/format_fmp4.go

View check run for this annotation

Codecov / codecov/patch

internal/record/format_fmp4.go#L151

Added line #L151 was not covered by tests

firstReceived := false

Expand Down Expand Up @@ -199,7 +205,7 @@ func (f *formatFMP4) initialize() {
ChromaSubsampling: 1,
ColorRange: false,
}
track := addTrack(codec)
track := addTrack(forma, codec)

Check warning on line 208 in internal/record/format_fmp4.go

View check run for this annotation

Codecov / codecov/patch

internal/record/format_fmp4.go#L208

Added line #L208 was not covered by tests

firstReceived := false

Expand Down Expand Up @@ -296,7 +302,7 @@ func (f *formatFMP4) initialize() {
SPS: sps,
PPS: pps,
}
track := addTrack(codec)
track := addTrack(forma, codec)

var dtsExtractor *h265.DTSExtractor

Expand Down Expand Up @@ -381,7 +387,7 @@ func (f *formatFMP4) initialize() {
SPS: sps,
PPS: pps,
}
track := addTrack(codec)
track := addTrack(forma, codec)

var dtsExtractor *h264.DTSExtractor

Expand Down Expand Up @@ -456,7 +462,7 @@ func (f *formatFMP4) initialize() {
codec := &fmp4.CodecMPEG4Video{
Config: config,
}
track := addTrack(codec)
track := addTrack(forma, codec)

Check warning on line 465 in internal/record/format_fmp4.go

View check run for this annotation

Codecov / codecov/patch

internal/record/format_fmp4.go#L465

Added line #L465 was not covered by tests

firstReceived := false
var lastPTS time.Duration
Expand Down Expand Up @@ -508,7 +514,7 @@ func (f *formatFMP4) initialize() {
0x14, 0x4a, 0x00, 0x01, 0x00, 0x00,
},
}
track := addTrack(codec)
track := addTrack(forma, codec)

Check warning on line 517 in internal/record/format_fmp4.go

View check run for this annotation

Codecov / codecov/patch

internal/record/format_fmp4.go#L517

Added line #L517 was not covered by tests

firstReceived := false
var lastPTS time.Duration
Expand Down Expand Up @@ -557,7 +563,7 @@ func (f *formatFMP4) initialize() {
Width: 800,
Height: 600,
}
track := addTrack(codec)
track := addTrack(forma, codec)

Check warning on line 566 in internal/record/format_fmp4.go

View check run for this annotation

Codecov / codecov/patch

internal/record/format_fmp4.go#L566

Added line #L566 was not covered by tests

parsed := false

Expand Down Expand Up @@ -595,7 +601,7 @@ func (f *formatFMP4) initialize() {
return 1
}(),
}
track := addTrack(codec)
track := addTrack(forma, codec)

Check warning on line 604 in internal/record/format_fmp4.go

View check run for this annotation

Codecov / codecov/patch

internal/record/format_fmp4.go#L604

Added line #L604 was not covered by tests

f.a.agent.Stream.AddReader(f.a.writer, media, forma, func(u unit.Unit) error {
tunit := u.(*unit.Opus)
Expand Down Expand Up @@ -626,7 +632,7 @@ func (f *formatFMP4) initialize() {
codec := &fmp4.CodecMPEG4Audio{
Config: *forma.GetConfig(),
}
track := addTrack(codec)
track := addTrack(forma, codec)

sampleRate := time.Duration(forma.ClockRate())

Expand Down Expand Up @@ -659,7 +665,7 @@ func (f *formatFMP4) initialize() {
SampleRate: 32000,
ChannelCount: 2,
}
track := addTrack(codec)
track := addTrack(forma, codec)

Check warning on line 668 in internal/record/format_fmp4.go

View check run for this annotation

Codecov / codecov/patch

internal/record/format_fmp4.go#L668

Added line #L668 was not covered by tests

parsed := false

Expand Down Expand Up @@ -713,7 +719,7 @@ func (f *formatFMP4) initialize() {
LfeOn: true,
BitRateCode: 7,
}
track := addTrack(codec)
track := addTrack(forma, codec)

Check warning on line 722 in internal/record/format_fmp4.go

View check run for this annotation

Codecov / codecov/patch

internal/record/format_fmp4.go#L722

Added line #L722 was not covered by tests

parsed := false

Expand Down Expand Up @@ -778,7 +784,7 @@ func (f *formatFMP4) initialize() {
SampleRate: forma.SampleRate,
ChannelCount: forma.ChannelCount,
}
track := addTrack(codec)
track := addTrack(forma, codec)

f.a.agent.Stream.AddReader(f.a.writer, media, forma, func(u unit.Unit) error {
tunit := u.(*unit.G711)
Expand Down Expand Up @@ -808,7 +814,7 @@ func (f *formatFMP4) initialize() {
SampleRate: forma.SampleRate,
ChannelCount: forma.ChannelCount,
}
track := addTrack(codec)
track := addTrack(forma, codec)

Check warning on line 817 in internal/record/format_fmp4.go

View check run for this annotation

Codecov / codecov/patch

internal/record/format_fmp4.go#L817

Added line #L817 was not covered by tests

f.a.agent.Stream.AddReader(f.a.writer, media, forma, func(u unit.Unit) error {
tunit := u.(*unit.LPCM)
Expand All @@ -827,14 +833,8 @@ func (f *formatFMP4) initialize() {
}
}

f.a.agent.Log(logger.Info, "recording %d %s",
len(f.tracks),
func() string {
if len(f.tracks) == 1 {
return "track"
}
return "tracks"
}())
f.a.agent.Log(logger.Info, "recording %s",
defs.FormatsInfo(formats))
}

func (f *formatFMP4) close() {
Expand Down
10 changes: 0 additions & 10 deletions internal/record/format_fmp4_track.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ type formatFMP4Track struct {
nextSample *sample
}

func newFormatFMP4Track(
f *formatFMP4,
initTrack *fmp4.InitTrack,
) *formatFMP4Track {
return &formatFMP4Track{
f: f,
initTrack: initTrack,
}
}

func (t *formatFMP4Track) record(sample *sample) error {
// wait the first video sample before setting hasVideo
if t.initTrack.Codec.IsVideo() {
Expand Down
32 changes: 15 additions & 17 deletions internal/record/format_mpegts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4video"
"github.com/bluenviron/mediacommon/pkg/formats/mpegts"

"github.com/bluenviron/mediamtx/internal/defs"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/unit"
)
Expand Down Expand Up @@ -50,20 +51,23 @@ type formatMPEGTS struct {

func (f *formatMPEGTS) initialize() {
var tracks []*mpegts.Track
var formats []rtspformat.Format

addTrack := func(codec mpegts.Codec) *mpegts.Track {
addTrack := func(format rtspformat.Format, codec mpegts.Codec) *mpegts.Track {
track := &mpegts.Track{
Codec: codec,
}

tracks = append(tracks, track)
formats = append(formats, format)
return track
}

for _, media := range f.a.agent.Stream.Desc().Medias {
for _, forma := range media.Formats {
switch forma := forma.(type) {
case *rtspformat.H265:
track := addTrack(&mpegts.CodecH265{})
track := addTrack(forma, &mpegts.CodecH265{})

var dtsExtractor *h265.DTSExtractor

Expand Down Expand Up @@ -91,7 +95,7 @@ func (f *formatMPEGTS) initialize() {
})

case *rtspformat.H264:
track := addTrack(&mpegts.CodecH264{})
track := addTrack(forma, &mpegts.CodecH264{})

var dtsExtractor *h264.DTSExtractor

Expand Down Expand Up @@ -119,7 +123,7 @@ func (f *formatMPEGTS) initialize() {
})

case *rtspformat.MPEG4Video:
track := addTrack(&mpegts.CodecMPEG4Video{})
track := addTrack(forma, &mpegts.CodecMPEG4Video{})

Check warning on line 126 in internal/record/format_mpegts.go

View check run for this annotation

Codecov / codecov/patch

internal/record/format_mpegts.go#L126

Added line #L126 was not covered by tests

firstReceived := false
var lastPTS time.Duration
Expand Down Expand Up @@ -149,7 +153,7 @@ func (f *formatMPEGTS) initialize() {
})

case *rtspformat.MPEG1Video:
track := addTrack(&mpegts.CodecMPEG1Video{})
track := addTrack(forma, &mpegts.CodecMPEG1Video{})

Check warning on line 156 in internal/record/format_mpegts.go

View check run for this annotation

Codecov / codecov/patch

internal/record/format_mpegts.go#L156

Added line #L156 was not covered by tests

firstReceived := false
var lastPTS time.Duration
Expand Down Expand Up @@ -179,7 +183,7 @@ func (f *formatMPEGTS) initialize() {
})

case *rtspformat.Opus:
track := addTrack(&mpegts.CodecOpus{
track := addTrack(forma, &mpegts.CodecOpus{

Check warning on line 186 in internal/record/format_mpegts.go

View check run for this annotation

Codecov / codecov/patch

internal/record/format_mpegts.go#L186

Added line #L186 was not covered by tests
ChannelCount: func() int {
if forma.IsStereo {
return 2
Expand All @@ -203,7 +207,7 @@ func (f *formatMPEGTS) initialize() {
})

case *rtspformat.MPEG4Audio:
track := addTrack(&mpegts.CodecMPEG4Audio{
track := addTrack(forma, &mpegts.CodecMPEG4Audio{
Config: *forma.GetConfig(),
})

Expand All @@ -222,7 +226,7 @@ func (f *formatMPEGTS) initialize() {
})

case *rtspformat.MPEG1Audio:
track := addTrack(&mpegts.CodecMPEG1Audio{})
track := addTrack(forma, &mpegts.CodecMPEG1Audio{})

Check warning on line 229 in internal/record/format_mpegts.go

View check run for this annotation

Codecov / codecov/patch

internal/record/format_mpegts.go#L229

Added line #L229 was not covered by tests

f.a.agent.Stream.AddReader(f.a.writer, media, forma, func(u unit.Unit) error {
tunit := u.(*unit.MPEG1Audio)
Expand All @@ -239,7 +243,7 @@ func (f *formatMPEGTS) initialize() {
})

case *rtspformat.AC3:
track := addTrack(&mpegts.CodecAC3{})
track := addTrack(forma, &mpegts.CodecAC3{})

Check warning on line 246 in internal/record/format_mpegts.go

View check run for this annotation

Codecov / codecov/patch

internal/record/format_mpegts.go#L246

Added line #L246 was not covered by tests

sampleRate := time.Duration(forma.SampleRate)

Expand Down Expand Up @@ -269,14 +273,8 @@ func (f *formatMPEGTS) initialize() {
f.bw = bufio.NewWriterSize(f.dw, mpegtsMaxBufferSize)
f.mw = mpegts.NewWriter(f.bw, tracks)

f.a.agent.Log(logger.Info, "recording %d %s",
len(tracks),
func() string {
if len(tracks) == 1 {
return "track"
}
return "tracks"
}())
f.a.agent.Log(logger.Info, "recording %s",
defs.FormatsInfo(formats))
}

func (f *formatMPEGTS) close() {
Expand Down
Loading

0 comments on commit 933029f

Please sign in to comment.