Renamed some transcoding stuff + now retrieve decoder codecs
This commit is contained in:
@@ -65,6 +65,31 @@ namespace Av
|
||||
res[StringUtils::stringToUpper(tag->key)] = tag->value;
|
||||
}
|
||||
}
|
||||
|
||||
DecodingCodec avcodecToDecodingCodec(AVCodecID codec)
|
||||
{
|
||||
switch (codec)
|
||||
{
|
||||
case AV_CODEC_ID_MP3: return DecodingCodec::MP3;
|
||||
case AV_CODEC_ID_AAC: return DecodingCodec::AAC;
|
||||
case AV_CODEC_ID_AC3: return DecodingCodec::AC3;
|
||||
case AV_CODEC_ID_VORBIS: return DecodingCodec::VORBIS;
|
||||
case AV_CODEC_ID_WMAV1: return DecodingCodec::WMAV1;
|
||||
case AV_CODEC_ID_WMAV2: return DecodingCodec::WMAV2;
|
||||
case AV_CODEC_ID_FLAC: return DecodingCodec::FLAC;
|
||||
case AV_CODEC_ID_ALAC: return DecodingCodec::ALAC;
|
||||
case AV_CODEC_ID_WAVPACK: return DecodingCodec::WAVPACK;
|
||||
case AV_CODEC_ID_MUSEPACK7: return DecodingCodec::MUSEPACK7;
|
||||
case AV_CODEC_ID_MUSEPACK8: return DecodingCodec::MUSEPACK8;
|
||||
case AV_CODEC_ID_APE: return DecodingCodec::APE;
|
||||
case AV_CODEC_ID_EAC3: return DecodingCodec::EAC3;
|
||||
case AV_CODEC_ID_MP4ALS: return DecodingCodec::MP4ALS;
|
||||
case AV_CODEC_ID_OPUS: return DecodingCodec::OPUS;
|
||||
case AV_CODEC_ID_SHORTEN: return DecodingCodec::SHORTEN;
|
||||
default:
|
||||
return DecodingCodec::UNKNOWN;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<IAudioFile> parseAudioFile(const std::filesystem::path& p)
|
||||
@@ -254,14 +279,17 @@ namespace Av
|
||||
res.emplace();
|
||||
res->index = streamIndex;
|
||||
res->bitrate = static_cast<std::size_t>(avstream->codecpar->bit_rate);
|
||||
res->codec = ::avcodec_get_name(avstream->codecpar->codec_id);
|
||||
assert(!res->codec.empty());
|
||||
res->codec = avcodecToDecodingCodec(avstream->codecpar->codec_id);
|
||||
res->codecName = ::avcodec_get_name(avstream->codecpar->codec_id);
|
||||
assert(!res->codecName.empty()); // doc says it is never NULL
|
||||
|
||||
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
|
||||
static const std::unordered_map<std::filesystem::path, std::string_view> entries
|
||||
{
|
||||
{".mp3", "audio/mpeg"},
|
||||
|
||||
@@ -1,106 +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 "TranscodeResourceHandler.hpp"
|
||||
#include "utils/Logger.hpp"
|
||||
|
||||
namespace Av
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::size_t
|
||||
doEstimateContentLength(const InputFileParameters& inputFileParameters, const TranscodeParameters& transcodeParameters)
|
||||
{
|
||||
const std::size_t estimatedContentLength {transcodeParameters.bitrate / 8 * static_cast<std::size_t>(std::chrono::duration_cast<std::chrono::milliseconds>(inputFileParameters.duration).count()) / 1000};
|
||||
return estimatedContentLength;
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<IResourceHandler>
|
||||
createTranscodeResourceHandler(const InputFileParameters& inputFileParameters, const TranscodeParameters& transcodeParameters, bool estimateContentLength)
|
||||
{
|
||||
return std::make_unique<TranscodeResourceHandler>(inputFileParameters, transcodeParameters, estimateContentLength);
|
||||
}
|
||||
|
||||
// TODO set some nice HTTP return code
|
||||
|
||||
TranscodeResourceHandler::TranscodeResourceHandler(const InputFileParameters& inputFileParameters, const TranscodeParameters& transcodeParameters, bool estimateContentLength)
|
||||
: _estimatedContentLength {estimateContentLength ? std::make_optional(doEstimateContentLength(inputFileParameters, transcodeParameters)) : std::nullopt}
|
||||
, _transcoder {inputFileParameters, transcodeParameters}
|
||||
{
|
||||
if (_estimatedContentLength)
|
||||
LMS_LOG(TRANSCODE, DEBUG) << "Estimated content length = " << *_estimatedContentLength;
|
||||
else
|
||||
LMS_LOG(TRANSCODE, DEBUG) << "Not using estimated content length";
|
||||
}
|
||||
|
||||
Wt::Http::ResponseContinuation*
|
||||
TranscodeResourceHandler::processRequest(const Wt::Http::Request& /*request*/, Wt::Http::Response& response)
|
||||
{
|
||||
if (_estimatedContentLength)
|
||||
response.setContentLength(*_estimatedContentLength);
|
||||
response.setMimeType(_transcoder.getOutputMimeType());
|
||||
LMS_LOG(TRANSCODE, DEBUG) << "Transcoder finished = " << _transcoder.finished() << ", total served bytes = " << _totalServedByteCount << ", mime type = " << _transcoder.getOutputMimeType();
|
||||
|
||||
if (_bytesReadyCount > 0)
|
||||
{
|
||||
LMS_LOG(TRANSCODE, DEBUG) << "Writing " << _bytesReadyCount << " bytes back to client";
|
||||
|
||||
response.out().write(reinterpret_cast<const char *>(&_buffer[0]), _bytesReadyCount);
|
||||
_totalServedByteCount += _bytesReadyCount;
|
||||
_bytesReadyCount = 0;
|
||||
}
|
||||
|
||||
if (!_transcoder.finished())
|
||||
{
|
||||
Wt::Http::ResponseContinuation *continuation {response.createContinuation()};
|
||||
continuation->waitForMoreData();
|
||||
_transcoder.asyncRead(_buffer.data(), _buffer.size(), [=](std::size_t nbBytesRead)
|
||||
{
|
||||
LMS_LOG(TRANSCODE, DEBUG) << "Have " << nbBytesRead << " more bytes to send back";
|
||||
|
||||
assert(_bytesReadyCount == 0);
|
||||
_bytesReadyCount = nbBytesRead;
|
||||
continuation->haveMoreData();
|
||||
});
|
||||
|
||||
return continuation;
|
||||
}
|
||||
else
|
||||
{
|
||||
// pad with 0 if necessary as duration may not be accurate
|
||||
if (_estimatedContentLength && *_estimatedContentLength > _totalServedByteCount)
|
||||
{
|
||||
const std::size_t padSize {*_estimatedContentLength - _totalServedByteCount};
|
||||
|
||||
LMS_LOG(TRANSCODE, DEBUG) << "Adding " << padSize << " padding bytes";
|
||||
|
||||
for (std::size_t i {}; i < padSize; ++i)
|
||||
response.out().put(0);
|
||||
|
||||
_totalServedByteCount += padSize;
|
||||
}
|
||||
|
||||
LMS_LOG(TRANSCODE, DEBUG) << "Transcoding finished. Total served byte count = " << _totalServedByteCount;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
+152
-142
@@ -28,181 +28,191 @@
|
||||
#include "utils/Logger.hpp"
|
||||
#include "utils/Service.hpp"
|
||||
|
||||
namespace Av {
|
||||
namespace Av::Transcoding
|
||||
{
|
||||
|
||||
#define LOG(sev) LMS_LOG(TRANSCODE, sev) << "[" << _debugId << "] - "
|
||||
|
||||
static std::atomic<size_t> globalId {};
|
||||
static std::filesystem::path ffmpegPath;
|
||||
static std::atomic<size_t> globalId{};
|
||||
static std::filesystem::path ffmpegPath;
|
||||
|
||||
void
|
||||
Transcoder::init()
|
||||
{
|
||||
ffmpegPath = Service<IConfig>::get()->getPath("ffmpeg-file", "/usr/bin/ffmpeg");
|
||||
if (!std::filesystem::exists(ffmpegPath))
|
||||
throw Exception {"File '" + ffmpegPath.string() + "' does not exist!"};
|
||||
}
|
||||
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";
|
||||
}
|
||||
|
||||
Transcoder::Transcoder(const InputFileParameters& inputFileParameters, const TranscodeParameters& transcodeParameters)
|
||||
: _debugId {globalId++}
|
||||
, _inputFileParameters {inputFileParameters}
|
||||
, _transcodeParameters {transcodeParameters}
|
||||
{
|
||||
start();
|
||||
}
|
||||
throw Exception{ "Invalid encoding" };
|
||||
}
|
||||
|
||||
Transcoder::~Transcoder() = default;
|
||||
void Transcoder::init()
|
||||
{
|
||||
ffmpegPath = Service<IConfig>::get()->getPath("ffmpeg-file", "/usr/bin/ffmpeg");
|
||||
if (!std::filesystem::exists(ffmpegPath))
|
||||
throw Exception{ "File '" + ffmpegPath.string() + "' does not exist!" };
|
||||
}
|
||||
|
||||
void
|
||||
Transcoder::start()
|
||||
{
|
||||
if (ffmpegPath.empty())
|
||||
init();
|
||||
Transcoder::Transcoder(const InputParameters& inputParameters, const OutputParameters& outputParameters)
|
||||
: _debugId{ globalId++ }
|
||||
, _inputParameters{ inputParameters }
|
||||
, _outputParameters{ outputParameters }
|
||||
{
|
||||
start();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (!std::filesystem::exists(_inputFileParameters.trackPath))
|
||||
throw Exception {"File '" + _inputFileParameters.trackPath.string() + "' does not exist!"};
|
||||
else if (!std::filesystem::is_regular_file( _inputFileParameters.trackPath) )
|
||||
throw Exception {"File '" + _inputFileParameters.trackPath.string() + "' is not regular!"};
|
||||
}
|
||||
catch (const std::filesystem::filesystem_error& e)
|
||||
{
|
||||
throw Exception {"File error '" + _inputFileParameters.trackPath.string() + "': " + e.what()};
|
||||
}
|
||||
Transcoder::~Transcoder() = default;
|
||||
|
||||
LOG(INFO) << "Transcoding file '" << _inputFileParameters.trackPath.string() << "'";
|
||||
void Transcoder::start()
|
||||
{
|
||||
if (ffmpegPath.empty())
|
||||
init();
|
||||
|
||||
std::vector<std::string> args;
|
||||
try
|
||||
{
|
||||
if (!std::filesystem::exists(_inputParameters.trackPath))
|
||||
throw Exception{ "File '" + _inputParameters.trackPath.string() + "' does not exist!" };
|
||||
else if (!std::filesystem::is_regular_file(_inputParameters.trackPath))
|
||||
throw Exception{ "File '" + _inputParameters.trackPath.string() + "' is not regular!" };
|
||||
}
|
||||
catch (const std::filesystem::filesystem_error& e)
|
||||
{
|
||||
throw Exception{ "File error '" + _inputParameters.trackPath.string() + "': " + e.what() };
|
||||
}
|
||||
|
||||
args.emplace_back(ffmpegPath.string());
|
||||
LOG(INFO) << "Transcoding file '" << _inputParameters.trackPath.string() << "'";
|
||||
|
||||
// Make sure:
|
||||
// - we do not produce anything in the stderr output
|
||||
// - we do not rely on input
|
||||
// in order not to block the whole forked process
|
||||
args.emplace_back("-loglevel");
|
||||
args.emplace_back("quiet");
|
||||
args.emplace_back("-nostdin");
|
||||
std::vector<std::string> args;
|
||||
|
||||
// input Offset
|
||||
{
|
||||
args.emplace_back("-ss");
|
||||
args.emplace_back(ffmpegPath.string());
|
||||
|
||||
std::ostringstream oss;
|
||||
oss << std::fixed << std::showpoint << std::setprecision(3) << (_transcodeParameters.offset.count() / float {1000});
|
||||
args.emplace_back(oss.str());
|
||||
}
|
||||
// Make sure:
|
||||
// - we do not produce anything in the stderr output
|
||||
// - we do not rely on input
|
||||
// in order not to block the whole forked process
|
||||
args.emplace_back("-loglevel");
|
||||
args.emplace_back("quiet");
|
||||
args.emplace_back("-nostdin");
|
||||
|
||||
// Input file
|
||||
args.emplace_back("-i");
|
||||
args.emplace_back(_inputFileParameters.trackPath.string());
|
||||
// input Offset
|
||||
{
|
||||
args.emplace_back("-ss");
|
||||
|
||||
// Stream mapping, if set
|
||||
if (_transcodeParameters.stream)
|
||||
{
|
||||
args.emplace_back("-map");
|
||||
args.emplace_back("0:" + std::to_string(*_transcodeParameters.stream));
|
||||
}
|
||||
std::ostringstream oss;
|
||||
oss << std::fixed << std::showpoint << std::setprecision(3) << (_outputParameters.offset.count() / float{ 1000 });
|
||||
args.emplace_back(oss.str());
|
||||
}
|
||||
|
||||
if (_transcodeParameters.stripMetadata)
|
||||
{
|
||||
// Strip metadata
|
||||
args.emplace_back("-map_metadata");
|
||||
args.emplace_back("-1");
|
||||
}
|
||||
// Input file
|
||||
args.emplace_back("-i");
|
||||
args.emplace_back(_inputParameters.trackPath.string());
|
||||
|
||||
// Skip video flows (including covers)
|
||||
args.emplace_back("-vn");
|
||||
// Stream mapping, if set
|
||||
if (_outputParameters.stream)
|
||||
{
|
||||
args.emplace_back("-map");
|
||||
args.emplace_back("0:" + std::to_string(*_outputParameters.stream));
|
||||
}
|
||||
|
||||
// Output bitrates
|
||||
args.emplace_back("-b:a");
|
||||
args.emplace_back(std::to_string(_transcodeParameters.bitrate));
|
||||
if (_outputParameters.stripMetadata)
|
||||
{
|
||||
// Strip metadata
|
||||
args.emplace_back("-map_metadata");
|
||||
args.emplace_back("-1");
|
||||
}
|
||||
|
||||
// Codecs and formats
|
||||
switch (_transcodeParameters.format)
|
||||
{
|
||||
case Format::MP3:
|
||||
args.emplace_back("-f");
|
||||
args.emplace_back("mp3");
|
||||
break;
|
||||
// Skip video flows (including covers)
|
||||
args.emplace_back("-vn");
|
||||
|
||||
case Format::OGG_OPUS:
|
||||
args.emplace_back("-acodec");
|
||||
args.emplace_back("libopus");
|
||||
args.emplace_back("-f");
|
||||
args.emplace_back("ogg");
|
||||
break;
|
||||
// Output bitrates
|
||||
args.emplace_back("-b:a");
|
||||
args.emplace_back(std::to_string(_outputParameters.bitrate));
|
||||
|
||||
case Format::MATROSKA_OPUS:
|
||||
args.emplace_back("-acodec");
|
||||
args.emplace_back("libopus");
|
||||
args.emplace_back("-f");
|
||||
args.emplace_back("matroska");
|
||||
break;
|
||||
// Codecs and formats
|
||||
switch (_outputParameters.format)
|
||||
{
|
||||
case OutputFormat::MP3:
|
||||
args.emplace_back("-f");
|
||||
args.emplace_back("mp3");
|
||||
break;
|
||||
|
||||
case Format::OGG_VORBIS:
|
||||
args.emplace_back("-acodec");
|
||||
args.emplace_back("libvorbis");
|
||||
args.emplace_back("-f");
|
||||
args.emplace_back("ogg");
|
||||
break;
|
||||
case OutputFormat::OGG_OPUS:
|
||||
args.emplace_back("-acodec");
|
||||
args.emplace_back("libopus");
|
||||
args.emplace_back("-f");
|
||||
args.emplace_back("ogg");
|
||||
break;
|
||||
|
||||
case Format::WEBM_VORBIS:
|
||||
args.emplace_back("-acodec");
|
||||
args.emplace_back("libvorbis");
|
||||
args.emplace_back("-f");
|
||||
args.emplace_back("webm");
|
||||
break;
|
||||
case OutputFormat::MATROSKA_OPUS:
|
||||
args.emplace_back("-acodec");
|
||||
args.emplace_back("libopus");
|
||||
args.emplace_back("-f");
|
||||
args.emplace_back("matroska");
|
||||
break;
|
||||
|
||||
default:
|
||||
throw Exception {"Unhandled format (" + std::to_string(static_cast<int>(_transcodeParameters.format)) + ")"};
|
||||
}
|
||||
case OutputFormat::OGG_VORBIS:
|
||||
args.emplace_back("-acodec");
|
||||
args.emplace_back("libvorbis");
|
||||
args.emplace_back("-f");
|
||||
args.emplace_back("ogg");
|
||||
break;
|
||||
|
||||
_outputMimeType = formatToMimetype(_transcodeParameters.format);
|
||||
case OutputFormat::WEBM_VORBIS:
|
||||
args.emplace_back("-acodec");
|
||||
args.emplace_back("libvorbis");
|
||||
args.emplace_back("-f");
|
||||
args.emplace_back("webm");
|
||||
break;
|
||||
|
||||
args.emplace_back("pipe:1");
|
||||
default:
|
||||
throw Exception{ "Unhandled format (" + std::to_string(static_cast<int>(_outputParameters.format)) + ")" };
|
||||
}
|
||||
|
||||
LOG(DEBUG) << "Dumping args (" << args.size() << ")";
|
||||
for (const std::string& arg : args)
|
||||
LOG(DEBUG) << "Arg = '" << arg << "'";
|
||||
_outputMimeType = formatToMimetype(_outputParameters.format);
|
||||
|
||||
// Caution: stdin must have been closed before
|
||||
try
|
||||
{
|
||||
_childProcess = Service<IChildProcessManager>::get()->spawnChildProcess(ffmpegPath, args);
|
||||
}
|
||||
catch (ChildProcessException& exception)
|
||||
{
|
||||
throw Exception {"Cannot execute '" + ffmpegPath.string() + "': " + exception.what()};
|
||||
}
|
||||
}
|
||||
args.emplace_back("pipe:1");
|
||||
|
||||
void
|
||||
Transcoder::asyncRead(std::byte* buffer, std::size_t bufferSize, ReadCallback readCallback)
|
||||
{
|
||||
assert(_childProcess);
|
||||
LOG(DEBUG) << "Dumping args (" << args.size() << ")";
|
||||
for (const std::string& arg : args)
|
||||
LOG(DEBUG) << "Arg = '" << arg << "'";
|
||||
|
||||
return _childProcess->asyncRead(buffer, bufferSize, [readCallback {std::move(readCallback)}](IChildProcess::ReadResult /*res*/, std::size_t nbBytesRead)
|
||||
{
|
||||
readCallback(nbBytesRead);
|
||||
});
|
||||
}
|
||||
// Caution: stdin must have been closed before
|
||||
try
|
||||
{
|
||||
_childProcess = Service<IChildProcessManager>::get()->spawnChildProcess(ffmpegPath, args);
|
||||
}
|
||||
catch (ChildProcessException& exception)
|
||||
{
|
||||
throw Exception{ "Cannot execute '" + ffmpegPath.string() + "': " + exception.what() };
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t
|
||||
Transcoder::readSome(std::byte* buffer, std::size_t bufferSize)
|
||||
{
|
||||
assert(_childProcess);
|
||||
void Transcoder::asyncRead(std::byte* buffer, std::size_t bufferSize, ReadCallback readCallback)
|
||||
{
|
||||
assert(_childProcess);
|
||||
|
||||
return _childProcess->readSome(buffer, bufferSize);
|
||||
}
|
||||
return _childProcess->asyncRead(buffer, bufferSize, [readCallback{ std::move(readCallback) }](IChildProcess::ReadResult /*res*/, std::size_t nbBytesRead)
|
||||
{
|
||||
readCallback(nbBytesRead);
|
||||
});
|
||||
}
|
||||
|
||||
bool
|
||||
Transcoder::finished() const
|
||||
{
|
||||
assert(_childProcess);
|
||||
std::size_t Transcoder::readSome(std::byte* buffer, std::size_t bufferSize)
|
||||
{
|
||||
assert(_childProcess);
|
||||
|
||||
return _childProcess->finished();
|
||||
}
|
||||
return _childProcess->readSome(buffer, bufferSize);
|
||||
}
|
||||
|
||||
} // namespace Transcode
|
||||
bool Transcoder::finished() const
|
||||
{
|
||||
assert(_childProcess);
|
||||
|
||||
return _childProcess->finished();
|
||||
}
|
||||
|
||||
} // namespace Av::Transcoding
|
||||
|
||||
@@ -22,47 +22,45 @@
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
|
||||
#include "av/TranscodeParameters.hpp"
|
||||
#include "av/TranscodingParameters.hpp"
|
||||
#include "av/Types.hpp"
|
||||
|
||||
class IChildProcess;
|
||||
|
||||
namespace Av
|
||||
namespace Av::Transcoding
|
||||
{
|
||||
class Transcoder
|
||||
{
|
||||
public:
|
||||
Transcoder(const InputFileParameters& inputFileParameters, const TranscodeParameters& transcodeParameters);
|
||||
~Transcoder();
|
||||
class Transcoder
|
||||
{
|
||||
public:
|
||||
Transcoder(const InputParameters& inputParameters, const OutputParameters& outputParameters);
|
||||
~Transcoder();
|
||||
|
||||
Transcoder(const Transcoder&) = delete;
|
||||
Transcoder& operator=(const Transcoder&) = delete;
|
||||
Transcoder(Transcoder&&) = delete;
|
||||
Transcoder& operator=(Transcoder&&) = delete;
|
||||
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);
|
||||
// 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 TranscodeParameters& getParameters() const { return _transcodeParameters; }
|
||||
const std::string& getOutputMimeType() const { return _outputMimeType; }
|
||||
const OutputParameters& getOutputParameters() const { return _outputParameters; }
|
||||
|
||||
bool finished() const;
|
||||
bool finished() const;
|
||||
|
||||
private:
|
||||
static void init();
|
||||
private:
|
||||
static void init();
|
||||
|
||||
void start();
|
||||
void start();
|
||||
|
||||
const std::size_t _debugId {};
|
||||
const InputFileParameters _inputFileParameters;
|
||||
const TranscodeParameters _transcodeParameters;
|
||||
const std::size_t _debugId{};
|
||||
const InputParameters _inputParameters;
|
||||
const OutputParameters _outputParameters;
|
||||
std::string _outputMimeType;
|
||||
|
||||
std::unique_ptr<IChildProcess> _childProcess;
|
||||
|
||||
std::string _outputMimeType;
|
||||
};
|
||||
|
||||
} // namespace Av
|
||||
std::unique_ptr<IChildProcess> _childProcess;
|
||||
};
|
||||
|
||||
} // namespace Av::Transcoding
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* 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 "utils/Logger.hpp"
|
||||
|
||||
namespace 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;
|
||||
}
|
||||
}
|
||||
|
||||
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(TRANSCODE, DEBUG) << "Estimated content length = " << *_estimatedContentLength;
|
||||
else
|
||||
LMS_LOG(TRANSCODE, 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(TRANSCODE, DEBUG) << "Transcoder finished = " << _transcoder.finished() << ", total served bytes = " << _totalServedByteCount << ", mime type = " << _transcoder.getOutputMimeType();
|
||||
|
||||
if (_bytesReadyCount > 0)
|
||||
{
|
||||
LMS_LOG(TRANSCODE, DEBUG) << "Writing " << _bytesReadyCount << " bytes back to client";
|
||||
|
||||
response.out().write(reinterpret_cast<const char*>(&_buffer[0]), _bytesReadyCount);
|
||||
_totalServedByteCount += _bytesReadyCount;
|
||||
_bytesReadyCount = 0;
|
||||
}
|
||||
|
||||
if (!_transcoder.finished())
|
||||
{
|
||||
Wt::Http::ResponseContinuation* continuation{ response.createContinuation() };
|
||||
continuation->waitForMoreData();
|
||||
_transcoder.asyncRead(_buffer.data(), _buffer.size(), [=](std::size_t nbBytesRead)
|
||||
{
|
||||
LMS_LOG(TRANSCODE, DEBUG) << "Have " << nbBytesRead << " more bytes to send back";
|
||||
|
||||
assert(_bytesReadyCount == 0);
|
||||
_bytesReadyCount = nbBytesRead;
|
||||
continuation->haveMoreData();
|
||||
});
|
||||
|
||||
return continuation;
|
||||
}
|
||||
else
|
||||
{
|
||||
// pad with 0 if necessary as duration may not be accurate
|
||||
if (_estimatedContentLength && *_estimatedContentLength > _totalServedByteCount)
|
||||
{
|
||||
const std::size_t padSize{ *_estimatedContentLength - _totalServedByteCount };
|
||||
|
||||
LMS_LOG(TRANSCODE, DEBUG) << "Adding " << padSize << " padding bytes";
|
||||
|
||||
for (std::size_t i{}; i < padSize; ++i)
|
||||
response.out().put(0);
|
||||
|
||||
_totalServedByteCount += padSize;
|
||||
}
|
||||
|
||||
LMS_LOG(TRANSCODE, DEBUG) << "Transcoding finished. Total served byte count = " << _totalServedByteCount;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -23,16 +23,16 @@
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
|
||||
#include "av/TranscodeParameters.hpp"
|
||||
#include "av/TranscodingParameters.hpp"
|
||||
#include "utils/IResourceHandler.hpp"
|
||||
#include "Transcoder.hpp"
|
||||
|
||||
namespace Av
|
||||
namespace Av::Transcoding
|
||||
{
|
||||
class TranscodeResourceHandler final : public IResourceHandler
|
||||
class TranscodingResourceHandler final : public IResourceHandler
|
||||
{
|
||||
public:
|
||||
TranscodeResourceHandler(const InputFileParameters& inputFileParameters, const TranscodeParameters& parameters, bool estimateContentLength);
|
||||
TranscodingResourceHandler(const InputParameters& inputParameters, const OutputParameters& outputParameters, bool estimateContentLength);
|
||||
|
||||
private:
|
||||
Wt::Http::ResponseContinuation* processRequest(const Wt::Http::Request& request, Wt::Http::Response& reponse) override;
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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/Types.hpp"
|
||||
|
||||
namespace Av
|
||||
{
|
||||
|
||||
std::string_view
|
||||
formatToMimetype(Format format)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
case Format::MP3: return "audio/mpeg";
|
||||
case Format::OGG_OPUS: return "audio/opus";
|
||||
case Format::MATROSKA_OPUS: return "audio/x-matroska";
|
||||
case Format::OGG_VORBIS: return "audio/ogg";
|
||||
case Format::WEBM_VORBIS: return "audio/webm";
|
||||
}
|
||||
|
||||
throw Exception {"Invalid encoding"};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user