Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache twitch ingest results #738

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build/egress/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM livekit/gstreamer:1.24.4-dev
FROM livekit/gstreamer:1.22.12-dev

ARG TARGETPLATFORM
ARG TARGETARCH
Expand Down Expand Up @@ -51,7 +51,7 @@ ENV TINI_VERSION v0.19.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-${TARGETARCH} /tini
RUN chmod +x /tini

FROM livekit/gstreamer:1.24.4-prod
FROM livekit/gstreamer:1.22.12-prod

ARG TARGETPLATFORM

Expand Down
4 changes: 3 additions & 1 deletion pkg/config/output_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type StreamConfig struct {

Urls []string
StreamInfo map[string]*livekit.StreamInfo

twitchTemplate string
}

func (p *PipelineConfig) GetStreamConfig() *StreamConfig {
Expand All @@ -50,7 +52,7 @@ func (p *PipelineConfig) getStreamConfig(outputType types.OutputType, urls []str
conf.StreamInfo = make(map[string]*livekit.StreamInfo)
var streamInfoList []*livekit.StreamInfo
for _, rawUrl := range urls {
url, redacted, err := ValidateUrl(rawUrl, outputType)
url, redacted, err := conf.ValidateUrl(rawUrl, outputType)
if err != nil {
return nil, err
}
Expand Down
35 changes: 26 additions & 9 deletions pkg/config/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

var twitchEndpoint = regexp.MustCompile("^rtmps?://.*\\.contribute\\.live-video\\.net/app/(.*)( live=1)?$")

func ValidateUrl(rawUrl string, outputType types.OutputType) (string, string, error) {
func (o *StreamConfig) ValidateUrl(rawUrl string, outputType types.OutputType) (string, string, error) {
parsed, err := url.Parse(rawUrl)
if err != nil {
return "", "", errors.ErrInvalidUrl(rawUrl, err.Error())
Expand All @@ -44,12 +44,12 @@ func ValidateUrl(rawUrl string, outputType types.OutputType) (string, string, er
if parsed.Scheme == "mux" {
rawUrl = fmt.Sprintf("rtmps://global-live.mux.com:443/app/%s", parsed.Host)
} else if parsed.Scheme == "twitch" {
rawUrl, err = updateTwitchURL(parsed.Host)
rawUrl, err = o.updateTwitchURL(parsed.Host)
if err != nil {
return "", "", errors.ErrInvalidUrl(rawUrl, err.Error())
}
} else if match := twitchEndpoint.FindStringSubmatch(rawUrl); len(match) > 0 {
updated, err := updateTwitchURL(match[1])
updated, err := o.updateTwitchURL(match[1])
if err == nil {
rawUrl = updated
}
Expand Down Expand Up @@ -99,12 +99,25 @@ func (o *StreamConfig) GetStreamUrl(rawUrl string) (string, error) {
return "", errors.ErrStreamNotFound(rawUrl)
}

func updateTwitchURL(key string) (string, error) {
func (o *StreamConfig) updateTwitchURL(key string) (string, error) {
if err := o.updateTwitchTemplate(); err != nil {
return "", err
}

return strings.ReplaceAll(o.twitchTemplate, "{stream_key}", key), nil
}

func (o *StreamConfig) updateTwitchTemplate() error {
if o.twitchTemplate != "" {
return nil
}

resp, err := http.Get("https://ingest.twitch.tv/ingests")
if err != nil {
return "", err
return err
}
defer resp.Body.Close()

var body struct {
Ingests []struct {
Name string `json:"name"`
Expand All @@ -114,14 +127,18 @@ func updateTwitchURL(key string) (string, error) {
} `json:"ingests"`
}
if err = json.NewDecoder(resp.Body).Decode(&body); err != nil {
return "", err
return err
}

for _, ingest := range body.Ingests {
if ingest.URLTemplateSecure != "" {
return strings.ReplaceAll(ingest.URLTemplateSecure, "{stream_key}", key), nil
o.twitchTemplate = ingest.URLTemplateSecure
return nil
} else if ingest.URLTemplate != "" {
return strings.ReplaceAll(ingest.URLTemplate, "{stream_key}", key), nil
o.twitchTemplate = ingest.URLTemplate
return nil
}
}
return "", errors.New("no ingest found")

return errors.New("no ingest found")
}
Loading