diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-12-28 20:39:20 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-12-28 20:39:20 +0100 |
commit | 9bcf29e9ce11bbc6a421b39eded8f4eb7d94a912 (patch) | |
tree | dd03b0ab0b037285d1827101abad6023f6c6afaf | |
parent | 5fd3fe8704374d9ffc77e61d467e13a587d1aff9 (diff) |
Added decoder/encoder factories.
-rw-r--r-- | decoder.cpp | 19 | ||||
-rw-r--r-- | decoder.h | 10 | ||||
-rw-r--r-- | decoders/mpg123_decoder.cpp | 2 | ||||
-rw-r--r-- | encoder.cpp | 18 | ||||
-rw-r--r-- | encoder.h | 10 | ||||
-rw-r--r-- | main.cpp | 4 |
6 files changed, 61 insertions, 2 deletions
diff --git a/decoder.cpp b/decoder.cpp new file mode 100644 index 0000000..e80c714 --- /dev/null +++ b/decoder.cpp @@ -0,0 +1,19 @@ +#include "decoder.h" +#include "decoders/mpg123_decoder.h" + +#include <map> + +namespace decoder { + typedef boost::function<DecoderBase*()> DecoderFactory; + std::map<std::string, DecoderFactory> decoder_factories; + +void init() { + mpg123_init(); // initialize the mpg123 library + decoder_factories["mpg123"] = boost::factory<DecoderMpg123*>(); +} + +DecoderBase *get_decoder(const std::string& name) { + return decoder_factories[name](); +} + +}; @@ -2,10 +2,20 @@ #define DECODER_H #include <boost/cstdint.hpp> +#include <boost/function.hpp> +#include <boost/functional/factory.hpp> +#include <boost/functional/value_factory.hpp> + +#include <string> class DecoderBase { public: virtual size_t decode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) = 0; }; +namespace decoder { + void init(); + DecoderBase *get_decoder(const std::string& name); +}; + #endif diff --git a/decoders/mpg123_decoder.cpp b/decoders/mpg123_decoder.cpp index 41aef40..0c2629f 100644 --- a/decoders/mpg123_decoder.cpp +++ b/decoders/mpg123_decoder.cpp @@ -6,8 +6,6 @@ #include <stdexcept> DecoderMpg123::DecoderMpg123() { - mpg123_init(); - int error; handle = mpg123_new("generic", &error); if(error) { diff --git a/encoder.cpp b/encoder.cpp new file mode 100644 index 0000000..9bea59b --- /dev/null +++ b/encoder.cpp @@ -0,0 +1,18 @@ +#include "encoder.h" +#include "encoders/lame_encoder.h" + +#include <map> + +namespace encoder { + typedef boost::function<EncoderBase*()> EncoderFactory; + std::map<std::string, EncoderFactory> encoder_factories; + +void init() { + encoder_factories["lame"] = boost::factory<EncoderLame*>(); +} + +EncoderBase *get_encoder(const std::string& name) { + return encoder_factories[name](); +} + +}; @@ -2,6 +2,11 @@ #define ENCODER_H #include <boost/cstdint.hpp> +#include <boost/function.hpp> +#include <boost/functional/factory.hpp> +#include <boost/functional/value_factory.hpp> + +#include <string> class EncoderBase { public: @@ -9,4 +14,9 @@ class EncoderBase { virtual size_t flush(uint8_t *output, size_t output_size) = 0; }; +namespace encoder { + void init(); + EncoderBase *get_encoder(const std::string& name); +}; + #endif @@ -1,4 +1,6 @@ #include "music.h" +#include "decoder.h" +#include "encoder.h" #include "httpd.h" #include <iostream> @@ -9,6 +11,8 @@ int main(int argc, char **argv) { try { music::init(argv[1]); + decoder::init(); + encoder::init(); boost::asio::io_service io_service; HTTPServer httpd(io_service); |