Using track id for the transcoding service

This commit is contained in:
emeric
2025-05-31 10:27:53 +02:00
parent dc52ea4ea5
commit 2557402367
12 changed files with 88 additions and 71 deletions
@@ -15,3 +15,8 @@ target_include_directories(lmstranscoding PRIVATE
target_link_libraries(lmstranscoding PRIVATE
lmsav
)
target_link_libraries(lmstranscoding PUBLIC
lmsdatabase
)
@@ -25,39 +25,19 @@
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)
std::unique_ptr<core::IResourceHandler> createResourceHandler(const av::InputParameters& inputParameters, const av::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 }
TranscodingResourceHandler::TranscodingResourceHandler(const av::InputParameters& inputParameters, const av::OutputParameters& outputParameters, std::optional<std::size_t> estimatedContentLength)
: _estimatedContentLength{ estimatedContentLength }
{
try
{
_transcoder = av::createTranscoder(toAv(inputParameters), toAv(outputParameters));
_transcoder = av::createTranscoder(inputParameters, outputParameters);
if (_estimatedContentLength)
LMS_LOG(TRANSCODING, DEBUG, "Estimated content length = " << *_estimatedContentLength);
@@ -23,20 +23,15 @@
#include <memory>
#include <optional>
#include "av/ITranscoder.hpp"
#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(const av::InputParameters& inputParameters, const av::OutputParameters& outputParameters, std::optional<std::size_t> estimatedContentLength);
~TranscodingResourceHandler() override;
TranscodingResourceHandler(const TranscodingResourceHandler&) = delete;
@@ -19,19 +19,38 @@
#include "TranscodingService.hpp"
#include "av/ITranscoder.hpp"
#include "core/ILogger.hpp"
#include "database/Db.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "TranscodingResourceHandler.hpp"
namespace lms::transcoding
{
std::unique_ptr<ITranscodingService> createTranscodingService(core::IChildProcessManager& childProcessManager)
namespace
{
return std::make_unique<TranscodingService>(childProcessManager);
av::OutputParameters toAv(const OutputParameters& out)
{
return { .format = static_cast<lms::av::OutputFormat>(out.format), .bitrate = out.bitrate, .stripMetadata = out.stripMetadata };
}
std::size_t doEstimateContentLength(std::size_t bitrate, std::chrono::milliseconds duration)
{
const std::size_t estimatedContentLength{ (bitrate / 8 * duration.count()) / 1000 };
return estimatedContentLength;
}
} // namespace
std::unique_ptr<ITranscodingService> createTranscodingService(db::Db& db, core::IChildProcessManager& childProcessManager)
{
return std::make_unique<TranscodingService>(db, childProcessManager);
}
TranscodingService::TranscodingService(core::IChildProcessManager& childProcessManager)
: _childProcessManager(childProcessManager)
TranscodingService::TranscodingService(db::Db& db, core::IChildProcessManager& childProcessManager)
: _db{ db }
, _childProcessManager(childProcessManager)
{
LMS_LOG(TRANSCODING, INFO, "Service started!");
}
@@ -43,6 +62,24 @@ namespace lms::transcoding
std::unique_ptr<core::IResourceHandler> TranscodingService::createResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength)
{
return std::make_unique<TranscodingResourceHandler>(inputParameters, outputParameters, estimateContentLength);
av::InputParameters avInputParams;
std::optional<std::size_t> estimatedContentLength;
{
auto& session{ _db.getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::Track::pointer track{ db::Track::find(session, inputParameters.trackId) };
if (!track)
return nullptr;
avInputParams.file = track->getAbsoluteFilePath();
avInputParams.offset = inputParameters.offset;
avInputParams.streamIndex = inputParameters.streamIndex;
if (estimateContentLength)
estimatedContentLength = doEstimateContentLength(outputParameters.bitrate, track->getDuration());
}
return std::make_unique<TranscodingResourceHandler>(avInputParams, toAv(outputParameters), estimatedContentLength);
}
} // namespace lms::transcoding
@@ -26,7 +26,7 @@ namespace lms::transcoding
class TranscodingService : public ITranscodingService
{
public:
explicit TranscodingService(core::IChildProcessManager& childProcessManager);
explicit TranscodingService(db::Db& db, core::IChildProcessManager& childProcessManager);
~TranscodingService() override;
TranscodingService(const TranscodingService&) = delete;
@@ -35,6 +35,7 @@ namespace lms::transcoding
private:
std::unique_ptr<core::IResourceHandler> createResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength) override;
db::Db& _db;
core::IChildProcessManager& _childProcessManager;
};
} // namespace lms::transcoding
@@ -23,19 +23,28 @@
#include <memory>
#include <optional>
namespace lms::core
#include "database/TrackId.hpp"
namespace lms
{
class IChildProcessManager;
class IResourceHandler;
} // namespace lms::core
namespace core
{
class IChildProcessManager;
class IResourceHandler;
} // namespace core
namespace db
{
class Db;
}
} // namespace lms
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
db::TrackId trackId;
std::chrono::milliseconds offset{}; // Offset in the track 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)
};
@@ -63,5 +72,5 @@ namespace lms::transcoding
virtual std::unique_ptr<core::IResourceHandler> createResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength) = 0;
};
std::unique_ptr<ITranscodingService> createTranscodingService(core::IChildProcessManager& childProcessManager);
std::unique_ptr<ITranscodingService> createTranscodingService(db::Db& db, core::IChildProcessManager& childProcessManager);
} // namespace lms::transcoding