Add resizer letterbox2
[jpcrr.git] / streamtools / output-drv-oggenc.cpp
blob78c2b3bc054949c82693cc995b0d45548fa9997d
1 #include "output-drv.hpp"
2 #include <cstdio>
3 #include <stdexcept>
4 #include <string>
5 #include <sstream>
7 namespace
9 std::string expand_options(const std::string& opts)
11 bool insert = true;
12 std::ostringstream ret;
13 for(size_t i = 0; i < opts.length(); i++) {
14 if(insert)
15 ret << " --";
16 insert = false;
17 switch(opts[i]) {
18 case ',':
19 insert = true;
20 break;
21 default:
22 ret << opts[i];
25 ret << " ";
26 return ret.str();
29 class output_driver_oggenc : public output_driver
31 public:
32 output_driver_oggenc(const std::string& _filename, const std::string& _options)
34 filename = _filename;
35 options = _options;
36 set_audio_callback<output_driver_oggenc>(*this, &output_driver_oggenc::audio_callback);
39 ~output_driver_oggenc()
41 pclose(out);
44 void ready()
46 const audio_settings& a = get_audio_settings();
48 std::stringstream commandline;
49 commandline << "oggenc -r -R " << a.get_rate() << " ";
50 commandline << expand_options(options);
51 commandline << "-o " << filename << " -";
52 std::string s = commandline.str();
53 out = popen(s.c_str(), "w");
54 if(!out) {
55 std::stringstream str;
56 str << "Can't run oggenc (" << s << ")";
57 throw std::runtime_error(str.str());
61 void audio_callback(short left, short right)
63 uint8_t rawdata[4];
64 rawdata[1] = ((unsigned short)left >> 8) & 0xFF;
65 rawdata[0] = ((unsigned short)left) & 0xFF;
66 rawdata[3] = ((unsigned short)right >> 8) & 0xFF;
67 rawdata[2] = ((unsigned short)right) & 0xFF;
68 if(fwrite(rawdata, 1, 4, out) < 4)
69 throw std::runtime_error("Error writing sample to oggenc");
71 private:
72 FILE* out;
73 std::string filename;
74 std::string options;
77 class output_driver_oggenc_factory : output_driver_factory
79 public:
80 output_driver_oggenc_factory()
81 : output_driver_factory("oggenc")
85 output_driver& make(const std::string& type, const std::string& name, const std::string& parameters)
87 return *new output_driver_oggenc(name, parameters);
89 } factory;