diff options
author | Goran Mekić <meka@tilda.center> | 2017-04-30 16:10:43 +0200 |
---|---|---|
committer | Goran Mekić <meka@tilda.center> | 2017-05-02 10:48:00 +0200 |
commit | 33639dddd8b1b01739994b7427d70ffc676cb22d (patch) | |
tree | 81dcd5a5e6f9428aadeb203fb28988ac67919f67 /drumgizmo/output | |
parent | ca5cf0102d2617f1a701e7a0a011ef4b02b14d0b (diff) |
Sanatize OSS output code
Diffstat (limited to 'drumgizmo/output')
-rw-r--r-- | drumgizmo/output/oss.cc | 35 | ||||
-rw-r--r-- | drumgizmo/output/oss.h | 6 |
2 files changed, 29 insertions, 12 deletions
diff --git a/drumgizmo/output/oss.cc b/drumgizmo/output/oss.cc index 9815460..80d8d2b 100644 --- a/drumgizmo/output/oss.cc +++ b/drumgizmo/output/oss.cc @@ -48,7 +48,7 @@ OSSOutputEngine::OSSOutputEngine() bool OSSOutputEngine::init(const Channels& channels) { - int tmp, mode = O_WRONLY, fragments; + std::size_t tmp, mode = O_WRONLY, fragments; num_channels = channels.size(); if((fd = open(dev.data(), mode, 0)) == -1) @@ -59,10 +59,19 @@ bool OSSOutputEngine::init(const Channels& channels) fragments = max_fragments << 16 | fragment_size; tmp = fragments; - if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1 || tmp != fragments) + if(ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1) { - perror("SNDCTL_DSP_SETFRAGMENT"); - exit(-1); + std::cerr << "Can not set buffer size: "; + std::cerr << std::strerror(errno) << std::endl; + return false; + } + if(tmp != fragments) + { + std::size_t real_fragment_size = fragments & 0xffff; + std::size_t real_max_fragments = (fragments - real_fragment_size) >> 16; + std::cerr << "Values corrected to:" << std::endl; + std::cerr << " fragment_size: " << real_fragment_size << std::endl; + std::cerr << " max_fragments: " << real_max_fragments << std::endl; } audio_buf_info info; @@ -72,7 +81,7 @@ bool OSSOutputEngine::init(const Channels& channels) std::cerr << std::strerror(errno) << std::endl; return false; } - buffer_size = info.bytes / 4; + buffer_size = info.bytes / sizeof(decltype (data)::value_type); tmp = format; if(ioctl(fd, SNDCTL_DSP_SETFMT, &tmp) == -1 || tmp != format) @@ -150,11 +159,19 @@ void OSSOutputEngine::setParm(const std::string& parm, const std::string& value) } if(fragment_size < 4) { - std::cerr << "[OSSoutputEngine] fragment_size must be at least 4\n"; - std::cerr << "Setting fragment_size to 4 ("; fragment_size = 4; - auto fragment_byte_size = 1 << fragment_size; - std::cerr << fragment_byte_size << "Bytes)" << std::endl; + std::cerr << "[OSSoutputEngine] fragment_size must be at least "; + std::cerr << fragment_size << std::endl; + std::cerr << "Setting fragment_size to "; + std::cerr << fragment_size << std::endl; + } + else if(fragment_size > 0xffff) + { + fragment_size = 0xffff; + std::cerr << "[OSSoutputEngine] fragment_size must be at most "; + std::cerr << fragment_size << std::endl; + std::cerr << "Setting fragment_size to "; + std::cerr << fragment_size << std::endl; } } else diff --git a/drumgizmo/output/oss.h b/drumgizmo/output/oss.h index 1233ec1..880ab9c 100644 --- a/drumgizmo/output/oss.h +++ b/drumgizmo/output/oss.h @@ -52,8 +52,8 @@ private: std::size_t num_channels; unsigned int srate; unsigned int format; - unsigned int max_fragments; - unsigned int fragment_size; - unsigned int buffer_size; + std::size_t max_fragments; + std::size_t fragment_size; + std::size_t buffer_size; std::vector<std::int32_t> data; }; |