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. --- music.cpp | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'music.cpp') 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