From f394de664a0e6f5d8a3133f52818dbfd19fb5e46 Mon Sep 17 00:00:00 2001 From: Deluan <deluan@deluan.com> Date: Mon, 24 Feb 2020 13:31:05 -0500 Subject: [PATCH] refactor: new transcoding engine. third (fourth?) time is a charm! --- engine/common.go | 2 +- engine/media_streamer.go | 229 +++++++++--------- engine/media_streamer_test.go | 72 ++++-- go.mod | 2 +- go.sum | 4 +- server/subsonic/helpers.go | 1 + ...ses AlbumList with data should match .JSON | 2 +- ...nses AlbumList with data should match .XML | 2 +- ...sponses Child with data should match .JSON | 2 +- ...esponses Child with data should match .XML | 2 +- ...ses Directory with data should match .JSON | 2 +- ...nses Directory with data should match .XML | 2 +- server/subsonic/responses/responses.go | 2 +- server/subsonic/stream.go | 35 ++- 14 files changed, 201 insertions(+), 158 deletions(-) diff --git a/engine/common.go b/engine/common.go index 257f37475..24e09dcb8 100644 --- a/engine/common.go +++ b/engine/common.go @@ -105,7 +105,7 @@ func FromMediaFile(mf *model.MediaFile) Entry { e.Created = mf.CreatedAt e.AlbumId = mf.AlbumID e.ArtistId = mf.ArtistID - e.Type = "music" // TODO Hardcoded for now + e.Type = "music" e.PlayCount = int32(mf.PlayCount) e.Starred = mf.StarredAt e.UserRating = mf.Rating diff --git a/engine/media_streamer.go b/engine/media_streamer.go index ae0ad40b6..b804e5935 100644 --- a/engine/media_streamer.go +++ b/engine/media_streamer.go @@ -4,10 +4,9 @@ import ( "context" "fmt" "io" - "net/http" + "mime" "os" "path/filepath" - "strings" "time" "github.com/deluan/navidrome/conf" @@ -16,11 +15,11 @@ import ( "github.com/deluan/navidrome/log" "github.com/deluan/navidrome/model" "github.com/deluan/navidrome/utils" - "gopkg.in/djherbis/fscache.v0" + "github.com/djherbis/fscache" ) type MediaStreamer interface { - NewFileSystem(ctx context.Context, maxBitRate int, format string) (http.FileSystem, error) + NewStream(ctx context.Context, id string, maxBitRate int, format string) (*Stream, error) } func NewMediaStreamer(ds model.DataStore, ffm ffmpeg.FFmpeg, cache fscache.Cache) MediaStreamer { @@ -33,30 +32,117 @@ type mediaStreamer struct { cache fscache.Cache } -func (ms *mediaStreamer) NewFileSystem(ctx context.Context, maxBitRate int, format string) (http.FileSystem, error) { - return &mediaFileSystem{ctx: ctx, ds: ms.ds, ffm: ms.ffm, cache: ms.cache, maxBitRate: maxBitRate, format: format}, nil +func (ms *mediaStreamer) NewStream(ctx context.Context, id string, maxBitRate int, reqFormat string) (*Stream, error) { + mf, err := ms.ds.MediaFile(ctx).Get(id) + if err != nil { + return nil, err + } + + format, bitRate := selectTranscodingOptions(mf, maxBitRate, reqFormat) + s := &Stream{ctx: ctx, mf: mf, format: format, bitRate: bitRate} + + if format == "raw" { + log.Debug(ctx, "Streaming raw file", "id", mf.ID, "path", mf.Path, + "requestBitrate", maxBitRate, "requestFormat", reqFormat, + "originalBitrate", mf.BitRate, "originalFormat", mf.Suffix) + f, err := os.Open(mf.Path) + if err != nil { + return nil, err + } + s.Reader = f + s.Closer = f + s.Seeker = f + s.format = mf.Suffix + return s, nil + } + + key := cacheKey(id, bitRate, format) + r, w, err := ms.cache.Get(key) + if err != nil { + log.Error(ctx, "Error creating stream caching buffer", "id", mf.ID, err) + return nil, err + } + + // If this is a brand new transcoding request, not in the cache, start transcoding + if w != nil { + log.Trace(ctx, "Cache miss. Starting new transcoding session", "id", mf.ID) + out, err := ms.ffm.StartTranscoding(ctx, mf.Path, bitRate, format) + if err != nil { + log.Error(ctx, "Error starting transcoder", "id", mf.ID, err) + return nil, os.ErrInvalid + } + go copyAndClose(ctx, w, out)() + } + + // If it is in the cache, check if the stream is done being written. If so, return a ReaderSeeker + if w == nil { + size := getFinalCachedSize(r) + if size > 0 { + log.Debug(ctx, "Streaming cached file", "id", mf.ID, "path", mf.Path, + "requestBitrate", maxBitRate, "requestFormat", reqFormat, + "originalBitrate", mf.BitRate, "originalFormat", mf.Suffix, "size", size) + sr := io.NewSectionReader(r, 0, size) + s.Reader = sr + s.Closer = r + s.Seeker = sr + s.format = format + return s, nil + } + } + + log.Debug(ctx, "Streaming transcoded file", "id", mf.ID, "path", mf.Path, + "requestBitrate", maxBitRate, "requestFormat", reqFormat, + "originalBitrate", mf.BitRate, "originalFormat", mf.Suffix) + // All other cases, just return a ReadCloser, without Seek capabilities + s.Reader = r + s.Closer = r + s.format = format + return s, nil } -type mediaFileSystem struct { - ctx context.Context - ds model.DataStore - maxBitRate int - format string - ffm ffmpeg.FFmpeg - cache fscache.Cache +func copyAndClose(ctx context.Context, w io.WriteCloser, r io.ReadCloser) func() { + return func() { + _, err := io.Copy(w, r) + if err != nil { + log.Error(ctx, "Error copying data to cache", err) + } + err = r.Close() + if err != nil { + log.Error(ctx, "Error closing transcode output", err) + } + err = w.Close() + if err != nil { + log.Error(ctx, "Error closing cache", err) + } + } } -func (fs *mediaFileSystem) selectTranscodingOptions(mf *model.MediaFile) (string, int) { +type Stream struct { + ctx context.Context + mf *model.MediaFile + bitRate int + format string + io.Reader + io.Closer + io.Seeker +} + +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) ModTime() time.Time { return s.mf.UpdatedAt } + +func selectTranscodingOptions(mf *model.MediaFile, maxBitRate int, format string) (string, int) { var bitRate int - var format string - if fs.format == "raw" || !conf.Server.EnableDownsampling { + if format == "raw" || !conf.Server.EnableDownsampling { return "raw", bitRate } else { - if fs.maxBitRate == 0 { + if maxBitRate == 0 { bitRate = mf.BitRate } else { - bitRate = utils.MinInt(mf.BitRate, fs.maxBitRate) + bitRate = utils.MinInt(mf.BitRate, maxBitRate) } format = "mp3" //mf.Suffix } @@ -70,110 +156,21 @@ func (fs *mediaFileSystem) selectTranscodingOptions(mf *model.MediaFile) (string return format, bitRate } -func (fs *mediaFileSystem) Open(name string) (http.File, error) { - id := strings.Trim(name, "/") - mf, err := fs.ds.MediaFile(fs.ctx).Get(id) - if err == model.ErrNotFound { - return nil, os.ErrNotExist - } - if err != nil { - log.Error("Error opening mediaFile", "id", id, err) - return nil, os.ErrInvalid - } - - format, bitRate := fs.selectTranscodingOptions(mf) - if format == "raw" { - log.Debug(fs.ctx, "Streaming raw file", "id", mf.ID, "path", mf.Path, - "requestBitrate", bitRate, "requestFormat", format, - "originalBitrate", mf.BitRate, "originalFormat", mf.Suffix) - return os.Open(mf.Path) - } - - log.Debug(fs.ctx, "Streaming transcoded file", "id", mf.ID, "path", mf.Path, - "requestBitrate", bitRate, "requestFormat", format, - "originalBitrate", mf.BitRate, "originalFormat", mf.Suffix) - - return fs.transcodeFile(mf, bitRate, format) +func cacheKey(id string, bitRate int, format string) string { + return fmt.Sprintf("%s.%d.%s", id, bitRate, format) } -func (fs *mediaFileSystem) transcodeFile(mf *model.MediaFile, bitRate int, format string) (*transcodingFile, error) { - key := fmt.Sprintf("%s.%d.%s", mf.ID, bitRate, format) - r, w, err := fs.cache.Get(key) - if err != nil { - log.Error("Error creating stream caching buffer", "id", mf.ID, err) - return nil, os.ErrInvalid - } - - // If it is a new file (not found in the cached), start a new transcoding session - if w != nil { - log.Debug("File not found in cache. Starting new transcoding session", "id", mf.ID) - out, err := fs.ffm.StartTranscoding(fs.ctx, mf.Path, bitRate, format) - if err != nil { - log.Error("Error starting transcoder", "id", mf.ID, err) - return nil, os.ErrInvalid +func getFinalCachedSize(r fscache.ReadAtCloser) int64 { + cr, ok := r.(*fscache.CacheReader) + if ok { + size, final, err := cr.Size() + if final && err == nil { + return size } - go func() { - io.Copy(w, out) - out.Close() - w.Close() - }() - } else { - log.Debug("Reading transcoded file from cache", "id", mf.ID) } - - return newTranscodingFile(fs.ctx, r, mf, bitRate), nil + return -1 } -// transcodingFile Implements http.File interface, required for the FileSystem. It needs a Closer, a Reader and -// a Seeker for the same stream. Because the fscache package only provides a ReaderAtCloser (without the Seek() -// method), we wrap that reader with a SectionReader, which provides a Seek(). But we still need the original -// reader, as we need to close the stream when the transfer is complete -func newTranscodingFile(ctx context.Context, reader fscache.ReadAtCloser, - mf *model.MediaFile, bitRate int) *transcodingFile { - - size := int64(mf.Duration*float32(bitRate*1000)) / 8 - return &transcodingFile{ - ctx: ctx, - mf: mf, - bitRate: bitRate, - size: size, - closer: reader, - ReadSeeker: io.NewSectionReader(reader, 0, size), - } -} - -type transcodingFile struct { - ctx context.Context - mf *model.MediaFile - bitRate int - size int64 - closer io.Closer - io.ReadSeeker -} - -func (tf *transcodingFile) Stat() (os.FileInfo, error) { - return &streamHandlerFileInfo{f: tf}, nil -} - -func (tf *transcodingFile) Close() error { - return tf.closer.Close() -} - -func (tf *transcodingFile) Readdir(count int) ([]os.FileInfo, error) { - return nil, nil -} - -type streamHandlerFileInfo struct { - f *transcodingFile -} - -func (fi *streamHandlerFileInfo) Name() string { return fi.f.mf.Title } -func (fi *streamHandlerFileInfo) ModTime() time.Time { return fi.f.mf.UpdatedAt } -func (fi *streamHandlerFileInfo) Size() int64 { return fi.f.size } -func (fi *streamHandlerFileInfo) Mode() os.FileMode { return os.FileMode(0777) } -func (fi *streamHandlerFileInfo) IsDir() bool { return false } -func (fi *streamHandlerFileInfo) Sys() interface{} { return nil } - func NewTranscodingCache() (fscache.Cache, error) { lru := fscache.NewLRUHaunter(0, conf.Server.MaxTranscodingCacheSize, 10*time.Minute) h := fscache.NewLRUHaunterStrategy(lru) diff --git a/engine/media_streamer_test.go b/engine/media_streamer_test.go index 81d8a02f4..5dc6d1303 100644 --- a/engine/media_streamer_test.go +++ b/engine/media_streamer_test.go @@ -4,7 +4,6 @@ import ( "context" "io" "io/ioutil" - "net/http" "os" "strings" @@ -12,53 +11,82 @@ import ( "github.com/deluan/navidrome/log" "github.com/deluan/navidrome/model" "github.com/deluan/navidrome/persistence" + "github.com/djherbis/fscache" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "gopkg.in/djherbis/fscache.v0" ) var _ = Describe("MediaStreamer", func() { - var streamer MediaStreamer var ds model.DataStore + var cache fscache.Cache + var tempDir string + ffmpeg := &fakeFFmpeg{} ctx := log.NewContext(nil) + BeforeSuite(func() { + tempDir, _ = ioutil.TempDir("", "stream_tests") + fs, _ := fscache.NewFs(tempDir, 0755) + cache, _ = fscache.NewCache(fs, nil) + }) + BeforeEach(func() { conf.Server.EnableDownsampling = true - fs := fscache.NewMemFs() - cache, _ := fscache.NewCache(fs, nil) ds = &persistence.MockDataStore{} - ds.MediaFile(ctx).(*persistence.MockMediaFile).SetData(`[{"id": "123", "path": "tests/fixtures/test.mp3", "bitRate": 128}]`, 1) - streamer = NewMediaStreamer(ds, &fakeFFmpeg{}, cache) + ds.MediaFile(ctx).(*persistence.MockMediaFile).SetData(`[{"id": "123", "path": "tests/fixtures/test.mp3", "bitRate": 128, "duration": 257.0}]`, 1) + streamer = NewMediaStreamer(ds, ffmpeg, cache) }) - getFile := func(id string, maxBitRate int, format string) (http.File, error) { - fs, _ := streamer.NewFileSystem(ctx, maxBitRate, format) - return fs.Open(id) - } + AfterSuite(func() { + os.RemoveAll(tempDir) + }) Context("NewFileSystem", func() { - It("returns a File if format is 'raw'", func() { - Expect(getFile("123", 0, "raw")).To(BeAssignableToTypeOf(&os.File{})) + It("returns a seekable stream if format is 'raw'", func() { + s, err := streamer.NewStream(ctx, "123", 0, "raw") + Expect(err).ToNot(HaveOccurred()) + Expect(s.Seekable()).To(BeTrue()) }) - It("returns a File if maxBitRate is 0", func() { - Expect(getFile("123", 0, "mp3")).To(BeAssignableToTypeOf(&os.File{})) + It("returns a seekable stream if maxBitRate is 0", func() { + s, err := streamer.NewStream(ctx, "123", 0, "mp3") + Expect(err).ToNot(HaveOccurred()) + Expect(s.Seekable()).To(BeTrue()) }) - It("returns a File if maxBitRate is higher than file bitRate", func() { - Expect(getFile("123", 256, "mp3")).To(BeAssignableToTypeOf(&os.File{})) + It("returns a seekable stream if maxBitRate is higher than file bitRate", func() { + s, err := streamer.NewStream(ctx, "123", 320, "mp3") + Expect(err).ToNot(HaveOccurred()) + Expect(s.Seekable()).To(BeTrue()) }) - It("returns a transcodingFile if maxBitRate is lower than file bitRate", func() { - s, err := getFile("123", 64, "mp3") + It("returns a NON seekable stream if transcode is required", func() { + s, err := streamer.NewStream(ctx, "123", 64, "mp3") Expect(err).To(BeNil()) - Expect(s).To(BeAssignableToTypeOf(&transcodingFile{})) - Expect(s.(*transcodingFile).bitRate).To(Equal(64)) + Expect(s.Seekable()).To(BeFalse()) + Expect(s.Duration()).To(Equal(float32(257.0))) + }) + It("returns a seekable stream if the file is complete in the cache", func() { + Eventually(func() bool { return ffmpeg.closed }).Should(BeTrue()) + s, err := streamer.NewStream(ctx, "123", 64, "mp3") + Expect(err).To(BeNil()) + Expect(s.Seekable()).To(BeTrue()) }) }) }) type fakeFFmpeg struct { + r io.Reader + closed bool } func (ff *fakeFFmpeg) StartTranscoding(ctx context.Context, path string, maxBitRate int, format string) (f io.ReadCloser, err error) { - return ioutil.NopCloser(strings.NewReader("fake data")), nil + ff.r = strings.NewReader("fake data") + return ff, nil +} + +func (ff *fakeFFmpeg) Read(p []byte) (n int, err error) { + return ff.r.Read(p) +} + +func (ff *fakeFFmpeg) Close() error { + ff.closed = true + return nil } diff --git a/go.mod b/go.mod index e5c5761c2..e89866c02 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/deluan/rest v0.0.0-20200114062534-0653ffe9eab4 github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/dhowden/tag v0.0.0-20191122115059-7e5c04feccd8 + github.com/djherbis/fscache v0.10.0 github.com/fatih/camelcase v0.0.0-20160318181535-f6a740d52f96 // indirect github.com/fatih/structs v1.0.0 // indirect github.com/go-chi/chi v4.0.3+incompatible @@ -39,7 +40,6 @@ require ( golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/djherbis/atime.v1 v1.0.0 // indirect - gopkg.in/djherbis/fscache.v0 v0.9.0 gopkg.in/djherbis/stream.v1 v1.2.0 // indirect gopkg.in/yaml.v2 v2.2.8 // indirect ) diff --git a/go.sum b/go.sum index ea4ab638a..5264ceb84 100644 --- a/go.sum +++ b/go.sum @@ -28,6 +28,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dhowden/tag v0.0.0-20191122115059-7e5c04feccd8 h1:nmFnmD8VZXkjDHIb1Gnfz50cgzUvGN72zLjPRXBW/hU= github.com/dhowden/tag v0.0.0-20191122115059-7e5c04feccd8/go.mod h1:SniNVYuaD1jmdEEvi+7ywb1QFR7agjeTdGKyFb0p7Rw= +github.com/djherbis/fscache v0.10.0 h1:+O3s3LwKL1Jfz7txRHpgKOz8/krh9w+NzfzxtFdbsQg= +github.com/djherbis/fscache v0.10.0/go.mod h1:yyPYtkNnnPXsW+81lAcQS6yab3G2CRfnPLotBvtbf0c= github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712 h1:aaQcKT9WumO6JEJcRyTqFVq4XUZiUcKR2/GI31TOcz8= github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4= @@ -174,8 +176,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/djherbis/atime.v1 v1.0.0 h1:eMRqB/JrLKocla2PBPKgQYg/p5UG4L6AUAs92aP7F60= gopkg.in/djherbis/atime.v1 v1.0.0/go.mod h1:hQIUStKmJfvf7xdh/wtK84qe+DsTV5LnA9lzxxtPpJ8= -gopkg.in/djherbis/fscache.v0 v0.9.0 h1:CBmOlHQKg99q0xATpQpSNAR970UN4vECB5SjzkuyLe0= -gopkg.in/djherbis/fscache.v0 v0.9.0/go.mod h1:izqJMuO+STCEMBEGFiwW5zPlamuiUOxMRpNzHT5cQHc= gopkg.in/djherbis/stream.v1 v1.2.0 h1:3tZuXO+RK8opjw8/BJr780h+eAPwOFfLHCKRKyYxk3s= gopkg.in/djherbis/stream.v1 v1.2.0/go.mod h1:aEV8CBVRmSpLamVJfM903Npic1IKmb2qS30VAZ+sssg= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= diff --git a/server/subsonic/helpers.go b/server/subsonic/helpers.go index 43136fccd..b9329b1eb 100644 --- a/server/subsonic/helpers.go +++ b/server/subsonic/helpers.go @@ -133,6 +133,7 @@ func ToChild(entry engine.Entry) responses.Child { child.AlbumId = entry.AlbumId child.ArtistId = entry.ArtistId child.Type = entry.Type + child.IsVideo = false child.UserRating = entry.UserRating child.SongCount = entry.SongCount // TODO Must be dynamic, based on player/transcoding config diff --git a/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses AlbumList with data should match .JSON b/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses AlbumList with data should match .JSON index 917958781..cf751c278 100644 --- a/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses AlbumList with data should match .JSON +++ b/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses AlbumList with data should match .JSON @@ -1 +1 @@ -{"status":"ok","version":"1.8.0","type":"navidrome","serverVersion":"v0.0.0","albumList":{"album":[{"id":"1","isDir":false,"title":"title"}]}} +{"status":"ok","version":"1.8.0","type":"navidrome","serverVersion":"v0.0.0","albumList":{"album":[{"id":"1","isDir":false,"title":"title","isVideo":false}]}} diff --git a/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses AlbumList with data should match .XML b/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses AlbumList with data should match .XML index b2a841784..7ccca0231 100644 --- a/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses AlbumList with data should match .XML +++ b/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses AlbumList with data should match .XML @@ -1 +1 @@ -<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0" type="navidrome" serverVersion="v0.0.0"><albumList><album id="1" isDir="false" title="title"></album></albumList></subsonic-response> +<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0" type="navidrome" serverVersion="v0.0.0"><albumList><album id="1" isDir="false" title="title" isVideo="false"></album></albumList></subsonic-response> diff --git a/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Child with data should match .JSON b/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Child with data should match .JSON index ab135140e..fd8f29668 100644 --- a/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Child with data should match .JSON +++ b/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Child with data should match .JSON @@ -1 +1 @@ -{"status":"ok","version":"1.8.0","type":"navidrome","serverVersion":"v0.0.0","directory":{"child":[{"id":"1","isDir":true,"title":"title","album":"album","artist":"artist","track":1,"year":1985,"genre":"Rock","coverArt":"1","size":"8421341","contentType":"audio/flac","suffix":"flac","starred":"2016-03-02T20:30:00Z","transcodedContentType":"audio/mpeg","transcodedSuffix":"mp3","duration":146,"bitRate":320}],"id":"1","name":"N"}} +{"status":"ok","version":"1.8.0","type":"navidrome","serverVersion":"v0.0.0","directory":{"child":[{"id":"1","isDir":true,"title":"title","album":"album","artist":"artist","track":1,"year":1985,"genre":"Rock","coverArt":"1","size":"8421341","contentType":"audio/flac","suffix":"flac","starred":"2016-03-02T20:30:00Z","transcodedContentType":"audio/mpeg","transcodedSuffix":"mp3","duration":146,"bitRate":320,"isVideo":false}],"id":"1","name":"N"}} diff --git a/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Child with data should match .XML b/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Child with data should match .XML index a22088866..1eeeaac1b 100644 --- a/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Child with data should match .XML +++ b/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Child with data should match .XML @@ -1 +1 @@ -<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0" type="navidrome" serverVersion="v0.0.0"><directory id="1" name="N"><child id="1" isDir="true" title="title" album="album" artist="artist" track="1" year="1985" genre="Rock" coverArt="1" size="8421341" contentType="audio/flac" suffix="flac" starred="2016-03-02T20:30:00Z" transcodedContentType="audio/mpeg" transcodedSuffix="mp3" duration="146" bitRate="320"></child></directory></subsonic-response> +<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0" type="navidrome" serverVersion="v0.0.0"><directory id="1" name="N"><child id="1" isDir="true" title="title" album="album" artist="artist" track="1" year="1985" genre="Rock" coverArt="1" size="8421341" contentType="audio/flac" suffix="flac" starred="2016-03-02T20:30:00Z" transcodedContentType="audio/mpeg" transcodedSuffix="mp3" duration="146" bitRate="320" isVideo="false"></child></directory></subsonic-response> diff --git a/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Directory with data should match .JSON b/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Directory with data should match .JSON index f8efcb064..ad1d1c3d1 100644 --- a/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Directory with data should match .JSON +++ b/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Directory with data should match .JSON @@ -1 +1 @@ -{"status":"ok","version":"1.8.0","type":"navidrome","serverVersion":"v0.0.0","directory":{"child":[{"id":"1","isDir":false,"title":"title"}],"id":"1","name":"N"}} +{"status":"ok","version":"1.8.0","type":"navidrome","serverVersion":"v0.0.0","directory":{"child":[{"id":"1","isDir":false,"title":"title","isVideo":false}],"id":"1","name":"N"}} diff --git a/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Directory with data should match .XML b/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Directory with data should match .XML index 4ecfa0401..d161bfb2b 100644 --- a/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Directory with data should match .XML +++ b/server/subsonic/responses/.snapshots/responses-snapshotMatcher-Match-Responses Directory with data should match .XML @@ -1 +1 @@ -<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0" type="navidrome" serverVersion="v0.0.0"><directory id="1" name="N"><child id="1" isDir="false" title="title"></child></directory></subsonic-response> +<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0" type="navidrome" serverVersion="v0.0.0"><directory id="1" name="N"><child id="1" isDir="false" title="title" isVideo="false"></child></directory></subsonic-response> diff --git a/server/subsonic/responses/responses.go b/server/subsonic/responses/responses.go index 6ef81c8d9..dbfd9576a 100644 --- a/server/subsonic/responses/responses.go +++ b/server/subsonic/responses/responses.go @@ -112,8 +112,8 @@ type Child struct { Type string `xml:"type,attr,omitempty" json:"type,omitempty"` UserRating int `xml:"userRating,attr,omitempty" json:"userRating,omitempty"` SongCount int `xml:"songCount,attr,omitempty" json:"songCount,omitempty"` + IsVideo bool `xml:"isVideo,attr" json:"isVideo"` /* - <xs:attribute name="isVideo" type="xs:boolean" use="optional"/> <!-- Added in 1.4.1 --> <xs:attribute name="averageRating" type="sub:AverageRating" use="optional"/> <!-- Added in 1.6.0 --> <xs:attribute name="bookmarkPosition" type="xs:long" use="optional"/> <!-- In millis. Added in 1.10.1 --> <xs:attribute name="originalWidth" type="xs:int" use="optional"/> <!-- Added in 1.13.0 --> diff --git a/server/subsonic/stream.go b/server/subsonic/stream.go index 9a537fd42..d151f22ad 100644 --- a/server/subsonic/stream.go +++ b/server/subsonic/stream.go @@ -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 }