Add global Downsampling feature (#1575)

* Add global downsampling feature

* Default to Opus &  consider player transcoder

* Add a test case for DefaultDownsamplingFormat

Co-authored-by: Deluan <deluan@navidrome.org>
This commit is contained in:
gauth-fr 2022-12-07 01:41:16 +01:00 committed by GitHub
parent 0cc1db54d4
commit 55ba39cb79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 1 deletions

View File

@ -67,6 +67,9 @@ type configOptions struct {
Spotify spotifyOptions
ListenBrainz listenBrainzOptions
//test downsampling
DefaultDownsamplingFormat string
// DevFlags. These are used to enable/disable debugging and incomplete features
DevLogSourceLine bool
DevLogLevels map[string]string
@ -268,6 +271,8 @@ func init() {
viper.SetDefault("listenbrainz.enabled", true)
viper.SetDefault("listenbrainz.baseurl", "https://api.listenbrainz.org/1/")
viper.SetDefault("defaultdownsamplingformat", consts.DefaultDownsamplingFormat)
// DevFlags. These are used to enable/disable debugging and incomplete features
viper.SetDefault("devlogsourceline", false)
viper.SetDefault("devautocreateadminpassword", "")

View File

@ -80,7 +80,8 @@ const (
)
var (
DefaultTranscodings = []map[string]interface{}{
DefaultDownsamplingFormat = "opus"
DefaultTranscodings = []map[string]interface{}{
{
"name": "mp3 audio",
"targetFormat": "mp3",

View File

@ -142,6 +142,10 @@ func selectTranscodingOptions(ctx context.Context, ds model.DataStore, mf *model
if p, ok := request.PlayerFrom(ctx); ok {
cBitRate = p.MaxBitRate
}
} else if reqBitRate > 0 && conf.Server.DefaultDownsamplingFormat != "" {
// If no format is specified and no transcoding associated to the player, but a bitrate is specfied, and there is no transcoding set for the player, we use the default downsampling format
log.Debug("Default Downsampling", "Using default downsampling format", conf.Server.DefaultDownsamplingFormat)
cFormat = conf.Server.DefaultDownsamplingFormat
}
}
if reqBitRate > 0 {

View File

@ -8,6 +8,7 @@ import (
"sync"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/conf/configtest"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/model/request"
@ -24,6 +25,7 @@ var _ = Describe("MediaStreamer", func() {
ctx := log.NewContext(context.TODO())
BeforeEach(func() {
DeferCleanup(configtest.SetupConfig())
conf.Server.DataFolder, _ = os.MkdirTemp("", "file_caches")
conf.Server.TranscodingCacheSize = "100MB"
ds = &tests.MockDataStore{MockedTranscoding: &tests.MockTranscodingRepo{}}
@ -115,6 +117,15 @@ var _ = Describe("MediaStreamer", func() {
Expect(format).To(Equal("raw"))
Expect(bitRate).To(Equal(320))
})
It("returns the DefaultDownsamplingFormat if a maxBitrate but not the format", func() {
conf.Server.DefaultDownsamplingFormat = "opus"
mf.Suffix = "FLAC"
mf.BitRate = 960
format, bitRate := selectTranscodingOptions(ctx, ds, mf, "", 128)
Expect(format).To(Equal("opus"))
Expect(bitRate).To(Equal(128))
})
})
Context("player has format configured", func() {

View File

@ -16,6 +16,8 @@ func (m *MockTranscodingRepo) FindByFormat(format string) (*model.Transcoding, e
return &model.Transcoding{ID: "mp31", TargetFormat: "mp3", DefaultBitRate: 160}, nil
case "oga":
return &model.Transcoding{ID: "oga1", TargetFormat: "oga", DefaultBitRate: 128}, nil
case "opus":
return &model.Transcoding{ID: "opus1", TargetFormat: "opus", DefaultBitRate: 96}, nil
default:
return nil, model.ErrNotFound
}