[pulseaudio-discuss] [PATCH 7/7] format: Add API to query a property's type

Arun Raghavan arun.raghavan at collabora.co.uk
Tue Feb 7 00:20:49 PST 2012


Since a given property can be single-valued, an array or (in the case of
ints) a range, clients need an API to figure out what type of value a
property holds. This adds such an API. The actual property type
enumeration is kept in the PA_PROP_* namespace and not the
PA_FORMAT_INFO* namespace so that it can later be reused for properties
generically if required.
---
 src/map-file            |    1 +
 src/pulse/format.c      |   74 +++++++++++++++++++++++++++++++++++++++++++++++
 src/pulse/format.h      |   24 +++++++++++++++
 src/tests/format-test.c |    5 +++
 4 files changed, 104 insertions(+), 0 deletions(-)

diff --git a/src/map-file b/src/map-file
index ee3d31d..5730fdf 100644
--- a/src/map-file
+++ b/src/map-file
@@ -168,6 +168,7 @@ pa_format_info_free;
 pa_format_info_free2;
 pa_format_info_from_string;
 pa_format_info_from_sample_spec;
+pa_format_info_get_prop_type;
 pa_format_info_get_prop_int;
 pa_format_info_get_prop_int_range;
 pa_format_info_get_prop_int_array;
diff --git a/src/pulse/format.c b/src/pulse/format.c
index 6176846..aee3dfc 100644
--- a/src/pulse/format.c
+++ b/src/pulse/format.c
@@ -280,6 +280,80 @@ int pa_format_info_to_sample_spec_fake(pa_format_info *f, pa_sample_spec *ss) {
     return 0;
 }
 
