/* * Copyright (C) 2015 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 . */ #pragma once #include #include #include #include #include #include #include #include #include "cover/ICoverArtGrabber.hpp" #include "cover/IEncodedImage.hpp" #include "database/Types.hpp" namespace Database { class Session; } namespace Av { class MediaFile; } namespace CoverArt { struct CacheEntryDesc { enum class Type { Track, Release, }; Type type; Database::IdType id; std::size_t size; bool operator==(const CacheEntryDesc& other) const { return type == other.type && id == other.id && size == other.size; } }; } // ns CoverArt namespace std { template<> class hash { public: size_t operator()(const CoverArt::CacheEntryDesc& e) const { size_t h = std::hash()(static_cast(e.type)); h ^= std::hash()(e.id) << 1; h ^= std::hash()(e.size) << 1; return h; } }; } // ns std namespace CoverArt { class Grabber : public IGrabber { public: 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; Grabber(Grabber&&) = delete; Grabber& operator=(Grabber&&) = delete; private: std::shared_ptr getFromTrack(Database::Session& dbSession, Database::IdType trackId, ImageSize width) override; std::shared_ptr getFromRelease(Database::Session& dbSession, Database::IdType releaseId, ImageSize width) override; void flushCache() override; std::shared_ptr getFromTrack(Database::Session& dbSession, Database::IdType trackId, ImageSize width, bool allowReleaseFallback); std::unique_ptr getFromAvMediaFile(const Av::MediaFile& input, ImageSize width) const; std::unique_ptr getFromCoverFile(const std::filesystem::path& p, ImageSize width) const; std::unique_ptr getFromTrack(const std::filesystem::path& path, ImageSize width) const; std::multimap getCoverPaths(const std::filesystem::path& directoryPath) const; std::unique_ptr getFromDirectory(const std::filesystem::path& directory, ImageSize width) const; std::unique_ptr getFromSameNamedFile(const std::filesystem::path& filePath, ImageSize width) const; std::shared_ptr getDefault(ImageSize width); bool checkCoverFile(const std::filesystem::path& directoryPath) const; std::shared_mutex _cacheMutex; std::unordered_map> _cache; std::unordered_map> _defaultCoverCache; std::atomic _cacheMisses {}; std::atomic _cacheHits {}; std::size_t _cacheSize {}; void saveToCache(const CacheEntryDesc& entryDesc, std::shared_ptr image); std::shared_ptr loadFromCache(const CacheEntryDesc& entryDesc); const std::filesystem::path _defaultCoverPath; const std::size_t _maxCacheSize; static inline const std::vector _fileExtensions {".jpg", ".jpeg", ".png", ".bmp"}; // TODO parametrize const std::size_t _maxFileSize; static inline const std::vector _preferredFileNames {"cover", "front"}; // TODO parametrize const unsigned _jpegQuality; }; } // namespace CoverArt