Skip to content

Commit

Permalink
hls: support reading multiple audio tracks (#2728)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Sep 22, 2024
1 parent 82f2f26 commit dba5058
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 117 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/abema/go-mp4 v1.2.0
github.com/alecthomas/kong v1.2.1
github.com/asticode/go-astits v1.13.0
github.com/bluenviron/gohlslib v1.4.0
github.com/bluenviron/gohlslib/v2 v2.0.0-20240922120058-777eb7502c6d
github.com/bluenviron/gortsplib/v4 v4.10.6
github.com/bluenviron/mediacommon v1.12.4
github.com/datarhei/gosrt v0.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ github.com/asticode/go-astits v1.13.0 h1:XOgkaadfZODnyZRR5Y0/DWkA9vrkLLPLeeOvDwf
github.com/asticode/go-astits v1.13.0/go.mod h1:QSHmknZ51pf6KJdHKZHJTLlMegIrhega3LPWz3ND/iI=
github.com/benburkert/openpgp v0.0.0-20160410205803-c2471f86866c h1:8XZeJrs4+ZYhJeJ2aZxADI2tGADS15AzIF8MQ8XAhT4=
github.com/benburkert/openpgp v0.0.0-20160410205803-c2471f86866c/go.mod h1:x1vxHcL/9AVzuk5HOloOEPrtJY0MaalYr78afXZ+pWI=
github.com/bluenviron/gohlslib v1.4.0 h1:3a9W1x8eqlxJUKt1sJCunPGtti5ALIY2ik4GU0RVe7E=
github.com/bluenviron/gohlslib v1.4.0/go.mod h1:q5ZElzNw5GRbV1VEI45qkcPbKBco6BP58QEY5HyFsmo=
github.com/bluenviron/gohlslib/v2 v2.0.0-20240922120058-777eb7502c6d h1:9A3/zdCFSAJLtza+6pVjY68gfje4FC82BDYYPOnE3s4=
github.com/bluenviron/gohlslib/v2 v2.0.0-20240922120058-777eb7502c6d/go.mod h1:DVvQIj+MjYydWuYDCgP+s0/GplDgUSpDNXCA/BVLhu4=
github.com/bluenviron/gortsplib/v4 v4.10.6 h1:KMvVcU21xxQQu1Jqn6D/z/FoIMn+QEKE1dBDWt4aWvg=
github.com/bluenviron/gortsplib/v4 v4.10.6/go.mod h1:/7C8qoGEsIQupuVw8YnXANpqBMNBpZ+51xFreLGiN2g=
github.com/bluenviron/mediacommon v1.12.4 h1:7VrA/W/iDB7VELquXqRjgjzUSJT3llZYgXjFN9WkByo=
Expand Down
2 changes: 1 addition & 1 deletion internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"
"time"

"github.com/bluenviron/gohlslib"
"github.com/bluenviron/gohlslib/v2"
"github.com/bluenviron/gortsplib/v4"
"github.com/bluenviron/gortsplib/v4/pkg/auth"

Expand Down
2 changes: 1 addition & 1 deletion internal/conf/hls_variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"

"github.com/bluenviron/gohlslib"
"github.com/bluenviron/gohlslib/v2"
)

// HLSVariant is the hlsVariant parameter.
Expand Down
206 changes: 110 additions & 96 deletions internal/protocols/hls/from_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"errors"
"fmt"

