Subsonic API: try to not transcode if the requested format/max bitrate is compatible with the actual file

This commit is contained in:
emeric
2023-10-15 20:49:34 +02:00
parent fbdc15bd5c
commit 6a3783a4ba
@@ -19,6 +19,7 @@
#include "MediaRetrieval.hpp" #include "MediaRetrieval.hpp"
#include "av/IAudioFile.hpp"
#include "av/TranscodeParameters.hpp" #include "av/TranscodeParameters.hpp"
#include "av/TranscodeResourceHandlerCreator.hpp" #include "av/TranscodeResourceHandlerCreator.hpp"
#include "av/Types.hpp" #include "av/Types.hpp"
@@ -99,8 +100,23 @@ namespace API::Subsonic
parameters.inputFileParameters.duration = track->getDuration(); parameters.inputFileParameters.duration = track->getDuration();
} }
if (format != "raw") // raw => no transcode if (format == "raw") // raw => no transcode
return parameters;
const auto audioFile{ Av::parseAudioFile(parameters.inputFileParameters.trackPath) };
// check if transcode is really needed or not
// same format as requested, bitrate is lower than requested => no need to transcode
if (const auto streamInfo{ audioFile->getBestStreamInfo() })
{ {
// assume reported codec is "mp3", "opus", "vorbis", etc.
if (StringUtils::stringCaseInsensitiveEqual(streamInfo->codec, format) && (maxBitRate == 0 || (streamInfo->bitrate / 1000) <= maxBitRate))
{
LMS_LOG(API_SUBSONIC, DEBUG) << "stream parameters are compatible with actual file: no transcode";
return parameters;
}
}
const User::pointer user{ User::find(context.dbSession, context.userId) }; const User::pointer user{ User::find(context.dbSession, context.userId) };
if (!user) if (!user)
throw UserNotAuthorizedError{}; throw UserNotAuthorizedError{};
@@ -117,8 +133,7 @@ namespace API::Subsonic
transcodeParameters.bitrate = user->getSubsonicDefaultTranscodeBitrate(); transcodeParameters.bitrate = user->getSubsonicDefaultTranscodeBitrate();
if (maxBitRate != 0) if (maxBitRate != 0)
transcodeParameters.bitrate = Utils::clamp(transcodeParameters.bitrate, std::size_t{ 48 }, maxBitRate); transcodeParameters.bitrate = Utils::clamp(transcodeParameters.bitrate, std::size_t{ 48000 }, maxBitRate * 1000);
}
return parameters; return parameters;
} }