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
impl/AudioFile.cpp
impl/RawResourceHandlerCreator.cpp
impl/Transcoder.cpp
impl/TranscodingResourceHandler.cpp
)
target_include_directories(lmsav INTERFACE
+1 -39
View File
@@ -34,7 +34,7 @@ extern "C"
#include "core/ILogger.hpp"
#include "core/String.hpp"
#include "av/Types.hpp"
#include "av/Exception.hpp"
namespace lms::av
{
@@ -321,42 +321,4 @@ namespace lms::av
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
@@ -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/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)
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::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()
{
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!" };
}
Transcoder::Transcoder(const InputParameters& inputParameters, const OutputParameters& outputParameters)
Transcoder::Transcoder(const InputParameters& inputParams, const OutputParameters& outputParams)
: _debugId{ globalId++ }
, _inputParameters{ inputParameters }
, _outputParameters{ outputParameters }
, _inputParams{ inputParams }
, _outputParams{ outputParams }
{
start();
}
@@ -80,17 +65,18 @@ namespace lms::av::transcoding
try
{
if (!std::filesystem::exists(_inputParameters.trackPath))
throw Exception{ "File '" + _inputParameters.trackPath.string() + "' does not exist!" };
if (!std::filesystem::is_regular_file(_inputParameters.trackPath))
throw Exception{ "File '" + _inputParameters.trackPath.string() + "' is not regular!" };
if (!std::filesystem::exists(_inputParams.file))
throw Exception{ "File " + _inputParams.file.string() + " does not exist!" };
if (!std::filesystem::is_regular_file(_inputParams.file))
throw Exception{ "File " + _inputParams.file.string() + " is not regular!" };
}
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;
@@ -109,22 +95,22 @@ namespace lms::av::transcoding
args.emplace_back("-ss");
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());
}
// Input file
args.emplace_back("-i");
args.emplace_back(_inputParameters.trackPath.string());
args.emplace_back(_inputParams.file.string());
// Stream mapping, if set
if (_outputParameters.stream)
if (_inputParams.streamIndex)
{
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
args.emplace_back("-map_metadata");
@@ -136,10 +122,10 @@ namespace lms::av::transcoding
// Output bitrates
args.emplace_back("-b:a");
args.emplace_back(std::to_string(_outputParameters.bitrate));
args.emplace_back(std::to_string(_outputParams.bitrate));
// Codecs and formats
switch (_outputParameters.format)
switch (_outputParams.format)
{
case OutputFormat::MP3:
args.emplace_back("-f");
@@ -175,11 +161,9 @@ namespace lms::av::transcoding
break;
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");
LOG(DEBUG, "Dumping args (" << args.size() << ")");
@@ -213,6 +197,25 @@ namespace lms::av::transcoding
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
{
assert(_childProcess);
@@ -220,4 +223,4 @@ namespace lms::av::transcoding
return _childProcess->finished();
}
} // namespace lms::av::transcoding
} // namespace lms::av
+14 -24
View File
@@ -19,47 +19,37 @@
#pragma once
#include <functional>
#include "av/TranscodingParameters.hpp"
#include "av/ITranscoder.hpp"
namespace lms::core
{
class IChildProcess;
}
namespace lms::av::transcoding
namespace lms::av
{
class Transcoder
class Transcoder : public ITranscoder
{
public:
Transcoder(const InputParameters& inputParameters, const OutputParameters& outputParameters);
~Transcoder();
~Transcoder() override;
Transcoder(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:
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();
const std::size_t _debugId{};
const InputParameters _inputParameters;
const OutputParameters _outputParameters;
std::string _outputMimeType;
const InputParameters _inputParams;
const OutputParameters _outputParams;
std::unique_ptr<core::IChildProcess> _childProcess;
};
} // namespace lms::av::transcoding
} // namespace lms::av
@@ -1,99 +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/>.
*/
#include "TranscodingResourceHandler.hpp"
#include "core/ILogger.hpp"
namespace lms::av::transcoding
{
namespace
{
std::size_t doEstimateContentLength(const InputParameters& inputParameters, const OutputParameters& outputParameters)
{
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;
}
} // namespace
std::unique_ptr<IResourceHandler> createResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength)
{
return std::make_unique<TranscodingResourceHandler>(inputParameters, outputParameters, estimateContentLength);
}
// TODO set some nice HTTP return code
TranscodingResourceHandler::TranscodingResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength)
: _estimatedContentLength{ estimateContentLength ? std::make_optional(doEstimateContentLength(inputParameters, outputParameters)) : std::nullopt }
, _transcoder{ inputParameters, outputParameters }
{
if (_estimatedContentLength)
LMS_LOG(TRANSCODING, DEBUG, "Estimated content length = " << *_estimatedContentLength);
else
LMS_LOG(TRANSCODING, DEBUG, "Not using estimated content length");
}
Wt::Http::ResponseContinuation* TranscodingResourceHandler::processRequest(const Wt::Http::Request& /*request*/, Wt::Http::Response& response)
{
if (_estimatedContentLength)
response.setContentLength(*_estimatedContentLength);
response.setMimeType(_transcoder.getOutputMimeType());
LMS_LOG(TRANSCODING, DEBUG, "Transcoder finished = " << _transcoder.finished() << ", total served bytes = " << _totalServedByteCount << ", mime type = " << _transcoder.getOutputMimeType());
if (_bytesReadyCount > 0)
{
LMS_LOG(TRANSCODING, DEBUG, "Writing " << _bytesReadyCount << " bytes back to client");
response.out().write(reinterpret_cast<const char*>(_buffer.data()), _bytesReadyCount);
_totalServedByteCount += _bytesReadyCount;
_bytesReadyCount = 0;
}
if (!_transcoder.finished())
{
Wt::Http::ResponseContinuation* continuation{ response.createContinuation() };
continuation->waitForMoreData();
_transcoder.asyncRead(_buffer.data(), _buffer.size(), [this, continuation](std::size_t nbBytesRead) {
LMS_LOG(TRANSCODING, DEBUG, "Have " << nbBytesRead << " more bytes to send back");
assert(_bytesReadyCount == 0);
_bytesReadyCount = nbBytesRead;
continuation->haveMoreData();
});
return continuation;
}
// pad with 0 if necessary as duration may not be accurate
if (_estimatedContentLength && *_estimatedContentLength > _totalServedByteCount)
{
const std::size_t padSize{ *_estimatedContentLength - _totalServedByteCount };
LMS_LOG(TRANSCODING, DEBUG, "Adding " << padSize << " padding bytes");
for (std::size_t i{}; i < padSize; ++i)
response.out().put(0);
_totalServedByteCount += padSize;
}
LMS_LOG(TRANSCODING, DEBUG, "Transcoding finished. Total served byte count = " << _totalServedByteCount);
return {};
}
} // namespace lms::av::transcoding
@@ -1,52 +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 <array>
#include <optional>
#include "av/TranscodingParameters.hpp"
#include "core/IResourceHandler.hpp"
#include "Transcoder.hpp"
namespace lms::av::transcoding
{
class TranscodingResourceHandler final : public IResourceHandler
{
public:
TranscodingResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength);
~TranscodingResourceHandler() override = default;
TranscodingResourceHandler(const TranscodingResourceHandler&) = delete;
TranscodingResourceHandler& operator=(const TranscodingResourceHandler&) = delete;
private:
Wt::Http::ResponseContinuation* processRequest(const Wt::Http::Request& request, Wt::Http::Response& response) override;
void abort() override{};
static constexpr std::size_t _chunkSize{ 262'144 };
std::optional<std::size_t> _estimatedContentLength;
std::array<std::byte, _chunkSize> _buffer;
std::size_t _bytesReadyCount{};
std::size_t _totalServedByteCount{};
Transcoder _transcoder;
};
} // namespace lms::av::transcoding
-3
View File
@@ -101,7 +101,4 @@ namespace lms::av
};
std::unique_ptr<IAudioFile> parseAudioFile(const std::filesystem::path& p);
std::string_view getMimeType(const std::filesystem::path& fileExtension);
} // 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,30 +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/>.
*/
#pragma once
#include <filesystem>
#include <memory>
#include "core/IResourceHandler.hpp"
namespace lms::av
{
std::unique_ptr<IResourceHandler> createRawResourceHandler(const std::filesystem::path& path);
}
@@ -1,53 +0,0 @@
/*
* Copyright (C) 2015 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 <chrono>
#include <filesystem>
#include <optional>
namespace lms::av::transcoding
{
struct InputParameters
{
std::filesystem::path trackPath;
std::chrono::milliseconds duration; // used to estimate content length
};
enum class OutputFormat
{
MP3,
OGG_OPUS,
MATROSKA_OPUS,
OGG_VORBIS,
WEBM_VORBIS,
};
std::string_view toMimetype(OutputFormat format);
struct OutputParameters
{
OutputFormat format;
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 };
};
} // namespace lms::av::transcoding
@@ -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