Reworked the project layout

This commit is contained in:
emeric
2014-10-10 20:41:34 +02:00
parent 92a176c899
commit 49ff050539
146 changed files with 156 additions and 162 deletions
+171
View File
@@ -0,0 +1,171 @@
/*
* 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/>.
*/
#include "AvFormat.hpp"
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "av/InputFormatContext.hpp"
#include "logger/Logger.hpp"
#include "Utils.hpp"
namespace MetaData
{
void
AvFormat::parse(const boost::filesystem::path& p, Items& items)
{
try {
Av::InputFormatContext input(p);
input.findStreamInfo(); // needed by input.getDurationSecs
std::map<std::string, std::string> metadata;
input.getMetadata().get(metadata);
// HACK or OGG files
// If we did not find tags, searched metadata in streams
if (metadata.empty())
{
// Get input streams
std::vector<Av::Stream> streams = input.getStreams();
BOOST_FOREACH(Av::Stream& stream, streams)
{
stream.getMetadata().get(metadata);
if (!metadata.empty())
break;
}
}
// Stream info
{
std::vector<Av::Stream> avStreams = input.getStreams();
std::vector<AudioStream> audioStreams;
std::vector<VideoStream> videoStreams;
std::vector<SubtitleStream> subtitleStreams;
BOOST_FOREACH(Av::Stream& avStream, avStreams)
{
switch(avStream.getCodecContext().getType())
{
case AVMEDIA_TYPE_VIDEO:
if (!avStream.hasAttachedPic())
{
VideoStream stream;
stream.bitRate = avStream.getCodecContext().getBitRate();
videoStreams.push_back(stream);
}
break;
case AVMEDIA_TYPE_AUDIO:
{
AudioStream stream;
stream.nbChannels = avStream.getCodecContext().getNbChannels();
stream.bitRate = avStream.getCodecContext().getBitRate();
audioStreams.push_back(stream);
}
break;
case AVMEDIA_TYPE_SUBTITLE:
{
subtitleStreams.push_back( SubtitleStream() );
}
break;
default:
break;
}
}
if (!videoStreams.empty())
items.insert( std::make_pair(MetaData::VideoStreams, videoStreams));
if (!audioStreams.empty())
items.insert( std::make_pair(MetaData::AudioStreams, audioStreams));
if (!subtitleStreams.empty())
items.insert( std::make_pair(MetaData::SubtitleStreams, SubtitleStreams));
}
// Duration
items.insert( std::make_pair(MetaData::Duration, boost::posix_time::time_duration( boost::posix_time::seconds( input.getDurationSecs() )) ));
// Embedded MetaData
// Make sure to convert strings into UTF-8
std::map<std::string, std::string>::const_iterator it;
for (it = metadata.begin(); it != metadata.end(); ++it)
{
if (boost::iequals(it->first, "artist"))
items.insert( std::make_pair(MetaData::Artist, string_trim( string_to_utf8(it->second)) ));
else if (boost::iequals(it->first, "album"))
items.insert( std::make_pair(MetaData::Album, string_trim( string_to_utf8(it->second)) ));
else if (boost::iequals(it->first, "title"))
items.insert( std::make_pair(MetaData::Title, string_trim( string_to_utf8(it->second)) ));
else if (boost::iequals(it->first, "track")) {
std::size_t number;
if (readAs<std::size_t>(it->second, number))
items.insert( std::make_pair(MetaData::TrackNumber, number ));
}
else if (boost::iequals(it->first, "disc"))
{
std::size_t number;
if (readAs<std::size_t>(it->second, number))
items.insert( std::make_pair(MetaData::DiscNumber, number ));
}
else if (boost::iequals(it->first, "date")
|| boost::iequals(it->first, "year")
|| boost::iequals(it->first, "WM/Year")
|| boost::iequals(it->first, "TDOR") // Original date fallback
|| boost::iequals(it->first, "TORY") // Original date fallback
)
{
boost::posix_time::ptime p;
if (readAsPosixTime(it->second, p))
items.insert( std::make_pair(MetaData::CreationTime, p));
}
else if (boost::iequals(it->first, "genre"))
{
std::list<std::string> genres;
if (readList(it->second, ";,", genres))
items.insert( std::make_pair(MetaData::Genres, genres));
}
/* else
LMS_LOG(MOD_METADATA, SEV_DEBUG) << "key = " << it->first << ", value = " << it->second;
*/
}
}
catch(std::exception &e)
{
LMS_LOG(MOD_METADATA, SEV_ERROR) << "Parsing of '" << p << "' failed!";
}
}
} // namespace MetaData
+42
View File
@@ -0,0 +1,42 @@
/*
* 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/>.
*/
#ifndef METADATA_AVFORMAT_HPP
#define METADATA_AVFORMAT_HPP
#include "MetaData.hpp"
namespace MetaData
{
// Implements AVFORMAT library
class AvFormat : public Parser
{
public:
void parse(const boost::filesystem::path& p, Items& items);
private:
};
} // namespace MetaData
#endif
+226
View File
@@ -0,0 +1,226 @@
/*
* 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/>.
*/
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/foreach.hpp>
#include "Utils.hpp"
#include "Extractor.hpp"
namespace MetaData
{
extern "C" {
/**
* Type of a function that libextractor calls for each
* meta data item found.
*
* @param cls closure (user-defined)
* @param plugin_name name of the plugin that produced this value;
* special values can be used (i.e. '&lt;zlib&gt;' for zlib being
* used in the main libextractor library and yielding
* meta data).
* @param type libextractor-type describing the meta data
* @param format basic format information about data
* @param data_mime_type mime-type of data (not of the original file);
* can be NULL (if mime-type is not known)
* @param data actual meta-data found
* @param data_len number of bytes in data
* @return 0 to continue extracting, 1 to abort
*/
int processMetaData(void *cls,
const char *plugin_name,
enum EXTRACTOR_MetaType type,
enum EXTRACTOR_MetaFormat format,
const char *data_mime_type,
const char *data,
size_t data_len)
{
Items& items = *(reinterpret_cast<Items*>(cls));
switch (type) {
case EXTRACTOR_METATYPE_ARTIST:
assert( std::string(data_mime_type) == "text/plain" );
items.insert( std::make_pair(MetaData::Artist, std::string( data, data_len ? data_len - 1 : 0)) );
break;
case EXTRACTOR_METATYPE_TITLE:
assert( std::string(data_mime_type) == "text/plain" );
items.insert( std::make_pair(MetaData::Title, std::string( data, data_len ? data_len - 1 : 0)) );
break;
case EXTRACTOR_METATYPE_ALBUM:
assert( std::string(data_mime_type) == "text/plain" );
items.insert( std::make_pair(MetaData::Album, std::string( data, data_len ? data_len - 1 : 0)) );
break;
case EXTRACTOR_METATYPE_GENRE:
assert( std::string(data_mime_type) == "text/plain" );
{
std::list<std::string> genres;
if (readList(std::string( data, data_len ? data_len - 1 : 0), ";-:/,", genres))
items.insert( std::make_pair(MetaData::Genre, genres));
}
break;
/* case EXTRACTOR_METATYPE_PICTURE:
std::cout << "picture spotted" << std::endl;
break;*/
case EXTRACTOR_METATYPE_COVER_PICTURE:
{
GenericData parsedData;
const unsigned char* dataStart = reinterpret_cast<const unsigned char*>(data);
parsedData.mimeType = std::string(data_mime_type);
parsedData.data = std::vector<unsigned char>( &dataStart[0], &dataStart[data_len]);
items.insert( std::make_pair(MetaData::Cover, parsedData));
}
break;
/* case EXTRACTOR_METATYPE_EVENT_PICTURE:
std::cout << "Event picture spotted" << std::endl;
break;*/
/* case EXTRACTOR_METATYPE_CONTRIBUTOR_PICTURE:
std::cout << "Contrib picture spotted" << std::endl;
break;*/
/* case EXTRACTOR_METATYPE_SONG_COUNT:
// How many songs in the album
break;*/
/* case EXTRACTOR_METATYPE_AUDIO_CODEC:
assert( std::string(data_mime_type) == "text/plain" );
std::cout << "Aduio codec: " << std::string( data, data_len ? data_len - 1 : 0) << std::endl;
break;*/
case EXTRACTOR_METATYPE_PUBLICATION_YEAR:
assert( std::string(data_mime_type) == "text/plain" );
std::cout << "publication year = " << std::string( data, data_len ? data_len - 1 : 0) << std::endl;
break;
case EXTRACTOR_METATYPE_PUBLICATION_DATE:
assert( std::string(data_mime_type) == "text/plain" );
std::cout << "publication date = " << std::string( data, data_len ? data_len - 1 : 0) << std::endl;
break;
case EXTRACTOR_METATYPE_ORIGINAL_RELEASE_YEAR:
assert( std::string(data_mime_type) == "text/plain" );
std::cout << "original release year = " << std::string( data, data_len ? data_len - 1 : 0) << std::endl;
break;
case EXTRACTOR_METATYPE_CREATION_TIME:
assert( std::string(data_mime_type) == "text/plain" );
{
boost::posix_time::ptime p;
if (readAs<boost::posix_time::ptime>(std::string( data, data_len ? data_len - 1 : 0), p))
items.insert( std::make_pair(MetaData::CreationTime, p));
else if (readAsPosixTime(std::string( data, data_len ? data_len - 1 : 0), p))
items.insert( std::make_pair(MetaData::CreationTime, p));
}
break;
case EXTRACTOR_METATYPE_DURATION:
assert( std::string(data_mime_type) == "text/plain" );
{
boost::posix_time::time_duration duration;
if (readAs<boost::posix_time::time_duration>( std::string( data, data_len ? data_len - 1 : 0), duration))
items.insert( std::make_pair(MetaData::Duration, duration) );
}
break;
case EXTRACTOR_METATYPE_TRACK_NUMBER:
assert( std::string(data_mime_type) == "text/plain" );
{
std::size_t number(0);
if (readAs<size_t>( std::string( data, data_len ? data_len - 1 : 0), number))
items.insert( std::make_pair(MetaData::TrackNumber, number) );
}
break;
case EXTRACTOR_METATYPE_DISC_NUMBER:
assert( std::string(data_mime_type) == "text/plain" );
{
std::size_t number(0);
if (readAs<size_t>( std::string( data, data_len ? data_len - 1 : 0), number))
items.insert( std::make_pair(MetaData::DiscNumber, number) );
}
break;
default:
/* if (std::string(data_mime_type) == "text/plain")
std::cout << "TYPE = " << type << ", data = '" << std::string(data, data_len ? data_len - 1 : 0) << "'" << std::endl;
else
std::cout << "TYPE = " << type << ", data_mime_type = '" << data_mime_type << "', data len = " << data_len << std::endl;
*/
break;
}
return 0;
}
};
Extractor::Extractor()
: _plugins( nullptr )
{
// _plugins = EXTRACTOR_plugin_add_config (nullptr, "mp3:ogg:flac:wav:gstreamer", EXTRACTOR_OPTION_DEFAULT_POLICY);
}
Extractor::~Extractor()
{
}
void
Extractor::parse(const boost::filesystem::path& p, Items& items)
{
if (boost::filesystem::is_regular(p)) {
_plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
if (_plugins == nullptr) {
throw std::runtime_error("EXTRACTOR_plugin_add_config failed!");
}
EXTRACTOR_extract (_plugins, p.string().c_str(), NULL, 0, &processMetaData, &items);
EXTRACTOR_plugin_remove_all (_plugins);
}
}
bool
Extractor::parseCover(const boost::filesystem::path& p, GenericData& data)
{
bool res = false;
Items items;
if (boost::filesystem::is_regular(p)) {
_plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
if (_plugins == nullptr) {
throw std::runtime_error("EXTRACTOR_plugin_add_config failed!");
}
EXTRACTOR_extract (_plugins, p.string().c_str(), NULL, 0, &processMetaData, &items);
EXTRACTOR_plugin_remove_all (_plugins);
if (items.find(MetaData::Cover) != items.end()) {
data = boost::any_cast<GenericData>(items[MetaData::Cover]);
res = true;
}
}
return res;
}
} // namespace MetaData
+51
View File
@@ -0,0 +1,51 @@
/*
* 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/>.
*/
#ifndef EXTRACTOR_HPP
#define EXTRACTOR_HPP
#include <extractor.h>
#include "MetaData.hpp"
namespace MetaData
{
// Implements GNU libextractor library
class Extractor : public Parser
{
public:
Extractor();
~Extractor();
void parse(const boost::filesystem::path& p, Items& items);
bool parseCover(const boost::filesystem::path& p, GenericData& data);
private:
struct EXTRACTOR_PluginList *_plugins;
};
} // namespace MetaData
#endif
+84
View File
@@ -0,0 +1,84 @@
/*
* 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/>.
*/
#ifndef METADATA_HPP
#define METADATA_HPP
#include <map>
#include <boost/any.hpp>
#include <boost/filesystem.hpp>
namespace MetaData
{
enum Type
{
Artist, // string
Title, // string
Album, // string
Genres, // list<string>
Duration, // boost::posix_time::time_duration
TrackNumber, // size_t
DiscNumber, // size_t
CreationTime, // boost::posix_time::ptime
Cover, // GenericData
AudioStreams, // vector<AudioStream>
VideoStreams, // vector<VideoStream>
SubtitleStreams, // vector<SubtitleStream>
};
// Used by Cover
struct GenericData {
std::string mimeType;
std::vector<unsigned char> data;
};
// Used by Streams
struct AudioStream {
std::size_t nbChannels;
std::size_t bitRate;
};
struct VideoStream {
std::size_t bitRate;
};
struct SubtitleStream {
;
};
// Type and associated data
// See enum Type's comments
typedef std::map<Type, boost::any> Items;
class Parser
{
public:
typedef std::shared_ptr<Parser> pointer;
virtual void parse(const boost::filesystem::path& p, Items& items) = 0;
};
} // namespace MetaData
#endif
+83
View File
@@ -0,0 +1,83 @@
/*
* 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/>.
*/
#include <string>
#include <sstream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/foreach.hpp>
#include "Utils.hpp"
namespace MetaData
{
bool readAsPosixTime(const std::string& str, boost::posix_time::ptime& time)
{
const std::locale formats[] = {
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y-%m-%d")),
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y-%b-%d")),
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y-%B-%d")),
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y/%m/%d")),
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%d.%m.%Y")),
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y-%m")),
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y/%m")),
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y.%m")),
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y")),
};
for(size_t i=0; i < sizeof(formats)/sizeof(formats[0]); ++i)
{
std::istringstream iss(str);
iss.imbue(formats[i]);
if (iss >> time)
return true;
}
return false;
}
bool readList(const std::string& str, const std::string& separators, std::list<std::string>& results)
{
std::string curStr;
BOOST_FOREACH(char c, str) {
if (separators.find(c) != std::string::npos) {
if (!curStr.empty()) {
results.push_back(string_to_utf8(curStr));
curStr.clear();
}
}
else {
if (curStr.empty() && std::isspace(c))
continue;
curStr.push_back(c);
}
}
if (!curStr.empty())
results.push_back(string_to_utf8(curStr));
return !str.empty();
}
}
+68
View File
@@ -0,0 +1,68 @@
/*
* 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/>.
*/
#ifndef METADATA_UTILS_HPP
#define METADATA_UTILS_HPP
#include <string>
#include <list>
#include <boost/locale.hpp>
namespace MetaData
{
bool readAsPosixTime(const std::string& str, boost::posix_time::ptime& time);
bool readList(const std::string& str, const std::string& separators, std::list<std::string>& results);
template<typename T>
static inline bool readAs(const std::string& str, T& data)
{
std::istringstream iss ( str );
return iss >> data;
}
std::string
static inline string_trim(const std::string& str,
const std::string& whitespace = " \t")
{
const auto strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return ""; // no content
const auto strEnd = str.find_last_not_of(whitespace);
const auto strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}
std::string
static inline string_to_utf8(const std::string& str)
{
return boost::locale::conv::to_utf<char>(str, "UTF-8");
}
} // namespace MetaData
#endif