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$")
33 #include "asterisk/channel.h"
34 #include "asterisk/module.h"
35 #include "asterisk/cli.h"
36 #include "asterisk/file.h"
38 /*! \brief Split the filename to basename and extension */
39 static int split_ext(char *filename
, char **name
, char **ext
)
41 *name
= *ext
= filename
;
43 if ((*ext
= strrchr(filename
, '.'))) {
48 if (ast_strlen_zero(*name
) || ast_strlen_zero(*ext
))
55 * \brief Convert a file from one format to another
57 * \param cmd command number
58 * \param a list of cli arguments
59 * \retval CLI_SUCCESS on success.
60 * \retval CLI_SHOWUSAGE or CLI_FAILURE on failure.
62 static char *handle_cli_file_convert(struct ast_cli_entry
*e
, int cmd
, struct ast_cli_args
*a
)
64 char *ret
= CLI_FAILURE
;
65 struct ast_filestream
*fs_in
= NULL
, *fs_out
= NULL
;
69 char *file_in
= NULL
, *file_out
= NULL
;
70 char *name_in
, *ext_in
, *name_out
, *ext_out
;
74 e
->command
= "file convert";
76 "Usage: file convert <file_in> <file_out>\n"
77 " Convert from file_in to file_out. If an absolute path\n"
78 " is not given, the default Asterisk sounds directory\n"
81 " file convert tt-weasels.gsm tt-weasels.ulaw\n";
87 /* ugly, can be removed when CLI entries have ast_module pointers */
88 ast_module_ref(ast_module_info
->self
);
90 if (a
->argc
!= 4 || ast_strlen_zero(a
->argv
[2]) || ast_strlen_zero(a
->argv
[3])) {
95 file_in
= ast_strdupa(a
->argv
[2]);
96 file_out
= ast_strdupa(a
->argv
[3]);
98 if (split_ext(file_in
, &name_in
, &ext_in
)) {
99 ast_cli(a
->fd
, "'%s' is an invalid filename!\n", a
->argv
[2]);
102 if (!(fs_in
= ast_readfile(name_in
, ext_in
, NULL
, O_RDONLY
, 0, 0))) {
103 ast_cli(a
->fd
, "Unable to open input file: %s\n", a
->argv
[2]);
107 if (split_ext(file_out
, &name_out
, &ext_out
)) {
108 ast_cli(a
->fd
, "'%s' is an invalid filename!\n", a
->argv
[3]);
111 if (!(fs_out
= ast_writefile(name_out
, ext_out
, NULL
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0, AST_FILE_MODE
))) {
112 ast_cli(a
->fd
, "Unable to open output file: %s\n", a
->argv
[3]);
118 while ((f
= ast_readframe(fs_in
))) {
119 if (ast_writestream(fs_out
, f
)) {
120 ast_cli(a
->fd
, "Failed to convert %s.%s to %s.%s!\n", name_in
, ext_in
, name_out
, ext_out
);
125 cost
= ast_tvdiff_ms(ast_tvnow(), start
);
126 ast_cli(a
->fd
, "Converted %s.%s to %s.%s in %dms\n", name_in
, ext_in
, name_out
, ext_out
, cost
);
131 ast_closestream(fs_out
);
132 if (ret
!= CLI_SUCCESS
)
133 ast_filedelete(name_out
, ext_out
);
137 ast_closestream(fs_in
);
139 ast_module_unref(ast_module_info
->self
);
144 static struct ast_cli_entry cli_convert
[] = {
145 AST_CLI_DEFINE(handle_cli_file_convert
, "Convert audio file")
148 static int unload_module(void)
150 ast_cli_unregister_multiple(cli_convert
, sizeof(cli_convert
) / sizeof(struct ast_cli_entry
));
154 static int load_module(void)
156 ast_cli_register_multiple(cli_convert
, sizeof(cli_convert
) / sizeof(struct ast_cli_entry
));
157 return AST_MODULE_LOAD_SUCCESS
;
160 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "File format conversion CLI command");