Skip to content

Commit

Permalink
roc-streaminggh-183 Report initialization errors via init_status()
Browse files Browse the repository at this point in the history
This commit implements a part of roc-streaminggh-183 (pipeline error forwarding),
needed for roc-streaminggh-614 (status codes for frame writers/readers).

It replaces is_valid() in pipeline elements with init_status(), and
forwards initialization errors through pipeline.

Without it, when a function should forward a status, and both
initialization and read/write errors can happen, it wouldn't be
clear what status to return in case of initialization error.

Sponsored-by: waspd
  • Loading branch information
gavv committed Jun 20, 2024
1 parent bc199cd commit 479f0d4
Show file tree
Hide file tree
Showing 193 changed files with 1,886 additions and 1,480 deletions.
45 changes: 22 additions & 23 deletions src/internal_modules/roc_audio/builtin_resampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,22 @@ BuiltinResampler::BuiltinResampler(core::IArena& arena,
, qt_sample_(float_to_fixedpoint(0))
, qt_dt_(0)
, cutoff_freq_(0.9f)
, valid_(false) {
, init_status_(status::NoStatus) {
if (!in_spec_.is_valid() || !out_spec_.is_valid() || !in_spec_.is_raw()
|| !out_spec_.is_raw()) {
roc_panic("builtin resampler: required valid sample specs with raw format:"
" in_spec=%s out_spec=%s",
sample_spec_to_str(in_spec_).c_str(),
sample_spec_to_str(out_spec_).c_str());
}

if (in_spec_.channel_set() != out_spec_.channel_set()) {
roc_panic("builtin resampler: required identical input and output channel sets:"
" in_spec=%s out_spec=%s",
sample_spec_to_str(in_spec_).c_str(),
sample_spec_to_str(out_spec_).c_str());
}

roc_log(
LogDebug,
"builtin resampler: initializing:"
Expand All @@ -154,25 +169,28 @@ BuiltinResampler::BuiltinResampler(core::IArena& arena,
(unsigned long)in_spec_.num_channels());

if (!check_config_()) {
init_status_ = status::StatusBadConfig;
return;
}

if (!fill_sinc_()) {
init_status_ = status::StatusNoMem;
return;
}

if (!alloc_frames_(frame_factory)) {
init_status_ = status::StatusNoMem;
return;
}

valid_ = true;
init_status_ = status::StatusOK;
}

BuiltinResampler::~BuiltinResampler() {
}

bool BuiltinResampler::is_valid() const {
return valid_;
status::StatusCode BuiltinResampler::init_status() const {
return init_status_;
}

bool BuiltinResampler::set_scaling(size_t input_sample_rate,
Expand Down Expand Up @@ -313,25 +331,6 @@ bool BuiltinResampler::alloc_frames_(FrameFactory& frame_factory) {
}

bool BuiltinResampler::check_config_() const {
if (!in_spec_.is_valid() || !out_spec_.is_valid() || !in_spec_.is_raw()
|| !out_spec_.is_raw()) {
roc_log(LogError,
"builtin resampler: invalid sample spec:"
" in_spec=%s out_spec=%s",
sample_spec_to_str(in_spec_).c_str(),
sample_spec_to_str(out_spec_).c_str());
return false;
}

if (in_spec_.channel_set() != out_spec_.channel_set()) {
roc_log(LogError,
"builtin resampler: input and output channel sets should be equal:"
" in_spec=%s out_spec=%s",
sample_spec_to_str(in_spec_).c_str(),
sample_spec_to_str(out_spec_).c_str());
return false;
}

if (frame_size_ != frame_size_ch_ * in_spec_.num_channels()) {
roc_log(LogError,
"builtin resampler: frame_size is not multiple of num_channels:"
Expand Down
6 changes: 3 additions & 3 deletions src/internal_modules/roc_audio/builtin_resampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class BuiltinResampler : public IResampler, public core::NonCopyable<> {

~BuiltinResampler();

//! Check if object is successfully constructed.
virtual bool is_valid() const;
//! Check if the object was successfully constructed.
virtual status::StatusCode init_status() const;

//! Set new resample factor.
//! @remarks
Expand Down Expand Up @@ -135,7 +135,7 @@ class BuiltinResampler : public IResampler, public core::NonCopyable<> {

const sample_t cutoff_freq_;

bool valid_;
status::StatusCode init_status_;
};

} // namespace audio
Expand Down
11 changes: 6 additions & 5 deletions src/internal_modules/roc_audio/channel_mapper_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ChannelMapperReader::ChannelMapperReader(IFrameReader& reader,
, mapper_(in_spec.channel_set(), out_spec.channel_set())
, in_spec_(in_spec)
, out_spec_(out_spec)
, valid_(false) {
, init_status_(status::NoStatus) {
if (!in_spec_.is_valid() || !out_spec_.is_valid() || !in_spec_.is_raw()
|| !out_spec_.is_raw()) {
roc_panic("channel mapper reader: required valid sample specs with raw format:"
Expand All @@ -43,20 +43,21 @@ ChannelMapperReader::ChannelMapperReader(IFrameReader& reader,
input_buf_ = frame_factory.new_raw_buffer();
if (!input_buf_) {
roc_log(LogError, "channel mapper reader: can't allocate temporary buffer");
init_status_ = status::StatusNoMem;
return;
}

input_buf_.reslice(0, input_buf_.capacity());

valid_ = true;
init_status_ = status::StatusOK;
}

bool ChannelMapperReader::is_valid() const {
return valid_;
status::StatusCode ChannelMapperReader::init_status() const {
return init_status_;
}

bool ChannelMapperReader::read(Frame& out_frame) {
roc_panic_if(!valid_);
roc_panic_if(init_status_ != status::StatusOK);

if (out_frame.num_raw_samples() % out_spec_.num_channels() != 0) {
roc_panic("channel mapper reader: unexpected frame size");
Expand Down
7 changes: 4 additions & 3 deletions src/internal_modules/roc_audio/channel_mapper_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "roc_core/noncopyable.h"
#include "roc_core/slice.h"
#include "roc_core/stddefs.h"
#include "roc_status/status_code.h"

namespace roc {
namespace audio {
Expand All @@ -33,8 +34,8 @@ class ChannelMapperReader : public IFrameReader, public core::NonCopyable<> {
const SampleSpec& in_spec,
const SampleSpec& out_spec);

//! Check if the object was succefully constructed.
bool is_valid() const;
//! Check if the object was successfully constructed.
status::StatusCode init_status() const;

//! Read audio frame.
virtual bool read(Frame& frame);
Expand All @@ -53,7 +54,7 @@ class ChannelMapperReader : public IFrameReader, public core::NonCopyable<> {
const SampleSpec in_spec_;
const SampleSpec out_spec_;

bool valid_;
status::StatusCode init_status_;
};

} // namespace audio
Expand Down
11 changes: 6 additions & 5 deletions src/internal_modules/roc_audio/channel_mapper_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ChannelMapperWriter::ChannelMapperWriter(IFrameWriter& writer,
, mapper_(in_spec.channel_set(), out_spec.channel_set())
, in_spec_(in_spec)
, out_spec_(out_spec)
, valid_(false) {
, init_status_(status::NoStatus) {
if (!in_spec_.is_valid() || !out_spec_.is_valid() || !in_spec_.is_raw()
|| !out_spec_.is_raw()) {
roc_panic("channel mapper writer: required valid sample specs with raw format:"
Expand All @@ -43,20 +43,21 @@ ChannelMapperWriter::ChannelMapperWriter(IFrameWriter& writer,
output_buf_ = frame_factory.new_raw_buffer();
if (!output_buf_) {
roc_log(LogError, "channel mapper writer: can't allocate temporary buffer");
init_status_ = status::StatusNoMem;
return;
}

output_buf_.reslice(0, output_buf_.capacity());

valid_ = true;
init_status_ = status::StatusOK;
}

bool ChannelMapperWriter::is_valid() const {
return valid_;
status::StatusCode ChannelMapperWriter::init_status() const {
return init_status_;
}

void ChannelMapperWriter::write(Frame& in_frame) {
roc_panic_if(!valid_);
roc_panic_if(init_status_ != status::StatusOK);

if (in_frame.num_raw_samples() % in_spec_.num_channels() != 0) {
roc_panic("channel mapper writer: unexpected frame size");
Expand Down
7 changes: 4 additions & 3 deletions src/internal_modules/roc_audio/channel_mapper_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "roc_core/noncopyable.h"
#include "roc_core/slice.h"
#include "roc_core/stddefs.h"
#include "roc_status/status_code.h"

namespace roc {
namespace audio {
Expand All @@ -33,8 +34,8 @@ class ChannelMapperWriter : public IFrameWriter, public core::NonCopyable<> {
const SampleSpec& in_spec,
const SampleSpec& out_spec);

//! Check if the object was succefully constructed.
bool is_valid() const;
//! Check if the object was successfully constructed.
status::StatusCode init_status() const;

//! Write audio frame.
virtual void write(Frame& frame);
Expand All @@ -53,7 +54,7 @@ class ChannelMapperWriter : public IFrameWriter, public core::NonCopyable<> {
const SampleSpec in_spec_;
const SampleSpec out_spec_;

bool valid_;
status::StatusCode init_status_;
};

} // namespace audio
Expand Down
53 changes: 26 additions & 27 deletions src/internal_modules/roc_audio/decimation_resampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,67 +44,66 @@ DecimationResampler::DecimationResampler(
, total_count_(0)
, decim_count_(0)
, report_limiter_(LogReportInterval)
, valid_(false) {
roc_log(LogDebug,
"decimation resampler: initializing: "
" frame_size=%lu num_ch=%lu use_inner_resampler=%d",
(unsigned long)InputFrameSize, (unsigned long)num_ch_,
(int)use_inner_resampler_);

, init_status_(status::NoStatus) {
if (!in_spec.is_valid() || !out_spec.is_valid() || !in_spec.is_raw()
|| !out_spec.is_raw()) {
roc_log(LogError,
"decimation resampler: invalid sample spec:"
" in_spec=%s out_spec=%s",
sample_spec_to_str(in_spec).c_str(),
sample_spec_to_str(out_spec).c_str());
return;
roc_panic("decimation resampler: required valid sample specs with raw format:"
" in_spec=%s out_spec=%s",
sample_spec_to_str(in_spec).c_str(),
sample_spec_to_str(out_spec).c_str());
}

if (in_spec.channel_set() != out_spec.channel_set()) {
roc_log(LogError,
"decimation resampler: input and output channel sets should be equal:"
" in_spec=%s out_spec=%s",
sample_spec_to_str(in_spec).c_str(),
sample_spec_to_str(out_spec).c_str());
return;
roc_panic(
"decimation resampler: required identical input and output channel sets:"
" in_spec=%s out_spec=%s",
sample_spec_to_str(in_spec).c_str(), sample_spec_to_str(out_spec).c_str());
}

roc_log(LogDebug,
"decimation resampler: initializing: "
" frame_size=%lu num_ch=%lu use_inner_resampler=%d",
(unsigned long)InputFrameSize, (unsigned long)num_ch_,
(int)use_inner_resampler_);

if (frame_factory.raw_buffer_size() < InputFrameSize * num_ch_) {
roc_log(LogError, "decimation resampler: can't allocate temporary buffer");
init_status_ = status::StatusNoSpace;
return;
}

in_buf_ = frame_factory.new_raw_buffer();
if (!in_buf_) {
roc_log(LogError, "decimation resampler: can't allocate temporary buffer");
init_status_ = status::StatusNoMem;
return;
}
in_buf_.reslice(0, InputFrameSize * num_ch_);

last_buf_ = frame_factory.new_raw_buffer();
if (!last_buf_) {
roc_log(LogError, "decimation resampler: can't allocate temporary buffer");
init_status_ = status::StatusNoMem;
return;
}
last_buf_.reslice(0, num_ch_);

memset(last_buf_.data(), 0, last_buf_.size() * sizeof(sample_t));

valid_ = true;
init_status_ = status::StatusOK;
}

DecimationResampler::~DecimationResampler() {
}

bool DecimationResampler::is_valid() const {
return valid_;
status::StatusCode DecimationResampler::init_status() const {
return init_status_;
}

bool DecimationResampler::set_scaling(size_t input_rate,
size_t output_rate,
float multiplier) {
roc_panic_if_not(is_valid());
roc_panic_if(init_status_ != status::StatusOK);

if (input_rate == 0 || output_rate == 0
|| multiplier <= 0
Expand Down Expand Up @@ -137,7 +136,7 @@ bool DecimationResampler::set_scaling(size_t input_rate,
}

const core::Slice<sample_t>& DecimationResampler::begin_push_input() {
roc_panic_if_not(is_valid());
roc_panic_if(init_status_ != status::StatusOK);

if (use_inner_resampler_) {
// return buffer of inner resampler
Expand All @@ -149,7 +148,7 @@ const core::Slice<sample_t>& DecimationResampler::begin_push_input() {
}

void DecimationResampler::end_push_input() {
roc_panic_if_not(is_valid());
roc_panic_if(init_status_ != status::StatusOK);

if (use_inner_resampler_) {
// start reading from inner resampler
Expand All @@ -164,7 +163,7 @@ void DecimationResampler::end_push_input() {
}

size_t DecimationResampler::pop_output(sample_t* out_data, size_t out_size) {
roc_panic_if_not(is_valid());
roc_panic_if(init_status_ != status::StatusOK);

size_t out_pos = 0;

Expand Down Expand Up @@ -233,7 +232,7 @@ size_t DecimationResampler::pop_output(sample_t* out_data, size_t out_size) {
}

float DecimationResampler::n_left_to_process() const {
roc_panic_if_not(is_valid());
roc_panic_if(init_status_ != status::StatusOK);

// how much samples are pending in our buffer
float n_samples = float(in_size_ - in_pos_) / output_spec_.sample_rate()
Expand Down
6 changes: 3 additions & 3 deletions src/internal_modules/roc_audio/decimation_resampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class DecimationResampler : public IResampler, public core::NonCopyable<> {

~DecimationResampler();

//! Check if object is successfully constructed.
virtual bool is_valid() const;
//! Check if the object was successfully constructed.
virtual status::StatusCode init_status() const;

//! Set new resample factor.
virtual bool set_scaling(size_t input_rate, size_t output_rate, float multiplier);
Expand Down Expand Up @@ -100,7 +100,7 @@ class DecimationResampler : public IResampler, public core::NonCopyable<> {
size_t decim_count_;
core::RateLimiter report_limiter_;

bool valid_;
status::StatusCode init_status_;
};

} // namespace audio
Expand Down
Loading

0 comments on commit 479f0d4

Please sign in to comment.