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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu May 23 16:10:55 CDT 2013


Author: kmoore
Date: Thu May 23 16:10:52 2013
New Revision: 389622

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=389622
Log:
Partial refactor on the way to being more generic

Added:
    team/kmoore/stasis-http_sounds/include/asterisk/media_index.h
      - copied, changed from r389585, team/kmoore/stasis-http_sounds/include/asterisk/sounds.h
    team/kmoore/stasis-http_sounds/main/media_index.c
      - copied, changed from r389585, team/kmoore/stasis-http_sounds/main/sounds.c
Removed:
    team/kmoore/stasis-http_sounds/include/asterisk/sounds.h
    team/kmoore/stasis-http_sounds/main/sounds.c
Modified:
    team/kmoore/stasis-http_sounds/include/asterisk/_private.h
    team/kmoore/stasis-http_sounds/main/asterisk.c
    team/kmoore/stasis-http_sounds/res/stasis_http/resource_sounds.c

Modified: team/kmoore/stasis-http_sounds/include/asterisk/_private.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-http_sounds/include/asterisk/_private.h?view=diff&rev=389622&r1=389621&r2=389622
==============================================================================
--- team/kmoore/stasis-http_sounds/include/asterisk/_private.h (original)
+++ team/kmoore/stasis-http_sounds/include/asterisk/_private.h Thu May 23 16:10:52 2013
@@ -124,6 +124,6 @@
 /*! \brief initializes the rtp engine arrays */
 int ast_rtp_engine_init(void);
 
-/*! \brief initialize the sounds indexer */
-int ast_sounds_init(void);
+/*! \brief initialize the media indexer */
+int ast_media_index_init(void);
 #endif /* _ASTERISK__PRIVATE_H */

Copied: team/kmoore/stasis-http_sounds/include/asterisk/media_index.h (from r389585, team/kmoore/stasis-http_sounds/include/asterisk/sounds.h)
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-http_sounds/include/asterisk/media_index.h?view=diff&rev=389622&p1=team/kmoore/stasis-http_sounds/include/asterisk/sounds.h&r1=389585&p2=team/kmoore/stasis-http_sounds/include/asterisk/media_index.h&r2=389622
==============================================================================
--- team/kmoore/stasis-http_sounds/include/asterisk/sounds.h (original)
+++ team/kmoore/stasis-http_sounds/include/asterisk/media_index.h Thu May 23 16:10:52 2013
@@ -20,8 +20,8 @@
  * \brief Sound file format and description indexer.
  */
 
