Skip to content

Commit

Permalink
Add --list-drivers -L support
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Dwyer committed Sep 22, 2019
1 parent 877defe commit 8d2c1a5
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/modules/roc_fec/target_openfec/roc_fec/of_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ void OFDecoder::reset_session_() {

if (OF_STATUS_OK
!= of_set_callback_functions(
of_sess_, source_cb_,
// OpenFEC doesn't repair fec-packets in case of Reed-Solomon FEC
// and prints curses to the console if we give it the callback for that
codec_id_ == OF_CODEC_REED_SOLOMON_GF_2_M_STABLE ? NULL : repair_cb_,
(void*)this)) {
of_sess_, source_cb_,
// OpenFEC doesn't repair fec-packets in case of Reed-Solomon FEC
// and prints curses to the console if we give it the callback for that
codec_id_ == OF_CODEC_REED_SOLOMON_GF_2_M_STABLE ? NULL : repair_cb_,
(void*)this)) {
roc_panic("of decoder: of_set_callback_functions() failed");
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/modules/roc_sndio/backend_dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ISink* BackendDispatcher::open_sink(core::IAllocator& allocator,
const char* driver,
const char* output,
const Config& config) {
IBackend* backend = select_backend_(driver, output, IBackend::ProbeSink);
IBackend* backend = select_backend_(driver, output, IBackend::FilterSink);
if (!backend) {
return NULL;
}
Expand All @@ -53,7 +53,7 @@ ISource* BackendDispatcher::open_source(core::IAllocator& allocator,
const char* driver,
const char* input,
const Config& config) {
IBackend* backend = select_backend_(driver, input, IBackend::ProbeSource);
IBackend* backend = select_backend_(driver, input, IBackend::FilterSource);
if (!backend) {
return NULL;
}
Expand All @@ -62,12 +62,13 @@ ISource* BackendDispatcher::open_source(core::IAllocator& allocator,

IBackend*
BackendDispatcher::select_backend_(const char* driver, const char* inout, int flags) {
if (IBackend* backend = probe_backends_(driver, inout, flags | IBackend::ProbeFile)) {
if (IBackend* backend =
probe_backends_(driver, inout, flags | IBackend::FilterFile)) {
return backend;
}

if (IBackend* backend = probe_backends_(
driver, inout, flags | IBackend::ProbeFile | IBackend::ProbeDevice)) {
driver, inout, flags | IBackend::FilterFile | IBackend::FilterDevice)) {
return backend;
}

Expand All @@ -90,5 +91,12 @@ void BackendDispatcher::add_backend_(IBackend& backend) {
backends_[n_backends_++] = &backend;
}

void BackendDispatcher::get_drivers(core::Array<DriverInfo>& arr,
IBackend::FilterFlags driver_type) {
for (size_t n = 0; n < n_backends_; n++) {
backends_[n]->get_drivers(arr, driver_type);
}
}

} // namespace sndio
} // namespace roc
4 changes: 4 additions & 0 deletions src/modules/roc_sndio/backend_dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ifndef ROC_SNDIO_BACKEND_DISPATCHER_H_
#define ROC_SNDIO_BACKEND_DISPATCHER_H_

#include "roc_core/array.h"
#include "roc_core/iallocator.h"
#include "roc_core/noncopyable.h"
#include "roc_core/shared_ptr.h"
Expand Down Expand Up @@ -46,6 +47,9 @@ class BackendDispatcher : public core::NonCopyable<> {
const char* input,
const Config& config);

//! Append supported drivers from all registered backends to Array
void get_drivers(core::Array<DriverInfo>& arr, IBackend::FilterFlags driver_type);

private:
friend class core::Singleton<BackendDispatcher>;

Expand Down
48 changes: 48 additions & 0 deletions src/modules/roc_sndio/driver_info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2019 Roc authors
*
* This Sink 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 <string.h>

#include "roc_sndio/driver_info.h"

namespace roc {
namespace sndio {

DriverInfo::DriverInfo() {
for (size_t x = 0; x < MaxSize; x++) {
name[x] = '\0';
}
}

DriverInfo::DriverInfo(const char* driver_name) {
size_t length = strlen(driver_name);
if (length > MaxSize - 1) {
length = MaxSize - 1;
}
strncpy(name, driver_name, MaxSize);
name[length] = '\0';
}

bool add_driver_uniq(core::Array<DriverInfo>& arr, const char* driver_name) {
roc_panic_if(driver_name == NULL);
for (size_t n = 0; n < arr.size(); n++) {
if (strcmp(driver_name, arr[n].name) == 0) {
return false;
}
}
if (arr.grow(arr.size() + 1)) {
DriverInfo new_driver(driver_name);
arr.push_back(new_driver);
return true;
} else {
return false;
}
}

} // namespace sndio
} // namespace roc
42 changes: 42 additions & 0 deletions src/modules/roc_sndio/driver_info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2019 Roc 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/.
*/

//! @file roc_sndio/driver_info.h
//! @brief Driver info interface.

#ifndef ROC_SNDIO_DRIVER_INFO_H_
#define ROC_SNDIO_DRIVER_INFO_H_

#include "roc_core/array.h"

namespace roc {
namespace sndio {

//! Driver info interface.
struct DriverInfo {
DriverInfo();

//! Max size of string
enum { MaxSize = 20 };

//! Parameterized Constructor initializes name, assumes driver_name is terminated with
//! null char
explicit DriverInfo(const char* driver_name);

//! Placeholder for the driver name
char name[MaxSize];
};

//! Append driver to array and ensure uniqueness, returns false if unable to add item,
//! or if unable to allocate space for item
bool add_driver_uniq(core::Array<DriverInfo>& arr, const char* driver_name);

} // namespace sndio
} // namespace roc

#endif // ROC_SNDIO_DRIVER_INFO_H_
15 changes: 10 additions & 5 deletions src/modules/roc_sndio/ibackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
#ifndef ROC_SNDIO_IBACKEND_H_
#define ROC_SNDIO_IBACKEND_H_

#include "roc_core/array.h"
#include "roc_core/iallocator.h"
#include "roc_core/shared_ptr.h"
#include "roc_sndio/config.h"
#include "roc_sndio/driver_info.h"
#include "roc_sndio/isink.h"
#include "roc_sndio/isource.h"

Expand All @@ -27,18 +29,18 @@ class IBackend {
virtual ~IBackend();

//! Probing flags.
enum ProbeFlags {
enum FilterFlags {
//! Input or output may be a sink.
ProbeSink = (1 << 0),
FilterSink = (1 << 0),

//! Input or output may be a source.
ProbeSource = (1 << 1),
FilterSource = (1 << 1),

//! Input or output may be a file.
ProbeFile = (1 << 2),
FilterFile = (1 << 2),

//! Input or output may be a device.
ProbeDevice = (1 << 3)
FilterDevice = (1 << 3)
};

//! Check whether the backend can handle given input or output.
Expand All @@ -55,6 +57,9 @@ class IBackend {
const char* driver,
const char* input,
const Config& config) = 0;

//! Append supported dirvers to Array
virtual void get_drivers(core::Array<DriverInfo>& arr, FilterFlags driver_type) = 0;
};

} // namespace sndio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "roc_core/log.h"
#include "roc_core/stddefs.h"
#include "roc_core/unique_ptr.h"
#include "roc_sndio/driver_info.h"
#include "roc_sndio/pulseaudio_backend.h"
#include "roc_sndio/pulseaudio_sink.h"

Expand All @@ -22,11 +23,11 @@ PulseaudioBackend::PulseaudioBackend() {
}

bool PulseaudioBackend::probe(const char* driver, const char*, int flags) {
if ((flags & ProbeDevice) == 0) {
if ((flags & FilterDevice) == 0) {
return false;
}

if ((flags & ProbeSink) == 0) {
if ((flags & FilterSink) == 0) {
return false;
}

Expand Down Expand Up @@ -57,5 +58,13 @@ ISource* PulseaudioBackend::open_source(core::IAllocator&,
return NULL;
}

void PulseaudioBackend::get_drivers(core::Array<DriverInfo>& arr,
FilterFlags driver_type) {
const char* format_name = "pulseaudio";
if (driver_type & FilterDevice) {
add_driver_uniq(arr, format_name);
}
}

} // namespace sndio
} // namespace roc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ifndef ROC_SNDIO_PULSEAUDIO_BACKEND_H_
#define ROC_SNDIO_PULSEAUDIO_BACKEND_H_

#include "roc_core/array.h"
#include "roc_core/noncopyable.h"
#include "roc_core/singleton.h"
#include "roc_sndio/ibackend.h"
Expand Down Expand Up @@ -42,6 +43,9 @@ class PulseaudioBackend : public IBackend, core::NonCopyable<> {
const char* input,
const Config& config);

//! Append supported drivers to Array
virtual void get_drivers(core::Array<DriverInfo>& arr, FilterFlags driver_type);

private:
friend class core::Singleton<PulseaudioBackend>;

Expand Down
26 changes: 24 additions & 2 deletions src/modules/roc_sndio/target_sox/roc_sndio/sox_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ bool SoxBackend::probe(const char* driver, const char* inout, int flags) {
}

if (handler->flags & SOX_FILE_DEVICE) {
if ((flags & ProbeDevice) == 0) {
if ((flags & FilterDevice) == 0) {
return false;
}
} else {
if ((flags & ProbeFile) == 0) {
if ((flags & FilterFile) == 0) {
return false;
}
}
Expand Down Expand Up @@ -196,5 +196,27 @@ ISource* SoxBackend::open_source(core::IAllocator& allocator,
return source.release();
}

void SoxBackend::get_drivers(core::Array<DriverInfo>& arr, FilterFlags driver_type) {
const sox_format_tab_t* formats = sox_get_format_fns();
char const* const* format_names;
for (size_t n = 0; formats[n].fn; n++) {
sox_format_handler_t const* handler = formats[n].fn();
bool match = false;
if (driver_type & FilterFile) {
match = !(handler->flags & SOX_FILE_DEVICE);
} else if (driver_type & FilterDevice) {
match = ((handler->flags & SOX_FILE_DEVICE)
&& !(handler->flags & SOX_FILE_PHONY));
}
if (match) {
for (format_names = handler->names; *format_names; ++format_names) {
if (!strchr(*format_names, '/')) {
add_driver_uniq(arr, *format_names);
}
}
}
}
}

} // namespace sndio
} // namespace roc
4 changes: 4 additions & 0 deletions src/modules/roc_sndio/target_sox/roc_sndio/sox_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class SoxBackend : public IBackend, core::NonCopyable<> {
const char* input,
const Config& config);

//! Append supported dirvers to Array, replicates the behavior of
//! display_supported_formats() from sox.c
virtual void get_drivers(core::Array<DriverInfo>& arr, FilterFlags driver_type);

private:
friend class core::Singleton<SoxBackend>;

Expand Down
2 changes: 2 additions & 0 deletions src/tools/roc_recv/cmdline.ggo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ usage "roc-recv OPTIONS"

section "Options"

option "list-drivers" L "list all supported audio drivers" optional

option "verbose" v "Increase verbosity level (may be used multiple times)"
multiple optional

Expand Down
23 changes: 22 additions & 1 deletion src/tools/roc_recv/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include "roc_audio/resampler_profile.h"
#include "roc_core/array.h"
#include "roc_core/crash.h"
#include "roc_core/heap_allocator.h"
#include "roc_core/log.h"
Expand All @@ -17,6 +18,7 @@
#include "roc_pipeline/parse_port.h"
#include "roc_pipeline/receiver.h"
#include "roc_sndio/backend_dispatcher.h"
#include "roc_sndio/driver_info.h"
#include "roc_sndio/pump.h"

#include "roc_recv/cmdline.h"
Expand All @@ -39,6 +41,26 @@ int main(int argc, char** argv) {
core::Logger::instance().set_level(
LogLevel(core::DefaultLogLevel + args.verbose_given));

core::HeapAllocator allocator;

if (args.list_drivers_given) {
core::Array<sndio::DriverInfo> device_driver_list(allocator);
core::Array<sndio::DriverInfo> file_driver_list(allocator);
sndio::BackendDispatcher::instance().get_drivers(device_driver_list,
sndio::IBackend::FilterDevice);
sndio::BackendDispatcher::instance().get_drivers(file_driver_list,
sndio::IBackend::FilterFile);
printf("%s\n", "device drivers:");
for (size_t n = 0; n < device_driver_list.size(); n++) {
printf(" %s\n", device_driver_list[n].name);
}
printf("\n%s\n", "file drivers:");
for (size_t m = 0; m < file_driver_list.size(); m++) {
printf(" %s\n", file_driver_list[m].name);
}
return 0;
}

pipeline::ReceiverConfig config;

size_t max_packet_size = 2048;
Expand Down Expand Up @@ -183,7 +205,6 @@ int main(int argc, char** argv) {
config.common.poisoning = args.poisoning_flag;
config.common.beeping = args.beeping_flag;

core::HeapAllocator allocator;
core::BufferPool<uint8_t> byte_buffer_pool(allocator, max_packet_size,
args.poisoning_flag);
core::BufferPool<audio::sample_t> sample_buffer_pool(
Expand Down
2 changes: 2 additions & 0 deletions src/tools/roc_send/cmdline.ggo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ usage "roc-send OPTIONS"

section "Options"

option "list-drivers" L "List all supported audio drivers" optional

option "verbose" v "Increase verbosity level (may be used multiple times)"
multiple optional

Expand Down
Loading

0 comments on commit 8d2c1a5

Please sign in to comment.