From ff5ca386e686299fb567f12f339768b326937a84 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Tue, 24 Oct 2023 11:24:21 +0200 Subject: [PATCH] audio codec: remove deprecated flag + new API fn Get the info from audio lavc in textual form instead with a new API to querying additional info about codec for user. The new API will allow adding additional useful information in help, eg. encoders present. this updates the commit a0dd6b9b85 --- src/audio/codec.cpp | 67 ++++++++++++++++++++++-------------- src/audio/codec.h | 14 ++++---- src/audio/codec/libavcodec.c | 13 +++++++ src/host.cpp | 21 +++++++++-- 4 files changed, 81 insertions(+), 34 deletions(-) diff --git a/src/audio/codec.cpp b/src/audio/codec.cpp index 7dd0a1ce5..ebb7c7803 100644 --- a/src/audio/codec.cpp +++ b/src/audio/codec.cpp @@ -45,6 +45,7 @@ #include "audio/utils.h" #include "debug.h" #include "lib_common.h" +#include "utils/macros.h" #include "utils/misc.h" #include @@ -53,26 +54,27 @@ static constexpr const char *MOD_NAME = "[acodec] "; +using std::get; using std::hash; using std::max; -using std::pair; using std::unordered_map; using std::string; +using std::tuple; using std::vector; static const unordered_map> audio_codec_info = { - {AC_NONE, { "(none)", 0, 0 } }, - { AC_PCM, { "PCM", 0x0001, 0 } }, - { AC_ALAW, { "A-law", 0x0006, 0 } }, - { AC_MULAW, { "u-law", 0x0007, 0 } }, - { AC_SPEEX, { "speex", 0xA109, AC_FLAG_DEPRECATED }}, + {AC_NONE, { "(none)", 0 } }, + { AC_PCM, { "PCM", 0x0001 } }, + { AC_ALAW, { "A-law", 0x0006 } }, + { AC_MULAW, { "u-law", 0x0007 } }, + { AC_SPEEX, { "speex", 0xA109 } }, { AC_OPUS, // fcc is "Opus", the TwoCC isn't defined - { "Opus", 0x7375704F, 0 } }, - { AC_G722, { "G.722", 0x028F, 0 } }, - { AC_MP3, { "MP3", 0x0055, 0 } }, - { AC_AAC, { "AAC", 0x00FF, 0 } }, - { AC_FLAC, { "FLAC", 0xF1AC, 0 } }, + { "Opus", 0x7375704F }}, + { AC_G722, { "G.722", 0x028F } }, + { AC_MP3, { "MP3", 0x0055 } }, + { AC_AAC, { "AAC", 0x00FF } }, + { AC_FLAC, { "FLAC", 0xF1AC } }, }; static struct audio_codec_state *audio_codec_init_real(const char *audio_codec_cfg, @@ -87,8 +89,25 @@ struct audio_codec_state { int bitrate; }; -vector> get_audio_codec_list() { - vector> ret; +static string +get_codec_desc(struct audio_codec_state *st) +{ + if (st->funcs->get_codec_info == nullptr) { + return {}; + } + char buf[STR_LEN]; + const char *ret = + st->funcs->get_codec_info(st->state[0], sizeof buf, buf); + if (ret == nullptr) { + return {}; + } + return ret; +} + +vector> +get_audio_codec_list() +{ + vector> ret; for (auto const &it : audio_codec_info) { if(it.first != AC_NONE) { @@ -96,11 +115,13 @@ vector> get_audio_codec_list() { audio_codec_init_real(get_name_to_audio_codec(it.first), AUDIO_CODER, true); bool available = false; + string desc; if(st){ available = true; + desc = get_codec_desc(st); audio_codec_done(st); } - ret.emplace_back(it.second, available); + ret.emplace_back(it.second, available, desc); } } @@ -120,23 +141,19 @@ void list_audio_codecs(void) { col() << "\t" << SBOLD("bitrate ") << " - codec bitrate " << SBOLD("per channel") << " (with optional k/M suffix)\n"; col() << "\n"; - bool deprecated_present = false; printf("Supported audio codecs:\n"); for (auto const &it : get_audio_codec_list()) { string notes; - if (!it.second) { + if (!get<1>(it)) { notes += " " TRED("unavailable"); + } else { + notes = get<2>(it); } - if ((it.first.flags & AC_FLAG_DEPRECATED) != 0) { - notes += " " TYELLOW("deprecated"); - deprecated_present = true; - } - col() << SBOLD("\t" << it.first.name) - << (notes.empty() ? "" : " -") << notes << "\n"; - } - if (deprecated_present) { - col() << "\nCodecs marked as \"deprecated\" may be removed in future.\n"; + col() << SBOLD("\t" << get<0>(it).name) + << (notes.empty() ? "" : " - ") << notes << "\n"; } + col() + << "\nCodecs marked as \"deprecated\" may be removed in future.\n"; } struct audio_codec_state *audio_codec_init(audio_codec_t audio_codec, diff --git a/src/audio/codec.h b/src/audio/codec.h index 5ad4b573b..44568d48b 100644 --- a/src/audio/codec.h +++ b/src/audio/codec.h @@ -40,7 +40,7 @@ #include "audio/types.h" -#define AUDIO_COMPRESS_ABI_VERSION 3 +#define AUDIO_COMPRESS_ABI_VERSION 4 typedef enum { AUDIO_CODER, @@ -53,23 +53,22 @@ struct audio_compress_info { audio_channel *(*compress)(void *, audio_channel *); audio_channel *(*decompress)(void *, audio_channel *); const int *(*get_samplerates)(void *); + /// returns additional text data to be printed in help + const char *(*get_codec_info)(void *, size_t buflen, char *buffer); void (*done)(void *); }; -enum audio_codec_flag { - AC_FLAG_DEPRECATED = 1 << 0, -}; - typedef struct { const char *name; /** @var tag * @brief TwoCC if defined, otherwise we define our tag */ uint32_t tag; - int flags; ///< enum audio_codec_flag } audio_codec_info_t; #ifdef __cplusplus +#include +#include struct audio_codec_state; struct audio_codec_state *audio_codec_init(audio_codec_t audio_codec, audio_codec_direction_t); @@ -81,7 +80,8 @@ audio_frame2 audio_codec_decompress(struct audio_codec_state *, audio_frame2 *); const int *audio_codec_get_supported_samplerates(struct audio_codec_state *); void audio_codec_done(struct audio_codec_state *); -std::vector> get_audio_codec_list(); +std::vector> +get_audio_codec_list(); #endif #ifdef __cplusplus diff --git a/src/audio/codec/libavcodec.c b/src/audio/codec/libavcodec.c index a33109f06..e4d218926 100644 --- a/src/audio/codec/libavcodec.c +++ b/src/audio/codec/libavcodec.c @@ -56,6 +56,7 @@ #include "audio/codec.h" #include "audio/utils.h" #include "libavcodec/lavc_common.h" +#include "utils/color_out.h" #include "utils/text.h" #define MAGIC 0xb135ca11 @@ -725,12 +726,24 @@ static void libavcodec_done(void *state) static const audio_codec_t supported_codecs[] = { AC_ALAW, AC_MULAW, AC_SPEEX, AC_OPUS, AC_G722, AC_FLAC, AC_MP3, AC_AAC, AC_NONE }; +static const char * +libavcodec_get_codec_desc(void *state, size_t buflen, char *buffer) +{ + (void) buflen, (void) buffer; + struct libavcodec_codec_state *s = state; + if (s->output_channel.codec == AC_SPEEX) { + return TYELLOW("deprecated"); + } + return NULL; +} + static const struct audio_compress_info libavcodec_audio_codec = { supported_codecs, libavcodec_init, libavcodec_compress, libavcodec_decompress, libavcodec_get_sample_rates, + libavcodec_get_codec_desc, libavcodec_done }; diff --git a/src/host.cpp b/src/host.cpp index 86cf6782f..b5d6dc38a 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -110,7 +110,24 @@ extern "C" { #include #endif -using namespace std; +using std::array; +using std::cout; +using std::endl; +using std::get; +using std::ifstream; +using std::left; +using std::list; +using std::make_tuple; +using std::max; +using std::mutex; +using std::pair; +using std::setw; +using std::string; +using std::thread; +using std::to_string; +using std::tuple; +using std::unordered_map; +using std::unique_lock; unsigned int audio_capture_channels = 0; unsigned int audio_capture_bps = 0; @@ -727,7 +744,7 @@ void print_capabilities(const char *cfg) } auto codecs = get_audio_codec_list(); for(const auto& codec : codecs){ - class_mod_map[LIBRARY_CLASS_AUDIO_COMPRESS].emplace(codec.first.name, nullptr); + class_mod_map[LIBRARY_CLASS_AUDIO_COMPRESS].emplace(get<0>(codec).name, nullptr); } if(conf == "noprobe"){