[asterisk-commits] kmoore: branch kmoore/stasis-http_sounds r389729 - in /team/kmoore/stasis-htt...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri May 24 13:39:40 CDT 2013


Author: kmoore
Date: Fri May 24 13:39:36 2013
New Revision: 389729

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=389729
Log:
Fix a bug and add auto-updating

Fix a bug in media indexing where the name of the description file was
used instead of the file being described.

Add format update messages pushed onto the system topic
(ast_system_topic()) so indexes can listen and update themselves
automatically.

Modified:
    team/kmoore/stasis-http_sounds/include/asterisk/format.h
    team/kmoore/stasis-http_sounds/main/file.c
    team/kmoore/stasis-http_sounds/main/media_index.c
    team/kmoore/stasis-http_sounds/main/sounds_index.c

Modified: team/kmoore/stasis-http_sounds/include/asterisk/format.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-http_sounds/include/asterisk/format.h?view=diff&rev=389729&r1=389728&r2=389729
==============================================================================
--- team/kmoore/stasis-http_sounds/include/asterisk/format.h (original)
+++ team/kmoore/stasis-http_sounds/include/asterisk/format.h Fri May 24 13:39:36 2013
@@ -471,4 +471,22 @@
  * \brief Get the best slinear format id for a given sample rate
  */
 enum ast_format_id ast_format_slin_by_rate(unsigned int rate);
+
+/*!
+ * \since 12
+ * \brief Get the message type used for signaling a format registration
+ *
+ * \retval Stasis message type for format registration
+ * \retval NULL on error
+ */
+struct stasis_message_type *ast_format_register_type(void);
+
+/*!
+ * \since 12
+ * \brief Get the message type used for signaling a format unregistration
+ *
+ * \retval Stasis message type for format unregistration
+ * \retval NULL on error
+ */
+struct stasis_message_type *ast_format_unregister_type(void);
 #endif /* _AST_FORMAT_H */

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=389729&r1=389728&r2=389729
==============================================================================
--- team/kmoore/stasis-http_sounds/main/file.c (original)
+++ team/kmoore/stasis-http_sounds/main/file.c Fri May 24 13:39:36 2013
@@ -51,6 +51,8 @@
 #include "asterisk/module.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/test.h"
+#include "asterisk/stasis.h"
+#include "asterisk/json.h"
 
 /*! \brief
  * The following variable controls the layout of localized sound files.
@@ -66,6 +68,55 @@
 
 static AST_RWLIST_HEAD_STATIC(formats, ast_format_def);
 
+STASIS_MESSAGE_TYPE_DEFN(ast_format_register_type);
+STASIS_MESSAGE_TYPE_DEFN(ast_format_unregister_type);
+
+static struct ast_json *json_array_from_list(const char *list, const char *sep)
+{
+	RAII_VAR(struct ast_json *, array, ast_json_array_create(), ast_json_unref);
+	RAII_VAR(char *, stringp, ast_strdup(list), ast_free);
+	char *ext;
+
+	while ((ext = strsep(&stringp, sep))) {
+		struct ast_json *ext_str = ast_json_string_create(ext);
+		if (!ext_str) {
+			return NULL;
+		}
+
+		/* append consumes the reference */
+		ast_json_array_append(array, ext_str);
+	}
+
+	ast_json_ref(array);
+	return array;
+}
+
+static int publish_format_update(const struct ast_format_def *f, struct stasis_message_type *type)
+{
+	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
+
+	json_object = ast_json_pack("{s: s, s: o}",
+		"format", f->name,
+		"extensions", json_array_from_list(f->exts, "|"));
+	if (!json_object) {
+		return -1;
+	}
+
+	if (!(json_payload = ast_json_payload_create(json_object))) {
+		return -1;
+	}
+
+	msg = stasis_message_create(type, json_payload);
+	if (!msg) {
+		return -1;
+	}
+
+	stasis_publish(ast_system_topic(), msg);
+	return 0;
+}
+
 int __ast_format_def_register(const struct ast_format_def *f, struct ast_module *mod)
 {
 	struct ast_format_def *tmp;
@@ -99,6 +150,7 @@
 	AST_RWLIST_INSERT_HEAD(&formats, tmp, list);
 	AST_RWLIST_UNLOCK(&formats);
 	ast_verb(2, "Registered file format %s, extension(s) %s\n", f->name, f->exts);
+	publish_format_update(f, ast_format_register_type());
 
 	return 0;
 }
@@ -112,6 +164,7 @@
 	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
 		if (!strcasecmp(name, tmp->name)) {
 			AST_RWLIST_REMOVE_CURRENT(list);
+			publish_format_update(tmp, ast_format_unregister_type());
 			ast_free(tmp);
 			res = 0;
 		}
