Mon Oct 8 12:39:04 2012

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 /*** MODULEINFO
00030    <support_level>core</support_level>
00031  ***/
00032 
00033 #include "asterisk.h"
00034 
00035 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00036 
00037 #include "asterisk/channel.h"
00038 #include "asterisk/module.h"
00039 #include "asterisk/cli.h"
00040 #include "asterisk/file.h"
00041 
00042 /*! \brief Split the filename to basename and extension */
00043 static int split_ext(char *filename, char **name, char **ext)
00044 {
00045    *name = *ext = filename;
00046    
00047    if ((*ext = strrchr(filename, '.'))) {
00048       **ext = '\0';
00049       (*ext)++;
00050    }
00051 
00052    if (ast_strlen_zero(*name) || ast_strlen_zero(*ext))
00053       return -1;
00054 
00055    return 0;
00056 }
00057 
00058 /*! 
00059  * \brief Convert a file from one format to another 
00060  * \param e CLI entry
00061  * \param cmd command number
00062  * \param a list of cli arguments
00063  * \retval CLI_SUCCESS on success.
00064  * \retval CLI_SHOWUSAGE or CLI_FAILURE on failure.
00065 */
00066 static char *handle_cli_file_convert(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00067 {
00068    char *ret = CLI_FAILURE;
00069    struct ast_filestream *fs_in = NULL, *fs_out = NULL;
00070    struct ast_frame *f;
00071    struct timeval start;
00072    int cost;
00073    char *file_in = NULL, *file_out = NULL;
00074    char *name_in, *ext_in, *name_out, *ext_out;
00075 
00076    switch (cmd) {
00077    case CLI_INIT:
00078       e->command = "file convert";
00079       e->usage =
00080          "Usage: file convert <file_in> <file_out>\n"
00081          "       Convert from file_in to file_out. If an absolute path\n"
00082          "       is not given, the default Asterisk sounds directory\n"
00083          "       will be used.\n\n"
00084          "       Example:\n"
00085          "           file convert tt-weasels.gsm tt-weasels.ulaw\n";
00086       return NULL;
00087    case CLI_GENERATE:
00088       return NULL;
00089    }
00090    
00091    /* ugly, can be removed when CLI entries have ast_module pointers */
00092    ast_module_ref(ast_module_info->self);
00093 
00094    if (a->argc != 4 || ast_strlen_zero(a->argv[2]) || ast_strlen_zero(a->argv[3])) {
00095       ret = CLI_SHOWUSAGE;
00096       goto fail_out; 
00097    }
00098 
00099    file_in = ast_strdupa(a->argv[2]);
00100    file_out = ast_strdupa(a->argv[3]);
00101 
00102    if (split_ext(file_in, &name_in, &ext_in)) {
00103       ast_cli(a->fd, "'%s' is an invalid filename!\n", a->argv[2]);
00104       goto fail_out;
00105    }
00106    if (!(fs_in = ast_readfile(name_in, ext_in, NULL, O_RDONLY, 0, 0))) {
00107       ast_cli(a->fd, "Unable to open input file: %s\n", a->argv[2]);
00108       goto fail_out;
00109    }
00110    
00111    if (split_ext(file_out, &name_out, &ext_out)) {
00112       ast_cli(a->fd, "'%s' is an invalid filename!\n", a->argv[3]);
00113       goto fail_out;
00114    }
00115    if (!(fs_out = ast_writefile(name_out, ext_out, NULL, O_CREAT|O_TRUNC|O_WRONLY, 0, AST_FILE_MODE))) {
00116       ast_cli(a->fd, "Unable to open output file: %s\n", a->argv[3]);
00117       goto fail_out;
00118    }
00119 
00120    start = ast_tvnow();
00121    
00122    while ((f = ast_readframe(fs_in))) {
00123       if (ast_writestream(fs_out, f)) {
00124          ast_frfree(f);
00125          ast_cli(a->fd, "Failed to convert %s.%s to %s.%s!\n", name_in, ext_in, name_out, ext_out);
00126          goto fail_out;
00127       }
00128       ast_frfree(f);
00129    }
00130 
00131    cost = ast_tvdiff_ms(ast_tvnow(), start);
00132    ast_cli(a->fd, "Converted %s.%s to %s.%s in %dms\n", name_in, ext_in, name_out, ext_out, cost);
00133    ret = CLI_SUCCESS;
00134 
00135 fail_out:
00136    if (fs_out) {
00137       ast_closestream(fs_out);
00138       if (ret != CLI_SUCCESS)
00139          ast_filedelete(name_out, ext_out);
00140    }
00141 
00142    if (fs_in) 
00143       ast_closestream(fs_in);
00144 
00145    ast_module_unref(ast_module_info->self);
00146 
00147    return ret;
00148 }
00149 
00150 static struct ast_cli_entry cli_convert[] = {
00151    AST_CLI_DEFINE(handle_cli_file_convert, "Convert audio file")
00152 };
00153 
00154 static int unload_module(void)
00155 {
00156    ast_cli_unregister_multiple(cli_convert, ARRAY_LEN(cli_convert));
00157    return 0;
00158 }
00159 
00160 static int load_module(void)
00161 {
00162    ast_cli_register_multiple(cli_convert, ARRAY_LEN(cli_convert));
00163    return AST_MODULE_LOAD_SUCCESS;
00164 }
00165 
00166 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "File format conversion CLI command");

Generated on Mon Oct 8 12:39:04 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7