Thu Oct 8 21:56:04 2009

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: 76067 $")
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    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 /*! \brief Convert a file from one format to another */
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    /* ugly, can be removed when CLI entries have ast_module pointers */
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_cli(fd, "Failed to convert %s.%s to %s.%s!\n", name_in, ext_in, name_out, ext_out);
00104          goto fail_out;
00105       }
00106    }
00107 
00108    cost = ast_tvdiff_ms(ast_tvnow(), start);
00109    ast_cli(fd, "Converted %s.%s to %s.%s in %dms\n", name_in, ext_in, name_out, ext_out, cost);
00110    ret = RESULT_SUCCESS;
00111 
00112 fail_out:
00113    if (fs_out) {
00114       ast_closestream(fs_out);
00115       if (ret != RESULT_SUCCESS)
00116          ast_filedelete(name_out, ext_out);
00117    }
00118 
00119    if (fs_in) 
00120       ast_closestream(fs_in);
00121 
00122    ast_module_unref(ast_module_info->self);
00123 
00124    return ret;
00125 }
00126 
00127 static int cli_audio_convert(int fd, int argc, char *argv[])
00128 {
00129    int ret = RESULT_FAILURE;
00130    struct ast_filestream *fs_in = NULL, *fs_out = NULL;
00131    struct ast_frame *f;
00132    struct timeval start;
00133    int cost;
00134    char *file_in = NULL, *file_out = NULL;
00135    char *name_in, *ext_in, *name_out, *ext_out;
00136    
00137    /* ugly, can be removed when CLI entries have ast_module pointers */
00138    ast_module_ref(ast_module_info->self);
00139 
00140    if (argc != 4 || ast_strlen_zero(argv[2]) || ast_strlen_zero(argv[3])) {
00141       ret = RESULT_SHOWUSAGE;
00142       goto fail_out; 
00143    }
00144 
00145    file_in = ast_strdupa(argv[2]);
00146    file_out = ast_strdupa(argv[3]);
00147 
00148    if (split_ext(file_in, &name_in, &ext_in)) {
00149       ast_cli(fd, "'%s' is an invalid filename!\n", argv[2]);
00150       goto fail_out;
00151    }
00152    if (!(fs_in = ast_readfile(name_in, ext_in, NULL, O_RDONLY, 0, 0))) {
00153       ast_cli(fd, "Unable to open input file: %s\n", argv[2]);
00154       goto fail_out;
00155    }
00156    
00157    if (split_ext(file_out, &name_out, &ext_out)) {
00158       ast_cli(fd, "'%s' is an invalid filename!\n", argv[3]);
00159       goto fail_out;
00160    }
00161    if (!(fs_out = ast_writefile(name_out, ext_out, NULL, O_CREAT|O_TRUNC|O_WRONLY, 0, 0644))) {
00162       ast_cli(fd, "Unable to open output file: %s\n", argv[3]);
00163       goto fail_out;
00164    }
00165 
00166    start = ast_tvnow();
00167    
00168    while ((f = ast_readframe(fs_in))) {
00169       if (ast_writestream(fs_out, f)) {
00170          ast_cli(fd, "Failed to convert %s.%s to %s.%s!\n", name_in, ext_in, name_out, ext_out);
00171          goto fail_out;
00172       }
00173    }
00174 
00175    cost = ast_tvdiff_ms(ast_tvnow(), start);
00176    ast_cli(fd, "Converted %s.%s to %s.%s in %dms\n", name_in, ext_in, name_out, ext_out, cost);
00177    ret = RESULT_SUCCESS;
00178 
00179 fail_out:
00180    if (fs_out) {
00181       ast_closestream(fs_out);
00182       if (ret != RESULT_SUCCESS)
00183          ast_filedelete(name_out, ext_out);
00184    }
00185 
00186    if (fs_in) 
00187       ast_closestream(fs_in);
00188 
00189    ast_module_unref(ast_module_info->self);
00190 
00191    return ret;
00192 }
00193 
00194 static char usage_audio_convert[] =
00195 "Usage: file convert <file_in> <file_out>\n"
00196 "    Convert from file_in to file_out. If an absolute path is not given, the\n"
00197 "default Asterisk sounds directory will be used.\n\n"
00198 "Example:\n"
00199 "    file convert tt-weasels.gsm tt-weasels.ulaw\n";
00200 
00201 static struct ast_cli_entry cli_convert_deprecated = {
00202    { "convert" , NULL },
00203    cli_audio_convert_deprecated, NULL,
00204    NULL };
00205 
00206 static struct ast_cli_entry cli_convert[] = {
00207    { { "file", "convert" , NULL },
00208    cli_audio_convert, "Convert audio file",
00209    usage_audio_convert, NULL, &cli_convert_deprecated },
00210 };
00211 
00212 static int unload_module(void)
00213 {
00214    ast_cli_unregister_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry));
00215    return 0;
00216 }
00217 
00218 static int load_module(void)
00219 {
00220    ast_cli_register_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry));
00221    return 0;
00222 }
00223 
00224 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "File format conversion CLI command");

Generated on Thu Oct 8 21:56:04 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6