diff --git a/server/ctrlsubsonic/handlers_raw.go b/server/ctrlsubsonic/handlers_raw.go index c68fd2e..88bc78e 100644 --- a/server/ctrlsubsonic/handlers_raw.go +++ b/server/ctrlsubsonic/handlers_raw.go @@ -168,6 +168,7 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R } encodeOptions := encode.Options{ TrackPath: trackPath, + TrackBitrate: track.Bitrate, CachePath: c.CachePath, ProfileName: pref.Profile, PreferredBitrate: params.GetOrInt("maxBitRate", 0), diff --git a/server/encode/encode.go b/server/encode/encode.go index ccb4d2c..1b5fc3c 100644 --- a/server/encode/encode.go +++ b/server/encode/encode.go @@ -146,14 +146,6 @@ func cacheKey(sourcePath string, profileName string, profile Profile) string { ) } -// getBitrate checks if the client forces bitrate lower than set in profile -func getBitrate(preferred, defined int) int { - if preferred != 0 && preferred < defined { - return preferred - } - return defined -} - type ( OnInvalidProfileFunc func() error OnCacheHitFunc func(Profile, string) error @@ -162,6 +154,7 @@ type ( type Options struct { TrackPath string + TrackBitrate int CachePath string ProfileName string PreferredBitrate int @@ -175,7 +168,20 @@ func Encode(opts Options) error { if !ok { return opts.OnInvalidProfile() } - profile.Bitrate = getBitrate(opts.PreferredBitrate, profile.Bitrate) + log.Printf("client requests %dk, transcoding profile %dk, track bitrate %dk \n", opts.PreferredBitrate, profile.Bitrate, opts.TrackBitrate) + if opts.PreferredBitrate != 0 && opts.PreferredBitrate >= opts.TrackBitrate { + log.Printf("Not transcoding, requested bitrate larger or equal to track bitrate \n") + return opts.OnInvalidProfile() + } else if opts.PreferredBitrate != 0 && opts.PreferredBitrate < opts.TrackBitrate { + profile.Bitrate = opts.PreferredBitrate + log.Printf("Transcoding according to client request of %dk \n", profile.Bitrate) + } else if opts.PreferredBitrate == 0 && profile.Bitrate >= opts.TrackBitrate { + log.Printf("Not transcoding, transcoding profile bitrate larger or equal to track bitrate \n") + return opts.OnInvalidProfile() + } else { + log.Printf("Transcoding according to transcoding profile of %dk \n", profile.Bitrate) + } + cacheKey := cacheKey(opts.TrackPath, opts.ProfileName, profile) cachePath := path.Join(opts.CachePath, cacheKey) if fileExists(cachePath) {