From 1d8082132a29cca11aa1373907e999fe6236f606 Mon Sep 17 00:00:00 2001 From: Victor Gaydov Date: Thu, 16 Nov 2023 19:04:38 +0400 Subject: [PATCH] gh-608 Improve --print-supported --- .../roc_address/print_supported.cpp | 11 +- .../roc_address/print_supported.h | 3 +- .../roc_audio/print_supported.cpp | 104 ++++++++++++++++++ .../roc_audio/print_supported.h | 26 +++++ .../roc_sndio/print_supported.cpp | 2 +- src/tools/roc_recv/main.cpp | 5 +- src/tools/roc_send/main.cpp | 3 +- 7 files changed, 141 insertions(+), 13 deletions(-) create mode 100644 src/internal_modules/roc_audio/print_supported.cpp create mode 100644 src/internal_modules/roc_audio/print_supported.h diff --git a/src/internal_modules/roc_address/print_supported.cpp b/src/internal_modules/roc_address/print_supported.cpp index 564acaca73..d7a32cf4aa 100644 --- a/src/internal_modules/roc_address/print_supported.cpp +++ b/src/internal_modules/roc_address/print_supported.cpp @@ -28,7 +28,7 @@ void print_interface_protos(core::Printer& prn, size_t size = 0; - prn.writef(" %s:", interface_to_str(interface)); + prn.writef(" %-12s ", interface_to_str(interface)); while (size < LineSize) { size += prn.writef(" %s%s%s", "", str, "://"); @@ -45,24 +45,25 @@ void print_interface_protos(core::Printer& prn, } // namespace -bool print_supported(ProtocolMap& protocol_map, core::IArena& arena) { +bool print_supported(core::IArena& arena) { core::Printer prn; core::Array interface_array(arena); core::StringList list(arena); - if (!protocol_map.get_supported_interfaces(interface_array)) { + if (!address::ProtocolMap::instance().get_supported_interfaces(interface_array)) { roc_log(LogError, "can't retrieve interface array"); return false; } for (size_t n_interface = 0; n_interface < interface_array.size(); n_interface++) { - if (!protocol_map.get_supported_protocols(interface_array[n_interface], list)) { + if (!address::ProtocolMap::instance().get_supported_protocols( + interface_array[n_interface], list)) { roc_log(LogError, "can't retrieve protocols list"); return false; } if (n_interface == 0) { - prn.writef("\nsupported network protocols:\n"); + prn.writef("supported schemes for network endpoints:\n"); } print_interface_protos(prn, interface_array[n_interface], list); diff --git a/src/internal_modules/roc_address/print_supported.h b/src/internal_modules/roc_address/print_supported.h index 6503ae56ef..b4f884201b 100644 --- a/src/internal_modules/roc_address/print_supported.h +++ b/src/internal_modules/roc_address/print_supported.h @@ -12,7 +12,6 @@ #ifndef ROC_ADDRESS_PRINT_SUPPORTED_H_ #define ROC_ADDRESS_PRINT_SUPPORTED_H_ -#include "roc_address/protocol_map.h" #include "roc_core/attributes.h" #include "roc_core/iarena.h" @@ -20,7 +19,7 @@ namespace roc { namespace address { //! Print supported interfaces and protocols. -ROC_ATTR_NODISCARD bool print_supported(ProtocolMap&, core::IArena&); +ROC_ATTR_NODISCARD bool print_supported(core::IArena&); } // namespace address } // namespace roc diff --git a/src/internal_modules/roc_audio/print_supported.cpp b/src/internal_modules/roc_audio/print_supported.cpp new file mode 100644 index 0000000000..a65f370d21 --- /dev/null +++ b/src/internal_modules/roc_audio/print_supported.cpp @@ -0,0 +1,104 @@ +/* + * 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 "roc_audio/print_supported.h" +#include "roc_audio/channel_defs.h" +#include "roc_audio/channel_tables.h" +#include "roc_audio/pcm_format.h" +#include "roc_core/macro_helpers.h" +#include "roc_core/printer.h" + +namespace roc { +namespace audio { + +namespace { + +void print_pcm_codes(core::Printer& prn) { + PcmTraits prev_traits, curr_traits; + + for (int code = 0; code < PcmCode_Max; code++) { + for (int endian = 0; endian < PcmEndian_Max; endian++) { + PcmFormat fmt; + fmt.code = (PcmCode)code; + fmt.endian = (PcmEndian)endian; + + curr_traits = pcm_format_traits(fmt); + + if (prev_traits.bit_depth != curr_traits.bit_depth + || prev_traits.bit_width != curr_traits.bit_width) { + if (curr_traits.bit_width % 8 == 0) { + prn.writef("\n %2d bit (%d byte) ", (int)curr_traits.bit_depth, + (int)curr_traits.bit_width / 8); + } else { + prn.writef("\n %d bit (%.2f byte) ", (int)curr_traits.bit_depth, + (double)curr_traits.bit_width / 8.); + } + } + + prev_traits = curr_traits; + + prn.writef(" %s", pcm_format_to_str(fmt)); + } + } +} + +void print_channel_names(core::Printer& prn) { + prn.writef(" front FL FR FC\n"); + prn.writef(" side SL SR\n"); + prn.writef(" back BL BR BC\n"); + prn.writef(" top front TFL TFR\n"); + prn.writef(" top mid TML TMR\n"); + prn.writef(" top back TBL TBR\n"); + prn.writef(" low freq LFE\n"); +} + +void print_channel_mask(core::Printer& prn, ChannelMask ch_mask) { + prn.writef(" %-13s (", channel_mask_to_str(ch_mask)); + + bool first = true; + + for (int ch = 0; ch < ChanPos_Max; ch++) { + if (ch_mask & (1 << ch)) { + if (!first) { + prn.writef(" "); + } + first = false; + prn.writef("%s", channel_pos_to_str((ChannelPosition)ch)); + } + } + + prn.writef(")\n"); +} + +} // namespace + +bool print_supported() { + core::Printer prn; + + prn.writef("\nsupported formats for audio devices:\n"); + prn.writef(" pcm\n"); + + prn.writef("\nsupported formats for network packets:\n"); + prn.writef(" pcm\n"); + + prn.writef("\nsupported pcm codes:"); + print_pcm_codes(prn); + + prn.writef("\n\nsupported channel names:\n"); + print_channel_names(prn); + + prn.writef("\npre-defined channel masks:\n"); + for (size_t i = 0; i < ROC_ARRAY_SIZE(ChanMaskNames); i++) { + print_channel_mask(prn, ChanMaskNames[i].mask); + } + + return true; +} + +} // namespace audio +} // namespace roc diff --git a/src/internal_modules/roc_audio/print_supported.h b/src/internal_modules/roc_audio/print_supported.h new file mode 100644 index 0000000000..c8b069fd44 --- /dev/null +++ b/src/internal_modules/roc_audio/print_supported.h @@ -0,0 +1,26 @@ +/* + * 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/. + */ + +//! @file roc_audio/print_supported.h +//! @brief Print supported encodings. + +#ifndef ROC_AUDIO_PRINT_SUPPORTED_H_ +#define ROC_AUDIO_PRINT_SUPPORTED_H_ + +#include "roc_core/attributes.h" + +namespace roc { +namespace audio { + +//! Print supported encodings. +ROC_ATTR_NODISCARD bool print_supported(); + +} // namespace audio +} // namespace roc + +#endif // ROC_AUDIO_PRINT_SUPPORTED_H_ diff --git a/src/internal_modules/roc_sndio/print_supported.cpp b/src/internal_modules/roc_sndio/print_supported.cpp index f397000902..b74e88a1a0 100644 --- a/src/internal_modules/roc_sndio/print_supported.cpp +++ b/src/internal_modules/roc_sndio/print_supported.cpp @@ -52,7 +52,7 @@ bool print_supported(BackendDispatcher& backend_dispatcher, core::IArena& arena) return false; } - prn.writef("supported schemes for audio devices and files:\n"); + prn.writef("\nsupported schemes for audio devices and files:\n"); print_string_list(prn, list, "", "://"); if (!backend_dispatcher.get_supported_formats(list)) { diff --git a/src/tools/roc_recv/main.cpp b/src/tools/roc_recv/main.cpp index 2869d2ff89..628649aedb 100644 --- a/src/tools/roc_recv/main.cpp +++ b/src/tools/roc_recv/main.cpp @@ -93,12 +93,11 @@ int main(int argc, char** argv) { sndio::BackendDispatcher backend_dispatcher(context.arena()); if (args.list_supported_given) { - if (!sndio::print_supported(backend_dispatcher, context.arena())) { + if (!address::print_supported(context.arena())) { return 1; } - if (!address::print_supported(address::ProtocolMap::instance(), - context.arena())) { + if (!sndio::print_supported(backend_dispatcher, context.arena())) { return 1; } diff --git a/src/tools/roc_send/main.cpp b/src/tools/roc_send/main.cpp index f272b71e0c..f3e16a3b21 100644 --- a/src/tools/roc_send/main.cpp +++ b/src/tools/roc_send/main.cpp @@ -95,8 +95,7 @@ int main(int argc, char** argv) { return 1; } - if (!address::print_supported(address::ProtocolMap::instance(), - context.arena())) { + if (!address::print_supported(context.arena())) { return 1; }