@@ -1692,10 +1745,14 @@
 static void file_shutdown(void)
 {
 	ast_cli_unregister_multiple(cli_file, ARRAY_LEN(cli_file));
+	STASIS_MESSAGE_TYPE_CLEANUP(ast_format_register_type);
+	STASIS_MESSAGE_TYPE_CLEANUP(ast_format_unregister_type);
 }
 
 int ast_file_init(void)
 {
+	STASIS_MESSAGE_TYPE_INIT(ast_format_register_type);
+	STASIS_MESSAGE_TYPE_INIT(ast_format_unregister_type);
 	ast_cli_register_multiple(cli_file, ARRAY_LEN(cli_file));
 	ast_register_atexit(file_shutdown);
 	return 0;

Modified: team/kmoore/stasis-http_sounds/main/media_index.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-http_sounds/main/media_index.c?view=diff&rev=389729&r1=389728&r2=389729
==============================================================================
--- team/kmoore/stasis-http_sounds/main/media_index.c (original)
+++ team/kmoore/stasis-http_sounds/main/media_index.c Fri May 24 13:39:36 2013
@@ -358,7 +358,11 @@
  * This currently processes core-sounds-*.txt and extra-sounds-*.txt, but will
  * process others if present.
  */
-static int process_description_file(struct ast_media_index *index, const char *base_dir, const char *variant_str, const char *filename)
+static int process_description_file(struct ast_media_index *index,
+	const char *base_dir,
+	const char *subdir,
+	const char *variant_str,
+	const char *filename)
 {
 	RAII_VAR(struct ast_str *, description_file_path, ast_str_create(64), ast_free);
 	RAII_VAR(struct ast_str *, cumulative_description, ast_str_create(64), ast_free);
@@ -375,7 +379,11 @@
 		return -1;
 	}
 
-	ast_str_set(&description_file_path, 0, "%s/%s/%s", base_dir, variant_str, filename);
+	if (ast_strlen_zero(subdir)) {
+		ast_str_set(&description_file_path, 0, "%s/%s/%s", base_dir, variant_str, filename);
+	} else {
+		ast_str_set(&description_file_path, 0, "%s/%s/%s/%s", base_dir, variant_str, subdir, filename);
+	}
 	f = fopen(ast_str_buffer(description_file_path), "r");
 	if (!f) {
 		ast_log(LOG_WARNING, "Could not open media description file '%s'\n", ast_str_buffer(description_file_path));
@@ -425,7 +433,7 @@
 				RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
 				variant = find_variant(index, file_id_persist, variant_str);
 				if (!variant) {
-					variant = alloc_variant(index, filename, variant_str);
+					variant = alloc_variant(index, file_id_persist, variant_str);
 					if (!variant) {
 						res = -1;
 						break;
@@ -450,7 +458,7 @@
 		RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
 		variant = find_variant(index, file_id_persist, variant_str);
 		if (!variant) {
-			variant = alloc_variant(index, filename, variant_str);
+			variant = alloc_variant(index, file_id_persist, variant_str);
 		}
 
 		if (variant) {
@@ -483,7 +491,7 @@
 
 	*ext++ = '\0';
 	if (!strcmp(ext, "txt")) {
-		if (!subdir && process_description_file(index, base_dir, variant_str, filename)) {
+		if (process_description_file(index, base_dir, subdir, variant_str, filename)) {
 			return -1;
 		}
 	} else {

Modified: team/kmoore/stasis-http_sounds/main/sounds_index.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-http_sounds/main/sounds_index.c?view=diff&rev=389729&r1=389728&r2=389729
==============================================================================
--- team/kmoore/stasis-http_sounds/main/sounds_index.c (original)
+++ team/kmoore/stasis-http_sounds/main/sounds_index.c Fri May 24 13:39:36 2013
@@ -35,6 +35,7 @@
 #include "asterisk/file.h"
 #include "asterisk/cli.h"
 #include "asterisk/_private.h"
+#include "asterisk/stasis_message_router.h"
 
 /*** MODULEINFO
 	<support_level>core</support_level>
@@ -44,6 +45,8 @@
 #define LANGUAGE_BUCKETS 7
 
 static struct ast_media_index *sounds_index;
+
+static struct stasis_message_router *sounds_system_router;
 
 /*! \brief Get the languages in which sound files are available */
 static struct ao2_container *get_languages(void)
@@ -141,6 +144,7 @@
 	char *language = obj;
 	struct ast_cli_args *a = arg;
         struct ast_format format;
+	int formats_shown = 0;
 	RAII_VAR(struct ast_format_cap *, cap, NULL, ast_format_cap_destroy);
 	RAII_VAR(char *, description, ast_media_get_description(sounds_index, a->argv[2], language), ast_free);
 
@@ -153,8 +157,13 @@
         ast_format_cap_iter_start(cap);
         while (!ast_format_cap_iter_next(cap, &format)) {
 		ast_cli(a->fd, "    Format: %s\n", ast_getformatname(&format));
+		formats_shown = 1;
         }
         ast_format_cap_iter_end(cap);
+
+	if (!formats_shown) {
+		ast_cli(a->fd, "    No Formats Available\n");
+	}
 
 	return 0;
 }
@@ -261,13 +270,42 @@
 	sounds_index = NULL;
 }
 
+static void format_update_cb(void *data, struct stasis_subscription *sub,
+	struct stasis_topic *topic, struct stasis_message *message)
+{
+	ast_sounds_reindex();
+}
+
 int ast_sounds_index_init(void)
 {
+	int res = 0;
 	sounds_index = NULL;
 	if (ast_sounds_reindex()) {
 		return -1;
 	}
-	ast_cli_register_multiple(cli_sounds, ARRAY_LEN(cli_sounds));
+	res |= ast_cli_register_multiple(cli_sounds, ARRAY_LEN(cli_sounds));
+
+	sounds_system_router = stasis_message_router_create(ast_system_topic());
+	if (!sounds_system_router) {
+		return -1;
+	}
+
+	res |= stasis_message_router_add(
+		sounds_system_router,
+		ast_format_register_type(),
+		format_update_cb,
+		NULL);
+
+	res |= stasis_message_router_add(
+		sounds_system_router,
+		ast_format_unregister_type(),
+		format_update_cb,
+		NULL);
+
+	if (res) {
+		return -1;
+	}
+
 	ast_register_atexit(sounds_cleanup);
 	return 0;
 }




More information about the asterisk-commits mailing list