#include "asterisk.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "asterisk/channel.h"
#include "asterisk/logger.h"
#include "asterisk/module.h"
#include "asterisk/cli.h"
#include "asterisk/file.h"
Include dependency graph for res_convert.c:
Go to the source code of this file.
Functions | |
AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"File format conversion CLI command") | |
static int | cli_audio_convert (int fd, int argc, char *argv[]) |
static int | cli_audio_convert_deprecated (int fd, int argc, char *argv[]) |
Convert a file from one format to another. | |
static int | load_module (void) |
static int | split_ext (char *filename, char **name, char **ext) |
Split the filename to basename and extension. | |
static int | unload_module (void) |
Variables | |
static struct ast_cli_entry | cli_convert [] |
static struct ast_cli_entry | cli_convert_deprecated |
static char | usage_audio_convert [] |
Definition in file res_convert.c.
AST_MODULE_INFO_STANDARD | ( | ASTERISK_GPL_KEY | , | |
"File format conversion CLI command" | ||||
) |
static int cli_audio_convert | ( | int | fd, | |
int | argc, | |||
char * | argv[] | |||
) | [static] |
Definition at line 124 of file res_convert.c.
References ast_cli(), ast_closestream(), ast_filedelete(), ast_module_ref(), ast_module_unref(), ast_readfile(), ast_readframe(), ast_strdupa, ast_strlen_zero(), ast_writefile(), ast_writestream(), f, RESULT_FAILURE, RESULT_SHOWUSAGE, RESULT_SUCCESS, and split_ext().
00125 { 00126 int ret = RESULT_FAILURE; 00127 struct ast_filestream *fs_in = NULL, *fs_out = NULL; 00128 struct ast_frame *f; 00129 struct timeval start; 00130 int cost; 00131 char *file_in = NULL, *file_out = NULL; 00132 char *name_in, *ext_in, *name_out, *ext_out; 00133 00134 /* ugly, can be removed when CLI entries have ast_module pointers */ 00135 ast_module_ref(ast_module_info->self); 00136 00137 if (argc != 4 || ast_strlen_zero(argv[2]) || ast_strlen_zero(argv[3])) { 00138 ret = RESULT_SHOWUSAGE; 00139 goto fail_out; 00140 } 00141 00142 file_in = ast_strdupa(argv[2]); 00143 file_out = ast_strdupa(argv[3]); 00144 00145 if (split_ext(file_in, &name_in, &ext_in)) { 00146 ast_cli(fd, "'%s' is an invalid filename!\n", argv[2]); 00147 goto fail_out; 00148 } 00149 if (!(fs_in = ast_readfile(name_in, ext_in, NULL, O_RDONLY, 0, 0))) { 00150 ast_cli(fd, "Unable to open input file: %s\n", argv[2]); 00151 goto fail_out; 00152 } 00153 00154 if (split_ext(file_out, &name_out, &ext_out)) { 00155 ast_cli(fd, "'%s' is an invalid filename!\n", argv[3]); 00156 goto fail_out; 00157 } 00158 if (!(fs_out = ast_writefile(name_out, ext_out, NULL, O_CREAT|O_TRUNC|O_WRONLY, 0, 0644))) { 00159 ast_cli(fd, "Unable to open output file: %s\n", argv[3]); 00160 goto fail_out; 00161 } 00162 00163 start = ast_tvnow(); 00164 00165 while ((f = ast_readframe(fs_in))) { 00166 if (ast_writestream(fs_out, f)) { 00167 ast_cli(fd, "Failed to convert %s.%s to %s.%s!\n", name_in, ext_in, name_out, ext_out); 00168 goto fail_out; 00169 } 00170 } 00171 00172 cost = ast_tvdiff_ms(ast_tvnow(), start); 00173 ast_cli(fd, "Converted %s.%s to %s.%s in %dms\n", name_in, ext_in, name_out, ext_out, cost); 00174 ret = RESULT_SUCCESS; 00175 00176 fail_out: 00177 if (fs_out) { 00178 ast_closestream(fs_out); 00179 if (ret != RESULT_SUCCESS) 00180 ast_filedelete(name_out, ext_out); 00181 } 00182 00183 if (fs_in) 00184 ast_closestream(fs_in); 00185 00186 ast_module_unref(ast_module_info->self); 00187 00188 return ret; 00189 }
static int cli_audio_convert_deprecated | ( | int | fd, | |
int | argc, | |||
char * | argv[] | |||
) | [static] |
Convert a file from one format to another.
Definition at line 57 of file res_convert.c.
References ast_cli(), ast_closestream(), ast_filedelete(), ast_module_ref(), ast_module_unref(), ast_readfile(), ast_readframe(), ast_strdupa, ast_strlen_zero(), ast_writefile(), ast_writestream(), f, RESULT_FAILURE, RESULT_SHOWUSAGE, RESULT_SUCCESS, and split_ext().
00058 { 00059 int ret = RESULT_FAILURE; 00060 struct ast_filestream *fs_in = NULL, *fs_out = NULL; 00061 struct ast_frame *f; 00062 struct timeval start; 00063 int cost; 00064 char *file_in = NULL, *file_out = NULL; 00065 char *name_in, *ext_in, *name_out, *ext_out; 00066 00067 /* ugly, can be removed when CLI entries have ast_module pointers */ 00068 ast_module_ref(ast_module_info->self); 00069 00070 if (argc != 3 || ast_strlen_zero(argv[1]) || ast_strlen_zero(argv[2])) { 00071 ret = RESULT_SHOWUSAGE; 00072 goto fail_out; 00073 } 00074 00075 file_in = ast_strdupa(argv[1]); 00076 file_out = ast_strdupa(argv[2]); 00077 00078 if (split_ext(file_in, &name_in, &ext_in)) { 00079 ast_cli(fd, "'%s' is an invalid filename!\n", argv[1]); 00080 goto fail_out; 00081 } 00082 if (!(fs_in = ast_readfile(name_in, ext_in, NULL, O_RDONLY, 0, 0))) { 00083 ast_cli(fd, "Unable to open input file: %s\n", argv[1]); 00084 goto fail_out; 00085 } 00086 00087 if (split_ext(file_out, &name_out, &ext_out)) { 00088 ast_cli(fd, "'%s' is an invalid filename!\n", argv[2]); 00089 goto fail_out; 00090 } 00091 if (!(fs_out = ast_writefile(name_out, ext_out, NULL, O_CREAT|O_TRUNC|O_WRONLY, 0, 0644))) { 00092 ast_cli(fd, "Unable to open output file: %s\n", argv[2]); 00093 goto fail_out; 00094 } 00095 00096 start = ast_tvnow(); 00097 00098 while ((f = ast_readframe(fs_in))) { 00099 if (ast_writestream(fs_out, f)) { 00100 ast_cli(fd, "Failed to convert %s.%s to %s.%s!\n", name_in, ext_in, name_out, ext_out); 00101 goto fail_out; 00102 } 00103 } 00104 00105 cost = ast_tvdiff_ms(ast_tvnow(), start); 00106 ast_cli(fd, "Converted %s.%s to %s.%s in %dms\n", name_in, ext_in, name_out, ext_out, cost); 00107 ret = RESULT_SUCCESS; 00108 00109 fail_out: 00110 if (fs_out) { 00111 ast_closestream(fs_out); 00112 if (ret != RESULT_SUCCESS) 00113 ast_filedelete(name_out, ext_out); 00114 } 00115 00116 if (fs_in) 00117 ast_closestream(fs_in); 00118 00119 ast_module_unref(ast_module_info->self); 00120 00121 return ret; 00122 }
static int load_module | ( | void | ) | [static] |
Definition at line 215 of file res_convert.c.
References ast_cli_register_multiple(), and cli_convert.
00216 { 00217 ast_cli_register_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry)); 00218 return 0; 00219 }
static int split_ext | ( | char * | filename, | |
char ** | name, | |||
char ** | ext | |||
) | [static] |
Split the filename to basename and extension.
Definition at line 44 of file res_convert.c.
References ast_strlen_zero(), and strsep().
Referenced by cli_audio_convert(), and cli_audio_convert_deprecated().
00045 { 00046 *name = *ext = filename; 00047 00048 strsep(ext, "."); 00049 00050 if (ast_strlen_zero(*name) || ast_strlen_zero(*ext)) 00051 return -1; 00052 00053 return 0; 00054 }
static int unload_module | ( | void | ) | [static] |
Definition at line 209 of file res_convert.c.
References ast_cli_unregister_multiple(), and cli_convert.
00210 { 00211 ast_cli_unregister_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry)); 00212 return 0; 00213 }
struct ast_cli_entry cli_convert[] [static] |
Initial value:
{ { { "file", "convert" , NULL }, cli_audio_convert, "Convert audio file", usage_audio_convert, NULL, &cli_convert_deprecated }, }
Definition at line 203 of file res_convert.c.
Referenced by load_module(), and unload_module().
struct ast_cli_entry cli_convert_deprecated [static] |
Initial value:
{ { "convert" , NULL }, cli_audio_convert_deprecated, NULL, NULL }
Definition at line 198 of file res_convert.c.
char usage_audio_convert[] [static] |
Definition at line 191 of file res_convert.c.