refactor: new transcoding engine. third (fourth?) time is a charm!
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package subsonic
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/deluan/navidrome/engine"
|
||||
"github.com/deluan/navidrome/log"
|
||||
"github.com/deluan/navidrome/server/subsonic/responses"
|
||||
"github.com/deluan/navidrome/utils"
|
||||
)
|
||||
@@ -24,15 +27,32 @@ func (c *StreamController) Stream(w http.ResponseWriter, r *http.Request) (*resp
|
||||
maxBitRate := utils.ParamInt(r, "maxBitRate", 0)
|
||||
format := utils.ParamString(r, "format")
|
||||
|
||||
fs, err := c.streamer.NewFileSystem(r.Context(), maxBitRate, format)
|
||||
stream, err := c.streamer.NewStream(r.Context(), id, maxBitRate, format)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
if err := stream.Close(); err != nil {
|
||||
log.Error("Error closing stream", "id", id, "file", stream.Name(), err)
|
||||
}
|
||||
}()
|
||||
|
||||
// To be able to use a http.FileSystem, we need to change the URL structure
|
||||
r.URL.Path = id
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("X-Content-Duration", strconv.FormatFloat(float64(stream.Duration()), 'G', -1, 32))
|
||||
|
||||
if stream.Seekable() {
|
||||
http.ServeContent(w, r, stream.Name(), stream.ModTime(), stream)
|
||||
} else {
|
||||
// If the stream doesn't provide a size (i.e. is not seekable), we can't support ranges/content-length
|
||||
w.Header().Set("Accept-Ranges", "none")
|
||||
w.Header().Set("Content-Type", stream.ContentType())
|
||||
if c, err := io.Copy(w, stream); err != nil {
|
||||
log.Error(r.Context(), "Error sending transcoded file", "id", id, err)
|
||||
} else {
|
||||
log.Trace(r.Context(), "Success sending transcode file", "id", id, "size", c)
|
||||
}
|
||||
}
|
||||
|
||||
http.FileServer(fs).ServeHTTP(w, r)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -42,14 +62,11 @@ func (c *StreamController) Download(w http.ResponseWriter, r *http.Request) (*re
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fs, err := c.streamer.NewFileSystem(r.Context(), 0, "raw")
|
||||
stream, err := c.streamer.NewStream(r.Context(), id, 0, "raw")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// To be able to use a http.FileSystem, we need to change the URL structure
|
||||
r.URL.Path = id
|
||||
|
||||
http.FileServer(fs).ServeHTTP(w, r)
|
||||
http.ServeContent(w, r, stream.Name(), stream.ModTime(), stream)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user