-
Notifications
You must be signed in to change notification settings - Fork 79
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
Adjust twitch ingest urls #726
Changes from all commits
0e7af7b
93dd6a6
35dea12
c355670
a4d2ebe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright 2023 LiveKit, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package config | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/go-jose/go-jose/v3/json" | ||
|
||
"github.com/livekit/egress/pkg/errors" | ||
"github.com/livekit/egress/pkg/types" | ||
"github.com/livekit/protocol/utils" | ||
) | ||
|
||
var twitchEndpoint = regexp.MustCompile("^rtmps?://.*\\.contribute\\.live-video\\.net/app/(.*)( live=1)?$") | ||
|
||
func ValidateUrl(rawUrl string, outputType types.OutputType) (string, string, error) { | ||
parsed, err := url.Parse(rawUrl) | ||
if err != nil { | ||
return "", "", errors.ErrInvalidUrl(rawUrl, err.Error()) | ||
} | ||
|
||
switch outputType { | ||
case types.OutputTypeRTMP: | ||
if parsed.Scheme == "mux" { | ||
rawUrl = fmt.Sprintf("rtmps://global-live.mux.com:443/app/%s", parsed.Host) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} else if parsed.Scheme == "twitch" { | ||
rawUrl, err = 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]) | ||
if err == nil { | ||
rawUrl = updated | ||
} | ||
} | ||
|
||
redacted, ok := utils.RedactStreamKey(rawUrl) | ||
if !ok { | ||
return "", "", errors.ErrInvalidUrl(rawUrl, "rtmp urls must be of format rtmp(s)://{host}(/{path})/{app}/{stream_key}( live=1)") | ||
} | ||
return rawUrl, redacted, nil | ||
|
||
case types.OutputTypeRaw: | ||
if parsed.Scheme != "ws" && parsed.Scheme != "wss" { | ||
return "", "", errors.ErrInvalidUrl(rawUrl, "invalid scheme") | ||
} | ||
return rawUrl, rawUrl, nil | ||
|
||
default: | ||
return "", "", errors.ErrInvalidInput("stream output type") | ||
} | ||
} | ||
|
||
func (o *StreamConfig) GetStreamUrl(rawUrl string) (string, error) { | ||
parsed, err := url.Parse(rawUrl) | ||
if err != nil { | ||
return "", errors.ErrInvalidUrl(rawUrl, err.Error()) | ||
} | ||
|
||
var twitchKey string | ||
if parsed.Scheme == "mux" { | ||
return fmt.Sprintf("rtmps://global-live.mux.com:443/app/%s", parsed.Host), nil | ||
} else if parsed.Scheme == "twitch" { | ||
twitchKey = parsed.Host | ||
} else if match := twitchEndpoint.FindStringSubmatch(rawUrl); len(match) > 0 { | ||
twitchKey = match[1] | ||
} else { | ||
return rawUrl, nil | ||
} | ||
|
||
// find twitch url by stream key because we can't rely on the ingest endpoint returning consistent results | ||
for u := range o.StreamInfo { | ||
if match := twitchEndpoint.FindStringSubmatch(u); len(match) > 0 && match[1] == twitchKey { | ||
return u, nil | ||
} | ||
} | ||
|
||
return "", errors.ErrStreamNotFound(rawUrl) | ||
} | ||
|
||
func updateTwitchURL(key string) (string, error) { | ||
resp, err := http.Get("https://ingest.twitch.tv/ingests") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could pass context here from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it only gets passed around for projectID in cloud |
||
if err != nil { | ||
return "", err | ||
} | ||
defer resp.Body.Close() | ||
var body struct { | ||
Ingests []struct { | ||
Name string `json:"name"` | ||
URLTemplate string `json:"url_template"` | ||
URLTemplateSecure string `json:"url_template_secure"` | ||
Priority int `json:"priority"` | ||
} `json:"ingests"` | ||
} | ||
if err = json.NewDecoder(resp.Body).Decode(&body); err != nil { | ||
return "", err | ||
} | ||
for _, ingest := range body.Ingests { | ||
if ingest.URLTemplateSecure != "" { | ||
return strings.ReplaceAll(ingest.URLTemplateSecure, "{stream_key}", key), nil | ||
} else if ingest.URLTemplate != "" { | ||
return strings.ReplaceAll(ingest.URLTemplate, "{stream_key}", key), nil | ||
} | ||
} | ||
return "", errors.New("no ingest found") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A nit, but if you use backquote you won't need to escape the slash.