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
@@ -3,7 +3,7 @@ sudo: required
dist: focal dist: focal
before_install: before_install:
- eval "${MATRIX_EVAL}" - eval "${MATRIX_EVAL}"
- sudo apt-get install build-essential cmake libboost-all-dev libconfig++-dev libavcodec-dev libavutil-dev libavformat-dev ffmpeg libgraphicsmagick++1-dev libpstreams-dev libconfig++-dev libpstreams-dev libtag1-dev libpam0g-dev - sudo apt-get install build-essential cmake libboost-all-dev libconfig++-dev libavcodec-dev libavutil-dev libavformat-dev ffmpeg libstb-dev libpstreams-dev libconfig++-dev libpstreams-dev libtag1-dev libpam0g-dev
- git clone https://github.com/emweb/wt.git wt - git clone https://github.com/emweb/wt.git wt
- pushd wt; - pushd wt;
- git checkout 4.4.0 - git checkout 4.4.0
+19 -1
View File
@@ -15,7 +15,25 @@ find_package(Taglib REQUIRED)
find_package(Boost REQUIRED COMPONENTS system program_options) find_package(Boost REQUIRED COMPONENTS system program_options)
find_package(PStreams REQUIRED) find_package(PStreams REQUIRED)
find_package(PAM) find_package(PAM)
pkg_check_modules(GRAPHICSMAGICKXX REQUIRED GraphicsMagick++) find_package(STB)
pkg_check_modules(GRAPHICSMAGICKXX GraphicsMagick++)
if (STB_FOUND)
set(IMAGE_LIBRARY STB CACHE STRING "STB library")
elseif (GRAPHICSMAGICKXX_FOUND)
set(IMAGE_LIBRARY GraphicksMagick++ CACHE STRING "GraphicsMagick++")
else ()
message(FATAL_ERROR "Cannot find suitable image library")
endif()
set_property(CACHE IMAGE_LIBRARY PROPERTY STRINGS GraphicksMagick++ STB)
if (IMAGE_LIBRARY STREQUAL GraphicksMagick++ AND NOT GRAPHICSMAGICKXX_FOUND)
message(FATAL_ERROR "GraphicksMagick++ not found")
elseif (IMAGE_LIBRARY STREQUAL STB AND NOT STB_FOUND)
message(FATAL_ERROR "STB not found")
endif ()
message(STATUS "IMAGE_LIBRARY set to ${IMAGE_LIBRARY}")
add_subdirectory(src) add_subdirectory(src)
+7 -4
View File
@@ -92,10 +92,11 @@ __Notes__:
* a C++17 compiler is needed * a C++17 compiler is needed
* ffmpeg version 4 minimum is required * ffmpeg version 4 minimum is required
```sh ```sh
apt-get install g++ cmake libboost-system-dev libavutil-dev libavformat-dev libgraphicsmagick++1-dev libconfig++-dev libpstreams-dev ffmpeg libtag1-dev libpam0g-dev apt-get install g++ cmake libboost-system-dev libavutil-dev libavformat-dev libstb-dev libconfig++-dev libpstreams-dev ffmpeg libtag1-dev libpam0g-dev
``` ```
__Notes__:
__Note__: package libpam0g-dev is optional (only for using PAM authentication) * libpam0g-dev is optional (only for using PAM authentication)
* libstb-dev can be replaced by libgraphicsmagick++1-dev (the latter will likely use more RAM)
You also need _Wt4_, which is not packaged yet on _Debian_. See [installation instructions](https://www.webtoolkit.eu/wt/doc/reference/html/InstallationUnix.html).</br> You also need _Wt4_, which is not packaged yet on _Debian_. See [installation instructions](https://www.webtoolkit.eu/wt/doc/reference/html/InstallationUnix.html).</br>
No optional requirement is needed, except openSSL if you plan not to deploy behind a reverse proxy (which is not recommended). No optional requirement is needed, except openSSL if you plan not to deploy behind a reverse proxy (which is not recommended).
@@ -110,7 +111,9 @@ mkdir build
cd build cd build
cmake .. -DCMAKE_BUILD_TYPE=Release cmake .. -DCMAKE_BUILD_TYPE=Release
``` ```
__Note__: in order to customize the installation directory, you can use the _-DCMAKE_INSTALL_PREFIX_ option (defaults to `/usr/local`). __Notes__:
* you can customize the installation directory using `-DCMAKE_INSTALL_PREFIX=path` (defaults to `/usr/local`).
* you can customize the image library using `-DIMAGE_LIBRARY=<STB|GraphicksMagick++>`
```sh ```sh
make make
+11
View File
@@ -0,0 +1,11 @@
find_path(STB_INCLUDE_DIR stb/stb.h)
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(
STB
FOUND_VAR STB_FOUND
REQUIRED_VARS STB_INCLUDE_DIR
)
+3
View File
@@ -48,3 +48,6 @@ cover-max-file-size = 10;
# Max cover cache size in MBytes # Max cover cache size in MBytes
cover-max-cache-size = 30; cover-max-cache-size = 30;
# JPEG quality for covers (range is 1-100)
cover-jpeg-quality = 75;
+1
View File
@@ -10,6 +10,7 @@ WorkingDirectory=/var/lms
ExecStart=/usr/bin/lms ExecStart=/usr/bin/lms
User=lms User=lms
Group=lms Group=lms
Environment=OMP_THREAD_LIMIT=1
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target
+11 -48
View File
@@ -5,7 +5,7 @@ WORKDIR /tmp/workdir
ARG MAKEFLAGS="-j2" ARG MAKEFLAGS="-j2"
ARG FFMPEG_VERSION=4.1.4 ARG FFMPEG_VERSION=4.1.4
ARG WT_VERSION=4.2.0 ARG WT_VERSION=4.2.0
ARG GRAPHICSMAGICK_VERSION=1.3.35 ARG STB_VERSION=b42009b3b9d4ca35bc703f5310eedc74f584be58
ARG PSTREAMS_VERSION=1.0.1 ARG PSTREAMS_VERSION=1.0.1
ARG LMS_VERSION=v3.6.3 ARG LMS_VERSION=v3.6.3
@@ -22,11 +22,9 @@ ARG BUILD_PACKAGES=" \
libtool \ libtool \
g++ \ g++ \
make \ make \
libjpeg-turbo-dev \
openjpeg-dev \ openjpeg-dev \
jasper-dev \ jasper-dev \
libpng-dev \ libpng-dev \
tiff-dev \
nasm \ nasm \
yasm \ yasm \
curl \ curl \
@@ -43,7 +41,15 @@ ARG BUILD_PACKAGES=" \
RUN apk add --no-cache --update ${BUILD_PACKAGES} RUN apk add --no-cache --update ${BUILD_PACKAGES}
## ffmpeg # STB
RUN \
DIR=/tmp/stb && mkdir -p ${DIR} && cd ${DIR} && \
curl -sLO https://github.com/nothings/stb/archive/${STB_VERSION}.tar.gz && \
tar -x --strip-components=1 -f ${STB_VERSION}.tar.gz && \
mkdir -p ${PREFIX}/include/stb && \
cp *.h ${PREFIX}/include/stb
# ffmpeg
RUN \ RUN \
DIR=/tmp/ffmpeg && mkdir -p ${DIR} && cd ${DIR} && \ DIR=/tmp/ffmpeg && mkdir -p ${DIR} && cd ${DIR} && \
curl -sLO https://ffmpeg.org/releases/ffmpeg-${FFMPEG_VERSION}.tar.bz2 && \ curl -sLO https://ffmpeg.org/releases/ffmpeg-${FFMPEG_VERSION}.tar.bz2 && \
@@ -73,7 +79,7 @@ RUN \
--enable-libopus \ --enable-libopus \
--enable-libvorbis \ --enable-libvorbis \
--disable-everything \ --disable-everything \
--enable-decoder=aac*,ac3*,alac,als,flac,mp3*,libopus,pcm*,libvorbis,wavpack,wma*,libopenjpg,png,tiff \ --enable-decoder=aac*,ac3*,alac,als,flac,mp3*,libopus,pcm*,libvorbis,wavpack,wma*,libopenjpg,png \
--enable-encoder=libmp3lame,libopus,libvorbis \ --enable-encoder=libmp3lame,libopus,libvorbis \
--enable-demuxer=aac,aiff,asf,flac,ipod,ogg,matroska,mov,mp3,mp4,wav,wv,webm \ --enable-demuxer=aac,aiff,asf,flac,ipod,ogg,matroska,mov,mp3,mp4,wav,wv,webm \
--enable-muxer=ogg,matroska,mp3,webm \ --enable-muxer=ogg,matroska,mp3,webm \
@@ -96,47 +102,6 @@ RUN \
make && \ make && \
make install make install
# GraphicsMagick++
RUN \
DIR=/tmp/graphicsmagick && mkdir -p ${DIR} && cd ${DIR} && \
curl -sL ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/1.3/GraphicsMagick-${GRAPHICSMAGICK_VERSION}.tar.gz -o ${GRAPHICSMAGICK_VERSION}.tar.gz && \
tar -x --strip-components=1 -f ${GRAPHICSMAGICK_VERSION}.tar.gz
RUN \
DIR=/tmp/graphicsmagick && mkdir -p ${DIR} && cd ${DIR} && \
./configure \
--prefix=${PREFIX} \
--enable-shared=yes \
--enable-static=no \
--disable-openmp \
--with-quantum-depth=8 \
--with-modules=no \
--with-frozenpaths=no \
--with-magick-plus-plus=yes \
--with-perl=no \
--with-bzlib=no \
--with-dps=no \
--with-fpx=no \
--with-jbig=no \
--with-webp=no \
--with-jp2=yes \
--with-jpeg=yes \
--with-lcms2=no \
--with-lzma=no \
--with-png=yes \
--with-tiff=yes \
--with-trio=no \
--with-ttf=no \
--with-tcmalloc=no \
--with-wmf=no \
--with-xml=no \
--with-zlib=yes \
--with-zstd=no \
--with-x=no && \
make && \
make install && \
make distclean
# libpstreams # libpstreams
RUN \ RUN \
DIR=/tmp/libpstreams && mkdir -p ${DIR} && cd ${DIR} && \ DIR=/tmp/libpstreams && mkdir -p ${DIR} && cd ${DIR} && \
@@ -183,11 +148,9 @@ LABEL version=${LMS_VERSION}
ARG RUNTIME_PACKAGES=" \ ARG RUNTIME_PACKAGES=" \
openssl \ openssl \
libjpeg-turbo \
openjpeg \ openjpeg \
jasper-libs \ jasper-libs \
libpng \ libpng \
tiff \
libogg \ libogg \
opus \ opus \
libvorbis \ libvorbis \
+1 -1
View File
@@ -39,7 +39,7 @@ static std::string averror_to_string(int error)
std::array<char, 128> buf = {0}; std::array<char, 128> buf = {0};
if (av_strerror(error, buf.data(), buf.size()) == 0) if (av_strerror(error, buf.data(), buf.size()) == 0)
return std::string(&buf[0]); return &buf[0];
else else
return "Unknown error"; return "Unknown error";
} }
+23 -8
View File
@@ -1,8 +1,6 @@
add_library(lmscover SHARED add_library(lmscover SHARED
impl/CoverArt.cpp
impl/CoverArtGrabber.cpp impl/CoverArtGrabber.cpp
impl/Image.cpp
) )
target_include_directories(lmscover INTERFACE target_include_directories(lmscover INTERFACE
@@ -11,22 +9,39 @@ target_include_directories(lmscover INTERFACE
target_include_directories(lmscover PRIVATE target_include_directories(lmscover PRIVATE
include include
${GRAPHICSMAGICKXX_INCLUDE_DIRS}
)
target_compile_options(lmscover PRIVATE
${GRAPHICSMAGICKXX_CFLAGS_OTHER}
) )
target_link_libraries(lmscover PRIVATE target_link_libraries(lmscover PRIVATE
lmsav lmsav
${GRAPHICSMAGICKXX_LIBRARIES}
) )
target_link_libraries(lmscover PUBLIC target_link_libraries(lmscover PUBLIC
lmsdatabase lmsdatabase
lmsutils
std::filesystem 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) install(TARGETS lmscover DESTINATION lib)
+85 -93
View File
@@ -25,58 +25,71 @@
#include "database/Session.hpp" #include "database/Session.hpp"
#include "database/Track.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/Logger.hpp"
#include "utils/Random.hpp" #include "utils/Random.hpp"
#include "utils/Utils.hpp"
#include "Exception.hpp"
#include "CoverArt.hpp" namespace CoverArt {
namespace {
static
bool bool
isFileSupported(const std::filesystem::path& file, const std::vector<std::filesystem::path>& extensions) 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)); return (std::find(std::cbegin(extensions), std::cend(extensions), file.extension()) != std::cend(extensions));
} }
} // namespace std::unique_ptr<IGrabber>
createGrabber(const std::filesystem::path& execPath,
namespace CoverArt { const std::filesystem::path& defaultCoverPath,
std::size_t maxCacheSize, std::size_t maxFileSize, unsigned jpegQuality)
std::unique_ptr<IGrabber> createGrabber(const std::filesystem::path& execPath, std::size_t maxCacheSize, std::size_t maxFileSize)
{ {
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, Grabber::Grabber(const std::filesystem::path& execPath,
const std::filesystem::path& defaultCoverPath,
std::size_t maxCacheSize, std::size_t maxCacheSize,
std::size_t maxFileSize) std::size_t maxFileSize,
: _maxCacheSize {maxCacheSize} unsigned jpegQuality)
: _defaultCoverPath {defaultCoverPath}
, _maxCacheSize {maxCacheSize}
, _maxFileSize {maxFileSize} , _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 cache size = " << _maxCacheSize;
LMS_LOG(COVER, INFO) << "Max file size = " << _maxFileSize; 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 try
{ {
RawImage defaultCover {p}; getDefault(512);
_defaultCover = defaultCover.encode();
LMS_LOG(COVER, INFO) << "Successfully read default cover image!";
} }
catch (const ImageException& e) 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> std::unique_ptr<IEncodedImage>
getFromAvMediaFile(const Av::MediaFile& input, Width width) Grabber::getFromAvMediaFile(const Av::MediaFile& input, ImageSize width) const
{ {
std::optional<EncodedImage> image; std::unique_ptr<IEncodedImage> image;
input.visitAttachedPictures([&](const Av::Picture& picture) input.visitAttachedPictures([&](const Av::Picture& picture)
{ {
@@ -85,12 +98,9 @@ getFromAvMediaFile(const Av::MediaFile& input, Width width)
try try
{ {
EncodedImage encodedImage {picture.data, picture.dataSize}; RawImage rawImage {picture.data, picture.dataSize};
rawImage.resize(width);
RawImage rawImage {encodedImage}; image = rawImage.encodeToJPEG(_jpegQuality);
rawImage.scale(width);
image = rawImage.encode();
} }
catch (const ImageException& e) catch (const ImageException& e)
{ {
@@ -101,17 +111,16 @@ getFromAvMediaFile(const Av::MediaFile& input, Width width)
return image; return image;
} }
static std::optional<EncodedImage> std::unique_ptr<IEncodedImage>
getFromFile(const std::filesystem::path& p, Width width) Grabber::getFromFile(const std::filesystem::path& p, ImageSize width) const
{ {
std::optional<EncodedImage> image; std::unique_ptr<IEncodedImage> image;
try try
{ {
RawImage rawImage {p}; RawImage rawImage {p};
rawImage.scale(width); rawImage.resize(width);
image = rawImage.encodeToJPEG(_jpegQuality);
image = rawImage.encode();
} }
catch (const ImageException& e) catch (const ImageException& e)
{ {
@@ -121,53 +130,50 @@ getFromFile(const std::filesystem::path& p, Width width)
return image; return image;
} }
EncodedImage std::shared_ptr<IEncodedImage>
Grabber::getDefault(Width width) Grabber::getDefault(ImageSize width)
{ {
{ {
std::shared_lock lock {_cacheMutex}; 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; return it->second;
} }
{ {
std::unique_lock lock {_cacheMutex}; 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; return it->second;
RawImage rawImage {*_defaultCover}; std::shared_ptr<IEncodedImage> image {getFromFile(_defaultCoverPath, width)};
rawImage.scale(width); _defaultCoverCache[width] = image;
EncodedImage res {rawImage.encode()}; LMS_LOG(COVER, DEBUG) << "Default cache entries = " << _defaultCoverCache.size();
_defaultCache[width] = res; return image;
LMS_LOG(COVER, DEBUG) << "Default cache entries = " << _defaultCache.size();
return res;
} }
} }
std::optional<EncodedImage> std::unique_ptr<IEncodedImage>
Grabber::getFromDirectory(const std::filesystem::path& p, std::string_view preferredFileName, Width width) const 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)}; const std::multimap<std::string, std::filesystem::path> coverPaths {getCoverPaths(p)};
auto tryLoadImageFromFilename = [&](std::string_view fileName) auto tryLoadImageFromFilename = [&](std::string_view fileName)
{ {
std::optional<EncodedImage> image; std::unique_ptr<IEncodedImage> image;
auto range {coverPaths.equal_range(std::string {fileName})}; auto range {coverPaths.equal_range(std::string {fileName})};
for (auto it {range.first}; it != range.second; ++it) for (auto it {range.first}; it != range.second; ++it)
{ {
image = getFromFile(it->second, width); image = getFromFile(it->second, width);
if (!image) if (image)
continue; break;
} }
return image; return image;
}; };
std::optional<EncodedImage> image; std::unique_ptr<IEncodedImage> image;
if (!preferredFileName.empty()) if (!preferredFileName.empty())
{ {
@@ -225,10 +231,10 @@ Grabber::getCoverPaths(const std::filesystem::path& directoryPath) const
return res; return res;
} }
std::optional<EncodedImage> std::unique_ptr<IEncodedImage>
Grabber::getFromTrack(const std::filesystem::path& p, Width width) const Grabber::getFromTrack(const std::filesystem::path& p, ImageSize width) const
{ {
std::optional<EncodedImage> image; std::unique_ptr<IEncodedImage> image;
try try
{ {
@@ -244,16 +250,16 @@ Grabber::getFromTrack(const std::filesystem::path& p, Width width) const
return image; return image;
} }
EncodedImage std::shared_ptr<IEncodedImage>
Grabber::getFromTrackInternal(Database::Session& dbSession, Database::IdType trackId, Width width) Grabber::getFromTrack(Database::Session& dbSession, Database::IdType trackId, ImageSize width)
{ {
using namespace Database; using namespace Database;
const CacheEntryDesc cacheEntryDesc {CacheEntryDesc::Type::Track, trackId, width}; const CacheEntryDesc cacheEntryDesc {CacheEntryDesc::Type::Track, trackId, width};
std::optional<EncodedImage> cover {loadFromCache(cacheEntryDesc)};
std::shared_ptr<IEncodedImage> cover {loadFromCache(cacheEntryDesc)};
if (cover) if (cover)
return *cover; return cover;
bool hasCover {}; bool hasCover {};
bool isMultiDisc {}; bool isMultiDisc {};
@@ -289,19 +295,20 @@ Grabber::getFromTrackInternal(Database::Session& dbSession, Database::IdType tra
if (!cover) if (!cover)
cover = getDefault(width); cover = getDefault(width);
saveToCache(cacheEntryDesc, *cover); if (cover)
saveToCache(cacheEntryDesc, cover);
return *cover; return cover;
} }
EncodedImage std::shared_ptr<IEncodedImage>
Grabber::getFromReleaseInternal(Database::Session& session, Database::IdType releaseId, Width width) Grabber::getFromRelease(Database::Session& session, Database::IdType releaseId, ImageSize width)
{ {
const CacheEntryDesc cacheEntryDesc {CacheEntryDesc::Type::Release, releaseId, width}; const CacheEntryDesc cacheEntryDesc {CacheEntryDesc::Type::Release, releaseId, width};
std::optional<EncodedImage> cover {loadFromCache(cacheEntryDesc)};
std::shared_ptr<IEncodedImage> cover {loadFromCache(cacheEntryDesc)};
if (cover) if (cover)
return *cover; return cover;
std::optional<Database::IdType> trackId; std::optional<Database::IdType> trackId;
{ {
@@ -317,16 +324,14 @@ Grabber::getFromReleaseInternal(Database::Session& session, Database::IdType rel
} }
if (trackId) if (trackId)
{ cover = getFromTrack(session, *trackId, width);
cover = getFromTrackInternal(session, *trackId, width); else
}
if (!cover)
cover = getDefault(width); cover = getDefault(width);
saveToCache(cacheEntryDesc, *cover); if (cover)
saveToCache(cacheEntryDesc, cover);
return *cover; return cover;
} }
void void
@@ -341,36 +346,23 @@ Grabber::flushCache()
_cache.clear(); _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 void
Grabber::saveToCache(const CacheEntryDesc& entryDesc, const EncodedImage& image) Grabber::saveToCache(const CacheEntryDesc& entryDesc, std::shared_ptr<IEncodedImage> image)
{ {
std::unique_lock lock {_cacheMutex}; std::unique_lock lock {_cacheMutex};
while (_cacheSize + image.getDataSize() > _maxCacheSize && !_cache.empty()) while (_cacheSize + image->getDataSize() > _maxCacheSize && !_cache.empty())
{ {
auto it {Random::pickRandom(_cache)}; auto itRandom {Random::pickRandom(_cache)};
_cacheSize -= it->second.getDataSize(); _cacheSize -= itRandom->second->getDataSize();
_cache.erase(it); _cache.erase(itRandom);
} }
_cacheSize += image.getDataSize(); _cacheSize += image->getDataSize();
_cache[entryDesc] = image; _cache[entryDesc] = image;
} }
std::optional<EncodedImage> std::shared_ptr<IEncodedImage>
Grabber::loadFromCache(const CacheEntryDesc& entryDesc) Grabber::loadFromCache(const CacheEntryDesc& entryDesc)
{ {
std::shared_lock lock {_cacheMutex}; std::shared_lock lock {_cacheMutex};
@@ -379,7 +371,7 @@ Grabber::loadFromCache(const CacheEntryDesc& entryDesc)
if (it == std::cend(_cache)) if (it == std::cend(_cache))
{ {
++_cacheMisses; ++_cacheMisses;
return std::nullopt; return nullptr;
} }
++_cacheHits; ++_cacheHits;
+25 -19
View File
@@ -21,6 +21,7 @@
#include <atomic> #include <atomic>
#include <filesystem> #include <filesystem>
#include <map>
#include <optional> #include <optional>
#include <shared_mutex> #include <shared_mutex>
#include <string_view> #include <string_view>
@@ -28,14 +29,19 @@
#include <vector> #include <vector>
#include "cover/ICoverArtGrabber.hpp" #include "cover/ICoverArtGrabber.hpp"
#include "cover/IEncodedImage.hpp"
#include "database/Types.hpp" #include "database/Types.hpp"
#include "Image.hpp"
namespace Database namespace Database
{ {
class Session; class Session;
} }
namespace Av
{
class MediaFile;
}
namespace CoverArt namespace CoverArt
{ {
struct CacheEntryDesc struct CacheEntryDesc
@@ -83,7 +89,11 @@ namespace CoverArt
class Grabber : public IGrabber class Grabber : public IGrabber
{ {
public: 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(const Grabber&) = delete;
Grabber& operator=(const Grabber&) = delete; Grabber& operator=(const Grabber&) = delete;
@@ -91,38 +101,34 @@ namespace CoverArt
Grabber& operator=(Grabber&&) = delete; Grabber& operator=(Grabber&&) = delete;
private: private:
std::shared_ptr<IEncodedImage> getFromTrack(Database::Session& dbSession, Database::IdType trackId, ImageSize width) override;
void setDefaultCover(const std::filesystem::path& defaultCoverPath) override; std::shared_ptr<IEncodedImage> getFromRelease(Database::Session& dbSession, Database::IdType releaseId, ImageSize width) 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;
void flushCache() override; void flushCache() override;
EncodedImage getFromTrackInternal(Database::Session& dbSession, Database::IdType trackId, Width width); std::unique_ptr<IEncodedImage> getFromAvMediaFile(const Av::MediaFile& input, ImageSize width) const;
EncodedImage getFromReleaseInternal(Database::Session& dbSession, Database::IdType releaseId, Width width); 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::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; std::unique_ptr<IEncodedImage> getFromDirectory(const std::filesystem::path& path, std::string_view preferredFileName, ImageSize width) const;
EncodedImage getDefault(Width width); std::shared_ptr<IEncodedImage> getDefault(ImageSize width);
EncodedImage resizeCoverOrFallback(EncodedImage image, Width width) const;
std::optional<EncodedImage> _defaultCover; // optional to defer initializing
std::shared_mutex _cacheMutex; std::shared_mutex _cacheMutex;
std::unordered_map<CacheEntryDesc, EncodedImage> _cache; std::unordered_map<CacheEntryDesc, std::shared_ptr<IEncodedImage>> _cache;
std::unordered_map<Width, EncodedImage> _defaultCache; std::unordered_map<ImageSize, std::shared_ptr<IEncodedImage>> _defaultCoverCache;
std::atomic<std::size_t> _cacheMisses {}; std::atomic<std::size_t> _cacheMisses {};
std::atomic<std::size_t> _cacheHits {}; std::atomic<std::size_t> _cacheHits {};
std::size_t _cacheSize {}; std::size_t _cacheSize {};
void saveToCache(const CacheEntryDesc& entryDesc, const EncodedImage& image); void saveToCache(const CacheEntryDesc& entryDesc, std::shared_ptr<IEncodedImage> image);
std::optional<EncodedImage> loadFromCache(const CacheEntryDesc& entryDesc); std::shared_ptr<IEncodedImage> loadFromCache(const CacheEntryDesc& entryDesc);
const std::filesystem::path _defaultCoverPath;
const std::size_t _maxCacheSize; const std::size_t _maxCacheSize;
static inline const std::vector<std::filesystem::path> _fileExtensions {".jpg", ".jpeg", ".png", ".bmp"}; // TODO parametrize static inline const std::vector<std::filesystem::path> _fileExtensions {".jpg", ".jpeg", ".png", ".bmp"}; // TODO parametrize
const std::size_t _maxFileSize; const std::size_t _maxFileSize;
static inline const std::vector<std::string> _preferredFileNames {"cover", "front"}; // TODO parametrize static inline const std::vector<std::string> _preferredFileNames {"cover", "front"}; // TODO parametrize
const unsigned _jpegQuality;
}; };
} // namespace CoverArt } // namespace CoverArt
@@ -19,17 +19,10 @@
#pragma once #pragma once
#include <filesystem>
#include <vector>
#include <Magick++.h>
#include "utils/Exception.hpp" #include "utils/Exception.hpp"
namespace CoverArt namespace CoverArt
{ {
void init(const std::filesystem::path& path);
// internal use only // internal use only
class ImageException : public LmsException class ImageException : public LmsException
{ {
@@ -37,37 +30,5 @@ namespace CoverArt
using LmsException::LmsException; 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 } // namespace CoverArt
@@ -17,31 +17,19 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>. * along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "CoverArt.hpp" #pragma once
#include <memory>
#include "cover/IEncodedImage.hpp"
namespace CoverArt namespace CoverArt
{ {
class IRawImage
CoverArt::CoverArt(EncodedImage image)
: _image {image}
{}
const std::byte*
CoverArt::getData() const
{ {
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/>. * along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "Image.hpp" #include "RawImage.hpp"
#include <atomic>
#include <fstream>
#include <magick/resource.h> #include <magick/resource.h>
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
#include "JPEGImage.hpp"
#include "Exception.hpp"
namespace CoverArt { namespace CoverArt::GraphicsMagick {
void void
init(const std::filesystem::path& path) init(const std::filesystem::path& path)
{ {
Magick::InitializeMagick(path.string().c_str()); 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)) if (!MagickLib::SetMagickResourceLimit(MagickLib::ThreadsResource, 1))
LMS_LOG(COVER, ERROR) << "Cannot set Magick thread resource limit to 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); LMS_LOG(COVER, INFO) << "Magick Disk resource limit = " << GetMagickResourceLimit(MagickLib::DiskResource);
} }
EncodedImage::EncodedImage(const std::byte* data, std::size_t dataSize) RawImage::RawImage(const std::byte* encodedData, std::size_t encodedDataSize)
: _blob {data, dataSize}
{ {
try
{
Magick::Blob blob {encodedData, encodedDataSize};
_image.read(blob);
} }
catch (Magick::WarningCoder& e)
EncodedImage::EncodedImage(Magick::Blob blob)
: _blob {blob}
{ {
LMS_LOG(COVER, WARNING) << "Caught Magick WarningCoder: " << e.what();
} }
catch (Magick::Warning& e)
const std::byte*
EncodedImage::getData() const
{ {
return reinterpret_cast<const std::byte*>(_blob.data()); LMS_LOG(COVER, WARNING) << "Caught Magick warning: " << e.what();
throw ImageException {std::string {"Magick read warning: "} + e.what()};
} }
catch (Magick::Exception& e)
std::size_t
EncodedImage::getDataSize() const
{ {
return _blob.length(); 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) RawImage::RawImage(const std::filesystem::path& p)
@@ -73,79 +76,45 @@ RawImage::RawImage(const std::filesystem::path& p)
} }
catch (Magick::WarningCoder& e) 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) 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()}; throw ImageException {std::string {"Magick read warning: "} + e.what()};
} }
catch (Magick::Exception& e) catch (Magick::Exception& e)
{ {
LMS_LOG(COVER, ERROR) << "Caught Magick exception while loading raw image '" << p.string() << "': " << e.what(); LMS_LOG(COVER, ERROR) << "Caught Magick exception: " << 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();
throw ImageException {std::string {"Magick read error: "} + e.what()}; throw ImageException {std::string {"Magick read error: "} + e.what()};
} }
} }
void void
RawImage::scale(std::size_t width) RawImage::resize(ImageSize width)
{ {
if (width == 0)
throw ImageException {"Bad width = 0"};
try try
{ {
_image.resize(Magick::Geometry {static_cast<unsigned int>(width), static_cast<unsigned int>(width)}); _image.resize(Magick::Geometry {static_cast<unsigned int>(width), static_cast<unsigned int>(width)});
} }
catch (Magick::Exception& e) 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()}; throw ImageException {std::string {"Magick resize error: "} + e.what()};
} }
} }
EncodedImage std::unique_ptr<IEncodedImage>
RawImage::encode() const RawImage::encodeToJPEG(unsigned quality) const
{ {
try return std::make_unique<JPEGImage>(*this, quality);
{
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()};
}
} }
} // 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/>. * along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #include <vector>
#include "cover/ICoverArt.hpp" #include "cover/IEncodedImage.hpp"
#include "Image.hpp"
namespace CoverArt namespace CoverArt::STB
{ {
class RawImage;
class CoverArt : public ICoverArt class JPEGImage : public IEncodedImage
{ {
public: public:
CoverArt(EncodedImage image); JPEGImage(const RawImage& rawImage, unsigned quality);
const std::byte* getData() const override;
std::size_t getDataSize() const override;
std::string_view getMimeType() const override;
private: 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 #pragma once
#include <cstddef>
#include <filesystem> #include <filesystem>
#include <memory> #include <memory>
#include <string_view>
#include "database/Types.hpp" #include "database/Types.hpp"
#include "cover/ICoverArt.hpp" #include "cover/IEncodedImage.hpp"
namespace Database namespace Database
{ {
@@ -34,22 +32,22 @@ namespace Database
namespace CoverArt namespace CoverArt
{ {
using Width = std::size_t;
class IGrabber class IGrabber
{ {
public: public:
virtual ~IGrabber() = default; virtual ~IGrabber() = default;
virtual void setDefaultCover(const std::filesystem::path& defaultCoverPath) = 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 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 void flushCache() = 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 } // namespace CoverArt
@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2020 Emeric Poupon * Copyright (C) 2015 Emeric Poupon
* *
* This file is part of LMS. * This file is part of LMS.
* *
@@ -24,11 +24,12 @@
namespace CoverArt namespace CoverArt
{ {
using ImageSize = std::size_t;
class ICoverArt class IEncodedImage
{ {
public: public:
virtual ~ICoverArt() = default; virtual ~IEncodedImage() = default;
virtual const std::byte* getData() const = 0; virtual const std::byte* getData() const = 0;
virtual std::size_t getDataSize() const = 0; virtual std::size_t getDataSize() const = 0;
@@ -36,3 +37,4 @@ namespace CoverArt
}; };
} // namespace CoverArt } // namespace CoverArt
-1
View File
@@ -9,7 +9,6 @@ add_library(lmsdatabase SHARED
impl/Release.cpp impl/Release.cpp
impl/ScanSettings.cpp impl/ScanSettings.cpp
impl/Session.cpp impl/Session.cpp
impl/SessionPool.cpp
impl/SqlQuery.cpp impl/SqlQuery.cpp
impl/Track.cpp impl/Track.cpp
impl/TrackBookmark.cpp impl/TrackBookmark.cpp
+4 -4
View File
@@ -32,14 +32,14 @@ Cluster::Cluster()
{ {
} }
Cluster::Cluster(Wt::Dbo::ptr<ClusterType> type, std::string name) Cluster::Cluster(Wt::Dbo::ptr<ClusterType> type, std::string_view name)
: _name(std::string(name, 0, _maxNameLength)), : _name(std::string {name, 0, _maxNameLength}),
_clusterType(type) _clusterType {type}
{ {
} }
Cluster::pointer 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(); session.checkUniqueLocked();
+19
View File
@@ -22,6 +22,7 @@
#include <Wt/Dbo/FixedSqlConnectionPool.h> #include <Wt/Dbo/FixedSqlConnectionPool.h>
#include <Wt/Dbo/backend/Sqlite3.h> #include <Wt/Dbo/backend/Sqlite3.h>
#include "database/Session.hpp"
#include "database/User.hpp" #include "database/User.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
@@ -57,6 +58,24 @@ Db::executeSql(const std::string& sql)
connection->executeSql(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) Db::ScopedConnection::ScopedConnection(Wt::Dbo::SqlConnectionPool& pool)
: _connectionPool {pool} : _connectionPool {pool}
-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 #pragma once
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
#include <Wt/Dbo/Dbo.h> #include <Wt/Dbo/Dbo.h>
@@ -41,7 +42,7 @@ class Cluster : public Wt::Dbo::Dbo<Cluster>
using pointer = Wt::Dbo::ptr<Cluster>; using pointer = Wt::Dbo::ptr<Cluster>;
Cluster(); Cluster();
Cluster(Wt::Dbo::ptr<ClusterType> type, std::string name); Cluster(Wt::Dbo::ptr<ClusterType> type, std::string_view name);
// Find utility // Find utility
static std::vector<pointer> getAll(Session& session); 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); static pointer getById(Session& session, IdType id);
// Create utility // 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 // Accessors
const std::string& getName() const { return _name; } const std::string& getName() const { return _name; }
+6 -1
View File
@@ -26,7 +26,7 @@
namespace Database { namespace Database {
// Session living class handling the database and the login class Session;
class Db class Db
{ {
public: public:
@@ -39,6 +39,8 @@ class Db
Db& operator=(const Db&) = delete; Db& operator=(const Db&) = delete;
Db& operator=(Db&&) = delete; Db& operator=(Db&&) = delete;
Session& getTLSSession();
private: private:
friend class Session; friend class Session;
@@ -88,6 +90,9 @@ class Db
std::shared_mutex _sharedMutex; std::shared_mutex _sharedMutex;
std::unique_ptr<Wt::Dbo::SqlConnectionPool> _connectionPool; std::unique_ptr<Wt::Dbo::SqlConnectionPool> _connectionPool;
std::mutex _tlsSessionsMutex;
std::vector<std::unique_ptr<Session>> _tlsSessions;
}; };
} // namespace Database } // namespace Database
@@ -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) 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)}; 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}); 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) switch (id.type)
{ {
case Id::Type::Track: case Id::Type::Track:
@@ -1911,9 +1911,9 @@ SubsonicResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Resp
clientName = clientInfo.name; 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()), boost::asio::ip::address::from_string(request.clientAddress()),
clientInfo.user, clientInfo.password)) clientInfo.user, clientInfo.password))
{ {
@@ -1925,16 +1925,16 @@ SubsonicResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Resp
throw LoginThrottledGenericError {}; throw LoginThrottledGenericError {};
} }
RequestContext requestContext {parameters, dbSession.get(), clientInfo.user, clientInfo.name}; RequestContext requestContext {parameters, dbSession, clientInfo.user, clientInfo.name};
auto itEntryPoint {requestEntryPoints.find(requestPath)}; auto itEntryPoint {requestEntryPoints.find(requestPath)};
if (itEntryPoint != requestEntryPoints.end()) if (itEntryPoint != requestEntryPoints.end())
{ {
if (itEntryPoint->second.mustBeAdmin) 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()) if (!user || !user->isAdmin())
throw UserNotAuthorizedError {}; throw UserNotAuthorizedError {};
} }
@@ -21,8 +21,6 @@
#include <Wt/WResource.h> #include <Wt/WResource.h>
#include <Wt/Http/Response.h> #include <Wt/Http/Response.h>
#include "database/SessionPool.hpp"
namespace Database namespace Database
{ {
class Db; class Db;
@@ -41,7 +39,7 @@ class SubsonicResource final : public Wt::WResource
void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) override; void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) override;
Database::SessionPool _sessionPool; Database::Db& _db;
}; };
} // namespace } // namespace
+6 -4
View File
@@ -28,6 +28,7 @@
#include "av/AvTranscoder.hpp" #include "av/AvTranscoder.hpp"
#include "cover/ICoverArtGrabber.hpp" #include "cover/ICoverArtGrabber.hpp"
#include "database/Db.hpp" #include "database/Db.hpp"
#include "database/Session.hpp"
#include "scanner/IMediaScanner.hpp" #include "scanner/IMediaScanner.hpp"
#include "recommendation/IEngine.hpp" #include "recommendation/IEngine.hpp"
#include "subsonic/SubsonicResource.hpp" #include "subsonic/SubsonicResource.hpp"
@@ -140,7 +141,7 @@ int main(int argc, char* argv[])
std::filesystem::create_directories(config->getPath("working-dir") / "cache"); std::filesystem::create_directories(config->getPath("working-dir") / "cache");
// Construct WT configuration and get the argc/argv back // 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()); std::vector<const char*> wtArgv(wtServerArgs.size());
for (std::size_t i = 0; i < wtServerArgs.size(); ++i) for (std::size_t i = 0; i < wtServerArgs.size(); ++i)
@@ -149,7 +150,7 @@ int main(int argc, char* argv[])
wtArgv[i] = wtServerArgs[i].c_str(); wtArgv[i] = wtServerArgs[i].c_str();
} }
Wt::WServer server(argv[0]); Wt::WServer server {argv[0]};
server.setServerConfiguration(wtServerArgs.size(), const_cast<char**>(&wtArgv[0])); server.setServerConfiguration(wtServerArgs.size(), const_cast<char**>(&wtArgv[0]));
// lib init // lib 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::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<Auth::IPasswordService> passwordService {Auth::createPasswordService(config->getULong("login-throttler-max-entriees", 10000))};
Service<CoverArt::IGrabber> coverArtService {CoverArt::createGrabber(argv[0], 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-cache-size", 30) * 1000 * 1000,
config->getULong("cover-max-file-size", 10) * 1000 * 1000)}; config->getULong("cover-max-file-size", 10) * 1000 * 1000,
coverArtService->setDefaultCover(server.appRoot() + "/images/unknown-cover.jpg"); config->getULong("cover-jpeg-quality", 75))};
Service<Recommendation::IEngine> recommendationEngineService {Recommendation::createEngine(database)}; Service<Recommendation::IEngine> recommendationEngineService {Recommendation::createEngine(database)};
recommendationEngineService->requestLoad(); recommendationEngineService->requestLoad();
Service<Scanner::IMediaScanner> mediaScannerService {Scanner::createMediaScanner(database)}; Service<Scanner::IMediaScanner> mediaScannerService {Scanner::createMediaScanner(database)};
+1
View File
@@ -29,6 +29,7 @@
#include "auth/IAuthTokenService.hpp" #include "auth/IAuthTokenService.hpp"
#include "auth/IPasswordService.hpp" #include "auth/IPasswordService.hpp"
#include "database/Session.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
#include "utils/Service.hpp" #include "utils/Service.hpp"
+19 -12
View File
@@ -32,6 +32,7 @@
#include "database/Cluster.hpp" #include "database/Cluster.hpp"
#include "database/Db.hpp" #include "database/Db.hpp"
#include "database/Release.hpp" #include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/User.hpp" #include "database/User.hpp"
#include "explore/Explore.hpp" #include "explore/Explore.hpp"
#include "explore/Filters.hpp" #include "explore/Filters.hpp"
@@ -71,13 +72,19 @@ LmsApplication::instance()
return reinterpret_cast<LmsApplication*>(Wt::WApplication::instance()); return reinterpret_cast<LmsApplication*>(Wt::WApplication::instance());
} }
Database::Session&
LmsApplication::getDbSession()
{
return _db.getTLSSession();
}
Wt::Dbo::ptr<Database::User> Wt::Dbo::ptr<Database::User>
LmsApplication::getUser() LmsApplication::getUser()
{ {
if (!_userId) if (!_userId)
return {}; return {};
return Database::User::getById(_dbSession, *_userId); return Database::User::getById(getDbSession(), *_userId);
} }
bool bool
@@ -89,7 +96,7 @@ LmsApplication::isUserAuthStrong() const
bool bool
LmsApplication::isUserAdmin() LmsApplication::isUserAdmin()
{ {
auto transaction {_dbSession.createSharedTransaction()}; auto transaction {getDbSession().createSharedTransaction()};
return getUser()->isAdmin(); return getUser()->isAdmin();
} }
@@ -97,7 +104,7 @@ LmsApplication::isUserAdmin()
bool bool
LmsApplication::isUserDemo() LmsApplication::isUserDemo()
{ {
auto transaction {_dbSession.createSharedTransaction()}; auto transaction {getDbSession().createSharedTransaction()};
return getUser()->isDemo(); return getUser()->isDemo();
} }
@@ -105,7 +112,7 @@ LmsApplication::isUserDemo()
std::string std::string
LmsApplication::getUserLoginName() LmsApplication::getUserLoginName()
{ {
auto transaction {_dbSession.createSharedTransaction()}; auto transaction {getDbSession().createSharedTransaction()};
return getUser()->getLoginName(); return getUser()->getLoginName();
} }
@@ -114,7 +121,7 @@ LmsApplication::LmsApplication(const Wt::WEnvironment& env,
Database::Db& db, Database::Db& db,
LmsApplicationGroupContainer& appGroups) LmsApplicationGroupContainer& appGroups)
: Wt::WApplication {env}, : Wt::WApplication {env},
_dbSession {db}, _db {db},
_appGroups {appGroups} _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 // If here is no account in the database, launch the first connection wizard
bool firstConnection {}; bool firstConnection {};
{ {
auto transaction {_dbSession.createSharedTransaction()}; auto transaction {getDbSession().createSharedTransaction()};
firstConnection = Database::User::getAll(_dbSession).empty(); firstConnection = Database::User::getAll(getDbSession()).empty();
} }
LMS_LOG(UI, DEBUG) << "Creating root widget. First connection = " << firstConnection; 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}; Database::User::UITheme theme {Database::User::defaultUITheme};
if (userId) if (userId)
{ {
auto transaction {_dbSession.createSharedTransaction()}; auto transaction {getDbSession().createSharedTransaction()};
const auto user {Database::User::getById(_dbSession, *userId)}; const auto user {Database::User::getById(getDbSession(), *userId)};
if (user) if (user)
theme = user->getUITheme(); theme = user->getUITheme();
} }
@@ -206,8 +213,8 @@ LmsApplication::LmsApplication(const Wt::WEnvironment& env,
auth->userLoggedIn.connect(this, [this](Database::IdType userId) auth->userLoggedIn.connect(this, [this](Database::IdType userId)
{ {
{ {
auto transaction {_dbSession.createSharedTransaction()}; auto transaction {getDbSession().createSharedTransaction()};
const auto user {Database::User::getById(_dbSession, userId)}; const auto user {Database::User::getById(getDbSession(), userId)};
if (user) if (user)
{ {
LmsTheme* lmsTheme {static_cast<LmsTheme*>(LmsApp->theme().get())}; LmsTheme* lmsTheme {static_cast<LmsTheme*>(LmsApp->theme().get())};
@@ -402,7 +409,7 @@ LmsApplication::handleUserLoggedOut()
LMS_LOG(UI, INFO) << "User '" << getUserLoginName() << " 'logged out"; LMS_LOG(UI, INFO) << "User '" << getUserLoginName() << " 'logged out";
{ {
auto transaction {_dbSession.createUniqueTransaction()}; auto transaction {getDbSession().createUniqueTransaction()};
getUser().modify()->clearAuthTokens(); getUser().modify()->clearAuthTokens();
} }
+3 -4
View File
@@ -24,8 +24,6 @@
#include <Wt/WApplication.h> #include <Wt/WApplication.h>
#include <Wt/WPopupMenu.h> #include <Wt/WPopupMenu.h>
#include "database/Db.hpp"
#include "database/Session.hpp"
#include "scanner/IMediaScanner.hpp" #include "scanner/IMediaScanner.hpp"
#include "LmsApplicationGroup.hpp" #include "LmsApplicationGroup.hpp"
@@ -35,6 +33,7 @@ namespace Database {
class Cluster; class Cluster;
class Db; class Db;
class Release; class Release;
class Session;
class User; class User;
} }
@@ -83,7 +82,7 @@ class LmsApplication : public Wt::WApplication
std::shared_ptr<ImageResource> getImageResource() { return _imageResource; } std::shared_ptr<ImageResource> getImageResource() { return _imageResource; }
std::shared_ptr<AudioTranscodeResource> getAudioTranscodeResource() { return _audioTranscodeResource; } std::shared_ptr<AudioTranscodeResource> getAudioTranscodeResource() { return _audioTranscodeResource; }
std::shared_ptr<AudioFileResource> getAudioFileResource() { return _audioFileResource; } std::shared_ptr<AudioFileResource> getAudioFileResource() { return _audioFileResource; }
Database::Session& getDbSession() { return _dbSession;} Database::Session& getDbSession(); // always thread safe
Wt::Dbo::ptr<Database::User> getUser(); Wt::Dbo::ptr<Database::User> getUser();
bool isUserAuthStrong() const; // user must be logged in prior this call bool isUserAuthStrong() const; // user must be logged in prior this call
@@ -126,8 +125,8 @@ class LmsApplication : public Wt::WApplication
void createHome(); void createHome();
Database::Db& _db;
Wt::Signal<> _preQuit; Wt::Signal<> _preQuit;
Database::Session _dbSession;
LmsApplicationGroupContainer& _appGroups; LmsApplicationGroupContainer& _appGroups;
Events _events; Events _events;
std::optional<Database::IdType> _userId; std::optional<Database::IdType> _userId;
+1
View File
@@ -27,6 +27,7 @@
#include "database/Artist.hpp" #include "database/Artist.hpp"
#include "database/Release.hpp" #include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp" #include "database/Track.hpp"
#include "database/TrackList.hpp" #include "database/TrackList.hpp"
#include "database/User.hpp" #include "database/User.hpp"
+1
View File
@@ -23,6 +23,7 @@
#include <Wt/WText.h> #include <Wt/WText.h>
#include "database/Cluster.hpp" #include "database/Cluster.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp" #include "database/Track.hpp"
#include "database/TrackList.hpp" #include "database/TrackList.hpp"
#include "database/User.hpp" #include "database/User.hpp"
+1
View File
@@ -33,6 +33,7 @@
#include "common/ValueStringModel.hpp" #include "common/ValueStringModel.hpp"
#include "auth/IPasswordService.hpp" #include "auth/IPasswordService.hpp"
#include "database/Session.hpp"
#include "utils/IConfig.hpp" #include "utils/IConfig.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
#include "utils/Service.hpp" #include "utils/Service.hpp"
@@ -28,6 +28,7 @@
#include "database/Cluster.hpp" #include "database/Cluster.hpp"
#include "database/ScanSettings.hpp" #include "database/ScanSettings.hpp"
#include "database/Session.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
#include "utils/Service.hpp" #include "utils/Service.hpp"
#include "utils/String.hpp" #include "utils/String.hpp"
+2
View File
@@ -25,6 +25,8 @@
#include <Wt/WPushButton.h> #include <Wt/WPushButton.h>
#include "auth/IPasswordService.hpp" #include "auth/IPasswordService.hpp"
#include "database/Session.hpp"
#include "database/User.hpp"
#include "utils/Exception.hpp" #include "utils/Exception.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
#include "utils/Service.hpp" #include "utils/Service.hpp"
+1
View File
@@ -29,6 +29,7 @@
#include "auth/IPasswordService.hpp" #include "auth/IPasswordService.hpp"
#include "database/User.hpp" #include "database/User.hpp"
#include "database/Session.hpp"
#include "utils/IConfig.hpp" #include "utils/IConfig.hpp"
#include "utils/Exception.hpp" #include "utils/Exception.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
+1
View File
@@ -24,6 +24,7 @@
#include <Wt/WTemplate.h> #include <Wt/WTemplate.h>
#include "database/User.hpp" #include "database/User.hpp"
#include "database/Session.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
#include "LmsApplication.hpp" #include "LmsApplication.hpp"
+1
View File
@@ -27,6 +27,7 @@
#include "database/Artist.hpp" #include "database/Artist.hpp"
#include "database/Release.hpp" #include "database/Release.hpp"
#include "database/ScanSettings.hpp" #include "database/ScanSettings.hpp"
#include "database/Session.hpp"
#include "database/User.hpp" #include "database/User.hpp"
#include "recommendation/IEngine.hpp" #include "recommendation/IEngine.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
+1
View File
@@ -25,6 +25,7 @@
#include "common/ValueStringModel.hpp" #include "common/ValueStringModel.hpp"
#include "database/Artist.hpp" #include "database/Artist.hpp"
#include "database/Session.hpp"
#include "database/User.hpp" #include "database/User.hpp"
#include "database/TrackList.hpp" #include "database/TrackList.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
+1
View File
@@ -25,6 +25,7 @@
#include "database/Artist.hpp" #include "database/Artist.hpp"
#include "database/Release.hpp" #include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp" #include "database/Track.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
+1
View File
@@ -25,6 +25,7 @@
#include <Wt/WTemplate.h> #include <Wt/WTemplate.h>
#include "database/Cluster.hpp" #include "database/Cluster.hpp"
#include "database/Session.hpp"
#include "LmsApplication.hpp" #include "LmsApplication.hpp"
+1
View File
@@ -20,6 +20,7 @@
#include "ReleasePopup.hpp" #include "ReleasePopup.hpp"
#include "database/Release.hpp" #include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/User.hpp" #include "database/User.hpp"
#include "resource/DownloadResource.hpp" #include "resource/DownloadResource.hpp"
#include "LmsApplication.hpp" #include "LmsApplication.hpp"
+1
View File
@@ -27,6 +27,7 @@
#include "database/Release.hpp" #include "database/Release.hpp"
#include "database/ScanSettings.hpp" #include "database/ScanSettings.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp" #include "database/Track.hpp"
#include "recommendation/IEngine.hpp" #include "recommendation/IEngine.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
+2 -1
View File
@@ -27,8 +27,9 @@
#include <Wt/WText.h> #include <Wt/WText.h>
#include "database/Release.hpp" #include "database/Release.hpp"
#include "database/User.hpp" #include "database/Session.hpp"
#include "database/TrackList.hpp" #include "database/TrackList.hpp"
#include "database/User.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
#include "utils/String.hpp" #include "utils/String.hpp"
+1
View File
@@ -24,6 +24,7 @@
#include "database/Artist.hpp" #include "database/Artist.hpp"
#include "database/Release.hpp" #include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp" #include "database/Track.hpp"
#include "resource/ImageResource.hpp" #include "resource/ImageResource.hpp"
+1
View File
@@ -19,6 +19,7 @@
#include "TrackPopup.hpp" #include "TrackPopup.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp" #include "database/Track.hpp"
#include "database/User.hpp" #include "database/User.hpp"
#include "resource/DownloadResource.hpp" #include "resource/DownloadResource.hpp"
+1
View File
@@ -26,6 +26,7 @@
#include "database/Artist.hpp" #include "database/Artist.hpp"
#include "database/Release.hpp" #include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp" #include "database/Track.hpp"
#include "database/TrackList.hpp" #include "database/TrackList.hpp"
+1 -3
View File
@@ -23,6 +23,7 @@
#include <Wt/Http/Response.h> #include <Wt/Http/Response.h>
#include "av/AvInfo.hpp" #include "av/AvInfo.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp" #include "database/Track.hpp"
#include "utils/FileResourceHandlerCreator.hpp" #include "utils/FileResourceHandlerCreator.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
@@ -48,9 +49,6 @@ static
std::optional<std::filesystem::path> std::optional<std::filesystem::path>
getTrackPathFromTrackId(Database::IdType trackId) getTrackPathFromTrackId(Database::IdType trackId)
{ {
// DbSession are not thread safe
Wt::WApplication::UpdateLock lock {LmsApp};
auto transaction {LmsApp->getDbSession().createSharedTransaction()}; auto transaction {LmsApp->getDbSession().createSharedTransaction()};
const Database::Track::pointer track {Database::Track::getById(LmsApp->getDbSession(), trackId)}; const Database::Track::pointer track {Database::Track::getById(LmsApp->getDbSession(), trackId)};
@@ -22,6 +22,7 @@
#include <Wt/Http/Response.h> #include <Wt/Http/Response.h>
#include "av/AvTranscoder.hpp" #include "av/AvTranscoder.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp" #include "database/Track.hpp"
#include "database/User.hpp" #include "database/User.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
@@ -148,8 +149,6 @@ AudioTranscodeResource::handleRequest(const Wt::Http::Request& request,
std::filesystem::path trackPath; std::filesystem::path trackPath;
{ {
// DbSession are not thread safe
Wt::WApplication::UpdateLock lock(LmsApp);
auto transaction {LmsApp->getDbSession().createSharedTransaction()}; auto transaction {LmsApp->getDbSession().createSharedTransaction()};
const Database::Track::pointer track {Database::Track::getById(LmsApp->getDbSession(), *trackId)}; 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/Artist.hpp"
#include "database/Release.hpp" #include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp" #include "database/Track.hpp"
#include "utils/Exception.hpp" #include "utils/Exception.hpp"
#include "utils/Logger.hpp" #include "utils/Logger.hpp"
@@ -182,7 +183,6 @@ DownloadArtistResource::DownloadArtistResource(Database::IdType artistId)
std::unique_ptr<Zip::Zipper> std::unique_ptr<Zip::Zipper>
DownloadArtistResource::createZipper() DownloadArtistResource::createZipper()
{ {
Wt::WApplication::UpdateLock lock {LmsApp}; // DbSession are not thread safe
auto transaction {LmsApp->getDbSession().createSharedTransaction()}; auto transaction {LmsApp->getDbSession().createSharedTransaction()};
const Database::Artist::pointer artist {Database::Artist::getById(LmsApp->getDbSession(), _artistId)}; 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> std::unique_ptr<Zip::Zipper>
DownloadReleaseResource::createZipper() DownloadReleaseResource::createZipper()
{ {
Wt::WApplication::UpdateLock lock {LmsApp}; // DbSession are not thread safe
auto transaction {LmsApp->getDbSession().createSharedTransaction()}; auto transaction {LmsApp->getDbSession().createSharedTransaction()};
const Database::Release::pointer release {Database::Release::getById(LmsApp->getDbSession(), _releaseId)}; 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> std::unique_ptr<Zip::Zipper>
DownloadTrackResource::createZipper() DownloadTrackResource::createZipper()
{ {
Wt::WApplication::UpdateLock lock {LmsApp}; // DbSession are not thread safe
auto transaction {LmsApp->getDbSession().createSharedTransaction()}; auto transaction {LmsApp->getDbSession().createSharedTransaction()};
const Database::Track::pointer track {Database::Track::getById(LmsApp->getDbSession(), _trackId)}; const Database::Track::pointer track {Database::Track::getById(LmsApp->getDbSession(), _trackId)};
+1 -9
View File
@@ -74,7 +74,7 @@ ImageResource::handleRequest(const Wt::Http::Request& request, Wt::Http::Respons
return; return;
} }
std::unique_ptr<CoverArt::ICoverArt> cover; std::shared_ptr<CoverArt::IEncodedImage> cover;
if (trackIdStr) if (trackIdStr)
{ {
@@ -87,12 +87,8 @@ ImageResource::handleRequest(const Wt::Http::Request& request, Wt::Http::Respons
return; 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) else if (releaseIdStr)
{ {
LOG(DEBUG) << "Requested cover for release " << *releaseIdStr << ", size = " << *size; LOG(DEBUG) << "Requested cover for release " << *releaseIdStr << ", size = " << *size;
@@ -101,12 +97,8 @@ ImageResource::handleRequest(const Wt::Http::Request& request, Wt::Http::Respons
if (!releaseId) if (!releaseId)
return; 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 else
{ {
LOG(DEBUG) << "No track or release provided"; LOG(DEBUG) << "No track or release provided";
+1 -3
View File
@@ -1,6 +1,4 @@
add_subdirectory(cover)
add_subdirectory(metadata) add_subdirectory(metadata)
add_subdirectory(recommendation) add_subdirectory(recommendation)
add_subdirectory(zipper) 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;
}