From ab6a7701552eb29876a8dd1f430970f04c4a9b58 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Tue, 7 Jan 2025 11:17:01 +0800 Subject: [PATCH] Sound: detect platform API Ref: #1454 --- src/detection/sound/sound.h | 1 + src/detection/sound/sound_apple.c | 1 + src/detection/sound/sound_bsd.c | 3 ++- src/detection/sound/sound_linux.c | 23 ++++++++++++++--------- src/detection/sound/sound_windows.cpp | 1 + src/modules/sound/sound.c | 3 +++ 6 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/detection/sound/sound.h b/src/detection/sound/sound.h index 6ad811044..fb8d05f7e 100644 --- a/src/detection/sound/sound.h +++ b/src/detection/sound/sound.h @@ -8,6 +8,7 @@ typedef struct FFSoundDevice { FFstrbuf identifier; FFstrbuf name; + FFstrbuf platformApi; uint8_t volume; // 0-100% bool main; bool active; diff --git a/src/detection/sound/sound_apple.c b/src/detection/sound/sound_apple.c index 22ecec2ac..1c9c3f630 100644 --- a/src/detection/sound/sound_apple.c +++ b/src/detection/sound/sound_apple.c @@ -73,6 +73,7 @@ const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */) device->volume = FF_SOUND_VOLUME_UNKNOWN; ffStrbufInitF(&device->identifier, "%u", (unsigned) deviceId); ffStrbufInit(&device->name); + ffStrbufInitStatic(&device->platformApi, "Core Audio"); ffCfStrGetString(name, &device->name); uint32_t muted; diff --git a/src/detection/sound/sound_bsd.c b/src/detection/sound/sound_bsd.c index 4487b42a4..fcc2730ad 100644 --- a/src/detection/sound/sound_bsd.c +++ b/src/detection/sound/sound_bsd.c @@ -39,7 +39,8 @@ const char* ffDetectSound(FFlist* devices) FFSoundDevice* device = ffListAdd(devices); ffStrbufInitS(&device->identifier, path); ffStrbufInitF(&device->name, "%s %s", ci.longname, ci.hw_info); - ffStrbufTrimRightSpace(&device->name); + ffStrbufTrimRightSpace(&device->name); + ffStrbufInitStatic(&device->platformApi, "OSS"); device->volume = mutemask & SOUND_MASK_VOLUME ? 0 : ((uint8_t) volume /*left*/ + (uint8_t) (volume >> 8) /*right*/) / 2; diff --git a/src/detection/sound/sound_linux.c b/src/detection/sound/sound_linux.c index a66fb0a59..bd309aca0 100644 --- a/src/detection/sound/sound_linux.c +++ b/src/detection/sound/sound_linux.c @@ -13,6 +13,7 @@ static void paSinkInfoCallback(pa_context *c, const pa_sink_info *i, int eol, vo FFSoundDevice* device = ffListAdd(userdata); ffStrbufInitS(&device->identifier, i->name); + ffStrbufInitS(&device->platformApi, "PulseAudio"); ffStrbufTrimRightSpace(&device->identifier); ffStrbufInitS(&device->name, i->description); ffStrbufTrimRightSpace(&device->name); @@ -22,20 +23,24 @@ static void paSinkInfoCallback(pa_context *c, const pa_sink_info *i, int eol, vo device->main = false; } -static void paServerInfoCallback(pa_context *c, const pa_server_info *i, void *userdata) +static void paServerInfoCallback(FF_MAYBE_UNUSED pa_context *c, const pa_server_info *i, void *userdata) { - FF_UNUSED(c); + if(!i) return; - if(!i) - return; + FF_STRBUF_AUTO_DESTROY api = ffStrbufCreate(); + const char* realServer = strstr(i->server_name, "(on "); + if (realServer) + { + ffStrbufSetS(&api, realServer + strlen("(on ")); + ffStrbufTrimRight(&api, ')'); + } + else + ffStrbufSetF(&api, "%s %s", i->server_name, i->server_version); FF_LIST_FOR_EACH(FFSoundDevice, device, *(FFlist*)userdata) { - if(ffStrbufEqualS(&device->identifier, i->default_sink_name)) - { - device->main = true; - break; - } + device->main = ffStrbufEqualS(&device->identifier, i->default_sink_name); + ffStrbufSet(&device->platformApi, &api); } } diff --git a/src/detection/sound/sound_windows.cpp b/src/detection/sound/sound_windows.cpp index 55e6e99b1..fece395f0 100644 --- a/src/detection/sound/sound_windows.cpp +++ b/src/detection/sound/sound_windows.cpp @@ -65,6 +65,7 @@ const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */) device->volume = FF_SOUND_VOLUME_UNKNOWN; ffStrbufInitWS(&device->identifier, immDeviceId); ffStrbufInit(&device->name); + ffStrbufInitStatic(&device->platformApi, "Core Audio APIs"); { PROPVARIANT __attribute__((__cleanup__(PropVariantClear))) friendlyName; diff --git a/src/modules/sound/sound.c b/src/modules/sound/sound.c index 756d47716..e91b378f4 100644 --- a/src/modules/sound/sound.c +++ b/src/modules/sound/sound.c @@ -58,6 +58,7 @@ static void printDevice(FFSoundOptions* options, const FFSoundDevice* device, ui FF_FORMAT_ARG(percentageNum, "volume-percentage"), FF_FORMAT_ARG(device->identifier, "identifier"), FF_FORMAT_ARG(percentageBar, "volume-percentage-bar"), + FF_FORMAT_ARG(device->platformApi, "platform-api"), })); } } @@ -218,6 +219,7 @@ void ffGenerateSoundJsonResult(FF_MAYBE_UNUSED FFSoundOptions* options, yyjson_m yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name); yyjson_mut_obj_add_strbuf(doc, obj, "identifier", &item->identifier); + yyjson_mut_obj_add_strbuf(doc, obj, "platformApi", &item->platformApi); } FF_LIST_FOR_EACH(FFSoundDevice, device, result) @@ -241,6 +243,7 @@ static FFModuleBaseInfo ffModuleInfo = { {"Volume (in percentage num)", "volume-percentage"}, {"Identifier", "identifier"}, {"Volume (in percentage bar)", "volume-percentage-bar"}, + {"Platform API used", "platform-api"}, })) };