Skip to content

Commit 9ff045c

Browse files
committed
aplay API: paas init parameters in a structure
+ add also parent module pointer
1 parent d9eaca6 commit 9ff045c

File tree

16 files changed

+135
-122
lines changed

16 files changed

+135
-122
lines changed

src/audio/audio.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,11 @@ int audio_init(struct state_audio **ret,
385385
cfg = delim + 1;
386386
}
387387

388-
int ret = audio_playback_init(device, cfg, &s->audio_playback_device);
388+
struct audio_playback_opts opts;
389+
snprintf_ch(opts.cfg, "%s", cfg);
390+
opts.parent = s->audio_receiver_module.get();
391+
const int ret = audio_playback_init(device, &opts,
392+
&s->audio_playback_device);
389393
free(device);
390394
if (ret != 0) {
391395
retval = ret;

src/audio/audio_playback.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ void audio_playback_help(bool full)
6969
list_modules(LIBRARY_CLASS_AUDIO_PLAYBACK, AUDIO_PLAYBACK_ABI_VERSION, full);
7070
}
7171

72-
int audio_playback_init(const char *device, const char *cfg, struct state_audio_playback **state)
72+
int
73+
audio_playback_init(const char *device, const struct audio_playback_opts *opts,
74+
struct state_audio_playback **state)
7375
{
74-
struct state_audio_playback *s;
75-
76-
s = calloc(1, sizeof(struct state_audio_playback));
76+
struct state_audio_playback *s= calloc(1, sizeof(*s));
7777
gettimeofday(&s->t0, NULL);
7878
s->funcs = load_library(device, LIBRARY_CLASS_AUDIO_PLAYBACK, AUDIO_PLAYBACK_ABI_VERSION);
7979

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

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

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

106106
struct state_audio_playback *audio_playback_init_null_device(void)
107107
{
108+
const struct audio_playback_opts opts = { 0 };
108109
struct state_audio_playback *device = NULL;
109-
int ret = audio_playback_init("none", "", &device);
110+
int ret = audio_playback_init("none", &opts, &device);
110111
if (ret != 0) {
111112
log_msg(LOG_LEVEL_ERROR, "Unable to initialize null audio playback: %d\n", ret);
112113
}

src/audio/audio_playback.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,22 @@
3838
#ifndef AUDIO_AUDIO_PLAYBACK_H_316AA23B_3EFF_4150_83D2_24A2295CB74A
3939
#define AUDIO_AUDIO_PLAYBACK_H_316AA23B_3EFF_4150_83D2_24A2295CB74A
4040

41+
#ifndef __cplusplus
42+
#include <stdbool.h>
43+
#endif // ! defined __cplusplus
44+
4145
#include "../types.h"
46+
#include "utils/macros.h" // for STR_LEN
4247

4348
struct audio_desc;
4449
struct audio_frame;
50+
struct module;
4551

4652
#ifdef __cplusplus
4753
extern "C" {
48-
#else
49-
#include <stdbool.h>
5054
#endif
5155

52-
#define AUDIO_PLAYBACK_ABI_VERSION 11
56+
#define AUDIO_PLAYBACK_ABI_VERSION 12
5357

5458
/** @anchor audio_playback_ctl_reqs
5559
* @name Audio playback control requests
@@ -80,9 +84,14 @@ extern "C" {
8084
#define AUDIO_PLAYBACK_PUT_NETWORK_DEVICE 3
8185
/// @}
8286

87+
struct audio_playback_opts {
88+
char cfg[STR_LEN];
89+
struct module *parent;
90+
};
91+
8392
struct audio_playback_info {
8493
device_probe_func probe;
85-
void *(*init)(const char *cfg); ///< @param cfg is not NULL
94+
void *(*init)(const struct audio_playback_opts *opts);
8695
void (*write)(void *state, const struct audio_frame *frame);
8796
/** Returns device supported format that matches best with propsed audio desc */
8897
bool (*ctl)(void *state, int request, void *data, size_t *len);
@@ -97,8 +106,9 @@ void audio_playback_init_devices(void);
97106
/**
98107
* @see display_init
99108
*/
100-
int audio_playback_init(const char *device, const char *cfg,
101-
struct state_audio_playback **);
109+
int audio_playback_init(const char *device,
110+
const struct audio_playback_opts *opts,
111+
struct state_audio_playback **state);
102112
struct state_audio_playback *audio_playback_init_null_device(void);
103113

104114
/**

src/audio/filter/playback.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,21 @@
3535
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3636
*/
3737

38-
#ifdef HAVE_CONFIG_H
39-
#include "config.h"
40-
#include "config_unix.h"
41-
#include "config_win32.h"
42-
#endif /* HAVE_CONFIG_H */
43-
38+
#include <cstdio> // for printf
4439
#include <memory>
4540
#include <string_view>
4641

47-
#include "debug.h"
4842
#include "module.h"
4943
#include "audio/audio_filter.h"
50-
#include "audio/types.h"
5144
#include "audio/audio_playback.h"
45+
#include "audio/types.h"
5246
#include "lib_common.h"
53-
#include "utils/string_view_utils.hpp"
47+
#include "messaging.h" // for free_message, new_response
48+
#include "utils/macros.h" // for snprintf_ch
5449
#include "utils/misc.h"
50+
#include "utils/string_view_utils.hpp"
51+
52+
struct state_audio_playback;
5553

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

90-
int ret = audio_playback_init(dev.c_str(), dev_cfg.c_str(), out_ptr(s->playback_dev));
88+
struct audio_playback_opts opts;
89+
snprintf_ch(opts.cfg, "%s", dev_cfg.c_str());
90+
opts.parent = nullptr;
91+
92+
int ret = audio_playback_init(dev.c_str(), &opts, out_ptr(s->playback_dev));
9193

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

114-
if (audio_playback_reconfigure(s->playback_dev.get(), s->bps * 8,
116+
if (!audio_playback_reconfigure(s->playback_dev.get(), s->bps * 8,
115117
s->ch_count,
116-
s->sample_rate) != TRUE) {
118+
s->sample_rate)) {
117119
return AF_FAILURE;
118120
}
119121

src/audio/playback/alsa.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -842,10 +842,15 @@ init_local_config_with_workaround(char const * pcm_node_name)
842842

843843
ADD_TO_PARAM("alsa-playback-api", "* alsa-playback-api={thread|sync|async}\n"
844844
" ALSA API.\n");
845-
static void * audio_play_alsa_init(const char *cfg)
845+
static void *
846+
audio_play_alsa_init(const struct audio_playback_opts *opts)
846847
{
848+
if (strcmp(opts->cfg, "help") == 0) {
849+
audio_play_alsa_help();
850+
return INIT_NOERR;
851+
}
852+
847853
int rc;
848-
const char *name;
849854

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

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

882-
if (strlen(cfg) > 0) {
883-
if(strcmp(cfg, "help") == 0) {
884-
audio_play_alsa_help();
885-
free(s);
886-
return INIT_NOERR;
887-
}
888-
name = cfg;
889-
} else {
890-
if (is_default_pulse()) {
891-
name = "pulse";
892-
} else {
893-
name = "default";
894-
}
887+
const char *name = is_default_pulse() ? "pulse" : "default";
888+
if (strlen(opts->cfg) > 0) {
889+
name = opts->cfg;
895890
}
896891

897-
char device[1024] = "pcm.";
898-
899-
strncat(device, name, sizeof(device) - strlen(device) - 1);
892+
char device[STR_LEN + 4];
893+
snprintf_ch(device, "pcm.%s", name);
900894

901895
if (s->playback_mode == SYNC) {
902896
s->local_config = init_local_config_with_workaround(device);

src/audio/playback/coreaudio.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,14 @@ static void audio_play_ca_help()
403403
deleter ? deleter(available_devices) : free(available_devices);
404404
}
405405

406-
407-
static void * audio_play_ca_init(const char *cfg)
406+
static void *
407+
audio_play_ca_init(const struct audio_playback_opts *opts)
408408
{
409+
if (strcmp(opts->cfg, "help") == 0) {
410+
audio_play_ca_help();
411+
return INIT_NOERR;
412+
}
413+
409414
OSStatus ret = noErr;
410415
AudioComponent comp;
411416
AudioComponentDescription comp_desc;
@@ -455,22 +460,17 @@ static void * audio_play_ca_init(const char *cfg)
455460
goto error;
456461
}
457462

458-
if (strcmp(cfg, "help") == 0) {
459-
audio_play_ca_help();
460-
delete s;
461-
return INIT_NOERR;
462-
}
463-
if (strlen(cfg) > 0) {
463+
if (strlen(opts->cfg) > 0) {
464464
try {
465-
device = stoi(cfg);
465+
device = stoi(opts->cfg);
466466
} catch (std::invalid_argument &e) {
467-
device = audio_ca_get_device_by_name(cfg, CA_DIR);
467+
device = audio_ca_get_device_by_name(opts->cfg, CA_DIR);
468468
if (device == UINT_MAX) {
469469
log_msg(LOG_LEVEL_ERROR,
470470
MOD_NAME
471471
"Wrong device index "
472472
"or unrecognized name \"%s\"!\n",
473-
cfg);
473+
opts->cfg);
474474
goto error;
475475
}
476476
}

src/audio/playback/decklink.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,14 @@ static void audio_play_decklink_help()
182182
}
183183
}
184184

185-
static void *audio_play_decklink_init(const char *cfg)
185+
static void *
186+
audio_play_decklink_init(const struct audio_playback_opts *opts)
186187
{
188+
if (strcmp(opts->cfg, "help") == 0) {
189+
audio_play_decklink_help();
190+
return INIT_NOERR;
191+
}
192+
187193
struct state_decklink *s = NULL;
188194
IDeckLinkIterator* deckLinkIterator;
189195
HRESULT result;
@@ -202,15 +208,11 @@ static void *audio_play_decklink_init(const char *cfg)
202208
s->magic = DECKLINK_MAGIC;
203209
s->audio_consumer_levels = -1;
204210

205-
if (strlen(cfg) == 0) {
211+
if (strlen(opts->cfg) == 0) {
206212
cardIdx = 0;
207213
fprintf(stderr, "Card number unset, using first found (see -r decklink:help)!\n");
208-
} else if (strcmp(cfg, "help") == 0) {
209-
audio_play_decklink_help();
210-
free(s);
211-
return INIT_NOERR;
212214
} else {
213-
char *tmp = strdup(cfg);
215+
char *tmp = strdup(opts->cfg);
214216
char *item, *save_ptr;
215217
item = strtok_r(tmp, ":", &save_ptr);
216218
if (item) {
@@ -224,10 +226,10 @@ static void *audio_play_decklink_init(const char *cfg)
224226
}
225227
item = strtok_r(NULL, ":", &save_ptr);
226228
if (item) {
227-
cardIdx = atoi(cfg);
229+
cardIdx = atoi(opts->cfg);
228230
}
229231
} else {
230-
cardIdx = atoi(cfg);
232+
cardIdx = atoi(opts->cfg);
231233
}
232234
}
233235
free(tmp);

src/audio/playback/dummy.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,18 @@ static void audio_play_dummy_help(void)
7373
color_printf("\t" TBOLD("debug") " - audio print frame TS + len\n");
7474
}
7575

76-
static void * audio_play_dummy_init(const char *cfg)
76+
static void *
77+
audio_play_dummy_init(const struct audio_playback_opts *opts)
7778
{
7879
struct state_dummy_aplay *s = calloc(1, sizeof *s);
79-
if (strcmp(cfg, "debug") == 0) {
80+
if (strcmp(opts->cfg, "debug") == 0) {
8081
s->debug = true;
81-
} else if (strcmp(cfg, "help") == 0) {
82+
} else if (strcmp(opts->cfg, "help") == 0) {
8283
audio_play_dummy_help();
8384
free(s);
8485
return INIT_NOERR;
85-
} else if (strlen(cfg) > 0) {
86-
MSG(ERROR, "Wrong option: %s\n", cfg);
86+
} else if (strlen(opts->cfg) > 0) {
87+
MSG(ERROR, "Wrong option: %s\n", opts->cfg);
8788
free(s);
8889
return NULL;
8990
}

src/audio/playback/dump.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,15 @@ static void audio_play_dump_help() {
7171
"\n");
7272
}
7373

74-
static void * audio_play_dump_init(const char *cfg){
74+
static void * audio_play_dump_init(const struct audio_playback_opts *opts){
75+
if (strcmp(opts->cfg, "help") == 0) {
76+
audio_play_dump_help();
77+
return INIT_NOERR;
78+
}
7579
struct audio_dump_state *s = new audio_dump_state();
7680

77-
if (strlen(cfg) > 0) {
78-
if (strcmp(cfg, "help") == 0) {
79-
audio_play_dump_help();
80-
delete s;
81-
return INIT_NOERR;
82-
}
83-
s->filename = cfg;
81+
if (strlen(opts->cfg) > 0) {
82+
s->filename = opts->cfg;
8483
} else {
8584
s->filename = "audio_dump";
8685
}

src/audio/playback/jack.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ static void audio_play_jack_help(const char *client_name)
152152
free(available_devices);
153153
}
154154

155-
static void * audio_play_jack_init(const char *cfg)
155+
static void *
156+
audio_play_jack_init(const struct audio_playback_opts *opts)
156157
{
157158
const char **ports;
158159
jack_status_t status;
@@ -174,7 +175,7 @@ static void * audio_play_jack_init(const char *cfg)
174175
}
175176

176177
char dup[STR_LEN];
177-
snprintf_ch(dup, "%s", cfg);
178+
snprintf_ch(dup, "%s", opts->cfg);
178179
char *tmp = dup, *item, *save_ptr;
179180
while ((item = strtok_r(tmp, ":", &save_ptr)) != NULL) {
180181
if (strcmp(item, "help") == 0) {
@@ -194,7 +195,7 @@ static void * audio_play_jack_init(const char *cfg)
194195
} else if (strstr(item, "name=") == item) {
195196
snprintf_ch(client_name, "%s", strchr(item, '=') + 1);
196197
} else { // the rest is the device name
197-
source_name = cfg + (item - dup);
198+
source_name = opts->cfg + (item - dup);
198199
break;
199200
}
200201
tmp = NULL;

0 commit comments

Comments
 (0)