+pa_prop_type_t pa_format_info_get_prop_type(pa_format_info *f, const char *key) {
+    const char *str;
+    json_object *o, *o1;
+    pa_prop_type_t type;
+
+    pa_assert(f);
+    pa_assert(key);
+
+    str = pa_proplist_gets(f->plist, key);
+    if (!str)
+        return PA_PROP_TYPE_INVALID;
+
+    o = json_tokener_parse(str);
+    if (is_error(o))
+        return PA_PROP_TYPE_INVALID;
+
+    switch (json_object_get_type(o)) {
+        case json_type_int:
+            type = PA_PROP_TYPE_INT;
+            break;
+
+        case json_type_string:
+            type = PA_PROP_TYPE_STRING;
+            break;
+
+        case json_type_array:
+            if (json_object_array_length(o) == 0) {
+                /* Unlikely, but let's account for this anyway. We need at
+                 * least one element to figure out the array type. */
+                type = PA_PROP_TYPE_INVALID;
+                break;
+            }
+
+            o1 = json_object_array_get_idx(o, 1);
+
+            if (json_object_get_type(o1) == json_type_int)
+                type = PA_PROP_TYPE_INT_ARRAY;
+            else if (json_object_get_type(o1) == json_type_string)
+                type = PA_PROP_TYPE_STRING_ARRAY;
+            else
+                type = PA_PROP_TYPE_INVALID;
+
+            json_object_put(o1);
+            break;
+
+        case json_type_object:
+            /* We actually know at this point that it's a int range, but let's
+             * confirm. */
+            o1 = json_object_object_get(o, PA_JSON_MIN_KEY);
+            if (!o1) {
+                type = PA_PROP_TYPE_INVALID;
+                break;
+            }
+            json_object_put(o1);
+
+            o1 = json_object_object_get(o, PA_JSON_MIN_KEY);
+            if (!o1) {
+                type = PA_PROP_TYPE_INVALID;
+                break;
+            }
+            json_object_put(o1);
+
+            type = PA_PROP_TYPE_INT_RANGE;
+            break;
+
+        default:
+            type = PA_PROP_TYPE_INVALID;
+            break;
+    }
+
+    json_object_put(o);
+    return type;
+}
+
 int pa_format_info_get_prop_int(pa_format_info *f, const char *key, int *v) {
     const char *str;
     json_object *o;
diff --git a/src/pulse/format.h b/src/pulse/format.h
index 5f71044..643a099 100644
--- a/src/pulse/format.h
+++ b/src/pulse/format.h
@@ -122,6 +122,30 @@ pa_format_info* pa_format_info_from_sample_spec(pa_sample_spec *ss, pa_channel_m
  * a negative integer if conversion failed and 0 on success. \since 2.0 */
 int pa_format_info_to_sample_spec(pa_format_info *f, pa_sample_spec *ss, pa_channel_map *map);
 
+/** Represents the type of value type of a property on a \ref pa_format_info. \since 2.0 */
+typedef enum pa_prop_type_t {
+    PA_PROP_TYPE_INT,
+    /**< Integer property */
+
+    PA_PROP_TYPE_INT_RANGE,
+    /**< Integer range property */
+
+    PA_PROP_TYPE_INT_ARRAY,
+    /**< Integer array property */
+
+    PA_PROP_TYPE_STRING,
+    /**< String property */
+
+    PA_PROP_TYPE_STRING_ARRAY,
+    /**< String array property */
+
+    PA_PROP_TYPE_INVALID = -1,
+    /**< Represents an invalid type */
+} pa_prop_type_t;
+
+/** Gets the type of property \a key in a given \ref pa_format_info. \since 2.0 */
+pa_prop_type_t pa_format_info_get_prop_type(pa_format_info *f, const char *key);
+
 /** Gets an integer property from the given format info. Returns 0 on success and a negative integer on failure. \since 2.0 */
 int pa_format_info_get_prop_int(pa_format_info *f, const char *key, int *v);
 /** Gets an integer range property from the given format info. Returns 0 on success and a negative integer on failure.
diff --git a/src/tests/format-test.c b/src/tests/format-test.c
index 1a3aade..bb66c85 100644
--- a/src/tests/format-test.c
+++ b/src/tests/format-test.c
@@ -105,18 +105,21 @@ int main(int argc, char *argv[]) {
     /* 9. Verify setting/getting an int */
     REINIT(f1);
     pa_format_info_set_prop_int(f1, "format.test_string", 42);
+    pa_assert(pa_format_info_get_prop_type(f1, "format.test_string") == PA_PROP_TYPE_INT);
     pa_assert(pa_format_info_get_prop_int(f1, "format.test_string", &temp_int1) == 0);
     pa_assert(temp_int1 == 42);
 
     /* 10. Verify setting/getting an int range */
     REINIT(f1);
     pa_format_info_set_prop_int_range(f1, "format.test_string", 0, 100);
+    pa_assert(pa_format_info_get_prop_type(f1, "format.test_string") == PA_PROP_TYPE_INT_RANGE);
     pa_assert(pa_format_info_get_prop_int_range(f1, "format.test_string", &temp_int1, &temp_int2) == 0);
     pa_assert(temp_int1 == 0 && temp_int2 == 100);
 
     /* 11. Verify setting/getting an int array */
     REINIT(f1);
     pa_format_info_set_prop_int_array(f1, "format.test_string", rates1, PA_ELEMENTSOF(rates1));
+    pa_assert(pa_format_info_get_prop_type(f1, "format.test_string") == PA_PROP_TYPE_INT_ARRAY);
     pa_assert(pa_format_info_get_prop_int_array(f1, "format.test_string", &temp_int_array, &temp_int1) == 0);
     pa_assert(temp_int1 == PA_ELEMENTSOF(rates1));
     for (i = 0; i < temp_int1; i++)
@@ -126,6 +129,7 @@ int main(int argc, char *argv[]) {
     /* 12. Verify setting/getting a string */
     REINIT(f1);
     pa_format_info_set_prop_string(f1, "format.test_string", "foo");
+    pa_assert(pa_format_info_get_prop_type(f1, "format.test_string") == PA_PROP_TYPE_STRING);
     pa_assert(pa_format_info_get_prop_string(f1, "format.test_string", &temp_str) == 0);
     pa_assert(pa_streq(temp_str, "foo"));
     pa_xfree(temp_str);
@@ -133,6 +137,7 @@ int main(int argc, char *argv[]) {
     /* 13. Verify setting/getting an int array */
     REINIT(f1);
     pa_format_info_set_prop_string_array(f1, "format.test_string", strings, PA_ELEMENTSOF(strings));
+    pa_assert(pa_format_info_get_prop_type(f1, "format.test_string") == PA_PROP_TYPE_STRING_ARRAY);
     pa_assert(pa_format_info_get_prop_string_array(f1, "format.test_string", &temp_str_array, &temp_int1) == 0);
     pa_assert(temp_int1 == PA_ELEMENTSOF(strings));
     for (i = 0; i < temp_int1; i++)
-- 
1.7.8.4



More information about the pulseaudio-discuss mailing list