From 329d8640f0c6d947d578586c03cb692b50df1d7b Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 11 Jan 2011 18:03:51 +0100 Subject: Moved EncoderLame's encode() and flush() into read(). --- encoders/lame_encoder.cpp | 14 +++----------- encoders/lame_encoder.h | 3 --- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/encoders/lame_encoder.cpp b/encoders/lame_encoder.cpp index 6616b87..f3816bf 100644 --- a/encoders/lame_encoder.cpp +++ b/encoders/lame_encoder.cpp @@ -15,24 +15,16 @@ EncoderLame::~EncoderLame() { lame_close(gfp); } -size_t EncoderLame::encode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) { - return lame_encode_buffer_interleaved(gfp, (short*)input, input_size / 4, output, output_size); -} - -size_t EncoderLame::flush(uint8_t *output, size_t output_size) { - return lame_encode_flush(gfp, output, output_size); -} - std::size_t EncoderLame::read(char* buf, std::size_t buf_size) { char src_data[0x30000]; std::streamsize src_read = source->read(src_data, 0x30000); if(src_read < 0) src_read = 0; - std::streamsize size = encode((const uint8_t*)src_data, src_read, (uint8_t*)buf, buf_size); + std::size_t size = lame_encode_buffer_interleaved(gfp, (short*)src_data, src_read / 4, (unsigned char*)buf, buf_size); // no more data, flush encoder if(src_read == 0 && size == 0) { - size = flush((uint8_t*)buf, buf_size); + size = lame_encode_flush(gfp, (unsigned char*)buf, buf_size); } return size; -} \ No newline at end of file +} diff --git a/encoders/lame_encoder.h b/encoders/lame_encoder.h index d150f60..79bc944 100644 --- a/encoders/lame_encoder.h +++ b/encoders/lame_encoder.h @@ -11,9 +11,6 @@ class EncoderLame : public Encoder { private: lame_global_flags *gfp; RawAudioSource::p source; - - size_t encode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size); - size_t flush(uint8_t *output, size_t output_size); public: EncoderLame(RawAudioSource::p source_); -- cgit v1.2.3 From dd8bc1d57d796ba628da34efb6b8f1a8a60c3dbb Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 4 Feb 2011 19:47:12 +0100 Subject: Moved music::update() to MusicDirectory::update(). --- music.cpp | 9 ++++++--- music.h | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/music.cpp b/music.cpp index ee9f2ba..b9cb05d 100644 --- a/music.cpp +++ b/music.cpp @@ -145,13 +145,13 @@ void music::begin_update(const std::string path) { MusicDirectory::p dir = get_directory(path); std::cout << boost::format("updater(%s) called") % path << std::endl; if(dir) { - update(dir->path); + dir->update(); } } /** Recursively update \p dir and its subdirectories. */ -void music::update(const MusicDirectory& dir) { +void MusicDirectory::update() { // TODO: Fix engine-specific SQL syntax inside this function. soci::session sql(config::vm["audist.database"].as()); @@ -196,7 +196,10 @@ void music::update(const MusicDirectory& dir) { } sql.close(); - std::for_each(dir.directories.begin(), dir.directories.end(), update); + for(PathListings::iterator it = directories.begin(); it != directories.end(); it++) { + MusicDirectory dir(*it); + dir.update(); + } } void MusicDirectory::render(HTTP::Connection::p req) { diff --git a/music.h b/music.h index f7d06bd..db69f5a 100644 --- a/music.h +++ b/music.h @@ -35,6 +35,7 @@ class MusicDirectory : public MusicListing { MusicDirectory(const fs::path root); virtual void render(HTTP::Connection::p req); + void update(); }; namespace music { @@ -46,7 +47,6 @@ namespace music { std::vector find(const std::map search); std::vector find(std::string search); void begin_update(const std::string path); - void update(const MusicDirectory& dir); }; #endif -- cgit v1.2.3 From 67a0e925ff1efc839ecb067d605bb45ebebf866a Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 4 Feb 2011 20:45:04 +0100 Subject: Moved all SQL code to database.cpp. --- database.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ database.h | 27 +++++++++++++++ music.cpp | 107 +++++++-------------------------------------------------- music.h | 7 ++-- 4 files changed, 155 insertions(+), 96 deletions(-) create mode 100644 database.cpp create mode 100644 database.h diff --git a/database.cpp b/database.cpp new file mode 100644 index 0000000..ea87e17 --- /dev/null +++ b/database.cpp @@ -0,0 +1,110 @@ +#include "database.h" +#include "config.h" + +#include +#include + +Database::Database() { + sql.open(config::vm["audist.database"].as()); +} + +Database::~Database() { + sql.close(); +} + +// NOTE: LIKE is case-insensitive by default on sqlite +std::vector Database::find(std::map search) { + soci::session sql(config::vm["audist.database"].as()); + soci::statement st(sql); + std::string filename, artist, album, title; + st.exchange(soci::into(filename)); + std::string query = "SELECT file_name FROM tracks WHERE "; + std::vector where_conditions; + + if(search.find("artist") != search.end()) { + where_conditions.push_back("artist_id IN (SELECT id FROM artists WHERE name LIKE :artist)"); + artist = "%"+search["artist"]+"%"; + st.exchange(soci::use(artist, "artist")); + } + + if(search.find("album") != search.end()) { + where_conditions.push_back("album_id IN (SELECT id FROM albums WHERE name LIKE :album)"); + album = "%"+search["album"]+"%"; + st.exchange(soci::use(album, "album")); + } + + if(search.find("title") != search.end()) { + where_conditions.push_back("name LIKE :title"); + artist = "%"+search["title"]+"%"; + st.exchange(soci::use(title, "title")); + } + + query += boost::algorithm::join(where_conditions, " AND "); + st.alloc(); + st.prepare(query); + st.define_and_bind(); + st.execute(true); + + std::vector results; + while(st.fetch()) { + MusicListing::p ml(new MusicTrack(filename)); + results.push_back(ml); + } + sql.close(); + + return results; +} + +std::vector Database::find(std::string search) { + soci::session sql(config::vm["audist.database"].as()); + + search = "%"+search+"%"; + soci::rowset rs = (sql.prepare << "SELECT DISTINCT file_name FROM tracks WHERE name LIKE :search OR file_name LIKE :search OR " + "artist_id IN (SELECT id FROM artists WHERE name LIKE :search) OR album_id IN (SELECT id FROM albums WHERE name LIKE :search)", + soci::use(search, "search")); + + std::vector results; + for(soci::rowset::const_iterator it = rs.begin(); it != rs.end(); it++) { + MusicListing::p ml(new MusicTrack(*it)); + results.push_back(ml); + } + + return results; +} + +// NOTE: last_insert_rowid() is sqlite-specific +void Database::update(fs::path track, Tag::p tag) { + int64_t artist_id = 0, album_id = 0, track_id = 0; + + if(tag->has_field("artist")) { + sql << "SELECT id FROM artists WHERE name = :name", soci::use(tag->fields["artist"]), soci::into(artist_id); + if(!sql.got_data()) { + sql << "INSERT INTO artists (name) VALUES (:name)", soci::use(tag->fields["artist"]); + sql << "SELECT last_insert_rowid()", soci::into(artist_id); + } + } + + if(tag->has_field("album")) { + std::string query(boost::str(boost::format("SELECT id FROM albums WHERE %s AND name = :name") % + (artist_id ? boost::str(boost::format("artist_id = %d") % artist_id) : "artist_id IS NULL"))); + sql << query, soci::use(tag->fields["album"]), soci::into(album_id); + if(!sql.got_data()) { + soci::indicator ind = (artist_id ? soci::i_ok : soci::i_null); + sql << "INSERT INTO albums (artist_id, name) VALUES (:artist_id, :name)", soci::use(artist_id, ind), soci::use(tag->fields["album"]); + sql << "SELECT last_insert_rowid()", soci::into(album_id); + } + } + + if(tag->has_field("title")) { + std::string query(boost::str(boost::format("SELECT id FROM tracks WHERE %s AND %s AND name = :name") % + (artist_id ? boost::str(boost::format("artist_id = %d") % artist_id) : "artist_id IS NULL") % + (album_id ? boost::str(boost::format("album_id = %d") % album_id) : "album_id IS NULL"))); + sql << query, soci::use(tag->fields["title"]), soci::into(track_id); + if(!sql.got_data()) { + soci::indicator artist_ind = (artist_id ? soci::i_ok : soci::i_null), + album_ind = (album_id ? soci::i_ok : soci::i_null); + sql << "INSERT INTO tracks (artist_id, album_id, name, file_name) VALUES (:artist_id, :album_id, :name, :file_name)", + soci::use(artist_id, artist_ind), soci::use(album_id, album_ind), soci::use(tag->fields["title"]), soci::use(track.string()); + } + } +} diff --git a/database.h b/database.h new file mode 100644 index 0000000..a6ec5ad --- /dev/null +++ b/database.h @@ -0,0 +1,27 @@ +#ifndef DATABASE_H +#define DATABASE_H + +#include "music.h" +#include "tag.h" + +#include +#include + +#include +#include +#include + +class Database { + private: + soci::session sql; + + public: + Database(); + virtual ~Database(); + + std::vector find(std::map search); + std::vector find(std::string search); + void update(fs::path track, Tag::p tag); +}; + +#endif diff --git a/music.cpp b/music.cpp index b9cb05d..de5575f 100644 --- a/music.cpp +++ b/music.cpp @@ -3,6 +3,7 @@ #include "encoder.h" #include "tag.h" #include "config.h" +#include "database.h" #include #include @@ -77,65 +78,16 @@ MusicDirectory::p music::get_directory(const std::string& path) { * Does a search on specific fields given by \p search. */ std::vector music::find(std::map search) { - soci::session sql(config::vm["audist.database"].as()); - soci::statement st(sql); - std::string filename, artist, album, title; - st.exchange(soci::into(filename)); - std::string query = "SELECT file_name FROM tracks WHERE "; - std::vector where_conditions; - - if(search.find("artist") != search.end()) { - where_conditions.push_back("artist_id IN (SELECT id FROM artists WHERE name LIKE :artist)"); - artist = "%"+search["artist"]+"%"; - st.exchange(soci::use(artist, "artist")); - } - - if(search.find("album") != search.end()) { - where_conditions.push_back("album_id IN (SELECT id FROM albums WHERE name LIKE :album)"); - album = "%"+search["album"]+"%"; - st.exchange(soci::use(album, "album")); - } - - if(search.find("title") != search.end()) { - where_conditions.push_back("name LIKE :title"); - artist = "%"+search["title"]+"%"; - st.exchange(soci::use(title, "title")); - } - - query += boost::algorithm::join(where_conditions, " AND "); - st.alloc(); - st.prepare(query); - st.define_and_bind(); - st.execute(true); - - std::vector results; - while(st.fetch()) { - MusicListing::p ml(new MusicTrack(filename)); - results.push_back(ml); - } - sql.close(); - - return results; + Database db; + return db.find(search); } /** Find tracks in the database. * Returns tracks where title, artist, album or filename matches \p search. */ std::vector music::find(std::string search) { - soci::session sql(config::vm["audist.database"].as()); - - search = "%"+search+"%"; - soci::rowset rs = (sql.prepare << "SELECT DISTINCT file_name FROM tracks WHERE name LIKE :search OR file_name LIKE :search OR " - "artist_id IN (SELECT id FROM artists WHERE name LIKE :search) OR album_id IN (SELECT id FROM albums WHERE name LIKE :search)", - soci::use(search, "search")); - - std::vector results; - for(soci::rowset::const_iterator it = rs.begin(); it != rs.end(); it++) { - MusicListing::p ml(new MusicTrack(*it)); - results.push_back(ml); - } - - return results; + Database db; + return db.find(search); } /** Initiate an update on \p path and its subdirs. @@ -145,60 +97,27 @@ void music::begin_update(const std::string path) { MusicDirectory::p dir = get_directory(path); std::cout << boost::format("updater(%s) called") % path << std::endl; if(dir) { - dir->update(); + Database db; + dir->update(db); } } /** Recursively update \p dir and its subdirectories. */ -void MusicDirectory::update() { - // TODO: Fix engine-specific SQL syntax inside this function. - soci::session sql(config::vm["audist.database"].as()); - - BOOST_FOREACH(fs::path t, dir.tracks) { - std::cout << "track " << t << std::endl; - Tag::p tag = Tag::load(t.string()); +void MusicDirectory::update(Database& db) { + BOOST_FOREACH(fs::path track, tracks) { + std::cout << "track " << track << std::endl; + Tag::p tag = Tag::load(track.string()); BOOST_FOREACH(Tag::Fields::value_type& f, tag->fields) { std::cout << boost::format(" %s: %s") % f.first % f.second << std::endl; } - int artist_id = 0, album_id = 0, track_id = 0; - - if(tag->has_field("artist")) { - sql << "SELECT id FROM artists WHERE name = :name", soci::use(tag->fields["artist"]), soci::into(artist_id); - if(!sql.got_data()) - sql << "INSERT INTO artists (name) VALUES (:name) RETURNING id", soci::use(tag->fields["artist"]), soci::into(artist_id); - } - - if(tag->has_field("album")) { - std::string query(boost::str(boost::format("SELECT id FROM albums WHERE %s AND name = :name") % - (artist_id ? boost::str(boost::format("artist_id = %d") % artist_id) : "artist_id IS NULL"))); - sql << query, soci::use(tag->fields["album"]), soci::into(album_id); - if(!sql.got_data()) { - soci::indicator ind = (artist_id ? soci::i_ok : soci::i_null); - sql << "INSERT INTO albums (artist_id, name) VALUES (:artist_id, :name) RETURNING id", - soci::use(artist_id, ind), soci::use(tag->fields["album"]), soci::into(album_id); - } - } - - if(tag->has_field("title")) { - std::string query(boost::str(boost::format("SELECT id FROM tracks WHERE %s AND %s AND name = :name") % - (artist_id ? boost::str(boost::format("artist_id = %d") % artist_id) : "artist_id IS NULL") % - (album_id ? boost::str(boost::format("album_id = %d") % album_id) : "album_id IS NULL"))); - sql << query, soci::use(tag->fields["title"]), soci::into(track_id); - if(!sql.got_data()) { - soci::indicator artist_ind = (artist_id ? soci::i_ok : soci::i_null), - album_ind = (album_id ? soci::i_ok : soci::i_null); - sql << "INSERT INTO tracks (artist_id, album_id, name, file_name) VALUES (:artist_id, :album_id, :name, :file_name)", - soci::use(artist_id, artist_ind), soci::use(album_id, album_ind), soci::use(tag->fields["title"]), soci::use(t.string()); - } - } + db.update(track, tag); } - sql.close(); for(PathListings::iterator it = directories.begin(); it != directories.end(); it++) { MusicDirectory dir(*it); - dir.update(); + dir.update(db); } } diff --git a/music.h b/music.h index db69f5a..17f99f2 100644 --- a/music.h +++ b/music.h @@ -25,6 +25,9 @@ class MusicTrack : public MusicListing { virtual void render(HTTP::Connection::p req); }; +// Forward declaration for MusicDirectory::update() +class Database; + //! Represents a directory. class MusicDirectory : public MusicListing { public: @@ -35,7 +38,7 @@ class MusicDirectory : public MusicListing { MusicDirectory(const fs::path root); virtual void render(HTTP::Connection::p req); - void update(); + void update(Database& db); }; namespace music { @@ -44,7 +47,7 @@ namespace music { MusicListing::p get(const HTTP::Connection::PathList& path); MusicListing::p get(const std::string& path); MusicDirectory::p get_directory(const std::string& path); - std::vector find(const std::map search); + std::vector find(std::map search); std::vector find(std::string search); void begin_update(const std::string path); }; -- cgit v1.2.3 From 6e7b8f94bf7fdc087cd1eed604eabed6070dffad Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 2 Mar 2011 19:22:33 +0100 Subject: Implemented simple caching for transcoded audio data. --- .gitignore | 3 +++ SConstruct | 1 + cache.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ cache.h | 26 ++++++++++++++++++++++++++ config.cpp | 1 + music.cpp | 30 +++++++++++++----------------- 6 files changed, 91 insertions(+), 17 deletions(-) create mode 100644 cache.cpp create mode 100644 cache.h diff --git a/.gitignore b/.gitignore index 85183e8..fc370ea 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ .sconsign.dblite .sconf_temp config.log +/audist.db +/static +/cache diff --git a/SConstruct b/SConstruct index 6b960cf..65311ab 100644 --- a/SConstruct +++ b/SConstruct @@ -24,6 +24,7 @@ else: env.ParseConfig('pkg-config --cflags --libs libmpg123') env.ParseConfig('pkg-config --cflags --libs id3tag') env.ParseConfig('pkg-config --cflags --libs vorbisenc') +env.ParseConfig('pkg-config --cflags --libs libssl') env.Program('audistd', Glob('*.cpp') + Glob('decoders/*.cpp') + Glob('encoders/*.cpp')) diff --git a/cache.cpp b/cache.cpp new file mode 100644 index 0000000..92e0cfa --- /dev/null +++ b/cache.cpp @@ -0,0 +1,47 @@ +#include "cache.h" +#include "config.h" + +#include +#include +#include + +#include +#include + +EncodedCache::EncodedCache(fs::path path, Decoder::p decoder, Encoder::p encoder) { + this->decoder = decoder; + this->encoder = encoder; + + // TODO: Differentiate between encoders? + SHA_CTX context; + unsigned char md[SHA_DIGEST_LENGTH]; + SHA_Init(&context); + SHA_Update(&context, (unsigned char*)path.string().c_str(), path.string().length()); + SHA_Final(md, &context); + for(int i = 0; i < SHA_DIGEST_LENGTH; i++) { + hash = hash + (boost::format("%02x") % (int)md[i]).str(); + } +} + +fs::path EncodedCache::get_path() { + return fs::path(config::vm["audist.cache_dir"].as()) / hash; +} + +void EncodedCache::create_cache() { + fs::path path = get_path(); + + // TODO: Locking? + fs::ofstream os(path, std::ios::out | std::ios::binary); + // TODO: Make an encoder-to-istream adapter to get rid of this: + + if(!os.is_open()) + throw std::runtime_error("failed to create cache object"); + char data[0x10000]; + std::streamsize size = 1; + while(size) { + size = encoder->read(data, 0x10000); + if(size > 0) + os.write(data, size); + } + os.close(); +} diff --git a/cache.h b/cache.h new file mode 100644 index 0000000..21d4a52 --- /dev/null +++ b/cache.h @@ -0,0 +1,26 @@ +#ifndef CACHE_H +#define CACHE_H + +#include "decoder.h" +#include "encoder.h" + +#include + +namespace fs = boost::filesystem; + +class EncodedCache { + private: + Decoder::p decoder; + Encoder::p encoder; + + std::string hash; + + public: + EncodedCache(fs::path path, Decoder::p decoder, Encoder::p encoder); + virtual ~EncodedCache() {}; + + fs::path get_path(); + void create_cache(); +}; + +#endif diff --git a/config.cpp b/config.cpp index 68e8a2f..910d0f0 100644 --- a/config.cpp +++ b/config.cpp @@ -14,6 +14,7 @@ void config::init() { ("audist.telnetd_port", po::value()->default_value(7681), "telnetd port") ("audist.threads", po::value()->default_value(10), "threads") ("audist.database", po::value(), "database string") + ("audist.cache_dir", po::value(), "cache dir") ; std::ifstream is("audist.conf", std::ios::in); po::store(po::parse_config_file(is, desc, true), vm); diff --git a/music.cpp b/music.cpp index de5575f..51c44a8 100644 --- a/music.cpp +++ b/music.cpp @@ -2,8 +2,8 @@ #include "decoder.h" #include "encoder.h" #include "tag.h" -#include "config.h" #include "database.h" +#include "cache.h" #include #include @@ -170,26 +170,22 @@ MusicDirectory::MusicDirectory(const fs::path root) { void MusicTrack::render(HTTP::Connection::p req) { req->add_header("content-type", "application/octet-stream"); + fs::path file_path = path; if(req->args.count("decoder") && req->args.count("encoder")) { Decoder::p decoder = Decoder::get(req->args["decoder"], path.string()); Encoder::p encoder = Encoder::get(req->args["encoder"], decoder); - - // TODO: Make an encoder-to-istream adapter to get rid of this: - char data[0x10000]; - std::streamsize size = 1; - while(size) { - size = encoder->read(data, 0x10000); - if(size > 0) - req->send_data(data, size); - } - - } else { - fs::ifstream is(path, std::ios::in | std::ios::binary); - is.seekg(0, std::ios::end); - req->add_header("content-length", boost::str(boost::format("%d") % is.tellg())); - is.seekg(0, std::ios::beg); - req->send_data(is); + EncodedCache ec(path, decoder, encoder); + file_path = ec.get_path(); + if(!fs::exists(file_path)) + ec.create_cache(); } + + fs::ifstream is(file_path, std::ios::in | std::ios::binary); + is.seekg(0, std::ios::end); + req->add_header("content-length", boost::str(boost::format("%d") % is.tellg())); + is.seekg(0, std::ios::beg); + + req->send_data(is); } -- cgit v1.2.3