Udated a lot of stuff

This commit is contained in:
WolverinDEV
2019-06-30 17:24:10 +02:00
parent 6b70b8d425
commit 160c6d83db
25 changed files with 642 additions and 176 deletions
@@ -34,6 +34,9 @@ namespace tc {
virtual bool valid() = 0;
virtual void finalize() = 0;
virtual void reset_encoder() = 0;
virtual void reset_decoder() = 0;
/**
* @return number of bytes written on success
*/
@@ -1,4 +1,5 @@
#include "OpusConverter.h"
#include "../../logger.h"
using namespace std;
using namespace tc::audio::codec;
@@ -11,27 +12,57 @@ bool OpusConverter::valid() {
}
bool OpusConverter::initialize(std::string &error, int application_type) {
int error_id = 0;
lock_guard lock(this->coder_lock);
this->_application_type = application_type;
this->encoder = opus_encoder_create((opus_int32) this->_sample_rate, (int) this->_channels, application_type, &error_id);
if(!this->encoder || error_id) {
error = "failed to create encoder (" + to_string(error_id) + ")";
if(!this->_initialize_encoder(error))
return false;
}
this->decoder = opus_decoder_create((opus_int32) this->_sample_rate, (int) this->_channels, &error_id);
if(!this->encoder || error_id) {
opus_encoder_destroy(this->encoder);
this->encoder = nullptr;
error = "failed to create decoder (" + to_string(error_id) + ")";
if(!this->_initialize_decoder(error)) {
this->reset_encoder();
return false;
}
return true;
}
void OpusConverter::reset_encoder() {
lock_guard lock(this->coder_lock);
string error;
bool flag_error = false;
if(!(flag_error |= !this->_finalize_encoder(error))) {
error = "finalize failed (" + error + ")";
}
if(!flag_error && !(flag_error |= !this->_initialize_encoder(error))) {
error = "initialize failed (" + error + ")";
}
if(flag_error)
log_warn(category::audio, tr("Failed to reset opus encoder: {}"), error);
}
void OpusConverter::reset_decoder() {
lock_guard lock(this->coder_lock);
string error;
bool flag_error = false;
if(!(flag_error |= !this->_finalize_decoder(error))) {
error = "finalize failed (" + error + ")";
}
if(!flag_error && !(flag_error |= !this->_initialize_decoder(error))) {
error = "initialize failed (" + error + ")";
}
if(flag_error)
log_warn(category::audio, tr("Failed to reset opus decoder: {}"), error);
}
void OpusConverter::finalize() {
lock_guard lock(this->coder_lock);
@@ -78,3 +109,45 @@ size_t OpusConverter::expected_encoded_length(size_t sample_count) {
//TODO calculate stuff
return 512;
}
bool OpusConverter::_initialize_decoder(std::string &error) {
if(!this->_finalize_decoder(error))
return false;
int error_id = 0;
this->decoder = opus_decoder_create((opus_int32) this->_sample_rate, (int) this->_channels, &error_id);
if(!this->encoder || error_id) {
error = "failed to create decoder (" + to_string(error_id) + ")";
return false;
}
return true;
}
bool OpusConverter::_initialize_encoder(std::string &error) {
if(!this->_finalize_encoder(error))
return false;
int error_id = 0;
this->encoder = opus_encoder_create((opus_int32) this->_sample_rate, (int) this->_channels, this->_application_type, &error_id);
if(!this->encoder || error_id) {
error = "failed to create encoder (" + to_string(error_id) + ")";
return false;
}
return true;
}
bool OpusConverter::_finalize_decoder(std::string &) {
if(this->decoder) {
opus_decoder_destroy(this->decoder);
this->decoder = nullptr;
}
return true;
}
bool OpusConverter::_finalize_encoder(std::string &) {
if(this->encoder) {
opus_encoder_destroy(this->encoder);
this->encoder = nullptr;
}
return true;
}
@@ -17,6 +17,9 @@ namespace tc {
bool initialize(std::string& /* error */, int /* application type */);
void finalize() override;
void reset_encoder() override;
void reset_decoder() override;
ssize_t encode(std::string & /* error */, const void * /* source */, void * /* target */, size_t /* target size */) override;
ssize_t decode(std::string & /* error */, const void */* source */, size_t /* source size */, void *pVoid1) override;
@@ -27,6 +30,13 @@ namespace tc {
std::mutex coder_lock;
OpusDecoder* decoder = nullptr;
OpusEncoder* encoder = nullptr;
int _application_type = 0;
bool _finalize_encoder(std::string& /* error */);
bool _finalize_decoder(std::string& /* error */);
bool _initialize_encoder(std::string& /* error */);
bool _initialize_decoder(std::string& /* error */);
};
}
}