Skip to content

Commit

Permalink
Refactored several tests, wav_source doesnt stop rate changing as of now
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrick87 committed Feb 24, 2024
1 parent 56e70b4 commit f139b8c
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@ bool SoxSource::open_() {
}

is_file_ = !(input_->handler.flags & SOX_FILE_DEVICE);

if(is_file_ && in_signal_.rate != input_->signal.rate && in_signal_.rate != 0){
roc_log(LogInfo, "sndfile source: can't set rate: samplerate in argument is different from file samplerate");
return false;
}

sample_spec_.set_sample_rate((unsigned long)input_->signal.rate);

roc_log(LogInfo,
Expand Down
1 change: 1 addition & 0 deletions src/tests/roc_sndio/target_sndfile/test_sndfile_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ TEST(sndfile_source, has_clock) {
CHECK(!sndfile_source.has_clock());
}


TEST(sndfile_source, sample_rate_auto) {
core::TempFile file("test.wav");

Expand Down
211 changes: 211 additions & 0 deletions src/tests/roc_sndio/test_backend_source.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/*
* Copyright (c) 2023 Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <CppUTest/TestHarness.h>

#include "test_helpers/mock_source.h"

#include "roc_core/buffer_factory.h"
#include "roc_core/scoped_ptr.h"
#include "roc_sndio/backend_map.h"
#include "roc_core/heap_arena.h"
#include "roc_core/stddefs.h"
#include "roc_core/temp_file.h"
#include "roc_sndio/pump.h"
#include "roc_sndio/sndfile_sink.h"
#include "roc_sndio/sndfile_source.h"
#include "roc_sndio/sox_sink.h"
#include "roc_sndio/sox_source.h"

namespace roc {
namespace sndio {

namespace {

enum {
MaxBufSize = 8192,
FrameSize = 500,
SampleRate = 44100,
ChMask = 0x3,
NumChans = 2
};

const audio::SampleSpec
SampleSpecs(SampleRate, audio::Sample_RawFormat, audio::ChanLayout_Surround, audio::ChanOrder_Smpte, ChMask);

const core::nanoseconds_t FrameDuration = FrameSize * core::Second
/ core::nanoseconds_t(SampleSpecs.sample_rate() * SampleSpecs.num_channels());

core::HeapArena arena;
core::BufferFactory<audio::sample_t> buffer_factory(arena, MaxBufSize);

bool supports_wav(IBackend &backend){
bool supports = false;
core::Array<DriverInfo, MaxDrivers> driver_list;
backend.discover_drivers(driver_list);
for (size_t n = 0; n < driver_list.size(); n++) {
if (strcmp(driver_list[n].name, "wav") == 0) {
supports = true;
break;
}
}

return supports;
}

} // namespace

TEST_GROUP(backend_source) {
Config sink_config;
Config source_config;

void setup() {
sink_config.sample_spec = audio::SampleSpec(
SampleRate, audio::Sample_RawFormat, audio::ChanLayout_Surround, audio::ChanOrder_Smpte, ChMask);
sink_config.frame_length = FrameDuration;

source_config.sample_spec = audio::SampleSpec();
source_config.frame_length = FrameDuration;
}
};

TEST(backend_source, noop) {
SndfileSource sndfile_source(arena, source_config);
SoxSource sox_source(arena, source_config);
}

TEST(backend_source, error) {
for (size_t n_backend = 0; n_backend < BackendMap::instance().num_backends(); n_backend++) {
IBackend &backend = BackendMap::instance().nth_backend(n_backend);
IDevice *backend_device = backend.open_device(DeviceType_Source, DriverType_File, NULL, "/bad/file", sink_config, arena);
CHECK(backend_device == NULL);
}
}

TEST(backend_source, has_clock) {
core::TempFile file("test.wav");

for (size_t n_backend = 0; n_backend < BackendMap::instance().num_backends(); n_backend++) {
IBackend& backend = BackendMap::instance().nth_backend(n_backend);

bool supports_wav = false;
core::Array<DriverInfo, MaxDrivers> driver_list;
backend.discover_drivers(driver_list);
for (size_t n = 0; n < driver_list.size(); n++) {
if (strcmp(driver_list[n].name, "wav") == 0) {
supports_wav = true;
break;
}
}
if(!supports_wav){
continue;
}

{
test::MockSource mock_source;
mock_source.add(MaxBufSize * 10);

IDevice* backend_device = backend.open_device(DeviceType_Sink, DriverType_File, NULL, file.path(), sink_config, arena);
CHECK(backend_device != NULL);
core::ScopedPtr<ISink> backend_sink(backend_device->to_sink(), arena);
CHECK(backend_sink != NULL);

Pump pump(buffer_factory, mock_source, NULL, *backend_sink, FrameDuration, SampleSpecs,
Pump::ModeOneshot);
CHECK(pump.is_valid());
CHECK(pump.run());
}

IDevice* backend_device = backend.open_device(DeviceType_Source, DriverType_File, NULL, file.path(), source_config, arena);
CHECK(backend_device != NULL);
core::ScopedPtr<ISource> backend_source(backend_device->to_source(), arena);
CHECK(backend_source != NULL);
CHECK(!backend_source->has_clock());
}
}

TEST(backend_source, sample_rate_auto) {
core::TempFile file("test.wav");

for (size_t n_backend = 0; n_backend < BackendMap::instance().num_backends(); n_backend++) {
IBackend& backend = BackendMap::instance().nth_backend(n_backend);

if (!supports_wav(backend)) {
continue;
}

{
test::MockSource mock_source;
mock_source.add(MaxBufSize * 10);

IDevice* backend_device = backend.open_device(DeviceType_Sink, DriverType_File, "wav", file.path(), sink_config, arena);
CHECK(backend_device != NULL);
core::ScopedPtr<ISink> backend_sink(backend_device->to_sink(), arena);
CHECK(backend_sink != NULL);

Pump pump(buffer_factory, mock_source, NULL, *backend_sink, FrameDuration, SampleSpecs, Pump::ModeOneshot);
CHECK(pump.is_valid());
CHECK(pump.run());
}
source_config.sample_spec.set_sample_rate(0);
source_config.frame_length = FrameDuration;

IDevice *backend_device = backend.open_device(DeviceType_Source, DriverType_File, "wav", file.path(), source_config, arena);
CHECK(backend_device != NULL);
core::ScopedPtr<ISource> backend_source(backend_device->to_source(), arena);
CHECK(backend_source != NULL);

CHECK(backend_source->sample_spec().sample_rate() == SampleRate);
}
}

TEST(backend_source, sample_rate_mismatch) {
core::TempFile file("test.wav");

for (size_t n_backend = 0; n_backend < BackendMap::instance().num_backends(); n_backend++) {
IBackend& backend = BackendMap::instance().nth_backend(n_backend);

if (!supports_wav(backend)) {
continue;
}

{
test::MockSource mock_source;
mock_source.add(MaxBufSize * 10);

IDevice* backend_device = backend.open_device(DeviceType_Sink, DriverType_File, "wav", file.path(), sink_config, arena);
CHECK(backend_device != NULL);
core::ScopedPtr<ISink> backend_sink(backend_device->to_sink(), arena);
CHECK(backend_sink != NULL);

Pump pump(buffer_factory, mock_source, NULL, *backend_sink, FrameDuration, SampleSpecs, Pump::ModeOneshot);
CHECK(pump.is_valid());
CHECK(pump.run());
}

source_config.sample_spec.set_sample_rate(SampleRate * 2);

IDevice * backend_device = backend.open_device(DeviceType_Source, DriverType_File, "wav", file.path(), source_config, arena);
printf("backend: %s\n", backend.name());
fflush(stdout);
if(strcmp(backend.name(), "wav") != 0){


CHECK(backend_device == NULL);
core::ScopedPtr<ISource> backend_source(backend_device->to_source(), arena);
CHECK(backend_source == NULL);
}else
{

}
}
}

}

}
95 changes: 43 additions & 52 deletions src/tests/roc_sndio/test_pump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ const core::nanoseconds_t BufDuration = BufSize * core::Second
core::HeapArena arena;
core::BufferFactory<audio::sample_t> buffer_factory(arena, BufSize);

bool supports_wav(IBackend &backend){
bool supports = false;
core::Array<DriverInfo, MaxDrivers> driver_list;
backend.discover_drivers(driver_list);
for (size_t n = 0; n < driver_list.size(); n++) {
if (strcmp(driver_list[n].name, "wav") == 0) {
supports = true;
break;
}
}
return supports;
}

} // namespace

TEST_GROUP(pump) {
Expand All @@ -62,52 +75,47 @@ TEST_GROUP(pump) {
};

TEST(pump, write_read) {
{

enum { NumSamples = BufSize * 10 };

for(size_t n_backend = 0; n_backend < BackendMap::instance().num_backends(); n_backend++){
test::MockSource mock_source;
mock_source.add(NumSamples);
core::TempFile file("test.wav");


IBackend &backend = BackendMap::instance().nth_backend(n_backend);

if (!supports_wav(backend)) {
continue;
}

printf("Currently on: %s\n", backend.name());
fflush(stdout);

{
IDevice *backend_device = backend.open_device(DeviceType_Sink, DriverType_File, "wav", file.path(), sink_config, arena);
if(backend_device == NULL){
printf("Failing sink test: %s\n", backend.name());
fflush(stdout);
}
else{
printf("Passing sink test: %s\n", backend.name());
fflush(stdout);
core::ScopedPtr<ISink> backend_sink(backend_device->to_sink(), arena);
Pump pump(buffer_factory, mock_source, NULL, *backend_sink, BufDuration, SampleSpecs,
Pump::ModeOneshot);
CHECK(pump.is_valid());
CHECK(pump.run());

CHECK(mock_source.num_returned() >= NumSamples - BufSize);
}
CHECK(backend_device != NULL);
printf("Passing sink test: %s\n", backend.name());
fflush(stdout);
core::ScopedPtr<ISink> backend_sink(backend_device->to_sink(), arena);
CHECK(backend_sink != NULL);
Pump pump(buffer_factory, mock_source, NULL, *backend_sink, BufDuration, SampleSpecs,
Pump::ModeOneshot);
CHECK(pump.is_valid());
CHECK(pump.run());

CHECK(mock_source.num_returned() >= NumSamples - BufSize);
}

printf("File path: %s\n", file.path());
fflush(stdout);

IDevice *backend_device = backend.open_device(DeviceType_Source, DriverType_File, "wav", file.path(), source_config, arena);
if(backend_device == NULL){
printf("Failing source test: %s\n", backend.name());
fflush(stdout);
continue;
}
CHECK(backend_device != NULL);

printf("Passing source test: %s\n\n", backend.name());

core::ScopedPtr<ISource> backend_source(backend_device->to_source(), arena);

CHECK(backend_source != NULL);
test::MockSink mock_writer;

Pump pump(buffer_factory,
Expand All @@ -123,7 +131,6 @@ TEST(pump, write_read) {
mock_writer.check(0, mock_source.num_returned());
}
}
} // namespace roc

TEST(pump, write_overwrite_read) {
enum { NumSamples = BufSize * 10 };
Expand All @@ -134,20 +141,16 @@ TEST(pump, write_overwrite_read) {

core::TempFile file("test.wav");
IBackend& backend = BackendMap::instance().nth_backend(n_backend);
// printf("Currently on: %s\n", backend.name());
// fflush(stdout);

if (!supports_wav(backend)) {
continue;
}

{
IDevice* backend_device = backend.open_device(DeviceType_Sink, DriverType_File, "wav", file.path(), sink_config, arena);
if (backend_device == NULL) {
// printf("Failing sink test: %s\n", backend.name());
// fflush(stdout);
continue;
}

// printf("Passing sink test: %s\n", backend.name());
// fflush(stdout);
CHECK(backend_device != NULL);
core::ScopedPtr<ISink> backend_sink(backend_device->to_sink(), arena);
CHECK(backend_sink != NULL);
Pump pump(buffer_factory, mock_source, NULL, *backend_sink, BufDuration, SampleSpecs, Pump::ModeOneshot);
CHECK(pump.is_valid());
CHECK(pump.run());
Expand All @@ -160,15 +163,9 @@ TEST(pump, write_overwrite_read) {

{
IDevice* backend_device = backend.open_device(DeviceType_Sink, DriverType_File, "wav", file.path(), sink_config, arena);
if (backend_device == NULL) {
printf("Failing sink test (overwrite): %s\n", backend.name());
fflush(stdout);
continue;
}

// printf("Passing sink test (overwrite): %s\n", backend.name());
// fflush(stdout);
CHECK(backend_device != NULL);
core::ScopedPtr<ISink> backend_sink(backend_device->to_sink(), arena);
CHECK(backend_sink != NULL);
Pump pump(buffer_factory, mock_source, NULL, *backend_sink, BufDuration, SampleSpecs, Pump::ModeOneshot);
CHECK(pump.is_valid());
CHECK(pump.run());
Expand All @@ -178,15 +175,9 @@ TEST(pump, write_overwrite_read) {
CHECK(num_returned1 >= NumSamples - BufSize);

IDevice* backend_device = backend.open_device(DeviceType_Source, DriverType_File, "wav", file.path(), source_config, arena);
if (backend_device == NULL) {
// printf("Failing source test: %s\n", backend.name());
// fflush(stdout);
continue;
}

// printf("Passing source test: %s\n\n", backend.name());

CHECK(backend_device != NULL);
core::ScopedPtr<ISource> backend_source(backend_device->to_source(), arena);
CHECK(backend_source != NULL);

test::MockSink mock_writer;

Expand Down

0 comments on commit f139b8c

Please sign in to comment.