[pulseaudio-discuss] [PATCH 4/7] format: Add more property getters

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


This adds integer range/array and string array property getters to the
pa_format_info API. Corresponding tests added as well to ensure the code
is valgrind-clean.

The corresponding functions are added to map-file manually for now.
---
 src/pulse/format.c      |  137 +++++++++++++++++++++++++++++++++++++++++++++++
 src/pulse/format.h      |   14 +++++
 src/tests/format-test.c |   42 ++++++++++++++-
 3 files changed, 192 insertions(+), 1 deletions(-)

diff --git a/src/pulse/format.c b/src/pulse/format.c
index 6e8e707..c0a96db 100644
--- a/src/pulse/format.c
+++ b/src/pulse/format.c
@@ -327,6 +327,90 @@ int pa_format_info_get_prop_int(pa_format_info *f, const char *key, int *v) {
     return 0;
 }
 
+int pa_format_info_get_prop_int_range(pa_format_info *f, const char *key, int *min, int *max) {
+    const char *str;
+    json_object *o, *o1;
+    int ret = -PA_ERR_INVALID;
+
+    pa_assert(f);
+    pa_assert(key);
+    pa_assert(min);
+    pa_assert(max);
+
+    str = pa_proplist_gets(f->plist, key);
+    if (!str)
+        return -PA_ERR_NOENTITY;
+
+    o = json_tokener_parse(str);
+    if (is_error(o))
+        return -PA_ERR_INVALID;
+
+    if (json_object_get_type(o) != json_type_object)
+        goto out;
+
+    if (!(o1 = json_object_object_get(o, PA_JSON_MIN_KEY)))
+        goto out;
+
+    *min = json_object_get_int(o1);
+    json_object_put(o1);
+
+    if (!(o1 = json_object_object_get(o, PA_JSON_MAX_KEY)))
+        goto out;
+
+    *max = json_object_get_int(o1);
+    json_object_put(o1);
+
+    ret = 0;
+
+out:
+    json_object_put(o);
+    return ret;
+}
+
+int pa_format_info_get_prop_int_array(pa_format_info *f, const char *key, int **values, int *n_values)
+{
+    const char *str;
+    json_object *o, *o1;
+    int i, ret = -PA_ERR_INVALID;
+
+    pa_assert(f);
+    pa_assert(key);
+    pa_assert(values);
+    pa_assert(n_values);
+
+    str = pa_proplist_gets(f->plist, key);
+    if (!str)
+        return -PA_ERR_NOENTITY;
+
+    o = json_tokener_parse(str);
+    if (is_error(o))
+        return -PA_ERR_INVALID;
+
+    if (json_object_get_type(o) != json_type_array)
+        goto out;
+
+    *n_values = json_object_array_length(o);
+    *values = pa_xnew(int, *n_values);
+
+    for (i = 0; i < *n_values; i++) {
+        o1 = json_object_array_get_idx(o, i);
+
+        if (json_object_get_type(o1) != json_type_int) {
+            json_object_put(o1);
+            goto out;
+        }
+
+        (*values)[i] = json_object_get_int(o1);
+        json_object_put(o1);
+    }
+
+    ret = 0;
+
+out:
+    json_object_put(o);
+    return ret;
+}
+
 int pa_format_info_get_prop_string(pa_format_info *f, const char *key, char **v) {
     const char *str = NULL;
     json_object *o;
@@ -354,6 +438,59 @@ int pa_format_info_get_prop_string(pa_format_info *f, const char *key, char **v)
     return 0;
 }
 
