Introduced new transcoding service

This commit is contained in:
emeric
2025-05-30 23:27:01 +02:00
parent 8cf2e2b660
commit dc52ea4ea5
32 changed files with 487 additions and 301 deletions
-2
View File
@@ -2,9 +2,7 @@ pkg_check_modules(LIBAV IMPORTED_TARGET libavcodec libavutil libavformat)
add_library(lmsav STATIC add_library(lmsav STATIC
impl/AudioFile.cpp impl/AudioFile.cpp
impl/RawResourceHandlerCreator.cpp
impl/Transcoder.cpp impl/Transcoder.cpp
impl/TranscodingResourceHandler.cpp
) )
target_include_directories(lmsav INTERFACE target_include_directories(lmsav INTERFACE
+1 -39
View File
@@ -34,7 +34,7 @@ extern "C"
#include "core/ILogger.hpp" #include "core/ILogger.hpp"
#include "core/String.hpp" #include "core/String.hpp"
#include "av/Types.hpp" #include "av/Exception.hpp"
namespace lms::av namespace lms::av
{ {
@@ -321,42 +321,4 @@ namespace lms::av
return res; return res;
} }
std::string_view getMimeType(const std::filesystem::path& fileExtension)
{
// List should be sync with the demuxers shipped in the lms's docker version
// + the _audioFileExtensions in ScanSettings
// std::filesystem::path does not seem to have std::hash specialization on freebsd
static const std::unordered_map<std::string, std::string_view> entries{
{ ".mp3", "audio/mpeg" },
{ ".ogg", "audio/ogg" },
{ ".oga", "audio/ogg" },
{ ".opus", "audio/opus" },
{ ".aac", "audio/aac" },
{ ".alac", "audio/mp4" },
{ ".m4a", "audio/mp4" },
{ ".m4b", "audio/mp4" },
{ ".flac", "audio/flac" },
{ ".webm", "audio/webm" },
{ ".wav", "audio/x-wav" },
{ ".wma", "audio/x-ms-wma" },
{ ".ape", "audio/x-monkeys-audio" },
{ ".mpc", "audio/x-musepack" },
{ ".shn", "audio/x-shn" },
{ ".aif", "audio/x-aiff" },
{ ".aiff", "audio/x-aiff" },
{ ".m3u", "audio/x-mpegurl" },
{ ".pls", "audio/x-scpls" },
{ ".dsf", "audio/x-dsd" },
{ ".wv", "audio/x-wavpack" },
{ ".wvp", "audio/x-wavpack" },
{ ".mka", "audio/x-matroska" },
};
auto it{ entries.find(core::stringUtils::stringToLower(fileExtension.c_str())) };
if (it == std::cend(entries))
return "";
return it->second;
}
} // namespace lms::av } // namespace lms::av
@@ -1,32 +0,0 @@
/*
* Copyright (C) 2023 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 "av/RawResourceHandlerCreator.hpp"
#include "av/IAudioFile.hpp"
#include "core/FileResourceHandlerCreator.hpp"
namespace lms::av
{
std::unique_ptr<IResourceHandler> createRawResourceHandler(const std::filesystem::path& path)
{
std::string_view mimeType{ getMimeType(path.extension()) };
return createFileResourceHandler(path, mimeType.empty() ? "application/octet-stream" : mimeType);
}
} // namespace lms::av
+45 -42
View File
@@ -27,35 +27,20 @@
#include "core/ILogger.hpp" #include "core/ILogger.hpp"
#include "core/Service.hpp" #include "core/Service.hpp"
#include "av/Types.hpp" #include "av/Exception.hpp"
namespace lms::av::transcoding namespace lms::av
{ {
#define LOG(severity, message) LMS_LOG(TRANSCODING, severity, "[" << _debugId << "] - " << message) #define LOG(severity, message) LMS_LOG(TRANSCODING, severity, "[" << _debugId << "] - " << message)
std::unique_ptr<ITranscoder> createTranscoder(const InputParameters& inputParameters, const OutputParameters& outputParameters)
{
return std::make_unique<Transcoder>(inputParameters, outputParameters);
}
static std::atomic<size_t> globalId{}; static std::atomic<size_t> globalId{};
static std::filesystem::path ffmpegPath; static std::filesystem::path ffmpegPath;
std::string_view formatToMimetype(OutputFormat format)
{
switch (format)
{
case OutputFormat::MP3:
return "audio/mpeg";
case OutputFormat::OGG_OPUS:
return "audio/opus";
case OutputFormat::MATROSKA_OPUS:
return "audio/x-matroska";
case OutputFormat::OGG_VORBIS:
return "audio/ogg";
case OutputFormat::WEBM_VORBIS:
return "audio/webm";
}
throw Exception{ "Invalid encoding" };
}
void Transcoder::init() void Transcoder::init()
{ {
ffmpegPath = core::Service<core::IConfig>::get()->getPath("ffmpeg-file", "/usr/bin/ffmpeg"); ffmpegPath = core::Service<core::IConfig>::get()->getPath("ffmpeg-file", "/usr/bin/ffmpeg");
@@ -63,10 +48,10 @@ namespace lms::av::transcoding
throw Exception{ "File '" + ffmpegPath.string() + "' does not exist!" }; throw Exception{ "File '" + ffmpegPath.string() + "' does not exist!" };
} }
Transcoder::Transcoder(const InputParameters& inputParameters, const OutputParameters& outputParameters) Transcoder::Transcoder(const InputParameters& inputParams, const OutputParameters& outputParams)
: _debugId{ globalId++ } : _debugId{ globalId++ }
, _inputParameters{ inputParameters } , _inputParams{ inputParams }
, _outputParameters{ outputParameters } , _outputParams{ outputParams }
{ {
start(); start();
} }
@@ -80,17 +65,18 @@ namespace lms::av::transcoding
try try
{ {
if (!std::filesystem::exists(_inputParameters.trackPath)) if (!std::filesystem::exists(_inputParams.file))
throw Exception{ "File '" + _inputParameters.trackPath.string() + "' does not exist!" }; throw Exception{ "File " + _inputParams.file.string() + " does not exist!" };
if (!std::filesystem::is_regular_file(_inputParameters.trackPath)) if (!std::filesystem::is_regular_file(_inputParams.file))
throw Exception{ "File '" + _inputParameters.trackPath.string() + "' is not regular!" }; throw Exception{ "File " + _inputParams.file.string() + " is not regular!" };
} }
catch (const std::filesystem::filesystem_error& e) catch (const std::filesystem::filesystem_error& e)
{ {
throw Exception{ "File error '" + _inputParameters.trackPath.string() + "': " + e.what() }; // TODO store/raise e.code()
throw Exception{ "File error '" + _inputParams.file.string() + "': " + e.what() };
} }
LOG(INFO, "Transcoding file " << _inputParameters.trackPath); LOG(INFO, "Transcoding file " << _inputParams.file);
std::vector<std::string> args; std::vector<std::string> args;
@@ -109,22 +95,22 @@ namespace lms::av::transcoding
args.emplace_back("-ss"); args.emplace_back("-ss");
std::ostringstream oss; std::ostringstream oss;
oss << std::fixed << std::showpoint << std::setprecision(3) << (_outputParameters.offset.count() / float{ 1'000 }); oss << std::fixed << std::showpoint << std::setprecision(3) << (_inputParams.offset.count() / float{ 1'000 });
args.emplace_back(oss.str()); args.emplace_back(oss.str());
} }
// Input file // Input file
args.emplace_back("-i"); args.emplace_back("-i");
args.emplace_back(_inputParameters.trackPath.string()); args.emplace_back(_inputParams.file.string());
// Stream mapping, if set // Stream mapping, if set
if (_outputParameters.stream) if (_inputParams.streamIndex)
{ {
args.emplace_back("-map"); args.emplace_back("-map");
args.emplace_back("0:" + std::to_string(*_outputParameters.stream)); args.emplace_back("0:" + std::to_string(*_inputParams.streamIndex));
} }
if (_outputParameters.stripMetadata) if (_outputParams.stripMetadata)
{ {
// Strip metadata // Strip metadata
args.emplace_back("-map_metadata"); args.emplace_back("-map_metadata");
@@ -136,10 +122,10 @@ namespace lms::av::transcoding
// Output bitrates // Output bitrates
args.emplace_back("-b:a"); args.emplace_back("-b:a");
args.emplace_back(std::to_string(_outputParameters.bitrate)); args.emplace_back(std::to_string(_outputParams.bitrate));
// Codecs and formats // Codecs and formats
switch (_outputParameters.format) switch (_outputParams.format)
{ {
case OutputFormat::MP3: case OutputFormat::MP3:
args.emplace_back("-f"); args.emplace_back("-f");
@@ -175,11 +161,9 @@ namespace lms::av::transcoding
break; break;
default: default:
throw Exception{ "Unhandled format (" + std::to_string(static_cast<int>(_outputParameters.format)) + ")" }; throw Exception{ "Unhandled format (" + std::to_string(static_cast<int>(_outputParams.format)) + ")" };
} }
_outputMimeType = formatToMimetype(_outputParameters.format);
args.emplace_back("pipe:1"); args.emplace_back("pipe:1");
LOG(DEBUG, "Dumping args (" << args.size() << ")"); LOG(DEBUG, "Dumping args (" << args.size() << ")");
@@ -213,6 +197,25 @@ namespace lms::av::transcoding
return _childProcess->readSome(buffer, bufferSize); return _childProcess->readSome(buffer, bufferSize);
} }
std::string_view Transcoder::getOutputMimeType() const
{
switch (_outputParams.format)
{
case OutputFormat::MP3:
return "audio/mpeg";
case OutputFormat::OGG_OPUS:
return "audio/opus";
case OutputFormat::MATROSKA_OPUS:
return "audio/x-matroska";
case OutputFormat::OGG_VORBIS:
return "audio/ogg";
case OutputFormat::WEBM_VORBIS:
return "audio/webm";
}
return "application/octet-stream"; // default, should not happen
}
bool Transcoder::finished() const bool Transcoder::finished() const
{ {
assert(_childProcess); assert(_childProcess);
@@ -220,4 +223,4 @@ namespace lms::av::transcoding
return _childProcess->finished(); return _childProcess->finished();
} }
} // namespace lms::av::transcoding } // namespace lms::av
+14 -24
View File
@@ -19,47 +19,37 @@
#pragma once #pragma once
#include <functional> #include "av/ITranscoder.hpp"
#include "av/TranscodingParameters.hpp"
namespace lms::core namespace lms::core
{ {
class IChildProcess; class IChildProcess;
} }
namespace lms::av::transcoding namespace lms::av
{ {
class Transcoder class Transcoder : public ITranscoder
{ {
public: public:
Transcoder(const InputParameters& inputParameters, const OutputParameters& outputParameters); Transcoder(const InputParameters& inputParameters, const OutputParameters& outputParameters);
~Transcoder(); ~Transcoder() override;
Transcoder(const Transcoder&) = delete; Transcoder(const Transcoder&) = delete;
Transcoder& operator=(const Transcoder&) = delete; Transcoder& operator=(const Transcoder&) = delete;
Transcoder(Transcoder&&) = delete;
Transcoder& operator=(Transcoder&&) = delete;
// non blocking calls
using ReadCallback = std::function<void(std::size_t nbReadBytes)>;
void asyncRead(std::byte* buffer, std::size_t bufferSize, ReadCallback);
std::size_t readSome(std::byte* buffer, std::size_t bufferSize);
const std::string& getOutputMimeType() const { return _outputMimeType; }
const OutputParameters& getOutputParameters() const { return _outputParameters; }
bool finished() const;
private: private:
static void init(); void asyncRead(std::byte* buffer, std::size_t bufferSize, ReadCallback) override;
std::size_t readSome(std::byte* buffer, std::size_t bufferSize) override;
std::string_view getOutputMimeType() const override;
const OutputParameters& getOutputParameters() const override { return _outputParams; }
bool finished() const override;
static void init();
void start(); void start();
const std::size_t _debugId{}; const std::size_t _debugId{};
const InputParameters _inputParameters; const InputParameters _inputParams;
const OutputParameters _outputParameters; const OutputParameters _outputParams;
std::string _outputMimeType;
std::unique_ptr<core::IChildProcess> _childProcess; std::unique_ptr<core::IChildProcess> _childProcess;
}; };
} // namespace lms::av::transcoding } // namespace lms::av
-3
View File
@@ -101,7 +101,4 @@ namespace lms::av
}; };
std::unique_ptr<IAudioFile> parseAudioFile(const std::filesystem::path& p); std::unique_ptr<IAudioFile> parseAudioFile(const std::filesystem::path& p);
std::string_view getMimeType(const std::filesystem::path& fileExtension);
} // namespace lms::av } // namespace lms::av
+71
View File
@@ -0,0 +1,71 @@
/*
* 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 <cstddef>
#include <filesystem>
#include <functional>
#include <memory>
#include <optional>
#include <string_view>
namespace lms::av
{
struct InputParameters
{
std::filesystem::path file; // Path to the input file
std::chrono::milliseconds offset{}; // Offset in the input file to start transcoding from
std::optional<std::size_t> streamIndex; // Index of the stream to be transcoded (select "best" audio stream if not set)
};
enum class OutputFormat
{
MP3,
OGG_OPUS,
MATROSKA_OPUS,
OGG_VORBIS,
WEBM_VORBIS,
};
struct OutputParameters
{
OutputFormat format;
std::size_t bitrate{ 128'000 };
bool stripMetadata{ true };
};
class ITranscoder
{
public:
virtual ~ITranscoder() = default;
// non blocking calls
using ReadCallback = std::function<void(std::size_t nbReadBytes)>;
virtual void asyncRead(std::byte* buffer, std::size_t bufferSize, ReadCallback callback) = 0;
virtual std::size_t readSome(std::byte* buffer, std::size_t bufferSize) = 0;
virtual std::string_view getOutputMimeType() const = 0;
virtual const OutputParameters& getOutputParameters() const = 0;
virtual bool finished() const = 0;
};
std::unique_ptr<ITranscoder> createTranscoder(const InputParameters& inputParameters, const OutputParameters& outputParameters);
} // namespace lms::av
@@ -1,32 +0,0 @@
/*
* Copyright (C) 2020 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 <memory>
#include "core/IResourceHandler.hpp"
namespace lms::av::transcoding
{
struct InputParameters;
struct OutputParameters;
std::unique_ptr<IResourceHandler> createResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength);
} // namespace lms::av::transcoding
+1
View File
@@ -12,6 +12,7 @@ add_library(lmscore STATIC
impl/FileResourceHandler.cpp impl/FileResourceHandler.cpp
impl/IOContextRunner.cpp impl/IOContextRunner.cpp
impl/Logger.cpp impl/Logger.cpp
impl/MimeTypes.cpp
impl/NetAddress.cpp impl/NetAddress.cpp
impl/PartialDateTime.cpp impl/PartialDateTime.cpp
impl/Path.cpp impl/Path.cpp
+4 -3
View File
@@ -22,12 +22,13 @@
#include <fstream> #include <fstream>
#include "core/ILogger.hpp" #include "core/ILogger.hpp"
#include "core/MimeTypes.hpp"
namespace lms namespace lms::core
{ {
std::unique_ptr<IResourceHandler> createFileResourceHandler(const std::filesystem::path& path, std::string_view mimeType) std::unique_ptr<IResourceHandler> createFileResourceHandler(const std::filesystem::path& path, std::string_view mimeType)
{ {
return std::make_unique<FileResourceHandler>(path, mimeType); return std::make_unique<FileResourceHandler>(path, mimeType.empty() ? getMimeType(path.extension()) : mimeType);
} }
FileResourceHandler::FileResourceHandler(const std::filesystem::path& path, std::string_view mimeType) FileResourceHandler::FileResourceHandler(const std::filesystem::path& path, std::string_view mimeType)
@@ -134,4 +135,4 @@ namespace lms
LMS_LOG(UTILS, DEBUG, "Job complete!"); LMS_LOG(UTILS, DEBUG, "Job complete!");
return nullptr; return nullptr;
} }
} // namespace lms } // namespace lms::core
+2 -2
View File
@@ -25,7 +25,7 @@
#include "core/IResourceHandler.hpp" #include "core/IResourceHandler.hpp"
namespace lms namespace lms::core
{ {
class FileResourceHandler final : public IResourceHandler class FileResourceHandler final : public IResourceHandler
{ {
@@ -43,4 +43,4 @@ namespace lms
::uint64_t _beyondLastByte{}; ::uint64_t _beyondLastByte{};
::uint64_t _offset{}; ::uint64_t _offset{};
}; };
} // namespace lms } // namespace lms::core
+79
View File
@@ -0,0 +1,79 @@
/*
* 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 "core/MimeTypes.hpp"
#include <unordered_map>
#include "core/String.hpp"
namespace lms::core
{
std::string_view getMimeType(const std::filesystem::path& fileExtension)
{
static const std::unordered_map<std::string, std::string_view> entries{
// audio
{ ".aac", "audio/aac" },
{ ".ac3", "audio/ac3" },
{ ".aif", "audio/x-aiff" },
{ ".aiff", "audio/x-aiff" },
{ ".alac", "audio/mp4" },
{ ".ape", "audio/x-monkeys-audio" },
{ ".dff", "audio/x-dsd-dff" },
{ ".dsdiff", "audio/x-dsd-diff" },
{ ".dsf", "audio/x-dsd" },
{ ".dsf", "audio/x-dsd-dsf" },
{ ".dts", "audio/vnd.dts" },
{ ".dtshd", "audio/vnd.dts.hd" },
{ ".eac3", "audio/eac3" },
{ ".flac", "audio/flac" },
{ ".m3u", "audio/x-mpegurl" },
{ ".m4a", "audio/mp4" },
{ ".m4b", "audio/mp4" },
{ ".mka", "audio/x-matroska" },
{ ".mka", "audio/x-matroska" },
{ ".mp3", "audio/mpeg" },
{ ".mpc", "audio/x-musepack" },
{ ".oga", "audio/ogg" },
{ ".ogg", "audio/ogg" },
{ ".opus", "audio/opus" },
{ ".pls", "audio/x-scpls" },
{ ".shn", "audio/x-shn" },
{ ".wav", "audio/x-wav" },
{ ".webm", "audio/webm" },
{ ".wma", "audio/x-ms-wma" },
{ ".wv", "audio/x-wavpack" },
{ ".wvp", "audio/x-wavpack" },
// image
{ ".bmp", "image/bmp" },
{ ".gif", "image/gif" },
{ ".jpg", "image/jpeg" },
{ ".jpeg", "image/jpeg" },
{ ".png", "image/png" },
{ ".webp", "image/webp" },
};
auto it{ entries.find(core::stringUtils::stringToLower(fileExtension.c_str())) };
if (it == std::cend(entries))
return "application/octet-stream";
return it->second;
}
} // namespace lms::core
@@ -25,7 +25,7 @@
#include "core/IResourceHandler.hpp" #include "core/IResourceHandler.hpp"
namespace lms namespace lms::core
{ {
std::unique_ptr<IResourceHandler> createFileResourceHandler(const std::filesystem::path& path, std::string_view mimeType); std::unique_ptr<IResourceHandler> createFileResourceHandler(const std::filesystem::path& path, std::string_view mimeType = "");
} }
@@ -22,8 +22,7 @@
#include <Wt/Http/Request.h> #include <Wt/Http/Request.h>
#include <Wt/Http/Response.h> #include <Wt/Http/Response.h>
// TODO, move elsewhere namespace lms::core
namespace lms
{ {
// Helper class to serve a resource (must be saved as continuation data if not complete) // Helper class to serve a resource (must be saved as continuation data if not complete)
class IResourceHandler class IResourceHandler
@@ -34,4 +33,4 @@ namespace lms
[[nodiscard]] virtual Wt::Http::ResponseContinuation* processRequest(const Wt::Http::Request& request, Wt::Http::Response& response) = 0; [[nodiscard]] virtual Wt::Http::ResponseContinuation* processRequest(const Wt::Http::Request& request, Wt::Http::Response& response) = 0;
virtual void abort() = 0; virtual void abort() = 0;
}; };
} // namespace lms } // namespace lms::core
@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2023 Emeric Poupon * Copyright (C) 2025 Emeric Poupon
* *
* This file is part of LMS. * This file is part of LMS.
* *
@@ -20,11 +20,9 @@
#pragma once #pragma once
#include <filesystem> #include <filesystem>
#include <memory> #include <string_view>
#include "core/IResourceHandler.hpp" namespace lms::core
namespace lms::av
{ {
std::unique_ptr<IResourceHandler> createRawResourceHandler(const std::filesystem::path& path); std::string_view getMimeType(const std::filesystem::path& fileExtension);
} }
@@ -19,8 +19,8 @@
#include "AvFormatImageReader.hpp" #include "AvFormatImageReader.hpp"
#include "av/Exception.hpp"
#include "av/IAudioFile.hpp" #include "av/IAudioFile.hpp"
#include "av/Types.hpp"
#include "metadata/Exception.hpp" #include "metadata/Exception.hpp"
namespace lms::metadata::avformat namespace lms::metadata::avformat
@@ -19,8 +19,8 @@
#include "AvFormatTagReader.hpp" #include "AvFormatTagReader.hpp"
#include "av/Exception.hpp"
#include "av/IAudioFile.hpp" #include "av/IAudioFile.hpp"
#include "av/Types.hpp"
#include "core/ILogger.hpp" #include "core/ILogger.hpp"
#include "core/String.hpp" #include "core/String.hpp"
#include "metadata/Exception.hpp" #include "metadata/Exception.hpp"
+1
View File
@@ -4,3 +4,4 @@ add_subdirectory(feedback)
add_subdirectory(recommendation) add_subdirectory(recommendation)
add_subdirectory(scanner) add_subdirectory(scanner)
add_subdirectory(scrobbling) add_subdirectory(scrobbling)
add_subdirectory(transcoding)
@@ -0,0 +1,17 @@
add_library(lmstranscoding STATIC
impl/TranscodingResourceHandler.cpp
impl/TranscodingService.cpp
)
target_include_directories(lmstranscoding INTERFACE
include
)
target_include_directories(lmstranscoding PRIVATE
include
impl
)
target_link_libraries(lmstranscoding PRIVATE
lmsav
)
@@ -18,9 +18,12 @@
*/ */
#include "TranscodingResourceHandler.hpp" #include "TranscodingResourceHandler.hpp"
#include "av/Exception.hpp"
#include "av/ITranscoder.hpp"
#include "core/ILogger.hpp" #include "core/ILogger.hpp"
namespace lms::av::transcoding namespace lms::transcoding
{ {
namespace namespace
{ {
@@ -29,9 +32,20 @@ namespace lms::av::transcoding
const std::size_t estimatedContentLength{ outputParameters.bitrate / 8 * static_cast<std::size_t>(std::chrono::duration_cast<std::chrono::milliseconds>(inputParameters.duration).count()) / 1000 }; const std::size_t estimatedContentLength{ outputParameters.bitrate / 8 * static_cast<std::size_t>(std::chrono::duration_cast<std::chrono::milliseconds>(inputParameters.duration).count()) / 1000 };
return estimatedContentLength; return estimatedContentLength;
} }
av::InputParameters toAv(const InputParameters& in)
{
return { .file = in.file, .offset = in.offset, .streamIndex = in.streamIndex };
}
av::OutputParameters toAv(const OutputParameters& out)
{
return { .format = static_cast<lms::av::OutputFormat>(out.format), .bitrate = out.bitrate, .stripMetadata = out.stripMetadata };
}
} // namespace } // namespace
std::unique_ptr<IResourceHandler> createResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength) std::unique_ptr<core::IResourceHandler> createResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength)
{ {
return std::make_unique<TranscodingResourceHandler>(inputParameters, outputParameters, estimateContentLength); return std::make_unique<TranscodingResourceHandler>(inputParameters, outputParameters, estimateContentLength);
} }
@@ -40,20 +54,36 @@ namespace lms::av::transcoding
TranscodingResourceHandler::TranscodingResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength) TranscodingResourceHandler::TranscodingResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength)
: _estimatedContentLength{ estimateContentLength ? std::make_optional(doEstimateContentLength(inputParameters, outputParameters)) : std::nullopt } : _estimatedContentLength{ estimateContentLength ? std::make_optional(doEstimateContentLength(inputParameters, outputParameters)) : std::nullopt }
, _transcoder{ inputParameters, outputParameters }
{ {
if (_estimatedContentLength) try
LMS_LOG(TRANSCODING, DEBUG, "Estimated content length = " << *_estimatedContentLength); {
else _transcoder = av::createTranscoder(toAv(inputParameters), toAv(outputParameters));
LMS_LOG(TRANSCODING, DEBUG, "Not using estimated content length");
if (_estimatedContentLength)
LMS_LOG(TRANSCODING, DEBUG, "Estimated content length = " << *_estimatedContentLength);
else
LMS_LOG(TRANSCODING, DEBUG, "Not using estimated content length");
}
catch (av::Exception& e)
{
LMS_LOG(TRANSCODING, ERROR, "Failed to create transcoder: " << e.what());
}
} }
TranscodingResourceHandler::~TranscodingResourceHandler() = default;
Wt::Http::ResponseContinuation* TranscodingResourceHandler::processRequest(const Wt::Http::Request& /*request*/, Wt::Http::Response& response) Wt::Http::ResponseContinuation* TranscodingResourceHandler::processRequest(const Wt::Http::Request& /*request*/, Wt::Http::Response& response)
{ {
if (!_transcoder)
{
response.setStatus(404);
return {};
}
if (_estimatedContentLength) if (_estimatedContentLength)
response.setContentLength(*_estimatedContentLength); response.setContentLength(*_estimatedContentLength);
response.setMimeType(_transcoder.getOutputMimeType()); response.setMimeType(std::string{ _transcoder->getOutputMimeType() });
LMS_LOG(TRANSCODING, DEBUG, "Transcoder finished = " << _transcoder.finished() << ", total served bytes = " << _totalServedByteCount << ", mime type = " << _transcoder.getOutputMimeType()); LMS_LOG(TRANSCODING, DEBUG, "Transcoder finished = " << _transcoder->finished() << ", total served bytes = " << _totalServedByteCount << ", mime type = " << _transcoder->getOutputMimeType());
if (_bytesReadyCount > 0) if (_bytesReadyCount > 0)
{ {
@@ -64,11 +94,11 @@ namespace lms::av::transcoding
_bytesReadyCount = 0; _bytesReadyCount = 0;
} }
if (!_transcoder.finished()) if (!_transcoder->finished())
{ {
Wt::Http::ResponseContinuation* continuation{ response.createContinuation() }; Wt::Http::ResponseContinuation* continuation{ response.createContinuation() };
continuation->waitForMoreData(); continuation->waitForMoreData();
_transcoder.asyncRead(_buffer.data(), _buffer.size(), [this, continuation](std::size_t nbBytesRead) { _transcoder->asyncRead(_buffer.data(), _buffer.size(), [this, continuation](std::size_t nbBytesRead) {
LMS_LOG(TRANSCODING, DEBUG, "Have " << nbBytesRead << " more bytes to send back"); LMS_LOG(TRANSCODING, DEBUG, "Have " << nbBytesRead << " more bytes to send back");
assert(_bytesReadyCount == 0); assert(_bytesReadyCount == 0);
@@ -96,4 +126,4 @@ namespace lms::av::transcoding
return {}; return {};
} }
} // namespace lms::av::transcoding } // namespace lms::transcoding
@@ -20,20 +20,24 @@
#pragma once #pragma once
#include <array> #include <array>
#include <memory>
#include <optional> #include <optional>
#include "av/TranscodingParameters.hpp"
#include "core/IResourceHandler.hpp" #include "core/IResourceHandler.hpp"
#include "services/transcoding/ITranscodingService.hpp"
#include "Transcoder.hpp" namespace lms::av
namespace lms::av::transcoding
{ {
class TranscodingResourceHandler final : public IResourceHandler class ITranscoder;
}
namespace lms::transcoding
{
class TranscodingResourceHandler final : public core::IResourceHandler
{ {
public: public:
TranscodingResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength); TranscodingResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength);
~TranscodingResourceHandler() override = default; ~TranscodingResourceHandler() override;
TranscodingResourceHandler(const TranscodingResourceHandler&) = delete; TranscodingResourceHandler(const TranscodingResourceHandler&) = delete;
TranscodingResourceHandler& operator=(const TranscodingResourceHandler&) = delete; TranscodingResourceHandler& operator=(const TranscodingResourceHandler&) = delete;
@@ -47,6 +51,6 @@ namespace lms::av::transcoding
std::array<std::byte, _chunkSize> _buffer; std::array<std::byte, _chunkSize> _buffer;
std::size_t _bytesReadyCount{}; std::size_t _bytesReadyCount{};
std::size_t _totalServedByteCount{}; std::size_t _totalServedByteCount{};
Transcoder _transcoder; std::unique_ptr<av::ITranscoder> _transcoder;
}; };
} // namespace lms::av::transcoding } // namespace lms::transcoding
@@ -0,0 +1,48 @@
/*
* 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 "TranscodingService.hpp"
#include "core/ILogger.hpp"
#include "TranscodingResourceHandler.hpp"
namespace lms::transcoding
{
std::unique_ptr<ITranscodingService> createTranscodingService(core::IChildProcessManager& childProcessManager)
{
return std::make_unique<TranscodingService>(childProcessManager);
}
TranscodingService::TranscodingService(core::IChildProcessManager& childProcessManager)
: _childProcessManager(childProcessManager)
{
LMS_LOG(TRANSCODING, INFO, "Service started!");
}
TranscodingService::~TranscodingService()
{
LMS_LOG(TRANSCODING, INFO, "Service stopped!");
}
std::unique_ptr<core::IResourceHandler> TranscodingService::createResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength)
{
return std::make_unique<TranscodingResourceHandler>(inputParameters, outputParameters, estimateContentLength);
}
} // namespace lms::transcoding
@@ -0,0 +1,40 @@
/*
* 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 "services/transcoding/ITranscodingService.hpp"
namespace lms::transcoding
{
class TranscodingService : public ITranscodingService
{
public:
explicit TranscodingService(core::IChildProcessManager& childProcessManager);
~TranscodingService() override;
TranscodingService(const TranscodingService&) = delete;
TranscodingService& operator=(const TranscodingService&) = delete;
private:
std::unique_ptr<core::IResourceHandler> createResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength) override;
core::IChildProcessManager& _childProcessManager;
};
} // namespace lms::transcoding
@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2015 Emeric Poupon * Copyright (C) 2025 Emeric Poupon
* *
* This file is part of LMS. * This file is part of LMS.
* *
@@ -19,16 +19,24 @@
#pragma once #pragma once
#include <chrono>
#include <filesystem> #include <filesystem>
#include <memory>
#include <optional> #include <optional>
namespace lms::av::transcoding namespace lms::core
{
class IChildProcessManager;
class IResourceHandler;
} // namespace lms::core
namespace lms::transcoding
{ {
struct InputParameters struct InputParameters
{ {
std::filesystem::path trackPath; std::filesystem::path file; // Path to the input file
std::chrono::milliseconds duration; // used to estimate content length std::chrono::milliseconds duration; // Offset in the input file to start transcoding from
std::chrono::milliseconds offset{}; // Offset in the input file to start transcoding from
std::optional<std::size_t> streamIndex; // Index of the stream to be transcoded (select "best" audio stream if not set)
}; };
enum class OutputFormat enum class OutputFormat
@@ -40,14 +48,20 @@ namespace lms::av::transcoding
WEBM_VORBIS, WEBM_VORBIS,
}; };
std::string_view toMimetype(OutputFormat format);
struct OutputParameters struct OutputParameters
{ {
OutputFormat format; OutputFormat format;
std::size_t bitrate{ 128'000 }; std::size_t bitrate{ 128'000 };
std::optional<std::size_t> stream; // Id of the stream to be transcoded (auto detect by default)
std::chrono::milliseconds offset{ 0 };
bool stripMetadata{ true }; bool stripMetadata{ true };
}; };
} // namespace lms::av::transcoding
class ITranscodingService
{
public:
virtual ~ITranscodingService() = default;
virtual std::unique_ptr<core::IResourceHandler> createResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength) = 0;
};
std::unique_ptr<ITranscodingService> createTranscodingService(core::IChildProcessManager& childProcessManager);
} // namespace lms::transcoding
+2 -1
View File
@@ -44,6 +44,7 @@ target_include_directories(lmssubsonic PRIVATE
) )
target_link_libraries(lmssubsonic PRIVATE target_link_libraries(lmssubsonic PRIVATE
lmsartwork
lmsauth lmsauth
lmsav lmsav
lmsdatabase lmsdatabase
@@ -51,7 +52,7 @@ target_link_libraries(lmssubsonic PRIVATE
lmsrecommendation lmsrecommendation
lmsscanner lmsscanner
lmsscrobbling lmsscrobbling
lmsartwork lmstranscoding
lmscore lmscore
std::filesystem std::filesystem
) )
@@ -19,11 +19,9 @@
#include "MediaRetrieval.hpp" #include "MediaRetrieval.hpp"
#include "av/Exception.hpp"
#include "av/IAudioFile.hpp" #include "av/IAudioFile.hpp"
#include "av/RawResourceHandlerCreator.hpp" #include "core/FileResourceHandlerCreator.hpp"
#include "av/TranscodingParameters.hpp"
#include "av/TranscodingResourceHandlerCreator.hpp"
#include "av/Types.hpp"
#include "core/ILogger.hpp" #include "core/ILogger.hpp"
#include "core/IResourceHandler.hpp" #include "core/IResourceHandler.hpp"
#include "core/String.hpp" #include "core/String.hpp"
@@ -34,6 +32,7 @@
#include "database/TrackLyrics.hpp" #include "database/TrackLyrics.hpp"
#include "database/User.hpp" #include "database/User.hpp"
#include "services/artwork/IArtworkService.hpp" #include "services/artwork/IArtworkService.hpp"
#include "services/transcoding/ITranscodingService.hpp"
#include "CoverArtId.hpp" #include "CoverArtId.hpp"
#include "ParameterParsing.hpp" #include "ParameterParsing.hpp"
@@ -47,12 +46,12 @@ namespace lms::api::subsonic
namespace namespace
{ {
std::optional<av::transcoding::OutputFormat> subsonicStreamFormatToAvOutputFormat(std::string_view format) std::optional<transcoding::OutputFormat> subsonicStreamFormatToAvOutputFormat(std::string_view format)
{ {
for (const auto& [str, avFormat] : std::initializer_list<std::pair<std::string_view, av::transcoding::OutputFormat>>{ for (const auto& [str, avFormat] : std::initializer_list<std::pair<std::string_view, transcoding::OutputFormat>>{
{ "mp3", av::transcoding::OutputFormat::MP3 }, { "mp3", transcoding::OutputFormat::MP3 },
{ "opus", av::transcoding::OutputFormat::OGG_OPUS }, { "opus", transcoding::OutputFormat::OGG_OPUS },
{ "vorbis", av::transcoding::OutputFormat::OGG_VORBIS }, { "vorbis", transcoding::OutputFormat::OGG_VORBIS },
}) })
{ {
if (core::stringUtils::stringCaseInsensitiveEqual(str, format)) if (core::stringUtils::stringCaseInsensitiveEqual(str, format))
@@ -61,37 +60,37 @@ namespace lms::api::subsonic
return std::nullopt; return std::nullopt;
} }
av::transcoding::OutputFormat userTranscodeFormatToAvFormat(db::TranscodingOutputFormat format) transcoding::OutputFormat userTranscodeFormatToAvFormat(db::TranscodingOutputFormat format)
{ {
switch (format) switch (format)
{ {
case db::TranscodingOutputFormat::MP3: case db::TranscodingOutputFormat::MP3:
return av::transcoding::OutputFormat::MP3; return transcoding::OutputFormat::MP3;
case db::TranscodingOutputFormat::OGG_OPUS: case db::TranscodingOutputFormat::OGG_OPUS:
return av::transcoding::OutputFormat::OGG_OPUS; return transcoding::OutputFormat::OGG_OPUS;
case db::TranscodingOutputFormat::MATROSKA_OPUS: case db::TranscodingOutputFormat::MATROSKA_OPUS:
return av::transcoding::OutputFormat::MATROSKA_OPUS; return transcoding::OutputFormat::MATROSKA_OPUS;
case db::TranscodingOutputFormat::OGG_VORBIS: case db::TranscodingOutputFormat::OGG_VORBIS:
return av::transcoding::OutputFormat::OGG_VORBIS; return transcoding::OutputFormat::OGG_VORBIS;
case db::TranscodingOutputFormat::WEBM_VORBIS: case db::TranscodingOutputFormat::WEBM_VORBIS:
return av::transcoding::OutputFormat::WEBM_VORBIS; return transcoding::OutputFormat::WEBM_VORBIS;
} }
return av::transcoding::OutputFormat::OGG_OPUS; return transcoding::OutputFormat::OGG_OPUS;
} }
bool isCodecCompatibleWithOutputFormat(av::DecodingCodec codec, av::transcoding::OutputFormat outputFormat) bool isCodecCompatibleWithOutputFormat(av::DecodingCodec codec, transcoding::OutputFormat outputFormat)
{ {
switch (outputFormat) switch (outputFormat)
{ {
case av::transcoding::OutputFormat::MP3: case transcoding::OutputFormat::MP3:
return codec == av::DecodingCodec::MP3; return codec == av::DecodingCodec::MP3;
case av::transcoding::OutputFormat::OGG_OPUS: case transcoding::OutputFormat::OGG_OPUS:
case av::transcoding::OutputFormat::MATROSKA_OPUS: case transcoding::OutputFormat::MATROSKA_OPUS:
return codec == av::DecodingCodec::OPUS; return codec == av::DecodingCodec::OPUS;
case av::transcoding::OutputFormat::OGG_VORBIS: case transcoding::OutputFormat::OGG_VORBIS:
case av::transcoding::OutputFormat::WEBM_VORBIS: case transcoding::OutputFormat::WEBM_VORBIS:
return codec == av::DecodingCodec::VORBIS; return codec == av::DecodingCodec::VORBIS;
} }
@@ -100,13 +99,14 @@ namespace lms::api::subsonic
struct StreamParameters struct StreamParameters
{ {
av::transcoding::InputParameters inputParameters; transcoding::InputParameters inputParameters;
std::optional<av::transcoding::OutputParameters> outputParameters; std::optional<transcoding::OutputParameters> outputParameters;
bool estimateContentLength{}; bool estimateContentLength{};
}; };
bool isOutputFormatCompatible(const std::filesystem::path& trackPath, av::transcoding::OutputFormat outputFormat) bool isOutputFormatCompatible(const std::filesystem::path& trackPath, transcoding::OutputFormat outputFormat)
{ {
// TODO: put this information in db during scan
try try
{ {
const auto audioFile{ av::parseAudioFile(trackPath) }; const auto audioFile{ av::parseAudioFile(trackPath) };
@@ -143,14 +143,15 @@ namespace lms::api::subsonic
if (!track) if (!track)
throw RequestedDataNotFoundError{}; throw RequestedDataNotFoundError{};
parameters.inputParameters.trackPath = track->getAbsoluteFilePath(); parameters.inputParameters.file = track->getAbsoluteFilePath();
parameters.inputParameters.duration = track->getDuration(); parameters.inputParameters.duration = track->getDuration();
parameters.inputParameters.offset = std::chrono::seconds{ timeOffset };
parameters.estimateContentLength = estimateContentLength; parameters.estimateContentLength = estimateContentLength;
if (format == "raw") // raw => no transcoding if (format == "raw") // raw => no transcoding
return parameters; return parameters;
std::optional<av::transcoding::OutputFormat> requestedFormat{ subsonicStreamFormatToAvOutputFormat(format) }; std::optional<transcoding::OutputFormat> requestedFormat{ subsonicStreamFormatToAvOutputFormat(format) };
if (!requestedFormat) if (!requestedFormat)
{ {
if (context.user->getSubsonicEnableTranscodingByDefault()) if (context.user->getSubsonicEnableTranscodingByDefault())
@@ -185,10 +186,8 @@ namespace lms::api::subsonic
if (maxBitRate) if (maxBitRate)
bitrate = std::min<std::size_t>(bitrate, maxBitRate); bitrate = std::min<std::size_t>(bitrate, maxBitRate);
av::transcoding::OutputParameters& outputParameters{ parameters.outputParameters.emplace() }; transcoding::OutputParameters& outputParameters{ parameters.outputParameters.emplace() };
outputParameters.stripMetadata = false; // We want clients to use metadata (offline use, replay gain, etc.) outputParameters.stripMetadata = false; // We want clients to use metadata (offline use, replay gain, etc.)
outputParameters.offset = std::chrono::seconds{ timeOffset };
outputParameters.format = *requestedFormat; outputParameters.format = *requestedFormat;
outputParameters.bitrate = bitrate; outputParameters.bitrate = bitrate;
@@ -266,7 +265,7 @@ namespace lms::api::subsonic
void handleDownload(RequestContext& context, const Wt::Http::Request& request, Wt::Http::Response& response) void handleDownload(RequestContext& context, const Wt::Http::Request& request, Wt::Http::Response& response)
{ {
std::shared_ptr<IResourceHandler> resourceHandler; std::shared_ptr<core::IResourceHandler> resourceHandler;
Wt::Http::ResponseContinuation* continuation{ request.continuation() }; Wt::Http::ResponseContinuation* continuation{ request.continuation() };
if (!continuation) if (!continuation)
@@ -285,11 +284,11 @@ namespace lms::api::subsonic
trackPath = track->getAbsoluteFilePath(); trackPath = track->getAbsoluteFilePath();
} }
resourceHandler = av::createRawResourceHandler(trackPath); resourceHandler = core::createFileResourceHandler(trackPath);
} }
else else
{ {
resourceHandler = Wt::cpp17::any_cast<std::shared_ptr<IResourceHandler>>(continuation->data()); resourceHandler = Wt::cpp17::any_cast<std::shared_ptr<core::IResourceHandler>>(continuation->data());
} }
continuation = resourceHandler->processRequest(request, response); continuation = resourceHandler->processRequest(request, response);
@@ -299,7 +298,7 @@ namespace lms::api::subsonic
void handleStream(RequestContext& context, const Wt::Http::Request& request, Wt::Http::Response& response) void handleStream(RequestContext& context, const Wt::Http::Request& request, Wt::Http::Response& response)
{ {
std::shared_ptr<IResourceHandler> resourceHandler; std::shared_ptr<core::IResourceHandler> resourceHandler;
try try
{ {
@@ -308,13 +307,13 @@ namespace lms::api::subsonic
{ {
StreamParameters streamParameters{ getStreamParameters(context) }; StreamParameters streamParameters{ getStreamParameters(context) };
if (streamParameters.outputParameters) if (streamParameters.outputParameters)
resourceHandler = av::transcoding::createResourceHandler(streamParameters.inputParameters, *streamParameters.outputParameters, streamParameters.estimateContentLength); resourceHandler = core::Service<transcoding::ITranscodingService>::get()->createResourceHandler(streamParameters.inputParameters, *streamParameters.outputParameters, streamParameters.estimateContentLength);
else else
resourceHandler = av::createRawResourceHandler(streamParameters.inputParameters.trackPath); resourceHandler = core::createFileResourceHandler(streamParameters.inputParameters.file);
} }
else else
{ {
resourceHandler = Wt::cpp17::any_cast<std::shared_ptr<IResourceHandler>>(continuation->data()); resourceHandler = Wt::cpp17::any_cast<std::shared_ptr<core::IResourceHandler>>(continuation->data());
} }
continuation = resourceHandler->processRequest(request, response); continuation = resourceHandler->processRequest(request, response);
+3 -2
View File
@@ -23,6 +23,7 @@
#include "av/IAudioFile.hpp" #include "av/IAudioFile.hpp"
#include "core/ITraceLogger.hpp" #include "core/ITraceLogger.hpp"
#include "core/MimeTypes.hpp"
#include "core/Service.hpp" #include "core/Service.hpp"
#include "core/String.hpp" #include "core/String.hpp"
#include "database/Artist.hpp" #include "database/Artist.hpp"
@@ -107,7 +108,7 @@ namespace lms::api::subsonic
{ {
const std::string fileSuffix{ formatToSuffix(context.user->getSubsonicDefaultTranscodingOutputFormat()) }; const std::string fileSuffix{ formatToSuffix(context.user->getSubsonicDefaultTranscodingOutputFormat()) };
trackResponse.setAttribute("transcodedSuffix", fileSuffix); trackResponse.setAttribute("transcodedSuffix", fileSuffix);
trackResponse.setAttribute("transcodedContentType", av::getMimeType(std::filesystem::path{ "." + fileSuffix })); trackResponse.setAttribute("transcodedContentType", core::getMimeType(std::filesystem::path{ "." + fileSuffix }));
} }
const Release::pointer release{ track->getRelease() }; const Release::pointer release{ track->getRelease() };
@@ -156,7 +157,7 @@ namespace lms::api::subsonic
trackResponse.setAttribute("bitRate", (track->getBitrate() / 1000)); trackResponse.setAttribute("bitRate", (track->getBitrate() / 1000));
trackResponse.setAttribute("type", "music"); trackResponse.setAttribute("type", "music");
trackResponse.setAttribute("created", core::stringUtils::toISO8601String(track->getAddedTime())); trackResponse.setAttribute("created", core::stringUtils::toISO8601String(track->getAddedTime()));
trackResponse.setAttribute("contentType", av::getMimeType(track->getAbsoluteFilePath().extension())); trackResponse.setAttribute("contentType", core::getMimeType(track->getAbsoluteFilePath().extension()));
if (const auto rating{ core::Service<feedback::IFeedbackService>::get()->getRating(context.user->getId(), track->getId()) }) if (const auto rating{ core::Service<feedback::IFeedbackService>::get()->getRating(context.user->getId(), track->getId()) })
trackResponse.setAttribute("userRating", *rating); trackResponse.setAttribute("userRating", *rating);
+1
View File
@@ -73,6 +73,7 @@ target_link_libraries(lms PRIVATE
lmsscrobbling lmsscrobbling
lmsartwork lmsartwork
lmssubsonic lmssubsonic
lmstranscoding
lmscore lmscore
) )
+2
View File
@@ -44,6 +44,7 @@
#include "services/recommendation/IRecommendationService.hpp" #include "services/recommendation/IRecommendationService.hpp"
#include "services/scanner/IScannerService.hpp" #include "services/scanner/IScannerService.hpp"
#include "services/scrobbling/IScrobblingService.hpp" #include "services/scrobbling/IScrobblingService.hpp"
#include "services/transcoding/ITranscodingService.hpp"
#include "subsonic/SubsonicResource.hpp" #include "subsonic/SubsonicResource.hpp"
#include "ui/Auth.hpp" #include "ui/Auth.hpp"
#include "ui/LmsApplication.hpp" #include "ui/LmsApplication.hpp"
@@ -373,6 +374,7 @@ namespace lms
core::Service<recommendation::IRecommendationService> recommendationService{ recommendation::createRecommendationService(database) }; core::Service<recommendation::IRecommendationService> recommendationService{ recommendation::createRecommendationService(database) };
core::Service<recommendation::IPlaylistGeneratorService> playlistGeneratorService{ recommendation::createPlaylistGeneratorService(database, *recommendationService.get()) }; core::Service<recommendation::IPlaylistGeneratorService> playlistGeneratorService{ recommendation::createPlaylistGeneratorService(database, *recommendationService.get()) };
core::Service<scanner::IScannerService> scannerService{ scanner::createScannerService(database) }; core::Service<scanner::IScannerService> scannerService{ scanner::createScannerService(database) };
core::Service<transcoding::ITranscodingService> transcodingService{ transcoding::createTranscodingService(*childProcessManagerService.get()) };
scannerService->getEvents().scanComplete.connect([&] { scannerService->getEvents().scanComplete.connect([&] {
// Flush cover cache even if no changes: // Flush cover cache even if no changes:
+4 -4
View File
@@ -21,7 +21,7 @@
#include <Wt/Http/Response.h> #include <Wt/Http/Response.h>
#include "av/RawResourceHandlerCreator.hpp" #include "core/FileResourceHandlerCreator.hpp"
#include "core/ILogger.hpp" #include "core/ILogger.hpp"
#include "core/ITraceLogger.hpp" #include "core/ITraceLogger.hpp"
#include "core/String.hpp" #include "core/String.hpp"
@@ -85,7 +85,7 @@ namespace lms::ui
{ {
LMS_SCOPED_TRACE_OVERVIEW("UI", "HandleAudioFileRequest"); LMS_SCOPED_TRACE_OVERVIEW("UI", "HandleAudioFileRequest");
std::shared_ptr<IResourceHandler> fileResourceHandler; std::shared_ptr<core::IResourceHandler> fileResourceHandler;
if (!request.continuation()) if (!request.continuation())
{ {
@@ -93,11 +93,11 @@ namespace lms::ui
if (!trackPath) if (!trackPath)
return; return;
fileResourceHandler = av::createRawResourceHandler(*trackPath); fileResourceHandler = core::createFileResourceHandler(*trackPath);
} }
else else
{ {
fileResourceHandler = Wt::cpp17::any_cast<std::shared_ptr<IResourceHandler>>(request.continuation()->data()); fileResourceHandler = Wt::cpp17::any_cast<std::shared_ptr<core::IResourceHandler>>(request.continuation()->data());
} }
auto* continuation{ fileResourceHandler->processRequest(request, response) }; auto* continuation{ fileResourceHandler->processRequest(request, response) };
@@ -23,14 +23,14 @@
#include <Wt/Http/Response.h> #include <Wt/Http/Response.h>
#include "av/TranscodingParameters.hpp"
#include "av/TranscodingResourceHandlerCreator.hpp"
#include "av/Types.hpp"
#include "core/ILogger.hpp" #include "core/ILogger.hpp"
#include "core/IResourceHandler.hpp"
#include "core/Service.hpp"
#include "core/String.hpp" #include "core/String.hpp"
#include "database/Session.hpp" #include "database/Session.hpp"
#include "database/Track.hpp" #include "database/Track.hpp"
#include "database/User.hpp" #include "database/User.hpp"
#include "services/transcoding/ITranscodingService.hpp"
#include "LmsApplication.hpp" #include "LmsApplication.hpp"
@@ -72,20 +72,20 @@ namespace lms::ui
{ {
namespace namespace
{ {
std::optional<av::transcoding::OutputFormat> AudioFormatToAvFormat(db::TranscodingOutputFormat format) std::optional<transcoding::OutputFormat> AudioFormatToAvFormat(db::TranscodingOutputFormat format)
{ {
switch (format) switch (format)
{ {
case db::TranscodingOutputFormat::MP3: case db::TranscodingOutputFormat::MP3:
return av::transcoding::OutputFormat::MP3; return transcoding::OutputFormat::MP3;
case db::TranscodingOutputFormat::OGG_OPUS: case db::TranscodingOutputFormat::OGG_OPUS:
return av::transcoding::OutputFormat::OGG_OPUS; return transcoding::OutputFormat::OGG_OPUS;
case db::TranscodingOutputFormat::MATROSKA_OPUS: case db::TranscodingOutputFormat::MATROSKA_OPUS:
return av::transcoding::OutputFormat::MATROSKA_OPUS; return transcoding::OutputFormat::MATROSKA_OPUS;
case db::TranscodingOutputFormat::OGG_VORBIS: case db::TranscodingOutputFormat::OGG_VORBIS:
return av::transcoding::OutputFormat::OGG_VORBIS; return transcoding::OutputFormat::OGG_VORBIS;
case db::TranscodingOutputFormat::WEBM_VORBIS: case db::TranscodingOutputFormat::WEBM_VORBIS:
return av::transcoding::OutputFormat::WEBM_VORBIS; return transcoding::OutputFormat::WEBM_VORBIS;
} }
TRANSCODE_LOG(ERROR, "Cannot convert from audio format to AV format"); TRANSCODE_LOG(ERROR, "Cannot convert from audio format to AV format");
@@ -112,8 +112,8 @@ namespace lms::ui
struct TranscodingParameters struct TranscodingParameters
{ {
av::transcoding::InputParameters inputParameters; transcoding::InputParameters inputParameters;
av::transcoding::OutputParameters outputParameters; transcoding::OutputParameters outputParameters;
}; };
std::optional<TranscodingParameters> readTranscodingParameters(const Wt::Http::Request& request) std::optional<TranscodingParameters> readTranscodingParameters(const Wt::Http::Request& request)
@@ -134,7 +134,7 @@ namespace lms::ui
return std::nullopt; return std::nullopt;
} }
const std::optional<av::transcoding::OutputFormat> avFormat{ AudioFormatToAvFormat(*format) }; const std::optional<transcoding::OutputFormat> avFormat{ AudioFormatToAvFormat(*format) };
if (!avFormat) if (!avFormat)
return std::nullopt; return std::nullopt;
@@ -152,14 +152,14 @@ namespace lms::ui
return std::nullopt; return std::nullopt;
} }
parameters.inputParameters.trackPath = track->getAbsoluteFilePath(); parameters.inputParameters.file = track->getAbsoluteFilePath();
parameters.inputParameters.duration = track->getDuration(); parameters.inputParameters.duration = track->getDuration();
parameters.inputParameters.offset = std::chrono::seconds{ offset };
} }
parameters.outputParameters.stripMetadata = true; parameters.outputParameters.stripMetadata = true;
parameters.outputParameters.format = *avFormat; parameters.outputParameters.format = *avFormat;
parameters.outputParameters.bitrate = *bitrate; parameters.outputParameters.bitrate = *bitrate;
parameters.outputParameters.offset = std::chrono::seconds{ offset };
return parameters; return parameters;
} }
@@ -177,31 +177,24 @@ namespace lms::ui
void AudioTranscodingResource::handleRequest(const Wt::Http::Request& request, Wt::Http::Response& response) void AudioTranscodingResource::handleRequest(const Wt::Http::Request& request, Wt::Http::Response& response)
{ {
std::shared_ptr<IResourceHandler> resourceHandler; std::shared_ptr<core::IResourceHandler> resourceHandler;
try Wt::Http::ResponseContinuation* continuation{ request.continuation() };
if (!continuation)
{ {
Wt::Http::ResponseContinuation* continuation{ request.continuation() }; if (const auto& parameters{ readTranscodingParameters(request) })
if (!continuation) resourceHandler = core::Service<transcoding::ITranscodingService>::get()->createResourceHandler(parameters->inputParameters, parameters->outputParameters, false /* estimate content length */);
{
if (const auto& parameters{ readTranscodingParameters(request) })
resourceHandler = av::transcoding::createResourceHandler(parameters->inputParameters, parameters->outputParameters, false /* estimate content length */);
}
else
{
resourceHandler = Wt::cpp17::any_cast<std::shared_ptr<IResourceHandler>>(continuation->data());
}
if (resourceHandler)
{
continuation = resourceHandler->processRequest(request, response);
if (continuation)
continuation->setData(resourceHandler);
}
} }
catch (const av::Exception& e) else
{ {
TRANSCODE_LOG(ERROR, "Caught Av exception: " << e.what()); resourceHandler = Wt::cpp17::any_cast<std::shared_ptr<core::IResourceHandler>>(continuation->data());
}
if (resourceHandler)
{
continuation = resourceHandler->processRequest(request, response);
if (continuation)
continuation->setData(resourceHandler);
} }
} }