Skip to content

Commit

Permalink
Refactor Node API
Browse files Browse the repository at this point in the history
  • Loading branch information
davidebeatrici committed Aug 2, 2024
1 parent 31165fa commit 78fbba9
Show file tree
Hide file tree
Showing 20 changed files with 142 additions and 171 deletions.
4 changes: 1 addition & 3 deletions include/crossaudio/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions include/crossaudio/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,31 @@
#define CROSSAUDIO_NODE_H

#include "Direction.h"
#include "ErrorCode.h"
#include "Macros.h"

#include <stddef.h>

struct CrossAudio_Node {
char *id;
char *name;
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
7 changes: 3 additions & 4 deletions src/Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include <stddef.h>

#define GET_BACKEND(name) \
#define GET_BACKEND(name) \
extern const BE_Impl name##_Impl; \
return &name##_Impl;

Expand All @@ -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;
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 2 additions & 6 deletions src/Engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include <stdlib.h>

typedef struct CrossAudio_Node Node;
typedef struct CrossAudio_Nodes Nodes;

Engine *CrossAudio_engineNew(const Backend backend) {
const BE_Impl *impl = backendGetImpl(backend);
Expand Down Expand Up @@ -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);
}
43 changes: 43 additions & 0 deletions src/Node.c
Original file line number Diff line number Diff line change
@@ -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 <https://www.mumble.info/LICENSE>.

#include "Node.h"

#include <stdlib.h>

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;
}
24 changes: 24 additions & 0 deletions src/Node.h
Original file line number Diff line number Diff line change
@@ -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 <https://www.mumble.info/LICENSE>.

#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
35 changes: 7 additions & 28 deletions src/backends/ALSA/ALSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#include "ALSA.hpp"

#include "Backend.h"
#include "Node.h"

#include <algorithm>
#include <bit>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <limits>
#include <map>
#include <thread>
#include <vector>
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -146,7 +142,6 @@ const BE_Impl ALSA_Impl = {
engineNameGet,
engineNameSet,
engineNodesGet,
engineNodesFree,

fluxNew,
fluxFree,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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') {
Expand Down
5 changes: 2 additions & 3 deletions src/backends/ALSA/ALSA.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down
33 changes: 7 additions & 26 deletions src/backends/OSS/OSS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "OSS.hpp"

#include "Backend.h"
#include "Node.h"

#include <bit>
#include <cerrno>
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -166,7 +163,6 @@ const BE_Impl OSS_Impl = {
engineNameGet,
engineNameSet,
engineNodesGet,
engineNodesFree,

fluxNew,
fluxFree,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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('.');
Expand Down
5 changes: 2 additions & 3 deletions src/backends/OSS/OSS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down
Loading

0 comments on commit 78fbba9

Please sign in to comment.