+int pa_format_info_get_prop_string_array(pa_format_info *f, const char *key, char ***values, int *n_values)
+{
+    const char *str;
+    json_object *o, *o1;
+    int i, ret = -PA_ERR_INVALID;
+
+    pa_assert(f);
+    pa_assert(key);
+    pa_assert(values);
+    pa_assert(n_values);
+
+    str = pa_proplist_gets(f->plist, key);
+    if (!str)
+        return -PA_ERR_NOENTITY;
+
+    o = json_tokener_parse(str);
+    if (is_error(o))
+        return -PA_ERR_INVALID;
+
+    if (json_object_get_type(o) != json_type_array)
+        goto out;
+
+    *n_values = json_object_array_length(o);
+    *values = pa_xnew(char *, *n_values);
+
+    for (i = 0; i < *n_values; i++) {
+        o1 = json_object_array_get_idx(o, i);
+
+        if (json_object_get_type(o1) != json_type_string) {
+            json_object_put(o1);
+            goto out;
+        }
+
+        (*values)[i] = pa_xstrdup(json_object_get_string(o1));
+        json_object_put(o1);
+    }
+
+    ret = 0;
+
+out:
+    json_object_put(o);
+    return ret;
+}
+
+void pa_format_info_free_string_array(char **values, int n_values) {
+    int i;
+
+    for (i = 0; i < n_values; i++)
+        pa_xfree(values[i]);
+
+    pa_xfree(values);
+}
+
 void pa_format_info_set_prop_int(pa_format_info *f, const char *key, int value) {
     json_object *o;
 
diff --git a/src/pulse/format.h b/src/pulse/format.h
index fd5727b..5f71044 100644
--- a/src/pulse/format.h
+++ b/src/pulse/format.h
@@ -124,9 +124,23 @@ int pa_format_info_to_sample_spec(pa_format_info *f, pa_sample_spec *ss, pa_chan
 
 /** 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.
+ * \since 2.0 */
+int pa_format_info_get_prop_int_range(pa_format_info *f, const char *key, int *min, int *max);
+/** Gets an integer array property from the given format info. \a values contains the values and \a n_values contains the
+ * number of elements. The caller must free \a values using \ref pa_xfree. Returns 0 on success and a negative integer on
+ * failure. \since 2.0 */
+int pa_format_info_get_prop_int_array(pa_format_info *f, const char *key, int **values, int *n_values);
 /** Gets a string property from the given format info.  The caller must free the returned string using \ref pa_xfree. Returns
  * 0 on success and a negative integer on failure. \since 2.0 */
 int pa_format_info_get_prop_string(pa_format_info *f, const char *key, char **v);
+/** Gets a string array property from the given format info. \a values contains the values and \a n_values contains
+ * the number of elements. The caller must free \a values using \ref pa_format_info_free_string_array. Returns 0 on success and
+ * a negative integer on failure. \since 2.0 */
+int pa_format_info_get_prop_string_array(pa_format_info *f, const char *key, char ***values, int *n_values);
+
+/** Frees a string array returned by \ref pa_format_info_get_prop_string_array. \since 2.0 */
+void pa_format_info_free_string_array(char **values, int n_values);
 
 /** Sets an integer property on the given format info. \since 1.0 */
 void pa_format_info_set_prop_int(pa_format_info *f, const char *key, int value);
diff --git a/src/tests/format-test.c b/src/tests/format-test.c
index fc3a599..1a3aade 100644
--- a/src/tests/format-test.c
+++ b/src/tests/format-test.c
@@ -24,7 +24,9 @@
 #include <stdlib.h>
 
 #include <pulsecore/macro.h>
+#include <pulsecore/core-util.h>
 #include <pulse/format.h>
+#include <pulse/xmalloc.h>
 
 #define INIT(f) f = pa_format_info_new()
 #define DEINIT(f) pa_format_info_free(f);
@@ -32,8 +34,9 @@
 
 int main(int argc, char *argv[]) {
     pa_format_info *f1 = NULL, *f2 = NULL;
-    int rates1[] = { 32000, 44100, 48000 };
+    int rates1[] = { 32000, 44100, 48000 }, i, temp_int1 = -1, temp_int2 = -1, *temp_int_array;
     const char *strings[] = { "thing1", "thing2", "thing3" };
+    char *temp_str, **temp_str_array;
 
     /* 1. Simple fixed format int check */
     INIT(f1); INIT(f2);
@@ -99,6 +102,43 @@ int main(int argc, char *argv[]) {
     pa_assert(!pa_format_info_is_compatible(f1, f2));
     pa_assert(!pa_format_info_is_compatible(f2, f1));
 
+    /* 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_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_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_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++)
+        pa_assert(temp_int_array[i] == rates1[i]);
+    pa_xfree(temp_int_array);
+
+    /* 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_string(f1, "format.test_string", &temp_str) == 0);
+    pa_assert(pa_streq(temp_str, "foo"));
+    pa_xfree(temp_str);
+
+    /* 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_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++)
+        pa_assert(pa_streq(temp_str_array[i], strings[i]));
+    pa_format_info_free_string_array(temp_str_array, temp_int1);
+
     DEINIT(f1);
     DEINIT(f2);
 
-- 
1.7.8.4



More information about the pulseaudio-discuss mailing list