Sound: support JSON format

This commit is contained in:
李通洲
2023-09-12 09:37:24 +08:00
parent 257439477e
commit f13b771317
2 changed files with 31 additions and 1 deletions
+30 -1
View File
@@ -85,7 +85,7 @@ void ffPrintSound(FFSoundOptions* options)
void ffInitSoundOptions(FFSoundOptions* options)
{
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_SOUND_MODULE_NAME, ffParseSoundCommandOptions, ffParseSoundJsonObject, ffPrintSound, NULL);
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_SOUND_MODULE_NAME, ffParseSoundCommandOptions, ffParseSoundJsonObject, ffPrintSound, ffGenerateSoundJson);
ffOptionInitModuleArg(&options->moduleArgs);
options->soundType = FF_SOUND_TYPE_MAIN;
@@ -149,3 +149,32 @@ void ffParseSoundJsonObject(FFSoundOptions* options, yyjson_val* module)
ffPrintError(FF_SOUND_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
}
}
void ffGenerateSoundJson(FF_MAYBE_UNUSED FFSoundOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFSoundDevice));
const char* error = ffDetectSound(&result);
if(error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
return;
}
if(result.length == 0)
{
yyjson_mut_obj_add_str(doc, module, "error", "No active sound devices found");
return;
}
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
FF_LIST_FOR_EACH(FFSoundDevice, item, result)
{
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
yyjson_mut_obj_add_bool(doc, obj, "active", item->active);
yyjson_mut_obj_add_bool(doc, obj, "main", item->main);
yyjson_mut_obj_add_uint(doc, obj, "volume", item->volume);
yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name);
yyjson_mut_obj_add_strbuf(doc, obj, "identifier", &item->identifier);
}
}
+1
View File
@@ -9,3 +9,4 @@ void ffInitSoundOptions(FFSoundOptions* options);
bool ffParseSoundCommandOptions(FFSoundOptions* options, const char* key, const char* value);
void ffDestroySoundOptions(FFSoundOptions* options);
void ffParseSoundJsonObject(FFSoundOptions* options, yyjson_val* module);
void ffGenerateSoundJson(FFSoundOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module);