![]() |
Home page |
Mailing list |
Docs
Asterisk developer's documentation :: Codename Pineapple
res_convert.c
Go to the documentation of this file.
00001 /* 00002 * Asterisk -- An open source telephony toolkit. 00003 * 00004 * Copyright (C) 2005, 2006, Digium, Inc. 00005 * 00006 * redice li <redice_li@yahoo.com> 00007 * Russell Bryant <russell@digium.com> 00008 * 00009 * See http://www.asterisk.org for more information about 00010 * the Asterisk project. Please do not directly contact 00011 * any of the maintainers of this project for assistance; 00012 * the project provides a web site, mailing lists and IRC 00013 * channels for your use. 00014 * 00015 * This program is free software, distributed under the terms of 00016 * the GNU General Public License Version 2. See the LICENSE file 00017 * at the top of the source tree. 00018 */ 00019 00020 /*! \file 00021 * 00022 * \brief file format conversion CLI command using Asterisk formats and translators 00023 * 00024 * \author redice li <redice_li@yahoo.com> 00025 * \author Russell Bryant <russell@digium.com> 00026 * 00027 */ 00028 00029 #include "asterisk.h" 00030 00031 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 48776 $") 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 /*! \brief Split the filename to basename and extension */ 00044 static int split_ext(char *filename, char **name, char **ext) 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 } 00055 00056 /*! \brief Convert a file from one format to another */ 00057 static int cli_audio_convert(int fd, int argc, char *argv[]) 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 != 4 || ast_strlen_zero(argv[2]) || ast_strlen_zero(argv[3])) { 00071 ret = RESULT_SHOWUSAGE; 00072 goto fail_out; 00073 } 00074 00075 file_in = ast_strdupa(argv[2]); 00076 file_out = ast_strdupa(argv[3]); 00077 00078 if (split_ext(file_in, &name_in, &ext_in)) { 00079 ast_cli(fd, "'%s' is an invalid filename!\n", argv[2]); 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[2]); 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[3]); 00089 goto fail_out; 00090 } 00091 if (!(fs_out = ast_writefile(name_out, ext_out, NULL, O_CREAT|O_TRUNC|O_WRONLY, 0, AST_FILE_MODE))) { 00092 ast_cli(fd, "Unable to open output file: %s\n", argv[3]); 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 } 00123 00124 static char usage_audio_convert[] = 00125 "Usage: file convert <file_in> <file_out>\n" 00126 " Convert from file_in to file_out. If an absolute path is not given, the\n" 00127 "default Asterisk sounds directory will be used.\n\n" 00128 "Example:\n" 00129 " file convert tt-weasels.gsm tt-weasels.ulaw\n"; 00130 00131 static struct ast_cli_entry cli_convert[] = { 00132 { { "file", "convert" , NULL }, 00133 cli_audio_convert, "Convert audio file", 00134 usage_audio_convert }, 00135 }; 00136 00137 static int unload_module(void) 00138 { 00139 ast_cli_unregister_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry)); 00140 return 0; 00141 } 00142 00143 static int load_module(void) 00144 { 00145 ast_cli_register_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry)); 00146 return 0; 00147 } 00148 00149 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "File format conversion CLI command");