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
;
48 if ((*ext
= strrchr(filename
, '.'))) {
53 if (ast_strlen_zero(*name
) || ast_strlen_zero(*ext
))
59 /*! \brief Convert a file from one format to another */
60 static int cli_audio_convert_deprecated(int fd
, int argc
, char *argv
[])
62 int ret
= RESULT_FAILURE
;
63 struct ast_filestream
*fs_in
= NULL
, *fs_out
= NULL
;
67 char *file_in
= NULL
, *file_out
= NULL
;
68 char *name_in
, *ext_in
, *name_out
, *ext_out
;
70 /* ugly, can be removed when CLI entries have ast_module pointers */
71 ast_module_ref(ast_module_info
->self
);
73 if (argc
!= 3 || ast_strlen_zero(argv
[1]) || ast_strlen_zero(argv
[2])) {
74 ret
= RESULT_SHOWUSAGE
;
78 file_in
= ast_strdupa(argv
[1]);
79 file_out
= ast_strdupa(argv
[2]);
81 if (split_ext(file_in
, &name_in
, &ext_in
)) {
82 ast_cli(fd
, "'%s' is an invalid filename!\n", argv
[1]);
85 if (!(fs_in
= ast_readfile(name_in
, ext_in
, NULL
, O_RDONLY
, 0, 0))) {
86 ast_cli(fd
, "Unable to open input file: %s\n", argv
[1]);
90 if (split_ext(file_out
, &name_out
, &ext_out
)) {
91 ast_cli(fd
, "'%s' is an invalid filename!\n", argv
[2]);
94 if (!(fs_out
= ast_writefile(name_out
, ext_out
, NULL
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0, 0644))) {
95 ast_cli(fd
, "Unable to open output file: %s\n", argv
[2]);
101 while ((f
= ast_readframe(fs_in
))) {
102 if (ast_writestream(fs_out
, f
)) {
103 ast_cli(fd
, "Failed to convert %s.%s to %s.%s!\n", name_in
, ext_in
, name_out
, ext_out
);
108 cost
= ast_tvdiff_ms(ast_tvnow(), start
);
109 ast_cli(fd
, "Converted %s.%s to %s.%s in %dms\n", name_in
, ext_in
, name_out
, ext_out
, cost
);
110 ret
= RESULT_SUCCESS
;
114 ast_closestream(fs_out
);
115 if (ret
!= RESULT_SUCCESS
)
116 ast_filedelete(name_out
, ext_out
);
120 ast_closestream(fs_in
);
122 ast_module_unref(ast_module_info
->self
);
127 static int cli_audio_convert(int fd
, int argc
, char *argv
[])
129 int ret
= RESULT_FAILURE
;
130 struct ast_filestream
*fs_in
= NULL
, *fs_out
= NULL
;
132 struct timeval start
;
134 char *file_in
= NULL
, *file_out
= NULL
;
135 char *name_in
, *ext_in
, *name_out
, *ext_out
;
137 /* ugly, can be removed when CLI entries have ast_module pointers */
138 ast_module_ref(ast_module_info
->self
);
140 if (argc
!= 4 || ast_strlen_zero(argv
[2]) || ast_strlen_zero(argv
[3])) {
141 ret
= RESULT_SHOWUSAGE
;
145 file_in
= ast_strdupa(argv
[2]);
146 file_out
= ast_strdupa(argv
[3]);
148 if (split_ext(file_in
, &name_in
, &ext_in
)) {
149 ast_cli(fd
, "'%s' is an invalid filename!\n", argv
[2]);
152 if (!(fs_in
= ast_readfile(name_in
, ext_in
, NULL
, O_RDONLY
, 0, 0))) {
153 ast_cli(fd
, "Unable to open input file: %s\n", argv
[2]);
157 if (split_ext(file_out
, &name_out
, &ext_out
)) {
158 ast_cli(fd
, "'%s' is an invalid filename!\n", argv
[3]);
161 if (!(fs_out
= ast_writefile(name_out
, ext_out
, NULL
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0, 0644))) {
162 ast_cli(fd
, "Unable to open output file: %s\n", argv
[3]);
168 while ((f
= ast_readframe(fs_in
))) {
169 if (ast_writestream(fs_out
, f
)) {
170 ast_cli(fd
, "Failed to convert %s.%s to %s.%s!\n", name_in
, ext_in
, name_out
, ext_out
);
175 cost
= ast_tvdiff_ms(ast_tvnow(), start
);
176 ast_cli(fd
, "Converted %s.%s to %s.%s in %dms\n", name_in
, ext_in
, name_out
, ext_out
, cost
);
177 ret
= RESULT_SUCCESS
;
181 ast_closestream(fs_out
);
182 if (ret
!= RESULT_SUCCESS
)
183 ast_filedelete(name_out
, ext_out
);
187 ast_closestream(fs_in
);
189 ast_module_unref(ast_module_info
->self
);
194 static char usage_audio_convert
[] =
195 "Usage: file convert <file_in> <file_out>\n"
196 " Convert from file_in to file_out. If an absolute path is not given, the\n"
197 "default Asterisk sounds directory will be used.\n\n"
199 " file convert tt-weasels.gsm tt-weasels.ulaw\n";
201 static struct ast_cli_entry cli_convert_deprecated
= {
202 { "convert" , NULL
},
203 cli_audio_convert_deprecated
, NULL
,
206 static struct ast_cli_entry cli_convert
[] = {
207 { { "file", "convert" , NULL
},
208 cli_audio_convert
, "Convert audio file",
209 usage_audio_convert
, NULL
, &cli_convert_deprecated
},
212 static int unload_module(void)
214 ast_cli_unregister_multiple(cli_convert
, sizeof(cli_convert
) / sizeof(struct ast_cli_entry
));
218 static int load_module(void)
220 ast_cli_register_multiple(cli_convert
, sizeof(cli_convert
) / sizeof(struct ast_cli_entry
));
224 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "File format conversion CLI command");