From: Seungbae Shin Date: Tue, 27 Apr 2021 11:12:24 +0000 (+0900) Subject: device-manager: enhance some if-else cascaded statements X-Git-Tag: submit/tizen/20210428.113701^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6ee4ef64b2d71bedd17136d044868d7ea4b4db2e;p=platform%2Fcore%2Fmultimedia%2Fpulseaudio-modules-tizen.git device-manager: enhance some if-else cascaded statements [Version] 13.0.62 [Issue Type] Revise Change-Id: I9f8ac0a13e3ee0ed92530f466537e7290e4e16e0 --- diff --git a/packaging/pulseaudio-modules-tizen.spec b/packaging/pulseaudio-modules-tizen.spec index 13add09..1c4f294 100644 --- a/packaging/pulseaudio-modules-tizen.spec +++ b/packaging/pulseaudio-modules-tizen.spec @@ -1,6 +1,6 @@ Name: pulseaudio-modules-tizen Summary: Pulseaudio modules for Tizen -Version: 13.0.61 +Version: 13.0.62 Release: 0 Group: Multimedia/Audio License: LGPL-2.1+ diff --git a/src/device-manager.c b/src/device-manager.c index aef1eee..fbefe47 100644 --- a/src/device-manager.c +++ b/src/device-manager.c @@ -227,6 +227,48 @@ static internal_codec_device internal_codec_devices[DEVICE_INDEX_MAX] = { .is_running[1] = false }, }; +typedef struct _device_module { + const char *module_name[2]; /* in/out */ + const char *device_string; + int (*custom_device_get_func)(pa_object *, char *); +} device_module_t; + +static int pulse_device_get_alsa_device_name(pa_object *pdevice, char *device_name); +static int pulse_device_get_tizen_device_name(pa_object *pdevice, char *device_name); + +static const device_module_t module_table[] = { + [DM_DEVICE_CLASS_NONE] = { + .module_name = { NULL, NULL }, + .device_string = NULL, + .custom_device_get_func = NULL + }, + [DM_DEVICE_CLASS_ALSA] = { + .module_name = { "module-alsa-source", "module-alsa-sink" }, + .device_string = NULL, + .custom_device_get_func = pulse_device_get_alsa_device_name + }, + [DM_DEVICE_CLASS_TIZEN] = { + .module_name = { "module-tizenaudio-source", "module-tizenaudio-sink" }, + .device_string = NULL, + .custom_device_get_func = pulse_device_get_tizen_device_name + }, + [DM_DEVICE_CLASS_BT] = { + .module_name = { NULL, "module-bluez5-device" }, + .device_string = "bt", + .custom_device_get_func = NULL + }, + [DM_DEVICE_CLASS_NULL] = { + .module_name = { "module-null-source", "module-null-sink" }, + .device_string = "null", + .custom_device_get_func = NULL + }, + [DM_DEVICE_CLASS_ACM] = { + .module_name = { NULL, "module-acm-sink" }, + .device_string = "acm", + .custom_device_get_func = NULL + } +}; + void simple_device_dump(pa_log_level_t level, const char *prefix, int id, const char *type, const char *name, int direction, int state) { pa_logl(level, "%s device id(%d) type(%s) name (%s) direction(%d) state(%d)", pa_strempty(prefix), id, pa_strnull(type), pa_strnull(name), direction, state); @@ -555,69 +597,18 @@ static bool pulse_device_is_monitor(pa_object *pdevice) { prop = pulse_device_get_proplist(pdevice); if ((device_class = pa_proplist_gets(prop, PA_PROP_DEVICE_CLASS))) { - if (pa_safe_streq(device_class, DEVICE_CLASS_MONITOR)) { + if (pa_safe_streq(device_class, DEVICE_CLASS_MONITOR)) return true; - } else { - return false; - } - } else { - return false; } -} - -/* Actually this device_name is not exactly same with alsa's form (ex. "hw:0,1") - * this is part of that. (ex. "0,1") */ -static int pulse_device_get_device_name(pa_object *pdevice, char *device_name) { - pa_proplist *prop; - prop = pulse_device_get_proplist(pdevice); - - if (pulse_device_is_alsa(pdevice)) { - const char *device_string_prop; - char *name_p; - - if (!(device_string_prop = pa_proplist_gets(prop, PA_PROP_DEVICE_STRING))) { - pa_log_error("failed to get property 'device.string'"); - return -1; - } - if (!(name_p = strchr(device_string_prop, ':'))) { - pa_log_error("failed to parse device string"); - return -1; - } - strncpy(device_name, name_p + 1, DEVICE_NAME_MAX); - } else if (pulse_device_is_tizenaudio(pdevice)) { - const char *card, *device; - if (!(card = pa_proplist_gets(prop, "tizen.card"))) { - pa_log_error("failed to get property 'tizen.card'"); - return -1; - } - if (!(device = pa_proplist_gets(prop, "tizen.device"))) { - pa_log_error("failed to get property 'tizen.device'"); - return -1; - } - snprintf(device_name, DEVICE_NAME_MAX, "%s,%s", card, device); - } else { - return -1; - } - return 0; + return false; } static const char* device_class_get_module_name(dm_device_class_t device_class, bool is_sink) { - if (device_class == DM_DEVICE_CLASS_NONE) { + if (device_class >= DM_DEVICE_CLASS_MAX) return NULL; - } else if (device_class == DM_DEVICE_CLASS_ALSA) { - return is_sink ? "module-alsa-sink" : "module-alsa-source"; - } else if (device_class == DM_DEVICE_CLASS_TIZEN) { - return is_sink ? "module-tizenaudio-sink" : "module-tizenaudio-source"; - } else if (device_class == DM_DEVICE_CLASS_BT) { - return is_sink ? "module-bluez5-device" : NULL; - } else if (device_class == DM_DEVICE_CLASS_NULL) { - return is_sink ? "module-null-sink" : "module-null-source"; - } else if (device_class == DM_DEVICE_CLASS_ACM) { - return is_sink ? "module-acm-sink" : NULL; - } else { - return NULL; - } + + return module_table[device_class].module_name[is_sink]; } static device_type_info* _device_manager_get_type_info(pa_idxset *type_infos, const char *type, const char *role) { @@ -935,6 +926,45 @@ static bool pulse_device_params_is_equal(pa_object *pdevice, const char *params) return device_params_is_equal(params, removed_module_args); } +static int pulse_device_get_alsa_device_name(pa_object *pdevice, char *device_name) { + pa_proplist *prop; + const char *device_string_prop; + char *name_p; + + prop = pulse_device_get_proplist(pdevice); + + if (!(device_string_prop = pa_proplist_gets(prop, PA_PROP_DEVICE_STRING))) { + pa_log_error("failed to get property 'device.string'"); + return -1; + } + if (!(name_p = strchr(device_string_prop, ':'))) { + pa_log_error("failed to parse device string"); + return -1; + } + + snprintf(device_name, DEVICE_NAME_MAX, "alsa:%s", name_p + 1); + return 0; +} + +static int pulse_device_get_tizen_device_name(pa_object *pdevice, char *device_name) { + pa_proplist *prop; + const char *card, *device; + + prop = pulse_device_get_proplist(pdevice); + + if (!(card = pa_proplist_gets(prop, "tizen.card"))) { + pa_log_error("failed to get property 'tizen.card'"); + return -1; + } + if (!(device = pa_proplist_gets(prop, "tizen.device"))) { + pa_log_error("failed to get property 'tizen.device'"); + return -1; + } + + snprintf(device_name, DEVICE_NAME_MAX, "tizen:%s,%s", card, device); + return 0; +} + static int pulse_device_get_device_string(pa_object *pdevice, char *device_string) { dm_device_class_t device_class; char device_name[DEVICE_NAME_MAX]; @@ -943,29 +973,19 @@ static int pulse_device_get_device_string(pa_object *pdevice, char *device_strin pa_assert(device_string); device_class = pulse_device_get_class(pdevice); + if (device_class <= DM_DEVICE_CLASS_NONE || + device_class >= DM_DEVICE_CLASS_MAX) + return -1; - if (device_class == DM_DEVICE_CLASS_ALSA) { - if (pulse_device_get_device_name(pdevice, device_name) < 0) - return -1; - snprintf(device_string, DEVICE_STR_MAX, "alsa:%s", device_name); - return 0; - } else if (device_class == DM_DEVICE_CLASS_NULL) { - snprintf(device_string, DEVICE_STR_MAX, "null"); - return 0; - } else if (device_class == DM_DEVICE_CLASS_TIZEN) { - if (pulse_device_get_device_name(pdevice, device_name) < 0) - return -1; - snprintf(device_string, DEVICE_STR_MAX, "tizen:%s", device_name); - return 0; - } else if (device_class == DM_DEVICE_CLASS_BT) { - snprintf(device_string, DEVICE_STR_MAX, "bt"); - return 0; - } else if (device_class == DM_DEVICE_CLASS_ACM) { - snprintf(device_string, DEVICE_STR_MAX, "acm"); - return 0; - } else { + if (module_table[device_class].custom_device_get_func && + module_table[device_class].custom_device_get_func(pdevice, device_name) < 0) return -1; - } + + snprintf(device_string, DEVICE_STR_MAX, "%s", + module_table[device_class].device_string ? + module_table[device_class].device_string : device_name); + + return 0; } /* pdevice is sink or source */