Skip to content

Commit

Permalink
Only accept specific file types in url pull (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
biglittlebigben authored Aug 10, 2023
1 parent 216c6c3 commit 0fa3342
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
32 changes: 30 additions & 2 deletions pkg/media/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

type Source interface {
GetSources() []*gst.Element
ValidateCaps(*gst.Caps) error
Start(ctx context.Context) error
Close() error
}
Expand Down Expand Up @@ -126,6 +127,28 @@ func (i *Input) Close() error {
}

func (i *Input) onPadAdded(_ *gst.Element, pad *gst.Pad) {
var err error

defer func() {
if err != nil {
msg := gst.NewErrorMessage(i.bin.Element, err, err.Error(), nil)
i.bin.Element.GetBus().Post(msg)
}
}()

typefind, err := i.bin.GetElementByName("typefind")
if err == nil && typefind != nil {
var caps interface{}
caps, err = typefind.GetProperty("caps")
if err == nil && caps != nil {
err = i.source.ValidateCaps(caps.(*gst.Caps))
if err != nil {
logger.Infow("input caps validation failed", "error", err)
return
}
}
}

// surface callback for first audio and video pads, plug in fakesink on the rest
i.lock.Lock()
newPad := false
Expand Down Expand Up @@ -156,11 +179,16 @@ func (i *Input) onPadAdded(_ *gst.Element, pad *gst.Pad) {
}
pad = ghostPad.Pad
} else {
sink, err := gst.NewElement("fakesink")
var sink *gst.Element

sink, err = gst.NewElement("fakesink")
if err != nil {
logger.Errorw("failed to create fakesink", err)
return
}
pads, err := sink.GetSinkPads()
var pads []*gst.Pad

pads, err = sink.GetSinkPads()
pad.Link(pads[0])
return
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/media/rtmp/appsrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ func (s *RTMPRelaySource) GetSources() []*gst.Element {
return []*gst.Element{s.flvSrc.Element}
}

func (s *RTMPRelaySource) ValidateCaps(caps *gst.Caps) error {
if caps.GetSize() == 0 {
return errors.ErrUnsupportedDecodeFormat
}

str := caps.GetStructureAt(0)
if str == nil {
return errors.ErrUnsupportedDecodeFormat
}

if str.Name() != "video/x-flv" {
return errors.ErrUnsupportedDecodeFormat
}

return nil
}

type appSrcWriter struct {
appSrc *app.Source
eos *atomic.Bool
Expand Down
32 changes: 32 additions & 0 deletions pkg/media/urlpull/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ import (
"github.com/livekit/ingress/pkg/params"
)

var (
supportedMimeTypes = []string{
"audio/x-m4a",
"application/x-hls",
"video/quicktime",
"video/x-matroska",
"video/webm",
"audio/ogg",
"application/x-id3",
"audio/mpeg",
}
)

type URLSource struct {
params *params.Params
src *gst.Element
Expand Down Expand Up @@ -85,6 +98,25 @@ func (u *URLSource) GetSources() []*gst.Element {
}
}

func (s *URLSource) ValidateCaps(caps *gst.Caps) error {
if caps.GetSize() == 0 {
return errors.ErrUnsupportedDecodeFormat
}

str := caps.GetStructureAt(0)
if str == nil {
return errors.ErrUnsupportedDecodeFormat
}

for _, mime := range supportedMimeTypes {
if str.Name() == mime {
return nil
}
}

return errors.ErrUnsupportedDecodeFormat
}

func (u *URLSource) Start(ctx context.Context) error {
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/media/whip/whipsrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func (s *WHIPSource) GetSources() []*gst.Element {
return ret
}

func (s *WHIPSource) ValidateCaps(*gst.Caps) error {
return nil
}

func (s *WHIPSource) getRelayUrl(kind types.StreamKind) string {
return fmt.Sprintf("%s/%s", s.params.RelayUrl, kind)
}

0 comments on commit 0fa3342

Please sign in to comment.