Skip to content

Commit

Permalink
Merge pull request #127 from sonroyaalmerol/nonstream-failover
Browse files Browse the repository at this point in the history
Add list of possible m3u mimetypes to check
  • Loading branch information
sonroyaalmerol committed Aug 26, 2024
2 parents c7f0e10 + 9b71148 commit 705e301
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions stream_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func proxyStream(ctx context.Context, m3uIndex int, resp *http.Response, r *http
if err != nil {
if err == io.EOF {
log.Printf("Stream ended (EOF reached): %s\n", r.RemoteAddr)
if strings.ToLower(resp.Header.Get("Content-Type")) == "application/x-mpegurl" {
if utils.IsPlaylist(resp) {
statusChan <- 2
return
}
Expand Down Expand Up @@ -283,7 +283,7 @@ func streamHandler(w http.ResponseWriter, r *http.Request, db *database.Instance
streamExitCode := <-exitStatus
log.Printf("Exit code %d received from %s\n", streamExitCode, selectedUrl)

if streamExitCode == 2 && strings.ToLower(resp.Header.Get("Content-Type")) == "application/x-mpegurl" {
if streamExitCode == 2 && utils.IsPlaylist(resp) {
log.Printf("Successfully proxied playlist: %s\n", r.RemoteAddr)
cancel()
} else if streamExitCode == 1 || streamExitCode == 2 {
Expand Down
15 changes: 15 additions & 0 deletions utils/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package utils

import (
"encoding/base64"
"net/http"
"slices"
"strings"
)

func GetStreamUrl(slug string) string {
Expand All @@ -15,3 +18,15 @@ func GetStreamSlugFromUrl(streamUID string) string {
}
return string(decoded)
}

func IsPlaylist(resp *http.Response) bool {
knownMimeTypes := []string{
"application/x-mpegurl",
"text/plain",
"audio/x-mpegurl",
"audio/mpegurl",
"application/vnd.apple.mpegurl",
}

return slices.Contains(knownMimeTypes, strings.ToLower(resp.Header.Get("Content-Type")))
}

0 comments on commit 705e301

Please sign in to comment.