#include <config.h>
#endif
+#include <json.h>
+
#include <pulse/internal.h>
#include <pulse/xmalloc.h>
#include <pulse/i18n.h>
#include "format.h"
+#define PA_JSON_MIN_KEY "min"
+#define PA_JSON_MAX_KEY "max"
+
+static int pa_format_info_prop_compatible(const char *one, const char *two);
+
const char *pa_encoding_to_string(pa_encoding_t e) {
static const char* const table[]= {
[PA_ENCODING_PCM] = "pcm",
value_one = pa_proplist_gets(first->plist, key);
value_two = pa_proplist_gets(second->plist, key);
- if (!value_two || !pa_streq(value_one, value_two))
+ if (!value_two || !pa_format_info_prop_compatible(value_one, value_two))
return FALSE;
}
f = pa_format_info_new();
f->encoding = PA_ENCODING_PCM;
- pa_proplist_sets(f->plist, PA_PROP_FORMAT_SAMPLE_FORMAT, pa_sample_format_to_string(ss->format));
- pa_proplist_setf(f->plist, PA_PROP_FORMAT_RATE, "%u", (unsigned int) ss->rate);
- pa_proplist_setf(f->plist, PA_PROP_FORMAT_CHANNELS, "%u", (unsigned int) ss->channels);
+ pa_format_info_set_prop_string(f, PA_PROP_FORMAT_SAMPLE_FORMAT, pa_sample_format_to_string(ss->format));
+ pa_format_info_set_prop_int(f, PA_PROP_FORMAT_RATE, ss->rate);
+ pa_format_info_set_prop_int(f, PA_PROP_FORMAT_CHANNELS, ss->channels);
if (map) {
pa_channel_map_snprint(cm, sizeof(cm), map);
- pa_proplist_setf(f->plist, PA_PROP_FORMAT_CHANNEL_MAP, "%s", cm);
+ pa_format_info_set_prop_string(f, PA_PROP_FORMAT_CHANNEL_MAP, cm);
}
return f;
/* For PCM streams */
pa_bool_t pa_format_info_to_sample_spec(pa_format_info *f, pa_sample_spec *ss, pa_channel_map *map) {
- const char *sf, *r, *ch;
- uint32_t channels;
+ char *sf = NULL, *m = NULL;
+ int rate, channels;
+ pa_bool_t ret = FALSE;
pa_assert(f);
pa_assert(ss);
pa_return_val_if_fail(f->encoding == PA_ENCODING_PCM, FALSE);
- pa_return_val_if_fail(sf = pa_proplist_gets(f->plist, PA_PROP_FORMAT_SAMPLE_FORMAT), FALSE);
- pa_return_val_if_fail(r = pa_proplist_gets(f->plist, PA_PROP_FORMAT_RATE), FALSE);
- pa_return_val_if_fail(ch = pa_proplist_gets(f->plist, PA_PROP_FORMAT_CHANNELS), 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;
- pa_return_val_if_fail((ss->format = pa_parse_sample_format(sf)) != PA_SAMPLE_INVALID, FALSE);
- pa_return_val_if_fail(pa_atou(r, &ss->rate) == 0, FALSE);
- pa_return_val_if_fail(pa_atou(ch, &channels) == 0, FALSE);
+ if ((ss->format = pa_parse_sample_format(sf)) == PA_SAMPLE_INVALID)
+ goto out;
+
+ ss->rate = (uint32_t) rate;
ss->channels = (uint8_t) channels;
if (map) {
- const char *m = pa_proplist_gets(f->plist, PA_PROP_FORMAT_CHANNEL_MAP);
pa_channel_map_init(map);
- if (m)
- pa_return_val_if_fail(pa_channel_map_parse(map, m) != NULL, FALSE);
+ if (!pa_format_info_get_prop_string(f, PA_PROP_FORMAT_CHANNEL_MAP, &m))
+ goto out;
+
+ if (m && pa_channel_map_parse(map, m) == NULL)
+ goto out;
}
- return TRUE;
+ ret = TRUE;
+
+out:
+ if (sf)
+ pa_xfree(sf);
+ if (m)
+ pa_xfree(m);
+
+ return ret;
}
/* For compressed streams */
pa_bool_t pa_format_info_to_sample_spec_fake(pa_format_info *f, pa_sample_spec *ss) {
- const char *r;
+ int rate;
pa_assert(f);
pa_assert(ss);
ss->format = PA_SAMPLE_S16LE;
ss->channels = 2;
- pa_return_val_if_fail(r = pa_proplist_gets(f->plist, PA_PROP_FORMAT_RATE), FALSE);
- pa_return_val_if_fail(pa_atou(r, &ss->rate) == 0, FALSE);
+ pa_return_val_if_fail(pa_format_info_get_prop_int(f, PA_PROP_FORMAT_RATE, &rate), FALSE);
+ ss->rate = (uint32_t) rate;
if (f->encoding == PA_ENCODING_EAC3_IEC61937)
ss->rate *= 4;
return TRUE;
}
+
+pa_bool_t pa_format_info_get_prop_int(pa_format_info *f, const char *key, int *v) {
+ const char *str;
+ json_object *o;
+
+ pa_assert(f);
+ pa_assert(key);
+ pa_assert(v);
+
+ pa_return_val_if_fail(str = pa_proplist_gets(f->plist, key), FALSE);
+ o = json_tokener_parse(str);
+ pa_return_val_if_fail(!is_error(o), FALSE);
+ if (json_object_get_type(o) != json_type_int) {
+ json_object_put(o);
+ return FALSE;
+ }
+
+ *v = json_object_get_int(o);
+ json_object_put(o);
+
+ return TRUE;
+}
+
+pa_bool_t pa_format_info_get_prop_string(pa_format_info *f, const char *key, char **v) {
+ const char *str = NULL;
+ json_object *o;
+
+ pa_assert(f);
+ pa_assert(key);
+ pa_assert(v);
+
+ pa_return_val_if_fail(str = pa_proplist_gets(f->plist, key), FALSE);
+ o = json_tokener_parse(str);
+ pa_return_val_if_fail(!is_error(o), FALSE);
+ if (json_object_get_type(o) != json_type_string) {
+ json_object_put(o);
+ return FALSE;
+ }
+
+ *v = pa_xstrdup(json_object_get_string(o));
+ json_object_put(o);
+
+ return TRUE;
+}
+
+void pa_format_info_set_prop_int(pa_format_info *f, const char *key, int value) {
+ json_object *o;
+
+ pa_assert(f);
+ pa_assert(key);
+
+ o = json_object_new_int(value);
+
+ pa_proplist_sets(f->plist, key, json_object_to_json_string(o));
+
+ json_object_put(o);
+}
+
+void pa_format_info_set_prop_int_array(pa_format_info *f, const char *key, const int *values, int n_values) {
+ json_object *o;
+ int i;
+
+ pa_assert(f);
+ pa_assert(key);
+
+ o = json_object_new_array();
+
+ for (i = 0; i < n_values; i++)
+ json_object_array_add(o, json_object_new_int(values[i]));
+
+ pa_proplist_sets(f->plist, key, json_object_to_json_string(o));
+
+ json_object_put(o);
+}
+
+void pa_format_info_set_prop_int_range(pa_format_info *f, const char *key, int min, int max) {
+ json_object *o;
+
+ pa_assert(f);
+ pa_assert(key);
+
+ o = json_object_new_object();
+
+ json_object_object_add(o, PA_JSON_MIN_KEY, json_object_new_int(min));
+ json_object_object_add(o, PA_JSON_MAX_KEY, json_object_new_int(max));
+
+ pa_proplist_sets(f->plist, key, json_object_to_json_string(o));
+
+ json_object_put(o);
+}
+
+void pa_format_info_set_prop_string(pa_format_info *f, const char *key, const char *value) {
+ json_object *o;
+
+ pa_assert(f);
+ pa_assert(key);
+
+ o = json_object_new_string(value);
+
+ pa_proplist_sets(f->plist, key, json_object_to_json_string(o));
+
+ json_object_put(o);
+}
+
+void pa_format_info_set_prop_string_array(pa_format_info *f, const char *key, const char **values, int n_values) {
+ json_object *o;
+ int i;
+
+ pa_assert(f);
+ pa_assert(key);
+
+ o = json_object_new_array();
+
+ for (i = 0; i < n_values; i++)
+ json_object_array_add(o, json_object_new_string(values[i]));
+
+ pa_proplist_sets(f->plist, key, json_object_to_json_string(o));
+
+ json_object_put(o);
+}
+
+static pa_bool_t pa_json_is_fixed_type(json_object *o)
+{
+ switch(json_object_get_type(o)) {
+ case json_type_object:
+ case json_type_array:
+ return FALSE;
+
+ default:
+ return TRUE;
+ }
+}
+
+static int pa_json_value_equal(json_object *o1, json_object *o2) {
+ return (json_object_get_type(o1) == json_object_get_type(o2)) &&
+ pa_streq(json_object_to_json_string(o1), json_object_to_json_string(o2));
+}
+
+static int pa_format_info_prop_compatible(const char *one, const char *two) {
+ json_object *o1 = NULL, *o2 = NULL;
+ int i, ret = 0;
+
+ o1 = json_tokener_parse(one);
+ if (is_error(o1))
+ goto out;
+
+ o2 = json_tokener_parse(two);
+ if (is_error(o2))
+ 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);
+
+ if (pa_json_is_fixed_type(o1) && pa_json_is_fixed_type(o2)) {
+ ret = pa_json_value_equal(o1, o2);
+ goto out;
+ }
+
+ if (pa_json_is_fixed_type(o1)) {
+ json_object *tmp = o2;
+ o2 = o1;
+ o1 = tmp;
+ }
+
+ /* o2 is now a fixed type, and o1 is not */
+
+ if (json_object_get_type(o1) == json_type_array) {
+ for (i = 0; i < json_object_array_length(o1); i++) {
+ if (pa_json_value_equal(json_object_array_get_idx(o1, i), o2)) {
+ ret = 1;
+ break;
+ }
+ }
+ } else if (json_object_get_type(o1) == json_type_object) {
+ /* o1 should be a range type */
+ int min, max, v;
+ json_object *o_min = NULL, *o_max = NULL;
+
+ if (json_object_get_type(o2) != json_type_int) {
+ /* We don't support non-integer ranges */
+ goto out;
+ }
+
+ o_min = json_object_object_get(o1, PA_JSON_MIN_KEY);
+ if (!o_min || json_object_get_type(o_min) != json_type_int)
+ goto out;
+
+ o_max = json_object_object_get(o1, PA_JSON_MAX_KEY);
+ if (!o_max || json_object_get_type(o_max) != json_type_int)
+ goto out;
+
+ v = json_object_get_int(o2);
+ min = json_object_get_int(o_min);
+ max = json_object_get_int(o_max);
+
+ ret = v >= min && v <= max;
+ } else {
+ pa_log_warn("Got a format type that we don't support");
+ }
+
+out:
+ if (o1)
+ json_object_put(o1);
+ if (o2)
+ json_object_put(o2);
+
+ return ret;
+}
--- /dev/null
+/***
+ This file is part of PulseAudio.
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+
+#include <pulsecore/macro.h>
+#include <pulse/format.h>
+
+#define INIT(f) f = pa_format_info_new()
+#define DEINIT(f) pa_format_info_free(f);
+#define REINIT(f) { DEINIT(f); INIT(f); }
+
+int main(int argc, char *argv[]) {
+ pa_format_info *f1 = NULL, *f2 = NULL;
+ int rates1[] = { 32000, 44100, 48000 };
+ const char *strings[] = { "thing1", "thing2", "thing3" };
+
+ /* 1. Simple fixed format int check */
+ INIT(f1); INIT(f2);
+ f1->encoding = PA_ENCODING_AC3_IEC61937;
+ pa_format_info_set_prop_int(f1, PA_PROP_FORMAT_RATE, 32000);
+ f2->encoding = PA_ENCODING_AC3_IEC61937;
+ pa_format_info_set_prop_int(f2, PA_PROP_FORMAT_RATE, 44100);
+ pa_assert(!pa_format_info_is_compatible(f1, f2));
+
+ /* 2. Check int array membership - positive */
+ REINIT(f1); REINIT(f2);
+ f1->encoding = PA_ENCODING_AC3_IEC61937;
+ pa_format_info_set_prop_int_array(f1, PA_PROP_FORMAT_RATE, rates1, PA_ELEMENTSOF(rates1));
+ f2->encoding = PA_ENCODING_AC3_IEC61937;
+ pa_format_info_set_prop_int(f2, PA_PROP_FORMAT_RATE, 44100);
+ pa_assert(pa_format_info_is_compatible(f1, f2));
+ pa_assert(pa_format_info_is_compatible(f2, f1));
+
+ /* 3. Check int array memebership - negative */
+ REINIT(f2);
+ f2->encoding = PA_ENCODING_AC3_IEC61937;
+ pa_format_info_set_prop_int(f2, PA_PROP_FORMAT_RATE, 96000);
+ pa_assert(!pa_format_info_is_compatible(f1, f2));
+ pa_assert(!pa_format_info_is_compatible(f2, f1));
+
+ /* 4. Check int range - positive */
+ REINIT(f1); REINIT(f2);
+ f1->encoding = PA_ENCODING_AC3_IEC61937;
+ pa_format_info_set_prop_int_range(f1, PA_PROP_FORMAT_RATE, 32000, 48000);
+ f2->encoding = PA_ENCODING_AC3_IEC61937;
+ pa_format_info_set_prop_int(f2, PA_PROP_FORMAT_RATE, 44100);
+ pa_assert(pa_format_info_is_compatible(f1, f2));
+ pa_assert(pa_format_info_is_compatible(f2, f1));
+
+ /* 5. Check int range - negative */
+ REINIT(f2);
+ f2->encoding = PA_ENCODING_AC3_IEC61937;
+ pa_format_info_set_prop_int(f2, PA_PROP_FORMAT_RATE, 96000);
+ pa_assert(!pa_format_info_is_compatible(f1, f2));
+ pa_assert(!pa_format_info_is_compatible(f2, f1));
+
+ /* 6. Simple fixed format string check */
+ REINIT(f1); REINIT(f2);
+ f1->encoding = PA_ENCODING_AC3_IEC61937;
+ pa_format_info_set_prop_string(f1, "format.test_string", "thing1");
+ f2->encoding = PA_ENCODING_AC3_IEC61937;
+ pa_format_info_set_prop_string(f2, "format.test_string", "notthing1");
+ pa_assert(!pa_format_info_is_compatible(f1, f2));
+
+ /* 7. Check string array membership - positive */
+ REINIT(f1); REINIT(f2);
+ f1->encoding = PA_ENCODING_AC3_IEC61937;
+ pa_format_info_set_prop_string_array(f1, "format.test_string", strings, PA_ELEMENTSOF(strings));
+ f2->encoding = PA_ENCODING_AC3_IEC61937;
+ pa_format_info_set_prop_string(f2, "format.test_string", "thing3");
+ pa_assert(pa_format_info_is_compatible(f1, f2));
+ pa_assert(pa_format_info_is_compatible(f2, f1));
+
+ /* 8. Check string array memebership - negative */
+ REINIT(f2);
+ f2->encoding = PA_ENCODING_AC3_IEC61937;
+ pa_format_info_set_prop_string(f2, "format.test_string", "thing5");
+ pa_assert(!pa_format_info_is_compatible(f1, f2));
+ pa_assert(!pa_format_info_is_compatible(f2, f1));
+
+ DEINIT(f1);
+ DEINIT(f2);
+
+ return 0;
+}