Skip to content

Commit 23197c9

Browse files
committed
aplay/mixer: pass audio_playback_opts to ctor
+ moved opt parsing to a separate function
1 parent 9ff045c commit 23197c9

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/audio/playback/mixer.cpp

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -240,32 +240,50 @@ class logarithmic_mix_algo : public generic_mix_algo<source_t, intermediate_t> {
240240
};
241241

242242
struct state_audio_mixer final {
243-
state_audio_mixer(const char *cfg) {
244-
if (cfg) {
245-
shared_ptr<char> tmp(strdup(cfg), free);
246-
char *item, *save_ptr;
247-
char *copy = tmp.get();
248-
249-
while ((item = strtok_r(copy, ":", &save_ptr))) {
250-
if (strncmp(item, "codec=", strlen("codec=")) == 0) {
251-
audio_codec = item + strlen("codec=");
252-
} else if (strncmp(item, "algo=", strlen("algo=")) == 0) {
253-
string algo = item + strlen("algo=");
254-
if (algo == "linear") {
255-
mixing_algorithm = decltype(mixing_algorithm)(new linear_mix_algo<sample_type_source, sample_type_mixed>());
256-
} else if (algo == "logarithmic") {
257-
mixing_algorithm = decltype(mixing_algorithm)(new logarithmic_mix_algo<sample_type_source, sample_type_mixed>());
258-
} else {
259-
LOG(LOG_LEVEL_ERROR) << "Unknown mixing algorithm: " << algo << "\n";
260-
throw 1;
261-
}
243+
private:
244+
void parse_opts(const struct audio_playback_opts *opts) noexcept(false)
245+
{
246+
char copy[STR_LEN];
247+
snprintf_ch(copy, "%s", opts->cfg);
248+
char *tmp = copy;
249+
char *item = nullptr;
250+
char *save_ptr = nullptr;
251+
252+
while ((item = strtok_r(tmp, ":", &save_ptr)) != nullptr) {
253+
if (strncmp(item, "codec=", strlen("codec=")) == 0) {
254+
audio_codec = item + strlen("codec=");
255+
} else if (strncmp(item, "algo=", strlen("algo=")) ==
256+
0) {
257+
string algo = item + strlen("algo=");
258+
if (algo == "linear") {
259+
mixing_algorithm =
260+
decltype(mixing_algorithm)(
261+
new linear_mix_algo<
262+
sample_type_source,
263+
sample_type_mixed>());
264+
} else if (algo == "logarithmic") {
265+
mixing_algorithm =
266+
decltype(mixing_algorithm)(
267+
new logarithmic_mix_algo<
268+
sample_type_source,
269+
sample_type_mixed>());
262270
} else {
263-
LOG(LOG_LEVEL_ERROR) << "Unknown option: " << item << "\n";
271+
LOG(LOG_LEVEL_ERROR)
272+
<< "Unknown mixing algorithm: "
273+
<< algo << "\n";
264274
throw 1;
265275
}
266-
copy = nullptr;
276+
} else {
277+
LOG(LOG_LEVEL_ERROR)
278+
<< "Unknown option: " << item << "\n";
279+
throw 1;
267280
}
281+
tmp = nullptr;
268282
}
283+
}
284+
public:
285+
state_audio_mixer(const struct audio_playback_opts *opts) {
286+
parse_opts(opts);
269287

270288
struct audio_codec_state *audio_coder =
271289
audio_codec_init_cfg(audio_codec.c_str(), AUDIO_CODER);
@@ -422,7 +440,7 @@ audio_play_mixer_init(const struct audio_playback_opts *opts)
422440
return INIT_NOERR;
423441
}
424442
try {
425-
return new state_audio_mixer{opts->cfg};
443+
return new state_audio_mixer(opts);
426444
} catch (...) {
427445
return nullptr;
428446
}

0 commit comments

Comments
 (0)