]> code.delx.au - pulseaudio/blobdiff - src/pulse/format.c
volume: Increase PA_SW_VOLUME_SNPRINT_DB_MAX
[pulseaudio] / src / pulse / format.c
index 617684691bcc43d81a0c3a6bf733b2f1bf9ccca5..729c2d2f75347f1929918dfd80772b5c406437aa 100644 (file)
@@ -30,6 +30,7 @@
 #include <pulse/internal.h>
 #include <pulse/xmalloc.h>
 
+#include <pulsecore/core-format.h>
 #include <pulsecore/core-util.h>
 #include <pulsecore/i18n.h>
 #include <pulsecore/macro.h>
@@ -47,6 +48,7 @@ static const char* const _encoding_str_table[]= {
     [PA_ENCODING_EAC3_IEC61937] = "eac3-iec61937",
     [PA_ENCODING_MPEG_IEC61937] = "mpeg-iec61937",
     [PA_ENCODING_DTS_IEC61937] = "dts-iec61937",
+    [PA_ENCODING_MPEG2_AAC_IEC61937] = "mpeg2-aac-iec61937",
     [PA_ENCODING_ANY] = "any",
 };
 
@@ -100,10 +102,6 @@ void pa_format_info_free(pa_format_info *f) {
     pa_xfree(f);
 }
 
-void pa_format_info_free2(pa_format_info *f, void *userdata) {
-    pa_format_info_free(f);
-}
-
 int pa_format_info_valid(const pa_format_info *f) {
     return (f->encoding >= 0 && f->encoding < PA_ENCODING_MAX && f->plist != NULL);
 }
@@ -173,7 +171,7 @@ error:
     goto out;
 }
 