-#ifndef _ASTERISK_SOUNDS_H
-#define _ASTERISK_SOUNDS_H
+#ifndef _ASTERISK_MEDIA_INDEX_H
+#define _ASTERISK_MEDIA_INDEX_H
 
 #if defined(__cplusplus) || defined(c_plusplus)
 extern "C" {
@@ -30,44 +30,54 @@
 struct ast_format_cap;
 
 /*!
- * \brief Get the description for a sound
+ * \brief Object representing a media index
+ */
+struct ast_media_index;
+
+/*!
+ * \brief Get the description for a media file
  *
+ * \param index Media index in which to query information
  * \param filename Name of the file for which to get the description
- * \param lang Language for which to get the description
+ * \param variant Media variant for which to get the description
  *
  * \retval NULL if not found
  * \return a copy of the description (must be ast_freed)
  */
-char *ast_sounds_get_description(const char *filename, const char *lang);
+char *ast_media_get_description(struct ast_media_index *index, const char *filename, const char *variant);
 
 /*!
- * \brief Get the ast_format_cap for a sound
+ * \brief Get the ast_format_cap for a media file
  *
+ * \param index Media index in which to query information
  * \param filename Name of the file for which to get the description
- * \param lang Language for which to get the description
+ * \param variant Media variant 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);
+struct ast_format_cap *ast_media_get_format_cap(struct ast_media_index *index, const char *filename, const char *variant);
 
 /*!
- * \brief Get the languages in which a sound is available
+ * \brief Get the languages in which a media file is available
  *
+ * \param index Media index in which to query information
  * \param filename Name of the file for which to get available languages
  *
  * \retval NULL on error
  * \return an ast_str_container filled with language strings
  */
-struct ao2_container *ast_sounds_get_languages(const char *filename);
+struct ao2_container *ast_media_get_variants(struct ast_media_index *index, const char *filename);
 
 /*!
- * \brief Get the a container of all sounds available on the system
+ * \brief Get the a container of all media available on the system
+ *
+ * \param index Media index in which to query information
  *
  * \retval NULL on error
- * \return an ast_str_container filled with sound file name strings
+ * \return an ast_str_container filled with media file name strings
  */
-struct ao2_container *ast_sounds_get_sounds(void);
+struct ao2_container *ast_media_get_media(struct ast_media_index *index);
 
 /*!
  * \brief Reload the sounds index
@@ -77,8 +87,16 @@
  */
 int ast_sounds_reindex(void);
 
+/*!
+ * \brief Get the sounds index
+ *
+ * \retval sounds index
+ * \retval NULL on failure
+ */
+struct ast_media_index *ast_sounds_get_index(void);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif
 
-#endif /* _ASTERISK_SOUNDS_H */
+#endif /* _ASTERISK_MEDIA_INDEX_H */

Modified: team/kmoore/stasis-http_sounds/main/asterisk.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-http_sounds/main/asterisk.c?view=diff&rev=389622&r1=389621&r2=389622
==============================================================================
--- team/kmoore/stasis-http_sounds/main/asterisk.c (original)
+++ team/kmoore/stasis-http_sounds/main/asterisk.c Thu May 23 16:10:52 2013
@@ -4350,7 +4350,7 @@
 		exit(1);
 	}
 