"github.com/bluenviron/gohlslib"
"github.com/bluenviron/gohlslib/pkg/codecs"
"github.com/bluenviron/gohlslib/v2"
"github.com/bluenviron/gohlslib/v2/pkg/codecs"
"github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/bluenviron/mediamtx/internal/asyncwriter"
"github.com/bluenviron/mediamtx/internal/logger"
Expand All @@ -22,189 +22,199 @@ func setupVideoTrack(
stream *stream.Stream,
writer *asyncwriter.Writer,
muxer *gohlslib.Muxer,
) format.Format {
setuppedFormats map[format.Format]struct{},
) {
var videoFormatAV1 *format.AV1
videoMedia := stream.Desc().FindFormat(&videoFormatAV1)

if videoFormatAV1 != nil {
track := &gohlslib.Track{
Codec: &codecs.AV1{},
}
muxer.Tracks = append(muxer.Tracks, track)
setuppedFormats[videoFormatAV1] = struct{}{}

stream.AddReader(writer, videoMedia, videoFormatAV1, func(u unit.Unit) error {
tunit := u.(*unit.AV1)

if tunit.TU == nil {
return nil
}

err := muxer.WriteAV1(tunit.NTP, tunit.PTS, tunit.TU)
err := muxer.WriteAV1(track, tunit.NTP, tunit.PTS, tunit.TU)
if err != nil {
return fmt.Errorf("muxer error: %w", err)
}

return nil
})

muxer.VideoTrack = &gohlslib.Track{
Codec: &codecs.AV1{},
}
return videoFormatAV1
return
}

var videoFormatVP9 *format.VP9
videoMedia = stream.Desc().FindFormat(&videoFormatVP9)

if videoFormatVP9 != nil {
track := &gohlslib.Track{
Codec: &codecs.VP9{},
}
muxer.Tracks = append(muxer.Tracks, track)
setuppedFormats[videoFormatVP9] = struct{}{}

stream.AddReader(writer, videoMedia, videoFormatVP9, func(u unit.Unit) error {
tunit := u.(*unit.VP9)

if tunit.Frame == nil {
return nil
}

err := muxer.WriteVP9(tunit.NTP, tunit.PTS, tunit.Frame)
err := muxer.WriteVP9(track, tunit.NTP, tunit.PTS, tunit.Frame)
if err != nil {
return fmt.Errorf("muxer error: %w", err)
}

return nil
})

muxer.VideoTrack = &gohlslib.Track{
Codec: &codecs.VP9{},
}
return videoFormatVP9
return
}

var videoFormatH265 *format.H265
videoMedia = stream.Desc().FindFormat(&videoFormatH265)

if videoFormatH265 != nil {
vps, sps, pps := videoFormatH265.SafeParams()
track := &gohlslib.Track{
Codec: &codecs.H265{
VPS: vps,
SPS: sps,
PPS: pps,
},
}
muxer.Tracks = append(muxer.Tracks, track)
setuppedFormats[videoFormatH265] = struct{}{}

stream.AddReader(writer, videoMedia, videoFormatH265, func(u unit.Unit) error {
tunit := u.(*unit.H265)

if tunit.AU == nil {
return nil
}

err := muxer.WriteH265(tunit.NTP, tunit.PTS, tunit.AU)
err := muxer.WriteH265(track, tunit.NTP, tunit.PTS, tunit.AU)
if err != nil {
return fmt.Errorf("muxer error: %w", err)
}

return nil
})

vps, sps, pps := videoFormatH265.SafeParams()

muxer.VideoTrack = &gohlslib.Track{
Codec: &codecs.H265{
VPS: vps,
SPS: sps,
PPS: pps,
},
}
return videoFormatH265
return
}

var videoFormatH264 *format.H264
videoMedia = stream.Desc().FindFormat(&videoFormatH264)

if videoFormatH264 != nil {
sps, pps := videoFormatH264.SafeParams()
track := &gohlslib.Track{
Codec: &codecs.H264{
SPS: sps,
PPS: pps,
},
}
muxer.Tracks = append(muxer.Tracks, track)
setuppedFormats[videoFormatH264] = struct{}{}

stream.AddReader(writer, videoMedia, videoFormatH264, func(u unit.Unit) error {
tunit := u.(*unit.H264)

if tunit.AU == nil {
return nil
}

err := muxer.WriteH264(tunit.NTP, tunit.PTS, tunit.AU)
err := muxer.WriteH264(track, tunit.NTP, tunit.PTS, tunit.AU)
if err != nil {
return fmt.Errorf("muxer error: %w", err)
}

return nil
})

sps, pps := videoFormatH264.SafeParams()

muxer.VideoTrack = &gohlslib.Track{
Codec: &codecs.H264{
SPS: sps,
PPS: pps,
},
}
return videoFormatH264
return
}

return nil
}

