Skip to content

Commit

Permalink
aplay API: paas init parameters in a structure
Browse files Browse the repository at this point in the history
+ add also parent module pointer
  • Loading branch information
MartinPulec committed Nov 22, 2024
1 parent e52029d commit dfd5e68
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 122 deletions.
6 changes: 5 additions & 1 deletion src/audio/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,11 @@ int audio_init(struct state_audio **ret,
cfg = delim + 1;
}

int ret = audio_playback_init(device, cfg, &s->audio_playback_device);
struct audio_playback_opts opts;
snprintf_ch(opts.cfg, "%s", cfg);
opts.parent = s->audio_receiver_module.get();
const int ret = audio_playback_init(device, &opts,
&s->audio_playback_device);
free(device);
if (ret != 0) {
retval = ret;
Expand Down
13 changes: 7 additions & 6 deletions src/audio/audio_playback.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ void audio_playback_help(bool full)
list_modules(LIBRARY_CLASS_AUDIO_PLAYBACK, AUDIO_PLAYBACK_ABI_VERSION, full);
}

int audio_playback_init(const char *device, const char *cfg, struct state_audio_playback **state)
int
audio_playback_init(const char *device, const struct audio_playback_opts *opts,
struct state_audio_playback **state)
{
struct state_audio_playback *s;

s = calloc(1, sizeof(struct state_audio_playback));
struct state_audio_playback *s= calloc(1, sizeof(*s));
gettimeofday(&s->t0, NULL);
s->funcs = load_library(device, LIBRARY_CLASS_AUDIO_PLAYBACK, AUDIO_PLAYBACK_ABI_VERSION);

Expand All @@ -83,7 +83,7 @@ int audio_playback_init(const char *device, const char *cfg, struct state_audio_
}

strncpy(s->name, device, sizeof s->name - 1);
s->state = s->funcs->init(cfg);
s->state = s->funcs->init(opts);

if(!s->state) {
log_msg(LOG_LEVEL_ERROR, "Error initializing audio playback.\n");
Expand All @@ -105,8 +105,9 @@ int audio_playback_init(const char *device, const char *cfg, struct state_audio_

struct state_audio_playback *audio_playback_init_null_device(void)
{
const struct audio_playback_opts opts = { 0 };
struct state_audio_playback *device = NULL;
int ret = audio_playback_init("none", "", &device);
int ret = audio_playback_init("none", &opts, &device);
if (ret != 0) {
log_msg(LOG_LEVEL_ERROR, "Unable to initialize null audio playback: %d\n", ret);
}
Expand Down
22 changes: 16 additions & 6 deletions src/audio/audio_playback.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,22 @@
#ifndef AUDIO_AUDIO_PLAYBACK_H_316AA23B_3EFF_4150_83D2_24A2295CB74A
#define AUDIO_AUDIO_PLAYBACK_H_316AA23B_3EFF_4150_83D2_24A2295CB74A

#ifndef __cplusplus
#include <stdbool.h>
#endif // ! defined __cplusplus

#include "../types.h"
#include "utils/macros.h" // for STR_LEN

struct audio_desc;
struct audio_frame;
struct module;

#ifdef __cplusplus
extern "C" {
#else
#include <stdbool.h>
#endif

#define AUDIO_PLAYBACK_ABI_VERSION 11
#define AUDIO_PLAYBACK_ABI_VERSION 12

/** @anchor audio_playback_ctl_reqs
* @name Audio playback control requests
Expand Down Expand Up @@ -80,9 +84,14 @@ extern "C" {
#define AUDIO_PLAYBACK_PUT_NETWORK_DEVICE 3
/// @}

struct audio_playback_opts {
char cfg[STR_LEN];
struct module *parent;
};

struct audio_playback_info {
device_probe_func probe;
void *(*init)(const char *cfg); ///< @param cfg is not NULL
void *(*init)(const struct audio_playback_opts *opts);
void (*write)(void *state, const struct audio_frame *frame);
/** Returns device supported format that matches best with propsed audio desc */
bool (*ctl)(void *state, int request, void *data, size_t *len);
Expand All @@ -97,8 +106,9 @@ void audio_playback_init_devices(void);
/**
* @see display_init
*/
int audio_playback_init(const char *device, const char *cfg,
struct state_audio_playback **);
int audio_playback_init(const char *device,
const struct audio_playback_opts *opts,
struct state_audio_playback **state);
struct state_audio_playback *audio_playback_init_null_device(void);

/**
Expand Down
26 changes: 14 additions & 12 deletions src/audio/filter/playback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,21 @@
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#include "config_unix.h"
#include "config_win32.h"
#endif /* HAVE_CONFIG_H */

#include <cstdio> // for printf
#include <memory>
#include <string_view>

#include "debug.h"
#include "module.h"
#include "audio/audio_filter.h"
#include "audio/types.h"
#include "audio/audio_playback.h"
#include "audio/types.h"
#include "lib_common.h"
#include "utils/string_view_utils.hpp"
#include "messaging.h" // for free_message, new_response
#include "utils/macros.h" // for snprintf_ch
#include "utils/misc.h"
#include "utils/string_view_utils.hpp"

struct state_audio_playback;

namespace{
struct Playback_dev_deleter{ void operator()(state_audio_playback *p){ audio_playback_done(p); } };
Expand Down Expand Up @@ -87,7 +85,11 @@ static af_result_code parse_cfg(state_playback *s, std::string_view cfg){
auto dev = std::string(tok);
auto dev_cfg = std::string(tokenize(cfg, ':'));

int ret = audio_playback_init(dev.c_str(), dev_cfg.c_str(), out_ptr(s->playback_dev));
struct audio_playback_opts opts;
snprintf_ch(opts.cfg, "%s", dev_cfg.c_str());
opts.parent = nullptr;

int ret = audio_playback_init(dev.c_str(), &opts, out_ptr(s->playback_dev));

return ret == 0 ? AF_OK : AF_FAILURE;
}
Expand All @@ -111,9 +113,9 @@ static af_result_code configure(void *state,
s->ch_count = in_ch_count;
s->sample_rate = in_sample_rate;

if (audio_playback_reconfigure(s->playback_dev.get(), s->bps * 8,
if (!audio_playback_reconfigure(s->playback_dev.get(), s->bps * 8,
s->ch_count,
s->sample_rate) != TRUE) {
s->sample_rate)) {
return AF_FAILURE;
}

Expand Down
30 changes: 12 additions & 18 deletions src/audio/playback/alsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,10 +842,15 @@ init_local_config_with_workaround(char const * pcm_node_name)

ADD_TO_PARAM("alsa-playback-api", "* alsa-playback-api={thread|sync|async}\n"
" ALSA API.\n");
static void * audio_play_alsa_init(const char *cfg)
static void *
audio_play_alsa_init(const struct audio_playback_opts *opts)
{
if (strcmp(opts->cfg, "help") == 0) {
audio_play_alsa_help();
return INIT_NOERR;
}

int rc;
const char *name;

struct state_alsa_playback *s = calloc(1, sizeof(struct state_alsa_playback));

Expand Down Expand Up @@ -879,24 +884,13 @@ static void * audio_play_alsa_init(const char *cfg)
log_msg(LOG_LEVEL_WARNING, MOD_NAME "Async API is experimental, in case of problems use either \"thread\" or \"sync\" API\n");
}

if (strlen(cfg) > 0) {
if(strcmp(cfg, "help") == 0) {
audio_play_alsa_help();
free(s);
return INIT_NOERR;
}
name = cfg;
} else {
if (is_default_pulse()) {
name = "pulse";
} else {
name = "default";
}
const char *name = is_default_pulse() ? "pulse" : "default";
if (strlen(opts->cfg) > 0) {
name = opts->cfg;
}

char device[1024] = "pcm.";

strncat(device, name, sizeof(device) - strlen(device) - 1);
char device[STR_LEN + 4];
snprintf_ch(device, "pcm.%s", name);

if (s->playback_mode == SYNC) {
s->local_config = init_local_config_with_workaround(device);
Expand Down
22 changes: 11 additions & 11 deletions src/audio/playback/coreaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,14 @@ static void audio_play_ca_help()
deleter ? deleter(available_devices) : free(available_devices);
}


static void * audio_play_ca_init(const char *cfg)
static void *
audio_play_ca_init(const struct audio_playback_opts *opts)
{
if (strcmp(opts->cfg, "help") == 0) {
audio_play_ca_help();
return INIT_NOERR;
}

OSStatus ret = noErr;
AudioComponent comp;
AudioComponentDescription comp_desc;
Expand Down Expand Up @@ -455,22 +460,17 @@ static void * audio_play_ca_init(const char *cfg)
goto error;
}

if (strcmp(cfg, "help") == 0) {
audio_play_ca_help();
delete s;
return INIT_NOERR;
}
if (strlen(cfg) > 0) {
if (strlen(opts->cfg) > 0) {
try {
device = stoi(cfg);
device = stoi(opts->cfg);
} catch (std::invalid_argument &e) {
device = audio_ca_get_device_by_name(cfg, CA_DIR);
device = audio_ca_get_device_by_name(opts->cfg, CA_DIR);
if (device == UINT_MAX) {
log_msg(LOG_LEVEL_ERROR,
MOD_NAME
"Wrong device index "
"or unrecognized name \"%s\"!\n",
cfg);
opts->cfg);
goto error;
}
}
Expand Down
20 changes: 11 additions & 9 deletions src/audio/playback/decklink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,14 @@ static void audio_play_decklink_help()
}
}

static void *audio_play_decklink_init(const char *cfg)
static void *
audio_play_decklink_init(const struct audio_playback_opts *opts)
{
if (strcmp(opts->cfg, "help") == 0) {
audio_play_decklink_help();
return INIT_NOERR;
}

struct state_decklink *s = NULL;
IDeckLinkIterator* deckLinkIterator;
HRESULT result;
Expand All @@ -202,15 +208,11 @@ static void *audio_play_decklink_init(const char *cfg)
s->magic = DECKLINK_MAGIC;
s->audio_consumer_levels = -1;

if (strlen(cfg) == 0) {
if (strlen(opts->cfg) == 0) {
cardIdx = 0;
fprintf(stderr, "Card number unset, using first found (see -r decklink:help)!\n");
} else if (strcmp(cfg, "help") == 0) {
audio_play_decklink_help();
free(s);
return INIT_NOERR;
} else {
char *tmp = strdup(cfg);
char *tmp = strdup(opts->cfg);
char *item, *save_ptr;
item = strtok_r(tmp, ":", &save_ptr);
if (item) {
Expand All @@ -224,10 +226,10 @@ static void *audio_play_decklink_init(const char *cfg)
}
item = strtok_r(NULL, ":", &save_ptr);
if (item) {
cardIdx = atoi(cfg);
cardIdx = atoi(opts->cfg);
}
} else {
cardIdx = atoi(cfg);
cardIdx = atoi(opts->cfg);
}
}
free(tmp);
Expand Down
11 changes: 6 additions & 5 deletions src/audio/playback/dummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,18 @@ static void audio_play_dummy_help(void)
color_printf("\t" TBOLD("debug") " - audio print frame TS + len\n");
}

static void * audio_play_dummy_init(const char *cfg)
static void *
audio_play_dummy_init(const struct audio_playback_opts *opts)
{
struct state_dummy_aplay *s = calloc(1, sizeof *s);
if (strcmp(cfg, "debug") == 0) {
if (strcmp(opts->cfg, "debug") == 0) {
s->debug = true;
} else if (strcmp(cfg, "help") == 0) {
} else if (strcmp(opts->cfg, "help") == 0) {
audio_play_dummy_help();
free(s);
return INIT_NOERR;
} else if (strlen(cfg) > 0) {
MSG(ERROR, "Wrong option: %s\n", cfg);
} else if (strlen(opts->cfg) > 0) {
MSG(ERROR, "Wrong option: %s\n", opts->cfg);
free(s);
return NULL;
}
Expand Down
15 changes: 7 additions & 8 deletions src/audio/playback/dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,15 @@ static void audio_play_dump_help() {
"\n");
}

static void * audio_play_dump_init(const char *cfg){
static void * audio_play_dump_init(const struct audio_playback_opts *opts){
if (strcmp(opts->cfg, "help") == 0) {
audio_play_dump_help();
return INIT_NOERR;
}
struct audio_dump_state *s = new audio_dump_state();

if (strlen(cfg) > 0) {
if (strcmp(cfg, "help") == 0) {
audio_play_dump_help();
delete s;
return INIT_NOERR;
}
s->filename = cfg;
if (strlen(opts->cfg) > 0) {
s->filename = opts->cfg;
} else {
s->filename = "audio_dump";
}
Expand Down
7 changes: 4 additions & 3 deletions src/audio/playback/jack.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ static void audio_play_jack_help(const char *client_name)
free(available_devices);
}

static void * audio_play_jack_init(const char *cfg)
static void *
audio_play_jack_init(const struct audio_playback_opts *opts)
{
const char **ports;
jack_status_t status;
Expand All @@ -173,7 +174,7 @@ static void * audio_play_jack_init(const char *cfg)
}

char dup[STR_LEN];
snprintf_ch(dup, "%s", cfg);
snprintf_ch(dup, "%s", opts->cfg);
char *tmp = dup, *item, *save_ptr;
while ((item = strtok_r(tmp, ":", &save_ptr)) != NULL) {
if (strcmp(item, "help") == 0) {
Expand All @@ -193,7 +194,7 @@ static void * audio_play_jack_init(const char *cfg)
} else if (strstr(item, "name=") == item) {
strcpy(client_name, item + strlen("name="));
} else { // the rest is the device name
source_name = cfg + (item - dup);
source_name = opts->cfg + (item - dup);
break;
}
tmp = NULL;
Expand Down
7 changes: 4 additions & 3 deletions src/audio/playback/mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,15 @@ static void audio_play_mixer_probe(struct device_info **available_devices, int *
*count = 0;
}

static void * audio_play_mixer_init(const char *cfg)
static void *
audio_play_mixer_init(const struct audio_playback_opts *opts)
{
if (strcmp(cfg, "help") == 0) {
if (strcmp(opts->cfg, "help") == 0) {
audio_play_mixer_help();
return INIT_NOERR;
}
try {
return new state_audio_mixer{cfg};
return new state_audio_mixer{opts->cfg};
} catch (...) {
return nullptr;
}
Expand Down
Loading

0 comments on commit dfd5e68

Please sign in to comment.