Skip to content

Commit

Permalink
update urls_test
Browse files Browse the repository at this point in the history
  • Loading branch information
frostbyte73 committed Aug 8, 2024
1 parent 0d59814 commit 65cc1c6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 40 deletions.
36 changes: 15 additions & 21 deletions pkg/config/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,13 @@ func (o *StreamConfig) ValidateUrl(rawUrl string, outputType types.OutputType) (
} else if parsedUrl.Scheme == "twitch" {
parsed, err = o.updateTwitchURL(parsedUrl.Host)
if err != nil {
err = errors.ErrInvalidUrl(rawUrl, err.Error())
return
}
} else if match := twitchEndpoint.FindStringSubmatch(rawUrl); len(match) > 0 {
updated, err := o.updateTwitchURL(match[1])
if err == nil {
if updated, err := o.updateTwitchURL(match[1]); err == nil {
parsed = updated
}
}

if parsed == "" {
} else {
parsed = rawUrl
}

Expand Down Expand Up @@ -122,30 +118,28 @@ func (o *StreamConfig) GetStream(rawUrl string) (*Stream, error) {
return nil, errors.ErrInvalidUrl(rawUrl, err.Error())
}

var parsed, twitchStreamID string
var parsed string
if parsedUrl.Scheme == "mux" {
parsed = fmt.Sprintf("rtmps://global-live.mux.com:443/app/%s", parsedUrl.Host)
} else if parsedUrl.Scheme == "twitch" {
twitchStreamID = parsedUrl.Host
parsed, err = o.updateTwitchURL(parsedUrl.Host)
if err != nil {
return nil, err
}
} else if match := twitchEndpoint.FindStringSubmatch(rawUrl); len(match) > 0 {
twitchStreamID = match[1]
parsed, err = o.updateTwitchURL(match[1])
if err != nil {
return nil, err
}
} else {
parsed = rawUrl
}

var stream *Stream
o.Streams.Range(func(url, s any) bool {
if (parsed != "" && url == parsed) || (twitchStreamID != "" && s.(*Stream).StreamID == twitchStreamID) {
stream = s.(*Stream)
return false
}
return true
})

if stream != nil {
return stream, nil
stream, ok := o.Streams.Load(parsed)
if !ok {
return nil, errors.ErrStreamNotFound(rawUrl)
}
return nil, errors.ErrStreamNotFound(rawUrl)
return stream.(*Stream), nil
}

func (o *StreamConfig) updateTwitchURL(key string) (string, error) {
Expand Down
40 changes: 21 additions & 19 deletions pkg/config/urls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ package config

import (
"regexp"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/livekit/egress/pkg/types"
"github.com/livekit/protocol/livekit"
)

func TestValidateUrl(t *testing.T) {
Expand All @@ -33,12 +33,12 @@ func TestValidateUrl(t *testing.T) {
for _, test := range []struct {
url string
twitch bool
updated string
parsed string
redacted string
}{
{
url: "mux://streamkey",
updated: "rtmps://global-live.mux.com:443/app/streamkey",
parsed: "rtmps://global-live.mux.com:443/app/streamkey",
redacted: "rtmps://global-live.mux.com:443/app/{str...key}",
},
{
Expand All @@ -51,52 +51,54 @@ func TestValidateUrl(t *testing.T) {
},
{
url: "rtmp://localhost:1935/live/streamkey",
updated: "rtmp://localhost:1935/live/streamkey",
parsed: "rtmp://localhost:1935/live/streamkey",
redacted: "rtmp://localhost:1935/live/{str...key}",
},
{
url: "rtmps://localhost:1935/live/streamkey",
updated: "rtmps://localhost:1935/live/streamkey",
parsed: "rtmps://localhost:1935/live/streamkey",
redacted: "rtmps://localhost:1935/live/{str...key}",
},
} {
updated, redacted, err := o.ValidateUrl(test.url, types.OutputTypeRTMP)
parsed, redacted, streamID, err := o.ValidateUrl(test.url, types.OutputTypeRTMP)
require.NoError(t, err)
require.NotEmpty(t, streamID)

if test.twitch {
require.NotEmpty(t, twitchUpdated.FindString(updated), updated)
require.NotEmpty(t, twitchUpdated.FindString(parsed), parsed)
require.NotEmpty(t, twitchRedacted.FindString(redacted), redacted)
} else {
require.Equal(t, test.updated, updated)
require.Equal(t, test.parsed, parsed)
require.Equal(t, test.redacted, redacted)
}
}
}

func TestGetUrl(t *testing.T) {
o := &StreamConfig{}
require.NoError(t, o.updateTwitchTemplate())

parsedTwitchUrl := strings.ReplaceAll(o.twitchTemplate, "{stream_key}", "streamkey")
urls := []string{
"rtmps://global-live.mux.com:443/app/streamkey",
"rtmp://sfo.contribute.live-video.net/app/streamkey",
"rtmp://sfo.contribute.live-video.net/app/streamkey",
parsedTwitchUrl,
parsedTwitchUrl,
"rtmp://localhost:1935/live/streamkey",
}

o := &StreamConfig{
StreamInfo: map[string]*livekit.StreamInfo{
urls[0]: {Url: urls[0]},
urls[1]: {Url: urls[1]},
urls[3]: {Url: urls[3]},
},
for _, url := range []string{urls[0], urls[1], urls[3]} {
_, err := o.AddStream(url, types.OutputTypeRTMP)
require.NoError(t, err)
}

for i, rawUrl := range []string{
"mux://streamkey",
"twitch://streamkey",
"rtmp://jfk.contribute.live-video.net/app/streamkey",
"rtmp://any.contribute.live-video.net/app/streamkey",
"rtmp://localhost:1935/live/streamkey",
} {
url, err := o.GetStreamUrl(rawUrl)
stream, err := o.GetStream(rawUrl)
require.NoError(t, err)
require.Equal(t, urls[i], url)
require.Equal(t, urls[i], stream.ParsedUrl)
}
}

0 comments on commit 65cc1c6

Please sign in to comment.