Wed Oct 28 11:46:18 2009

Asterisk developer's documentation


res_convert.c File Reference

file format conversion CLI command using Asterisk formats and translators More...

#include "asterisk.h"
#include "asterisk/channel.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

static void __reg_module (void)
static void __unreg_module (void)
static char * handle_cli_file_convert (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 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_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "File format conversion CLI command" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, }
static const struct
ast_module_info
ast_module_info = &__mod_info
static struct ast_cli_entry cli_convert []


Detailed Description

file format conversion CLI command using Asterisk formats and translators

Author:
redice li <redice_li@yahoo.com>

Russell Bryant <russell@digium.com>

Definition in file res_convert.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 162 of file res_convert.c.

static void __unreg_module ( void   )  [static]

Definition at line 162 of file res_convert.c.

static char* handle_cli_file_convert ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Convert a file from one format to another.

Parameters:
e CLI entry
cmd command number
a list of cli arguments
Return values:
CLI_SUCCESS on success.
CLI_SHOWUSAGE or CLI_FAILURE on failure.

Definition at line 62 of file res_convert.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_closestream(), AST_FILE_MODE, ast_filedelete(), ast_frfree, ast_module_ref(), ast_module_unref(), ast_readfile(), ast_readframe(), ast_strdupa, ast_strlen_zero(), ast_tvdiff_ms(), ast_tvnow(), ast_writefile(), ast_writestream(), CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, f, ast_cli_args::fd, split_ext(), and ast_cli_entry::usage.

00063 {
00064    char *ret = CLI_FAILURE;
00065    struct ast_filestream *fs_in = NULL, *fs_out = NULL;
00066    struct ast_frame *f;
00067    struct timeval start;
00068    int cost;
00069    char *file_in = NULL, *file_out = NULL;
00070    char *name_in, *ext_in, *name_out, *ext_out;
00071 
00072    switch (cmd) {
00073    case CLI_INIT:
00074       e->command = "file convert";
00075       e->usage =
00076          "Usage: file convert <file_in> <file_out>\n"
00077          "       Convert from file_in to file_out. If an absolute path\n"
00078          "       is not given, the default Asterisk sounds directory\n"
00079          "       will be used.\n\n"
00080          "       Example:\n"
00081          "           file convert tt-weasels.gsm tt-weasels.ulaw\n";
00082       return NULL;
00083    case CLI_GENERATE:
00084       return NULL;
00085    }
00086    
00087    /* ugly, can be removed when CLI entries have ast_module pointers */
00088    ast_module_ref(ast_module_info->self);
00089 
00090    if (a->argc != 4 || ast_strlen_zero(a->argv[2]) || ast_strlen_zero(a->argv[3])) {
00091       ret = CLI_SHOWUSAGE;
00092       goto fail_out; 
00093    }
00094 
00095    file_in = ast_strdupa(a->argv[2]);
00096    file_out = ast_strdupa(a->argv[3]);
00097 
00098    if (split_ext(file_in, &name_in, &ext_in)) {
00099       ast_cli(a->fd, "'%s' is an invalid filename!\n", a->argv[2]);
00100       goto fail_out;
00101    }
00102    if (!(fs_in = ast_readfile(name_in, ext_in, NULL, O_RDONLY, 0, 0))) {
00103       ast_cli(a->fd, "Unable to open input file: %s\n", a->argv[2]);
00104       goto fail_out;
00105    }
00106    
00107    if (split_ext(file_out, &name_out, &ext_out)) {
00108       ast_cli(a->fd, "'%s' is an invalid filename!\n", a->argv[3]);
00109       goto fail_out;
00110    }
00111    if (!(fs_out = ast_writefile(name_out, ext_out, NULL, O_CREAT|O_TRUNC|O_WRONLY, 0, AST_FILE_MODE))) {
00112       ast_cli(a->fd, "Unable to open output file: %s\n", a->argv[3]);
00113       goto fail_out;
00114    }
00115 
00116    start = ast_tvnow();
00117    
00118    while ((f = ast_readframe(fs_in))) {
00119       if (ast_writestream(fs_out, f)) {
00120          ast_frfree(f);
00121          ast_cli(a->fd, "Failed to convert %s.%s to %s.%s!\n", name_in, ext_in, name_out, ext_out);
00122          goto fail_out;
00123       }
00124       ast_frfree(f);
00125    }
00126 
00127    cost = ast_tvdiff_ms(ast_tvnow(), start);
00128    ast_cli(a->fd, "Converted %s.%s to %s.%s in %dms\n", name_in, ext_in, name_out, ext_out, cost);
00129    ret = CLI_SUCCESS;
00130 
00131 fail_out:
00132    if (fs_out) {
00133       ast_closestream(fs_out);
00134       if (ret != CLI_SUCCESS)
00135          ast_filedelete(name_out, ext_out);
00136    }
00137 
00138    if (fs_in) 
00139       ast_closestream(fs_in);
00140 
00141    ast_module_unref(ast_module_info->self);
00142 
00143    return ret;
00144 }

static int load_module ( void   )  [static]

Definition at line 156 of file res_convert.c.

References ast_cli_register_multiple(), and AST_MODULE_LOAD_SUCCESS.

00157 {
00158    ast_cli_register_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry));
00159    return AST_MODULE_LOAD_SUCCESS;
00160 }

static int split_ext ( char *  filename,
char **  name,
char **  ext 
) [static]

Split the filename to basename and extension.

Definition at line 39 of file res_convert.c.

References ast_strlen_zero().

Referenced by handle_cli_file_convert().

00040 {
00041    *name = *ext = filename;
00042    
00043    if ((*ext = strrchr(filename, '.'))) {
00044       **ext = '\0';
00045       (*ext)++;
00046    }
00047 
00048    if (ast_strlen_zero(*name) || ast_strlen_zero(*ext))
00049       return -1;
00050 
00051    return 0;
00052 }

static int unload_module ( void   )  [static]

Definition at line 150 of file res_convert.c.

References ast_cli_unregister_multiple().

00151 {
00152    ast_cli_unregister_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry));
00153    return 0;
00154 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "File format conversion CLI command" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } [static]

Definition at line 162 of file res_convert.c.

const struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 162 of file res_convert.c.

struct ast_cli_entry cli_convert[] [static]

Initial value:

 {
   AST_CLI_DEFINE(handle_cli_file_convert, "Convert audio file")
}

Definition at line 146 of file res_convert.c.


Generated on Wed Oct 28 11:46:18 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6