Skip to content

Commit

Permalink
Prepare API for node addition/removal callback(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidebeatrici committed Sep 28, 2024
1 parent 6b1272c commit 08431c4
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 14 deletions.
10 changes: 9 additions & 1 deletion include/crossaudio/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@
struct CrossAudio_Engine;
struct CrossAudio_Node;

struct CrossAudio_EngineFeedback {
void *userData;

void (*nodeAdded)(void *userData, struct CrossAudio_Node *node);
void (*nodeRemoved)(void *userData, struct CrossAudio_Node *node);
};

#ifdef __cplusplus
extern "C" {
#endif

CROSSAUDIO_EXPORT struct CrossAudio_Engine *CrossAudio_engineNew(enum CrossAudio_Backend backend);
CROSSAUDIO_EXPORT enum CrossAudio_ErrorCode CrossAudio_engineFree(struct CrossAudio_Engine *engine);

CROSSAUDIO_EXPORT enum CrossAudio_ErrorCode CrossAudio_engineStart(struct CrossAudio_Engine *engine);
CROSSAUDIO_EXPORT enum CrossAudio_ErrorCode CrossAudio_engineStart(struct CrossAudio_Engine *engine,
const struct CrossAudio_EngineFeedback *feedback);
CROSSAUDIO_EXPORT enum CrossAudio_ErrorCode CrossAudio_engineStop(struct CrossAudio_Engine *engine);

CROSSAUDIO_EXPORT const char *CrossAudio_engineNameGet(struct CrossAudio_Engine *engine);
Expand Down
3 changes: 2 additions & 1 deletion src/Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
typedef enum CrossAudio_Backend Backend;
typedef enum CrossAudio_ErrorCode ErrorCode;

typedef struct CrossAudio_EngineFeedback EngineFeedback;
typedef struct CrossAudio_FluxConfig FluxConfig;
typedef struct CrossAudio_FluxFeedback FluxFeedback;
typedef struct CrossAudio_Nodes Nodes;
Expand All @@ -35,7 +36,7 @@ typedef struct BE_Impl {

BE_Engine *(*engineNew)(void);
ErrorCode (*engineFree)(BE_Engine *engine);
ErrorCode (*engineStart)(BE_Engine *engine);
ErrorCode (*engineStart)(BE_Engine *engine, const EngineFeedback *feedback);
ErrorCode (*engineStop)(BE_Engine *engine);
const char *(*engineNameGet)(BE_Engine *engine);
ErrorCode (*engineNameSet)(BE_Engine *engine, const char *name);
Expand Down
4 changes: 2 additions & 2 deletions src/Engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ ErrorCode CrossAudio_engineFree(Engine *engine) {
return ec;
}

ErrorCode CrossAudio_engineStart(Engine *engine) {
return engine->beImpl->engineStart(engine->beData);
ErrorCode CrossAudio_engineStart(Engine *engine, const EngineFeedback *feedback) {
return engine->beImpl->engineStart(engine->beData, feedback);
}

ErrorCode CrossAudio_engineStop(Engine *engine) {
Expand Down
6 changes: 6 additions & 0 deletions src/Node.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ static void freeNodeInner(Node *node) {
free(node->name);
}

Node *nodeNew(void) {
Node *node = calloc(1, sizeof(Node));

return node;
}

Nodes *nodesNew(const size_t count) {
Nodes *nodes = malloc(sizeof(Nodes));

Expand Down
1 change: 1 addition & 0 deletions src/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef struct CrossAudio_Nodes Nodes;
extern "C" {
#endif

Node *nodeNew(void);
Nodes *nodesNew(size_t count);

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion src/backends/ALSA/ALSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static ErrorCode engineFree(BE_Engine *engine) {
return CROSSAUDIO_EC_OK;
}

static ErrorCode engineStart(BE_Engine *engine) {
static ErrorCode engineStart(BE_Engine *engine, const EngineFeedback *) {
return toImpl(engine)->start();
}

Expand Down
2 changes: 1 addition & 1 deletion src/backends/OSS/OSS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static ErrorCode engineFree(BE_Engine *engine) {
return CROSSAUDIO_EC_OK;
}

static ErrorCode engineStart(BE_Engine *engine) {
static ErrorCode engineStart(BE_Engine *engine, const EngineFeedback *) {
return toImpl(engine)->start();
}

Expand Down
2 changes: 1 addition & 1 deletion src/backends/PipeWire/PipeWire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static ErrorCode engineFree(BE_Engine *engine) {
return CROSSAUDIO_EC_OK;
}

static ErrorCode engineStart(BE_Engine *engine) {
static ErrorCode engineStart(BE_Engine *engine, const EngineFeedback *) {
return toImpl(engine)->start();
}

Expand Down
2 changes: 1 addition & 1 deletion src/backends/PulseAudio/PulseAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static ErrorCode engineFree(BE_Engine *engine) {
return CROSSAUDIO_EC_OK;
}

static ErrorCode engineStart(BE_Engine *engine) {
static ErrorCode engineStart(BE_Engine *engine, const EngineFeedback *) {
return toImpl(engine)->start();
}

Expand Down
2 changes: 1 addition & 1 deletion src/backends/Sndio/Sndio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static ErrorCode engineFree(BE_Engine *engine) {
return CROSSAUDIO_EC_OK;
}

static ErrorCode engineStart(BE_Engine *) {
static ErrorCode engineStart(BE_Engine *, const EngineFeedback *) {
return CROSSAUDIO_EC_OK;
}

Expand Down
2 changes: 1 addition & 1 deletion src/backends/WASAPI/WASAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static ErrorCode engineFree(BE_Engine *engine) {
return CROSSAUDIO_EC_OK;
}

static ErrorCode engineStart(BE_Engine *engine) {
static ErrorCode engineStart(BE_Engine *engine, const EngineFeedback *) {
return toImpl(engine)->start();
}

Expand Down
5 changes: 3 additions & 2 deletions src/tests/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef enum CrossAudio_Backend Backend;
typedef enum CrossAudio_ErrorCode ErrorCode;

typedef struct CrossAudio_Engine Engine;
typedef struct CrossAudio_EngineFeedback EngineFeedback;
typedef struct CrossAudio_Flux Flux;
typedef struct CrossAudio_FluxConfig FluxConfig;
typedef struct CrossAudio_FluxFeedback FluxFeedback;
Expand All @@ -50,14 +51,14 @@ static inline bool deinitBackend() {
return true;
}

static inline Engine *createEngine() {
static inline Engine *createEngine(const EngineFeedback *feedback) {
Engine *engine = CrossAudio_engineNew(BACKEND);
if (!engine) {
printf("CrossAudio_engineNew() failed!\n");
return NULL;
}

const ErrorCode ec = CrossAudio_engineStart(engine);
const ErrorCode ec = CrossAudio_engineStart(engine, feedback);
if (ec != CROSSAUDIO_EC_OK) {
printf("CrossAudio_engineStart() failed with error \"%s\"!\n", CrossAudio_ErrorCodeText(ec));
CrossAudio_engineFree(engine);
Expand Down
19 changes: 18 additions & 1 deletion src/tests/TestEnum/Enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,32 @@

#include "crossaudio/Node.h"

#include <stddef.h>
#include <stdio.h>

typedef struct CrossAudio_Node Node;
typedef struct CrossAudio_Nodes Nodes;

static void nodeAdded(void *userData, Node *node) {
(void)userData;
printf("Node added: [%s] %s (%s)\n", node->id, node->name, CrossAudio_DirectionText(node->direction));
CrossAudio_nodeFree(node);
}

static void nodeRemoved(void *userData, Node *node) {
(void)userData;
printf("Node removed: [%s] %s (%s)\n", node->id, node->name, CrossAudio_DirectionText(node->direction));
CrossAudio_nodeFree(node);
}

int main() {
if (!initBackend()) {
return 1;
}

Engine *engine = createEngine();
const EngineFeedback feedback = { .userData = NULL, .nodeAdded = nodeAdded, .nodeRemoved = nodeRemoved };

Engine *engine = createEngine(&feedback);
if (!engine) {
return 2;
}
Expand Down
3 changes: 2 additions & 1 deletion src/tests/TestLoopback/Loopback.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "RingBuffer.h"

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

Expand Down Expand Up @@ -64,7 +65,7 @@ int main(const int argc, const char *argv[]) {
return 1;
}

Engine *engine = createEngine();
Engine *engine = createEngine(NULL);
if (!engine) {
return 2;
}
Expand Down

0 comments on commit 08431c4

Please sign in to comment.