-	if (ast_sounds_init()) {
+	if (ast_media_index_init()) {
 		printf("%s", term_quit());
 		exit(1);
 	}

Copied: team/kmoore/stasis-http_sounds/main/media_index.c (from r389585, team/kmoore/stasis-http_sounds/main/sounds.c)
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-http_sounds/main/media_index.c?view=diff&rev=389622&p1=team/kmoore/stasis-http_sounds/main/sounds.c&r1=389585&p2=team/kmoore/stasis-http_sounds/main/media_index.c&r2=389622
==============================================================================
--- team/kmoore/stasis-http_sounds/main/sounds.c (original)
+++ team/kmoore/stasis-http_sounds/main/media_index.c Thu May 23 16:10:52 2013
@@ -30,7 +30,7 @@
 #include "asterisk/format.h"
 #include "asterisk/format_cap.h"
 #include "asterisk/paths.h"	/* use ast_config_AST_DATA_DIR */
-#include "asterisk/sounds.h"
+#include "asterisk/media_index.h"
 #include "asterisk/file.h"
 #include "asterisk/cli.h"
 #include "asterisk/module.h"
@@ -41,33 +41,31 @@
  ***/
 
 /*! \brief The number of buckets to be used for storing language-keyed objects */
-#define LANGUAGE_BUCKETS 7
+#define VARIANT_BUCKETS 7
 
 /*! \brief The number of buckets to be used for storing sound filename-keyed objects */
-#define SOUNDS_BUCKETS 157
-
-static struct ao2_container *sounds_index;
+#define INDEX_BUCKETS 157
 
 /*! \brief Structure to hold a list of the format variations for a sound file in a specific language */
-struct sound_variant {
+struct media_variant {
 	AST_DECLARE_STRING_FIELDS(
-		AST_STRING_FIELD(language);	/*!< The language this sound is available in */
+		AST_STRING_FIELD(variant);	/*!< The variant this media is available in */
 		AST_STRING_FIELD(description);	/*!< The description of the sound */
 	);
 	struct ast_format_cap *formats;	/*!< The formats this sound is available in for this language */
 };
 
-static void sound_variant_destroy(void *obj)
-{
-	struct sound_variant *variant = obj;
+static void media_variant_destroy(void *obj)
+{
+	struct media_variant *variant = obj;
 
 	ast_string_field_free_memory(variant);
 	variant->formats = ast_format_cap_destroy(variant->formats);
 }
 
-static struct sound_variant *sound_variant_alloc(const char *language)
-{
-	RAII_VAR(struct sound_variant *, variant, ao2_alloc(sizeof(*variant), sound_variant_destroy), ao2_cleanup);
+static struct media_variant *media_variant_alloc(const char *variant_str)
+{
+	RAII_VAR(struct media_variant *, variant, ao2_alloc(sizeof(*variant), media_variant_destroy), ao2_cleanup);
 
 	if (ast_string_field_init(variant, 8)) {
 		return NULL;
@@ -78,23 +76,23 @@
 		return NULL;
 	}
 
-	ast_string_field_set(variant, language, language);
+	ast_string_field_set(variant, variant, variant_str);
 
 	ao2_ref(variant, 1);
 	return variant;
 }
 
-static int sound_variant_hash(const void *obj, const int flags)
-{
-	const char *language = (flags & OBJ_KEY) ? obj : ((struct sound_variant*) obj)->language;
+static int media_variant_hash(const void *obj, const int flags)
+{
+	const char *language = (flags & OBJ_KEY) ? obj : ((struct media_variant*) obj)->variant;
 	return ast_str_case_hash(language);
 }
 
-static int sound_variant_cmp(void *obj, void *arg, int flags)
-{
-	struct sound_variant *opt1 = obj, *opt2 = arg;
-	const char *language = (flags & OBJ_KEY) ? arg : opt2->language;
-	return strcasecmp(opt1->language, language) ? 0 : CMP_MATCH | CMP_STOP;
+static int media_variant_cmp(void *obj, void *arg, int flags)
+{
+	struct media_variant *opt1 = obj, *opt2 = arg;
+	const char *language = (flags & OBJ_KEY) ? arg : opt2->variant;
+	return strcasecmp(opt1->variant, language) ? 0 : CMP_MATCH | CMP_STOP;
 }
 
 /*! \brief Structure to hold information about a sound file */
@@ -122,7 +120,7 @@
 		return NULL;
 	}
 
-	info->variant_list = ao2_container_alloc(LANGUAGE_BUCKETS, sound_variant_hash, sound_variant_cmp);
+	info->variant_list = ao2_container_alloc(VARIANT_BUCKETS, media_variant_hash, media_variant_cmp);
 	if (!info->variant_list) {
 		return NULL;
 	}
@@ -146,25 +144,51 @@
 	return strcasecmp(opt1->name, name) ? 0 : CMP_MATCH | CMP_STOP;
 }
 
+struct ast_media_index {
+	struct ao2_container *index;
+};
+
+static void media_index_dtor(void *obj)
+{
+	struct ast_media_index *index = obj;
+	ao2_cleanup(index->index);
+	index->index = NULL;
+}
+
+static struct ast_media_index *media_index_alloc(void)
+{
+	RAII_VAR(struct ast_media_index *, index, ao2_alloc(sizeof(*index), media_index_dtor), ao2_cleanup);
+
+	index->index = ao2_container_alloc(INDEX_BUCKETS, sound_info_hash, sound_info_cmp);
+	if (!index->index) {
+		return NULL;
+	}
+
+	ao2_ref(index, +1);
+	return index;
+}
+
+static struct ast_media_index *sounds_index;
+
 /*! \brief Get the languages in which sound files are available */
 static struct ao2_container *get_languages(void)
 {
 	RAII_VAR(struct ao2_container *, lang_dirs, NULL, ao2_cleanup);
 	struct dirent* dent;
 	DIR* srcdir;
-	RAII_VAR(struct ast_str *, sounds_dir, ast_str_create(64), ast_free);
-
-	lang_dirs = ast_str_container_alloc(LANGUAGE_BUCKETS);
-	if (!sounds_dir || !lang_dirs) {
-		return NULL;
-	}
-
-	ast_str_set(&sounds_dir, 0, "%s/sounds", ast_config_AST_DATA_DIR);
-
-	srcdir = opendir(ast_str_buffer(sounds_dir));
+	RAII_VAR(struct ast_str *, media_dir, ast_str_create(64), ast_free);
+
+	lang_dirs = ast_str_container_alloc(VARIANT_BUCKETS);
+	if (!media_dir || !lang_dirs) {
+		return NULL;
+	}
+
+	ast_str_set(&media_dir, 0, "%s/sounds", ast_config_AST_DATA_DIR);
+
+	srcdir = opendir(ast_str_buffer(media_dir));
 
 	if (srcdir == NULL) {
-		ast_log(LOG_ERROR, "Failed to open %s\n", ast_str_buffer(sounds_dir));
+		ast_log(LOG_ERROR, "Failed to open %s\n", ast_str_buffer(media_dir));
 		return NULL;
 	}
 
@@ -190,11 +214,11 @@
 	return lang_dirs;
 }
 
-static struct sound_variant *find_variant(const char *filename, const char *lang)
+static struct media_variant *find_variant(struct ast_media_index *index, const char *filename, const char *lang)
 {
 	RAII_VAR(struct sound_info *, info, NULL, ao2_cleanup);
 
-	info = ao2_find(sounds_index, filename, OBJ_KEY);
+	info = ao2_find(index->index, filename, OBJ_KEY);
 	if (!info) {
 		return NULL;
 	}
@@ -202,13 +226,13 @@
 	return ao2_find(info->variant_list, lang, OBJ_KEY);
 }
 
-/*! \brief create the appropriate sound_variant and any necessary structures */
-static struct sound_variant *alloc_variant(const char *filename, const char *lang)
+/*! \brief create the appropriate media_variant and any necessary structures */
+static struct media_variant *alloc_variant(struct ast_media_index *index, const char *filename, const char *variant_str)
 {
 	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);
+	RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
+
+	info = ao2_find(index->index, filename, OBJ_KEY);
 	if (!info) {
 		/* This is the first time the index has seen this filename,
 		 * allocate and link */
@@ -217,14 +241,14 @@
 			return NULL;
 		}
 
-		ao2_link(sounds_index, info);
-	}
-
-	variant = ao2_find(info->variant_list, lang, OBJ_KEY);
+		ao2_link(index->index, info);
+	}
+
+	variant = ao2_find(info->variant_list, variant_str, OBJ_KEY);
 	if (!variant) {
 		/* This is the first time the index has seen this language for
 		 * this filename, allocate and link */
-		variant = sound_variant_alloc(lang);
+		variant = media_variant_alloc(variant_str);
 		if (!variant) {
 			return NULL;
 		}
@@ -236,15 +260,14 @@
 	return variant;
 }
 
-/*! \brief Get the description associated with the sound filename for the given language if available */
-char *ast_sounds_get_description(const char *filename, const char *lang)
-{
-	RAII_VAR(struct sound_variant *, variant, NULL, ao2_cleanup);
-	if (ast_strlen_zero(filename) || ast_strlen_zero(lang)) {
-		return NULL;
-	}
-
-	variant = find_variant(filename, lang);
+char *ast_media_get_description(struct ast_media_index *index, const char *filename, const char *variant_str)
+{
+	RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
+	if (ast_strlen_zero(filename) || ast_strlen_zero(variant_str)) {
+		return NULL;
+	}
+
+	variant = find_variant(index, filename, variant_str);
 	if (!variant) {
 		return NULL;
 	}
@@ -252,15 +275,14 @@
 	return ast_strdup(variant->description);
 }
 
-/*! \brief Get the ast_format_cap associated with the sound filename and language if available */
-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 (ast_strlen_zero(filename) || ast_strlen_zero(lang)) {
-		return NULL;
-	}
-
-	variant = find_variant(filename, lang);
+struct ast_format_cap *ast_media_get_format_cap(struct ast_media_index *index, const char *filename, const char *variant_str)
+{
+	RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
+	if (ast_strlen_zero(filename) || ast_strlen_zero(variant_str)) {
+		return NULL;
+	}
+
+	variant = find_variant(index, filename, variant_str);
 	if (!variant) {
 		return NULL;
 	}
@@ -271,68 +293,66 @@
 /*! \brief Add the variant's language to the list of languages requested */
 static int add_language_cb(void *obj, void *arg, int flags)
 {
-	struct sound_variant *variant = obj;
+	struct media_variant *variant = obj;
 	struct ao2_container *languages = arg;
-	ast_str_container_add(languages, variant->language);
-	return 0;
-}
-
-/*! \brief Return the requested container of languages */
-struct ao2_container *ast_sounds_get_languages(const char *filename)
+	ast_str_container_add(languages, variant->variant);
+	return 0;
+}
+
+struct ao2_container *ast_media_get_variants(struct ast_media_index *index, const char *filename)
 {
 	RAII_VAR(struct sound_info *, info, NULL, ao2_cleanup);
-	RAII_VAR(struct ao2_container *, languages, NULL, ao2_cleanup);
+	RAII_VAR(struct ao2_container *, variants, NULL, ao2_cleanup);
 	if (!filename) {
 		return NULL;
 	}
 
-	languages = ast_str_container_alloc(LANGUAGE_BUCKETS);
-	if (!languages) {
-		return NULL;
-	}
-
-	info = ao2_find(sounds_index, filename, OBJ_KEY);
+	variants = ast_str_container_alloc(VARIANT_BUCKETS);
+	if (!variants) {
+		return NULL;
+	}
+
+	info = ao2_find(index->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;
+	ao2_callback(info->variant_list, OBJ_NODATA, add_language_cb, variants);
+
+	ao2_ref(variants, +1);
+	return variants;
 }
 
 /*! \brief Add the sound_info's sound filename to the container of filenames requested */
 static int add_sound_cb(void *obj, void *arg, int flags)
 {
 	struct sound_info *info = obj;
-	struct ao2_container *sounds = arg;
-	ast_str_container_add(sounds, info->name);
-	return 0;
-}
-
-/*! \brief Return the requested container of sounds */
-struct ao2_container *ast_sounds_get_sounds(void)
-{
-	RAII_VAR(struct ao2_container *, sounds, NULL, ao2_cleanup);
-
-	sounds = ast_str_container_alloc(SOUNDS_BUCKETS);
-	if (!sounds) {
-		return NULL;
-	}
-
-	ao2_callback(sounds_index, OBJ_NODATA, add_sound_cb, sounds);
-
-	ao2_ref(sounds, +1);
-	return sounds;
+	struct ao2_container *media = arg;
+	ast_str_container_add(media, info->name);
+	return 0;
+}
+
+struct ao2_container *ast_media_get_media(struct ast_media_index *index)
+{
+	RAII_VAR(struct ao2_container *, media, NULL, ao2_cleanup);
+
+	media = ast_str_container_alloc(INDEX_BUCKETS);
+	if (!media) {
+		return NULL;
+	}
+
+	ao2_callback(index->index, OBJ_NODATA, add_sound_cb, media);
+
+	ao2_ref(media, +1);
+	return media;
 }
 
 /*! \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, find_variant(filename, lang), ao2_cleanup);
+static int update_file_format_info(struct ast_media_index *index, const char *filename, const char *lang, const struct ast_format *file_format)
+{
+	RAII_VAR(struct media_variant *, variant, find_variant(index, filename, lang), ao2_cleanup);
 	if (!variant) {
-		variant = alloc_variant(filename, lang);
+		variant = alloc_variant(index, filename, lang);
 		if (!variant) {
 			return -1;
 		}
@@ -343,7 +363,7 @@
 }
 
 /*! \brief Process a sound file into the index */
-static int process_sound_file(const char *lang, const char *subdir, const char *filename_stripped, const char *ext)
+static int process_sound_file(struct ast_media_index *index, const char *variant, const char *subdir, const char *filename_stripped, const char *ext)
 {
 	const struct ast_format *file_format;
 	const char *file_identifier = filename_stripped;
@@ -366,19 +386,19 @@
 		file_identifier = ast_str_buffer(file_id_str);
 	}
 
-	if (update_file_format_info(file_identifier, lang, file_format)) {
+	if (update_file_format_info(index, file_identifier, variant, file_format)) {
 		return -1;
 	}
 	return 0;
 }
 
 /*!
- * \brief Process a sounds description text file
+ * \brief Process a media description text file
  *
  * This currently processes core-sounds-*.txt and extra-sounds-*.txt, but will
  * process others if present.
  */
-static int process_description_file(const char *lang, const char *filename)
+static int process_description_file(struct ast_media_index *index, 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);
@@ -395,7 +415,7 @@
 		return -1;
 	}
 
-	ast_str_set(&description_file_path, 0, "%s/sounds/%s/%s", ast_config_AST_DATA_DIR, lang, filename);
+	ast_str_set(&description_file_path, 0, "%s/sounds/%s/%s", ast_config_AST_DATA_DIR, variant_str, filename);
 	f = fopen(ast_str_buffer(description_file_path), "r");
 	if (!f) {
 		ast_log(LOG_WARNING, "Could not open sound description file '%s'\n", ast_str_buffer(description_file_path));
@@ -442,10 +462,10 @@
 		} 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))) {
-				RAII_VAR(struct sound_variant *, variant, NULL, ao2_cleanup);
-				variant = find_variant(file_id_persist, lang);
+				RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
+				variant = find_variant(index, file_id_persist, variant_str);
 				if (!variant) {
-					variant = alloc_variant(filename, lang);
+					variant = alloc_variant(index, filename, variant_str);
 					if (!variant) {
 						res = -1;
 						break;
@@ -467,10 +487,10 @@
 
 	/* handle the last one */
 	if (file_id_persist && !ast_strlen_zero(ast_str_buffer(cumulative_description))) {
-		RAII_VAR(struct sound_variant *, variant, NULL, ao2_cleanup);
-		variant = find_variant(file_id_persist, lang);
+		RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
+		variant = find_variant(index, file_id_persist, variant_str);
 		if (!variant) {
-			variant = alloc_variant(filename, lang);
+			variant = alloc_variant(index, filename, variant_str);
 		}
 
 		if (variant) {
@@ -486,7 +506,7 @@
 }
 
 /*! \brief process an individual file listing */
-static int process_file(const char *lang, const char *subdir, const char *filename)
+static int process_file(struct ast_media_index *index, const char *variant_str, const char *subdir, const char *filename)
 {
 	RAII_VAR(char *, filename_stripped, ast_strdup(filename), ast_free);
 	char *ext;
@@ -503,11 +523,11 @@
 
 	*ext++ = '\0';
 	if (!strcmp(ext, "txt")) {
-		if (!subdir && process_description_file(lang, filename)) {
+		if (!subdir && process_description_file(index, variant_str, filename)) {
 			return -1;
 		}
 	} else {
-		if (process_sound_file(lang, subdir, filename_stripped, ext)) {
+		if (process_sound_file(index, variant_str, subdir, filename_stripped, ext)) {
 			return -1;
 		}
 	}
@@ -554,11 +574,12 @@
 		}
 
 		if (S_ISDIR(st.st_mode)) {
-			/* only recurse one level */
 			if (!subdir) {
-				update_index_cb(lang, dent->d_name, 0);
+				update_index_cb(lang, dent->d_name, flags);
 			} else {
-				ast_log(LOG_ERROR, "Not recursing again on lang %s subdir %s, subsubdir %s\n", lang, subdir, dent->d_name);
+				RAII_VAR(struct ast_str *, new_subdir, ast_str_create(64), ast_free);
+				ast_str_set(&new_subdir, 0, "%s/%s", subdir, dent->d_name);
+				update_index_cb(lang, ast_str_buffer(new_subdir), flags);
 			}
 			continue;
 		}
@@ -567,7 +588,7 @@
 			continue;
 		}
 
-		if (process_file(lang, subdir, dent->d_name)) {
+		if (process_file(sounds_index, lang, subdir, dent->d_name)) {
 			res = -1;
 			break;
 		}
@@ -577,7 +598,7 @@
 	return res;
 }
 
-/*! \brief Reindex sounds and sound descriptions */
+/*! \brief Index sounds and sound descriptions */
 static int index_sounds(void)
 {
 	RAII_VAR(struct ao2_container *, languages, get_languages(), ao2_cleanup);
@@ -586,7 +607,7 @@
 		return -1;
 	}
 
-	sounds_index = ao2_container_alloc(SOUNDS_BUCKETS, sound_info_hash, sound_info_cmp);
+	sounds_index = media_index_alloc();
 	if (!sounds_index) {
 		return -1;
 	}
@@ -622,11 +643,11 @@
 
 static int show_sound_info_cb(void *obj, void *arg, int flags)
 {
-	struct sound_variant *variant = obj;
+	struct media_variant *variant = obj;
 	struct ast_cli_args *a = arg;
         struct ast_format format;
 
-	ast_cli(a->fd, "  Language %s:\n", variant->language);
+	ast_cli(a->fd, "  Language %s:\n", variant->variant);
 	ast_cli(a->fd, "    Description: %s\n", variant->description);
 
         ast_format_cap_iter_start(variant->formats);
@@ -683,7 +704,7 @@
 		char *match = NULL;
 		struct sound_info *info;
 
-		it_sounds = ao2_iterator_init(sounds_index, 0);
+		it_sounds = ao2_iterator_init(sounds_index->index, 0);
                 while ((info = ao2_iterator_next(&it_sounds))) {
                         if (!strncasecmp(a->word, info->name, length) && ++which > a->n) {
                                 match = ast_strdup(info->name);
@@ -699,12 +720,12 @@
 
 	if (a->argc == 2) {
 		ast_cli(a->fd, "Available audio files:\n");
-		ao2_callback(sounds_index, OBJ_MULTIPLE | OBJ_NODATA, show_sounds_cb, a);
+		ao2_callback(sounds_index->index, OBJ_MULTIPLE | OBJ_NODATA, show_sounds_cb, a);
 		return CLI_SUCCESS;
 	}
 
 	if (a->argc == 3) {
-		RAII_VAR(struct sound_info *, info, ao2_find(sounds_index, a->argv[2], OBJ_KEY), ao2_cleanup);
+		RAII_VAR(struct sound_info *, info, ao2_find(sounds_index->index, a->argv[2], OBJ_KEY), ao2_cleanup);
 		if (!info) {
 			ast_cli(a->fd, "ERROR: File %s not found in index\n", a->argv[2]);
 			return CLI_FAILURE;
@@ -724,20 +745,26 @@
 	AST_CLI_DEFINE(handle_cli_sounds_reload, "Reload sounds index"),
 };
 
-static void sounds_cleanup(void)
+static void media_cleanup(void)
 {
 	ast_cli_unregister_multiple(cli_sounds, ARRAY_LEN(cli_sounds));
 	drop_sound_index();
 }
 
-int ast_sounds_init(void)
-{
+int ast_media_index_init(void)
+{
+	ao2_cleanup(sounds_index);
 	sounds_index = NULL;
 	if (index_sounds()) {
 		return -1;
 	}
 	ast_cli_register_multiple(cli_sounds, ARRAY_LEN(cli_sounds));
-	ast_register_atexit(sounds_cleanup);
-	return 0;
-}
-
+	ast_register_atexit(media_cleanup);
+	return 0;
+}
+
+struct ast_media_index *ast_sounds_get_index(void)
+{
+	ao2_ref(sounds_index, +1);
+	return sounds_index;
+}

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=389622&r1=389621&r2=389622
==============================================================================
--- team/kmoore/stasis-http_sounds/res/stasis_http/resource_sounds.c (original)
+++ team/kmoore/stasis-http_sounds/res/stasis_http/resource_sounds.c Thu May 23 16:10:52 2013
@@ -28,7 +28,7 @@
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "resource_sounds.h"
-#include "asterisk/sounds.h"
+#include "asterisk/media_index.h"
 #include "asterisk/format.h"
 #include "asterisk/format_cap.h"
 #include "asterisk/json.h"
@@ -46,7 +46,17 @@
 	char *language = obj;
 	struct lang_format_info *args = arg;
 	struct ast_format format;
-	RAII_VAR(struct ast_format_cap *, cap, ast_sounds_get_format_cap(args->filename, language), ast_format_cap_destroy);
+	RAII_VAR(struct ast_format_cap *, cap, NULL, ast_format_cap_destroy);
+	RAII_VAR(struct ast_media_index *, sounds_index, ast_sounds_get_index(), ao2_cleanup);
+
+	if (!sounds_index) {
+		return CMP_STOP;
+	}
+
+	cap = ast_media_get_format_cap(sounds_index, args->filename, language);
+	if (!cap) {
+		return CMP_STOP;
+	}
 
 	ast_format_cap_iter_start(cap);
 	while (!ast_format_cap_iter_next(cap, &format)) {
@@ -91,8 +101,13 @@
 	RAII_VAR(char *, description, NULL, ast_free);
 	struct ast_json *format_lang_list;
 	struct lang_format_info info;
-
-	description = ast_sounds_get_description(filename, "en");
+	RAII_VAR(struct ast_media_index *, sounds_index, ast_sounds_get_index(), ao2_cleanup);
+
+	if (!sounds_index) {
+		return NULL;
+	}
+
+	description = ast_media_get_description(sounds_index, filename, "en");
 	if (ast_strlen_zero(description)) {
 		sound = ast_json_pack("{s: s, s: []}",
 			"id", filename,
@@ -112,7 +127,7 @@
 		return NULL;
 	}
 
-	languages = ast_sounds_get_languages(filename);
+	languages = ast_media_get_variants(sounds_index, filename);
 	if (!languages || !ao2_container_count(languages)) {
 		return NULL;
 	}
@@ -161,15 +176,23 @@
 {
 	RAII_VAR(struct ao2_container *, sound_files, NULL, ao2_cleanup);
 	struct ast_json *sounds_blob;
-
-	sound_files = ast_sounds_get_sounds();
+	RAII_VAR(struct ast_media_index *, sounds_index, ast_sounds_get_index(), ao2_cleanup);
+
+	if (!sounds_index) {
+		stasis_http_response_error(response, 500, "Internal Error", "Sounds index not available");
+		return;
+	}
+
+	sound_files = ast_media_get_media(sounds_index);
 	if (!sound_files) {
 		stasis_http_response_error(response, 500, "Internal Error", "Allocation Error");
+		return;
 	}
 
 	sounds_blob = ast_json_array_create();
 	if (!sounds_blob) {
 		stasis_http_response_error(response, 500, "Internal Error", "Allocation Error");
+		return;
 	}
 
 	ao2_callback_data(sound_files, OBJ_NODATA, append_sound_cb, sounds_blob, args);




More information about the asterisk-commits mailing list