From cc437c7cfeead79e0cf887c56d7928b8801b56f2 Mon Sep 17 00:00:00 2001 From: emeric Date: Mon, 15 Apr 2019 13:08:57 +0200 Subject: [PATCH] API Subsonic: respect the user's bitrate/max bitrate in the stream request --- src/api/subsonic/SubsonicResource.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/api/subsonic/SubsonicResource.cpp b/src/api/subsonic/SubsonicResource.cpp index d943f206..637a1741 100644 --- a/src/api/subsonic/SubsonicResource.cpp +++ b/src/api/subsonic/SubsonicResource.cpp @@ -1281,18 +1281,22 @@ createTranscoder(RequestContext& context) Id id {getMandatoryParameterAs(context.parameters, "id")}; // Optional params - std::size_t maxBitRate {getParameterAs(context.parameters, "maxBitRate").get_value_or(128)}; - - // "If set to zero, no limit is imposed" - if (maxBitRate == 0) - maxBitRate = 128; - - maxBitRate = clamp(maxBitRate, std::size_t {48}, std::size_t {320}); + boost::optional maxBitRate {getParameterAs(context.parameters, "maxBitRate")}; boost::filesystem::path trackPath; { Wt::Dbo::Transaction transaction {context.db.getSession()}; + Database::User::pointer user {context.db.getUser(context.userName)}; + if (!user) + throw Error {Error::Code::RequestedDataNotFound}; + + // "If set to zero, no limit is imposed" + if (!maxBitRate || *maxBitRate == 0) + maxBitRate = user->getAudioBitrate() / 1000; + + *maxBitRate = clamp(*maxBitRate, std::size_t {48}, user->getMaxAudioBitrate() / 1000); + auto track {Database::Track::getById(context.db.getSession(), id.value)}; if (!track) throw Error {Error::Code::RequestedDataNotFound}; @@ -1303,7 +1307,7 @@ createTranscoder(RequestContext& context) Av::TranscodeParameters parameters {}; parameters.stripMetadata = false; // Since it can be cached and some players read the metadata from the downloaded file - parameters.bitrate = maxBitRate * 1000; + parameters.bitrate = *maxBitRate * 1000; parameters.encoding = Av::Encoding::MP3; return std::make_shared(trackPath, parameters);