diff --git a/engine/media_streamer.go b/engine/media_streamer.go index 5cc9bed52..835908a0f 100644 --- a/engine/media_streamer.go +++ b/engine/media_streamer.go @@ -138,8 +138,11 @@ type Stream struct { func (s *Stream) Seekable() bool { return s.Seeker != nil } func (s *Stream) Duration() float32 { return s.mf.Duration } func (s *Stream) ContentType() string { return mime.TypeByExtension("." + s.format) } -func (s *Stream) Name() string { return s.mf.Path } +func (s *Stream) Name() string { return s.mf.Title + "." + s.format } func (s *Stream) ModTime() time.Time { return s.mf.UpdatedAt } +func (s *Stream) EstimatedContentLength() int { + return int(s.mf.Duration * float32(s.bitRate) / 8 * 1024) +} // TODO This function deserves some love (refactoring) func selectTranscodingOptions(ctx context.Context, ds model.DataStore, mf *model.MediaFile, reqFormat string, reqBitRate int) (format string, bitRate int) { diff --git a/server/subsonic/stream.go b/server/subsonic/stream.go index 6b991f0c2..7c4d4e532 100644 --- a/server/subsonic/stream.go +++ b/server/subsonic/stream.go @@ -26,6 +26,7 @@ func (c *StreamController) Stream(w http.ResponseWriter, r *http.Request) (*resp } maxBitRate := utils.ParamInt(r, "maxBitRate", 0) format := utils.ParamString(r, "format") + estimateContentLength := utils.ParamBool(r, "estimateContentLength", false) stream, err := c.streamer.NewStream(r.Context(), id, format, maxBitRate) if err != nil { @@ -46,6 +47,14 @@ func (c *StreamController) Stream(w http.ResponseWriter, r *http.Request) (*resp // 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 Client requests the estimated content-length, send it + if estimateContentLength { + length := strconv.Itoa(stream.EstimatedContentLength()) + log.Trace(r.Context(), "Estimated content-length", "contentLength", length) + w.Header().Set("Content-Length", length) + } + if c, err := io.Copy(w, stream); err != nil { log.Error(r.Context(), "Error sending transcoded file", "id", id, err) } else {