From 83deeeafe2e81b42ff485e4582d4c1ac00872f50 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 28 Dec 2010 21:08:58 +0100 Subject: Added a simple interface to transcoding by specifying the 'decoder' and 'encoder' query arguments. --- httpd.cpp | 2 +- music.cpp | 29 ++++++++++++++++++++++------- music.h | 8 ++++---- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/httpd.cpp b/httpd.cpp index 92eb9e8..203f4e0 100644 --- a/httpd.cpp +++ b/httpd.cpp @@ -27,7 +27,7 @@ void HTTPConnection::handle_read(const boost::system::error_code& error, size_t res.code = 200; res.status = "OK"; - ml->render(res); + ml->render(req, res); } else { res.code = 404; res.status = "Not Found"; diff --git a/music.cpp b/music.cpp index b36ba7b..8321b38 100644 --- a/music.cpp +++ b/music.cpp @@ -1,4 +1,7 @@ #include "music.h" +#include "decoder.h" +#include "encoder.h" +#include "transcode.h" #include #include @@ -8,7 +11,10 @@ namespace music { MusicDirectory *root_directory = NULL; -void init(const fs::path root) { +void init(std::string root) { + // remove trailing slash + if(boost::algorithm::ends_with(root, "/")) + root = root.substr(0, root.size()-1); root_directory = new MusicDirectory(root); } @@ -41,7 +47,7 @@ MusicListing *find(std::string path) { }; // namespace music -void MusicDirectory::render(HTTPResponse& res) { +void MusicDirectory::render(HTTPRequest& req, HTTPResponse& res) { res.add_header("content-type", "text/html"); for(MusicDirectories::iterator it = directories.begin(); it != directories.end(); it++) { @@ -75,7 +81,7 @@ MusicDirectory::MusicDirectory(const fs::path root) { } } -void MusicTrack::render(HTTPResponse& res) { +void MusicTrack::render(HTTPRequest& req, HTTPResponse& res) { res.add_header("content-type", "application/octet-stream"); fs::ifstream is(path, std::ios::binary | std::ios::in); @@ -83,9 +89,18 @@ void MusicTrack::render(HTTPResponse& res) { res.add_header("content-length", boost::str(boost::format("%d") % is.tellg())); is.seekg(0, std::ios::beg); - char data[0x1000]; - while(is.good()) { - is.read(data, 0x1000); - res.write(data, is.gcount()); + if(req.query.find("decoder") != req.query.end() && req.query.find("encoder") != req.query.end()) { + DecoderBase *d = decoder::get_decoder(req.query["decoder"]); + EncoderBase *e = encoder::get_encoder(req.query["encoder"]); + Transcoder t(is, res, *d, *e); + t.run(); + delete d; + delete e; + } else { + char data[0x1000]; + while(is.good()) { + is.read(data, 0x1000); + res.write(data, is.gcount()); + } } } diff --git a/music.h b/music.h index 45c40fb..a86c6ac 100644 --- a/music.h +++ b/music.h @@ -12,13 +12,13 @@ namespace fs = boost::filesystem; class MusicListing { public: fs::path path; - virtual void render(HTTPResponse& res) = 0; + virtual void render(HTTPRequest& req, HTTPResponse& res) = 0; }; class MusicTrack : public MusicListing { public: MusicTrack(const fs::path path); - virtual void render(HTTPResponse& res); + virtual void render(HTTPRequest& req, HTTPResponse& res); }; class MusicDirectory; @@ -31,12 +31,12 @@ class MusicDirectory : public MusicListing { MusicTracks tracks; MusicDirectory(const fs::path root); - virtual void render(HTTPResponse& res); + virtual void render(HTTPRequest& req, HTTPResponse& res); }; namespace music { extern MusicDirectory *root_directory; - void init(const fs::path root); + void init(std::string root); MusicListing *find(std::string path, MusicDirectory& root); MusicListing *find(std::string path); }; -- cgit v1.2.3