Mon May 14 04:43:00 2007

Asterisk developer's documentation


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$")
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_deprecated(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 != 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 }
00123 
00124 static int cli_audio_convert(int fd, int argc, char *argv[])
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 }
00190 
00191 static char usage_audio_convert[] =
00192 "Usage: file convert <file_in> <file_out>\n"
00193 "    Convert from file_in to file_out. If an absolute path is not given, the\n"
00194 "default Asterisk sounds directory will be used.\n\n"
00195 "Example:\n"
00196 "    file convert tt-weasels.gsm tt-weasels.ulaw\n";
00197 
00198 static struct ast_cli_entry cli_convert_deprecated = {
00199    { "convert" , NULL },
00200    cli_audio_convert_deprecated, NULL,
00201    NULL };
00202 
00203 static struct ast_cli_entry cli_convert[] = {
00204    { { "file", "convert" , NULL },
00205    cli_audio_convert, "Convert audio file",
00206    usage_audio_convert, NULL, &cli_convert_deprecated },
00207 };
00208 
00209 static int unload_module(void)
00210 {
00211    ast_cli_unregister_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry));
00212    return 0;
00213 }
00214 
00215 static int load_module(void)
00216 {
00217    ast_cli_register_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry));
00218    return 0;
00219 }
00220 
00221 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "File format conversion CLI command");

Generated on Mon May 14 04:43:00 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1