From 78fbba93af760697a9c5167f1db539f6cc23cac6 Mon Sep 17 00:00:00 2001 From: Davide Beatrici Date: Fri, 2 Aug 2024 03:59:07 +0200 Subject: [PATCH] Refactor Node API --- include/crossaudio/Engine.h | 4 +-- include/crossaudio/Node.h | 20 ++++++++++++ src/Backend.h | 7 ++--- src/CMakeLists.txt | 2 ++ src/Engine.c | 8 ++--- src/Node.c | 43 ++++++++++++++++++++++++++ src/Node.h | 24 ++++++++++++++ src/backends/ALSA/ALSA.cpp | 35 +++++---------------- src/backends/ALSA/ALSA.hpp | 5 ++- src/backends/OSS/OSS.cpp | 33 +++++--------------- src/backends/OSS/OSS.hpp | 5 ++- src/backends/PipeWire/PipeWire.cpp | 32 ++++--------------- src/backends/PipeWire/PipeWire.hpp | 5 ++- src/backends/PulseAudio/PulseAudio.cpp | 32 ++++--------------- src/backends/PulseAudio/PulseAudio.hpp | 5 ++- src/backends/Sndio/Sndio.cpp | 7 +---- src/backends/Sndio/Sndio.hpp | 2 -- src/backends/WASAPI/WASAPI.cpp | 30 +++--------------- src/backends/WASAPI/WASAPI.hpp | 5 ++- src/tests/TestEnum/Enum.c | 9 +++--- 20 files changed, 142 insertions(+), 171 deletions(-) create mode 100644 src/Node.c create mode 100644 src/Node.h diff --git a/include/crossaudio/Engine.h b/include/crossaudio/Engine.h index 63fcebf..a5afdb3 100644 --- a/include/crossaudio/Engine.h +++ b/include/crossaudio/Engine.h @@ -25,9 +25,7 @@ CROSSAUDIO_EXPORT const char *CrossAudio_engineNameGet(struct CrossAudio_Engine CROSSAUDIO_EXPORT enum CrossAudio_ErrorCode CrossAudio_engineNameSet(struct CrossAudio_Engine *engine, const char *name); -CROSSAUDIO_EXPORT struct CrossAudio_Node *CrossAudio_engineNodesGet(struct CrossAudio_Engine *engine); -CROSSAUDIO_EXPORT enum CrossAudio_ErrorCode CrossAudio_engineNodesFree(struct CrossAudio_Engine *engine, - struct CrossAudio_Node *nodes); +CROSSAUDIO_EXPORT struct CrossAudio_Nodes *CrossAudio_engineNodesGet(struct CrossAudio_Engine *engine); #ifdef __cplusplus } diff --git a/include/crossaudio/Node.h b/include/crossaudio/Node.h index 5809b48..caded99 100644 --- a/include/crossaudio/Node.h +++ b/include/crossaudio/Node.h @@ -7,6 +7,10 @@ #define CROSSAUDIO_NODE_H #include "Direction.h" +#include "ErrorCode.h" +#include "Macros.h" + +#include struct CrossAudio_Node { char *id; @@ -14,4 +18,20 @@ struct CrossAudio_Node { enum CrossAudio_Direction direction; }; +struct CrossAudio_Nodes { + size_t count; + struct CrossAudio_Node *items; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +CROSSAUDIO_EXPORT enum CrossAudio_ErrorCode CrossAudio_nodeFree(struct CrossAudio_Node *node); +CROSSAUDIO_EXPORT enum CrossAudio_ErrorCode CrossAudio_nodesFree(struct CrossAudio_Nodes *nodes); + +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/Backend.h b/src/Backend.h index d59cc1e..628f298 100644 --- a/src/Backend.h +++ b/src/Backend.h @@ -12,7 +12,7 @@ #include -#define GET_BACKEND(name) \ +#define GET_BACKEND(name) \ extern const BE_Impl name##_Impl; \ return &name##_Impl; @@ -21,7 +21,7 @@ typedef enum CrossAudio_ErrorCode ErrorCode; typedef struct CrossAudio_FluxConfig FluxConfig; typedef struct CrossAudio_FluxFeedback FluxFeedback; -typedef struct CrossAudio_Node Node; +typedef struct CrossAudio_Nodes Nodes; typedef struct BE_Engine BE_Engine; typedef struct BE_Flux BE_Flux; @@ -39,8 +39,7 @@ typedef struct BE_Impl { ErrorCode (*engineStop)(BE_Engine *engine); const char *(*engineNameGet)(BE_Engine *engine); ErrorCode (*engineNameSet)(BE_Engine *engine, const char *name); - Node *(*engineNodesGet)(BE_Engine *engine); - ErrorCode (*engineNodesFree)(BE_Engine *engine, Node *nodes); + Nodes *(*engineNodesGet)(BE_Engine *engine); BE_Flux *(*fluxNew)(BE_Engine *engine); ErrorCode (*fluxFree)(BE_Flux *flux); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7e342ec..34d8494 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,6 +38,8 @@ target_sources(crossaudio "Engine.h" "Flux.c" "Flux.h" + "Node.c" + "Node.h" PUBLIC "${INCLUDE_DIR}/crossaudio/Backend.h" "${INCLUDE_DIR}/crossaudio/BitFormat.h" diff --git a/src/Engine.c b/src/Engine.c index 1e8cba9..3de060a 100644 --- a/src/Engine.c +++ b/src/Engine.c @@ -9,7 +9,7 @@ #include -typedef struct CrossAudio_Node Node; +typedef struct CrossAudio_Nodes Nodes; Engine *CrossAudio_engineNew(const Backend backend) { const BE_Impl *impl = backendGetImpl(backend); @@ -55,10 +55,6 @@ ErrorCode CrossAudio_engineNameSet(Engine *engine, const char *name) { return engine->beImpl->engineNameSet(engine->beData, name); } -Node *CrossAudio_engineNodesGet(Engine *engine) { +Nodes *CrossAudio_engineNodesGet(Engine *engine) { return engine->beImpl->engineNodesGet(engine->beData); } - -ErrorCode CrossAudio_engineNodesFree(Engine *engine, Node *nodes) { - return engine->beImpl->engineNodesFree(engine->beData, nodes); -} diff --git a/src/Node.c b/src/Node.c new file mode 100644 index 0000000..4810f0d --- /dev/null +++ b/src/Node.c @@ -0,0 +1,43 @@ +// Copyright 2024 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at . + +#include "Node.h" + +#include + +typedef enum CrossAudio_ErrorCode ErrorCode; + +static void freeNodeInner(Node *node) { + free(node->id); + free(node->name); +} + +Nodes *nodesNew(const size_t count) { + Nodes *nodes = malloc(sizeof(Nodes)); + + nodes->count = count; + nodes->items = calloc(count, sizeof(Node)); + + return nodes; +} + +ErrorCode CrossAudio_nodeFree(Node *node) { + freeNodeInner(node); + + free(node); + + return CROSSAUDIO_EC_OK; +} + +ErrorCode CrossAudio_nodesFree(Nodes *nodes) { + for (size_t i = 0; i < nodes->count; ++i) { + freeNodeInner(&nodes->items[i]); + } + + free(nodes->items); + free(nodes); + + return CROSSAUDIO_EC_OK; +} diff --git a/src/Node.h b/src/Node.h new file mode 100644 index 0000000..89738e2 --- /dev/null +++ b/src/Node.h @@ -0,0 +1,24 @@ +// Copyright 2024 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at . + +#ifndef CROSSAUDIO_SRC_NODE_H +#define CROSSAUDIO_SRC_NODE_H + +#include "crossaudio/Node.h" + +typedef struct CrossAudio_Node Node; +typedef struct CrossAudio_Nodes Nodes; + +#ifdef __cplusplus +extern "C" { +#endif + +Nodes *nodesNew(size_t count); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/backends/ALSA/ALSA.cpp b/src/backends/ALSA/ALSA.cpp index e89f534..2d5af6f 100644 --- a/src/backends/ALSA/ALSA.cpp +++ b/src/backends/ALSA/ALSA.cpp @@ -6,6 +6,7 @@ #include "ALSA.hpp" #include "Backend.h" +#include "Node.h" #include #include @@ -13,7 +14,6 @@ #include #include #include -#include #include #include #include @@ -93,14 +93,10 @@ static ErrorCode engineNameSet(BE_Engine *engine, const char *name) { return toImpl(engine)->nameSet(name); } -static Node *engineNodesGet(BE_Engine *engine) { +static Nodes *engineNodesGet(BE_Engine *engine) { return toImpl(engine)->engineNodesGet(); } -static ErrorCode engineNodesFree(BE_Engine *engine, Node *nodes) { - return toImpl(engine)->engineNodesFree(nodes); -} - static BE_Flux *fluxNew(BE_Engine *) { return reinterpret_cast< BE_Flux * >(new Flux()); } @@ -146,7 +142,6 @@ const BE_Impl ALSA_Impl = { engineNameGet, engineNameSet, engineNodesGet, - engineNodesFree, fluxNew, fluxFree, @@ -183,7 +178,7 @@ ErrorCode Engine::nameSet(const char *name) { return CROSSAUDIO_EC_OK; } -::Node *Engine::engineNodesGet() { +Nodes *Engine::engineNodesGet() { void **hints; if (snd_device_name_hint(-1, "pcm", &hints) < 0) { return nullptr; @@ -218,33 +213,17 @@ ::Node *Engine::engineNodesGet() { return nullptr; } - auto nodes = static_cast< ::Node * >(calloc(nodeMap.size() + 1, sizeof(::Node))); + auto nodes = nodesNew(nodeMap.size()); for (auto [i, iter] = std::tuple{ std::size_t(0), nodeMap.cbegin() }; iter != nodeMap.cend(); ++i, ++iter) { - nodes[i].id = const_cast< char *>(iter->first.data()); - nodes[i].name = const_cast< char *>(iter->second.first.data()); - nodes[i].direction = iter->second.second; + nodes->items[i].id = const_cast< char *>(iter->first.data()); + nodes->items[i].name = const_cast< char *>(iter->second.first.data()); + nodes->items[i].direction = iter->second.second; } return nodes; } -ErrorCode Engine::engineNodesFree(::Node *nodes) { - for (size_t i = 0; i < std::numeric_limits< size_t >::max(); ++i) { - auto &node = nodes[i]; - if (!node.id) { - break; - } - - free(node.id); - free(node.name); - } - - free(nodes); - - return CROSSAUDIO_EC_OK; -} - void Engine::cleanNodeName(char *name) { for (; *name != '\0'; ++name) { if (*name == '\n') { diff --git a/src/backends/ALSA/ALSA.hpp b/src/backends/ALSA/ALSA.hpp index 6ec582b..49535f8 100644 --- a/src/backends/ALSA/ALSA.hpp +++ b/src/backends/ALSA/ALSA.hpp @@ -18,7 +18,7 @@ typedef CrossAudio_FluxConfig FluxConfig; typedef CrossAudio_FluxFeedback FluxFeedback; typedef CrossAudio_ErrorCode ErrorCode; -typedef CrossAudio_Node Node; +typedef CrossAudio_Nodes Nodes; struct BE_Impl; @@ -39,8 +39,7 @@ class Engine { const char *nameGet() const; ErrorCode nameSet(const char *name); - ::Node *engineNodesGet(); - ErrorCode engineNodesFree(::Node *nodes); + Nodes *engineNodesGet(); ErrorCode start(); ErrorCode stop(); diff --git a/src/backends/OSS/OSS.cpp b/src/backends/OSS/OSS.cpp index 94657df..52db058 100644 --- a/src/backends/OSS/OSS.cpp +++ b/src/backends/OSS/OSS.cpp @@ -6,6 +6,7 @@ #include "OSS.hpp" #include "Backend.h" +#include "Node.h" #include #include @@ -113,14 +114,10 @@ static ErrorCode engineNameSet(BE_Engine *engine, const char *name) { return toImpl(engine)->nameSet(name); } -static Node *engineNodesGet(BE_Engine *engine) { +static Nodes *engineNodesGet(BE_Engine *engine) { return toImpl(engine)->engineNodesGet(); } -static ErrorCode engineNodesFree(BE_Engine *engine, Node *nodes) { - return toImpl(engine)->engineNodesFree(nodes); -} - static BE_Flux *fluxNew(BE_Engine *) { return reinterpret_cast< BE_Flux * >(new Flux()); } @@ -166,7 +163,6 @@ const BE_Impl OSS_Impl = { engineNameGet, engineNameSet, engineNodesGet, - engineNodesFree, fluxNew, fluxFree, @@ -224,7 +220,7 @@ ErrorCode Engine::nameSet(const char *name) { return CROSSAUDIO_EC_OK; } -::Node *Engine::engineNodesGet() { +Nodes *Engine::engineNodesGet() { oss_sysinfo sysInfo; if (!getSysInfo(sysInfo, m_fd)) { return nullptr; @@ -255,32 +251,17 @@ ::Node *Engine::engineNodesGet() { direction = static_cast< Direction >(dir); } - auto nodes = static_cast< ::Node * >(calloc(nodeMap.size() + 1, sizeof(::Node))); + auto nodes = nodesNew(nodeMap.size()); for (auto [i, iter] = std::tuple{ std::size_t(0), nodeMap.cbegin() }; iter != nodeMap.cend(); ++i, ++iter) { - nodes[i].id = strdup(iter->first.data()); - nodes[i].name = strrchr(nodes[i].id, '/') + 1; - nodes[i].direction = iter->second; + nodes->items[i].id = strdup(iter->first.data()); + nodes->items[i].name = strdup(strrchr(iter->first.data(), '/') + 1); + nodes->items[i].direction = iter->second; } return nodes; } -ErrorCode Engine::engineNodesFree(::Node *nodes) { - for (size_t i = 0; i < std::numeric_limits< size_t >::max(); ++i) { - auto &node = nodes[i]; - if (!node.id) { - break; - } - - free(node.id); - } - - free(nodes); - - return CROSSAUDIO_EC_OK; -} - // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=246231 void Engine::fixNodeID(std::string &str) { const auto dotPos = str.find_last_of('.'); diff --git a/src/backends/OSS/OSS.hpp b/src/backends/OSS/OSS.hpp index 21dabce..26eef41 100644 --- a/src/backends/OSS/OSS.hpp +++ b/src/backends/OSS/OSS.hpp @@ -20,7 +20,7 @@ typedef CrossAudio_FluxConfig FluxConfig; typedef CrossAudio_FluxFeedback FluxFeedback; typedef CrossAudio_ErrorCode ErrorCode; -typedef CrossAudio_Node Node; +typedef CrossAudio_Nodes Nodes; struct BE_Impl; @@ -41,8 +41,7 @@ class Engine { const char *nameGet() const; ErrorCode nameSet(const char *name); - ::Node *engineNodesGet(); - ErrorCode engineNodesFree(::Node *nodes); + Nodes *engineNodesGet(); ErrorCode start(); ErrorCode stop(); diff --git a/src/backends/PipeWire/PipeWire.cpp b/src/backends/PipeWire/PipeWire.cpp index 35bd6bd..3fb089a 100644 --- a/src/backends/PipeWire/PipeWire.cpp +++ b/src/backends/PipeWire/PipeWire.cpp @@ -5,11 +5,12 @@ #include "PipeWire.hpp" +#include "Node.h" + #include "crossaudio/Macros.h" #include #include -#include #include #include @@ -110,14 +111,10 @@ static ErrorCode engineNameSet(BE_Engine *engine, const char *name) { return toImpl(engine)->nameSet(name); } -static Node *engineNodesGet(BE_Engine *engine) { +static Nodes *engineNodesGet(BE_Engine *engine) { return toImpl(engine)->engineNodesGet(); } -static ErrorCode engineNodesFree(BE_Engine *engine, Node *nodes) { - return toImpl(engine)->engineNodesFree(nodes); -} - static BE_Flux *fluxNew(BE_Engine *engine) { if (auto flux = new Flux(*toImpl(engine))) { if (*flux) { @@ -171,7 +168,6 @@ constexpr BE_Impl PipeWire_Impl = { engineNameGet, engineNameSet, engineNodesGet, - engineNodesFree, fluxNew, fluxFree, @@ -310,16 +306,16 @@ ErrorCode Engine::nameSet(const char *name) { return ret >= 1 ? CROSSAUDIO_EC_OK : CROSSAUDIO_EC_GENERIC; } -::Node *Engine::engineNodesGet() { +Nodes *Engine::engineNodesGet() { const auto lock = locker(); - auto nodes = static_cast< ::Node * >(calloc(m_nodes.size() + 1, sizeof(::Node))); + auto nodes = nodesNew(m_nodes.size()); size_t i = 0; for (const auto &iter : m_nodes) { const auto &nodeIn = iter.second; - auto &nodeOut = nodes[i++]; + auto &nodeOut = nodes->items[i++]; nodeOut.direction = nodeIn.direction; @@ -333,22 +329,6 @@ ::Node *Engine::engineNodesGet() { return nodes; } -ErrorCode Engine::engineNodesFree(::Node *nodes) { - for (size_t i = 0; i < std::numeric_limits< size_t >::max(); ++i) { - auto &node = nodes[i]; - if (!node.id) { - break; - } - - free(node.id); - free(node.name); - } - - free(nodes); - - return CROSSAUDIO_EC_OK; -} - void Engine::addNode(const uint32_t id, const spa_dict *props) { const char *serialStr = spa_dict_lookup(props, PW_KEY_OBJECT_SERIAL); if (!serialStr) { diff --git a/src/backends/PipeWire/PipeWire.hpp b/src/backends/PipeWire/PipeWire.hpp index 70de4af..8b73fd1 100644 --- a/src/backends/PipeWire/PipeWire.hpp +++ b/src/backends/PipeWire/PipeWire.hpp @@ -25,7 +25,7 @@ using FluxConfig = CrossAudio_FluxConfig; using FluxFeedback = CrossAudio_FluxFeedback; using Direction = CrossAudio_Direction; -using Node = CrossAudio_Node; +using Nodes = CrossAudio_Nodes; struct spa_audio_info_raw; @@ -68,8 +68,7 @@ class Engine { const char *nameGet() const; ErrorCode nameSet(const char *name); - ::Node *engineNodesGet(); - ErrorCode engineNodesFree(::Node *nodes); + Nodes *engineNodesGet(); ErrorCode start(); ErrorCode stop(); diff --git a/src/backends/PulseAudio/PulseAudio.cpp b/src/backends/PulseAudio/PulseAudio.cpp index 2c08b48..9b98f2b 100644 --- a/src/backends/PulseAudio/PulseAudio.cpp +++ b/src/backends/PulseAudio/PulseAudio.cpp @@ -5,10 +5,11 @@ #include "PulseAudio.hpp" +#include "Node.h" + #include #include #include -#include #include #include @@ -93,14 +94,10 @@ static ErrorCode engineNameSet(BE_Engine *engine, const char *name) { return toImpl(engine)->nameSet(name); } -static Node *engineNodesGet(BE_Engine *engine) { +static Nodes *engineNodesGet(BE_Engine *engine) { return toImpl(engine)->engineNodesGet(); } -static ErrorCode engineNodesFree(BE_Engine *engine, Node *nodes) { - return toImpl(engine)->engineNodesFree(nodes); -} - static BE_Flux *fluxNew(BE_Engine *engine) { return reinterpret_cast< BE_Flux * >(new Flux(*toImpl(engine))); } @@ -146,7 +143,6 @@ constexpr BE_Impl PulseAudio_Impl = { engineNameGet, engineNameSet, engineNodesGet, - engineNodesFree, fluxNew, fluxFree, @@ -255,16 +251,16 @@ ErrorCode Engine::nameSet(const char *name) { return CROSSAUDIO_EC_OK; } -::Node *Engine::engineNodesGet() { +Nodes *Engine::engineNodesGet() { const std::unique_lock lock(m_nodesLock); - auto nodes = static_cast< ::Node * >(calloc(m_nodes.size() + 1, sizeof(::Node))); + auto nodes = nodesNew(m_nodes.size()); size_t i = 0; for (const auto &iter : m_nodes) { const auto &nodeIn = iter.second; - auto &nodeOut = nodes[i++]; + auto &nodeOut = nodes->items[i++]; nodeOut.id = strdup(nodeIn.name.data()); nodeOut.name = strdup(nodeIn.description.data()); @@ -274,22 +270,6 @@ ::Node *Engine::engineNodesGet() { return nodes; } -ErrorCode Engine::engineNodesFree(::Node *nodes) { - for (size_t i = 0; i < std::numeric_limits< size_t >::max(); ++i) { - auto &node = nodes[i]; - if (!node.id) { - break; - } - - free(node.id); - free(node.name); - } - - free(nodes); - - return CROSSAUDIO_EC_OK; -} - std::string Engine::defaultInName() { std::unique_lock lock(m_nodesLock); diff --git a/src/backends/PulseAudio/PulseAudio.hpp b/src/backends/PulseAudio/PulseAudio.hpp index ca32669..0102224 100644 --- a/src/backends/PulseAudio/PulseAudio.hpp +++ b/src/backends/PulseAudio/PulseAudio.hpp @@ -25,7 +25,7 @@ using FluxConfig = CrossAudio_FluxConfig; using FluxFeedback = CrossAudio_FluxFeedback; using Direction = CrossAudio_Direction; -using Node = CrossAudio_Node; +using Nodes = CrossAudio_Nodes; namespace pulseaudio { class Engine { @@ -61,8 +61,7 @@ class Engine { const char *nameGet() const; ErrorCode nameSet(const char *name); - ::Node *engineNodesGet(); - ErrorCode engineNodesFree(::Node *nodes); + Nodes *engineNodesGet(); ErrorCode start(); ErrorCode stop(); diff --git a/src/backends/Sndio/Sndio.cpp b/src/backends/Sndio/Sndio.cpp index 70afab2..c074d7f 100644 --- a/src/backends/Sndio/Sndio.cpp +++ b/src/backends/Sndio/Sndio.cpp @@ -93,14 +93,10 @@ static ErrorCode engineNameSet(BE_Engine *, const char *) { return CROSSAUDIO_EC_OK; } -static Node *engineNodesGet(BE_Engine *) { +static Nodes *engineNodesGet(BE_Engine *) { return nullptr; } -static ErrorCode engineNodesFree(BE_Engine *, Node *) { - return CROSSAUDIO_EC_OK; -} - static BE_Flux *fluxNew(BE_Engine *) { return reinterpret_cast< BE_Flux * >(new Flux()); } @@ -146,7 +142,6 @@ constexpr BE_Impl Sndio_Impl = { engineNameGet, engineNameSet, engineNodesGet, - engineNodesFree, fluxNew, fluxFree, diff --git a/src/backends/Sndio/Sndio.hpp b/src/backends/Sndio/Sndio.hpp index f366a91..0348459 100644 --- a/src/backends/Sndio/Sndio.hpp +++ b/src/backends/Sndio/Sndio.hpp @@ -10,7 +10,6 @@ #include "crossaudio/ErrorCode.h" #include "crossaudio/Flux.h" -#include "crossaudio/Node.h" #include #include @@ -20,7 +19,6 @@ typedef CrossAudio_FluxConfig FluxConfig; typedef CrossAudio_FluxFeedback FluxFeedback; typedef CrossAudio_ErrorCode ErrorCode; -typedef CrossAudio_Node Node; struct BE_Impl; diff --git a/src/backends/WASAPI/WASAPI.cpp b/src/backends/WASAPI/WASAPI.cpp index d535446..3468b97 100755 --- a/src/backends/WASAPI/WASAPI.cpp +++ b/src/backends/WASAPI/WASAPI.cpp @@ -6,6 +6,7 @@ #include "WASAPI.hpp" #include "Backend.h" +#include "Node.h" #include #include @@ -129,14 +130,10 @@ static ErrorCode engineNameSet(BE_Engine *engine, const char *name) { return toImpl(engine)->nameSet(name); } -static Node *engineNodesGet(BE_Engine *engine) { +static Nodes *engineNodesGet(BE_Engine *engine) { return toImpl(engine)->engineNodesGet(); } -static ErrorCode engineNodesFree(BE_Engine *engine, Node *nodes) { - return toImpl(engine)->engineNodesFree(nodes); -} - static BE_Flux *fluxNew(BE_Engine *engine) { if (auto flux = new Flux(*toImpl(engine))) { if (*flux) { @@ -190,7 +187,6 @@ const BE_Impl WASAPI_Impl = { engineNameGet, engineNameSet, engineNodesGet, - engineNodesFree, fluxNew, fluxFree, @@ -233,7 +229,7 @@ ErrorCode Engine::nameSet(const char *name) { return CROSSAUDIO_EC_OK; } -::Node *Engine::engineNodesGet() { +Nodes *Engine::engineNodesGet() { IMMDeviceCollection *collection; if (m_enumerator->EnumAudioEndpoints(eAll, DEVICE_STATE_ACTIVE, &collection) != S_OK) { return nullptr; @@ -242,7 +238,7 @@ ::Node *Engine::engineNodesGet() { UINT count = 0; collection->GetCount(&count); - auto nodes = static_cast< ::Node * >(calloc(count + 1, sizeof(::Node))); + auto nodes = nodesNew(count); for (decltype(count) i = 0; i < count; ++i) { IMMDevice *device; @@ -258,7 +254,7 @@ ::Node *Engine::engineNodesGet() { LPWSTR id; if (device->GetId(&id) == S_OK) { - auto &node = nodes[i]; + auto &node = nodes->items[i]; IMMEndpoint *endpoint; if (device->QueryInterface(__uuidof(IMMEndpoint), reinterpret_cast< void ** >(&endpoint)) == S_OK) { @@ -302,22 +298,6 @@ ::Node *Engine::engineNodesGet() { return nodes; } -ErrorCode Engine::engineNodesFree(::Node *nodes) { - for (size_t i = 0; i < std::numeric_limits< size_t >::max(); ++i) { - auto &node = nodes[i]; - if (!node.id) { - break; - } - - free(node.id); - free(node.name); - } - - free(nodes); - - return CROSSAUDIO_EC_OK; -} - Flux::Flux(Engine &engine) : m_engine(engine), m_feedback(), m_device(nullptr), m_client(nullptr), m_event(CreateEvent(nullptr, false, false, nullptr)) { diff --git a/src/backends/WASAPI/WASAPI.hpp b/src/backends/WASAPI/WASAPI.hpp index 14a0fd4..4b14be8 100644 --- a/src/backends/WASAPI/WASAPI.hpp +++ b/src/backends/WASAPI/WASAPI.hpp @@ -18,7 +18,7 @@ typedef CrossAudio_FluxConfig FluxConfig; typedef CrossAudio_FluxFeedback FluxFeedback; typedef CrossAudio_ErrorCode ErrorCode; -typedef CrossAudio_Node Node; +typedef CrossAudio_Nodes Nodes; struct BE_Impl; @@ -48,8 +48,7 @@ class Engine { const char *nameGet() const; ErrorCode nameSet(const char *name); - ::Node *engineNodesGet(); - ErrorCode engineNodesFree(::Node *nodes); + Nodes *engineNodesGet(); ErrorCode start(); ErrorCode stop(); diff --git a/src/tests/TestEnum/Enum.c b/src/tests/TestEnum/Enum.c index 47a53c3..5c0f988 100644 --- a/src/tests/TestEnum/Enum.c +++ b/src/tests/TestEnum/Enum.c @@ -8,6 +8,7 @@ #include "crossaudio/Node.h" typedef struct CrossAudio_Node Node; +typedef struct CrossAudio_Nodes Nodes; int main() { if (!initBackend()) { @@ -22,14 +23,14 @@ int main() { int ret = 0; do { - Node *nodes = CrossAudio_engineNodesGet(engine); + Nodes *nodes = CrossAudio_engineNodesGet(engine); if (!nodes) { ret = 3; break; } - for (size_t i = 0; i < SIZE_MAX; ++i) { - const Node *node = &nodes[i]; + for (size_t i = 0; i < nodes->count; ++i) { + const Node *node = &nodes->items[i]; if (!node->id) { break; } @@ -37,7 +38,7 @@ int main() { printf("[%s] %s (%s)\n", node->id, node->name, CrossAudio_DirectionText(node->direction)); } - CrossAudio_engineNodesFree(engine, nodes); + CrossAudio_nodesFree(nodes); } while (getchar() != 'q'); if (!destroyEngine(engine)) {