2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Kinsey Moore <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 * \brief Sound file format and description index.
28 #include "asterisk/utils.h"
29 #include "asterisk/lock.h"
30 #include "asterisk/format.h"
31 #include "asterisk/format_cap.h"
32 #include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */
33 #include "asterisk/media_index.h"
34 #include "asterisk/sounds_index.h"
35 #include "asterisk/file.h"
36 #include "asterisk/cli.h"
37 #include "asterisk/_private.h"
38 #include "asterisk/stasis_message_router.h"
39 #include "asterisk/stasis_system.h"
42 <support_level>core</support_level>
45 /*! \brief The number of buckets to be used for storing language-keyed objects */
46 #define LANGUAGE_BUCKETS 7
48 static struct ast_media_index *sounds_index;
50 static struct stasis_message_router *sounds_system_router;
52 /*! \brief Get the languages in which sound files are available */
53 static struct ao2_container *get_languages(void)
55 RAII_VAR(struct ao2_container *, lang_dirs, NULL, ao2_cleanup);
58 RAII_VAR(struct ast_str *, media_dir, ast_str_create(64), ast_free);
59 RAII_VAR(struct ast_str *, variant_dir, ast_str_create(64), ast_free);
61 lang_dirs = ast_str_container_alloc(LANGUAGE_BUCKETS);
62 if (!media_dir || !lang_dirs) {
66 ast_str_set(&media_dir, 0, "%s/sounds", ast_config_AST_DATA_DIR);
68 srcdir = opendir(ast_str_buffer(media_dir));
71 ast_log(LOG_ERROR, "Failed to open %s\n", ast_str_buffer(media_dir));
75 while((dent = readdir(srcdir)) != NULL) {
78 if(!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) {
82 ast_str_reset(variant_dir);
83 ast_str_set(&variant_dir, 0, "%s/%s", ast_str_buffer(media_dir), dent->d_name);
85 if (stat(ast_str_buffer(variant_dir), &st) < 0) {
86 ast_log(LOG_ERROR, "Failed to stat %s\n", ast_str_buffer(variant_dir));
90 if (S_ISDIR(st.st_mode)) {
91 ast_str_container_add(lang_dirs, dent->d_name);
96 ao2_ref(lang_dirs, +1);
100 /*! \brief Callback to process an individual language directory or subdirectory */
101 static int update_index_cb(void *obj, void *arg, int flags)
104 struct ast_media_index *index = arg;
106 if (ast_media_index_update(index, lang)) {
112 AST_MUTEX_DEFINE_STATIC(reload_lock);
114 int ast_sounds_reindex(void)
116 RAII_VAR(struct ast_str *, sounds_dir, NULL, ast_free);
117 RAII_VAR(struct ao2_container *, languages, NULL, ao2_cleanup);
118 RAII_VAR(char *, failed_index, NULL, ao2_cleanup);
119 RAII_VAR(struct ast_media_index *, new_index, NULL, ao2_cleanup);
120 struct ast_media_index *old_index;
122 SCOPED_MUTEX(lock, &reload_lock);
124 old_index = sounds_index;
125 languages = get_languages();
126 sounds_dir = ast_str_create(64);
128 if (!languages || !sounds_dir) {
132 ast_str_set(&sounds_dir, 0, "%s/sounds", ast_config_AST_DATA_DIR);
133 new_index = ast_media_index_create(ast_str_buffer(sounds_dir));
138 failed_index = ao2_callback(languages, 0, update_index_cb, new_index);
143 ao2_ref(new_index, +1);
144 sounds_index = new_index;
145 ao2_cleanup(old_index);
149 static int show_sounds_cb(void *obj, void *arg, int flags)
152 struct ast_cli_args *a = arg;
153 ast_cli(a->fd, "%s\n", name);
157 static int show_sound_info_cb(void *obj, void *arg, int flags)
159 char *language = obj;
160 struct ast_cli_args *a = arg;
161 struct ast_format *format;
162 int formats_shown = 0;
163 RAII_VAR(struct ast_media_index *, local_index, ast_sounds_get_index(), ao2_cleanup);
164 struct ast_format_cap *cap;
165 const char *description = ast_media_get_description(local_index, a->argv[3], language);
167 ast_cli(a->fd, " Language %s:\n", language);
168 if (!ast_strlen_zero(description)) {
169 ast_cli(a->fd, " Description: %s\n", description);
172 cap = ast_media_get_format_cap(local_index, a->argv[3], language);
175 for (x = 0; x < ast_format_cap_count(cap); x++) {
176 format = ast_format_cap_get_format(cap, x);
177 ast_cli(a->fd, " Format: %s\n", ast_format_get_name(format));
184 if (!formats_shown) {
185 ast_cli(a->fd, " No Formats Available\n");
191 /*! \brief Show a list of sounds available on the system */
192 static char *handle_cli_sounds_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
196 e->command = "core show sounds";
198 "Usage: core show sounds\n"
199 " Shows a listing of sound files available on the system.\n";
206 RAII_VAR(struct ao2_container *, sound_files, ast_media_get_media(sounds_index), ao2_cleanup);
211 ast_cli(a->fd, "Available audio files:\n");
212 ao2_callback(sound_files, OBJ_MULTIPLE | OBJ_NODATA, show_sounds_cb, a);
216 return CLI_SHOWUSAGE;
219 /*! \brief Show details about a sound available in the system */
220 static char *handle_cli_sound_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
224 e->command = "core show sound";
226 "Usage: core show sound [soundid]\n"
227 " Shows information about the specified sound.\n";
231 int length = strlen(a->word);
233 struct ao2_iterator it_sounds;
236 RAII_VAR(struct ao2_container *, sound_files, ast_media_get_media(sounds_index), ao2_cleanup);
241 it_sounds = ao2_iterator_init(sound_files, 0);
242 while ((filename = ao2_iterator_next(&it_sounds))) {
243 if (!strncasecmp(a->word, filename, length) && ++which > a->n) {
244 match = ast_strdup(filename);
245 ao2_ref(filename, -1);
248 ao2_ref(filename, -1);
250 ao2_iterator_destroy(&it_sounds);
256 RAII_VAR(struct ao2_container *, variants, ast_media_get_variants(sounds_index, a->argv[3]), ao2_cleanup);
257 if (!variants || !ao2_container_count(variants)) {
258 ast_cli(a->fd, "ERROR: File %s not found in index\n", a->argv[3]);
262 ast_cli(a->fd, "Indexed Information for %s:\n", a->argv[3]);
263 ao2_callback(variants, OBJ_MULTIPLE | OBJ_NODATA, show_sound_info_cb, a);
267 return CLI_SHOWUSAGE;
270 /*! \brief Struct for registering CLI commands */
271 static struct ast_cli_entry cli_sounds[] = {
272 AST_CLI_DEFINE(handle_cli_sounds_show, "Shows available sounds"),
273 AST_CLI_DEFINE(handle_cli_sound_show, "Shows details about a specific sound"),
276 static void sounds_cleanup(void)
278 stasis_message_router_unsubscribe_and_join(sounds_system_router);
279 sounds_system_router = NULL;
280 ast_cli_unregister_multiple(cli_sounds, ARRAY_LEN(cli_sounds));
281 ao2_cleanup(sounds_index);
285 static void format_update_cb(void *data, struct stasis_subscription *sub,
286 struct stasis_message *message)
288 ast_sounds_reindex();
291 int ast_sounds_index_init(void)
295 if (ast_sounds_reindex()) {
298 res |= ast_cli_register_multiple(cli_sounds, ARRAY_LEN(cli_sounds));
300 sounds_system_router = stasis_message_router_create(ast_system_topic());
301 if (!sounds_system_router) {
305 res |= stasis_message_router_add(
306 sounds_system_router,
307 ast_format_register_type(),
311 res |= stasis_message_router_add(
312 sounds_system_router,
313 ast_format_unregister_type(),
321 ast_register_atexit(sounds_cleanup);
325 struct ast_media_index *ast_sounds_get_index(void)
327 ao2_ref(sounds_index, +1);