From 3a3bab26aea5c064c07c307d1e8ccf463edae568 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 3 Jan 2011 20:50:59 +0100 Subject: Decoder, encoder and transcoder-related classes and factories now uses boost::shared_ptr. --- decoder.cpp | 19 ++++++++++--------- decoder.h | 13 ++++++++----- encoder.cpp | 19 ++++++++++--------- encoder.h | 11 +++++++---- music.cpp | 8 +++----- transcode.cpp | 6 +++--- transcode.h | 6 +++--- 7 files changed, 44 insertions(+), 38 deletions(-) diff --git a/decoder.cpp b/decoder.cpp index e80c714..cffb8cf 100644 --- a/decoder.cpp +++ b/decoder.cpp @@ -3,17 +3,18 @@ #include -namespace decoder { - typedef boost::function DecoderFactory; - std::map decoder_factories; +DecoderFilter::DecoderFilter(DecoderBase::p decoder_) { + decoder = decoder_; +} + +typedef boost::function()> DecoderFactory; +std::map decoder_factories; -void init() { +void decoder::init() { mpg123_init(); // initialize the mpg123 library - decoder_factories["mpg123"] = boost::factory(); + decoder_factories["mpg123"] = boost::factory >(); } -DecoderBase *get_decoder(const std::string& name) { - return decoder_factories[name](); +DecoderFilter::p decoder::get_decoder(const std::string& name) { + return DecoderFilter::p(new DecoderFilter(DecoderBase::p(decoder_factories[name]()))); } - -}; diff --git a/decoder.h b/decoder.h index a7aaad9..3694053 100644 --- a/decoder.h +++ b/decoder.h @@ -7,32 +7,35 @@ #include #include #include +#include #include class DecoderBase { public: + typedef boost::shared_ptr p; virtual size_t decode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) = 0; }; class DecoderFilter : public boost::iostreams::multichar_input_filter { private: - DecoderBase& decoder; + DecoderBase::p decoder; public: - DecoderFilter(DecoderBase& decoder_) : decoder(decoder_) {}; + typedef boost::shared_ptr p; + DecoderFilter(DecoderBase::p decoder_); template std::streamsize read(Source& src, char *s, std::streamsize n) { char src_data[0x2000]; std::streamsize src_read = boost::iostreams::read(src, src_data, 0x2000); if(src_read < 0) src_read = 0; - return decoder.decode((const uint8_t*)src_data, src_read, (uint8_t*)s, n); - }; + return decoder->decode((const uint8_t*)src_data, src_read, (uint8_t*)s, n); + } }; namespace decoder { void init(); - DecoderBase *get_decoder(const std::string& name); + DecoderFilter::p get_decoder(const std::string& name); }; #endif diff --git a/encoder.cpp b/encoder.cpp index 9bea59b..d0566fb 100644 --- a/encoder.cpp +++ b/encoder.cpp @@ -3,16 +3,17 @@ #include -namespace encoder { - typedef boost::function EncoderFactory; - std::map encoder_factories; - -void init() { - encoder_factories["lame"] = boost::factory(); +EncoderFilter::EncoderFilter(EncoderBase::p encoder_) { + encoder = encoder_; } -EncoderBase *get_encoder(const std::string& name) { - return encoder_factories[name](); +typedef boost::function()> EncoderFactory; +std::map encoder_factories; + +void encoder::init() { + encoder_factories["lame"] = boost::factory >(); } -}; +EncoderFilter::p encoder::get_encoder(const std::string& name) { + return EncoderFilter::p(new EncoderFilter(EncoderBase::p(encoder_factories[name]()))); +} diff --git a/encoder.h b/encoder.h index 7021cdc..1ff0879 100644 --- a/encoder.h +++ b/encoder.h @@ -7,33 +7,36 @@ #include #include #include +#include #include class EncoderBase { public: + typedef boost::shared_ptr p; virtual size_t encode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) = 0; virtual size_t flush(uint8_t *output, size_t output_size) = 0; }; class EncoderFilter : public boost::iostreams::multichar_input_filter { private: - EncoderBase& encoder; + EncoderBase::p encoder; public: - EncoderFilter(EncoderBase& encoder_) : encoder(encoder_) {}; + typedef boost::shared_ptr p; + EncoderFilter(EncoderBase::p encoder_); template std::streamsize read(Source& src, char *s, std::streamsize n) { char src_data[0x2000]; std::streamsize src_read = boost::iostreams::read(src, src_data, 0x2000); if(src_read < 0) src_read = 0; - return encoder.encode((const uint8_t*)src_data, src_read, (uint8_t*)s, n); + return encoder->encode((const uint8_t*)src_data, src_read, (uint8_t*)s, n); }; }; namespace encoder { void init(); - EncoderBase *get_encoder(const std::string& name); + EncoderFilter::p get_encoder(const std::string& name); }; #endif diff --git a/music.cpp b/music.cpp index fcc0daf..15ddbba 100644 --- a/music.cpp +++ b/music.cpp @@ -133,12 +133,10 @@ void MusicTrack::render(HTTP::Connection::p req) { delete t; if(req->args.count("decoder") && req->args.count("encoder")) { - DecoderBase *d = decoder::get_decoder(req->args["decoder"]); - EncoderBase *e = encoder::get_encoder(req->args["encoder"]); - Transcoder t(path.string(), req, *d, *e); + DecoderFilter::p d = decoder::get_decoder(req->args["decoder"]); + EncoderFilter::p e = encoder::get_encoder(req->args["encoder"]); + Transcoder t(path.string(), req, d, e); t.run(); - delete d; - delete e; } else { fs::ifstream is(path, std::ios::in | std::ios::binary); is.seekg(0, std::ios::end); diff --git a/transcode.cpp b/transcode.cpp index f3698a3..394f604 100644 --- a/transcode.cpp +++ b/transcode.cpp @@ -3,15 +3,15 @@ #include #include -Transcoder::Transcoder(std::string p, HTTP::Connection::p r, DecoderBase& d, EncoderBase& e) : path(p), res(r), decoder(d), encoder(e) { +Transcoder::Transcoder(std::string p, HTTP::Connection::p r, DecoderFilter::p d, EncoderFilter::p e) : path(p), res(r), decoder(d), encoder(e) { } void Transcoder::run() { const std::streamsize buffer_size = 0x1000; boost::iostreams::file_source is(path, std::ios::in | std::ios::binary); boost::iostreams::filtering_istream s; - s.push(EncoderFilter(encoder), buffer_size); - s.push(DecoderFilter(decoder), buffer_size); + s.push(*encoder.get(), buffer_size); + s.push(*decoder.get(), buffer_size); s.push(is, buffer_size); res->send_data(s); diff --git a/transcode.h b/transcode.h index 70d0113..56f16c1 100644 --- a/transcode.h +++ b/transcode.h @@ -11,11 +11,11 @@ class Transcoder { private: std::string path; HTTP::Connection::p res; - DecoderBase& decoder; - EncoderBase& encoder; + DecoderFilter::p decoder; + EncoderFilter::p encoder; public: - Transcoder(std::string p, HTTP::Connection::p r, DecoderBase& d, EncoderBase& e); + Transcoder(std::string p, HTTP::Connection::p r, DecoderFilter::p d, EncoderFilter::p e); void run(); }; -- cgit v1.2.3