00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "asterisk.h"
00030
00031 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 196826 $")
00032
00033 #include <stdio.h>
00034 #include <string.h>
00035 #include <stdlib.h>
00036
00037 #include "asterisk/channel.h"
00038 #include "asterisk/logger.h"
00039 #include "asterisk/module.h"
00040 #include "asterisk/cli.h"
00041 #include "asterisk/file.h"
00042
00043
00044 static int split_ext(char *filename, char **name, char **ext)
00045 {
00046 *name = *ext = filename;
00047
00048 if ((*ext = strrchr(filename, '.'))) {
00049 **ext = '\0';
00050 (*ext)++;
00051 }
00052
00053 if (ast_strlen_zero(*name) || ast_strlen_zero(*ext))
00054 return -1;
00055
00056 return 0;
00057 }
00058
00059
00060 static int cli_audio_convert_deprecated(int fd, int argc, char *argv[])
00061 {
00062 int ret = RESULT_FAILURE;
00063 struct ast_filestream *fs_in = NULL, *fs_out = NULL;
00064 struct ast_frame *f;
00065 struct timeval start;
00066 int cost;
00067 char *file_in = NULL, *file_out = NULL;
00068 char *name_in, *ext_in, *name_out, *ext_out;
00069
00070
00071 ast_module_ref(ast_module_info->self);
00072
00073 if (argc != 3 || ast_strlen_zero(argv[1]) || ast_strlen_zero(argv[2])) {
00074 ret = RESULT_SHOWUSAGE;
00075 goto fail_out;
00076 }
00077
00078 file_in = ast_strdupa(argv[1]);
00079 file_out = ast_strdupa(argv[2]);
00080
00081 if (split_ext(file_in, &name_in, &ext_in)) {
00082 ast_cli(fd, "'%s' is an invalid filename!\n", argv[1]);
00083 goto fail_out;
00084 }
00085 if (!(fs_in = ast_readfile(name_in, ext_in, NULL, O_RDONLY, 0, 0))) {
00086 ast_cli(fd, "Unable to open input file: %s\n", argv[1]);
00087 goto fail_out;
00088 }
00089
00090 if (split_ext(file_out, &name_out, &ext_out)) {
00091 ast_cli(fd, "'%s' is an invalid filename!\n", argv[2]);
00092 goto fail_out;
00093 }
00094 if (!(fs_out = ast_writefile(name_out, ext_out, NULL, O_CREAT|O_TRUNC|O_WRONLY, 0, 0644))) {
00095 ast_cli(fd, "Unable to open output file: %s\n", argv[2]);
00096 goto fail_out;
00097 }
00098
00099 start = ast_tvnow();
00100
00101 while ((f = ast_readframe(fs_in))) {
00102 if (ast_writestream(fs_out, f)) {
00103 ast_frfree(f);
00104 ast_cli(fd, "Failed to convert %s.%s to %s.%s!\n", name_in, ext_in, name_out, ext_out);
00105 goto fail_out;
00106 }
00107 ast_frfree(f);
00108 }
00109
00110 cost = ast_tvdiff_ms(ast_tvnow(), start);
00111 ast_cli(fd, "Converted %s.%s to %s.%s in %dms\n", name_in, ext_in, name_out, ext_out, cost);
00112 ret = RESULT_SUCCESS;
00113
00114 fail_out:
00115 if (fs_out) {
00116 ast_closestream(fs_out);
00117 if (ret != RESULT_SUCCESS)
00118 ast_filedelete(name_out, ext_out);
00119 }
00120
00121 if (fs_in)
00122 ast_closestream(fs_in);
00123
00124 ast_module_unref(ast_module_info->self);
00125
00126 return ret;
00127 }
00128
00129 static int cli_audio_convert(int fd, int argc, char *argv[])
00130 {
00131 int ret = RESULT_FAILURE;
00132 struct ast_filestream *fs_in = NULL, *fs_out = NULL;
00133 struct ast_frame *f;
00134 struct timeval start;
00135 int cost;
00136 char *file_in = NULL, *file_out = NULL;
00137 char *name_in, *ext_in, *name_out, *ext_out;
00138
00139
00140 ast_module_ref(ast_module_info->self);
00141
00142 if (argc != 4 || ast_strlen_zero(argv[2]) || ast_strlen_zero(argv[3])) {
00143 ret = RESULT_SHOWUSAGE;
00144 goto fail_out;
00145 }
00146
00147 file_in = ast_strdupa(argv[2]);
00148 file_out = ast_strdupa(argv[3]);
00149
00150 if (split_ext(file_in, &name_in, &ext_in)) {
00151 ast_cli(fd, "'%s' is an invalid filename!\n", argv[2]);
00152 goto fail_out;
00153 }
00154 if (!(fs_in = ast_readfile(name_in, ext_in, NULL, O_RDONLY, 0, 0))) {
00155 ast_cli(fd, "Unable to open input file: %s\n", argv[2]);
00156 goto fail_out;
00157 }
00158
00159 if (split_ext(file_out, &name_out, &ext_out)) {
00160 ast_cli(fd, "'%s' is an invalid filename!\n", argv[3]);
00161 goto fail_out;
00162 }
00163 if (!(fs_out = ast_writefile(name_out, ext_out, NULL, O_CREAT|O_TRUNC|O_WRONLY, 0, 0644))) {
00164 ast_cli(fd, "Unable to open output file: %s\n", argv[3]);
00165 goto fail_out;
00166 }
00167
00168 start = ast_tvnow();
00169
00170 while ((f = ast_readframe(fs_in))) {
00171 if (ast_writestream(fs_out, f)) {
00172 ast_frfree(f);
00173 ast_cli(fd, "Failed to convert %s.%s to %s.%s!\n", name_in, ext_in, name_out, ext_out);
00174 goto fail_out;
00175 }
00176 ast_frfree(f);
00177 }
00178
00179 cost = ast_tvdiff_ms(ast_tvnow(), start);
00180 ast_cli(fd, "Converted %s.%s to %s.%s in %dms\n", name_in, ext_in, name_out, ext_out, cost);
00181 ret = RESULT_SUCCESS;
00182
00183 fail_out:
00184 if (fs_out) {
00185 ast_closestream(fs_out);
00186 if (ret != RESULT_SUCCESS)
00187 ast_filedelete(name_out, ext_out);
00188 }
00189
00190 if (fs_in)
00191 ast_closestream(fs_in);
00192
00193 ast_module_unref(ast_module_info->self);
00194
00195 return ret;
00196 }
00197
00198 static char usage_audio_convert[] =
00199 "Usage: file convert <file_in> <file_out>\n"
00200 " Convert from file_in to file_out. If an absolute path is not given, the\n"
00201 "default Asterisk sounds directory will be used.\n\n"
00202 "Example:\n"
00203 " file convert tt-weasels.gsm tt-weasels.ulaw\n";
00204
00205 static struct ast_cli_entry cli_convert_deprecated = {
00206 { "convert" , NULL },
00207 cli_audio_convert_deprecated, NULL,
00208 NULL };
00209
00210 static struct ast_cli_entry cli_convert[] = {
00211 { { "file", "convert" , NULL },
00212 cli_audio_convert, "Convert audio file",
00213 usage_audio_convert, NULL, &cli_convert_deprecated },
00214 };
00215
00216 static int unload_module(void)
00217 {
00218 ast_cli_unregister_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry));
00219 return 0;
00220 }
00221
00222 static int load_module(void)
00223 {
00224 ast_cli_register_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry));
00225 return 0;
00226 }
00227
00228 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "File format conversion CLI command");