2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005, 2006, 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>
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/channel.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/module.h"
40 #include "asterisk/cli.h"
41 #include "asterisk/file.h"
43 /*! \brief Split the filename to basename and extension */
44 static int split_ext(char *filename, char **name, char **ext)
46 *name = *ext = filename;
50 if (ast_strlen_zero(*name) || ast_strlen_zero(*ext))
56 /*! \brief Convert a file from one format to another */
57 static int cli_audio_convert_deprecated(int fd, int argc, char *argv[])
59 int ret = RESULT_FAILURE;
60 struct ast_filestream *fs_in = NULL, *fs_out = NULL;
64 char *file_in = NULL, *file_out = NULL;
65 char *name_in, *ext_in, *name_out, *ext_out;
67 /* ugly, can be removed when CLI entries have ast_module pointers */
68 ast_module_ref(ast_module_info->self);
70 if (argc != 3 || ast_strlen_zero(argv[1]) || ast_strlen_zero(argv[2])) {
71 ret = RESULT_SHOWUSAGE;
75 file_in = ast_strdupa(argv[1]);
76 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_module_unref(ast_module_info->self);
124 static int cli_audio_convert(int fd, int argc, char *argv[])
126 int ret = RESULT_FAILURE;
127 struct ast_filestream *fs_in = NULL, *fs_out = NULL;
129 struct timeval start;
131 char *file_in = NULL, *file_out = NULL;
132 char *name_in, *ext_in, *name_out, *ext_out;
134 /* ugly, can be removed when CLI entries have ast_module pointers */
135 ast_module_ref(ast_module_info->self);
137 if (argc != 4 || ast_strlen_zero(argv[2]) || ast_strlen_zero(argv[3])) {
138 ret = RESULT_SHOWUSAGE;
142 file_in = ast_strdupa(argv[2]);
143 file_out = ast_strdupa(argv[3]);
145 if (split_ext(file_in, &name_in, &ext_in)) {
146 ast_cli(fd, "'%s' is an invalid filename!\n", argv[2]);
149 if (!(fs_in = ast_readfile(name_in, ext_in, NULL, O_RDONLY, 0, 0))) {
150 ast_cli(fd, "Unable to open input file: %s\n", argv[2]);
154 if (split_ext(file_out, &name_out, &ext_out)) {
155 ast_cli(fd, "'%s' is an invalid filename!\n", argv[3]);
158 if (!(fs_out = ast_writefile(name_out, ext_out, NULL, O_CREAT|O_TRUNC|O_WRONLY, 0, 0644))) {
159 ast_cli(fd, "Unable to open output file: %s\n", argv[3]);
165 while ((f = ast_readframe(fs_in))) {
166 if (ast_writestream(fs_out, f)) {
167 ast_cli(fd, "Failed to convert %s.%s to %s.%s!\n", name_in, ext_in, name_out, ext_out);
172 cost = ast_tvdiff_ms(ast_tvnow(), start);
173 ast_cli(fd, "Converted %s.%s to %s.%s in %dms\n", name_in, ext_in, name_out, ext_out, cost);
174 ret = RESULT_SUCCESS;
178 ast_closestream(fs_out);
179 if (ret != RESULT_SUCCESS)
180 ast_filedelete(name_out, ext_out);
184 ast_closestream(fs_in);
186 ast_module_unref(ast_module_info->self);
191 static char usage_audio_convert[] =
192 "Usage: file convert <file_in> <file_out>\n"
193 " Convert from file_in to file_out. If an absolute path is not given, the\n"
194 "default Asterisk sounds directory will be used.\n\n"
196 " file convert tt-weasels.gsm tt-weasels.ulaw\n";
198 static struct ast_cli_entry cli_convert_deprecated = {
199 { "convert" , NULL },
200 cli_audio_convert_deprecated, NULL,
203 static struct ast_cli_entry cli_convert[] = {
204 { { "file", "convert" , NULL },
205 cli_audio_convert, "Convert audio file",
206 usage_audio_convert, NULL, &cli_convert_deprecated },
209 static int unload_module(void)
211 ast_cli_unregister_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry));
215 static int load_module(void)
217 ast_cli_register_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry));
221 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "File format conversion CLI command");