[asterisk-commits] kmoore: branch kmoore/stasis-http_sounds r387629 - in /team/kmoore/stasis-htt...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Sat May 4 09:12:36 CDT 2013
Author: kmoore
Date: Sat May 4 09:12:32 2013
New Revision: 387629
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=387629
Log:
sounds/{id} is now functional to some extent
Outstanding bug: The sounds index needs to be reloaded once all formats
are loaded. It needs to be moved to a separate module.
Modified:
team/kmoore/stasis-http_sounds/include/asterisk/file.h
team/kmoore/stasis-http_sounds/main/file.c
team/kmoore/stasis-http_sounds/res/stasis_http/resource_sounds.c
team/kmoore/stasis-http_sounds/res/stasis_http/resource_sounds.h
team/kmoore/stasis-http_sounds/rest-api/api-docs/sounds.json
Modified: team/kmoore/stasis-http_sounds/include/asterisk/file.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-http_sounds/include/asterisk/file.h?view=diff&rev=387629&r1=387628&r2=387629
==============================================================================
--- team/kmoore/stasis-http_sounds/include/asterisk/file.h (original)
+++ team/kmoore/stasis-http_sounds/include/asterisk/file.h Sat May 4 09:12:32 2013
@@ -377,15 +377,36 @@
char *ast_format_str_reduce(char *fmts);
/*!
- * \brief Get the description for a sound from the text file included with english sounds
+ * \brief Get the description for a sound
*
* \param filename Name of the file for which to get the description
* \param lang Language for which to get the description
*
* \retval NULL if not found
- * \return a pointer to the description (does not need to be ast_freed)
- */
-const char *ast_sounds_get_description(const char *filename, const char *lang);
+ * \return a copy of the description (must be ast_freed)
+ */
+char *ast_sounds_get_description(const char *filename, const char *lang);
+
+/*!
+ * \brief Get the ast_format_cap for a sound
+ *
+ * \param filename Name of the file for which to get the description
+ * \param lang Language for which to get the description
+ *
+ * \retval NULL if not found
+ * \return a copy of the format capabilities (must be destroyed)
+ */
+struct ast_format_cap *ast_sounds_get_format_cap(const char *filename, const char *lang);
+
+/*!
+ * \brief Get the languages in which a sound is available
+ *
+ * \param filename Name of the file for which to get available languages
+ *
+ * \retval NULL if not found
+ * \return an ast_str_container filled with language strings
+ */
+struct ao2_container *ast_sounds_get_languages(const char *filename);
#if defined(__cplusplus) || defined(c_plusplus)
}
Modified: team/kmoore/stasis-http_sounds/main/file.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-http_sounds/main/file.c?view=diff&rev=387629&r1=387628&r2=387629
==============================================================================
--- team/kmoore/stasis-http_sounds/main/file.c (original)
+++ team/kmoore/stasis-http_sounds/main/file.c Sat May 4 09:12:32 2013
@@ -68,9 +68,9 @@
static AST_RWLIST_HEAD_STATIC(formats, ast_format_def);
-#define SOUND_VARIANT_BUCKETS 13
-
-#define LANGUAGE_DIR_BUCKETS 13
+#define SOUND_VARIANT_BUCKETS 7
+
+#define LANGUAGE_DIR_BUCKETS 7
static struct ao2_container *sounds_index;
@@ -1742,22 +1742,6 @@
return orig;
}
-const char *ast_sounds_get_description(const char *filename, const char *lang)
-{
- RAII_VAR(struct sound_info *, info, ao2_find(sounds_index, filename, OBJ_KEY), ao2_cleanup);
- RAII_VAR(struct sound_variant *, variant, NULL, ao2_cleanup);
- if (!info || !lang) {
- return NULL;
- }
-
- variant = ao2_find(info->variant_list, lang, OBJ_KEY);
- if (!variant) {
- return NULL;
- }
-
- return variant->description;
-}
-
static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
#define FORMAT "%-10s %-10s %-20s\n"
@@ -1852,13 +1836,16 @@
}
/*! \brief Find or create the appropriate sound_variant and any necessary structures */
-static struct sound_variant *get_variant(const char *filename, const char *lang)
+static struct sound_variant *get_variant(const char *filename, const char *lang, int no_alloc)
{
RAII_VAR(struct sound_info *, info, NULL, ao2_cleanup);
RAII_VAR(struct sound_variant *, variant, NULL, ao2_cleanup);
info = ao2_find(sounds_index, filename, OBJ_KEY);
if (!info) {
+ if (no_alloc) {
+ return NULL;
+ }
/* This is the first time the index has seen this filename,
* allocate and link */
info = sound_info_alloc(filename);
@@ -1871,6 +1858,9 @@
variant = ao2_find(info->variant_list, lang, OBJ_KEY);
if (!variant) {
+ if (no_alloc) {
+ return NULL;
+ }
/* This is the first time the index has seen this language for
* this filename, allocate and link */
variant = sound_variant_alloc(lang);
@@ -1885,10 +1875,72 @@
return variant;
}
+char *ast_sounds_get_description(const char *filename, const char *lang)
+{
+ RAII_VAR(struct sound_variant *, variant, NULL, ao2_cleanup);
+ if (!filename || !lang) {
+ return NULL;
+ }
+
+ variant = get_variant(filename, lang, 1);
+ if (!variant) {
+ return NULL;
+ }
+
+ return ast_strdup(variant->description);
+}
+
+struct ast_format_cap *ast_sounds_get_format_cap(const char *filename, const char *lang)
+{
+ RAII_VAR(struct sound_variant *, variant, NULL, ao2_cleanup);
+ if (!filename || !lang) {
+ return NULL;
+ }
+
+ variant = get_variant(filename, lang, 1);
+ if (!variant) {
+ return NULL;
+ }
+
+ return ast_format_cap_dup(variant->formats);
+}
+
+static int add_language_cb(void *obj, void *arg, int flags)
+{
+ struct sound_variant *variant = obj;
+ struct ao2_container *languages = arg;
+ ast_str_container_add(languages, variant->language);
+ return 0;
+}
+
+struct ao2_container *ast_sounds_get_languages(const char *filename)
+{
+ RAII_VAR(struct sound_info *, info, NULL, ao2_cleanup);
+ RAII_VAR(struct ao2_container *, languages, NULL, ao2_cleanup);
+ if (!filename) {
+ return NULL;
+ }
+
+ languages = ast_str_container_alloc(LANGUAGE_DIR_BUCKETS);
+ if (!languages) {
+ return NULL;
+ }
+
+ info = ao2_find(sounds_index, filename, OBJ_KEY);
+ if (!info) {
+ return NULL;
+ }
+
+ ao2_callback(info->variant_list, OBJ_NODATA, add_language_cb, languages);
+
+ ao2_ref(languages, +1);
+ return languages;
+}
+
/*! \brief Update an index with new format/language information */
static int update_file_format_info(const char *filename, const char *lang, const struct ast_format *file_format)
{
- RAII_VAR(struct sound_variant *, variant, get_variant(filename, lang), ao2_cleanup);
+ RAII_VAR(struct sound_variant *, variant, get_variant(filename, lang, 0), ao2_cleanup);
if (!variant) {
return -1;
}
@@ -1961,7 +2013,6 @@
while (!feof(f)) {
char *file_identifier, *description;
- RAII_VAR(struct sound_variant *, variant, NULL, ao2_cleanup);
if (!fgets(buf, sizeof(buf), f)) {
continue;
}
@@ -1994,7 +2045,8 @@
} else {
/* if there's text in cumulative_description, archive it and start anew */
if (file_id_persist && !ast_strlen_zero(ast_str_buffer(cumulative_description))) {
- variant = get_variant(file_id_persist, lang);
+ RAII_VAR(struct sound_variant *, variant, NULL, ao2_cleanup);
+ variant = get_variant(file_id_persist, lang, 0);
if (!variant) {
res = -1;
break;
@@ -2015,14 +2067,14 @@
/* handle the last one */
if (file_id_persist && !ast_strlen_zero(ast_str_buffer(cumulative_description))) {
- variant = get_variant(file_id_persist, lang);
- if (!variant) {
+ RAII_VAR(struct sound_variant *, variant, NULL, ao2_cleanup);
+ variant = get_variant(file_id_persist, lang, 0);
+ if (variant) {
+ ast_string_field_set(variant, description, ast_str_buffer(cumulative_description));
+ ast_free(file_id_persist);
+ } else {
res = -1;
- break;
- }
-
- ast_string_field_set(variant, description, ast_str_buffer(cumulative_description));
- ast_free(file_id_persist);
+ }
}
fclose(f);
Modified: team/kmoore/stasis-http_sounds/res/stasis_http/resource_sounds.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-http_sounds/res/stasis_http/resource_sounds.c?view=diff&rev=387629&r1=387628&r2=387629
==============================================================================
--- team/kmoore/stasis-http_sounds/res/stasis_http/resource_sounds.c (original)
+++ team/kmoore/stasis-http_sounds/res/stasis_http/resource_sounds.c Sat May 4 09:12:32 2013
@@ -29,12 +29,41 @@
#include "resource_sounds.h"
#include "asterisk/file.h"
+#include "asterisk/format.h"
+#include "asterisk/format_cap.h"
+#include "asterisk/json.h"
+
+static int add_format_information_cb(void *obj, void *arg, void *data, int flags)
+{
+ char *language = obj;
+ struct ast_json *format_list = arg;
+ char *filename = data;
+ struct ast_format format;
+ RAII_VAR(struct ast_format_cap *, cap, ast_sounds_get_format_cap(filename, language), ast_format_cap_destroy);
+
+ ast_format_cap_iter_start(cap);
+ while (!ast_format_cap_iter_next(cap, &format)) {
+ struct ast_json *lang_format_pair = ast_json_pack("{s: s, s: s}",
+ "language", language,
+ "format", ast_getformatname(&format));
+ if (!lang_format_pair) {
+ ast_format_cap_iter_end(cap);
+ return CMP_STOP;
+ }
+
+ ast_json_array_append(format_list, lang_format_pair);
+ }
+ ast_format_cap_iter_end(cap);
+ return 0;
+}
static struct ast_json *create_sound_blob(const char *filename)
{
RAII_VAR(struct ast_json *, sound, NULL, ast_json_unref);
- const char *description;
+ RAII_VAR(struct ao2_container *, languages, NULL, ao2_cleanup);
+ RAII_VAR(char *, description, NULL, ast_free);
struct ast_json *format_lang_list;
+ char *filename_dup;
description = ast_sounds_get_description(filename, "en");
if (!description) {
@@ -56,6 +85,13 @@
return NULL;
}
+ languages = ast_sounds_get_languages(filename);
+ if (!languages || !ao2_container_count(languages)) {
+ return NULL;
+ }
+
+ filename_dup = ast_strdupa(filename);
+ ao2_callback_data(languages, OBJ_NODATA, add_format_information_cb, format_lang_list, filename_dup);
return ast_json_ref(sound);
}
Modified: team/kmoore/stasis-http_sounds/res/stasis_http/resource_sounds.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-http_sounds/res/stasis_http/resource_sounds.h?view=diff&rev=387629&r1=387628&r2=387629
==============================================================================
--- team/kmoore/stasis-http_sounds/res/stasis_http/resource_sounds.h (original)
+++ team/kmoore/stasis-http_sounds/res/stasis_http/resource_sounds.h Sat May 4 09:12:32 2013
@@ -74,7 +74,7 @@
* - id: string (required)
* - formats: List[FormatLangPair] (required)
* FormatLangPair
- * - lang: string (required)
+ * - language: string (required)
* - format: string (required)
*/
Modified: team/kmoore/stasis-http_sounds/rest-api/api-docs/sounds.json
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-http_sounds/rest-api/api-docs/sounds.json?view=diff&rev=387629&r1=387628&r2=387629
==============================================================================
--- team/kmoore/stasis-http_sounds/rest-api/api-docs/sounds.json (original)
+++ team/kmoore/stasis-http_sounds/rest-api/api-docs/sounds.json Sat May 4 09:12:32 2013
@@ -61,7 +61,7 @@
"FormatLangPair": {
"id": "FormatLangPair",
"properties": {
- "lang": {
+ "language": {
"required": true,
"type": "string"
},
More information about the asterisk-commits
mailing list