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
+1
View File
@@ -4,3 +4,4 @@ add_subdirectory(feedback)
add_subdirectory(recommendation)
add_subdirectory(scanner)
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
)
@@ -0,0 +1,129 @@
/*
* 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 "av/Exception.hpp"
#include "av/ITranscoder.hpp"
#include "core/ILogger.hpp"
namespace lms::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;
}
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
std::unique_ptr<core::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 }
{
try
{
_transcoder = av::createTranscoder(toAv(inputParameters), toAv(outputParameters));
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)
{
if (!_transcoder)
{
response.setStatus(404);
return {};
}
if (_estimatedContentLength)
response.setContentLength(*_estimatedContentLength);
response.setMimeType(std::string{ _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::transcoding
@@ -0,0 +1,56 @@
/*
* 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 <memory>
#include <optional>
#include "core/IResourceHandler.hpp"
#include "services/transcoding/ITranscodingService.hpp"
namespace lms::av
{
class ITranscoder;
}
namespace lms::transcoding
{
class TranscodingResourceHandler final : public core::IResourceHandler
{
public:
TranscodingResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength);
~TranscodingResourceHandler() override;
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{};
std::unique_ptr<av::ITranscoder> _transcoder;
};
} // 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
@@ -0,0 +1,67 @@
/*
* 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 <filesystem>
#include <memory>
#include <optional>
namespace lms::core
{
class IChildProcessManager;
class IResourceHandler;
} // namespace lms::core
namespace lms::transcoding
{
struct InputParameters
{
std::filesystem::path file; // Path to the input file
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
{
MP3,
OGG_OPUS,
MATROSKA_OPUS,
OGG_VORBIS,
WEBM_VORBIS,
};
struct OutputParameters
{
OutputFormat format;
std::size_t bitrate{ 128'000 };
bool stripMetadata{ true };
};
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