Skip to content

Commit

Permalink
Merge pull request #104 from sonroyaalmerol/m3u-support
Browse files Browse the repository at this point in the history
Consider success if a playlist file reaches EOF
  • Loading branch information
sonroyaalmerol authored Aug 24, 2024
2 parents a8a396b + 8e9f2f0 commit 4e3762c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 5 additions & 2 deletions stream_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func proxyStream(m3uIndex int, resp *http.Response, r *http.Request, w http.Resp
if err != nil {
if err == io.EOF {
log.Printf("Stream ended (EOF reached): %s\n", r.RemoteAddr)
statusChan <- 1
statusChan <- 2
return
}
log.Printf("Error reading stream: %s\n", err.Error())
Expand Down Expand Up @@ -201,7 +201,10 @@ 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 == 1 {
if streamExitCode == 2 && utils.IsPlaylistFile(selectedUrl) {
log.Printf("Successfully proxied playlist (M3U) file: %s\n", r.RemoteAddr)
cancel()
} else if streamExitCode == 1 || streamExitCode == 2 {
// Retry on server-side connection errors
log.Printf("Retrying other servers...\n")
} else {
Expand Down
11 changes: 10 additions & 1 deletion utils/url.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package utils

import "encoding/base64"
import (
"encoding/base64"
"strings"
)

func GetStreamUrl(slug string) string {
return base64.URLEncoding.EncodeToString([]byte(slug))
Expand All @@ -13,3 +16,9 @@ func GetStreamSlugFromUrl(streamUID string) string {
}
return string(decoded)
}

func IsPlaylistFile(url string) bool {
urlClean := strings.TrimSpace(strings.ToLower(url))

return strings.HasSuffix(urlClean, ".m3u") || strings.HasSuffix(urlClean, ".m3u8")
}

0 comments on commit 4e3762c

Please sign in to comment.