Skip to content
This repository has been archived by the owner on Nov 27, 2021. It is now read-only.

Commit

Permalink
fix some channels not loading at all using HLS service
Browse files Browse the repository at this point in the history
  • Loading branch information
erkexzcx committed Mar 27, 2021
1 parent 105cef7 commit cda0e3d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion hls/content_m3u8.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func handleEstablishedContentM3U8(w http.ResponseWriter, r *http.Request, cr *Co
switch {
case contentType == "application/vnd.apple.mpegurl" || contentType == "application/x-mpegurl": // M3U8 metadata
content := rewriteLinks(&resp.Body, prefix, cr.Channel.LinkM3u8Ref.linkRoot)
addHeaders(resp.Header, w.Header())
addHeaders(resp.Header, w.Header(), false)
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, content)
default: // media (or anything else)
Expand Down
2 changes: 1 addition & 1 deletion hls/content_media.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func handleContentMedia(w http.ResponseWriter, r *http.Request, cr *ContentReque

func handleEstablishedContentMedia(w http.ResponseWriter, r *http.Request, cr *ContentRequest, resp *http.Response) {
cr.Channel.Mux.Unlock() // So other clients can watch it too
addHeaders(resp.Header, w.Header())
addHeaders(resp.Header, w.Header(), true)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
cr.Channel.Mux.Lock() // To prevent runtime error because we use 'defer' to unlock mux
Expand Down
10 changes: 8 additions & 2 deletions hls/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func response(link string) (*http.Response, error) {
return nil, errors.New(link + " returned HTTP code " + strconv.Itoa(resp.StatusCode))
}

func addHeaders(from, to http.Header) {
func addHeaders(from, to http.Header, contentLength bool) {
for k, v := range from {
switch k {
case "Connection":
Expand All @@ -140,7 +140,13 @@ func addHeaders(from, to http.Header) {
case "Date":
to.Set("Date", strings.Join(v, "; "))
case "Content-Length":
to.Set("Content-Length", strings.Join(v, "; "))
// This is only useful for unaltered media files. It should not be copied for M3U8 requests because
// players will not attempt to receive more bytes from HTTP server than are set here, therefore some M3U8
// contents would not load. E.g. CURL would display error "curl: (18) transfer closed with 83 bytes remaining to read"
// if set for M3U8 requests.
if contentLength {
to.Set("Content-Length", strings.Join(v, "; "))
}
}
}
}

0 comments on commit cda0e3d

Please sign in to comment.