allow tonezone to live in a different place than DAHDI/Zaptel, since dahdi-tools...
[asterisk-bristuff.git] / res / res_convert.c
blob7c4e04e8336972a95a0faeff97ad39196ababde4
1 /*
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.
20 /*! \file
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>
27 */
29 #include "asterisk.h"
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
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, '.'))) {
49 **ext = '\0';
50 (*ext)++;
53 if (ast_strlen_zero(*name) || ast_strlen_zero(*ext))
54 return -1;
56 return 0;
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;
64 struct ast_frame *f;
65 struct timeval start;
66 int cost;
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;
75 goto fail_out;
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]);
83 goto fail_out;
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]);
87 goto fail_out;
90 if (split_ext(file_out, &name_out, &ext_out)) {
91 ast_cli(fd, "'%s' is an invalid filename!\n", argv[2]);
92 goto fail_out;
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]);
96 goto fail_out;
99 start = ast_tvnow();
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);
104 goto fail_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;
112 fail_out:
113 if (fs_out) {
114 ast_closestream(fs_out);
115 if (ret != RESULT_SUCCESS)
116 ast_filedelete(name_out, ext_out);
119 if (fs_in)
120 ast_closestream(fs_in);
122 ast_module_unref(ast_module_info->self);
124 return ret;
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;
131 struct ast_frame *f;
132 struct timeval start;
133 int cost;
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;
142 goto fail_out;
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]);
150 goto fail_out;
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]);
154 goto fail_out;
157 if (split_ext(file_out, &name_out, &ext_out)) {
158 ast_cli(fd, "'%s' is an invalid filename!\n", argv[3]);
159 goto fail_out;
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]);
163 goto fail_out;
166 start = ast_tvnow();
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);
171 goto fail_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;
179 fail_out:
180 if (fs_out) {
181 ast_closestream(fs_out);
182 if (ret != RESULT_SUCCESS)
183 ast_filedelete(name_out, ext_out);
186 if (fs_in)
187 ast_closestream(fs_in);
189 ast_module_unref(ast_module_info->self);
191 return ret;
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"
198 "Example:\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,
204 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));
215 return 0;
218 static int load_module(void)
220 ast_cli_register_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry));
221 return 0;
224 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "File format conversion CLI command");