func setupAudioTrack(
func setupAudioTracks(
stream *stream.Stream,
writer *asyncwriter.Writer,
muxer *gohlslib.Muxer,
l logger.Writer,
) format.Format {
var audioFormatOpus *format.Opus
audioMedia := stream.Desc().FindFormat(&audioFormatOpus)

if audioFormatOpus != nil {
stream.AddReader(writer, audioMedia, audioFormatOpus, func(u unit.Unit) error {
tunit := u.(*unit.Opus)

err := muxer.WriteOpus(
tunit.NTP,
tunit.PTS,
tunit.Packets)
if err != nil {
return fmt.Errorf("muxer error: %w", err)
}

return nil
})

muxer.AudioTrack = &gohlslib.Track{
Codec: &codecs.Opus{
ChannelCount: audioFormatOpus.ChannelCount,
},
}
return audioFormatOpus
}
setuppedFormats map[format.Format]struct{},
) {
for _, media := range stream.Desc().Medias {
for _, forma := range media.Formats {
switch forma := forma.(type) {
case *format.Opus:
track := &gohlslib.Track{
Codec: &codecs.Opus{
ChannelCount: forma.ChannelCount,
},
}
muxer.Tracks = append(muxer.Tracks, track)
setuppedFormats[forma] = struct{}{}

var audioFormatMPEG4Audio *format.MPEG4Audio
audioMedia = stream.Desc().FindFormat(&audioFormatMPEG4Audio)
stream.AddReader(writer, media, forma, func(u unit.Unit) error {
tunit := u.(*unit.Opus)

if audioFormatMPEG4Audio != nil {
co := audioFormatMPEG4Audio.GetConfig()
if co == nil {
l.Log(logger.Warn, "skipping MPEG-4 audio track: tracks without explicit configuration are not supported")
} else {
stream.AddReader(writer, audioMedia, audioFormatMPEG4Audio, func(u unit.Unit) error {
tunit := u.(*unit.MPEG4Audio)
err := muxer.WriteOpus(
track,
tunit.NTP,
tunit.PTS,
tunit.Packets)
if err != nil {
return fmt.Errorf("muxer error: %w", err)
}

if tunit.AUs == nil {
return nil
})

case *format.MPEG4Audio:
co := forma.GetConfig()
if co == nil {
l.Log(logger.Warn, "skipping MPEG-4 audio track: tracks without explicit configuration are not supported")
} else {
track := &gohlslib.Track{
Codec: &codecs.MPEG4Audio{
Config: *co,
},
}
muxer.Tracks = append(muxer.Tracks, track)
setuppedFormats[forma] = struct{}{}

stream.AddReader(writer, media, forma, func(u unit.Unit) error {
tunit := u.(*unit.MPEG4Audio)

if tunit.AUs == nil {
return nil
}

err := muxer.WriteMPEG4Audio(
track,
tunit.NTP,
tunit.PTS,
tunit.AUs)
if err != nil {
return fmt.Errorf("muxer error: %w", err)
}

return nil
})
}

err := muxer.WriteMPEG4Audio(
tunit.NTP,
tunit.PTS,
tunit.AUs)
if err != nil {
return fmt.Errorf("muxer error: %w", err)
}

return nil
})

muxer.AudioTrack = &gohlslib.Track{
Codec: &codecs.MPEG4Audio{
Config: *co,
},
}
return audioFormatMPEG4Audio
}
}

return nil
}

// FromStream maps a MediaMTX stream to a HLS muxer.
Expand All @@ -214,26 +224,30 @@ func FromStream(
muxer *gohlslib.Muxer,
l logger.Writer,
) error {
videoFormat := setupVideoTrack(
setuppedFormats := make(map[format.Format]struct{})

setupVideoTrack(
stream,
writer,
muxer,
setuppedFormats,
)

audioFormat := setupAudioTrack(
setupAudioTracks(
stream,
writer,
muxer,
l,
setuppedFormats,
)

if videoFormat == nil && audioFormat == nil {
if len(muxer.Tracks) == 0 {
return ErrNoSupportedCodecs
}

for _, media := range stream.Desc().Medias {
for _, forma := range media.Formats {
if forma != videoFormat && forma != audioFormat {
if _, ok := setuppedFormats[forma]; !ok {
l.Log(logger.Warn, "skipping track with codec %s", forma.Codec())
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/protocols/hls/from_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/bluenviron/gohlslib"
"github.com/bluenviron/gohlslib/v2"
"github.com/bluenviron/gortsplib/v4/pkg/description"
"github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/bluenviron/mediamtx/internal/asyncwriter"
Expand Down
4 changes: 2 additions & 2 deletions internal/protocols/hls/to_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package hls
import (
"time"

"github.com/bluenviron/gohlslib"
"github.com/bluenviron/gohlslib/pkg/codecs"
"github.com/bluenviron/gohlslib/v2"
"github.com/bluenviron/gohlslib/v2/pkg/codecs"
"github.com/bluenviron/gortsplib/v4/pkg/description"
"github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/bluenviron/mediamtx/internal/stream"
Expand Down
Loading

0 comments on commit dba5058

Please sign in to comment.