[UI] Better handling covers: giving browsers a chance to properly cache them
This commit is contained in:
@@ -101,7 +101,7 @@ InputFormatContext::findStreamInfo(void)
|
|||||||
std::size_t
|
std::size_t
|
||||||
InputFormatContext::getDurationSecs() const
|
InputFormatContext::getDurationSecs() const
|
||||||
{
|
{
|
||||||
if (native()->duration != AV_NOPTS_VALUE )
|
if (static_cast<int>(native()->duration) != AV_NOPTS_VALUE )
|
||||||
return native()->duration / AV_TIME_BASE;
|
return native()->duration / AV_TIME_BASE;
|
||||||
else
|
else
|
||||||
return 0; // TODO, do something better?
|
return 0; // TODO, do something better?
|
||||||
@@ -128,18 +128,39 @@ InputFormatContext::getNbPictures(void) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
InputFormatContext::getPictures(std::vector< std::vector<unsigned char> >& pictures) const
|
InputFormatContext::getPictures(std::vector<Picture>& pictures) const
|
||||||
{
|
{
|
||||||
|
static const std::map<int, std::string> codecMimeMap =
|
||||||
|
{
|
||||||
|
{ AV_CODEC_ID_BMP, "image/x-bmp" },
|
||||||
|
{ AV_CODEC_ID_GIF, "image/gif" },
|
||||||
|
{ AV_CODEC_ID_MJPEG, "image/jpeg" },
|
||||||
|
{ AV_CODEC_ID_PNG, "image/png" },
|
||||||
|
{ AV_CODEC_ID_PNG, "image/x-png" },
|
||||||
|
{ AV_CODEC_ID_PPM, "image/x-portable-pixmap" },
|
||||||
|
};
|
||||||
|
|
||||||
for (std::size_t i = 0; i < native()->nb_streams; ++i)
|
for (std::size_t i = 0; i < native()->nb_streams; ++i)
|
||||||
{
|
{
|
||||||
if (native()->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC)
|
Stream stream(native()->streams[i]);
|
||||||
|
|
||||||
|
if (stream.hasAttachedPic())
|
||||||
{
|
{
|
||||||
|
Picture picture;
|
||||||
|
|
||||||
|
auto itMime = codecMimeMap.find(stream.getCodecContext().getCodecId());
|
||||||
|
if (itMime != codecMimeMap.end())
|
||||||
|
picture.mimeType = itMime->second;
|
||||||
|
else
|
||||||
|
picture.mimeType = "application/octet-stream";
|
||||||
|
|
||||||
|
LMS_LOG(MOD_AV, SEV_DEBUG) << "MIME set to '" << picture.mimeType << "'" << std::endl;
|
||||||
|
|
||||||
AVPacket pkt = native()->streams[i]->attached_pic;
|
AVPacket pkt = native()->streams[i]->attached_pic;
|
||||||
|
|
||||||
std::vector<unsigned char> data;
|
std::copy(pkt.data, pkt.data + pkt.size, std::back_inserter(picture.data));
|
||||||
std::copy(pkt.data, pkt.data + pkt.size, std::back_inserter(data));
|
|
||||||
|
|
||||||
pictures.push_back( data );
|
pictures.push_back( picture );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,11 @@
|
|||||||
namespace Av
|
namespace Av
|
||||||
{
|
{
|
||||||
|
|
||||||
|
struct Picture {
|
||||||
|
std::string mimeType;
|
||||||
|
std::vector<unsigned char> data;
|
||||||
|
};
|
||||||
|
|
||||||
class InputFormatContext : public FormatContext
|
class InputFormatContext : public FormatContext
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -47,7 +52,7 @@ class InputFormatContext : public FormatContext
|
|||||||
|
|
||||||
// Get attached pictures
|
// Get attached pictures
|
||||||
std::size_t getNbPictures(void) const;
|
std::size_t getNbPictures(void) const;
|
||||||
void getPictures(std::vector< std::vector<unsigned char> >& pictures) const;
|
void getPictures(std::vector<Picture>& pictures) const;
|
||||||
|
|
||||||
// Get the streams
|
// Get the streams
|
||||||
std::vector<Stream> getStreams(void);
|
std::vector<Stream> getStreams(void);
|
||||||
|
|||||||
@@ -37,11 +37,11 @@ Grabber::getFromInputFormatContext(const Av::InputFormatContext& input)
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::vector< std::vector<unsigned char> > pictures;
|
std::vector<Av::Picture> pictures;
|
||||||
input.getPictures(pictures);
|
input.getPictures(pictures);
|
||||||
|
|
||||||
BOOST_FOREACH(const std::vector<unsigned char>& picture, pictures)
|
BOOST_FOREACH(const Av::Picture& picture, pictures)
|
||||||
res.push_back( CoverArt("application/octet-stream", picture) );
|
res.push_back( CoverArt(picture.mimeType, picture.data) );
|
||||||
|
|
||||||
}
|
}
|
||||||
catch(std::exception& e)
|
catch(std::exception& e)
|
||||||
|
|||||||
+11
-41
@@ -34,15 +34,12 @@
|
|||||||
|
|
||||||
#include "PlayQueue.hpp"
|
#include "PlayQueue.hpp"
|
||||||
|
|
||||||
static const int CoverRole = Wt::UserRole + 1;
|
static const int NameRole = Wt::UserRole;
|
||||||
static const int NameRole = Wt::UserRole + 2;
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
void swapRows(Wt::WStandardItemModel *model, int row1, int row2)
|
void swapRows(Wt::WStandardItemModel *model, int row1, int row2)
|
||||||
{
|
{
|
||||||
LMS_LOG(MOD_UI, SEV_DEBUG) << "Swap called row1 = " << row1 << ", row2 = " << row2 << ", CoverRole = " << CoverRole;
|
|
||||||
|
|
||||||
// Swap data column by column
|
// Swap data column by column
|
||||||
for (int i = 0; i < model->columnCount(); ++i)
|
for (int i = 0; i < model->columnCount(); ++i)
|
||||||
{
|
{
|
||||||
@@ -55,15 +52,7 @@ namespace {
|
|||||||
model->setItemData(index1, model->itemData(index2)); // 2 -> 1
|
model->setItemData(index1, model->itemData(index2)); // 2 -> 1
|
||||||
model->setItemData(index2, tmp); // tmp-> 2
|
model->setItemData(index2, tmp); // tmp-> 2
|
||||||
}
|
}
|
||||||
|
// caution: swap data associated with our custom roles if any!!
|
||||||
// swap data associated with our custom roles
|
|
||||||
const std::vector<int>(roles) = {CoverRole, NameRole};
|
|
||||||
BOOST_FOREACH(int role, roles)
|
|
||||||
{
|
|
||||||
auto tmp = model->data(index1, role);
|
|
||||||
model->setData(index1, model->data(index2, role), role);
|
|
||||||
model->setData(index2, tmp, role);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -245,33 +234,7 @@ class PlayQueueItemDelegate : public Wt::WItemDelegate
|
|||||||
{
|
{
|
||||||
Wt::WWidget* res;
|
Wt::WWidget* res;
|
||||||
|
|
||||||
Wt::WString path = Wt::asString(index.data(CoverRole));
|
if (!index.data(NameRole).empty())
|
||||||
if (!path.empty())
|
|
||||||
{
|
|
||||||
// Create an image for this track
|
|
||||||
Wt::WImage* image = new Wt::WImage( );
|
|
||||||
Wt::WResource* resource;
|
|
||||||
if (path != "none")
|
|
||||||
resource = new CoverResource(boost::filesystem::path(path.toUTF8()), 64, image);
|
|
||||||
else
|
|
||||||
resource = new Wt::WFileResource("image/jpeg", Wt::WApplication::instance()->docRoot() + "/images/unknown-cover.jpg", image);
|
|
||||||
|
|
||||||
image->setImageLink(Wt::WLink(resource));
|
|
||||||
|
|
||||||
// Apply style if any
|
|
||||||
Wt::WString styleClass = Wt::asString(index.data(Wt::StyleClassRole));
|
|
||||||
|
|
||||||
// Apply selection style if any
|
|
||||||
if (flags & Wt::RenderSelected)
|
|
||||||
styleClass += " " + Wt::WApplication::instance()->theme()->activeClass();
|
|
||||||
|
|
||||||
image->setStyleClass(styleClass);
|
|
||||||
|
|
||||||
res = image;
|
|
||||||
res->setObjectName("z");
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (!index.data(NameRole).empty())
|
|
||||||
{
|
{
|
||||||
Name name = boost::any_cast<Name>(index.data(NameRole));
|
Name name = boost::any_cast<Name>(index.data(NameRole));
|
||||||
|
|
||||||
@@ -351,6 +314,7 @@ _trackSelector(new TrackSelector())
|
|||||||
|
|
||||||
}, std::placeholders::_1, std::placeholders::_2));
|
}, std::placeholders::_1, std::placeholders::_2));
|
||||||
|
|
||||||
|
_coverResource = new CoverResource(db, 64);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -409,7 +373,13 @@ PlayQueue::addTracks(const std::vector<Database::Track::id_type>& trackIds)
|
|||||||
_model->insertRows(dataRow, 1);
|
_model->insertRows(dataRow, 1);
|
||||||
|
|
||||||
_model->setData(dataRow, COLUMN_ID_TRACK_ID, track.id(), Wt::UserRole);
|
_model->setData(dataRow, COLUMN_ID_TRACK_ID, track.id(), Wt::UserRole);
|
||||||
_model->setData(dataRow, COLUMN_ID_COVER, track->hasCover() ? track->getPath() : "none", CoverRole);
|
|
||||||
|
std::string coverUrl;
|
||||||
|
if (track->hasCover())
|
||||||
|
coverUrl = _coverResource->url() + "&coverid=" + Wt::asString(track.id()).toUTF8();
|
||||||
|
else
|
||||||
|
coverUrl = "images/unknown-cover.jpg";
|
||||||
|
_model->setData(dataRow, COLUMN_ID_COVER, coverUrl, Wt::DecorationRole);
|
||||||
|
|
||||||
Name name;
|
Name name;
|
||||||
name.track = Wt::WString::fromUTF8(track->getName());
|
name.track = Wt::WString::fromUTF8(track->getName());
|
||||||
|
|||||||
@@ -26,6 +26,8 @@
|
|||||||
#include "database/DatabaseHandler.hpp"
|
#include "database/DatabaseHandler.hpp"
|
||||||
#include "database/AudioTypes.hpp"
|
#include "database/AudioTypes.hpp"
|
||||||
|
|
||||||
|
#include "resource/CoverResource.hpp"
|
||||||
|
|
||||||
namespace UserInterface {
|
namespace UserInterface {
|
||||||
|
|
||||||
class PlayQueueItemDelegate;
|
class PlayQueueItemDelegate;
|
||||||
@@ -78,6 +80,8 @@ class PlayQueue : public Wt::WTableView
|
|||||||
|
|
||||||
PlayQueueItemDelegate* _itemDelegate;
|
PlayQueueItemDelegate* _itemDelegate;
|
||||||
|
|
||||||
|
CoverResource* _coverResource;
|
||||||
|
|
||||||
int _curPlayedTrackPos;
|
int _curPlayedTrackPos;
|
||||||
std::unique_ptr<TrackSelector> _trackSelector;
|
std::unique_ptr<TrackSelector> _trackSelector;
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
#include <boost/foreach.hpp>
|
#include <boost/foreach.hpp>
|
||||||
|
|
||||||
|
#include <Wt/WApplication>
|
||||||
#include <Wt/Http/Response>
|
#include <Wt/Http/Response>
|
||||||
|
|
||||||
#include "logger/Logger.hpp"
|
#include "logger/Logger.hpp"
|
||||||
@@ -29,9 +30,9 @@
|
|||||||
|
|
||||||
namespace UserInterface {
|
namespace UserInterface {
|
||||||
|
|
||||||
CoverResource::CoverResource(const boost::filesystem::path& path, std::size_t size, Wt::WObject *parent)
|
CoverResource::CoverResource(Database::Handler& db, std::size_t size, Wt::WObject *parent)
|
||||||
: Wt::WResource(parent),
|
: Wt::WResource(parent),
|
||||||
_path(path),
|
_db(db),
|
||||||
_size(size)
|
_size(size)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -45,25 +46,50 @@ CoverResource:: ~CoverResource()
|
|||||||
void
|
void
|
||||||
CoverResource::handleRequest(const Wt::Http::Request& request, Wt::Http::Response& response)
|
CoverResource::handleRequest(const Wt::Http::Request& request, Wt::Http::Response& response)
|
||||||
{
|
{
|
||||||
if (_data.empty())
|
// Get the id of the track
|
||||||
|
const std::string *trackIdStr = request.getParameter("coverid");
|
||||||
|
|
||||||
|
if (trackIdStr)
|
||||||
{
|
{
|
||||||
std::vector<CoverArt::CoverArt> covers = CoverArt::Grabber::getFromTrack( _path );
|
Database::Track::id_type trackId;
|
||||||
|
{
|
||||||
|
std::istringstream iss(*trackIdStr); iss >> trackId;
|
||||||
|
}
|
||||||
|
|
||||||
|
Wt::Dbo::Transaction transaction(_db.getSession());
|
||||||
|
|
||||||
|
Database::Track::pointer track = Database::Track::getById(_db.getSession(), trackId);
|
||||||
|
std::vector<CoverArt::CoverArt> covers = CoverArt::Grabber::getFromTrack(track);
|
||||||
|
|
||||||
|
transaction.commit();
|
||||||
|
|
||||||
BOOST_FOREACH(CoverArt::CoverArt& cover, covers)
|
BOOST_FOREACH(CoverArt::CoverArt& cover, covers)
|
||||||
{
|
{
|
||||||
cover.scale(_size);
|
if (cover.scale(_size))
|
||||||
_data = cover.getData();
|
{
|
||||||
_mimeType = cover.getMimeType();
|
response.setMimeType( cover.getMimeType() );
|
||||||
break;
|
|
||||||
|
BOOST_FOREACH(unsigned char c, cover.getData())
|
||||||
|
response.out().put( c );
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
LMS_LOG(MOD_UI, SEV_DEBUG) << "Resize error for track id = " << trackId;
|
||||||
}
|
}
|
||||||
|
LMS_LOG(MOD_UI, SEV_DEBUG) << "no valid cover found for track id = " << trackId;
|
||||||
|
{
|
||||||
|
std::ifstream ist(Wt::WApplication::instance()->docRoot() + "/images/unknown-cover.jpg");
|
||||||
|
|
||||||
// TODO if not found fallback on an empty image
|
char c;
|
||||||
|
while(ist.get(c))
|
||||||
|
response.out().put( c );
|
||||||
|
|
||||||
|
response.setMimeType("image/jpeg");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
response.setMimeType(_mimeType);
|
LMS_LOG(MOD_UI, SEV_DEBUG) << "no cover id parameter";
|
||||||
|
|
||||||
for (unsigned int i = 0; i < _data.size(); ++i)
|
|
||||||
response.out().put(_data[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,11 +17,15 @@
|
|||||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifndef COVER_RESOURCE_HPP_
|
||||||
|
#define COVER_RESOURCE_HPP_
|
||||||
|
|
||||||
#include <boost/foreach.hpp>
|
#include <boost/foreach.hpp>
|
||||||
#include <boost/filesystem.hpp>
|
#include <boost/filesystem.hpp>
|
||||||
|
|
||||||
#include <Wt/WResource>
|
#include <Wt/WResource>
|
||||||
|
|
||||||
|
#include "database/DatabaseHandler.hpp"
|
||||||
#include "database/AudioTypes.hpp"
|
#include "database/AudioTypes.hpp"
|
||||||
|
|
||||||
namespace UserInterface {
|
namespace UserInterface {
|
||||||
@@ -29,8 +33,8 @@ namespace UserInterface {
|
|||||||
class CoverResource : public Wt::WResource
|
class CoverResource : public Wt::WResource
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CoverResource(const boost::filesystem::path& p, // track to get cover from
|
CoverResource(Database::Handler& db,
|
||||||
std::size_t size, // size * size pixels
|
std::size_t size, // size * size pixels
|
||||||
Wt::WObject *parent = 0);
|
Wt::WObject *parent = 0);
|
||||||
~CoverResource();
|
~CoverResource();
|
||||||
|
|
||||||
@@ -38,13 +42,10 @@ class CoverResource : public Wt::WResource
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
boost::filesystem::path _path;
|
Database::Handler& _db;
|
||||||
std::size_t _size;
|
std::size_t _size;
|
||||||
std::string _mimeType;
|
|
||||||
|
|
||||||
std::vector<unsigned char> _data;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace UserInterface
|
} // namespace UserInterface
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user