diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-12-28 21:08:58 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-12-28 21:08:58 +0100 |
commit | 83deeeafe2e81b42ff485e4582d4c1ac00872f50 (patch) | |
tree | 324d2e8bf8ae5cf01a1c56b3496f0ecf29749928 | |
parent | 9bcf29e9ce11bbc6a421b39eded8f4eb7d94a912 (diff) |
Added a simple interface to transcoding by specifying the 'decoder' and 'encoder' query arguments.
-rw-r--r-- | httpd.cpp | 2 | ||||
-rw-r--r-- | music.cpp | 29 | ||||
-rw-r--r-- | music.h | 8 |
3 files changed, 27 insertions, 12 deletions
@@ -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"; @@ -1,4 +1,7 @@ #include "music.h" +#include "decoder.h" +#include "encoder.h" +#include "transcode.h" #include <boost/format.hpp> #include <boost/algorithm/string/predicate.hpp> @@ -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()); + } } } @@ -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); }; |