Made STB the default image manipulation library to reduce memory usage (GraphicsMagick can still be selected). closes #93

This commit is contained in:
emeric
2020-10-17 14:58:41 +02:00
parent e79dae48d1
commit c04f57ceb5
63 changed files with 866 additions and 545 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ if (PAM_FOUND)
target_sources(lmsauth PRIVATE impl/pam/PAM.cpp)
target_include_directories(lmsauth PRIVATE ${PAM_INCLUDE_DIR})
target_link_libraries(lmsauth PRIVATE ${PAM_LIBRARIES})
endif(PAM_FOUND)
endif (PAM_FOUND)
install(TARGETS lmsauth DESTINATION lib)
+1 -1
View File
@@ -39,7 +39,7 @@ static std::string averror_to_string(int error)
std::array<char, 128> buf = {0};
if (av_strerror(error, buf.data(), buf.size()) == 0)
return std::string(&buf[0]);
return &buf[0];
else
return "Unknown error";
}
+23 -8
View File
@@ -1,8 +1,6 @@
add_library(lmscover SHARED
impl/CoverArt.cpp
impl/CoverArtGrabber.cpp
impl/Image.cpp
)
target_include_directories(lmscover INTERFACE
@@ -11,22 +9,39 @@ target_include_directories(lmscover INTERFACE
target_include_directories(lmscover PRIVATE
include
${GRAPHICSMAGICKXX_INCLUDE_DIRS}
)
target_compile_options(lmscover PRIVATE
${GRAPHICSMAGICKXX_CFLAGS_OTHER}
)
target_link_libraries(lmscover PRIVATE
lmsav
${GRAPHICSMAGICKXX_LIBRARIES}
)
target_link_libraries(lmscover PUBLIC
lmsdatabase
lmsutils
std::filesystem
)
if (IMAGE_LIBRARY STREQUAL STB)
target_sources(lmscover PRIVATE
impl/stb/JPEGImage.cpp
impl/stb/RawImage.cpp
)
target_compile_options(lmscover PRIVATE "-DLMS_SUPPORT_IMAGE_STB")
target_include_directories(lmscover PRIVATE ${STB_INCLUDE_DIR})
elseif (IMAGE_LIBRARY STREQUAL GraphicksMagick++)
target_sources(lmscover PRIVATE
impl/graphicsmagick/JPEGImage.cpp
impl/graphicsmagick/RawImage.cpp
)
target_compile_options(lmscover PRIVATE "-DLMS_SUPPORT_IMAGE_GM")
target_include_directories(lmscover PRIVATE ${GRAPHICSMAGICKXX_INCLUDE_DIRS})
target_compile_options(lmscover PRIVATE ${GRAPHICSMAGICKXX_CFLAGS_OTHER})
target_link_libraries(lmscover PRIVATE ${GRAPHICSMAGICKXX_LIBRARIES})
else ()
message(FATAL_ERROR "Invalid IMAGE_LIBRARY provided")
endif()
target_include_directories(lmscover PRIVATE impl)
install(TARGETS lmscover DESTINATION lib)
+85 -93
View File
@@ -25,58 +25,71 @@
#include "database/Session.hpp"
#include "database/Track.hpp"
#if LMS_SUPPORT_IMAGE_STB
#include "stb/RawImage.hpp"
using RawImage = CoverArt::STB::RawImage;
#elif LMS_SUPPORT_IMAGE_GM
#include "graphicsmagick/RawImage.hpp"
using RawImage = CoverArt::GraphicsMagick::RawImage;
#endif
#include "utils/Logger.hpp"
#include "utils/Random.hpp"
#include "utils/Utils.hpp"
#include "Exception.hpp"
#include "CoverArt.hpp"
namespace {
namespace CoverArt {
static
bool
isFileSupported(const std::filesystem::path& file, const std::vector<std::filesystem::path>& extensions)
{
return (std::find(std::cbegin(extensions), std::cend(extensions), file.extension()) != std::cend(extensions));
}
} // namespace
namespace CoverArt {
std::unique_ptr<IGrabber> createGrabber(const std::filesystem::path& execPath, std::size_t maxCacheSize, std::size_t maxFileSize)
std::unique_ptr<IGrabber>
createGrabber(const std::filesystem::path& execPath,
const std::filesystem::path& defaultCoverPath,
std::size_t maxCacheSize, std::size_t maxFileSize, unsigned jpegQuality)
{
return std::make_unique<Grabber>(execPath, maxCacheSize, maxFileSize);
return std::make_unique<Grabber>(execPath, defaultCoverPath, maxCacheSize, maxFileSize, jpegQuality);
}
Grabber::Grabber(const std::filesystem::path& execPath,
const std::filesystem::path& defaultCoverPath,
std::size_t maxCacheSize,
std::size_t maxFileSize)
: _maxCacheSize {maxCacheSize}
std::size_t maxFileSize,
unsigned jpegQuality)
: _defaultCoverPath {defaultCoverPath}
, _maxCacheSize {maxCacheSize}
, _maxFileSize {maxFileSize}
, _jpegQuality {clamp<unsigned>(jpegQuality, 1, 100)}
{
LMS_LOG(COVER, INFO) << "Default cover path = '" << _defaultCoverPath.string() << "'";
LMS_LOG(COVER, INFO) << "Max cache size = " << _maxCacheSize;
LMS_LOG(COVER, INFO) << "Max file size = " << _maxFileSize;
init(execPath);
}
LMS_LOG(COVER, INFO) << "JPEG export quality = " << _jpegQuality;
#if LMS_SUPPORT_IMAGE_GM
GraphicsMagick::init(execPath);
#else
(void)execPath;
#endif
void
Grabber::setDefaultCover(const std::filesystem::path& p)
{
try
{
RawImage defaultCover {p};
_defaultCover = defaultCover.encode();
LMS_LOG(COVER, INFO) << "Successfully read default cover image!";
getDefault(512);
}
catch (const ImageException& e)
{
throw LmsException("Cannot read default cover file '" + p.string() + "'");
throw LmsException("Cannot read default cover file '" + _defaultCoverPath.string() + "': " + e.what());
}
}
static std::optional<EncodedImage>
getFromAvMediaFile(const Av::MediaFile& input, Width width)
std::unique_ptr<IEncodedImage>
Grabber::getFromAvMediaFile(const Av::MediaFile& input, ImageSize width) const
{
std::optional<EncodedImage> image;
std::unique_ptr<IEncodedImage> image;
input.visitAttachedPictures([&](const Av::Picture& picture)
{
@@ -85,12 +98,9 @@ getFromAvMediaFile(const Av::MediaFile& input, Width width)
try
{
EncodedImage encodedImage {picture.data, picture.dataSize};
RawImage rawImage {encodedImage};
rawImage.scale(width);
image = rawImage.encode();
RawImage rawImage {picture.data, picture.dataSize};
rawImage.resize(width);
image = rawImage.encodeToJPEG(_jpegQuality);
}
catch (const ImageException& e)
{
@@ -101,17 +111,16 @@ getFromAvMediaFile(const Av::MediaFile& input, Width width)
return image;
}
static std::optional<EncodedImage>
getFromFile(const std::filesystem::path& p, Width width)
std::unique_ptr<IEncodedImage>
Grabber::getFromFile(const std::filesystem::path& p, ImageSize width) const
{
std::optional<EncodedImage> image;
std::unique_ptr<IEncodedImage> image;
try
{
RawImage rawImage {p};
rawImage.scale(width);
image = rawImage.encode();
rawImage.resize(width);
image = rawImage.encodeToJPEG(_jpegQuality);
}
catch (const ImageException& e)
{
@@ -121,53 +130,50 @@ getFromFile(const std::filesystem::path& p, Width width)
return image;
}
EncodedImage
Grabber::getDefault(Width width)
std::shared_ptr<IEncodedImage>
Grabber::getDefault(ImageSize width)
{
{
std::shared_lock lock {_cacheMutex};
if (auto it {_defaultCache.find(width)}; it != std::cend(_defaultCache))
if (auto it {_defaultCoverCache.find(width)}; it != std::cend(_defaultCoverCache))
return it->second;
}
{
std::unique_lock lock {_cacheMutex};
if (auto it {_defaultCache.find(width)}; it != std::cend(_defaultCache))
if (auto it {_defaultCoverCache.find(width)}; it != std::cend(_defaultCoverCache))
return it->second;
RawImage rawImage {*_defaultCover};
rawImage.scale(width);
EncodedImage res {rawImage.encode()};
std::shared_ptr<IEncodedImage> image {getFromFile(_defaultCoverPath, width)};
_defaultCoverCache[width] = image;
LMS_LOG(COVER, DEBUG) << "Default cache entries = " << _defaultCoverCache.size();
_defaultCache[width] = res;
LMS_LOG(COVER, DEBUG) << "Default cache entries = " << _defaultCache.size();
return res;
return image;
}
}
std::optional<EncodedImage>
Grabber::getFromDirectory(const std::filesystem::path& p, std::string_view preferredFileName, Width width) const
std::unique_ptr<IEncodedImage>
Grabber::getFromDirectory(const std::filesystem::path& p, std::string_view preferredFileName, ImageSize width) const
{
const std::multimap<std::string, std::filesystem::path> coverPaths {getCoverPaths(p)};
auto tryLoadImageFromFilename = [&](std::string_view fileName)
{
std::optional<EncodedImage> image;
std::unique_ptr<IEncodedImage> image;
auto range {coverPaths.equal_range(std::string {fileName})};
for (auto it {range.first}; it != range.second; ++it)
{
image = getFromFile(it->second, width);
if (!image)
continue;
if (image)
break;
}
return image;
};
std::optional<EncodedImage> image;
std::unique_ptr<IEncodedImage> image;
if (!preferredFileName.empty())
{
@@ -225,10 +231,10 @@ Grabber::getCoverPaths(const std::filesystem::path& directoryPath) const
return res;
}
std::optional<EncodedImage>
Grabber::getFromTrack(const std::filesystem::path& p, Width width) const
std::unique_ptr<IEncodedImage>
Grabber::getFromTrack(const std::filesystem::path& p, ImageSize width) const
{
std::optional<EncodedImage> image;
std::unique_ptr<IEncodedImage> image;
try
{
@@ -244,16 +250,16 @@ Grabber::getFromTrack(const std::filesystem::path& p, Width width) const
return image;
}
EncodedImage
Grabber::getFromTrackInternal(Database::Session& dbSession, Database::IdType trackId, Width width)
std::shared_ptr<IEncodedImage>
Grabber::getFromTrack(Database::Session& dbSession, Database::IdType trackId, ImageSize width)
{
using namespace Database;
const CacheEntryDesc cacheEntryDesc {CacheEntryDesc::Type::Track, trackId, width};
std::optional<EncodedImage> cover {loadFromCache(cacheEntryDesc)};
std::shared_ptr<IEncodedImage> cover {loadFromCache(cacheEntryDesc)};
if (cover)
return *cover;
return cover;
bool hasCover {};
bool isMultiDisc {};
@@ -289,19 +295,20 @@ Grabber::getFromTrackInternal(Database::Session& dbSession, Database::IdType tra
if (!cover)
cover = getDefault(width);
saveToCache(cacheEntryDesc, *cover);
if (cover)
saveToCache(cacheEntryDesc, cover);
return *cover;
return cover;
}
EncodedImage
Grabber::getFromReleaseInternal(Database::Session& session, Database::IdType releaseId, Width width)
std::shared_ptr<IEncodedImage>
Grabber::getFromRelease(Database::Session& session, Database::IdType releaseId, ImageSize width)
{
const CacheEntryDesc cacheEntryDesc {CacheEntryDesc::Type::Release, releaseId, width};
std::optional<EncodedImage> cover {loadFromCache(cacheEntryDesc)};
std::shared_ptr<IEncodedImage> cover {loadFromCache(cacheEntryDesc)};
if (cover)
return *cover;
return cover;
std::optional<Database::IdType> trackId;
{
@@ -317,16 +324,14 @@ Grabber::getFromReleaseInternal(Database::Session& session, Database::IdType rel
}
if (trackId)
{
cover = getFromTrackInternal(session, *trackId, width);
}
if (!cover)
cover = getFromTrack(session, *trackId, width);
else
cover = getDefault(width);
saveToCache(cacheEntryDesc, *cover);
if (cover)
saveToCache(cacheEntryDesc, cover);
return *cover;
return cover;
}
void
@@ -341,36 +346,23 @@ Grabber::flushCache()
_cache.clear();
}
std::unique_ptr<ICoverArt>
Grabber::getFromTrack(Database::Session& session, Database::IdType trackId, std::size_t width)
{
CoverArt toto {getFromTrackInternal(session, trackId, width)};
return std::make_unique<CoverArt>(getFromTrackInternal(session, trackId, width));
}
std::unique_ptr<ICoverArt>
Grabber::getFromRelease(Database::Session& session, Database::IdType releaseId, std::size_t width)
{
return std::make_unique<CoverArt>(getFromReleaseInternal(session, releaseId, width));
}
void
Grabber::saveToCache(const CacheEntryDesc& entryDesc, const EncodedImage& image)
Grabber::saveToCache(const CacheEntryDesc& entryDesc, std::shared_ptr<IEncodedImage> image)
{
std::unique_lock lock {_cacheMutex};
while (_cacheSize + image.getDataSize() > _maxCacheSize && !_cache.empty())
while (_cacheSize + image->getDataSize() > _maxCacheSize && !_cache.empty())
{
auto it {Random::pickRandom(_cache)};
_cacheSize -= it->second.getDataSize();
_cache.erase(it);
auto itRandom {Random::pickRandom(_cache)};
_cacheSize -= itRandom->second->getDataSize();
_cache.erase(itRandom);
}
_cacheSize += image.getDataSize();
_cacheSize += image->getDataSize();
_cache[entryDesc] = image;
}
std::optional<EncodedImage>
std::shared_ptr<IEncodedImage>
Grabber::loadFromCache(const CacheEntryDesc& entryDesc)
{
std::shared_lock lock {_cacheMutex};
@@ -379,7 +371,7 @@ Grabber::loadFromCache(const CacheEntryDesc& entryDesc)
if (it == std::cend(_cache))
{
++_cacheMisses;
return std::nullopt;
return nullptr;
}
++_cacheHits;
+25 -19
View File
@@ -21,6 +21,7 @@
#include <atomic>
#include <filesystem>
#include <map>
#include <optional>
#include <shared_mutex>
#include <string_view>
@@ -28,14 +29,19 @@
#include <vector>
#include "cover/ICoverArtGrabber.hpp"
#include "cover/IEncodedImage.hpp"
#include "database/Types.hpp"
#include "Image.hpp"
namespace Database
{
class Session;
}
namespace Av
{
class MediaFile;
}
namespace CoverArt
{
struct CacheEntryDesc
@@ -83,7 +89,11 @@ namespace CoverArt
class Grabber : public IGrabber
{
public:
Grabber(const std::filesystem::path& execPath, std::size_t maxCacheEntries, std::size_t maxFileSize);
Grabber(const std::filesystem::path& execPath,
const std::filesystem::path& defaultCoverPath,
std::size_t maxCacheEntries,
std::size_t maxFileSize,
unsigned jpegQuality);
Grabber(const Grabber&) = delete;
Grabber& operator=(const Grabber&) = delete;
@@ -91,38 +101,34 @@ namespace CoverArt
Grabber& operator=(Grabber&&) = delete;
private:
void setDefaultCover(const std::filesystem::path& defaultCoverPath) override;
std::unique_ptr<ICoverArt> getFromTrack(Database::Session& dbSession, Database::IdType trackId, Width width) override;
std::unique_ptr<ICoverArt> getFromRelease(Database::Session& dbSession, Database::IdType releaseId, Width width) override;
std::shared_ptr<IEncodedImage> getFromTrack(Database::Session& dbSession, Database::IdType trackId, ImageSize width) override;
std::shared_ptr<IEncodedImage> getFromRelease(Database::Session& dbSession, Database::IdType releaseId, ImageSize width) override;
void flushCache() override;
EncodedImage getFromTrackInternal(Database::Session& dbSession, Database::IdType trackId, Width width);
EncodedImage getFromReleaseInternal(Database::Session& dbSession, Database::IdType releaseId, Width width);
std::unique_ptr<IEncodedImage> getFromAvMediaFile(const Av::MediaFile& input, ImageSize width) const;
std::unique_ptr<IEncodedImage> getFromFile(const std::filesystem::path& p, ImageSize width) const;
std::optional<EncodedImage> getFromTrack(const std::filesystem::path& path, Width width) const;
std::unique_ptr<IEncodedImage> getFromTrack(const std::filesystem::path& path, ImageSize width) const;
std::multimap<std::string, std::filesystem::path> getCoverPaths(const std::filesystem::path& directoryPath) const;
std::optional<EncodedImage> getFromDirectory(const std::filesystem::path& path, std::string_view preferredFileName, Width width) const;
EncodedImage getDefault(Width width);
EncodedImage resizeCoverOrFallback(EncodedImage image, Width width) const;
std::optional<EncodedImage> _defaultCover; // optional to defer initializing
std::unique_ptr<IEncodedImage> getFromDirectory(const std::filesystem::path& path, std::string_view preferredFileName, ImageSize width) const;
std::shared_ptr<IEncodedImage> getDefault(ImageSize width);
std::shared_mutex _cacheMutex;
std::unordered_map<CacheEntryDesc, EncodedImage> _cache;
std::unordered_map<Width, EncodedImage> _defaultCache;
std::unordered_map<CacheEntryDesc, std::shared_ptr<IEncodedImage>> _cache;
std::unordered_map<ImageSize, std::shared_ptr<IEncodedImage>> _defaultCoverCache;
std::atomic<std::size_t> _cacheMisses {};
std::atomic<std::size_t> _cacheHits {};
std::size_t _cacheSize {};
void saveToCache(const CacheEntryDesc& entryDesc, const EncodedImage& image);
std::optional<EncodedImage> loadFromCache(const CacheEntryDesc& entryDesc);
void saveToCache(const CacheEntryDesc& entryDesc, std::shared_ptr<IEncodedImage> image);
std::shared_ptr<IEncodedImage> loadFromCache(const CacheEntryDesc& entryDesc);
const std::filesystem::path _defaultCoverPath;
const std::size_t _maxCacheSize;
static inline const std::vector<std::filesystem::path> _fileExtensions {".jpg", ".jpeg", ".png", ".bmp"}; // TODO parametrize
const std::size_t _maxFileSize;
static inline const std::vector<std::string> _preferredFileNames {"cover", "front"}; // TODO parametrize
const unsigned _jpegQuality;
};
} // namespace CoverArt
@@ -19,17 +19,10 @@
#pragma once
#include <filesystem>
#include <vector>
#include <Magick++.h>
#include "utils/Exception.hpp"
namespace CoverArt
{
void init(const std::filesystem::path& path);
// internal use only
class ImageException : public LmsException
{
@@ -37,37 +30,5 @@ namespace CoverArt
using LmsException::LmsException;
};
class EncodedImage
{
public:
EncodedImage() = default;
EncodedImage(const std::byte* data, std::size_t dataSize);
const std::byte* getData() const;
std::size_t getDataSize() const;
private:
friend class RawImage;
EncodedImage(Magick::Blob blob);
Magick::Blob _blob;
};
class RawImage
{
public:
RawImage(const std::filesystem::path& p);
RawImage(const EncodedImage& encodedImage);
// Operations
void scale(std::size_t width);
// output
EncodedImage encode() const;
private:
Magick::Image _image;
};
} // namespace CoverArt
@@ -17,31 +17,19 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CoverArt.hpp"
#pragma once
#include <memory>
#include "cover/IEncodedImage.hpp"
namespace CoverArt
{
CoverArt::CoverArt(EncodedImage image)
: _image {image}
{}
const std::byte*
CoverArt::getData() const
class IRawImage
{
return _image.getData();
}
public:
virtual void resize(ImageSize width) = 0;
virtual std::unique_ptr<IEncodedImage> encodeToJPEG(unsigned quality) const = 0;
};
}
std::size_t
CoverArt::getDataSize() const
{
return _image.getDataSize();
}
std::string_view
CoverArt::getMimeType() const
{
return "image/jpeg";
}
} // namespace CoverArt
@@ -0,0 +1,55 @@
/*
* 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 "JPEGImage.hpp"
#include "Exception.hpp"
#include "RawImage.hpp"
#include "utils/Logger.hpp"
namespace CoverArt::GraphicsMagick
{
JPEGImage::JPEGImage(const RawImage& rawImage, unsigned quality)
{
try
{
Magick::Image image {rawImage.getMagickImage()};
image.magick("JPEG");
image.quality(quality);
image.write(&_blob);
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR) << "Caught Magick exception: " << e.what();
throw ImageException {std::string {"Magick read error: "} + e.what()};
}
}
const std::byte*
JPEGImage::getData() const
{
return reinterpret_cast<const std::byte*>(_blob.data());
}
std::size_t
JPEGImage::getDataSize() const
{
return _blob.length();
}
}
@@ -0,0 +1,45 @@
/*
* 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
#ifndef LMS_SUPPORT_IMAGE_GM
#error "Bad configuration"
#endif
#include <Magick++.h>
#include "cover/IEncodedImage.hpp"
namespace CoverArt::GraphicsMagick
{
class RawImage;
class JPEGImage : public IEncodedImage
{
public:
JPEGImage(const RawImage& rawImage, unsigned quality);
private:
const std::byte* getData() const override;
std::size_t getDataSize() const override;
std::string_view getMimeType() const override { return "image/jpeg"; }
Magick::Blob _blob;
};
}
@@ -17,22 +17,24 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Image.hpp"
#include <atomic>
#include <fstream>
#include "RawImage.hpp"
#include <magick/resource.h>
#include "utils/Logger.hpp"
#include "JPEGImage.hpp"
#include "Exception.hpp"
namespace CoverArt {
namespace CoverArt::GraphicsMagick {
void
init(const std::filesystem::path& path)
{
Magick::InitializeMagick(path.string().c_str());
if (auto nbThreads {MagickLib::GetMagickResourceLimit(MagickLib::ThreadsResource)}; nbThreads != 1)
LMS_LOG(COVER, WARNING) << "Consider setting env var OMP_NUM_THREADS=1 to save resources";
if (!MagickLib::SetMagickResourceLimit(MagickLib::ThreadsResource, 1))
LMS_LOG(COVER, ERROR) << "Cannot set Magick thread resource limit to 1!";
@@ -43,26 +45,27 @@ init(const std::filesystem::path& path)
LMS_LOG(COVER, INFO) << "Magick Disk resource limit = " << GetMagickResourceLimit(MagickLib::DiskResource);
}
EncodedImage::EncodedImage(const std::byte* data, std::size_t dataSize)
: _blob {data, dataSize}
RawImage::RawImage(const std::byte* encodedData, std::size_t encodedDataSize)
{
}
EncodedImage::EncodedImage(Magick::Blob blob)
: _blob {blob}
{
}
const std::byte*
EncodedImage::getData() const
{
return reinterpret_cast<const std::byte*>(_blob.data());
}
std::size_t
EncodedImage::getDataSize() const
{
return _blob.length();
try
{
Magick::Blob blob {encodedData, encodedDataSize};
_image.read(blob);
}
catch (Magick::WarningCoder& e)
{
LMS_LOG(COVER, WARNING) << "Caught Magick WarningCoder: " << e.what();
}
catch (Magick::Warning& e)
{
LMS_LOG(COVER, WARNING) << "Caught Magick warning: " << e.what();
throw ImageException {std::string {"Magick read warning: "} + e.what()};
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR) << "Caught Magick exception: " << e.what();
throw ImageException {std::string {"Magick read error: "} + e.what()};
}
}
RawImage::RawImage(const std::filesystem::path& p)
@@ -73,79 +76,45 @@ RawImage::RawImage(const std::filesystem::path& p)
}
catch (Magick::WarningCoder& e)
{
LMS_LOG(COVER, WARNING) << "Caught Magick WarningCoder while loading image '" << p.string() << "': " << e.what();
LMS_LOG(COVER, WARNING) << "Caught Magick WarningCoder: " << e.what();
}
catch (Magick::Warning& e)
{
LMS_LOG(COVER, WARNING) << "Caught Magick warning while loading raw image '" << p.string() << "': " << e.what();
LMS_LOG(COVER, WARNING) << "Caught Magick warning: " << e.what();
throw ImageException {std::string {"Magick read warning: "} + e.what()};
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR) << "Caught Magick exception while loading raw image '" << p.string() << "': " << e.what();
throw ImageException {std::string {"Magick read error: "} + e.what()};
}
}
RawImage::RawImage(const EncodedImage& encodedImage)
{
try
{
_image.read(encodedImage._blob);
}
catch (Magick::WarningCoder& e)
{
LMS_LOG(COVER, WARNING) << "Caught Magick WarningCoder while loading raw image: " << e.what();
}
catch (Magick::Warning& e)
{
LMS_LOG(COVER, WARNING) << "Caught Magick warning while loading raw image: " << e.what();
throw ImageException {std::string {"Magick read warning: "} + e.what()};
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR) << "Caught Magick exception while loading raw image: " << e.what();
LMS_LOG(COVER, ERROR) << "Caught Magick exception: " << e.what();
throw ImageException {std::string {"Magick read error: "} + e.what()};
}
}
void
RawImage::scale(std::size_t width)
RawImage::resize(ImageSize width)
{
if (width == 0)
throw ImageException {"Bad width = 0"};
try
{
_image.resize(Magick::Geometry {static_cast<unsigned int>(width), static_cast<unsigned int>(width)});
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR) << "Caught Magick exception during scale: " << e.what();
LMS_LOG(COVER, ERROR) << "Caught Magick exception while resizing: " << e.what();
throw ImageException {std::string {"Magick resize error: "} + e.what()};
}
}
EncodedImage
RawImage::encode() const
std::unique_ptr<IEncodedImage>
RawImage::encodeToJPEG(unsigned quality) const
{
try
{
Magick::Image outputImage {_image};
outputImage.magick("JPEG");
Magick::Blob blob;
outputImage.write(&blob);
return EncodedImage {blob};
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR) << "Caught Magick exception while encoding raw image: " << e.what();
throw ImageException {std::string {"Magick encode error: "} + e.what()};
}
return std::make_unique<JPEGImage>(*this, quality);
}
} // namespace CoverArt
Magick::Image
RawImage::getMagickImage() const
{
return _image;
}
} // namespace CoverArt::GraphicsMagick
@@ -0,0 +1,54 @@
/*
* 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
#ifndef LMS_SUPPORT_IMAGE_GM
#error "Bad configuration"
#endif
#include <Magick++.h>
#include <cstddef>
#include <filesystem>
#include "cover/IEncodedImage.hpp"
#include "IRawImage.hpp"
namespace CoverArt::GraphicsMagick
{
void init(const std::filesystem::path& path);
class RawImage : IRawImage
{
public:
RawImage(const std::byte* encodedData, std::size_t encodedDataSize);
RawImage(const std::filesystem::path& path);
void resize(ImageSize width) override;
std::unique_ptr<IEncodedImage> encodeToJPEG(unsigned quality) const override;
private:
friend class JPEGImage;
Magick::Image getMagickImage() const;
Magick::Image _image;
};
}
+61
View File
@@ -0,0 +1,61 @@
/*
* 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 "JPEGImage.hpp"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb/stb_image_write.h>
#include "RawImage.hpp"
#include "Exception.hpp"
namespace CoverArt::STB
{
JPEGImage::JPEGImage(const RawImage& rawImage, unsigned quality)
{
auto writeCb {[](void* ctx, void* writeData, int writeSize)
{
auto& output {*reinterpret_cast<std::vector<std::byte>*>(ctx)};
const std::size_t currentOutputSize {output.size()};
output.resize(currentOutputSize + writeSize);
std::copy(reinterpret_cast<const std::byte*>(writeData), reinterpret_cast<const std::byte*>(writeData) + writeSize, output.data() + currentOutputSize);
}};
if (stbi_write_jpg_to_func(writeCb, &_data, rawImage.getWidth(), rawImage.getHeight(), 3, rawImage.getData(), quality) == 0)
{
_data.clear();
throw ImageException {"Failed to export in jpeg format!"};
}
}
const std::byte*
JPEGImage::getData() const
{
if (_data.empty())
return nullptr;
return &_data.front();
}
std::size_t
JPEGImage::getDataSize() const
{
return _data.size();
}
}
@@ -17,25 +17,23 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include "cover/ICoverArt.hpp"
#include "Image.hpp"
#include "cover/IEncodedImage.hpp"
namespace CoverArt
namespace CoverArt::STB
{
class CoverArt : public ICoverArt
class RawImage;
class JPEGImage : public IEncodedImage
{
public:
CoverArt(EncodedImage image);
const std::byte* getData() const override;
std::size_t getDataSize() const override;
std::string_view getMimeType() const override;
JPEGImage(const RawImage& rawImage, unsigned quality);
private:
EncodedImage _image;
};
const std::byte* getData() const override;
std::size_t getDataSize() const override;
std::string_view getMimeType() const override { return "image/jpeg"; }
} // namespace CoverArt
std::vector<std::byte> _data;
};
}
+114
View File
@@ -0,0 +1,114 @@
/*
* 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 "RawImage.hpp"
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL
#define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM
#include <stb/stb_image.h>
#include <stb/stb_image_resize.h>
#include "JPEGImage.hpp"
#include "Exception.hpp"
namespace CoverArt::STB
{
RawImage::RawImage(const std::byte* encodedData, std::size_t encodedDataSize)
{
int n;
_data = UniquePtrFree {stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(encodedData), encodedDataSize, &_width, &_height, &n, 3), std::free};
if (!_data)
throw ImageException {"Cannot load image from memory"};
}
RawImage::RawImage(const std::filesystem::path& p)
{
int n;
_data = UniquePtrFree {stbi_load(p.string().c_str(), &_width, &_height, &n, 3), std::free};
if (!_data)
throw ImageException {"Cannot load image from memory"};
}
void
RawImage::resize(ImageSize width)
{
size_t height;
if (_width == _height)
{
height = width;
}
else if (_width > _height)
{
height = (size_t)((float)width/_width*_height);
}
else
{
height = width;
width = (size_t)((float)height/_height*_width);
}
UniquePtrFree resizedData {reinterpret_cast<unsigned char*>(malloc(width*height*3)), std::free};
if (!resizedData)
throw ImageException {"Cannot allocate memory for resized image!"};
if (stbir_resize_uint8_srgb(reinterpret_cast<const unsigned char*>(_data.get()), _width, _height, 0,
reinterpret_cast<unsigned char*>(resizedData.get()), width, height, 0,
3, STBIR_ALPHA_CHANNEL_NONE, 0) == 0)
{
throw ImageException {"Failed to resize image!"};
}
_data = std::move(resizedData);
_height = height;
_width = width;
}
std::unique_ptr<IEncodedImage>
RawImage::encodeToJPEG(unsigned quality) const
{
return std::make_unique<JPEGImage>(*this, quality);
}
ImageSize
RawImage::getWidth() const
{
return _width;
}
ImageSize
RawImage::getHeight() const
{
return _height;
}
const std::byte*
RawImage::getData() const
{
if (!_data)
return nullptr;
return reinterpret_cast<const std::byte*>(_data.get());
}
}
+54
View File
@@ -0,0 +1,54 @@
/*
* 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
#ifndef LMS_SUPPORT_IMAGE_STB
#error "Bad configuration"
#endif
#include <cstddef>
#include <filesystem>
#include "cover/IEncodedImage.hpp"
#include "IRawImage.hpp"
namespace CoverArt::STB
{
class RawImage : public IRawImage
{
public:
RawImage(const std::byte* encodedData, std::size_t encodedDataSize);
RawImage(const std::filesystem::path& path);
void resize(ImageSize width) override;
std::unique_ptr<IEncodedImage> encodeToJPEG(unsigned quality) const override;
ImageSize getWidth() const;
ImageSize getHeight() const;
const std::byte* getData() const;
private:
int _width;
int _height;
using UniquePtrFree = std::unique_ptr<unsigned char, decltype(&std::free)>;
UniquePtrFree _data {nullptr, std::free};
};
}
@@ -19,13 +19,11 @@
#pragma once
#include <cstddef>
#include <filesystem>
#include <memory>
#include <string_view>
#include "database/Types.hpp"
#include "cover/ICoverArt.hpp"
#include "cover/IEncodedImage.hpp"
namespace Database
{
@@ -34,22 +32,22 @@ namespace Database
namespace CoverArt
{
using Width = std::size_t;
class IGrabber
{
public:
virtual ~IGrabber() = default;
virtual void setDefaultCover(const std::filesystem::path& defaultCoverPath) = 0;
virtual std::unique_ptr<ICoverArt> getFromTrack(Database::Session& dbSession, Database::IdType trackId, Width width) = 0;
virtual std::unique_ptr<ICoverArt> getFromRelease(Database::Session& dbSession, Database::IdType releaseId, Width width) = 0;
virtual std::shared_ptr<IEncodedImage> getFromTrack(Database::Session& dbSession, Database::IdType trackId, ImageSize width) = 0;
virtual std::shared_ptr<IEncodedImage> getFromRelease(Database::Session& dbSession, Database::IdType releaseId, ImageSize width) = 0;
virtual void flushCache() = 0;
};
std::unique_ptr<IGrabber> createGrabber(const std::filesystem::path& execPath, std::size_t maxCacheEntries, std::size_t maxFileSize);
std::unique_ptr<IGrabber> createGrabber(const std::filesystem::path& execPath,
const std::filesystem::path& defaultCoverPath,
std::size_t maxCacheEntries,
std::size_t maxFileSize,
unsigned jpegQuality);
} // namespace CoverArt
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Emeric Poupon
* Copyright (C) 2015 Emeric Poupon
*
* This file is part of LMS.
*
@@ -24,11 +24,12 @@
namespace CoverArt
{
using ImageSize = std::size_t;
class ICoverArt
class IEncodedImage
{
public:
virtual ~ICoverArt() = default;
virtual ~IEncodedImage() = default;
virtual const std::byte* getData() const = 0;
virtual std::size_t getDataSize() const = 0;
@@ -36,3 +37,4 @@ namespace CoverArt
};
} // namespace CoverArt
-1
View File
@@ -9,7 +9,6 @@ add_library(lmsdatabase SHARED
impl/Release.cpp
impl/ScanSettings.cpp
impl/Session.cpp
impl/SessionPool.cpp
impl/SqlQuery.cpp
impl/Track.cpp
impl/TrackBookmark.cpp
+4 -4
View File
@@ -32,14 +32,14 @@ Cluster::Cluster()
{
}
Cluster::Cluster(Wt::Dbo::ptr<ClusterType> type, std::string name)
: _name(std::string(name, 0, _maxNameLength)),
_clusterType(type)
Cluster::Cluster(Wt::Dbo::ptr<ClusterType> type, std::string_view name)
: _name(std::string {name, 0, _maxNameLength}),
_clusterType {type}
{
}
Cluster::pointer
Cluster::create(Session& session, Wt::Dbo::ptr<ClusterType> type, std::string name)
Cluster::create(Session& session, Wt::Dbo::ptr<ClusterType> type, std::string_view name)
{
session.checkUniqueLocked();
+19
View File
@@ -22,6 +22,7 @@
#include <Wt/Dbo/FixedSqlConnectionPool.h>
#include <Wt/Dbo/backend/Sqlite3.h>
#include "database/Session.hpp"
#include "database/User.hpp"
#include "utils/Logger.hpp"
@@ -57,6 +58,24 @@ Db::executeSql(const std::string& sql)
connection->executeSql(sql);
}
Session&
Db::getTLSSession()
{
static thread_local Session* tlsSession {};
if (!tlsSession)
{
auto newSession {std::make_unique<Session>(*this)};
tlsSession = newSession.get();
{
std::scoped_lock lock {_tlsSessionsMutex};
_tlsSessions.push_back(std::move(newSession));
}
}
return *tlsSession;
}
Db::ScopedConnection::ScopedConnection(Wt::Dbo::SqlConnectionPool& pool)
: _connectionPool {pool}
+2 -2
View File
@@ -362,13 +362,13 @@ Session::checkSharedLocked()
UniqueTransaction
Session::createUniqueTransaction()
{
return UniqueTransaction{_db.getMutex(), _session};
return UniqueTransaction {_db.getMutex(), _session};
}
SharedTransaction
Session::createSharedTransaction()
{
return SharedTransaction{_db.getMutex(), _session};
return SharedTransaction {_db.getMutex(), _session};
}
void
-69
View File
@@ -1,69 +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 "database/SessionPool.hpp"
#include "database/Session.hpp"
#include "utils/Exception.hpp"
#include "utils/Logger.hpp"
namespace Database {
SessionPool::SessionPool(Db& database, std::size_t maxSessionCount)
: _db {database},
_maxSessionCount {maxSessionCount}
{
}
Session&
SessionPool::acquireSession()
{
std::scoped_lock lock {_mutex};
if (_freeSessions.empty())
{
if (_acquiredSessions.size() == _maxSessionCount)
throw LmsException {"Too many database sessions!"};
_freeSessions.emplace_back(std::make_unique<Session>(_db));
}
std::unique_ptr<Session> session {std::move(_freeSessions.back())};
_freeSessions.pop_back();
_acquiredSessions.push_back(std::move(session));
return *_acquiredSessions.back().get();
}
void
SessionPool::releaseSession(Session& sessionToRelease)
{
std::scoped_lock lock {_mutex};
auto it {std::find_if(std::begin(_acquiredSessions), std::end(_acquiredSessions), [&](const std::unique_ptr<Session>& session) { return session.get() == &sessionToRelease; })};
if (it == std::end(_acquiredSessions))
throw LmsException {"Unknown released Session!"};
std::unique_ptr<Session> session {std::move(*it)};
_acquiredSessions.erase(it);
_freeSessions.push_back(std::move(session));
}
} // namespace Database
@@ -20,6 +20,7 @@
#pragma once
#include <string>
#include <string_view>
#include <vector>
#include <Wt/Dbo/Dbo.h>
@@ -41,7 +42,7 @@ class Cluster : public Wt::Dbo::Dbo<Cluster>
using pointer = Wt::Dbo::ptr<Cluster>;
Cluster();
Cluster(Wt::Dbo::ptr<ClusterType> type, std::string name);
Cluster(Wt::Dbo::ptr<ClusterType> type, std::string_view name);
// Find utility
static std::vector<pointer> getAll(Session& session);
@@ -49,7 +50,7 @@ class Cluster : public Wt::Dbo::Dbo<Cluster>
static pointer getById(Session& session, IdType id);
// Create utility
static pointer create(Session& session, Wt::Dbo::ptr<ClusterType> type, std::string name);
static pointer create(Session& session, Wt::Dbo::ptr<ClusterType> type, std::string_view name);
// Accessors
const std::string& getName() const { return _name; }
+6 -1
View File
@@ -26,7 +26,7 @@
namespace Database {
// Session living class handling the database and the login
class Session;
class Db
{
public:
@@ -39,6 +39,8 @@ class Db
Db& operator=(const Db&) = delete;
Db& operator=(Db&&) = delete;
Session& getTLSSession();
private:
friend class Session;
@@ -88,6 +90,9 @@ class Db
std::shared_mutex _sharedMutex;
std::unique_ptr<Wt::Dbo::SqlConnectionPool> _connectionPool;
std::mutex _tlsSessionsMutex;
std::vector<std::unique_ptr<Session>> _tlsSessions;
};
} // namespace Database
@@ -84,7 +84,7 @@ class Session
void doDatabaseMigrationIfNeeded();
Db& _db;
Db& _db;
Wt::Dbo::Session _session;
};
@@ -1,72 +0,0 @@
/*
* Copyright (C) 2013 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 <mutex>
#include <vector>
#include "Session.hpp"
namespace Database {
class SessionPool
{
public:
class ScopedSession
{
public:
ScopedSession(SessionPool& pool) : _pool {pool}, _session {_pool.acquireSession()} {}
~ScopedSession() { _pool.releaseSession(_session); }
ScopedSession(const ScopedSession&) = delete;
ScopedSession(ScopedSession&&) = delete;
ScopedSession& operator=(const ScopedSession&) = delete;
ScopedSession& operator=(ScopedSession&&) = delete;
Session& get() { return _session; }
private:
SessionPool& _pool;
Session& _session;
};
SessionPool(Db& database, std::size_t maxSessionCount = 30);
SessionPool(const SessionPool&) = delete;
SessionPool(SessionPool&&) = delete;
SessionPool& operator=(const SessionPool&) = delete;
SessionPool& operator=(SessionPool&&) = delete;
private:
friend class ScopedSession;
Session& acquireSession();
void releaseSession(Session& session);
std::mutex _mutex;
Db& _db;
std::size_t _maxSessionCount;
std::vector<std::unique_ptr<Session>> _freeSessions;
std::vector<std::unique_ptr<Session>> _acquiredSessions;
};
} // namespace Database
+7 -7
View File
@@ -157,7 +157,7 @@ getClientInfo(const Wt::Http::ParameterMap& parameters)
}
SubsonicResource::SubsonicResource(Db& db)
: _sessionPool {db}
: _db {db}
{
}
@@ -1746,7 +1746,7 @@ handleGetCoverArt(RequestContext& context, const Wt::Http::Request& /*request*/,
std::size_t size {getParameterAs<std::size_t>(context.parameters, "size").value_or(256)};
size = clamp(size, std::size_t {32}, std::size_t {1024});
std::unique_ptr<CoverArt::ICoverArt> cover;
std::shared_ptr<CoverArt::IEncodedImage> cover;
switch (id.type)
{
case Id::Type::Track:
@@ -1911,9 +1911,9 @@ SubsonicResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Resp
clientName = clientInfo.name;
SessionPool::ScopedSession dbSession {_sessionPool};
Session& dbSession {_db.getTLSSession()};
switch (Service<Auth::IPasswordService>::get()->checkUserPassword(dbSession.get(),
switch (Service<Auth::IPasswordService>::get()->checkUserPassword(dbSession,
boost::asio::ip::address::from_string(request.clientAddress()),
clientInfo.user, clientInfo.password))
{
@@ -1925,16 +1925,16 @@ SubsonicResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Resp
throw LoginThrottledGenericError {};
}
RequestContext requestContext {parameters, dbSession.get(), clientInfo.user, clientInfo.name};
RequestContext requestContext {parameters, dbSession, clientInfo.user, clientInfo.name};
auto itEntryPoint {requestEntryPoints.find(requestPath)};
if (itEntryPoint != requestEntryPoints.end())
{
if (itEntryPoint->second.mustBeAdmin)
{
auto transaction {dbSession.get().createSharedTransaction()};
auto transaction {dbSession.createSharedTransaction()};
User::pointer user {User::getByLoginName(dbSession.get(), clientInfo.user)};
User::pointer user {User::getByLoginName(dbSession, clientInfo.user)};
if (!user || !user->isAdmin())
throw UserNotAuthorizedError {};
}
@@ -21,8 +21,6 @@
#include <Wt/WResource.h>
#include <Wt/Http/Response.h>
#include "database/SessionPool.hpp"
namespace Database
{
class Db;
@@ -41,7 +39,7 @@ class SubsonicResource final : public Wt::WResource
void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) override;
Database::SessionPool _sessionPool;
Database::Db& _db;
};
} // namespace
+7 -5
View File
@@ -28,6 +28,7 @@
#include "av/AvTranscoder.hpp"
#include "cover/ICoverArtGrabber.hpp"
#include "database/Db.hpp"
#include "database/Session.hpp"
#include "scanner/IMediaScanner.hpp"
#include "recommendation/IEngine.hpp"
#include "subsonic/SubsonicResource.hpp"
@@ -140,7 +141,7 @@ int main(int argc, char* argv[])
std::filesystem::create_directories(config->getPath("working-dir") / "cache");
// Construct WT configuration and get the argc/argv back
std::vector<std::string> wtServerArgs = generateWtConfig(argv[0]);
const std::vector<std::string> wtServerArgs {generateWtConfig(argv[0])};
std::vector<const char*> wtArgv(wtServerArgs.size());
for (std::size_t i = 0; i < wtServerArgs.size(); ++i)
@@ -149,8 +150,8 @@ int main(int argc, char* argv[])
wtArgv[i] = wtServerArgs[i].c_str();
}
Wt::WServer server(argv[0]);
server.setServerConfiguration (wtServerArgs.size(), const_cast<char**>(&wtArgv[0]));
Wt::WServer server {argv[0]};
server.setServerConfiguration(wtServerArgs.size(), const_cast<char**>(&wtArgv[0]));
// lib init
Av::Transcoder::init();
@@ -169,9 +170,10 @@ int main(int argc, char* argv[])
Service<Auth::IAuthTokenService> authTokenService {Auth::createAuthTokenService(config->getULong("login-throttler-max-entriees", 10000))};
Service<Auth::IPasswordService> passwordService {Auth::createPasswordService(config->getULong("login-throttler-max-entriees", 10000))};
Service<CoverArt::IGrabber> coverArtService {CoverArt::createGrabber(argv[0],
server.appRoot() + "/images/unknown-cover.jpg",
config->getULong("cover-max-cache-size", 30) * 1000 * 1000,
config->getULong("cover-max-file-size", 10) * 1000 * 1000)};
coverArtService->setDefaultCover(server.appRoot() + "/images/unknown-cover.jpg");
config->getULong("cover-max-file-size", 10) * 1000 * 1000,
config->getULong("cover-jpeg-quality", 75))};
Service<Recommendation::IEngine> recommendationEngineService {Recommendation::createEngine(database)};
recommendationEngineService->requestLoad();
Service<Scanner::IMediaScanner> mediaScannerService {Scanner::createMediaScanner(database)};
+1
View File
@@ -29,6 +29,7 @@
#include "auth/IAuthTokenService.hpp"
#include "auth/IPasswordService.hpp"
#include "database/Session.hpp"
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
+20 -13
View File
@@ -32,6 +32,7 @@
#include "database/Cluster.hpp"
#include "database/Db.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/User.hpp"
#include "explore/Explore.hpp"
#include "explore/Filters.hpp"
@@ -65,19 +66,25 @@ LmsApplication::create(const Wt::WEnvironment& env, Database::Db& db, LmsApplica
return std::make_unique<LmsApplication>(env, db, appGroups);
}
LmsApplication*
LmsApplication*
LmsApplication::instance()
{
return reinterpret_cast<LmsApplication*>(Wt::WApplication::instance());
}
Database::Session&
LmsApplication::getDbSession()
{
return _db.getTLSSession();
}
Wt::Dbo::ptr<Database::User>
LmsApplication::getUser()
{
if (!_userId)
return {};
return Database::User::getById(_dbSession, *_userId);
return Database::User::getById(getDbSession(), *_userId);
}
bool
@@ -89,7 +96,7 @@ LmsApplication::isUserAuthStrong() const
bool
LmsApplication::isUserAdmin()
{
auto transaction {_dbSession.createSharedTransaction()};
auto transaction {getDbSession().createSharedTransaction()};
return getUser()->isAdmin();
}
@@ -97,7 +104,7 @@ LmsApplication::isUserAdmin()
bool
LmsApplication::isUserDemo()
{
auto transaction {_dbSession.createSharedTransaction()};
auto transaction {getDbSession().createSharedTransaction()};
return getUser()->isDemo();
}
@@ -105,7 +112,7 @@ LmsApplication::isUserDemo()
std::string
LmsApplication::getUserLoginName()
{
auto transaction {_dbSession.createSharedTransaction()};
auto transaction {getDbSession().createSharedTransaction()};
return getUser()->getLoginName();
}
@@ -114,7 +121,7 @@ LmsApplication::LmsApplication(const Wt::WEnvironment& env,
Database::Db& db,
LmsApplicationGroupContainer& appGroups)
: Wt::WApplication {env},
_dbSession {db},
_db {db},
_appGroups {appGroups}
{
@@ -155,8 +162,8 @@ LmsApplication::LmsApplication(const Wt::WEnvironment& env,
// If here is no account in the database, launch the first connection wizard
bool firstConnection {};
{
auto transaction {_dbSession.createSharedTransaction()};
firstConnection = Database::User::getAll(_dbSession).empty();
auto transaction {getDbSession().createSharedTransaction()};
firstConnection = Database::User::getAll(getDbSession()).empty();
}
LMS_LOG(UI, DEBUG) << "Creating root widget. First connection = " << firstConnection;
@@ -174,8 +181,8 @@ LmsApplication::LmsApplication(const Wt::WEnvironment& env,
Database::User::UITheme theme {Database::User::defaultUITheme};
if (userId)
{
auto transaction {_dbSession.createSharedTransaction()};
const auto user {Database::User::getById(_dbSession, *userId)};
auto transaction {getDbSession().createSharedTransaction()};
const auto user {Database::User::getById(getDbSession(), *userId)};
if (user)
theme = user->getUITheme();
}
@@ -206,8 +213,8 @@ LmsApplication::LmsApplication(const Wt::WEnvironment& env,
auth->userLoggedIn.connect(this, [this](Database::IdType userId)
{
{
auto transaction {_dbSession.createSharedTransaction()};
const auto user {Database::User::getById(_dbSession, userId)};
auto transaction {getDbSession().createSharedTransaction()};
const auto user {Database::User::getById(getDbSession(), userId)};
if (user)
{
LmsTheme* lmsTheme {static_cast<LmsTheme*>(LmsApp->theme().get())};
@@ -402,7 +409,7 @@ LmsApplication::handleUserLoggedOut()
LMS_LOG(UI, INFO) << "User '" << getUserLoginName() << " 'logged out";
{
auto transaction {_dbSession.createUniqueTransaction()};
auto transaction {getDbSession().createUniqueTransaction()};
getUser().modify()->clearAuthTokens();
}
+3 -4
View File
@@ -24,8 +24,6 @@
#include <Wt/WApplication.h>
#include <Wt/WPopupMenu.h>
#include "database/Db.hpp"
#include "database/Session.hpp"
#include "scanner/IMediaScanner.hpp"
#include "LmsApplicationGroup.hpp"
@@ -35,6 +33,7 @@ namespace Database {
class Cluster;
class Db;
class Release;
class Session;
class User;
}
@@ -83,7 +82,7 @@ class LmsApplication : public Wt::WApplication
std::shared_ptr<ImageResource> getImageResource() { return _imageResource; }
std::shared_ptr<AudioTranscodeResource> getAudioTranscodeResource() { return _audioTranscodeResource; }
std::shared_ptr<AudioFileResource> getAudioFileResource() { return _audioFileResource; }
Database::Session& getDbSession() { return _dbSession;}
Database::Session& getDbSession(); // always thread safe
Wt::Dbo::ptr<Database::User> getUser();
bool isUserAuthStrong() const; // user must be logged in prior this call
@@ -126,8 +125,8 @@ class LmsApplication : public Wt::WApplication
void createHome();
Database::Db& _db;
Wt::Signal<> _preQuit;
Database::Session _dbSession;
LmsApplicationGroupContainer& _appGroups;
Events _events;
std::optional<Database::IdType> _userId;
+1
View File
@@ -27,6 +27,7 @@
#include "database/Artist.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackList.hpp"
#include "database/User.hpp"
+1
View File
@@ -23,6 +23,7 @@
#include <Wt/WText.h>
#include "database/Cluster.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackList.hpp"
#include "database/User.hpp"
+1
View File
@@ -33,6 +33,7 @@
#include "common/ValueStringModel.hpp"
#include "auth/IPasswordService.hpp"
#include "database/Session.hpp"
#include "utils/IConfig.hpp"
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
@@ -28,6 +28,7 @@
#include "database/Cluster.hpp"
#include "database/ScanSettings.hpp"
#include "database/Session.hpp"
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
#include "utils/String.hpp"
+2
View File
@@ -25,6 +25,8 @@
#include <Wt/WPushButton.h>
#include "auth/IPasswordService.hpp"
#include "database/Session.hpp"
#include "database/User.hpp"
#include "utils/Exception.hpp"
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
+1
View File
@@ -29,6 +29,7 @@
#include "auth/IPasswordService.hpp"
#include "database/User.hpp"
#include "database/Session.hpp"
#include "utils/IConfig.hpp"
#include "utils/Exception.hpp"
#include "utils/Logger.hpp"
+1
View File
@@ -24,6 +24,7 @@
#include <Wt/WTemplate.h>
#include "database/User.hpp"
#include "database/Session.hpp"
#include "utils/Logger.hpp"
#include "LmsApplication.hpp"
+1
View File
@@ -27,6 +27,7 @@
#include "database/Artist.hpp"
#include "database/Release.hpp"
#include "database/ScanSettings.hpp"
#include "database/Session.hpp"
#include "database/User.hpp"
#include "recommendation/IEngine.hpp"
#include "utils/Logger.hpp"
+1
View File
@@ -25,6 +25,7 @@
#include "common/ValueStringModel.hpp"
#include "database/Artist.hpp"
#include "database/Session.hpp"
#include "database/User.hpp"
#include "database/TrackList.hpp"
#include "utils/Logger.hpp"
+1
View File
@@ -25,6 +25,7 @@
#include "database/Artist.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "utils/Logger.hpp"
+1
View File
@@ -25,6 +25,7 @@
#include <Wt/WTemplate.h>
#include "database/Cluster.hpp"
#include "database/Session.hpp"
#include "LmsApplication.hpp"
+1
View File
@@ -20,6 +20,7 @@
#include "ReleasePopup.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/User.hpp"
#include "resource/DownloadResource.hpp"
#include "LmsApplication.hpp"
+1
View File
@@ -27,6 +27,7 @@
#include "database/Release.hpp"
#include "database/ScanSettings.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "recommendation/IEngine.hpp"
#include "utils/Logger.hpp"
+2 -1
View File
@@ -27,8 +27,9 @@
#include <Wt/WText.h>
#include "database/Release.hpp"
#include "database/User.hpp"
#include "database/Session.hpp"
#include "database/TrackList.hpp"
#include "database/User.hpp"
#include "utils/Logger.hpp"
#include "utils/String.hpp"
+1
View File
@@ -24,6 +24,7 @@
#include "database/Artist.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "resource/ImageResource.hpp"
+1
View File
@@ -19,6 +19,7 @@
#include "TrackPopup.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/User.hpp"
#include "resource/DownloadResource.hpp"
+1
View File
@@ -26,6 +26,7 @@
#include "database/Artist.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackList.hpp"
+1 -3
View File
@@ -23,6 +23,7 @@
#include <Wt/Http/Response.h>
#include "av/AvInfo.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "utils/FileResourceHandlerCreator.hpp"
#include "utils/Logger.hpp"
@@ -48,9 +49,6 @@ static
std::optional<std::filesystem::path>
getTrackPathFromTrackId(Database::IdType trackId)
{
// DbSession are not thread safe
Wt::WApplication::UpdateLock lock {LmsApp};
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
const Database::Track::pointer track {Database::Track::getById(LmsApp->getDbSession(), trackId)};
@@ -22,6 +22,7 @@
#include <Wt/Http/Response.h>
#include "av/AvTranscoder.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/User.hpp"
#include "utils/Logger.hpp"
@@ -148,8 +149,6 @@ AudioTranscodeResource::handleRequest(const Wt::Http::Request& request,
std::filesystem::path trackPath;
{
// DbSession are not thread safe
Wt::WApplication::UpdateLock lock(LmsApp);
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
const Database::Track::pointer track {Database::Track::getById(LmsApp->getDbSession(), *trackId)};
+1 -3
View File
@@ -27,6 +27,7 @@
#include "database/Artist.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "utils/Exception.hpp"
#include "utils/Logger.hpp"
@@ -182,7 +183,6 @@ DownloadArtistResource::DownloadArtistResource(Database::IdType artistId)
std::unique_ptr<Zip::Zipper>
DownloadArtistResource::createZipper()
{
Wt::WApplication::UpdateLock lock {LmsApp}; // DbSession are not thread safe
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
const Database::Artist::pointer artist {Database::Artist::getById(LmsApp->getDbSession(), _artistId)};
@@ -209,7 +209,6 @@ DownloadReleaseResource::DownloadReleaseResource(Database::IdType releaseId)
std::unique_ptr<Zip::Zipper>
DownloadReleaseResource::createZipper()
{
Wt::WApplication::UpdateLock lock {LmsApp}; // DbSession are not thread safe
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
const Database::Release::pointer release {Database::Release::getById(LmsApp->getDbSession(), _releaseId)};
@@ -235,7 +234,6 @@ DownloadTrackResource::DownloadTrackResource(Database::IdType trackId)
std::unique_ptr<Zip::Zipper>
DownloadTrackResource::createZipper()
{
Wt::WApplication::UpdateLock lock {LmsApp}; // DbSession are not thread safe
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
const Database::Track::pointer track {Database::Track::getById(LmsApp->getDbSession(), _trackId)};
+3 -11
View File
@@ -74,7 +74,7 @@ ImageResource::handleRequest(const Wt::Http::Request& request, Wt::Http::Respons
return;
}
std::unique_ptr<CoverArt::ICoverArt> cover;
std::shared_ptr<CoverArt::IEncodedImage> cover;
if (trackIdStr)
{
@@ -87,11 +87,7 @@ ImageResource::handleRequest(const Wt::Http::Request& request, Wt::Http::Respons
return;
}
// DbSession are not thread safe
{
Wt::WApplication::UpdateLock lock {LmsApp};
cover = Service<CoverArt::IGrabber>::get()->getFromTrack(LmsApp->getDbSession(), *trackId, *size);
}
cover = Service<CoverArt::IGrabber>::get()->getFromTrack(LmsApp->getDbSession(), *trackId, *size);
}
else if (releaseIdStr)
{
@@ -101,11 +97,7 @@ ImageResource::handleRequest(const Wt::Http::Request& request, Wt::Http::Respons
if (!releaseId)
return;
// DbSession are not thread safe
{
Wt::WApplication::UpdateLock lock {LmsApp};
cover = Service<CoverArt::IGrabber>::get()->getFromRelease(LmsApp->getDbSession(), *releaseId, *size);
}
cover = Service<CoverArt::IGrabber>::get()->getFromRelease(LmsApp->getDbSession(), *releaseId, *size);
}
else
{
+1 -3
View File
@@ -1,6 +1,4 @@
add_subdirectory(cover)
add_subdirectory(metadata)
add_subdirectory(recommendation)
add_subdirectory(zipper)
+10
View File
@@ -0,0 +1,10 @@
add_executable(lms-cover
LmsCover.cpp
)
target_link_libraries(lms-cover PRIVATE
lmscover
Boost::program_options
)
+107
View File
@@ -0,0 +1,107 @@
/*
* 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 <filesystem>
#include <iostream>
#include <stdexcept>
#include <stdlib.h>
#include <boost/program_options.hpp>
#include "cover/ICoverArtGrabber.hpp"
#include "database/Db.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "utils/IConfig.hpp"
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
#include "utils/StreamLogger.hpp"
static
void
dumpTrackCovers(Database::Session& session, CoverArt::ImageSize width)
{
std::vector<Database::IdType> trackIds;
{
auto transaction {session.createSharedTransaction()};
trackIds = Database::Track::getAllIds(session);
}
for (Database::IdType trackId : trackIds)
{
std::cout << "Getting cover for track id " << trackId << std::endl;
Service<CoverArt::IGrabber>::get()->getFromTrack(session, trackId, width);
}
}
int main(int argc, char *argv[])
{
try
{
namespace po = boost::program_options;
// log to stdout
Service<Logger> logger {std::make_unique<StreamLogger>(std::cout)};
po::options_description desc{"Allowed options"};
desc.add_options()
("help,h", "print usage message")
("conf,c", po::value<std::string>()->default_value("/etc/lms.conf"), "LMS config file")
("default-cover,d", po::value<std::string>(), "Default cover path")
("tracks,t", "dump covers for tracks")
("size,s", po::value<unsigned>()->default_value(512), "Requested cover size")
("quality,q", po::value<unsigned>()->default_value(75), "JPEG quality (1-100)")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
if (vm.count("help"))
{
std::cout << desc << std::endl;
return EXIT_SUCCESS;
}
Service<IConfig> config {createConfig(vm["conf"].as<std::string>())};
Service<CoverArt::IGrabber> coverArtService {CoverArt::createGrabber(argv[0],
vm["default-cover"].as<std::string>(),
config->getULong("cover-max-cache-size", 30) * 1000 * 1000,
config->getULong("cover-max-file-size", 10) * 1000 * 1000,
config->getULong("cover-jpeg-quality", vm["quality"].as<unsigned>())
)};
Database::Db db {config->getPath("working-dir") / "lms.db"};
Database::Session session {db};
if (vm.count("tracks"))
dumpTrackCovers(session, vm["size"].as<unsigned>());
}
catch( std::exception& e)
{
std::cerr << "Caught exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}