2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005, Digium, Inc.
6 * redice li <redice_li@yahoo.com>
7 * Russell Bryant <russell@digium.com>
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
22 * \brief file format conversion CLI command using Asterisk formats and translators
24 * \author redice li <redice_li@yahoo.com>
25 * \author Russell Bryant <russell@digium.com>
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include "asterisk/channel.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/module.h"
39 #include "asterisk/cli.h"
40 #include "asterisk/file.h"
42 struct module_symbols *me;
44 /*! \brief Split the filename to basename and extension */
45 static int split_ext(char *filename, char **name, char **ext)
47 *name = *ext = filename;
51 if (ast_strlen_zero(*name) || ast_strlen_zero(*ext))
57 /*! \brief Convert a file from one format to another */
58 static int cli_audio_convert(int fd, int argc, char *argv[])
60 int ret = RESULT_FAILURE;
61 struct ast_filestream *fs_in = NULL, *fs_out = NULL;
65 char *file_in = NULL, *file_out = NULL;
66 char *name_in, *ext_in, *name_out, *ext_out;
68 ast_atomic_fetchadd_int(&me->usecnt, +1);
70 if (argc != 3 || ast_strlen_zero(argv[1]) || ast_strlen_zero(argv[2])) {
71 ret = RESULT_SHOWUSAGE;
75 if (!(file_in = ast_strdupa(argv[1])) || !(file_out = ast_strdupa(argv[2])))
78 if (split_ext(file_in, &name_in, &ext_in)) {
79 ast_cli(fd, "'%s' is an invalid filename!\n", argv[1]);
82 if (!(fs_in = ast_readfile(name_in, ext_in, NULL, O_RDONLY, 0, 0))) {
83 ast_cli(fd, "Unable to open input file: %s\n", argv[1]);
87 if (split_ext(file_out, &name_out, &ext_out)) {
88 ast_cli(fd, "'%s' is an invalid filename!\n", argv[2]);
91 if (!(fs_out = ast_writefile(name_out, ext_out, NULL, O_CREAT|O_TRUNC|O_WRONLY, 0, 0644))) {
92 ast_cli(fd, "Unable to open output file: %s\n", argv[2]);
98 while ((f = ast_readframe(fs_in))) {
99 if (ast_writestream(fs_out, f)) {
100 ast_cli(fd, "Failed to convert %s.%s to %s.%s!\n", name_in, ext_in, name_out, ext_out);
105 cost = ast_tvdiff_ms(ast_tvnow(), start);
106 ast_cli(fd, "Converted %s.%s to %s.%s in %dms\n", name_in, ext_in, name_out, ext_out, cost);
107 ret = RESULT_SUCCESS;
111 ast_closestream(fs_out);
112 if (ret != RESULT_SUCCESS)
113 ast_filedelete(name_out, ext_out);
117 ast_closestream(fs_in);
119 ast_atomic_fetchadd_int(&me->usecnt, -1);
124 static char usage_audio_convert[] =
125 "Usage: convert <file_in> <file_out>\n"
126 " Convert from file_in to file_out. If an absolute path is not given, the\n"
127 "default Asterisk sounds directory will be used.\n\n"
129 " convert tt-weasels.gsm tt-weasels.ulaw\n";
131 static struct ast_cli_entry audio_convert_cli={
132 { "convert" , NULL }, cli_audio_convert, "Convert audio files", usage_audio_convert
135 static int unload_module(void *mod)
137 return ast_cli_unregister(&audio_convert_cli);
140 static int load_module(void *mod)
143 return ast_cli_register(&audio_convert_cli);
146 static const char *description(void)
148 return "File format conversion CLI command";
151 static const char *key(void)
153 return ASTERISK_GPL_KEY;