Skip to content

🎧 Your Personal Streaming Service

Add global Downsampling feature (#1575)

gauth-fr55ba39cb7989
diff --git a/conf/configuration.go b/conf/configuration.go
index f265f3bbe..0690bc469 100644
--- a/conf/configuration.go
+++ b/conf/configuration.go
@@ -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", "")
diff --git a/consts/consts.go b/consts/consts.go
index e22e4fa5e..e4a1cbdf5 100644
--- a/consts/consts.go
+++ b/consts/consts.go
@@ -80,7 +80,8 @@ const (
)
var (
- DefaultTranscodings = []map[string]interface{}{
+ DefaultDownsamplingFormat = "opus"
+ DefaultTranscodings = []map[string]interface{}{
{
"name": "mp3 audio",
"targetFormat": "mp3",
diff --git a/core/media_streamer.go b/core/media_streamer.go
index ec5e94fc7..c35afb734 100644
--- a/core/media_streamer.go
+++ b/core/media_streamer.go
@@ -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 {
diff --git a/core/media_streamer_test.go b/core/media_streamer_test.go
index 780a061d8..727f61fd5 100644
--- a/core/media_streamer_test.go
+++ b/core/media_streamer_test.go
@@ -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() {
diff --git a/tests/mock_transcoding_repo.go b/tests/mock_transcoding_repo.go
index c91e543c1..12db0d7be 100644
--- a/tests/mock_transcoding_repo.go
+++ b/tests/mock_transcoding_repo.go
@@ -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
}