From: Arun Raghavan Date: Fri, 12 Aug 2011 16:25:48 +0000 (+0530) Subject: format: Add string to pa_format_info conversion function X-Git-Url: https://code.delx.au/pulseaudio/commitdiff_plain/8bffbcde1b9ac41703be9c242f4f9f172d5649bb format: Add string to pa_format_info conversion function This will help accept string formats from the command like (so we can set formats using pactl). --- diff --git a/src/pulse/format.c b/src/pulse/format.c index 112b103c..f4eed836 100644 --- a/src/pulse/format.c +++ b/src/pulse/format.c @@ -41,20 +41,30 @@ 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", - [PA_ENCODING_AC3_IEC61937] = "ac3-iec61937", - [PA_ENCODING_EAC3_IEC61937] = "eac3-iec61937", - [PA_ENCODING_MPEG_IEC61937] = "mpeg-iec61937", - [PA_ENCODING_DTS_IEC61937] = "dts-iec61937", - [PA_ENCODING_ANY] = "any", - }; +static const char* const _encoding_str_table[]= { + [PA_ENCODING_PCM] = "pcm", + [PA_ENCODING_AC3_IEC61937] = "ac3-iec61937", + [PA_ENCODING_EAC3_IEC61937] = "eac3-iec61937", + [PA_ENCODING_MPEG_IEC61937] = "mpeg-iec61937", + [PA_ENCODING_DTS_IEC61937] = "dts-iec61937", + [PA_ENCODING_ANY] = "any", +}; +const char *pa_encoding_to_string(pa_encoding_t e) { if (e < 0 || e >= PA_ENCODING_MAX) return NULL; - return table[e]; + return _encoding_str_table[e]; +} + +pa_encoding_t pa_encoding_from_string(const char *encoding) { + pa_encoding_t e; + + for (e = PA_ENCODING_ANY; e < PA_ENCODING_MAX; e++) + if (pa_streq(_encoding_str_table[e], encoding)) + return e; + + return PA_ENCODING_INVALID; } pa_format_info* pa_format_info_new(void) { @@ -125,6 +135,44 @@ char *pa_format_info_snprint(char *s, size_t l, const pa_format_info *f) { return s; } +pa_format_info* pa_format_info_from_string(const char *str) { + pa_format_info *f = pa_format_info_new(); + char *encoding = NULL, *properties = NULL; + size_t pos; + + pos = strcspn(str, ","); + + encoding = pa_xstrndup(str, pos); + f->encoding = pa_encoding_from_string(pa_strip(encoding)); + if (f->encoding == PA_ENCODING_INVALID) + goto error; + + if (pos != strlen(str)) { + pa_proplist *plist; + + properties = pa_xstrdup(&str[pos+1]); + plist = pa_proplist_from_string(properties); + + if (!plist) + goto error; + + pa_proplist_free(f->plist); + f->plist = plist; + } + +out: + if (encoding) + pa_xfree(encoding); + if (properties) + pa_xfree(properties); + return f; + +error: + pa_format_info_free(f); + f = NULL; + goto out; +} + int pa_format_info_is_compatible(pa_format_info *first, pa_format_info *second) { const char *key; void *state = NULL; diff --git a/src/pulse/format.h b/src/pulse/format.h index 821149ca..b8b829ef 100644 --- a/src/pulse/format.h +++ b/src/pulse/format.h @@ -62,6 +62,9 @@ typedef enum pa_encoding { /** Returns a printable string representing the given encoding type. \since 1.0 */ const char *pa_encoding_to_string(pa_encoding_t e) PA_GCC_CONST; +/** Converts a string of the form returned by \a pa_encoding_to_string() back to a \a pa_encoding_t. \since 1.0 */ +pa_encoding_t pa_encoding_from_string(const char *encoding); + /**< Represents the format of data provided in a stream or processed by a sink. \since 1.0 */ typedef struct pa_format_info { pa_encoding_t encoding; @@ -105,6 +108,10 @@ int pa_format_info_is_compatible(pa_format_info *first, pa_format_info *second); /** Return a human-readable string representing the given format. \since 1.0 */ char *pa_format_info_snprint(char *s, size_t l, const pa_format_info *f); +/** Parse a human-readable string of the form generated by + * \a pa_format_info_snprint() into a pa_format_info structure. \since 1.0 */ +pa_format_info* pa_format_info_from_string(const char *str); + /** Sets an integer property on the given format info */ void pa_format_info_set_prop_int(pa_format_info *f, const char *key, int value); /** Sets a property with a list of integer values on the given format info */