-int pa_format_info_is_compatible(pa_format_info *first, pa_format_info *second) {
+int pa_format_info_is_compatible(const pa_format_info *first, const pa_format_info *second) {
     const char *key;
     void *state = NULL;
 
@@ -181,7 +179,7 @@ int pa_format_info_is_compatible(pa_format_info *first, pa_format_info *second)
     pa_assert(second);
 
     if (first->encoding != second->encoding)
-        return FALSE;
+        return false;
 
     while ((key = pa_proplist_iterate(first->plist, &state))) {
         const char *value_one, *value_two;
@@ -190,13 +188,13 @@ int pa_format_info_is_compatible(pa_format_info *first, pa_format_info *second)
         value_two = pa_proplist_gets(second->plist, key);
 
         if (!value_two || !pa_format_info_prop_compatible(value_one, value_two))
-            return FALSE;
+            return false;
     }
 
-    return TRUE;
+    return true;
 }
 
-pa_format_info* pa_format_info_from_sample_spec(pa_sample_spec *ss, pa_channel_map *map) {
+pa_format_info* pa_format_info_from_sample_spec(const pa_sample_spec *ss, const pa_channel_map *map) {
     char cm[PA_CHANNEL_MAP_SNPRINT_MAX];
     pa_format_info *f;
 
@@ -219,68 +217,100 @@ pa_format_info* pa_format_info_from_sample_spec(pa_sample_spec *ss, pa_channel_m
 }
 
 /* For PCM streams */
-int pa_format_info_to_sample_spec(pa_format_info *f, pa_sample_spec *ss, pa_channel_map *map) {
-    char *sf = NULL, *m = NULL;
-    int rate, channels;
-    int ret = -PA_ERR_INVALID;
-
+int pa_format_info_to_sample_spec(const pa_format_info *f, pa_sample_spec *ss, pa_channel_map *map) {
     pa_assert(f);
     pa_assert(ss);
-    pa_return_val_if_fail(f->encoding == PA_ENCODING_PCM, FALSE);
 
-    if (pa_format_info_get_prop_string(f, PA_PROP_FORMAT_SAMPLE_FORMAT, &sf))
-        goto out;
-    if (pa_format_info_get_prop_int(f, PA_PROP_FORMAT_RATE, &rate))
-        goto out;
-    if (pa_format_info_get_prop_int(f, PA_PROP_FORMAT_CHANNELS, &channels))
-        goto out;
+    if (!pa_format_info_is_pcm(f))
+        return pa_format_info_to_sample_spec_fake(f, ss, map);
 
-    if ((ss->format = pa_parse_sample_format(sf)) == PA_SAMPLE_INVALID)
-        goto out;
+    if (pa_format_info_get_sample_format(f, &ss->format) < 0)
+        return -PA_ERR_INVALID;
+    if (pa_format_info_get_rate(f, &ss->rate) < 0)
+        return -PA_ERR_INVALID;
+    if (pa_format_info_get_channels(f, &ss->channels) < 0)
+        return -PA_ERR_INVALID;
+    if (map && pa_format_info_get_channel_map(f, map) < 0)
+        return -PA_ERR_INVALID;
 
-    ss->rate = (uint32_t) rate;
-    ss->channels = (uint8_t) channels;
+    return 0;
+}
 
-    if (map) {
-        pa_channel_map_init(map);
+pa_prop_type_t pa_format_info_get_prop_type(const pa_format_info *f, const char *key) {
+    const char *str;
+    json_object *o, *o1;
+    pa_prop_type_t type;
 
-        if (pa_format_info_get_prop_string(f, PA_PROP_FORMAT_CHANNEL_MAP, &m) == 0)
-            if (pa_channel_map_parse(map, m) == NULL)
-                goto out;
-    }
+    pa_assert(f);
+    pa_assert(key);
 
-    ret = 0;
+    str = pa_proplist_gets(f->plist, key);
+    if (!str)
+        return PA_PROP_TYPE_INVALID;
 
-out:
-    if (sf)
-        pa_xfree(sf);
-    if (m)
-        pa_xfree(m);
+    o = json_tokener_parse(str);
+    if (is_error(o))
+        return PA_PROP_TYPE_INVALID;
 
-    return ret;
-}
+    switch (json_object_get_type(o)) {
+        case json_type_int:
+            type = PA_PROP_TYPE_INT;
+            break;
 
-/* For compressed streams */
-int pa_format_info_to_sample_spec_fake(pa_format_info *f, pa_sample_spec *ss) {
-    int rate;
+        case json_type_string:
+            type = PA_PROP_TYPE_STRING;
+            break;
 
-    pa_assert(f);
-    pa_assert(ss);
-    pa_return_val_if_fail(f->encoding != PA_ENCODING_PCM, -PA_ERR_INVALID);
+        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;
+            }
 
-    ss->format = PA_SAMPLE_S16LE;
-    ss->channels = 2;
+            o1 = json_object_array_get_idx(o, 1);
 
-    pa_return_val_if_fail(pa_format_info_get_prop_int(f, PA_PROP_FORMAT_RATE, &rate) == 0, -PA_ERR_INVALID);
-    ss->rate = (uint32_t) rate;
+            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;
 
-    if (f->encoding == PA_ENCODING_EAC3_IEC61937)
-        ss->rate *= 4;
+            json_object_put(o1);
+            break;
 
-    return 0;
+        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_MAX_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) {
+int pa_format_info_get_prop_int(const pa_format_info *f, const char *key, int *v) {
     const char *str;
     json_object *o;
 
@@ -293,10 +323,13 @@ int pa_format_info_get_prop_int(pa_format_info *f, const char *key, int *v) {
         return -PA_ERR_NOENTITY;
 
     o = json_tokener_parse(str);
-    if (is_error(o))
+    if (is_error(o)) {
+        pa_log_debug("Failed to parse format info property '%s'.", key);
         return -PA_ERR_INVALID;
+    }
 
     if (json_object_get_type(o) != json_type_int) {
+        pa_log_debug("Format info property '%s' type is not int.", key);
         json_object_put(o);
         return -PA_ERR_INVALID;
     }
@@ -307,7 +340,7 @@ 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) {
+int pa_format_info_get_prop_int_range(const pa_format_info *f, const char *key, int *min, int *max) {
     const char *str;
     json_object *o, *o1;
     int ret = -PA_ERR_INVALID;
@@ -322,8 +355,10 @@ int pa_format_info_get_prop_int_range(pa_format_info *f, const char *key, int *m
         return -PA_ERR_NOENTITY;
 
     o = json_tokener_parse(str);
-    if (is_error(o))
+    if (is_error(o)) {
+        pa_log_debug("Failed to parse format info property '%s'.", key);
         return -PA_ERR_INVALID;
+    }
 
     if (json_object_get_type(o) != json_type_object)
         goto out;
@@ -343,12 +378,14 @@ int pa_format_info_get_prop_int_range(pa_format_info *f, const char *key, int *m
     ret = 0;
 
 out:
+    if (ret < 0)
+        pa_log_debug("Format info property '%s' is not a valid int range.", key);
+
     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)
-{
+int pa_format_info_get_prop_int_array(const 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;
@@ -363,8 +400,10 @@ int pa_format_info_get_prop_int_array(pa_format_info *f, const char *key, int **
         return -PA_ERR_NOENTITY;
 
     o = json_tokener_parse(str);
-    if (is_error(o))
+    if (is_error(o)) {
+        pa_log_debug("Failed to parse format info property '%s'.", key);
         return -PA_ERR_INVALID;
+    }
 
     if (json_object_get_type(o) != json_type_array)
         goto out;
@@ -387,11 +426,14 @@ int pa_format_info_get_prop_int_array(pa_format_info *f, const char *key, int **
     ret = 0;
 
 out:
+    if (ret < 0)
+        pa_log_debug("Format info property '%s' is not a valid int array.", key);
+
     json_object_put(o);
     return ret;
 }
 
-int pa_format_info_get_prop_string(pa_format_info *f, const char *key, char **v) {
+int pa_format_info_get_prop_string(const pa_format_info *f, const char *key, char **v) {
     const char *str = NULL;
     json_object *o;
 
@@ -404,10 +446,13 @@ int pa_format_info_get_prop_string(pa_format_info *f, const char *key, char **v)
         return -PA_ERR_NOENTITY;
 
     o = json_tokener_parse(str);
-    if (is_error(o))
+    if (is_error(o)) {
+        pa_log_debug("Failed to parse format info property '%s'.", key);
         return -PA_ERR_INVALID;
+    }
 
     if (json_object_get_type(o) != json_type_string) {
+        pa_log_debug("Format info property '%s' type is not string.", key);
         json_object_put(o);
         return -PA_ERR_INVALID;
     }
@@ -418,8 +463,7 @@ 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)
-{
+int pa_format_info_get_prop_string_array(const 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;
@@ -434,8 +478,10 @@ int pa_format_info_get_prop_string_array(pa_format_info *f, const char *key, cha
         return -PA_ERR_NOENTITY;
 
     o = json_tokener_parse(str);
-    if (is_error(o))
+    if (is_error(o)) {
+        pa_log_debug("Failed to parse format info property '%s'.", key);
         return -PA_ERR_INVALID;
+    }
 
     if (json_object_get_type(o) != json_type_array)
         goto out;
@@ -458,6 +504,9 @@ int pa_format_info_get_prop_string_array(pa_format_info *f, const char *key, cha
     ret = 0;
 
 out:
+    if (ret < 0)
+        pa_log_debug("Format info property '%s' is not a valid string array.", key);
+
     json_object_put(o);
     return ret;
 }
@@ -567,15 +616,14 @@ void pa_format_info_set_prop_string_array(pa_format_info *f, const char *key, co
     json_object_put(o);
 }
 
-static pa_bool_t pa_json_is_fixed_type(json_object *o)
-{
+static bool pa_json_is_fixed_type(json_object *o) {
     switch(json_object_get_type(o)) {
         case json_type_object:
         case json_type_array:
-            return FALSE;
+            return false;
 
         default:
-            return TRUE;
+            return true;
     }
 }
 
@@ -597,7 +645,7 @@ static int pa_format_info_prop_compatible(const char *one, const char *two) {
         goto out;
 
     /* We don't deal with both values being non-fixed - just because there is no immediate need (FIXME) */
-    pa_return_val_if_fail(pa_json_is_fixed_type(o1) || pa_json_is_fixed_type(o2), FALSE);
+    pa_return_val_if_fail(pa_json_is_fixed_type(o1) || pa_json_is_fixed_type(o2), false);
 
     if (pa_json_is_fixed_type(o1) && pa_json_is_fixed_type(o2)) {
         ret = pa_json_value_equal(o1, o2);