bc5263908e563869c2938ea29f6e5c84c20bb31c
[asterisk/asterisk.git] / main / sounds_index.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Kinsey Moore <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  * \brief Sound file format and description index.
21  */
22
23 #include "asterisk.h"
24
25 #include <dirent.h>
26 #include <sys/stat.h>
27
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"
40
41 /*** MODULEINFO
42         <support_level>core</support_level>
43  ***/
44
45 /*! \brief The number of buckets to be used for storing language-keyed objects */
46 #define LANGUAGE_BUCKETS 7
47
48 static struct ast_media_index *sounds_index;
49
50 static struct stasis_message_router *sounds_system_router;
51
52 /*! \brief Get the languages in which sound files are available */
53 static struct ao2_container *get_languages(void)
54 {
55         RAII_VAR(struct ao2_container *, lang_dirs, NULL, ao2_cleanup);
56         struct dirent* dent;
57         DIR* srcdir;
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);
60
61         lang_dirs = ast_str_container_alloc(LANGUAGE_BUCKETS);
62         if (!media_dir || !lang_dirs) {
63                 return NULL;
64         }
65
66         ast_str_set(&media_dir, 0, "%s/sounds", ast_config_AST_DATA_DIR);
67
68         srcdir = opendir(ast_str_buffer(media_dir));
69
70         if (srcdir == NULL) {
71                 ast_log(LOG_ERROR, "Failed to open %s\n", ast_str_buffer(media_dir));
72                 return NULL;
73         }
74
75         while((dent = readdir(srcdir)) != NULL) {
76                 struct stat st;
77
78                 if(!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) {
79                         continue;
80                 }
81
82                 ast_str_reset(variant_dir);
83                 ast_str_set(&variant_dir, 0, "%s/%s", ast_str_buffer(media_dir), dent->d_name);
84
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));
87                         continue;
88                 }
89
90                 if (S_ISDIR(st.st_mode)) {
91                         ast_str_container_add(lang_dirs, dent->d_name);
92                 }
93         }
94
95         closedir(srcdir);
96         ao2_ref(lang_dirs, +1);
97         return lang_dirs;
98 }
99
100 /*! \brief Callback to process an individual language directory or subdirectory */
101 static int update_index_cb(void *obj, void *arg, int flags)
102 {
103         char *lang = obj;
104         struct ast_media_index *index = arg;
105
106         if (ast_media_index_update(index, lang)) {
107                 return CMP_MATCH;
108         }
109         return 0;
110 }
111
112 AST_MUTEX_DEFINE_STATIC(reload_lock);
113
114 int ast_sounds_reindex(void)
115 {
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;
121
122         SCOPED_MUTEX(lock, &reload_lock);
123
124         old_index = sounds_index;
125         languages = get_languages();
126         sounds_dir = ast_str_create(64);
127
128         if (!languages || !sounds_dir) {
129                 return -1;
130         }
131
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));
134         if (!new_index) {
135                 return -1;
136         }
137
138         failed_index = ao2_callback(languages, 0, update_index_cb, new_index);
139         if (failed_index) {
140                 return -1;
141         }
142
143         ao2_ref(new_index, +1);
144         sounds_index = new_index;
145         ao2_cleanup(old_index);
146         return 0;
147 }
148
149 static int show_sounds_cb(void *obj, void *arg, int flags)
150 {
151         char *name = obj;
152         struct ast_cli_args *a = arg;
153         ast_cli(a->fd, "%s\n", name);
154         return 0;
155 }
156
157 static int show_sound_info_cb(void *obj, void *arg, int flags)
158 {
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);
166
167         ast_cli(a->fd, "  Language %s:\n", language);
168         if (!ast_strlen_zero(description)) {
169                 ast_cli(a->fd, "    Description: %s\n", description);
170         }
171
172         cap = ast_media_get_format_cap(local_index, a->argv[3], language);
173         if (cap) {
174                 int x;
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));
178                         ao2_ref(format, -1);
179                         formats_shown = 1;
180                 }
181                 ao2_ref(cap, -1);
182         }
183
184         if (!formats_shown) {
185                 ast_cli(a->fd, "    No Formats Available\n");
186         }
187
188         return 0;
189 }
190
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)
193 {
194         switch (cmd) {
195         case CLI_INIT:
196                 e->command = "core show sounds";
197                 e->usage =
198                         "Usage: core show sounds\n"
199                         "       Shows a listing of sound files available on the system.\n";
200                 return NULL;
201         case CLI_GENERATE:
202                 return NULL;
203         }
204
205         if (a->argc == 3) {
206                 RAII_VAR(struct ao2_container *, sound_files, ast_media_get_media(sounds_index), ao2_cleanup);
207                 if (!sound_files) {
208                         return CLI_FAILURE;
209                 }
210
211                 ast_cli(a->fd, "Available audio files:\n");
212                 ao2_callback(sound_files, OBJ_MULTIPLE | OBJ_NODATA, show_sounds_cb, a);
213                 return CLI_SUCCESS;
214         }
215
216         return CLI_SHOWUSAGE;
217 }
218
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)
221 {
222         switch (cmd) {
223         case CLI_INIT:
224                 e->command = "core show sound";
225                 e->usage =
226                         "Usage: core show sound [soundid]\n"
227                         "       Shows information about the specified sound.\n";
228                 return NULL;
229         case CLI_GENERATE:
230         {
231                 int length = strlen(a->word);
232                 int which = 0;
233                 struct ao2_iterator it_sounds;
234                 char *match = NULL;
235                 char *filename;
236                 RAII_VAR(struct ao2_container *, sound_files, ast_media_get_media(sounds_index), ao2_cleanup);
237                 if (!sound_files) {
238                         return NULL;
239                 }
240
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);
246                                 break;
247                         }
248                         ao2_ref(filename, -1);
249                 }
250                 ao2_iterator_destroy(&it_sounds);
251                 return match;
252         }
253         }
254
255         if (a->argc == 4) {
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]);
259                         return CLI_FAILURE;
260                 }
261
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);
264                 return CLI_SUCCESS;
265         }
266
267         return CLI_SHOWUSAGE;
268 }
269
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"),
274 };
275
276 static void sounds_cleanup(void)
277 {
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);
282         sounds_index = NULL;
283 }
284
285 static void format_update_cb(void *data, struct stasis_subscription *sub,
286         struct stasis_message *message)
287 {
288         ast_sounds_reindex();
289 }
290
291 int ast_sounds_index_init(void)
292 {
293         int res = 0;
294         sounds_index = NULL;
295         if (ast_sounds_reindex()) {
296                 return -1;
297         }
298         res |= ast_cli_register_multiple(cli_sounds, ARRAY_LEN(cli_sounds));
299
300         sounds_system_router = stasis_message_router_create(ast_system_topic());
301         if (!sounds_system_router) {
302                 return -1;
303         }
304
305         res |= stasis_message_router_add(
306                 sounds_system_router,
307                 ast_format_register_type(),
308                 format_update_cb,
309                 NULL);
310
311         res |= stasis_message_router_add(
312                 sounds_system_router,
313                 ast_format_unregister_type(),
314                 format_update_cb,
315                 NULL);
316
317         if (res) {
318                 return -1;
319         }
320
321         ast_register_atexit(sounds_cleanup);
322         return 0;
323 }
324
325 struct ast_media_index *ast_sounds_get_index(void)
326 {
327         ao2_ref(sounds_index, +1);
328         return sounds_index;
329 }