Added podcast support, only from subsonic API for now, ref #726

This commit is contained in:
emeric
2025-09-13 15:04:13 +02:00
parent 1d584ebcc6
commit 932e7715f5
111 changed files with 4717 additions and 188 deletions
@@ -19,31 +19,37 @@
#include "MediaRetrieval.hpp"
#include "av/Exception.hpp"
#include "av/IAudioFile.hpp"
#include <chrono>
#include "core/FileResourceHandlerCreator.hpp"
#include "core/ILogger.hpp"
#include "core/IResourceHandler.hpp"
#include "core/String.hpp"
#include "core/Utils.hpp"
#include "av/Exception.hpp"
#include "av/IAudioFile.hpp"
#include "database/Session.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "database/objects/PodcastEpisodeId.hpp"
#include "database/objects/Track.hpp"
#include "database/objects/TrackEmbeddedImageId.hpp"
#include "database/objects/TrackLyrics.hpp"
#include "database/objects/User.hpp"
#include "services/artwork/IArtworkService.hpp"
#include "services/podcast/IPodcastService.hpp"
#include "services/transcoding/ITranscodingService.hpp"
#include "CoverArtId.hpp"
#include "ParameterParsing.hpp"
#include "RequestContext.hpp"
#include "SubsonicId.hpp"
#include "SubsonicResponse.hpp"
#include "responses/Lyrics.hpp"
namespace lms::api::subsonic
{
using namespace db;
namespace
{
std::optional<transcoding::OutputFormat> subsonicStreamFormatToAvOutputFormat(std::string_view format)
@@ -100,8 +106,8 @@ namespace lms::api::subsonic
struct StreamParameters
{
transcoding::InputParameters inputParameters;
std::string inputMimeType; // set if known
std::optional<transcoding::OutputParameters> outputParameters;
std::filesystem::path trackPath;
bool estimateContentLength{};
};
@@ -125,10 +131,57 @@ namespace lms::api::subsonic
}
}
using AudioFileId = std::variant<db::TrackId, db::PodcastEpisodeId>;
struct AudioFileInfo
{
std::filesystem::path path;
std::chrono::milliseconds duration{};
std::size_t bitrate{};
std::string mimeType; // set if known
};
AudioFileInfo getAudioFileInfo(db::Session& session, AudioFileId audioFileId)
{
AudioFileInfo res;
auto transaction{ session.createReadTransaction() };
if (const db::TrackId * trackId{ std::get_if<db::TrackId>(&audioFileId) })
{
const db::Track::pointer track{ db::Track::find(session, *trackId) };
if (!track)
throw RequestedDataNotFoundError{};
res.path = track->getAbsoluteFilePath();
res.duration = track->getDuration();
res.bitrate = track->getBitrate();
}
else if (const db::PodcastEpisodeId * episodeId{ std::get_if<db::PodcastEpisodeId>(&audioFileId) })
{
const db::PodcastEpisode::pointer episode{ db::PodcastEpisode::find(session, *episodeId) };
if (!episode)
throw RequestedDataNotFoundError{};
std::filesystem::path podcastCachePath{ core::Service<podcast::IPodcastService>::get()->getCachePath() };
res.path = podcastCachePath / episode->getAudioRelativeFilePath();
res.duration = episode->getDuration();
res.bitrate = episode->getEnclosureLength() / std::chrono::duration_cast<std::chrono::seconds>(episode->getDuration()).count() * 8;
res.mimeType = episode->getEnclosureContentType();
}
return res;
}
StreamParameters getStreamParameters(RequestContext& context)
{
// Mandatory params
const TrackId id{ getMandatoryParameterAs<TrackId>(context.parameters, "id") };
const auto trackId{ getParameterAs<db::TrackId>(context.parameters, "id") };
const auto podcastEpisodeId{ getParameterAs<db::PodcastEpisodeId>(context.parameters, "id") };
if (!trackId && !podcastEpisodeId)
throw RequiredParameterMissingError{ "id" };
const AudioFileId audioId{ trackId ? AudioFileId{ *trackId } : AudioFileId{ *podcastEpisodeId } };
// Optional params
std::size_t maxBitRate{ getParameterAs<std::size_t>(context.parameters, "maxBitRate").value_or(0) * 1000 }; // "If set to zero, no limit is imposed", given in kpbs
@@ -136,21 +189,18 @@ namespace lms::api::subsonic
std::size_t timeOffset{ getParameterAs<std::size_t>(context.parameters, "timeOffset").value_or(0) };
bool estimateContentLength{ getParameterAs<bool>(context.parameters, "estimateContentLength").value_or(false) };
const AudioFileInfo audioFileInfo{ getAudioFileInfo(context.dbSession, audioId) };
StreamParameters parameters;
auto transaction{ context.dbSession.createReadTransaction() };
const auto track{ Track::find(context.dbSession, id) };
if (!track)
throw RequestedDataNotFoundError{};
parameters.inputParameters.trackId = id;
parameters.inputParameters.filePath = audioFileInfo.path;
parameters.inputParameters.duration = audioFileInfo.duration;
parameters.inputParameters.offset = std::chrono::seconds{ timeOffset };
parameters.inputMimeType = audioFileInfo.mimeType;
parameters.estimateContentLength = estimateContentLength;
parameters.trackPath = track->getAbsoluteFilePath();
if (format == "raw") // raw => no transcoding
return parameters;
if (format == "raw") // raw => no transcoding
return parameters; // TODO: what if offset is not 0?
std::optional<transcoding::OutputFormat> requestedFormat{ subsonicStreamFormatToAvOutputFormat(format) };
if (!requestedFormat)
@@ -159,7 +209,7 @@ namespace lms::api::subsonic
requestedFormat = userTranscodeFormatToAvFormat(context.user->getSubsonicDefaultTranscodingOutputFormat());
}
if (!requestedFormat && (maxBitRate == 0 || track->getBitrate() <= maxBitRate))
if (!requestedFormat && (maxBitRate == 0 || audioFileInfo.bitrate <= maxBitRate))
{
LMS_LOG(API_SUBSONIC, DEBUG, "File's bitrate is compatible with parameters => no transcoding");
return parameters; // no transcoding needed
@@ -169,9 +219,9 @@ namespace lms::api::subsonic
// same codec => apply max bitrate
// otherwise => apply default bitrate (because we can't really compare bitrates between formats) + max bitrate)
std::size_t bitrate{};
if (requestedFormat && isOutputFormatCompatible(track->getAbsoluteFilePath(), *requestedFormat))
if (requestedFormat && isOutputFormatCompatible(audioFileInfo.path, *requestedFormat))
{
if (maxBitRate == 0 || track->getBitrate() <= maxBitRate)
if (maxBitRate == 0 || audioFileInfo.bitrate <= maxBitRate)
{
LMS_LOG(API_SUBSONIC, DEBUG, "File's bitrate and format are compatible with parameters => no transcoding");
return parameters; // no transcoding needed
@@ -218,7 +268,7 @@ namespace lms::api::subsonic
// Choice: we return only the first lyrics if the track has many lyrics
db::TrackLyrics::FindParameters lyricsParams;
lyricsParams.setTrack(tracks.results[0]);
lyricsParams.setSortMethod(TrackLyricsSortMethod::ExternalFirst);
lyricsParams.setSortMethod(db::TrackLyricsSortMethod::ExternalFirst);
lyricsParams.setRange(db::Range{ 0, 1 });
db::TrackLyrics::find(context.dbSession, lyricsParams, [&](const db::TrackLyrics::pointer& lyrics) {
@@ -278,7 +328,7 @@ namespace lms::api::subsonic
{
auto transaction{ context.dbSession.createReadTransaction() };
auto track{ Track::find(context.dbSession, id) };
auto track{ db::Track::find(context.dbSession, id) };
if (!track)
throw RequestedDataNotFoundError{};
@@ -310,7 +360,7 @@ namespace lms::api::subsonic
if (streamParameters.outputParameters)
resourceHandler = core::Service<transcoding::ITranscodingService>::get()->createResourceHandler(streamParameters.inputParameters, *streamParameters.outputParameters, streamParameters.estimateContentLength);
else
resourceHandler = core::createFileResourceHandler(streamParameters.trackPath);
resourceHandler = core::createFileResourceHandler(streamParameters.inputParameters.filePath, streamParameters.inputMimeType);
}
else
{
@@ -323,6 +373,7 @@ namespace lms::api::subsonic
}
catch (const av::Exception& e)
{
response.setStatus(404); // report not found if something wrong happened
LMS_LOG(API_SUBSONIC, ERROR, "Caught Av exception: " << e.what());
}
}
@@ -0,0 +1,158 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Podcast.hpp"
#include "database/Session.hpp"
#include "database/objects/Podcast.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "services/podcast/IPodcastService.hpp"
#include "ParameterParsing.hpp"
#include "RequestContext.hpp"
#include "SubsonicId.hpp"
#include "SubsonicResponse.hpp"
#include "responses/Podcast.hpp"
namespace lms::api::subsonic
{
Response handleGetPodcasts(RequestContext& context)
{
const bool includeEpisodes{ getParameterAs<bool>(context.parameters, "includeEpisodes").value_or(true) };
const std::optional<db::PodcastId> podcastId{ getParameterAs<db::PodcastId>(context.parameters, "id") };
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
Response::Node& podcastsNode{ response.createNode("podcasts") };
podcastsNode.createEmptyArrayChild("channel");
auto transaction{ context.dbSession.createReadTransaction() };
auto processPodcast{ [&](const db::Podcast::pointer& podcast) {
podcastsNode.addArrayChild("channel", createPodcastNode(context, podcast, includeEpisodes));
} };
if (podcastId.has_value())
{
db::Podcast::pointer podcast{ db::Podcast::find(context.dbSession, podcastId.value()) };
if (!podcast)
throw RequestedDataNotFoundError{};
processPodcast(podcast);
}
else
db::Podcast::find(context.dbSession, processPodcast);
return response;
}
Response handleGetNewestPodcasts(RequestContext& context)
{
std::size_t count{ getParameterAs<std::size_t>(context.parameters, "count").value_or(20) };
count = std::min<std::size_t>(count, 100);
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
Response::Node& newestPodcastsNode{ response.createNode("newestPodcasts") };
newestPodcastsNode.createEmptyArrayChild("episode");
{
auto transaction{ context.dbSession.createReadTransaction() };
db::PodcastEpisode::FindParameters findParameters;
findParameters.setRange(db::Range{ .offset = 0, .size = count });
db::PodcastEpisode::find(context.dbSession, findParameters, [&](const db::PodcastEpisode::pointer& episode) {
newestPodcastsNode.addArrayChild("episode", createPodcastEpisodeNode(episode));
});
}
return response;
}
Response handleRefreshPodcasts(RequestContext& context)
{
core::Service<podcast::IPodcastService>::get()->refreshPodcasts();
return Response::createOkResponse(context.serverProtocolVersion);
}
Response handleCreatePodcastChannel(RequestContext& context)
{
// Mandatory parameters
const std::string url{ getMandatoryParameterAs<std::string>(context.parameters, "url") };
if (url.empty() || !(url.starts_with("http://") || url.starts_with("https://")))
throw BadParameterGenericError{ "Invalid url" };
// no effect if podcast already exists
core::Service<podcast::IPodcastService>::get()->addPodcast(url);
return Response::createOkResponse(context.serverProtocolVersion);
}
Response handleDeletePodcastChannel(RequestContext& context)
{
// Mandatory parameters
const db::PodcastId podcastId{ getMandatoryParameterAs<db::PodcastId>(context.parameters, "id") };
if (!core::Service<podcast::IPodcastService>::get()->removePodcast(podcastId))
throw RequestedDataNotFoundError{};
return Response::createOkResponse(context.serverProtocolVersion);
}
Response handleDeletePodcastEpisode(RequestContext& context)
{
// Mandatory parameters
const db::PodcastEpisodeId episodeId{ getMandatoryParameterAs<db::PodcastEpisodeId>(context.parameters, "id") };
if (!core::Service<podcast::IPodcastService>::get()->deletePodcastEpisode(episodeId))
throw RequestedDataNotFoundError{};
return Response::createOkResponse(context.serverProtocolVersion);
}
Response handleDownloadPodcastEpisode(RequestContext& context)
{
// Mandatory parameters
const db::PodcastEpisodeId episodeId{ getMandatoryParameterAs<db::PodcastEpisodeId>(context.parameters, "id") };
if (!core::Service<podcast::IPodcastService>::get()->downloadPodcastEpisode(episodeId))
throw RequestedDataNotFoundError{};
return Response::createOkResponse(context.serverProtocolVersion);
}
Response handleGetPodcastEpisode(RequestContext& context)
{
// Mandatory parameters
const db::PodcastEpisodeId episodeId{ getMandatoryParameterAs<db::PodcastEpisodeId>(context.parameters, "id") };
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
auto transaction{ context.dbSession.createReadTransaction() };
const db::PodcastEpisode::pointer episode{ db::PodcastEpisode::find(context.dbSession, episodeId) };
if (!episode)
throw RequestedDataNotFoundError{};
response.addNode("podcastEpisode", createPodcastEpisodeNode(episode));
return response;
}
} // namespace lms::api::subsonic
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "RequestContext.hpp"
#include "SubsonicResponse.hpp"
namespace lms::api::subsonic
{
Response handleGetPodcasts(RequestContext& context);
Response handleGetNewestPodcasts(RequestContext& context);
Response handleRefreshPodcasts(RequestContext& context);
Response handleCreatePodcastChannel(RequestContext& context);
Response handleDeletePodcastChannel(RequestContext& context);
Response handleDeletePodcastEpisode(RequestContext& context);
Response handleDownloadPodcastEpisode(RequestContext& context);
Response handleGetPodcastEpisode(RequestContext& context);
} // namespace lms::api::subsonic
@@ -47,6 +47,12 @@ namespace lms::api::subsonic
apiKeyAuthentication.addArrayValue("versions", 1);
}
{
Response::Node& apiKeyAuthentication{ response.createArrayNode("openSubsonicExtensions") };
apiKeyAuthentication.setAttribute("name", "getPodcastEpisode");
apiKeyAuthentication.addArrayValue("versions", 1);
}
return response;
};
} // namespace lms::api::subsonic