mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-19 15:23:36 +00:00
* fix(stream): cap DSD transcoded to lossless PCM at 88.2kHz DSD64 was sent as 352.8kHz FLAC/WAV, which many clients cannot play. Client and codec profile limits below 88.2kHz still apply. ffprobe already reports DSD at rate/8 with 8 bits per sample, so only rates read from tags are divided, and any DSD depth maps to 24-bit PCM. Probed files used to come out at 44.1kHz with a bit depth of 8. Fixes #6288 Co-authored-by: Olli Sulopuisto <1051255+ollisulopuisto@users.noreply.github.com> Signed-off-by: Junker der Provinz <133605895+junkerderprovinz@users.noreply.github.com> * fix(stream): let a client's sample rate limit replace the DSD cap The 88.2kHz cap on DSD transcoded to lossless PCM is meant for clients that say nothing about sample rates, like the legacy stream endpoint. Applied unconditionally, it also overrode clients that declared their own audioSamplerate limit: a client accepting up to 192kHz got 88.2kHz, and one accepting only 96kHz or 192kHz got no compatible profile at all. The cap now applies only when the client has no audioSamplerate limitation for the target codec. Otherwise the client's limit decides the rate. * refactor(stream): share the audio codec profile matcher Direct play checks, transcode limitations and the new DSD cap check each looped over the client's codec profiles with the same audio-type and codec alias filter. They now use one audioCodecProfiles iterator, so the DSD cap decision cannot drift from the limitations that are actually applied. targetSampleRate takes a single capDSD flag instead of two adjacent bools. * test(stream): table-drive the DSD to FLAC sample rate specs Six specs repeated the same DSD source and FLAC transcoding profile and differed only in the source rate, the client's codec profiles and the expected rate. A DescribeTable lists the cap policy case by case, and every entry now also checks the 24-bit depth. --------- Signed-off-by: Junker der Provinz <133605895+junkerderprovinz@users.noreply.github.com> Co-authored-by: Olli Sulopuisto <1051255+ollisulopuisto@users.noreply.github.com> Co-authored-by: Deluan Quintão <deluan@navidrome.org>
195 lines
5.4 KiB
Go
195 lines
5.4 KiB
Go
package stream
|
|
|
|
import (
|
|
"iter"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// adjustResult represents the outcome of applying a limitation to a transcoded stream value
|
|
type adjustResult int
|
|
|
|
const (
|
|
adjustNone adjustResult = iota // Value already satisfies the limitation
|
|
adjustAdjusted // Value was changed to fit the limitation
|
|
adjustCannotFit // Cannot satisfy the limitation (reject this profile)
|
|
)
|
|
|
|
// checkLimitations checks codec profile limitations against source stream details.
|
|
// Returns "" if all limitations pass, or a typed reason string for the first failure.
|
|
func checkLimitations(src *Details, limitations []Limitation) string {
|
|
for _, lim := range limitations {
|
|
var ok bool
|
|
var reason string
|
|
|
|
switch lim.Name {
|
|
case LimitationAudioChannels:
|
|
ok = checkIntLimitation(src.Channels, lim.Comparison, lim.Values)
|
|
reason = "audio channels not supported"
|
|
case LimitationAudioSamplerate:
|
|
ok = checkIntLimitation(src.SampleRate, lim.Comparison, lim.Values)
|
|
reason = "audio samplerate not supported"
|
|
case LimitationAudioBitrate:
|
|
ok = checkIntLimitation(src.Bitrate, lim.Comparison, lim.Values)
|
|
reason = "audio bitrate not supported"
|
|
case LimitationAudioBitdepth:
|
|
ok = checkIntLimitation(src.BitDepth, lim.Comparison, lim.Values)
|
|
reason = "audio bitdepth not supported"
|
|
case LimitationAudioProfile:
|
|
ok = checkStringLimitation(src.Profile, lim.Comparison, lim.Values)
|
|
reason = "audio profile not supported"
|
|
default:
|
|
continue
|
|
}
|
|
|
|
if !ok && lim.Required {
|
|
return reason
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func audioCodecProfiles(clientInfo *ClientInfo, codec string) iter.Seq[CodecProfile] {
|
|
return func(yield func(CodecProfile) bool) {
|
|
for _, cp := range clientInfo.CodecProfiles {
|
|
if strings.EqualFold(cp.Type, CodecProfileTypeAudio) && matchesCodec(codec, []string{cp.Name}) {
|
|
if !yield(cp) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func hasCodecLimitation(clientInfo *ClientInfo, codec, name string) bool {
|
|
for cp := range audioCodecProfiles(clientInfo, codec) {
|
|
if slices.ContainsFunc(cp.Limitations, func(lim Limitation) bool { return strings.EqualFold(lim.Name, name) }) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// applyLimitation adjusts a transcoded stream parameter to satisfy the limitation.
|
|
// Returns the adjustment result.
|
|
func applyLimitation(sourceBitrate int, lim *Limitation, ts *Details) adjustResult {
|
|
switch lim.Name {
|
|
case LimitationAudioChannels:
|
|
return applyIntLimitation(lim.Comparison, lim.Values, ts.Channels, func(v int) { ts.Channels = v })
|
|
case LimitationAudioBitrate:
|
|
current := ts.Bitrate
|
|
if current == 0 {
|
|
current = sourceBitrate
|
|
}
|
|
return applyIntLimitation(lim.Comparison, lim.Values, current, func(v int) { ts.Bitrate = v })
|
|
case LimitationAudioSamplerate:
|
|
return applyIntLimitation(lim.Comparison, lim.Values, ts.SampleRate, func(v int) { ts.SampleRate = v })
|
|
case LimitationAudioBitdepth:
|
|
if ts.BitDepth > 0 {
|
|
return applyIntLimitation(lim.Comparison, lim.Values, ts.BitDepth, func(v int) { ts.BitDepth = v })
|
|
}
|
|
case LimitationAudioProfile:
|
|
// TODO: implement when audio profile data is available
|
|
}
|
|
return adjustNone
|
|
}
|
|
|
|
// applyIntLimitation applies a limitation comparison to a value.
|
|
// If the value needs adjusting, calls the setter and returns the result.
|
|
func applyIntLimitation(comparison string, values []string, current int, setter func(int)) adjustResult {
|
|
if len(values) == 0 {
|
|
return adjustNone
|
|
}
|
|
|
|
switch comparison {
|
|
case ComparisonLessThanEqual:
|
|
limit, ok := parseInt(values[0])
|
|
if !ok {
|
|
return adjustNone
|
|
}
|
|
if current <= limit {
|
|
return adjustNone
|
|
}
|
|
setter(limit)
|
|
return adjustAdjusted
|
|
case ComparisonGreaterThanEqual:
|
|
limit, ok := parseInt(values[0])
|
|
if !ok {
|
|
return adjustNone
|
|
}
|
|
if current >= limit {
|
|
return adjustNone
|
|
}
|
|
// Cannot upscale
|
|
return adjustCannotFit
|
|
case ComparisonEquals:
|
|
// Check if current value matches any allowed value
|
|
for _, v := range values {
|
|
if limit, ok := parseInt(v); ok && current == limit {
|
|
return adjustNone
|
|
}
|
|
}
|
|
// Find the closest allowed value below current (don't upscale)
|
|
var closest int
|
|
found := false
|
|
for _, v := range values {
|
|
if limit, ok := parseInt(v); ok && limit < current {
|
|
if !found || limit > closest {
|
|
closest = limit
|
|
found = true
|
|
}
|
|
}
|
|
}
|
|
if found {
|
|
setter(closest)
|
|
return adjustAdjusted
|
|
}
|
|
return adjustCannotFit
|
|
case ComparisonNotEquals:
|
|
for _, v := range values {
|
|
if limit, ok := parseInt(v); ok && current == limit {
|
|
return adjustCannotFit
|
|
}
|
|
}
|
|
return adjustNone
|
|
}
|
|
|
|
return adjustNone
|
|
}
|
|
|
|
func checkIntLimitation(value int, comparison string, values []string) bool {
|
|
return applyIntLimitation(comparison, values, value, func(int) {}) == adjustNone
|
|
}
|
|
|
|
// checkStringLimitation checks a string value against a limitation.
|
|
// Only Equals and NotEquals comparisons are meaningful for strings.
|
|
// LessThanEqual/GreaterThanEqual are not applicable and always pass.
|
|
func checkStringLimitation(value string, comparison string, values []string) bool {
|
|
switch comparison {
|
|
case ComparisonEquals:
|
|
for _, v := range values {
|
|
if strings.EqualFold(value, v) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
case ComparisonNotEquals:
|
|
for _, v := range values {
|
|
if strings.EqualFold(value, v) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
return true
|
|
}
|
|
|
|
func parseInt(s string) (int, bool) {
|
|
v, err := strconv.Atoi(s)
|
|
if err != nil || v < 0 {
|
|
return 0, false
|
|
}
|
|
return v, true
|
|
}
|