Send estimated content length if requested
This commit is contained in:
@@ -138,8 +138,11 @@ type Stream struct {
|
|||||||
func (s *Stream) Seekable() bool { return s.Seeker != nil }
|
func (s *Stream) Seekable() bool { return s.Seeker != nil }
|
||||||
func (s *Stream) Duration() float32 { return s.mf.Duration }
|
func (s *Stream) Duration() float32 { return s.mf.Duration }
|
||||||
func (s *Stream) ContentType() string { return mime.TypeByExtension("." + s.format) }
|
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) 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)
|
// 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) {
|
func selectTranscodingOptions(ctx context.Context, ds model.DataStore, mf *model.MediaFile, reqFormat string, reqBitRate int) (format string, bitRate int) {
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ func (c *StreamController) Stream(w http.ResponseWriter, r *http.Request) (*resp
|
|||||||
}
|
}
|
||||||
maxBitRate := utils.ParamInt(r, "maxBitRate", 0)
|
maxBitRate := utils.ParamInt(r, "maxBitRate", 0)
|
||||||
format := utils.ParamString(r, "format")
|
format := utils.ParamString(r, "format")
|
||||||
|
estimateContentLength := utils.ParamBool(r, "estimateContentLength", false)
|
||||||
|
|
||||||
stream, err := c.streamer.NewStream(r.Context(), id, format, maxBitRate)
|
stream, err := c.streamer.NewStream(r.Context(), id, format, maxBitRate)
|
||||||
if err != nil {
|
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
|
// 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("Accept-Ranges", "none")
|
||||||
w.Header().Set("Content-Type", stream.ContentType())
|
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 {
|
if c, err := io.Copy(w, stream); err != nil {
|
||||||
log.Error(r.Context(), "Error sending transcoded file", "id", id, err)
|
log.Error(r.Context(), "Error sending transcoded file", "id", id, err)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user